Operational semantics of resolution and productivity in Horn clause logic

This paper presents a study of operational and type-theoretic properties of different resolution strategies in Horn clause logic. We distinguish four different kinds of resolution: resolution by unification (SLD-resolution), resolution by term-matching, the recently introduced structural resolution, and partial (or lazy) resolution. We express them all uniformly as abstract reduction systems, which allows us to undertake a thorough comparative analysis of their properties. To match this small-step semantics, we propose to take Howard’s System H as a type-theoretic semantic counterpart. Using System H, we interpret Horn formulas as types, and a derivation for a given formula as the proof term inhabiting the type given by the formula. We prove soundness of these abstract reduction systems relative to System H, and we show completeness of SLD-resolution and structural resolution relative to System H. We identify conditions under which structural resolution is operationally equivalent to SLD-resolution. We show correspondence between term-matching resolution for Horn clause programs without existential variables and term rewriting.


Introduction
Horn clause logic is a fragment of first-order logic that gives theoretical foundation to logic programming.A set of Horn clauses is called a logic program.SLD-resolution is the most common algorithm in logic programming for automatically inferring whether, given a logic program and a first-order formula A, σ A holds for some substitution σ .SLD-resolution is semi-decidable as not all derivations by SLD-resolution terminate.Terminating SLD-resolution is quite well understood (see for example the textbook by Lloyd [Llo87]), and SLD-resolution has been successfully incorporated into a number of logic programming language implementations.However, nonterminating SLD-resolution is more challenging to handle.
The importance of developing approaches for computing with infinite data structures in logic programming has been argued by van Emden, Lloyd, et al [Llo87] in the 80s and more recently the topic has been revived by Gupta, Simon et al. [GBM + 07, SBMG07].In the classical approach [Llo87], a semantic view was taken: if a nonterminating SLD-resolution derivation for and A accumulates computed substitutions σ 0 , σ 1 , . . . in such a way that (. . .(σ 1 (σ 0 (A)))) is an infinite ground formula, then (. . .(σ 1 (σ 0 (A)))) is said to be computable at infinity.Computation at infinity is proven to be sound with respect to the greatest Herbrand model, i.e., given a logic program , if a formula A is computable at infinity with respect to , then A is also in the greatest Herbrand model of .Importantly for us, the notion of infinite formula computed at infinity captures the modern-day notion of producing an infinite data structure.We will use the terminology global productivity to describe computation at infinity.For example, the derivation shown in Example 1 is globally productive, as it computes the infinite stream of zeros at infinity.However, this approach did not result in implementation and in general proving global productivity is nontrivial.
An alternative approach has been proposed by Gupta, Simon et al. [GBM + 07, SBMG07]: subgoals produced in the course of an infinite SLD-derivation can be memorized, and if any two subgoals are unifiable, then the derivation is said to be closed coinductively.This approach was implemented as an extension to Prolog and called CoLP (Coinductive Logic Programming).Its applications are limited, as CoLP does not terminate for the SLD derivation that produces an infinite formula with irrational tree structure, as in this case the derivation does not feature any unifiable subgoals.CoLP's approach was not intended to capture the notion of global productivity.For example, the query P (x ) for the clause P (y) ⇒ P (y) will exhibit a cycle and will be coinductively proven by CoLP, but it will not compute an infinite formula at infinity.In other words, the derivation for P (x ) is not globally productive despite being coinductively provable by CoLP.
In this paper, we introduce yet another approach to the potentially infinite derivations by the SLD-resolution.When SLD-resolution produces a finite or infinite ground answer for a variable in the query, we say the query is locally productive at that variable (see Definition 12).This gives us an alternative notion of productivity for logic programming.In order to formally define this notion of local productivity, we introduce a lazy version of resolution (called partial resolution).Firstly, we label those variables in the queries for which we want to compute substitutions.Partial resolution then takes the labels into account when performing the computation, by giving priority to subgoals with labelled variables.The resolution will stop when all the labels in the queries are eliminated, or in other words, when all required substitutions have been computed.
Finally, a third notion of productivity for logic programming, an observational productivity, has been recently introduced by Johann, Komendantskaya et al. [JKK15,KJM16].It depends on a new kind of resolution (structural resolution).Structural resolution depends crucially on term-matching resolution, obtained by restricting unification used in the SLD-resolution to term matching.Term-matching resolution is used in e.g.type class resolution [JJM97] in functional programming.It has different operational properties compared to the SLD-resolution.For example, taking the program in Example 1, the query Stream(x ) can not be reduced by term-matching resolution, as it is not possible to match Stream(Cons(0, y)) with Stream(x ).
Note that the overall derivation is nonterminating, but all term-matching derivations are finite in the above resolution trace.This separation of term-matching and unification allows the formulation of observational productivity: given a program and a query A, if a derivation for A is infinite, and it features only terminating term-matching resolution steps, then this derivation is called observationally productive.As discussed by Komendantskaya, Johann et al. [KJ15,KJM16] observational productivity implies global productivity.
To illustrate these three notions of productivity, we consider three logic programs in the following table.• Program 1 is not productive for the query P (x ) by any of these three notions of productivity: it does not compute an infinite ground formula, it is not terminating by term-matching resolution, and it does not compute a ground answer for x .
• Program 2 is globally productive for the query P (x ) as it computes an infinite formula P (K (K (. ..))).We can see that 2 is observationally productive because it is terminating by term-matching resolution.Also, 2 is locally productive at x for the query P (x ) since SLD-resolution computes a ground infinite answer K (K (. ..)) for the variable x .
• Program 3 is not globally productive for the query P (x , y) as SLD-resolution computes an infinite formula P (x , G(G(. ..))) that is not ground.It is observationally productive, since the second argument for P is decreasing from right to left by the subterm relation, i.e. y < subterm G(y) and that ensures termination of term-matching resolution.Note that 3 is not locally productive at x for the query P (x , y), but it is locally productive at y since G(G(. ..)) is an infinite ground answer for y.
In this paper, we establish a framework for a comparative analysis of different kinds of resolution and different notions of productivity.Firstly, we use a uniform style of small-step semantics for all these kinds of resolution and formulate them as abstract reduction systems.We call the resulting abstract reduction systems LP-Unif, partial LP-Unif, LP-TM, and LP-Struct, respectively.Using this framework, we ask and answer several research questions about operational properties and relations of these reduction systems.Are LP-Unif and LP-Struct equivalent for terminating derivations, and under what conditions?Are LP-Unif and LP-Struct equivalent for observationally productive programs?Since the termination of LP-TM is essential for the observational productivity, are there any suitable program transformation methods to ensure LP-TM termination?
We give a type-theoretic semantics to all these reduction systems.Notably, we take System H (based on Howard's work [How80]) as a calculus to capture the type-theoretic meaning of logic programming.We show that LP-Unif, partial LP-Unif, LP-TM and LP-Struct are sound relative to System H.Moreover, LP-Unif is complete relative to System H, and under a meaning preserving transformation, LP-Struct is also complete relative to System H.
We discover that, given a program and a formula A, LP-Struct is operationally equivalent to LP-Unif under two conditions: when all LP-Struct derivations for are observaionally productive and when all clauses in are non-overlapping.Thus the termination of LP-TM plays a crucial role not only in ensuring observational productivity, but also in ensuring the operational equivalence of LP-Struct and LP-Unif, which in its turn is crucial for our proofs of soundness and completeness of LP-Struct with respect to H.
We give a formal analysis of properties of LP-TM resolution.We show how LP-TM relates to term rewriting systems by introducing a transformation method that translates any logic program without existential variables into a term rewriting system (we call this process functionalisation).After functionalisation, standard term rewriting methods for detecting termination can be applied.We also give an alternative transformational method that renders all logic programs LP-TM terminating and non-overlapping.The method is related to Kleene's realizability method [Kle52], and we therefore call it realizability transformation.
The technical content of this paper is organized as follows.
• In Sect.2, we prove soundness and completeness of LP-Unif with respect to the type system H.This means H can be used to model logic programming.
• In Sect.3, we formally define partial resolution as an abstract reduction system and call it partial LP-Unif.
Based on this formalism we define local productivity.Partial LP-Unif provides a possibility of shifting the focus from deciding entailment for a given query to computing substitution answers.• In Sect.4, we formally define LP-Struct and identify two conditions that ensure that LP-Struct is operationally equivalent to LP-Unif.• In Sect.5, we define functionalisation and show the exact relation of LP-TM to term rewriting systems.We use existing termination detection techniques from term rewriting to detect termination of LP-TM.• In Sect.6, we define realizability transformation and show that this transformation preserves the operational meaning of a logic program.We use it to show the equivalence of LP-Struct and LP-Unif for the transformed program.As a corollary, we obtain the soundness and completeness of LP-Struct relative to System H for the transformed program.
Finally, in Sects.7 and 8 we survey related work and conclude the paper.

Horn formulas as types
In this section, we use Howard's type system H to model logic programming.We use an abstract reduction system (called LP-Unif) to model the small-step semantics of the SLD-resolution.The purpose of this section is to set up a type-theoretic framework for the rest of the paper, where Horn formulas are viewed as types in a type system and resolution corresponds to the proof construction.We show the correspondence between the small-step semantics of resolution and System H.This result can be viewed as an alternative to the classical-style soundness and completeness results for the SLD-resolution relative to Herbrand models.Using System H as an alternative semantics for logic programming may be beneficial in two ways: (1) the usual notion of an unsuccessful SLD-derivation can be understood as proving an implicative formula in which the unresolved subgoals comprise the antecedent (see Lemma 1).(2) It allows further extensions such as adding fixpoint typing rule by Fu et al [FKSP16], which provides proofs for some nonterminating computations.Note that the type for the constant in the rule Axiom is required to be Horn formula.It has been observed that the Cut rule and proper axioms in intuitionistic sequent calculus can emulate logic programming [GTL89] (Sect.13.4).
The following rule is a version of Cut rule, working only with Horn formulas.We can use rules Abs and App to emulate Cut rule, thus the Cut rule is admissible in Howard's system H.We will use C to denote the deduction system that consists of rules Axiom, Cut, Inst, and Gen.The subsystem C provides a natural framework to work with Horn formulas, but H is more expressive, since it allows full intuitionistic formulas, e.g.H would allow a formula of the form (F 1 ⇒ F 2 ) ⇒ F 3 .
Definition 3 Beta-reduction on proof evidence is defined as the congruence closure of the following relation: (λ a.e) e → β [e /a]e.We say a proof evidence e is strongly normalizing if e admits no infinite → β -reductions.
The following three theorems are standard for a type system such as H.For proofs we refer the reader to Barendregt's excellent book [Bar92].Note that system C as a type system is not type preserving.Definition 4 (Unification) We say that t is unifiable with t with substitution γ (denoted t ∼ γ t ), if {t t } * γ according to the following rules: Unification can be routinely extended to atomic formulas.The symbol ⊥ denotes failure of unification.The following is a formulation of the SLD-resolution as a reduction system, as given in Nilsson and Maluszynski [NM90].
The second subscript in the reduction is intended as a state, it will be updated by composition along with reductions.Notation γ • γ should be read as follows: the old state γ is updated, producing a new state γ • γ .We assume fresh names in the form of new numeric indices for the quantified variables each time the above rule is applied.We write ; when we leave the associated state implicit.We use ; * to denote the reflexive and transitive closure of ;.Notation ; * γ is used when the final state along the reduction path is γ .
Example 2 Consider the following logic program , consisting of Horn formulas labelled by κ 1 , κ 2 , κ 3 , defining connectivity for a graph with three nodes: The usual SLD-resolution for the query Connect(x , y) can be represented as the following LP-Unif reduction: The first reduction ; κ 1 ,[x /x 1 ,y/z 1 ] unifies the query Connect(x , y) with the head of the rule κ 1 (which is Connect(x 1 , z 1 ) after renaming) with the substitution [x /x 1 , y/z 1 ] (x 1 is replaced by x and z 1 is replaced by y).So the query is resolved with κ 1 , producing the next queries: Connect(x , y 1 ), Connect(y 1 , y).Note that the substitution in the subscript of ; is a state that will be updated alongside the derivation.In the final state we have an answer [Node 3 /y, Node 1 /x ] for the query Connect(x , y).

Soundness and completeness of LP-Unif
We have introduced the Howard's system H and LP-Unif.Now we will show the soundness of LP-Unif, i.e., we show that a reduction by LP-Unif corresponds to an intuitionistic proof.On the other hand, any first order ground evidence of type A in H corresponds to a successful LP-Unif reduction (which is the essence of the completeness result).
Lemma 1 Proof By induction on the length of the reduction.
• Base Case.Suppose the length is one, namely, by the rules Axiom and Inst.
Thus by Cut rule, we have e : The soundness lemma above ensures that every LP-Unif reduction and its answer are meaningful.The usual notion of failure in logic programming can be understood as proving an implicative formula in which the antecedent is comprised of the failed subgoals.The notion of success corresponds to a proof of an atomic formula.For example, consider the logic program κ 1 : P 3 (K ) ⇒ P 1 (K ), κ 2 : P 2 (K ) ⇒ P 3 (K ).We know that the query P 1 (x ) will fail.But by Lemma 1, we know the resolution for query P 1 (x ) will stop at P 2 (K ) with substitution [K /x ], and we have the proof λ a.κ 1 (κ 2 a) : So in a sense the failed query P 1 (x ) is still meaningful under the type theoretic interpretation.In Sect. 3 we will use LP-Unif in a way that it does not have to resolve all the queries, but it still computes useful answers for the variable that we care about.
An evidence is ground if it does not contain free evidence variables.The proof of completeness relies on the strong normalisation and the type preservation property of H.We first show that the normal form of the proof given by a successful LP-Unif reduction is first-order.We then show that, if an atomic formula is inhabited by a ground evidence, there exists a successful LP-Unif reduction for it.
Definition 7 (First-order proof evidence) We define first-order proof evidence as follows.
• A variable proof evidence a and a constant proof evidence κ are first-order.
• If n, n are first-order, then n n is first-order.
then either e is a proof evidence constant, variable or it is normalisable to the form λ a.n, where n is first-order normal proof evidence.
Proof By induction on the derivation of e : [∀ x .]A⇒ B .
By IH, we know that e 1 κ or e 1 λ a.n 1 ; e 2 κ or e 2 λ b. λ d .n 2 , where n 1 , n 2 are fist-order.We know that e 1 a will be normalizable to a first-order proof evidence.And e 2 b will be normalized to either κ b or λ d .Proposition 2 If e is a ground first-order evidence, then it is of the following form:

Partial LP-Unif by labelling
In the previous section, we have given a type-theoretic semantics to logic programming.According to it, an answer for a given query is a substitution applied to a formula that is inhabited by a proof evidence.In that sense, the soundness lemma (Lemma 1) gives type-theoretic meaning to any LP-Unif reduction, even if it is a partial derivation, i.e. has unresolved subgoals.In this section, we build upon this result, and propose a lazy version of LP-Unif, drawing inspiration from lazy functional languages such as Haskell.In particular, we propose to label certain variables in a given query, in order to prioritize those variables for which we want to compute substitutions.Partial LP-Unif resolution will only resolve the subgoals that contain labelled variables.This requires to extend the usual unification to account for labels.We call the resulting unification algorithm labelled unification and the resulting reduction strategypartial LP-Unif.

Definition 8
We extend the term definition: , where x v is a labelled variable.Definitions of a Horn formula and a formula are extended accordingly.
A label on a variable can be informally understood as a case-expression on a variable in lazy functional language.When a query has a labelled variable, it forces resolution to compute a value for it.But since we are in logic programming, the only way to force such evaluation is through label propagation and elimination.The following definition extends unification to achieve this.
We write t v to denote the labelled version of t, in which all the variables of t are labelled.Note that Definition 9 (Labelled unification) We say that t is unifiable with t with substitution γ (denoted t ∼ γ t ), if {t t } * γ according to the following rules: We use t (E ) to denote a labelling operation that labels all the variables in E that occur in t.Formally, t (E ) is defined as σ E , where [x v /x ] ∈ σ for any x ∈ FV(t).The set of equations {x 1 t 1 , . . ., x n t n } can be viewed as a substitution.The labelled unification of terms can be extended routinely to the unification of atomic formulas.We write | t | to denote erasing all the labels in t.We write | σ | to denote removing all the labels in the substitution σ , and L(A) to denote the set of labelled variables in A. The following lemma shows that labelled unification is functionally equivalent to the usual (unlabelled) unification.
We use γ (A) to denote another labelling operation that labels all variables in A that are labelled in the codomain of γ .Formally, γ (A) is defined as σ A, such that, for any Definition 10 (Partial LP-Unif) We define a reduction relation on a multiset of atomic formulas: The labelling operation γ is used in the above definition to make sure that the labels are correctly propagated to B 1 , . . ., B m .Consider κ 1 : ⇒ P 1 (Int), κ 2 : ⇒ P 2 (Int), κ 3 : ∀ x .P 1 (x ), P 2 (x ) ⇒ Q(List(x )), and the query we need to apply γ to P 1 (x ) and P 2 (x ) to obtain the resolvents P 1 (x v ) and P 2 (x v ).
As a consequence of Lemma 3, the partial LP-Unif is essentially a control strategy for LP-Unif.
Note that the above lemma implies, as a corollary, that partial LP-Unif is sound with respect to H.

Definition 11 If
{A} * γ {B 1 , . . ., B n }, L(A) ∅ and L(B i ) ∅ for all i , then we say γ is the relative answer for the labelled variables in A with respect to B 1 , . . ., B n .
Comparing to LP-Unif, partial LP-Unif does not resolve subgoals without labelled variables, and therefore it terminates as soon as all the labelled formulas are resolved.From the pragmatic perspective, the termination of partial LP-Unif signifies that we have obtained all the answers we need for the labelled variables, and thus no further computation is necessary.From the computational perspective, some queries may give rise to nonterminating LP-Unif reduction and the partial LP-Unif strategy offers a lazy version of LP-Unif as an alternative.This is a useful lightweight solution, as checking (non)termination for logic programming can be at best only semi-decidable.From the theoretical perspective, relative answers are meaningful according to type-theoretic semantics of Sect. 2 via Lemma 4.
Labels on variables give us a precise way to formalize the notion of local productivity we mentioned in Introduction.
Definition 12 (Local productivity) We say the queries A 1 , . . ., A n are locally productive at the set of labelled variables V iff ({A 1 , . . ., A n }, V ) ∈ LProd m for any m ≥ 0. We define ({A 1 , . . ., A n }, V ) ∈ LProd m as follows: where L(γ ) denotes the set of labelled variables in the codomain of γ .
Note that according to the definition, if a query is trivially locally productive at the empty set of labelled variables and is trivially locally unproductive if the set of labelled variables are not a subset of the labelled variables in the query.Thus in general it is more sensible to ask local productivity of a query at all its labelled variables.
Local productivity is defined by quantifying over all natural numbers m on LProd m , this allows us to prove local productivity by induction on natural numbers.The requirement γ (x v ) K (t 1 , . . ., t n ) in the definition of LProd m ensures that the answer for a labelled variable x v is at least observable at the function symbol K .
To illustrate local productivity, let us consider the query P (x v ) and the logic program κ : ∀ x .P (x ) ⇒ P (K (x )).We want to show that {P (x v )} is locally productive at x v for any variable x .We just need to show for any x , ({P (x v )}, {x v }) ∈ LProd m for all m.We proceed by induction on m.Suppose m 0, we know that which is by the inductive assumption.This is a very simple case of showing local productivity.In general, it is very challenging to prove local productivity in advance, and we leave this to future work.
Partial productivity and global productivity do not coincide, in general.For example, for a logic program κ : P (K ) ⇒ P (K ), the query P (x v ) is locally productive, but it is not globally productive, as, using terminology of Lloyd [Llo87], no infinite term gets produced at infinity.
We give two further examples of performing finite computation on infinite data structures using partial LP-Unif.
Example 3 Consider the following logic program , that observes, via the Nth predicate, elements of an infinite stream of successive integers defined by From: For the query Nth(S (Z ), y, z v ), From(S (Z ), y), we only want to know the answer for z v , i.e., the 2nd element in the stream generated by From(S (Z ), y).We observe the following reduction: x 4 ]•γ 3 {From(S (S (S (Z ))), y 4 )} Thus S (S (Z )) is the answer for z v relative to From(S (S (S (Z ))), y 4 ), i.e. the 2nd element in the stream generated by From(S (Z ), y) is S (S (Z )).
Example 4 Consider the following logic program : The formula Fib(y, App(x , y), s) ⇒ Fib(x , y, App(x , s)) is intended to generate (potentially infinitely long) Fibonacci word.For example, A, B , . .(where "," and "•" both are shorthand for App, each element of the stream is the concatenation of the previous two) for query Fib(A, B , y v ).Now let us execute the query Take(S (S (S (Z ))), y, z v ), Fib(A, B , y), Intuitively, that query computes the prefix of length 3 in a Fibonacci word: Note that a relative answer γ for a query A can be meaningless under Herbrand model semantics, as γ A, or indeed any of its ground instances, may not be in the least or greatest Herbrand model of the logic program.The following example illustrates this fact.
Example 5 Consider Example 3. Suppose the Horn formula κ 1 is replaced with the following Horn formula: where False(a) is a formula that does not unify with any clause in the given program.However, we can still perform the same partial LP-Unif derivation for the query Nth(S (Z ), y, z v ), From(S (Z ), y), just as was shown in Example 3.But no instance of From(S (Z ), y) will be contained in the least or greatest Herbrand model of that program.

