Built-in Predicates - PowerPoint PPT Presentation

About This Presentation
Title:

Built-in Predicates

Description:

Title: Logic Programming Author: T. K. Prasad Last modified by: test Created Date: 9/30/1996 6:28:10 PM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 24
Provided by: TK63
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Built-in Predicates


1
Built-in Predicates
  • t.k.prasad_at_wright.edu
  • http//www.knoesis.org/tkprasad/

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

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

4
(contd)
  • ?-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.

5
Input Prolog program from files
  • ?-consult(eg).
  • ?-eg.
  • ?-eg.pl.
  • ?-reconsult(eg).
  • From keyboard
  • ?-consult(user).
  • D

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

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

8
Term I/O
  • read(X)
  • write(X)
  • display(X)
  • nl
  • tab(N)
  • ?-read(X).
  • a b
  • X ab
  • ?-write(ab).
  • ab
  • ?-display(ab).
  • (a,b)

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

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,_)

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

12
Debugging Predicates
  • ?- trace.
  • ?- notrace.
  • ?- spy(p).
  • ?- spy(q/2).
  • Trace stops at every goal.
  • ltRETUTNgt takes to the next goal.
  • l (leap) goes to next spy-point.

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

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

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

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

17
(contd)
  • collect(L) -
  • retract(queue(X)), !,
  • ( X bottom,!, L
  • L X Rest,
  • collect(Rest) ).

18
Alternative Implemention
  • findallCM(X, Goal, _) -
  • asserta(queue(bottom)), call(Goal),
  • asserta(queue(X)),
  • fail.
  • findallCM(_, _, L) -
  • collect(,M), !, L M.

19
(contd)
  • collect(S,L) -
  • getNext(X), !,
  • collect(XS,L).
  • collect(L,L).
  • getNext(S,L) -
  • retract(queue(X)), !,
  • X \ bottom.

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

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)

22
(contd)
  • 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)

23
(contd)
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com