Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCP1211-Logic Programming Formal Syntax and Semantics of Logic Programs Faculty of Information Technology Multimedia University.

Similar presentations


Presentation on theme: "TCP1211-Logic Programming Formal Syntax and Semantics of Logic Programs Faculty of Information Technology Multimedia University."— Presentation transcript:

1

2 TCP1211-Logic Programming Formal Syntax and Semantics of Logic Programs Faculty of Information Technology Multimedia University

3 TCP1211-Logic Programming Lecture Outline Syntax of Logic programs. Syntax of Logic programs. Propositional Logic. Propositional Logic. Predicate Logic. (and why predicate logic?) Predicate Logic. (and why predicate logic?) Semantics of a Logic Program. Semantics of a Logic Program.

4 TCP1211-Logic Programming Three kinds of symbols likes(mother(X),X) :- good(X). woman(mother(Y)). woman(ann). good(husband(ann)). Relation Symbols (Predicate Symbols) : likes, good, woman. Name a relation between data objects. Begin with a lower-case letter. The number of arguments called arity: likes/2, good/1, woman/1.

5 TCP1211-Logic Programming likes(mother(X),X) :- good(X). woman(mother(Y)). woman(ann). good(husband(ann)). Function Symbols: mother, husband. Construct data objects. Only used in writing the arguments of a formula. Function symbols

6 TCP1211-Logic Programming Variables likes(mother(X),X) :- good(X). woman(mother(Y)). woman(ann). good(husband(ann)). Variables: X, Y. Represent unspecified objects. Start with an upper-case letter.

7 TCP1211-Logic Programming List of new terms so far… Predicate/relation symbol Predicate/relation symbol Function symbol Function symbol What next  What next  Formal syntax of Logic Programs Formal syntax of Logic Programs Formal definition of a clause Formal definition of a clause What is a literal ? What is a literal ? Formal definition of a term. Formal definition of a term. The alphabet of a program. The alphabet of a program.

8 TCP1211-Logic Programming Formal Syntax of Logic Programs likes(mother(X),X) :- good(X). woman(mother(X)). woman(ann). good(husband(ann)). A program is a set of clauses. From a logical point of view, the order in which these clauses are written has no importance. Clauses Syntax of a Program

9 TCP1211-Logic Programming Syntax of a clause likes(ann, X) :- toy(X), plays(ann, X). A clause is a formula: P :- Q 1,…, Q n. P is a literal called the head of the clause, and Q 1,…,Q n are literals that together form the body of the clause. In the case n = 0, there are no literals in the body; such a clause is written P :-. Literals HeadBody likes(ann, snoopy) :-. Or Simply likes(ann, snoopy). No Literals

10 TCP1211-Logic Programming Syntax of a Literal likes(mother(X), X) A literal is a formula: p(t 1,…,t k ) where p is a relation symbol of arity k and (t 1,…,t k ) are k terms. In the case k = 0, the literal is written simply as p. 2 Terms (Arity 2) Relation Symbol likes() Or Simply likes 0 Term (Arity 0)

11 TCP1211-Logic Programming Syntax of a Term likes(mother(husband(X)), X) ---- A Literal A term is either a variable like X, or it is a compound term f(t 1,…, t k ) where f is a function symbol of arity k, and (t 1,…, t k ) are k smaller terms. A function symbol with no arguments is a constant, written simply as f. 2 Terms Variable (Simple Term) Compound Term Function Symbol (Arity 1) Term of Term (Also Compound Term) Term of Term of Term (Variable, A Simple Term)

12 TCP1211-Logic Programming An Alphabet of a Program likes(mother(X),X) :- good(X). woman(mother(X)). woman(ann). good(husband(ann)). An alphabet of a logic program is a set of relation and function symbols used in the program, together with their arities. We say a program T is well-formed with respect to an alphabet L if all the relation and function symbols used in T are drawn from L and used with the correct arity. Alphabet = {likes/2, woman/1, good/1; mother/1, husband/1, ann/0} Relation Symbols Function Symbols Semicolon

