Bratko, Chapter 7: More Built-in Procedures 7.1 Testing the type of terms - var(X) is true if X is an uninstantiated variable - nonvar(X) is true if X is a term other than a variable, or X is an instantiated variable - atom(X) is true if X "stands for" (is or is instantiated as) an atom - integer(X) is true if X stands for an integer - float(X) is true if X stands for a floating point number (no real(X)) - atomic(X) is true if X stands for an integer or atom - examples - cryptarithmetic (fig7.2) 7.2 Constructing and decomposing terms: =.., functor, arg, name - (Term =.. L) - true if L is a list whose first element is the functor of Term and whose remaining elements are the arguments of Term - can construct or decompose terms - examples - operation on arguments for different-sized argument lists - subterm substitution [Fig7.3] - goal construction (call(G) instead of G) - functor(Term,F,N) - true if F is the functor of Term and N is the arity of F - arg(N,Term,A) - true if A is the Nth argument in Term - arguments number left to right from 1 7.3 Various kinds of equality - (X = Y) is true if X and Y match (pattern match) - (X is E) is true if X matches the value of the arithmetic expression E - (E1 =:= E2) is true if the arithmetic expressions E1 and E2 are equal - (E1 =\= E2) is true if the arithmetic expressions E1 and E2 are not equal - (T1 == T2) - true if terms T1 and T2 are identical - even variables must have same names - (T1 \== T2) is true if T1 and T2 are not identical - examples - count (p176) 7.4 Database Manipulation - assert(C) always succeeds and adds clause C to the database - retract(C) deletes a clause that matches C - deletes more than one such clause upon backtracking - examples - assert(man(plato)). - assert( (mortal(X) :- plato(X)) ). - asserta(C) adds C at the beginning of the database - can be used to store solutions - solve(problem1,Solution), asserta(solve(problem1,Solution)). - called memoization, caching, speedup learning - assertz(C) adds C at the end of the database - example 'maketable' on page 180 7.5 Control facilities - review: !, fail, true, not(P) :- P,!,fail., call(P) - repeat: iteration through backtracking - defined: repeat. repeat :- repeat. - use: repeat, set_up, (stopping_condition, !; loop_body, fail). 7.6 bagof, setof, findall - when you want to remember all solutions in a list - typically lost through backtracking - bagof(X,P,L) - true if the list L is all the objects X such that P is satisfied - can use the ^ operator to ignore variables (existentially quantify them) - X ^ P means there exists an X such that P is true (i.e., X ignored) - setof(X,P,L) - true if the list L is all the objects X such that P is satisfied - arguments in L are ordered with no duplicates - findall(X,P,L) [Fig7.4] - true if the list L is all the objects X such that P is satisfied - ignores differences in non-X variables in P (like the ^ operator) - see example starting on 182 - likes(X,Y) - setof((Y,S), setof(X,likes(X,Y),S), SS). HW - Exercise 7.1, page 169. - Exercise 7.3, page 174. - Exercise 7.8, page 184. - hard - Exercise 7.4, page 174. - Exercise 7.5, page 174.