0% found this document useful (0 votes)
12 views17 pages

LPN03

The lecture introduces recursive definitions in Prolog, showcasing several examples, including predicates for digestion, descent, numerals, and addition. It highlights the differences between the declarative and procedural meanings of Prolog programs and discusses the limitations of Prolog as a logic programming language. The next lecture will focus on lists in Prolog, an important recursive data structure.

Uploaded by

damfont
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views17 pages

LPN03

The lecture introduces recursive definitions in Prolog, showcasing several examples, including predicates for digestion, descent, numerals, and addition. It highlights the differences between the declarative and procedural meanings of Prolog programs and discusses the limitations of Prolog as a logic programming language. The next lecture will focus on lists in Prolog, an important recursive data structure.

Uploaded by

damfont
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

• •

Lecture 3: Recursion

• Theory
– Introduce recursive definitions in Prolog
– Four examples
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– Show that there can be mismatches between the


declarative and procedural meaning of a Prolog
program

• Exercises
– Exercises of LPN chapter 3
– Practical work

Recursive Definitions

• Prolog predicates can be defined


recursively
• A predicate is recursively defined if one
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

or more rules in its definition refers to


itself

• • 1
• •

Example 1: Eating

isDigesting(X,Y):- justAte(X,Y).
isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

justAte(mosquito,blood(john)).
justAte(frog,mosquito).
justAte(stork,frog).

?-

Picture of the situation

justAte
X Y
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

isDigesting

• • 2
• •

Picture of the situation

justAte
X Y
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

isDigesting

justAte isDigesting
X Z Y
isDigesting

Example 1: Eating

isDigesting(X,Y):- justAte(X,Y).
isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

justAte(mosquito,blood(john)).
justAte(frog,mosquito).
justAte(stork,frog).

?- isDigesting(stork,mosquito).

• • 3

© Patrick Blackburn, Johan Bos & Kristina Striegnitz © Patrick Blackburn, Johan Bos & Kristina Striegnitz

?-

?- p.
p:- p.
p:- p.

Another recursive definition


Another recursive definition


4
• •

Another recursive definition

p:- p.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- p.
ERROR: out of memory

Example 2: Decendant

child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z), child(Z,Y).

• • 5
• •

Example 2: Decendant

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

child(donna,emily).

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z), child(Z,Y).

Example 2: Decendant

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

child(donna,emily).

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z), child(Z,Y).

?- descend(anna,donna).
no
?-

• • 6
• •

Example 2: Decendant

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
child(donna,emily).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z), child(Z,Y).
descend(X,Y):- child(X,Z), child(Z,U), child(U,Y).

?-

Example 2: Decendant

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

child(donna,emily).

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z), descend(Z,Y).

?-

• • 7
• •

Example 2: Decendant

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

child(donna,emily).

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z), descend(Z,Y).

?- descend(anna,donna).

Search tree

• Draw search tree for

?- descend(anna,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• • 8
• •

Example 3: Successor

• Suppose we use the following way to


write numerals:
1. 0 is a numeral.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

2. If X is a numeral, then so is succ(X).

Example 3: Successor

numeral(0).
numeral(succ(X)):- numeral(X).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• • 9
• •

Example 3: Successor

numeral(0).
numeral(succ(X)):- numeral(X).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- numeral(succ(succ(succ(0)))).
yes
?-

Example 3: Successor

numeral(0).
numeral(succ(X)):- numeral(X).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- numeral(X).

• • 10
• •

Example 3: Successor

numeral(0).
numeral(succ(X)):- numeral(X).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- numeral(X).
X=0;
X=succ(0);
X=succ(succ(0));
X=succ(succ(succ(0)));
X=succ(succ(succ(succ(0))))

Example 4: Addition
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- add(succ(succ(0)),succ(succ(succ(0))), Result).
Result=succ(succ(succ(succ(succ(0)))))
yes

• • 11
• •

Example 4: Addition

add(0,X,X). %%% base clause


© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- add(succ(succ(0)),succ(succ(succ(0))), Result).
Result=succ(succ(succ(succ(succ(0)))))
yes

Example 4: Addition

add(0,X,X). %%% base clause

add(succ(X),Y,succ(Z)):- %%% recursive clause


© Patrick Blackburn, Johan Bos & Kristina Striegnitz

add(X,Y,Z).

?- add(succ(succ(0)),succ(succ(succ(0))), Result).
Result=succ(succ(succ(succ(succ(0)))))
yes

• • 12

© Patrick Blackburn, Johan Bos & Kristina Striegnitz © Patrick Blackburn, Johan Bos & Kristina Striegnitz

• Draw search tree

Exercises
Search tree


13
• •

Prolog and Logic

• Prolog was the first reasonable attempt


to create a logic programming language
– Programmer gives a declarative
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

specification of the problem, using the


language of logic
– The programmer should not have to tell
the computer what to do
– To get information, the programmer simply
asks a query

Prolog and Logic

• Prolog does some important steps in


this direction, but nevertheless,
Prolog is not a full logic programming
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

language!
• Prolog has a specific way of answering
queries:
– Search knowledge base from top to
bottom
– Processes clauses from left to right
– Backtracking to recover from bad choices

• • 14
• •

descend1.pl

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

child(donna,emily).

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z), descend(Z,Y).

?- descend(A,B).
A=anna
B=bridget

descend2.pl

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

child(donna,emily).

descend(X,Y):- child(X,Z), descend(Z,Y).


descend(X,Y):- child(X,Y).

?- descend(A,B).
A=anna
B=emily

• • 15
• •

descend3.pl

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

child(donna,emily).

descend(X,Y):- descend(Z,Y), child(X,Z).


descend(X,Y):- child(X,Y).

?- descend(A,B).
ERROR: OUT OF LOCAL STACK

descend4.pl

child(anna,bridget).
child(bridget,caroline).
child(caroline,donna).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

child(donna,emily).

descend(X,Y):- child(X,Y).
descend(X,Y):- descend(Z,Y), child(X,Z).

?- descend(A,B).

• • 16
• •

Summary of this lecture

• In this lecture we introduced recursive


predicates
• We also looked at the differences
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

between the declarative and the


procedural meaning of Prolog programs
• We have identified some of the
shortcomings of Prolog seen as a
logical programming language

Next lecture

• Introduce lists in Prolog


– Important recursive data structure in
Prolog programming
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– Define the member/2 predicate, a


fundamental Prolog tool for working with
lists
– Discuss the idea of recursing down lists

• • 17

You might also like