Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Conjunction is a logical operator that connects two logical terms.

Similar presentations


Presentation on theme: "A Conjunction is a logical operator that connects two logical terms."— Presentation transcript:

1 A Conjunction is a logical operator that connects two logical terms.
A conjunction between the two terms will result in the whole expression to evaluate to true if both terms evaluate to true. If either or both terms in the expression evaluate to false, the whole expression evaluates to false. (The word "conjunction" is used mainly in the context of logic and logic programming, it is equivalent to an "AND" in Java, C++, etc..) Conjunction , In Prolog for conjunction, comma ',' is used and it is read as 'and'.

2 Cont… conjunction within a rule body
In Prolog, we are allowed to write rule bodies that contain conjunctions. Remember when we said that a rule head is true if the body is true; if we use a conjunction in the body, the head of the rule is considered true, if and only if all terms in the body are also true. Lets consider the following pet(maggie). pet(bounty). pet(ginger). fast(maggie). fast(ginger). lazy(bounty). big(maggie). big(ginger). friendly(ginger). cat(X):- pet(X), lazy(X). dog(X):- pet(X), fast(X), big(X), friendly(X). - cat(X). X = bounty ; false. - dog(ginger). true. - dog(maggie). conjunctions can only occur at the body of the rule. No conjunctions are allowed at the head of the rule. For example, we cannot have a rule as follows: smart(X),dog(X) :- pet(X), % WRONG

3 conjunction within a query
Conjunctions are not just used within rule bodies, they are also used within queries. For example, we can ask Prolog to find both a Cat and a Dog  from the knowledge base at the same time. ?- dog(X),cat(Y). X = ginger, Y = bounty . The above query, we asked Prolog to unify X with a variable such that dog(X) is true and to unify Y with a constant such that cat(Y) is true. The knowledge base was successful, hence, it returned "X = ginger, Y = bounty". We can also ask for certain facts in conjunction, for example: ?- big(maggie),big(ginger). true. ?- big(maggie),lazy(ginger). false. ?- fast(maggie), pet(bounty), cat(X). X = bounty .

4 Rules in Prolog Let’s augment our program with a new constraint:
eats(sam, dal). eats(josie, samosas). eats(sam, curry). eats(josie, curry). eats(rajiv, burgers). eats(rajiv, dal). compatible(Person1, Person2) :- eats(Person1, Food), eats(Person2, Food). “Person1 and Person2 are compatible if there exists some Food that they both eat.” “One way to satisfy the head of this rule is to satisfy the body.” You type the query: compatible(rajiv, X). Prolog answers: X=sam. Prolog doesn’t report that Person1=rajiv, Person2=sam, Food=dal. These act like local variables in the rule. It already forgot about them. body head means “if” – it’s supposed to look like “” /425 Declarative Methods - J. Eisner

5 Rules Rules allow us to make conditional statements about our world. Each rule can have several variations, called clauses. These clauses give us different choices about how to perform inference about our world. Consider the following 'All men are mortal': We can express this as the following Prolog rule mortal(X) :- human(X). The clause can be read in two ways (called either a declarative or a procedural interpretation) The declarative interpretation is "For a given X, X is mortal if X is human. " The procedural interpretation is "To prove the main goal that X is mortal, prove the sub goal that X is human." Rule 1

6 Rule 2 We know when this happens because Prolog prints yes.
mortal(X) :- human(X). human(socrates). If we now pose the question to Prolog ?- mortal(socrates). The Prolog interpreter would respond as follows: Yes Why was this? Well in order to solve the query ?- mortal(socrates)., we used the rule we saw previously. This said that in order to prove someone mortal, we had to prove them to be human. Thus from the goal Prolog generates the subgoal of showing human(socrates). In the above example we were able to match human(socrates) against the database described at the top of this card In Prolog we say that the sub goal succeeded, and as a result the overall goal succeeded. We know when this happens because Prolog prints yes. Rule 2

7 Rule 3 We can also use variables within queries. For example,
we might wish to see if there is somebody who is mortal. This is done by the following line. ?- mortal(P). The Prolog interpreter responds. P = socrates yes This means that Prolog was able to prove the goal by binding the variable P to Socrates. This was done by again proving someone was mortal by proving the sub goal that they were human. Prolog thus asked if there was any P that was human. This matches against the clause human(Socrates) thereby binding P to Socrates. This binding is then passed back to the parent goal, and the results in the printout we saw above.

8 Sometimes we may wish to specify alternative ways of proving a particular thing.
This we can do by using different rules and facts with the same name. For example, we can represent the sentence 'Something is fun if its a red car or a blue bike or it is ice cream' as follows: fun(X) :- /* an item is fun if */ red(X), /* the item is red */ car(X). /* and it is a car */ fun(X) :- /* or an item is fun if */ blue(X), /* the item is blue */ bike(X). /* and it is a bike */ fun(ice_cream). /* and ice cream is also fun. */ This program says that we have three ways of finding out if something is fun. Something is fun if it is a red and a car or blue and a bike, or if it is ice cream. These three options are represent in Prolog by three clauses of the predicate fun. Just like we saw for pure facts, Prolog will start from the first clause (Beit a rule or fact) of fun and try that. If that does not succeed, we try the next clause. We only fail when we run out of rules or facts to try. Rule 4