13 TCP1211-Logic Programming Propositional Logic "It's raining"True or False "I'll get wet"True or False ”He is crying"True or False "I won't give him an ice-cream"True or False Propositional logic may be viewed as a representation language which allows us to express and reason with statements that are either true or false. Examples of such statements are: Logical operators such as , , , , etc. are often used to combined statements into compound statements. The truth value of a compound statement is dependent on the truth value of its constituents.

14 TCP1211-Logic Programming Implication (  ) A particularly important operator in mathematical logic is implication (  ), which expresses the idea that one condition always is accompanied by another. We write P  Q (P implies Q) to say “whenever P holds, Q also holds”. P = It's rainingQ = I'll get wet P  Q IF It's raining THEN I'll get wet

15 TCP1211-Logic Programming The Meaning of Implication (  ) using Truth Table QUESTION: Is the statement P  Q true (valid)? P = You are cryingQ = I won’t give you an icecream P  Q IF You are crying THEN I won’t give you an icecream ANSWER: The statement can be either true or false depending on the cases whether P and Q are true or false. P Q P  Q T T T T F F F T T F F T 4 Possible Cases Suppose a parent says to a child “If you are crying, I won’t give you an icecream”, the child stops crying, and doesn’t get any icecream. In logic, the parent did not tell a lie.

16 TCP1211-Logic Programming P = You are cryingQ = I won’t give you an icecream P  Q IF You are crying THEN I won’t give you an icecream P Q P  Q T T T T F F F T T F F T Only 3 Possible Cases A logical implication, P  Q (P logically implies Q), is an assertion that an implication P  Q (P implies Q) is always true, and thus eliminating the possibility of P to be true and Q to be false. P  Q This Case Is Not Possible Now

17 TCP1211-Logic Programming Predicate Logic "There is a person whom Sue likes"True or False "Ann likes all the animals"True or False A limitation of propositional logic is the impossibility of expressing general statements concerning similar cases. Predicate logic is more expressive than propositional logic, and such general statements can be specified in its language through the use of quantifiers. Examples of such statements are:  (X)[person(X)  likes(sue, X)]Existentially Quantified  (X)[animal(X)  likes(ann, X)]Universally Quantified Mathematically,

18 TCP1211-Logic Programming "There is a person whom Sue likes" "Ann likes all the animals"  (X) [person(X)  likes(sue, X)] "There exists X such that X is a person and Sue likes X"  (X) [animal(X)  likes(sue, X)] "For all X such that X is an animal and Ann likes X" Existential Quantifier Universal Quantifier

19 TCP1211-Logic Programming Variable Quantification in Prolog In Prolog, a variable is either existentially or universally quantified depending on the context in which it is used. In a literal which is used as a goal or sub goal: "There exists X such that X is a person and Sue likes X" ?- person(X), likes(sue, X). % Query/Initial Goal happy(sue) :- person(X), good(X). % Rule Body/Subsequent Goal "Sue is happy if there exists X such that X is a person and X is good" “In Both Cases Variable X Is Existentially Quantified.”

20 TCP1211-Logic Programming In a literal which is used as a rule head: "For all X such that Sue likes X if X is an animal" likes(sue, X) :- animal(X). % Rule Head likes(mother(X), X). % Fact/Rule Without Body "For all X such that mother of X likes X" “In Both Cases Variable X Is Universally Quantified.”

21 TCP1211-Logic Programming "For all X such that Sue likes X if X is a toy, and There exists Y such that Y plays with X" likes(sue, X) :- toy(X), person(Y), plays(Y, X). X is Universally Quantified Y is Existentially Quantified "Sue likes every toy some person plays with"

22 TCP1211-Logic Programming The Semantics of a Prolog Clause Semantically, a clause in a Prolog program, written as Q :- P. is exactly a logical implication P  Q. A unit clause Q :-. can be considered as True  Q. likes(sue, snoopy) :- toy(snoopy), plays(sue, snoopy). %Rule toy(snoopy)  plays(sue, snoopy)  likes(sue, snoopy) Means likes(sue, snoopy). %Fact Means True  likes(sue, snoopy)

