TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
AI Present and Future
Alan Smaill
University of Edinburgh, School of Informatics
22/01/19
Alan Smaill AI Present and Future 22/01/19 1/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Admin
Drop-in Lab sessions 9–11 on Mondays start next week.
Some material to work through for next week will be on-linelater today.
You should work through the material yourself;Learn Prolog Now will help here.
The lab session next week gives you a chance to get feedbackand let you discuss any problems you may have.
Alan Smaill AI Present and Future 22/01/19 2/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Today
Logic Programming: the idea
Overview of the “pure” part of the language.
Inbuilt search strategy, and consequences.
Reasoning with lists, trees
See Learn Prolog Now, chs 1–4http:
//www.learnprolognow.org/lpnpage.php?pageid=online
Alan Smaill AI Present and Future 22/01/19 3/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
The Ideal
Program specifications can be written in logic.
Specifications are independent of computers.
Rules of logic can prove that a specification can be realised,even if computers didn’t exist.
But proof can also be done by a computer smart enough tofind the right proof.
Alan Smaill AI Present and Future 22/01/19 4/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
The Ideal
Program specifications can be written in logic.
Specifications are independent of computers.
Rules of logic can prove that a specification can be realised,even if computers didn’t exist.
But proof can also be done by a computer smart enough tofind the right proof.
So specifications and programs are . . . the same.
Alan Smaill AI Present and Future 22/01/19 4/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
The Ideal
Program specifications can be written in logic.
Specifications are independent of computers.
Rules of logic can prove that a specification can be realised,even if computers didn’t exist.
But proof can also be done by a computer smart enough tofind the right proof.
So specifications and programs are nearly the same.
Alan Smaill AI Present and Future 22/01/19 4/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Grammar for first-order logic
. . . aka predicate calculusDefine terms by
term ::= constant
| var| fn symbol ( term list )
term list ::= term
| term , term list
Alan Smaill AI Present and Future 22/01/19 5/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Formulas (= making a statement)
form ::= pred ( term list )
| ¬ form
| form ∨ form
| form ∧ form
| form → form
| ∀ var form
| ∃ var form
Use precedence to disambiguate (or brackets).
Alan Smaill AI Present and Future 22/01/19 6/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Modelling the world
We suppose that the world can be describe in terms of
set of objects
functions
relations
where
functions map objects to objects
relations with n arguments describe properties of n objects.
Alan Smaill AI Present and Future 22/01/19 7/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Example
Look at a blocks world:
base
a b
c
d
e
Describe using objects with chosen names (constants):
base, a, b, c, d, e.
Say which object is on which other with a 2-place relation, on:on(e, c), on(c, a), on(e, d), on(d, b), on(b, base), on(a, base)
Alan Smaill AI Present and Future 22/01/19 8/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Definite clause subset
Rather than dealing with full predicate logic, take a subset, like thepropositional definite clauses.
Drop ∨,¬ and explicit quantifiers.
statements: pred ( term list )
Definite clauses:
statement(statement ∧ · · · ∧ statement)→ statement
Queries: statement ∧ · · · ∧ statement
Definite clauses are implicitly universally quantified (∀)Queries are implicitly existentially quantified (∃)
Alan Smaill AI Present and Future 22/01/19 9/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Definite clause subset
Rather than dealing with full predicate logic, take a subset, like thepropositional definite clauses.
Drop ∨,¬ and explicit quantifiers.
statements: pred ( term list )
Definite clauses:
statement(statement ∧ · · · ∧ statement)→ statement
Queries: statement ∧ · · · ∧ statement
Definite clauses are implicitly universally quantified (∀)Queries are implicitly existentially quantified (∃)
Alan Smaill AI Present and Future 22/01/19 9/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Getting started
We’ll use SWI Prolog
Available on all DICE machines
Assessed coursework will require up-to-date version of swipl.
Freely available: see
http://www.swi-prolog.org/
On-line documentation:http: // www. sics. se/ isl/ sicstuswww/ site/
Manual:http: // www. swi-prolog. org/ pldoc/ doc_ for?
object= manual
Alan Smaill AI Present and Future 22/01/19 10/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Getting started
We’ll use SWI Prolog
Available on all DICE machines
Assessed coursework will require up-to-date version of swipl.
Freely available: see
http://www.swi-prolog.org/
On-line documentation:http: // www. sics. se/ isl/ sicstuswww/ site/
Manual:http: // www. swi-prolog. org/ pldoc/ doc_ for?
object= manual
Alan Smaill AI Present and Future 22/01/19 10/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Prolog syntax
Variables written with initial capital letterX, X15, ListOfInts, . . .
Constants, function symbols, predicates start with lower caseletterbartSimpsonJnr, a123, b′ . . .
Clause written in form:a :- b, c.
Alan Smaill AI Present and Future 22/01/19 11/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Unification
The equation t = u is a basic goalwith a special meaning
What happens if we ask:
?- X = c.
?- f(X,g(Y,Z)) = f(c,g(X,Y)).
?- f(X,g(Y,f(X))) = f(c,g(X,Y)).
And how does it do that?
This shows how a successful query returns a witness for theexistential variable: a term that makes the query provable whenplugged into the query.
Alan Smaill AI Present and Future 22/01/19 12/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Unification II
?- X = c.
X=c
yes
?- f(X,g(Y,Z)) = f(c,g(X,Y)).
X=c
Y=c
Z=c
yes
?- f(X,g(Y,f(X))) = f(c,g(X,Y)).
no
Alan Smaill AI Present and Future 22/01/19 13/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Unification III
A substitution is a mapping from variables to terms
X1 = t1, . . . ,Xn = tn
Given two terms t and u
with free variables X1, . . . ,Xn,
a unifier is a substitution that makes t and u identical whenapplied to t and u.
Alan Smaill AI Present and Future 22/01/19 14/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Example I
f(X,g(Y,Z)) = f(c,g(X,Y))
X=c
Y=X
Z=Y
f
X g
Y Z
f
c g
X Y
Alan Smaill AI Present and Future 22/01/19 15/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Example I: apply the substitution
f(X,g(Y,Z)) = f(c,g(X,Y))
X=c
Y=c
Z=c
f
c g
c c
f
c g
c c
Alan Smaill AI Present and Future 22/01/19 16/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Example II
f(X,g(Y,f(X))) = f(c,g(X,Y))
X=c
Y=X
f
X g
Y f
X
f
c g
X Y
Alan Smaill AI Present and Future 22/01/19 17/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Example II: apply partial substitution
f(X,g(Y,f(X))) = f(c,g(X,Y))
X=c
Y=c
Y=f(X)
f(X)=c???
f
c g
c f
c
f
c g
c c
Alan Smaill AI Present and Future 22/01/19 18/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Execution model
The query is run by trying to find a solution to the goal using theclauses:
Unification is used to match goals and clauses
There may be zero, one, or many solutions
Execution may backtrack
Alan Smaill AI Present and Future 22/01/19 19/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth-first search
Basic Idea:
To solve atomic goal A:
If B is a fact in the program, and there is a substitution θsuch that θ(A) = θ(B), then return answer θ;
else,if B :-G1, ...,Gn is a clause in the program,and θ unifies A with B,
then solve θ(G1), . . . , θ(Gn)
else give up on this goal:
backtrack to last choice point
Clauses are tried in declaration order
Compound goals are tried in left-right order
Alan Smaill AI Present and Future 22/01/19 20/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth-first search
Basic Idea:
To solve atomic goal A:
If B is a fact in the program, and there is a substitution θsuch that θ(A) = θ(B), then return answer θ;
else,if B :-G1, ...,Gn is a clause in the program,and θ unifies A with B,
then solve θ(G1), . . . , θ(Gn)
else give up on this goal:
backtrack to last choice point
Clauses are tried in declaration order
Compound goals are tried in left-right order
Alan Smaill AI Present and Future 22/01/19 20/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth-first search
Prolog tries clauses in order of appearance in the program.We look at a couple of search trees for query execution.Assume: foo(a). foo(b). foo(c).
then:
?- foo(X). foo(X)
X=a X=b X=c
Alan Smaill AI Present and Future 22/01/19 21/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth-first search
Prolog tries clauses in order of appearance in the program.We look at a couple of search trees for query execution.Assume: foo(a). foo(b). foo(c).
then:
?- foo(X).
X=afoo(X)
X=a X=b X=c
Alan Smaill AI Present and Future 22/01/19 21/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth-first search
Prolog tries clauses in order of appearance in the program.We look at a couple of search trees for query execution.Assume: foo(a). foo(b). foo(c).
then:
?- foo(X).
X=a ;
X=b
foo(X)
X=a X=b X=c
Alan Smaill AI Present and Future 22/01/19 21/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth-first search
Prolog tries clauses in order of appearance in the program.We look at a couple of search trees for query execution.Assume: foo(a). foo(b). foo(c).
then:
?- foo(X).
X=a ;
X=b ;
X=c
foo(X)
X=a X=b X=c
Alan Smaill AI Present and Future 22/01/19 21/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth-first search
Prolog tries clauses in order of appearance in the program.We look at a couple of search trees for query execution.Assume: foo(a). foo(b). foo(c).
then:
?- foo(X).
X=a ;
X=b ;
X=c ;
no
foo(X)
X=a X=b X=c
Alan Smaill AI Present and Future 22/01/19 21/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X). bar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X). bar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X). bar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X). bar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X). bar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X). bar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X). bar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X).
X = cbar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Depth first search ctd
Prolog backtracks to the last choice point if a sub-goal fails.Assume: bar(b). bar(c). baz(c). then:
?- bar(X),baz(X).
X = c ;
no
bar(X),baz(X)
X=b
baz(b)
X=c
baz(c)
Alan Smaill AI Present and Future 22/01/19 22/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Recursion
In recursive use, the same predicate is used in the head (lhs) of therule as in the body (rhs)(in the second clause below):
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z),
ancestor(Z,Y).
This is a fine declarative description of what it is to be an ancestor.
But watch out for the traps!!!
Alan Smaill AI Present and Future 22/01/19 23/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Recursion
In recursive use, the same predicate is used in the head (lhs) of therule as in the body (rhs)(in the second clause below):
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z),
ancestor(Z,Y).
This is a fine declarative description of what it is to be an ancestor.
But watch out for the traps!!!
Alan Smaill AI Present and Future 22/01/19 23/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Recursion: order can matter
Take the program for ancestor/2 with clauses in the oppositeorder:
ancestor(X,Y) :- parent(X,Z),
ancestor(Z,Y).
ancestor(X,Y) :- parent(X,Y).
This may be less efficient – looks for longest path first.
More likely to loop – if the parent/2 relation has cycles.
HEURISTIC: write base cases first (ie non-recursive cases).
Alan Smaill AI Present and Future 22/01/19 24/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Recursion: order can matter
Take the program for ancestor/2 with clauses in the oppositeorder:
ancestor(X,Y) :- parent(X,Z),
ancestor(Z,Y).
ancestor(X,Y) :- parent(X,Y).
This may be less efficient – looks for longest path first.
More likely to loop – if the parent/2 relation has cycles.
HEURISTIC: write base cases first (ie non-recursive cases).
Alan Smaill AI Present and Future 22/01/19 24/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Rule order affects search
parent(a,b).
parent(b,c).ancestor(a,b)
parent(a,Z),ancestor(Z,b)
ancestor(b,b)
Z=b
parent(a,b)
Alan Smaill AI Present and Future 22/01/19 25/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Rule order affects search
parent(a,b).
parent(b,a).ancestor(a,b)
parent(a,Z),ancestor(Z,b)
ancestor(b,b)
parent(b,W),ancestor(W,b)
ancestor(a,b)
...
W=a
Z=b
Alan Smaill AI Present and Future 22/01/19 26/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Recursion again
Goal order can matter!
ancestor3(X,Y) :- parent(X,Y).
ancestor3(X,Y) :- ancestor3(Z,Y),
parent(X,Z)
This returns all solutions, then loops, eg with the following facts:
parent(a,b).
parent(b,c).
Heuristic: keep recursive calls late in the body
Alan Smaill AI Present and Future 22/01/19 27/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Recursion again
Goal order can matter!
ancestor3(X,Y) :- parent(X,Y).
ancestor3(X,Y) :- ancestor3(Z,Y),
parent(X,Z)
This returns all solutions, then loops, eg with the following facts:
parent(a,b).
parent(b,c).
Heuristic: keep recursive calls late in the body
Alan Smaill AI Present and Future 22/01/19 27/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Recursion and terms
Terms can be arbitrarily nested
Example: unary natural numbers
nat(z).
nat(s(X)) :- nat(X).
To do interesting things, we need recursion.
Alan Smaill AI Present and Future 22/01/19 28/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Recursion and terms
Terms can be arbitrarily nested
Example: unary natural numbers
nat(z).
nat(s(X)) :- nat(X).
To do interesting things, we need recursion.
Alan Smaill AI Present and Future 22/01/19 28/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Addition, subtraction
Addition:
add(z,N,N).
add(s(N),M,s(P)) :- add(N,M,P).
Run in reverse to get all M,N that sum to P:
?- add(X,Y,s(s(s(z)))).
X=z,Y=s(s(s(z)));
X=s(Z),Y=s(s(z));
...
Use to define leq/2:
leq(M,N) :- add(M,_,N).
Here “_” is a so-called anonymous variable;use to avoid warning of singleton variable in Prolog programs.Can also use, for example, _X, _Anon.
Alan Smaill AI Present and Future 22/01/19 29/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Addition, subtraction
Addition:
add(z,N,N).
add(s(N),M,s(P)) :- add(N,M,P).
Run in reverse to get all M,N that sum to P:
?- add(X,Y,s(s(s(z)))).
X=z,Y=s(s(s(z)));
X=s(Z),Y=s(s(z));
...
Use to define leq/2:
leq(M,N) :- add(M,_,N).
Here “_” is a so-called anonymous variable;use to avoid warning of singleton variable in Prolog programs.Can also use, for example, _X, _Anon.
Alan Smaill AI Present and Future 22/01/19 29/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Addition, subtraction
Addition:
add(z,N,N).
add(s(N),M,s(P)) :- add(N,M,P).
Run in reverse to get all M,N that sum to P:
?- add(X,Y,s(s(s(z)))).
X=z,Y=s(s(s(z)));
X=s(Z),Y=s(s(z));
...
Use to define leq/2:
leq(M,N) :- add(M,_,N).
Here “_” is a so-called anonymous variable;use to avoid warning of singleton variable in Prolog programs.Can also use, for example, _X, _Anon.
Alan Smaill AI Present and Future 22/01/19 29/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Addition, subtraction
Addition:
add(z,N,N).
add(s(N),M,s(P)) :- add(N,M,P).
Run in reverse to get all M,N that sum to P:
?- add(X,Y,s(s(s(z)))).
X=z,Y=s(s(s(z)));
X=s(Z),Y=s(s(z));
...
Use to define leq/2:
leq(M,N) :- add(M,_,N).
Here “_” is a so-called anonymous variable;use to avoid warning of singleton variable in Prolog programs.Can also use, for example, _X, _Anon.
Alan Smaill AI Present and Future 22/01/19 29/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Addition, subtraction
Addition:
add(z,N,N).
add(s(N),M,s(P)) :- add(N,M,P).
Run in reverse to get all M,N that sum to P:
?- add(X,Y,s(s(s(z)))).
X=z,Y=s(s(s(z)));
X=s(Z),Y=s(s(z));
...
Use to define leq/2:
leq(M,N) :- add(M,_,N).
Here “_” is a so-called anonymous variable;use to avoid warning of singleton variable in Prolog programs.Can also use, for example, _X, _Anon.
Alan Smaill AI Present and Future 22/01/19 29/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Multiplication
Now define multiplication:
multiply(z,N,z). % or: multiply(z,_,z).
multiply(s(N),M,P) :-
multiply(N,M,Q), add(M,Q,P).
square(N,M) :- multiply(N,N,M).
Alan Smaill AI Present and Future 22/01/19 30/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
lists
Lists are built-in (and very useful) data structures.
Syntax:
[1,2,3,4]
[a,[1,2,3],42,’forty-two’] % nested list
[a,b,c|Xs] % first three elements,
% and Rest
?- [1,2,3,4] = [A,B,C|D].
A = 1, B = 2, C = 3, D = [4].
Alan Smaill AI Present and Future 22/01/19 31/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
lists
Lists are built-in (and very useful) data structures.
Syntax:
[1,2,3,4]
[a,[1,2,3],42,’forty-two’] % nested list
[a,b,c|Xs] % first three elements,
% and Rest
?- [1,2,3,4] = [A,B,C|D].
A = 1, B = 2, C = 3, D = [4].
Alan Smaill AI Present and Future 22/01/19 31/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
List Processing
Predicate to recognise lists:
list([]).
list([X|L]) :- list(L).
Examples: list append
append([],L,L).
append([X|L],M,[X|N]) :- append(L,M,N).
Alan Smaill AI Present and Future 22/01/19 32/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
List Processing
Predicate to recognise lists:
list([]).
list([X|L]) :- list(L).
Examples: list append
append([],L,L).
append([X|L],M,[X|N]) :- append(L,M,N).
Alan Smaill AI Present and Future 22/01/19 32/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
append in action
Forward direction:
?- append([1,2],[3,4],X).
X = [1,2,3,4]
Backward direction
?- append(X,Y,[1,2,3,4]).
X=[], Y=[1,2,3,4];
X=[1],Y=[2,3,4];
...
Alan Smaill AI Present and Future 22/01/19 33/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
append in action
Forward direction:
?- append([1,2],[3,4],X).
X = [1,2,3,4]
Backward direction
?- append(X,Y,[1,2,3,4]).
X=[], Y=[1,2,3,4];
X=[1],Y=[2,3,4];
...
Alan Smaill AI Present and Future 22/01/19 33/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Mode annotations
There are recognised ways of indicating properties of Prologprocedures.
Notation: append(+,+,-)
Expect to be called with the first two arguments ground, andthird a variable (which we normally expect to bound after thecall)
Similarly, append(-,-,+)
Call with last argument ground, first two as variables(which we normally expect to be bound after the call).
Not “code”, but often used in annotations
“?” annotation used where any term may appear— i.e. ground, variable, or compound term with variables.
Alan Smaill AI Present and Future 22/01/19 34/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Mode annotations
There are recognised ways of indicating properties of Prologprocedures.
Notation: append(+,+,-)
Expect to be called with the first two arguments ground, andthird a variable (which we normally expect to bound after thecall)
Similarly, append(-,-,+)
Call with last argument ground, first two as variables(which we normally expect to be bound after the call).
Not “code”, but often used in annotations
“?” annotation used where any term may appear— i.e. ground, variable, or compound term with variables.
Alan Smaill AI Present and Future 22/01/19 34/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Mode annotations
There are recognised ways of indicating properties of Prologprocedures.
Notation: append(+,+,-)
Expect to be called with the first two arguments ground, andthird a variable (which we normally expect to bound after thecall)
Similarly, append(-,-,+)
Call with last argument ground, first two as variables(which we normally expect to be bound after the call).
Not “code”, but often used in annotations
“?” annotation used where any term may appear— i.e. ground, variable, or compound term with variables.
Alan Smaill AI Present and Future 22/01/19 34/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
List Processing ctd
When is something a member of a list?
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
Typical modes:member(+,+)
member(-,+)
Alan Smaill AI Present and Future 22/01/19 35/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
List Processing ctd
When is something a member of a list?
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
Typical modes:member(+,+)
member(-,+)
Alan Smaill AI Present and Future 22/01/19 35/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
List processing ctd
Removing an element of a list:
remove(X, [X|L], L).
remove(X, [Y|L], [Y|M]) :- remove(X, L, M).
NB: removes one occurrence of X;fails if X is not a member of the list.
Typical mode:remove(+,+,-)
Alan Smaill AI Present and Future 22/01/19 36/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
List processing ctd
Removing an element of a list:
remove(X, [X|L], L).
remove(X, [Y|L], [Y|M]) :- remove(X, L, M).
NB: removes one occurrence of X;fails if X is not a member of the list.
Typical mode:remove(+,+,-)
Alan Smaill AI Present and Future 22/01/19 36/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
List processing ctd
Zip: pairing of corresponding elements of lists:assumed to be of same length.
zip([],[],[]).
zip([X|L], [Y|M], [(X,Y)|N]) :- zip(L, M, N).
Typical modes:
zip(+,+,-).
zip(-,-,+). % unzip
Alan Smaill AI Present and Future 22/01/19 37/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Records
Can use terms to define data structures:
pb([entry(alan, ’156-675’),...]).
and operations on them:
pb_lookup(pb(B), P, N) :-
member(entry(P,N), B).
pb_insert(pb(B), P, N, pb([entry(P,N) | B])).
pb_remove(pb(B), P, pb(B2)) :-
remove(entry(P,_), B, B2).
Alan Smaill AI Present and Future 22/01/19 38/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Records
Can use terms to define data structures:
pb([entry(alan, ’156-675’),...]).
and operations on them:
pb_lookup(pb(B), P, N) :-
member(entry(P,N), B).
pb_insert(pb(B), P, N, pb([entry(P,N) | B])).
pb_remove(pb(B), P, pb(B2)) :-
remove(entry(P,_), B, B2).
Alan Smaill AI Present and Future 22/01/19 38/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Trees
We can define (binary) trees with data (at the nodes).
tree(leaf).
tree(node( Data, LT, RT )) :- tree(LT), tree(RT).
Data membership in a tree —using “;” for alternatives in the body of a clause.
mem_tree(X, node(X, _, _)).
mem_tree(X, node(_, LT, RT)) :-
mem_tree(X, LT) ;
mem_tree(X, RT).
Alan Smaill AI Present and Future 22/01/19 39/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Trees
We can define (binary) trees with data (at the nodes).
tree(leaf).
tree(node( Data, LT, RT )) :- tree(LT), tree(RT).
Data membership in a tree —using “;” for alternatives in the body of a clause.
mem_tree(X, node(X, _, _)).
mem_tree(X, node(_, LT, RT)) :-
mem_tree(X, LT) ;
mem_tree(X, RT).
Alan Smaill AI Present and Future 22/01/19 39/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Preorder traversal
Pick up the data in a particular order:start at root, traverse recursively left subtree, then right subtree.
preorder(leaf, []).
preorder(node(X, LT, RT), [X|N]) :-
preorder(LT, LO),
preorder(RT, RO),
append( LO, RO, N).
What happens if we run this in reverse?
Alan Smaill AI Present and Future 22/01/19 40/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Preorder traversal
Pick up the data in a particular order:start at root, traverse recursively left subtree, then right subtree.
preorder(leaf, []).
preorder(node(X, LT, RT), [X|N]) :-
preorder(LT, LO),
preorder(RT, RO),
append( LO, RO, N).
What happens if we run this in reverse?
Alan Smaill AI Present and Future 22/01/19 40/41
TH
E
U N I V E RS
IT
Y
OF
ED I N B U
RG
H
Seen today
Logic Programming: the idea
Overview of the “pure” part of the language.
Inbuilt search strategy, and consequences.
Reasoning with lists, trees
Alan Smaill AI Present and Future 22/01/19 41/41