Namespaces
Variants
Actions

Difference between revisions of "Logic programming"

From Encyclopedia of Mathematics
Jump to: navigation, search
(Importing text file)
 
m (tex encoded by computer)
 
Line 1: Line 1:
 +
<!--
 +
l1101701.png
 +
$#A+1 = 118 n = 0
 +
$#C+1 = 118 : ~/encyclopedia/old_files/data/L110/L.1100170 Logic programming,
 +
Automatically converted into TeX, above some diagnostics.
 +
Please remove this comment and the {{TEX|auto}} line below,
 +
if TeX found to be correct.
 +
-->
 +
 +
{{TEX|auto}}
 +
{{TEX|done}}
 +
 
''LP''
 
''LP''
  
Line 6: Line 18:
  
 
==Herbrand universa.==
 
==Herbrand universa.==
Suppose that <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l1101701.png" /> is a finite set of function symbols. Every symbol in <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l1101702.png" /> has an arity: a non-negative integer. Assume that <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l1101703.png" /> contains at least one <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l1101704.png" />-ary symbol (a constant symbol). The set <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l1101705.png" /> determines a corresponding Herbrand universe <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l1101706.png" /> of closed <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l1101708.png" />-terms, which is the least set <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l1101709.png" /> of formal expressions (built using the symbols from <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017010.png" />, brackets, and the comma) such that if <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017011.png" /> and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017012.png" /> is <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017013.png" />-ary, then <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017014.png" />. <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017015.png" /> constitutes the free <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017016.png" />-algebra in which a function symbol <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017017.png" /> is interpreted as the function that assigns the value <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017018.png" /> to arguments <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017019.png" />.
+
Suppose that $  {\mathcal F} $
 +
is a finite set of function symbols. Every symbol in $  {\mathcal F} $
 +
has an arity: a non-negative integer. Assume that $  {\mathcal F} $
 +
contains at least one 0 $-
 +
ary symbol (a constant symbol). The set $  {\mathcal F} $
 +
determines a corresponding Herbrand universe $  U _  {\mathcal F}  $
 +
of closed $  {\mathcal F} $-
 +
terms, which is the least set $  U $
 +
of formal expressions (built using the symbols from $  {\mathcal F} $,  
 +
brackets, and the comma) such that if $  t _ {1} \dots t _ {n} \in U $
 +
and $  \mathbf f \in {\mathcal F} $
 +
is $  n $-
 +
ary, then $  \mathbf f ( t _ {1} \dots t _ {n} ) \in U $.  
 +
$  U _  {\mathcal F}  $
 +
constitutes the free $  {\mathcal F} $-
 +
algebra in which a function symbol $  \mathbf f \in {\mathcal F} $
 +
is interpreted as the function that assigns the value $  \mathbf f ( t _ {1} \dots t _ {n} ) $
 +
to arguments $  t _ {1} \dots t _ {n} $.
  
Most data types can be viewed as Herbrand universa. For instance, the non-negative integers <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017020.png" /> can be identified with the terms <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017021.png" /> generated from one constant symbol <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017022.png" /> with the help of one unary function symbol <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017023.png" />. Similarly, one can view lists, trees, etc. as generated by means of suitable function symbols.
+
Most data types can be viewed as Herbrand universa. For instance, the non-negative integers 0,1,2, \dots $
 +
can be identified with the terms $  \mathbf o, \mathbf s ( \mathbf o ) , \mathbf s ( \mathbf s ( \mathbf o ) ) , \dots $
 +
generated from one constant symbol $  \mathbf o $
 +
with the help of one unary function symbol $  \mathbf s $.  
 +
Similarly, one can view lists, trees, etc. as generated by means of suitable function symbols.
  
 
==Horn-clause logic.==
 
==Horn-clause logic.==
The set of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017025.png" />-terms is generated from <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017026.png" /> in the same way as is <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017027.png" />, but this time allowing variable symbols <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017028.png" /> as terms as well.
+
The set of $  {\mathcal F} $-
 +
terms is generated from $  {\mathcal F} $
 +
in the same way as is $  U _  {\mathcal F}  $,  
 +
but this time allowing variable symbols $  x,y,z, \dots $
 +
as terms as well.
  
Let <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017029.png" /> be a finite set of relation symbols (with arities). An <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017031.png" />-atom is an expression of the form <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017032.png" /> where <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017033.png" /> is an <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017034.png" />-ary relation symbol and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017035.png" /> are terms.
+
Let $  {\mathcal R} $
 +
be a finite set of relation symbols (with arities). An $  {\mathcal R} $-
 +
atom is an expression of the form $  \mathbf r ( t _ {1} \dots t _ {n} ) $
 +
where $  \mathbf r $
 +
is an $  n $-
 +
ary relation symbol and $  t _ {1} \dots t _ {n} $
 +
are terms.
  
A positive literal is the same as an atom. A negative literal is the negation <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017036.png" /> of an atom <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017037.png" />.
+
A positive literal is the same as an atom. A negative literal is the negation $  \neg A $
 +
of an atom $  A $.
  
A clause is a finite sequence of (positive and/or negative) literals. Logically, a clause <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017038.png" /> is identified with the disjunctive formula <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017039.png" />, where variables are read as if they were quantified universally.
+
A clause is a finite sequence of (positive and/or negative) literals. Logically, a clause $  ( L _ {1} \dots L _ {k} ) $
 +
is identified with the disjunctive formula $  L _ {1} \lor \dots \lor L _ {k} $,  
 +
where variables are read as if they were quantified universally.
  
 
A rule or Horn clause is a clause in which exactly one literal is positive. Finally, a (logic) program is a finite set of rules.
 
A rule or Horn clause is a clause in which exactly one literal is positive. Finally, a (logic) program is a finite set of rules.
  
Note that a Horn clause like <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017040.png" /> is identified with the formula <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017041.png" />, which in turn is logically equivalent with the implication <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017042.png" />. It is logic programming custom to write this as
+
Note that a Horn clause like $  ( A, \neg B _ {1} \dots \neg B _ {k} ) $
 +
is identified with the formula $  A \lor \neg B _ {1} \lor \dots \lor \neg B _ {k} $,  
 +
which in turn is logically equivalent with the implication $  B _ {1} \wedge \dots \wedge B _ {k} \rightarrow A $.  
 +
It is logic programming custom to write this as
  
<table class="eq" style="width:100%;"> <tr><td valign="top" style="width:94%;text-align:center;"><img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017043.png" /></td> </tr></table>
+
$$
 +
A \leftarrow B _ {1} \dots B _ {k} ,
 +
$$
  
 
suggesting a ( "procedural" ) interpretation of rules as something to be executed ( "in order to execute A, execute B1…Bk" ).
 
suggesting a ( "procedural" ) interpretation of rules as something to be executed ( "in order to execute A, execute B1…Bk" ).
Line 30: Line 81:
 
logic programming started with the observation that many operations over a Herbrand universe can be described using a program. For instance, consider the simple case of addition over the non-negative integers. This operation is determined by the following two recursion equations:
 
logic programming started with the observation that many operations over a Herbrand universe can be described using a program. For instance, consider the simple case of addition over the non-negative integers. This operation is determined by the following two recursion equations:
  
<table class="eq" style="width:100%;"> <tr><td valign="top" style="width:94%;text-align:center;"><img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017044.png" /></td> </tr></table>
+
$$
 +
x + \mathbf o = x,
 +
$$
  
<table class="eq" style="width:100%;"> <tr><td valign="top" style="width:94%;text-align:center;"><img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017045.png" /></td> </tr></table>
+
$$
 +
x + \mathbf s ( y ) = \mathbf s ( x + y ) ,
 +
$$
  
which enable a unique evaluation of every sum of two numbers expressed in terms of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017046.png" /> and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017047.png" />. (E.g., <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017048.png" />.) The recursion equations transform into the following program involving a <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017049.png" />-ary relation symbol <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017050.png" />:
+
which enable a unique evaluation of every sum of two numbers expressed in terms of $  \mathbf o $
 +
and $  \mathbf s $.  
 +
(E.g., $  3 + 2 = 3 + \mathbf s ( 1 ) = \mathbf s ( 3 + 1 ) = \mathbf s ( 3 + \mathbf s ( \mathbf o ) ) = \mathbf s ( \mathbf s ( 3 + \mathbf o ) ) = \mathbf s ( \mathbf s ( 3 ) ) = 5 $.)  
 +
The recursion equations transform into the following program involving a $  3 $-
 +
ary relation symbol $  \mathbf{sum } $:
  
<table class="eq" style="width:100%;"> <tr><td valign="top" style="width:94%;text-align:center;"><img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017051.png" /></td> </tr></table>
+
$$
 +
\mathbf{sum } ( x, \mathbf o,x ) \leftarrow
 +
$$
  
<table class="eq" style="width:100%;"> <tr><td valign="top" style="width:94%;text-align:center;"><img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017052.png" /></td> </tr></table>
+
$$
 +
\mathbf{sum } ( x, \mathbf s ( y ) , \mathbf s ( z ) ) \leftarrow \mathbf{sum } ( x,y,z ) .
 +
$$
  
In what way do these rules express addition? First, they are clearly satisfied by the Herbrand model that consists of the Herbrand universe <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017053.png" /> (the free algebra over <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017054.png" /> and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017055.png" />) supplied with the graph of the addition operation as a meaning for the symbol <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017056.png" />. However, there are many more relations that similarly make <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017057.png" /> into a Herbrand model of this program. Nevertheless, addition here stands out: it happens to make <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017058.png" /> into the least Herbrand model (having fewest true atoms) of this program.
+
In what way do these rules express addition? First, they are clearly satisfied by the Herbrand model that consists of the Herbrand universe $  U _ {\{ \mathbf o, \mathbf s \} }  $(
 +
the free algebra over $  \mathbf o $
 +
and $  \mathbf s $)  
 +
supplied with the graph of the addition operation as a meaning for the symbol $  \mathbf{sum } $.  
 +
However, there are many more relations that similarly make $  U _ {\{ \mathbf o, \mathbf s \} }  $
 +
into a Herbrand model of this program. Nevertheless, addition here stands out: it happens to make $  U _ {\{ \mathbf o, \mathbf s \} }  $
 +
into the least Herbrand model (having fewest true atoms) of this program.
  
The following computational completeness theorem holds: For every computable operation over <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017059.png" /> one can construct a program such that in its least Herbrand model the graph of the operation occurs as meaning of one of the relation symbols.
+
The following computational completeness theorem holds: For every computable operation over $  U _  {\mathcal F}  $
 +
one can construct a program such that in its least Herbrand model the graph of the operation occurs as meaning of one of the relation symbols.
  
 
==Execution of programs.==
 
==Execution of programs.==
 
The above explains that least Herbrand models can be viewed as meanings of programs and that every computable operation surfaces in such a meaning. However, programs are meant to be executed. This is where the notion of linear resolution comes in.
 
The above explains that least Herbrand models can be viewed as meanings of programs and that every computable operation surfaces in such a meaning. However, programs are meant to be executed. This is where the notion of linear resolution comes in.
  
A substitution is a finite function that maps variables to terms. If <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017060.png" /> is an expression and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017061.png" /> a substitution, then <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017062.png" /> is the expression obtained from <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017063.png" /> by replacing every variable from the domain of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017064.png" /> occurring in <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017065.png" /> by its <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017066.png" />-image.
+
A substitution is a finite function that maps variables to terms. If $  E $
 +
is an expression and $  \alpha $
 +
a substitution, then $  E \alpha $
 +
is the expression obtained from $  E $
 +
by replacing every variable from the domain of $  \alpha $
 +
occurring in $  E $
 +
by its $  \alpha $-
 +
image.
  
The substitution <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017067.png" /> unifies the expressions <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017068.png" /> and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017069.png" /> if <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017070.png" />. It is a most general unifier (mgu) of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017071.png" /> and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017072.png" /> if it unifies them and for every other unifier <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017073.png" /> there exists a substitution <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017074.png" /> such that <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017075.png" />.
+
The substitution $  \alpha $
 +
unifies the expressions $  E _ {1} $
 +
and $  E _ {2} $
 +
if $  E _ {1} \alpha = E _ {2} \alpha $.  
 +
It is a most general unifier (mgu) of $  E _ {1} $
 +
and $  E _ {2} $
 +
if it unifies them and for every other unifier $  \beta $
 +
there exists a substitution $  \gamma $
 +
such that $  \beta = \alpha \gamma $.
  
 
The following unification theorem holds: If two expressions have a unifier, then they have an mgu as well. Both unifiability and mgu can be determined algorithmically.
 
The following unification theorem holds: If two expressions have a unifier, then they have an mgu as well. Both unifiability and mgu can be determined algorithmically.
Line 57: Line 142:
 
A query represents a condition to be solved. Linear resolution aims at finding such solutions.
 
A query represents a condition to be solved. Linear resolution aims at finding such solutions.
  
The notation <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017076.png" />, query <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017077.png" /> resolves to <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017078.png" /> via mgu <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017079.png" />, is used to describe the circumstance that for some atom <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017080.png" /> of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017081.png" /> and some rule <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017082.png" /> of the program under consideration:
+
The notation $  Q _ {1} {\mapsto ^  \alpha  } Q _ {2} $,  
 +
query $  Q _ {1} $
 +
resolves to $  Q _ {2} $
 +
via mgu $  \alpha $,  
 +
is used to describe the circumstance that for some atom $  A $
 +
of $  Q _ {1} $
 +
and some rule $  B \leftarrow B _ {1} \dots B _ {k} $
 +
of the program under consideration:
  
a) <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017083.png" /> is an mgu of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017084.png" /> and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017085.png" />,
+
a) $  \alpha $
 +
