Built-in Predicates
230 likes | 342 Vues
Explore a detailed breakdown of Prolog built-in predicates for efficient program development, including file I/O, debugging, term manipulation, and database interaction. Learn how to interpret terms as goals, access database clauses, and implement advanced functions like findall. Master Prolog's powerful features to enhance your programming skills.
Built-in Predicates
E N D
Presentation Transcript
Built-in Predicates t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/ L8BuiltIns
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 L8BuiltIns
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). L8BuiltIns
(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. L8BuiltIns
Input Prolog program from files ?-consult(eg). ?-[eg]. ?-[‘eg.pl’]. ?-reconsult(eg). • From keyboard … ?-consult(user). … ^D L8BuiltIns
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). L8BuiltIns
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 L8BuiltIns
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) L8BuiltIns
Term to/from list ?- f(a,b) =.. L. L = [f,a,b] ?- Z =.. [p,a,f(X,Y)]. Z = p(a,f(X,Y)) L8BuiltIns
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,_) L8BuiltIns
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 L8BuiltIns
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. L8BuiltIns
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 L8BuiltIns
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 L8BuiltIns
Defining basic call-predicate call( true ) :- !. call( (G1, G2) ) :- !, call(G1), call(G2). call( G ) :- clause(G,B), call(B). L8BuiltIns
Implementing findall findallB(X, Goal, Xlist) :- call(Goal), assertz(queue(X)), fail ; assertz(queue(bottom)), collect(Xlist). L8BuiltIns
(cont’d) collect(L) :- retract(queue(X)), !, (X == bottom,!, L = [] ; L = [X | Rest], collect(Rest) ). L8BuiltIns
Alternative Implemention findallCM(X, Goal, _) :- asserta(queue(bottom)), call(Goal), asserta(queue(X)), fail. findallCM(_, _, L) :- collect([],M), !, L = M. L8BuiltIns
(cont’d) collect(S,L) :- getNext(X), !, collect([X|S],L). collect(L,L). getNext(S,L) :- retract(queue(X)), !, X \== bottom. L8BuiltIns
Database e(happy). e(sad). m(tom,happy). m(bev,sad). m(amy,happy). L8BuiltIns
Queries • Both definitions agree on the following query. ?- findallB(em(E,P),m(P,E),EC). ?- findallCM(em(E,P),m(P,E),EC). EC = [ em(happy,tom), em(sad,bev), em(happy,amy)] L8BuiltIns
(cont’d) • Both definitions do not agree on the following query. ?- findallB(EC, (e(E), findallB(em(E,P),m(P,E),EC)),Ans). EC = [[[em(happy,tom), em(happy,amy)], em(sad,bev)]] L8BuiltIns
(cont’d) • Both definitions do not agree on the following query. ?- findallCM(EC, (e(E), findallCM(em(E,P),m(P,E),EC)),Ans). EC = [[em(happy,tom), em(happy,amy)], [em(sad,bev)]] L8BuiltIns