Logic Programming: Recursion, lists, data structures Alan Smaill - - PowerPoint PPT Presentation

logic programming recursion lists data structures
SMART_READER_LITE
LIVE PREVIEW

Logic Programming: Recursion, lists, data structures Alan Smaill - - PowerPoint PPT Presentation

N I V E U R S E I H T T Y O H F G R E U D I B N Logic Programming: Recursion, lists, data structures Alan Smaill Sep 28 2015 Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 1/28 Today N


slide-1
SLIDE 1

T H E U N I V E R S I T Y O F E D I N B U R G H

Logic Programming: Recursion, lists, data structures

Alan Smaill Sep 28 2015

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 1/28

slide-2
SLIDE 2

T H E U N I V E R S I T Y O F E D I N B U R G H

Today

Recursion

proof search practical concerns

List processing Programming with terms as data structures.

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 2/28

slide-3
SLIDE 3

T H E U N I V E R S I T Y O F E D I N B U R G H

Recursion

So far the rules we have seen have been (mostly) non-recursive. This is a limit on what can be expressed. Without recursion, we cannot define transitive closure eg define ancestor/2 in terms of parent/2.

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 3/28

slide-4
SLIDE 4

T H E U N I V E R S I T Y O F E D I N B U R G H

Recursion ctd

In recursive use, the same predicate is used in the head (lhs) of the rule 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).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 4/28

slide-5
SLIDE 5

T H E U N I V E R S I T Y O F E D I N B U R G H

Recursion ctd

In recursive use, the same predicate is used in the head (lhs) of the rule 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 4/28

slide-6
SLIDE 6

T H E U N I V E R S I T Y O F E D I N B U R G H

Reminder: depth-first search

Prolog searches depth-first in program order (“top to bottom”): Regardless of context . . . even if there is an “obvious” solution elsewhere in the search space.

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 5/28

slide-7
SLIDE 7

T H E U N I V E R S I T Y O F E D I N B U R G H

Reminder: depth-first search

Prolog searches depth-first in program order (“top to bottom”): Regardless of context . . . even if there is an “obvious” solution elsewhere in the search space. p :- p. p. ?- p. — the query will loop on the first clause, and fail to terminate.

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 5/28

slide-8
SLIDE 8

T H E U N I V E R S I T Y O F E D I N B U R G H

Recursion: order can matter

Take the program for ancestor/2 with clauses in the opposite

  • rder:

ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). ancestor(X,Y) :- parent(X,Y).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 6/28

slide-9
SLIDE 9

T H E U N I V E R S I T Y O F E D I N B U R G H

Recursion: order can matter

Take the program for ancestor/2 with clauses in the opposite

  • rder:

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 Logic Programming: Recursion, lists, data structures Sep 28 2015 6/28

slide-10
SLIDE 10

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 7/28

slide-11
SLIDE 11

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 8/28

slide-12
SLIDE 12

T H E U N I V E R S I T Y O F E D I N B U R G H

Recursion again

Goal order can matter! ancestor3(X,Y) :- parent(X,Y). ancestor3(X,Y) :- ancestor3(Z,Y), parent(X,Z)

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 9/28

slide-13
SLIDE 13

T H E U N I V E R S I T Y O F E D I N B U R G 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).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 9/28

slide-14
SLIDE 14

T H E U N I V E R S I T Y O F E D I N B U R G H

Goal order affects search

ancestor3(X,b) parent(X,b) X=a ancestor3(Z,b), parent(X,Z) parent(Z,b), parent(X,Z) parent(X,a) Z=a ancestor3(W,b), parent(Z,W), parent(X,Z) . . . . . .

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 10/28

slide-15
SLIDE 15

T H E U N I V E R S I T Y O F E D I N B U R G H

More recursion

Clause order can matter. ancestor4(X,Y) :- ancestor4(Z,Y), parent(X,Z). ancestor4(X,Y) :- parent(X,Y).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 11/28

slide-16
SLIDE 16

T H E U N I V E R S I T Y O F E D I N B U R G H

More recursion

Clause order can matter. ancestor4(X,Y) :- ancestor4(Z,Y), parent(X,Z). ancestor4(X,Y) :- parent(X,Y). This will always loop. Heuristic: put non-recursive goals first.

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 11/28

slide-17
SLIDE 17

T H E U N I V E R S I T Y O F E D I N B U R G H

Goal order matters

ancestor4(X,Y) ancestor4(X,Z),parent(Z,Y) ancestor4(X,W),parent(W,Z),parent(Z,Y) ancestor(X,V),parent(V,W),parent(W,Z),parent(Z,Y) . . .

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 12/28

slide-18
SLIDE 18

T H E U N I V E R S I T Y O F E D I N B U R G H

Recursion and terms

Terms can be arbitrarily nested Example: unary natural numbers nat(z). nat(s(X)) :- nat(X).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 13/28

slide-19
SLIDE 19

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 13/28

slide-20
SLIDE 20

T H E U N I V E R S I T Y O F E D I N B U R G H

Addition, subtraction

Addition: add(z,N,N). add(s(N),M,s(P)) :- add(N,M,P).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28

slide-21
SLIDE 21

T H E U N I V E R S I T Y O F E D I N B U R G 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:

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28

slide-22
SLIDE 22

