arxiv:1511.07865v1 [cs.lo] 24 nov 2015 filein lp on par with productivity-based observational...

36
arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 A Structural Resolution: a Framework for Coinductive Proof Search and Proof Construction in Horn Clause Logic Ekaterina Komendantskaya, School of Sciences and Engineering (Computing), University of Dundee, UK Patricia Johann, Department of Computer Science, Appalachian State University, USA Logic programming (LP) is a programming language based on first-order Horn clause logic that uses SLD-resolution as a semi-decision procedure. Finite SLD-computations are inductively sound and complete with respect to least Herbrand mod- els of logic programs. Dually, the corecursive approach to SLD-resolution views infinite SLD-computations as successively approximating infinite terms contained in programs’ greatest complete Herbrand models. State-of-the-art algorithms imple- menting corecursion in LP are based on loop detection. However, such algorithms support inference of logical entailment only for rational terms, and they do not account for the important property of productivity in infinite SLD-computations. Loop detection thus lags behind coinductive methods in interactive theorem proving (ITP) and term-rewriting systems (TRS). Structural resolution is a newly proposed alternative to SLD-resolution that makes it possible to define and semi-decide a notion of productivity approrpriate to LP. In this paper we show that productivity supports the development of a new coin- ductive proof principle for LP that semi-decides logical entailment by observing finite fragments of resolution computations for productive programs. This severs the dependence of coinductive proof on term rationality, and puts coinductive methods in LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- tural resolution relative to Herbrand model semantics for productive inductive, coinductive, and mixed inductive-coinductive logic programs. Categories and Subject Descriptors: F.3.2 [Semantics of Programming Languages]: Operational Semantics; F.4.1 [Math- ematical Logic]: Logic and Constraint Programming Additional Key Words and Phrases: Logic programming, resolution, induction, coinduction, infinite derivations, Herbrand models. ACM Reference Format: Ekaterina Komendantskaya, et al., 2015. Structural Resolution ACM Trans. Comput. Logic V, N, Article A (January YYYY), 36 pages. DOI:http://dx.doi.org/10.1145/0000000.0000000 1. INTRODUCTION 1.1. A Symmetry of Inductive and Coinductive Methods Logic Programming (LP) is a programming language based on Horn clause logic. If P is a logic program and t is a (first-order) term, then LP provides a mechanism for automatically inferring whether or not P logically entails t . The traditional (inductive) approach to LP is based on least fixed point semantics [Kowalski 1974; Lloyd 1987] of logic programs, and defines, for every such program P, the least Herbrand model for P, i.e., the smallest set of (finite) ground terms inductively entailed by P. The work was supported by EPSRC grants EP/J014222/1 and EP/K031864/1 and NSF award 1420175. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies show this notice on the first page or initial screen of a display along with the full citation. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, to republish, to post on servers, to redistribute to lists, or to use any component of this work in other works requires prior specific permission and/or a fee. Permissions may be requested from Publications Dept., ACM, Inc., 2 Penn Plaza, Suite 701, New York, NY 10121-0701 USA, fax +1 (212) 869-0481, or [email protected]. c YYYY ACM 1529-3785/YYYY/01-ARTA $15.00 DOI:http://dx.doi.org/10.1145/0000000.0000000 ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Upload: vannga

Post on 22-Jun-2019

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

arX

iv:1

511.

0786

5v1

[cs.

LO]

24 N

ov 2

015

A

Structural Resolution: a Framework for Coinductive Proof Search andProof Construction in Horn Clause Logic

Ekaterina Komendantskaya, School of Sciences and Engineering (Computing), University of Dundee, UKPatricia Johann, Department of Computer Science, Appalachian State University, USA

Logic programming (LP) is a programming language based on first-order Horn clause logic that uses SLD-resolution as asemi-decision procedure. Finite SLD-computations are inductively sound and complete with respect to least Herbrand mod-els of logic programs. Dually, the corecursive approach to SLD-resolution views infinite SLD-computations as successivelyapproximating infinite terms contained in programs’ greatest complete Herbrand models. State-of-the-art algorithmsimple-menting corecursion in LP are based on loop detection. However, such algorithms support inference of logical entailmentonly for rational terms, and they do not account for the important property of productivity in infinite SLD-computations. Loopdetection thus lags behind coinductive methods in interactive theorem proving (ITP) and term-rewriting systems (TRS).

Structural resolution is a newly proposed alternative to SLD-resolution that makes it possible to define and semi-decide anotion of productivity approrpriate to LP. In this paper we show that productivity supports the development of a new coin-ductive proof principle for LP that semi-decides logical entailment by observing finite fragments of resolution computationsfor productive programs. This severs the dependence of coinductive proof on term rationality, and puts coinductive methodsin LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc-tural resolution relative to Herbrand model semantics for productive inductive, coinductive, and mixed inductive-coinductivelogic programs.

Categories and Subject Descriptors: F.3.2 [Semantics of Programming Languages]: Operational Semantics; F.4.1 [Math-ematical Logic]: Logic and Constraint Programming

Additional Key Words and Phrases: Logic programming, resolution, induction, coinduction, infinite derivations, Herbrandmodels.

ACM Reference Format:Ekaterina Komendantskaya, et al., 2015. Structural Resolution ACM Trans. Comput. LogicV, N, Article A (January YYYY),36 pages.DOI:http://dx.doi.org/10.1145/0000000.0000000

1. INTRODUCTION

1.1. A Symmetry of Inductive and Coinductive Methods

Logic Programming (LP) is a programming language based on Horn clause logic. IfP is a logicprogram andt is a (first-order) term, then LP provides a mechanism for automatically inferringwhether or notP logically entailst. The traditional (inductive) approach to LP is based on leastfixed point semantics [Kowalski 1974; Lloyd 1987] of logic programs, and defines, for every suchprogramP, theleast Herbrand modelfor P, i.e., the smallest set of (finite) ground termsinductivelyentailedby P.

The work was supported by EPSRC grants EP/J014222/1 and EP/K031864/1 and NSF award 1420175.Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without feeprovided that copies are not made or distributed for profit orcommercial advantage and that copies show this notice on thefirst page or initial screen of a display along with the full citation. Copyrights for components of this work owned by othersthan ACM must be honored. Abstracting with credit is permitted. To copy otherwise, to republish, to post on servers, toredistribute to lists, or to use any component of this work inother works requires prior specific permission and/or a fee.Permissions may be requested from Publications Dept., ACM,Inc., 2 Penn Plaza, Suite 701, New York, NY 10121-0701USA, fax+1 (212) 869-0481, or [email protected]© YYYY ACM 1529-3785/YYYY/01-ARTA $15.00DOI:http://dx.doi.org/10.1145/0000000.0000000

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 2: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:2

Example1.1. The programP1 defines the set of natural numbers:

0. nat(0) ←1. nat(s(X)) ← nat(X)

The least Herbrand model forP1 comprises the termsnat(0), nat(s(0)), nat(s(s(0))), . . .

The clauses ofP1 can be viewed as inference rulesnat(0) and nat(X)

nat(s(X)) , and theleast Herbrand model can be seen as the set obtained by the forward closure of theserules. Some approaches to LP and first-order sequent calculiare based on this induc-tive view [Baelde 2008; Brotherston and Simpson 2011] of programs, which is entirely stan-dard [Sangiorgi 2012]. A similar view underlies inductive type definitions in interactive theo-rem proving (ITP) [Agda Development Team 2015; Coq Development Team 2015]. For example,P1 also corresponds to the following Coq definition of an inductive type:

Inductive nat : Type :=| 0 : nat| S : nat→ nat.

In addition to viewing logic programs inductively, we can also view them coinductively. Thegreatest complete Herbrand modelfor a programP takes the backward closure of the rules derivedfrom P’s clauses, thereby producing the largest set of finite and infinite ground termscoinductivelyentailedby P. For example, the greatest complete Herbrand model forP1 is the set containing all ofthe finite terms in the least Herbrand model forP1, together with the termnat(s(s(...))) represent-ing the first limit ordinal. The coinductive view of logic programs corresponds to coinductive typedefinitions in ITP.

As it turns out, some logic programs have no natural inductive semantics and should instead beinterpreted coinductively:

Example1.2. The programP2 defining streams of natural numbers comprises the clauses ofP1and the following additional one:

2. nats(scons(X,Y)) ← nat(X),nats(Y)