is an mgu of $  A $
 +
and $  B $,
  
b) <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017086.png" /> is obtained from <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017087.png" /> by replacing <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017088.png" /> by <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017089.png" />.
+
b) $  Q _ {2} $
 +
is obtained from $  Q _ {1} \alpha $
 +
by replacing $  A \alpha $
 +
by $  B _ {1} \alpha \dots B _ {k} \alpha $.
  
A derivation of the query <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017090.png" /> relative to a program is a finite sequence
+
A derivation of the query $  Q $
 +
relative to a program is a finite sequence
  
<table class="eq" style="width:100%;"> <tr><td valign="top" style="width:94%;text-align:center;"><img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017091.png" /></td> </tr></table>
+
$$
 +
Q _ {0} = Q {\mapsto ^ { {\alpha _ 1} } } Q _ {1} {\mapsto ^ { {\alpha _ 2} } } \dots {\mapsto ^ { {\alpha _ n} } } Q _ {n}  $$
  
 
where every step employs a variant of a rule of the program in which all variables are fresh (i.e., not entering earlier steps).
 
where every step employs a variant of a rule of the program in which all variables are fresh (i.e., not entering earlier steps).
  
Such a derivation is successful if its last query is empty. As an example, here is a successful derivation of the atomic query <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017092.png" /> (that  "asks"  for the sum <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017093.png" /> of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017094.png" /> and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017095.png" />) relative to the program of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017096.png" /> rules given earlier. Numbers are identified with suitable terms and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017097.png" /> indicates substituting <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017098.png" /> for <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l11017099.png" />.
+
Such a derivation is successful if its last query is empty. As an example, here is a successful derivation of the atomic query $  \mathbf{sum } ( 3,2,y ) $(
 +
that  "asks"  for the sum $  y $
 +
of $  3 $
 +
and $  2 $)  
 +
