Alex
Alex

Reputation: 2232

Prolog - Unification of complex terms

I am currently studying for an AI exam and my final thing to revise is Prolog.

Below you can see the question I have to solve:

If possible, unify uncle(brother(W), Q) with uncle(john, mother(S))

I am completely lost as on a high level, it doesn't unify, but I guess my knowledge of Prolog is a bit outdated.

Could someone help me understand how to unify them if possible? I don't understand how I would unify functors to atoms.

Thank you!

Upvotes: 1

Views: 171

Answers (1)

Guy Coder
Guy Coder

Reputation: 24976

I find that when learning syntactic unification that it works better if you first decompose the terms into abstract syntax trees then do the unification. Doing the decomposition really helps with nested list. So after decomposition you have brother(W) trying to unify with john which will fail. Once you have a failure you can skip the rest of the unification process.

While these are not abstract syntax trees, the predicates =.. and write_term help in converting complex terms to ASTs.


Using =../2

?- uncle(brother(W),Q) =.. List.
List = [uncle, brother(W), Q].

?- uncle(john,mother(S)) =.. List.
List = [uncle, john, mother(S)].

and then trying to unify the individual terms

?- brother(W) = john.
false.

For Prolog list use write_term/2 with option dotlists(true)

?- write_term([a],[dotlists(true)]).
.(a,[])
true.

?- write_term([a,b],[dotlists(true)]).
.(a,.(b,[]))
true.

?- write_term([a,B],[dotlists(true)]).
.(a,.(_15276,[]))
true.

?- write_term([a,[b]],[dotlists(true)]).
.(a,.(.(b,[]),[]))
true.

?- write_term([a,b,[c],[d,[e,[f],g]],h],[dotlists(true)]).
.(a,.(b,.(.(c,[]),.(.(d,.(.(e,.(.(f,[]),.(g,[]))),[])),.(h,[])))))
true.

In this post are some actual ASTs for learning syntactic unification.

Do to limitation of Discourse I could not get the images to line up with the actual Prolog terms as I wanted but it is better than not having them.

Upvotes: 1

Related Questions