T H E U N I V E R S I T Y O F E D I N B U R G 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)); ...

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28

slide-23
SLIDE 23

T H E U N I V E R S I T Y O F E D I N B U R G 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).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28

slide-24
SLIDE 24

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28

slide-25
SLIDE 25

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 15/28

slide-26
SLIDE 26

T H E U N I V E R S I T Y O F E D I N B U R G H

List Processing

Recall built-in list syntax: list([]). list([X|L]) :- list(L).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 16/28

slide-27
SLIDE 27

T H E U N I V E R S I T Y O F E D I N B U R G H

List Processing

Recall built-in list syntax: list([]). list([X|L]) :- list(L). Examples: list append append([],L,L). append([X|L],M,[X|N]) :- append(L,M,N).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 16/28

slide-28
SLIDE 28

T H E U N I V E R S I T Y O F E D I N B U R G H

append in action

Forward direction: ?- append([1,2],[3,4],X). X = [1,2,3,4]

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 17/28

slide-29
SLIDE 29

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 17/28

slide-30
SLIDE 30

T H E U N I V E R S I T Y O F E D I N B U R G H

Mode annotations

These are recognised ways of indicating properties of Prolog procedures. Notation: append(+,+,-)

Expect to be called with the first two arguments ground, and third a variable (which we normally expect to bound after the call)

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 18/28

slide-31
SLIDE 31

T H E U N I V E R S I T Y O F E D I N B U R G H

Mode annotations

These are recognised ways of indicating properties of Prolog procedures. Notation: append(+,+,-)

Expect to be called with the first two arguments ground, and third a variable (which we normally expect to bound after the call)

Similarly, append(-,-,+)

Call with last argument ground, first two as variables (which we normally expect to be bound after the call).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 18/28

slide-32
SLIDE 32

T H E U N I V E R S I T Y O F E D I N B U R G H

Mode annotations

These are recognised ways of indicating properties of Prolog procedures. Notation: append(+,+,-)

Expect to be called with the first two arguments ground, and third a variable (which we normally expect to bound after the call)

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 Logic Programming: Recursion, lists, data structures Sep 28 2015 18/28

slide-33
SLIDE 33

T H E U N I V E R S I T Y O F E D I N B U R G H

List Processing ctd

When is something a member of a list? member(X, [X|_]). member(X, [_|T]) :- member(X, T).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 19/28

slide-34
SLIDE 34

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 19/28

slide-35
SLIDE 35

T H E U N I V E R S I T Y O F E D I N B U R G 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).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 20/28

slide-36
SLIDE 36

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 20/28

slide-37
SLIDE 37

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 21/28

slide-38
SLIDE 38

T H E U N I V E R S I T Y O F E D I N B U R G H

List flattening

Write a flatten predicate flatten/2 that

Given a list of (lists of . . . ) Produces a list of individual elements in the original order.

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 22/28

slide-39
SLIDE 39

T H E U N I V E R S I T Y O F E D I N B U R G H

List flattening

Write a flatten predicate flatten/2 that

Given a list of (lists of . . . ) Produces a list of individual elements in the original order. Examples: ?- flatten([[1,2],[3,4]], L). L = [1,2,3,4] ?- flatten([[1,2],[3,[4,5]],6],L). L = [1,2,3,4,5,6] ?- flatten([3,X,[4,5]],L). L = [3,X,4,5]

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 22/28

slide-40
SLIDE 40

T H E U N I V E R S I T Y O F E D I N B U R G H

List flattening

flatten([], []). flatten([H|T], M) :- flatten(H, Hf), flatten(T, Tf), append(Hf, Tf, M). flatten(X, [X]) :- ??? % non-list case; how treat variables?!?!

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 23/28

slide-41
SLIDE 41

T H E U N I V E R S I T Y O F E D I N B U R G H

Records

Can use terms to define data structures: pb([entry(alan, ’156-675’),...]).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 24/28

slide-42
SLIDE 42

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 24/28

slide-43
SLIDE 43

T H E U N I V E R S I T Y O F E D I N B U R G H

Trees

We can define (binary) trees with data (at the nodes). tree(leaf). tree(node( Data, LT, RT )) :- tree(LT), tree(RT).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 25/28

slide-44
SLIDE 44

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 25/28

slide-45
SLIDE 45

T H E U N I V E R S I T Y O F E D I N B U R G 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).

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 26/28

slide-46
SLIDE 46

T H E U N I V E R S I T Y O F E D I N B U R G 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 Logic Programming: Recursion, lists, data structures Sep 28 2015 26/28

slide-47
SLIDE 47

T H E U N I V E R S I T Y O F E D I N B U R G H

Tutorials next week

The tutorial questions are on the web page; you should work through these before the tutorial. It’s recommended to use the sicstus emacs mode to interact with Prolog and edit source code. This mode is invoked automatically when editing Prolog files (with suffix .pl) on DICE. (See sicstus documentation if you want to set this up for yourself.) You can find out about the mode by “C-h m” in emacs when the mode is in use, or via sicstus documentation.

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 27/28

slide-48
SLIDE 48

T H E U N I V E R S I T Y O F E D I N B U R G H

Coming Attractions

Non-logical features:

Expression evaluation I/O “cut” (pruning proof search)

Further reading

Learn Prolog Now, ch 3–4

Tutorial questions on web page.

Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 28/28