64
Logic Programming Lists. Recursion

Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Logic ProgrammingLists. Recursion

Page 2: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

I Lists

I Recursion

Page 3: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Introducing lists

I Lists are a common data structure in symbolic computation.

I Lists contain elements that are ordered.

I Elements of lists are terms (any type, including other lists).

I Lists are the only data type in LISP

I They are a data structure in Prolog.

I Lists can represent practically any structure.

Page 4: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Introducing lists

I Lists are a common data structure in symbolic computation.

I Lists contain elements that are ordered.

I Elements of lists are terms (any type, including other lists).

I Lists are the only data type in LISP

I They are a data structure in Prolog.

I Lists can represent practically any structure.

Page 5: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Introducing lists

I Lists are a common data structure in symbolic computation.

I Lists contain elements that are ordered.

I Elements of lists are terms (any type, including other lists).

I Lists are the only data type in LISP

I They are a data structure in Prolog.

I Lists can represent practically any structure.

Page 6: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Introducing lists

I Lists are a common data structure in symbolic computation.

I Lists contain elements that are ordered.

I Elements of lists are terms (any type, including other lists).

I Lists are the only data type in LISP

I They are a data structure in Prolog.

I Lists can represent practically any structure.

Page 7: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Introducing lists

I Lists are a common data structure in symbolic computation.

I Lists contain elements that are ordered.

I Elements of lists are terms (any type, including other lists).

I Lists are the only data type in LISP

I They are a data structure in Prolog.

I Lists can represent practically any structure.

Page 8: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Introducing lists

I Lists are a common data structure in symbolic computation.

I Lists contain elements that are ordered.

I Elements of lists are terms (any type, including other lists).

I Lists are the only data type in LISP

I They are a data structure in Prolog.

I Lists can represent practically any structure.

Page 9: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Lists (inductive domain)

I “Base case”: [ ] – the empty list.

I “General case” : .(h, t) – the nonempty list, where:

I h - the head, can be any term,I t - the tail, must be a list.

Page 10: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Lists (inductive domain)

I “Base case”: [ ] – the empty list.I “General case” : .(h, t) – the nonempty list, where:

I h - the head, can be any term,I t - the tail, must be a list.

Page 11: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Lists (inductive domain)

I “Base case”: [ ] – the empty list.I “General case” : .(h, t) – the nonempty list, where:

I h - the head, can be any term,

I t - the tail, must be a list.

Page 12: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Lists (inductive domain)

I “Base case”: [ ] – the empty list.I “General case” : .(h, t) – the nonempty list, where:

I h - the head, can be any term,I t - the tail, must be a list.

Page 13: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List representations

I .(a, [ ]) is represented as

a [ ]

“tree

representation”

or� [ ]

a

“vine

representation”

Page 14: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List representations (cont’d)

I .(a, .(b, [ ])) is

� � [ ]

a b

I .(a, b) is not a list, but it is a legal Prolog structure,represented as

� b

a

Page 15: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List representations (cont’d)

I .(a, .(b, [ ])) is

� � [ ]

a b

I .(a, b) is not a list, but it is a legal Prolog structure,represented as

� b

a

Page 16: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List representations (cont’d)

I .(.( a, []), .(a, .(X, [ ]))) is represented as

� � � [ ]

� [ ] a X

a

Page 17: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Syntactic sugar for lists

I To simplify the notation, “,” can be used to separate theelements.

I The lists introduced above are now:

[a],[a, b],[[ a ], a, X].

Page 18: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Syntactic sugar for lists

I To simplify the notation, “,” can be used to separate theelements.

I The lists introduced above are now:

[a],[a, b],[[ a ], a, X].

Page 19: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Syntactic sugar for lists

I To simplify the notation, “,” can be used to separate theelements.

I The lists introduced above are now:

[a],

[a, b],[[ a ], a, X].

Page 20: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Syntactic sugar for lists

I To simplify the notation, “,” can be used to separate theelements.

I The lists introduced above are now:

[a],[a, b],

[[ a ], a, X].

Page 21: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Syntactic sugar for lists

I To simplify the notation, “,” can be used to separate theelements.

I The lists introduced above are now:

[a],[a, b],[[ a ], a, X].

Page 22: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List manipulation

I Lists are naturally split between the head and the tail.

I Prolog offers a construct to take advantage of this: [H | T].

I Consider the following example:

p ( [ 1 , 2 , 3 ] ) .p ( [ the , cat , sat , [ on , the , mat ] ] ) .

I Prolog will give:

?−p ( [ H | T ] ) .H = 1 ,T = [ 2 , 3 ] ;H = theT = [ cat , sat , [ on , the , mat ] ] ;no

I Attention! [a | b] is not a list, but it is a valid Prologexpression, corresponding to .(a, b)

Page 23: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List manipulation

I Lists are naturally split between the head and the tail.

I Prolog offers a construct to take advantage of this: [H | T].

I Consider the following example:

p ( [ 1 , 2 , 3 ] ) .p ( [ the , cat , sat , [ on , the , mat ] ] ) .

I Prolog will give:

?−p ( [ H | T ] ) .H = 1 ,T = [ 2 , 3 ] ;H = theT = [ cat , sat , [ on , the , mat ] ] ;no

I Attention! [a | b] is not a list, but it is a valid Prologexpression, corresponding to .(a, b)

Page 24: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List manipulation

I Lists are naturally split between the head and the tail.

I Prolog offers a construct to take advantage of this: [H | T].

I Consider the following example:

p ( [ 1 , 2 , 3 ] ) .p ( [ the , cat , sat , [ on , the , mat ] ] ) .

I Prolog will give:

?−p ( [ H | T ] ) .H = 1 ,T = [ 2 , 3 ] ;H = theT = [ cat , sat , [ on , the , mat ] ] ;no

I Attention! [a | b] is not a list, but it is a valid Prologexpression, corresponding to .(a, b)

Page 25: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List manipulation

I Lists are naturally split between the head and the tail.

I Prolog offers a construct to take advantage of this: [H | T].

I Consider the following example:

p ( [ 1 , 2 , 3 ] ) .p ( [ the , cat , sat , [ on , the , mat ] ] ) .

I Prolog will give:

?−p ( [ H | T ] ) .H = 1 ,T = [ 2 , 3 ] ;H = theT = [ cat , sat , [ on , the , mat ] ] ;no

I Attention! [a | b] is not a list, but it is a valid Prologexpression, corresponding to .(a, b)

Page 26: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

List manipulation

I Lists are naturally split between the head and the tail.

I Prolog offers a construct to take advantage of this: [H | T].

I Consider the following example:

p ( [ 1 , 2 , 3 ] ) .p ( [ the , cat , sat , [ on , the , mat ] ] ) .

I Prolog will give:

?−p ( [ H | T ] ) .H = 1 ,T = [ 2 , 3 ] ;H = theT = [ cat , sat , [ on , the , mat ] ] ;no

I Attention! [a | b] is not a list, but it is a valid Prologexpression, corresponding to .(a, b)

Page 27: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Unifying lists: examples

[ X , Y, Z ] = [ john , l i k e s , f i s h ]X = j o h nY = l i k e sZ = f i s h

[ c a t ] = [ X | Y ]X = c a tY = [ ]

[ X , Y | Z ] = [ mary , l i k e s , wine ]X = maryY = l i k e sZ = [ wine ]

Page 28: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Unifying lists: examples

[ X , Y, Z ] = [ john , l i k e s , f i s h ]X = j o h nY = l i k e sZ = f i s h

[ c a t ] = [ X | Y ]X = c a tY = [ ]

[ X , Y | Z ] = [ mary , l i k e s , wine ]X = maryY = l i k e sZ = [ wine ]

Page 29: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Unifying lists: examples

[ X , Y, Z ] = [ john , l i k e s , f i s h ]X = j o h nY = l i k e sZ = f i s h

[ c a t ] = [ X | Y ]X = c a tY = [ ]

[ X , Y | Z ] = [ mary , l i k e s , wine ]X = maryY = l i k e sZ = [ wine ]

Page 30: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Unifying lists: examples (cont’d)

[ [ the , Y ] | Z ] = [ [ X, h a r e ] , [ i s , h e r e ] ]X = t heY = h a r eZ = [ [ i s , h e r e ] ]

[ g o l d e n | T] = [ go lden , n o r f o l k ]T = [ n o r f o l k ]

[ v a l e , h o r s e ] = [ horse , X ]f a l s e

[ w h i t e |Q] = [ P | h o r s e ]P = w h i t eQ = h o r s e

Page 31: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Unifying lists: examples (cont’d)

[ [ the , Y ] | Z ] = [ [ X, h a r e ] , [ i s , h e r e ] ]X = t heY = h a r eZ = [ [ i s , h e r e ] ]

[ g o l d e n | T] = [ go lden , n o r f o l k ]T = [ n o r f o l k ]

[ v a l e , h o r s e ] = [ horse , X ]f a l s e

[ w h i t e |Q] = [ P | h o r s e ]P = w h i t eQ = h o r s e

Page 32: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Unifying lists: examples (cont’d)

[ [ the , Y ] | Z ] = [ [ X, h a r e ] , [ i s , h e r e ] ]X = t heY = h a r eZ = [ [ i s , h e r e ] ]

[ g o l d e n | T] = [ go lden , n o r f o l k ]T = [ n o r f o l k ]

[ v a l e , h o r s e ] = [ horse , X ]f a l s e

[ w h i t e |Q] = [ P | h o r s e ]P = w h i t eQ = h o r s e

Page 33: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Unifying lists: examples (cont’d)

[ [ the , Y ] | Z ] = [ [ X, h a r e ] , [ i s , h e r e ] ]X = t heY = h a r eZ = [ [ i s , h e r e ] ]

[ g o l d e n | T] = [ go lden , n o r f o l k ]T = [ n o r f o l k ]

[ v a l e , h o r s e ] = [ horse , X ]f a l s e

[ w h i t e |Q] = [ P | h o r s e ]P = w h i t eQ = h o r s e

Page 34: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Strings

I In Prolog, strings are written inside double quotation marks.

I Example: ”a string ”.

I Internally, a string is a list of the corresponding ASCII codesfor the characters in the string.

I ?− X = ”a s t r i n g ” .X = [ 9 7 , 32 , 115 , 116 , 114 , 105 , 110 , 1 0 3 ] .

Page 35: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Summary

I Items of interest:I the anatomy of a list in Prolog .(h, t)I graphic representations of lists: “tree representation”, “vine

representation”,I syntactic sugar for lists [...] ,I list manipulation: head-tail notation [H|T],I strings as lists,I unifying lists.

Page 36: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:

I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 37: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:

I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 38: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:

I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 39: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:

I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 40: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:

I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 41: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:

I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 42: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:

I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 43: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:I recursion describes computation in inductive domains,

I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 44: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,

I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 45: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.

I As a result, a recursive procedure will have to describe thebehaviour for:

(a) The “simplest” objects, and/or the objects/situations forwhich the computation stops, i.e. the boundary conditions, and

(b) the general case, which describes the recursive call.

Page 46: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:

(a) The “simplest” objects, and/or the objects/situations forwhich the computation stops, i.e. the boundary conditions, and

(b) the general case, which describes the recursive call.

Page 47: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and

(b) the general case, which describes the recursive call.

Page 48: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Induction/RecursionI Inductive domain:

I A domain composed of objects constructed in a “manageableway”, i.e.:

I there are some “simplest”(atomic) objects, that cannot bedecomposed,

I there are “complex” objects that can be decomposed intofinitely many simpler objects,

I and this decomposition process can be performed finitely manytimes before one reaches the “simplest” objects.

I In such domains, one can use induction as an inference rule.

I Recursion is the dual of induction, i.e.:I recursion describes computation in inductive domains,I recursive procedures (functions, predicates) call themselves,I but the recursive call has to be done on a “simpler” object.I As a result, a recursive procedure will have to describe the

behaviour for:(a) The “simplest” objects, and/or the objects/situations for

which the computation stops, i.e. the boundary conditions, and(b) the general case, which describes the recursive call.

Page 49: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Example: lists as an inductive domain

I simplest object: the empty list [ ].

I any other list is made of a head and a tail (the tail should bea list): [H|T].

Page 50: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Example: lists as an inductive domain

I simplest object: the empty list [ ].

I any other list is made of a head and a tail (the tail should bea list): [H|T].

Page 51: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Example: member

I Implement in Prolog the predicate member/2, such thatmember(X, Y) is true when X is a member of the list Y.

% The boundary c o n d i t i o n .member (X, [ X | ] ) .% The r e c u r s i v e c o n d i t i o n .member (X, [ |Y]) :−

member (X, Y ) .

I The boundary condition is, in this case, the condition forwhich the computation stops (not necessarily for the”simplest” list, which is [ ]).

I For [ ] the predicate is false, therefore it will be omitted.

I Note that the recursive call is on a smaller list (secondargument). The elements in the recursive call are gettingsmaller in such a way that eventually the computation willsucceed, or reach the empty list and fail. predicate for theempty list (where it fails).

Page 52: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Example: member

I Implement in Prolog the predicate member/2, such thatmember(X, Y) is true when X is a member of the list Y.

% The boundary c o n d i t i o n .member (X, [ X | ] ) .% The r e c u r s i v e c o n d i t i o n .member (X, [ |Y]) :−

member (X, Y ) .

I The boundary condition is, in this case, the condition forwhich the computation stops (not necessarily for the”simplest” list, which is [ ]).

I For [ ] the predicate is false, therefore it will be omitted.

I Note that the recursive call is on a smaller list (secondargument). The elements in the recursive call are gettingsmaller in such a way that eventually the computation willsucceed, or reach the empty list and fail. predicate for theempty list (where it fails).

Page 53: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Example: member

I Implement in Prolog the predicate member/2, such thatmember(X, Y) is true when X is a member of the list Y.

% The boundary c o n d i t i o n .member (X, [ X | ] ) .% The r e c u r s i v e c o n d i t i o n .member (X, [ |Y]) :−

member (X, Y ) .

I The boundary condition is, in this case, the condition forwhich the computation stops (not necessarily for the”simplest” list, which is [ ]).

I For [ ] the predicate is false, therefore it will be omitted.

I Note that the recursive call is on a smaller list (secondargument). The elements in the recursive call are gettingsmaller in such a way that eventually the computation willsucceed, or reach the empty list and fail. predicate for theempty list (where it fails).

Page 54: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Example: member

I Implement in Prolog the predicate member/2, such thatmember(X, Y) is true when X is a member of the list Y.

% The boundary c o n d i t i o n .member (X, [ X | ] ) .% The r e c u r s i v e c o n d i t i o n .member (X, [ |Y]) :−

member (X, Y ) .

I The boundary condition is, in this case, the condition forwhich the computation stops (not necessarily for the”simplest” list, which is [ ]).

I For [ ] the predicate is false, therefore it will be omitted.

I Note that the recursive call is on a smaller list (secondargument). The elements in the recursive call are gettingsmaller in such a way that eventually the computation willsucceed, or reach the empty list and fail. predicate for theempty list (where it fails).

Page 55: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Example: member

I Implement in Prolog the predicate member/2, such thatmember(X, Y) is true when X is a member of the list Y.

% The boundary c o n d i t i o n .member (X, [ X | ] ) .% The r e c u r s i v e c o n d i t i o n .member (X, [ |Y]) :−

member (X, Y ) .

I The boundary condition is, in this case, the condition forwhich the computation stops (not necessarily for the”simplest” list, which is [ ]).

I For [ ] the predicate is false, therefore it will be omitted.

I Note that the recursive call is on a smaller list (secondargument). The elements in the recursive call are gettingsmaller in such a way that eventually the computation willsucceed, or reach the empty list and fail. predicate for theempty list (where it fails).

Page 56: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

When to use the recursion?

I Avoid circular definitions:

p a r e n t (X, Y):− c h i l d (Y, X ) .c h i l d (X, Y):− p a r e n t (Y, X ) .

I Careful with left recursion:

p e r s o n (X):− p e r s o n (Y) , mother (X, Y ) .p e r s o n ( adam ) .

In this case,

?−p e r s o n (X ) .

will loop (no chance to backtrack). Prolog tries to satisfy therule and this leads to the loop.

Page 57: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

When to use the recursion?

I Avoid circular definitions:

p a r e n t (X, Y):− c h i l d (Y, X ) .c h i l d (X, Y):− p a r e n t (Y, X ) .

I Careful with left recursion:

p e r s o n (X):− p e r s o n (Y) , mother (X, Y ) .p e r s o n ( adam ) .

In this case,

?−p e r s o n (X ) .

will loop (no chance to backtrack). Prolog tries to satisfy therule and this leads to the loop.

Page 58: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

I Order of facts, rules in the database:

i s l i s t ( [ A |B]) :− i s l i s t (B ) .i s l i s t ( [ ] ) .

The following query will loop:

?− i s l i s t (X)

I The order in which the rules and facts are given matters. Ingeneral, place facts before rules.

Page 59: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Exercises

I Define predicates in Prolog for:

1. The length of a list2. The sum of elements of a list3. The reverse of a list4. The list of elements on even positions5. The concatenation of two lists.

Page 60: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Exercises

I Define predicates in Prolog for:

1. The length of a list

2. The sum of elements of a list3. The reverse of a list4. The list of elements on even positions5. The concatenation of two lists.

Page 61: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Exercises

I Define predicates in Prolog for:

1. The length of a list2. The sum of elements of a list

3. The reverse of a list4. The list of elements on even positions5. The concatenation of two lists.

Page 62: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Exercises

I Define predicates in Prolog for:

1. The length of a list2. The sum of elements of a list3. The reverse of a list

4. The list of elements on even positions5. The concatenation of two lists.

Page 63: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Exercises

I Define predicates in Prolog for:

1. The length of a list2. The sum of elements of a list3. The reverse of a list4. The list of elements on even positions

5. The concatenation of two lists.

Page 64: Logic Programming - Lists. Recursionstaff.fmi.uvt.ro/~isabela.dramnesc/LP/Lecture2_LP.pdf · Recursion. I Lists I Recursion. Introducing lists I Listsare a common data structure in

Exercises

I Define predicates in Prolog for:

1. The length of a list2. The sum of elements of a list3. The reverse of a list4. The list of elements on even positions5. The concatenation of two lists.