LOGIC Programming Rules
LOGIC Programming Rules
Logic
• Logic is not concerned with what is true. Logic is the study of what follows from
what. What conclusions follow from a set of premises.
• It can be defined as study of principles of correct reasoning.
• The main thing we study in logic are principles governing the validity of arguments
and check that whether certain conclusion follows from some given assumption.
• Consider:
Alice likes everyone who likes logic
Alice likes logic
1-3
Symbolic Logic
• Method of representing logical expressions
through the use of symbols and variables, rather
than in ordinary language.
• It provides the benefit of removing the ambiguity
that is generally seen in ordinary languages like
English.
• Its normally divided into two branches:
1. Propositional Logic
2. Predicate Logic
Propositional Logic
• Simplest form of formal logic.
• All statements made are called propositions.
• The FORMAL LOGIC is concerned with syntax of
statements and not with their semantics.
• It deals with manipulation of logical variables
which represents proposition.
• Propositional Logic is concerned with the subset of
declarative sentences that can be either true or
false.
• Propositional Logic takes only two values, either TRUE
or FALSE.
A proposition is basically a declarative sentence
that has a truth value. Truth value can either be
true or false, but it needs to be assigned any of
the two values and not be ambiguous.
Example:
Let A be a Proposition: The machine is
defective. Let B be a Proposition: The
production is less.
Why we use Propositional Logic?
• Its easier to check formulas.
• We can exploit the Boolean nature for
efficient reasoning.
• Its easier to incrementally add formulas.
• It can be extended infinitely to many variables
using logical quantifiers.
The following statements :
negations = [
(word("not"), ""),
(word("cannot"), "can"),
(word("can't"), "can"),
(word("won't"), "will"),
(word("ain't"), "is"),
("n't", ""), # matches as part of a word: didn't, couldn't, etc.
]
English: If you build it, he will come.
Logic: (P ⇒ Q)
P: you build it
Q: he will come
English: A ham sandwich is better than nothing and nothing is better than
eternal happiness therefore a ham sandwich is better than eternal happiness.
Logic: ((P ⋀ Q) ⇒ R)
P: A ham sandwich is better than nothing
Q: nothing is better than eternal happiness
R: a ham sandwich is better than eternal happiness
English: Either Danny didn't come to the party or Virgil didn't come to the
party.
Head Body
If
Example
To write a rule, X is a sister of Y
• X is a female.
• X has mother M and father F and
• Y has the same mother and father as X does.
sister_of(X, Y) :-
female(X),
parents(X, M, F),
parents(Y, M, F).
Example
• To define a rule “X is a grandfather of Y, if X is a
father of Z and Z is a parent of Y ” using logic
programming convention, then we write
grandfather(X, Y) :- father(X, Z) , parent(Z, Y).
no
Example
?- 2*3+4 = X+Y.
** The term 2*3+4 has principal functor +
X=2*3 and therefore unifies X+Y with X
instantiated to 2*3 and Y instantiated to 4 **
yes
Example
?- [a,b,c] = [X,Y,Z].
** Lists unify just like other terms
** X=a Y=b Z=c
yes
Example
Do the following pairs of items unify (match) ?
eats(fred,tomatoes)
eats(WHOM,WHAT)
Yes WHOM = fred and WHAT = tomatoes.
Example
cd(29,beatles,sgt_pepper).
cd(A,B,help).
No sgt_pepper and help do not unify
f(X,Y)
f(P,P)
Yes X = P and Y = P. A variable (such as X) can be
bound to another variable (such as P). In this case
we can also infer that X = Y
Example
f(X,a)
f(a,X)
Yes X = a
Example
likes(jane,X)
likes(X,jim)
No X can not be bound to both jane and jim
f(foo,L)
f(A,A)
Yes A = foo and A = L. Hence L = foo
A and L are variables