Title: Formal Syntax and Semantics of Logic Programs
1Formal Syntax and Semantics of Logic Programs
Faculty of Information Technology Multimedia
University
2Lecture Outline
- Syntax of Logic programs.
- Propositional Logic.
- Predicate Logic. (and why predicate logic?)
- Semantics of a Logic Program.
3Three 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.
4Function symbols
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.
5Variables
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.
6List of new terms so far
- Predicate/relation symbol
- Function symbol
- What next ?
- Formal syntax of Logic Programs
- Formal definition of a clause
- What is a literal ?
- Formal definition of a term.
- The alphabet of a program.
7Formal Syntax of Logic Programs
Syntax of a Program
likes(mother(X),X) - good(X). woman(mother(X)).
woman(ann). good(husband(ann)).
Clauses
A program is a set of clauses. From a logical
point of view, the order in which these clauses
are written has no importance.
8Syntax of a clause
Literals
No Literals
likes(ann, X) - toy(X), plays(ann, X).
likes(ann, snoopy) - . Or
Simply likes(ann, snoopy).
Head
Body
A clause is a formula P - Q1,, Qn. P is
a literal called the head of the clause, and
Q1,,Qn 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
-.
9Syntax of a Literal
2 Terms (Arity 2)
0 Term (Arity 0)
likes(mother(X), X)
likes() Or Simply likes
Relation Symbol
A literal is a formula p(t1,,tk) where p
is a relation symbol of arity k and (t1,,tk) are
k terms. In the case k 0, the literal is
written simply as p.
10Syntax of a Term
Compound Term
Variable (Simple Term)
2 Terms
likes(mother(husband(X)), X) ---- A
Literal
Function Symbol (Arity 1)
Term of Term of Term (Variable, A Simple Term)
Term of Term (Also Compound Term)
A term is either a variable like X, or it is a
compound term f(t1,, tk) where f is a
function symbol of arity k, and (t1,, tk) are k
smaller terms. A function symbol with no
arguments is a constant, written simply as f.
11An Alphabet of a Program
likes(mother(X),X) - good(X). woman(mother(X)).
woman(ann). good(husband(ann)).
Semicolon
Alphabet likes/2, woman/1, good/1 mother/1,
husband/1, ann/0
Function Symbols
Relation Symbols
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.
12Propositional Logic
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
"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
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.
13Implication (?)
P It's raining
Q I'll get wet
P ? Q
IF It's raining THEN I'll get wet
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.
14The Meaning of Implication (?) using Truth Table
P You are crying
Q I wont give you an icecream
P ? Q
IF You are crying THEN I wont give you an
icecream
QUESTION Is the statement P ? Q true (valid)?
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 wont give you an icecream, the child
stops crying, and doesnt get any icecream. In
logic, the parent did not tell a lie.
15P You are crying
Q I wont give you an icecream
P ? Q
IF You are crying THEN I wont give you an
icecream
P Q P ? Q T T T T F
F F T T F F T
P ? Q
This Case Is Not Possible Now
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.
16Predicate Logic
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
"There is a person whom Sue likes" True or
False "Ann likes all the animals" True or False
Mathematically,
?(X)person(X) ? likes(sue, X) Existentially
Quantified ?(X)animal(X) ? likes(ann,
X) Universally Quantified
17"There is a person whom Sue likes" "Ann likes all
the animals"
Existential Quantifier
?(X) person(X) ? likes(sue, X) "There exists X
such that X is a person and Sue likes X"
Universal Quantifier
?(X) animal(X) ? likes(sue, X) "For all X such
that X is an animal and Ann likes X"
18Variable 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
?- person(X), likes(sue, X). Query/Initial Goal
"There exists X such that X is a person and Sue
likes X"
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.
19In a literal which is used as a rule head
likes(sue, X) - animal(X). Rule Head
"For all X such that Sue likes X if X is an
animal"
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.
20X is Universally Quantified
likes(sue, X) - toy(X), person(Y), plays(Y, X).
Y is Existentially Quantified
"For all X such that Sue likes X if X is a toy,
and There exists Y such that Y plays with X"
"Sue likes every toy some person plays with"
21The Semantics of a Prolog Clause
likes(sue, snoopy) - toy(snoopy), plays(sue,
snoopy). Rule
Means
toy(snoopy) ? plays(sue, snoopy) ? likes(sue,
snoopy)
likes(sue, snoopy). Fact
Means
True ? likes(sue, snoopy)
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.
22The Semantics of a Prolog Goal
?- toy(snoopy), plays(sue, snoopy). Query/Goal
Means
toy(snoopy) ? plays(sue, snoopy) ? True
A goal in a Prolog program, written as - P. can
be considered as P ? True.
23The Semantics of a Prolog Program
likes(sue,snoopy) - toy(snoopy),
plays(sue,snoopy). toy(snoopy). plays(sue,snoopy).
Means
?
toy(snoopy) ? plays(sue, snoopy) ? likes(sue,
snoopy)
?
True ? toy(snoopy)
True ? plays(sue, snoopy)
A Prolog program is regarded as the conjunction
of its clauses, and its logical semantics consist
of the truth of its clauses.
24What is an interpretation?
Kuala Lumpur
is
This unit clause will be a true fact under the
given interpretation.
i(k, c(m)).
capital
Malaysia
"Kuala Lumpur is the capital of Malaysia"
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.
25If the logic program is given a new
interpretation now
Kuala Lumpur
is located
This unit clause will not be a true fact under
the new interpretation.
i(k, c(m)).
centre of
Melaka
"Kuala Lumpur is located at the centre of Melaka"
Same unit clause, true previously but false now !
26A Prolog program which contains two non-unit
clauses (rules)
f(N,F) - N 0, F 1. f(N,F) - N gt 0, N1 is
N-1, f(N1,F1), F is NF1.
Relation Symbol f
Variables N, N1, F, F1
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.
27Formal Semantics (cont.)
l(m(a), a). A one-clause program
a Ann constant, m mother
of function, l likes relation
I(a) Ann I(m) mother of I(l) likes
I
No variables involved !
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.
28l(m(X), X) - g(X, Y). A one-clause program
m mother of function, l
likes relation, g good in relation
I(m) mother of I(l) likes I(g) good in
I
X a person, Y a subject
A(X) ? persons A(Y) ? subjects
A
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.
29Now 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(t1,...,tk))
true if and only if I(p)(I(A)(t1),...,I(A)(tk))
true, for every assignment of variables A.
(2) for a non-unit clause, I(h -
b1,...,bk) true unless there is some assignment
of variables A such that I(A)(h) false
and I(A)(b1)...I(A)(bk) 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, ?- g1,g2,...,gk
provided that I satisfies each of the gi.
30For revision
- What are the three symbols that you know?
- How do you define a clause, literal, term and
alphabet? - Propositional Logic vs. Predicate Logic
- Semantics of a Prolog clause, goal and program.
- The interpretation given to a Prolog program.