CS 451 - Programming Languages - Fall 2007
Prolog
Loyola College >
Department of Computer Science >
Dr. James Glenn >
CS 451 >
Lecture Notes and Examples >
Prolog
lucy:~/451/F2007/Prolog> prolog
GNU Prolog 1.2.18
By Daniel Diaz
Copyright (C) 1999-2004 Daniel Diaz
| ?- [family].
compiling /home/jglenn/451/F2007/Prolog/family.pl for byte code...
/home/jglenn/451/F2007/Prolog/family.pl compiled, 18 lines read - 2365 bytes written, 6 ms
yes
| ?- spouse(steve,sandi).
yes
| ?- spouse(sandi,steve).
no
| ?- married(sandi,steve).
yes
| ?- married(X,Y).
X = steve
Y = sandi ? ;
X = sandi
Y = steve
yes
| ?- grandparent(X,Y).
X = dorothy
Y = liisa ? a
X = dorothy
Y = john
X = dorothy
Y = traci
X = olive
Y = traci
yes
| ?- son(X,Y).
X = steve
Y = dorothy ? a
no
| ?-
family.pl
father(steve,liisa).
father(steve,john).
father(steve,traci).
mother(judy,liisa).
mother(judy,john).
mother(sandi,traci).
mother(dorothy,steve).
mother(olive,sandi).
spouse(steve,sandi).
married(X,Y) :- spouse(X,Y).
married(X,Y) :- spouse(Y,X).
parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
grandparent(X,Y) :- parent(X,Z),parent(Z,Y).
male(X) :- father(X,_), !.
son(X,Y) :- parent(Y,X),male(X).
This code can also be downloaded from the file
family.pl.