Logic programming
Logic programming is a programming paradigm based on formal logic. A program written in a logic programming language is a set of sentences in logical form, expressing facts and rules about some problem domain. Major logic programming language families include Prolog, Answer set programming (ASP) and Datalog. In all of these languages, rules are written in the form of clauses:
 H : B_{1}, …, B_{n}.
and are read declaratively as logical implications:
 H if B_{1} and … and B_{n}.
H is called the head of the rule and B_{1}, …, B_{n} is called the body. Facts are rules that have no body, and are written in the simplified form:
 H.
In the simplest case in which H, B_{1}, …, B_{n} are all atomic formulae, these clauses are called definite clauses or Horn clauses. However, there exist many extensions of this simple case, the most important one being the case in which conditions in the body of a clause can also be negations of atomic formulae. Logic programming languages that include this extension have the knowledge representation capabilities of a nonmonotonic logic.
In ASP and Datalog, logic programs have only a declarative reading, and their execution is performed by means of a proof procedure or model generator whose behaviour is not meant to be under the control of the programmer. However, in the Prolog family of languages, logic programs also have a procedural interpretation as goalreduction procedures:
 to solve H, solve B_{1}, and ... and solve B_{n}.
Consider, for example, the following clause:
 fallible(X) : human(X).
based on an example used by Terry Winograd ^{[1]} to illustrate the programming language Planner. As a clause in a logic program, it can be used both as a procedure to test whether X is fallible by testing whether X is human, and as a procedure to find an X that is fallible by finding an X that is human. Even facts have a procedural interpretation. For example, the clause:
 human(socrates).
can be used both as a procedure to show that socrates is human, and as a procedure to find an X that is human by "assigning" socrates to X.
The declarative reading of logic programs can be used by a programmer to verify their correctness. Moreover, logicbased program transformation techniques can also be used to transform logic programs into logically equivalent programs that are more efficient. In the Prolog family of logic programming languages, the programmer can also use the known problemsolving behaviour of the execution mechanism to improve the efficiency of programs.
Contents
 1 History
 2 Concepts
 3 Variants and extensions
 3.1 Prolog
 3.2 Abductive logic programming
 3.3 Metalogic programming
 3.4 Constraint logic programming
 3.5 Concurrent logic programming
 3.6 Concurrent constraint logic programming
 3.7 Inductive logic programming
 3.8 Higherorder logic programming
 3.9 Linear logic programming
 3.10 Objectoriented logic programming
 3.11 Transaction logic programming
 4 See also
 5 References
 6 Further reading
 7 External links
History
The use of mathematical logic to represent and execute computer programs is also a feature of the lambda calculus, developed by Alonzo Church in the 1930s. However, the first proposal to use the clausal form of logic for representing computer programs was made by Cordell Green.^{[2]} This used an axiomatization of a subset of LISP, together with a representation of an inputoutput relation, to compute the relation by simulating the execution of the program in LISP. Foster and Elcock's Absys, on the other hand, employed a combination of equations and lambda calculus in an assertional programming language which places no constraints on the order in which operations are performed.^{[3]}
Logic programming in its present form can be traced back to debates in the late 1960s and early 1970s about declarative versus procedural representations of knowledge in Artificial Intelligence. Advocates of declarative representations were notably working at Stanford, associated with John McCarthy, Bertram Raphael and Cordell Green, and in Edinburgh, with John Alan Robinson (an academic visitor from Syracuse University), Pat Hayes, and Robert Kowalski. Advocates of procedural representations were mainly centered at MIT, under the leadership of Marvin Minsky and Seymour Papert.^{[citation needed]}
Although it was based on the proof methods of logic, Planner, developed at MIT, was the first language to emerge within this proceduralist paradigm.^{[4]} Planner featured patterndirected invocation of procedural plans from goals (i.e. goalreduction or backward chaining) and from assertions (i.e. forward chaining). The most influential implementation of Planner was the subset of Planner, called MicroPlanner, implemented by Gerry Sussman, Eugene Charniak and Terry Winograd. It was used to implement Winograd's naturallanguage understanding program SHRDLU, which was a landmark at that time.^{[1]} To cope with the very limited memory systems at the time, Planner used a backtracking control structure so that only one possible computation path had to be stored at a time. Planner gave rise to the programming languages QA4, Popler, Conniver, QLISP, and the concurrent language Ether.^{[citation needed]}
Hayes and Kowalski in Edinburgh tried to reconcile the logicbased declarative approach to knowledge representation with Planner's procedural approach. Hayes (1973) developed an equational language, Golux, in which different procedures could be obtained by altering the behavior of the theorem prover.^{[5]} Kowalski, on the other hand, developed SLD resolution,^{[6]} a variant of SLresolution,^{[7]} and showed how it treats implications as goalreduction procedures. Kowalski collaborated with Colmerauer in Marseille, who developed these ideas in the design and implementation of the programming language Prolog.
The Association for Logic Programming was founded to promote Logic Programming in 1986.
Prolog gave rise to the programming languages ALF, Fril, Gödel, Mercury, Oz, Ciao, Visual Prolog, XSB, and λProlog, as well as a variety of concurrent logic programming languages,^{[8]} constraint logic programming languages and datalog.^{[citation needed]}
Concepts
Logic and control
Logic programming can be viewed as controlled deduction. An important concept in logic programming is the separation of programs into their logic component and their control component. With pure logic programming languages, the logic component alone determines the solutions produced. The control component can be varied to provide alternative ways of executing a logic program. This notion is captured by the slogan
 Algorithm = Logic + Control
