Вы находитесь на странице: 1из 34

Logic Programming

Logic programming had its beginning in early 1970s. It was primarily introduced by Kowalski (1974) and Colmerauer et al. (1973) following the earlier work in automated theorem proving and Artificial Intelligence. Logic programming is based on first order predicate logic. Clauses are very common in logic programming which have special forms of first order predicate logic (FOL) formula. Program in logic programming is a collection of clauses. Clause in logic programming adopts a special clausal notation.

Queries are solved using resolution principle. Several logic programming languages have been developed since then reflecting different choices of clause selection. In the family of logic programming there are two categories viz., Prolog and its extensions (Prolog-II, IC-Prolog, MU-Prolog, LPA Prolog, Edinburgh Prolog etc.) based on sequential execution and other languages, such as PARALOG, concurrent Prolog, GHC etc are based on parallel execution. Prolog is an acronym for Programming in Logic and is based on logic programming concepts The treatment of non determinism distinguishes between sequential and parallel languages.

clause in logic programming is represented in a clausal notation given by A1, , Ak B1,, Bt , where Aj are positive literals and Bk are negative literals.

Conversion of a Clause into Clausal Representation


A clause in FOL is a closed formula of the form, (x1) (xn) (L1V V Lm), where each Lk , (1 k n) is a literal and xk , (1 k n) are all the variables occurring in L1, , Lm .

Let us remove the prefix (x1) (xn) from the above representation for the sake of simplicity because all the variables appearing in a clause are universally quantified . Therefore, a clause is represented as L1V L2 V V Lm , where Lk , (1 k n) are literals free from quantifiers. Now separate positive and negative literals in the clause as follows.: (L1V L2 V V Lm) (A1 V VAk V ~ B1 V V~ Bt),
where positive negative literals. m = k + t, Aj, (1 j k) are literals and Bj , (1 j t) are

(A1 V VAk ) V ~ (B1 Bt) (B1 Bt) (A1 V VAk ) {since P Q ~ P V Q}

Clausal notation is written in the form : (A1 V VAk ) (B1 Bt) Or A1, , Ak B1,, Bt . Here Aj , (1 j k) are positive literals and Bi , (1 i t) are negative literals. It must be noted that interpretation of A B is same as B A .

In clausal notation, all variables are assumed to be universally quantified. Bi , (1 i t) (negative literals) are called antecedents and Aj , (1 j k) (positive literals) are called consequents. Commas in antecedent and consequent denote conjunction and disjunction respectively.

Logic Programming Concepts


A program clause is a clause of the form A B1, , Bn that contains precisely one positive literal. It is also called conditional rule. A in a program clause is called head and (B1, , Bn) is called body of the rule. The informal semantics of A B1, , Bn in logic is that "for all interpretations, if B1... Bn is true, then A is true". Formal semantics of the rule A B1, , Bn in logic programming is that "A is proved if B1, .Bn are proved together".

Here A, B1, B2, .Bn are predicates along with the arguments and the symbol denotes "implied by. A unit clause is a clause of the form A . This is unconditional clause. Alternatively, a program clause with only one positive literal and with no negative literals is called unit clause. It has an empty body always Its informal semantics in logic is that "for all interpretations, A is true. Formal interpretation of unit clause in logic programming is that "A is valid".

A logic program is a finite sequence of program clauses. A goal clause is a clause of the form B1, , Bn with no positive literal or an empty consequent. Each Bk (1 k n) is called a sub goal of the goal clause. The meaning of a logic program P is the set of ground goals deducible from P. An empty clause is the clause with empty consequent and empty antecedents. This clause is to be understood as a contradiction. It is graphically denoted by square / rectangle.

Applying the results of FOL to the logic programs, a goal G with respect to a program P (finite set of clauses) is solved by showing that the set of clauses P { ~ G} is unsatisfiable or there is a resolution refutation of P { ~ G}. If so, then G is logical consequence of a program P. The basic constructs of logic programming are inherited from FOL. There are three basic statements: facts, rules and queries. These are special forms of clauses as already mentioned earlier.

