Presentation is loading. Please wait.

Presentation is loading. Please wait.

MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.

Similar presentations


Presentation on theme: "MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2."— Presentation transcript:

1 MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2

2 MB: 5 March 2001CS360 Lecture 42 Prolog Lists n The elements of a list can be anything including other lists. n Remember that atoms could be made from a sequence of special characters, the empty list is the special atom “[ ]”.

3 MB: 5 March 2001CS360 Lecture 43 Lists n A non-empty list always contains two things, a head and the tail. It is a structured data object. The functor name is “.” and its arity is 2. n The list consisting of the item “3” is:.(3,[ ])

4 MB: 5 March 2001CS360 Lecture 44 Lists cont’d n The list consisting of the two items “3” and “x” is:.(3,.(x,[ ])) n Lists are one of the most pervasive data structures in Prolog, so there is a special notation for them: [3, x]

5 MB: 5 March 2001CS360 Lecture 45 Lists cont’d n Often want to describe beginning of list without specifying the rest of it. For example,.(3,.(x, T)) describes a list whose first two items are 3 and x, and whose remaining items could be anything (including empty). n Prolog provides a special notation for doing this, “|”, e.g., [3,x |T]

6 MB: 5 March 2001CS360 Lecture 46 Lists n [3, x | T] matches : – [3,x], – [3,x,y(5)], – and [3,x,56, U, name(mike, barley)] (among others) n with T = – [ ], – [y(5)], – and [56,U,name(mike, barley)]

7 MB: 5 March 2001CS360 Lecture 47 Definition of List n List definition: – list([ ]). – list([I|L1]) :- list(L1).

8 MB: 5 March 2001CS360 Lecture 48 List Operations n Since lists are an inductively defined data structure, expect operations to be inductively defined. n One common list operation is checking whether something is a member of the list. – member(Item, List)

9 MB: 5 March 2001CS360 Lecture 49 List Membership n If defining list membership inductively, need to figure out base case for list variable. n Base case for defn of list is [ ], but not appropriate for base case of membership. Why? n Need to look elsewhere. What’s the simplest case for deciding membership?

10 MB: 5 March 2001CS360 Lecture 410 List Membership n It’s the first item in the list. Maybe this can be our base case. – member(Item, List) :- List = [Item | _ ]. n Prolog is pattern-directed, i.e., does pattern matching, can use this to simplify base case: – member( I, [ I | _ ]).

11 MB: 5 March 2001CS360 Lecture 411 List Membership n What if item is not the first one in list, then what? Then need to check if it’s in the tail. – member(I, [ _ | T ]) :- member(I, T). n Don’t we have to check for empty list case? – No, because if we hit the empty list, then I is not in the list, so we should fail.

12 MB: 5 March 2001CS360 Lecture 412 List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] :- member(I, T). Query: member(x, [ ]). Response: no Execution Trace: [member(x,[ ])] 

13 MB: 5 March 2001CS360 Lecture 413 List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] :- member(I, T). Query: member(x, [ w, x]). Response: Partial Execution Trace: [member(x,[ w, x ])] 2. I=x, T=[x] [member(x, [x])] continue trace

14 MB: 5 March 2001CS360 Lecture 414 List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] :- member(I, T). Query: member(X, [ w, x]). Response: X=w ? ; Partial Execution Trace: [member(X,[ w, x ])] 1. X=I, I=w [ ] continue trace

15 MB: 5 March 2001CS360 Lecture 415 List Concatenation n Define relation conc(L1, L2, L3) where L3 is the result of concatenating L1 onto front of L2. n What is the base case? n Go back to defn of list, what is base case?

16 MB: 5 March 2001CS360 Lecture 416 List Concatenation n List defn base case is [ ], should this be our base case for defining concatenation? n conc([ ], ?, ?) - what is the result of concatenating [ ] onto anything? Are there special cases? n conc([ ], L2, L2).

