Programming in Logic Prolog Lists and List Operations
- Slides: 31
Programming in Logic: Prolog Lists and List Operations Readings: Sections 3. 1 & 3. 2 MB: 5 March 2001 CS 360 Lecture 4 1
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 “[ ]”. MB: 5 March 2001 CS 360 Lecture 4 2
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, [ ]) MB: 5 March 2001 CS 360 Lecture 4 3
Lists cont’d 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] n MB: 5 March 2001 CS 360 Lecture 4 4
Lists cont’d 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] n MB: 5 March 2001 CS 360 Lecture 4 5
Lists n [3, x | T] matches : – – – n [3, x], [3, x, y(5)], and [3, x, 56, U, name(mike, barley)] (among others) with T = – – – [ ], [y(5)], and [56, U, name(mike, barley)] MB: 5 March 2001 CS 360 Lecture 4 6
Definition of List n List definition: – – list([ ]). list([I|L 1]) : - list(L 1). MB: 5 March 2001 CS 360 Lecture 4 7
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) MB: 5 March 2001 CS 360 Lecture 4 8
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? MB: 5 March 2001 CS 360 Lecture 4 9
List Membership n It’s the first item in the list. Maybe this can be our base case. – n member(Item, List) : - List = [Item | _ ]. Prolog is pattern-directed, i. e. , does pattern matching, can use this to simplify base case: – member( I, [ I | _ ]). MB: 5 March 2001 CS 360 Lecture 4 10
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. – n member(I, [ _ | T ]) : - member(I, T). 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. MB: 5 March 2001 CS 360 Lecture 4 11
List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] : - member(I, T). Query: member(x, [ ]). Response: no MB: 5 March 2001 Execution Trace: [member(x, [ ])] CS 360 Lecture 4 12
List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] : - member(I, T). Partial Execution Trace: [member(x, [ w, x ])] 2. I=x, T=[x] Query: member(x, [ w, x]). [member(x, [x])] Response: MB: 5 March 2001 continue trace CS 360 Lecture 4 13
List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] : - member(I, T). Partial Execution Trace: [member(X, [ w, x ])] 1. X=I, I=w [] Query: member(X, [ w, x]). continue trace Response: X=w ? ; MB: 5 March 2001 CS 360 Lecture 4 14
List Concatenation n Define relation conc(L 1, L 2, L 3) where L 3 is the result of concatenating L 1 onto front of L 2. n What is the base case? n Go back to defn of list, what is base case? MB: 5 March 2001 CS 360 Lecture 4 15
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([ ], L 2). MB: 5 March 2001 CS 360 Lecture 4 16
List Concatenation 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 | L 1], L 2, [ I | L 3]) n What’s the relation between L 1, L 2, and L 3? n conc(L 1, L 2, L 3) n MB: 5 March 2001 CS 360 Lecture 4 17
List Concatenation n Full definition: – – n Try doing an execution trace for the query: – n conc([ ], L, L). conc([ I | L 1], L 2, [I|L 3]) : - conc(L 1, L 2, L 3). conc(L 1, L 2, [1, 2, 3]). What are the bindings for L 1 and L 2 if keep asking for alternatives? MB: 5 March 2001 CS 360 Lecture 4 18
Multi-Way Uses of Relations 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. n MB: 5 March 2001 CS 360 Lecture 4 19
Some Uses of Concatenation n member(I, L) : - conc(_, [ I | _ ], L). n last( Item, List) : - conc(_ , [Item], List). n sublist(Sub. List, List) : conc(L 1, L 2, List), conc(Sub. List, _, L 2). MB: 5 March 2001 CS 360 Lecture 4 20
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! MB: 5 March 2001 CS 360 Lecture 4 21
List Operations n Adds item to front of list: – n add(Item, List, [Item | List]). Given the following code: add(1, [ ], L 1), add(2, L 1, L 2), add(3, L 2, L 3). What would be the binding for L 3? MB: 5 March 2001 CS 360 Lecture 4 22
List Operations n Deletes item from list: – – n del(Item, [Item| Tail], Tail). del(Item, [Y | Tail], [Y | Tail 1]) : del(Item, Tail 1). 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? MB: 5 March 2001 CS 360 Lecture 4 23
n Insert item into list: – n insert(I, List, New. List) : - del(I, New. List, List). insert/3 also non-deterministic, what would insert(x, [1, 2, 3], L) bind to L if repeatedly ask for alternatives? MB: 5 March 2001 CS 360 Lecture 4 24
Permutation of a List n Let’s define the “permutation” relation: – n Are we clear about what is a permutation? – n perm(List, Permutation). Look at examples. What type of definition will we look for? MB: 5 March 2001 CS 360 Lecture 4 25
Defining Permutation Relation n Where do we look for our cases? n What should be our base case? MB: 5 March 2001 CS 360 Lecture 4 26
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? MB: 5 March 2001 CS 360 Lecture 4 27
Permutation Defined permutation([ ], [ ]). n permutation([X | L 1], Perm) : permutation(L 1, L 2), insert(X, L 2, Perm). n MB: 5 March 2001 CS 360 Lecture 4 28
Homework Quiz n Write definitions for the following relations: – reverse(List, Reverse. List) – sub. Set(Set, Sub. Set) – flatten(List, Flat. List) MB: 5 March 2001 CS 360 Lecture 4 29
Summary 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. n MB: 5 March 2001 CS 360 Lecture 4 30
Summary cont’d 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. n MB: 5 March 2001 CS 360 Lecture 4 31
- Prolog
- Cryptarithmetic problem logic+logic=prolog
- Visual prolog examples
- Download prolog
- Prolog resolution
- Concatinates
- Prolog programming
- Prolog programming
- List manipulation in prolog
- Eliminate consecutive duplicates of list elements prolog
- Prolog empty list
- Prolog reverse list
- First order logic vs propositional logic
- First order logic vs propositional logic
- First order logic vs propositional logic
- Combinational logic vs sequential logic
- Software development plan
- Is it x y or y x
- Combinational logic sequential logic 차이
- Logic chapter 3
- Logic operations involving binary images
- The objective of sharpening spatial filter is to
- Logic programming tutorial
- Algoritma looping
- Ladder logic programming
- Induction reasoning
- Automated logic programming
- Abductive logic programming alp
- Visual logic programming
- Programming logic exercises
- Logic programming
- Operations management linear programming