0% found this document useful (0 votes)
61 views

Matching: The Built-In Predicates and

The document describes a crossword puzzle with 6 words to be arranged in a grid. It provides a knowledge base with the letters of each word. The task is to write a predicate crosswd/6 that computes all arrangements of the words in the grid, with the first 3 arguments for vertical words left to right and the next 3 for horizontal words top to bottom.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Matching: The Built-In Predicates and

The document describes a crossword puzzle with 6 words to be arranged in a grid. It provides a knowledge base with the letters of each word. The task is to write a predicate crosswd/6 that computes all arrangements of the words in the grid, with the first 3 arguments for vertical words left to right and the next 3 for horizontal words top to bottom.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Matching: the built-in

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))).

Prolog also allows you to use = as an infix operator. So, instead of


writing =(harry,harry) you can write harry = harry.

Matching in Prolog is a destructive operation because variables get


instantiated. So, the terms that are matched may be different after matching
to what they were before matching. Here is a sequence of queries that
illustrates this.
?- X = harry, Y = hermione.
X = harry
Y = hermione ;
no

Matching the variable X with harry and the variable Y (a different variable)


with hermione works and instantiates both variables.
?- X = harry, X = hermione.
no
After the first goal X = harry has been processed, the variable is
instantiated to harry. harry does not match with hermione (different
atoms), so that the second goal fails.

The built-in predicate \=/2 works in the opposite way: Arg1 \= Arg2 is true


if Arg1 = Arg2 fails, and vice versa. (Note that not all Prolog implementations
provide \=/2. SWI Prolog does, but Sicstus, for example, doesn't.)

Try some queries involving = and \=. For example,


?- s(np(pn(dobey)),vp(v(likes),np(pn(harry)))) =
s(np(pn(dobey)),v(likes),np(pn(harry))).
?- s(X,vp(v(sings))) = s(np(pn(dobey)),Y).
?- s(X,vp(v(sings))) = s(np(pn(dobey)),vp(X)).
?- father(X) = X.

Trace: watching how Prolog works


Load yesterday's knowledge base kb2.pl.
?- consult('kb2.pl').
% kb2.pl compiled 0.00 sec, 1,304 bytes
yes

Then type trace. and hit return:


?- trace.
yes
Now, Prolog is in trace mode and will show you step by step how it is
searching for answers. For example, pose the
query happy(aunt_petunia). Prolog will show you the first goal that is
sets out to prove:
Call: (7) happy(aunt_petunia)
Hitting return will show you the next step. In lines starting with Call Prolog
is telling which is the goal that it is at the moment attempting to
prove. Fail: ... means that the specified goal failed,
and Exit: ... means that it succeeded. With Redo: ... Prolog is telling
that it is trying to find an alternative way of proving a goal which it has
already attempted to prove before.

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.

A very simple sentence generator


Here is a tiny lexicon and mini grammar with only one rule which defines a
sentence as consisting of five words: an article, a noun, a verb, and again an
article and a noun.
word(article,a).
word(article,every).
word(noun,criminal).
word(noun,'big kahuna burger').
word(verb,eats).
word(verb,likes).
sentence(Word1,Word2,Word3,Word4,Word5) :-
word(article,Word1),
word(noun,Word2),
word(verb,Word3),
word(article,Word4),
word(noun,Word5).
Download knowledge base here.

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).

The first answer that Prolog gives you is


A=a, B=criminal, C=eats, D=a, E=criminal.

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'.

Then there are no other possibilities of instantiating E and backtracking hence


looks at alternatives for D. And so on...

Finding paths through a maze


Imagine that the following knowledge base describes a maze. The facts
determine which points are connected, i.e., from which point you can get to
which other point in one step. Furthermore, imagine that all paths are one-
way streets, so that you can only walk them in one direction. So, you can get
from point 1 to point 2, but not the other way round.
connected(1,2).
connected(3,4).
connected(5,6).
connected(7,8).
connected(9,10).
connected(12,13).
connected(13,14).
connected(15,16).
connected(17,18).
connected(19,20).
connected(4,1).
connected(6,3).
connected(4,7).
connected(6,11).
connected(14,9).
connected(11,15).
connected(16,12).
connected(14,17).
connected(16,19).

Download knowledge base here.

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?

Traveling by car, train, and plane


We are given the following knowledge base of travel information:
byCar(auckland,hamilton).
byCar(hamilton,raglan).
byCar(valmont,saarbruecken).
byCar(valmont,metz).

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).

Write a predicate travel/2 which determines whether it is possible to travel


from one place to another by 'chaining together' car, train, and plane
journeys. For example, your program should answer yes to the
query travel(valmont,raglan).
Hint

The general structure of the predicate travel/2 is just as for the


predicates path/2 and ancestor_of/2: we can travel from X to Y if we
can get from X to Y in one single step, and we can travel from X to Y if we
can make one step to Z and it is possible to travel from Z to Y. The only
difference with travel is that there are several possibilities of what 'one
step' can look like, because we can do it by car, train, or plane.

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.

Then look at the recursive rule:


travel(X,Y) :- onestep(X,Z),
travel(Z,Y).

The clause should return a structure representing that a step from X to Z was


taken and what is the path that has to be taken to get from Z to Y. This path
is computed by the recursive call to the predicate travel.

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:

abalone, abandon, anagram, connect, elegant, enhance.


They are to be arranged in a crossword puzzle like fashion in the grid given
below.

The following knowledge base represents a lexicon containing these words.


word(abalone,a,b,a,l,o,n,e).
word(abandon,a,b,a,n,d,o,n).
word(enhance,e,n,h,a,n,c,e).
word(anagram,a,n,a,g,r,a,m).
word(connect,c,o,n,n,e,c,t).
word(elegant,e,l,e,g,a,n,t).

Download knowledge base.

Write a predicate crosswd/6 that computes all the different ways of how to fill


the grid. The first three arguments should be the vertical words from left to
right and the following three arguments the horizontal words from top to
bottom.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy