Using Definite Knowledge: Lists and Difference Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Using Definite Knowledge: Lists and Difference Lists

Description:

cons(a, cons(b, cons(c, cons(d, nil)))) denotes [a,b,c,d]. UNIVERSITY ... an ending of R, and the elements of R before A are the elements of L in reverse order. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 14
Provided by: MarcoVa
Learn more at: https://www.cse.sc.edu
Category:

less

Transcript and Presenter's Notes

Title: Using Definite Knowledge: Lists and Difference Lists


1
Using Definite KnowledgeLists and Difference
Lists
  • Notes for Ch.3 of Poole et al.
  • CSCE 580
  • Marco Valtorta

2
Lists
  • An example of abstract knowledge lists
    (lists.pl)
  • We will concentrate on the traditional
    representation that uses the constant nil and the
    function symbol cons.
  • analogy with 0 and succ in Peano arithmetic.
  • Other representations are possible, e.g.
  • Elt(L,I,E) E is the I-th element of list L
  • Size(L,N) List L has N elements

3
nil and cons
  • The constant nil denotes the empty list
  • The function symbol cons is defined so that
    cons(H,T) denotes the list whose first element
    (head of the list) is H and whose remaining
    elements are in the list (tail) T
  • nil and cons are not predefined symbols. We
    could use, say empty and attach, with no
    change for the computer.
  • Ex. cons(a, cons(b, cons(c, cons(d, nil))))
    denotes a,b,c,d.

4
Some Predicates on Lists
  • list(nil).
  • list(cons(H,T)) lt- list(T).
  • Our lists are not typed.
  • append(nil,Z,Z).
  • append(cons(A,X), Y, cons(A,Z)) lt-
    append(X,Y,Z).
  • We axiomatize on each case of the first argument.
    Correctness is proved by induction when the
    clauses are used in a top-down proof procedure, a
    recursive computation occurs.

5
List Notation
  • nil is written
  • cons(H,T) is written HT
  • cons(a,cons(b,nil)) is written a,b (etc.)
  • This is just syntactic sugar!
  • append(,Z,Z).
  • append(AX, Y, AZ) lt- append(X,Y,Z).

6
Member and notin
  • member(X, XL).
  • member(X, HR) lt- member(X,R).
  • notin(A, ).
  • notin(A, HT) lt- different(A,H) notin(A,T).
  • different can be hard to axiomatize!

7
Naïve Reverse
  • rev(,).
  • rev(HT, R) lt- rev(T,RT) append(RT,H,R).
  • A proof for rev has length quadratic in the
    length of the first argument, since a proof of
    append is linear in the size of its first
    argument.

8
Linear-Time Reverse
  • reverse(L,R) lt- rev3(L,,R).
  • rev3(,R,R).
  • rev3(HT,A,R) lt- rev3(T,HA,R).
  • A proof using reverse has length linear in the
    size of L (the list being reversed). The second
    argument of rev3, A, is used as an accumulator
    as elements are stripped off the first list, they
    are added to the accumulator.

9
Reverse with an accumulator in ML
  • Fun rev1(nil, M) M
  • rev1(xxs, ys) rev1(xs, xys)
  • Fun reverse(L) rev1(L,nil)
  • Consider
  • rev1(a1,a2,,an,b1,b2,,bm)
  • After the call
  • rev1(a2,a3,,an,a1,b1,b2,,bm
  • On our way to
  • an,an-1,a1,b1,b2,,bm

10
Difference Lists
  • The difference list L-E contains the elements of
    L that occur before E. E is a suffix of L.
  • E.g., a,b,c,d c,d is a,b
  • Trick a,b,c,dE E is a,b,c,d!
  • Difference lists allow concatenation of lists in
    constant time, as in imperative languages.

11
Append and Difference Lists
  • Difference lists can be concatenated in constant
    time by using append_dl
  • ?append_dl(a,bA A, c,dC C, Result)
  • unify with append_dl(X Y, Y Z, X Z)
  • X / a,bA
  • Y / A
  • A / c,dC
  • Z / C
  • Result / X Z, i.e. Result / a,bA C,
    i.e.
  • Result / a,b,c,dC C

12
Some Caveats
  • While the result of append_dl has an
    uninstatiated tail, the first argument does not.
  • You cannot use it as an argument to append_dl in
    the same clause.
  • Difference lists are powerful but
    counterintuitive. They appear most often in
    procedures that were first implemented with
    conventional lists and then carefully rewritten
    for efficiency Covington, p.183.

13
Rev3 and Difference Lists
  • In rev3, R-A is a difference list that contains
    the elements of L in reverse order A is an
    ending of R, and the elements of R before A are
    the elements of L in reverse order.
  • When A is the empty list, R contains the reverse
    of list L. (See text for full argument.)
  • It is more natural to view A as an accumulator,
    but difference lists become important in natural
    language processing, because you do not want to
    reverse lists of words!
Write a Comment
User Comments (0)
About PowerShow.com