Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

17
cs774 (Prasad) L4ListProcessing 1 List processing in Prolog [email protected] http://www.knoesis.org/ tkprasad/

description

(cont’d) List containing elements a, b, and c (in that order):.(a,.(b,.(c, []))) cs774 (Prasad)L4ListProcessing3 special list functor for cons empty list

Transcript of Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Page 1: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

cs774 (Prasad) L4ListProcessing 1

List processing in Prolog

[email protected]://www.knoesis.org/tkprasad/

Page 2: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

List Structures• List containing elements a, b, and c (in that

order):[a, b, c]

[a | [b, c]][a, b | [c] ]

cs774 (Prasad) L4ListProcessing 2

element separator

cons operator

Page 3: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

(cont’d)• List containing elements a, b, and c (in that

order):

.(a, .(b, .(c, [])))

cs774 (Prasad) L4ListProcessing 3

special list functor for cons

empty list

Page 4: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Membership Test: Scheme vs Prolog(define (member e lis)(cond ((null? lis) #f)

((eq? e (car lis)) #t )(else (member e (cdr lis)))))

> (member 'a '(1 21 2))#f

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

?- member(1,[12,1,3]).true ;

cs774 (Prasad) L4ListProcessing 4

Page 5: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Cartesian Product

cs774 (Prasad) L4ListProcessing 5

} |),{( BbAabaBA cart([],_,[]).cart([HA|TA],B,AB) :-

pair(HA,B,C),cart(TA,B,D), append(C,D,AB).

pair(_,[],[]).pair(HA,[HB|TB],[(HA,HB)|R]) :-

pair(HA,TB,R).

Page 6: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Permutations

• [a,b] ->permute-> [[a,b], [b,a]]

• [a,b,c] ->permute-> [[c,a,b],[a,c,b],[a,b,c]] [[c,b,a],[b,c,a],[b,a,c]]

cs774 (Prasad) L4ListProcessing 6

Page 7: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

cs774 (Prasad) L4ListProcessing 7

permutation([],[]).permutation([H|T],P) :-

permutation(T,Q),insert(H,Q,P).

insert(H,Q,[H|Q]).insert(H,[S|R],[S|T]) :-

insert(H,R,T).

/*Generate all permutations through backtracking */

?-setof(X,permutation([1,2,3],X),R). R = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]].

Page 8: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Collection Predicates

p(a,b).p(b,b).p(c,b).p(e,d).p(a,d).p(a,a).

/* collect all values of X for each Y */

?-setof(X,p(X,Y),S).

Y = a,S = [a] ;

Y = b,S = [a, b, c] ;

Y = d,S = [a, e].

cs774 (Prasad) L4ListProcessing 8

Page 9: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

(cont’d)

p(a,b).p(b,b).p(c,b).p(e,d).p(a,d).p(a,a).

/* collect all values of Y for which an X exists */

?-setof(Y,X^p(X,Y),S).

S = [a, b, d] ;

cs774 (Prasad) L4ListProcessing 9

ordered, no dups

Page 10: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

(cont’d)

p(a,b).p(b,b).p(c,b).p(e,d).p(a,d).p(a,a).

/* collect all values of X for each Y */

?-bagof(X,p(X,Y),S).

Y = a,S = [a] ;

Y = b,S = [a, b, c] ;

Y = d,S = [e, a].

cs774 (Prasad) L4ListProcessing 10

Page 11: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

(cont’d)

p(a,b).p(b,b).p(c,b).p(e,d).p(a,d).p(a,a).

/* collect all values of Y for which an X exists */

?-bagof(Y,X^p(X,Y),S).

S = [b, b, b, d, d, a].

cs774 (Prasad) L4ListProcessing 11

unordered, dups present

Page 12: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

(cont’d)

p(a,b).p(b,b).p(c,b).p(e,d).p(a,d).p(a,a).

/* collect all values of X for which a Y exists , in order*/

?-findall(X,p(X,Y),S).

S = [a, b, c, e, a, a].

/* Free variables existentially quantified by default. */

?-findall(Y,X^p(X,Y),S).

S = [b, b, b, d, d, a].

cs774 (Prasad) L4ListProcessing 12

Page 13: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Operators• Normal syntax of a term : prefix form

f(g(x,Y),5)• Normal syntax of an expression with operators :

infix formx * Y + 5

• Besides operator name and arity, the following information is necessary to disambiguate expressions containing operators.

• Associativity• Precedence• Fixity (prefix/infix/postfix)

cs774 (Prasad) L4ListProcessing 13

Page 14: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

INFIX• x f y : Right-associative• y f x : Left-associative• x f x : Non-associative

PREFIX• f x : Non-associative• f y : Right-associative

POSTFIX• x f : Non-associative• y f : Left-associative

Operator Declaration:- op(precedence,

type, name).Examples:

:- op( 1200, xfx, [ :-, --> ]). :- op(500, yfx, [ +, - ]). :- op( 500, fx, [ +, - ]).

:- op(900, fy, [ \+ ]).

Higher precedence class => Lower binding strength

cs774 (Prasad) L4ListProcessing 14

Page 15: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

| 1200 |xfx |-->, :- | | 1200 | fx |:-, ?- | | 1150 | fx |dynamic, discontiguous, initialization, mod- | | | |ule_transparent, multifile, thread_local,| | | |volatile |

| 1100 |xfy |;, | | | 1050 |xfy |->, op*-> | | 1000 |xfy |, | | 900 | fy |\+ | | 900 | fx |~ | | 700 |xfx |<, =, =.., =@=, =:=, =<, ==, =\=, >, >=, @<, | | | |@=<, @>, @>=, \=, \==, is | | 600 |xfy |: | | 500 | yfx |+, -, /\, \/, xor | | 500 | fx |? | | 400 | yfx |*, /, //, rdiv, <<, >>, mod, rem | | 200 |xfx |** | | 200 |xfy |^ | |__200_|_fy__|+,_-,_\________________________________________|_

Table 4.1: Predefined system operators

cs774 (Prasad) L4ListProcessing 15

Page 16: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Examples : “Term Disambiguation”

• p; q; r• p :- q :- r• :- :- p• \+ \+ p• a + b + c• a ^ b ^ c• a – b / c

• p;(q;r)• Illegal• Illegal• \+ (\+ p)• (a + b) + c• a ^ ( b ^ c)• a – ( b / c )

cs774 (Prasad) L4ListProcessing 16

Page 17: Cs774 (Prasad)L4ListProcessing1 List processing in Prolog

Exercise

p :- a, b -> c ; d.p :- (a, (b -> c ; d)).

vsp :- (((a, b) -> c) ; d).

q :- b -> c ; d , a.q :-(b -> (c ; (d , a))).

vsq :-((b -> c) ; (d , a)).

cs774 (Prasad) L4ListProcessing 17