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

Built-in Predicates

Dec 30, 2015

Download

Documents

noah-rutledge

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Built-in Predicates

cs774 (Prasad) L8BuiltIns 1

Built-in Predicates

[email protected]

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

Page 2: Built-in Predicates

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

Page 3: Built-in Predicates

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

Page 4: Built-in Predicates

(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

Page 5: Built-in Predicates

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

?-[eg].

?-[‘eg.pl’].

?-reconsult(eg).

• From keyboard …

?-consult(user).

^D

cs774 (Prasad) L8BuiltIns 5

Page 6: Built-in Predicates

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

Page 7: Built-in Predicates

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

Page 8: Built-in Predicates

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

Page 9: Built-in Predicates

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

Page 10: Built-in Predicates

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

Page 11: Built-in Predicates

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

Page 12: Built-in Predicates

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

Page 13: Built-in Predicates

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

Page 14: Built-in Predicates

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

Page 15: Built-in Predicates

Defining basic call-predicate

call( true ) :- !.

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

call(G1), call(G2).

call( G ) :-

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

cs774 (Prasad) L8BuiltIns 15

Page 16: Built-in Predicates

Implementing findall

findallB(X, Goal, Xlist) :-

call(Goal),

assertz(queue(X)),

fail ;

assertz(queue(bottom)),

collect(Xlist).

cs774 (Prasad) L8BuiltIns 16

Page 17: Built-in Predicates

(cont’d)

collect(L) :-

retract(queue(X)), !,

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

L = [X | Rest],

collect(Rest) ).

cs774 (Prasad) L8BuiltIns 17

Page 18: Built-in Predicates

Alternative Implemention

findallCM(X, Goal, _) :-

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

asserta(queue(X)),

fail.

findallCM(_, _, L) :-

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

cs774 (Prasad) L8BuiltIns 18

Page 19: Built-in Predicates

(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

Page 20: Built-in Predicates

Database

e(happy).

e(sad).

m(tom,happy).

m(bev,sad).

m(amy,happy).

cs774 (Prasad) L8BuiltIns 20

Page 21: Built-in Predicates

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

Page 22: Built-in Predicates

(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

Page 23: Built-in Predicates

(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