Professional Documents
Culture Documents
Run it and wait until you get the SWI-Prolog window with the prompt "?-".
When Prolog is ready it prints a prompt (?-) and you can type
queries (goals, questions). Try this one:
?- member(X,[1,2,3]).
*** NOTE THAT ALL PROLOG QUERIES MUST END WITH A FULL STOP (.) !!!
X=1
X=1;
X=2;
X=3;
No
4 ?-
(The number 4 before the prompt indicates how many times the prompt
has been printed.)
These are all the solutions of this query, i.e. all values of
the variable X that satisfy the query (all members of the list).
*** NOTE THAT THE VARIABLES MUST BEGIN WITH A CAPITAL LETTER !!!
If you whant to get all solutions in a list you can use this:
4 ?- findall(X,member(X,[1,2,3]),L).
X = _G354
9 ?- member(X,[1,2,3]),member(X,[a,b,3,c,2]).
X=2;
X=3;
No
5 ?-
X = _G504
L = [2, 3]
*** YOU MAY TYPE THE QUERIES IN MULTIPLE LINES AND PUT A DOT (.) AT THE END
OF THE LAST LINE. FOR EXAMPLE:
13 ?- member(X,
| [1,2,
| 3
| ]
| ).
X=1
Prolog prints a bar (|) in the beginning of the line, indicating that
this is a continuation.
14 ?- member (X,[1,2,3]).
ERROR: Syntax error: Operator expected
ERROR: member (X,[1,2,3]
ERROR: ** here **
ERROR: ) .
14 ?-
14 ?- member(X,[1,2,3].
ERROR: Syntax error: Unexpected end of clause
ERROR: member(X,[1,2,3]
ERROR: ** here **
ERROR: .
*** ANY NUMBER OF BLANKS OR NEW LINES CAN BE USED AFTER A COMMA.
*** MAKE SURE THAT ALL PARENTHESES AND SRUARE BRACKETS ARE
PROPERLY PAIRED !!!
14 ?- MEMBER(X,[1,2,3]).
ERROR: Syntax error: Operator expected
ERROR: MEMBER(X,[1,2,3]
ERROR: ** here **
ERROR: ) .
14 ?-
*** TYPE ALL PREDICATES OR GOALS (as member) with lower case letters !!!
=================================================================
II. Examples of problem solving in Prolog. Using files.
=================================================================
arc(1,2).
arc(2,3).
arc(3,4).
arc(3,5).
arc(2,5).
arc(5,6).
arc(2,6).
Now we want to define a procedure to find a path between two nodes X and Y
in this graph. That is, we want to define a predicate path(X,Y,Path).
We just follow our intuition:
path(X,Y,[arc(X,Y)]) :- arc(X,Y).
2. Otherwise, if there is an arc between X and some other node Z, and there is
a path from Z to Y, then there is a path from X to Y too. That is,
path(X,Y,[arc(X,Z)|P]) :- arc(X,Z),path(Z,Y,P).
3. The only thing that is not so intuitive is how we get the third argument
of path. This is a technicality that allows us to create lists and is a
built-in feature of Prolog.
Now, we have to add these two rules (clauses) along with the set of facts
describing the graph to the Prolog database. To do this we need to create
a text file and then copy/paste the above two rules and 7 facts into it.
Assume we use, for example, NOTEPAD and create a file named "path.pl" in
the folder "c:/prolog".
We can load this file into the Prolog database by using the following
query:
?- ['c:/prolog/path.pl'].
Find the path (or all possible paths, if you use ;) from node 1 to node 6.
?- path(1,6,P).
Prolog has a built-in predicate that can be used for adding such numbers.
This is the same prdicate that appends lists.
X+Y=5
X-Y=3
Obviously, both equations must be true, i.e. we need a conjunction between the
two append's. So, with the following query get the answers:
?- append(X,Y,[1,1,1,1,1]),append(Y,[1,1,1],X).
X=[1,1,1,1]
Y=[1]
21 ?- append(X,Y,[1,1,1,1,1]).
X = []
Y = [1, 1, 1, 1, 1] ;
X = [1]
Y = [1, 1, 1, 1] ;
X = [1, 1]
Y = [1, 1, 1] ;
X = [1, 1, 1]
Y = [1, 1] ;
X = [1, 1, 1, 1]
Y = [1] ;
X = [1, 1, 1, 1, 1]
Y = [] ;
No
22 ?-
Happy Prolog-ing...