Presentation is loading. Please wait.

Presentation is loading. Please wait.

As the inference engine performs unification and resolution, it sometimes backtracks in unwanted ways. mother(sue, sam). mother(sue, mae). mother(mia,

Similar presentations


Presentation on theme: "As the inference engine performs unification and resolution, it sometimes backtracks in unwanted ways. mother(sue, sam). mother(sue, mae). mother(mia,"— Presentation transcript:

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

2 The cut is a device in Prolog for blocking backtracking. 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. Syntax: ! A cut is a treated like true fact.

3 SWI Prolog allows an interactive alternative to consult. ?- [user]. |: equal(X,X). |: Syntax: [user] This permits the user to enter facts and rules interactively. (Use ctrl+d to terminate.) I/O ?- write(‘Hi Mom’), X is 3+5, write(X). Syntax: read(X) and write(X) Note that input to read uses |: prompt and terminates with period. Comments ?- [user]. |: % any single line comment can be placed here Syntax: % Misc. not (X) ; + - / * // mod ** == \= = =

4 Prolog lists are denoted with enclosing [ ] [a, b, c, d] note that comas separate consecutive list items Variables can be used to separate a list: [ expr1 | expr2 ] denotes first item expr1 and rest of the list expr2.

5 Example 1 head(List, A) iff A == (car List) Example 2 tail(List, Z) iff Z == (cdr List) Example 3 add(L, A, List2) iff List2 == (cons A L) Anonymous Variables Notation (underscore) _ When a variable’s unified value is unimportant it is better to make it anonymous.

6 Example 4 cat(A,B,C) iff for lists (A, B and C) C is A concatenated with B Example 5 ith(L,N,A) iff A is the nth item in list L Example 6. min(L,M) iff M is the minimum item from list L Example 7 sorted(L) iff L is sorted from least to greatest (Note that this can check for sortedness, but not perform a sort.)


Download ppt "As the inference engine performs unification and resolution, it sometimes backtracks in unwanted ways. mother(sue, sam). mother(sue, mae). mother(mia,"

Similar presentations


Ads by Google