Prolog2 (1)

550 views

Published on

  • Be the first to comment

Prolog2 (1)

  1. 1. Prolog Built-in predicates Dr. M. A. Pasha
  2. 2. Utility goals• help(S) S should be a symbolic atom, e.g., help(assert).• halt Stops Prolog, resume operating system.• trace, notrace Turns trace on and off, respectively.
  3. 3. Universals• true Always succeeds as a goal.• fail Always fails as a goal.
  4. 4. Loading Prolog programs• consult(F) Loads program from the file F. F should be bound to file designator expression, e.g., F=/home/user/prolog/sample.pl, depending on the file system.• reconsult(F) Like consult except that each predicate already defined has its definition replaced by the new definition being loaded.• [F1,F2,...] Bracket notation, meaning consult F1, then consult F2, then ...
  5. 5. Testing types• var(Term) succeeds if Term is currently uninstantiated (which therefore has not been bound to anything, except possibly another uninstantiated variable).• nonvar(Term) succeeds if Term is currently instantiated (opposite of var/1).• atom(Term) succeeds if Term is currently instantiated to an atom.• integer(Term) succeeds if Term is currently instantiated to an integer.• float(Term) succeeds if Term is currently instantiated to a floating point number.• number(Term) succeeds if Term is currently instantiated to an integer or a floating point number.• atomic(Term) succeeds if Term is currently instantiated to an atom, an integer or a floating point number.• string(Term) Tests whether Term is bound to a string.
  6. 6. Arithmetic Predicates• X is E Evaluate E and unify the result with X.• X + Y When evaluated, yields the sum of X and Y.• X - Y When evaluated, yields the difference of X and Y.• X * Y When evaluated, yields the product of X and Y.• X / Y When evaluated, yields the quotient of X and Y.• X mod Y When evaluated, yields the remainder of X divided by Y.• X =:= Y Evaluate X and Y and compare them for equality.• X == Y Evaluate X and Y and succeed if they are not equal. ...and similarly for >, <, >=, =<.
  7. 7. ?- X=2,Y=3,Z is X+Y.X = 2,Y = 3,Z = 5.?- X=3, Y=5, X =:= Y .False?- X=3, Y=5, X >= Y.false.X=3, Y=5, X =< Y.X = 3,Y = 5.
  8. 8. Equality of Prolog expressions• X = Y, X =Y Tests whether X and Y can be unified, or cannot, respectively. For example, ?- [X,Y|R] = [a,b,c]. X = a, Y = b, R = [c] ?- [X,Y,Z] = [a,b]. No• X ==Y, X == Y Tests whether X and Y are currently co-bound, i.e., have been bound to or share same value, or not, respectively. For example, ?- X = 3, Y = 1 * 3, X == Y. no ?- X = a, [Y|_]= [a,b,c], X == Y. X = a, Y = a
  9. 9. Testing for variables• ground(G) Tests whether G has unbound logical variables.• var(X) Tests whether X is bound to a Prolog variable.
  10. 10. Database Manipulation P• assert(X) Add X to the database. For syntactic reasons, if X is not a base clause, use assert((X)).• asserta(X) Add X to the database in front of other clauses of this predicate.• assertz(X) Add X to the database after other clauses of this predicate.• retract(X) Remove X from the database. For syntactic reasons, if X is not a base clause, use retract((X)).• abolish(F,A) Remove all clauses with functor F and arity A from the database.• clause(X,V) Find a clause in the database whose head (left hand side) matches X and whose body (right hand side) matches V. To find a base clause, use true for V.
  11. 11. Assert and Retract• asserta(C) Assert clause C into database above other clauses with the same key predicate. The key predicate of a clause is the first predicate encountered when the clause is read from left to right.• assertz(C), assert(C) Assert clause C into database below other clauses with the same key predicate.• retract(C) Retract C from the database. C must be sufficiently instantiated to determine the predicate key.
  12. 12. Binding a logical variable to a numeric value• X is E Binds logical variable V to the numerical value of E. The expression E must either be a number or be a number-valued expression, conventionally parenthesized,• ?- X is 22, Y is X * 3, Z is sqrt(Y). X = 22 . Y = 66 . Z = 8.12404 .• ?- X is sin(45). X = 0.8509035245341184.
  13. 13. Control Predicates• X ; Y X or Y. Try X first; if it fails (possibly after being backtracked into), try Y.• (X -> Y) If X, then try Y, otherwise fail. Y will not be backtracked into.• (X -> Y ; Z) If X, then try Y, else try Z. X will not be backtracked into.• not X (Sometimes written +X or not(X)) Succeed only when X fails.• true Succeed once, but fail when backtracked into.• repeat Always succeed, even when backtracked into.• fail Never succeed.• ! (Pronounced ``cut".) Acts like true, but cannot be backtracked past, and prevents any other clauses of the predicate it occurs in from being tried.• abort Return immediately to the top-level Prolog prompt.
  14. 14. Negation as failure• not(Q), +Q Negation-as-failure, as if defined by:• not(Q) :- call(Q), !, fail. not(Q).
  15. 15. Prolog terms and clauses as data• name(A,L) Convert between atom and list.• ?- name(pasha, P). P = [112, 97, 115, 104, 97].• ?- parent(a,X) = .. L. L = [parent, a, _X001]• ?- P=..[parent,pasha, abdul]. P= parent(pasha,abdul)
  16. 16. Associativity• associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses.• Operators may be left-associative (meaning the operations are grouped from the left), right-associative (meaning the operations are grouped from the right) or non-associative (meaning there is no defined grouping).
  17. 17. Prolog operatorsOperator Type• xfx infix nonassociative xfy infix right-associative yfx infix left-associative fx prefix nonassociative fy prefix right-associative xf postfix nonassociative yf postfix left-associative
  18. 18. Prolog operators• :- xfx, fx• ?- fx• ; xfy• , xfy• not fy• is, =.. , < xfx• +, - yfx, fx• *, / yfx• ^ xfy
  19. 19. • :- op(P,T,O). Declare an operator symbol. For example, with source program ...• :- op(500,xfx,has_color). a has_color red. b has_color blue.Then ...• ?- b has_color C. C = red ?- What has_color red. What = aP is precedence, an integer. Larger P has less precedence (ability to group). Precedence number values for built-ins depend upon the actual Prolog system. User needs to find out what these values are. (See the reference materials or use the help facility with keyword operator).
  20. 20. Finding all answers• findall(Things,GoalExpression,Bag) Compute all Things which satisfy the GoalExpresssion and collect them in the list Bag. If the GoalExpression fails, Bag will be the empty list []. findall treats all variables in GoalExpression as if they are existentially quantified.• bagof(Things,GoalExpression,Bag) Compute all Things which satisfy the GoalExpresssion and collect them in the list Bag. bagof fails if GoalExpression fails. Free variables in GoalExpression could be bound, yielding many bags.• setof(Things,GoalExpression,Bag) Compute all Things which satisfy the GoalExpresssion and collect them in the list Bag. Similar to bagof except that Bag will not contain duplicates and it will be sorted.
  21. 21. Exampleson(X,Y):- ?- findall(X, father(X,Y), Bag). father(Y,X), male(X). Bag = [afzal, afzal, nazir,daughter(X, Y):- afzal, afzal]. father(Y, X), female(X).grandfather(X, Y):- father(X, Z), ?- findall(Y, father(X,Y), Bag). father(Z,Y). Bag = [bilal, zubair, afzal,father(afzal, bilal).father(afzal, zubair). humara, sumara].father(nazir, afzal).father(afzal, humara).father(afzal, sumara).female(sumara).female(humara).
  22. 22. p(1,3,5). p(2,4,1). p(3,5,2). p(4,3,1). p(5,2,4).?- bagof(Z,p(X,Y,Z),Bag).X = 1, Y = 3, Bag = [5] ; The predicates bagof and setof yieldX = 2,Y = 4,Bag = [1] ; collections for individual bindings of theX = 3,Y = 5,Bag = [2] ; free variables in the goal.X = 4,Y = 3,Bag = [1] ; setof yields a sorted version of theX = 5,Y = 2,Bag = [4]. collection without duplicates.?- findall(Z,p(X,Y,Z),Bag).Bag = [5, 1, 2, 1, 4].
  23. 23. Output Predicates• write(X) Write the single value X to the current output file.• writeq(X) Write X with quotes as needed so it can be read in again.• tab(N) Write N blanks to the current output file.• nl Write a newline to the current output file.• put(X) Write the character whose ASCII value is X to the current output file.• tell(File) Open File as the current output file.• told Close the current output file.
  24. 24. Input predicates• read(X) Read one clause from the current input and unify it with X. If there is no further input, X is unified with end_of_file.• get(X) Read one printing character from the current input file and unify the ASCII code of that character (an integer) with X.• get0(X) Read one character from the current input file and unify the ASCII code of that character with X.• see(File) Open File as the current input file.• seen Close the current input file.
  25. 25. To avoid binding variables, use an existential quantifier expression. For example the goal bagof(Z,X^Y^p(X,Y,Z),Bag) asks for "the Bag of Zs such that there exists an X and there exists a Y such that p(X,Y,Z)". ?-setof(Z,X^Y^p(X,Y,Z),Bag). Bag = [1, 2, 4, 5].findall acts like bagof with all free variables automaticallyexistentially quantified. In addition findall returns an empty list []there is no goal satisfaction, whereas bagof fails. ?- bagof(Z,(p(X,Y,Z),Z>5),Bag). False. ?- findall(Z,(p(X,Y,Z),Z>5),Bag). Bag = []
  26. 26. Listing and Debugging Predicates• listing(P) Display predicate P. P may be a predicate name, a structure of the form Name/Arity, or a bracked list of the above.• trace Turn on tracing.• notrace Turn off tracing.• spy P Turn on tracing when predicate P is called. P may be a predicate name, a structure of the form Name/Arity, or a non-bracked list of the above.• nospy P Turn off spying for P.• nospyall Turn off all spypoints.• Debug Enable spypoints (allow them to initiate tracing.).• Nodebug Disable spypoints (without removing them).

×