No terms defined bynats are contained in the least Herbrand model forP2, butP2’s greatest com-plete Herbrand model contains the infinite termt = nats(scons(0,scons(0, . . .) representing theinfinite stream of (all) natural numbers.

The programP2 corresponds to the following Coq definition of a coinductivetype:

CoInductive nats : Type :=SCons : nat→ nats→ nats.

The formal relation between logic programs, Herbrand models, and types is analysed in[Heintze and Jaffar 1992].

1.2. Preconditions for an Operational Semantics?

The (least and greatest complete) Herbrand models for programs, as defined by (forward and back-ward rule closure, respectively, of) their clauses, provide one important way to understand logicprograms. But an equally important way is via their computational behaviours. Rather than usingHerbrand models to give meaning to “inductive” and “coinductive” logic programs, we can also usethe operational properties of SLD-resolution to assign programs semantics that take into accountthe computational behaviours that deliver those models. Ideally, we would like to do this in sucha way that the symmetry between the Herbrand model interpretations of inductive and coinduc-

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 3: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:3

tive programs as the sets of terms (i.e., the types) they define is preserved by these computationalinterpretations.

The transition from types to computations is natural in ITP,where recursive functions consumeinputs of inductive types and, dually, corecursive functions produce outputs of coinductive types.Since systems such as Coq and Agda require recursive functions to be terminating in order to besound, and since SLD-resolution similarly requires a logicprogram’s derivations to be terminatingin order for them to be sound with respect to that program’s least Herbrand model, we might duallyexpect a logic program’s non-terminating derivations to compute terms in its greatest completeHerbrand model. However, non-termination does not play a role for coinduction dual to that playedby termination for induction. In particular, the fact that alogic program admits non-terminatingSLD-derivations does not, on its own, guarantee that the program’s computations completely captureits greatest complete Herbrand model:

Example1.3. The following “bad” program gives rise to an infinite SLD-derivation:

0. bad(f(X)) ← bad(f(X))

Although this program does not compute any infinite terms, the infinite termbad(f(f(...))) is in itsgreatest complete Herbrand model.

It is important to note that the “badness” of this program is unrelated to the fact that LP is untyped.The following corecursive function is equally “bad”, and will be rejected by Coq:

CoInductive Stream A : Type :=SCons : A→ Stream A→ Stream A.

CoFixpoint bad (f : A → A) (x : A) : Stream A := bad f (f x).

The problem here actually lies in the fact that both the LP andthe ITP versions of the above“bad” program fail to satisfy the important property of productivity. The productivity requirementon corecursive programs for systems such as Coq and Agda reflects the fact that an infinite compu-tation can only be consistent with its intended coinductivesemantics if it isglobally productive, i.e.,if it actually produces an infinite object in the limit. But inorder to give an operational meaning to“in the limit” — which is not itself a computationally tractable concept — productivity is usuallyinterpreted in terms of finite observability. Specifically,a function can be (finitely) observed to beglobally productive if each part of its infinite output can begenerated in finite time. We call this kindof productivityobservational productivity. A similar notion of an observationally productive infinitecomputation has also been given for term rewriting systems (TRS) [Endrullis et al. 2010]. Moreover,a variety of syntactic guardedness checks have been developed to semi-decide observational produc-tivity in ITP in practice [Coquand 1994; Gimenez 1998]. However, prior to [Johann et al. 2015b],LP did not have any notion of an observationally productive program, and therefore did not have acorresponding operational semantics based on any such notion.

1.3. Symmetry Broken

It is well-known that termination captures the least Herbrand model semantics of (inductive) logicprograms computationally: the terminating and successfulSLD-derivations for any programP givea decision procedure for membership in the least Herbrand model for P. For example, after a finitenumber of SLD-derivation steps we can conclude thatnat(X) is in the least Herbrand model forprogramP1 if X = 0. Termination of SLD-derivations thus serves as a computational preconditionfor deciding logical entailment.

But for programs, likeP2, that admit non-terminating derivations, SLD-resolutiongives only asemi-decision procedure for logical entailment. Indeed, if an SLD-derivation for a program and aquery terminates with success, then we definitely know that the program logically entails the termbeing queried, and thus that this term is in the greatest complete Herbrand model for the program.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 4: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:4

But if an SLD-derivation for the program and query does not terminate, then we can infer nothing.It is therefore natural to ask:

Question: It is possible to capture the greatest complete Herbrand model semantics for potentiallynon-terminating logic programs computationally? If so, how?

That is, can we restore the symmetry between terminating andpotentially non-terminating logicprograms so that that the correspondence between a terminating program’s Herbrand semantics andits computational behaviour also holds for non-terminating programs?

In one attempt to match the greatest complete Herbrand semantics for potentially non-terminatingprograms, an operational counterpart — calledcomputations at infinity— was introduced in the1980s [Lloyd 1987; van Emden and Abdallah 1985]. The operational semantics of a potentiallynon-terminating logic programP was then taken to be the set of all infinite ground terms com-putable byP at infinity. For example, the infinite ground termt in Example 1.2 is computable byP2at infinity starting with the query ?← nats(X). Although computations at infinity do better capturethe computational behaviour of non-terminating logic programs, they are still only sound, and notcomplete, with respect to those programs’ greatest complete Herbrand models. For example, theinfinite termbad(f(f(...))) is in the greatest complete Herbrand model for the “bad” program ofExample 1.3, as noted there, but is not computable at infinityby that program.

Interestingly, computations at infinity capture the same intuition about globally productive infiniteSLD-derivations that underlies the productivity requirement for corecursive functions in ITP andTRS. That is, they insist that each infinite SLD-derivation actually produces an (infinite) term. Thisobservation leads us to adapt the terminology of [Lloyd 1987; van Emden and Abdallah 1985] andsay that a logic programP is SLD-productiveif every infinite SLD-derivation forP computes aninfinite term at infinity. SLD-productivity captures the difference in computational behavior betweenprograms, likeP2, that actually do compute terms at infinity, from “bad” programs, like that ofExample 1.3, that do not. While computations at infinity are not complete with respect to greatestcomplete Herbrand models for non-SLD-productive logic programs, for SLD-productive programsthey are. For example, the SLD-productive program below is similar to our non-SLD-productive“bad” program and, its greatest complete Herbrand model is computed in the same way:

0. good(f(X)) ← good(X)

But because this program is SLD-productive — and, therefore, “good” — the infinite termgood(f(f(...))) corresponding to the problematic term above is not only in its greatest completeHerbrand model, but is also computable at infinity.

In light of the above, we concentrate on productive logic programs, shifting our focus away fromgreatest complete Herbrand models and toward computationsat infinity, to give such programs amore computationally relevant semantics. But a big challenge still remains: even for productiveprograms, the notion of computations at infinity does not by itself give rise to implementations.Specifically, although SLD-productivity captures the important requirement that infinite computa-tions actually produce output, it does not give a corresponding notion of finite observability, as ITPand TRS productivity do. We therefore refine our question above to ask:

Question (refined): Can we formulate a computational semantics for LP that redefines productivityin terms of finite observability, as is done elsewhere in the study of programming languages, and thatdoes this in such a way that it both yields implementations and ensures soundness and completenesswith respect to computations at infinity (rather than greatest complete Herbrand models)? If so,how?

Thirty years after the initial investigations into coinductive computations, coinductive logic pro-gramming, implemented as CoLP, was introduced [Gupta et al.2007; Simon et al. 2007]. CoLP pro-vides practical methods for terminating infinite SLD-derivations. These are based on the principleof coinduction, which is in turn based on the ability to form coinductive hypotheses and succeed

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 5: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:5

when coinductive conclusions are reached. CoLP’s search for coinductive hypotheses is based on aloop detection mechanism that requires the programmer to supply annotations marking every pred-icate as either inductive or coinductive. For coinductive predicates, CoLP observes finite fragmentsof SLD-derivations, checks them for unifying subgoals, andterminates when loops determined bysuch subgoals are found. A similar loop detection method is employed for type class inference inthe Glasgow Haskell Compiler (GHC) [Lammel and Jones 2005], and CoLP itself is used for typeclass inference in Featherweight Java [Ancona and Lagorio 2011].

Example1.4. If nats is marked as coinductive inP2, then the query ?← nats(X)gives rise to an SLD-derivation with a sequencenats(scons(0,Y′)), nats(scons(0,Y′′)),nats(scons(0,Y′′′)), . . . of subgoals. Observing thatnats(scons(0,Y′)) andnats(scons(0,Y′′)))unify and thus comprise a loop, CoLP concludes thatnats(X) has been proved by coinductive hy-pothesisnats(scons(0,Y′)) and coinductive conclusionnats(scons(0,Y′′)). CoLP returns theanswerX= scons(0,X) in the form of a “circular” term indicating thatP2 logically entails the termt in Example 1.2.

CoLP is sound, but incomplete, relative to greatest complete Herbrand models [Gupta et al. 2007;Simon et al. 2007]. But, perhaps surprisingly, it isneithersoundnor complete relative to compu-tations at infinity. CoLP is not sound because our “bad” program from Example 1.3 computes noinfinite terms at infinity for the query ?← bad(X), whereas CoLP notices a loop and reports success(assuming the predicatebad is marked as coinductive). CoLP is not complete because not all termscomputable at infinity by all programs can be inferred by CoLP. In fact, CoLP’s loop detectionmechanism can only terminate if the term computable at infinity is arational term [Courcelle 1983;Jaffar and Stuckey 1986]. Rational terms are terms that can be represented as trees that have a finitenumber of distinct subtrees, and can therefore be expressedin a closed finite form computed by cir-cular unification. The “circular” termX = scons(0,X) in Example 1.4 is so expressed. For irrationalterms, CoLP simply does not terminate:

Example1.5. The programP3 defines addition on the Peano numbers, together with the streamof Fibonacci numbers:

0. add(0,Y,Y) ←1. add(s(X),Y,s(Z)) ← add(X,Y,Z)2. fibs(X,Y,cons(X,S)) ← add(X,Y,Z),fibs(Y,Z,S)

From a coinductive perspective,P3 is semantically and computationally meaningful. It com-putes the infinite termt∗ = fibs(0,s(0),cons(0,cons(s(0),cons(s(0),cons(s(s(0)), . . .)))),and thus the stream of Fibonacci numbers (in the third argument to fibs). The term t∗ isboth computable at infinity byP3 and contained inP3’s greatest complete Herbrand model.Nevertheless, when CoLP processes the sequencefibs(0,s(0),cons(0,S)), fibs(s(0),s(0),cons(s(0),S′)), fibs(s(0),s(s(0),cons(s(0),S′′)), . . . of subgoals for the programP3 and query?← fibs(0,s(0),X) giving rise tot∗, it cannot unify any two of them, and thus does not terminate.

The upshot is that CoLP cannot faithfully capture the operational meaning of computations atinfinity. In this paper, we formulate a new operational semantics for LP that better correspondsto computations at infinity by first expressing productivityin terms of finite observability, and thenimplementing infinite proofs via finitely formulated coinductive hypotheses. In formulating our newoperational semantics we are guided by several specific aims:

(i) The loop detection mechanism of CoLP can be seen as specifying anobservational principle. If aloop is detected, then the computation is terminated and a finite observation of the infinite outputis made. However, as our “bad” program of Example 1.3 illustrates, loop detection is not a goodtool for determining productivity of derivations.We therefore seek a new observational principlefor determining productivity of logic programs.

(ii) The loop detection mechanism of CoLP can also serve as a means of finding coinductive hy-potheses and conclusions, but it limits us to working only with programs defining rational objects

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 6: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:6

in their greatest complete Herbrand models.We therefore seek a new, more general coinductiveprinciple for LP that goes beyond CoLP’s loop detection.

(iii) Items (i) and (ii) show that productivity and entailment are realised by the same algorithm (i.e.,loop detection) in CoLP. However, elsewhere in programminglanguage semantics, deciding ter-mination and productivity operationally precedes, but is not equivalent to, deciding entailment.For example, in inductive LP a terminating SLD-derivation need not necessarily be successful,and productivity is statically checked in ITP. Productivity and coinductive entailment should sim-ilarly be separated in LP.We therefore seek observational and coinductive principles for LP thatachieve this separation.

(iv) The ability to semi-decide productivity and coinductive entailment in CoLP depends on whetheror not the object computable at infinity can be expressed in a closed form by a regular pattern. Thisconstraint is limiting and, in fact, unnecessary, as experience with ITP and TRS shows. For ex-ample, the following corecursive function defining the stream of Fibonacci numbers is productiveand thus perfectly acceptable by Coq, even though the term representing the stream of Fibonaccinumbers is irrational:

CoFixpoint fib (n m : nat) :=SCons n (fib m (n+m)).

We therefore seek methods that can (semi-)decide productivity and coinductive entailment for logicprograms even when infinite objects computable at infinity cannot always be expressed in a finiteregular form.

1.4. Symmetry Restored

We now show by example how the techniques developed in this paper achieve the aims enumer-ated in the preceding subsection. The remainder of the paperis concerned with formalising thesetechniques.

An elegant solution to (i) has been proposed in [Komendantskaya et al. 2014;Johann et al. 2015b], where SLD-derivation steps are separated into those that can be takenby term rewriting, which uses only matching, and those that require unification. Significantly, finitesequences of term rewriting steps can serve as a mechanism for making finite observations ofpotentially infinite derivations.

Example1.6. The coinductive programP4 has the single clause

0. from(X,scons(X,Y))← from(s(X),Y)

Given the query ?← from(0,X), and writing [ , ] as an abbreviation for the stream constructorscons here, we have that the infinite termt ′ = from(0, [0, [s(0), [s(s(0)), . . .]]]) is computable atinfinity by P4 and is also contained in the greatest Herbrand model forP4. By the same argumentas in Examples 1.4 and 1.5, coinductive reasoning on this query cannot be handled by the loopdetection mechanism of CoLP because the termt ′ is irrational.

Separation of the infinite derivation that computest ′ at infinity into term rewriting and unificationsteps can be represented as below, with term rewriting stepsshown vertically and unification stepsshown horizontally. This separation makes it easy to see that P4 is finitely observable, in the sensethat all of its derivations by term rewriting alone terminate.

from(0,X)

{X 7→[0,X′]}→

from(0, [0,X′ ])

from(s(0),X′)

{X′ 7→[s(0),X′′]}→

from(0, [0, [s(0),X′′ ]])

from(s(0), [s(0),X′′ ])

from(s(s(0)),X′′)

{X′′ 7→[s(s(0)),X′′′]}→

It is intuitively pleasing to represent sequences of term rewriting reductions as trees. We calltheserewriting treesto mark their resemblance to TRS [Terese 2003]. Full SLD-derivation steps

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 7: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:7

can be represented by transitions between rewriting trees.These transitions are determined bymost general unifiers of rewriting tree leaves with program clauses; see Section 2 below, as wellas [Johann et al. 2015a], for more detail. In [Fu and Komendantskaya 2015; Johann et al. 2015a]this method of separating SLD-derivations into rewriting steps and unification-driven steps (illus-trated in Example 1.6) is calledstructural resolution, or S-resolutionfor short.

With S-resolution in hand, we can define a logic program to be(observationally) productiveif itis finitely observable, i.e., if all of its rewriting trees are finite. Our “good” program above is againproductive, whereas the “bad” one is not — but now “productive” means productive in this newobservational sense. S-resolution thus allows us to carry over the distinction between global and ob-servational productivity from ITP to LP, since SLD-resolution gives the former whereas S-resolutiongives the latter. To formally define a notion of global productivity for S-resolution, we generalise thenotion of SLD-computable at infinity to that of S-computableat infinity. For the simple program inExample 1.6, we have thatt ′ is S-computable at infinity if there is an infinite sequence ofunificationsteps starting withfrom(0,X) such that the root term of the rewriting tree produced at infinity ist ′. We can see from the figure above that our new notion of observational productivity ensures thatevery tree transition adds more structure to terms already present in a rewriting tree to ensure furtherprogress with term rewriting in the next one. Observationalproductivity thus ensures production ofan infinite term at infinity.

Thus far we have found a solution to problem (i) and the start of a solution to (iii): loop detectionno longer plays the role of a productivity checker, we have anobservational approach to productivity,and productivity as a program property is separated from semi-deciding coinductive entailment.However, LP is not just a programming language, but is also a theorem prover, and the question oflogical entailment is thus crucial. This brings us to item (ii). Given a productive program, not everyinfinite S-derivation for it necessarily computes an infinite term that is coinductively entailed by it:

Example1.7. The programP5 is a slight modification ofP4:

0. from(X,scons(X,Y))← from(s(X),Y),error(0)

This program is productive, and gives rise to infinite S-derivations, such as that depicted below, mak-ing an infinite number of unification steps for terms involvingfrom and perhaps none forerror(0).

from(0,X)

{X 7→[0,X′]}→

from(0, [0,X′ ])

from(s(0),X′), error(0)

{X′ 7→[s(0),X′′]}→

from(0, [0, [s(0),X′′ ]])

from(s(0), [s(0),X′′ ]]), error(0), error(0)

from(s(s(0))),X′′), error(0), error(0)

{X′′ 7→[s(s(0)),X′′′]}→

However, since this program’s clause’s second subgoalerror(0) is unprovable,P5 does not logi-cally entailt ′, and we do not want to consider the above S-computation at infinity sound.

A similar modification can be made to programP2 so that the resulting program gives rise toinfinite S-derivations that compute the rational infinite term t in Example 1.2, butt would not belogically entailed by that modified program. We see again that productivity, logical entailment, andrationality do not coincide.

The situation can be even more intricate, as inductive fragments of programs can be interleavedwith coinductive fragments, as for programP3 in Example 1.5. Any S-derivation that infinitely ex-pands the subgoals involvingfibs but does not synchronously find finite proofs foradd will beunusable for (semi-)deciding logical entailment for this program. These examples show that obser-vational productivity is an important condition, but is notitself sufficient for ensuring coinductiveentailment. For programs likeP3 andP5, for which coinductive fragments of proofs can be inter-leaved with inductive fragments, we must develop a more sophisticated representation of branchesand nodes in rewriting trees in order to force evaluation of inductive subgoals. We elaborate the

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 8: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:8

details in Section 2, but for now we just note that the formal definition ofS-computations at infinitygiven in Section 4 carefully takes into account this inductive-coinductive nature of LP proof search.S-computations at infinity are thus those guaranteed to produce not just any infinite terms, but thosethat are actually logically entailed by the program under consideration.

To have a solution to (ii), it remains to define a coinductive proof principle appropriate for S-resolution. Recall that SLD-derivations are transitions between terms, and that CoLP’s coinductivehypotheses and conclusions are given by pairs of looping terms. By contrast, S-derivations aretransitions between rewriting trees. This suggests that properties of rewriting trees should be used inidentifying coinductive hypotheses and conclusions. In CoLP, pairs of looping terms in a derivationare automatically detected by unifying all subgoals in its history. Identifying coinductive hypothesesand conclusions via rewriting trees will require a new approach to automated discovery of suitableinvariants.

in Section 5, we propose a method that, based on identification of a suitable invariant, firstautomatically extracts coinductive hypotheses from initial fragments of infinite derivations andthen uses those coinductive hypotheses to close those infinite derivations. For example, the sec-ond rewriting tree in the previously illustrated S-derivation for programP4 can inductively beseen as asserting the implicationfrom(s(0),X′)⇒ from(0,scons(0,X′)). Indeed, the tree couldbe seen as a guarantee that if we can find a proof forfrom(s(0),X′) then we will have a prooffor from(0,scons(0,X′)). We can use this observation to form the coinductive hypothesis CH:from(0,scons(0,X′))⇒ from(s(0),X′).

In accordance with principle of guarded coinduction, we cannot apply this coinductive hypothesiswithin the same (here, the second) rewriting tree in the derivation. Instead, to guard the applicationof coinductive hypothesis, we must verify that it is applicable in some subsequent rewriting tree.Since the coinductive hypothesis can be applied in the thirdtree, we say that the derivation up to thethird rewriting tree is acoinductive prooffor from(0,X) by CH. Viewing LP queries existentially,as is customary, this coinductive proof forfrom(0,X) implies there is a substitutionθ for from(0,X)such thatθ (from(0,X)) is coinductively entailed byP4. The derivation up to the third tree partiallyobserves such substitution:X 7→ scons(0,scons(s(0),X′′)).

If we compare this process of coinductive hypothesis formation to CoLP, we will notice twomain differences. First, CoLP’s loop detection only supports the formation of coinductive hy-potheses of atomic form. In the example derivation above, the coinductive hypothesesfrom(0,X),from(s(0),X′), andfrom(s(s(0)),X′′) detected by CoLP do not unify, which prevents CoLP frommaking coinductive conclusions. But thanks to S-resolution’s rewriting trees, we can automaticallyform coinductive hypotheses that take the form of more general Horn clauses. Secondly, this newmethod of forming coinductive hypotheses does not depend onthe assumption that the terms gener-ated by the S-derivation at infinity must be regular. Therefore, the key to its soundness is in findingthe “right” rewriting subtree on which to form the coinductive hypothesis and on finding the con-ditions under which application of the coinductive hypothesis is guarded. Section 5 considers thesematters formally.

For mixed induction and coinduction, further conditions are needed to embed inductive deriva-tions into coinductive proofs. Subject to careful analysisof this mix, for any productive logic pro-gramP, coinductive proofs forP are sound and complete relative to S-computations at infinity. Tothe best of our knowledge, no analogue of this result has everbefore been presented in the literature.

In fact, we have solved not only problem (ii), but problem (iv) as well. Indeed, we have gen-eralised the method of detecting coinductive hypothesis and conclusions beyond what is possiblein CoLP, and we can also coinductively observe proofs of logical entailment even when no finiterepresentation of an infinite term S-computable at infinity is possible. Altogether, S-resolution hasallowed us to restore the symmetry between inductive and coinductive methods in LP. Taking the se-mantics of computations at infinity [Lloyd 1987; van Emden and Abdallah 1985] as a starting point,S-resolution gives rise to a new observation-based operational semantics that is both sound and com-plete relative to S-computations at infinity, and embodies the new principles (i) through (iv) as well.More generally, S-resolution establishes a new observational means of analysing logic programs

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 9: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:9

that not only separates the notions of productivity and logical entailment, but also introduces anew coinductive principle for automating coinductive proofs for both rational and irrational infiniteterms.

1.5. Main Results

This paper gives a complete description of the declarative and operational semantics of S-resolutionfor inductive, coinductive, and mixed inductive-coinductive fragments of LP. In Section 2 we intro-duce background definitions concerning LP, including leastand greatest complete Herbrand modelsemantics and operational semantics of SLD- and S-resolution given by reduction systems. In Sec-tion 2.4, we prove the soundness, and show the incompleteness, of S-resolution reductions withrespect to least Herbrand models. In Section 3, we regain completeness of proof search by intro-ducing rewriting trees and rewriting tree transitions (which we callS-derivations), and proving thesoundness and completeness of successful S-derivations with respect to least Herbrand models.This completes the discussion of inductive properties of proof-search by S-resolution, and lays thefoundation for developing a new coinductive operational semantics for LP via S-resolution in Sec-tions 4 and 5. In Section 4, we define S-computations at infinity and show that they generalise SLD-computations at infinity. We reconstruct the standard soundness result for computations at infinityrelative to greatest complete Herbrand models, but now for S-computations at infinity. In Section 5we formulate a new coinductive proof principle suitable forthe construction of coinductive proofsin LP, and prove our main result (Theorem 5.17): the soundness and completeness of coinductiveproofs with respect to S-computations at infinity. We get thesoundness of coinductive proofs withrespect to greatest complete Herbrand models as a corollary(Corollary 5.18).

Together with previously known results [Lloyd 1987; van Emden and Abdallah 1985], this paperdraws the following picture (present contributions shown in italics):

t has a finite (inductively successful) S-resolution reduction{Theorem 2.13}

⇒ σ(t) is in the least Herbrand model forP for someσ{Theorem 3.15}

⇔ t has an S-refutation (inductive proof)

and

t is SLD-computable at infinity byt ′

{Proposition 4.9}⇒ t is S-computable at infinity (or implied at infinity) by t′

{Theorem 5.17}⇔ there is a coinductive proof for t′

{Corollary 5.18}⇒ σ(t ′) is in the greatest complete Herbrand model forP for someσ

In Section 6 we conclude and discuss related work.S-resolution is completely general, and soundly observes coinductive proofs even for pro-

grams that mix induction and coinduction. An implementation of this paper is available athttp://staff.computing.dundee.ac.uk/katya/CoALP/.

2. PRELIMINARIES

In this section we introduce Horn clauses, and recall the declarative (big-step) semantics of logicprograms given by least and greatest complete Herbrand models. We also introduce structural reso-lution reduction by means of an operational (small-step) semantics. To enable the analysis of coin-ductive semantics and infinite terms, we adopt the standard view of terms as trees [Courcelle 1983;Jaffar and Stuckey 1986; Lloyd 1987].

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 10: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:10

2.1. First-Order Signatures, Terms, Clauses

We writeN∗ for the set of all finite words over the setN of natural numbers. The length ofw∈ N∗

is denoted|w|. The empty wordε has length 0; we identifyi ∈N and the wordi of length 1. Lettersfrom the end of the alphabet denote words of any length, and letters from the middle of the alphabetdenote words of length 1. The concatenation ofw andu is denotedwu; v is a prefixof w if thereexists au such thatw= vu, and aproper prefixof w if u 6= ε.

A setL⊆ N∗ is a(finitely branching) tree languageprovided: i) for allw∈ N

