Matching: The Built-In Predicates and
Matching: The Built-In Predicates and
predicates = and \=
Prolog provides a built-in predicate which takes two arguments and check
whether they match. This predicate is =/2. How does Prolog answer to the
following queries? Then about it first, and then type it into Prolog to check
whether you are right.
?- =(harry,harry).
?- =(harry,'Harry').
?- =(f(a,b),f(a(b))).
Finish stepping through this trace and do traces for the other queries that we
discussed in the lecture. I.e. pose the query happy(X) with respect to
knowledge base kb2.pl and the query wizard(X) with respect to the knowledge
base kb3.pl.
notrace turns the trace mode off. In SWI Prolog, the trace mode is only active for one query after
typing trace.
What query do you have to pose in order to find out which sentences the
grammar can generate? In which order will Prolog generate the sentences?
Make a prediction about the order, then try it out. If it is not clear for you why
Prolog generates the sentence in the order that you are seeing, do a trace.
Solution
Solution
The query that you have to pose is:
?- sentence(A,B,C,D,E).
For each word, Prolog chooses the first one with the required properties that
it can find in its knowledge base. Backtracking then first looks for alternatives
of the last choice that has been made, i.e. the choice to
instantiate E with criminal. The alternative that Prolog finds is 'big
kahuna burger' and the second answer is thus:
A=a, B=criminal, C=eats, D=a, E='big kahuna burger'.
Write a predicate path/2 that tells you from which point in the maze you can
get to which other point when chaining together connections given in the
above knowledge base.
Hint
Now ask some queries. Can you get from point 5 to point 10? Which other
point can you get to when starting in point 1? And which points can be
reached from point 13?
byTrain(metz,frankfurt).
byTrain(saarbruecken,frankfurt).
byTrain(metz,paris).
byTrain(saarbruecken,paris).
byPlane(frankfurt,bangkok).
byPlane(frankfurt,singapore).
byPlane(paris,losAngeles).
byPlane(bangkok,auckland).
byPlane(losAngeles,auckland).
Solution
travel(X,Y) :- onestep(X,Y).
travel(X,Y) :- onestep(X,Z),
travel(Z,Y).
onestep(X,Y) :- byCar(X,Y).
onestep(X,Y) :- byTrain(X,Y).
onestep(X,Y) :- byPlane(X,Y).
So, by using travel/2 to query the above database, you can find out that it is
possible to go from Valmont to Raglan. In case you are planning a travel,
that's already very good information, but what you would then really want to
know is how exactly to get from Valmont to Raglan. Write a
predicate travel/3 which tells you how to travel from one place to another.
The program should, e.g., answer yes to the
query travel(valmont,paris,go(valmont,metz,go(metz,paris))) and X =
go(valmont,metz,go(metz,paris,go(paris,losAngeles))) to the query
travel(valmont,losAngeles,X).
Hint
Hint
Each new step that is taken, has to be remembered. Look at the base case of
the recursion (travel(X,Y) :- onestep(X,Y).)first and extend it so
that travel records in its third argument that a step from X to Y was taken.
Hint
travel(X,Y,go(X,Y)) :- onestep(X,Y).
travel(X,Y,go(X,Z,Path)) :- onestep(X,Z),
travel(Z,Y,Path).
Extend the predicate travel/3 so that it not only tells you via which other
cities you have to go to get from one place to another, but also how (i.e. by
car, train, or plane) you get from one city to the next.
Hint
Hint
For each step that you take, you have to remember which means of
transportation was used. Then extend the structure that you already built in
the previous exercise by including this information.
Solution
travel(X,Y,go(X,Y,Transport)) :- onestep(X,Y,Transport).
travel(X,Y,go(X,Z,Transport,Path)) :- onestep(X,Z,Transport),
travel(Z,Y,Path).
onestep(X,Y,byCar) :- byCar(X,Y).
onestep(X,Y,byTrain) :- byTrain(X,Y).
onestep(X,Y,byPlane) :- byPlane(X,Y).
Crossword puzzle
Here are six English words: