Top Banner
Artificial Intelligence Course (40417) Sharif University of Technology
30

Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Jul 18, 2018

Download

Documents

doankhuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Artificial Intelligence Course (40417)Sharif University of Technology

Page 2: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Representation of lists Some operations on lists

Membership

Concatenation

Adding an item

Deleting an item

Sublist

Permutations

Page 3: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Representation of lists Some operations on lists

Membership

Concatenation

Adding an item

Deleting an item

Sublist

Permutations

Page 4: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

The list is

a simple data structure widely used in non-numeric programming

a sequence of any number of items

[ ann, tennis, tom, skiing]

Page 5: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Empty list: a Prolog atom, []. Non empty list consisting of:

1. the first item, called the head of the list;

2. the remaining part, called the tail.

the head can be any Prolog object, the tail has to be a list.

Page 6: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

? Hobbies1 = [tennis, music] ,Hobbies2 = [ skiing, food],L = [ ann, Hobbies1 , tom, Hobbies2].

Hobbies1 = [ tennis, music]Hobbies2 = [ skiing, food]L = [ ann, [tennis,music], tom, [skiing,food] ]

Page 7: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

It is often practical to treat the whole tail as a single object. For example:

L = [a,b,c]

Tail = [b,c] and

L = [a I Tail]

vertical bar

Page 8: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Prolog accepts lists written as:

[ Item1 , Item2, ... ]

[ Head I Tail]

[Item1, Item2, ... | Others]

Page 9: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Representation of lists Some operations on lists

Membership

Concatenation

Adding an item

Deleting an item

Sublist

Permutations

Page 10: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Lists can be used to represent sets,note that: the order of elements in a set does not matter. same object can occur repeatedly in a list.

the most common operations on lists whether some object is an element of a list,

corresponds to the set membership; concatenation of two lists, correspond to the union of

sets; adding a new object to a list, or deleting some object

from it.

Page 11: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

The goal member( X, L) is true if X occurs in L. where X is an object and L is a list

X is a member of L if either:

1. X is the head of L, or

2. X is a member of the tail of L.

member( X, [X I Tail] ) .member( X, [Head I Tail] ) :-member( X, Tail).

Page 12: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

The goal conc( L1, L2, L3) is true if L1 and L2 are two lists, and L3 is their concatenation

Page 13: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

if the first argument is the empty list then the second and the third arguments must be the same list; conc( [], L, L).

If the first argument of conc is a non-empty list then it has a head and a tail and must look like this: [X I L1]

Page 14: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

conc( [], L, L).conc( [X I L1], L2, [X I L3]) :-conc(L1,L2,L3).

Page 15: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

?- conc( [a,[b,c],d], [a,[],b], L).L = [a, [b,c], d, a, [], b]

we can use conc in the inverse direction for decomposing a given list into two lists:

?-conc( L1, L2, [a,b,c] ).

Page 16: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

conc( Before,[may | After],

[jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec] ).

Before = [jan,feb,mar,apr]

After = [jun,jul,aug,sep,oct,nov,dec]

?- conc( _ , [Month1,may,Month2], [jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec] ).

Month l = apr

Month2 = jun

Page 17: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

?- L1 = [a,b,z,z,c,z,z,z,d,e],

conc( L2, [z,z,z| _], L1).

L1 = [a,b,z,z,c,z,z,z,d,e]

L2 = [a,b,z,z,c]

Page 18: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

member1( X, L) :- conc(L1, [X | L2], L).

Note the difference between member and member1!

member1( X, L):- conc( _, [X | _], L).

Page 19: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Easiest way is to put the new item in front of the list.

If X is the new item and the list to which X is added is L then the resulting list is simply: [X I L]

add( X, L, [X I L] ).

Page 20: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

The goal del( X, L, L1) is true if L1 is equal to the list L with the item X removed.

1. If X is the head of the list then the result after the deletion is the tail of the list.

2. If X is in the tail then it is deleted from there.

Page 21: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

del( X, [X I Tail], Tail).del( X, [Y I Tail], [Y I Tail 1] ) :-del( X, Tail, Tail1).

Page 22: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

?- del( a, L, [1,2,3] ).

L = [a,1,2,3];

L = [1,a,2,3];

L = [1,2,a,3];

L = [1,2,3,a];

no

insert( X, List, BiggerList) :- del( X, BiggerList, List).

Page 23: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Some X is a member of List if X can be deleted from List:

member2( X, List):-del( X, List, _).

Page 24: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

The goal sublist( L1, L2 ) is true if L1 is a sublist of L2.

S is a sublist of L if:

1. L can be decomposed into two lists, L1 and L2, and

2. L2 can be decomposed into two lists, S and some L3.

Page 25: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

sublist( S, L) :-conc( L1, L2, L),conc( S, L3, L2).

Page 26: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

The goal permutation( L, P) is true id P is a permutation of L.

The intention is to generate permutations of a list through backtracking using the permutation procedure.

Page 27: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

1. If the first list is empty then the second list must also be empty.

2. If the first list is not empty then it has the form [X I L], and a permutation of such a list can be constructed in this way: first permute L obtaining L1 and then insert X at any position into Ll.

Page 28: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Permutation( [], [] ).

permutation( [X I L], P):-permutation( L, L1),insert( X, L1, P).

Page 29: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

The difference?!.

permutation2( [], [] ).

permutation2( L, [X I P] ) :-del( X, L, L1),permutation2( L1, P)

Page 30: Artificial Intelligence Course (40417) Sharif University of …ce.sharif.edu/courses/95-96/2/ce417-1/resources/root/prolog/Prolog... · the head can be any Prolog object, ... Note

Any question?!