17 MB: 5 March 2001CS360 Lecture 417 List Concatenation n What should the inductive step be? n What was the inductive step for list defn? n What should the head look like? n conc([ I | L1], L2, [ I | L3]) n What’s the relation between L1, L2, and L3? n conc(L1, L2, L3)

18 MB: 5 March 2001CS360 Lecture 418 List Concatenation n Full definition: – conc([ ], L, L). – conc([ I | L1], L2, [I|L3]) :- conc(L1, L2, L3). n Try doing an execution trace for the query: – conc(L1, L2, [1, 2, 3]). n What are the bindings for L1 and L2 if keep asking for alternatives?

19 MB: 5 March 2001CS360 Lecture 419 Multi-Way Uses of Relations n We have seen that one nice feature of logic programming is its absence of control. n This means we can define one central relation and use it in a number of different ways. What it means depends upon which arguments are variables, partially variablized and/or constants. n conc/3 is an example of such a central relation.

20 MB: 5 March 2001CS360 Lecture 420 Some Uses of Concatenation n member(I, L) :- conc(_, [ I | _ ], L). n last( Item, List) :- conc(_, [Item], List). n sublist(SubList, List) :- conc(L1, L2, List), conc(SubList, _, L2).

21 MB: 5 March 2001CS360 Lecture 421 Clarity n We don’t really need to write defns for member/2 and last/2, could just use conc/3. n What have we gained by writing those definitions? n We write their definitions because we want it to be obvious what we’re trying to do!

22 MB: 5 March 2001CS360 Lecture 422 List Operations n Adds item to front of list: – add(Item, List, [Item | List]). n Given the following code: add(1,[ ],L1), add(2, L1,L2), add(3,L2,L3). What would be the binding for L3?

23 MB: 5 March 2001CS360 Lecture 423 List Operations n Deletes item from list: – del(Item, [Item| Tail], Tail). – del(Item, [Y | Tail], [Y | Tail1]) :- del(Item, Tail, Tail1). n del/2 is non-deterministic, what would del(1,[1,2,1,3,1],L). What would be the bindings for L if we repeatedly asked for new alternatives?

24 MB: 5 March 2001CS360 Lecture 424 n Insert item into list: – insert(I,List,NewList) :- del(I, NewList, List). n insert/3 also non-deterministic, what would insert(x, [1,2,3], L) bind to L if repeatedly ask for alternatives?

25 MB: 5 March 2001CS360 Lecture 425 Permutation of a List n Let’s define the “permutation” relation: – perm(List, Permutation). n Are we clear about what is a permutation? – Look at examples. n What type of definition will we look for?

26 MB: 5 March 2001CS360 Lecture 426 Defining Permutation Relation n Where do we look for our cases? n What should be our base case?

27 MB: 5 March 2001CS360 Lecture 427 Defining Permutation Relation n What should be our inductive case? n What should the head look like? n What’s the relationship between the different parts?

28 MB: 5 March 2001CS360 Lecture 428 Permutation Defined n permutation([ ],[ ]). n permutation([X | L1], Perm) :- permutation(L1, L2), insert(X, L2, Perm).

29 MB: 5 March 2001CS360 Lecture 429 Homework Quiz n Write definitions for the following relations: – reverse(List, ReverseList) – subSet(Set, SubSet) – flatten(List, FlatList)

30 MB: 5 March 2001CS360 Lecture 430 Summary n If data structure defined inductively then usually operations are defined inductively. n However, sometimes the data structure base case does not make sense for the operation, then need to find new base case. n First part of coming up with inductive case is finding what the head should be, often part of head is data structure inductive case.

31 MB: 5 March 2001CS360 Lecture 431 Summary cont’d n Defining relations in pure Prolog allows definitions to be used in many ways. n However, when some uses have common name (e.g., “last”) then should define new relation from old using the common name.


Download ppt "MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2."

Similar presentations


Ads by Google