where "Logic" represents a logic program and "Control" represents different theoremproving strategies.^{[9]}
Problem solving
In the simplified, propositional case in which a logic program and a toplevel atomic goal contain no variables, backward reasoning determines an andor tree, which constitutes the search space for solving the goal. The toplevel goal is the root of the tree. Given any node in the tree and any clause whose head matches the node, there exists a set of child nodes corresponding to the subgoals in the body of the clause. These child nodes are grouped together by an "and". The alternative sets of children corresponding to alternative ways of solving the node are grouped together by an "or".
Any search strategy can be used to search this space. Prolog uses a sequential, lastinfirstout, backtracking strategy, in which only one alternative and one subgoal is considered at a time. Other search strategies, such as parallel search, intelligent backtracking, or bestfirst search to find an optimal solution, are also possible.
In the more general case, where subgoals share variables, other strategies can be used, such as choosing the subgoal that is most highly instantiated or that is sufficiently instantiated so that only one procedure applies. Such strategies are used, for example, in concurrent logic programming.
Negation as failure
For most practical applications, as well as for applications that require nonmonotonic reasoning in artificial intelligence, Horn clause logic programs need to be extended to normal logic programs, with negative conditions. A clause in a normal logic program has the form:
 H : A_{1}, …, A_{n}, not B_{1}, …, not B_{n}.
and is read declaratively as a logical implication:
 H if A_{1} and … and A_{n} and not B_{1} and … and not B_{n}.
where H and all the A_{i} and B_{i} are atomic formulas. The negation in the negative literals not B_{i} is commonly referred to as "negation as failure", because in most implementations, a negative condition not B_{i} is shown to hold by showing that the positive condition B_{i} fails to hold. For example:
 canfly(X) : bird(X), not abnormal(X).
 abnormal(X) : wounded(X).
 bird(john).
 bird(mary).
 wounded(john).
Given the goal of finding something that can fly:
 : canfly(X).
there are two candidate solutions, which solve the first subgoal bird(X), namely X = john and X = mary. The second subgoal not abnormal(john) of the first candidate solution fails, because wounded(john) succeeds and therefore abnormal(john) succeeds. However, The second subgoal not abnormal(mary) of the second candidate solution succeeds, because wounded(mary) fails and therefore abnormal(mary) fails. Therefore, X = mary is the only solution of the goal.
MicroPlanner had a construct, called "thnot", which when applied to an expression returns the value true if (and only if) the evaluation of the expression fails. An equivalent operator is normally builtin in modern Prolog's implementations. It is normally written as not(Goal)
or \+ Goal
, where Goal
is some goal (proposition) to be proved by the program. This operator differs from negation in firstorder logic: a negation such as \+ X == 1
fails when the variable X
has been bound to the atom 1
, but it succeeds in all other cases, including when X
is unbound. This makes Prolog's reasoning nonmonotonic: X = 1, \+ X == 1
always fails, while \+ X == 1, X = 1
can succeed, binding X
to 1
, depending on whether X
was initially bound (note that standard Prolog executes goals in lefttoright order).
The logical status of negation as failure was unresolved until Keith Clark [1978] showed that, under certain natural conditions, it is a correct (and sometimes complete) implementation of classical negation with respect to the completion of the program. Completion amounts roughly to regarding the set of all the program clauses with the same predicate on the left hand side, say
 H : Body_{1}.
 …
 H : Body_{k}.
