How to convert if else to fully declarative in Prolog?

This is one of my class questions.

Question

I was able to create my own Prolog program with a bunch of if-else, but I was told that my program is not completely declarative, as its one of the fundamental principles in Prolog.

Here is my code

%start with :- go. is_spicy :- write('Do you want spicy? (yes/no)'), read(Preference), ( Preference == yes -> recommend("Curry"); recommend("Kurma") ). is_fry :- write('Do you want fry food? (yes/no)'), read(Preference), ( Preference == yes -> recommend("StirFry"); recommend("Chicken") ). is_chili :- write('Do you want chili? (yes/no)'), read(Preference), ( Preference == yes -> recommend("Sambal"); recommend("Singgang") ). recommend(Food) :- write('We recommend you '), write(Food). go :- write('Which food type do you prefer? (indian, chinese, malay): '), read(FoodType), ( FoodType == indian -> is_spicy; FoodType == chinese -> is_fry; FoodType == malay -> is_chili; writeln('Please try again') ). 

Does anyone know how to make it "completely declarative"?

+7
declarative prolog logical-purity
source share
2 answers

If you access redundancy in your code by striking out data from logic, you end up with a much more declarative code.

So, encode the data structure and provide a โ€œtranslatorโ€ capable of asking questions and inferring consequences.

for example

 dt(food_type, [indian -> dt(spicy, [ y -> curry, n -> curma ]) ,chinese -> dt(fry, [ y -> stirFry, n -> chicken ]) ,malay -> dt(chili, [ y -> sambal, n -> singgang ]) ]). interpreter(dt(About, Choices), Choice) :- % present a menu for choices % recurse on selected path % when it reach a leaf, just unify interpreter(Choice, Choice). 

You may want to specialize the menu only for y / n, but it is up to you

edit

Presentation of the menu and acceptance of the selection require additional logical programming, for example:

 solve(Choice) :- dt(About, Choices), interpreter(dt(About, Choices), Choice). % present a menu for choices % recurse on selected path interpreter(dt(About, Choices), Choice) :- ask_user(About, Choices, ChoiceAbout), interpreter(ChoiceAbout, Choice). % when it reach a leaf, just unify interpreter(Choice, Choice). ask_user(About, Choices, Choice) :- format('your choice about ~w ?~n', [About]), % show user the context of choice forall(member(C->_,Choices), format(' ~w~n', [C])), read(U), memberchk(U->Choice, Choices). % note: if memberchk above fails (user doesn't input a correct choice) % you should provide an alternate ask_user here, otherwise ... % just see what the code does % 

Session Example:

 % /home/carlo/Desktop/pl/choices compiled into choices 0.00 sec, 0 clauses ?- solve(C). your choice about food_type ? indian chinese malay |: chinese. your choice about fry ? y n |: n. C = chicken . 
+5
source share

CapelliC gave an answer that was more subtle, which I like because I also found out some of his answers.

Here is a simpler version that should help connect the dots.

The trick was to think of the desired end result as a set of separate rules, for example. food(indian,spicy) , and then how to get to them. As soon as I changed the answers from yes / no to the values โ€‹โ€‹of the variables, the rest was downhill. Obviously, yes / no can be used, but this requires additional coding.

 food(indian) :- write('Do you want spicy? (spicy/not_spicy)'), read(Preference), food(indian,Preference). food(chinese) :- write('Do you want fry food? (fry/not_fry)'), read(Preference), food(chinese,Preference). food(malay) :- write('Do you want chili? (chili/not_chili)'), read(Preference), food(malay,Preference). food(indian,spicy) :- recommend("Curry"). food(indian,not_spicy) :- recommend("Kurma"). food(chinese,fry) :- recommend("StirFry"). food(chinese,not_fry) :- recommend("Chicken"). food(malay,chili) :- recommend("Sambal"). food(malay,not_chili) :- recommend("Singgang"). recommend(Food) :- write('We recommend you '), write(Food). go :- write('Which food type do you prefer? (indian, chinese, malay): '), read(FoodType), food(FoodType). 
+3
source share

All Articles