Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge: Lists and Difference Lists Notes for Ch.3 of Poole.

Similar presentations


Presentation on theme: "UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge: Lists and Difference Lists Notes for Ch.3 of Poole."— Presentation transcript:

1 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge: Lists and Difference Lists Notes for Ch.3 of Poole et al. CSCE 580 Marco Valtorta

2 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Some Predicates on Lists list(nil). list(cons(H,T)) <- list(T). –Our lists are not typed. append(nil,Z,Z). append(cons(A,X), Y, cons(A,Z)) <- 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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering List Notation nil is written [] cons(H,T) is written [H|T] cons(a,cons(b,nil)) is written [a,b] (etc.) –This is just syntactic sugar! append([],Z,Z). append([A|X], Y, [A|Z]) <- append(X,Y,Z).

6 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Member and notin member(X, [X|L]). member(X, [H|R]) <- member(X,R). notin(A, []). notin(A, [H|T]) <- different(A,H) & notin(A,T). different can be hard to axiomatize!

7 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Naïve Reverse rev([],[]). rev([H|T], R) <- 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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Linear-Time Reverse reverse(L,R) <- rev3(L,[],R). rev3([],R,R). rev3([H|T],A,R) <- rev3(T,[H|A],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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Reverse with an accumulator in ML Fun rev1(nil, M) = M | rev1(x::xs, ys) = rev1(xs, x::ys); 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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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,d|E] – E is [a,b,c,d]! Difference lists allow concatenation of lists in constant time, as in imperative languages.

11 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Append and Difference Lists Difference lists can be concatenated in constant time by using append_dl: ?append_dl([a,b|A] – A, [c,d|C] – C, Result) unify with append_dl(X – Y, Y – Z, X – Z): {X / [a,b|A]} {Y / A} {A / [c,d|C]} {Z / C} {Result / X – Z}, i.e. {Result / [a,b|A] – C}, i.e. {Result / [a,b,c,d|C} – C}

12 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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!


Download ppt "UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge: Lists and Difference Lists Notes for Ch.3 of Poole."

Similar presentations


Ads by Google