as a definition of the predicate
 H iff (Body_{1} or … or Body_{k})
where "iff" means "if and only if". Writing the completion also requires explicit use of the equality predicate and the inclusion of a set of appropriate axioms for equality. However, the implementation of negation by failure needs only the ifhalves of the definitions without the axioms of equality.
For example, the completion of the program above is:
 canfly(X) iff bird(X), not abnormal(X).
 abnormal(X) iff wounded(X).
 bird(X) iff X = john or X = mary.
 X = X.
 not john = mary.
 not mary = john.
The notion of completion is closely related to McCarthy's circumscription semantics for default reasoning, and to the closed world assumption.
As an alternative to the completion semantics, negation as failure can also be interpreted epistemically, as in the stable model semantics of answer set programming. In this interpretation not(B_{i}) means literally that B_{i} is not known or not believed. The epistemic interpretation has the advantage that it can be combined very simply with classical negation, as in "extended logic programming", to formalise such phrases as "the contrary can not be shown", where "contrary" is classical negation and "can not be shown" is the epistemic interpretation of negation as failure.
Knowledge representation
The fact that Horn clauses can be given a procedural interpretation and, vice versa, that goalreduction procedures can be understood as Horn clauses + backward reasoning means that logic programs combine declarative and procedural representations of knowledge. The inclusion of negation as failure means that logic programming is a kind of nonmonotonic logic.
Despite its simplicity compared with classical logic, this combination of Horn clauses and negation as failure has proved to be surprisingly expressive. For example, it has been shown to correspond, with some further extensions, quite naturally to the semiformal language of legislation. It is also a natural language for expressing commonsense laws of cause and effect, as in the situation calculus and event calculus.
Variants and extensions
Prolog
The programming language Prolog was developed in 1972 by Alain Colmerauer. It emerged from a collaboration between Colmerauer in Marseille and Robert Kowalski in Edinburgh. Colmerauer was working on natural language understanding, using logic to represent semantics and using resolution for questionanswering. During the summer of 1971, Colmerauer and Kowalski discovered that the clausal form of logic could be used to represent formal grammars and that resolution theorem provers could be used for parsing. They observed that some theorem provers, like hyperresolution, behave as bottomup parsers and others, like SLresolution (1971), behave as topdown parsers.
It was in the following summer of 1972, that Kowalski, again working with Colmerauer, developed the procedural interpretation of implications. This dual declarative/procedural interpretation later became formalised in the Prolog notation
 H : B_{1}, …, B_{n}.
which can be read (and used) both declaratively and procedurally. It also became clear that such clauses could be restricted to definite clauses or Horn clauses, where H, B_{1}, …, B_{n} are all atomic predicate logic formulae, and that SLresolution could be restricted (and generalised) to LUSH or SLDresolution. Kowalski's procedural interpretation and LUSH were described in a 1973 memo, published in 1974.^{[6]}
Colmerauer, with Philippe Roussel, used this dual interpretation of clauses as the basis of Prolog, which was implemented in the summer and autumn of 1972. The first Prolog program, also written in 1972 and implemented in Marseille, was a French questionanswering system. The use of Prolog as a practical programming language was given great momentum by the development of a compiler by David Warren in Edinburgh in 1977. Experiments demonstrated that Edinburgh Prolog could compete with the processing speed of other symbolic programming languages such as Lisp. Edinburgh Prolog became the de facto standard and strongly influenced the definition of ISO standard Prolog.
Abductive logic programming
Abductive logic programming is an extension of normal Logic Programming that allows some predicates, declared as abducible predicates, to be "open" or undefined. A clause in an abductive logic program has the form:
 H : B_{1}, …, B_{n}, A_{1}, …, A_{n}.
