Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS2326 Week2: Logic and Prolog Lee McCluskey First term:

Similar presentations


Presentation on theme: "CIS2326 Week2: Logic and Prolog Lee McCluskey First term:"— Presentation transcript:

1 CIS2326 Week2: Logic and Prolog Lee McCluskey Email lee@hud.ac.uklee@hud.ac.uk First term: http://scom.hud.ac.uk/scomtlm/cha2555/

2 Artform Research Group Summary This week we will look at the basic structure and operation of Prolog, and consider its connection to Logic,

3 Artform Research Group Logics – a popular form of knowledge representation All men are mortalEnglish Socrates is a man Therefore Socrates is mortal Ax Man(x) => Mortal(x)First-Order Logic Man(Socrates) |= Mortal(Socrates) mortal(X) :- man(X).Prolog man(socrates). QUERY- mortal(socrates). PROLOG RETURNS - Yes.

4 Artform Research Group Logics I n this course we will consider “Classical Logic” - where propositions (or “formulae) can only be 2 valued (true or false) There are other logic families for representing/dealing with all kinds of human knowledge.. - Time - Belief - Uncertainty / Fuzziness - Possibility and Certainty - Change - Action They all stem from Classical Logic

5 Artform Research Group Rest of lecture: Prolog 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. – lowercase parameters are constants – uppercase parameters are vaiables

6 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 “rough” Syntax Definition for rules and facts in Prolog is as follows: ::=. ::= ( ) ::= | | ( ) ::= :-.

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

8 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

9 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.

10 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….

11 Artform Research Group Practicals Exs from Notes.. Matching a. father(X,esau) with father(isaac,Z). b. cost(X,Y,40) with cost(U,V,60). c. sentence(X,predicate(Y,object(Z))) with sentence(subject(bill),Predicate) d. sentence(X,predicate(Z)) with sentence(subject(bill),predicate(verb(hit),object(bill)))

12 Artform Research Group Example 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.

13 Artform Research Group Summary Prolog programs are lists of facts and rules. To execute a program, you type a query. The Prolog interpreter tries to execute (prove) the query using the rules like procedures, and using matching on variables, and backtracking when a rule fails. Practical: 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…


Download ppt "CIS2326 Week2: Logic and Prolog Lee McCluskey First term:"

Similar presentations


Ads by Google