∗ and alli, j ∈ N, ifw j ∈ L thenw∈ L and, for alli < j, wi ∈ L; and ii) for allw∈ L, the set of alli ∈N such thatwi ∈ Lis finite. A non-empty tree language always containsε, which we call itsroot. Thedepthof a treelanguageL is the maximum length of a word inL. A tree language isfinite if it is a finite subset ofN∗, andinfiniteotherwise. A wordw∈ L is also called anodeof L. If w= w0w1...wl thenw0w1...wk

for k < l is anancestorof w. The nodew is theparentof wi, and nodeswi for i ∈ N arechildrenof w. A branchof a tree languageL is a subsetL′ of L such that, for allw,v∈ L′, w is an ancestorof v or v is an ancestor ofw. If L is a tree language andw is a node ofL, thesubtree of L at wisL\w= {v | wv∈ L}.

A signatureΣ is a non-empty set offunction symbols, each with an associated arity. The arity off ∈Σ is denotedarity( f ). To define terms overΣ, we assume a countably infinite setVar of variablesdisjoint fromΣ, each with arity 0. Capital letters from the end of the alphabet denote variables inVar. If L is a non-empty tree language andΣ is a signature, then atermoverΣ is a functiont : L→Σ∪Var such that, for allw∈ L, arity(t(w)) = |{i |wi ∈ L}|. Terms are finite or infinite according astheir domains are finite or infinite. A termt has a depthdepth(t) = max{|w| | w∈ L}. The subtreesubterm(t,w) of t at nodew is given byt ′ : (L\w)→ Σ∪V, wheret ′(v) = t(wv) for eachwv∈ L\w.The set of finite (infinite) terms over a signatureΣ is denoted byTerm(Σ) (Term∞(Σ)). The set ofall (i.e., finiteand infinite) terms overΣ is denoted byTermω(Σ). Terms with no occurrences ofvariables areground. We writeGTerm(Σ) (GTerm∞(Σ), GTermω (Σ)) for the set of finite (infinite,all) ground terms overΣ.

A substitutionover Σ is a total functionσ : Var→ Termω(Σ). We say thatσ is a groundingsubstitution for t if σ(t) ∈ GTermω (Σ), and is just agrounding substitutionif its codomain isGTermω(Σ). We write id for the identity substitution. The set of all substitutionsover a signatureΣ is Substω (Σ) and the set of all substitutions overΣ with only finite terms in their codomainsis Subst(Σ). Substitutions are extended from variables to terms homomorphically: if t ∈ Term(Σ)andσ ∈ Substω (Σ), then theapplicationσ(t) is (σ(t))(w) = t(w) if t(w) 6∈ Var, and(σ(t))(w) =(σ(X))(v) if w= uv, t(u) = X, andX∈Var. Composition of substitutions is denoted by juxtaposition.Composition is associative, so we writeσ3σ2σ1 rather than(σ3σ2)σ1 or σ3(σ2σ1).

A substitutionσ ∈ Subst(Σ) is aunifier for t,u∈ Term(Σ) if σ(t) = σ(u), and is amatcherfor tagainstu if σ(t) = u. If t,u∈ Termω (Σ), then we say thatu is aninstanceof t if σ(t) = u for someσ ∈ Substω (Σ); note that ift,u∈ Subst(Σ), i.e., if t andu are finite terms, then the codomain ofσcan be taken, without loss of generality, to involve only finite terms. A substitutionσ1 ∈ Subst(Σ)is more generalthan a substitutionσ2 ∈ Subst(Σ) if there exists a substitutionσ ∈ Substω (Σ) suchthatσσ1(X) = σ2(X) for everyX∈ Var. A substitutionσ ∈ Subst(Σ) is amost general unifier(mgu)for t andu, denotedt ∼σ u, if it is a unifier fort andu and is more general than any other such unifier.A most general matcher(mgm) σ for t againstu, denotedt ≺σ u, is defined analogously. Both mgusand mgms are unique up to variable renaming if they exist. Unification is reflexive, symmetric,and transitive, but matching is reflexive and transitive only. Mgus and mgms are computable byRobinson’s seminal unification algorithm [Robinson 1965].

In many unification algorithms, theoccurs checkcondition is imposed, so that substitution bind-ings of the formX 7→ t(X), wheret(X) is a term containingX, are disallowed. In this case, mgusand mgms can always be taken to beidempotent, i.e., such that the sets of variables appearing intheir domains and codomains are disjoint. The occurs check is critical for termination of unificationalgorithms, and this is, in turn, crucial for the soundness of classical SLD-resolution; see below.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 11: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:11

In logic programming, a clauseC over Σ is a pair (A, [B0, ...,Bn]), whereA ∈ Term(Σ) and[B0, . . .Bn] is a list of terms inTerm(Σ); such a clause is usually writtenA← B0, . . . ,Bn. Notethat the list of terms can be the empty list[ ]. We will identify the singleton list[t] with the termt when convenient. The headA of C is denotedhead(C) and the bodyB0, . . . ,Bn of C is denotedbody(C). A goal clause GoverΣ is a clause ?← B0, . . . ,Bn overΣ∪{?}, where ? is a special sym-bol not inΣ∪Var. We abuse terminology and consider a goal clause overΣ to be a clause overΣ.The set of all clauses overΣ is denoted byClause(Σ). A logic programoverΣ is a total functionfrom a set{0,1, . . . ,n} ⊆ N to the set of non-goal clauses overΣ. The clauseP(i) is called theith

clause ofP. If a clauseC is P(i) for somei, we writeC∈ P. The set of all logic programs overΣ isdenotedLP(Σ). Thepredicateof a clauseC is the top symbol of the termhead(C). The predicatesof a program are the predicates of its clauses. Thearity of P∈ LP(Σ) is the number of clauses inP,i.e., is|dom(P)|, and is denotedarity(P). Thearity of C∈ Clause(Σ) is |body(C)|, and is similarlydenotedarity(C).

We extend substitutions from variables to clauses and programs homomorphically. We omit thesestandard definitions. The variables of a clauseC can be renamed with “fresh” variables to get anα-equivalent clause that is interchangeable withC. We assume variables have been renamed whenconvenient. This is standard and helps avoid circular (non-terminating) unification and matching.

2.2. Big-step Inductive and Coinductive Semantics for LP

We recall the least and greatest complete Herbrand model constructions for LP [Lloyd 1987]. Weexpress the definitions in the form of a big-step semantics for LP, thereby exposing duality of induc-tive and coinductive semantics for LP in the style of [Sangiorgi 2012]. We start by giving inductiveinterpretations to logic programs.

Definition 2.1. Let P∈ LP(Σ). Thebig-step rule for Pis given by

P |= σ(B1), . . . ,P |= σ(Bn)

P |= σ(A)

whereA← B1, . . .Bn is a clause inP andσ ∈ Substω(Σ) is a grounding substitution.

Following standard terminology (see, e.g., [Sangiorgi 2012]), we say that an inference rule isap-plied forward if it is applied from top to bottom, and that it isapplied backwardif it is appliedfrom bottom to top. If a set of terms is closed under forward (backward) application of an inferencerule, we say that it isclosed forward(resp.,closed backward) under that rule. If theith clause ofP∈ LP(Σ) is involved in an application of the big-step rule forP, then we may say that we haveapplied thebig-step rule for P(i).

Definition 2.2. The least Herbrand modelfor P∈ LP(Σ) is the smallest setMP ⊆ GTerm(Σ)that is closed forward under the big-step rule forP.

Example2.3. The least Herbrand model forP1 is {nat(0), nat(s(0)), nat(s(s(0))), . . .}.

The requirement thatMP ⊆ GTerm(Σ) entails that only ground substitutionsσ ∈ Subst(Σ) areused in the forward applications of the big-step rule involved in the construction ofMP. Next we givecoinductive interpretations to logic programs. For this wedo not impose any finiteness requirementon the codomain terms ofσ .

Definition 2.4. Thegreatest complete Herbrand modelfor P∈ LP(Σ) is the largest setMωP ⊆

GTermω(Σ) that is closed backward under the big-step rule forP.

Example2.5. The greatest complete Herbrand model forP1 is {nat(0), nat(s(0)),nat(s(s(0))), . . .}

⋃{nat(s(s(...)))}. Indeed, there is an infinite inference fornat(s(s(...))) ob-

tained by repeatedly applying the big-step rule forP1(1) backward.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 12: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:12

Definitions 2.2 and 2.4 could alternatively be given in termsof least and greatest fixed pointoperators, as in, e.g., [Lloyd 1987]. To ensure thatGTerm(Σ) and GTermω(Σ) are non-empty,and thus that the least and greatest Herbrand model constructions are as intended, it is standard inthe literature to assume thatΣ contains at least one function symbol of arity 0. We will makethisassumption throughout the remainder of this paper.

2.3. Small-step Semantics for LP

Following [Fu and Komendantskaya 2015], we distinguish thefollowing three kinds of reductionfor LP.

Definition 2.6. If P∈ LP(Σ) andt1, . . . , tn ∈ Term(Σ), then

— SLD-resolution reduction: [t1, . . . , ti , . . . , tn]❀P [σ(t1), . . . ,σ(ti−1),σ(B0), . . .σ(Bm),σ(ti+1), . . . ,σ(tn)]if A← B0, . . . ,Bm∈ P andti ∼σ A.

— rewriting reduction: [t1, . . . , ti , . . . , tn] →P [t1, . . . , ti−1,σ(B0), . . .σ(Bm), ti+1, . . . , tn] if A ←B0, . . . ,Bm∈ P andA≺σ ti .

— substitution reduction: [t1, . . . , ti , . . . , tn] →P [σ(t1), . . . ,σ(ti), . . . ,σ(tn)] if A← B0, . . . ,Bm∈ P andti ∼σ A.

If r is any reduction relation, we will abuse terminology and call any (possibly empty) sequence ofr-reduction steps anr-reduction. When there exists no listL of terms such that[t1, . . . , ti , . . . , tn]→P Lwe say that[t1, . . . , tn] is in→-normal formwith respect toP. We write [t1, ..., tn]→

µP to indicate

the reduction of[t1, ..., tn] to its→-normal form with respect toP if this normal form exists, andto indicate an infinite reduction of[t1, ..., tn] with respect toP otherwise. We write→n to denoterewriting byat most nsteps of→, wheren is a natural number. We will use similar notations for❀

and→ as required. Throughout this paper we may omit explicit mention of P and/or suppressP asa subscript on reductions when it is clear from context.

We are now in a position to define the structural resolution reduction, also called theS-resolutionreductionfor short. We have:

Definition 2.7. ForP∈LP(Σ), thestructural resolution reductionwith respect toP is →1P ◦→

µP.

It is not hard to see that the reduction relation❀P models traditional SLD-resolutionsteps [Lloyd 1987] with respect toP, and, writing❀sP for →1

P ◦ →µP, that the reduction relation

❀sP models S-resolution steps with respect toP. If an SLD-resolution, rewriting, or S-resolutionreduction with respect toP starts with[t], then we say it is a reductionfor t with respect toP. Ifthere exists ann such that[t]❀n

P [ ] or [t]❀nsP [ ], then we say that this reduction fort is inductively

successful. For SLD-resolution reductions this agrees with standard logic programming terminol-ogy.

If we regard the termt as a “query”, then we may regard the compositionσn◦ . . .◦σ1 of the sub-stitutionsσ1, . . . ,σn ∈ Subst(Σ) involved in the steps of an inductively successful SLD-resolutionreduction fort as an “answer” to this query, and we may think of the reductionas computing thisanswer. Such a composition for an initial sequence of SLD-resolution reductions in a possibly non-terminating SLD-resolution reduction fort can similarly be regarded as computing a partial answerto that query. We use this terminology for rewriting and S-resolution reductions as well.

Example2.8. The following are SLD-resolution, rewriting, and S-resolution reductions, respec-tively, with respect toP2:

— [nats(X)]❀ [nat(X′),nats(Y)]❀ [nats(Y)]❀ [nat(X′′),nats(Y′)]❀ . . .

— [nats(X)]

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 13: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:13

— [nats(X)] →µ [nats(X)] →1 [nats(scons(X′,Y))] →µ [nat(X′),nats(Y)] →1

[nat(0),nats(Y)]→µ [nats(Y)] →1 [nats(scons(X′′,Y′))]→µ . . .

In the S-resolution reduction above,[nats(X)]→µ [nats(X)] in 0 steps, since[nats(X)] is alreadyin →-normal form. The initial sequences of the SLD-resolution and S-resolution reductions eachcompute the partial answer{X 7→ scons(0,scons(X′′,Y′))} to the querynats(X).

The observation that, even for coinductive program likeP2, →µ reductions are finite and thuscan serve as measures of finite observation, has led to the following definition of observationalproductivity in LP, first introduced in [Komendantskaya et al. 2014]:

Definition 2.9. A programP∈ LP(Σ) is observationally productiveif →P is strongly normal-ising, i.e., if every rewriting reduction with respect toP is finite.

Example2.10. The programsP1,P2,P3,P4, andP5 are all observationally productive, as is theprogramP7 defined in Example 2.15 below. By contrast, the “bad” programof Example 1.3 and theprogramP6 defined in Example 2.11 below are not.

A similar notion of observational productivity, in terms ofstrong normalisation of term rewriting,has recently been introduced for copatterns in functional programming [Basold and Hansen 2015].

A general analysis of observational productivity for LP is rather subtle. Indeed, there are programsP and queriest for which there are inductively successful SLD-resolutionreductions, but for whichP nevertheless fails to be observationally productive because there exist no inductively successfulS-resolution reductions.

Example2.11. Consider the graph connectivity program [Sterling and Shapiro 1986]P6 givenby:

0. conn(X,Y)← conn(X,Z),conn(Z,Y)1. conn(a,b)←2. conn(b,c)←

Although there exist inductively successful SLD-resolution reductions forconn(X,Y) with respectto P6, there are no such inductively successful S-resolution reductions. Indeed, the only S-resolutionreductions forconn(X,Y)with respect toP6 are infinite rewriting reductions that, with each rewritingreduction, accumulate an additional term involvingconn. A representative example of such an S-resolution reduction is

[conn(X,Y)]→ [conn(X,X′),conn(X′,Y)]→ [conn(X,X′′),conn(X′′,X′),conn(X′,Y)]→ . . .

Thus,P6 is not observationally productive.

With this in mind, we first turn our attention to analysing theinductive properties of S-resolutionreductions.

2.4. Inductive Properties of S-Resolution Reductions

In this section, we discuss whether, and under which conditions, S-resolution reductions areinductively sound and complete. First we recall that SLD-resolution is inductively sound andcomplete [Lloyd 1987]. The standard results of inductive soundness and completeness for SLD-resolution [Lloyd 1987] can be summarised as:

THEOREM 2.12. Let P∈ LP(Σ) and t∈ Term(Σ).

— (Inductive soundness of SLD-resolution reductions) If t❀nP [] for some n and computes answerθ ,

then there exists a term t′ ∈GTerm(Σ) such that t′ ∈MP and t′ is an instance ofθ (t).— (Inductive completeness of SLD-resolution reductions) If t ∈ MP, then there exists a term t′ ∈

Term(Σ) that yields an SLD-resolution reduction t′ ❀nP [] that computes answerθ ∈ Subst(Σ)

such that t is an instance ofθ (t ′).

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 14: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:14

We now show that, in contrast to SLD-resolution reductions,S-resolution reductions are induc-tively sound but incomplete. We first establish inductive soundness.

THEOREM 2.13. (Inductive soundness of S-resolution reductions) If t❀nsP [] for some n and

computes answerθ , then there exists a term t′ ∈ Term(Σ) such that t′ ∈MP and t′ is an instance ofθ (t).

PROOF. The proof is by induction onn in ❀nsP. It is a simple adaptation of the soundness proof

for SLD-resolution reductions given in, e.g., [Lloyd 1987].

To show that S-resolution reductions are not inductively complete, it suffices to provide oneexample of a programP and a termt such thatP |= θ (t) but no inductively successful S-resolutionreduction exists fort. We will in fact give two such examples, each of which is representative of adifferent way in which S-resolution reductions can fail to be inductively complete.

Example2.14. ConsiderP6 and the S-resolution reduction shown in Example 2.11. The in-stantiationconn(a,c) of conn(X,Y) is in the least Herbrand model ofP6, but there are no finiteS-resolution reductions, and therefore no inductively successful S-resolution reductions, forP6 andthe queryconn(X,Y). This shows that programs that are not observationally productive need not beinductively complete.

In light of Example 2.14 it is tempting to try to prove the inductive completeness of S-resolutionreductions for observationally productive logic programsonly. However, this would not solve theproblem, as the following example confirms:

Example2.15. Consider the programP7 given by:

0. p(c)←1. p(X)← q(X)

We have thatP7 |= p(c) for the instantiationp(c) of p(X), but there is no inductively successfulS-resolution reduction forP7 andp(X).

ProgramP7 is an example ofoverlappingprogram, i.e., a program containing clauses whoseheads unify. We could show that, for programs that are both observationally productive and non-overlapping, S-resolution reductions are inductively complete. However, restricting attention to non-overlapping programs would seriously affect generality ofour results, and would have the effect ofmaking S-resolution even less suited for inductive proof search than SLD-resolution. We preferinstead to refine S-resolution so that it is inductively complete for all programs. The question iswhether or not such refinement is possible.

