70 likes | 427 Views
excessive backtracking. As the inference engine performs unification and resolution, it sometimes backtracks in unwanted ways. mother(sue, sam). mother(sue, mae). mother(mia, sue). mother(kay, jim). mother(kay, max). mother(mia, joe). mother(mia, bob). father(jim, sam).
E N D
excessive backtracking As the inference engine performs unification and resolution, it sometimes backtracks in unwanted ways. mother(sue, sam). mother(sue, mae). mother(mia, sue). mother(kay, jim). mother(kay, max). mother(mia, joe). mother(mia, bob). father(jim, sam). father(jim, mae). father(ed, sue). father(moe, jim). father(moe, max). father(ed, joe). father(ed, bob). child(C, P) :- parent(P, C). parent(P, C) :- mother(P, C). parent(P, C) :- father(P, C). siblings(A,B) :- parent(P,A), parent(P,B), A \= B. Consider the following query: ?-siblings(A,B).
the cut The cut is a device in Prolog for blocking backtracking. Syntax: ! mother(sue, sam). mother(sue, mae). mother(mia, sue). mother(kay, jim). mother(kay, max). mother(mia, joe). mother(mia, bob). father(jim, sam). father(jim, mae). father(ed, sue). father(moe, jim). father(moe, max). father(ed, joe). father(ed, bob). child(C, P) :- parent(P, C). parent(P, C) :- !, mother(P, C). parent(P, C) :- !, father(P, C). siblings(A,B) :- parent(P,A), parent(P,B), A \= B. A cut is a treated like true fact.
Other Prolog-isms SWI Prolog allows an interactive alternative to consult. Syntax: [user] This permits the user to enter facts and rules interactively. (Use ctrl+d to terminate.) ?- [user]. |: equal(X,X). |: <ctrl-d> I/O Syntax: read(X) and write(X) Note that input to read uses |: prompt and terminates with period. ?- write(‘Hi Mom’),X is 3+5, write(X). Comments Syntax: % ?- [user]. |: % any single line comment can be placed here Misc. not (X) ; + - / * // mod ** == \= < > =< >=
note that comas separate consecutive list items denotes first item expr1 and rest of the list expr2. List - Prolog Data Structure Prolog lists are denoted with enclosing [ ] [a, b, c, d] Variables can be used to separate a list: [expr1 | expr2 ]
Example 1 head(List, A) iff A == (car List) head( [A|B], A ). Example 2 tail(List, Z) iff Z == (cdr List) tail( [A|Z], Z ). Example 3 add(L, A, List2) iff List2 == (cons A L) add(L, A, [A|L]). Anonymous Variables Notation (underscore) _ When a variable’s unified value is unimportant it is better to make it anonymous. head( [A|_], A ). tail( [_|Z], Z ).
Example 4 cat(A,B,C) iff for lists (A, B and C) C is A concatenated with B cat([ ], L, L). cat([X | L1], L2, [X | L3]) :- cat(L1, L2, L3). Example 5 ith(L,N,A) iff A is the nth item in list L ith([X | L1], 1, X). ith([ _ | L1], N, A) :- N>1, Nless1 is N-1, ith(L1, Nless1, A). Example 6 min(L,M) iff M is the minimum item from list L min([X], X). min([X | L], X) :- min(L,Z), X=<Z. min([X | L], Z) :- min(L,Z), Z=<X. Example 7 isSorted(L) iff L is sorted from least to greatest isSorted( [] ). isSorted( [ _ ] ). isSorted( [X | [Y | L]]) :- X=<Y, isSorted([Y | L] ). (Note that this can check for sortedness, but not perform a sort.)