0% found this document useful (0 votes)
34 views33 pages

Programming in Logic: Prolog: Lists and List Operations Readings: Sections 3.1 & 3.2

This document summarizes a lecture on Prolog lists and common list operations. It discusses how lists are represented in Prolog and how operations like membership, concatenation, addition/deletion are defined recursively in a way that mirrors the recursive definition of lists. It also covers more complex operations like permutations and gives examples of defining new relations in terms of existing ones to make their purpose clearer.

Uploaded by

Amimul Ihsan
Copyright
© Attribution Non-Commercial (BY-NC)
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)
34 views33 pages

Programming in Logic: Prolog: Lists and List Operations Readings: Sections 3.1 & 3.2

This document summarizes a lecture on Prolog lists and common list operations. It discusses how lists are represented in Prolog and how operations like membership, concatenation, addition/deletion are defined recursively in a way that mirrors the recursive definition of lists. It also covers more complex operations like permutations and gives examples of defining new relations in terms of existing ones to make their purpose clearer.

Uploaded by

Amimul Ihsan
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 33

Programming in Logic: Prolog

Lists and List Operations Readings: Sections 3.1 & 3.2


MB: 5 March 2001 CS360 Lecture 4 1

Review
Declarative

MB: 5 March 2001

semantics of pure Prolog program defines when a goal is true and for what instantiation of variables. Commas between goals mean and, semicolons mean or. Procedural semantics are determined by clause and goal ordering. Goal failure causes backtracking and unbinding of affected variables.
CS360 Lecture 4

Programming Patterns
Today We

we start actually looking at programs

will look at a lot of them!!

You

should look for some underlying patterns of how to program in Prolog

MB: 5 March 2001

CS360 Lecture 4

Prolog Lists
The

elements of a list can be anything including other lists. that atoms could be made from a sequence of special characters, the empty list is the special atom [ ].

Remember

MB: 5 March 2001

CS360 Lecture 4

Lists
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. list consisting of the item 3 is: .(3,[ ])

The

MB: 5 March 2001

CS360 Lecture 4

Lists contd
The

list consisting of the two items 3 and x is: .(3, .(x,[ ])) Lists are one of the most pervasive data structures in Prolog, so there is a special notation for them: [3, x]

MB: 5 March 2001

CS360 Lecture 4

Lists contd
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). Prolog provides a special notation for doing this, |, e.g., [3,x |T]

MB: 5 March 2001

CS360 Lecture 4

Lists
[3,

x | T] matches :

[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)]


CS360 Lecture 4 8

MB: 5 March 2001

Definition of List

List

definition:

list([ ]). list([I|L1]) :- list(L1).

MB: 5 March 2001

CS360 Lecture 4

List Operations
Since

lists are an inductively defined data structure, expect operations to be inductively defined. common list operation is checking whether something is a member of the list.

One

member(Item, List)

MB: 5 March 2001

CS360 Lecture 4

10

List Membership

If defining list membership inductively, need to figure out base case for list variable. Base case for defn of list is [ ], but not appropriate for base case of membership. Why? Need to look elsewhere. Whats the simplest case for deciding membership?

MB: 5 March 2001

CS360 Lecture 4

11

List Membership
Its

the first item in the list. Maybe this can be our base case.
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

CS360 Lecture 4

12

List Membership
What

if item is not the first one in list, then what? Then need to check if its in the tail.
member(I, [ _ | T ]) :- member(I, T).

Dont

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

CS360 Lecture 4

13

List Membership
KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] :- member(I, T). Query: member(x, [ ]). Response: no Execution Trace: [member(x,[ ])]

MB: 5 March 2001

CS360 Lecture 4

14

List Membership
KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] :- member(I, T). Query: member(x, [ w, x]). Response: continue trace Partial Execution Trace: [member(x,[ w, x ])]
2. I=x, T=[x]

[member(x, [x])]

MB: 5 March 2001

CS360 Lecture 4

15

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

MB: 5 March 2001

CS360 Lecture 4

16

List Concatenation
Define

relation conc(L1, L2, L3) where L3 is the result of concatenating L1 onto front of L2. is the base case?

What Go

back to defn of list, what is base case?

MB: 5 March 2001

CS360 Lecture 4

17

List Concatenation
List

defn base case is [ ], should this be our base case for defining concatenation? ], ?, ?) - what is the result of concatenating [ ] onto anything? Are there special cases? ], L2, L2).
CS360 Lecture 4 18

conc([

conc([
MB: 5 March 2001

List Concatenation
What

should the inductive step be? What was the inductive step for list defn? What should the head look like? conc([ I | L1], L2, [ I | L3]) Whats the relation between L1, L2, and L3? conc(L1, L2, L3)

MB: 5 March 2001

CS360 Lecture 4

19

List Concatenation
Full

definition:

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

Try

doing an execution trace for the query:

conc(L1, L2, [1, 2, 3]).

What

are the bindings for L1 and L2 if keep asking for alternatives?


CS360 Lecture 4 20

MB: 5 March 2001

Multi-Way Uses of Relations


We

have seen that one nice feature of logic programming is its absence of control. 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. conc/3 is an example of such a central relation.

MB: 5 March 2001

CS360 Lecture 4

21

Some Uses of Concatenation


member(I, last(

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

Item, List) :- conc(_ , [Item], List).

sublist(SubList,

List) :conc(L1, L2, List), conc(SubList, _, L2).


CS360 Lecture 4 22

MB: 5 March 2001

Clarity
We

dont really need to write defns for member /2 and last/2, could just use conc/3. have we gained by writing those definitions? write their definitions because we want it to be obvious what were trying to do!
CS360 Lecture 4 23

What

We

MB: 5 March 2001

List Operations
Adds

item to front of list:

add(Item, List, [Item | List]).

Given

the following code: add(1,[ ],L1), add(2, L1,L2), add(3,L2,L3). What would be the binding for L3?

MB: 5 March 2001

CS360 Lecture 4

24

List Operations
Deletes

item from list:

del(Item, [Item| Tail], Tail). del(Item, [Y | Tail], [Y | Tail1]) :del(Item, Tail, Tail1).

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?
CS360 Lecture 4 25

MB: 5 March 2001

Insert

item into list:

insert(I,List,NewList) :- del(I, NewList, 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

CS360 Lecture 4

26

Permutation of a List
Lets

define the permutation relation:

perm(List, Permutation).

Are

we clear about what is a permutation? type of definition will we look for?

Look at examples.

What

MB: 5 March 2001

CS360 Lecture 4

27

Defining Permutation Relation


Where

do we look for our cases?

What

should be our base case?

MB: 5 March 2001

CS360 Lecture 4

28

Defining Permutation Relation


What What

should be our inductive case? should the head look like? the relationship between the different

Whats

parts?

MB: 5 March 2001

CS360 Lecture 4

29

Permutation Defined
permutation([

],[ ]). permutation([X | L1], Perm) :permutation(L1, L2), insert(X, L2, Perm).

MB: 5 March 2001

CS360 Lecture 4

30

Homework Quiz
Write

definitions for the following relations:

reverse(List, ReverseList) subSet(Set, SubSet) flatten(List, FlatList)

MB: 5 March 2001

CS360 Lecture 4

31

Summary
If

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

Summary contd
Defining

relations in pure Prolog allows definitions to be used in many ways. However, when some uses have common name (e.g., last) then should define new relation from old using the common name.

MB: 5 March 2001

CS360 Lecture 4

33

You might also like