70 likes | 449 Views
SWI Prolog. SWI Prolog is a popular, open, implementation of Prolog that is based upon WAM (Warren Abstract Machine). This implementation is freely available and runs on various Unix systems (including OS X), as well as Windows platforms. (See www.swi-prolog.org ). Using SWI Prolog.
E N D
SWI Prolog SWI Prolog is a popular, open, implementation of Prolog that is based upon WAM (Warren Abstract Machine). This implementation is freely available and runs on various Unix systems (including OS X), as well as Windows platforms. (See www.swi-prolog.org). Using SWI Prolog Startup: swipl Once started, SWI prolog behaves in an interactive (interpreter) style. Prompt: ?- Some Basic Commands To compile a collection of facts & rules: consult(filename). To terminate use of the interpreter: halt. To enter a query: (see Prolog notation for goals) SWI Response to Queries fail means the query (goal) cannot be satisfied true means the query (goal) is true Variable assignments are reported for satisfaction of queries involving variables.
Sample Session family.pl file session 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. > swipl Welcome to SWI Prolog (Version 5.0.10) … ?- consult(family). % family compiled 0.00 sec. 2,584 bytes true. ?- mother(sue, sam). true. ?- mother(mae, sam). fail. ?- mother(M, max). M = kay. ?- parent(P, max). P = kay; P = moe; fail.
Variable Queries When an SWI query contains variables, the Prolog response is to show a complete set of variable bindings that make the query true. …but there may be other sets of bindings that also satisfy the query. Therefore, after each set of bindings the session waits for one of two responses: ; is a request for another set of bindings return (enter) terminates the query.
gcd Revisited Axiom 1:If (N 1), then gcd(N,1) = 1 Axiom 2:If (N 1) and (N < D), then gcd(N,D) = gcd(D,N) Axiom 3:If (N 1) and (D 1) and (N mod D = 0), then gcd(N,D) = D Axiom 4:If (D > 1) and (N > D) and (N mod D 0), then gcd(N,D) = gcd(D, N mod D) gcd.pl file gcd(N,1,1) :- N >= 1. gcd(N,D,Z) :- N >= 1, N < D, gcd(D,N,Z). gcd(N,D,D) :- N >= 1, D >= 1, N mod D=:=0. gcd(N,D,Z) :- D > 1, N > D, NewD is N mod D, gcd(D, NewD, Z).