where H is an atomic formula that is not abducible, all the B_{i} are literals whose predicates are not abducible, and the A_{i} are atomic formulas whose predicates are abducible. The abducible predicates can be constrained by integrity constraints, which can have the form:
 false : B_{1}, …, B_{n}.
where the B_{i} are arbitrary literals (defined or abducible, and atomic or negated). For example:
 canfly(X) : bird(X), normal(X).
 false : normal(X), wounded(X).
 bird(john).
 bird(mary).
 wounded(john).
where the predicate normal is abducible.
Problem solving is achieved by deriving hypotheses expressed in terms of the abducible predicates as solutions of problems to be solved. These problems can be either observations that need to be explained (as in classical abductive reasoning) or goals to be solved (as in normal logic programming). For example, the hypothesis normal(mary) explains the observation canfly(mary). Moreover, the same hypothesis entails the only solution X = mary of the goal of finding something that can fly:
 : canfly(X).
Abductive logic programming has been used for fault diagnosis, planning, natural language processing and machine learning. It has also been used to interpret Negation as Failure as a form of abductive reasoning.
Metalogic programming
Because mathematical logic has a long tradition of distinguishing between object language and metalanguage, logic programming also allows metalevel programming. The simplest metalogic program is the socalled "vanilla" metainterpreter:
solve(true). solve((A,B)): solve(A),solve(B). solve(A): clause(A,B),solve(B).
where true represents an empty conjunction, and clause(A,B) means there is an objectlevel clause of the form A : B.
Metalogic programming allows objectlevel and metalevel representations to be combined, as in natural language. It can also be used to implement any logic that is specified by means of inference rules.
Constraint logic programming
Constraint logic programming combines Horn clause logic programming with constraint solving. It extends Horn clauses by allowing some predicates, declared as constraint predicates, to occur as literals in the body of clauses. A constraint logic program is a set of clauses of the form:
 H : C_{1}, …, C_{n} B_{1}, …, B_{n}.
where H and all the B_{i} are atomic formulas, and the C_{i} are constraints. Declaratively, such clauses are read as ordinary logical implications:
 H if C_{1} and … and C_{n} and B_{1} and … and B_{n}.
However, whereas the predicates in the heads of clauses are defined by the constraint logic program, the predicates in the constraints are predefined by some domainspecific modeltheoretic structure or theory.
Procedurally, subgoals whose predicates are defined by the program are solved by goalreduction, as in ordinary logic programming, but constraints are checked for satisfiability by a domainspecific constraintsolver, which implements the semantics of the constraint predicates. An initial problem is solved by reducing it to a satisfiable conjunction of constraints.
The following constraint logic program represents a toy temporal database of john's history as a teacher:
 teaches(john, hardware, T) : 1990 ≤ T, T < 1999.
 teaches(john, software, T) : 1999 ≤ T, T < 2005.
 teaches(john, logic, T) : 2005 ≤ T, T ≤ 2012.
 rank(john, instructor, T) : 1990 ≤ T, T < 2010.
 rank(john, professor, T) : 2010 ≤ T, T < 2014.
Here ≤ and < are constraint predicates, with their usual intended semantics. The following goal clause queries the database to find out when john both taught logic and was a professor:
 : teaches(john, logic, T), rank(john, professor, T).
The solution is 2010 ≤ T, T ≤ 2012.
Constraint logic programming has been used to solve problems in such fields as civil engineering, mechanical engineering, digital circuit verification, automated timetabling, air traffic control, and finance. It is closely related to abductive logic programming.
Concurrent logic programming
Concurrent logic programming integrates concepts of logic programming with concurrent programming. Its development was given a big impetus in the 1980s by its choice for the systems programming language of the Japanese Fifth Generation Project (FGCS).^{[10]}
A concurrent logic program is a set of guarded Horn clauses of the form:

 H : G_{1}, …, G_{n}  B_{1}, …, B_{n}.
The conjunction G_{1}, … , G_{n} is called the guard of the clause, and  is the commitment operator. Declaratively, guarded Horn clauses are read as ordinary logical implications:

 H if G_{1} and … and G_{n} and B_{1} and … and B_{n}.