9 All identically-named variables within a particular rule (e. g
All identically-named variables within a particular rule (e.g. all occurrences of, say, X in the first fun rule below) are constrained to have one and the same instantiation for each solution to a particular query. Thus variable name scoping is per-individual rule (often called a clause). The same variable name may appear in different clauses of a rule, or rules with different names. Each time it is treated as something specific to its context. A variable X may occur in the program many times with many different bindings. It will only have the same bindings when you tell it to. Rule 5 fun(X) :- red(X), car(X). blue(X), bike(X). Looks to Prolog Like fun(X_1) :- red(X_1), car(X_1). fun(X_2) :- blue(X_2), bike(X_2).

10 Exercise Are the following rules syntactically correct ?
1) a :- b, c, d:- e f. No There is a missing comma between the e and f; also the sign :- should not usually be used within the body of a rule. 2) happy(X):- a , b. Yes Though it is strange that the variable X is not used within the body of the rule. Sicstus would warn you about this singleton variable (a variable only represented once in the clause) when you consulted this program. 3) happy(X):- hasmoney(X) & has_friends(X). No The & symbol is not recognised by prolog, to indicate and use a comma. 4)fun(fish):- blue(betty), bike(yamaha). Yes Syntactically correct - despite its apparent meaningless! Ask your tutor if any of the above answers are not clear. Press the Back button to return to the tutorial. Exercise

11 Syntax of Terms Term Constants Compound Term/Structures Variables Atom
Names an individual Stands for an individual unable to be named when program is written Names an individual that has parts Atom Number likes(john, mary) book(dickens, Z, cricket) f(x) [1, 3, g(a), 7, 9] -(+(15, 17), t) t X Gross_pay Diagnosis _257 _ alpha17 gross_pay john_smith dyspepsia + =/= ’12Q&A’ 1 57 1.618 2.04e-27 -13.6

12 Terms: Constants Constants can either be: Numbers:
integers are the usual form (e.g. 1, 0, -1, etc), but floating-point numbers can also be used (e.g. 3.0E7) Symbolic (non-numeric) constants: always start with a lower case alphabetic character and contain any mixture of letters, digits, and underscores (but no spaces, punctuation, or an initial capital). e.g. abc, big_long_constant, x4_3t). String constants: are anything between single quotes e.g. ‘Like this’. AIPP Lecture 2: Prolog Fundamentals 27/09/04

13 Terms: Variables Variables always start with an upper case alphabetic character or an underscore. Other than the first character they can be made up of any mixture of letters, digits, and underscores. e.g. X, ABC, _89two5, _very_long_variable There are no “types” for variables (or constants) – a variable can take any value. All Prolog variables have a “local” scope: they only keep the same value within a clause; the same variable used outside of a clause does not inherit the value (this would be a “global” scope). AIPP Lecture 2: Prolog Fundamentals 27/09/04

14 SYNTAX FOR VARIABLES Strings of letters, digits, and underscores, starting with uppercase letter X Results Object2B Participant_list _x _335 Lexical scope of variable names is one clause Underscore stands for an anonymous variable Each appearance of underscore: another anon. var.

15 STRUCTURES Structures are objects that have several components
For example: dates are structured objects with three components Date 17 June 2006 can be represented by term: date( 17, june, 2006) functor arguments An argument can be any object, also a structure

16 TREE REPRESENTATION OF STRUCTURES
Often, structures are pictured as trees date( 17, june, 2006) date june

17 somerelationship(a,b,c,[1,2,3])
Structures To create a single data element from a collection of related terms we use a structure. A structure is constructed from a functor (a constant symbol) and one of more components. somerelationship(a,b,c,[1,2,3]) The components can be of any type: atoms, integers, variables, or structures. As functors are treated as data objects just like constants they can be unified with variables functor |?- X = date(04,10,04). X = date(04,10,04)? yes AIPP Lecture 3: Recursion, Structures, and Lists 30/09/04

18 Structure unification
2 structures will unify if the functors are the same, they have the same number of components, and all the components unify. | ?- person(Nm,london,Age) = person(bob,london,48). Nm = bob, Age = 48? yes | ?- person(Someone,_,45) = person(harry,dundee,45). Someone = harry ? (A plain underscore ‘_’ is not bound to any value. By using it you are telling Prolog to ignore this argument and not report it.) AIPP Lecture 3: Recursion, Structures, and Lists 30/09/04

19 Structure unification (2)
A structure may also have another structure as a component. |?-addr(flat(4),street(‘Home Str.’),postcode(eh8_9lw)) = addr(flat(Z),Yy,postcode(Xxx)). Z = 4, Yy = street(‘Home Str.’), Xxx = eh8_9lw ? yes Unification of nested structures works recursively: first it unifies the entire structure, then it tries to unify the nested structures. Remember to close brackets! Reported variables are ordered according to number of characters in the variable name. AIPP Lecture 3: Recursion, Structures, and Lists 30/09/04

20 Structures = facts? The syntax of structures and facts is identical but: Structures are not facts as they are not stored in the database as being true (followed by a period ‘.’); Structures are generally just used to group data; Functors do not have to match predicate names. However predicates can be stored as structures command(X):- X. | ?- X = write(‘Passing a command’), command(X). Passing a command X = write('Passing a command') ? yes By instantiating a variable with a structure which is also a predicate you can pass commands. AIPP Lecture 3: Recursion, Structures, and Lists 30/09/04


Download ppt "A Conjunction is a logical operator that connects two logical terms."

Similar presentations


Ads by Google