Disscussion
This section presented an experiment in applying the type-theoretic semantics formulated in the earlier section to a practical problem of establishing a sound lazy derivation strategy for resolution.It de-emphasizes the usual notions of refutation and entailment in logic programming.Based on the type-theoretic semantics given to resolution via soundness and completeness theorems of Sect.2, every reduction path of a given query is computationally meaningful.This generalizes the traditional declarative semantics approach to logic programming, according to which only refutation-i.e. the reduction that lead to a normal form given by the empty set-is given a model theoretic meaning.
Labels we introduced in this section allowed us to annotate the intention of making an observation, and the labelled unification was formulated to hereditarily preserve this intention.Thus, we achieved a computational behavior that is similar to lazy functional programming languages, i.e. partial LP-Unif can make finite observations on the infinite data.
Related work exists on supporting lazy computation in logic programming.One is by annotating each predicate to be inductive/coinductive [SBMG07], with the intention of resolving the inductive predicate eagerly and memorizing the coinductive predicate at each step, so that one can stop the resolution whenever the current query is a variant of the previous memorized coinductive predicate.Our approach differs in that memorization and variant detection are not needed.

Structual resolution
In this section, we represent structural resolution using the abstract reduction formalism we introduced in Section 2. We first define structural resolution as LP-Struct reduction, thereby also defining LP-TM reduction, which replaces the unification in LP-Unif by term-matching.We then identify two conditions under which LP-Unif and LP-Struct are operationally equivalent.These two conditions are the termination of LP-TM and the nonoverlapping requirement for Horn clauses.
Given a program and a set of queries {B 1 , . . ., B n }, LP-TM uses only term-matching reduction to reduce {B 1 , . . ., B n }: Definition 14 (LP-TM) Given a logic program , LP-TM is given by an abstract reduction system ( , →).
LP-TM is also sound w.r.t. the type system of Definition 2, which implies that we can obtain a proof for each reductoin of the query.

