23
cs774 (Prasad) L8BuiltIns 1 Built-in Predicates [email protected] http://www.knoesis.org/ tkprasad/

Built-in Predicates

Embed Size (px)

DESCRIPTION

Built-in Predicates. [email protected] http://www.knoesis.org/tkprasad/. Example Categories. Program Updates File I/O Opening/closing, character I/O, term I/O Value Classification using type predicates Manipulating terms and programs Debugging predicates. - PowerPoint PPT Presentation

Citation preview

cs774 (Prasad) L8BuiltIns 1

Built-in Predicates

[email protected]

http://www.knoesis.org/tkprasad/

Example Categories

• Program Updates

• File I/O • Opening/closing, character I/O, term I/O

• Value Classification using type predicates

• Manipulating terms and programs

• Debugging predicates

cs774 (Prasad) L8BuiltIns 2

Database Manipulation Predicates

q(b).

?-asserta(q(a)).

?-listing(q).

q(a).

q(b).

?-assertz(q(c)).

?-listing(q).

q(a).

q(b).

q(c).

cs774 (Prasad) L8BuiltIns 3

(cont’d)?-retract(q(a)).

?- q(a).

no

?-abolish(q/1).

?-retractall(p).

?- q(X).

no • These are extra-logical predicates that change the

“program” on the fly.

• Useful for updating databases or simulating persistence of values through backtracking.

cs774 (Prasad) L8BuiltIns 4

Input Prolog program from files?-consult(eg).

?-[eg].

?-[‘eg.pl’].

?-reconsult(eg).

• From keyboard …

?-consult(user).

^D

cs774 (Prasad) L8BuiltIns 5

Communication with files• At anytime during the execution of a Prolog program, only two

files are active : current input stream and current output stream.– Opening

see(fileName).

tell(fileName).

– Closing

seen.

told. – Currently active stream

seeing(X).

telling(user).

cs774 (Prasad) L8BuiltIns 6

Character I/O

• get(X) : read next, non-blank character

• get0(X) : read next character (ISO Std.)

• put(X) : write the character (given X is bound to character encoding)

?-get(X).

:e

X = 101

?-put(101).

e

?-put(‘e’).

e

?-put(“e”).

e

cs774 (Prasad) L8BuiltIns 7

Term I/O• read(X)

• write(X)

• display(X)

• nl• tab(N)

?-read(X).

:a + b

X = a+b

?-write(a+b).

a+b

?-display(a+b).

+(a,b)

cs774 (Prasad) L8BuiltIns 8

Term to/from list

?- f(a,b) =.. L.

L = [f,a,b]

?- Z =.. [p,a,f(X,Y)].

Z = p(a,f(X,Y))

cs774 (Prasad) L8BuiltIns 9

Term construction and inspection• functor(Term, FunctionSymbol, Arity)

?- functor(f(a,b,c), F, N).

F = f

N = 3• arg(Number, Term, Argument)

?- arg(3, f(a,b,g(c,d)), T).

T = g(c,d)

?- functor(T,g,2), arg(1,T,a).

T = g(a,_)

cs774 (Prasad) L8BuiltIns 10

Atom to/from list• name(Atom, List)

?- name(abc,L).

L = [97,98,99]

?- name(N,[66,67,68]).

N = ‘ABC’

?- name(N,”abc”).

N = abc

?- name(123,[49,50,51]).

true

cs774 (Prasad) L8BuiltIns 11

Debugging Predicates?- trace.

?- notrace.

?- spy(p).

?- spy(q/2).

• Trace stops at every goal.

• <RETUTN> takes to the next goal.

• l (leap) goes to next spy-point.

cs774 (Prasad) L8BuiltIns 12

Interpretation of term as a goal

• call meta-predicate• cf. eval function in

LISP

– as predicate formula

?- p(X).– as object term

?- call(p(X)).

• Call interprets a “data structure” as a piece of “program”.

– Requires dynamic compilation and execution

cs774 (Prasad) L8BuiltIns 13

Accessing “database” clauses

clause(Head, Body).

– Iterates over term representations of head and body of clauses of the loaded program

• Fundamental to meta-programming, specifically, for writing meta-interpreters

cs774 (Prasad) L8BuiltIns 14

Defining basic call-predicate

call( true ) :- !.

call( (G1, G2) ) :- !,

call(G1), call(G2).

call( G ) :-

clause(G,B), call(B).

cs774 (Prasad) L8BuiltIns 15

Implementing findall

findallB(X, Goal, Xlist) :-

call(Goal),

assertz(queue(X)),

fail ;

assertz(queue(bottom)),

collect(Xlist).

cs774 (Prasad) L8BuiltIns 16

(cont’d)

collect(L) :-

retract(queue(X)), !,

( X == bottom,!, L = [] ;

L = [X | Rest],

collect(Rest) ).

cs774 (Prasad) L8BuiltIns 17

Alternative Implemention

findallCM(X, Goal, _) :-

asserta(queue(bottom)), call(Goal),

asserta(queue(X)),

fail.

findallCM(_, _, L) :-

collect([],M), !, L = M.

cs774 (Prasad) L8BuiltIns 18

(cont’d)

collect(S,L) :-

getNext(X), !,

collect([X|S],L).

collect(L,L).

getNext(S,L) :-

retract(queue(X)), !,

X \== bottom.cs774 (Prasad) L8BuiltIns 19

Database

e(happy).

e(sad).

m(tom,happy).

m(bev,sad).

m(amy,happy).

cs774 (Prasad) L8BuiltIns 20

Queries

• Both definitions agree on the following query.

?- findallB(em(E,P),m(P,E)m(P,E),EC).

?- findallCM(em(E,P),m(P,E)m(P,E),EC).

EC = [ em(happy,tom), em(sad,bev),

em(happy,amy)]

cs774 (Prasad) L8BuiltIns 21

(cont’d)• Both definitions do not agree on the

following query.

?- findallB(EC, (e(E), findallBfindallB((em(E,P),m(P,E),ECem(E,P),m(P,E),EC))),

Ans).

EC = [[[ em(happy,tom),

em(happy,amy)], em(sad,bev)]]

cs774 (Prasad) L8BuiltIns 22

(cont’d)• Both definitions do not agree on the

following query.

?- findallCM(EC, (e(E), findallCMfindallCM((em(E,P),m(P,E),ECem(E,P),m(P,E),EC))),

Ans).

EC = [[ em(happy,tom),

em(happy,amy)], [em(sad,bev)]]

cs774 (Prasad) L8BuiltIns 23