relative to the program of $  \mathbf{sum } $
 +
rules given earlier. Numbers are identified with suitable terms and $  y/t $
 +
indicates substituting $  t $
 +
for $  y $.
  
<table class="eq" style="width:100%;"> <tr><td valign="top" style="width:94%;text-align:center;"><img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170100.png" /></td> </tr></table>
+
$$
 +
\mathbf{sum } ( 3,2,y ) {\mapsto ^ { {y/ }  \mathbf s ( y ) } } \mathbf{sum } ( 3,1,y ) {\mapsto ^ { {y/ }  \mathbf s ( y ) } }
 +
$$
  
<table class="eq" style="width:100%;"> <tr><td valign="top" style="width:94%;text-align:center;"><img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170101.png" /></td> </tr></table>
+
$$
 +
{\mapsto ^ { {y/ }  \mathbf s ( y ) } } \mathbf{sum } ( 3, \mathbf o,y ) {\mapsto ^ { {y/3 }  } } \emptyset
 +
$$
  
 
The first two steps of this derivation are executed using the second rule, whereas the last step employs the first one. Only the relevant portions of the mgu's are indicated.
 
The first two steps of this derivation are executed using the second rule, whereas the last step employs the first one. Only the relevant portions of the mgu's are indicated.
  
Composing the three mgu's in this example results in the computed answer substitution of the derivation that sets <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170102.png" /> to <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170103.png" />, i.e., to <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170104.png" />. The derivation is nothing but a transform of the earlier computation showing that <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170105.png" /> and <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170106.png" /> add up to <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170107.png" />.
+
Composing the three mgu's in this example results in the computed answer substitution of the derivation that sets $  y $
 +
