2
votes

This program when queried with patient(X) directly returns false

patient(X):-cancer(X);diabetes(X).

cancer(X):-pain(X).

diabetes(X):-sugar(X).

pain(X):-readPain(X)=='y'.
sugar(X):-readSugar(X)=='y'.


readPain(X):-write('is it painful?'),nl,read(X).
readSugar(X):-write('Do you have sugar problems?'),nl,read(X).
1
readSugar(X) == 'y'?Willem Van Onsem
Prolog predicates are not functions. They do not return values the way you are using them.lurker

1 Answers

1
votes

You write readPain(X)=='y'.. But Prolog interprets this as (==)(readPain(X),'y'). Prolog sees readPain(X) as a term, not as a predicate call.

In order to ask a if a person has pain, you should use:

pain :-
    readPain(y).

Now it will query the user, and if the user writes y., the predicate will succeed. So can ask:

patient :- cancer; diabetes.

cancer :- pain.

diabetes :- sugar.

pain :-
    readPain(y).
sugar:-
    readSugar(y).

readPain(X) :-
    write('is it painful?'),nl,read(X).
readSugar(X) :-
    write('Do you have sugar problems?'),nl,read(X).

The read/1 predicate queries the user for a term, parses that term, and will unify the parameter with that term. So that means that after you have called readPain(X), X will have the value y in case the user has written y.. By performing unification with y, we check if that is indeed the case.