An intuitive answer to this question comes from reconsidering Example 2.15. There, the interleav-ing of→µ and→1 has the effect of restricting the search space. Indeed, oncethe rewriting portionof the only possible S-resolution reduction onp(X) is performed, the new subgoalq(X) prevents usfrom revisiting the initial goalp(X) and unifying it with the clauseP7(0), as would be needed foran inductively successful S-resolution reduction. This ishow we lose inductive completeness of theproof search.

One simple remedy would be to redefine S-resolution reductions to be(→1 ◦ →n)-reductions,wheren ranges over all non-negative integers. This would indeed restore inductive completenessof S-resolution for overlapping programs. But it would at the same time destroy our notion of ob-servational productivity, which depends crucially on→µ . An alternative solution would keep ourdefinitions of S-resolution reductions and observational productivity intact, but also find a way tokeep track of all of the unification opportunities arising inthe proof search. This is exactly the routewe take here.

Kowalski [Kowalski 1974] famously observed thatLogic Programming = Logic + Control. ForKowalski, the logic component was given by SLD-resolution reductions, and the control componentby an algorithm coding the choice of search strategy. As it turns out, SLD-resolution reductions are

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 15: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:15

sound and complete irrespective of the control component. What we would like to do in this paper isrevise the very logic of LP by replacing SLD-resolution reductions with S-resolution reductions. Asalready noted in the introduction, this new logic promises to bring a natural notion of observationalproductivity to LP, and has the potential to, finally, put coinductive proof in LP on par with that ininteractive theorem provers.

However, inductive completeness is a minimum “sanity check” for any new logic used to investi-gate coinductive proof in LP and, unfortunately, we have just seen that the new logic of S-resolutionis not inductively complete on its own. As it turns out, defining a notion of S-resolution that isboth inductively complete and capable of capturing observational productivity requires imposingan appropriate notion of control on this logic. In the next section we therefore define S-resolutionin terms of rewriting trees, which are also defined there. Rewriting trees allow us to neatly inte-grate precisely the control on S-resolution reductions needed to achieve both of these aims for theunderlying logic of S-resolution reductions. We thus arrive at our own variant of Kowalski’s for-mula, namelyStructural Logic Programming = Logic + Control— but now the logic is given byS-resolution reductions and the control component is captured by rewriting trees. The remainder ofthe paper is devoted to developing the above formula into a formal theory.

3. INDUCTIVE SOUNDNESS AND COMPLETENESS OF STRUCTURAL RESOLUTION

To ensure that S-resolution reductions are inductively complete, we need to impose more control onthe rewriting reductions involved in them. To do this, we first note that the rewriting reduction inExample 2.15 can be represented as the tree

p(X)

q(X)

Now, we would also like to reflect within this tree the fact that p(X) to unifies with the head ofclauseP7(0) and, more generally, to reflect the fact that any term can, in principle, unify with thehead of any clause in the program. We can record these possible unifications in tree form, as follows:

p(X)

P7(0)? q(X)

P7(0)? P7(1)?

We can now follow-up each of these possibilities and in this way extend our proof search. Todo this formally, we distinguish two kinds of nodes:and-nodes, which capture terms coming fromclause bodies, andor-nodes, which capture the idea that every term can, in principle, match severalclause heads. We also introduceor-node variablesto signify the possibility of unifying a term withthe head of a clause when the matching of that term against that clause head fails. This carefultracking of possibilities allows us to construct the inductively successful S-resolution reduction forp(X) and programP7 shown in Figure 1. The figure depicts two rewriting trees, each modelling allpossible rewriting reductions for the given query (represented as a goal clause) with respect toP7.Rewriting trees have alternating levels of or-nodes and and-nodes, as well as or-node variables (X1,X2, andX3 in the figure) ranging over rewriting trees. By unifyingp(X) with P7(0) we replace theor-node variableX1 in the first rewriting tree with a new rewriting tree (in this case consisting ofjust the single nodep(c)←) to transition to the second rewriting tree shown. When a node containsa clause, such asp(c)←, that has an empty body, it is equivalent to an empty subgoal.Thus, theunderlined subtree of the second rewriting tree in Figure 1 represents the inductively successfulS-resolution reductionP7 ⊢ p(c)→ [].

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 16: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:16

?← p(X)

p(X)

X1 p(X)← q(X)

q(X)

X2 X3

{X 7→c}→X1

?← p(c)

p(c)

p(c)← p(c)← q(c)

q(c)

X2 X3

Fig. 1. A tree transition for the overlapping programP7 and the goal clause← p(X). Underlined in the second rewritingtree is a inductively successful S-resolution reduction.

3.1. Modeling →µ by Rewriting Trees

We now proceed to define the construction formally. For this,we first observe that a clauseC over asignatureΣ that is of the formA←B0, . . . ,Bn can be naturally represented as the total function (alsocalledC) from the finite tree languageL = {ε,0, ...,n} of depth 1 toTerm(Σ) such thatC(ε) = AandC(i) = Bi for i = dom(C)\ {ε}. With this respresentation of clauses in hand, we can formaliseour notion of a rewriting tree.

Definition 3.1. Let VR be a countably infinite set of variables disjoint fromVar. If P∈ LP(Σ),C ∈ Clause(Σ), and σ ∈ Subst(Σ) is idempotent, then the treerew(P,C,σ) is the functionT :dom(T)→ Term(Σ)∪Clause(Σ)∪VR, wheredom(T) 6= /0 is a tree language defined simultanouslywith rew(P,C,σ), such that:

(1) T(ε) = σ(C) and, for alli ∈ dom(C)\ {ε}, T(i) = σ(C(i)).(2) Forw∈ dom(T) with |w| even and|w|> 0, T(w) ∈ Clause(Σ)∪VR. Moreover,

(a) If T(w) ∈VR, then{ j | w j ∈ dom(T)}= /0.(b) If T(w) = B ∈ Clause(Σ), then there exists a clauseP(i) and aθ ∈ Subst(Σ) such that

P(i) ≺θ head(B). Moreover, for everyj ∈ dom(P(i)) \ {ε}, w j ∈ dom(T) and T(w j) =σ(θ (P(i)( j))).

(3) Forw∈ dom(T) with |w| odd,T(w) ∈ Term(Σ). Moreover, for everyi ∈ dom(P), we have(a) wi ∈ dom(T).

(b) T(wi) =

{σ(θ (P(i))) if head(P(i))≺θ T(w)a freshX ∈VR otherwise

(4) No other words are indom(T).

T(w) is anor-nodeif |w| is even, and anand-nodeif |w| is odd.

If P∈ LP(Σ), thenT is arewriting tree for Pif it is either the empty tree orrew(P,C,σ) for someC andσ . Since mgms are unique up to variable renaming,rew(P,C,σ) is as well. A rewriting treefor a programP is finite or infinite according as its domain is finite or infinite. We writeRew(P)(Rew∞(P), Rewω(P)) for the set of all finite (infinite,all) rewriting trees forP.

This style of tree definition mimics the classical style of defining terms as maps from a treelanguage to a given domain [Lloyd 1987; Courcelle 1983]. As with tree representations of terms,arity constraintsare imposed on rewriting trees. The arity constraints in items 2b and 3a specify thatthe arity of an and-node is the number of clauses in the program and the arity of an or-node is thenumber of terms in its clause body. The arity constraint in item 2a specifies that or-node variablesmust have arity 0. Or-node variables indicate where in a rewriting tree substitution can take place.

Example3.2. The rewriting trees rew(P3,? ← fibs(0,s(0),X), id), rew(P3, ? ←fibs(0,s(0),cons(0,S)), id), and rew(P3,? ← fibs(0,s(0),cons(0,S)), {Z 7→ s(0)}) areshown in Figure 2. Note the or-node variables and the arities. An or-node can have arity 0, 1, or 2

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 17: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:17

?← fibs(0,s(0),X) →X3

fibs(0,s(0),X)

X1 X2 X3

?← fibs(0,s(0), [0,S])

fibs(0,s(0), [0,S])

X1 X2 fibs(0,s(0), [0,S])← add(0,s(0),Z),fibs(s(0),Z,S)

add(0,s(0),Z)

X4 X5 X6

fibs(s(0),Z,S)

X7 X8 X9

→X4 ?← fibs(0,s(0), [0,S]) →X9 . . .

fibs(0,s(0), [0,S])

X1 X2 fibs(0,s(0), [0,S])← add(0,s(0),s(0)),fibs(s(0),s(0),S)

add(0,s(0),s(0))

add(0,s(0),s(0))← X5 X6

fibs(s(0),s(0),S)

X7 X8 X9

Fig. 2. An initial fragment of an S-derivation forP3 andfibs(0,s(0),X). The three rewriting trees shown arerew(P3,?←fibs(0,s(0),X), id), rew(P3, ?← fibs(0,s(0),cons(0,S)), id), andrew(P3,?← fibs(0,s(0),cons(0,S)), {Z 7→ s(0)}),respectively. To save space in the figure we abbreviatecons( , ) by [ , ], and similarly below.

according as its clause body contains 0, 1, or 2 terms, and every and-node has arity 3 becauseP3has three clauses.

Although perhaps mysterious at first, the third parameterσ in Definition 3.1 forT = rew(P,C,σ)is necessary account for variables occurring inT not affected by mgms computed duringT ’s con-struction. It plays a crucial role in ensuring that applyinga substitution to a rewriting tree againyields a rewriting tree [Johann et al. 2015a]. We have:

Definition 3.3. Let P ∈ LP(Σ), C ∈ Clause(Σ), σ ,σ ′ ∈ Subst(Σ) idempotent, andT =rew(P,C,σ). Then the rewriting treeσ ′(T) is defined as follows:

— for everyw ∈ dom(T) such thatT(w) is an and-node or non-variable or-node,(σ ′(T))(w) =σ ′(T(w)).

— for everywi ∈ dom(T) such thatT(wi) ∈ VR, if head(P(i)) ≺θ σ ′(T)(w), then(σ ′(T))(wiv) =rew(P,θ (P(i)),σ ′σ)(v). (Note v = ε is possible.) If no mgm ofhead(P(i)) againstσ ′(T)(w)exists, then(σ ′(T))(wi) = T(wi).

Both conditions in the above definition are critical for ensuring that σ ′(T) satisfies Defini-tion 3.1. We then have the following substitution theorem for rewriting trees. It is provedin [Johann et al. 2015a].

THEOREM 3.4. Let P∈ LP(Σ), C∈ Clause(Σ), andθ ,σ ∈ Subst(Σ). Thenθ (rew(P,C,σ)) =rew(P,C,θσ).

We can now formally establish the relation between rewriting reductions and rewriting trees. Wefirst have the following proposition, which is an immediate consequence of Definitions 2.9 and 3.1:

PROPOSITION 3.5. P ∈ LP(Σ) is observationally productive iff, for every term t∈ Term(Σ)and every substitutionσ ∈ Subst(Σ), rew(P,?← t,σ) is finite.

We can further establish a correspondence between certain subtrees of rewriting trees and induc-tively successful S-resolution reductions.

Definition 3.6. A treeT ′ is a rewriting subtreeof a rewriting treeT if dom(T ′) ⊆ dom(T) andthe following properties hold:

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 18: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:18

?← conn(a,c) →X3

conn(a,c)

conn(a,c)← conn(a,Z),conn(Z,c)

conn(a,Z)

.

.

.X3 X4

conn(Z,c)

.

.

.X5 X6

X1 X2

?← conn(a,c)

conn(a,c)

conn(a,c)← conn(a,b),conn(b,c)

conn(a,b)

.

.

.conn(a,b)← X4

conn(b,c)

.

.

.X5 conn(b,c)←

X1 X2

Fig. 3. An S-refutation for the programP6 of Example 2.11 andconn(a,c). The left treerew(P6,?← conn(a,c), id) isnot an inductive success tree. However, the right treerew(P6,?← conn(a,c),{Z 7→ b}) is. The inductive success subtree ofthe right tree is underlined.

(1) T ′(ε) = T(ε).(2) If w∈ dom(T ′) with |w| even, thenT ′(w) = T(w), wi ∈ dom(T ′) for everywi ∈ dom(T), and

T ′(wi) = T(wi).(3) If w∈ dom(T ′) with |w| odd, thenT ′(w) = T(w), there exists a uniquei with wi ∈ dom(T) such

thatwi ∈ dom(T ′), andT ′(wi) = T(wi) for this i.

Rewriting subtrees can be either finite or infinite. Note thatthe and-nodes in item 2 grow childrenby universal quantification, whereas the or-nodes in item 3 grow them by existential quantification.

Definition 3.7. If T ∈Rewω (P), then an or-node ofT is aninductive success nodeif it is a non-variable leaf node ofT. If T ′ is a finite rewriting subtree ofT all of whose leaf nodes are inductivesuccess nodes ofT, thenT ′ is aninductive success subtreeof T. If T contains an inductive successsubtree then we callT an inductive success tree.

The following proposition is immediate from Definitions 3.1and 3.7:

PROPOSITION 3.8. If P ∈ LP(Σ) and t∈ Term(Σ), then P⊢ t→n [ ] for some n iffrew(P,?←t, id) is an inductive success tree.

With these preliminary results in hand we can now begin to show that rewriting trees impose onS-resolution reductions precisely the control required toprove their soundness and completenesswith respect to least Herbrand models. We first observe that:

THEOREM 3.9. Let P∈ LP(Σ) and t∈ Term(Σ).

— If rew(P,?← t,σ) is an inductive success tree for someσ ∈ Subst(Σ) then, for every instancet ′ ∈GTerm(Σ) of σ(t), t′ ∈MP.

— If t ∈MP, then there exists a grounding substitutionθ ∈ Subst(Σ) such thatrew(P,?← t,θ ) isan inductive success tree.

PROOF. The proof is by induction on the depth of rewriting trees.

Example3.10. The termconn(a,c) is in MP6. The treerew(P6,?← conn(a,c), id) is not aninductive success tree, as Figure 3 shows. However,rew(P6,?← conn(a,c),θ ), for θ = {Z′ 7→ b},is indeed an inductive success tree. This accords with Theorem 3.9.

3.2. Modeling → by Transitions Between Rewriting Trees

Next we define transitions between rewriting trees. Such transitions are defined by the famil-iar notion of a resolvent, and assume a suitable algorithm for renaming “free” clause variablesapart [Johann et al. 2015a]. LetP ∈ LP(Σ) andt ∈ Term(Σ). If head(P(i)) ∼θ t, thenθ is calledthe resolventof P(i) and t. If no suchθ exists thenP(i) and t havenull resolvent. A non-nullresolvent is aninternal resolventif it P(i)≺θ t and anexternal resolventotherwise.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 19: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:19

Definition 3.11. Let T = rew(P,C,σ) ∈ Rewω(P). If X = T(wi) ∈ VR, then the rewriting treeTX is defined as follows: If the external resolventθ for P(i) andT(w) is null, thenTX is the emptytree. Ifθ is non-null, thenTX = rew(P,C,θσ).

If X ∈ VR, we denote the computation ofTX from T ∈ Rewω (Σ) by T → TX. The operationT → TX is a tree transitionfor P andC; specifically, we call the tree transitionT → TX the treetransition forT with respect toX. A tree transitionfor P ∈ LP(Σ) is a tree transition forP andsomeC ∈ Clause(Σ). If T → TX is a tree transition and ifX = T(w), then we say that both thenodeT(w) and the branch ofT that this node lies on areexpandedin this transition. A (finite orinfinite) sequenceT0 = rew(P,?← t, id)→ T1→ T2→ . . . of tree transitions forP is a structuraltree resolution derivation, or simply anS-derivationfor short, forP andt. An S-derivation forPandt is said to be anS-refutation, or aninductive proof, for t with respect toP, if it is of the formT0→ T1→ ...→ Tn for somen, whereTn is an inductive success tree. Figure 2 shows an initialfragment of an infinite S-derivation for the programP3 andfibs(0,s(0),X). The derivations shownin Figures 1 and 3 are inductive proofs forP7 andp(c), and forP6 andconn(a,c), respectively. Notethat the final trees of Figures 1 and 3 show nodes corresponding to (finite) inductively successfulS-reductions forP7 andp(c), and forP6 andconn(a,c), respectively, underlined.

If eachθi is the external resolvent associated with the tree transitionTi−1→ Ti in an S-derivationT0 = rew(P,?← t, id)→ T1→ ...→ Tn, thenθ1, ...,θn is thesequence of resolvents associated withthat S-derivation. In this case, each treeTi in the S-derivation is given byrew(P, ?← t, θi . . .θ2θ1).Note how the third parameter composes the mgus.

Example3.12. The S-derivation in Figure 2 starts withrew(P3,?← fibs(0, s(0),X), id). Itssecond tree can be seen asrew(P3,?← fibs(0,s(0),X),θ1), whereθ1 = {X 7→ cons(0,S)}, andits third tree asrew(P3,?← fibs(0,s(0),X),θ2θ1), whereθ2 = {Z 7→ s(0)}. Here,θ1 andθ2 arethe resolvents for the tree transitions for the first and the second trees with respect toX3 andX4,respectively.

We have just formally rendered the formulaStructural Logic Programming = S-Resolution Re-ductions + Control: we embedded proof search choices and or-node variable substitutions into S-resolution reductions via rewriting trees, thus obtainingthe notion of an S-derivation and the induc-tive proof methodology we callstructural resolution, or S-resolutionfor short. It now remains toexploit the inductive and coinductive properties of our newtheory of S-resolution.

3.3. Inductive Soundness and Completeness of S-Resolution

Before exploiting the coinductive properties of S-resolution we investigate its inductive properties.Some S-derivations for a programP and a termt may be S-refutations and some not, but terminationof one S-derivation in other than an inductive success tree does not mean no S-refutation exists forP andt. This reflects the facts that inductive success is an existential property, and that entailmentfor Horn clauses is only semi-decidable. In this section we present our inductive soundness andcompleteness results for S-resolution. We note that these do not require logic programs to be eitherobservationally productive or non-overlapping.

Example3.13. An S-derivation for the programP6 andconn(a,c) is shown in Figure 3. TheprogramP6 is not observationally productive. An inductive success subtree of the derivation’s finaltree is indicated by underlining. It contains the inductivesuccess nodes labelledconn(a,b)← andconn(b,c)← . Since its final tree is an inductive success tree, this S-derivation is an S-refutationfor P6 andconn(a,c).

Example3.14. An S-refutation for the overlapping programP7 andp(c) is shown in Figure 1.An inductive success subtree of the derivation’s final tree is indicated by underlining.

Inductive soundness and completeness of S-resolution are simple corollaries of Theorem 3.9:

THEOREM 3.15. Let P∈ LP(Σ) and t∈ Term(Σ).

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 20: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:20

— (Inductive soundness of S-resolution) If there is an S-refutation for P and t that computes answerθ , then there exists a term t′ ∈GTerm(Σ) such that t′ ∈MP and t′ is an instance ofθ (t).

— (Inductive completeness of S-resolution) If t∈ MP, then there exist a term t′ ∈ Term(Σ) thatyields an S-refutation for P and t′ that computes answerθ ∈ Subst(Σ) such that t is an instanceof θ (t ′).

We also have the following corollary of Theorem 3.9:

COROLLARY 3.16. Let P∈LP(Σ) and t∈Term(Σ). If there is an S-refutation T0 = rew(P,?←t, id)→ T1→ . . .→ Tn with associated external resolventsσ1, . . . ,σn then, for all grounding substi-tutionsθ ∈ Subst(Σ) for σn . . .σ1(t), θσn . . .σ1(t) ∈ MP.

For an S-refutationrew(P,?← t, id) → T1 → . . . → Tn with associated external resolventsσ1, . . . ,σn, the rewriting treeTn = rew(P, ?← t,σn . . .σ1) can be regarded as a proof witness con-structed for the queryt.

The correspondence between the soundness and completenessof S-refutations and the classicaltheorems of LP captures the (existential) property of inductive success in S-resolution reductions.Our results do not, however, mention failure, which is a universal (and thus more computationallyexpensive) property to establish. Theorems 3.9 and 3.15 also show that rewriting trees can distin-guish derivations proving logical entailment existentially — i.e., for some (ground) instances only— from those proving it universally — i.e., for all (ground) instances. Indeed, Theorems 3.9 andTheorem 3.15 show that proof search by unification has existential properties.

Example3.17. Since rew(P1,?← nat(X), id) is not an inductive success tree,P1 does notlogically entail the universally quantified formula∀X.nat(X). Similarly, since rew(P6,?←conn(X,Y), id) is not an inductive success tree,P6 does not logically entail∀X,Y.conn(X,Y).On the other hand, if we added a clauseconn(X,X) ← to P6, then, for resulting programP′6,rew(P′6,?← conn(X,X), id) would be an inductive success tree, and we would be able to infer thatP′6 does indeed logically entail∀X.conn(X,X).

Throughout this section, finiteness of inductive success subtrees (and thus of their correspondingrewriting reductions and S-derivations) has served as a precondition for our inductive soundnessand completeness results. In the next section we restore thebroken symmetry by defining coin-ductive proof methods that require observational productivity of S-derivations as a precondition ofcoinductive soundness.

4. COINDUCTIVE SOUNDNESS OF S-RESOLUTION

In this section, we show that S-resolution can capture not just inductive declarative and operationalsemantics of LP, but coinductive such semantics as well. We start by defining greatest completeHerbrand models of logic programs, following [Lloyd 1987] closely, then proceed by defining a no-tion of S-computations at infinity, and conclude with a soundness theorem relating the two. We taketime to compare the computational properties of SLD-computations at infinity and S-computationsat infinity, and prove that the latter extends the former. Since this section develops the theory of S-resolution for coinductive LP, observational productivity is a necessary precondition for establishingits results.

A first attempt to give an operational semantics corresponding to greatest complete Herbrandmodels of logic programs was captured by the notion of acomputation at infinityfor SLD-resolution[van Emden and Abdallah 1985; Lloyd 1987]. Computations at infinity are usually given relative toan ultrametric on terms, constructed as follows:

Definition 4.1. A truncationfor a signatureΣ is a mappingγ ′ : N×Termω(Σ)→ Term(Σ∪⋄),where⋄ is a new nullary symbol not inΣ, and, for allt ∈ Termω (Σ) and n ∈ N, the followingconditions hold:

— dom(γ ′(n, t)) = {m∈ dom(t) | |m| ≤ n},

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 21: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:21

— γ ′(n, t) = t(m) if |m|< n, and— γ ′(n, t) = ⋄ if |m|= n.

For t,s∈ Termω (Σ), we defineγ(s, t) = min{n | γ ′(n,s) 6= γ ′(n, t)}, so thatγ(s, t) is the least depthat whicht ands differ. If we further defined(s, t) = 0 if s= t andd(s, t) = 2−γ(s,t) otherwise, then(Termω (Σ),d) is an ultrametric space.

The definition of SLD-computable at infinity relative to a given ultrametric is taken directlyfrom [Lloyd 1987]:

Definition 4.2. An SLD-resolution reduction isfair if either it is finite, or it is infinite and, forevery atomB appearing in some goal in the SLD-derivation, (a further instantiated version of)B ischosen within a finite number of steps. The termt ∈GTerm∞(Σ) is SLD-computable at infinitywithrespect to a programP ∈ LP(Σ) if there exist at ′ ∈ Term(Σ) and an infinite fair SLD-resolutionreductionG0 = t ′,G1,G2, . . .Gk . . . with mgusθ1,θ2, . . .θk . . . such thatd(t,θk . . .θ1(t ′)) → 0 ask→ ∞. If such at ′ exists, we say thatt is SLD-computable at infinity byt ′.

The fairness requirement ensures that infinite SLD-resolution reductions that infinitely resolveagainst some subgoals while completely ignoring others do not satisfy the definition of SLD-computable at infinity. For example,from(0, [0, [s(0), [s(s(0)), . . .]]]) is not SLD-computable atinfinity by P5 because no computation that infinitely resolves with subgoals involving onlyfrom isfair.

In this section we see thatSLD-Computations at Infinity = Global Productivity + Control. Here,“global productivity” (as opposed to observational productivity) requires that each fair infinite SLD-resolution reduction for a program computes an infinite termat infinity. The “control” componentdetermines the proof search strategy for SLD-computationsat infinity to be constrained by fairness.We will see other variations on Kowalski’s formula below.

Letting P ∈ LP(Σ) and definingCP = {t ∈ GTerm∞(Σ) | t is SLD-computable at infinity withrespect toP by somet ′ ∈ Term(Σ)}, we have thatCP ⊆ Mω

P ([van Emden and Abdallah 1985;Lloyd 1987]).

4.1. S-Computations at Infinity

We can define a notion of computation at infinity for S-resolution to serve as an analogue of Def-inition 4.2 for SLD-resolution. As a method of “control” appropriate to S-resolution, we introducelight typing for signatures, similar to that in [Gupta et al.2007; Simon et al. 2007]. We introducetwo types — namely, inductive and coinductive — together with, for any signatureΣ, a typing func-tion Ty: Σ→ τ for Σ that marks each symbol inΣ as one or the other. We adopt the convention thatany symbol not explicitly marked as coinductive is taken to be marked as inductive by default. Wenote that in SLD-computations at infinity all symbols are implicitly marked as coinductive.

We extend the typing as inductive or coinductive from symbols to terms and to nodes of rewrit-ing trees. A termt ∈ Term(Σ) is inductive or coinductive according ast(ε) is. If P ∈ LP(Σ) andT ∈ Rewω (P), then an and-nodeT(w) is coinductive ifT(w)(ε) is coinductive, and is inductiveotherwise; an or-node inT(w) is coinductive or inductive according as its parent node is.A vari-able or-nodeT(w) = X is openif there exists a tree transitionT → TX, and isclosedotherwise. Avariable or-node iscoinductively openif it is open and coinductive. IfT ′ is a rewriting subtree ofT,thenT ′ is coinductively openif it contains coinductively open nodes, and isinductively closedif allof its open nodes are coinductive.

S-computations at infinity focus on observationally productive programs and rely on propertiesof lightly typed rewriting trees. We have:

Definition 4.3. Let P∈ LP(Σ) be observationally productive, letTybe a typing function forΣ,and lett ∈GTerm∞(Σ). We say thatt ′ ∈ Term(Σ) finitely approximates t with respect to P and Ty,or is afinite approximation of t with respect to P and Ty, if the following hold:

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 22: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:22

(1) There is an infinite S-derivationT0 = rew(P,?← t ′, id)→ T1→ . . .Tk → . . . with associatedresolventsθ1,θ2, . . .θk . . . such thatd(t,θk...θ1(t ′))→ 0 ask→ ∞

(2) This derivation contains infinitely many treesTi1,Ti2, . . . with an infinite sequence of corre-sponding rewriting subtreesT ′i1,T

′i2, . . . such that

i) eachT ′i jis inductively closed and coinductively open