Fact (Unit clause): The simplest kind of statement is called fact which states a relation between objects such as
BROTHER ( john, mike) . This fact says that john is a brother of mike. BROTHER is a predicate symbol that shows relationship between two individuals named john and mike.

Rule (Program Clause): If we have 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)

Query (Goal): Query is used to retrieve information from logic program. Answering a query with respect to a logic program is to determine whether the query is a logical consequence of a program or not. Logical consequences are obtained by applying resolution rule. Query can be simple or conjunctive. A simple query ( B) is one which has only one sub goal and is a logical consequence of a program if B is deducible from it.

A conjunctive query is conjunction of sub goals ( B1, B2, .Bn ) which is a logical consequence of a program if all the sub goals in the query are logical consequences of it. Within each type of query, a goal(s) can be ground or non ground. In ground query, the goal(s) contains constants. Answer to ground query is either yes or no depending upon whether it is a logical consequence of a logic program or not.

In non ground query, the goal(s) should have atleast one variable as an argument. Comma , in the query denotes logical conjunction ( ) to separate sub goals and is different from the comma used to separate the arguments of the predicate in the goals of the query. Selection of the sub goal to be reduced in a conjunctive query is arbitrary in logic programming. In any given resolvent, all the sub goals must be reduced.

Constants: Constants are numbers (e.g., 23, 5 etc), string (e.g., 'mary', 'robert' etc) and symbols (e.g., mary, robert etc) a descriptive names in lower case letters Variables: Variable stands for an unspecified but single entity rather than a store location in memory.
The scope of the variable is a clause in which it appears. Variables can appear in facts, rules or goals. Variables in a fact or rule are universally quantified whereas in goals are existentially quantified.

Example: Consider program.

the

following

logic

GRANDFATHER (x, y) FATHER (x, z) , PARENT (z, y) PARENT (x, y) FATHER (x, y) PARENT (x, y) MOTHER (x, y) FATHER ( abraham, robert) FATHER ( robert, mike)

In FOL, above program is represented by a set of clauses as:


S = { GRANDFATHER (x, y) V ~ FATHER (x, z) V ~ PARENT (z, y), PARENT(x, y) V ~ FATHER (x, y), PARENT(x, y) V ~ MOTHER (x, y), FATHER ( abraham, robert), FATHER ( robert, mike) }

Let us number the clauses of S as follows: i. GRANDFATHER (x, y) V ~ FATHER (x, z) V ~ PARENT(z, y) ii. PARENT(x, y) V ~ FATHER (x, y) iii. PARENT(x, y) V ~ MOTHER (x, y), iv. FATHER ( abraham, robert) v. FATHER ( robert, mike)

Simple queries :
(a) Ground Query Query: Is abraham a grandfather of mike ? Goal: GRANDFATHER (abraham, mike).

In FOL, ~ GRANDFATHER (abraham, mike) is negation of Goal = GRANDFATHER (abraham, mike). Include {~Goal} in the set S and show using resolution refutation that S {~ Goal} is unsatisfiable in order to conclude the Goal. Let ~ Goal is numbered as ( vi) in continuation of first five clauses of S listed above. vi. ~ GRANDFATHER (abraham, mike)

(i)
{x / abraham, y / mike}

( vi )

( iv) ~ FATHER (abraham, z) V ~ PARENT (z,


mike)
{z / robert}

~ PARENT (robert, mike) ~ FATHER (robert, mike)

( ii)

(v)

Answer: Yes

(b)

Non ground queries Query: Does there exist x such that x is a father of robert ? {Who is father of robert?} FATHER (x, robert). Include ~ FATHER (x, robert) in the given set S and show that we get a refutation with some most general unifier. Hence it is proved that FATHER (x, robert) logical consequence of S with binding of { x / abraham}.