Theorem 7 (Soundness of LP-TM)
Comparing the soundness lemma and Theorem 7, we see that for LP-TM, there is no need to accumulate substitutions, this is due to the use of term-matching instead of unification for the LP-TM reduction.
We use → μ to denote a reduction path to a →-normal form.If the →-normal form does not exist, then → μ denotes an infinite reduction path.We write → 1 to denote at most one step of →.
We can now formally define structural resolution within our formal framework.Given a program and a set of queries {B 1 , . . ., B n }, LP-Struct first uses term-matching reduction to reduce {B 1 , . . ., B n } to a normal form, then performs one step substitutional reduction, and then repeats this process.
If a finite term-matching reduction path does not exist, then → μ • → 1 denotes an infinite path.When we write {A}(→ μ • → 1 ) * {C }, it means a nontrivial finite path will be of the shape

LP-Struct and LP-Unif
LP-Struct exibits different execution behavior compared to LP-Unif.In general, they are not equivalent.Consider the program and the finite LP-Unif derivation of Example 2. LP-Unif has a finite successful derivation for the query Connect(x , y), but we have the following non-terminating reduction by LP-Struct: LP-TM termination is important for LP-Struct in two aspects: 1.It is one of the conditions that ensure the operational equivalence of LP-Struct and LP-Unif.2. The finiteness of LP-TM reduction is used in defining the observational productivity in logic programming.
The following example shows that LP-TM termination alone is not sufficient to establish that LP-Unif and LP-Struct are operationally equivalent.
Example 6 Consider the following logic program (we use K to denote a constant): The program is LP-TM terminating.For query P (x ), we have {P (x )} ; κ 1 ,[K /x ] ∅ with LP-Unif, but there is only one reduction path {P (x )} → κ 2 {Q(x )} → for LP-Struct.
Thus the termination of LP-TM is insufficient for establishing the relation between LP-Struct and LP-Unif.In Example 6, the problem is caused by the overlapping heads P (K ) and P (x ).Motivated by the non-overlapping condition for rewrite rules in term rewriting systems ([BN98, Ter03]), we introduce the following definition.
The following lemma shows that an LP-TM step can be viewed as an LP-Unif step without affecting the accumulated substiution.
The following lemma shows that one LP-Struct step corresponds to several LP-Unif steps, given the nonoverlapping requirement.