ii) each inductive leaf in eachT ′i jis an inductive success node

iii) each coinductive variable node is open and, for each such nodeT ′i j(w) in eachT ′i j

, there exists

m> j such thatT ′im(wv) is coinductively open for somev

Then t is S-computable at infinity with respect to P and Tyif there is at ′ ∈ Term(Σ) such thatt ′ finitely approximatest with respect toP and Ty. We defineSTy

P = {t ∈ GTerm∞(Σ) | t is S-computable at infinity with respect toP andTy}.

Here we see thatS-Computations at Infinity = Global Productivity of S-Derivations + Control.The first condition in Definition 4.3 ensures “global productivity” and the second is concerned with“control”. But Definition 4.3’s requirement that programs are observationally productive is alsoused to control S-derivations via observations. We will seebelow that, as the “control” componentbecomes increasingly sophisticated, it can capture richercases of coinductive entailment than everbefore.

Example4.4. ConsiderP3 and letTy be the type function marking (only) the predicatefibsas coinductive. Ift ′ = fibs(0,s(0),X), thent ′ finitely approximates, with respect toP3 andTy,the infinite ground termt∗ from Example 1.5 representing the stream of Fibonacci numbers. Thust∗ is S-computable at infinity with respect toP3 andTy. Figure 2 shows an initial fragment of theS-derivation witnessing this. The infinite termt∗ is also SLD-computable at infinity with respect toP3.

However, S- and SLD-computability at infinity do not coincide:

Example4.5. Let P7 be the program comprising the clauses of Examples 1.2 and 1.5, togetherwith p(X)← fibs(0,s(0),X) andp(X)← nats(X), and letTy be the typing function that marksfibs andnats as coinductive. Ift ′ = p(X), thent ′ finitely approximates, with respect toP7 andTy,the infinite ground termp(cons(0,cons(s(0), . . .))) representing the stream of Fibonacci numbersand the infinite ground termp(scons(0,scons(s(0), . . .))) representing the stream of natural num-bers. Both of these infinite terms are thus S-computable at infinity with respect toP7 andTy. On theother hand, there is no fair SLD-resolution reduction starting with p(X) since (a further instantiationof) p(X) cannot be selected more than once. As a result, the notion of SLD-computable at infinitycannot even be formulated for derivations starting withp(X), and neither of the two infinite termsabove is SLD-computable at infinity with respect toP7.

Each of the “control” requirements i, ii, and iii in Definition 4.3 is crucial to the correct formula-tion of notion of a finite approximation for S-resolution, and thus to the notion of S-computability atinfinity. For Condition i, we note that some S-derivations expand inductive nodes infinitely, whichcan block the expansion of coinductive nodes. We do not want such S-derivations to be valid finiteapproximations. For example, we wantnats(scons(0,scons(0, . . .))) to be S-computable at infin-ity with respect toP2 if nats is marked coinductive andnat is marked inductive, but we do notwant nats(scons(s(s . . .)),Y)) to be so computable. Condition i ensures that only S-derivationsthat infinitely expand only coinductive nodes are valid finite approximations.

For Condition ii, we note that some S-derivations may have unsuccessful inductive subderiva-tions. We do not want these to be valid finite approximations.For example,P5 admits such deriva-tions. Condition ii ensures that only S-derivations with successful inductive subderivations are validfinite approximations.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 23: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:23