to $  \mathbf s ( \mathbf s ( 3 ) ) $,  
 +
i.e., to $  5 $.  
 +
The derivation is nothing but a transform of the earlier computation showing that $  3 $
 +
and $  2 $
 +
add up to $  5 $.
  
 
==Correspondence.==
 
==Correspondence.==
 
Meanings of programs and executions (computations) correspond in the following way (cf. the example derivation), thereby witnessing the successful accomplishment of the logic programming ideal in this context.
 
Meanings of programs and executions (computations) correspond in the following way (cf. the example derivation), thereby witnessing the successful accomplishment of the logic programming ideal in this context.
  
The soundness theorem: If <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170108.png" /> is the computed answer substitution of a successful derivation of the query <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170109.png" /> via a program, then <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170110.png" /> follows logically from that program.
+
The soundness theorem: If $  \alpha $
 +
is the computed answer substitution of a successful derivation of the query $  Q $
 +
via a program, then $  Q \alpha $
 +
follows logically from that program.
  
 
The converse is almost true.
 
The converse is almost true.
  
The completeness theorem: If <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170111.png" /> follows logically from a program, then there exists a computed answer substitution <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170112.png" /> of a successful derivation of <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170113.png" /> via that program such that for some substitution <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170114.png" />, <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170115.png" />.
+
The completeness theorem: If $  Q \alpha $
 +
