CHA2555 Week2 Practical: Lee McCluskey First term:
Artform Research Group Prolog Overview… parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). -- a Prolog program is a sequence of CLAUSES, each ending in ‘.’ -- each clause may be either a FACT or a RULE. ::=. ::= ( ) ::= | | ( ) ::= :-.
Artform Research Group Prolog Structure parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). A rule contains a HEAD and RULE BODY separated by the ':-' symbol. Each head must be a predicate and each rule body is a sequence of predicates separated by commas Rules can be read two ways: DECLARATIVELY or PROCEDURALLY. ** X is the grandad of Y is true if there exists a Z such that X is the father of Z and Z is the parent of Y. (declarative) ** To prove X is the grandad of Y, first prove X is the father of some Z, then prove this Z is the parent of Y. (procedural)
Artform Research Group to execute Prolog programs parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). To execute/activate/run a program a GOAL is typed to the Prolog interpreter. It responds with no (failure) or yes (success) together with the successful instantiations of variables that appeared in the goal. ?- grandad(jacob,frank). -yes ?- grandad(X,frank). X = jacob -yes
Artform Research Group how Prolog works…. parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). To solve a goal such as: ?-grandad(jacob,frank). Prolog tries to MATCH this goal with facts and rule heads in the program, starting from the top and working down - search. -- constants/predicate symbols must match with constants/predicate symbols -- variables can match with any term When a variable MATCHES with a term we say it is instantiated.
Artform Research Group how Prolog works…. parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). To solve a goal such as: ?-grandad(jacob,frank). When a rule head is matched, matching variables become instantiated throughout the rule, and the TAIL of the rule is executed as if it were a GOAL. So Prolog executes ‘father(jacob,Z),parent(Z,frank)’ next….
Artform Research Group Practicals Exs from Notes.. Matching n a. father(X,esau) with father(isaac,Z). n b. cost(X,Y,40) with cost(U,V,60). n c. sentence(X,predicate(Y,object(Z))) with n sentence(subject(bill),Predicate) n d. sentence(X,predicate(Z)) with n sentence(subject(bill),predicate(verb(hit),object(bill)))
Artform Research Group Practicals Exs from Notes.. Backtracking furry(tabby). furry(leo). small(tabby). has_whiskers(leo). has_big_teeth(leo). has_whiskers(tabby). cat(X) :- has_whiskers(X), furry(X). timid(X) :- cat(X), small(X). If the query '?-timid(X)' was typed, the second rule's first predicate 'cat(X)' would succeed with X = leo; this, however, would cause the second predicate to fail and control would backtrack to 'cat(X)'. The last successful match is then failed: this is the match of furry(leo) of the 'cat' rule with the identical fact in the database. furry(leo) fails completely since it can find no alternative match; consequently backtracking takes place in the 'cat' rule which eventually results in a success with X = tabby. The predicate 'small(X)' with this binding then succeeds.
Artform Research Group Summary Prolog – Matching and Backtracking Continue with the online exercises. Make sure you understand Prolog’s procedural method (up to the end of section 2) Advance to section 3 and beyond…