For Condition iii, we note that even within one rewriting subtree there may be several choicesof coinductive nodes to expand in a S-derivation. We want allsuch nodes to be infinitely ex-panded in a valid finite approximation. For example, ifP8 comprises the clauses ofP2 andP3 withfibs andnats marked coinductive, together withfibnats(X,Y) ← fibs(0,s(0),X),nats(Y),then S-derivations that infinitely expandfibs but only finitely expandnats compute at infin-ity terms of the formfibnats(cons(0,(cons(s(0), . . .))),scons(t1, t2)), for some finite termst1 andt2. Since these computations do not expose the coinductive nature ofnats, we do not wantthese to be valid finite approximations. But we do want S-derivations that compute terms of theform fibnats(cons(0,(cons(s(0), . . .)),scons( ,scons( , . . .))) to be valid finite approxima-tions. Condition iii ensures that only S-derivations infinitely expanding all coinductive nodes arevalid finite approximations.

4.2. Soundness of S-Computations at Infinity

We now investigate the relationship between SLD- and S-computations at infinity. The next twoexamples show that, for a givenP ∈ LP(Σ) and a typing functionTy for Σ, neitherCP ⊆ STy

P norSTy

P ⊆ CP need hold.

Example4.6. To see thatSTyP ⊆ CP need not hold, we note that Example 4.5 shows that

p(cons(0,cons(s(0), . . .))) andp(scons(0, scons(s(0), . . .))) are in STyP7

for Ty as there, butneither is inCP7.

Example4.7. To see thatCP ⊆ STyP need not hold, we first note that the infinite termt =

nat(s(s(. . .))) is SLD-computable at infinity with respect toP1 by nat(X), and is thus inCP1.But if Tymarksnat as inductive, thent 6∈ STy

P1. Similarly, in the mixed inductive-coinductive setting

we have thatt ′ = nats(scons(s(s(. . .)),scons(0,scons(s(0), . . .))) is SLD-computable at infin-ity with respect toP2 by nats(X), and is thus inCP2. But if Ty′ is the typing function that marks

only nats as coinductive then, sincenat is (implicitly) marked as inductive,t ′ 6∈ STy′

P2.

Although for any specific typing functionTy we need have neitherCP ⊆ STyP nor STy

P ⊆CP, con-sidering all typing functions simultaneously recovers a connection between SLD-computability atinfinity and S-computability at infinity.

Definition 4.8. If P ∈ LP(Σ) is observationally productive, thenSP =⋃{STy

P |Ty isa typing function forΣ}.

The rest of this section formalises the relationship between CP, SP, andMωP .

PROPOSITION 4.9. Let P∈ LP(Σ) be observationally productive. If t∈ Term∞(Σ) is SLD-computable at infinity by t′ ∈ Term(Σ) with respect to P, then there exists a typing function Ty forΣ such that t is S-computable at infinity by t′ with respect to P and Ty.

PROOF. We must show that, for anyt ∈ GTerm∞(Σ), if t is SLD-computable at infinity byt ′

with respect toP, then there is a typing functionTy for Σ such thatt is S-computable at infinity byt ′ with respect toP andTy. Sincet is SLD-computable at infinity, there exist at ′ ∈ Term(Σ) and aninfinite fair SLD-resolution reductionD of the formG0 = t ′→G1→G2 . . .→Gk→ . . . with mgusθ1,θ2, . . .θk . . . such thatd(t,θk . . .θ1(t ′))→ 0 ask→ ∞.

To show thatt is in SP, considert ′, let Ty be the typing function marking all symbols inΣ ascoinductive. We construct an infinite S-derivationD∗ by first observing that each SLD-resolutionreduction step inD proceeds either by matching or by unification. IfGi1, Gi2,.... is the sequence oflists in D out of which SLD-resolution reductions steps proceed by unification, then letD∗ be theinfinite S-derivationT0 = rew(P,?← t ′, id)→ T1→ . . .Tj → . . ., whereTj = rew(P,?← t ′,θi j ...θi1).We claim thatt is S-computable at infinity with respect toP andTyvia the infinite S-derivationD∗.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 24: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:24

The first condition of Definition 4.3 is satisfied becaused(t,θk . . .θ1(t ′))→ 0 ask→ ∞ by theproperties ofD, and thusd(t,θi j . . .θi1(t

′))→ 0 as j → ∞ by construction ofD∗. To see that thesecond condition of Definition 4.3 is satisfied, recall thatD is fair and infinite. SinceD is infiniteandTy does not permit inductive typing,D∗ contains (inductively closed and) coinductively openrewriting trees infinitely often. As a result,D∗ satisfies i. SinceTydoes not permit inductive typing,D∗ satisfies ii trivially. AndD∗ satisfies iii becauseD is both fair and infinite.

We have the following immediate corollary:

COROLLARY 4.10. If P ∈ LP(Σ) is observationally productive, then CP ⊆ SP.

Although Corollary 4.10 shows that, for an observationallyproductive programP,CP⊆ SP holds,Example 4.12 shows that, even for such programs,CP is, in general, apropersubset ofSP.

THEOREM 4.11. (Coinductive soundness of S-computations at infinity) If P∈ LP(Σ) is obser-vationally productive, thenSP⊆Mω

P .

PROOF. SinceCP ⊆ MωP , we need only show that ift ∈ SP \CP, thent ∈ Mω

P . If t ∈ SP \CP,thent ∈ GTerm∞(Σ) andt is S-computable at infinity but not SLD-computable at infinity. Thus,SP\CP contains only those terms that are S-computable at infinity but fail to have fair infinite SLD-resolution reductions. Thus, every SLD-resolution reduction computingt at infinity must containsome atomB no further instantiated version of which is selected withinfinitely many steps.

Without loss of generality we may assume the infinite SLD-resolution reductionD = G0,G1, . . .corresponding to the S-computation oft at infinity is such thatG0 = B. We complete the proofby coinduction on the subreduction of this SLD-resolution reduction starting withG1, making thecoinductive hypothesis that, for all termst ′ occurring inG1, . . ., we have thatt ′ ∈ SP\CP impliest ′ ∈Mω

P . Then sincet is S-computable at infinity byB, there must be an infinite SLD-resolution reductionfor B with associated resolvents. . .θk . . .θ1 such thatt = θ ∗(B) for the infinite compositionθ ∗ =. . .θk . . .θ1. Moreover,B must unify withhead(P(i)) for some clauseP(i) in P. (If B unifies withhead(P( j)) for other j, too, then all are handled in the same way.) Again without loss of generality,we may assume that, in fact,P(i) has the formB← t1, . . . , tn, with no instances ofB amongt1, . . . , tn.Moreover, at least oneti must admit an infinite SLD-resolution reduction.

If B does not share any variables with anyti admitting an infinite SLD-resolution reduction, thenno infinite instance ofB can be S-computable or SLD-computable at infinity with respect toP. Thus,B must share variables with at least one suchti . Consider the SLD-resolution subreduction of theinfinite SLD-resolution reduction forB for eachti .

If the SLD-resolution subreduction forti is a finite successful SLD-resolution reduction, thenθ ∗(ti) ∈Mω

P by construction ofMωP as the backward closure of the big-step rule forP. On the other

hand, if the SLD-resolution subreduction forti is infinite, then eitherθ ∗(ti) is SLD-computable atinfinity, in which caseθ ∗(ti) ∈CP ⊆Mω

P , or θ ∗(ti) ∈ SP\CP, in which case we use the coinductivehypothesis to conclude thatθ ∗(ti) ∈Mω

P . In either case, eachθ ∗(ti) ∈MωP so by construction ofMω

Pwe must also havet = θ ∗(B) ∈Mω

P .

Theorem 4.11 shows that, for observationally productive programs, S-computations at infinityare sound with respect to greatest complete Herbrand models. Together with Corollary 4.10 andthe fact thatCP is a proper subset ofSP, it shows that S-computations at infinity give a moreprecise operational semantics to infinite computations than do SLD-computations at infinity. Thecorresponding completeness result — namely, thatMω

P ⊆ SP — does not hold, even ifP is ob-servationally productive. The problem arises whenP does not admit any infinite S-resolution re-ductions. For example, ifP7 is the program with the single clauseanySuccessor(s(X))← , thenanySuccessor(s(s(. . .))) ∈ Mω

P7. But P7 admits no infinite S-derivations, so no (infinite) terms

are S-computable at infinity with respect toP7 and Ty for any typing functionTy. A similar

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 25: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:25

problem arises whenP fails the occurs check. For example, ifP8 comprises the single clausep(X,f(X)) ← p(X,X), with p marked as coinductive, thenp(f(f(. . .)),f(f(. . .))) is in Mω

P8but is not

S-computable at infinity with respect toP8 andTy. This case is subtly different from the first one,sinceP8 defines an infinite termX= f(X) only if unification without the occurs check is permitted.

Theorem 4.11 ensures that (finite) coinductive terms logically entail the infinite terms they finitelyapproximate. But there may, in general, be programs for which coinductive terms also logicallyentail other finite terms.

Example4.12. Consider the programP9 comprising the clause ofP4 and the clause

1. p(Y)← from(0,X)

and supposeTy types onlyfrom as coinductive. Althoughfrom(0,X) finitely approximates an in-finite term with respect toP9 andTy, no infinite instance ofp(Y) is S-computable at infinity withrespect toP9 andTy. Nevertheless,p(0) and other instances ofp(Y) are logically entailed byP9 andthus inMω

P9.

The following definition takes such situations into account:

Definition 4.13. Let P ∈ LP(Σ) be observationally productive, letTy be a typing function forΣ, and lett ∈ Term(Σ). Thent is implied at infinity with respect to P and Tyif there exist termst1, . . . , tn ∈ GTerm∞(Σ), each of which is S-computable at infinity with respect toP andTy, andthere exists a sequence of rewriting reductionst → . . .→ [t ′1, . . . , t

′n] such that, for eachti , θ (t ′i ) = ti

for someθ ∈ Substω (Σ). We defineSITyP = {t ∈ GTermω(Σ) | t is S-computable at infinity or S-

implied at infinity with respect toP andTy}.

Example4.14. Consider once again the termp(Y) from Example 4.12 Althoughp(Y) is notcomputableat infinity with respect toP9 andTy as in Example 4.12, it is indeedimpliedat infinitywith respect toP9 andTy.

Defining SIP =⋃{SITy

P |Ty is a typing function forΣ} gives the following corollary of Theo-rem 4.11:

COROLLARY 4.15. If P ∈ LP(Σ) is observationally productive, thenSIP⊆MωP .

Although sound, neither S- nor SLD-computations at infinityprovide a finite, implementablesemi-decision procedure for coinductive logical entailment. Intuitively, this is because computa-tions at infinity are defined in terms of global productivity.The development of a coinductive proofprinciple that, for observationally productive programs,is both sound and complete relative to S-computations at infinity is the main contribution of Section5.

5. A COINDUCTIVE PROOF PRINCIPLE FOR S-RESOLUTION

We have seen that observational productivity can improve the control component in our variant

S-Computations at Infinity = Global Productivity of S-Derivations + Control (*)

of the famous 1980s formula. However, our goal now is to replace (*) with the observational, andthus more structural and constructive, alternative:

Coinductive Proof = Observational Productivity + Control (**)

A new coinductive proof principle for LP, incorporating both the sound formation and application ofcoinductive hypotheses, will be the key to realising this formula. We will also show in this sectionthat, under the hypothesis of observational productivity,(*) and (**) are equally expressive, in thesense that coinductive proofs correspond precisely to S-computations at infinity.

We take observational productivity of programs as the defining feature of our new approach.Observational productivity is semi-decidable [Johann et al. 2015b], and we can observe finite frag-

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 26: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:26

ments of S-derivations, so we only need to formulate criteria that allow to make coinductive conclu-sions on the basis of such observations. For example, in the derivation fragment forP3 in Figures 2and 4, we must be able to tell which rewriting tree observation is “good enough” to conclude thecoinductive proof thatP3 logically entailsfibs(0,s(0),X), for someX. For this, we need to intro-duce a suitable coinductive proof principle for S-derivations.

5.1. Coinductive Hypothesis Formation and Application

To define the notion of a coinductive hypothesis for an S-derivation we look more closely at thebig-step and small-step semantics of LP given in Section 2. For any observationally productiveprogramP ∈ LP(Σ), term t ∈ Term(Σ), and subtreeT ′ of T = rew(P,?← t,σ), T ′ correspondsto a terminating rewriting reductiont →µ [t1, . . . , tn], wheret1, . . . tn label the parent nodes of theopen nodes ofT ′. Taking the inductive interpretation of the big step rule for P, T ′ may be read assaying that, for anyθ ∈ Subst(Σ), if θ (ti) ∈ MP for i = 1, ...,n, thenθ (t) ∈ MP. Similarly, takingthe coinductive interpretation of the big step rule forP, T ′ may be read as saying that, if there existsa θ ∈ Subst(Σ) such thatθ (t) ∈ Mω

P , then each ofθ (ti) ∈ MωP for i = 1, ...,n. We will use these

observations to find a suitableθ and form the coinductive hypothesesθ (ti)← θ (t) for eachti in T ′.However, as discussed below, we will only want to form these coinductive hypotheses for thosetiswhose heads are coinductive predicates.

Definition 5.1. Let P ∈ LP(Σ) be observationally productive,Ty be a typing function forΣ,t ∈Term(Σ), σ ∈ Subst(Σ), andT ′ be a rewriting subtree of the rewriting treeT = rew(P,?← t,σ).If T ′(wi) is a coinductively open node for somewi ∈ dom(T), then the clauseT(w)← T(0) is acoinductive hypothesisthat isformed with respect to Tandinduced by T(wi).

Example5.2. For the logic programP3 and the typing functionTy marking onlyfibs as coin-ductive, the clauses

C1 : fibs(s(0),Z,S)← fibs(0,s(0),cons(0,S))

and

C2 : fibs(s(0),s(0),S)← fibs(0,s(0),cons(0,S))

are coinductive hypotheses induced byT1(0212) andT2(0212), whereT1 andT2 are the second andthird rewriting trees of Figure 2, respectively.

Once we form a coinductive hypothesis we can ask how we can apply it in an S-derivation for anobservationally productive logic programP. The next definition answers this question.

Definition 5.3. Let T0 = rew(P,?← t, id) → . . . → Ti → . . . → Tj → . . . → Tn be an S-derivation, and letCH be a coinductive hypothesis induced byTi(wk) for some rewriting sub-tree of Ti = rew(P,?← t,θ ). ThenCH can be applied at Tj(w) if Tj = rew(P,?← t,θ ′θ ) andhead(CH)≺θ ′ Tj(w).

By Theorem 3.4 we know thatTj is necessarily of the formrew(P,?← t,θ ′θ ) for someθ ′ ∈Subst(Σ). The content of Definition 5.3 thus lies in the requirement that head(CH) ≺θ ′ Tj(w) viathe specific mgmθ ′.

Example5.4. The coinductive hypothesisC2 formed as in Example 5.2 can be applied atT5(021), whereT5 is the third rewriting tree of Figure 4.

It is not hard to see that, of the two coinductive hypothesesC1 andC2, onlyC2 is sound with re-spect toMω

P3. Indeed,C1 does not hold for allZ under the coinductive interpretation ofP3, but rather

only for s(0). What remains now is to formulate conditions guaranteeing that the coinductive hy-potheses formed as in Definition 5.1 are actually sound with respect to greatest complete Herbrandmodels, and under which they can be applied to construct valid coinductive proofs.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 27: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:27

...→X9

?← fibs(0,s(0), [0, [s(0),S′]])

fibs(0,s(0), [0, [s(0),S′]])

X1 X2 fibs(0,s(0), [0, [s(0),S′]])← add(0,s(0),s(0)),fibs(s(0),s(0), [s(0),S′])

add(0,s(0),s(0))

add(0,s(0),s(0))← X5 X6

fibs(s(0),s(0), [s(0),S′])

X7 X8 fibs(s(0),s(0), [s(0),S′])← add(s(0),s(0),Z′),fibs(s(0),Z′,S′)

add(s(0),s(0),Z′)

X10 X11 X12

fibs(s(0),Z′,S′)

X13 X14 X15

→X11

?← fibs(0,s(0), [0, [s(0),S′]])

fibs(0,s(0), [0, [s(0),S′]])

X1 X2 fibs(0,s(0), [0, [s(0),S′]])← add(0,s(0),s(0)),fibs(s(0),s(0), [s(0),S′])

add(0,s(0),s(0))

add(0,s(0),s(0))← X5 X6

fibs(s(0),s(0), [s(0),S′])

X7 X8 fibs(s(0),s(0), [s(0),S′])← add(s(0),s(0),s(Z′′)),fibs(s(0),s(Z′′),S′)

add(s(0),s(0),s(Z′′))

X10 add(s(0),s(0),s(Z′′))← add(0,s(0),Z′′)

add(0,s(0),Z′′)

X16 X17 X18

X12

fibs(s(0),s(Z′′),S′)

X13 X14 X15

→X16

?← fibs(0,s(0), [0, [s(0),S′]])

fibs(0,s(0), [0, [s(0),S′]])

X1 X2 fibs(0,s(0), [0, [s(0),S′]])← add(0,s(0),s(0)),fibs(s(0),s(0), [s(0),S′])

add(0,s(0),s(0))

add(0,s(0),s(0))← X5 X6

fibs(s(0),s(0), [s(0),S′])

X7 X8 fibs(s(0),s(0), [s(0),S′])← add(s(0),s(0),s(s(0))),fibs(s(0),s(Z′′),S′)

add(s(0),s(0),s(s(0)))

X10 add(s(0),s(0),s(s(0)))← add(0,s(0),s(0))

add(0,s(0),s(0))

add(0,s(0),s(0))← X17 X18

X12

fibs(s(0),s(s(0)),S′)

X13 X14 X15

→X15 ...

Fig. 4. Three more tree transitions, continuing the S-derivationof Figure 2. All loops in this S-derivation are underlined.Continuing this S-derivation yields a coinductiv proof forfibs(0,s(0),X), as discussed in Example 5.16.

It is a genuinely hard problem to both formulate and appropriately apply sound coinductive hy-potheses in the general case. Interactive theorem provers like Coq and Agda, for example, leave theformation of coinductive hypotheses to the user. Our demonstration in this section that S-resolutiongives rise to a general method for formulating and applying sound coinductive hypotheses, and thusto constructing valid coinductive proofs, in LP is thus a major contribution of this paper.

We end this subsection by observing two major reasons why a coinductive hypothesis may fail tobe sound with respect to greatest complete Herbrand models:

a)The coinductive hypothesis may be formed too early.In Example 5.2, for instance, theunsoundcoinductive hypothesisC1 is formed for a rewriting tree in which the inductive part of the proof

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 28: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:28

(i.e., the proof foradd(0,s(0),Z)) is not yet complete, and thus beforeZ is properly instantiated.By contrast, thesoundcoinductive hypothesisC2, which is formed for a later rewriting tree, isthe instantiation ofC1 under the substitution{Z 7→ s(0)} used to complete the inductive part ofthe proof.

We will solve the problem of premature formation of coinductive hypotheses by giving precisecriteria for constructing suitable substitutionsθ to be used in forming sound coinductive hypothe-ses, as discussed at the start of thus subsection. Such substititions are effectively constructed whenexternal resolvents are computed in S-derivations. What weneed, therefore, are criteria for iden-tifying which rewriting trees in an S-derivation coinductive hypotheses should be formed withrespect to. For Example 5.2, the problem of the premature formation ofC1 would be solved ifwe formed coinductive hypotheses only with respect to rewriting trees that coinductively observesuccess. We will formulate more general criteria in Definition 5.11 below.

b) The program may be “wrongly” typed.

Example5.5. Consider the following logic programP8:

0. p(X)← q(X)1. q(0)←2. p(f(X))← p(X)

If Ty is the typing function that marks onlyq as coinductive, thenTydoes not reflect the inductivenature ofq and the coinductive nature ofp, as reflected inP8’s clauses. Indeed,Ty “erroneously”marksq as coinductive andp as inductive. Nevertheless, we have the following rewriting treeTfor P8 andp(X):

?← p(X)

p(X)

p(X)← q(X)

q(X)

X3 X4 X5

X1 X2

Ty permits the formation of coinductive hypotheses whose heads are terms with rootq, so wecan form the coinductive hypothesisq(X)← p(X) induced byT(000). Note, however, that thiscoinductive hypothesis is not sound with respect to the greatest complete Herbrand model forP8. For example,q(X)← p(X) does not hold under the coinductive interpretation ofP8 whenX isf(Y).

The next section introduces sufficient “control” to restrict the “logic” of coinductive hypothesisformation in such a way that the desired equivalence of (*) and (**) can be recovered.

5.2. Sound Formation and Application of Coinductive Hypotheses

