Builtin Predicates t k prasadwright edu http www
Built-in Predicates t. k. prasad@wright. edu http: //www. knoesis. org/tkprasad/ cs 774 (Prasad) L 8 Built. Ins 1
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 cs 774 (Prasad) L 8 Built. Ins 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). cs 774 (Prasad) L 8 Built. Ins 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. cs 774 (Prasad) L 8 Built. Ins 4
Input Prolog program from files ? -consult(eg). ? -[eg]. ? -[‘eg. pl’]. ? -reconsult(eg). • From keyboard … ? -consult(user). … ^D cs 774 (Prasad) L 8 Built. Ins 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(file. Name). tell(file. Name). – Closing seen. told. – Currently active stream cs 774 (Prasad) seeing(X). telling(user). L 8 Built. Ins 6
Character I/O get(X) : read next, non-blank character • get 0(X) : read next character (ISO Std. ) • put(X) : write the character (given X is bound to character encoding) • cs 774 (Prasad) L 8 Built. Ins ? -get(X). : e X = 101 ? -put(101). e ? -put(‘e’). e ? -put(“e”). e 7
Term I/O • ? -read(X). : a + b X = a+b read(X) • write(X) • display(X) ? -write(a+b). a+b • nl • tab(N) cs 774 (Prasad) ? -display(a+b). +(a, b) L 8 Built. Ins 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)) cs 774 (Prasad) L 8 Built. Ins 9
Term construction and inspection • functor(Term, Function. Symbol, 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, _) cs 774 (Prasad) L 8 Built. Ins 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 cs 774 (Prasad) L 8 Built. Ins 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. cs 774 (Prasad) L 8 Built. Ins 12
Interpretation of term as a goal • call meta-predicate • cf. eval function in LISP – as predicate formula ? - p(X). • Call interprets a “data structure” as a piece of “program”. – as object term – Requires dynamic compilation and execution ? - call(p(X)). cs 774 (Prasad) L 8 Built. Ins 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 cs 774 (Prasad) L 8 Built. Ins 14
Defining basic call-predicate call( true ) : - !. call( (G 1, G 2) ) : - !, call(G 1), call(G 2). call( G ) : clause(G, B), call(B). cs 774 (Prasad) L 8 Built. Ins 15
Implementing findall. B(X, Goal, Xlist) : call(Goal), assertz(queue(X)), fail ; assertz(queue(bottom)), collect(Xlist). cs 774 (Prasad) L 8 Built. Ins 16
(cont’d) collect(L) : retract(queue(X)), !, ( X == bottom, !, L = [] ; L = [X | Rest], collect(Rest) ). cs 774 (Prasad) L 8 Built. Ins 17
Alternative Implemention findall. CM(X, Goal, _) : asserta(queue(bottom)), call(Goal), asserta(queue(X)), fail. findall. CM(_, _, L) : collect([], M), !, L = M. cs 774 (Prasad) L 8 Built. Ins 18
(cont’d) collect(S, L) : get. Next(X), !, collect([X|S], L). collect(L, L). get. Next(S, L) : retract(queue(X)), !, X == bottom. cs 774 (Prasad) L 8 Built. Ins 19
Database e(happy). e(sad). m(tom, happy). m(bev, sad). m(amy, happy). cs 774 (Prasad) L 8 Built. Ins 20
Queries • Both definitions agree on the following query. ? - findall. B(em(E, P), m(P, E), EC). m(P, E) ? - findall. CM(em(E, P), m(P, E), EC). m(P, E) EC = [ em(happy, tom), em(sad, bev), em(happy, amy)] cs 774 (Prasad) L 8 Built. Ins 21
(cont’d) • Both definitions do not agree on the following query. ? - findall. B(EC, (e(E), findall. B(em(E, P), m(P, E), EC)), Ans). EC = [[[ em(happy, tom), em(happy, amy)], em(sad, bev)]] cs 774 (Prasad) L 8 Built. Ins 22
(cont’d) • Both definitions do not agree on the following query. ? - findall. CM(EC, (e(E), findall. CM(em(E, P), m(P, E), EC)), Ans). EC = [[ em(happy, tom), em(happy, amy)], [em(sad, bev)]] cs 774 (Prasad) L 8 Built. Ins 23
- Slides: 23