Logic Programming: Alan Smaill
Logic Programming: Alan Smaill
Logic Programming: Alan Smaill
U S
IT
TH
Y
O F
H
G
E
R
D I U
N B
Logic Programming
Alan Smaill
Sep 21 2015
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
Official descriptor:
http: // www. drps. ed. ac. uk/ 15-16/ dpt/
cxinfr09031. htm
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
Today we aim
to get a general grasp of the main ideas behind Logic
Programming;
why you might use it;
and how to get started programming in LP.
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
$ sicstus
?-
?- print( ’hello world’).
hello world
yes
IT
TH
Y
O F
H
G
E
R
D I U
N B
An atom is:
a sequence of alphanumeric characters
usually started with a lower case letter
or a string enclosed in single quotes
Examples:
IT
TH
Y
O F
H
G
E
R
D I U
N B
Examples:
X Y Parent Foo
IT
TH
Y
O F
H
G
E
R
D I U
N B
p(t1,...,tn)
Examples:
father(homer, bart)
mother(marge, bart)
IT
TH
Y
O F
H
G
E
R
D I U
N B
A predicate has
a name – father in father(homer, bart)
an arity – how many arguments: 2 in father(homer, bart)
IT
TH
Y
O F
H
G
E
R
D I U
N B
father(homer, bart).
mother(marge, bart).
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
father(abe,homer).
father(homer, bart).
father(homer, lisa).
father(homer, maggie).
father(ned, rod).
father(ned, todd).
father(chief_wiggum,ralph).
mother(marge, bart).
mother(marge, lisa).
mother(marge, maggie).
...
IT
TH
Y
O F
H
G
E
R
D I U
N B
We can now query, and Prolog will search for possible answers:
?- father(X,bart).
X = homer ;
no
?- father(X,Z), mother(Y,Z).
X = homer, Y = marge, Z = bart ;
X = homer, Y = marge, Z = lisa ;
X = homer, Y = marge, Z = maggie ;
no
IT
TH
Y
O F
H
G
E
R
D I U
N B
This means:
p(ts1) holds if q(ts2) holds and . . . and r(tsN) holds.
Example:
sibling(X,Y) :- parent(Z,X), parent(Z,Y).
IT
TH
Y
O F
H
G
E
R
D I U
N B
Comments:
/* multiple
line comment */
To quit Sicstus, type
?- halt.
. . . or control-D.
IT
TH
Y
O F
H
G
E
R
D I U
N B
?- consult(’simpsons.pl’).
?- consult(simpsons). or
?- [simpsons].
IT
TH
Y
O F
H
G
E
R
D I U
N B
/* hello.pl
* James Cheney
* Sept. 20, 2010
*/
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
Course text:
“Learn Prolog Now!” (Blackburn et. al.): on-line at:
http: // www. learnprolognow. org/
IT
TH
Y
O F
H
G
E
R
D I U
N B
IT
TH
Y
O F
H
G
E
R
D I U
N B
Compound Terms
Equality and Unification
How Prolog searches for answers