follows logically from a program, then there exists a computed answer substitution $  \beta $
 +
of a successful derivation of $  Q $
 +
via that program such that for some substitution $  \gamma $,  
 +
$  Q \alpha = Q \beta \gamma $.
  
(The reason that <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170116.png" /> itself may not be computed is that resolution produces answers that are  "as general as possible" .)
+
(The reason that $  \alpha $
 +
itself may not be computed is that resolution produces answers that are  "as general as possible" .)
  
 
==Prolog.==
 
==Prolog.==
Line 106: Line 229:
  
 
===Negation.===
 
===Negation.===
Although the logic programming model as described above is computationally complete, in many cases the need arises for more expressibility. In particular, it is sometimes desirable to allow rules <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170117.png" /> and queries <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170118.png" /> where one or more of the literals <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170119.png" /> carry a negation symbol. Prolog does offer this possibility.
+
Although the logic programming model as described above is computationally complete, in many cases the need arises for more expressibility. In particular, it is sometimes desirable to allow rules $  A \leftarrow B _ {1} \dots B _ {k} $
 +
and queries $  B _ {1} \dots B _ {k} $
 +
where one or more of the literals $  B _ {1} \dots B _ {k} $
 +
carry a negation symbol. Prolog does offer this possibility.
  
A main problem is that this move destroys the logic programming ideal: the intuitive meaning of such rules is no longer evident, although several proposals (some employing [[Many-valued logic|many-valued logic]]) have tried to restore this deficiency. The second problem, that of execution, is dealt with by Prolog in a practical way. A negated atom <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170120.png" /> is resolved successfully only if the tree of derivations for the atom <img align="absmiddle" border="0" src="https://www.encyclopediaofmath.org/legacyimages/l/l110/l110170/l110170121.png" /> has no infinite branch and no successful derivation.
+
A main problem is that this move destroys the logic programming ideal: the intuitive meaning of such rules is no longer evident, although several proposals (some employing [[Many-valued logic|many-valued logic]]) have tried to restore this deficiency. The second problem, that of execution, is dealt with by Prolog in a practical way. A negated atom $  \neg B $
 +
is resolved successfully only if the tree of derivations for the atom $  B $
 +
has no infinite branch and no successful derivation.
  
 
Although this way of executing negations has many useful applications, the general meaning of this way of handling negation remains problematic.
 
Although this way of executing negations has many useful applications, the general meaning of this way of handling negation remains problematic.

Latest revision as of 04:11, 6 June 2020


LP

Logic programming came into existence in the early 1970s. Its ideal is to write programs in the language of logic, thereby aiming at an immediately obvious intuitive meaning, whereas execution can be left to an interpreter.

This ideal has been realized to a great extent in the case of positive logic programming, which forms the common core of systems of logic programming. The logical language employed here is that of Horn-clause (first-order) logic. Programs can be executed by linear resolution, which is the special case of the resolution procedure that not only is capable of refuting but has computing power at the same time.

Herbrand universa.

Suppose that $ {\mathcal F} $ is a finite set of function symbols. Every symbol in $ {\mathcal F} $ has an arity: a non-negative integer. Assume that $ {\mathcal F} $ contains at least one $ 0 $- ary symbol (a constant symbol). The set $ {\mathcal F} $ determines a corresponding Herbrand universe $ U _ {\mathcal F} $ of closed $ {\mathcal F} $- terms, which is the least set $ U $ of formal expressions (built using the symbols from $ {\mathcal F} $, brackets, and the comma) such that if $ t _ {1} \dots t _ {n} \in U $ and $ \mathbf f \in {\mathcal F} $ is $ n $- ary, then $ \mathbf f ( t _ {1} \dots t _ {n} ) \in U $. $ U _ {\mathcal F} $ constitutes the free $ {\mathcal F} $- algebra in which a function symbol $ \mathbf f \in {\mathcal F} $ is interpreted as the function that assigns the value $ \mathbf f ( t _ {1} \dots t _ {n} ) $ to arguments $ t _ {1} \dots t _ {n} $.

Most data types can be viewed as Herbrand universa. For instance, the non-negative integers $ 0,1,2, \dots $ can be identified with the terms $ \mathbf o, \mathbf s ( \mathbf o ) , \mathbf s ( \mathbf s ( \mathbf o ) ) , \dots $ generated from one constant symbol $ \mathbf o $ with the help of one unary function symbol $ \mathbf s $. Similarly, one can view lists, trees, etc. as generated by means of suitable function symbols.

Horn-clause logic.

The set of $ {\mathcal F} $- terms is generated from $ {\mathcal F} $ in the same way as is $ U _ {\mathcal F} $, but this time allowing variable symbols $ x,y,z, \dots $ as terms as well.

Let $ {\mathcal R} $ be a finite set of relation symbols (with arities). An $ {\mathcal R} $- atom is an expression of the form $ \mathbf r ( t _ {1} \dots t _ {n} ) $ where $ \mathbf r $ is an $ n $- ary relation symbol and $ t _ {1} \dots t _ {n} $ are terms.

A positive literal is the same as an atom. A negative literal is the negation $ \neg A $ of an atom $ A $.

A clause is a finite sequence of (positive and/or negative) literals. Logically, a clause $ ( L _ {1} \dots L _ {k} ) $ is identified with the disjunctive formula $ L _ {1} \lor \dots \lor L _ {k} $, where variables are read as if they were quantified universally.

A rule or Horn clause is a clause in which exactly one literal is positive. Finally, a (logic) program is a finite set of rules.

Note that a Horn clause like $ ( A, \neg B _ {1} \dots \neg B _ {k} ) $ is identified with the formula $ A \lor \neg B _ {1} \lor \dots \lor \neg B _ {k} $, which in turn is logically equivalent with the implication $ B _ {1} \wedge \dots \wedge B _ {k} \rightarrow A $. It is logic programming custom to write this as

$$ A \leftarrow B _ {1} \dots B _ {k} , $$

suggesting a ( "procedural" ) interpretation of rules as something to be executed ( "in order to execute A, execute B1…Bk" ).

Meaning of programs.

logic programming started with the observation that many operations over a Herbrand universe can be described using a program. For instance, consider the simple case of addition over the non-negative integers. This operation is determined by the following two recursion equations:

$$ x + \mathbf o = x, $$

$$ x + \mathbf s ( y ) = \mathbf s ( x + y ) , $$

which enable a unique evaluation of every sum of two numbers expressed in terms of $ \mathbf o $ and $ \mathbf s $. (E.g., $ 3 + 2 = 3 + \mathbf s ( 1 ) = \mathbf s ( 3 + 1 ) = \mathbf s ( 3 + \mathbf s ( \mathbf o ) ) = \mathbf s ( \mathbf s ( 3 + \mathbf o ) ) = \mathbf s ( \mathbf s ( 3 ) ) = 5 $.) The recursion equations transform into the following program involving a $ 3 $- ary relation symbol $ \mathbf{sum } $:

$$ \mathbf{sum } ( x, \mathbf o,x ) \leftarrow $$

$$ \mathbf{sum } ( x, \mathbf s ( y ) , \mathbf s ( z ) ) \leftarrow \mathbf{sum } ( x,y,z ) . $$

In what way do these rules express addition? First, they are clearly satisfied by the Herbrand model that consists of the Herbrand universe $ U _ {\{ \mathbf o, \mathbf s \} } $( the free algebra over $ \mathbf o $ and $ \mathbf s $) supplied with the graph of the addition operation as a meaning for the symbol $ \mathbf{sum } $. However, there are many more relations that similarly make $ U _ {\{ \mathbf o, \mathbf s \} } $ into a Herbrand model of this program. Nevertheless, addition here stands out: it happens to make $ U _ {\{ \mathbf o, \mathbf s \} } $ into the least Herbrand model (having fewest true atoms) of this program.

The following computational completeness theorem holds: For every computable operation over $ U _ {\mathcal F} $ one can construct a program such that in its least Herbrand model the graph of the operation occurs as meaning of one of the relation symbols.

Execution of programs.

The above explains that least Herbrand models can be viewed as meanings of programs and that every computable operation surfaces in such a meaning. However, programs are meant to be executed. This is where the notion of linear resolution comes in.

A substitution is a finite function that maps variables to terms. If $ E $ is an expression and $ \alpha $ a substitution, then $ E \alpha $ is the expression obtained from $ E $ by replacing every variable from the domain of $ \alpha $ occurring in $ E $ by its $ \alpha $- image.

The substitution $ \alpha $ unifies the expressions $ E _ {1} $ and $ E _ {2} $ if $ E _ {1} \alpha = E _ {2} \alpha $. It is a most general unifier (mgu) of $ E _ {1} $ and $ E _ {2} $ if it unifies them and for every other unifier $ \beta $ there exists a substitution $ \gamma $ such that $ \beta = \alpha \gamma $.

The following unification theorem holds: If two expressions have a unifier, then they have an mgu as well. Both unifiability and mgu can be determined algorithmically.

A query is a finite sequence of atoms. In particular, the empty sequence is a query. The intuitive meaning of a query is the conjunction of its atoms (variables conceived of as universally quantified). Therefore, the empty query stands for the logically true formula.

A query represents a condition to be solved. Linear resolution aims at finding such solutions.

The notation $ Q _ {1} {\mapsto ^ \alpha } Q _ {2} $, query $ Q _ {1} $ resolves to $ Q _ {2} $ via mgu $ \alpha $, is used to describe the circumstance that for some atom $ A $ of $ Q _ {1} $ and some rule $ B \leftarrow B _ {1} \dots B _ {k} $ of the program under consideration:

a) $ \alpha $ is an mgu of $ A $ and $ B $,

