Category-based Modularisation for Equational Logic Programming R azvan Diaconescu
Programming Research Group, University of Oxford
Abstract: Although modularisation is basic to modern computing, it has been little studied for logic-based programming. We treat modularisation for equational logic programming using the institution of category-based equational logic in three
dierent ways: (1) to provide a generic satisfaction condition for equational logics; (2) to give a category-based semantics for queries and their solutions; and (3) as an abstract de nition of compilation from one (equational) logic programming language to another. Regarding (2), we study soundness and completeness for equational logic programming queries and their solutions. This can be understood as ordinary soundness and completeness in a suitable \non-logical" institution. Soundness holds for all module imports, but completeness only holds for conservative module imports. Categorybased equational signatures are seen as modules, and morphisms of such signatures as module imports. Regarding (3), completeness corresponds to compiler correctness. The results of this research applies to languages based on a wide class of equational logic systems, including Horn clause logic, with or without equality; all variants of order and many sorted equational logic, including working modulo a set of axioms; constraint logic programming over arbitrary user-de ned data types; and any combination of the above. Most importantly, due to the abstraction level, this research gives the possibility to have semantics and to study modularisation for equational logic programming developed over non-conventional structures.
Contents
1 Introduction
1.1 Modularisation : : : : : : : : : : : : : : : : : : : : : 1.1.1 Some history : : : : : : : : : : : : : : : : : : 1.2 Equational Logic Programming : : : : : : : : : : : : 1.3 Category-based Equational Logic : : : : : : : : : : : 1.3.1 Beyond conventional \abstract model theory" 1.4 The Institution of Category-based Equational Logic :
2 Preliminaries
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
: : : : : :
2.1 Limits and colimits : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2.2 2-categories : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2.3 Categorical Relations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
2 2 3 3 4 5 5
6 6 7 8
Current address: Institute of Mathematics of the Romanian Academy, PO Box 1-764 70700 Bucharest, Romania; e-mail
[email protected]
1
3 Institutions and Modularisation
3.1 Parametric Modules and Views : : : : : : : : : : : : : : : : : : : : : : : :
4 Category-based Equational Logic
4.1 Models and Domains : : : : : : : : : : : 4.2 Examples : : : : : : : : : : : : : : : : : 4.2.1 Many Sorted Algebra : : : : : : : 4.2.2 Order Sorted Algebra : : : : : : : 4.2.3 Horn Clause Logic : : : : : : : : 4.2.4 Equational Logic Modulo Axioms 4.2.5 Constraint Logic : : : : : : : : : 4.2.6 Summary of Examples : : : : : : 4.3 Equations, Queries and Satisfaction : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
: : : : : : : : :
5 The Institution of Category-based Equational Logic
5.1 Many-Sorted Institutions : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5.2 Sentence Translations along Morphisms of Category-based Equational Signatures : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5.2.1 Kleisli translations : : : : : : : : : : : : : : : : : : : : : : : : : : : 5.3 The Satisfaction Condition : : : : : : : : : : : : : : : : : : : : : : : : : : :
8 9
11 12 12 12 13 13 14 14 15 15
18 19
20 21 25
6 Queries, Solutions and Modularisation
26
7 Compilation between ELP languages 8 Conclusions and Future Work
30 31
6.1 The Institution of Queries and Substitutions : : : : : : : : : : : : : : : : : 6.2 Soundness and Completeness for Module Imports : : : : : : : : : : : : : :
26 28
1 Introduction The work reported in this paper is part of the research project on category-based semantics for equational and constraint logic programming carried out in Oxford in connection to the development and implementation of the language Eqlog (see [13]).
1.1 Modularisation
A promising approach to developing large and complex systems (which may be software, hardware, or both) is to start from a description of the system as an interconnection of some speci cation modules. This permits the veri cation of many properties to be carried out at the level of design rather than code, and thus should improve reliability. With suitable mechanical support, it might also improve the eciency of the development process. In addition, it promotes reuse, because some modules may be taken directly from a library, or else may be modi cations of library modules. For this reason, many modern programming and speci cation languages support some form of modularisation, and most mathematical results about modules have appeared in the context of formal software engineering, particularly speci cation languages. 2
1.1.1 Some history
The earliest work on software modules with which we are familiar is by Parnas [36, 37, 38]. Program modules dier from earlier program structuring mechanisms such as subroutines, procedures and blocks, in that they may include a number of procedure and data de nitions, may be parameterized, may import other modules, and may hide certain elements. A major motivation for modules in this sense is to facilitate the modi cation of software, by localizing the representation of data and the operations that depend upon it; this is called information hiding. Such modules support software reuse because they can be speci ed, veri ed, and compiled separately. Note that this notion of module is essentially syntactic : it concerns texts that describe systems. The earliest work that we know on speci cation modules is by Goguen and Burstall, for their speci cation language Clear [5, 6], the semantics of which is based on institutions. This approach to modules has been applied to various logic-based languages, particularly OBJ [27], Eqlog [22], foops [24, 28] (which combines the functional and object paradigms), and fooplog [24] (which combines functional, logic and object paradigms); it could also be applied to any pure logic-based programming language, such as (pure) Lisp and (pure) Prolog. In [15], it is even extended to imperative programming. Clear introduced the ideas that a speci cation module determines a theory, and that such theories can be put together using colimits. Building on Clear, Goguen and Burstall studied laws for horizontal structuring relationships, and vertical implementing (also called \re nement") relationships, concluding that the axioms of a 2-category should be satis ed. In [10] it is shown that all reasonable institutions support certain simple operations on theories; what properties ensure that these operations have various desirable properties is also explored. A new categorical axiomatisation of the notion of inclusion permits simple de nitions for these operations on theories. Much interesting work using institutions has also been done by Tarlecki [44, 45, 46, 47] and by Sannella and Tarlecki [40, 41, 42].
1.2 Equational Logic Programming
Equational logic programming (abreviatted ELP) uni es logic programming based on Horn clause logic and equational programming based on equational logic. The main dierence between ELP and equational programming lies in the fact that the former deals with the problem of solving queries. ELP can also be regarded as joining two major cultures in Computing: algebraic speci cation and logic programming. The query solving capability extends equational programming to a very powerful paradigm in which a speci cation is already a program (or at least is very close to being a program). This approach gives an elegant way to integrate (semantic) equality into logic programming, a major problem in the logic programming tradition, because many of the compromises of the logic programming ideal found in actual languages (e.g., Prolog) have to do with the inability of relational programming to cope with equality. ELP provides as much practical programming power as possible without compromising the underlying logic. Modularisation for ELP has been studied less. Although the approach on modules underlying this paper is that of the Eqlog language which is itself based on work on the speci cation language Clear, we deal with modularisation problems speci c to ELP rather than algebraic speci cation.. Here, we recall some basic concepts and results of this approach on modules at the level of institutions, but will concentrate on developing the 3
abstract structures adequate to the study of the interaction between the module system and (the solutions of) ELP queries. Our work applies not only to Eqlog, but to any ELP language rigorously based on some form of equational logic and whose module system is based on institutions. For example, a basic problem for ELP modularisation is the soundness and completeness of the translation of queries and their solutions along module imports. Given a module import P ! P 0 (technically regarded as a morphism of theories), is sound i for any query q in P , any of its solutions is translated to a solution form of (q ). The completeness of means that any solution of (q ) corresponds to an solution of q . Our notion of module import is not restricted to inclusion of theories, a module import could be any morphism of theories. In this context, we prove the soundness property for arbitrary module imports. The completeness property is proven to hold for the case of (essentially) persistent module imports (i.e., any model of a subtheory can be expanded freely to a model of the supertheory).
1.3 Category-based Equational Logic
The work in this paper is built on top of the framework called category-based equational logic (abbreviated CBEL) introduced in [14, 13]. CBEL abstracts out the essential ingredients of equational logic. Equations, deduction, models (algebras), congruences, satisfaction, etc. are treated in an arbitrary category of models satisfying certain mild conditions, including a forgetful functor to a category of domains. This encodes the principle that a model interprets a signature (often called a \vocabulary" in classical logic) into a domain, usually a set, or for typed systems, a collection of sets. Category-based equational logic programming extends this generalisation to computation, including rewriting, paramodulation, and constraint solving. By viewing terms semantically as elements of a free model rather than as syntactic constructs, the universal quanti ers in equations are generalised to (usually free) models, and valuations are generalised to model morphisms. The theory of CBEL introduces concepts and results at the highest appropriate level of abstraction, and then gradually make them more concrete. For example, the completeness of category-based equational deduction is proved at a very abstract level which applies to Horn clause logic and extensible constraint logic programming just as well as to many and order sorted equational logics. Completeness theorems for particular logics appear by specialising the abstract rules of deduction to exploit more speci c assumptions and reveal the usual syntactic formulations (see [13, 14]). The completeness of category-based equational deduction leads to a generic Herbrand theorem in the style of [23], characterising Herbrand models as initial models of the program regarded as an equational theory (see [13, 14]). When applied to constraint logics [13], this gives a Herbrand theorem for extensible constraint logic programming. This rather sophisticated new result requires minimal eort using the category-based (abbreviatted CB) machinery. As emphasised in [13], CBEL gives the possibility to develop equational logic (programming) over non-conventional structures. In this way, ELP is liberated from the traditional set theoretic point of view. This is similar to the way functional programming and algebraic speci cation got their true meaning and power with cartesian closed categories and institutions, respectively. The development of ELP over dierent type of models and domains (some of them could have a much richer structure than the usual set theoretic domains) 4
might prove very bene cial in terms of unifying ELP with other programming paradigms.
1.3.1 Beyond conventional \abstract model theory"
The approach of this paper is abstract model theory in roughly the same spirit as the \Hungarian School" (e.g., [1]), which means much more than the familiar logical tradition of abstracting Tarskian semantics to extend classical rst order model theory towards other logical systems [3, 2]. The CB framework is closer to the theory of institutions [19] in that: it parameterises over signatures, rather than assuming a xed signature given in advance; it abstracts Tarski's semantic de nition of truth [48] to a (functorial) relation of satisfaction between models and sentences; and it uses category theory to achieve generality and simplicity, for example, by letting models be objects in an arbitrary category. Two dierences between our approach and that of institutions are: our satisfaction is signi cantly less abstract than institutional satisfaction, because it captures the essence of equational logic satisfaction; and our framework does not have any direct formulation of the axiom for institutions that \truth is invariant under change of notation." Nonetheless, in this paper we will show how CBEL naturally gives rise to an institution.
1.4 The Institution of Category-based Equational Logic
The theory of institutions [19] provides an abstract mathematical formulation of the concept of `logical system' very adequate for the study of modularisation in declarative programming languages rigorously based on logical systems. In order to use the machinery provided by the theory of institutions to modularisation problems speci c to ELP, we integrate CBEL with institutions. The institution of CBEL provides the most abstract framework which is still concrete enough to deal with concepts like queries and solutions. In fact, this research promotes the CBEL institution as the mathematical structure underlying the study of modularisation for ELP. In this context: 1. we show that translations of queries along module imports appear as morphisms of Kleisli categories, 2. we show how the more general case of quanti ers as models (rather than collections of variables) reveals that the translations of the quanti ers along module imports are simply free constructions, and 3. we use the CBEL institution for encoding ELP modules as signatures and module imports as morphisms of signatures. This dierent level of use of the CBEL institution is the basis for a CB semantics for ELP queries and solutions. 5
The soundness and completeness problem for translations of queries and their solutions along module imports is shown to be an instance of the soundness and completeness at the level of institutions with an entailment system. This fact resorts to a special and rather eccentric institution having collections of logical variables as signatures, queries as models, and substitutions as sentences. A query satis es a substitution i the substitution is a solution for the query. The only inference rule de ning the entailment relation encodes the translation of substitutions along module imports.
2 Preliminaries This work some familiarity with the basics of universal algebra and category theory, and generally uses the same notation as Mac Lane [32], except that composition is denoted by \;" and written in the diagrammatic order. The application of functions (functors) to arguments may be written either normally using parentheses, or else in diagrammatic order without parentheses. Categories usually have a name with rst letter in capital bbold font; for example the category of sets is Set , and the category of categories is Cat . The opposite of a category C is denoted by Cop ; it has the same class of objects as C, but all arrows are reversed. Functors are usually (but not always!) denoted by caligraphic capital letters, particularly for `functor variables' as opposed to functors whose action is known. The class of objects of a category C is denoted by jCj; also the set of arrows in C having the object a as source and the object b as target is denoted by C(a ; b ).
2.1 Limits and colimits
C C. A cone : d ! C consists of an object A diagram in a category C is a functor J ?!
i d 2 jCj (called the apex) and a jJ j-indexed family of arrows fd ?! C (i )gi 2jJ j such that
j ; C (u ) = i for any u in J . A limit of C is a \minimal" cone : c ! C over C , in the sense that for any other cone : d ! C there exists a unique f : d ! c in C such that f ; = .
j
u
C (j )
C(u)
3D 3 D 3 DD 3 D 3 D 3 3 3 3 3 3 3 Y
b
j
j
f
/
i
C (i )
/
i zz z
List . op append : List List -> List [assoc] . var L : List . eq append(L , empty) = L . eq append(empty , L) = L . endth
The operation append is associative and has the empty list as an identity. In this way, List is a re nement of the theory of monoids. There is a view from MON to List: view list from MON to List is sort Mon to List . op (_*_) to append . op e to empty . endv
The instance LIST*[list] of the generic module LIST* via list is the pushout of MON ,! with list. In this example, the operation # appends all lists from a list of lists. By the previous theorem, LIST*[list] protects List. This fact can be checked directly as well. 2 In this example, LIST*[list] is a simple module expression involving essentially only one instantiation of a generic module. The evaluation of this module expression was obtained as a pushout in the category of theories. In the case of more complicated module expressions4 the evaluation is done by taking the colimit of the corresponding diagram in the category of theories (see [5, 6] or [19] for examples). LIST
4 Category-based Equational Logic In this section we present the essentials of the theory of CBEL that are necessary for this paper. The full development of this theory can be found in [13]. 4 Possibly involving combinations between various kinds of module imports and instantiations of generic
modules via views.
11
4.1 Models and Domains
The semantics of a logical system is given by its models. In general, soundness of the inference rules of a logical system is checked against its models using a satisfaction relation, in the style of Tarski [48]. We assume that models and their morphisms form a category. As in institutions [19], CB equational logics are \localised" to signatures. A model is an interpretation of a particular signature into a domain. Thus any model has an underlying domain, and this correspondence is functorial. Moreover, any two parallel model morphisms identical as maps between their domains should be the same. These assumptions are summed up in the following:
[Basic Framework]: There is an abstract category of models A and a forgetful functor U : A ! X to a category of domains X that is faithful and preserves
pullbacks.
The simplicity of these assumptions re ects the simplicity of equational logic. The condition that U preserves pullbacks relates to congruences being equivalences (see [13, 14]. Note that (A; U ) can be regarded as a concrete category (in the sense of [30]) over the category of domains. In practice, the forgetful functor U always has a left adjoint F , which means that for every X 2 jXj, thought as a domain of variables, there is a model X F , free in the sense that there is a canonical interpretation X : X ! X FU of the variables into the model satisfying the following universal property: for each f : X ! AU interpreting variables in a model A, there is a unique model morphism f ] : X F ! A extending f , in the sense that X ; f ]U = f . X
X C C C C C C
f
/
X FU
v v v v v ] v U v
f
XF
z z z z ] z z
f
AU A Note that U preserves pullbacks when it has a left adjoint (e.g., see [32]). !
z
}
4.2 Examples
This subsection brie y sketches several examples, assuming familiarity with their basic concepts, and showing how they fall under the Basic Framework.
4.2.1 Many Sorted Algebra
This example is probably the best known. Expositions of many sorted algebra (abbreviated MSA) including the standard de nitions for signature, algebra, homomorphism, etc., can be found many places, e.g., [26, 17], and are assumed here. Given a many sorted signature (S ; ), let Alg denote the category of -algebras with -homomorphisms. There is a forgetful functor U : Alg ! Set S from -algebras to S -sorted sets, forgetting the interpretations of the operation symbols in . This functor has a left adjoint. Given a set X of variable symbols, let T(X ) denote the (S -sorted) term algebra with operation symbols from and variable symbols from X ; it is the free -algebra generated by X , in the sense that if v : X ! A is an assignment, i.e., a (many sorted) function to a -algebra 12
A, then there is a unique extension of v to a -homomorphism v ] : T (X ) ! A. We let T denote the initial term -algebra, T (;), recalling that this means there is a unique -homomorphism !A : T ! A for any -algebra A. Call t 2 T a ground -term. Given a ground -term t , let tA denote the element !A(t ) in A. Call A reachable i !A is surjective, i.e., i each element of A is \named" by some ground term.
4.2.2 Order Sorted Algebra
Order sorted algebra (abbreviated OSA) adds to MSA a partial ordering on sorts, which is interpreted as inclusion among the corresponding carriers; all approaches to OSA share this essential idea. See [20] for a recent survey, including all basic OSA de nitions (signature, algebra, homomorphism, regularity, etc.). Given an order sorted signature (S ; ; ), the -algebras and their homomorphisms form a category Alg of models for OSA. The forgetful functor U : Alg ! Set S forgets both the algebraic and the order sorted structure. We emphasise that the domains for OSA should not have an order sorted structure, as is supported by the way OSA is implemented. Other approaches to OSA mentioned in [20] can be treated similarly.
4.2.3 Horn Clause Logic
Theorem 4.1 below describes an embedding of the category of models of a rst order signature as a retract of the category of algebras of an MSA signature obtained from the original rst order signature by turning predicates into operations. Interpreting predicates as boolean valued operations is hardly new; it has even been used to lift narrowing to an operational semantics for logic programming [8]. However, this approach (from [12]; see also [14, 13]) is somewhat dierent, because it does not assume a full boolean structure on the new sort of truth values. Moreover, the model theoretic aspect is emphasised. A rst order signature (S ; ; ) consists of a many sorted signature (S ; ) of function symbols, plus a family = fw j w 2 S g of predicate symbols. Given a rst order signature (S ; ; ), let Mod ; denote the category of (S ; ; )-models and their morphisms.
Theorem 4.1 Given a many sorted rst order signature (S ; ; ), construct an MSA signature (S b ; b [ b ) as follows: S b = S [ fb g, for b 62 S , b[];b = ftg and bw ;s = w ;s for w ; s 2 S S and t a new symbol, and bs1:::sn ;b = fb j 2 s1:::sn g. Then: 1. There is a forgetful functor H; : Algb [b ! Mod; such that for all 2 and all (b [ b )-algebras A, we have a 2 H (A) i Ab (a ) = tA. 2. The functor H; has a left adjoint left inverse E; . 3. There is a \translation" ; of (S ; ; )-Horn clauses to conditional (b [ b )equations that regards every -equation as a b -equation in the canonical way and maps an atom (x ) to the (b [ b )-equation b (x ) = t. ;
13
2
4. For any Horn clause ' and any (b [ b )-algebra A, A j=b [b ; (') i H; (A) j=; ' :
A consequence of this result is that given a rst order signature (S ; ; ), the category of models for Horn clause logic (abbreviated HCL) can be taken as Algb [b instead of Mod;, and its sentences as conditional equations instead of Horn clauses. Notice that in HCL, unlike MSA, the forgetful functor from models to domains, Algb [b ! Set S , is not monadic.
4.2.4 Equational Logic Modulo Axioms
Equational deduction modulo a set of axioms (abbreviated ELM) is needed for rewriting when there are non-orientable equations; detailed expositions are given in [17, 29, 9, 31]. Although in practice non-orientable rules are mostly unconditional, there is no theoretical reason to exclude equational deduction modulo a set of conditional equations. Idempotence is a non-orientable conditional axiom, when given in the form x + y = x if x = y.
De nition 4.2 Given an MSA signature (S ; ) and a set E of -equations, a -term modulo E is an element t of T;E (X ), the quotient of the term algebra T (X ) by E . 2 Equational deduction modulo E generalises the usual concepts of MSA to \concepts modulo E ", including the inference rules [17]. A model theory for equational logic modulo E requires an adequate notion of model, and it is natural to use Alg;E , which gives \algebras modulo axioms" (i.e., all -algebras satifying each axiom in E ). The category of domains is the category Set S of S -sorted sets and functions, and the forgetful functor U;E : Alg;E ! Set S forgets both the axioms and the MSA structure.
Example 4.3 The logic of Mosses's uni ed algebras [35] can be regarded as equational
logic modulo a conditional theory. All uni ed speci cations of a given uni ed signature contain a core of Horn clauses. Uni ed algebras appear as models of this speci cation. 2
4.2.5 Constraint Logic
This example is based on the more elaborated CB semantics for constraint logic programming given in [13]. This treatment of constraint logic (abbreviated CL) as CB equational logic extends Goguen-Meseguer's approach to constraint logic programming developed in the context of the language Eqlog [23]. We sketch here a brief description of the main ideas within the framework of Horn clause logic with equality rather than internalising them into CBEL.5 Let be a [Horn clause logic with equality] signature of \built-ins", and ,! 0 be an extension containing new sorts, new operations and new predicates. Recall the existence of Mod() : Mod(0 ) ! Mod(), the usual reduct functor on models. Let A be a -model of \built-ins". All these determine a constraint logic whose category of models is the 0 S comma category (A#Mod()) and whose category of domains is Set where S 0 is the set of 5 [13]
devotes a whole chapter to the semantics of extensible constraint logic programming within the framework of CBELs.
14
sorts of the signature 0. More explicitly, a model appears as a 0-model B together with an interpretation A ! Mod()B of the built-ins. A
b
/
B
B B B B 0 BB
b
h
B 0 Model morphisms h are simply 0-morphisms which preserve (i.e., commute with) the interpretation of the built-ins. The forgetful functor (A#Mod()) ! Set S 0 from models of constraint logic to domains forgets both the interpretation of the built-ins and the 0-model structure. A constraint identity s = t in variables X , where X is an S 0-sorted set, is formed by a pair of generalised polynomials which are elements of A[X ], i.e., the coproduct between the 0. By following free model T0 (X ) and the free expansion of A along the inclusion ?! the embedding of Horn clause logic into equational logic described in Section 4.2.3, the concept of constraint identity subsumes also the relational constraints. In the case of constraint logics, the de nition of the satisfaction relation betweenc models and sentences follows the general principles of equational satisfaction. Let A ?! C be a model in constraint logic. Because of the universal property of free expansions and coproducts, any valuation h : X ! C extends uniquely to a model morphism [h ]; c ] : A[X ] ! C . The model C satis es the identity s = t with respect to the valuation h i [h ]; c ](s ) = [h ]; c ](t ). !
4.2.6 Summary of Examples
The following summarises the examples discussed above: A (category of models) U forgets: MSA Alg algebraic structure OSA Alg algebraic structure + order sortedness HCL Algb [b algebraic structure + sort b ELM Alg;E algebraic structure + axioms CL (A#Alg ()) comma category structure + algebraic structure Any combination of these logical systems is possible, e.g., order sorted Horn clause logic with equality. The logic underlying Eqlog combines all these systems, plus constraint logic programming.
4.3 Equations, Queries and Satisfaction
Equations are traditionally pairs of terms constructed from the symbols of a signature plus some variables. Goguen and Meseguer [21] rst made quanti ers part of the concept of equation, for MSA. Although terms are syntactic constructs, from a model theoretic perspective they are just elements of the free term model over the set of quanti ed variables. Any valuation of the variables into a model extends uniquely to a model morphism evaluating both sides of the equation. Thus, a more semantic treatment of quanti cation regards quanti ers as models rather than sets of variables, and regards valuations as model morphisms rather than functions; this was already done in [7] for MSA. This non-trivial 15
generalisation of equation and satisfaction extends naturally to equational deduction, and in our opinion gives a pleasing unity and generality to the whole area. hs ;t i De nition 4.4 Let A be any model. Then a U -identity on A is a binary relation k ?! AU on the underlying domain of A. An identity hs ; t i in A is satis ed in a model B with respect to a model morphism h : A ! B i s ; h U = t ; h U . This is denoted B j= hs ; t i[h ]. A U -equation is a universally quanti ed expression (8A)hs ; t i where A is a model representing the quanti er and hs ; t i is an identity in A. A model B satis es (8A)hs ; t i i B satis es the identity hs ; t i for all model morphisms h : A ! B . This is written B j= (8A)hs ; t i. A U -query is an existentially quanti ed expression (9A)hs ; t i where A is a model representing the quanti er and hs ; t i is an identity in A. A solution of (9A)hs ; t i in a model B is any model morphism h : A ! B for which hs ; t i is satis ed in B with respect to h . When B is free , h is called an solution form. 2 The notion of U -equation (query) deals with families of equations (queries), rather than
single equations (queries), as sentences. This agrees with Rodenburg's work [39] showing that equational logic with conjunction satis es the Craig Interpolation Property6 whereas the usual formaulation does not. Our terminology is in uenced by Lassez who replaced the traditional logic programming terminology of computed answer substitution by that of solved form [33]. The modern terminology has the advantage to allow more exibility for the representations of solutions (i.e., staying away of the traditional representations of solution forms as substitutions is very bene cial at the level of operational semantics) and is also more intuitive (i.e., solutions in dierent models can be obtained by interpreting the solutions forms).
Example 4.5 OSA equations Let (S ; ; ) be a coherent (i.e., regular and locally ltered) order sorted signature and let X be an S -sorted set of variables. The collection of all wellformed -terms over X , denoted T (X ), has a canonical structure as an order sorted -algebra. An order sorted equation (8X )t1 =s t2 is an universally quanti ed pair of terms having the same sort (i.e., t1; t2 2 (T (X ))s ). Any parallel pair of many sorted functions k ! T (X ) de nes a many sorted family of such equations. Given an order sorted -algebra A, any valuation v : X ! A of variables X into A extends uniquely to an order sorted -morphism v ] : T (X ) ! A giving the denotations in A for the terms in T (X ). A satis es the identity t1 =s t2 with respect to the valuation v i t1 and t2 have the same denotation, i.e., v ] (t1 ) = v ](t2 ). When dealing with a many t1 ;t2 i sorted family of equations k h?! T (X ), the satisfaction of ht1; t2i by A with respect to ] the valuation v means t1; v U = t2; v ]U . It appears that this de nition of order sorted equations is more restrictive than the one given by Goguen and Meseguer [25]. However, the two can be shown to agree. In [25], an order sorted equation (8X )t1 = t2 is a universally quanti ed pair of terms having the least sorts LS (t1) and LS (t2) in the same connected component. An order sorted algebra ] ] satis es t1 = t2 with respect to the valuation v i vLS(t 1 ) (t1) = vLS(t2 ) (t2 ). Let's consider w a common supersort of both LS (t1) and LS (t2). Then for any order sorted algebra A and any valuation h : X ! A, we have A j= t1 = t2[h ] i A j= t1 =w t2[h ]. 6 The Craig Interpolation Property
is an important semantic property for logical systems [10].
16
This de nition of order sorted equations also holds without assuming coherence of the signature by using annotated terms (or parse trees). 2
Example 4.6 Let be an algebraic signature and and let E be a collection of -equations. An equation modulo E [17], denoted (8X )t1 =E t2, is a universally quanti ed pair of
elements in T;E (X ) (i.e., t1 and t2 are terms modulo E ). Any parallel pair of functions k ! T;E (X ) de nes a family of such equations. A (; E )-algebra satis es t1 =E t2 for the valuation v : X ! A i v ](t1) = v ] (t2), where v ] is the unique extension of v to a -homomorphism T;E (X ) ! A. 2
De nition 4.7 (8A)hs 0; t 0i if hs ; t i is a U -conditional equation quanti ed by the model A, where hs ; t i are the hypotheses of the conditional equation. A model B satis es (8A)hs 0; t 0i if hs ; t i i for any morphism h : A ! B , s ; h U = t ; h U implies s 0; h U = t 0; h U . 2
[14, 13] develops a proof theory for CBEL. This approach to the completeness of CB equational deduction is traditional in that the central concept is the congruence determined by a set ? of (possibly conditional) equations on a model A (e.g., see [4]). The most abstract completeness result states the equivalence of two versions of this congruence: all unconditional equations quanti ed by A that can be syntactically inferred from ?; and all unconditional equations quanti ed by A that are semantic consequences of ?. Our semantic treatment of equation and satisfaction allows the congruences determined by ? on free models and on other models to be treated the same way. Under some additional conditions related to the niteness of the hypotheses of the conditions in ? and to the niteness of the model operations (both of them encoded in category-theoretic terms), this congruence can be obtained in an eective way. Despite the generality and abstraction, the rules of inference for CB equational deduction can be made explicit for concrete examples, and can be recognised even in the most abstract formulation. A relevant consequence of the completeness results for CB equational deduction is a generic Herbrand's Theorem (in two versions) formulated in the style of [23], i.e., characterising Herbrand models as initial models of the program regarded as an equational theory. This provides mathematical foundations for the ELP paradigm in the style of Eqlog [22, 23]. When applied to constraint logics [13], this gives a version of Herbrand's Theorem for extensible constraint logic programming. Despite the sophistication of this last result, it is obtained with minimal eort due to the CB machinery. CBEL yields a semantic approach to completeness of the operational semantics of various programming paradigms, independent of the logic involved, as long as it is equational in our very broad sense. Rewriting is de ned on algebraic entities more abstract than terms by abstracting the properties of contexts from the standard cases, and paramodulation is generalised to semantic paramodulation, de ned by an inference rule over an arbitrary xed model (see [13] or [11] for the CB approach to semantic paramodulation). This contrasts with the usual combinatorial paramodulation-based operational approaches. This semantic perspective motivates paramodulation modulo a model morphism, which we use to show that computing in the quotient model of a theory is the same as computing modulo that theory. 17
5 The Institution of Category-based Equational Logic To get an institution for CBEL, we need to de ne signature morphisms for CBEL, and de ne how models and sentences translate along signature morphisms; in particular, we need to know how quanti ers translate along signature morphisms. Then we must check that the satisfaction relation between CBEL models and the sentences in De nition 4.4 satis es the satisfaction condition. It will help to rst look closer at the MSA case: De nition 5.1 An MSA signature morphism ' : (S ; ) ! (S 0; 0) is a pair hf ; g i, where f : S ! S 0 is a map on sorts and g is an S S -indexed family of maps gu ;s : u ;s ! 0f (u);f (s) on operation symbols. 2 Any MSA signature morphism 0' determines a forgetful functor Alg (') : Alg0 ! Alg on models and another Set f : Set S ! Set S on domains, such that
Alg0
U0
Set S 0 /
Alg(')
Set f
Alg U Set S commutes, where U and U 0 are the corresponding forgetful functors from many sorted algebras to many sorted sets. Each forgetful functor has a left adjoint, so that any model has a free extension along a signature morphism7. Forgetting model structure rst along a signature morphism and then to domains is the same as forgetting rst to domains and then along domain structure. These ideas are formalised by the following: De nition 5.2 A CB equational signature is a functor U : A ! X, and A morphism of CB equational signatures is a pair of functors hM; Di : U ! U 0 such that M; U = U 0; D and D has a left adjoint. 2
/
A0
U0
X0 /
M
D
A U X Notice (see De nition 3.3) that a morphism of CB equational signatures is liberal i M has a left adjoint. The following shows the analogy of concepts in MSA and CBEL:
/
MSA signature (S ; ) S ' = hf ; g i f g Set f Alg (') -equation
CBEL functor U : A ! X X A
hM; Di D M D M U -equation
7 While
free extensions along theory morphisms are problematic in many logical systems, most still support free extensions along signature morphisms, including rst order logic.
18
5.1 Many-Sorted Institutions
This subsection introduces a class of institutions where the notion of sort is made explicit in signatures. Then the category of domains for a theory is in fact the category of models for the simple signature having just the sorts of the signature of the theory. Assuming some liberality of the institution, the forgetful functor from the models of the theory to domains has a left adjoint. Any such institution can be regarded as a fragment of the CBEL institution in a natural way. This gives us a generic way to show that MSA, OSA, HCL, ELM etc. are CBELs.
De nition 5.3 A many sorted institution is a tuple of the form = = (Sign ; S ort ; Mod; Sen ; j= ) such that (Sign ; Mod; Sen ; j=) is an institution, S ort : Sign ! Set is a functor having a left adjoint left inverse Q, and = is liberal on signature morphisms. A domain in = is a signature of the form Q(S ) for S an arbitrary set. 2 Now the main result:
Proposition 5.4 Let = = (Sign ; S ort ; Mod; Sen ; j=) be a many sorted institution with " the co-unit of the persistent adjunction Q a S ort : Set * Sign . Then any signature morphism ' : ! 0 determines a liberal morphism of CB equational signatures hMod('); Mod(Q(S ort (')))i : U ! U ; where U = Mod(") is the forgetful functor from the category Mod() of -models to the category Mod(Q(S ort ())) of domains. Proof: Any signature morphism ' : ! 0 induces a translation of sorts 0 S ort (') : S ort ! S ort which determines a domain reduct functor Mod(Q(S ort ')) : Mod(Q(S ort 0 )) ! Mod(Q(S ort )) having a left adjoint Q(S ort ')$ in the virtue of the liberality of the institution = on signature morphisms. Mod(') has a 0
left adjoint by the liberality of '.
Mod(') Mod() Mod(0 ) Mod("0 ) Mod(" ) Mod(Q(S ort ')) Mod(Q(S ort )) Mod(Q(S ort 0 )) o
o
Q(S ort ')$ /
The diagram commutes on right adjoints because of the naturality of ", i.e., "; ' = Q(S ort '); "0 , and by the application of the model functor to this identity. 2 The liberality condition of De nition 5.3 is a very mild condition in practice. Even institutions notorious for not being liberal, like rst order logic, are still liberal on signature morphisms. 19
Corollary 5.5 The signature morphisms in MSA, OSA, HCL, ELM are all liberal morphisms of CB equational signatures. Proof: In all cases this holds by the liberality of the respective institution on signature morphisms. A special mention is necessary for ELM. In this institution the signature morphisms are MSA theory morphisms, and we use the liberality of the institution of MSA. 2
5.2 Sentence Translations along Morphisms of Category-based Equational Signatures
Before de ning translations of equations along CB equational signature morphisms, we look again at the many sorted case. Some readers may rst wish to review Kan extensions, e.g., in [32].
Example 5.6 A function f : S ! S 0 translates an S -sorted set X into the S 0-sorted set
X by taking the (pointwise) left Kan extension of f along X : Xs0 =
a
f (s)=s 0
Xs for any sort s 0 2 S 0 : S
f
S0
/
B B B B B B
X
S0
X
Set An MSA signature morphism ' = hf ; g i : (S ; ) ! (S 0; 0) de nes an S -sorted map 'X : T (X ) ! T0 (X )' as follows: X
Set !
X
U (T(X ))
MM MM MM MM MM
j
/
j ]U ='X
U (T0 (X )') First note that X U (T0 (X )') because if x 2 Xs then x 2 Xf(s) and Xf(s) T0 (X )f (s) = (T0 (X )')s ; let j : X ! U (T0 (X )' ) denote this inclusion. Now de ne 'X = j ] , where j ] is the unique extension of j to a -homomorphism T (X ) ! T0 (X )'. Then a -equation (8X )ht1; t2i translates to the 0-equation (8X )h'X (t1); 'X (t2)i. 2 &
Notice that the term algebra T0 (X ) is exactly the free extension of T(X ) along '. From this, we conclude that: Translations of quanti ers are free extensions along signature morphisms.
This also covers quanti ers that are not free models. The translation of equations along signature morphisms in MSA is a particular case of the following: U De nition 5.7 Let hM; Di be a liberal morphism of CB equational signatures (A?! X) ! U 0 0 0 M (A ?!X ). Then the U -equation (8A)hs ; t i translates to the U -equation (8A )hs ; t i, 0
20
I
I
I DD
/
t s
t D s D
AU AU AM MU = AD U 0 D where D denotes the left adjoint of D, M denotes the left adjoint of M, and denote the units of the adjunctions determined by M and D, and s and t denote the unique \extensions" of s ; AU and t ; AU to maps in X0. Similarly, the U -query (9A)hs ; t i translates to the U 0-query (9AM)hs ; t i. 2
/
5.2.1 Kleisli translations
In this paragraph we study the case when the sentences, either equations or queries, are quanti ed by \variables". This corresponds to the existence of \term" models, i.e., the existence of left adjoints to the forgetful functors from models to domains. In this case, the translation described by De nition 5.7 are characterized as a morphism (i.e., functor) of Kleisli categories satisfying a certain universal property. This result together with the Satisfaction Condition for CBEL constitute the technical basis for the development of the CB semantics of ELP queries and their solutions in the context of modularisation in the style of the programming language Eqlog (see Section 6). By using the same notations as in De nition 5.7, further assume that U and U 0 have left adjoints F and, F 0 respectively, with and " and, 0 and "0 respectively, the units and the co-units of the respective adjunctions. Fix a domain x 2 jXj. We may assume that (x F )M = (x D )F 0 in the virtue of the general principle of composition of adjunctions.
Fact 5.8 The diagram of De nition 5.7 de ning the translations of equations and queries
reads as:
k
k t
x FU
2
/
x F U (x F ) /
kDD
t D
M
MU = x D F 0U 0D
Lemma 5.9 There exists a unique natural transformation : D; F ! F 0; M such that 0D = D ; U . Moreover, M" = U 0 ; "0 M and F = F ; D . Proof: The natural transformation is uniquely de ned by the formula 0D = D; U
by using the universal property of the unit . Now, by the triangular laws for adjunctions, we have U 0D; M"U = MU ; M"U = 1M;U , and by the previous formula and the triangular laws for adjunctions we have U 0D; U 0 U ; "0MU = U 00D; "0U 0D = 1U 0 ;D = 1M;U . Then U 0D; M"U = U 0D; U 0 U ; "0MU . By the universal property of the unit , we deduce M" = U 0 ; "0M. x
x /
x
xF
x DD
x D 0 D /
x D D
x FUx /
D
DFU
21
x D F 0U 0 D
x D U x /
D
F 0MU
For the last identity, x x 2 jXj. Then x ; x FU ; x D U = x ; x D D; x D U = x ; x D 0 D = x ; x F U
(by the naturality of ) (by the De nition of ) (as unit of the composite of adjunctions in two dierent ways) By the universal property of x we deduce that x F ; x D = x F .
2
Corollary 5.10 When CB equational signatures have left adjoints, we can de ne the trans-
lation of sentences along morphisms of CB equational signatures that are not necessarily liberal. Proof: By replacing F from Fact 5.8 with F ; D . 2 In order to give the universal characterization of this translation as a morphism of Kleisli categories we have to resort to the (rather sophisticated) theory of monads in 2-categories developed by Street in [43]: De nition 5.11 Let C be a 2-category. S A monad hX ; S i consists of an object X , a 1-cell X ?! X and a pair of 2-cells 1 ?! S, S ; S ?! S (called the unit and the multiplication) satisfying the commutative diagrams S
S
SS
BBB BB BB BB BB B /
o
S
| || || ||| | |||
S
S
SSS
S
SS /
S
SS
S
/
U A monad functor hU ; i : hX ; S i ! hY ; T i consists of a 1-cell X ?! Y and a 2-cell U ; T ?! S ; U satisfying the commutative diagrams
UT O
U
U
U
SU
UT
"
U
/
T
UTT
F F F F F F F
/
SUT
S /
SSU
U
SU
/
A monad functor transformation hU ; i ?! hU 0; 0i is a 2-cell U ?! U 0 satisfying the commutative diagram
UT
T
U 0T /
0
SU S SU 0 The 2-category Mnd (C) has monads as objects, monad functors as 1-cells, and monad functor transformations as 2-cells. 2 De nition 5.12 For any 2-category C, let C denote the 2-category obtained from C by reversing all 1-cells (so that C(x ; y ) = C(y ; x )). Mnd (C) has the monads of C as objects, monad opfunctors of C as 1-cells and monad opfunctor transformations as 2-cells.
/
2
22
Theorem 5.13 (from [43]) In a 2-category C suppose hX ; T i and hX 0; T 0i are monads. Any adjunction H a D : X * X 0 sets up a natural bijection between the monad functors hD ; i : hX 0; T 0i ! hX ; T i and the monad opfunctors hH ; i : hX ; T i ! hX 0; T 0i. 2 Also, any CB equational signature canonically determines a monad. However, CB equational signatures are more general than monads because some adjunctions fail to be monadic. As already mentioned, an important class of examples in this sense is given by the order sorted theories.
De nition 5.14 CB equational signatures form a 2-category EqSig such that objects are CB equational signatures, 1-cells are morphisms of CB equational signatures, and 2-cells h; i : hM; Di ! hM0; D0i are pairs of natural transformations : M ! M0, : D ! D0 such that U = U 0 . 2
Corollary 5.15 There exists a forgetful 2-functor Mnd : EqSig ! Mnd (Cat ) which determines (see Theorem 5.13) a canonical 2-functor Mndop : EqSig ! Mnd (Cat ) mapping
morphisms of CB equational signatures to monad opfunctors. Proof: Mnd maps a CB equational signature U : A ! X to its attached monad hX; T i of Cat , morphisms of equational logics hM; Di to monad functors hD; Ui : hX0; T 0i ! hX; T i ( de ned by Lemma 5.9) and maps 2-cells h; i to monad functor transformations . Straightforward calculations assure the correctness of these de nitions. Mndop maps morphisms of CB equational signatures hM; Di to the monad opfunctors h D ; i : hX; T i ! hX0; T 0i corresponding to the monad functor hD; Ui, where D is the left-adjoint to D. 2 Recall from [32] that any monad hX; T i in Cat determines a Kleisli category XT having the same objects as X but \substitutions" as arrows, i.e., XT (x ; y ) = fh [ j h 2 X(x ; y T )g The composition of arrows in XT is given by h [; h 0[ = (h ; h 0T ; z )[: x h y T h 0T z T T z z T When the monad is determined by a CB signature U , the Kleisli category XT is in fact the substitution system determined by U . In this case, a simple calculation shows that the composition in XT corresponds exactly to the composition of substitutions: /
x
/
h /
y FU O
y
y
/
h 0] U /
h
23
z FU
u :
u u u u 0 u u u
When there is no danger of confusion we identify X(x ; y FU ) with XT (x ; y ) via the bijection [. Following [43], for any 2-category C, there is an \inclusion" 2-functor I ncC : C ! Mnd (C) mapping each object X to the trivial monad hX ; 1i. The well-known construction of the Eilenberg-Moore algebras categories appears as a right 2-adjoint to I ncCat [43]. The following de nition is the basis in [43] for recovering the theory of monadicity in the abstract framework of an arbitrary 2-category C:
De nition 5.16 The 2-category C admits construction of algebras i I ncC has a right 2-adjoint. 2
Theorem 5.17 (from [43]) Cat admits construction of algebras. The left 2-adjoint to I ncC at : Cat ! Mnd (Cat ) is the Kleisli construction, which evaluated at hX; T i is XT and the unit hJT ; !i : hX; T i ! hXT ; 1i is given by JT : X ! XT with xJT = x for any x 2 jXj, and fJT = (f ; x 0)[ for any f 2 X(x ; x 0),
and ! : T ; JT ! JT with x ! = (1x T )[ for any x 2 jXj.
hX; T i hJT ;!i hXT ; 1i
XT
/
JJ JJ JJ h i JJ
K ;
2
hK;1i
K
hY; 1i %
Y
From Theorem 5.17 and Corollary 5.15 we deduce the main result of this paragraph: U Corollary 5.18 For any morphism of CB equational signatures hM; Di : (A ?! X) ! U 0 0 0 (A ?! X ) there exists a unique functor K : XT ! XT such that JT ; K = D ; JT , and (1x T )[K = (x )[, where : T ; D ! D ; JT is the natural transformation part of Mndop hM; Di. 0
0
0
0
Mndop (U ) = hX; T i hJT ;!i hXT ; 1i /
hK;1i
Mndop hM;Di= h D ;i
Mndop (U 0) = hX0; T 0hiJT 0 ;!0i hX0T 0 ; 1i
/
2
By spelling out the two properties of K we get exactly the translation described by the version of De nition 5.7 presented at the beginning of this paragraph (see Fact 5.8).
24
5.3 The Satisfaction Condition
The following result can be regarded as a generic proof of the Satisfaction Condition for equational logical systems.8 The unconditional equational version of this theorem can be extended to conditional equations without any problem. U Theorem 5.19 Let hM; Di be a liberal morphism of CB equational signatures (A ?! U X) ! (A0 ?! X0). Then for any model B 2 jA0j and for any sentence (A)hs ; t i, with 2 f8; 9g, B j=U (AM )hs ; t i i B M j=U (A)hs ; t i 0
0
Proof: The right adjoint M determines a natural bijection A(A; BM) ' A0(AM; B ) h h B such that h = mapping each model morphism A! B M to the model morphism AM ! A; h M. A
G G
A /
AM M
G G G G G
h
For each v : k ! AU , we have: k ; (v ; h U 0)D = k ; v D; h U 0D = v ; AU ; h U 0D = v ; AU ; h MU = v; hU Therefore,
hM
BM #
(by De nition 5.7)
i s ; hU 0 = t ; h U 0 i k ; (s ; h U 0)D = k ; (t ; h U 0)D i s ; h U = t ; h U i B M j=U (8A)hs ; t i A similar argument works for the case of queries. 2 B j=U 0 (8AM)hs ; t i
h for all AM! B h BM for all A!
In the case when the sentences are quanti ed by variables, rather than models, we have the following corollary: U Corollary 5.20 Let hM; Di be a morphism of CB equational signatures (A ?! X) ! U 0 0 0 0 (A ?! X ) such that F and F are left adjoints to U and U , respectively. Then for any model B 2 jA0j and for any sentence (x )hs ; t i, with 2 f8; 9g and x a domain in X, B j=U (x D )hs ; t i i B M j=U (x )hs ; t i 0
0
Proof: By using the last equation Lemma 5.9, the existence of a left adjoint ot M is no
longer necessary. 2
8 All examples in Section 4.2 generate
[equational] institutions by following the same pattern.
25
The fact that EqSig comes naturally equipped with a 2-categorical structure reinforces the argument of Goguen and Burstall [18] that the signatures of any chartable institution form a 2-category. The presentation of the sentence functor as a Kleisli translation projects a new light on the duality between syntax and semantics in category based equational logic: the sentence functor is a model functor when reversing the 1-cells in Cat !
6 Queries, Solutions and Modularisation This section gives a categorical semantics for ELP queries and their solutions in the context of modularisation in the style of OBJ and Eqlog, and discusses the soundness and completeness of module imports. We take the view of [10] that modules are presentations and that module imports are morphisms of presentations. A denotational semantics for ELP is given in [23] based on initial algebra semantics. Due to the presence of logical variables, the denotation of an ELP module is given by an adjunction rather than an initial model, in fact the adjunction determined by the forgetful functor from the category of models of the given module to the category of domains representing the mathematical structure for collections of logical variables. Our approach exploits the fact that CB equational signatures are abstract enough to include ELP modules9. The principle underlying our semantics for ELP queries and their solutions is formulated as follows: Module denotation is abstracted to CB equational signatures with left adjoints.
De nition 6.1 Let P be an ELP module. Then its denotation [ P ] is the forgetful functor [ P ] : Mod(P ) ! Dom(P ) from its models to its domains. 2 For the purpose of this paper it is not necessary to de ne the denotation of a module import P ?! P 0 ; we just let it be a morphism of CB equational signatures [ ] : [ P ] ! [ P 0] .
De nition 6.2 A query for an ELP module P is a [ P ] -query. A solution for a query (9B )ht1; t2i in a P -model A is a morphism h : B ! A such that t1; h [ P ] = t2; h [ P ] . Let P ?! P 0 be a module import. Then the translation of queries along (i.e.,
from P -queries to P 0-queries) is given by translation along the morphism of CB equational signatures [ ] as described in De nition 5.7. We denote this translation by . 2 The interpretation of the satisfaction condition (Theorem 5.19) in this context is that for any P -query q , any module import : P ! P 0, and any P 0-model A, there is a canonical one-to-one correspondence between the solutions of q in A and the solutions of q in AM, where M is the model reduct component of [ ] .
6.1 The Institution of Queries and Substitutions
As known, solutions for queries in ELP can be regarded as uni ers. The next fact is consistent to Goguen's approach on uni ers as co-cones in Kleisli categories as expressed in [16]: 9 This is technically similar to the way ELM is encoded
26
as a CBEL.
Fact 6.3 Let q = (9X )ht1; t2i be a query for the program P whose quanti cation is given by \variables", i.e., X 2 jDom(P )j. A solution form for q is a co-cone for the parallel pair ht1[; t2[i in Dom(P )TP , where TP is the monad determined by the forgetful functor [ P ] : Mod(P ) ! Dom(P ). 2 The relationship between queries and substitutions can be formalised as a Satisfaction Relation in a particular institution in which queries play the r^ole of models and substitutions play the r^ole of sentences. The source of a certain substitution has to match the quanti er of a certain query in the same way the sentences and models of logical systems belong to the same language (i.e., signature). This suggests that the signatures for the institution of queries as models and of substitutions as sentences should be given by collections of logical variables. De nition 6.4 Assume a xed module P . We de ne an institution =P consisting of the following data: Sign = Dom(P )opTP ,10 i.e., signatures are domains and signature morphisms are substitutions, Mod(X ) = f(9X )ht1; t2i j t1; t2 in TP (X )g for each domain X in Dom(P ), where TP is the opmonad determined by the right adjoint forgetful functor [ P ] . Each map f [ in Dom(P )TP (X ; X 0) = Dom(P )TP (X 0 ; X ) determines a reduct functor Mod(f ) : Mod(X 0) ! Mod(X ) such that Mod(f )(q 0 ) = q 0 ; f ] for any query q 0 in Mod(X 0), Sen (X ) = fh; s i j P op! P 0; s is a P 0-substitution of the logical variables X g. Each map f [ in Dom(P )TP (X ; X 0) determines a sentence translation Sen (f ) : Sen (X ) ! Sen (X 0 ) by Sen (f )h; s i = h; f ; s ]i for any P 0-substitution s and any module import , and q j=X h; s i i s is a solution form for the query q .
2
Proposition 6.5 Given any module P , the previous construction =P de nes an institution.
Proof: All we have to prove is the satisfaction condition for the institution =P . Consider a domain map X 0 !f TP (X ), an arbitrary P -query q 0 = (9X 0)ht1 ; t2i, and an arbitrary sentence h; s i 2 Sen (X ). Then q 0 j=X h; f ; s ] i i t1 ; (f ; s ])] = t2; (f ; s ] )] (by De nition 6.4) 0
2
10The opposite of the
i i i
t1 ; f ]; s ] = t2 ; f ] ; s ] (t1; f ]); s ] = (t2; f ]); s ] q 0 ; f ] j=X h; s i
Kleisli category Dom(P )TP .
27
(by Corollary 5.18) (by De nition 6.4)
6.2 Soundness and Completeness for Module Imports
De nition 6.6 Let : P ! P 0 be a module import. Then is sound i for any P -query q and any solution form s for q , s is a solution form for q ; also is complete i for any
P -query q and any solution form s 0 for q there exists a solution form s for q such that s 0 = s . 2
A sound and complete module import P ! P 0 \protects"11 solution forms, in the sense that any P -query has the same solutions in P 0 as in P .
Fact 6.7 The composition of sound [complete] module imports is sound [complete]. 2 There is great similarity between soundness and completeness for module imports and for logical systems. In fact, show that both are instances of a CB formulation of soundness and completeness for institutions.
Proposition 6.8 In the institution =P introduced by De nition 6.4, consider the entailment relation `X (parameterised by signatures, i.e., P -domains)12 de ned by the following
inference rule encoding the translation of solutions forms along imports of P : h1P ; s i P ?! P0 : h; s i Consider an arbitrary P -query q = (9X )ht1; t2i. Let q denote the set of all consequences of q of the form h1P ; s i, i.e., the set of all solution forms for q . Then 1. q `X h; s i implies q j=X h; s i for all s i is sound, and 2. q j=X h; s i implies q `X h; s i for all s i is complete.
Proof: The correctness of the de nition of the entailment relation can be easily veri ed by checking all conditions from the de nition of an entailment system (see [10] or [34]). The proof of this proposition is essentially based on the observation that q `X h; s i means that there exists s0 a P -substitution that is a solution form for q and such that s = s0. The rest is given by De nition 6.6. 2
U U De nition 6.9 A morphism of CB equational signatures hM; Di : (A ?! X) ! (A0 ?! X0) is essentially persistent i it is liberal and the adjunctions corresponding to both M and D are persistent. 0
A module import is essentially persistent i its denotation [ ] is an essentially persistent morphism of CB equational signatures. 2
When domains are many sorted sets, the persistency of the adjunction on domains corresponds exactly to the injectivity on sorts of the module import; this relates the use of persistency for protecting extensions of built-ins in Eqlog [23]. U U Lemma 6.10 Let hM; Di : (A ?! X) ! (A0 ?! X0) be an essentially persistent morphism of CB equational signatures. Consider q a U -query. Then: 0
11Compare this with \protecting imports" in the sense of OBJ [27]. 12See [10, 34] for the de nition of entailment relations in institutions.
28
D embeds X as a full subcategory of X0, and q^ has exactly the same solution forms in X as q , where q^ denotes the U 0-query obtained by translating q along hM; Di. Proof: For any query q and model A denote its solutions in the model A by Sol (q ; A).
The image of D in X0 is a full subcategory as a consequence of the persistency of the adjunction determined by D. Since D is also injective on objects, it embeds X as a full subcategory of X0. For the rest of the proof we identify X with the image of D . Let F and F 0 be left adjoints to U and U 0, respectively. For any y 2 jXj, we have: Sol (q ; y F ) = Sol (q ; (y F )M M) (persistency) = Sol (^q ; (y F )M) (Theorem 5.19, Satisfaction Condition for queries) = Sol (^q ; y F 0) (composition of adjoints) The conclusion of the lemma follows now by applying Corollary 5.20. 2 P 0 be a module import. Then Theorem 6.11 Completeness Let P ?!
1. is sound, and 2. is complete whenever it is essentially persistent.
Proof: Let q be a query in P .
1. Assume s is a solution form for q . Then s [ coequalises q [, where q [ is the parallel pair of arrows in the Kleisli category Dom(P )TP corresponding to the P -query q , and s [ is the arrow in Dom(P )TP corresponding to the substitution s . By Corollary 5.18, [ ] determines a functor K : Dom(P )TP ! Dom(P 0)TP 0 . This means that (s )[ = s [K coequalises (q )[ = q [ K, which means that s is an solution form for q . 2. By applying the previous lemma to the case of the essentially persistent morphism of CB equational signatures [ ] : [ P ] ! [ P 0] . 2 The following examples were actualy run under the Eqlog system:
Example 6.12 Consider the generic module LIST* from Example 3.7. Notice that MON ,! is an essentially persistent module import. The query
LIST
select LIST* . find X Y : Mon such that X * Y = Y * X .
has exactly the same solution forms in MON as in LIST*. 2 The lack of persistency might destroy the completeness of module imports as in the following:
Example 6.13 Consider the following theories:
29
th SOURCE is sorts S1 S2 . op a : -> S1 . op b : -> S2 . op f : S1 -> S2 . endth th TARGET is sort S . op a' : -> S . op b' : -> S . op f : S -> S . eq f(b') = b' . endth
and the following view: view V sort op a op b op f endv
from SOURCE to TARGET is S1 to S . sort S2 to S . to a' . to b' . to f .
The TARGET-query find X : S such that f(X) = b' .
has a solution form (i.e., X:S->b') although the SOURCE-query select SOURCE . find X : S1 such that f(X) = b .
does not have any solution form. 2
7 Compilation between ELP languages In this section we discuss the applications of the CBEL institution to compilation of ELP modules. We consider only ELP languages that are rigorously based on some form of equational logic. This includes Eqlog (based on order sorted Horn clause logic with equality) and even (pure) Prolog (via Section 4.2.3). This assumption is consistent to the ideal of logical programming formulated by Goguen and Meseguer in [23], i.e., any \logical programming" language is based on some institution. In the case of ELP, the institution =(L) underlying a language L should be based on some form of equational logic, therefore, it is natural to assume that =(L) is a part of the CBEL institution. Because in our approach the institutions underlying the programming languages are all part of the same institution, the de nition of the compilation function doesn't use entities 30
external to the CBEL institution, like morphisms of institutions. This simplies signi cantly the study of compilation between ELP languages. Let L1 and L2 be two ELP languages whose underlying institutions are =(L1) = (Sign1; Mod1; Sen1; j=1) and =(L2) = (Sign2; Mod2; Sen2 ; j=2), respectively. (c ;C ) De nition 7.1 A compiler L ?! L2 from the ELP language L1 to the ELP language c 1 L2 consists of a family ( ! C ())2jSign j of morphisms of CB equational signatures such
that
C : Sign1 ! Sign2 is a functor, and c is natural in , i.e.,
c /
C ()
0
C (0)
commutes.
C ()
c0 /
2 (c ;C ) De nition 7.2 Consider a compiler L1 ?! L2. Let P be a module in L1, i.e., P is a
collection of -sentences, where is the signature of P . Then P compiles to c (P ) = fe Kc j e 2 P g, where Kc is the translation of -sentences to C ()-sentences de ned by the morphism of CB equational signatures c .13 2
This de nition can be extended without any diculty to the compilation of queries and substitutions. Notice also that any module import P ! P 0 compiles to a module import C (P ) C!() C (P 0) (see Corollary 5.18). The soundness result for module imports (1. of Theorem 6.11) interprets that any compiled solution is still a solution for the compiled query. The completeness result (2. of Theorem 6.11) gives the correctness of the compiler, in the sense that no new solutions exist besides the compiled ones. (c ;C ) Corollary 7.3 A compiler L1 ?! L2 between ELP languages is correct i c is essentially persistent for each signature in L1. 2
8 Conclusions and Future Work We developed a category-based approach to equational logic programming ELP modularisation within the framework of the CBEL institution. This categorical structure provides a generic satisfaction condition for equational logic systems, a framework for a CB semantics for ELP queries and their solutions, and 13See
Corollary 5.18 or Fact 5.8
31
an abstract de nition for compilation from one (equational) logic programming lan-
guage to another. The CBEL institution emerges as a powerful structure adequate to the mathematical study of ELP modularisation problems. This research applies to languages rigorously based on various equational logic systems, including the ones already used in the design of some languages, but also new ones14. Programming languages based on non-conventional equational logic systems may lead to a clean integration between the ELP and other programming paradigms. Future work on CB modularisation for ELP should exploit the potential of the CBEL institution to deal with other problems speci c to ELP modularisation. One particularly interesting topic is extensible modular constraint logic programming. This programming paradigm deals with combining dierent constraint solvers for dierent data types in a modular fashion. By following the embedding of constraint logic into CBEL introduced by [13], the CBEL institution provides the right framework for dealing with the combination of dierent speci cations in constraint logic. Consequently, the results on queries and their solutions in this paper apply directly to constraint solving. I hope to present these ideas in a forthcoming paper.
Acknowledgments Warmest thanks go to Professor Joseph Goguen who supervised the project on categorybased semantics for equational and constraint logic programming in Oxford, with whom I started working on Modularisation years ago, and who oered me his invaluable friendship during the past years. Many thanks go towards my friend and coleague Hendrik Hilberdink with whom I had numerous interesting discussions on Modularisation on dierent ocassions during the past few years. My friend Joce Paine was permanently dissatis ed with the lack of real module systems in the actual commercial logic programming systems; this dissatisfaction was a source of encouragement for this work.
References
[1] Hajnal Andreka and Istvan Nemeti. A general axiomatizability theorem formulated in terms of coneinjective subcategories. In B. Csakany, E. Fried, and E.T. Schmidt, editors, Universal Algebra, pages 13{35. North-Holland, 1981. Colloquia Mathematics Societas Janos Bolyai, 29. [2] Jon Barwise. Axioms for abstract model theory. Annals of Mathematical Logic, 7:221{265, 1974. [3] Jon Barwise and Solomon Feferman. Model-Theoretic Logics. Springer, 1985. [4] Garrett Birkho. On the structure of abstract algebras. Proceedings of the Cambridge Philosophical Society, 31:433{454, 1935. [5] Rod Burstall and Joseph Goguen. Putting theories together to make speci cations. In Raj Reddy, editor, Proceedings, Fifth International Joint Conference on Arti cial Intelligence, pages 1045{1058. Department of Computer Science, Carnegie-Mellon University, 1977. [6] Rod Burstall and Joseph Goguen. The semantics of Clear, a speci cation language. In Dines Bjorner, editor, Proceedings, 1979 Copenhagen Winter School on Abstract Software Speci cation, pages 292{ 332. Springer, 1980. Lecture Notes in Computer Science, Volume 86; based on unpublished notes 14As long as they can be regarded as CBEL.
32
[7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18]
[19] [20] [21] [22]
handed out at the Symposium on Algebra and Applications, Stefan Banach Center, Warsaw, Poland, 1978. Virgil Cazanescu. Local equational logic. In Zoltan Esik, editor, Proceedings, 9th International Conference on Fundamentals of Computation Theory FCT'93, pages 162{170. Springer-Verlag, 1993. Lecture Notes in Computer Science, Volume 710. Nachum Dershowitz. Computing with rewrite rules. Technical Report ATR-83(8478)-1, The Aerospace Corp., 1983. Nachum Dershowitz and Jean-Pierre Jouannaud. Rewriting systems. In Jan van Leeuwen, editor, Handbook of Theoretical Computer Science, Volume B: Formal Methods and Semantics, pages 243{ 320. North Holland, 1990. Razvan Diaconescu, Joseph Goguen, and Petros Stefaneas. Logical support for modularisation. In Gerard Huet and Gordon Plotkin, editors, Logical Environments, pages 83{130. Cambridge, 1993. Proceedings of a Workshop held in Edinburgh, Scotland, May 1991. Razvan Diaconescu. Completeness of semantic paramodulation: a category-based approach. International Journal of Foundations of Computer Science. To appear. Razvan Diaconescu. The logic of Horn clauses is equational. Technical Report PRG-TR-3-93, Programming Research Group, University of Oxford, 1990. Razvan Diaconescu. Category-based Semantics for Equational and Constraint Logic Programming. PhD thesis, University of Oxford, 1994. Razvan Diaconescu. Completeness of category-based equational deduction. Mathematical Structures in Computer Science, to appear 1995. Joseph Goguen. Reusing and interconnecting software components. Computer, 19(2):16{28, February 1986. Reprinted in Tutorial: Software Reusability, Peter Freeman, editor, IEEE Computer Society, 1987, pages 251{263, and in Domain Analysis and Software Systems Modelling , Ruben Prieto-Daz and Guillermo Arango, editors, IEEE Computer Society, 1991, pages 125{137. Joseph Goguen. What is uni cation? A categorical view of substitution, equation and solution. In Maurice Nivat and Hassan At-Kaci, editors, Resolution of Equations in Algebraic Structures, Volume 1: Algebraic Techniques, pages 217{261. Academic, 1989. Also Report SRI-CSL-88-2R2, SRI International, Computer Science Lab, August 1988. Joseph Goguen. Theorem Proving and Algebra. MIT, to appear 1995. Joseph Goguen and Rod Burstall. A study in the foundations of programming methodology: Speci cations, institutions, charters and parchments. In David Pitt, Samson Abramsky, Axel Poigne, and David Rydeheard, editors, Proceedings, Conference on Category Theory and Computer Programming, pages 313{333. Springer, 1986. Lecture Notes in Computer Science, Volume 240; also, Report CSLI-86-54, Center for the Study of Language and Information, Stanford University, June 1986. Joseph Goguen and Rod Burstall. Institutions: Abstract model theory for speci cation and programming. Journal of the Association for Computing Machinery, 39(1):95{146, January 1992. Draft appears as Report ECS-LFCS-90-106, Computer Science Department, University of Edinburgh, January 1990. Joseph Goguen and Razvan Diaconescu. An Oxford survey of order sorted algebra. Mathematical Structures in Computer Science, 4(4):363{392, 1994. Joseph Goguen and Jose Meseguer. Completeness of many-sorted equational logic. Houston Journal of Mathematics, 11(3):307{334, 1985. Preliminary versions have appeared in: SIGPLAN Notices, July 1981, Volume 16, Number 7, pages 24{37; SRI Computer Science Lab, Report CSL-135, May 1982. Joseph Goguen and Jose Meseguer. Eqlog: Equality, types, and generic modules for logic programming. In Douglas DeGroot and Gary Lindstrom, editors, Logic Programming: Functions, Relations and Equations, pages 295{363. Prentice-Hall, 1986. An earlier version appears in Journal of Logic Programming, Volume 1, Number 2, pages 179{210, September 1984.
33
[23] Joseph Goguen and Jose Meseguer. Models and equality for logical programming. In Hartmut Ehrig, Giorgio Levi, Robert Kowalski, and Ugo Montanari, editors, Proceedings, 1987 TAPSOFT, pages 1{22. Springer, 1987. Lecture Notes in Computer Science, Volume 250. [24] Joseph Goguen and Jose Meseguer. Unifying functional, object-oriented and relational programming, with logical semantics. In Bruce Shriver and Peter Wegner, editors, Research Directions in ObjectOriented Programming, pages 417{477. MIT, 1987. Preliminary version in SIGPLAN Notices, Volume 21, Number 10, pages 153{162, October 1986. [25] Joseph Goguen and Jose Meseguer. Order-sorted algebra I: Equational deduction for multiple inheritance, overloading, exceptions and partial operations. Theoretical Computer Science, 105(2):217{273, 1992. Also, Programming Research Group Technical Monograph PRG{80, Oxford University, December 1989. [26] Joseph Goguen, James Thatcher, and Eric Wagner. An initial algebra approach to the speci cation, correctness and implementation of abstract data types. Technical Report RC 6487, IBM T.J. Watson Research Center, October 1976. In Current Trends in Programming Methodology, IV, Raymond Yeh, editor, Prentice-Hall, 1978, pages 80{149. [27] Joseph Goguen, Timothy Winkler, Jose Meseguer, Kokichi Futatsugi, and Jean-Pierre Jouannaud. Introducing OBJ. In Joseph Goguen, editor, Algebraic Speci cation with OBJ: An Introduction with Case Studies. Cambridge, to appear 1995. Also to appear as Technical Report from SRI International. [28] Joseph Goguen and David Wolfram. On types and FOOPS. In Robert Meersman, William Kent, and Samit Khosla, editors, Object Oriented Databases: Analysis, Design and Construction, pages 1{22. North Holland, 1991. Proceedings, IFIP TC2 Conference, Windermere, UK, 2{6 July 1990. [29] Gerard Huet and Derek Oppen. Equations and rewrite rules: A survey. In Ron Book, editor, Formal Language Theory: Perspectives and Open Problems, pages 349{405. Academic, 1980. [30] Horts Herrlich Jiri Adamek and George Strecker. Abstract and Concrete Categories. John Wiley, 1990. [31] Jan Willem Klop. Term rewriting systems: from Church-Rosser to Knuth-Bendix and beyond. In Samson Abramsky, Dov Gabbay, and Tom Maibaum, editors, Handbook of Logic in Computer Science. Oxford, 1992. [32] Saunders Mac Lane. Categories for the Working Mathematician. Springer, 1971. [33] Jean-Louis Lassez, Michael Maher, and Kimbal Marriott. Uni cation revisited. In Jack Minker, editor, Foundations of Deductive Databases and Logic Programming, pages 587{625. Morgan Kaufmann, 1988. [34] Jose Meseguer. General logics. In H.-D. Ebbinghaus et al., editors, Proceedings, Logic Colloquium, 1987, pages 275{329. North-Holland, 1989. [35] Peter Mosses. Uni ed algebras and institutions. In Proceedings, Fourth Annual Conference on Logic in Computer Science, pages 304{312. IEEE, 1989. [36] David Parnas. Information distribution aspects of design methodology. Information Processing '72, 71:339{344, 1972. Proceedings of 1972 IFIP Congress. [37] David Parnas. On the criteria to be used in decomposing systems into modules. Communications of the Association for Computing Machinery, 15:1053{1058, 1972. [38] David Parnas. A technique for software module speci cation. Communications of the Association for Computing Machinery, 15:330{336, 1972. [39] Pieter-Hendrik Rodenburg. A simple algebraic proof of the equational interpolation theorem. Algebra Universalis, 28:48{51, 1991. [40] Donald Sannella and Andrzej Tarlecki. Extended ML: an institution independent framework for formal program development. In David Pitt, Samson Abramsky, Axel Poigne, and David Rydeheard, editors, Proceedings, Summer Workshop on Category Theory and Computer Programming, pages 364{389. Springer, 1986. Lecture Notes in Computer Science, Volume 240.
34
[41] Donald Sannella and Andrzej Tarlecki. On observational equivalence and algebraic speci cation. Journal of Computer and System Science, 34:150{178, 1987. Earlier version in Proceedings, Colloquium on Trees in Algebra and Programming, Lecture Notes in Computer Science, Volume 185, Springer, 1985. [42] Donald Sannella and Andrzej Tarlecki. Speci cations in an arbitrary institution. Information and Control, 76:165{210, 1988. Earlier version in Proceedings, International Symposium on the Semantics of Data Types, Lecture Notes in Computer Science, Volume 173, Springer, 1985. [43] Ross Street. The formal theory of monads. Jour. of Pure and Applied Algebra, (2):149{169, 1972. [44] Andrzej Tarlecki. Free constructions in algebraic institutions. In M.P. Chytil and V. Koubek, editors, Proceedings, International Symposium on Mathematical Foundations of Computer Science, pages 526{ 534. Springer, 1984. Lecture Notes in Computer Science, Volume 176; extended version, University of Edinburgh, Computer Science Department, Report CSR-149-83. [45] Andrzej Tarlecki. Bits and pieces of the theory of institutions. In David Pitt, Samson Abramsky, Axel Poigne, and David Rydeheard, editors, Proceedings, Summer Workshop on Category Theory and Computer Programming, pages 334{360. Springer, 1986. Lecture Notes in Computer Science, Volume 240. [46] Andrzej Tarlecki. On the existence of free models in abstract algebraic institutions. Theoretical Computer Science, 37:269{304, 1986. Preliminary version, University of Edinburgh, Computer Science Department, Report CSR-165-84, 1984. [47] Andrzej Tarlecki. Quasi-varieties in abstract algebraic institutions. Journal of Computer and System Sciences, 33(3):333{360, 1986. Original version, University of Edinburgh, Report CSR-173-84. [48] Alfred Tarski. The semantic conception of truth. Philos. Phenomenological Research, 4:13{47, 1944.
35