Download A Beginners Guide to Visual Prolog

Transcript
This predicate tells us that someone with the name Name is a child when it has a father or a mother.
Assume that we have a database with several facts for the functors “father” and “mother”. Let’s call
this predicate with a certain name, say
child(“John”).
Now this call can obviously fail when no fact is found with the name “John”. When on the other hand
it finds a fact for father(“John”, Father) then it succeeds and it returns the first solution. But it also
returns a backtrackpoint because there is another clause in the database it can try. So this predicate is
nondeterm, it can come back with one of {fail, succeed, backtrackpoint}. Now let’s add a cut to the
first clause:
child(Name) :father(Name, Father), !, write(“Father is: “, Father).
child(Name) :mother(Name, Mother), write(“Mother is: “, Mother).
When the first claus succeeds, the cut will prevent backtracking. So the predicate can come back with
one of {fail, succeed}, that is mode = determ. Finally you can also change the predicate into the mode
procedure. Take a look at this:
child(Name) :father(Name, Father), !, write(“Father is: “, Father).
child(Name) :mother(Name, Mother), !, write(“Mother is: “, Mother).
child(_) :write(“No known parents”).
The last clause plays the role of a catch_all clause. When the other two clauses fail, the last one will
succeed, no matter the Name used in the call. In this definition because of the cuts only one clause
will succeed and because of the catch-all clause, at least one will succeed. So mode = procedure.
<predicateFlow>
The option <predicateFlow> indicates whether an argument is an input argument or output argument.
When you call a predicate, the input arguments must have a value. On return you use the output
arguments to retrieve the values that you need from the predicate. The <predicateFlow> is given by a
sequence of the characters ‘i’ and ‘o’ between round brackets and separated by commas. The
character ‘i’ indicates an inputargument, the character ‘o’ indicates an outputargument. The characters
are written without the quotes. They must be given in the order in which the arguments are given in
the <argumentlist>. When a predicate has five arguments and the first three are input and the last two
are output then the <predicateFlow> looks like
(i, i, i, o, o)
So a predicate declaration can take the form
myPredicate : (string Title, integer NrOfPages) procedure (i,o).
The declaration says that myPredicate has two arguments, one string and one integer, the mode is
procedure (that means it always succeeds, no backtrackpoints), that you have to input the string and
that it will return the number of pages in the second argument.
It is possible to declare more than one <predicateFlow>. The declared <predicateMode> applies to all
flows that follow it. When different flows have diffferent modes, you must declare it. It can lead to a
sequence like:
... ... procedure (i, o) nondeterm (o, o).
152