b) $ Q _ {2} $ is obtained from $ Q _ {1} \alpha $ by replacing $ A \alpha $ by $ B _ {1} \alpha \dots B _ {k} \alpha $.

A derivation of the query $ Q $ relative to a program is a finite sequence

$$ Q _ {0} = Q {\mapsto ^ { {\alpha _ 1} } } Q _ {1} {\mapsto ^ { {\alpha _ 2} } } \dots {\mapsto ^ { {\alpha _ n} } } Q _ {n} $$

where every step employs a variant of a rule of the program in which all variables are fresh (i.e., not entering earlier steps).

Such a derivation is successful if its last query is empty. As an example, here is a successful derivation of the atomic query $ \mathbf{sum } ( 3,2,y ) $( that "asks" for the sum $ y $ of $ 3 $ and $ 2 $) relative to the program of $ \mathbf{sum } $ rules given earlier. Numbers are identified with suitable terms and $ y/t $ indicates substituting $ t $ for $ y $.

$$ \mathbf{sum } ( 3,2,y ) {\mapsto ^ { {y/ } \mathbf s ( y ) } } \mathbf{sum } ( 3,1,y ) {\mapsto ^ { {y/ } \mathbf s ( y ) } } $$

$$ {\mapsto ^ { {y/ } \mathbf s ( y ) } } \mathbf{sum } ( 3, \mathbf o,y ) {\mapsto ^ { {y/3 } } } \emptyset $$