Lemma 6
Suppose is non-overlapping and {A 1 , . . ., Note that the above theorem does not rely on the whole program termination, therefore it establishes equivalence of LP-Unif and LP-Struct even for nonterminating programs such as the Stream example, as long as they are LP-TM terminating and non-overlapping.This result has not been described in previous work.

Discussion
Structural resolution was first introduced in Komendantskaya and Power's work ([KP11, KPS16]) under the name of coalgebraic logic programming.It was further developed into a resolution method based on resolution trees (called rewriting trees) generated by term-matching ([JKK15, KJ15]).The formulation of LP-Struct in this paper is based on the abstract reduction system framework, instead of the tree formalism in previous work.As a consequence, for overlapping logic programs, the reduction-based LP-Struct behaves differently compared to the tree-based formalism (see e.g.Example 6).The novelty of our development in this section is the articulation of the two conditions that ensure the operational equivalence of LP-Struct and LP-Unif (Theorem 9).

Functionalisation of LP-TM
One of the features of LP-Struct is that it refines SLD-resolution by a combination of term-matching and unification.LP-TM itself is used in the type class context reduction [JJM97].The termination behavior of LP-TM is of practical interest.For example, termination for the type class inference is essential to achieve decidability of the type inference in languages such as Haskell ([LPJ05], Section 5).Of course, the termination of LP-TM also implies observational productivity in the context of LP-Struct.As explained in Introduction and Sect.4, LP-TM termination is not only essential to ensure the equivalence of LP-Struct and LP-Unif, but also is important to allow viewing the LP-TM reductions within the nonterminating LP-Struct reduction as finite observations.
On the other hand, termination and nontermination detection are well-studied in the context of term rewriting.In this section we show a method that reuses the techniques developed in term rewriting to detect termination of LP-TM.We first define a process called functionalisation that transforms a set of Horn clauses into a set of rewrite rules, where the execution of a query is seen as a process of rewriting the query to its proof.As a result, termination and nontermination detection techniques from term rewriting can be applied to LP-TM, assuming the logic program contains no existential variables.
In this section we work only with the Horn formulas without existential variables, i.e. for any Horn formula ∀ x .A 1 , . . ., A n ⇒ B , we have i FV(A i ) ⊆ FV(B ).The restriction that Horn clauses should not contain existential variables comes directly from a similar requirement imposed in term-rewriting.
Since the idea of functionalisation is to view LP-TM resolution for a query as a rewriting process to its proof evidence, the rewriting is defined on mixed terms, i.e. a mixture of atomic formulas and proof evidence.

Mixed term q
Note that C can be ground.Let C[q 1 , . . ., q n ] mean replacing all the • in C from left to right by q 1 , . . ., q n .Definition 19 (Functionalisation) We can construct a set of rewrite rule K ( ) from a set of axioms as follows.For each κ : ∀ x .A 1 , . . ., A n ⇒ B ∈ , we define a rewrite rule B → κ A 1 . . .A n ∈ K ( ) on mixed terms.We call κ an axiom symbol.
Note that the evidence constant κ for A 1 , . . ., A n ⇒ B becomes a mixed term function symbol of arity n, with A 1 , . . ., A n as its arguments, which is denoted by κ A 1 . . .A n .
Definition 20 We define a relation C → C to mean that C can be obtained from C by replacing a • in C by some C 1 , where C 1 ≡ •.We also write the reflexive and transitive closure of this relation as → * .
The following lemmas show that each LP-TM step corresponds exactly to a rewrite step after functionalisation.As a consequence, it is possible to determine the termination behavior of LP-TM by analyzing the corresponding term rewriting system.
where C, C do not contain any atomic formulas and and vice versa.So C can be obtained by replacing the i th where C → * C and C, C do not contain any atomic formulas.
Proof We prove both direction together.By induction on the length of → * .Base Case.By Lemma 10.
Step Case.

Left to Right:
Theorem 10 {A} → * ∅ iff A → * e, and e is a ground evidence.As a consequence, the query A is LP-TM (non)terminating iff A is (non)terminating for K ( ).
Proof By Lemma 11. 2 In practice, functionalisation can also be used to implement LP-TM, especially if computing the proof evidence is the only goal.For example, this is the case for type class inference [FKSP16].
Now we demonstrate how to apply a convenient termination technique in term rewriting called dependency pair method [AG00] to analyze the termination behavior of LP-TM.

Definition 21
We define the dependency pairs generated from K ( ) to be E (K ( )) Definition 22 A (potentially infinite) sequence of pairs q 1 → q 1 , q 2 → q 2 , . . . in E (K ( )) is a K ( )-chain iff there is a substitution σ with σ q i ≡ σ q i+1 for all i .
The above definition of K ( )-chain is using the condition σ q i ≡ σ q i+1 instead of σ q i → * σ q i+1 in term rewriting [AG00].Since the dependency pairs generated from a logic program will always be in the form of A → B , where A, B are atomic formulas, rewriting under the predicate is not possible.This greatly simplifies the termination detection for LP-TM.

Theorem 11 (Arts-Giesl [AG00])K ( ) is terminating iff no infinite K ( )-chain exist.
Theorem 11 allows us to detect the termination of K (A) by looking at the possible K ( )-chain.
Example 7 Consider the following program : The dependency pairs of are P (List(x )) → P (List(x )) and P (List(x )) → P (x ).We can see P (List(x )) → P (List(x )) can form an infinite E (K ( ))-chain, thus K ( ) is not terminating.So is not LP-TM terminating.

Realizability transformation and LP-Struct
Functionalisation provides a way to detect LP-TM termination for LP-Struct.But sometimes there are logic programs that are not LP-TM terminating but are still meaningful from the LP-Unif perspective (cf.Example 2).For these programs, we still want to be able to use LP-Struct.To solve this problem, we define a meaning preserving realizability transformation that transforms any logic program into LP-TM terminating one.
Realizability [Kle52]( 82) is a technique that uses a number representing the proof of a number-theoretic formula.The transformation described here is similar in the sense that we use a first-order term to represent the proof of a Horn formula.More specifically, we use a first-order term as an extra argument for Horn formula to represent a proof of that formula.
Lemma 2 and Theorem 5 show that we can use a first-order term to represent a normalized proof evidence.
Definition 23 (Representing first-order proof evidence) Let φ be a mapping from proof evidence variables to firstorder terms.We define a representation function • φ from first-order normal proof evidence to first-order terms.
Let A ≡ P (t 1 , . . ., t n ) be an atomic formula and t be a term such that ( i FV(t i )) ∩ FV(t ) ∅, we write A[t ] to abbreviate a new atomic formula P (t 1 , . . ., t n , t ).
Definition 24 (Realizability transformation) We define a transformation F on Horn formula and its normalized proof evidence: where y 1 , . . ., y m are all fresh and distinct.
where y 1 , . . ., y m are all fresh and distinct.
The realizability transformation systematically associates a proof to each predicate, so that the proof can be recorded alongside with reductions.Let F ( ) mean applying the realizability transformation to every axiom in .
Example 8 The following logic program F ( ) is the result of applying realizability transformation on the program in Example 2.
Before the realizability transformation, we have the following judgement in H: We can apply the transformation, we get: Operational semantics of resolution 469 which is the same as We write (F ( ), ;), to mean given axioms F ( ), use LP-Unif to reduce a given query.Note that for a query A in ( , ;), it becomes a query A[t] for some t such that FV(A) ∩ FV(t) ∅ in (F ( ), ;).
The following theorem shows that realizability transformation does not change the type-theoretic meaning of a program.This is important because it means we can apply different resolution strategies to resolve the query on the transformed program without worrying about the change of meaning.Later we will see that the behavior of LP-Struct is different for the original program and the transformed program.• Base Case.
In this case, we know that F (κ : By Lemma 2, we know that the normal form of e 1 is κ 1 or λ a.n 1 , and the normal form of e 1 is κ 2 or λ bd .n 2 , with n 1 , n 2 are first-order.
e 1 ≡ κ 1 , e 2 ≡ κ 2 .By IH, we know that So by Gen and Inst, we have -The other cases are handle similarly.
Thus we have the following reduction: There are logic programs that are overlapping and LP-TM nonterminating (as e.g. the program of Example 2), we would still like to obtain a meaningful execution behaviour for LP-Struct, especially if LP-Unif aready allows successful derivations for the programs.Luckily, we can apply realizability transformation to such programs and apply LP-Struct reduction.

Proposition 3 For any program , F ( ) is LP-TM terminating and non-overlapping.
Proof First, we need to show →-reduction is strongly normalizing in (F ( ), →).By Definition 24, we can establish a decreasing measurement(from right to left, using the strict subterm relation) for each rule in F ( ), since the last argument in the head of each rule is strictly larger than the ones in the body.Then, non-overlapping property is due to the fact that all the heads of the rules in F ( ) will be guarded by the unique function symbol in Definition 24. 2 Corollary 1 (Equivalence of LP-Unif and LP-Struct) Proof By Theorem 9 and Proposition 3. 2 Using the above corollary and soundness and completeness of LP-Unif, we deduce as a corollary that LP-Struct is sound and complete relative to system H for the transformed logic program.
Realizability transformation uses the extra argument as decreasing measurement in the program to achieve termination of →-reduction.At the same time this extra argument makes the program non-overlapping.Realizability transformation does not modify the proof-theoretic meaning and the execution behaviour of LP-Unif.The next example shows that not every transformation technique for obtaining structurally decreasing LP-TM reductions has such properties: Example 11 Consider the following program: κ 1 : ⇒ P (Int) κ 2 : ∀ x .P (x ), P (List(x )) ⇒ P (List(x )) It is a folklore method to add a structurally decreasing argument as a measurement to ensure finiteness of → μ .κ 1 : ⇒ P (Int, 0) κ 2 : ∀ x .∀ y.P (x , y), P (List(x ), y) ⇒ P (List(x ), S (y)) We denote the above program as .Indeed with the measurement we add, the term-matching reduction in will be finite.But the reduction for query P (List(Int), z ) using LP-Unif reduction will fail: {P (List(Int), z )} ; κ 2 ,[Int/x ,S (y 1 )/z ] {P (Int, y 1 ), P (List(Int), y 1 )} ; κ 2 ,[0/y 1 ,Int/x ,S (0)/z ] {P (List(Int), 0)} ; However, the query P (List(Int)) on the original program using unification reduction will diverge.

Related work
Proof Search, Logic Programming and Type Theory.To the best of our knowledge, studying logic programming proof-theoretically dates back to Girard's suggestion to use the cut rule to model resolution for Horn formulas [GTL89,Chapter 13.4].Miller et al. [MNPS91] use cut-free sequent calculus to represent a proof for a query.More specifically, given a query Q and a logic program P, Q has a refutation iff there is a derivation in cut-free sequent calculus for P Q.Using sequent calculus as a proof theoretic framework gives the flexibility to incorporate different kinds of formulas, e.g.classical formulas and linear formulas into this framework.
Interactive theorem prover Twelf [PS99] pioneered implementation of proof search on top of a depedently typed system called LF [HL07].Similar to Twelf, we believe that type systems serve as a suitable foundation for logic programming.Comparing to Twelf, we specify and analyze different resolution strategies (other than SLD-resolution) and study their intrinsic relations.We also pay more attention to various kinds of productivity compared to Twelf.
Structural Resolution.Structural resolution is a result of joint research efforts by Komendantskaya et al. ([JKK15,KP11,KPS16]).The goal of the analysis of structural resolution is to support sound coinductive reasoning in logic programming.For example, given the query Take(S (S (S (Z ))), y, z ), Fib(A, B , y) in Example 4, one may want not only to obtain a substitution for z , but also a guarantee that the queries to Fib are nonterminating and, moreover, that derivations for Fib will not fail if continued to infinity.To support this, productivity analysis has been developed [KJ15,KJM16] as a compile time technique to detect observational productivity of logic programs.
Coinductive Logic Programming.Gupta et al. [GBM + 07]'s coinductive logic programming (CoLP) extends SLD-resolution with a method to use atomic coinductive hypotheses.That is, during the execution, if the current queries {C 1 , . . ., C i , . . ., C n } contain a query C i that unifies via γ with a C in the earlier execution, then the next step of resolution will be given by {γ C 1 , . . ., γ C i−1 , γ C i+1 , . . ., γ C n }.The coinductvie hyposesis mechanism in CoLP can be viewed as a form of loop detection.However, CoLP cannot detect hypotheses for more complex patterns of coinduction that produce coinductive subgoals that fail to unify.As discussed in introduction, it is not a suitable tool to analyze the productivity of infinite data structures in logic programming.
Proof Relevant Corecursive Resolution.In our previous work [FKSP16], we extended system H with fixpoint operator to allow constructing corecursive proof evidence (given by proof terms containing fixpoint operator) for certain nonterminating LP-TM reductions.The type system that we use to justify the corecursive proof evidence is an extension of Howard's system H with the fixpoint typing rule.There, the main challenge was to heuristically and automatically construct corecursive evidence for a given query.
n 2 .So by Proposition 1, we conclude that λ a. λ b.(e 2 b) (e 1 a) is normalizable to λ a. λ b.n for some first-order normal term n. • The Gen and Inst cases are straightforward.2 Theorem 5 If e : [∀ x .]⇒ B in C, then e is normalizable to a first-order proof evidence.Now let us prove the completeness theorem.
The diverging behavior above is due to the divergence of LP-TM reduction.Definition 16 (LP-TM termination) We say a programis LP-TM terminating iff it admits no infinite →reduction.

Theorem 12
If e : [∀ x ].A ⇒ B in C and e normalized to n, then F ( ) F (n : [∀ x ].A ⇒ B ) in H. Proof By induction on the derivation of e : [∀ x ].A ⇒ B .
Proof evidence is given by lambda terms.We use capitalised words to denote function symbols.Constant evidence is denoted by κ.We write A 1 , . . ., A n ⇒ B as a short hand for A 1 ⇒ • • • ⇒ A n ⇒ B .We write ∀ x .F for quantifying over all free term variables in F , and [∀ x ].F denotes F or ∀ x .F .We use A to denote A 1 , . . ., A n , when the number n is unimportant.If n is zero for A ⇒ B , then we write ⇒ B .Horn clause formulas have the form ∀ x .A ⇒ B , and queries are given by atomic formulas.We use FV(t) to denote the set of all free term variables in t.