1 / 23

Built-in Predicates

Built-in Predicates. t.k.prasad@wright.edu 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.

Télécharger la présentation

Built-in Predicates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Built-in Predicates t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/ L8BuiltIns

  2. 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

  3. 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

  4. (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

  5. Input Prolog program from files ?-consult(eg). ?-[eg]. ?-[‘eg.pl’]. ?-reconsult(eg). • From keyboard … ?-consult(user). … ^D L8BuiltIns

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. Defining basic call-predicate call( true ) :- !. call( (G1, G2) ) :- !, call(G1), call(G2). call( G ) :- clause(G,B), call(B). L8BuiltIns

  16. Implementing findall findallB(X, Goal, Xlist) :- call(Goal), assertz(queue(X)), fail ; assertz(queue(bottom)), collect(Xlist). L8BuiltIns

  17. (cont’d) collect(L) :- retract(queue(X)), !, (X == bottom,!, L = [] ; L = [X | Rest], collect(Rest) ). L8BuiltIns

  18. Alternative Implemention findallCM(X, Goal, _) :- asserta(queue(bottom)), call(Goal), asserta(queue(X)), fail. findallCM(_, _, L) :- collect([],M), !, L = M. L8BuiltIns

  19. (cont’d) collect(S,L) :- getNext(X), !, collect([X|S],L). collect(L,L). getNext(S,L) :- retract(queue(X)), !, X \== bottom. L8BuiltIns

  20. Database e(happy). e(sad). m(tom,happy). m(bev,sad). m(amy,happy). L8BuiltIns

  21. 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

  22. (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

  23. (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

More Related