How does Prolog answer questions Include a description of Ma
How does Prolog answer questions? Include a description of:
-Matching / unification
-backtracking
Solution
I\'ll answer this question with an example wherein I\'ll be explaining both the Matching and backtracking processes simultaneously:
likes(a, sports).
likes(a, music).
likes(c, music).
likes(d,animals).
likes(d,X) :- likes(X,sports).
likes(a,X) :- likes(d,X).
?- likes(a,X)
Now the goal initially is : likes(a , X). After that, the next goal to prove is likes(d, X)? Then is it likes(X, sports). Then does X become a?
Solution: Remeber, variables are never \"instantiated\" - they are unified. Since Prolog is an implementation of Horn Calculus it really should be viewed as an instantaneous calculation - like the solutions of a quadratic equation; they exist the instant you write down the problem regardless of when you actually work them out. Prolog works a bit like a time machine, so if I have X = Y, Y = Z, Z = a then it kind of goes back in time to instantiate X with a (and Y too), but it is also ephemeral in nature that as soon as backtracking occurs the unifications can be undone.
As per the above code, Prolog would try to unify the goal against the first fact, likes(a, sports), and it would succeed. X would be unified with sports. Now, if you asked Prolog to continue then it would next unify X with m (the second fact) and if you continued again it would skip the next three facts/rules, and try to prove likes(a,X) :- likes(d,X). This would lead to trying to prove likes(d,X) which succeeds with the fact likes(d,animals) so X, in the original goal, would unify with animals and if you asked it to continue again you would find that it tries to prove likes(d,X) :- likes(X,sports). which leads to X unifying with a, so the original goal would suggest that a likes a.
Kindly provide the feedback,