We first address the issue of the incorrect coinductive typing, since it relates to the problem of searchin S-derivations as well. Intuitively, we need to check that, if a coinductive hypothesis for a programP is induced by a nodeT(w) in a rewriting treeT, thenP’s structure does indeed imply the recursivenature of the termT(w).

To this end we observe that:

1) Any term occurring in any rewriting tree for a program (other than a trivial rewriting tree withonly one root node) is inevitably an instantiated form of some term occurring in some clause inthat program. This follows from the properties of matching.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 29: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:29

2) If the program is observationally productive and recursive then each of its rewriting trees musthave at least one term containing a pattern that reduces fromthe root of the rewriting tree to itsleaves, and thus terminates the rewriting reduction.

3) By the properties of matching, the most general instance of such a pattern must be given by asubterm in some clause head.

The invariant coinductive property we are looking for regarding the coinductive nodes must some-how relate the three observations above.

In the running example (Example 1.6) of the Introduction, the second and the third rewritingtrees in the derivation forP4 andfrom(0,X) contain pairs of terms that exhibit a reducing pattern[ ,Z] 7→ Z from their roots to their leaves. These pairs ensure the finiteness of the rewriting trees.However, only the third treeT2 allows us to verify that this pattern remains invariant after tworewriting steps using the same clause. Note that the patternmatches with the subterm[X,Y] — i.e.,scons(X,Y) — in the head ofP4’s clause.

We now must check that exactly this same clause subterm[X,Y] is involved in the unificationstep determining the tree transition out of each rewriting tree. That is, we must check that whenever[ , ] is consumedby rewriting steps from the root to the leaves of the rewriting tree, exactly[ , ] isproduced(added back) in the next tree transition. Since it is exactlythe subterm[X,Y] of P4’s clausethat determines the consumer-producer property describedabove, we have found the invariant prop-erty confirming the coinductive nature offrom. We may therefore form the coinductive hypothesisfrom(s(s(0)),X)← from(0, [0, [s(0),X]]) induced byT2(00).

To apply this coinductive hypothesis soundly, we must find another rewriting tree further in theS-derivation satisfying the same property. The fourth treeT3 in the continuation of the S-derivationin Example 1.6, shown below, has exactly the same invariant pattern of consuming[ , ] from its rootto its leaves and producing[ , ] via the next tree transition:

...{X′′ 7→[s(s(0)),X′′′]}

→from(0, [0, [s(0), [s(s(0)),X′′′ ]]])

from(s(0), [s(0), [s(s(0)),X′′′ ]])

from(s(s(0)), [s(s(0)),X′′′ ])

from(s(s(s(0))),X′′′)

{X′′′ 7→[s(s(s(0))),X′′′′]}→ ...

We thus apply the coinductive hypothesis atT3(00), which allows us to terminate the S-derivationand say that the fragment leading toT3 (together with the termfrom(0, [0, [s(0), [s(s(0)),X′′′]]]))gives a coinductive proof of the fact thatP4 logically entailsfrom(0,X) for someX. In fact, we cansay more about thisX: it must have the form[0, [s(0), [s(s(0)),X′′′]]], and there must exist an infiniteterm inMω

P4that is an instance offrom(0, [0, [s(0), [s(s(0)),X′′′]]]). This is the essence of our main

observational soundnessresult, Corollary 5.18, below. (From the discussion in Example 1.6, we canfurther say that the infinite term in question ist ′ = from(0, [0, [s(0), [s(s(0)), . . .]]]).)

To formalise the above intuition about theconsumeraspect of coinductive proofs we need asuitable ordering on terms. We have:

Definition 5.6. If t1, t2 ∈ Term(Σ), thent2 is acontractionof t1 if there existsw∈ dom(t2) suchthat the following hold:

1) t2(w) is a variable or a constant2) For all proper prefixesv of w, t1(v) = t2(v)3) arity(t1(w))> 0

If the following also holds, thent2 is arecursive contractionof t1:

4) subterm(t1,w) containst2(w), i.e.,subterm(t1,w)(w′) = t2(w) for somew′,

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 30: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:30

We write t1 ⊲w t2 if t2 is a contraction oft1 at nodew, and simplyt1 ⊲ t2 whenw is clear fromcontext. When conditions 1, 2, and 3 of Definition 5.6 hold we distinguishvariable contractionsandconstant contractionsaccording ast2(w) is a variable or constant, and callsubterm(t1,w) acontraction measurefor t1 ⊲w t2 at nodew. We callsubterm(t1,w) a recursive contraction measurefor t1 ⊲w t2 atw if condition 4 holds, too.

We can now check whether contraction measures are consumed from the root to the leaves ofbranches of a rewriting tree, as well as whether they are consumed by matching with the sameclause. The following notion of a guarded loop in a rewritingtree does this.

Definition 5.7. If P∈ LP(Σ) andC∈Clause(Σ), andσ ∈ Subst(Σ), thenT = rew(P,C,σ) hasthe loop (T(w),T(v)) if there existw,v∈ dom(T) such that the following hold:

1) w is a proper prefix ofv2) T(w) andT(v) are and-nodes withT(w)(ε) = T(v)(ε)3) If w′ andv′ are parent nodes ofw andv respectively, thenT(w′) = θ (P(i)) andT(v′) = θ ′(P(i))

for someP(i) ∈ P and mgmsθ andθ ′

If T has a loop(T(w),T(v)), whereT(w) ⊲ T(v) with recursive contraction measuret ′′, then(T(w),T(v)) is guardedby (P(i), t ′′). The loop isunguardedotherwise.

Example5.8. The guarded loop(fibs(s(0),s(0),cons(s(0),S′)), fibs(s(0),Z′,S′) is under-lined in the first rewriting tree of Figure 4. This loop is guarded by(P(2),cons(s(0),S′)).

We have now shown how to formalise and implement the search for a term pattern consumedwithin a loop of a rewriting tree. Formalising our intuitions about theproduceraspect of coinduc-tive proofs, i.e., defining what it means for a pattern to be produced in a rewriting tree transition,is slightly more involved. Indeed, the formal definition of pattern production is not trivial since, forprograms likeP3 andP4 that compute irrational terms at infinity, the patterns produced by externalresolvents will be irregular at every step. Thus, in order toautomatically find invariants in a com-putation, we must find a way to abstract over the most general instances of such patterns. To dothis, we observe that any pattern produced by an external resolvent must match a subterm of theclause head that determined this external resolvent, and capture this observation using the notion ofa clause projectionfor a tree transition, as given in Definition 5.9. If the pattern captured by clauseprojection also happens to be a pattern that has been consumed by a preceding rewriting tree, thenthis pattern is part of acoinductive invariant, also defined in Definition 5.9, for this transition.

Definition 5.9. Let P∈ LP(Σ), t ∈ Term(Σ), T = rew(P,?← t,θ ), andTX = rew(P,?← t ′,σθ )with associated tree transitionT → TX for a variableX in T, induced by an external resolventσ ∈ Subst(Σ) for someP(k) ∈ P and somet labelling an and-node ofT. Theclause projectionforthe transitionT→ TX , denoted byπ(TX), is the set of triples{(P( j), t∗,v) | there existst ′ ∈ T(w) forsomew∈ dom(T) such thatT(w j) ∈VR but TX(w j) /∈VR, t ′ is a variable contraction ofσ(t ′) withcontraction measuret ′′, and there exists a non-variable proper subtermt∗ of head(P( j)) at nodevsuch thatt ′′ is an instance oft∗}. When we wish to distinguish between various elements ofπ(TX),we will say that the triple(P( j), t∗,v) ∈ π(TX) is formed on T(w j). Thecoinductive invariantatTX ,denotedci(TX), is {(P( j), t∗,v) ∈ π(TX) | (P( j), t∗,v) is formed onT(w j), and there is a loop in thebranch ofT from T(ε) to T(w j) that is guarded by(P( j), t∗∗) and such thatt∗∗ is an instance oft∗}.

Example5.10. If T andT ′ are the first and third rewriting trees in Figure 4, then haveπ(T ′X15) =

ci(T ′X15) = {(P3(2),cons(X,S),2)}. On the other hand,π(TX11) = {(P3(1),s(Z),2)}. (Note that

(P3(2),cons(X,S),2) 6∈ π(TX11) sinceTX11(02121i) ∈VR for all i.) Moreover,ci(TX11) = /0 because,althoughT contains the guarded loop(fibs(s(0),s(0),cons(s(0),S′)),fibs(s(0),Z′,S′)), thisloop is not guarded by a pair whose first component is the clause involved in the tree transitionT→ TX11, as is required by Definition 5.9.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 31: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:31

Finally, we can address the issue of premature formation of coinductive hypotheses by incorporat-ing a requirement that forces them to be sufficiently instantiated. This is the last step in formulatingour new coinductive principle for S-resolution.

Definition 5.11. Let P∈ LP(Σ) be observationally productive, lett ∈ Term(Σ), and letTy bea typing function forΣ. If T0 = rew(P,?← t, id)→ T1→ . . .→ Tn is an S-derivation, we say thatthe coinductive hypothesisTi(w)← Ti(0) induced byTi(w j) = X ∈VR is well-formedin a rewritingsubtreeT ′i of Ti if each leaf ofT ′i is either coinductively open or an inductive success node,Ti(w j) isalso a node ofT ′i , andci((Ti)X) 6= /0. We say that a well-formed coinductive hypothesisTi(w)←Ti(0)is formed onci((Ti)X).

Example5.12. The coinductive hypothesisq(X)← p(X) is not well-formed for any S-derivationinvolving the rewriting treeT depicted in Example 5.5 becauseci(TXi ) = /0 for i = 1, ...,5.

Example5.13. For the S-derivation depicted in Figures 2 and 4, only the coinductive hypothesis

C3 : fibs(s(0),s(s(0)),S′)← fibs(0,s(0), [0, [s(0),S′]])

induced byT5(021212) for the third treeT5 of Figure 4 is a well-formed coinductive hypothesis.Although the other trees in both figures give rise to coinductive hypotheses, these are not well-formed. In particular, the coinductive hypothesisC1 of Example 5.2 is formed with respect to arewriting subtree containing inductive leaves that are notinductive success nodes. On the otherhand, the coinductive hypothesisC2 of Example 5.2 is indeed formed with respect to a rewritingsubtree all of whose inductive leaves are inductive successnodes, but the coinductive invariant forthe nodeT2(0212), with respect to whichC2 is formed, is empty. Thus, althoughC2 is a validcoinductive hypothesis, it is not well-formed in the sense of Definition 5.11. Indeed, the first well-formed coinductive hypothesis for the S-derivation of Figures 2 and 4 isC3.

We are aware that our definitions of a coinductive hypothesisand a well-formed coinduc-tive hypothesis may result in the formation of hypotheses that may not look intuitive. Con-sider, for example, programP9 from Example 4.12. Construction of coinductive hypothesesfor the S-derivation starting withT0 = rew(P9,?← p(Y), id) proceeds very much as for thederivation starting withrew(P4,?← from(0,X), id) discussed at the start of this subsection.However, the coinductive hypothesis constructed is nowfrom(s(s(0)),X)← p(Y) rather thanfrom(s(s(0)),X)← from(0, [0, [s(0),X]]). Intuitively, this is justified by the following rewritingreduction:

p(Y)

from(0, [0, [s(0),X′′ ]])

from(s(0), [s(0),X′′ ])

from(s(s(0)),X′′)

Formally, however, the second condition of Definitions 5.11entails that a coinductive hypothesisis well-formed only when there is a guarded loop on the branchof a tree containing the node onwhich the relevant coinductive invariant is formed. To verify that the above coinductive hypothe-sis is well-formed we therefore observe that the loop(from(0, [0, [s(0),X]]),from(s(s(0)),X)) isappropriately present in the tree depicted above.

It is, of course, possible to change Definition 5.11 to require a well-formed coinductive hy-pothesis to have the forml2← l1, where(l1, l2) is the loop involved in the definition of the cor-responding coinductive invariant. In particular, we coulddefine the well-formed coinductive hy-pothesis determined by the above rewriting tree to befrom(s(s(0)),X)← from(0, [0, [s(0),X]]),rather thanfrom(s(s(0)),X)← p(Y). Although we refrain from doing this in order to preservethe greater generality of our method, we do note that exactlythe same loop justifies well-formed

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 32: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:32

coinductive hypotheses that are related in this way. For example, the coinductive hypothesesfrom(s(s(0)),X)← p(Y) andfrom(s(s(0)),X)← from(0, [0, [s(0),X]]) are both justified by theloop (from(0, [0, [s(0),X′′]]),from(s(s(0)),X′′)).

Applications of coinductive hypotheses needs to be guarded. In particular, a coinductive hypoth-esis may not be applied on a node in the same rewriting tree with respect to which it was formed. Inother words, the application of a coinductive hypothesis must be guarded by a tree transition.

Definition 5.14. Let P ∈ LP(Σ) is an observationally productive logic program,t ∈ Term(Σ)and letTy be a typing function forΣ. Let T0 = rew(P,?← t, id), andT0→ T1→ . . .→ Ti → . . .→Tj , with Ti strictly precedingTj , be an S-derivation such thatC : Ti(w)← Ti(0) is a well-formedcoinductive hypothesis induced byTi(wk) =Y and formed onci((Ti)Y). ThenC is soundly appliedatTj(w) if it can be applied atTj(w), whereTj(w) belongs to a rewriting subtreeT ′j of Tj such that:

— each leaf ofT ′j is either coinductively open or an inductive success node— for some coinductive nodeX = T ′j (wv), ci((Tj)X) 6= /0— ci((Tj)X) = ci((Ti)Y)

When the above conditions hold, we say thatC is soundly applied atTj(w) to close Tj(wv), and thatTj(wv) is acoinductive success nodeof Tj with respect toP andTy.

If a given rewriting tree constructed within an S-derivation has more than one coinductive leaf,then each leaf needs to be closed by some coinductive hypothesis in the course of that S-derivationin order to have a valid coinductive proof. In an implementation this will, of course, affect proofsearch strategies. Above we have motivated giving priorityto resolvents against inductive nodesin rewriting trees. In practice we may additionally elect not to form resolvents against coinductivesuccess nodes, and to terminate branches of rewriting treesat such nodes instead.

5.3. Soundness of Coinductive Proofs

If T ′ is a rewriting subtree ofT = rew(P,?← t,σ), then we can distinguish two cases: 1) all leafnodes ofT ′ are inductive success nodes ofT with respect toP andTy, in which caseT ′ is a successsubtree, and 2)T ′ contains at least one coinductively open or-node, all coinductively open or-nodesare coinductive success nodes with respect toP andTy, and all inductive leaves are inductive successnodes with respect toP andTy. If 2) holds, then we say thatT observes a coinductive proof for twith respect to P and Ty.

Definition 5.15. Let P∈ LP(Σ) be observationally productive, letTy be a typing function forΣ, and lett ∈ Term(Σ). An S-derivationD: T0 = rew(P,?← t, id)→ . . .→ Tn is acoinductive proofof t if there exist coinductive hypothesesC1, . . . ,Ck such that

— there exists a rewriting treeTi that strictly precedesTn in D andC1, . . . ,Ck are well-formed insome rewriting subtreeT ′i of Ti .

— Tn observes a coinductive proof fort with respect toP andTy and all its coinductive successnodes are closed byC1, . . . ,Ck.

In this case, we say thatD is a coinductive proof fort by coinductive hypotheses C1, . . . ,Ck.

Example5.16. Let P3 andTy be as in Example 4.4. Figure 2 shows an initial fragment of anS-derivation forP3 andfibs(0,s(0),X). It is not yet a coinductive proof, but if it is continued as inFigure 4 and if the final tree shown is denotedT ′, then the tree transitionT ′ → T ′X15

produces thecoinductive invariantci(T ′X15

) of Example 5.10. If this S-derivation is continued, giving priority toresolvents involving inductive leaves, then it reaches a treeT ′′Y whose coinductive invariant is equalto ci(T ′X15

). This gives a coinductive proof forfibs(0,s(0),X).

In Figures 2 and 4, expanding inductive nodes is given priority over expanding coinductive nodes.This is a consequence of the definition of a correct coinductive proof, which requires that inductive

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 33: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:33

fragments of proofs are finished before forming a coinductive hypothesis or conclusion. This leadsto a sophisticated interleaving of inductive and coinductive computations in S-derivations.

For “correct” typings of coinductive predicates, S-derivations compute as expected. But “incor-rect” typings are more subtle. Incorrectly typing an intuitively inductive predicate as coinductiveallows us to view its computations coinductively. For example, if nat in P1 is marked coinductive,then there is a coinductive proof fornat(X) that finitely approximatesnat(s(s...)) with respect toprogramP1. By contrast, incorrectly typing an intuitively coinductive predicate as inductive maylead to inconclusive computations. For example, ifnats is marked inductive inP2, then queriesinvolving nats have neither inductive nor coinductive proofs.

Theorem 5.17 establishes our main result: for observationally productive programs, coinductiveproofs are sound and complete relative to S-computations atinfinity.

