L4-ListsAndListOperations

Embed Size (px)

Citation preview

  • 7/27/2019 L4-ListsAndListOperations

    1/33

    MB: 5 March 2001 CS360 Lecture 4 1

    Programming in Logic: Prolog

    Lists and List Operations

    Readings: Sections 3.1 & 3.2

  • 7/27/2019 L4-ListsAndListOperations

    2/33

    MB: 5 March 2001 CS360 Lecture 4 2

    Review

    Declarative semantics of pure Prolog programdefines when a goal is true and for what

    instantiation of variables.

    Commas between goals mean and, semicolonsmean or.

    Procedural semantics are determined by clauseand goal ordering.

    Goal failure causes backtracking and unbindingof affected variables.

  • 7/27/2019 L4-ListsAndListOperations

    3/33

    MB: 5 March 2001 CS360 Lecture 4 3

    Programming Patterns

    Today we start actually looking at programs

    We will look at a lot of them!!

    You should look for some underlying patternsof how to program in Prolog

  • 7/27/2019 L4-ListsAndListOperations

    4/33

    MB: 5 March 2001 CS360 Lecture 4 4

    Prolog Lists

    The elements of a list can be anything includingother lists.

    Remember that atoms could be made from asequence of special characters, the empty list is

    the special atom [ ].

  • 7/27/2019 L4-ListsAndListOperations

    5/33

    MB: 5 March 2001 CS360 Lecture 4 5

    Lists

    A non-empty list always contains two things, aheadand the tail. It is a structured data object.

    The functor name is . and its arity is 2.

    The list consisting of the item 3 is: .(3,[ ])

  • 7/27/2019 L4-ListsAndListOperations

    6/33

    MB: 5 March 2001 CS360 Lecture 4 6

    Lists contd

    The list consisting of the two items 3 and xis: .(3,.(x,[ ]))

    Lists are one of the most pervasive datastructures in Prolog, so there is a special

    notation for them: [3, x]

  • 7/27/2019 L4-ListsAndListOperations

    7/33

    MB: 5 March 2001 CS360 Lecture 4 7

    Lists contd

    Often want to describe beginning of list withoutspecifying the rest of it. For example,.(3,.(x,

    T)) describes a list whose first two items are 3

    andx, and whose remaining items could beanything (including empty).

    Prolog provides a special notation for doingthis, |, e.g., [3,x |T]

  • 7/27/2019 L4-ListsAndListOperations

    8/33

    MB: 5 March 2001 CS360 Lecture 4 8

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

  • 7/27/2019 L4-ListsAndListOperations

    9/33

    MB: 5 March 2001 CS360 Lecture 4 9

    Definition of List

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

  • 7/27/2019 L4-ListsAndListOperations

    10/33

    MB: 5 March 2001 CS360 Lecture 4 10

    List Operations

    Since lists are an inductively defined datastructure, expect operations to be inductively

    defined.

    One common list operation is checking whethersomething is a member of the list.

    member(Item, List)

  • 7/27/2019 L4-ListsAndListOperations

    11/33

    MB: 5 March 2001 CS360 Lecture 4 11

    List Membership

    If defining list membership inductively, need to figureout base case for list variable.

    Base case for defn of list is [ ], but not appropriate forbase case of membership. Why?

    Need to look elsewhere. Whats the simplest case fordeciding membership?

  • 7/27/2019 L4-ListsAndListOperations

    12/33

    MB: 5 March 2001 CS360 Lecture 4 12

    List Membership

    Its the first item in the list. Maybe this can beour base case.

    member(Item, List) :- List = [Item | _ ].

    Prolog is pattern-directed, i.e., does patternmatching, can use this to simplify base case:

    member( I, [ I | _ ]).

  • 7/27/2019 L4-ListsAndListOperations

    13/33

    MB: 5 March 2001 CS360 Lecture 4 13

    List Membership

    What if item is not the first one in list, thenwhat? 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, thenIis not in

    the list, so we should fail.

  • 7/27/2019 L4-ListsAndListOperations

    14/33

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

    Response: noExecution Trace:[member(x,[ ])]

  • 7/27/2019 L4-ListsAndListOperations

    15/33

    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:

    Partial Execution Trace:[member(x,[ w, x ])] 2. I=x, T=[x][member(x, [x])]

    continue trace

  • 7/27/2019 L4-ListsAndListOperations

    16/33

    MB: 5 March 2001 CS360 Lecture 4 16

    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

  • 7/27/2019 L4-ListsAndListOperations

    17/33

    MB: 5 March 2001 CS360 Lecture 4 17

    List Concatenation

    Define relation conc(L1, L2, L3) whereL3 is theresult of concatenatingL1 onto front ofL2.

    What is the base case?

    Go back to defn of list, what is base case?

  • 7/27/2019 L4-ListsAndListOperations

    18/33

    MB: 5 March 2001 CS360 Lecture 4 18

    List Concatenation

    List defn base case is [ ], should this be ourbase case for defining concatenation?

    conc([ ], ?, ?) - what is the result ofconcatenating [ ] onto anything? Are there

    special cases?

    conc([ ], L2, L2).

  • 7/27/2019 L4-ListsAndListOperations

    19/33

    MB: 5 March 2001 CS360 Lecture 4 19

    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 betweenL1, L2, andL3? conc(L1, L2, L3)

  • 7/27/2019 L4-ListsAndListOperations

    20/33

    MB: 5 March 2001 CS360 Lecture 4 20

    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 forL1 andL2 if keepasking for alternatives?

  • 7/27/2019 L4-ListsAndListOperations

    21/33

    MB: 5 March 2001 CS360 Lecture 4 21

    Multi-Way Uses of Relations

    We have seen that one nice feature of logicprogramming is its absence of control.

    This means we can define one central relationand 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.

  • 7/27/2019 L4-ListsAndListOperations

    22/33

    MB: 5 March 2001 CS360 Lecture 4 22

    Some Uses of Concatenation

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

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

    sublist(SubList, List) :-conc(L1, L2, List),

    conc(SubList, _, L2).

  • 7/27/2019 L4-ListsAndListOperations

    23/33

    MB: 5 March 2001 CS360 Lecture 4 23

    Clarity

    We dont really need to write defns formember/2 and last/2, could just use conc/3.

    What have we gained by writing thosedefinitions?

    We write their definitions because we want it tobe obvious what were trying to do!

  • 7/27/2019 L4-ListsAndListOperations

    24/33

    MB: 5 March 2001 CS360 Lecture 4 24

    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?

  • 7/27/2019 L4-ListsAndListOperations

    25/33

    MB: 5 March 2001 CS360 Lecture 4 25

    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 woulddel(1,[1,2,1,3,1],L).

    What would be the bindings forL if we

    repeatedly asked for new alternatives?

  • 7/27/2019 L4-ListsAndListOperations

    26/33

    MB: 5 March 2001 CS360 Lecture 4 26

    Insert item into list: insert(I,List,NewList) :- del(I, NewList, List).

    insert/3 also non-deterministic, what wouldinsert(x, [1,2,3], L)

    bind toL if repeatedly ask for alternatives?

  • 7/27/2019 L4-ListsAndListOperations

    27/33

    MB: 5 March 2001 CS360 Lecture 4 27

    Permutation of a List

    Lets define the permutation relation:perm(List, Permutation).

    Are we clear about what is a permutation? Look at examples.

    What type of definition will we look for?

  • 7/27/2019 L4-ListsAndListOperations

    28/33

    MB: 5 March 2001 CS360 Lecture 4 28

    Defining Permutation Relation

    Where do we look for our cases?

    What should be our base case?

  • 7/27/2019 L4-ListsAndListOperations

    29/33

    MB: 5 March 2001 CS360 Lecture 4 29

    Defining Permutation Relation

    What should be our inductive case?

    What should the head look like?

    Whats the relationship between the differentparts?

  • 7/27/2019 L4-ListsAndListOperations

    30/33

    MB: 5 March 2001 CS360 Lecture 4 30

    Permutation Defined

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

    permutation(L1, L2),

    insert(X, L2, Perm).

  • 7/27/2019 L4-ListsAndListOperations

    31/33

    MB: 5 March 2001 CS360 Lecture 4 31

    Homework Quiz

    Write definitions for the following relations: reverse(List, ReverseList)

    subSet(Set, SubSet)

    flatten(List, FlatList)

  • 7/27/2019 L4-ListsAndListOperations

    32/33

    MB: 5 March 2001 CS360 Lecture 4 32

    Summary

    If data structure defined inductively thenusually operations are defined inductively.

    However, sometimes the data structure basecase does not make sense for the operation,

    then need to find new base case.

    First part of coming up with inductive case isfinding what the head should be, often part ofhead is data structure inductive case.

  • 7/27/2019 L4-ListsAndListOperations

    33/33

    MB: 5 March 2001 CS360 Lecture 4 33

    Summary contd

    Defining relations in pure Prolog allowsdefinitions 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.