However, procedurally, when there are several clauses whose heads H match a given goal, then all of the clauses are executed in parallel, checking whether their guards G_{1}, … , G_{n} hold. If the guards of more than one clause hold, then a committed choice is made to one of the clauses, and execution proceedes with the subgoals B_{1}, …, B_{n} of the chosen clause. These subgoals can also be executed in parallel. Thus concurrent logic programming implements a form of "don't care nondeterminism", rather than "don't know nondeterminism".
For example, the following concurrent logic program defines a predicate shuffle(Left, Right, Merge) , which can be used to shuffle two lists Left and Right, combining them into a single list Merge that preserves the ordering of the two lists Left and Right:
 shuffle([], [], []).
 shuffle(Left, Right, Merge) : Left = [First  Rest]  Merge = [First  ShortMerge], shuffle(Rest, Right, ShortMerge).
 shuffle(Left, Right, Merge) : Right = [First  Rest]  Merge = [First  ShortMerge], shuffle(Left, Rest, ShortMerge).
Here, [] represents the empty list, and [Head  Tail] represents a list with first element Head followed by list Tail, as in Prolog. (Notice that the first occurrence of  in the second and third clauses is the list constructor, whereas the second occurrence of  is the commitment operator.) The program can be used, for example, to shuffle the lists [ace, queen, king] and [1, 4, 2] by invoking the goal clause:
 shuffle([ace, queen, king], [1, 4, 2], Merge).
The program will nondeterministically generate a single solution, for example Merge = [ace, queen, 1, king, 4, 2].
Arguably, concurrent logic programming is based on message passing and consequently is subject to the same indeterminacy as other concurrent messagepassing systems, such as Actors (see Indeterminacy in concurrent computation). Carl Hewitt^{[citation needed]} has argued that, concurrent logic programming is not based on logic in his sense that computational steps cannot be logically deduced [Hewitt and Agha, 1988]. However, in concurrent logic programming, any result of a terminating computation is a logical consequence of the program, and any partial result of a partial computation is a logical consequence of the program and the residual goal (process network). Consequently, the indeterminacy of computations implies that not all logical consequences of the program can be deduced.
 REDIRECT Template:POVstatement
Concurrent constraint logic programming
Concurrent constraint logic programming combines concurrent logic programming and constraint logic programming, using constraints to control concurrency. A clause can contain a guard, which is a set of constraints that may block the applicability of the clause. When the guards of several clauses are satisfied, concurrent constraint logic programming makes a committed choice to the use of only one.
Inductive logic programming
Inductive logic programming is concerned with generalizing positive and negative examples in the context of background knowledge: machine learning of logic programs. Recent work in this area, combining logic programming, learning and probability, has given rise to the new field of statistical relational learning and probabilistic inductive logic programming.
Higherorder logic programming
Several researchers have extended logic programming with higherorder programming features derived from higherorder logic, such as predicate variables. Such languages include the Prolog extensions HiLog and λProlog.
Linear logic programming
Basing logic programming within linear logic has resulted in the design of logic programming languages that are considerably more expressive than those based on classical logic. Horn clause programs can only represent state change by the change in arguments to predicates. In linear logic programming, one can use the ambient linear logic to support state change. Some early designs of logic programming languages based on linear logic include LO [Andreoli & Pareschi, 1991], Lolli,^{[11]} ACL,^{[12]} and Forum [Miller, 1996]. Forum provides a goaldirected interpretation of all of linear logic.
Objectoriented logic programming
Flogic extends logic programming with objects and the frame syntax. A number of systems are based on Flogic, including Flora2, FLORID, and a highly scalable commercial system Ontobroker.
Logtalk extends the Prolog programming language with support for objects, protocols, and other OOP concepts. Highly portable, it supports most standardcomplaint Prolog systems as backend compilers.
Transaction logic programming
Transaction logic is an extension of logic programming with a logical theory of statemodifying updates. It has both a modeltheoretic semantics and a procedural one. An implementation of a subset of Transaction logic is available in the Flora2 system. Other prototypes are also available.
See also
 Boolean satisfiability problem
 Constraint logic programming
 Datalog
 Functional programming
 Inductive logic programming
 Fuzzy logic
 Logic in computer science (includes Formal methods)
 Logic programming languages
 Programming paradigm
 R++
 Reasoning system
 Relational programming
 Satisfiability
