ai present and future · 2019. 1. 22. · prolog syntax variables written with initial capital...
TRANSCRIPT
![Page 1: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/1.jpg)
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
![Page 2: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/2.jpg)
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
![Page 3: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/3.jpg)
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
![Page 4: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/4.jpg)
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
![Page 5: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/5.jpg)
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
![Page 6: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/6.jpg)
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
![Page 7: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/7.jpg)
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
![Page 8: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/8.jpg)
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
![Page 9: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/9.jpg)
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
![Page 10: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/10.jpg)
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
![Page 11: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/11.jpg)
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
![Page 12: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/12.jpg)
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
![Page 13: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/13.jpg)
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
![Page 14: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/14.jpg)
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
![Page 15: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/15.jpg)
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
![Page 16: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/16.jpg)
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
![Page 17: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/17.jpg)
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
![Page 18: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/18.jpg)
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
![Page 19: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/19.jpg)
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
![Page 20: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/20.jpg)
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
![Page 21: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/21.jpg)
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
![Page 22: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/22.jpg)
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
![Page 23: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/23.jpg)
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
![Page 24: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/24.jpg)
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
![Page 25: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/25.jpg)
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
![Page 26: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/26.jpg)
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
![Page 27: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/27.jpg)
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
![Page 28: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/28.jpg)
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
![Page 29: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/29.jpg)
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
![Page 30: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/30.jpg)
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
![Page 31: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/31.jpg)
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
![Page 32: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/32.jpg)
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
![Page 33: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/33.jpg)
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
![Page 34: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/34.jpg)
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
![Page 35: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/35.jpg)
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
![Page 36: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/36.jpg)
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
![Page 37: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/37.jpg)
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
![Page 38: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/38.jpg)
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
![Page 39: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/39.jpg)
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
![Page 40: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/40.jpg)
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
![Page 41: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/41.jpg)
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
![Page 42: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/42.jpg)
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
![Page 43: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/43.jpg)
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
![Page 44: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/44.jpg)
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
![Page 45: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/45.jpg)
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
![Page 46: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/46.jpg)
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
![Page 47: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/47.jpg)
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
![Page 48: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/48.jpg)
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
![Page 49: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/49.jpg)
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
![Page 50: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/50.jpg)
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
![Page 51: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/51.jpg)
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
![Page 52: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/52.jpg)
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
![Page 53: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/53.jpg)
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
![Page 54: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/54.jpg)
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
![Page 55: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/55.jpg)
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
![Page 56: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/56.jpg)
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
![Page 57: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/57.jpg)
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
![Page 58: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/58.jpg)
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
![Page 59: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/59.jpg)
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
![Page 60: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/60.jpg)
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
![Page 61: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/61.jpg)
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
![Page 62: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/62.jpg)
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
![Page 63: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/63.jpg)
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
![Page 64: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/64.jpg)
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
![Page 65: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/65.jpg)
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
![Page 66: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/66.jpg)
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
![Page 67: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/67.jpg)
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
![Page 68: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/68.jpg)
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
![Page 69: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/69.jpg)
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
![Page 70: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/70.jpg)
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
![Page 71: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/71.jpg)
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
![Page 72: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/72.jpg)
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
![Page 73: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/73.jpg)
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
![Page 74: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/74.jpg)
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
![Page 75: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/75.jpg)
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
![Page 76: AI Present and Future · 2019. 1. 22. · Prolog syntax Variables written with initial capital letter X;X15;ListOfInts;... Constants, function symbols, predicates start with lower](https://reader035.vdocument.in/reader035/viewer/2022071509/6129a74f95c3552bc25baffb/html5/thumbnails/76.jpg)
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