THEOREM 5.17. Let P∈ LP(Σ) be observationally productive, let Ty be a typing function forΣ, and let t∈ Term(Σ). Then there exists a t∗ ∈GTermω (Σ) that is either S-computable at infinityor implied at infinity with respect to P and Ty by t iff there exists a coinductive proof for t withrespect to P and Ty.

PROOF. Let P and Ty be as in the theorem. For the forward direction, first supposet∗ ∈GTerm∞(Σ) is S-computable at infinity byt with respect toP andTy. Then there exists an infi-nite S-derivationD starting withT = rew(P,?← t, id) that contains, infinitely often, rewriting treescontaining rewriting subtrees satisfying conditions i, ii, and iii of Definition 4.3. Since, as shownin [Johann et al. 2015b], the set of clause projections for any S-derivation is finite,D will eventu-ally reach a rewriting treeT ′ containing a rewriting subtreeT ′′ with, say, exactlyn coinductivelyopen nodes, and these nodes induce coinductive hypothesesC1, . . . ,Cn that are well-formed inTiand soundly applied at nodesT∗(wi) of some rewriting treeT∗ such thatT ′ strictly precedesT∗.But then the finite derivationT→ ...→ T∗ is a coinductive proof fort∗ with respect toP andTy. Ift∗ ∈GTerm(Σ) is implied at infinity, rather than S-computable at infinity,with respect toP andTy,then there must existt1, . . . , tn ∈GTerm∞(Σ) that are S-computable at infinity with respect toP andTy and are such thatt∗→ ...→ [t ′1, ..., t

′n] for somet ′1, ..., t

′n ∈ Term(Σ) andθ ∈ Subst(Σ) such that

θ (t ′i ) = ti for i = 1, ...,n. We proceed with the proofs for each oft1, . . . , tn as above, and constructthe desired coinductive proof fort∗ from those fort1, . . . , tn.

The proof in the other direction is by coinduction. SupposeD is the derivationT0 = rew(P,?←t, id)→ . . .→ T∗, and thatD is a coinductive proof fort with respect toP andTy by coinductivehypothesesC1, . . . ,Cn. Then by Definition 5.15, there must exist a treeT ′ that strictly precedesT∗

in D and has a rewriting subtreeT ′′ each leaf of which is either coinductively open or an inductivesuccess node, and in whichC1, . . . ,Cn are well-formed. Moreover,T∗ observes a coinductive proofwith respect toP and Ty, and all ofT∗’s coinductive success nodes are closed byC1, ...,Cn. Inparticular, each ofC1, ...,Cn is soundly applied at some nodewi of T∗. By observingT ′, we form thecoinductive hypothesis that there exists an infinite subderivationD′ of D continuingT0→ ...→ T ′

in whichC1, . . . ,Cn are soundly applied again. SinceC1, . . . ,Cn are soundly applied inT∗, we usethe coinductive hypothesis and close the proof by coinduction, asserting that a subderivationD′ ofD continuingT0→ ...→ T∗ exists in whichC1, . . . ,Cn are soundly applied infinitely often. But sucha subderivation shows thatt finitely approximates the termt∗ = θ ∗(t) ∈ GTerm∞(Σ) with respectto P andTy, whereθ ∗ ∈ Subst∞(Σ) is the composition of the external resolvents inD′. Note thatthe above coinductive argument is well-formed since we do not apply our coinductive hypothesison the same fragment ofD that gave rise to the coinductive hypothesis, and since all of the nodesthat give rise toC1, . . . ,Cn in T ′′ are expanded inT∗.

Theorem 5.17 gives us, at last, an observational coinductive semantics that can be used to talkabout productivity and coinductive entailment in LP. Soundness of coinductive proof relative togreatest complete Herbrand models of observationally productive programs follows from Corol-lary 4.15 and Theorem 5.17.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 34: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:34

COROLLARY 5.18. (Coinductive soundness of coinductive proofs) Let P∈ LP(Σ) be observa-tionally productive, Ty be a typing function for P, and t∈ Term(Σ). If there exists a coinductiveproof for t with respect to P and Ty, then there exists an instance of t in Mω

P .

6. CONCLUSIONS, RELATED WORK, AND FUTURE WORK

This paper gives a first complete formal account of the declarative and operational semantics ofstructural (i.e., S-) resolution. We started with characterisation of S-resolution in terms of big-stepand small-step operational semantics, and then showed thata rewriting tree representation of thisoperational semantics is inductively sound and complete, as well as coinductively sound. Since ob-servational productivity is one of the most striking features of S-resolution, much of this paper’sdiscussion is centred around then subject of productivity in its many guises: SLD-computationsat infinity, S-computations at infinity, and coinductively observed proofs. We have shown how anapproach to productivity and coinduction based on S-resolution makes it possible to formalise thedistinction between global and observational productivity. This puts LP (and the broader family ofresolution-based methods) on par with coinductive methodsin ITP and TRS. We have also shownthat our new notion of observational productivity supportsthe formulation of a new coinductiveproof principle that gives rise to finite S-derivations thatare sound and complete relative to theSLD-computations at infinity known from the 1980s [Lloyd 1987; van Emden and Abdallah 1985].The webpagehttp://staff.computing.dundee.ac.uk/katya/CoALP/contains implementa-tion prototypes of the resulting coinductive methods in Prolog, Go, and Haskell.

In the course of the paper we have repeatedly revised Kowalski’s formulaLogic Programming =Logic + Control. We have exposed that, in classical LP, the formula was in fact understood asSLD-resolution = SLD-Resolution Reductions + Search Strategies in the inductive setting [Lloyd 1987],andSLD-Computations at Infinity = Global Productivity of SLD-Resolution Reductions + Fairnessin the coinductive setting. In CoLP this formula is renderedasCoLP = SLD-Resolution Reductions+ Loop Detection. Put into this context, this paper introduces the followingstructural reformulationof this formula:Structural Logic Programming = S-Resolution Reductions + Control, where thecontrol is now via rewriting trees. However, by contrast with the above approaches, we revise notjust the control, but also the logic, of LP. It is precisely our new logic that allows us to defineobservational productivity, and to formulate our new coinductive proof principle.

The research reported herein continues the tradition of study of infinite-term models of Hornclause logic [Jaffar and Stuckey 1986; Lloyd 1987; van Emdenand Abdallah 1985; Jaume 2000].In particular, we have given a full characterisation of S-resolution relative to the least and greatestfixed point semantics of LP, as is standard in the classical LPliterature. Moreover, we have con-nected the classical work on least and greatest complete Herbrand models of LP to the more moderncoalgebraic notation [Sangiorgi 2012] in Section 2. Our definitions of term trees and rewriting treesrelate to the line of research into infinite (term-) trees [Courcelle 1983; Jaffar and Stuckey 1986;Johann et al. 2015a].

Our new approach to coinduction in LP unifies and generalisesthose reported previously in, forexample, [Lloyd 1987; Gupta et al. 2007; Simon et al. 2007]. Our new approach is observational,and thus, by contrast with that of [Lloyd 1987], implementable. Our approach does not considernon-productive programs sound, but covers programs likeP3 and P4 that define irrational termsand therefore cannot be handled by CoLP [Gupta et al. 2007; Simon et al. 2007]. Non-productiveprograms, like our “bad” program from Example 1.3, can be transformed into observationally pro-ductive programs and then analysed using our S-resolution-based methods. For example, our “bad”program can be automatically transformed into the productive program

0. better(f(X),t(Y)) ← better(f(X),Y)

S-resolution arose from coalgebraic studies of LP [Komendantskaya et al. 2014;Komendantskaya and Power 2011a; Komendantskaya and Power 2011b], and these were subse-quently developed into a bialgebraic semantics [Bonchi andZanasi 2013; Bonchi and Zanasi 2015].However, the bialgebraic development takes the coalgebraic semantics of LP in a direction different

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 35: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:35

from our productivity-based analysis of S-resolution. Investigating possible connections betweenobservational productivity of logic programs and their bialgebraic semantics offers an interestingavenue for future work.

The definition of observationally productive logic programs given in this paper closely resem-bles the definitions of productive and guarded corecursive functions in ITP — particularly in Coqand Agda, as illustrated in the introduction. The coinductive proof principle (by means of forma-tion and guarded application of coinductive hypotheses) described in this paper takes its inspi-ration from the coinductive proof principle developed in the Calculus of Coinductive Construc-tions [Coquand 1994; Gimenez 1998]. Precise analysis of the relationship between that coinductiveproof principle and the one developed here would require theimposition of a type-theoretic in-terpretation on S-resolution. A type-theoretic view of S-resolution for inductive programs is givenin [Fu and Komendantskaya 2015]. A preliminary investigation of how coinductive hypothesis for-mation for Horn clauses can be interpreted type-theoretically is given in [Fu et al. 2016].

Productivity has also become a well-established topic of research within TRS community; see,e.g., [Endrullis et al. 2010; Endrullis et al. 2015]. The definition of productivity for TRS relates toobservational productivity defined in this paper, and reflects the intuition of finite observability offragments of computations. However, because S-resolutionproductivity is defined viaterminationof rewriting reductions, it also strongly connects to the termination literature for TRS [Terese 2003].Our definition in Section 2 of S-resolution in terms of reduction systems makes the connectionbetween S-resolution and TRS explicit, and thus encouragescross-pollination between research inS-resolution and TRS.

The fact that productivity of S-resolution depends crucially on termination of rewriting reductionsmakes this work relevant to co-patterns [Abel et al. 2013]. In particular, [Basold and Hansen 2015]considers a notion of productivity for co-patterns based onstrong normalisation of term-rewriting.This is similar to our notion of observational productivityfor logic programs. Further investigationof applications of S-resolution in the context of co-patterns is under way.

Observationally productive S-derivations may be seen as anexample of clocked corecur-sion [Atkey and McBride 2013], where finite rewriting trees give the measures of observation ina corecursive computation. Formal investigation of this relation is a future work.

Another related area of research is the study of coinductionin first order calculi other than Hornclause logic, including fixed-point linear logics (e.g.MuLJ) [Baelde 2008] and coinductive sequentcalculi [Brotherston and Simpson 2011]. One important methodological difference between MuLJ(implemented as Bedwyr) [Baelde 2008] and S-resolution is that Bedwyr begins with a strongcalculus for (co)induction and explores its implementations, while S-resolution begins with LP’scomputational structure and constructs such a calculus directly from it. Notably, Bedwyr requirescycle/invariant detection, accomplished via heuristics that are incomplete but practically useful.S-resolution provides a more principled, automatic, and complete methodology for this, and thusoffers new implementation possibilities for systems like Bedwyr. We believe that a combination ofthese approaches will be of interest well outside of the LP community.

Overall, we see the work presented here as laying a new foundation for automated coinduc-tive inference well beyond LP. In particular, we expect our new methods to allow us to extendtype inference algorithms for a variety of programming languages [Ancona and Lagorio 2011;Lammel and Jones 2005; Abel et al. 2013] to accommodate richer forms of coinduction. We arecurrently exploring this enticing new research direction.

7. ACKNOWLEDGMENTS

We thank the following colleagues for discussions that encouraged and inspired this work: AndreasAbel, Davide Ancona, Henning Basold, Peng Fu, Gopal Gupta, Helle Hansen, Martin Hofmannand Tom Schrijvers. We particularly thank Martin Schmidt, Vladimir Komendantskiy and FrantisekFarka, who at different times implemented prototypes of CoAlgebraic Logic Programming (CoALP)and S-Resolution: their input has been invaluable for shaping this work.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.

Page 36: arXiv:1511.07865v1 [cs.LO] 24 Nov 2015 filein LP on par with productivity-based observational approaches to coinduction in ITP and TRS. We prove soundness of struc- We prove soundness

A:36

REFERENCES

Andreas Abel, Brigitte Pientka, David Thibodeau, and AntonSetzer. 2013. Copatterns: programming infinite structuresbyobservations. InPOPL’13. 27–38.

Agda Development Team. 2015. AGDA Reference Manual. (2015). http://appserv.cs.chalmers.se/users/ulfn/wiki/agda.php.Davide Ancona and Giobanni Lagorio. 2011. Idealized coinductive type systems for imperative object-oriented programs.

RAIRO - Theory of Information and Applications45, 1 (2011), 3–33.Robert Atkey and Conor McBride. 2013. Productive coprogramming with guarded recursion. InICFP’13. ACM, 197–208.David Baelde. 2008.A Linear Approach to the Proof-Theory of Least and Greatest Fixed Points. Ph.D. Dissertation. Ecole

Polytechnique, Paris.Henning Basold and Helle Hvid Hansen. 2015. Well-definedness and Observational Equivalence for Inductive-Coinductive

Programs.Submitted(2015).Filippo Bonchi and Fabio Zanasi. 2013. Saturated Semanticsfor Coalgebraic Logic Programming. InCALCO’13. Springer,

80–94.Filippo Bonchi and Fabio Zanasi. 2015. Bialgebraic Semantics for Logic Programming.CoRRabs/1502.06095 (2015).

http://arxiv.org/abs/1502.06095James Brotherston and Alex Simpson. 2011. Sequent calculi for induction and infinite descent.Journal of Logic and Com-

putation21, 6 (2011), 1177–1216.Coq Development Team. 2015. Coq Reference Manual. (2015). https://coq.inria.fr/.Thierry Coquand. 1994. Infinite Objects in Type Theory. InTYPES’93, Vol. 806. 62–78.Bruno Courcelle. 1983. Fundamental Properties of Infinite Trees.Theoretical Computer Science25 (1983), 95–169.Jorg Endrullis, Clemens Grabmayer, Dimitri Hendriks, Ariya Isihara, and Jan Willem Klop. 2010. Productivity of stream

definitions.Theoretical Computer Science411, 4-5 (2010), 765–782.Jorg Endrullis, Helle Hvid Hansen, Dimitri Hendriks, Andrew Polonsky, and Alexandra Silva. 2015. A Coinductive Frame-

work for Infinitary Rewriting and Equational Reasoning. InRTA’15. 143–159.Peng Fu and Ekaterina Komendantskaya. 2015. A Type-Theoretic Approach to Resolution. InLOPSTR’15.Peng Fu, Ekaterina Komendantskaya, Tom Schrijvers, AndrewPond, and Patricia Johann. 2016. Proof Relevant Corecursive

Resolution. InFLOPS’16.Eduardo Gimenez. 1998. Structural Recursive Definitions in Type Theory. InICALP’98. 397–408.Gopal Gupta, Ajay Bansal, Richard Min, Luke Simon, and Ajay Mallya. 2007. Coinductive Logic Programming and Its

Applications. InICALP’07. 27–44.Nevin Heintze and Joxan Jaffar. 1992. Semantic Types for Logic Programs. InTypes in Logic Programming. 141–155.Joxan Jaffar and Peter J. Stuckey. 1986. Semantics of Infinite Tree Logic Programming.Theoretical Computer Science46, 3

(1986), 141–158.Mathieu Jaume. 2000. Logic Programming and Co-inductive Definitions. InCSL’00. 343–355.P. Johann, E. Komendantskaya, F. Farka, P. Fu, and M. Schmidt. 2015b. A Structural Approach to Productivity and Guard-

edness in Logic Programming. InDraft.Patricia Johann, Ekaterina Komendantskaya, and Vladimir Komendantskiy. 2015a. Structural Resolution for Logic Program-

ming. InTechnical Communications of ICLP’15.Ekaterina Komendantskaya and John Power. 2011a. Coalgebraic Derivations in Logic Programming. InCSL’11. 352–366.Ekaterina Komendantskaya and John Power. 2011b. Coalgebraic Semantics for Derivations in Logic Programming. In

CALCO’11. 268–282.Ekaterina Komendantskaya, John Power, and Martin Schmidt.2014. Coalgebraic Logic Programming: from semantics to

implementation.Journal of Logic and Computation(2014).Robert A. Kowalski. 1974. Predicate Logic as a Programming Language.Information Processing74 (1974), 569–574.Ralf Lammel and Simon Peyton Jones. 2005. Scrap Your Boilerplate with Class: Extensible Generic Functions. InICFP’05.

204–215.John W. Lloyd. 1987.Foundations of Logic Programming(2nd ed.). Springer-Verlag.J. Alan Robinson. 1965. A Machine-Oriented Logic Based on the Resolution Principle.J. ACM12, 1 (1965), 23–41.Davide Sangiorgi. 2012.Introduction to Bisimulation and Coinduction. Cambridge University Press.Luke Simon, Ajay Bansal, Ajay Mallya, and Gopal Gupta. 2007.Co-Logic Programming: Extending Logic Programming

with Coinduction. InICALP’07. 472–483.Leon Sterling and Ehud Shapiro. 1986.The Art of Prolog. MIT Press.Terese. 2003.Term Rewriting Systems. Cambridge University Press.Maarten H. van Emden and M. A. Nait Abdallah. 1985. Top-Down Semantics of Fair Computations of Logic Programs.

Journal of Logic Programming2, 1 (1985), 67–75.

ACM Transactions on Computational Logic, Vol. V, No. N, Article A, Publication date: January YYYY.