CIS505/705 Sample solution 1. For these clauses: (1) expensive(house). (2) expensive(car). (3) likes(john, X) :- expensive(X). (4) likes(X, mary). (5) likes(ed, Z). (6) likes(mary, Y) :- likes(Y, john). There are two answers to this query: ?- likes(A, house). Draw, as best you can, the search trees for the two answers: {} ?- likes(A, house) doesn't match Lines 1-2. Matches Line 3: { A == john, house == X} ?- expensive(X) matches Line 1. SUCCESS {} ?- likes(A, house) doesn't match Line 4. Matches Line 5: { A == ed, house == Z } ?- . Nothing more to prove. SUCCESS Try the example with SWI Prolog: ?- trace(expensive). ?- trace(likes). ?- likes(A, house). 2. Code the following facts into Prolog definitions. You are welcome to use Prolog pattern-matching style: "eggs are fresh" "lettice is fresh" (define fresh(.) ) fresh(eggs). fresh(lettuce). (OR, fresh(X) :- X = eggs ; X = lettuce.) "everything that is fresh is tasty" (define tasty(.) ) ("if X is fresh, then X is tasty") tasty(X) :- fresh(X). "john likes everything that is tasty" (define likes(.,.) ) ("if X is tasty, then john likes X") likes(john, X) :- tasty(X). "everyone who likes eggs likes everything that is fresh" Step 1: untangle into if-then format: "if X likes eggs, then if Y is fresh then X likes Y" Step 2: write as a predicate-calculus assertion: Forall X (likes(X, eggs) -> (Forall Y (fresh(Y) -> likes(X,Y)))) Step 3: use logical laws to reformat as a Horn clause: Forall X Forall Y ( (likes(X, eggs) ^ fresh(Y)) -> likes(X,Y) ) Step 4: code in Prolog (drop leading quantifiers, reverse the -> ): likes(X,Y) :- likes(X, eggs), fresh(Y).