Academia.eduAcademia.edu

Bialgebraic Semantics for Logic Programming

Bialgebrae provide an abstract framework encompassing the semantics of different kinds of computational models. In this paper we propose a bialgebraic approach to the semantics of logic programming. Our methodology is to study logic programs as reactive systems and exploit abstract techniques developed in that setting. First we use saturation to model the operational semantics of logic programs as coalgebrae on presheaves. Then, we make explicit the underlying algebraic structure by using bialgebrae on presheaves. The resulting semantics turns out to be compositional with respect to conjunction and term substitution. Also, it encodes a parallel model of computation, whose soundness is guaranteed by a built-in notion of synchronisation between different threads.

BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING FILIPPO BONCHI AND FABIO ZANASI Ecole Normale Supérieure de Lyon, CNRS, Inria, UCBL, Université de Lyon — Laboratoire de l’Informatique du Parallélisme. e-mail address: filippo.bonchi@ens-lyon.fr Ecole Normale Supérieure de Lyon, CNRS, Inria, UCBL, Université de Lyon — Laboratoire de l’Informatique du Parallélisme. e-mail address: fabio.zanasi@ens-lyon.fr Abstract. Bialgebrae provide an abstract framework encompassing the semantics of different kinds of computational models. In this paper we propose a bialgebraic approach to the semantics of logic programming. Our methodology is to study logic programs as reactive systems and exploit abstract techniques developed in that setting. First we use saturation to model the operational semantics of logic programs as coalgebrae on presheaves. Then, we make explicit the underlying algebraic structure by using bialgebrae on presheaves. The resulting semantics turns out to be compositional with respect to conjunction and term substitution. Also, it encodes a parallel model of computation, whose soundness is guaranteed by a built-in notion of synchronisation between different threads. 1. Introduction A fundamental tenet for the semantics of programming languages is compositionality: the meaning of a program expression is reducible to the meaning of its subexpressions. This allows inductive reasoning on the structure of programs, and provides several techniques to prove properties of these. In the last decades, much research has been devoted to develop abstract frameworks for defining compositional semantics for different sorts of computational models. For instance, in the setting of concurrency theory, several rule formats [1] have been introduced for ensuring certain behavioural equivalences to be congruences with respect to the syntactic operators of process algebrae. These works have inspired the Mathematical Operational Semantics by Turi and Plotkin [50] where the semantics of a language is supposed to be a bialgebra for a given distributive law representing a so called abstract GSOS specification. A main drawback for this approach is its poor expressiveness, in the sense that many relevant computational models do not naturally fit into the bialgebraic framework. Prime examples of these, still from concurrency theory, are Petri nets and the calculus of Mobile 1998 ACM Subject Classification: F.3.2. Key words and phrases: Logic Programming, Coalgebrae on presheaves, Bialgebrae, Compositionality. LOGICAL METHODS IN COMPUTER SCIENCE DOI:10.2168/LMCS-??? 1 c Filippo Bonchi and Fabio Zanasi Creative Commons 2 FILIPPO BONCHI AND FABIO ZANASI Ambients [13]: defining the operational behaviour of these systems in terms of their components seems to be an intrinsically complex task that needs serious ingenuity of researchers (see, e.g, [11] for a recent compositional semantics of Petri nets). Motivated by these considerations, Milner initiated a research program devoted to systematically derive compositional semantics for more flexible semantics specification called reactive systems [34]. As shown in [9], these can be modeled as coalgebrae on presheaf categories and the resulting compositional semantics can be obtained by means of saturation, a technique that we will detail later. In this paper we study logic programs as reactive systems, applying the aforementioned techniques to obtain a compositional semantics for logic programming. Our approach consists of two steps. First, we model the saturated semantics of a program by means of coalgebrae on presheaves. This allows us to achieve a first form of compositionality, with respect to the substitution of the logic signature. Then, we extend our approach to a bialgebraic setting, making the resulting semantics compositional also with respect to the internal structure of goals. In the remainder of this introduction, we describe these two steps in more detail. Coalgebrae on Presheaves and Saturated Semantics. Coalgebrae on presheaves have been successfully employed to provide semantics to nominal calculi: sophisticated process calculi with complex mechanisms for variable binding, like the π-calculus [21, 22]. The idea is to have an index category C of interfaces (or names), and encode as a presheaf F : C → Set the mapping of any object i of C to the set of states having i as interface, and any arrow f : i → j to a function switching the interface of states from i to j. The operational semantics of the calculus will arise as a notion of transition between states, that is, as a coalgebra α : F → B(F), where B : SetC → SetC is a functor on presheaves encoding the kind of behavior that we want to express. As an arrow in a presheaf category, α has to be a natural transformation, i.e. it should commute with arrows f : i → j in the index category C. Unfortunately, this naturality requirement may fail when the structure of C is rich enough, as for instance when noninjective substitutions [40, 48] or name fusions [39, 6] occur. As a concrete example, consider the π-calculus term t = āhxi|b(y) consisting of a process āhxi sending a message x on a channel named a, in parallel with b(y) receiving a message on a channel named b. Since the names a and b are different, the two processes cannot synchronize. Conversely the term tθ = āhxi|a(y), that is obtained by applying the substitution θ mapping b to a, can synchronize. If θ is an arrow of the index category C, then the operational semantics α is not natural since α(tθ) 6= α(t)θ, where θ denotes the application of θ to the transitions of t. As a direct consequence, also the unique morphism to the terminal coalgebra is not natural: this means that the abstract semantics of π-calculus is not compositional, in the sense that bisimilarity is not a congruence w.r.t. name substitutions. In order to make bisimilarity a congruence, Sangiorgi introduced in [46] open bisimilarity, that is defined by considering the transitions of processes under all possible name substitutions θ. The approach of saturated semantics [9] can be seen as a generalization of open bisimilarity, relying on analogous principles: the operational semantics α is “saturated” w.r.t. the arrows of the index category C, resulting in a natural transformation α♯ in SetC . In [6, 41], this is achieved by first shifting the definition of α to the category Set|C| of presheaves indexed by the discretization |C| of C. Since |C| does not have other arrow BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 3 than the identities, α is trivially a natural transformation in this setting. The source of α is U(F) ∈ Set|C| , where U : SetC → Set|C| is a forgetful functor defined by composition with the inclusion ι : |C| → C. The functor U has a right adjoint K : Set|C| → SetC sending a presheaf to its right Kan extension along ι. The adjoint pair U ⊣ K induces an isomorphism (·)♯X,Y : Set|C| [U(X), Y ] → SetC [X, K(Y )] mapping α to α♯ . The latter is a natural transformation in SetC and, consequently, the abstract semantics results to be compositional. In the first part of the paper, we show that the saturated approach can be fruitfully instantiated to coalgebraic logic programming [30, 32, 31, 33], which consists of a novel semantics for logic programming and a parallel resolution algorithm based on coinductive trees. These are a variant of ∧∨-trees [26] modeling parallel implementations of logic programming, where the soundness of the derivations represented by a tree is guaranteed by the restriction to term-matching (whose algorithm, differently from unification, is parallelizable [19]). There are two analogies with the π-calculus: (a) the state space is modeled by a presheaf on the index category Lop Σ , that is the (opposite) Lawvere Theory associated with some signature Σ; (b) the operational semantics given in [32] fails to be a natural transformation op in SetLΣ : Example 3.6 provides a counter-example which is similar to the π-calculus term t discussed above. The authors of [32] obviate to (b) by relaxing naturality to lax naturality: the operational semantics p of a logic program is given as an arrow in the category Lax (Lop Σ , Poset) op of locally ordered functors F : LΣ → Poset and lax natural transformations between them. They show the existence of a cofree comonad that induces a morphism [[·]]p mapping atoms (i.e., atomic formulae) to coinductive trees. Since [[·]]p is not natural but lax natural, the semantics provided by coinductive trees is not compositional, in the sense that, for some atoms A and substitution θ, [[Aθ]]p 6= [[A]]p θ where [[Aθ]]p is the coinductive tree associated with Aθ and [[A]]p θ denotes the result of applying θ to each atom occurring in the tree [[A]]p . Instead of introducing laxness, we propose to tackle the non-naturality of p with a saturated approach. It turns out that, in the context of logic programming, the saturation map (·)♯ has a neat description in terms of substitution mechanisms: while p performs term-matching between the atoms and the heads of clauses of a given logic program, its op saturation p♯ (given as a coalgebra in SetLΣ ) performs unification. It is worth to remark here that not only most general unifiers are considered but all possible unifiers. A cofree construction leading to a map [[·]]p♯ can be obtained by very standard cateop gorical tools, such as terminal sequences [2]. This is possible because, as Set, both SetLΣ op and Set|LΣ | are (co)complete categories, whereas in the lax approach, Lax (Lop Σ , Poset) not being (co)complete, more indirect and more sophisticated categorical constructions are needed [31, Sec. 4]. By naturality of p♯ , the semantics given by [[·]]p♯ turns out to be compositional, as in the desiderata. Analogously to [[·]]p , also [[·]]p♯ maps atoms to tree structures, which we call saturated ∧∨-trees. They generalize coinductive trees, in the sense that the latter can be seen as a “desaturation” of saturated ∧∨-trees, where all unifiers that are not term-matchers have been discarded. This observation leads to a translation from saturated 4 FILIPPO BONCHI AND FABIO ZANASI to coinductive trees, based on the counit ǫ of the adjunction U ⊣ K. It follows that our framework encompasses the semantics in [32, 31]. Analogously to what is done in [31], we propose a notion of refutation subtree of a given saturated ∧∨-tree, intuitively corresponding to an SLD-refutation of an atomic goal in a program. In our approach, not all the refutation subtrees represent sound derivations, because the same variable may be substituted for different terms in the various branches. We thus study the class of synched refutation subtrees: they are the ones in which, at each step of the represented derivation, the same substitution is applied on all the atoms considered on different branches. Refutation subtrees with this property do represent sound derivations and are preserved by the desaturation procedure. This leads to a result of soundness and completeness of our semantics with respect to SLD-resolution, crucially using both compositionality and the translation into coinductive trees. Bialgebraic Semantics of Goals. In the second part of this paper we extend our framework to model the saturated semantics of goals instead of single atoms. This broadening of perspective is justified by a second form of compositionality that we want to study. Given atoms A and B, one can see the goal {A, B} as their conjunction A ∧ B: the idea is that a resolution for A∧B requires a resolution for A and one for B. Our aim is to take this logical structure into account, proposing a semantics for A ∧ B that can be equivalently given as the “conjunction” (at a higher level) of the semantics [[A]]p♯ and [[B]]p♯ . Formally, we will model the structure given by ∧ as an algebra on the space of goals. To properly extend our saturated approach, the coalgebra p♯ encoding a logic program needs to be compatible with such algebraic structure: the formal ingredient to achieve this will be a distributive law δ involving the type of the algebra of goals and the one of the coalgebra p♯ . This will give raise to an extension of p♯ to a δ-bialgebra p♯δ on the space of goals, via a categorical construction that is commonplace in computer science. For instance, when instantiated to a non-deterministic automaton t, it yields the well-known powerset construction t on t: the states of t are like atoms of a program, and the ones of t are collections of states, like goals are collections of atoms. Thanks to the compatibility of the operational semantics p♯δ , the induced map [[·]]p♯ will δ also be compatible with the algebraic structure of goals, resulting in the compositionality property sketched above, [[A ∧ B]]p♯ = [[A]]p♯ ∧ [[B]]p♯ δ δ δ where on the right side [[·]]p♯ is applied to goals consisting of just one atom, A or B. As we δ did for [[·]]p♯ , we will represent the targets of [[·]]p♯ as trees, which we call saturated ∨-trees. δ Like saturated ∧∨-trees, they encode derivations by (generalized) unification, which now apply to goals instead of single atoms. This operational understanding of [[·]]p♯ allows for a δ more concrete grasp on the higher order conjunction ∧: it can be seen as the operation of “gluing together” (depthwise) saturated ∨-trees. Beyond this form of compositionality, our approach exhibits another appealing feature, arising by definition of δ. When solving a goal G in a program P, the operational semantics p♯δ attempts to perform unification simultaneously on each atom in G with heads in P, by applying the same substitution on all atoms in the goal. If this form of “synchronous” resolution is not possible — for instance, if there is no unique substitution for all atoms in G — then the computation encoded by p♯δ ends up in a failure. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 5 This behavior is rather different from the one of the standard SLD-resolution algorithm for logic programming, where unification is performed sequentially on one atom of the goal at a time. However, we are able to show that the semantics [[·]]p♯ is sound and complete with δ respect to SLD-resolution, meaning that no expressivity is lost in assuming a synchronous derivation procedure as the one above. This result extends the completeness theorem, previously stated for the semantics [[·]]p♯ , from atomic to arbitrary goals. It relies on a notion of refutation subtree for saturated ∨-trees that is in a sense more natural than the one needed for saturated ∧∨-trees. Whereas in the latter we had to impose specific constraints to ensure that refutation subtrees only represent sound derivations, there is no such need in saturated ∨-trees, because the required synchronisation property is guaranteed by construction. A Fistful of Trees. The following table summarises how the saturated derivation trees that we introduce compare with the trees appearing in the logic programming literature. nodes are atoms nodes are goals substitution mechanism most general unification term-matching unification ∧∨-trees coinductive trees saturated ∧∨-trees (Def. 2.6, also called (Def. 3.5, [31, 33]) (Def. 4.5) parallel and-or trees [26]) saturated ∨-trees SLD-trees (e.g. [35]) (Def. 10.1) The computation described by SLD-trees is inherently sequential, whereas all the others in the table exhibit and-or parallelism (cf. Section 2.5). More specifically, (saturated) ∧∨-trees and coinductive trees express independent and-parallelism: there is no exchange of information between the computations involving different atoms in the goal. Instead, saturated ∨-trees encode a dependent form of and-parallelism: at each step every atom of the goal is matched with heads of the program, but they have to agree on the same substitution, thus requiring a form of communication between different threads. Since independent and-parallelism does not cohere well with unification, (saturated) ∧∨-trees may represent unsound derivations. Instead, they are always sound by construction in coinductive trees (because of the restriction to term-matching) and saturated ∨-trees (because the atoms in the goal are processed synchronously, by applying the same substitution). Related works. Our starting point is the key observation that, in coalgebraic logic programming [30, 32, 31, 33], the operational semantics fails to be a natural transformation. As an alternative to the lax approach of the above line of research, we propose saturation which, in the case of logic programming, boils down to unification with respect to all substituions, making the whole approach closer to standard semantics, like Herbrand models [51, 15] (where only ground instances are considered) or the C-semantics of [20] (considering also non-ground instances). As a result, our approach differs sensibly from [30, 32, 31, 33]: in that series of works, the aim is to give an operational semantics to coinductive logic programs and, at the same time, to exploit and-or parallelism. In our case, the semantics is only meant to model standard (recursive) logic programs and the synchronisation mechanism built-in in saturated ∨-trees imposes a form of dependency to and-parallelism. 6 FILIPPO BONCHI AND FABIO ZANASI The two forms of compositionality that we investigate appear in various forms in the standard literature, see e.g. [5, 35]. Our interest is to derive them as the result of applying the categorical machinery — coalgebrae and bialgebrae on presheaves — that has been fruitfully adopted in the setting of process calculi, as detailed above. For instance, in the open π-calculus one aims at the same two forms of compositionality: with respect to name substitution — corresponding to term substitutions in logic programming — and parallel composition of processes — corresponding to conjunction of atoms in a goal. We should also mention other categorical perspectives on (extensions of) logic programming, such as [18, 28, 4, 9]. Amongst these, the most relevant for us is [9] since it achieves compositionality with respect to substitutions and ∧ by exploiting a form of saturation: arrows of the index category are both substitutions and ∧-contexts of the shape G1 ∧ − ∧ G2 (for some goals G1 , G2 ). The starting observation in [9] is that the construction of relative pushouts [34] instantiated to such category captures the notion of most general unifiers. Beyond logic programming, the idea of using saturation to achieve compositionality is even older than [46] (see e.g. [42]). As far as we know, [17] is the first work where saturation is explored in terms of coalgebrae. It is interesting to note that, in [16], some of the same authors also proposed laxness as a solution for the lack of compositionality of Petri nets. A third approach, alternative to laxness and saturation, may be possible by taking a special kind of “powerobject” functor as done in [39, 48] for giving a coalgebraic semantics to fusion and open π-calculus. We have chosen saturated semantics for its generality: it works for any behavioral functor B and it models a phenomenon that occurs in many different computational models (see e.g. [8]). Finally, the approach consisting in saturating and building a bialgebraic model already appeared in [23] (amongst others). In that work, a sort of saturated semantics is achieved by transposing along the adjunction between SetI and SetF obtained from the injection of the category I of finite sets and injective functions into the category F of all functions. An interesting construction, missing in [23], is the one of the distributive law δ for saturated semantics: in our work, δ is built in a canonical way out of the distributive law for the non-saturated semantics. Synopsis. After introducing the necessary background in Section 2, we recall the framework of coalgebraic logic programming of [30, 32, 31, 33] in Section 3. In Section 4 we propose saturated semantics, allowing us to achieve the first compositionality property. In Section 5 we compare saturated semantics with the lax approach of [32]. This is instrumental for proving, in Section 6, soundness and completeness of saturated semantics with respect to SLD-resolution on atomic goals. In the second part of the paper we present the bialgebraic semantics for arbitrary goals. We start in Section 7 and 8 by considering the simpler setting of ground logic programs. In particular, Section 7 shows the second compositionality property and Section 8 draws a comparison with the coalgebraic semantics. Section 9 and Section 10 generalize the results of the previous two sections to arbitrary logic programs. In particular, we conclude Section 10 by proving soundness and completeness of the bialgebraic semantics of goals with respect to SLD-resolution, extending the analogous result for atomic goals in Section 6. The present work extends the conference paper [10] with more examples, proofs and the new material presented in Sections 7, 8, 9 and 10. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 7 Acknowledgements. We thank E. Komendantskaya, T. Hirschowitz, U. Montanari, D. Petrisan, J. Power, M. Sammartino and the anonymous referees of the conference version of this work for the helpful comments. We acknowledge support by project ANR 12IS02001 PACE. 2. Background In this section we fix some terminology, notation and basic results, mainly concerning category theory and logic programming. 2.1. Categories and Presheaves. Given a (small) category C, |C| denotes the category with the same objects as C but no other arrow than the identities. With a little abuse of notation, X ∈ C indicates that X is an object of C and C[X, Y ] the set of arrows from X to Y . We denote with X × Y the product of objects X, Y ∈ C with projections π1 : X × Y → X and π2 : X × Y → Y . Given Z ∈ C and arrows f : Z → X and g : Z → Y , we denote with hf, gi : Z → X × Y the arrow given by universal property of X × Y . We use the notation End(C) for the category of endofunctors on C and natural transformations. A C-indexed presheaf is any functor G : C → Set. We write SetC for the category of C-indexed presheaves and natural transformations. Throughout this paper we will need to extend functors on Set to functors on presheaf categories. For this purpose, it will be useful to state the following construction. Definition 2.1. Given a category C, the functor (·)C : End(Set) → End(SetC ) is defined as follows. • Given an object F : Set → Set, FC : SetC → SetC is defined on G: C → Set as F ◦ G and on α : G ⇒ H as the natural transformation given by  . FH(n) F(αn ) FG(n) −−−−→ n∈C • Given an arrow γ : F ⇒ B of End(Set), γ C : FC ⇒ BC is a natural transformation   βG of natural transformations, where each β given by the family FG −→ BG C G∈Set   γG(n) . βG is defined by FG(n) −−−→ BG(n) n∈C We call FC and γ C extensions of F and γ respectively. We will mainly work with categories of presheaves indexed by Lop Σ — the (opposite) Lawvere theory on a signature Σ, defined in Section 2.4 — and its discretization |Lop Σ |. ˘ To simplify notation, we will follow the convention of writing (·) for the extension functor op op |L | c Σ . (·)LΣ , i.e., (·)C where C = Lop Σ , and (·) for (·) 2.2. Monads and Distributive Laws. A monad in a category C is a functor T : C → C together with two natural transformations η : id ⇒ T and µ : TT ⇒ T, called respectively unit and multiplication of T, which are required to satisfy the following equations for any X ∈ C: µX ◦ ηTX = µX ◦ TηX = id X and TµX ◦ µXX = µX ◦ µX . We shall also make use of the triple notation (T, η, µ) for monads. A distributive law of a monad (T, η T , µT ) over a 8 FILIPPO BONCHI AND FABIO ZANASI monad (M, η M , µM ) is a natural transformation λ : TM ⇒ MT making the diagrams below commute: TX M) T(ηX   TMX O T ηMX MX TM2 X TX λX M ηTX TµM X / MTX O λMX / MTMX MλX / M2 TX   TMX O / MTX O λX µT MX T MηX MµT X T 2 MX MX µM TX TλMX / TMTX λTX / MT 2 X A distributive law λ : TM ⇒ MT between monads yields a monad MT with unit η MT := ηTM ◦ η T and multiplication µMT : = MµT ◦ µM TT ◦ MλT . We introduce now two weaker notions of the law. A distributive law of a monad (T, η T , µT ) over a functor M is a natural transformation λ : TM ⇒ MT such that only the two bottommost squares above commute. One step further in generalization, we call a distributive law of a functor T over a functor M any natural transformation from TM to MT. With the next proposition we observe that the extension functor (·)C (Definition 2.1) preserves the monad structure. Proposition 2.2. If (T, η, µ) is a monad in Set then (T C , η C , µC ) is a monad in SetC . Moreover, if λ : TM → MT is a distributive law of monads in Set then λC : T C MC → MC T C is a distributive law of monads in SetC . Proof. The action of (·)C on arrows of End(Set) is given componentwise. This means that commutativity of the diagrams involving η C , µC and λC in SetC follows by the one of the corresponding diagrams in Set. 2.3. Algebrae, Coalgebrae and Bialgebrae. Given a functor F : C → C, a F-algebra on X ∈ C is an arrow h : F(X) → X, also written as a pair (X, h). A morphism between F-algebrae (X, h) and (Y, i) is an arrow f : X → Y such that f ◦ h = i ◦ F(f ). Dually, a B-coalgebra on X ∈ C is an arrow p : X → B(X), also written as a pair (X, p). A morphism between B-coalgebrae (X, p) and (Y, q) is an arrow g : X → Y such that q ◦ g = B(g) ◦ p. We fix notation CoAlg(B) for the category of B-coalgebrae and their morphisms. If it exists, the final B-coalgebra is the terminal object in CoAlg(B). The cofree B-coalgebra on X ∈ C is given by (Ω, π2 ◦ ω : Ω → B(Ω)), where (Ω, ω) is the terminal object in CoAlg(X × B(·)). Let λ : FB ⇒ BF be a distributive law between functors F, B : C → C. A λ-bialgebra is a triple (X, h, p) where h : FX → X is an F-algebra and p : X → BX is a B-coalgebra subject to the compatibility property given by commutativity of the following diagram: FX h /X Fp p / BX O Bh  FBX λX / BFX A λ-bialgebra morphism from (X, h, p) to (Y, i, q) is an arrow f : X → Y that is both a B-coalgebra morphism from (X, p) to (Y, q) and an F-algebra morphism from (X, h) to BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 9 (Y, i). We fix notation BiAlg(λ) for the category of λ-bialgebrae and their morphisms. If it exists, the final λ-bialgebra is the terminal object in BiAlg(λ). Next we record some useful constructions of bialgebrae out of coalgebrae. When F is a monad and λ is a distributive law of the monad F over the functor B, then any BFcoalgebra canonically lifts to a λ-bialgebra as guaranteed by the following proposition (for a proof see e.g. [27]). Proposition 2.3. Given a monad T : C → C and a functor B : C → C, let λ : TB ⇒ BT be a distributive law of the monad T over the functor B. Given a BT-coalgebra p : X → BTX, define the B-coalgebra pλ : TX → BTX as TX Tp B(µT X) / TBTX λTX / BTTX / BTX . Then (TX, µTX , pλ ) forms a λ-bialgebra. Moreover, this assignment extends to a functor from CoAlg(BT) to BiAlg(λ) mapping: • a BT-coalgebra (X, p) to the λ-bialgebra (TX, µTX , pλ ); • a morphism f : X → Y of BT-coalgebrae to a morphism Tf : TX → TY of λbialgebrae. We recall also the following folklore result (see e.g. [29]) on the relation between final coalgebrae and final bialgebrae. Proposition 2.4. Let λ : FB ⇒ BF be a distributive law between functors F, B : C → C Fc and suppose that a final B-coalgebra c : X → BX exists. Form the B-coalgebra h : FX −→ λ → BFX and let h′ : FX → X be the unique B-coalgebra morphism given by finality FBX −−X of c : X → BX. Then (X, h′ , c) is the final λ-bialgebra. 2.4. Terms, Atoms and Substitutions. We fix a signature Σ of function symbols, each equipped with a fixed arity, and a countably infinite set Var = {x1 , x2 , x3 , . . . } of variables. We model substitutions and unification of terms over Σ and Var according to the categorical perspective of [24, 12]. To this aim, let the (opposite) Lawvere Theory of Σ be a category op Lop Σ where objects are natural numbers, with n ∈ LΣ intuitively representing variables op x1 , x2 , . . . , xn from Var . For any two n, m ∈ Lop Σ , the set LΣ [n, m] consists of all n-tuples ht1 , . . . , tn i of terms where only variables among x1 , . . . , xm occur. The identity on n ∈ Lop Σ, 1 1 denoted by id n , is given by the tuple hx1 , . . . , xn i. The composition of ht1 , . . . , tn i : n → m and ht21 , . . . , t2m i : m → m′ is the tuple ht1 , . . . , tn i : n → m′ , where ti is the term t1i in which every variable xj has been replaced with t2j , for 1 ≤ j ≤ m and 1 ≤ i ≤ n. We call substitutions the arrows of Lop Σ and use Greek letters θ, σ and τ to denote them. Given θ1 : n → m1 and θ2 : n → m2 , a unifier of θ1 and θ2 is a pair of substitutions σ : m1 → m and τ : m2 → m, where m is some object of Lop Σ , such that σ ◦ θ1 = τ ◦ θ2 . The most general unifier of θ1 and θ2 is a unifier with a universal property, i.e. a pushout of the θ1 θ2 diagram m1 ←− n −→ m2 . An alphabet A consists of a signature Σ, a set of variables Var and a set of predicate symbols P, P1 , P2 , . . . each assigned an arity. Given P of arity n and Σ-terms t1 , . . . , tn , P (t1 , . . . , tn ) is called an atom. We use Latin capital letters A, B, . . . for atoms. Given a substitution θ = ht1 , . . . , tn i : n → m and an atom A with variables among x1 , . . . , xn , we adopt the standard notation of logic programming in denoting with Aθ the atom obtained 10 FILIPPO BONCHI AND FABIO ZANASI by replacing xi with ti in A, for 1 ≤ i ≤ n. The atom Aθ is called a substitution instance of A. The notation {A1 , . . . , Am }θ is a shorthand for {A1 θ, . . . , Am θ}. Given atoms A1 and A2 , we say that A1 unifies with A2 (equivalently, they are unifiable) if they are of the form A1 = P (t1 , . . . , tn ), A2 = P (t′1 , . . . , t′n ) and a unifier hσ, τ i of ht1 , . . . , tn i and ht′1 , . . . , t′n i exists. Observe that, by definition of unifier, this amounts to saying that A1 σ = A2 τ . Term matching is a particular case of unification, where σ is the identity substitution. In this case we say that hσ, τ i is a term-matcher of A1 and A2 , meaning that A1 = A2 τ . 2.5. Logic Programming. A logic program P consists of a finite set of clauses C written as H ← B1 , . . . , Bk . The components H and B1 , . . . , Bk are atoms, where H is called the head of C and B1 , . . . , Bk form the body of C. One can think of H ← B1 , . . . , Bk as representing the first-order formula (B1 ∧ · · · ∧ Bk ) → H. We say that P is ground if only ground atoms (i.e. without variables) occur in its clauses. The central algorithm of logic programming is SLD-resolution, checking whether a finite collection of atoms G (called a goal and usually modeled as a set or a list) is refutable in P. A run of the algorithm on inputs G and P gives rise to an SLD-derivation, whose steps of computation can be sketched as follows. At the initial step 0, a set of atoms G0 (the current goal) is initialized as G. At each step i, an atom Ai is selected in the current goal Gi and one checks whether Ai is unifiable with the head of some clause of the program. If not, the computation terminates with a failure. Otherwise, one such clause Ci = H ← B1 , . . . , Bk is selected: by a classical result, since Ai and H unify, they have also a most general unifier hσi , τi i. The goal Gi+1 for the step i + 1 is given as {B1 , . . . , Bk }τi ∪ (Gi \ {Ai })σi . Such a computation is called an SLD-refutation if it terminates in a finite number (say n) of steps with Gn = ∅. In this case one calls computed answer the substitution given by composing the first projections σn , . . . , σ0 of the most general unifiers associated with each step of the computation. The goal G is refutable in P if an SLD-refutation for G in P exists. A correct answer is a substitution θ for which there exist a computed answer τ and a substituion σ such that σ ◦ τ = θ. We refer to [35] for a more detailed introduction to SLD-resolution. Convention 2.5. In any derivation of G in P, the standard convention is that the variables occurring in the clause Ci considered at step i do not appear in goals Gi−1 , . . . , G0 . This guarantees that the computed answer is a well-defined substitution and may require a dynamic (i.e. at step i) renaming of variables appearing in Ci . The associated procedure is called standardizing the variables apart and we assume it throughout the paper without explicit mention. It also justifies our definition (Section 2.4) of the most general unifier as pushout of two substitutions with different target, whereas it is also modeled in the literature as the coequalizer of two substitutions with the same target, see e.g. [24]. The different target corresponds to the two substitutions depending on disjoint sets of variables. Relevant for our exposition are ∧∨-trees (“and-or trees”) [26], which represent executions of SLD-resolution exploiting two forms of parallelism: and-parallelism, corresponding to simultaneous refutation-search of multiple atoms in a goal, and or-parallelism, exploring multiple attempts to refute the same goal. These are also called and-or parallel derivation tress in [33]. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 11 Definition 2.6. Given a logic program P and an atom A, the (parallel) ∧∨-tree for A in P is the possibly infinite tree T satisfying the following properties: (1) Each node in T is either an ∧-node or an ∨-node. (2) Each ∧-node is labeled with one atom and its children are ∨-nodes. (3) The root of T is an ∧-node labeled with A. (4) Each ∨-node is labeled with • and its children are ∧-nodes. (5) For every ∧-node s in T , let A′ be its label. For every clause H ← B1 , . . . , Bk of P and most general unifier hσ, τ i of A′ and H, s has exactly one child t, and viceversa. For each atom B in {B1 , . . . , Bk }τ , t has exactly one child labeled with B, and viceversa. As standard for any tree, we have a notion of depth: the root is at depth 0 and depth i + 1 is given by the children of nodes at depth i. In our graphical representation of trees, we draw arcs between a node and its children which we call edges. A subtree of a tree T is a set of nodes of T forming a tree T ′ , such that the child relation between nodes in T ′ agrees with the one they have in T . An SLD-resolution for the singleton goal {A} is represented as a particular kind of subtree of the ∧∨-tree for A, called derivation subtree. The intuition is that derivation subtrees encode “deterministic” computations, that is, no branching given by or-parallelism is allowed. Refutation subtrees are those derivation subtrees yielding an SLD-refutation of {A}: all paths lead to a leaf with no atoms left to be refuted. Definition 2.7. Let T be the ∧∨-tree for an atom A in a program P. A subtree T ′ of T is a derivation subtree if it satisfies the following conditions: (1) the root of T ′ is the root of T ; (2) if an ∧-node of T belongs to T ′ , then just one of its children belongs to T ′ ; (3) if an ∨-node of T belongs to T ′ , then all its children belong to T ′ . A refutation subtree (called success subtree in [31]) is a finite derivation subtree with only ∨-nodes as leaves. 3. Coalgebraic Logic Programming In this section we recall the framework of coalgebraic logic programming, as introduced in [30, 32, 31, 33]. 3.1. The Ground Case. We begin by considering the coalgebraic semantics of ground logic programs [30]. For the sequel we fix an alphabet A, a set At of ground atoms and a ground logic program P. The behavior of P is represented by a coalgebra p : At → Pf Pf (At) on Set, where Pf is the finite powerset functor and p is defined as follows: p : A 7→ {{B1 , . . . , Bk } | H ← B1 , . . . , Bk is a clause of P and A = H}. The idea is that p maps an atom A ∈ At to the set of bodies of clauses of P whose head H unifies with A, i.e. (in the ground case) A = H. Therefore p(A) ∈ Pf Pf (At) can be seen as representing the ∧∨-tree of A in P up to depth 2, according to Definition 2.6: each element {B1 , . . . , Bk } of p(A) corresponds to a child of the root, whose children are labeled with B1 , . . . , Bk . The full tree is recovered as an element of C(Pf Pf )(At), where C(Pf Pf ) is the cofree comonad on Pf Pf , standardly provided by the following construction [2, 52]. 12 FILIPPO BONCHI AND FABIO ZANASI Construction 3.1. The terminal sequence for the functor At×Pf Pf (·) : Set → Set consists of sequences of objects Xα and arrows ζα : Xα+1 → Xα , defined by induction on α as follows.   At α=0 π1 α=0 Xα := ζα := At × Pf Pf (Xβ ) α=β+1 id At × Pf Pf (ζβ ) α=β+1 For α a limit ordinal, Xα is given as a limit of the sequence and a function ζα : Xα → Xβ is given for each β < α by the limiting property of Xα . By [52] it follows that the sequence given above converges to a limit Xγ such that ζγ : Xγ+1 → Xγ is an isomorphism forming the final At ×Pf Pf (·)-coalgebra (Xγ , ζγ−1 ). Since Xγ+1 is defined as At × Pf Pf (Xγ ), there is a projection function π2 : Xγ+1 → Pf Pf (Xγ ) which makes π2 ◦ ζγ−1 : Xγ → Pf Pf (Xγ ) the cofree Pf Pf -coalgebra on At. This induces the cofree comonad C(Pf Pf ) : Set → Set on Pf Pf as a functor mapping At to Xγ . As the elements of the cofree Pf -coalgebra on a set X are standardly presented as finitely branching trees where nodes have elements of X as labels [52, 45], those of the cofree Pf Pf -coalgebra on X can be seen as finitely branching trees with two sorts of nodes occurring at alternating depth, where only one sort has the X-labeling. We now define a C(Pf Pf )-coalgebra [[·]]p : At → C(Pf Pf )(At). Construction 3.2. Given a ground program P, let p : At → Pf Pf (At) be the coalgebra associated with P. We define a cone {pα : At → Xα }α<γ on the terminal sequence of Construction 3.1 as follows:  id At α=0 pα := hid At , (Pf Pf (pβ ) ◦ p)i α = β + 1. For α a limit ordinal, pα : At → Xα is provided by the limiting property of Xα . Then in particular Xγ = C(Pf Pf )(At) yields a function [[·]]p : At → C(Pf Pf )(At). Given an atom A ∈ At, the tree [[A]]p ∈ C(Pf Pf )(At) is built by iteratively applying the map p, first to A, then to each atom in p(A), and so on. For each natural number m, pm maps A to its ∧∨-tree up to depth m. As shown in [30], the limit [[·]]p of all such approximations provides the full ∧∨-tree of A. Example 3.3. Consider the following ground logic program, based on an alphabet consisting of a signature {a0 , b0 , c0 } and predicates p(−, −), q(−). p(b, c) ← q(a), q(b), q(c) p(b, b) ← p(b, a), p(b, c) p(b, b) ← q(c) q(c) ← The corresponding coalgebra p : At → Pf Pf (At) and the ∧∨-tree [[p(b, b)]]p ∈ C(Pf Pf )(At) are depicted below on the left and on the right, respectively. p(p(b, c)) p(p(b, b)) p(q(c)) p(A) = = = = {{q(a), q(b), q(c)}} {{p(b, a), p(b, c)}{q(c)}} {{}} {} for A ∈ At \ {p(b, c), p(b, b), q(c)} p(b, b) • q(c) • p(b, a) p(b, c) • • q(a) q(b) q(c) • BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 13 3.2. The General Case. In the sequel we recall the extension of the coalgebraic semantics to arbitrary (i.e. possibly non-ground) logic programs presented in [32, 31]. A motivating observation for their approach is that, in presence of variables, ∧∨-trees are not guaranteed to represent sound derivations. The problem lies in the interplay between variable dependencies and unification, which makes SLD-derivations for logic programs inherently sequential processes [19]. Example 3.4. Consider the signature Σ = {cons2 , succ1 , zero0 , nil0 } and the predicates List(−), Nat(−). The program NatList, encoding the definition of lists of natural numbers, will be our running example of a non-ground logic program. List(cons(x1 , x2 )) ← Nat(x1 ), List(x2 ) List(nil) ← Nat(succ(x1 )) ← Nat(x1 ) Nat(zero) ← Let A be the atom List(cons(x1 , cons(x2 , x1 ))). It is intuitively clear that there is no substitution of variables making A represent a list of natural numbers: we should replace x1 with a “number” (for instance zero) in its first occurrence and with a “list” (for instance nil) in its second occurrence. Consequently, there is no SLD-refutation for {A} in NatList. However, consider the ∧∨-tree of A in NatList, for which we provide a partial representation as follows. List(cons(x1 , cons(x2 , x1 ))) • • ... Nat(x1 ) List(cons(x2 , x1 )) • • Nat(x2 ) ... List(x1 ) • • ... The above tree seems to yield an SLD-refutation: List(cons(x1 , cons(x2 , x1 ))) is refuted by proving Nat(x1 ) and List(cons(x2 , x1 )). However, the associated computed answer would be ill-defined, as it is given by substituting x2 with zero and x1 both with zero and with nil (the computed answer of Nat(x1 ) maps x1 to zero and the computed answer of List(cons(x2 , x1 )) maps x1 to nil). To obviate to this problem, in [32] coinductive trees are introduced as a sound variant of ∧∨-trees, where unification is restricted to term-matching. This constraint is sufficient to guarantee that coinductive trees only represent sound derivations: the key intuition is that a term-matcher is a unifier that leaves untouched the current goal, meaning that the “previous history” of the derivation remains uncorrupted. Before formally defining coinductive trees, it is worth recalling that, in [32], the collection of atoms (based on an alphabet A) is modeled as a presheaf At : Lop Σ → Set. The index op category is the (opposite) Lawvere Theory LΣ of Σ, as defined above. For each natural number n ∈ Lop Σ , At(n) is defined as the set of atoms with variables among x1 , . . . , xn . Given an arrow θ ∈ Lop Σ [n, m], the function At(θ) : At(n) → At(m) is defined by substitution, i.e. At(θ)(A) := Aθ. By definition, whenever an atom A belongs to At(n), then it also belongs to At(n′ ), for all n′ ≥ n. However, the occurrences of the same atom in At(n) and At(n′ ) (for n 6= n′ ) are considered distinct: the atoms A ∈ At(n) and A ∈ At(n′ ) can be thought of as two states x1 , . . . , xn ⊢ A and x1 , . . . , xn′ ⊢ A with two different interfaces x1 , . . . , xn and x1 , . . . , xn′ . For this reason, when referring to an atom A, it is important to always specify the set At(n) to which it belongs. 14 FILIPPO BONCHI AND FABIO ZANASI Definition 3.5. Given a logic program P, a natural number n and an atom A ∈ At(n), the n-coinductive tree for A in P is the possibly infinite tree T satisfying properties 1-4 of Definition 2.6 and property 5 replaced by the following1: (5) For every ∧-node s in T , let A′ ∈ At(n) be its label. For every clause H ← B1 , . . . , Bk of P and every term-matcher hid n , τ i of A′ and H, with B1 τ, . . . , Bk τ ∈ At(n), s has exactly one child t, and viceversa. For each atom B in {B1 , . . . , Bk }τ , t has exactly one child labeled with B, and viceversa. We recall from [32] the categorical formalization of this class of trees. The first step is to generalize the definition of the coalgebra p associated with a program P. Definition 3.5 suggests how p should act on an atom A ∈ At(n), for a fixed n: A 7→ {{B1 , . . . , Bk }τ | H ← B1 , . . . , Bk is a clause of P, A = Hτ and B1 τ, . . . , Bk τ ∈ At(n)}. (3.1) For each clause H ← B1 , . . . , Bk , there might be infinitely (but countably) many substitutions τ such that A = Hτ (see e.g. [32]). Thus the object on the right-hand side of (3.1) will be associated with the functor Pc Pf : Set → Set, where Pc and Pf are respectively the countable powerset functor and the finite powerset functor. In order to formalize this as a op op Lop ˘f : SetLop Σ → SetLΣ and P Σ → SetLΣ be extencoalgebra on At : Lop Σ → Set, let P̆c : Set sions of Pc and Pf respectively, given according to Definition 2.1. Then one would like to fix (3.1) as the definition of the n-component of a natural transformation p : At → P̆c P˘f (At). The key problem with this formulation is that p would not be a natural transformation, as shown by the following example. Example 3.6. Let NatList be the same program of Example 3.4. Fix a substitution ˘ θ = hnili : 1 → 0 and, for each n ∈ Lop Σ , suppose that p(n) : At(n) → P̆c Pf (At)(n) is defined according to (3.1). Then the following square does not commute. At(1) p(1) / P̆c P˘f (At)(1) P˘c P˘f (At)(θ) At(θ)   At(0) p(0) / P̆c P˘f (At)(0) A counterexample is provided by the atom List(x1 ) ∈ At(1). Passing through the bottomleft corner of the square, List(x1 ) is mapped first to List(nil) ∈ At(0) and then to {∅} ∈ P̆c P˘f (At)(0) - intuitively, this yields a refutation of the goal {List(x1 )} with substitution of x1 with nil. Passing through the top-right corner, List(x1 ) is mapped first to ∅ ∈ P̆c P˘f (At)(1) and then to ∅ ∈ P̆c P˘f (At)(0), i.e. the computation ends up in a failure. In [32, Sec.4] the authors overcome this difficulty by relaxing the naturality requirefc P ff -coalgebra in the category Lax (Lop , Poset) ment. The morphism p is defined as a P Σ op of locally ordered functors F : LΣ → Poset and lax natural transformations, with each fc P ff the extension of P̆c P˘f to an endofunctor component p(n) given according to (3.1) and P op on Lax (LΣ , Poset). 1Our notion of coinductive tree corresponds to the notion of coinductive forest of breadth n as in [31, Def. 4.4], the only difference being that we “glue” together all trees of the forest into a single tree. It agrees with the definition given in [33, Def. 4.1] apart for the fact that the parameter n is fixed. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 15 The lax approach fixes the problem, but presents also some drawbacks. Unlike the op categories Set and SetLΣ , Lax (Lop Σ , Poset) is neither complete nor cocomplete, meaning f f that a cofree comonad on Pc Pf cannot be retrieved through the standard Constructions fc P ff -coalgebrae 3.1 and 3.2 that were used in the ground case. Moreover, the category of P becomes problematic, because coalgebra maps are subject to a commutativity property stricter than the one of lax natural transformations. These two issues force the formalization of non-ground logic program to use quite different (and more sophisticated) categorical tools than the ones employed for the ground case. Finally, as stressed in the Introduction, the laxness of p makes the resulting semantics not compositional. 4. Saturated Semantics Motivated by the observations of the previous section, we propose a saturated approach to the semantics of logic programs. For this purpose, we consider an adjunction between presheaf categories as depicted on the left.  |Lop Σ| U ' op SetLd Σ ⊥  ι / Lop Σ op Set|LΣ | F  | K(F) Set K The left adjoint U is the forgetful functor, given by precomposition with the inclusion op functor ι : |Lop | ֒→ Lop . As shown in [36, Th.X.1], U has a right adjoint K : Set|LΣ | → Σ Σ op SetLΣ sending F : |Lop Σ | → Set to its right Kan extension along ι. This is a presheaf K(F) : Lop → Set mapping an object n of Lop Σ Σ to Y K(F)(n) := F(m) θ∈Lop Σ [n,m] where m is any object of Lop Σ . Intuitively, K(F)(n) is a set of tuples indexed by arrows with source n and such that, at index θ : n → m, there are elements of F(m). We use ẋ ẏ, . . . to denote such tuples and we write ẋ(θ) to denote the element at index θ of the tuple ẋ. Alternatively, when it is important to show how the elements depend from the indexes, we use hxiθ:n→m (or simply hxiθ ) to denote the tuple having at index θ the element x. With this notation, we can express the behavior of K(F) : Lop Σ → Set on an arrow θ : n → m as K(F)(θ) : ẋ 7→ hẋ(σ ◦ θ)iσ:m→m′ . (4.1) ′ Lop Σ [m, m ], The tuple hẋ(σ ◦ θ)iσ ∈ K(F)(m) can be intuitively read as follows: for each σ ∈ ′ ] in the input tuple ẋ. the element indexed by σ is the one indexed by σ ◦ θ ∈ Lop Σ [n, m op All this concerns the behavior of K on the objects of Set|LΣ | . For an arrow f : F → G op in Set|LΣ | , the natural transformation K(f ) is defined as an indexwise application of f on tuples from K(F). For all n ∈ Lop Σ , ẋ ∈ K(F)(n), K(f )(n) : ẋ 7→ hf (m)(ẋ(θ))iθ:n→m . Lop Σ → Set, the unit η of the adjunction is instantiated to a morphism For any presheaf F : ηF : F → KU(F) given as follows: for all n ∈ Lop Σ , X ∈ F(n), ηF (n) : X 7→ hF(θ)(X)iθ:n→m . (4.2) 16 FILIPPO BONCHI AND FABIO ZANASI When taking F to be At, ηAt : At → KU(At) maps an atom to its saturation: for each A ∈ At(n), the tuple ηAt (n)(A) consists of all substitution instances At(θ)(A) = Aθ of A, each indexed by the corresponding θ ∈ Lop Σ [n, m]. As shown in Example 3.6, given a program P, the family of functions p defined by (3.1) op op fails to be a morphism in SetLΣ . However, it forms a morphism in Set|LΣ | cc P cf (UAt) p : UAt → P cc and P cf denote the extensions of Pc and Pf to Set|Lop Σ | , given as in Definition where P op 2.1. The naturality requirement is trivially satisfied in Set|LΣ | , since |Lop Σ | is discrete. The Lop ♯ c c Σ adjunction induces a morphism p : At → KPc Pf U(At) in Set , defined as ηAt K(p) cc P cf U(At). At −−→ KU(At) −−−→ KP (4.3) cc P cf U. The idea is to let S play the same role as Pf Pf in In the sequel, we write S for KP the ground case, with the coalgebra p♯ : At → S(At) encoding the program P. An atom A ∈ At(n) is mapped to hp(m)(Aσ)iσ:n→m , that is: p♯ (n) : A 7→ h{{B1 , . . . , Bk }τ | H ← B1 , . . . , Bk is a clause of P, Aσ = Hτ and B1 τ, . . . , Bk τ ∈ At(m)}iσ:n→m . (4.4) Intuitively, p♯ (n) retrieves all unifiers hσ, τ i of A and heads of P: first, Aσ ∈ At(m) arises as a component of the saturation of A, according to ηAt (n); then, the substitution τ is given by term-matching on Aσ, according to K(p)(m). By naturality of p♯ , we achieve the property of “commuting with substitutions” that was precluded by the term-matching approach, as shown by the following rephrasing of Example 3.6. Example 4.1. Consider the same square of Example 3.6, with p♯ in place of p and S in place of P̆c P˘f . The atom List(x1 ) ∈ At(1) together with the substitution θ = hnili : 1 → 0 does not constitute a counterexample to commutativity anymore. Indeed p♯ (1) maps List(x1 ) to the tuple hp(n)(List(x1 )σ)iσ : 1→n , which is then mapped by S(At)(θ) to hp(n)(List(x1 )σ ′ ◦ θ)iσ′ : 0→n according to (4.1). Observe that the latter is just the tuple hp(n)(List(nil)σ ′ )iσ′ : 0→n obtained by applying first At(θ) and then p♯ (0) to List(x1 ). Another benefit of saturated semantics is that p♯ : At → S(At) lives in a (co)complete category which behaves (pointwise) as Set. This allows us to follow the same steps as in the ground case, constructing a coalgebra for the cofree comonad C(S) as a straightforward generalization of Constructions 3.1 and 3.2. For this purpose, we first need to verify the following technical lemma. Proposition 4.2. The functor S is accessible and the terminal sequence for At × S(·) converges to a final At × S(·)-coalgebra. Proof. By [52, Th.7], in order to show convergence of the terminal sequence it suffices to prove that S is an accessible mono-preserving functor. Since these properties are preserved by composition, we show them separately for each component of S: • Being adjoint functors between accessible categories, K and U are accessible themselves [3, Prop.2.23]. Moreover, they are both right adjoints: in particular, U is right adjoint to the left Kan extension functor along ι : |C| ֒→ C. It follows that both preserve limits, whence they preserve monos. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 17 op op cc : Set|Lop cf : Set|Lop Σ | → Set|LΣ | and P Σ | → Set|LΣ | , it is well• Concerning functors P known that Pc : Set → Set and Pf : Set → Set are both mono-preserving accessible cc and P cf also have these properties, because functors on Set. It follows that P (co)limits in presheaf categories are computed objectwise and monos are exactly the objectwise injective morphisms (as shown for instance in [37, Ch.6]). op op Construction 4.3. The terminal sequence for the functor At × S(·) : SetLΣ → SetLΣ consists of a sequence of objects Xα and arrows δα : Xα+1 → Xα , which are defined just as in Construction 3.1, with S replacing Pf Pf . By Proposition 4.2, this sequence converges to a limit Xγ such that Xγ ∼ = Xγ+1 and Xγ is the carrier of the cofree S-coalgebra on At. Since S is accessible (Proposition 4.2), the cofree comonad C(S) exists and maps At to Xγ given as in Construction 4.3. Below we provide a C(S)-coalgebra structure [[·]]p♯ : At → C(S)(At) to At. Construction 4.4. The terminal sequence for At × S(·) induces a cone {p♯α : At → Xα }α<γ as in Construction 3.2 with p♯ and S replacing p and Pf Pf . This yields a natural transformation [[·]]p♯ : At → Xγ , where Xγ = C(S)(At). p♯ : As in the ground case, the coalgebra [[·]]p♯ is constructed as an iterative application of we call saturated ∧∨-tree the associated tree structure. Definition 4.5. Given a logic program P, a natural number n and an atom A ∈ At(n), the saturated ∧∨-tree for A in P is the possibly infinite tree T satisfying properties 1-3 of Definition 2.6 and properties 4 and 5 replaced by the following: (4) Each ∨-node is labeled with a substitution σ and its children are ∧-nodes. (5) For every ∧-node s in T , let A′ ∈ At(n′ ) be its label. For every clause H ← B1 , . . . , Bk of P and every unifier hσ, τ i of A′ and H, with σ : n′ → m′ and B1 τ, . . . , Bk τ ∈ At(m′ ), s has exactly one child t labeled with σ, and viceversa. For each atom B in {B1 , . . . , Bk }τ , t has exactly one child labeled with B, and viceversa. We have now seen three kinds of tree, exhibiting different substitution mechanisms. In saturated ∧∨-trees one considers all the unifiers, whereas in ∧∨-trees and coinductive trees one restricts to most general unifiers and term-matchers respectively. Moreover, in a coinductive tree each ∧-node is labeled with an atom in At(n) for a fixed n, while in a saturated ∧∨-tree n can dynamically change. Example 4.6. Part of the infinite saturated ∧∨-tree of List(x1 ) ∈ At(1) in NatList is depicted below. Note that not all labels of ∧-nodes belong to At(1), as it would be the case for a coinductive tree: such information is inherited from the label of the parent ∨-node, which is now a substitution. For instance, both Nat(x1 ) and List(x2 ) belong to At(2), since their parent is labeled with hcons(x1 , x2 )i : 1 → 2 (using the convention that the target of a substitution is the largest index appearing among its variables). List(x1 ) hnili hzero, x2 i hcons(x1 , x2 )i hcons(x1 , cons(x1 , x2 ))i ... Nat(x1 ) List(x2 ) Nat(x1 ) List(cons(x1 , x2 )) ... hx1 , nili hzero, x2 i ... ... ... 18 FILIPPO BONCHI AND FABIO ZANASI Next we verify that the notion of saturated ∧∨-tree is indeed the one given by saturated semantics. In the following proposition and in the rest of the paper, with an abuse of notation we use [[A]]p♯ to denote the application of [[·]]p♯ (n) to A ∈ At(n) without mentioning the object n ∈ Lop Σ. Proposition 4.7 (Adequacy). For all n and A ∈ At(n), the saturated ∧∨-tree of A in a program P is [[A]]p♯ . Proof. Fix n ∈ Lop Σ . Just as Construction 3.1 allows to describe the cofree Pf Pf -coalgebra C(Pf Pf )(At) on At as the space of trees with two sorts of nodes occurring at alternating depth and one sort labelled by At, observing Construction 4.3 we can provide a similar description for the elements of C(S)(At)(n). Those are also trees with two sorts of nodes occurring at alternating depth. One sort (the one of ∧-nodes), is labeled with elements of At(m) for some m ∈ Lop Σ . The root itself is an ∧-node labeled with some atom in At(n). The ∨-nodes children of an ∧-node B ∈ At(m) are not given by a plain set, as in the ground case, but instead by a tuple ẋ ∈ S(At)(m). We can represent ẋ by drawing a child ∨-node t cc P cf At(m). labeled with the substitution θ : m → m′ for each element St of the set ẋ(θ) ∈ P ′ c The ∧-node children of t are labeled with the elements of St ∈ Pf At(m ). The saturated ∧∨-tree for an atom A ∈ At(n), as in Definition 4.5, is a tree of the above kind and thus an element of C(S)(At)(n). The function A 7→ TA mapping A in its saturated ∧∨-tree TA extends to an At × S(·)-coalgebra morphism from At — with coalgebraic structure given by hid , p♯ i — to C(S)(At). By construction [[·]]p♯ is also an At × S(·)-coalgebra morphism. Since C(S)(At) is the final At × S(·)-coalgebra, these two morphisms must coincide, meaning that [[·]]p♯ maps A into its saturated ∧∨-tree TA . For an arrow θ ∈ Lop Σ [n, m], we write θ for C(S)(At)(θ) : C(S)(At)(n) → C(S)(At)(m). With this notation, we can state the compositionality result motivating our approach. Its proof is an immediate consequence of the naturality of [[·]]p♯ . Theorem 4.8 (Compositionality). For all atoms A ∈ At(n) and substitutions θ ∈ Lop Σ [n, m], [[Aθ]]p♯ = [[A]]p♯ θ. We conclude this section with a concrete description of the behavior of the operator θ, ′ for a given substitution θ ∈ Lop Σ [n, m]. Let r be the root of a tree T ∈ C(S)(At)(n) and r the root of T θ. Then (1) the node r has label A iff r′ has label Aθ; (2) the node r has a child t with label σ ◦ θ and children t1 , . . . , tn iff r′ has a child t′ with label σ and children t1 . . . tn . Note that the children t1 , . . . , tn are exactly the same in both trees: θ only modifies the root and the ∨-nodes at depth 1 of T , while it leaves untouched all the others. This peculiar behavior can be better understood by observing that the definition of K(F)(θ), as in (4.1), is independent of the presheaf F. As a result, θ = Xγ (θ) is independent of all the Xα s built in Construction 4.3. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 19 Example 4.9. Recall from Example 4.6 the saturated ∧∨-tree [[List(x1 )]]p♯ . For θ = hcons(x1 , x2 )i, the tree [[List(x1 )]]p♯ θ is depicted below. List(cons(x1 , x2 )) hzero, x2 i id2 hx1 , cons(x1 , x2 )i ... Nat(x1 ) List(x2 ) Nat(x1 ) List(cons(x1 , x2 )) ... hx1 , nili hzero, x2 i ... ... ... 5. Desaturation One of the main features of coinductive trees is to represent (sound) and-or parallel derivations of goals. This leads the authors of [31] to a resolution algorithm exploiting the two forms of parallelism. Motivated by these developments, we include coinductive trees in our framework, showing how they can be obtained as a “desaturation” of saturated ∧∨-trees. For this purpose, the key ingredient is given by the counit ǫ of the adjunction U ⊣ K. Given a presheaf F : |Lop Σ | → Set, the morphism ǫF : UK(F) → F is defined as follows: for all n ∈ Lop and ẋ ∈ UK(F)(n), Σ ǫF (n) : ẋ 7→ ẋ(id n ) (5.1) where ẋ(id n ) is the element of the input tuple ẋ which is indexed by the identity substitution id n ∈ Lop Σ [n, n]. In the logic programming perspective, the intuition is that, while the unit of the adjunction provides the saturation of an atom, the counit reverses the process. It takes the saturation of an atom and gives back the substitution instance given by the identity, that is, the atom itself. We now want to define a “desaturation” map d from saturated ∧∨-trees to coinductive trees, acting as a pointwise application of ǫUAt . For this purpose, first we state the cc P cf for later reference. construction of the cofree comonad on P op cc P cf (·) : Set|Lop Σ | → Set|LΣ | consists Construction 5.1. The terminal sequence for UAt × P of sequences of objects Yα and arrows ξα : Yα+1 → Yα , defined by induction on α as follows.   UAt α=0 π1 α=0 Yα := ξα := c c c c UAt × Pc Pf (Yβ ) α=β+1 id UAt × Pc Pf (ξβ ) α=β+1 For α a limit ordinal, Yα and ξα are defined as expected. As stated in the proof of Proposition cc P cf is a mono-preserving accessible functors. Then by [52, Th.7] we know that the 4.2, P sequence given above converges to a limit Yχ such that Yχ ∼ = Yχ+1 and Yχ is the value op |Lop | |L | c c c c cc P cf of C(Pc Pf ) : Set Σ → Set Σ on UAt, where C(Pc Pf ) is the cofree comonad on P induced by the terminal sequence given above, analogously to Construction 3.1.  cc P cf )(UAt). The next construction defines the desidered morphism d : U C(S)(At) → C(P Construction 5.2. Consider the image of the terminal sequence converging to C(S)(At) = op op Xγ (Construction 4.3) under the forgetful functor U : SetLΣ → Set|LΣ | . We define a 20 FILIPPO BONCHI AND FABIO ZANASI sequence of natural transformations {dα : U(Xα ) → Yα }α<γ as follows2: ( id UAt α=0  dα := c c id UAt × Pc Pf (dβ ) ◦ ǫPcc Pc U(X ) α = β + 1. f β dβ .Y Oβ U(Xβ ) O U(δβ ) ξβ dβ+1 . 5 Yβ+1 U(Xβ+1 ) id UAt ×ǫPc Pc U(X c f β) ) cf (dβ ) cc P id UAt ×P cc P cf U(Xβ ) UAt × P For α<γ a limit ordinal, an arrow dα : U(Xα ) → Yα is provided by the limiting property of Yα . In order to show that the limit case is well defined, observe that, for every β < α, the above square commutes, that is, ξβ ◦ dβ+1 = dβ ◦ U(δβ ). This can be easily checked by ordinal induction, using the fact that ǫPcc Pc U(X ) is a natural transformation for each β < α. f β  cc P cf )(UAt). We now turn to the definition of a natural transformation d : U C(S)(At) → C(P If χ ≤ γ, then this is provided by dχ : U(Xχ ) → Yχ together with the limiting property of U(Xγ ) on U(Xχ ). In case γ < χ, observe that, since Xγ is isomorphic to Xγ+1 , then Xγ is isomorphic to Xζ for all ζ > γ, and in particular Xγ ∼ = Xχ . Then we can suitably extend the sequence to have a natural transformation dχ : U(Xχ ) → Yχ . The morphism d is given as the composition of dχ with the isomorphism between U(Xγ ) and U(Xχ ). The next theorem states that d is a translation from saturated to coinductive trees: given an atom A ∈ At(n), it maps [[A]]p♯ to the n-coinductive tree of A. The key intuition is that n-coinductive trees can be seen as saturated ∧∨-trees where the labeling of ∨-nodes has been restricted to the identity substitution id n , represented as • (see Definition 3.5). The operation of pruning all ∨-nodes (and their descendants) in [[A]]p♯ which are not labeled with id n is precisely what is provided by Construction 5.2, in virtue of the definition of the counit ǫ given in (5.1). Theorem 5.3 (Desaturation). Let [[·]]p♯ : At → C(S)(At) be defined for a logic program P  cc P cf )(UAt) be defined according to according to Construction 4.4 and d : U C(S)(At) → C(P op Construction 5.2.  Then for all n ∈ |LΣ | and A ∈ UAt(n), the n-coinductive tree of A in P is d ◦ U([[·]]p♯ ) (n)(A). Theorem 5.3 provides an alternative formalization for the coinductive tree semantics [32], given by composition of the saturated semantics with desaturation. In fact it represents a different approach to the non-compositionality problem: instead of relaxing naturality to lax naturality, we simply forget about all the arrows of the index category Lop Σ , shifting op Lop |L | the framework from Set Σ to Set Σ . The substitutions on trees (that are essential, for 2Concerning the successor case, observe that id cc UAt × Pc Pf (dβ ) ◦ ǫP c U(X cP  is in fact an arrow from  c c cc P cf U(Xβ ) , UAt × UKPc Pf U(Xβ ) to Yβ+1 . However, the former is isomorphic to U(Xβ+1 ) = U At × KP because U is a right adjoint (as observed in Proposition 4.2) and thence it commutes with products. c f β) BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 21 instance, for the resolution algorithm given in [31]) exist at the saturated level, i.e. in C(S)(At), and they are given precisely as the operator θ described at the end of Section 4. Example 5.4. The coinductive tree for List(cons(x1 , x2 )) in NatList is depicted on the right. It is constructed by desaturating the tree [[List(cons(x1 , x2 ))]]p♯ in Example 4.9, i.e., by pruning all the ∨-nodes (and their descendants) that are not labeled with id2 . List(cons(x1 , x2 )) id2 Nat(x1 ) List(x2 ) 6. Soundness and Completeness The notion of coinductive tree leads to a semantics that is sound and complete with respect to SLD-resolution [31, Th.4.8]. To this aim, a key role is played by derivation subtrees of coinductive trees: they are defined exactly as derivation subtrees of ∧∨-trees (Definition 2.7) and represent SLD-derivations where the computation only advances by term-matching. Similarly, we now want to define a notion of subtree for saturated semantics. This requires care: saturated ∧∨-trees are associated with unification, which is more liberal than term-matching. In particular, like ∧∨-trees, they may represent unsound derivation strategies (cf. Example 3.4). However, in saturated ∧∨-trees all unifiers, not just the most general ones, are taken into account. This gives enough flexibility to shape a sound notion of subtree, based on an implicit synchronization of the substitutions used in different branches. Definition 6.1. Let T be the saturated ∧∨-tree for an atom A in a program P. A subtree T ′ of T is called a synched derivation subtree if it satisfies properties 1-3 of Definition 2.7 and the following condition: (4) all ∨-nodes of T ′ at the same depth are labeled with the same substitution. A synched refutation subtree is a finite synched derivation subtree with only ∨-nodes as leaves. Its answer is the substitution θ2k+1 ◦ . . . θ3 ◦ θ1 , where θi is the (unique) substitution labeling the ∨-nodes of depth i and 2k + 1 is its maximal depth. The prefix “synched” emphasizes the restriction to and-parallelism encoded in Definition 6.1. Intuitively, we force all subgoals at the same depth to proceed with the same substitution. For instance, this rules out the unsound derivation of Example 3.4. For a comparison with Definition 2.7, note that derivation subtrees can be seen as special instances of synched derivation subtrees where all the substitutions are forced to be identities. We are now in position to compare the different semantics for logic programming. Theorem 6.2 (Soundness and Completeness). Let P be a logic program and A ∈ At(n) an atom. The following are equivalent. (1) The saturated ∧∨-tree for A in P has a synched refutation subtree with answer θ. (2) For some m ∈ Lop Σ , the m-coinductive tree for Aθ in P has a refutation subtree. (3) There is an SLD-refutation for {A} in P with correct answer θ. In statement (3), note that θ is the correct (and not the computed) answer: indeed, the unifiers associated with each derivation step in the synched refutation subtree are not necessarily the most general ones. Towards a proof of Theorem 6.2, a key role is played by the following proposition. 22 FILIPPO BONCHI AND FABIO ZANASI List(c(x1 , (c(x1 , x2 )))) List(c(s(z), (c(s(z), n)))) hx1 , x2 i id 0 Nat(x1 ) List(c(x1 , x2 )) Nat(s(z)) List(c(s(z), n)) hs(x1 ), ni hs(x1 ), ni id 0 id 0 Nat(x1 ) Nat(s(x1 )) List(n) Nat(z) Nat(s(z)) List(n) hzi hzi hzi id 0 id 0 id 0 Nat(z) Nat(z) id 0 id 0 Figure 1: Successful synched derivation subtrees for List(cons(x1 , (cons(x1 , x2 )))) (left) and List(cons(succ(zero), (cons(succ(zero), nil)))) (right) in NatList. The symbols cons, nil, succ and zero are abbreviated to c, n, s and z respectively. Proposition 6.3. Let P be a logic program and A ∈ At(n) an atom. If [[A]]p♯ has a synched refutation subtree with answer θ : n → m, then [[A]]p♯ θ has a synched refutation subtree whose ∨-nodes are all labeled with idm . Proof. See Appendix A. Figure 1 provides an example of the construction needed for Proposition 6.3. Note that the root of the rightmost tree is labeled with an atom of the form Aθ, where θ and A are respectively the answer and the label of the root of the leftmost tree. The rightmost tree is a refutation subtree of the 0-coinductive tree for Aθ and can be obtained from the leftmost tree via a procedure involving the operator θ discussed at the end of Section 4. We are now ready to provide a proof of Theorem 6.2 combining Proposition 6.3, the desaturation procedure of Theorem 5.3 and the compositionality result of Theorem 4.8. Proof of Theorem 6.2. The statement (2 ⇔ 3) is a rephrasing of [31, Th.4.8], whence we focus on proving (1 ⇔ 2), where m is always the target object of θ. (1 ⇒ 2). If [[A]]p♯ has a synched refutation subtree with answer θ, then by Proposition 6.3, [[A]]p♯ θ has a synched refutation subtree T whose ∨-nodes are all labeled with idm . Compositionality (Theorem 4.8) guarantees that T is a synched refutation subtree of [[Aθ]]p♯ . Since all the ∨-nodes of T are labeled with idm , T is preserved by desaturation. This means that T is a refutation subtree of d(U([[·]]p♯ ))(m)(Aθ) which, by Theorem 5.3, is the mcoinductive tree for Aθ in P. (2 ⇐ 1). If the m coinductive tree for Aθ has a refutation subtree T then, by Theorem 5.3, this is also the case for d(U([[·]]p♯ ))(m)(Aθ). This means that T is a synched derivation subtree of [[Aθ]]p♯ whose ∨-nodes are all labeled by idm . By compositionality, T is also a subtree of [[A]]p♯ θ. Let t be the ∨-node at the first depth of T . By construction of the operator θ, the root of [[A]]p♯ has a child t′ labeled with θ having the same children as t. Therefore [[A]]p♯ has a synched refutation subtree with answer θ. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 23 7. Bialgebraic Semantics of Goals: the Ground Case In this section we lay the foundations of a (saturated) semantics that applies directly to goals. As outlined in the introduction, a main motivation for this extension is the study of yet another form of compositionality, now with respect to the algebraic structure of goals. First, we approach the question in the simpler case of ground logic programs. Such a program P, that in Section 3.1 has been represented as a Pf Pf -coalgebra p : At → Pf Pf (At), will now be modeled as a certain bialgebra. The coalgebraic part will be given by the endofunctor Pf : this corresponds to the outer occurrence of Pf in p : At → Pf Pf (At) and encodes non-determinism, i.e., the possibility that an atom matches multiple heads in a program. The algebraic part, encoding the internal structure of a goal, instead will be given by a monad T that corresponds to the inner occurrence of Pf in p : At → Pf Pf (At). Intuitively, T(At) represents a goal, i.e. a collection of atoms, to be processed in parallel. To formally define the type of our bialgebrae, we need to provide a distributive law λ : TPf ⇒ Pf T. Our specification for λ stems from the observation that Pf (At) models a disjunction of atoms, whereas T(At) models a conjunction. Then λ should just distribute conjunction over disjunction, for instance: (A1 ∨ A2 ) ∧ (B1 ∨ B2 ) 7→ (A1 ∧ B1 ) ∨ (A1 ∧ B2 ) ∨ (A2 ∧ B1 ) ∨ (A2 ∧ B2 ). (7.1) Provided this specification for λ, one could naively think of modelling T as Pf itself, so that the conjunction T(At) is represented as a finite set of atoms. However, this would pose a problem with the naturality of λ. Consider again the example above, now formalized with T = Pf , and suppose to check naturality for f : At → At defined as (A1 7→ B1 , A2 7→ B2 ): TPf (At) λAt / Pf T(At) Pf T(f ) TPf (f )  TPf (At) λAt (7.2)  / Pf T(At) The function λAt ◦ TPf (f ) maps {{A1 , A2 }, {B1 , B2 }} first to {{B1 , B2 }} and then to {{B1 }, {B2 }}. Instead Pf T(f ) ◦ λAt maps {{A1 , A2 }, {B1 , B2 }} first to {{A1 , B1 }, {A1 , B2 }, {A2 , B1 }, {A2 , B2 }} and then to {{B1 }, {B1 , B2 }, {B2 }}. Therefore our specification (7.1) for λ, with T modeled as Pf , would not yield a natural transformation. For this reason, we model instead the elements of T(At) as lists of atoms. Formally, we let T be the list endofunctor L : Set → Set mapping a set X into the set of lists of elements of X and a function f : X → Y into its componentwise application to lists in L(X). Such a L : X ⇒ LX given by x 7→ [x] (where [x] is the list with functor forms a monad with unit ηX a single element x) and a multiplication given by flattening: for l1 , . . . , ln elements of L(X), µL X : LLX ⇒ LX maps [l1 , . . . , ln ] to l1 :: . . . :: ln , where :: denotes list concatenation. In virtue of its definition, we will make use of the notation :: for the function µL X , whenever X is clear from the context. There is a distributive law λ : LPf ⇒ Pf L of the monad Pf over the monad L given by assignments [X1 , . . . , Xn ] 7→ {[x1 , . . . , xn ] | xi ∈ Xi } (cf. [38, Ex. 2.4.8]). One can readily check that this definition implements the specification given in (7.1): [{A1 , A2 }, {B1 , B2 }] 7→ {[A1 , B1 ], [A1 , B2 ], [A2 , B1 ], [A2 , B2 ]}. 24 FILIPPO BONCHI AND FABIO ZANASI Also, the counterexample to commutativity of (7.2) given above is neutralized as follows. [{A1 , A2 }, {B1 , B2 }] ✤ λAt ❴ / {[A1 , B1 ], [A1 , B2 ], [A2 , B1 ], [A2 , B2 ]} ❴   TPf (f ) Pf T(f ) [{B1 , B2 }, {B1 , B2 }] ✤ λAt / {[B1 , B1 ], [B1 , B2 ], [B2 , B1 ], [B2 , B2 ]} Remark 7.1. Non-commutativity of diagram (7.2) is essentially due to the idempotence of the powerset constructor. Thus, in order to achieve naturality, multisets instead of lists would also work, as both structures are not idempotent. We chose lists in conformity with implementations of SLD-resolution (for instance PROLOG) where atoms are processed following their order in the goal. Convention 7.2. As motivated above, we will develop our framework modeling goals as lists instead of sets. This requires a mild reformulation of the constructions presented in Section 3.1, with L taking the role of the inner occurrence of Pf : throughout this and the next section we suppose that a ground logic program P is encoded as a coalgebra p : At → Pf L(At) (instead of p : At → Pf Pf (At)) and its semantics as a map [[·]]p : At → C(Pf L)(At). It is immediate to see that this is an harmless variation on the framework for coalgebraic logic programming developed so far and all our results still hold. Accordingly, ∧∨-trees are now elements of C(Pf L)(At) (instead of C(Pf Pf )(At) as in Section 3) and therefore the children of an ∨-node form a list rather than a set. For that reason, while keeping the standard representation of ∧∨-trees, we shall implicitly assume a left to right ordering amongst children of ∨-nodes (cf. Example 8.7). We now have all the formal ingredients to define the extension from coalgebraic to bialgebraic operational semantics. Construction 7.3. Let P be a ground logic program and p : At → Pf L(At) be the coalgebra associated with P. We define the Pf -coalgebra pλ : LAt → Pf LAt in the way prescribed by Proposition 2.3 as LAt Lp P ( :: ) / LPf LAt λLAt / Pf LLAt f / Pf LAt . By Proposition 2.3, (LAt, :: , pλ ) forms a λ-bialgebra. Remark 7.4. Equivalently, the map pλ : LAt → Pf LAt may be obtained with the following universal construction. Let UL ⊣ FL be the canonical adjunction between Set and the category EM(L) of Eilenberg-Moore algebrae for the monad L. By using the distributive law λ we can define an L-algebra structure on Pf LAt as h := Pf ( :: ) ◦ λLAt : LPf LAt → Pf LAt. Let pe: (LAt, :: ) → (Pf LAt, h) be the unique extension of p : At → Pf LAt to an algebra morphism in EM(L) along the adjunction UL ⊣ FL . The map pλ : LAt → Pf LAt of Construction 7.3 is simply UL (e p). By definition it makes the following diagram commute. L ηAt At p  z Pf LAt pλ / LAt (7.3) BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 25 As investigated in [47], the same pattern that here leads to pλ from a given p has many relevant instances. Most notably, the standard powerset construction for non-deterministic automata can be presented in a bialgebraic setting, as a mapping t 7→ t, where t encodes a non-deterministic automaton on state space X, t a deterministic one on state space P = t. It is worth noticing that the PX and a diagram analogous to (7.3) yields t ◦ ηX powerset construction performs a determinization of the automaton t: non-determinism is internalized, that is, an element of the state space PX models a disjunction of states. Our construction follows a different intuition: the non-determinism given by clauses with the same head is preserved and what is internalized is the possible ∧-parallel behavior of a computation: indeed, an element of LAt models a conjunction of atoms. Example 7.5. Let pλ : LAt → Pf LAt be constructed out of the coalgebra p : At → Pf LAt associated with the logic program of Example 3.3. For a concrete grasp on Construction 7.3, we compute pλ ([p(b, b), p(b, c)]) ∈ Pf LAt. First, observe that p : At → Pf LAt assigns to the atom p(b, b) the set {[q(c)], [p(b, a), p(b, c)]} and to p(b, c) the set {[q(a), q(b), q(c)]}. It is therefore easy to see that Lp maps [p(b, b), p(b, c)] into the list of sets of lists [{[q(c)], [p(b, a), p(b, c)]}, {[q(a), q(b), q(c)]}]. This is mapped by λLAt into the set of lists of lists {[[q(c)], [q(a), q(b), q(c)]], [[p(b, a), p(b, c)], [q(a), q(b), q(c)]]} and finally, via Pf ( :: ) into the set of lists {[q(c), q(a), q(b), q(c)], [p(b, a), p(b, c), q(a), q(b), q(c)]} that is the value pλ ([p(b, b), p(b, c)]). The operational reading of such a computation is that [p(b, b), p(b, c)] is a goal given by the conjunction of atoms p(b, b) and p(b, c). The action of pλ tells us that, in order to refute [p(b, b), p(b, c)], one has to refute either [q(c), q(a), q(b), q(c)] (the first element of pλ ([p(b, b), p(b, c)])) or [p(b, a), p(b, c), q(a), q(b), q(c)] (the second element of pλ ([p(b, b), p(b, c)])). For later use, it is interesting to observe also that pλ ([ ]) = {[ ]} for any program p: this Lp λ Pf ( :: ) is because [ ] 7→ [ ] → 7 LAt {[ ]} 7→ {[ ]}. Remark 7.6. For yet another view on Construction 7.3, we remark that the map pλ can be presented in terms of the following SOS-rules, where l → l′ means l′ ∈ pλ (l). l1 l ∈ p(A) [A] → l l2 l1 → l1′ l2 → l2′ l1 :: l2 → l1′ :: l2′ l3 [] → [] Rule (l1) corresponds to commutativity of (7.3). Rule (l2) states that pλ preserves the algebraic structure of LAt given by list concatenation :: . Operationally, this means that one step of (parallel) resolution for l1 :: l2 requires both a step for l1 and a step for l2 . Finally, rule (l3) encodes the trivial behavior of pλ on the empty list, as observed in Example 7.5. We now provide a cofree construction for the semantics [[·]]pλ arising from pλ . Construction 7.7. Let C(Pf )(LAt) denote the cofree Pf -coalgebra on LAt, obtained anal- ∼ = ogously to Construction 3.1. It enjoys a final LAt×Pf (·)-coalgebra structure c : C(Pf )(LAt) − → LAt × Pf (C(Pf )(LAt)). We now want to lift its universal property to the setting of bialgebrae, showing that C(Pf )(LAt) forms a final λ′ -bialgebra, where λ′ : L(LAt × Pf (·)) ⇒ 26 FILIPPO BONCHI AND FABIO ZANASI LAt × Pf L(·) is a distributive law of the monad L over the functor LAt × Pf (·) defined by L(LAt × Pf (X)) hLπ1 ,Lπ2 i / LLAt × LPf (X) :: ×λX / LAt × Pf L(X) . For this purpose, we apply the construction of Proposition 2.4. First, using finality of ∼ = c : C(Pf )(LAt) − → LAt × Pf (C(Pf )(LAt)) we define an L-algebra :: : L(C(Pf )(LAt)) → C(Pf )(LAt) as follows: :: L(C(Pf )(LAt)) / C(Pf )(LAt) Lc  L(LAt × Pf (C(Pf )(LAt))) λ′C(P c f )(LAt)  LAt × Pf (L(C(Pf )(LAt))) id LAt ×Pf ( :: )  / LAt × Pf (C(Pf )(LAt)) This algebraic structure yields the final λ′ -bialgebra C(Pf )(LAt), :: , c) as guaranteed by Proposition 2.4. Now we turn to the definition of the semantics [[·]]pλ : LAt → C(Pf )(LAt). First, observe that the λ-bialgebra (LAt, :: , pλ ) canonically extends to a λ′ -bialgebra by letting hid LAt , pλ i be the LAt × Pf (·)-coalgebra structure on LAt. We can then use finality of C(Pf )(LAt), :: , c) to obtain the unique λ′ -bialgebra morphism [[·]]pλ : LAt → C(Pf )(LAt) making the following diagram commute: LLAt L[[·]]p λ / L(C(Pf )(LAt)) :: :: [[·]]p  LAt  λ / C(Pf )(LAt) (7.4) c hid LAt ,pλ i  LAt × Pf (LAt) id LAt ×Pf ([[·]]p ) λ  / LAt × Pf (C(Pf )(LAt)). One can check that the above construction of the map [[·]]pλ can be equivalently obtained by building a cone with vertex LAt on the terminal sequence generating C(Pf )(LAt), analogously to Construction 3.2. Since [[·]]pλ is given as a morphism of bialgebrae instead of plain coalgebrae, it will preserve the algebraic structure of LAt. This allow us to state the motivating compositionality property of bialgebraic semantics where, intuitively, list concatenation :: models the conjunction ∧ described in the introduction. Theorem 7.8 (∧-Compositionality). Given lists l1 , . . . , lk of atoms in At: [[l1 :: . . . :: lk ]]pλ = [[l1 ]]pλ :: . . . :: [[lk ]]pλ . Where l1 :: . . . :: lk is notation for :: ([l1 , . . . , lk ]) = µL At ([l1 , . . . , lk ]) and [[l1 ]]pλ :: . . . :: [[lk ]]pλ for :: ([[[l1 ]]pλ , . . . , [[lk ]]pλ ]). BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 27 Proof. The statement is given by the following derivation: [[l1 :: . . . :: lk ]]pλ = [[·]]pλ ◦ :: ([l1 , . . . , lk ]) = :: ◦ L[[·]]pλ ([l1 , . . . , lk ]) = :: ([[[l1 ]]pλ , . . . , [[lk ]]pλ ]) = [[l1 ]]pλ :: . . . :: [[lk ]]pλ . The first and the last equality are just given by unfolding notation. The second equality amounts to commutativity of the top square in diagram (7.4) and the third one is given by definition of L on the function [[·]]pλ . Example 7.9. Recall from Example 7.5, that pλ ([p(b, b)]) = {[q(c)], [p(b, a), p(b, c)]} and pλ ([p(b, c)]) = {[q(a), q(b), q(c)]}. Below we represent the values [[p(b, b)]]pλ (on the left) and [[p(b, c)]]pλ (on the right) as trees. [p(b, c)] [p(b, b)] [q(c)] [p(b, a), p(b, c)] [q(a), q(b), q(c)] [] [] ... The idea is that edges represent the transitions of the rule system of Remark 7.6. The node [q(c)] has a child labeled with the empty list, since p(q(c)) = {[ ]} (cf. Example 3.3) and thus, by virtue of rule (l1), pλ ([q(c)]) = {[ ]}. Instead, [q(a), q(b), q(c)] has no children because pλ ([q(a), q(b), q(c)]) is empty. That follows by the fact that pλ (q(a)) — and, in fact, also pλ (q(b)) — is empty and thus we cannot trigger rule (l2) to let [q(a), q(b), q(c)] make a transition. Intuitively, this means that [q(a), q(b), q(c)] cannot be refuted because not all of its atoms have a refutation. A similar consideration holds for [p(b, a), p(b, c)]. In Example 7.5, we have shown that pλ ([p(b, b), p(b, c)]) = {[q(c), q(a), q(b), q(c)], [p(b, a), p(b, c), q(a), q(b), q(c)]} and, by similar arguments to those above, we can show that both pλ ([q(c), q(a), q(b), q(c)]) and pλ ([p(b, a), p(b, c), q(a), q(b), q(c)]) are empty. Therefore, we can depict [[p(b, b), p(b, c)]]pλ as the tree below. [p(b, b), p(b, c)] [q(c), q(a), q(b), q(c)] [p(b, a), p(b, c), q(a), q(b), q(c)] By virtue of Theorem 7.8, such a tree can be computed also by concatenating via :: the tree [[[p(b, b)]]]pλ with the tree [[[p(b, c)]]]pλ depicted above. The operation :: on trees T1 and T2 can also be described as follows. (1) If the root of T1 has label l1 and the root of T2 has label l2 , then the root of T1 :: T2 has label l1 :: l2 ; (2) If T1 has a child T1′ and T2 has a child T2′ , then T1 :: T2 has a child T1′ :: T2′ . Observe that such trees are rather different from the ∧∨-trees introduced in Definition 2.6 (cf. also Example 3.3): all nodes are of the same kind (there is no more distinction between 28 FILIPPO BONCHI AND FABIO ZANASI ∧-nodes and ∨-nodes) and are labeled by lists of atoms (rather than just atoms). In the next section, we will formally introduce such trees under the name of (parallel) ∨-trees and show that they provide a sound and complete semantics for ground logic programs. 8. Soundness and Completeness of Bialgebraic Ground Semantics In this section we investigate the relation between the semantics [[·]]p and [[·]]pλ , stating a relative soundness and completeness result. In Section 3.1 we observed that [[·]]p — of type At → C(Pf L)(At), following Convention 7.2 — maps an atom A into its ∧∨-tree (Definition 2.6). As a first step of our analysis, we provide an analogous operational understanding for the value [[l]]pλ ∈ C(Pf )(LAt) associated with a goal l ∈ LAt. The resulting notion of ∨-tree will correspond to the one intuitively given in Example 7.9. Definition 8.1. Given a ground logic program P and a list l ∈ LAt of atoms, the (parallel) ∨-tree for l in P is the possibly infinite tree T satisfying the following properties: (1) Each node in T is labeled with a list of atoms and the root is labeled with l. (2) Let s be a node in T with label l′ = [A1 , . . . , Ak ]. For every list [C1 , . . . , Ck ] of clauses of P such that Hi = Ai for each Ci = H i ← B1i , . . . , Bji , s has exactly one child t, and viceversa. The node t is labeled with the list l1 :: . . . :: lk , where li = [B1i , . . . , Bji ] is the body of clause Ci . Differently from ∧∨-trees, where two kinds of nodes yield a distinction between orand and-parallelism, ∨-trees have only one kind of nodes, intuitively corresponding to orparallelism. The and-parallelism, which in ∧∨-trees is given by the branching of and-nodes labeled with an atom, is encoded in ∨-trees by the labeling of nodes with lists of atoms. The children of a node labeled with l yield the result of simultaneously matching each atom in l with heads in the program (cf. rule (l2) in Remark 7.6).3 Analogously to Proposition 4.7, it is immediate to check the following observation. Proposition 8.2 (Adequacy). Given a list of atoms l ∈ LAt and a program P, [[l]]pλ ∈ C(Pf )(LAt) is the ∨-tree for l in P. We are now in position to provide a translation between the two notions of tree associated respectively with the semantics [[·]]p and [[·]]pλ . Construction 8.3. There is a canonical representation of ∧∨-trees as ∨-trees given as follows. First recall that the domain C(Pf L)(At) of ∧∨-trees is the final At ×Pf L(·)-coalgebra,  ∼ = say with structure given by u : C(Pf L)(At) − → At × Pf L C(Pf L)(At) (cf. ConstrucL × id ) ◦ u be the extension of u to an LAt × P L(·)-coalgebra. We tion 3.1). Let d := (ηAt f now define an LAt × Pf (·)-coalgebra structure dλ′ on L(C(Pf L)(At)) in the way prescribed 3This synchronous form of computation is what makes ∨-trees essentially different from SLD-trees [35], in which also nodes are labeled with lists, but the parent-child relation describes the unification of a single atom in the parent node. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 29 by Proposition 2.3: d λ′ Ld   / LAt × Pf L C(Pf L)(At) O := L(C(Pf L)(At)) L LAt × Pf L C(Pf L)(At)  λ′C(P  id LAt ×Pf ( :: ) f L)(At) / LAt × Pf LL C(Pf L)(At)  Then we use finality of C(Pf )(LAt) (the domain of ∨-trees) to obtain our representation L map r := [[·]]d ′ ◦ ηC(P as in the following commutative diagram. f L)(At) λ L ηC(P C(Pf L)(At) f L)(At) [[·]]d / L(C(Pf L)(At)) λ′ / C(Pf )(LAt) u  d At × Pf L C(Pf L)(At) L ×id ηAt ' z   LAt × Pf L C(Pf L)(At) c d λ′  id LAt ×Pf ([[·]]d λ′ )  / LAt × Pf C(Pf )(LAt) (8.1) The representation map r is a well-behaved translation between the semantics given by [[·]]p and the one given by [[·]]pλ , as shown by the following property. Proposition 8.4. For all A ∈ At, r([[A]]p ) = [[[A]]]pλ . Proof. By construction [[·]]p : At → C(Pf L)(At) is a Pf L-coalgebra morphism and thus clearly also an LAt ×Pf L(·)-coalgebra morphism. By Proposition 2.3 the canonical mapping of an LAt × Pf L(·)-coalgebra to a λ′ -bialgebra At L ,pi hηAt LLAt  :: 7→ LAt  L ,pi hηAt λ′  LAt × Pf L(At) LAt × Pf LAt C(Pf L)(At) LLC(Pf L)(At) d  LAt × Pf L(C(Pf L)(At))  7→ :: LC(Pf L)(At)  d λ′ LAt × Pf LC(Pf L)(At) is functorial, meaning that L[[·]]p : LAt → L(C(Pf L)(At)) is a λ′ -bialgebra morphism and thus in particular a morphism of LAt × Pf (·)-coalgebrae. By construction [[·]]pλ and [[·]]d ′ λ are also LAt × Pf (·)-coalgebra morphisms. Therefore [[·]]d ′ ◦ L[[·]]p = [[·]]pλ by finality of λ 30 FILIPPO BONCHI AND FABIO ZANASI L yields the statement of the proposition: C(Pf )(LAt). Precomposing both sides with ηAt L L L = [[·]]d ′ ◦ ηC(P ◦ [[·]]p = r ◦ [[·]]p . [[·]]pλ ◦ ηAt = [[·]]d ′ ◦ L[[·]]p ◦ ηAt f L)(At) λ λ In order to study soundness and completeness of [[·]]pλ , we give a notion of refutation for ∨-trees. Definition 8.5. Let T ∈ C(Pf )(LAt) be an ∨-tree. A derivation subtree of T is a sequence of nodes s1 , s2 , . . . such that s1 is the root of T and si+1 is a child of si . A refutation subtree is a finite derivation subtree s1 , . . . , sn where the last node sn is labeled with the empty list. In fact, derivation subtrees of ∨-trees have no branching: they are just paths starting from the root. This is coherent with the previously introduced notions of subtree (cf. Definition 2.7): there the only branching allowed was given by and-parallelism, which in the case of ∨-tress has been internalized inside the node labels. For refutation subtrees, the intuition is that they represents paths of computation where all atoms in the initial goal [A1 , . . . , An ] have been refuted, whence eventually the current goal becomes the empty list. Theorem 8.6 (Soundness and Completeness). Let A1 , . . . , An be atoms in At. Then [[[A1 , . . . , An ]]]pλ has a refutation subtree if and only if [[Ai ]]p has a refutation subtree for each Ai . Proof. The statement is given by the following derivation: [[Ai ]]p has a refutation subtree for each Ai iff r([[Ai ]]p ) has a refutation subtree for each Ai iff [[[Ai ]]]pλ has a refutation subtree for each Ai iff [[[A1 ]]]pλ :: . . . :: [[[An ]]]pλ has a refutation subtree iff [[[A1 , . . . , An ]]]pλ has a refutation subtree The first and the third equivalence are given by checking that the property of having a refutation subtree is preserved and reflected both by the representation map r : C(Pf L)(At) → C(Pf )(LAt) (cf. Construction 8.3) and by the concatenation operation :: on ∨-trees. The second equivalence is given by Proposition 8.4 and the last one by Theorem 7.8. Example 8.7. Consider the ground logic program in Example 3.3 and compare the ∧∨-tree [[p(b, b)]]p with the ∨-tree [[[p(b, b)]]]pλ — that is, r([[p(b, b)]]p ) — depicted below on the left and right, respectively. [p(b, b)] p(b, b) • q(c) • p(b, a) ✤ p(b, c) • q(b) / [q(c)] [p(b, a), p(b, c)] [] • q(a) r q(c) [] • ... Following Convention 7.2, children of ∨-nodes form a list which is implicit in the representation above: for instance, in the tree on the left p(b, a) and p(b, c) are displayed according to their order in [p(b, a), p(b, c)] ∈ p(p(b, b)). BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 31 This convention is instrumental in illustrating the behaviour of the representation map r: all the children t1 , . . . , tk of an ∨-nodes (labeled with •) of an ∧∨-trees T are grouped in r(T ) into a single node whose label lists all the atoms labeling t1 , . . . , tk . For instance, the rightmost child of [[p(b, b)]]p becomes in [[[p(b, b)]]]pλ a node labeled with [p(b, a), p(b, c)]. It is also worth to note that the whole subtree reachable from [[p(b, c)]]p is pruned: since pλ (p(b, a)) is the empty set, then also pλ ([p(b, a), p(b, c)]) is empty. Intuitively, p(b, c) should be proved in conjunction with p(b, a) which has no proof and therefore the (parallel) resolution of [p(b, a), p(b, c)] can stop immediately. Instead, the unique refutation subtree of [[p(b, b)]]p (that is p(b, b) − • − q(c) − •) is preserved in r([[p(b, b)]]p ). Indeed, [p(b, b)] − [q(c)] − [ ] is a refutation subtree of [[p(b, b)]]pλ . 9. Bialgebraic Semantics of Goals: the General Case In the sequel we generalize the bialgebraic semantics for goals from ground to arbitrary logic op programs. Our approach is to extend the saturated semantics [[·]]p♯ in SetLΣ introduced in Section 4. This will yield compositionality both with respect to the substitutions in the index category Lop Σ (cf. Theorem 4.8) and with respect to the concatenation of goals (extending the result of Theorem 7.8). Convention 9.1. As we did for ground programs (cf. Convention 7.2), also for arbitrary logic programs we intend to model goals as lists of atoms. This requires a mild reformulation of our framework for saturated semantics. For this purpose, we introduce the extension op b : Set|Lop Σ | → Set|LΣ | of L : Set → Set as in Definition 2.1. Since L is a monad, then L b is also a monad by Proposition 2.2. A logic program P is now encoded in Set|Lop Σ | as L cc LU(At) b cc P cf U(At)) and its saturation in a coalgebra p : UAt → P (instead of p : UAt → P op cc LU(At). b SetLΣ as a colgebra p♯ : At → KP The saturated semantics of P (Construction cc LU)(At). b 4.4) is given by [[·]]p♯ : At → C(KP All results stated in Section 4, 5 and 6 continue to hold in this setting. Our first task is to generalize Construction 7.3. The extension p♯δ of a saturated logic cc LU(At) b program p♯ : At → KP to a bialgebra will depend on a distributive law δ, yet to be op op defined. The domain of p♯δ will be the presheaf L̆At ∈ SetLΣ of goals, where L̆ : SetLΣ → op SetLΣ extends L : Set → Set as in Definition 2.1. Since L is a monad, by Proposition 2.2 L̆ is also a monad with unit and multiplication given componentwise by the ones of L. For ground logic programs, the definition of a bialgebra involves the construction of a Pf -coalgebra out of a Pf L-coalgebra. For arbitrary logic programs there is a type mismatch, cc LU(At) b because in p♯ : At → KP the functor L̆ does not apply to At. However, this can be easily overcome by observing the following general property of the adjunction U ⊣ K. op op op b : Set|LΣ | → Set|LΣ | , F̆ : SetLΣ → Proposition 9.2. Let F : Set → Set be a functor and F op SetLΣ its extensions to presheaf categories, given as in Definition 2.1. Then Fb U = UF̆. 32 FILIPPO BONCHI AND FABIO ZANASI op b the following diagram commutes, where Proof. Fix G ∈ SetLΣ . By definition of U, F̆ and F op op ι : |LΣ | ֒→ LΣ is the inclusion functor. b FUG |Lop Σ| ι UG / Set < F & / Set ? G  Lop Σ F̆G b This gives the following derivation: FUG = FUG = FGι = F̆Gι = UF̆G. The reasoning on Lop arrows of Set Σ is analogous. cc LU(At) b By Proposition 9.2, we can consider p♯ : At → KP as having the type p♯ : At → KUP̆c L̆(At). Thus the bialgebra constructed out of p♯ will be formed by a KUP̆c -coalgebra p♯δ : L̆At → KUP̆c L̆(At). In order to define it by applying Proposition 2.3, the next step is to define δ as a distributive law of type L̆(KUP̆c ) ⇒ (KUP̆c )L̆. For this purpose, our strategy will be to construct δ as the combination of different distributive laws. First, recall the distributive law λ : LPf ⇒ Pf L of monads introduced in Section 7: we override notation by calling λ the distributive law of type LPc ⇒ Pc L defined as the one involving Pf . By Proposition 2.2, λ : LPc ⇒ Pc L extends to a distributive law op of monads λ̆ : L̆P̆c ⇒ P̆c L̆ in SetLΣ . Next, we introduce two other distributive laws: one for P̆c over KU and the other for L̆ over KU. In fact, because KU is a monad arising from the adjunction U ⊣ K and extensions commute with U (Proposition 9.2), we can let ϕ: L̆(KU) ⇒ (KU)L̆ ψ: P̆c (KU) ⇒ (KU)P̆c be defined in a canonical way, using the following general result. Proposition 9.3. Let C and D be categories with an adjunction U ⊣ K for U : C → D b : D → D be two monads such that UT̆ = TU. b and K : D → C. Let T̆ : C → C and T Then, there is a distributive law of monads λ : T̆(KU) ⇒ (KU)T̆ defined for all X ∈ C by b KUX )♭ )♯ (T(id (9.1) where (·)♭X,Z : C[X, KZ] → D[UX, Z] and (·)♯X,Z : D[UX, Z] → C[X, KZ] are the components of the canonical bijection given by the adjunction U ⊣ K. Proof. See Appendix A. op op In our case, C = SetLΣ , D = Set|LΣ | and the required property of commuting with b and P̆c , P cc by Proposition 9.2. In the sequel we U is given for both pairs of monads L̆, L provide the explicit calculation of the distributive law ϕ : L̆KU ⇒ KUL̆ according to (9.1). op op Lop Σ → SetLΣ For this purpose, fix a presheaf G ∈ SetLΣ and n ∈ Lop Σ . By definition ϕG : Set b is given on n ∈ Lop Σ as the following function (where UL̆ = LU by Proposition 9.2): b KUG )♭ )♯ (n) : L̆KUG(n) (L(id ηL̆KUG (n) / KUL̆KUG(n) b KUG )♭ )(n) K(L(id / KUL̆G(n). BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 33 We now compute ϕG (n) on a list of tuples [ẋ1 , . . . , ẋk ]. This is first mapped onto the value [ẋ1 , . . . , ẋk ] ✤ ηL̆KUG (n) / hL̆KUG(θ)[ẋ1 , . . . , ẋk ]iθ : n→m = h[hẋ1 (σ ◦ θ)iσ , . . . , hẋk (σ ◦ θ)iσ ]iθ where the unit η is computed as in (4.2) and the equality follows by definition of K(UG) ♭ on arrows θ ∈ Lop Σ [n, m] — cf. (4.1). Then we apply (id KUG ) componentwise in the list elements of the tuple: h[hẋ1 (σ ◦ θ)iσ , . . . , hẋk (σ ◦ θ)iσ ]iθ ✤ b KUG )♭ )(n) K(L(id / h[hẋ1 (σ ◦ θ)iσ (id n ), . . . , hẋk (σ ◦ θ)iσ (id n )]iθ = h[ẋ1 (θ), . . . , ẋk (θ)]iθ .  By definition of (·)♭ , (id KUG )♭ (n) = ǫUG ◦ U(id KUG (n) : UKUG(n) → UG(n) maps a tuple ẋ into its element ẋ(id n ) — cf. the definition (5.1) of the counit ǫ. The equality holds because hẋi (σ ◦ θ)iσ (id n ) = ẋi (id n ◦ θ) = ẋi (θ). The calculation leading to the definition of ψ : P̆c KU ⇒ KUP̆c is analogous. In conclusion we obtain the following definitions for distributive laws of monads ϕ and ψ: ϕG (n) : L̆KUG(n) → KUL̆G(n) [ẋ1 , . . . , ẋk ] 7→ h[ẋ1 (θ), . . . , ẋk (θ)]iθ : n→m ψG (n) : P̆c KUG(n) {ẋi }i∈I → KUP̆c G(n) 7→ h{ẋi (θ)}i∈I iθ : n→m where I is a countable set of indices. Note that the existence of distributive laws ϕ and ψ implies in particular that KUL̆ and KUP̆c are monads. We have now all ingredients to define the distributive law δ : L̆(KUP̆c ) ⇒ (KUP̆c )L̆: δ: ϕP˘ c L̆KUP̆c / KUL̆P̆c KUλ̆ / KUP̆c L̆ (9.2) op Concretely, given G ∈ SetLΣ and n ∈ Lop Σ , the function δG (n) is defined by [hX1 iθ , . . . , hXk iθ ] ✤ ϕP˘ G (n) c KUλ̆G (n) / h{[x1 , . . . , xk ] | xi ∈ Xi }iθ / h[X1 , . . . , Xk ]iθ ✤ Proposition 9.4. δ : L̆(KUP̆c ) ⇒ (KUP̆c )L̆ is a distributive law of the monad L̆ over the monad KUP̆c . Proof. In [14] it is proven that the natural transformation δ defined as in (9.2) (or, equivalently, the natural transformation ψL̆ ◦ P̆c ϕ : P̆c L̆KU ⇒ KUP̆c L̆) is a distributive law yielding the monad KUP̆c L̆ if one can prove that the three distributive laws λ̆, ϕ and ψ satisfy a compatibility condition called Yang-Baxter equation. This is given by commutativity of the following diagram, which can be easily verified by definition of λ̆, ϕ and ψ. L̆ψ L̆KU P̆c 6 ϕP˘c / KUL̆P̆c KUλ̆ ( L̆P̆c KU λ̆KU ( P̆c L̆KU P˘ cϕ / P̆c KUL̆ ψ L̆ KU P̆c L̆ 6 34 FILIPPO BONCHI AND FABIO ZANASI Convention 9.5. Throughout the rest of the paper, we do not need to manipulate further op op the components of the functor KUP̆c : SetLΣ → SetLΣ and thus we adopt the shorter notation R for it. We are now in position to extend Construction 7.3 to arbitrary logic programs. Construction 9.6. Let P be a logic program and p♯ : At → RL̆(At) be the associated op coalgebra in SetLΣ . We define the R-coalgebra p♯δ : LAt → RL̆(At) in the way prescribed by Proposition 2.3 as L̆p♯ L̆At / L̆RL̆At δL̆At R( :: ) / RL̆L̆At / RL̆At L where :: is µL̆ At , defined componentwise by list concatenation µ since L̆ is the extension of ♯ L. By Proposition 2.3, (LAt, :: , pδ ) forms a δ-bialgebra. In order to have a more concrete intuition, we spell out the details of the above con♯ struction. Fixed n ∈ Lop Σ , L̆p (n) maps a list [A1 , . . . Ak ] ∈ LAt(n) into the list of tuples [hX1 iθ , . . . hXk iθ ] p♯ (n)(A where each Xi = i )(θ) is a set of lists of atoms. This is mapped by δL̆At into the tuple of sets (of lists of lists) h{[l1 , . . . , lk ]|li ∈ Xi }iθ and, finally, by R( :: ) into the tuple of sets (of list) h{l1 :: . . . :: lk |li ∈ Xi }iθ . θ Alternatively, p♯δ can be inductively defined from p♯ by the following rules, where l → l′ stands for l′ ∈ p♯δ (l)(θ). l1 l′ ∈ p♯ (A)(θ) θ [A] → l′ θ l2 θ l1 → l1′ l2 → l2′ θ l1 :: l2 → l1′ :: l2′ l3 θ [] → [] The rule system extends the one provided for the ground case (cf. Remark 7.6) by labeling transitions with the substitution applied on the goal side. Observe that rule (l2) is the same as the one for parallel composition in CSP [44]: the composite system can evolve only if its parallel components are able to synchronise on some common label θ. Example 9.7. Consider the logic program NatList in Example 3.6 and the atoms Nat(x1 ) and List(cons(x1 , x2 )), both in At(2). The morphism p♯ : At → KUP̆c L̆(At) maps these atoms into the tuples defined for all θ ∈ Lop Σ [2, m] as  if x1 θ = zero  {[ ]} {[Nat(t)]} if x1 θ = succ(t) p♯ (Nat(x1 ))(θ) =  ∅ otherwise ♯ p (List(cons(x1 , x2 )))(θ) = {[Nat(x1 )θ, List(x2 )θ]} for some Σ-term t. By application of rule (l1), such tuples are the same of p♯δ ([Nat(x1 )]) and p♯δ ([List(cons(x1 , x2 ))]) and, by mean of rule (l2), it is easy to compute the value of p♯δ BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 35 on the list of atoms [Nat(x1 ), List(cons(x1 , x2 ))].  if x1 θ = zero  {[Nat(zero), List(x2 )θ]} {[Nat(t), Nat(succ(t)), List(x2 )θ]} if x1 θ = succ(t) p♯δ ([Nat(x1 ), List(cons(x1 , x2 ))])(θ) =  ∅ otherwise Intuitively, p♯δ forces all the atoms of a list to synchronize by choosing a common substitution. For instance, [List(cons(x1 , x2 ))] can make a transition with any substitution θ but, when in parallel with [Nat(x1 )], it cannot evolve (and thus cannot be refuted) for those substitutions that do not allow Nat(x1 ) to evolve — i.e., those θ belonging to the third case above. We now generalize Construction 7.7 to define the cofree semantics [[·]]p♯ arising from p♯δ . δ Construction 9.8. The cofree R-coalgebra C(R)(L̆At) on L̆At, defined following the ∼ = same steps of Construction 4.3, forms the final L̆At × R(·)-coalgebra c : C(R)(L̆At) − → L̆At × R(C(R)(L̆At)). We now build its canonical extension to a final δ ′ -bialgebra, where δ ′ : L̆(L̆At × R(·)) ⇒ L̆At × RL̆(·) is a distributive law of the monad L̆ over the functor L̆At × R(·) defined in terms of δ: δG′ : L̆(L̆At × R(G)) hL̆π1 ,L̆π2 i / L̆L̆At × L̆R(G) :: ×δG / L̆At × RL̆(G) . For this purpose, we construct a L̆-algebra :: : L̆(C(R)(L̆At)) → C(R)(L̆At), using finality of C(R)(L̆At): :: L̆(C(R)(L̆At)) / C(R)(L̆At) L̆c  L̆(L̆At × R(C(R)(L̆At))) c δ′ C(R)(L̆At)  L̆At × R(L̆(C(R)(L̆At)))  / L̆At × R(C(R)(L̆At)) id L̆At ×R( :: ) Proposition 2.4 guarantees that C(R)(L̆At), :: , c) is the final δ ′ -bialgebra. We now turn to the definition of the semantics [[·]]p♯ : L̆At → C(R)(L̆At). We let it be the δ unique δ ′ -bialgebra morphism from L̆At to C(R)(L̆At), given by finality of C(R)(L̆At), :: , c): L̆[[·]] p L̆L̆At ♯ δ / L̆(C(R)(L̆At)) :: :: [[·]]  L̆At hid L̆At ,p♯δ i p  ♯ δ / C(R)(L̆At) c  L̆At × R(L̆At) id L̆At ×R([[·]] ♯) p δ  / L̆At × R(C(R)(L̆At)). The next result states that bialgebraic semantics exhibits two forms of compositionality: it respects both the substitutions in Lop Σ (by saturation) and the internal structure of goals. 36 FILIPPO BONCHI AND FABIO ZANASI hz,x2 i [Nat(x1 )] ... hx1 ,x2 i hz,x2 i hs(x1 ),x2 i [] [Nat(x1 )] . . . [Nat(x1 ), List(x2 )] ... hz,x2 i hz,ni [] [] [] ... ... ... [] ... hs(x1 ),x2 i [Nat(z), List(x2 )] [Nat(s(x1 )), List(x2 )] ... hz,ni ... [List(c(x1 , x2 ))] ... hx1 ,ni hx1 ,ni [] [Nat(x1 )] ... ... ... hz,ni [Nat(z)] . . . ... [] [] ... ... ... [] ... Figure 2: Part of the bialgebraic semantics of [Nat(x1 )] (left) and [List(cons(x1 , x2 ))] (right) in At(2). We use the convention that unlabeled edges stand for edges with any substitution. ... [Nat(x1 ), List(c(x1 , x2 ))] hs(x1 ),x2 i hz,x2 i [Nat(z), List(x2 )] ... [Nat(x1 ), Nat(s(x1 )), List(x2 )] hx1 ,ni ... ... ... hz,ni [] [Nat(z)] . . . [] [] ... ... Figure 3: Part of the bialgebraic semantics of [Nat(x1 ), List(cons(x1 , x2 ))]. In order to formulate such theorem, given a substitution θ ∈ Lop Σ [n, m], we use notation: θl := L̆(At)(θ) : L̆(At)(n) → L̆(At)(m) θ := C(R)(LAt)(θ) : C(R)(LAt)(n) → C(R)(LAt)(m). Theorem 9.9 (Two-Fold Compositionality). Let l1 , . . . , lk be list of atoms in At(n) and θ ∈ Lop Σ [n, m] a substitution. The following two equalities hold: [[θl l1 :: . . . :: θl lk ]]p♯ = θ[[l1 ]]p♯ :: . . . :: θ[[lk ]]p♯ = θ([[l1 ]]p♯ :: . . . :: [[lk ]]p♯ ). δ δ Where l1 :: . . . :: lk is notation for :: ([l1 , . . . , lk ]) = for :: (n)([[[l1 ]]p♯ , . . . , [[lk ]]p♯ ]). δ δ δ δ µL̆ At (n)([l1 , . . . , lk ]) and [[l1 ]]p♯ :: . . . :: [[lk ]]p♯ δ δ δ Proof. The proof is entirely analogous to the one for the ground case, see Theorem 7.8. op Commutativity with substitutions is given by naturality of [[·]]p♯ , :: and :: in SetLΣ . δ BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 37 Example 9.10. In Example 9.7 we have computed the values of p♯δ for the lists [Nat(x1 )] and [List(cons(x1 , x2 ))]. Figure 2 shows (a finite part of) their bialgebraic semantics [[[Nat(x1 )]]]p♯ and [[[List(cons(x1 , x2 ))]]]p♯ . These are depicted as ∨-trees (Definition 8.1) δ δ where edges are labeled with substitutions. Analogously to the ground case, one can think of the edges as (labeled) transitions generated by the rule presentation of pδ given above. It is instructive to observe that, while [Nat(x1 )] has one hzero, x2 i-child, [Nat(x1 ), List(x2 )] cannot have a child with such substitution: in order to progress [Nat(x1 ), List(x2 )] needs a substitution which makes progress at the same time both Nat(x1 ) and List(x2 ) like, for instance, hzero, nili. In Example 9.7 we discussed the value p♯δ ([Nat(x1 ), List(cons(x1 , x2 ))]). Figure 3 shows the bialgebraic semantics [[[Nat(x1 ), List(cons(x1 , x2 ))]]]p♯ for such list. By virtue of Theδ orem 9.9, it can be equivalently obtained by concatenating via :: the trees [[[Nat(x1 )]]]p♯ δ and [[[List(cons(x1 , x2 ))]]]p♯ in Figure 2. Similarly to the ground case, the operation of δ concatenating two trees T1 , T2 can be described as follows. (1) If the root of T1 has label l1 and the root of T2 has label l2 , then the root of T1 :: T2 has label l1 :: l2 ; (2) if T1 has a θ-child T1′ and T2 has a θ-child T2′ , then T1 :: T2 has a θ-child T1′ :: T2′ . For instance, while [List(cons(x1 , x2 ))] has one hx1 , x2 i-child, [Nat(x1 ), List(cons(x1 , x2 ))] has no hx1 , x2 i-children because [Nat(x1 )] has no hx1 , x2 i-children. Instead it has one hzero, x2 i-child labeled with [ ] :: [Nat(zero), List(x2 )] and one hsucc(x1 ), x2 i-child labeled with [Nat(x1 )] :: [Nat(succ(x1 )), List(x2 )]. The latter node has no hx1 , nili-children because [Nat(x1 )] has no hx1 , nili-children. 10. Soundness and Completeness of Bialgebraic Semantics In this section we study the relationship between the bialgebraic semantics [[·]]p♯ and the δ other approaches investigated so far. First, analogously to the ground case, we provide an explicit description of the elements of C(Pf )(LAt) that [[·]]p♯ associates with goals. This δ formalizes the notion of tree given in Example 9.10. Definition 10.1. Given a logic program P, n ∈ Lop Σ and a list l ∈ LAt(n) of atoms, the (parallel) saturated ∨-tree for l in P is the possibly infinite tree T satisfying the following properties: (1) Each node s in T is labeled with a list of atoms ls ∈ LAt(m) for some m ∈ Lop Σ and the root is labeled with l. For any child t of s, say labeled with a list lt ∈ LAt(z), the edge from s to t is labeled with a substitution σ : m → z. (2) Let s be a node in T with label l′ = [A1 , . . . , Ak ] ∈ LAt(m). For all substitutions σ, τ1 , . . . , τk and list [C1 , . . . , Ck ] of clauses of P such that, for each Ci = H i ← B1i , . . . , Bji , hσ, τi i is a unifier of Ai and H i , s has exactly one child t, and viceversa. Furthermore, the edge connecting s to t is labeled with σ and the node t is labeled with the list l1 :: . . . :: lk , where li = [τi B1i , . . . , τi Bji ] is given by applying τi to the body of clause Ci . Saturated ∨-trees extend the ∨-trees introduced in Definition 8.1 and we can formulate the same adequacy result. 38 FILIPPO BONCHI AND FABIO ZANASI Proposition 10.2 (Adequacy). Given a list of atoms l ∈ LAt(n) and a program P, [[l]]p♯ ∈ δ C(Pf )(LAt)(n) is the saturated ∨-tree for l in P. It is worth clarifying that the substitutions labeling edges in a saturated ∨-tree play the same role as the substitutions labeling or-nodes in saturated ∧∨-trees. For any node in [[l]]p♯ , say labeled with l1 , an outgoing edge with label θ connecting to a child l2 means that δ l2 is an element of p♯δ (l′ )(θ) — cf. the rule presentation of p♯δ in Section 9. Analogously to Construction 8.3, we now set a translation rsat : C(RL̆)(At) → C(R)(L̆At) between saturated ∨∧-trees (Definition 4.5) and saturated ∨-trees.  ∼ = Construction 10.3. Let u : C(RL̆)(At) − → RL̆At × C(RL̆)(At) be the final At × RL̆(·)- L̆ × id ◦ u as the extension of u coalgebra structure on C(RL̆)(At) and define d : = ηAt to a L̆At × RL̆(·)-coalgebra. Next we provide a L̆At × R(·)-coalgebra structure dδ′ on L̆(C(RL̆)(At)) in the way prescribed by Proposition 2.3: dδ ′ L̆d   / L̆At × RL̆ C(RL̆)(At) O := L̆(C(RL̆)(At)) L̆ L̆At × RL̆ C(RL̆)(At)  id L̆At ×R( :: ) λ′ C(RL̆)(At) / L̆At × RL̆L̆ C(RL̆)(At) By finality of C(R)(L̆At) we obtain our representation map rsat := [[·]]d ′ ◦ η L̆ η L̆ C(RL̆)(At) / L̆(C(RL̆)(At)) [[·]]d δ′  C(RL̆)(At) δ C(RL̆)(At)  . / C(R)(L̆At) u  d At × RL̆ C(RL̆)(At) L̆ ×id ηAt '   { L̆At × RL̆ C(RL̆)(At)  c dδ ′ id L̆At ×R([[·]]d ′ ) δ (10.1)  / L̆At × RC(R)(L̆At) Proposition 10.4. For any atom A ∈ At(n), rsat ([[A]]p♯ ) = [[A]]p♯ . δ Proof. The proof is entirely analogous to the one provided for the ground case, see Proposition 8.4. We now focus on the notion of refutation subtree associated with saturated ∨-trees. As outlined in the introduction, here lies one of the main motivations for bialgebraic semantics. When defining refutation subtrees for saturated semantics (Definition 6.1), we had to require that they were synched. The corresponding operational intuition is that, at each derivation step, the proof-search for the atoms in the current goal can only advance by applying to all of them the same substitution. If we did not impose such condition, we would take into account derivation subtrees yielding an unsound refutation, where the same variable is substituted for different values, as shown in Example 3.4. The deep reason for requiring such constraint is that, to be sound, the explicit andparallelism exhibited by saturated ∧∨-trees has to respect some form of dependency between BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 39 the substitutions applied on different branches. Coinductive trees (Definition 3.5) achieve it by construction, because all substitutions applied in the goal have to be identities. For saturated ∨-trees, this property is also given by construction, but in a more general way: at each step the same substitution (not necessarily the identity) is applied on all the atoms of the goal. This synchronicity property is already encoded in the operational semantics p♯δ , as immediately observable in its rule presentation, and arises by definition of δ. By these considerations, subtrees of saturated ∨-trees are always synched (in the sense of Definition 6.1) and we can define a sound notion of derivation as in the ground case, without the need of additional constraints. Definition 10.5. Let T be a saturated ∨-tree in C(R)(LAt)(n) for some n ∈ Lop Σ. A derivation subtree of T is a sequence of nodes s1 , s2 , . . . such that s1 is the root of T and si+1 is a child of si . A refutation subtree is a finite derivation subtree s1 , . . . , sk where the last element sk is labeled with the empty list. Its answer is the substitution θk ◦ . . . θ2 ◦ θ1 , where θi is the substitution labeling the edge between si and si+1 . The following statement about refutation subtrees will be useful later. Proposition 10.6. Let P be a logic program and l ∈ LAt(n) a list of atoms. If [[l]]p♯ has a δ refutation subtree with answer θ : n → m, then it has also a refutation subtree T = s1 , s2 , . . . with the same answer where the edge connecting s1 and s2 is labeled with θ and all the other edges in T are labeled with id m . Proof. See Appendix A. Example 10.7. The saturated ∨-trees for [Nat(x1 )] and [List(cons(x1 , x2 ))] in Figure 2 contain several refutation subtrees: for instance, [List(cons(x1 , x2 ))] hzero,x2 i [Nat(zero), List(cons(x2 ))] hx1 ,nili [] (10.2) is a refutation subtree of [List(cons(x1 , x2 ))] with answer hx1 , nili◦hzero, x2 i = hzero, nili. Observe that [Nat(x1 )] has a refutation subtree with the same answer: indeed for any substitution θ : 2 → m, [Nat(x1 )] hzero,x2 i [] θ [] (10.3) θ is a refutation subtree with answer θ ◦ hzero, x2 i. This is because rule (l3) yields [ ] → [ ], which is graphically represented in our figures by an unlabeled edge. It is interesting to note that concatenating (10.3) (where θ = hx1 , nili) with (10.2) via :: yields a refutation subtree for [Nat(x1 ), List(cons(x1 , x2 ))] with the same answer: this is [Nat(x1 ), List(cons(x1 , x2 ))] hzero,x2 i [Nat(zero), List(cons(x2 ))] hx1 ,nili [] (10.4) depicted at the center of Figure 3. More generally, if two trees T1 and T2 have refutation subtrees with the same sequence of substitutions θk ◦ . . . θ2 ◦ θ1 , then also T1 :: T2 has a refutation subtree with the same sequence. For an example involving rsat , consider part of the saturated ∧∨-tree for List(cons(x1 , x2 )) depicted below (Example 4.9 discusses a different part of the same tree). This is mapped by rsat into the saturated ∨-tree of [List(cons(x1 , x2 ))] shown on the right of Figure 2. 40 FILIPPO BONCHI AND FABIO ZANASI List(c(x1 , x2 )) hx1 , x2 i Nat(x1 ) hz, x2 i List(x2 ) hz, x2 i hz, ni hz, ni List(x2 ) Nat(z) ... hs(x1 ), x2 i Nat(s(x1 )) List(x2 ) ... hx1 , ni hx1 , ni ... hx1 , ni hx1 , ni hz, ni hx1 , ni Nat(x1 ) Nat(z) ... hx1 , x2 i hz, ni ... The representation map rsat behaves similarly to the one given for the ground case (cf. Example 8.7), the main difference being that, in saturated ∧∨-trees, ∨-nodes are now labeled with substitutions: the effect of rsat is to move such substitutions to the edges of the target saturated ∨-trees. For instance, the label hx1 , x2 i on the ∨-node (on the left above) is moved in Figure 2 to the edge connecting the root [List(cons(x1 , x2 ))] with the node labeled with [Nat(x1 ), List(x2 )]. Observe that this node has one hzero, nili-child and no children associated with hzero, x2 i or hx1 , nili: instead, those two substitutions label one child of Nat(x1 ) and one of List(x2 ), respectively (on the left above). Intuitively, the children of [Nat(x1 ), List(x2 )] are given by considering only the children of Nat(x1 ) and those of List(x2 ) labeled with the same substitution. It is worth to observe that, for every synched refutation subtree T ′ (Definition 6.1) of a saturated ∧∨-tree T there is a refutation subtree in rsat (T ) with the same answer. The effect on T ′ of applying rsat to T can be described by the following procedure: for every depth in T ′ , (a) all the ∧-nodes are grouped into a single node whose label lists all the labels of these ∧-nodes; (b) the ∨-nodes become an edge whose label is the common substitution labeling all these ∨-nodes. For an example, we depict below a synched derivation subtree on the left and the corresponding subtree on the right. (In the saturated ∧∨-tree above there are other three synched refutation subtrees: the reader can find the corresponding refutation subtrees in the ∨-tree on the right of Figure 2.) [List(c(x1 , x2 ))] List(c(x1 , x2 )) hx1 , x2 i hx1 ,x2 i Nat(x1 ) List(x2 ) hz, ni hz, ni [Nat(x1 ), List(x2 )] hz,ni [] Such transformation is neither injective nor surjective. Against surjectivity, consider the refutation subtree (10.3): it does not correspond to any synched derivation subtree because it contains two occurrences of [ ], while the above procedure always transforms synched derivation subtrees into refutation subtrees with exactly one occurrence of [ ]. Nevertheless, using the construction of Proposition 10.6 one can show that for every refutation subtree in rsat (T ) there exists a synched refutation subtree in T with the same answer. For instance, BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 41 (10.3) corresponds to the synched refutation subtree with root Nat(x1 ) and the only other node a child of Nat(x1 ) labeled with θ ◦ hzero, x2 i. Generalizing the observations of Example 10.7 above, we have that a saturated ∧∨-tree T has a synched derivation subtree with answer θ if and only if rsat (T ) has a refutation subtree with answer θ. In case T = [[A]]p♯ for some atom A ∈ At(n), then rsat (T ) = [[[A]]]p♯ δ by Proposition 10.4. This yields the following statement. Proposition 10.8. Fix an atom A ∈ At(n) and a program P. The following are equivalent. (I) The saturated ∨-tree for [A] ∈ L̆At(n) in P has a refutation subtree with answer θ. (II) The saturated ∧∨-tree for A in P has a synched refutation subtree with answer θ. Corollary 10.9 (Soundness and Completeness I). Let P be a logic program and A ∈ At(n) an atom. The following statement is equivalent to any of the three of Theorem 6.2. (4) The saturated ∨-tree for [A] ∈ L̆At(n) in P has a refutation subtree with answer θ. Proof. It suffices to prove the equivalence between (4) and statement (1) of Theorem 6.2, which is given by Proposition 10.8. In fact, since both bialgebraic semantics and SLD-resolution are defined on arbitrary goals, we can state the following stronger result. Theorem 10.10 (Soundness and Completeness II). Let P be a logic program and G = [A1 , . . . , Ak ] ∈ L̆At(n) be a goal. The following are equivalent. (1) The saturated ∨-tree for G in P has a refutation subtree with answer θ. (2) There is an SLD-refutation for G in P with correct answer θ. Proof. Fix a program P and the goal G = [A1 , . . . , Ak ]. The statement is given by the following reasoning. G has an SLD-refutation with correct answer θ (Corollary 10.9) ⇔ each [Ai ] has an SLD-refutation with correct answer θ ⇔ each [[[Ai ]]]p♯ has a refutation subtree with answer θ δ ⇔ [[[A1 ]]]p♯ :: . . . :: [[[Ak ]]]p♯ has a refutation subtree with answer θ δ (Theorem 9.9) δ ⇔ [[G]]p♯ has a refutation subtree with answer θ. δ The first equivalence is a basic fact stemming from the definition of SLD-resolution. The third equivalence comes from the observation that, analogously to the ground case, :: preserves and reflects the property of yielding a refutation. To see this, suppose that [[[Ak ]]]p♯ , . . . , [[[Ak ]]]p♯ all have refutation subtrees with answer θ : n → m. By Proposiδ δ tion 10.6, for each i we can pick a refutation subtree Ti of [[[Ai ]]]p♯ with answer θ, such δ that θ is the substitution labeling the edge connecting depth 0 and 1 and all the other edges are labeled with id m . Therefore, at each depth, T1 , . . . , Tk all have the same substitution labeling the corresponding edge. This means that the operation :: applied to [[[A1 ]]]p♯ , . . . , [[[Ak ]]]p♯ has the effect of “gluing together” T1 , . . . , Tk into a refutation subtree δ δ T of [[[A1 ]]]p♯ :: . . . :: [[[Ak ]]]p♯ with answer θ, just as in Example 10.7, where (10.3) and (10.2) δ δ were glued to form (10.4). Starting instead with a refutation subtree in [[[A1 ]]]p♯ :: . . . :: [[[Ak ]]]p♯ , δ δ one clearly has a decomposition in the converse direction. 42 FILIPPO BONCHI AND FABIO ZANASI 11. Conclusions The first part of this work proposed a coalgebraic semantics for logic programming, extending the framework introduced in [30] for the case of ground logic programs. Our approach has been formulated in terms of coalgebrae on presheaves, whose nice categorical properties made harmless to reuse the very same constructions as in the ground case. A critical point of this generalization was to achieve compositionality with respect to substitutions, which we obtained by employing saturation techniques. We emphasized how these can be explained in terms of substitution mechanisms: while the operational semantics p proposed in [32] is associated with term-matching, its saturation p♯ corresponds to unification. The map p♯ gave rise to the notion of saturated ∧∨-tree, as the model of computation represented in our semantics. We observed that coinductive trees, introduced in [32], can be seen as a desaturated version of saturated ∧∨-trees, and we compared the two notions with a translation. Eventually, we tailored a notion of subtree (of a saturated ∧∨-tree), called synched derivation subtree, representing a sound derivation of a goal in a program. This led to a result of soundness and completeness of our semantics with respect to SLD-resolution. In the second part of the paper, we extended our framework to model the saturated semantics of goals with bialgebrae on presheaves. The main feature of this approach was yet another form of compositionality: the semantics of a goal G can be equivalently expressed as the “pasting” of the semantics of the single atoms composing G. This property arose naturally via universal categorical constructions based on monads and distributive laws. The corresponding operational description was given the name of saturated ∨-trees. The synchronisation of different branches of a derivation subtree, which was imposed on saturated ∧∨-trees, is now given by construction: in saturated ∨-trees the parallel resolution of each atom in the goal always proceeds with the same substitution. On the base of these observations, we extended the soundness and completeness result for saturated semantics to the SLD-resolution of arbitrary (and not just atomic) goals. Saturated ∨-trees carry more information than traditional denotational models like Herbrand or C-models [20]. The latter can be obtained by saturated ∨-trees as follows: a substitution θ is an answer of the saturated ∨-tree of a goal G if and only if Gθ belongs to the minimal C-model. For future work, we would like to find the right categorical machinery to transform saturated ∨-trees into C-models. The approach should be close to the one used in [7] for the semantics of non-deterministic automata with ǫ-transitions: first, the branching structure of ∨-trees is flattened into sets of sequences of substitutions (similarly to passing from bisimilarity to trace equivalence); second, the substitutions in a sequence are composed to form a single substitution (similarly to composing a sequence of words to form a single word). Moreover, we find of interest to investigate infinite computations and the semantics of coinductive logic programming [25]. These have been fruitfully explored within the approach based on coinductive trees [31]. We expect our analysis of the notion of synchronisation for derivation subtrees to bring further insights on the question. Another line of research concerns a deeper understanding of saturation. A drawback of saturated semantics is that one has to take into account all the arrows in the index category, which are usually infinitely many. This problem has been tackled in [8] for powerset-like functors, by employing normalized coalgebrae: one considers only a minimal set of arrows, as an “optimal” representation of all the arrows in the index category. It is not immediate to see how this approach can be generalized to arbitrary functors, as it would be in the BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 43 formulation of saturation in terms of right Kan extension. We believe that the case of logic programming represents an ideal benchmark to investigate this question. Indeed, minimal arrows exactly correspond to most general unifiers. If we understand how the minimal arrow approach works in this setting, we could take aim at a direct categorical modeling of SLD-resolution and its tree representation. References [1] L. Aceto, W. Fokkink, and C. Verhoef. Structural operational semantics. In Handbook of Process Algebra. Elsevier, 2001. [2] J. Adámek and V. Koubek. On the greatest fixed point of a set functor. Theor. Comput. Sci., 150:57–75, 1995. [3] J. Adámek and J. Rosický. Locally Presentable and Accessible Categories. Cambridge University Press, 1994. [4] G. Amato, J. Lipton, and R. McGrail. On the algebraic structure of declarative programming languages. Theor. Comput. Sci., 410(46):4626–4671, 2009. [5] K. R. Apt and M. H. van Emden. Contributions to the theory of logic programming. J. ACM, 29(3):841– 862, July 1982. [6] F. Bonchi, M. G. Buscemi, V. Ciancia, and F. Gadducci. A presheaf environment for the explicit fusion calculus. J. Autom. Reason., 49(2):161–183, 2012. [7] F. Bonchi, S. Milius, A. Silva, and F. Zanasi. How to kill epsilons with a dagger - A coalgebraic take on systems with algebraic label structure. In Coalgebraic Methods in Computer Science - 12th IFIP WG 1.3 International Workshop, CMCS 2014, Colocated with ETAPS 2014, Grenoble, France, April 5-6, 2014, Revised Selected Papers, pages 53–74, 2014. [8] F. Bonchi and U. Montanari. Coalgebraic symbolic semantics. In CALCO, volume 5728 of LNCS, pages 173–190. Springer, 2009. [9] F. Bonchi and U. Montanari. Reactive systems, (semi-)saturated semantics and coalgebras on presheaves. Theor. Comput. Sci., 410(41):4044–4066, 2009. [10] F. Bonchi and F. Zanasi. Saturated semantics for coalgebraic logic programming. In CALCO, pages 80–94, 2013. [11] R. Bruni, H. C. Melgratti, U. Montanari, and P. Sobocinski. Connector algebras for C/E and P/T nets’ interactions. Logical Methods in Computer Science, 9(3), 2013. [12] R. Bruni, U. Montanari, and F. Rossi. An interactive semantics of logic programming. Theory and Practice of Logic Programming, 1(6):647–690, 2001. [13] L. Cardelli and A. D. Gordon. Mobile ambients. In M. Nivat, editor, FoSSaCS, volume 1378 of Lecture Notes in Computer Science, pages 140–155. Springer, 1998. [14] E. Cheng. Iterated distributive laws. Math. Proc. Camb. Philos. Soc., 150(3):459–487, 2011. [15] K. Clark. Predicate Logic as a Computational Formalism. Research monograph / Department of Computing, Imperial College of Science and Technology, University of London. University of London, 1980. [16] A. Corradini, M. Große-Rhode, and R. Heckel. A coalgebraic presentation of structured transition systems. Theor. Comput. Sci., 260(1-2):27–55, 2001. [17] A. Corradini, R. Heckel, and U. Montanari. From SOS specifications to structured coalgebras: How to make bisimulation a congruence. ENTCS, 19(0):118 – 141, 1999. [18] A. Corradini and U. Montanari. An algebraic semantics for structured transition systems and its application to logic programs. Theor. Comput. Sci., 103(1):51 – 106, 1992. [19] C. Dwork, P. C. Kanellakis, and J. C. Mitchell. On the sequential nature of unification. The Journal of Logic Programming, 1(1):35 – 50, 1984. [20] M. Falaschi, G. Levi, C. Palamidessi, and M. Martelli. Declarative modeling of the operational behavior of logic languages. Theoretical Computer Science, 69(3):289 – 318, 1989. [21] M. P. Fiore, E. Moggi, and D. Sangiorgi. A fully abstract model for the π-calculus. Inf. Comput., 179(1):76–117, 2002. [22] M. P. Fiore and S. Staton. A congruence rule format for name-passing process calculi from mathematical structural operational semantics. In LICS, pages 49–58. IEEE, 2006. [23] M. P. Fiore and D. Turi. Semantics of name and value passing. In LICS, pages 93–104. IEEE, 2001. 44 FILIPPO BONCHI AND FABIO ZANASI [24] J. A. Goguen. What is unification? - a categorical view of substitution, equation and solution. In Resolution of Equations in Algebraic Structures, Volume 1: Algebraic Techniques, pages 217–261. Academic, 1989. [25] G. Gupta, A. Bansal, R. Min, L. Simon, and A. Mallya. Coinductive logic programming and its applications. In ICLP, pages 27–44, 2007. [26] G. Gupta and V. S. Costa. Optimal implementation of and-or parallel prolog. Future Generation Computer Systems, 10(1):71–92, 1994. [27] B. Jacobs, A. Silva, and A. Sokolova. Trace semantics via determinization. In CMCS, pages 109–129, 2012. [28] Y. Kinoshita and A. J. Power. A fibrational semantics for logic programs. In Extensions of Logic Programming, pages 177–191. Springer, 1996. [29] B. Klin. Bialgebras for structural operational semantics: An introduction. Theor. Comput. Sci., 412(38):5043–5069, 2011. [30] E. Komendantskaya, G. McCusker, and J. Power. Coalgebraic semantics for parallel derivation strategies in logic programming. In AMAST, volume 6486 of LNCS, pages 111–127. Springer, 2011. [31] E. Komendantskaya and J. Power. Coalgebraic derivations in logic programming. In CSL, volume 12 of LIPIcs, pages 352–366. Schloss Dagstuhl, 2011. [32] E. Komendantskaya and J. Power. Coalgebraic semantics for derivations in logic programming. In CALCO, volume 6859 of LNCS, pages 268–282. Springer, 2011. [33] E. Komendantskaya, J. Power, and M. Schmidt. Coalgebraic logic programming: from semantics to implementation. Journal of Logic and Computation, 2014. [34] J. J. Leifer and R. Milner. Deriving bisimulation congruences for reactive systems. In CONCUR, volume 1877, pages 243–258, 2000. [35] J. W. Lloyd. Foundations of Logic Programming. Springer-Verlag New York, Inc., Secaucus, NJ, USA, 2nd edition, 1993. [36] S. Mac Lane. Categories for the Working Mathematician. Graduate Texts in Mathematics. Springer, 2nd edition, Sept. 1998. [37] S. MacLane and I. Moerdijk. Sheaves in Geometry and Logic: A First Introduction to Topos Theory. Springer, corrected edition, May 1992. [38] E. Manes and P. Mulry. Monad compositions I: general constructions and recursive distributive laws. Theory and Applications of Categories, 18(7):172–208, 2007. [39] M. Miculan. A categorical model of the fusion calculus. ENTCS, 218:275–293, 2008. [40] M. Miculan and K. Yemane. A unifying model of variables and names. In FOSSACS, volume 3441 of LNCS, pages 170–186. Springer, 2005. [41] U. Montanari and M. Sammartino. A network-conscious pi-calculus and its coalgebraic semantics. Submitted to TCS (Festschrift for Glynn Winskel). [42] U. Montanari and V. Sassone. Dynamic congruence vs. progressing bisimulation for CCS. FI, 16(1):171– 199, 1992. [43] P. S. Mulry. Lifting theorems for Kleisli categories. In MFPS, pages 304–319, 1993. [44] A. W. Roscoe, C. A. Hoare, and R. Bird. The theory and practice of concurrency, volume 169. Prentice Hall Englewood Cliffs, 1998. [45] J. J. M. M. Rutten. Universal coalgebra: a theory of systems. Theor. Comput. Sci., 249(1):3–80, 2000. [46] D. Sangiorgi. A theory of bisimulation for the pi-calculus. Acta Inf., 33(1):69–97, 1996. [47] A. Silva, F. Bonchi, M. M. Bonsangue, and J. J. M. M. Rutten. Generalizing the powerset construction, coalgebraically. In FSTTCS, pages 272–283, 2010. [48] S. Staton. Relating coalgebraic notions of bisimulation. In CALCO, volume 5728 of LNCS, pages 191– 205. Springer, 2009. [49] M. Tanaka. Pseudo-distributive Laws and a Unified Framework for Variable Binding. PhD thesis, University of Edinburgh, 2005. [50] D. Turi and G. D. Plotkin. Towards a mathematical operational semantics. In LICS, pages 280–291. IEEE Computer Society, 1997. [51] M. H. Van Emden and R. A. Kowalski. The semantics of predicate logic as a programming language. J. ACM, 23(4):733–742, Oct. 1976. [52] J. Worrell. Terminal sequences for accessible endofunctors. ENTCS, 19:24–38, 1999. BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 45 Appendix A. Proofs In this appendix we collect the proofs of some of the statements in the main text, whose details are not of direct relevance for our exposition. Proposition 6.3. Let P be a logic program and A ∈ At(n) an atom. If [[A]]p♯ has a synched refutation subtree with answer θ : n → m, then [[A]]p♯ θ has a synched refutation subtree whose ∨-nodes are all labeled with idm . Proof. First, we fix the two following properties, holding for all the ∧-nodes of [[A]]p♯ . ′ (†) Let θ, θ′ be two arrows in Lop Σ such that θ ◦ θ is defined. If an ∧-node s has a child t such that (a) the label of t is θ and (b) t has children labeled with B1 , . . . , Bn , then s has also another child t′ such that (a) the label of t′ is θ′ ◦ θ and (b) t′ has children labeled with B1 θ′ , . . . , Bn θ′ . ′ ′ (‡) Let θ, θ′ , σ, σ ′ be four arrows in Lop Σ such that σ ◦ θ = σ ◦ θ . If an ∧-node labeled with A′ has a child t such that (a) the label of t is θ and (b) t has children labeled with B1 , . . . , Bn , then each node labeled with A′ θ′ has a child t′ such that (a) the label of t′ is σ ′ and (b) t′ has children labeled with B1 σ, . . . , Bn σ. Assume that [[A]]p♯ has a synched refutation subtree T whose ∨-nodes are labeled with θ1 , θ3 , . . . , θ2k+1 (where θi is the substitution labeling the ∨-nodes of depth i). We prove that [[A]]p♯ has another synched refutation subtree T ′ whose first ∨-node is labeled with θ = θ2k+1 ◦ θ2k−1 ◦ . . . ◦ θ1 and all the other ∨-nodes are labeled with identities. By assumption, the root r has a child (in T ) that is labeled with θ1 . Assume that its children are labeled with B12 . . . Bn2 2 . By (†), r has another child t′ (in [[A]]p♯ ), that (a) is labeled with θ and (b) has children labeled with B12 σ3 . . . Bn2 σ3 where σ3 = θk+1 ◦θk−1 ◦. . .◦θ3 . These children form depth 2 of T ′ (the root r and t form, respectively, depth 0 and 1). We now build the other depths. For an even i ≤ 2k, let σi+1 denote θ2k+1 ◦θ2k−1 ◦· · ·◦θi+1 and let B1i , . . . , Bni i be the labels of the ∧-nodes of T at depth i. The depth i of T ′ is given by ∧-nodes labeled with B1i σi+1 , . . . , Bni i σi+1 ; the depth i + 1 by ∨-nodes all labeled with idm . It is easy to see that T ′ is a subtree of [[A]]p♯ : by assumption the nodes labeled with B1i , . . . , Bni i have children in T all labeled with θi+1 ; since σi+3 ◦ θi+1 = idm ◦ σi+1 , by property (‡), the nodes labeled with B1i σi+1 , . . . , Bni i σi+1 have children (in [[A]]p♯ ) that (a) are labeled with idm and (b) have children with labels B1i+2 σi+3 , . . . , Bni+2 σ . i+2 i+3 Once we have built T ′ , we can easily conclude. Recall that t′ (the first ∨-node of T ′ ) is labeled with θ. Following the construction at the end of Section 4, the root of [[A]]p♯ θ has a child that is labeled with idm and that has the same children as t′ . Therefore [[A]]p♯ θ has a synched refutation subtree with answer idm . Next we provide a proof of the following statement. Proposition 9.3. Let C and D be categories with an adjunction U ⊣ K for U : C → D b : D → D be two monads such that UT̆ = TU. b Then, and K : D → C. Let T̆ : C → C and T there is a distributive law of monads λ : T̆(KU) ⇒ (KU)T̆ defined for all X ∈ C by b KUX )♭ )♯ (T(id where (·)♭X,Z : C[X, KZ] → D[UX, Z] and (·)♯X,Z : D[UX, Z] → C[X, KZ] are the components of the canonical bijection given by the adjunction U ⊣ K. 46 FILIPPO BONCHI AND FABIO ZANASI For this purpose, we first need to state some auxiliary preliminaries on monads and distributive laws. Given a monad (M, η M , µM ) on a category C, we use Kℓ(M) to denote its Kleisli category and J : C → Kℓ(M) to denote the canonical functor acting as identity on objects and mapping f ∈ C[X, Y ] into ηY ◦ f ∈ Kℓ(KU)[X, Y ]. The lifting of a monad (T, η, µ) in C is a monad (T ′ , η ′ , µ′ ) in Kℓ(M) such that JT = T ′ J and η ′ , µ′ are given on JX ∈ Kℓ(M) (i.e. X ∈ C) respectively as J(ηX ) and J(µX ). The following “folklore” result gives an alternative description of distributive laws in terms of liftings to Kleisli categories, see e.g. [49, §4], [43]. Proposition A.1. Let (M, η M , µM ) be a monad on a category C. For every monad (T, η T , µT ) on C, there is a bijective correspondence between liftings of (T, η T , µT ) to Kℓ(M) and distributive laws of the monad T over the monad M. We are now ready to supply the proof of Proposition 9.3. Proof of Proposition 9.3. By Proposition A.1, it suffices that we define a monad lifting T̆ to Kℓ(KU). For this purpose, we will use the canonical comparison functor H : Kℓ(KU) → D associated with Kℓ(KU) (see e.g. [36, §VI.5]), enjoying the following property: HJ = U. (A.1) Given X, Y ∈ |Kℓ(KU)| and f ∈ Kℓ(KU)[X, Y ], the functor H is defined as H : Kℓ(KU) → D X 7→ UX f X→Y f♭ 7→ UX → UY where f ♭ is given by observing that f in Kℓ(KU) is a morphism f : X → KUY in C and using the bijective correspondence (·)♭X,Z : C[X, KZ] → D[UX, Z]. We have now the ingredients to introduce the monad T̈ on Kℓ(KU) that we will later show to be a lifting of T̆. On objects JX of Kℓ(KU), the functor T̈ : Kℓ(KU) → Kℓ(KU) is defined as JT̆X (note that all objects in Kℓ(KU) are of the shape JX for some X ∈ C). For f f♭ f an arrow JX → JY in Kℓ(KU), (i.e., X → KUY in C), we take UX → UY in D and apply b ♭ b ♭ Tf Tf b b to obtain TUX b → TUY which, by assumption, is UT̆X → UT̆Y . Using the bijective T b ♭ )♯ (Tf correspondence (·)♯X,Z : D[U X, Z] → C[X, KZ], we obtain T̆X → KUT̆Y in C, that is an arrow JT̆X b ♭ )♯ (Tf b ♭ )♯ . The unit of the monad η T̈ is → JT̆Y in Kℓ(KU). We define T̈f as (Tf X Jη T̆ JµT̆ defined as X = JX →X JT̆X = T̈X. The multiplication µT̈X as T̈ T̈X = JT̆ T̆X →X JT̆X = T̈X. One can readily check that (T̈, η T̈ , µT̈ ) is a monad. In order to verify that (T̈, η T̈ , µT̈ ) is a monad lifting of (T̆, η T̆ , µT̆ ), it only remains to check that T̈J = JT̆, since the unit and multiplication are simply defined by applying J to BIALGEBRAIC SEMANTICS FOR LOGIC PROGRAMMING 47 η T̆ and µT̆ . For objects, it follows from the definition of T̈. For arrows f ∈ C[X, Y ], T̈Jf T̈JX → T̈JY = JT̆X = JT̆X b (T(Jf )♭ ) ♯ → JT̆Y (definition of H) → JT̆Y (by A.1) → JT̆X = JT̆X b )♯ (TUf (UT̆f )♯ → JT̆Y (HJT̆f )♯ → (by assumption) JT̆Y (by A.1) JT̆Y (definition of H) JT̆X → JT̆Y. ((·)♯ and (·)♭ form a bijection) JT̆X = (definition of T̈) b (THJf )♯ = = JT̆Y JT̆X ((JT̆f )♭ )♯ → JT̆f = By Proposition A.1, we thus obtain a distributive law of monads λ : T̆(KU) ⇒ (KU)T̆. Following the correspondence in [49], this is effectively constructed for all X ∈ C as follows. Let id KUX be the identity on C and ιX : JKU X → JX be the corresponding arrow in Kℓ(KU). Then T̈(ιX ) : JT̆KUX → JT̆X in Kℓ(KU) corresponds to an arrow T̈(ιX ) : T̆KUX → KUT̆X in C, which is how λ is defined on X. Unfolding the definition of b KUX )♭ )♯ . T̈, this means that λX = (T(id Proposition 10.6. Let P be a logic program and l ∈ LAt(n) a list of atoms. If [[l]]p♯ has a δ refutation subtree with answer θ : n → m, then it has also a refutation subtree T = s1 , s2 , . . . with the same answer where the edge connecting s1 and s2 is labeled with θ and all the other edges in T are labeled with id m . Proof. The proof follows closely the one of Proposition 6.3. First, observe that properties analogous to (†) and (‡) in the proof of Proposition 6.3 hold for ∨-trees, since both saturated ∧∨- and ∨-trees are based on unification. We express them using the rule presentation of p♯δ : τ σ l4 l1 → l2 l5 σ ′ ◦σ l1 → l2 σ ◦ τ = σ′ ◦ τ ′ σ′ l1 → l2 σ ′ l1 τ ′ → l2 σ where lθ is the result of applying θ to each atom in l. Rule (l4) corresponds to (†) and (l5) to (‡). Now suppose that T ′ = t1 , t2 , . . . , tk is a refutation subtree of [[l]]p♯ , say with answer δ θ = θk ◦ . . . ◦ θ2 ◦ θ1 and where ti labeled with a list li . We build T inductively as follows. Depth 0 in T is given by the root t1 labeled with l1 = l. By construction θ1 labels the edge between t1 and t2 in T ′ . Thus, by rule (l4), in [[l]]p♯ the root t1 has also an edge θ targeting δ a child s2 with label θk ◦ . . . ◦ θ2 l2 . We let s2 be the node of depth 1 in T . Inductively, suppose that we built T up to depth i < k. We know that in T ′ the node ti is connected to ti+1 by an edge labeled with θi . Also, by inductive hypothesis the node si in T is labeled with θk ◦ . . . ◦ θi li . Since (θk ◦ . . . ◦ θi+1 ) ◦ θi = id m ◦ (θk ◦ . . . ◦ θi ), then by rule (l5) the node si is connected to a node si+1 in [[l]]p♯ labeled with θk ◦ . . . ◦ θi+1 li+1 via an edge δ labeled with id m . We let si+1 be the node of depth i + 1 in T . It is clear by construction that T is a refutation subtree of [[l]]p♯ with the required properties. δ