2. All in prolog
Instance and queries
Anonymous Variable
Arithmetic in Prolog
Review A Family Tree
3. Term
Constant Compound Term Variable
Names an individual Names an individual Stands for an individual
that has parts unable to be named when
program is written
Atom Number
likes(john, mary) X
alpha17 0 book(dickens, Z, cricket) Gross_pay
gross_pay 1 f(x) Diagnosis
john_smith 57 [1, 3, g(a), 7, 9] _257
dyspepsia 1.618 -(+(15, 17), t) _
+ 2.04e-27 15 + 17 - t
=/= -13.6
’12Q&A’
11. arithmetic comparisons automatically evaluate expressions
X =:= Y X and Y must both be arithmetic expressions (no variables)
X == Y
X > Y ?- 12 =:= 6+6.
X >= Y Yes
X < Y
X =< Y ?- X =:= 6+6.
ERROR: Arguments are not sufficiently instantiated
Expr1 =:= Expr2
True if expression Expr1 evaluates to a
number equal to Expr2.
12. it is sometimes convenient to write functors/predicates as operators
predefined: +(2, 3) 2 + 3
user defined? likes(dave, cubs) dave likes cubs
13. Find:
pam tom grandfather
grandmother
grandson
bob liz
granddaughter
brother
ann pat
sister
jim
14. Recursive Rules: Predecessor Relation
Some X is an indirect predecessor of some Z if there is a
partnership chain of people between X and Z.
X is a direct predecessor of Z
X is an indirect predecessor of
predecessor(X, Z) :- X Z predecessor(X, Z) :-
parent(X, Z). parent(X, Y1),
parent
parent(Y1, Y2),
parent(Y2, Z).
X Y1
parent predecessor parent predecessor
Z Y2
parent
Z
14
15. Recursive Rules: Predecessor Relation Recursive
predecessor(X, Z) :-
Predecessor of any depth. parent(X, Y),
predecessor(Y, Z).
Define predecessor relation in terms of itself.
For all X and Z
X is a predecessor of Z if
there is a Y such that
(1) X is a parent of Y and
(2) Y is a predecessor of Z.
predecessor(X, Z) :-
parent(X, Z).
15