23 TCP1211-Logic Programming The Semantics of a Prolog Goal A goal in a Prolog program, written as :- P. can be considered as P  True. ?- toy(snoopy), plays(sue, snoopy). %Query/Goal toy(snoopy)  plays(sue, snoopy)  True Means

24 TCP1211-Logic Programming The Semantics of a Prolog Program A Prolog program is regarded as the conjunction of its clauses, and its logical semantics consist of the truth of its clauses. Means likes(sue,snoopy) :- toy(snoopy), plays(sue,snoopy). toy(snoopy). plays(sue,snoopy). toy(snoopy)  plays(sue, snoopy)  likes(sue, snoopy)  True  toy(snoopy)  True  plays(sue, snoopy)

25 TCP1211-Logic Programming What is an interpretation? i(k, c(m)). A clause in a logic program can be either true or false depending on the interpretation given to the program. This interpretation is to assign meaning to the symbols used in the program, as well as to define the domain of the variables. is Kuala Lumpur capital Malaysia "Kuala Lumpur is the capital of Malaysia" This unit clause will be a true fact under the given interpretation.

26 TCP1211-Logic Programming i(k, c(m)). If the logic program is given a new interpretation now: is located Kuala Lumpur centre of Melaka "Kuala Lumpur is located at the centre of Melaka" This unit clause will not be a true fact under the new interpretation. Same unit clause, true previously but false now !

27 TCP1211-Logic Programming f(N,F) :- N = 0, F = 1. f(N,F) :- N > 0, N1 is N-1, f(N1,F1), F is N*F1. A Prolog program which contains two non-unit clauses (rules): If we interpret the relation symbol f as the factorial function then the program clauses are all true when interpreted over the domain of integers. If we interpret the relation symbol f as the fibonacci function or interpret it over the domain of characters then the program clauses are all false. An interpretation that makes all of the program clauses true is called a model of the program. Relation Symbol : fVariables : N, N1, F, F1

28 TCP1211-Logic Programming Formal Semantics (cont.) Interpretation I of a logic program P requires that there be a domain set D, and the following assignments: (a) Each constant symbol of P is assigned to an element of D; (b) Each function symbol of P is assigned to some particular function over domain D having the same arity; (c) Each relation symbol of P is assigned to some particular relation on D having the same arity. l(m(a), a). % A one-clause program a Ann constant, m mother of function, l likes relation I = I(a) = Ann I(m) = mother of I(l) = likes No variables involved !

29 TCP1211-Logic Programming l(m(X), X) :- g(X, Y). % A one-clause program m mother of function, l likes relation, g good in relation I = X a person, Y a subject A = I(m) = mother of I(l) = likes I(g) = good in A(X)  { persons } A(Y)  { subjects } Additional requirements must hold when there are variables used in a logic program. An assignment of variables A is a function from the variables of P to the domain D.

30 TCP1211-Logic Programming Now we can formally define the truth of logic clauses with respect to an interpretation I and a variable assignment A as follow: (1) for a unit clause, I(p(t 1,...,t k )) = true if and only if I(p)(I(A)(t 1 ),...,I(A)(t k )) = true, for every assignment of variables A. (2) for a non-unit clause, I(h :- b 1,...,b k ) = true unless there is some assignment of variables A such that I(A)(h) = false and I(A)(b 1 )=...=I(A)(b k ) = true. An interpretation I is a model for P provided I(c)=true for every clause c of P. We say that I satisfies a goal, ?- g 1,g 2,...,g k provided that I satisfies each of the g i.

31 TCP1211-Logic Programming For revision… What are the three symbols that you know? What are the three symbols that you know? How do you define a clause, literal, term and alphabet? How do you define a clause, literal, term and alphabet? Propositional Logic vs. Predicate Logic Propositional Logic vs. Predicate Logic Semantics of a Prolog clause, goal and program. Semantics of a Prolog clause, goal and program. The interpretation given to a Prolog program. The interpretation given to a Prolog program.


Download ppt "TCP1211-Logic Programming Formal Syntax and Semantics of Logic Programs Faculty of Information Technology Multimedia University."

Similar presentations


Ads by Google