UNit 4 - Logic Programming
UNit 4 - Logic Programming
Programming
• We now examine a radically different paradigm for
programming: declarative programming
– rather than writing control constructs (loops, selection
statements, subroutines)
– you specify knowledge and how that knowledge is to be
applied through a series of rules
– the programming language environment uses one or more
built-in methods to reason over the knowledge and prove
things (or answer questions)
• in logic programming, the common approach is to apply the methods
of resolution and unification
• They can build powerful problem solving systems
with little programming expertise
– they have been used extensively in AI research
– Developed by Alain Colmerauer, Phillippe Roussell,
and Robert Kowlalski.
Terminology
• Logic Programming is a specific type of a more general
class: production systems (also called rule-based
systems)
– a production system is a collection of facts (knowledge), rules
(which are another form of knowledge) and control strategies
•Arithmetic Operators
•Relational Operators
•Logical Operators
Arithmetic Operators
It uses is/2 built-in predicate. This predicate is predefined as an
infix operator. The is/2 predicate is placed between the two
arguments.
For example:
A = 6 * B + C - 3.2 + S - T / 4
Arithmetic Operators
1. A + B sum of A and B
2. A - B difference of A and B
3. A * B product of A and B
4. A / B quotient of A and B
6. A ^ B A to the power of B
7. - A negative of A
Arithmetic Operators
1.sin(A) sine of A
2. cos(A) cosine of A
2. 15 is 9 + 6 - 13 + 20
no
3. 22 is 9 + 6 - 13 + 20
yes
Relational Operators
The relational operators are =:=, >, <, >=, =/=, =<.
2. 59=\=63.
yes
– Equivalence means that
Logic Operators both expressions have
identical truth tables
Name Symbol Example Meaning – Implication is like an if-
negation a not a then statement
• if a is true then b is true
conjunction ab a and b • note that this does not
disjunction ab a or b necessarily mean that if
a is false that b must also
equivalence ab a is equivalent be false
to b – Universal quantifier says
that this is true no matter
implication ab a implies b what x is
ab b implies a – Existential quantifier says
X.P that there is an X that
universal For all X, P is fulfills the statement
true
existential X.P There exists a X.(woman(X) human(X))
value of X – if X is a woman, then X is a human
such that P is
true X.(mother(mary, X) male(X))
– Mary has a son (X)
Logic operators
• Not operator
To provide the negation, not/1 prefix operator can be placed before any goal. If
the original goal fails, the negation goal succeeds. If the original goal
succeeds, the negation goal fails.
Example:
dog(boxer).
?- not dog(boxer).
no
?- \+ dog(boxer).
False
Logic operators
• Disjunction Operator
The ';/2' is the disjunction operator. It is an infix operator, which represents 'or'. It
takes two arguments, and both the arguments are goals. If either Goal1 or
Goal2 succeeds, the Goal1;Goal2 succeeds.
Examples:
9<4; 10 is 7+3.
Output:Yes
Examples:
9<4,10 is 7+3.
Output:No
Defining GCD:
gcd(X,Y,D) :- X=Y, D is X.
gcd(X,Y,D) :- X<Y, Y1 is Y - X, gcd(X, Y1, D).
gcd(X,Y,D) :- X>Y, gcd(Y, X, D).
Defining Length:
length([ ], 0). // empty list has a length of 0
length([ _ | Tail, N) :- length(Tail, N1), N is 1 + N1. // a list that has an
// item _ and a Tail is length N if the length of Tail is N1 where N = 1 + N1