References
This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. (February 2012) 
 ↑ ^{1.0} ^{1.1} T. Winograd (1972). "Understanding natural language". Cognitive psychology,. 3 (1): 1–191. doi:10.1016/00100285(72)900023.
 ↑ Cordell Green. Application of Theorem Proving to Problem Solving IJCAI 1969.
 ↑ J.M. Foster and E.W. Elcock. ABSYS 1: An Incremental Compiler for Assertions: an Introduction, Machine Intelligence 4, Edinburgh U Press, 1969, pp. 423–429
 ↑ Carl Hewitt. Planner: A Language for Proving Theorems in Robots IJCAI 1969.
 ↑ Pat Hayes. Computation and Deduction. In Proceedings of the 2nd MFCS Symposium. Czechoslovak Academy of Sciences, 1973, pp. 105–118.
 ↑ ^{6.0} ^{6.1} Robert Kowalski Predicate Logic as a Programming Language Memo 70, Department of Artificial Intelligence, Edinburgh University. 1973. Also in Proceedings IFIP Congress, Stockholm, North Holland Publishing Co., 1974, pp. 569–574.
 ↑ Robert Kowalski and Donald and Kuehner Linear Resolution with Selection Function Artificial Intelligence, Vol. 2, 1971, pp. 227–60.
 ↑ Shapiro, Ehud (1989). The family of concurrent logic programming languages (PDF). International Summer School on Logic, Algebra and Computation. Also appeared in Shapiro, E. (1989). "The family of concurrent logic programming languages". ACM Computing Surveys. 21 (3): 413. doi:10.1145/72551.72555.
 ↑ R.A.Kowalski (July 1979). "Algorithm=Logic + Control". Communications of the ACM. 22 (7): 424–436. doi:10.1145/359131.359136.
 ↑ Shunichi Uchida and Kazuhiro Fuchi Proceedings of the FGCS Project Evaluation Workshop Institute for New Generation Computer Technology (ICOT). 1992.
 ↑ Joshua Hodas and Dale Miller. Logic Programming in a Fragment of Intuitionistic Linear Logic, Information and Computation, 1994, 110(2), 327365.
 ↑ Naoki Kobayashi and Akinori Yonezawa. Asynchronous communication model based on linear logic, Formal Aspects of Computing, 1994, 279294.
General introductions
 Baral, C.; Gelfond, M. (1994). "Logic programming and knowledge representation" (PDF). The Journal of Logic Programming. 1920: 73. doi:10.1016/07431066(94)900256.
 Robert Kowalski. The Early Years of Logic Programming Kowalski, R. A. (1988). "The early years of logic programming" (PDF). Communications of the ACM. 31: 38. doi:10.1145/35043.35046.
 Lloyd, J. W. (1987). Foundations of Logic Programming. (2nd edition). SpringerVerlag.
Other sources
 John McCarthy. Programs with common sense Symposium on Mechanization of Thought Processes. National Physical Laboratory. Teddington, England. 1958.
 D. Miller, G. Nadathur, F. Pfenning, A. Scedrov. Uniform proofs as a foundation for logic programming, Annals of Pure and Applied Logic, vol. 51, pp 125–157, 1991.
 Ehud Shapiro (Editor). Concurrent Prolog MIT Press. 1987.
 James Slagle. Experiments with a Deductive QuestionAnswering Program CACM. December 1965.
Further reading
 Carl Hewitt. Procedural Embedding of Knowledge In Planner IJCAI 1971.
 Carl Hewitt. The repeated demise of logic programming and why it will be reincarnated What Went Wrong and Why: Lessons from AI Research and Applications. Technical Report SS0608. AAAI Press. March 2006.
 Evgeny Dantsin, Thomas Eiter, Georg Gottlob, Andrei Voronkov: Complexity and expressive power of logic programming. ACM Comput. Surv. 33(3): 374425 (2001)
 Ulf Nilsson and Jan Maluszynski, Logic, Programming and Prolog