The first two steps of this derivation are executed using the second rule, whereas the last step employs the first one. Only the relevant portions of the mgu's are indicated.

Composing the three mgu's in this example results in the computed answer substitution of the derivation that sets $ y $ to $ \mathbf s ( \mathbf s ( 3 ) ) $, i.e., to $ 5 $. The derivation is nothing but a transform of the earlier computation showing that $ 3 $ and $ 2 $ add up to $ 5 $.

Correspondence.

Meanings of programs and executions (computations) correspond in the following way (cf. the example derivation), thereby witnessing the successful accomplishment of the logic programming ideal in this context.

The soundness theorem: If $ \alpha $ is the computed answer substitution of a successful derivation of the query $ Q $ via a program, then $ Q \alpha $ follows logically from that program.

The converse is almost true.

The completeness theorem: If $ Q \alpha $ follows logically from a program, then there exists a computed answer substitution $ \beta $ of a successful derivation of $ Q $ via that program such that for some substitution $ \gamma $, $ Q \alpha = Q \beta \gamma $.

(The reason that $ \alpha $ itself may not be computed is that resolution produces answers that are "as general as possible" .)

Prolog.

Linear resolution is the core of the Prolog system, which was developed more or less independently from, but at the same time as, logic programming theory. Thus, logic programming can be viewed as the theoretical basis underlying Prolog. However, many divergencies exist between logic programming theory and Prolog practice.