Similarly the following simple non ground query can be solved Query: Does there exist x such that abraham is a father of x?" {abraham is father of whom?} FATHER (abraham, x). Query : Do there exist x and y such that x is a father of y?" { who is father of whom?} FATHER (x, y).

Conjunctive queries:
(a) Ground Query Query:Is abraham a father of robert is a father of mike? robert and

FATHER(abraham, robert), FATHER ( robert, mike).

Answer: (b)

yes

Non ground queries: x = robert

FATHER (abraham, x), FATHER ( x, mike).

Answer:

Resolution: Resolution is a powerful inference technique. There are three important cases of resolution. In backward chaining, one of the clauses resolved is a goal clause. In forward chaining, one of the clauses is always a given fact. In rule collapsing, two rules are resolved with first rules left side matching some thing with the right side of second rule and combine into one rule. The resolution in all three cases is illustrated in the following ways.

Backward Chaining: Goal clause is resolved with one of the given clause. In clausal form: i. a b, c, d. ii. a. {goal clause} Result of resolution: b, c, d. {conjunctive sub goals} In logic form i. aV~bV~cV~d ii. ~a Result of resolution: ~bV ~cV~ d

Forward Chaining: One of the clauses is always a given fact. In clausal form: i. a b, c, d. ii. c. Result of resolution: a b, d. In logic form i. aV~bV~cV~d ii. c Result of resolution: aV ~bV~ d

Rule Collapsing:Two given rules are resolved with one rules left side matching with the right side of other rule and combine into one rule. In clausal form: i. a b, c, d. ii. c e, f. Result of resolution: a b, d, e, f. In logic form i. aV~bV~cV~d ii. cV~eV~f Result of resolution: a V ~ b V ~ d V ~ e V ~ f

Abstract Interpreter for Logic Program


An abstract interpreter for logic program takes program P and a goal /query as an input. It performs computations and shows that the goal is a logical consequence of P or not. The heart of computational model of logic programs is unification. The following interpreter performs computations for a goal (ground or non ground).

Input: A program P and a goal G Output: If G is a logical consequence of P then for ground query, output Yes and for non ground query, output the most general unifier obtained while satisfying goal G else output No. Method: Initialize Resolvent with G; Progress = true; While ((Resolvent empty) and (Progress = true) ) do { Choose a goal from Resolvent and store it in S;

if ( a clause A B1,.,Bn where, n 0 from P such that S and A unify with most general unifier (mgu) ) then replace S by B1,.,Bn where, n 0 in the Resolvent after applying to the Resolvent else Progress = false } {end of while} If (Resolvent = empty and Progress = true) then ( if = then output Yes else output )else output No. Stop.

For some computations, there is only one clause from the program that can reduce each subgoal. Such a computation is called deterministic. If there are alternative choices for the sub goals, then computation becomes non deterministic. To handle non deterministic computation, develop all possible reduction in parallel or sequentially using some clause selection strategy. Generate all possible computation paths and correct solutions are reported.

Each iteration of the while loop of an interpreter corresponds to a single application of modus ponen rule which means that: Given a rule of the form A B1, .Bn , where, n 1 and the facts B1 , ...., Bn, using modus ponen law A can be deduced if A B1,,Bn is an instance of A B1, ,Bn . Proof tree of a ground goal consists of nodes and edges which represent the goals reduced during the computation. Search tree of a non ground goal with respect to a program is a trace of all possible paths in the computation of the goal.

Consider the following logic.


SON(x, y) FATHER(y, x) , MALE(x) FATHER( abraham, robert) FATHER( robert, mike) MALE(abraham) MALE(robert) MALE(mike) Generate Proof tree for a goal : SON( mike, robert)

Proof Tree:

SON( mike, robert)

FATHER(robert, mike), MALE(mike)

Search tree for

SON(x, y)

SON(x, y) FATHER(y, x), MALE(x) {y / abraham, x / robert} mike} MALE(robert) {y / robert, x /

MALE(mike)

Вам также может понравиться