Top Banner
Prolog Built-in predicates Dr. M. A. Pasha
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Prolog2 (1)

Prolog Built-in predicates

Dr. M. A. Pasha

Page 2: Prolog2 (1)

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.

Page 3: Prolog2 (1)

Universals

• true Always succeeds as a goal.

• fail Always fails as a goal.

Page 4: Prolog2 (1)

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

Page 5: Prolog2 (1)

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.

Page 6: Prolog2 (1)

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

Page 7: Prolog2 (1)

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

Page 8: Prolog2 (1)

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

Page 9: Prolog2 (1)

Testing for variables

• ground(G) Tests whether G has unbound logical variables.

• var(X) Tests whether X is bound to a Prolog variable.

Page 10: Prolog2 (1)

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.

Page 11: Prolog2 (1)

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.

Page 12: Prolog2 (1)

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.

Page 13: Prolog2 (1)

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.

Page 14: Prolog2 (1)

Negation as failure

• not(Q), \+Q Negation-as-failure, as if defined by:

• not(Q) :- call(Q), !, fail. not(Q).

Page 15: Prolog2 (1)

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)

Page 16: Prolog2 (1)

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

Page 17: Prolog2 (1)

Prolog operators

Operator 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

Page 18: Prolog2 (1)

Prolog operators

• :- xfx, fx• ?- fx • ; xfy • , xfy • not fy • is, =.. , < xfx • +, - yfx, fx • *, / yfx • ^ xfy

Page 19: Prolog2 (1)

• :- 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').

Page 20: Prolog2 (1)

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.

Page 21: Prolog2 (1)

Exampleson(X,Y):-

father(Y,X),male(X).

daughter(X, Y):- father(Y, X),female(X).

grandfather(X, Y):- father(X, Z), father(Z,Y).

father(afzal, bilal).father(afzal, zubair).

father(nazir, afzal).father(afzal, humara).father(afzal, sumara).

female(sumara).female(humara).

?- findall(X, father(X,Y), Bag).Bag = [afzal, afzal, nazir,

afzal, afzal].

?- findall(Y, father(X,Y), Bag).Bag = [bilal, zubair, afzal,

humara, sumara].

Page 22: Prolog2 (1)

?- bagof(Z,p(X,Y,Z),Bag). X = 1, Y = 3, Bag = [5] ;X = 2,Y = 4,Bag = [1] ;X = 3,Y = 5,Bag = [2] ;X = 4,Y = 3,Bag = [1] ;X = 5,Y = 2,Bag = [4].

?- findall(Z,p(X,Y,Z),Bag).Bag = [5, 1, 2, 1, 4].

The predicates bagof and setof yield collections for individual bindings of the free variables in the goal. setof yields a sorted version of the collection without duplicates.

p(1,3,5).p(2,4,1).p(3,5,2).p(4,3,1).p(5,2,4).

Page 23: Prolog2 (1)

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.

Page 24: Prolog2 (1)

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.

Page 25: Prolog2 (1)

?- bagof(Z,(p(X,Y,Z),Z>5),Bag). False. ?- findall(Z,(p(X,Y,Z),Z>5),Bag). Bag = []

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 Z's such that there exists an X and there exists a Y such that p(X,Y,Z)".

findall acts like bagof with all free variables automatically existentially quantified. In addition findall returns an empty list [] there is no goal satisfaction, whereas bagof fails.

?-setof(Z,X^Y^p(X,Y,Z),Bag).Bag = [1, 2, 4, 5].

Page 26: Prolog2 (1)

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

Listing and Debugging Predicates