Occur check.

A relatively small difference is the fact that the Prolog implementation of the mgu algorithm is incorrect. For reasons of cheapness it omits the so-called occur check. In most situations in practice, though, this incorrectness will not surface.

Search strategy.

At every stage in the construction of a derivation, two choices have to be made:

i) which of the atoms in the given query should be resolved;

ii) which of the rules of the program should be employed to accomplish this. (The example derivation does not illustrate these choices.)

It can be shown that the choice of i) has no influence on the final outcome. (If a successful derivation exists, one with the same computed answer substitution will be found no matter how i) is dealt with. Usual Prolog implementations always resolve left-most atoms.) However, choice of ii) is of utmost importance. Given a selection rule that handles i), all derivations for a query via a given program form a tree. The tree splits if more than one rule is applicable to the selected atom. A branch through the tree can be successful (ending at the empty query), unsuccessful (ending at a non-empty query the selected atom of which is not resolvable using any rule), or infinite. Prolog's search of this tree is "depth-first" (cf. also Depth-first search), employing at every step the first rule in the listing of the program that is applicable to the selected (i.e., left-most) atom. At the end of an unsuccessful branch it backtracks to the last choice and tries the next rule. It goes without saying that Prolog may follow an infinite branch all the way, and, so doing, can miss a successful branch. This brings along the important question of which programs terminate on which queries.

Negation.

Although the logic programming model as described above is computationally complete, in many cases the need arises for more expressibility. In particular, it is sometimes desirable to allow rules $ A \leftarrow B _ {1} \dots B _ {k} $ and queries $ B _ {1} \dots B _ {k} $ where one or more of the literals $ B _ {1} \dots B _ {k} $ carry a negation symbol. Prolog does offer this possibility.

A main problem is that this move destroys the logic programming ideal: the intuitive meaning of such rules is no longer evident, although several proposals (some employing many-valued logic) have tried to restore this deficiency. The second problem, that of execution, is dealt with by Prolog in a practical way. A negated atom $ \neg B $ is resolved successfully only if the tree of derivations for the atom $ B $ has no infinite branch and no successful derivation.

Although this way of executing negations has many useful applications, the general meaning of this way of handling negation remains problematic.

Prolog has many extra features (such as the cut, of which negation is but one application), most of which are equally cooperative in destroying the logic programming ideal described above. The problem to close the gap between logic programming theory and Prolog practice is a persistent one.

Where to look.

logic programming has its own periodical: the Journal of Logic Programming. A few introductory texts are [a1], [a5] and [a3]. The problem of negation in logic programming is reviewed in [a2]. One of the many texts on Prolog is [a7]. The history of logic programming theory can be traced to [a4]. The general notion of resolution, in particular that of most general unification, is due to [a6].

References

[a1] K.R. Apt, "Logic programming" J. van Leeuwen (ed.) , Handbook of Theoretical Computer Science , Elsevier (1990) pp. 493–574
[a2] K.R. Apt, R. Bol, "Logic programming and negation: a survey" J. Logic Programming , 19–20 (1994) pp. 9–71
[a3] K. Doets, "From logic to logic programming" , MIT (1994)
[a4] R.A. Kowalski, "Predicate logic as a programming language" , Proceedings IFIP'74 , North-Holland (1974) pp. 569–574
[a5] J.W. Lloyd, "Foundations of logic programming" , Springer (1987) (Edition: Second, extended)
[a6] J.A. Robinson, "A machine-oriented logic based on the resolution principle" J. ACM , 12 (1965) pp. 23–41
[a7] L. Sterling, E. Shapiro, "The art of Prolog" , MIT (1994) (Edition: Second)
How to Cite This Entry:
Logic programming. Encyclopedia of Mathematics. URL: http://encyclopediaofmath.org/index.php?title=Logic_programming&oldid=15538
This article was adapted from an original article by K. Doets (originator), which appeared in Encyclopedia of Mathematics - ISBN 1402006098. See original article