Modules over relative monads for syntax and semantics
We give an algebraic characterization of the syntax and semantics of a class of languages with variable binding. We introduce a notion of 2-signature: such a signature specifies not only the terms of a language, but also reduction rules on those te…
Authors: Benedikt Ahrens
MODULES O VER RELA TIVE MONADS F OR SYNT AX AND SEMANTICS BENEDIKT AHRENS Abstract. W e give an algebraic c haracterization of the syntax and seman tics of a class of languages with v ariable binding. W e introduce a notion of 2–signatur e : such a signature specifies not only the terms of a language, but also r e duction rules on those terms. T o any 2–signature S we asso ciate a category of “mo dels” of S . This category has an initial ob ject, whic h integrates the terms freely generated b y S , and which is equipped with reductions according to the inequations giv en in S . W e call this initial object the language gener ated by S . Mo dels of a 2–signature are built from r elative monads and mo dules ov er suc h monads. Through the use of monads, the models — and in particular, the initial mo del — come equipped with a substitution op er ation that is compatible with reduction in a suitable sense. The initiality theorem is for malized in the pro of assistan t Coq , yielding a mac hin- ery which, when fed with a 2–signature, provides the asso ciated programming language with reduction relation and certified substitution. Contents 1. In tro duction 2 1.1. Summary 2 1.2. Related W ork 4 1.3. Synopsis 5 2. Relativ e Monads & Mo dules 5 2.1. Mo dules ov er Relative Monads 5 2.2. Constructions on Monads and Mo dules 9 2.3. Deriv ed Modules 11 3. 2–Signatures and their Represen tations 12 3.1. Arities, 1–Signatures and their Representations 13 3.2. Inequations ov er 1–Signatures 15 3.3. Initialit y for 2–Signatures 17 3.4. Some Remarks 20 4. F ormalization in the pro of assistant Co q 21 4.1. Arities as Lists 21 4.2. Represen tations 22 4.3. Morphisms of Representations 23 4.4. Category of Representations 24 4.5. Initialit y without Inequations 25 4.6. Inequations and Initial Represen tation of a 2–Signature 27 4.7. Λ β : Lam b da Calculus with β reduction 29 5. Conclusions & F uture W ork 31 1 2 BENEDIKT AHRENS References 32 1. Introduction W e giv e an algebraic c haracterization, via a univ ersal prop erty , of the program- ming language generated b y a signatur e . More precisely , we define a notion of 2–signatur e whic h allows the sp ecification of the syntax of a programming language — via a 1–signature, say , S — as well as its semantics in form of reduction rules, sp ecified through a set A of inequations ov er S . T o an y 1–signature S w e associate a category of mo dels of S . Giv en a 2–signature ( S, A ) , the inequations of A giv e rise to a satisfaction predicate on the models of S , and thus sp ecify a full sub category of mo dels of S whic h satisfy the inequations of A . W e call this subcategory the c ate gory of mo dels of ( S, A ) . Our main theorem states that this category has an initial ob ject — the programming language asso ciated to ( S, A ) —, which integrates the terms generated b y S , equipp ed with the reduction relation generated by the inequations of A . The theorem has b een fully certified in the pro of assistant Co q (Co q 2010 ). The Co q theory files as well as do cumentation are a v ailable online at http://math.unice.fr/lab o ratoire/logiciels . 1.1. Summary. W e define a notion of 2–signature in order to sp ecify the terms and reduction rules of functional programming languages. Giv en any 2–signature, w e c haracterize its asso ciated programming language as initial ob ject in some category , thus giving an algebraic definition of abstract syn tax with semantics. This c haracterization of syntax with reduction rules is giv en in tw o steps: 1. A t first pur e syntax is c haracterized as initial ob ject in some category . Here w e use the term “pure” to express the fact that no semantic aspects such as reductions on terms are considered. This characterization is actually a consequence of a result by Hirscho witz and Maggesi ( 2007a ) (cf. Sec. 1.1.1 ). 2. Afterw ards we consider ine quations sp ecifying r e duction rules . Given a set of reduction rules for terms, we build up on the preceding result to giv e an algebraic characterization of syntax with r e duction . In summary , the merit of this w ork is to give an algebraic characterization of syntax with r e duction rules , building up on such a characterization for pur e syntax giv en b y Hirsc ho witz and Maggesi ( 2007a ). Our approach is based on relative monads (Altenkirch et al. 2010 ) from the category Set of sets to the category Pre of preorders and mo dules ov er such monads. Compared to traditional monads, relative monads allo w for different categories as domain and co domain. W e no w explain the ab o v e tw o p oints in more detail: 1.1.1. Pur e Syntax. An arity sp ecifies the type of a term constructor: it is given by a list of natural n um b ers, the length of which sp ecifies the num ber of argumen ts of the constructor. The list entries sp ecify the num b er of v ariables that are b ound in the corresp onding argument. A 1–signature is a family of arities. T o any 1–signature S w e asso ciate a category Rep ∆ ( S ) of r epr esentations — or “mo dels” — of S , where a mo del of S is built from a relative monad on the functor ∆ : Set → Pre . MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 3 This category has an initial ob ject (cf. Lem. 43 ), which integrates the terms freely generated by the signature. W e call this ob ject the (pur e) syntax asso ciate d to S . As men tioned abov e, we use the term “pure” to distinguish this initial ob ject from the initial object asso ciated to a 2–signatur e , whic h giv es an analogous c haracterization of syntax with r e duction rules (cf. b elow). Initialit y for pure syntax is actually a consequence of a related initiality theorem pro ved by Hirscho witz and Maggesi ( 2007a ): in that work, the authors asso ciate, to an y signature S , a category Rep ( S ) of mo dels of S , where a mo del is built from a monad ov er Set . W e connect the corresp onding categories by exhibiting a pair of adjoin t functors (cf. Lem. 42 ) b et w een our category Rep ∆ ( S ) of representations of S and that of Hirscho witz and Maggesi, Rep ( S ) ∆ ∗ ( ( U ∗ h h ⊥ Rep ∆ ( S ) . W e thus obtain an initial ob ject in our category Rep ∆ ( S ) using the fact that left adjoin ts are cocontinuous: the image under the functor ∆ ∗ : Rep ( S ) → Rep ∆ ( S ) of the initial ob ject in the category Rep ( S ) is initial in Rep ∆ ( S ) . 1.1.2. Syntax with R e duction Rules. Giv en a 1–signature S , an S –ine quation E = ( α, γ ) associates a pair ( α R , γ R ) of p ar al lel morphisms in a suitable category to any represen tation R of S . In a sense made precise later, we can ask whether α R ≤ γ R , due to our use of relative monads into the category Pre of preorders. If this is the case, we say that R satisfies the inequation E . A 2–signatur e is a pair ( S, A ) consisting of a 1–signature S , which sp ecifies the terms of a language, together with a set A of S –ine quations , which sp ecifies reduction rules on those terms. Given a 2–signature ( S, A ) , we call r epr esentation of ( S, A ) any representation of S that satisfies each inequation of A . The c ate gory of r epr esentations of ( S, A ) is defined to be the full subcategory of represen tations of S whose objects are represen tations of ( S, A ) . W e w ould like to exhibit an initial ob ject in the category of represen tations of ( S, A ) , and th us must rule out inequations which are never satisfied. W e call classic S –inequation any S –inequation whose co domain is of a particular form. Our main result states that for any set A of classic S –inequations the category of represen tations of ( S, A ) has an initial object. The class of classic inequations is large enough to accoun t for the fundamental reduction rules; in particular, b eta and eta reductions are giv en by classic inequations. Our definitions ensure that any reduction rule b etw een terms that is expressed b y an inequation E ∈ A is automatically propagated into subterms. The set A of inequations hence only needs to contain some “generating” inequations, a fact that is well illustrated b y our example 2–signature Λ β of the unt yp ed lambda calculus with b eta reduction. This signature has only one inequation β whic h expresses b eta reduction at the root of a term, λx.M ( N ) M [ x := N ] . 4 BENEDIKT AHRENS The initial representation of Λ β is giv en by the unt yped lambda calculus, equipped with the reflexive and transitiv e beta reduction relation β as presented by Baren- dregt and Barendsen ( 1994 ). 1.2. Related W ork. Initial Semantics results for syntax with v ariable binding were first presented on the LICS’99 conference. Those results are concerned only with the syntactic asp e ct of languages: they characterize the set of terms of a language as an initial ob ject in some category , while not taking into accoun t reductions on terms. In lac k of a better name, we refer to this kind of initiality results as pur ely syntactic . Some of these initiality theorems hav e b een extended to also incorp orate semantic asp ects, e.g., in form of equiv alence relations b etw een terms. These extensions are review ed in the second paragraph. Purely syntactic results. Initial Seman tics for “pure” syntax — i.e. without consid- ering semantic asp ects — with v ariable binding were presented by several p eople indep enden tly , differing in the mo delling of v ariable binding: The nominal appr o ach b y Gabba y and Pitts ( 1999 ) (see also Gabbay and Pitts 2001 ; Pitts 2003 ) uses a set theory enriched with atoms to establish an initiality result. Their approac h mo dels lambda abstraction as a constructor whic h takes a pair of a v ariable name and a term as arguments. In contrast to the other techniques men tioned in this list, in the nominal approach syntactic equalit y is different from α –equiv alence. Hofmann ( 1999 ) pro ves an initiality result mo delling v ariable binding in a Higher–Order Abstract Syn tax (HOAS) style. Fiore et al. ( 1999 ) (also Fiore 2002 ; Fiore 2005 ) mo del v ariable binding through nested datatypes as introduced by Bird and Meertens ( 1998 ). Fiore et al.’s approac h is extended to simply–typ e d syntax b y Miculan and Scagnetto ( 2003 ). T anaka and Po wer ( 2005 ) generalize and subsume those three approaches to a general category of contexts. An ov erview of this work and references to more tec hnical pap ers is given by Po w er ( 2007 ). Hirscho witz and Maggesi ( 2007a ) pro v e an initialit y result for unt yp ed syntax based on the notion of mo dule over a monad . Their w ork has b een extended to simply–t yped syntax by Zsidó ( 2010 ). Incorp orating Seman tics. Rewriting in nominal settings has b een examined by F ernández and Gabbay ( 2007 ). Ghani and Lüth ( 2003 ) present rewriting for algebraic theories without v ariable binding; they characterize equational theories (with a symmetry rule) resp. rewrite systems (with r eflexivity and tr ansitivity rule, but without symmetry ) as c o e qualizers resp. c oinserters in a category of monads on the categories Set resp. Pre . Fiore and Hur ( 2007 ) hav e extended Fiore’s work to in tegrate seman tic aspects in to initialit y results. In particular, Hur’s thesis (Hur 2010 ) is dedicated to e quational systems for syntax with v ariable binding. In a “F urther research” section (Hur 2010 , Chap. 9.3), Hur suggests the use of preorders, or more generally , arbitrary relations to mo del in equational systems. Hirsc howitz and Maggesi ( 2007a ) prov e initiality of the set of lambda terms mo dulo b eta and eta conv ersion in a category of exp onential monads . In an unpublished pap er (Hirscho witz and Maggesi 2007b ) they define a notion of half–e quation and e quation to express congruence b et w een terms. W e adopt their definition in this pap er, but in terpret a pair of half–equations as in equation rather than equation. This emphasizes the dynamic viewp oint of reductions as dir e cte d equalities rather than the static , mathematical viewp oint one obtains b y considering symmetric relations. In a “F uture W ork” section, Hirsc ho witz and Maggesi ( 2010 , Sect. 8) MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 5 men tion the idea of using preorders as an approach to model seman tics, and they suggest interpreting the unt yp ed lam b da calculus with beta and eta reduction rule as a monad ov er the category Pre of preordered sets. The present w ork gives an alternativ e viewp oint to their suggestion b y considering the lambda calculus with b eta reduction — and a class of programming languages in general — as a preorder– v alued r elative monad on the functor ∆ : Set → Pre . The rationale underlying our use of relativ e monads from sets to preorders is that w e consider c ontexts to b e giv en b y unstructured sets, whereas terms of a language carry structure in form of a reduction relation. In this view it is reasonable to suppose v ariables and terms to live in differ ent categories, whic h is p ossible through the use of relative monads on the functor ∆ : Set → Pre (cf. Def. 3 ) instead of traditional monads (cf. also Rem. 63 ). Relative monads were in troduced by Altenkirch et al. ( 2010 ). In that w ork, the authors characterize the unt yp ed lambda calculus as a relative monad ov er the inclusion functor from finite sets to sets. Their p oint of view can b e combined with ours, leading to considering monads on the functor ∆ ◦ i : Fin → Pre , cf. Rem. 62 . T. Hirsc howitz ( 2011 ), taking the viewp oint of Categorical Seman tics, defines a category Sig of 2–signatures for simply–typ e d syn tax with reduction rules, and constructs an adjunction b etw een Sig and the category 2CCCat of small cartesian closed 2–categories. He thus asso ciates to any signature a 2–category of types, terms and reductions satisfying a univ ersal prop erty . More precisely , terms are giv en by morphisms in this category , and reductions are expressed b y the existence of 2–cells b et w een terms. His approac h differs from ours in the wa y in which v ariable binding is mo delled: Hirsc ho witz enco des binding in a Higher–Order Abstract Syntax (HOAS) st yle through exp onen tials. 1.3. Synopsis. In the first section we review the definition of (relative) monads and define mo dules o v er those monads as w ell as their morphisms. Some constructions on monads and modules are giv en, which will b e of imp ortance in what follows. In the second section w e define arities, half–equations and inequations, as well as their representations. Afterwards we state our main result. The running example in the first t wo sections is the 2–signature Λ β of the lam b da calculus with b eta reduction. In the third section w e describ e some elements of the formalization of the main theorem in the proof assistant Co q . Some conclusions and future work are stated in the last section. 2. Rela tive Monads & Modules This section presents the category–theoretic structures from whic h our mo dels of a signature are built. A t first we review relative monads as defined b y Altenkirc h et al. ( 2010 ) and define mo dules for those monads (cf. Sec. 2.1 ). Afterw ards (cf. Secs. 2.2 and 2.3 ) w e p ort some constructions from mo dules ov er (traditional) monads, as presented, for instance, by Hirscho witz and Maggesi ( 2007a ), Zsidó ( 2010 ) and Ahrens and Zsidó ( 2011 ), to mo dules ov er relative monads. 2.1. Mo dules ov er Relative Monads. W e review the definition of relativ e monad as giv en by Altenkirch et al. ( 2010 ) and define suitable morphisms for them. As an example, we consider the lambda calculus as a relative monad from sets to preorders, 6 BENEDIKT AHRENS on the functor ∆ : Set → Pre . Finally , we define mo dules ov er relative monads and morphisms of such mo dules. Definition 1 (Relativ e Monad) . Given c ate gories C and D and a functor F : C → D , a (relative) monad P : C F → D on F is given by • a map P : C → D on the obje cts of C , • for e ach obje ct c of C , a morphism η c ∈ D ( F c, P c ) and • for al l obje cts c and d of C a Kleisli op er ation σ c,d : D ( F c, P d ) → D ( P c, P d ) such that the fol lowing diagr ams c ommute for al l suitable morphisms f and g : F c η c / / f ! ! P c σ ( f ) P d P c σ ( η c ) id + + P c P c σ ( f ) / / σ ( σ ( g ) ◦ f ) ! ! P d σ ( g ) P e. Her e and later we omit the obje ct indic es of the Kleisli op er ation. Remark 2. A monad P is e quipp e d with a functorial structur e (also denote d by P ) by setting, for a morphism f : a → b in C , P ( f ) := lift P ( f ) := σ ( η ◦ F f ) , the functoriality axioms b eing a c onse quenc e of the monad axioms. W e are mainly interested in monads on a sp ecific functor: Definition 3 ( ∆ : Set → Pre ) . W e c al l ∆ : Set → Pre the functor fr om sets to pr e or der e d sets which asso ciates to e ach set X the set itself to gether with the smal lest pr e or der, i.e. the diagonal of X , ∆( X ) := ( X, δ X ) . The functor ∆ is a ful l em b edding , i.e. it is ful ly faithful and inje ctive on obje cts. F urthermor e it is left adjoint to the for getful functor U : Pre → Set , Set ∆ % % U e e ⊥ Pre , that is, the emb e dding ∆ : Set → Pre is a c or efle ction. W e denote by ϕ the family of isomorphisms ϕ X,Y : Pre (∆ X, Y ) ∼ = Set ( X, U Y ) . W e omit the indic es of ϕ whenever they c an b e de duc e d fr om the c ontext. Lemma 4 (Monads ov er ∆ and Monads on Set ) . L et P b e a monad on ∆ . By p ostc omp osing with the for getful functor U : Pre → Set we obtain a monad ¯ P : Set → Set . The substitution is define d, for f ∈ Set ( a, U P b ) by setting σ ¯ P ( f ) := U σ ( ϕ − 1 f ) , MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 7 making use of the adjunction f 7→ ϕ − 1 f ∈ Pre (∆ a, P b ) of Def. 3 . Conversely, to any monad Q on Set we asso ciate a r elative monad by p ostc omp osing with the functor ∆ : ∆ Q : Set ∆ → Pre . The abov e construction in Lem. 4 actually is an instance of a general lemma: given an adjunction F a G suc h that GF = Id C , one can asso ciate a relativ e monad on F to any monad on C and vice versa. These maps are functorial, and one obtains an adjunction b etw een a suitable category of relative monads on F and a category of monads on C . Details will be rep orted elsewhere. Example 5. Consider the set of al l lamb da terms indexe d by their set of fr e e variables LC( V ) ::= V ar : V → LC( V ) | Abs : LC( V 0 ) → LC( V ) | App : LC( V ) × LC( V ) → LC( V ) , wher e V 0 := V + {∗} is the set V enriche d with a new distinguishe d element, i.e. a c ontext extende d by one additional fr e e variable. W e o c c asional ly write λ for Abs and denote applic ation by juxtap osition. A ltenkir ch and R eus ( 1999 ) interpr et LC as a monad over sets. Altenkir ch et al. ( 2010 ) interpr et LC as a r elative monad on the inclusion functor i : Fin → Set , by r estricting c ontexts to b e given by finite sets (cf. also R em. 62 ). W e e quip e ach LC ( V ) with a pr e or der taken as the r eflexive–tr ansitive closur e of the r elation gener ate d by the rule ( β ) λM ( N ) ≤ M [ ∗ := N ] and its pr op agation into subterms. This defines a monad fr om sets to pr e or ders LC β : Set ∆ → Pre . The family η LC is given by the c onstructor V ar , and the Kleisli op er ation σ X,Y : Pre ∆ X, LC β ( Y ) → Pre LC β ( X ) , LC β ( Y ) is given by simultane ous substitution. V ia the adjunction of Def. 3 the substitution c an also b e r e ad as σ X,Y : Set X, LC( Y ) → Pre LC β ( X ) , LC β ( Y ) . The substitution c an henc e b e chosen as for the monad LC , but one has to pr ove the additional pr op erty of monotonicity in the first–or der ar gument. Inspired by the example ab o v e we sometimes call the Kleisli op eration of a monad monadic substitution , ev en for monads that do not denote the terms of a language o ver free v ariables. F or tw o monads P and Q from C to D a morphism of monads is a family of morphisms τ c ∈ D ( P c, Qc ) that is compatible with the monadic structure: Definition 6 (Morphism of Relative Monads) . Given two r elative monads P and Q fr om C to D on the same functor F : C → D , a morphism of monads fr om P 8 BENEDIKT AHRENS to Q is given by a c ol le ction of morphisms τ c ∈ D ( P c, Qc ) such that the fol lowing diagr am c ommutes for any suitable morphism f : P c σ P ( f ) / / τ c P d τ d F c η P c / / η Q c P c τ c Qc σ Q ( τ d ◦ f ) / / Qd Qc A s a c onse quenc e fr om these c ommutativity pr op erties the family τ is a natur al tr ansformation b etwe en the functors induc e d by the monads P and Q . Monads on F : C → D and their morphisms form a category RMon ( F ) where iden- tit y and comp osition of morphisms are defined b y p oint wise identit y and comp osition of morphisms. W e hav e a similar category of traditional monads: Definition 7 (Category of Monads) . W e denote by Mon ( C ) the c ate gory of monads on C and morphisms of such monads. Mor e pr e cisely, a morphism f : P → Q in Mon ( C ) is given by a family τ c : C ( P c, Qc ) of morphisms that is c omp atible with the monadic structur e, analo gously to the diagr ams of Def. 6 . Lemma 8 (A djunction b et w een RMon (∆) and Mon ( Set ) ) . The maps define d in L em. 4 give rise to an adjunction b etwe en the c ate gory of monads over ∆ and the c ate gory of monads over sets, wher e the functor U ∗ is define d on obje cts as in L em. 4 by U ∗ ( R ) := ¯ R : Mon ( Set ) ∆ ∗ ( ( U ∗ h h ⊥ RMon (∆) . Informally , the notion of relative monad on a base functor F is obtained from the notion of monad (in Manes st yle, i.e. with Kleisli op eration) b y inserting applications of the functor F where necessary (Altenkirc h et al. 2010 ). Similarly , one obtains the notion of mo dule o ver a relativ e monad from the notion of module ov er a monad — in form of a Kleisli op eration as presen ted, for instance, by Ahrens and Zsidó ( 2011 ) — by inserting applications of the base functor F : Definition 9 (Mo dule ov er Relativ e Monad) . L et P : C F → D b e a r elative monad and let E b e a c ate gory. A relativ e mo dule M o v er P with co domain E is given by • a map M : C → E on the obje cts of the c ate gories involve d and • for al l obje cts c, d of C a map ς c,d : D ( F c, P d ) → E ( M c, M d ) such that the fol lowing diagr ams c ommute for al l suitable morphisms f and g : M c ς ( f ) / / ς ( σ ( g ) ◦ f ) ! ! M d ς ( g ) M c ς ( η c ) id * * M e M c. MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 9 F unctoriality for suc h a mo dule M is defined in a similar w ay as for monads, by setting, for any morphism f : c → d in C , M ( f ) := rmlift M ( f ) := ς ( η ◦ F f ) . Example 10 (Monoids and Monoid A ctions) . A n anonymous r efer e e suggeste d the fol lowing example: let F : 1 → Set b e the functor on the one–obje ct c ate gory which maps to the final obje ct, i.e. the singleton set. Then a r elative monad on F is a monoid. Given a r elative monad G on F , a G –mo dule with c o domain Set is a monoid action of G . The following examples of mo dules are instances of more general constructions explained in the next section: Example 11 (T autological Mo dule) . The map LC β : V 7→ LC β ( V ) yields a mo dule over the monad LC β , the tautological mo dule LC β . Example 12 (Deriv ed Mo dule) . L et V 0 := V + {∗} . The map LC β 0 : V 7→ LC β ( V 0 ) inherits the structur e of an LC β –mo dule fr om the tautolo gic al LC β –mo dule LC β of Ex. 11 . W e c al l LC β 0 the derived mo dule of the mo dule LC β . Example 13 (Pro duct of Mo dules) . The map V 7→ LC β ( V ) × LC β ( V ) inherits an LC β –mo dule structur e fr om LC β . A mo dule morphism is giv en by a family of morphisms that is compatible with mo dule substitution: Definition 14 (Mo dule Morphism) . L et P b e a r elative monad on F : C → D , and let M and N b e two r elative mo dules over P with c o domain E . A morphism of relative P –mo dules fr om M to N is given by a c ol le ction of morphisms ρ c ∈ E ( M c, N c ) such that for any morphism f ∈ D ( F c, P d ) the fol lowing diagr am c ommutes: M c ς M ( f ) / / ρ c M d ρ d N c ς N ( f ) / / N d The mo dules ov er P with co domain E and morphisms b etw een them form a category called RMo d ( P , E ) . Note that the “R” here stands for “relative”, not for “right” as opp osed to “left” . Composition and identit y morphisms of mo dules are defined b y p oin t wise comp osition and identit y , similarly to the category of monads. Example 15 (Exs. 11 , 12 , 13 cont.) . A bstr action and applic ation ar e morphisms of LC β –mo dules, Abs : LC β 0 → LC β , App : LC β × LC β → LC β . 2.2. Constructions on Monads and Mo dules. The constructions on mo dules o ver monads as used by Hirscho witz and Maggesi ( 2007a ) carry ov er to relative monads: 10 BENEDIKT AHRENS Definition 16 (T autological Mo dule) . Given a monad P on F : C → D , we define the tautological mo dule (also denote d by P ) over P to b e the mo dule ( M , ς ) := ( P , σ ) , i.e. with obje ct map P and mo dule substitution given by the monad substitution. Thus the monad P c an b e c onsider e d as an obje ct in the c ate gory RMo d( P , D ) . Definition 17 (Constan t and terminal mo dule) . L et P b e a monad on F : C → D . F or any obje ct e ∈ E the c onstant map T e : C → E , c 7→ e for al l c ∈ C yields a P –mo dule ( T e , id ) . In p articular, if E has a terminal obje ct 1 E , then the c onstant mo dule ( T 1 E , id) is terminal in RMo d( P , E ) . Definition 18 (P ostcomp osition with a functor) . L et P b e a monad on F : C → D , and let M b e a P –mo dule with c o domain E . L et G : E → X b e a functor. Then the obje ct map G ◦ M : C → X define d by c 7→ G ( M ( c )) is e quipp e d with a P –mo dule structur e by setting, for c, d ∈ C and f ∈ D ( F c, P d ) , ς G ◦ M ( f ) := G ( ς M ( f )) . F or M := P and G a c onstant functor mapping to an obje ct x ∈ X and its identity morphism id x , we obtain the c onstant mo dule ( T x , id) as in Def. 17 . Let P and Q b e tw o monads on F : C → D . Given a monad morphism h : P → Q , w e can turn an y Q –module M in to a P –mo dule, by “pulling it back” along h : Definition 19 (Pullbac k module) . L et h : P → Q b e a morphism of monads on F : C → D and let M b e a Q –mo dule with c o domain E . W e define a P –mo dule h ∗ M to E with obje ct map c 7→ M c by setting ς h ∗ M ( f ) := ς M ( h d ◦ f ) . This mo dule is c al le d the pullback mo dule of M along h . The pul lb ack extends to mo dule morphisms and is functorial. Remark 20. The pul lb ack P –mo dule h ∗ M has the same underlying functor as the Q –mo dule M . It is mer ely the substitution action that changes: while ς M exp e cts morphisms in C ( F c, Qd ) as ar guments, the substitution of h ∗ M exp e cts morphisms in C ( F c, P d ) . Definition 21 (Induced mo dule morphism) . With the same notation as b efor e, the monad morphism h induc es a morphism of P –mo dules h : P → h ∗ Q . Remark 22. Note that the pr e c e ding two c onstructions do not change the functor r esp. natur al tr ansformation underlying the mo dule r esp. morphism of mo dules. The following construction explains Ex. 13 : Definition 23 (Pro ducts) . Supp ose the c ate gory E is e quipp e d with a pr o duct. L et M and N b e P –mo dules with c o domain E . Then the map (on obje cts) C → E , c 7→ M c × N c is e quipp e d with a mo dule substitution by setting ς M × N ( f ) := ς M ( f ) × ς N ( f ) . This c onstruction extends to a pr o duct on RMod( P , E ) . There is also a category where modules o v er differen t monads are grouped together: MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 11 Definition 24. L et C , D and E b e c ate gories and F : C → D b e a functor. W e define the c ate gory LMo d ( F , E ) (“L” for “lar ge”) to b e the c ate gory whose obje cts ar e p airs ( R, M ) , wher e R is a monad over F : C → D and M is an R –mo dule. A morphism fr om ( R, M ) to ( S, N ) is a p air ( ρ, τ ) wher e ρ : R → S is a monad morphism and τ is an R –mo dule morphism τ : M → ρ ∗ N . W e are particularly in terested in monads ov er the functor ∆ : Set → Pre . The follo wing construction — derivation — applies to mo dules ov er such monads. 2.3. Deriv ed Mo dules. Roughly sp eaking, a binding constructor makes free v ari- ables disappear. Its input are hence terms in an extended context, i.e. with (one or more) additional free v ariables compared to the output. Derivation is ab out context extension. F ormally , given a set V ( V for v ariables), w e consider a new set V 0 := V + {∗} whic h denotes V enric hed with a new distinguished elemen t – the “fresh” v ariable. The map V 7→ V 0 can be extended to a monad on the category of sets and is hence functorial. Giv en a map f : V → W and w ∈ W , w e call default( f , w ) : V 0 → W the copro duct map defined by default( f , w ) = [ f , λx.w ] . Definition 25 (Deriv ed Mo dule) . Given a monad P on ∆ : Set → Pre and a P –mo dule M with c o domain E , we define the deriv ed mo dule by setting M 0 ( V ) := M ( V 0 ) . F or a morphism f ∈ Pre (∆ V , P W ) the mo dule substitution for the derive d mo dule is given by ς M 0 ( f ) := ς M (shift( f )) . Her e the shifte d map shift( f ) ∈ Pre (∆( V 0 ) , P ( W 0 )) is define d via the adjunction of Def. 3 as shift( f ) := ϕ − 1 default P (inl) ◦ f , η (inr( ∗ )) , wher e [inl , inr] = id W 0 . Deriv ation extends to an endofunctor on the category of P –mo dules with co domain E . Remark 26. When P is a monad of terms over fr e e variables, the map shift f sends the additional variable of V 0 to η P ( ∗ ) , i.e. to the term c onsisting of just the “fr eshest” fr e e variable. When r e cursively substituting with a map f : V → P ( W ) , terms under a binder such as λ must b e substitute d with the map shift( f ) . Definition 27. Given a natur al numb er n , we write M n for the mo dule M derive d n times. Given a list s = [ n 1 , . . . , n m ] of natur al numb ers, we write M s := M n 1 × . . . × M n m . 12 BENEDIKT AHRENS Pr o duct and derivation ar e functorial, and we use the same notation on mor- phisms . That is, given a morphism of P –mo dules ρ : M → N , we write ρ s := ρ n 1 × . . . × ρ n m : M s → N s . The pullback op eration comm utes with pro ducts and deriv ations : Lemma 28. L et C and D b e c ate gories and E b e a c ate gory with pr o ducts. L et P and Q b e monads on F : C → D and ρ : P → Q a monad morphism. L et M and N b e P –mo dules with c o domain E . The pul lb ack functor is c artesian: ρ ∗ ( M × N ) ∼ = ρ ∗ M × ρ ∗ N . Lemma 29. L et P b e a r elative monad on ∆ : Set → Pre and M a mo dule over P with c o domain E . Then we have ρ ∗ ( M 0 ) ∼ = ( ρ ∗ M ) 0 . Definition 30 (Substitution of one v ariable) . W e denote by wPre (“w” for “we ak”) the c ate gory whose obje cts ar e pr e or der e d sets, and wher e a morphism fr om A to B is a map b etwe en the underlying sets, that is, a morphism U A → U B in Set . Given a monad P : Set → Pre on ∆ , and a P –mo dule M with c o domain Pre , we c an c onsider M as a P –mo dule with c o domain wPre by p ostc omp osing with the inje ction. W e denote this mo dule by ˆ M ∈ RMo d ( P , wPre ) . F or any set X , we define the substitution of just one variable, subst X : P ( X 0 ) × P ( X ) → P ( X ) , ( y , z ) 7→ y [ ∗ := z ] := σ (default( η X , z ))( y ) . This defines a morphism of P –mo dules with c o domain wPre , subst P : ˆ P 0 × ˆ P → ˆ P . Remark 31. Note that the substitution mo dule morphism define d ab ove is by c onstruction monotone in its first ar gument, but not in its se c ond ar gument. This is the r e ason why we c annot c onsider subst P as a morphism of P –mo dules subst P : P 0 × P → P , but have to switch to the c ate gory wPre . This fact and a way to ensur e monotonicity also in the se c ond ar gument ar e explaine d mor e gener al ly in R em. 61 . 3. 2–Signa tures and their Represent a tions In this section w e define 2–signatur es and their representations. W e then pro v e that, giv en an y 2–signature, its asso ciated category of representations has an initial ob ject, the language generated by the 2–signature. An arity describes the num ber of arguments and binding b eha viour of a con- structor of a syntax. A 1–signatur e S is a family of arities and as such sp ecifies the terms of a language. An ine quation over S — also called S –ine quation — expresses a reduction rule on the terms of the syn tax associated to S . A 2–signatur e is given b y a 1–signature S and a set of S –inequations. W e define r epr esentations of a 1–signature S analogously to Hirsc ho witz and Maggesi ( 2007a ) and Zsidó ( 2010 ), except that we use r elative monads and modules o ver such monads. Afterw ards w e use ine quations over S to specify reduction rules, and we consider those representations of S that satisfy the given inequations. W e sho w that among those represen tations there is an initial representation, whic h MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 13 in tegrates the terms generated by S , equipp ed with reductions according to the giv en inequations. Our inequations are precisely Hirscho witz and Maggesi’s (2007) e quations , i.e. parallel pairs of half–equations. W e simply in terpret such a pair to define a (dir e cte d) r e duction rather than an equalit y . Throughout this section the running example is the 2–signature Λ β whic h sp ecifies the lambda calculus with b eta reduction. 3.1. Arities, 1–Signatures and their Representations. W e first consider pure syn tax, i.e. syntax without reductions. W e sp ecify syn tax by a 1–signatur e : Definition 32 (Arit y , 1–Signature) . A n arity is a list of natur al numb ers. A 1–signature is a family of arities. In tuitively , the length of an arity sp ecifies the num ber of arguments of a con- structor, and the i –th en try of the arity sp ecifies the num b er of v ariables whic h are b ound by the constructor in the i –th argument. Example 33. The 1–signatur e Λ of untyp e d lamb da c alculus is given by the two arities app := [0 , 0] , abs := [1] . Definition 34. L et s := [ n 1 , n 2 , . . . , n m ] b e an arity and P b e a monad on the functor ∆ : Set → Pre . W e c al l dom( s, P ) := P s = P n 1 × . . . × P n m the domain mo dule of s for P . Note that we use the notation define d in Def. 27 . Definition 35 (Represen tations of a 1–Signature) . A represen tation R of a 1– signature S is given by a monad P over the functor ∆ : Set → Pre and, for e ach arity s = [ n 1 , n 2 , . . . , n m ] ∈ S , a morphism of P –mo dules s R : dom( s, P ) → P . Given a r epr esentation R , we denote its underlying monad by R as wel l. Example 36 (Ex. 33 contin ued) . A r epr esentation R of the 1–signatur e Λ is given by • a monad R : Set ∆ → Pre and • two morphisms of R –mo dules in RMo d( R, Pre ) , app R : R × R → R and abs R : R 0 → R . Remark 37. A r epr esentation of a 1–signatur e à la Hirschowitz and Maggesi ( 2007a ) is define d analo gously, exc ept for the use of (plain) monads on the c ate gory of sets and mo dules ther e on inste ad of relativ e monads and mo dules on r elative monads. Definition 38. T o any r epr esentation R of a 1–signatur e S in a r elative monad R as define d in Def. 35 we asso ciate a r epr esentation U ∗ ( R ) of S in the monad ¯ R (cf. L em. 4 ) in the sense of Hirschowitz and Maggesi ( 2007a ) by p ostc omp osing the r epr esentation mo dule morphism of any arity s of S with the for getful functor fr om pr e or ders to sets: s R : R s → R 7→ s U ∗ R : ¯ R s → ¯ R , 14 BENEDIKT AHRENS wher e we use ¯ ( R s ) = ( ¯ R ) s . Conversely, to any r epr esentation Q of S in a monad Q over sets we c an asso ciate a r epr esentation ∆ ∗ Q of S in the r elative monad ∆ ∗ Q on ∆ , by p ostc omp osing the r epr esentation mo dule morphisms with ∆ . Morphisms of representations are monad morphisms whic h commute with the represen tation morphisms of mo dules: Definition 39 (Morphism of Representations) . L et P and Q b e r epr esentations of a signatur e S . A morphism of representations f : P → Q is a morphism of monads f : P → Q such that the fol lowing diagr am c ommutes for any arity s of S: P s s P / / f s P f f ∗ Q s f ∗ s Q / / f ∗ Q. Note that we make extensive use of the notation defined in Def. 27 . T o make sense of this diagram it is necessary to recall the constructions on mo dules of section 2.2 . The diagram lives in the category RMo d ( P , Pre ) . The vertices are obtained from the tautological modules P resp. Q o ver the monads P resp. Q b y applying the deriv ation and pullback functors as well as by the use of the pro duct in the category of P –mo dules into Pre . The vertical morphisms are mo dule morphisms induced b y f , to whic h functoriality of deriv ation and products are applied. F urthermore, instances of Lems. 28 and 29 are hidden in the low er left corner. The low er horizontal morphism makes use of the functoriality of the pullback op eration. Example 40 (Ex. 36 contin ued) . L et P and R b e two r epr esentations of Λ . A morphism fr om P to R is given by a morphism of monads f : P → R such that the fol lowing diagr ams of P –mo dule morphisms c ommute: P × P app P / / f × f P f P 0 abs P / / f 0 P f f ∗ ( R × R ) f ∗ (app R ) / / f ∗ R f ∗ R 0 f ∗ (abs R ) / / f ∗ R. Comp osition and identit y morphisms of representations are giv en by comp osition and identit y of monad morphisms. W e obtain a category of represen tations: Definition 41 (Category of Representations) . R epr esentations of S and their morphisms form a c ate gory Rep ∆ ( S ) . Since w e are not considering an y reductions on terms y et, but only plain syn tax, it comes as no surprise that, for any 1–signature S , our category of represen tations of S relates to Hirscho witz and Maggesi’s: Lemma 42. The assignment of Def. 38 extends to an adjunction b etwe en our c ate gory of r epr esentations Rep ∆ ( S ) in r elative monads on ∆ and Hirschowitz and Maggesi’s c ate gory Rep ( S ) of r epr esentations in monads over sets: MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 15 Rep ( S ) ∆ ∗ ' ' U ∗ g g ⊥ Rep ∆ ( S ) . Lemma 43. Given a signatur e S , the c ate gory of r epr esentations of S in r elative monads on ∆ has an initial r epr esentation. Its underlying monad asso ciates to any set V of variables the set of terms of the language sp e cifie d by S in the c ontext V , e quipp e d with the diagonal pr e or der. Pr o of. This is a direct consequence of the fact that left adjoin ts preserv e colimits (Mac Lane 1998 , Chap. V.5), thus, in particular, initial objects. Remark 44. The formalization in Coq of L em. 43 (cf. Se c. 4 ) do es not app e al to Hirschowitz and Maggesi’s r esult, but c onstructs the initial obje ct fr om scr atch. 3.2. Inequations ov er 1–Signatures. Consider the b eta rule of lambda calculus, λM ( N ) M [ ∗ := N ] . W e would like to express suc h a rule through a suitable inequation. In our formalism, abstraction and application are considered as morphisms of mo dules (cf. Ex. 15 ), and so is substitution (cf. Def. 30 ). This suggest to define (in)equations ov er a 1–signature S as p ar al lel p airs of mo dule morphisms, indexed by represen tations of S . Put differen tly , an (in)equation asso ciates a parallel pair of mo dule morphisms to an y representation of S . Hirsc howitz and Maggesi ( 2007b ) sp ecify equations through suc h pairs of (indexed) mo dule morphisms ov er (plain) monads. W e adapt their definition to our use of r elative monads and mo dules ov er suc h monads. Afterw ards w e simply interpret a pair of half–equations as ine quation rather than equation. Definition 45 (Category of Half–Equations) . L et S b e a signatur e. A n S –mo dule U is a functor fr om the c ate gory of r epr esentations of S to the c ate gory of mo dules LMo d (∆ , wPre ) c ommuting with the for getful functor to the c ate gory of r elative monads on ∆ : Rep ∆ ( S ) U / / & & LMo d(∆ , wPre ) w w RMon (∆) . W e define a morphism of S –mo dules to b e a natur al tr ansformation which b e c omes the identity when c omp ose d with the for getful functor. W e c al l these morphisms half–equations . The c ol le ction of S –mo dules and their morphisms yield a c ate gory which we c al l the c ate gory of S –mo dules (or the c ate gory of half–e quations). Remark 46. Obje cts of LMo d (∆ , wPre ) ar e p airs of a monad P on ∆ : Set → Pre and a P –mo dule M . Given an S –mo dule U , we sometimes write U ( R ) for the se c ond c omp onent of U ( R ) , i.e. for the mo dule over the monad (underlying the r epr esentation) R , se e for instanc e R em. 47 . W e also write U R X := U ( R )( X ) 16 BENEDIKT AHRENS for the value of an S –mo dule at the r epr esentation R and the set X . Similarly, for a half–e quation α : U → V we write α R X := α ( R )( X ) : U R X → V R X . Remark 47. A half–e quation α fr om S –mo dule U to V asso ciates to any r epr esen- tation P a morphism of P –mo dules α P : U ( P ) → V ( P ) in RMo d ( P , wPre ) such that for any morphism of S –r epr esentations f : P → R the fol lowing diagr am c ommutes: ( P , U ( P )) α P / / ( f ,U ( f )) ( P , V ( P )) ( f ,V ( f )) ( R, f ∗ ( U ( R ))) α R / / ( R, f ∗ ( V ( R ))) Lemma 48. The c ate gory of S –mo dules is c artesian. W e give some examples of generic S –mo dules. The inductive class of S –mo dules th us defined is of imp ortance later. Definition 49 (Classic S –mo dule) . W e c al l classic any S –mo dule satisfying the fol lowing inductive pr e dic ate: • The map Θ : R 7→ ( R , ˆ R ) is a classic S –mo dule. • If the S –mo dule M : R 7→ ( M 1 ( R ) , M 2 ( R )) is classic, so is M 0 : R 7→ M 1 ( R ) , M 2 ( R ) 0 . • If M and N ar e classic, so is M × N : R 7→ M 1 ( R ) , M 2 ( R ) × N 2 ( R ) . • The terminal mo dule ∗ : R 7→ ( R, 1) is classic. U sing the same notation as in Def. 27 , any list of natur al numb ers defines uniquely a classic S –mo dule. The following examples of half–equations are building blo cks for the inequation sp ecifying b eta reduction: Definition 50. The substitution op er ation subst : R 7→ subst R : ˆ R 0 × ˆ R → ˆ R is a half–e quation over any signatur e S . Its domain and c o domain ar e classic. Example 51 (Ex. 33 contin ued) . The map app ◦ (abs × id) : R 7→ app R ◦ (abs R × id R ) : ˆ R 0 × ˆ R → ˆ R is a half–e quation over the signatur e Λ . Definition 52. A ny arity s = [ n 1 , . . . , n m ] ∈ S defines a classic S –mo dule dom( s ) : R 7→ R s . An ine quation is a pair of parallel half–equations: MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 17 Definition 53 (Inequation, 2–Signature) . Given a 1–signatur e S , an S –inequation is a p air of p ar al lel half–e quations b etwe en S –mo dules. W e write α ≤ γ : U → V for the ine quation ( α, γ ) with domain U and c o domain V . A 2–signature is a p air ( S, A ) of a 1–signatur e S and a set A of S –ine quations. Giv en a set A of S –inequations, we can ask whether a given representation R of S satisfies the inequations of A . If this is the case, w e call R a representation of the 2–signature ( S, A ) : Definition 54 (Represen tations of a 2–Signature) . L et α ≤ γ : U → V b e an ine quation over S , and let R b e a r epr esentation of S . W e say that R satisfies α ≤ γ if α R ≤ γ R p ointwise, i.e. if for any set X and any y ∈ U ( R )( X ) , α R X ( y ) ≤ γ R X ( y ) . F or a set A of S –ine quations, we c al l represen tation of ( S, A ) any r epr esentation of S that satisfies e ach ine quation of A . W e define the c ate gory of r epr esentations of the 2–signatur e ( S, A ) to b e the ful l sub c ate gory in the c ate gory of r epr esentations of S whose obje cts ar e r epr esentations of ( S, A ) . Example 55 (Ex. 51 contin ued) . W e denote by β the Λ –ine quation ( β ) app ◦ (abs × id) ≤ subst . W e c al l Λ β the 2–signatur e ((app , abs) , β ) . A r epr esentation P of Λ β is given by • a monad P : Set ∆ → Pre and • two morphisms of P –mo dules app : P × P → P and abs : P 0 → P such that for any set X and any y ∈ P ( X 0 ) and z ∈ P X app X (abs X ( y ) , z ) ≤ y [ ∗ := z ] . 3.3. Initialit y for 2–Signatures. Giv en a 2–signature ( S, A ) , we would lik e to exhibit an initial object in its asso ciated category of represen tations. Ho w ev er, we ha ve to rule out inequations which are nev er satisfied, since an empt y category ob viously do es not not ha ve an initial object. W e restrict ourselves to inequations with a classic codomain: Definition 56 (Classic Inequation) . W e say that an S –ine quation is classic if its c odomain is classic. Theorem 57. F or any set of classic S –ine quations A , the c ate gory of r epr esentations of ( S, A ) has an initial obje ct. Pr o of. The basic ingredients for building the initial representation are given by the initial representation ∆ ∗ Σ in the category Rep ∆ ( S ) (cf. Lem. 43 ) or, equiv alen tly , b y the initial represen tation Σ in Rep ( S ) . W e call Σ the monad underlying the represen tation Σ . The pro of consists of 3 steps: at first, w e define a preorder ≤ A on the terms of Σ , induced b y the set A of inequations. Afterw ards we sho w that the data of the represen tation Σ — substitution, represen tation morphisms etc. — is compatible with the preorder ≤ A in a suitable sense. This will yield a representation Σ A of ( S, A ) . Finally we show that Σ A is the initial suc h representation. 18 BENEDIKT AHRENS — The monad underlying the initial r epr esentation: F or an y set X , w e equip Σ X with a preorder A b y setting, for x, y ∈ Σ X , (3.1) x ≤ A y : ⇔ ∀ R : Rep ( S, A ) , i R ( x ) ≤ R i R ( y ) , where i R : Σ → ¯ R is the initial morphism of represen tations in monads coming from Zsidó’s theorem (or, equiv alen tly , the initial morphism ∆ ∗ Σ → R ). W e hav e to sho w that the map X 7→ Σ A X := (Σ X , ≤ A ) yields a relativ e monad ov er ∆ . The missing fact to pro v e is that the substitution of the monad Σ with a morphism f ∈ Pre (∆ X , Σ A Y ) ∼ = Set ( X, Σ Y ) is compatible with the order ≤ A : given any f ∈ Pre (∆ X, Σ A Y ) w e show that σ Σ ( f ) ∈ Set (Σ X, Σ Y ) is monotone with respect to ≤ A and hence (the carrier of) a morphism σ ( f ) ∈ Pre (Σ A X, Σ A Y ) . W e o v erload the infix symbol = to denote monadic substitution. Supp ose x ≤ A y , we show x = f ≤ A y = f . Using the definition of ≤ A , we must show, for an y representation R of ( S, A ) , i R ( x = f ) ≤ R i R ( y = f ) . Since i R is a morphism of representations, it is compatible with the substitutions of Σ and ¯ R ; w e hav e i R ( x = f ) = i R ( x ) = i R ◦ f . Rewriting this equality and its equiv alen t for y in the curren t goal yields the goal i R ( x ) = i R ◦ f ≤ A i R ( y ) = i R ◦ f , whic h is true since the substitution of R (whose underlying map is that of ¯ R ) is monotone in the first argumen t (cf. Rem. 61 ) and i R ( x ) ≤ R i R ( y ) by assumption. W e hence ha v e defined a monad Σ A on ∆ . W e in terrupt the pro of for an imp ortant lemma: Lemma 58. Given a classic functor V : Rep ∆ ( S ) → LMod (∆ , wPre ) fr om the c ate gory of r epr esentations in monads on ∆ to the lar ge c ate gory of mo dules over such mo dules with c o domain c ate gory wPre , we have x ≤ A y ∈ V (Σ)( X ) ⇔ ∀ R : Rep ( S, A ) , V ( i R )( x ) ≤ V R X V ( i R )( y ) , wher e now and later we omit the ar gument X , e.g. in V ( i R )( X )( x ) . Pr o of of L emma 58 . The pro of is done b y induction on the deriv ation of “ V classic” . The only interesting case is where V = M × N is a product: ( x 1 , y 1 ) ≤ ( x 2 , y 2 ) ⇔ x 1 ≤ x 2 ∧ y 1 ≤ y 2 ⇔ ∀ R , M ( i R )( x 1 ) ≤ M ( i R )( x 2 ) ∧ ∀ R, N ( i R )( y 1 ) ≤ N ( i R )( y 2 ) ⇔ ∀ R , M ( i R )( x 1 ) ≤ M ( i R )( x 2 ) ∧ N ( i R )( y 1 ) ≤ N ( i R )( y 2 ) ⇔ ∀ R , V ( i R )( x 1 , y 1 ) ≤ V ( i R )( x 2 , y 2 ) . MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 19 — R epr esenting S in Σ A : An y arit y s ∈ S should be represen ted by the module morphism s Σ , i.e. the represen tation of s in Σ . W e ha v e to show that those representations are compatible with the preorder A . Given x ≤ A y in dom ( s, Σ)( X ) , w e sho w (omitting the argumen t X in s Σ ( X )( x ) ) s Σ ( x ) ≤ A s Σ ( y ) . By definition, we hav e to sho w that, for an y representation R as before, i R ( s Σ ( x )) ≤ R i R ( s Σ ( y )) . Since i R is a morphism of represen tations, it commutes with the represen tational mo dule morphisms — the corresp onding diagram is similar to the diagram of Def. 39 . By rewriting with this equality we obtain the goal s R dom( s )( i R ) ( x ) ≤ R s R dom( s )( i R ) ( y ) . This goal is prov ed by instantiating Lem. 58 with the classic S –mo dule dom ( s ) (cf. Def. 52 ) and the fact that s R is monotone. W e hence hav e established a represen tation – which we call Σ A – of S in Σ A . — Σ A satisfies A : The next step is to show that the representation Σ A satisfies A . Giv en an inequation α ≤ γ : U → V of A with a classic S –mo dule V , we m ust sho w that for any set X and any x ∈ U (Σ A )( X ) in the domain of α w e hav e (3.2) α Σ A X ( x ) ≤ A γ Σ A X ( x ) . In the following we omit the subscript X . By Lemma 58 the goal is equiv alent to (3.3) ∀ R : Rep ( S, A ) , V ( i R )( α Σ A ( x )) ≤ V R X V ( i R )( γ Σ A ( x )) . Let R b e a representation of ( S, A ) . W e con tin ue by proving ( 3.3 ) for R . By Remark 47 and the fact that i R is also the carrier of a morphism of S –representations from ∆Σ to R (cf. Lemma 42 ) w e can rewrite the goal as α R U ( i R )( x ) ≤ V R X γ R U ( i R )( x ) , whic h is true since R satisfies A . — Initiality of Σ A : Giv en any representation R of ( S, A ) , the morphism i R is monotone with resp ect to the orders on Σ A and R b y construction of ≤ A . It is hence a morphism of represen tations from Σ A to R . Unicit y of the morphisms i R follo ws from its unicity in the category of represen tations of S , i.e. without inequations. Hence Σ A is the initial ob ject in the category of representations of ( S, A ) . Example 59 (Ex. 55 contin ued) . The only ine quation of the signatur e Λ β is classic. The initial r epr esentation of Λ β is given by the monad LC β to gether with the LC β –mo dule morphisms Abs and App (cf. Ex. 15 ) as r epr esentation structur e. 20 BENEDIKT AHRENS 3.4. Some Remarks. Remark 60 (ab out “generating” inequations) . Given a 2–signatur e ( S, A ) and a r epr esentation R of S , the r epr esentation morphism of mo dules s R of any arity s of S is monotone. F or the initial r epr esentation Σ A of ( S, A ) this me ans that any r e duction b etwe en terms of Σ , which is sp e cifie d by an ine quation of A , is automat- ic al ly pr op agate d into subterms. Similarly, the r e duction r elation on Σ gener ate d by A is by c onstruction r eflexive and tr ansitive, sinc e we c onsider r epr esentations in monads with c o domain Pre . F or the example of Λ β this me ans that in or der to obtain the “c omplete” r e duction r elation β , it is sufficient to sp e cify only one ine quation for the β –rule λM ( N ) ≤ M [ ∗ := N ] . Remark 61 (ab out substitution) . The substitution in Ex. 5 is c omp atible with the or der on terms in the fol lowing sense: 1. M ≤ N implies M [ ∗ := A ] ≤ N [ ∗ := A ] and 2. A ≤ B implies M [ ∗ := A ] ≤ M [ ∗ := B ] . The first implic ation is a gener al fact ab out r elative monads on ∆ : for any such monad P and any f ∈ Pre (∆ V , P W ) , the substitution σ X,Y ( f ) ∈ Pre ( P V , P W ) is monotone. The se c ond monotonicity pr op erty, however, is not enc o de d in the fr amework we give in the pr esent p ap er. A differ ent definition of monad which would enfor c e implic ation 2 to hold is given by c onsidering Pre as a c ate gory enriche d over itself, or as a 2–c ate gory: given morphisms f , g : Pre ( V , W ) we say that f ⇒ g iff f ≤ g iff ∀ v : V , f ( v ) ≤ g ( v ) . A monad P would then have to b e e quipp e d with a substitution action that is given, for any two sets V and W , by a functor (of pr e or ders) σ V ,W : Pre (∆ V , P W ) → Pre ( P V , P W ) . U sing this “enriche d” definition of monad — which is employe d in another work of ours (Ahr ens 2012b ) — for the r epr esentations of a 2–signatur e, we c an pr ove that any language define d by a 2–signatur e satisfies the se c ond implic ation as wel l. The pr o of is available in our Co q libr ary. Remark 62 (ab out finite con texts) . A ltenkir ch et al. ( 2010 ) char acterize the untyp e d lamb da c alculus as a r elative monad on the inclusion functor i : Fin → Set fr om finite sets to sets. A n anonymous r efer e e suggeste d c ombining our viewp oint — syntax as monad over ∆ : Set → Pre — with Altenkir ch et al.’s: one might c onsider the lamb da c alculus as a r elative monad on the c omp osition ∆ ◦ i : Fin → Pre , and, mor e gener al ly, one might c onsider r epr esentations of a signatur e ( S, A ) over monads on ∆ ◦ i : Fin → Pre . The ab ove the or em r emains true when r eplacing monads on ∆ by monads on ∆ ◦ i everywher e. A n e quivalenc e b etwe en the the or em thus obtaine d and our Thm. 57 might b e establishe d in a way similar to what Zsidó do es in her PhD thesis: she shows, by me ans of adjunctions b etwe en the r esp e ctive c ate gories of mo dels, the e quivalenc e b etwe en the appr o ach of Fior e et al. ( 1999 ) — b ase d on monoids over finite c ontexts — and the appr o ach of Hirschowitz and Maggesi ( 2007a ), wher e mo dels ar e built fr om monads on the c ate gory Set , i.e. over arbitr ary c ontexts. MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 21 Remark 63 (ab out monads on Pre ) . A s mentione d in Se c. 1.2 , Ghani and Lüth ( 2003 ) and Hirschowitz and Maggesi ( 2010 ) suggest the use of monads over the c ate gory Pre of pr e or der e d sets for mo del ling syntax with a r ewriting r elation. Inde e d, r epr esentations of a signatur e ( S, A ) c ould b e analo gously define d for such monads. The ab ove c onstruction of the initial r epr esentation of ( S, A ) c arries over to r ep- r esentations in such monads, thus yielding an initiality r esult in which syntax is mo del le d as monad on Pre . It might b e inter esting to establish a pr e cise c onne ction — e.g., in form of adjunctions — b etwe en the r esulting c ate gories of r epr esentations in monads on Pre and r epr esentations in r elative monads on ∆ . 4. F ormaliza tion in the proof assist ant Coq In this section we explain some elements of the formalization of the initiality theorem in the pro of assistant Co q . Ho w ev er, w e only explain the implementations of definitions and lemmas that are sp ecific to the theorem. W e base ourselves on a general library of category theoretic concepts the formalization details of which w e do not go into. The in terested reader can find an in–depth description and the complete Co q co de online 1 . The implemen tation of categories, monads and mo dules ov er monads (whic h are analogous to the implementation of their relative coun terparts used here) is explained in detail by Ahrens and Zsidó ( 2011 ). F or a morphism f from ob ject a to ob ject b in any category w e write f : a ---> b . Composition of morphisms f : a → b and g : b → c is written f ;; g . 4.1. Arities as Lists. A ccording to Def. 32 , a 1–signature consists of an indexing t yp e and, for eac h index, a list of natural n umbers, indicating the n umber of argumen ts of a constructor, as well as the num ber of v ariables bound in each argumen t. In the formalization they are simply called “signatures”: Notation "[ T ]" := (list T) (at level 5). Record Signature : Type := { sig_index : Type ; sig : sig_index -> [nat] }. Next we formalize context extension according to a natural num b er, cf. Sec. 2.3 . These definitions are imp ortant for the definition of the module morphisms we asso ciate to an arit y , cf. b elow. Context extension is actually functorial. Given a natural num ber n and a set of v ariables V , we recursively define the set V ** n to b e the set V enriched with n additional v ariables: Fixpoint pow (n : nat) (V : TYPE) : TYPE := match n with | 0 => V | S n’ => pow n’ (option V) end . Notation "V ** n" := (pow n V) (at level 10). Fixpoint pow_map (l : nat) V W (f : V ---> W) : V ** l ---> W ** l := match l return V ** l ---> W ** l with | 0 => f 1 http://math.unice.fr/laboratoire/logiciels 22 BENEDIKT AHRENS | S n’ => pow_map (^ f) end . Notation "f ^^ l" := (pow_map (l:=l) f) (at level 10). 4.2. Represen tations. Giv en an arity s , i.e. a list of natural n um b ers s (cf. Def. 32 ), and a relativ e monad P on the functor ∆ , w e need to define the product mo dule P s . More generally , we define M s for an y P –mo dule M with codomain Pre . W e build this mo dule from scratch instead of relying on the category–theoretic constructions such as pro duct and deriv ation functor for the mo dule categories, allo wing us to omit the insertion of isomorphisms in the st yle of Lems. 28 and 29 . The reasons for this design choice are explained elsewhere (Ahrens and Zsidó 2011 ). Giv en any mo dule M o ver a monad P from sets to preordered sets, we define the pro duct type prod_mod_c as an inductive type family parametrized by a con text and dep endent on a list of naturals. A ctually w e define at first the carrier dep ending not on a mo dule, but just on a carrier map M : TYPE -> Ord . The relation on the pro duct is induced by that on M . Variable M : TYPE -> Ord. Inductive prod_mod_c (V : TYPE) : [nat] -> Type := | TTT : prod_mod_c V nil | CONSTR : forall b bs, M (V ** b)-> prod_mod_c V bs -> prod_mod_c V (b::bs) . Notation "a -:- b" := (CONSTR a b) (at level 60). Inductive prod_mod_c_rel (V : TYPE) : forall n, relation (prod_mod_c M V n) := | TTT_rel : forall x y : prod_mod_c M V nil, prod_mod_c_rel x y | CONSTR_rel : forall n l, forall x y : M (V ** n), forall a b : prod_mod_c M V l, x << y -> prod_mod_c_rel a b -> prod_mod_c_rel (x -:- a) (y -:- b). Note that the infixed “ << ” is o verloaded notation and denotes the relation of any preordered set. F or any given list a of naturals and any set V of v ariables, the set prod_mod_c V a equipp ed with the relation prod_mod_c_rel V a is in fact a preordered set. F or the proof of transitivit y we rely on the Co q tactic dependent induction , thus on the axioms JMeq.JMeq_eq : forall (A : Type ) (x y : A), x ~= y -> x = y Eqdep.Eq_rect_eq.eq_rect_eq : forall (U : Type ) (p : U) (Q : U -> Type ) (x : Q p) (h : p = p), x = eq_rect p Q x p h from the Co q standard library . No w if M is not just a map of type TYPE -> Ord , but a mo dule ov er some relativ e monad P o ver Delta (the functor ∆ : Set → Pre ), we equip the pro duct map with a mo dule substitution in form of a recursiv e function: Variable M : RModule P Delta. Fixpoint pm_mkl l V W (f : Delta V ---> P W) (X : prod_mod_c ( fun V => M V) V l) : prod_mod_c _ W l := match X in prod_mod_c _ _ l return prod_mod_c ( fun V => M V) W l with MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 23 | TTT => TTT _ W | elem -:- elems => rmkleisli (RModule_struct := M) (lshift _ f) elem -:- pm_mkl f elems end . (* ... *) Definition prod_mod (a : [nat]) := Build_RModule (prod_mod_struct a). Afterw ards we prov e by induction that this map is indeed monotone with resp ect to the preorder prod_mod_c_rel defined ab o v e. Altogether, we obtain a mo dule prod_mod M l for an y mo dule M : RMOD P Ord and any list of naturals l . T o any arity ar : [nat] and a mo dule M o ver a monad P w e asso ciate a t yp e of module morphisms modhom_from_arity ar M . Represen ting ar in M then means giving a term of t yp e modhom_from_arity ar M . In Def. 35 we hav e defined re- presentations in monads only . Indeed w e instan tiate M with the tautological mo dule later. Variable P : RMonad Delta. Definition modhom_from_arity (M : RModule P Ord) (ar : [nat]) : Type := RModule_Hom (prod_mod M ar) M. F or the rest of the section, a representation S is fixed via a Co q section v ariable: Variable S : Signature. As just men tioned, representing the signature S in a monad P (cf. Def. 35 ) means pro viding a suitable mo dule morphism for an y arity of S , i.e. providing, for any elemen t of the indexing set sig_index S , a term of t yp e modhom_from_arity P ( sig i) : Definition Repr (P : RMonad Delta) := forall i : sig_index S, modhom_from_arity P (sig i). Record Representation := { rep_monad :> RMonad Delta ; repr : Repr rep_monad }. Note that the pro jecton rep_monad is declared as a c o er cion via the sp ecial syntax :> . This co ercion allows for abuse of notation in Co q as we do informally according to Def. 35 , cf. Sec. 4.6 for a use of this coercion. 4.3. Morphisms of Represen tations. A morphism of representations from P to Q ist given by a monad morphism f : P → Q b et w een the underlying monads suc h that a diagram commutes for any arit y , cf. Def. 39 . The main task in the implemen tation is to define this diagram for a given arit y ` , and, more sp ecifically , the left vertical morphism dom( `, f ) = f ` : P ` → f ∗ Q ` , using the notation of Def. 27 . Since the carrier of P ` is defined as an inductiv e type, it mak es sense to define f ` b y recursion on the inductiv e t yp e underlying P ` , named prod_mod_c P V l : 24 BENEDIKT AHRENS Variables P Q : RMonad Delta. Variable f : RMonad_Hom P Q. Fixpoint Prod_mor_c (l : [nat]) (V : TYPE) (X : prod_mod_c ( fun V => P V) V l) : (prod_mod_c _ V l) := match X in prod_mod_c _ _ l return f* (prod_mod Q l) V with | TTT => TTT _ _ | elem -:- elems => f _ elem -:- Prod_mor_c elems end . Pro ving this map monotone is an easy exercise, as w ell as its commutation prop erty with substitution, yielding the aforementioned mo dule morphism. No w we hav e all the ingredien ts we need in order to define the diagram of Def. 39 . F or an arity a the diagram reads as follows: Variable a : [nat]. Variable RepP : modhom_from_arity P a. Variable RepQ : modhom_from_arity Q a. Notation "f * M" := (# (PbRMOD f _ ) M). Definition commute := Prod_mor a ;; f * RepQ == RepP ;; f^. Here f^ denotes the mo dule morphism induced b y a monad morphism, cf. Def. 21 . Using the preceding definition, we define morphisms of representations of S : Variables P Q : Representation. Class Representation_Hom_struct (f : RMonad_Hom P Q) := repr_hom_s : forall i : sig_index S, commute f (repr P i) (repr Q i). Record Representation_Hom : Type := { repr_hom_c :> RMonad_Hom P Q; repr_hom :> Representation_Hom_struct repr_hom_c }. 4.4. Category of Representations. In this section we describ e in more detail the category of representations of a 1–signature, cf. Def. 41 . The comp osition of morphisms of representations f : P → Q and g : Q → R is essentially done b y comp osing the underlying monad morphisms. One has to sho w that this morphism indeed commutes with the representation morphisms of P and R . Similarly , the iden tity monad morphism of (the monad underlying) a representation P yields a morphism of representations. F ed with some suitable lemma, the Program framew ork do es the job for us: Program Instance Rep_comp_struct : Representation_Hom_struct (RMonad_comp f g). Program Instance Rep_Id_struct : Representation_Hom_struct ( RMonad_id P). Since equalit y of morphisms of represen tations is defined as equalit y of the underlying monad morphisms, the categorical prop erties of comp osition are established already as part of the definition of the category RMONAD F for any functor F . MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 25 Program Instance REP_struct : Cat_struct (@Representation_Hom S) := { mor_oid a c := eq_Rep_oid a c; id a := Rep_Id a; comp P Q R f g := Rep_Comp f g }. Definition REP := Build_Cat REP_struct. 4.5. Initialit y without Inequations. W e construct the initial ob ject of the cate- gory REP . In the informal pro of of Thm. 43 this initial ob ject is the image under a left adjoint of the initial ob ject in a category of representations as defined by Hirsc howitz and Maggesi ( 2007a ). F or the formal pro of we decide to implemen t the initial ob ject of REP directly , in order to obtain a compact formalization. Inductive UTS (V : TYPE) : TYPE := | Var : V -> UTS V | Build : forall (i : sig_index S), UTS_list V (sig i) -> UTS V with UTS_list (V : TYPE) : [nat] -> Type := | TT : UTS_list V nil | constr : forall b bs, UTS (V ** b) -> UTS_list V bs -> UTS_list V (b::bs). Notation "a -::- b" := (constr a b). Definition UTS_sm V := Delta (UTS V). W e define renaming and, built on top of renaming, substitution: Fixpoint rename (V W: TYPE ) (f : V ---> W) (v : UTS V):= match v in UTS _ return UTS W with | Var v => Var (f v) | Build i l => Build (l //-- f) end with list_rename V t (l : UTS_list V t) W (f : V ---> W) : UTS_list W t := match l in UTS_list _ t return UTS_list W t with | TT => TT W | constr b bs elem elems => elem //- f ^^ b -::- elems //-- f end where "x //- f" := (rename f x) and "x //-- f" := (list_rename x f). Fixpoint subst (V W : TYPE) (f : V ---> UTS W) (v : UTS V) : UTS W := match v in UTS _ return UTS _ with | Var v => f v | Build i l => Build (l >>== f) end with list_subst V W t (l : UTS_list V t) (f : V ---> UTS W) : UTS_list W t := 26 BENEDIKT AHRENS match l in UTS_list _ t return UTS_list W t with | TT => TT W | elem -::- elems => elem >== _lshift f -::- elems >>== f end where "x >== f" := (subst f x) and "x >>== f" := (list_subst x f). Renaming and substitution as just defined correspond to functoriality and monadic substitution for representations in monads as done by Hirsc howitz and Maggesi ( 2007a ). W e, according to Lem. 4 , hav e to apply the functor Delta in some places: Program Instance UTS_sm_rmonad : RMonad_struct Delta UTS_sm := { rweta c := #Delta (@Var c); rkleisli a b f := #Delta (subst f) }. Canonical Structure UTSM := Build_RMonad UTS_sm_rmonad. The monad UTSM is easily equipp ed with a represen tation of the signature S ; the carrier of the represen tation of i : sig_index S is given b y the function fun (X : prod_mod_c _ V (sig i)) => Build (i:=i) (UTSl_f_pm (V:=V) X ) that is, by the constructor Build i of the type UTS , precomp osed with an isomor- phism UTSl_f_pm from prod_mod_c UTS to UTS_list . W e thus obtain a represen- tation UTSRepr of the signature S . Giv en another representation, say , R , of S , the morphism init from UTSRepr to R is defined b y recursion: Fixpoint init V (v : UTS V) : R V := match v in UTS _ return R V with | Var v => rweta (RMonad_struct := R) V v | Build i X => repr R i V (init_list X) end with init_list l (V : TYPE) (s : UTS_list V l) : prod_mod R l V := match s in UTS_list _ l return prod_mod R l V with | TT => TTT _ _ | elem -::- elems => init elem -:- init_list elems end . This map init is compatible with lifting and substitution in UTSM and R , resp ectively: Lemma init_lift V x W (f : V ---> W) : init (x //- f) = rlift R f (init x). Lemma init_kleisli V (v : UTS V) W (f : Delta V ---> UTS_sm W) : init (v >== f) = rkleisli (f ;; @init_sm W) (init v). where init_sm W is the (trivially) monotone version of init W — the adjunct of init W under the adjunction of Def. 3 . The latter of those lemmas constitutes an imp ortan t part of the pro of that init is the carrier of a module morphism from UTSM to R . It is trivial to pro v e that init is also compatible with the representation MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 27 structure of UTSRepr and R , thus the carrier of a morphism of represen tations called init_rep : UTSRepr ---> R . Afterw ards unicit y of init_rep is prov ed: Lemma init_unique : forall f : UTSRepr ---> R , f == init_rep. Finally we establish initiality b y an instance declaration of the corresp onding class. The pro of field of the class stating unicity is filled automatically by the Program framew ork, using the aforemen tioned lemma: Program Instance UTS_initial : Initial (REP S) := { Init := UTSRepr ; InitMor R := init_rep R }. 4.6. Inequations and Initial Represen tation of a 2–Signature. F or a 1– signature S , an S -mo dule is defined to be a functor from representations of S to the category whose ob jects are pairs of a monad P and a mo dule M o ver P , cf. Def. 45 . The use of the cumbersome category LMo d (∆ , wPre ) of pairs ensures that representations in a monad P go to P –mo dules. In Co q we represent this dep endency via a dep enden t type. W e do not mak e use of the functor prop erties of S –mo dules. The b elo w definition makes use of tw o c o er cions . Firstly , we may write a : C b ecause the “ob ject” pro jection of the category record (whose definition we omit) is declared as a coercion. Secondly , the monad underlying an y represen tation can b e accessed without explicit projection using the co ercion w e mentioned ab ov e. Record S_Module := { s_mod :> forall R : REP S, RMOD R wOrd ; s_mod_hom :> forall (R T : REP S)(f : R ---> T), s_mod R ---> PbRMod f (s_mod T) }. Notation "U @ f" := (s_mod_hom U f)(at level 4). Note that w e write U@f for the image of the morphism of representations f under the S –mo dule U . Source and target module of f are implicit arguments in this application. A half-equation is a natural transformation b etw een S -mo dules. W e need the naturalit y condition in the follo wing. Since we hav e not formalized S -mo dules as functors, we hav e to state naturality explicitly: Class half_equation_struct (U V : S_Module) (half_eq : forall R : REP S, U R ---> V R) := { comm_eq_s : forall (R T : REP S) (f : R ---> T), U @ f ;; PbRMod_Hom _ (half_eq T) == half_eq R ;; V @ f }. Record half_equation (U V : S_Module) := { half_eq :> forall R : REP S, U R ---> V R ; half_eq_s :> half_equation_struct half_eq }. W e no w formalize classic S –mo dules. An y list of natural num bers sp ecifies uniquely a classic S –mo dule, cf. Def. 49 . Given a list of naturals codl , we call this S –mo dule S_Mod_classic codl . A classic half–e quation is a half–equation with a classic codomain, and a classic inequation is a pair of parallel classic half–equations (cf. Def. 56 ): 28 BENEDIKT AHRENS Definition half_eq_classic (U : S_Module)(codl : [nat]) := half_equation U (S_Mod_classic codl). Record ineq_classic := { Dom : S_Module ; Cod : [nat] ; eq1 : half_eq_classic Dom Cod ; eq2 : half_eq_classic Dom Cod }. Giv e a representation P and a (classic) inequation e , w e chec k whether P satisfies e b y point wise comparison (cf. Def. 54 ): Definition satisfies_ineq (e : ineq_classic) (P : REP S) := forall c (x : Dom e P c), eq1 _ _ _ x << eq2 _ _ _ x. Definition Inequations (A : Type ) := A -> ineq_classic. Definition satisfies_ineqs A (T : Inequations A) (R : REP S) := forall a, satisfies_ineq (T a) R. W e formalize sets of classic inequations as pairs of an indexing t yp e A together with a term of t yp e Inequations A , that is, a map from A to the type of classic inequations ineq_classic . The category of representations of ( S, A ) is obtained as a full sub category of the category of representations of S . The following declaration pro duces a subcategory from predicates on the type of representations and on the (dep enden t) type of morphisms of represen tations, yielding the category PROP_REP of representations of ( S, A ) : Variable A : Type . Variable T : Inequations A. Program Instance Ineq_Rep : SubCat_compat (REP S) ( fun P => satisfies_ineqs T P) ( fun a b f => True). Definition INEQ_REP : Cat := SubCat Ineq_Rep. W e no w construct the initial ob ject of INEQ_REP . The relation on the initial ob ject is defined precisely as in the paper pro of, cf. Eq. ( 3.1 ): Definition prop_rel_c X (x y : UTS S X) : Prop := forall R : PROP_REP, init (FINJ _ R) x << init (FINJ _ R) y. Here, FINJ _ R denotes the representation R as a representation of S , i.e. the injection of R in the category REP S of represen tations of S . The relation defined ab o v e is indeed a preorder, and w e define the monad UTSP to b e the monad whose underlying sets are identical to UTSM , namely the sets defined b y UTS , but equipp ed with this new preorder. This monad UTSP is denoted by Σ A in the pap er pro of. The represen tation mo dule morphisms of the initial represen tation UTSRepr can b e reused after having pro v ed their compatibility with the new order, yielding a representation UTSProp . This represen tation satisfies the inequations of T : Lemma UTSPRepr_sig_prop : satisfies_ineqs T UTSProp. W e explicitly inject the representation into the category of representations of ( S, A ) : Definition UTSPROP : INEQ_REP := exist ( fun R : Representation S => satisfies_ineqs T R) UTSProp UTSPRepr_sig_prop. MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 29 In order to build the initial morphism tow ards any representation R : INEQ_REP , w e first build the corresp onding morphism in the category of represen tations of S : Definition init_prop_re : UTSPropr ---> (FINJ _ R) := ... whic h we then inject, analoguously to the initial represen tation, into the sub category of representations of ( S, A ) : Definition init_prop : UTSPROP ---> R := exist _ (init_prop_re R) I. Finally we obtain our Thm. 57 . An initial object of a category is given b y an ob ject Init of this category , a map asso ciating go an y object R a morphism InitMor R : Init ---> R , and a pro of of uniqueness of any such morphism. W e instantiate the type class Initial for the category INEQ_REP of representations of ( S, A ) : Program Instance INITIAL_INEQ_REP : Initial INEQ_REP := { Init := UTSPROP ; InitMor := init_prop ; InitMorUnique := init_prop_unique }. W e c heck its type after closing all the sections, thus abstracting from the section v ariables: Check INITIAL_INEQ_REP. INITIAL_INEQ_REP : forall (S : Signature) (A : Type ) (T : Inequations S A), Initial (INEQ_REP (S:=S) (A:=A) T) 4.7. Λ β : Lambda Calculus with β reduction. W e implemen t the example 2– signature Λ β , cf. Ex. 55 . Throughout this section, we use use a custom notation in Co q for the datatype of lists: Notation "[[ x ; .. ; y ]]" := (cons x .. (cons y nil) ..). 4.7.1. The 1–Signatur e Λ . In order to sp ecify the 1–signature Λ (cf. Ex. 33 ), we first define an indexing set Lambda_index consisting of tw o elements, ABS and APP . This indexing set reflects the fact that the signature Λ consists of t w o arities. The record instance Lambda is a term of type Signature . The map sig Lambda then asso ciates the corresp onding lists of naturals to eac h of these elements, according to Def. 32 : Inductive Lambda_index := ABS | APP. Definition Lambda : Signature := {| sig_index := Lambda_index ; sig := fun x => match x with | ABS => [[ 1 ]] | APP => [[ 0 ; 0]] end |}. 30 BENEDIKT AHRENS 4.7.2. The Λ –Ine quation β . The definition of the inequation β (cf. Ex. 55 ) is a more c hallenging task, since a half–equation is not just combinatory data like a 1–arit y , but given by suitable mo dule morphisms. A t first, we define the substitution of one v ariable (cf. Def. 50 ) as a half–equation. The carrier subst_carrier of the substitution is defined as in Def. 30 . Afterw ards w e prov e that this carrier satisfies the prop erties of a mo dule morphism, that is, is compatible with substitution in the source and target mo dules. After abstracting from the section v ariable R , we obtain a function subst_module_mor whic h, giv en an y representation R of S , yields the substitution mo dule morphism asso ciated to (the monad underlying) R . Variable S : Signature. Variable R : REP S. Definition subst_carrier : ( forall c : TYPE, (S_Mod_classic_ob [[1; 0]] R) c ---> (S_Mod_classic_ob [[0]] R) c) := ... Program Instance sub_struct : RModule_Hom_struct (M:=S_Mod_classic_ob [[1 ; 0]] R) (N:=S_Mod_classic_ob [[0]] R) subst_carrier. Definition subst_module_mor := Build_RModule_Hom (sub_struct R). The last step is to pro v e “naturalit y” . W e recall that we do not implement S – mo dules as functors, but just as the data part of functors. This is wh y we put the w ord natur ality in quotes. After the pro of we define our first half–equation, subst_half_eq . Program Instance subst_half_s : half_equation_struct (U:= S_Mod_classic [[1 ; 0]]) (V:= S_Mod_classic [[0]]) subst_module_mor. Definition subst_half_eq := Build_half_equation subst_half_s. The definition of the second half–equation of Ex. 51 is possible for an y 1–signature with abstraction and application, such as the 1–signature Λ . T o k eep the example simple, we only define the half–equation for Λ . The needed steps are precisely the same as for the substitution half–equation, so we just giv e the statements. Definition beta_carrier : ( forall c : TYPE, (S_Mod_classic_ob [[1; 0]] R) c ---> (S_Mod_classic_ob [[0]] R) c) := ... Program Instance beta_struct : RModule_Hom_struct (M:=S_Mod_classic_ob [[1 ; 0]] R) (N:=S_Mod_classic_ob [[0]] R) beta_carrier. Definition beta_module_mor := Build_RModule_Hom beta_struct. Program Instance beta_half_s : half_equation_struct (U:=S_Mod_classic Lambda [[1 ; 0]]) (V:=S_Mod_classic Lambda [[0]]) beta_module_mor. Definition beta_half_eq := Build_half_equation beta_half_s. MODULES OVER RELA TIVE MONADS FOR SYNT AX AND SEMANTICS 31 W e pac kage b oth half–equations into one inequation, the b eta rule of Ex. 55 : Definition beta_rule : ineq_classic Lambda := {| eq1 := beta_half_eq ; eq2 := subst_half_eq Lambda |}. W e can no w asso ciate a short name to the category of representations of Λ β , where, for increased clarity , we sp ecify the implicit argumen ts: Definition Lambda_beta_Cat := INEQ_REP (S:=Lambda)(A:=unit)( fun x : unit => beta_rule). Our formal definition allo ws that an inequation appears multiple times in a 2– signature, whereas in the informal definition w e ha ve sets of inequations. Unlike for arities, ha ving several copies of the same inequation do es not change the re- sulting category or its initial object. The initial representation is obtained via the sp ecification Definition Lambda_beta := @Init _ _ _ (INITIAL_INEQ_REP ( fun x : unit => beta_rule)). 5. Conclusions & Future W ork W e hav e presented an initialit y result for abstract syntax whic h integrates seman- tics sp ecified by reduction rules by means of preorders. It is based on relative monads and mo dules o v er such monads. Reduction rules are sp ecified by ine quations , whose definition is largely inspired by Hirscho witz and Maggesi’s equations (Hirscho witz and Maggesi 2007b ). F or any 2–signature ( S, A ) with classic inequations, w e con- struct the initial ob ject in the category of representations of ( S, A ) . The theorem is pro ved formally in the proof assistant Co q . On another line of w ork (Ahrens 2012a ) we hav e extended Zsidó’s initiality result (Zsidó 2010 , Chap. 6) for simply–t yp ed syn tax to allow for mo dels o ver varying ob ject types. In this wa y initialit y accounts for translation b et w een languages ov er differen t types. Both lines of w ork, v arying ob ject types and the in tegration of op erational semantics, can b e com bined: in (Ahrens 2012b ) w e presen t an initiality result which allows for sp ecification of reductions as well as c hange of ob ject t yp es. As an example, we consider the language PCF with its usual small–step semantics. By equipping LC β with a represen tation of PCF, we obtain a translation of PCF to LC β whic h is faithful with respect to seman tics. Our approac h should b e extended to more complex type systems featuring dep endent t yp es or p olymorphism. Another interesting feature to work on are c onditional reductions/rewritings. A more fine–grained account of reduction might b e given by considering gr aphs instead of preordered sets. A cknow le dgments. W e are grateful to André Hirsc ho witz and Marco Maggesi for in v aluable discussions. W e thank the anonymous referees for their careful reading and suggestions, which led to several improv emen ts in the presen tation. W e finally thank Thorsten Altenkirch and Conor McBride for their editorial w ork. 32 BENEDIKT AHRENS References Ahrens, Benedikt (2012a). “Extended Initiality for Typed Abstract Syntax”. In: L o gic al Metho ds in Computer Scienc e 8.2, pp. 1 –35. doi : 10 .2168 / LMCS - 8(2: 1)2012 . – (2012b). “Initialit y for Typed Syntax and Seman tics”. In: W oLLIC . Ed. b y C.-H. Luk e Ong and Ruy J. G. B. de Queiroz. V ol. 7456. Lecture Notes in Computer Science. Springer, pp. 127–141. isbn : 978-3-642-32620-2. Ahrens, Benedikt and Julianna Zsidó (2011). “Initial Seman tics for higher–order t yp ed syntax in Coq”. In: Journal of F ormalize d R e asoning 4.1, pp. 25–69. Altenkirc h, Thorsten and Bernhard Reus (1999). “Monadic presentations of lam b da terms using generalized inductive types”. In: Computer Scienc e L o gic, 13th International W orkshop, CSL ’99 , pp. 453–468. Altenkirc h, Thorsten, James Chapman, and T armo Uustalu (2010). “Monads Need Not Be Endofunctors”. In: FOSSA CS . Ed. by C.-H. Luke Ong. V ol. 6014. Lecture Notes in Computer Science. Springer, pp. 297–311. isbn : 978-3-642-12031-2. Barendregt, Henk and Erik Barendsen (1994). Intr o duction to L amb da Calculus . ftp://ftp.cs.ru.nl/pub/CompMath.F ound/lamb da.pdf . revised 2000. Bird, Richard S. and Lam b ert Meertens (June 1998). “Nested Datat yp es”. In: LNCS 1422: Pr o c e e dings of Mathematics of Pr o gr am Construction . Ed. b y Johan Jeuring. Marstrand, Sweden: Springer-V erlag, pp. 52–67. Co q (2010). The Co q Pr o of A ssistant . http://co q.inria.fr . url : http://coq.inria.fr . F ernández, Marib el and Murdo c h J. Gabbay (June 2007). “Nominal rewriting”. In: Information and Computation 205.6, pp. 917–965. doi : 10.1016/j.ic.2006.12.002 . Fiore, Marcelo (2002). “Seman tic analysis of normalisation b y ev aluation for typed lam b da calculus”. In: Pr o c e e dings of the 4th A CM SIGPLAN international c onfer- enc e on Principles and pr actic e of de clar ative pr o gr amming . PPDP ’02. Pittsburgh, P A, USA: ACM, pp. 26–37. isbn : 1-58113-528-9. doi : http : / / doi . acm . org / 10 . 1145/571157.571161 . – (2005). “Mathematical Mo dels of Computational and Combinatorial Structures”. In: F oSSaCS . Ed. by Vladimiro Sassone. V ol. 3441. Lecture Notes in Computer Science. Springer, pp. 25–46. isbn : 3-540-25388-2. Fiore, Marcelo and Chung-Kil Hur (2007). “Equational Systems and F ree Construc- tions (Extended Abstract)”. In: ICALP . Ed. by Lars Arge, Christian Cac hin, T omasz Jurdzinski, and Andrzej T arlec ki. V ol. 4596. Lecture Notes in Computer Science. Springer, pp. 607–618. isbn : 978-3-540-73419-2. Fiore, Marcelo, Gordon Plotkin, and Daniele T uri (1999). “Abstract Syntax and V ariable Binding”. In: Pr o c e e dings of the 14th A nnual IEEE Symp osium on L o gic in Computer Scienc e . LICS ’99. W ashington, DC, USA: IEEE Computer So ciety, pp. 193–202. isbn : 0-7695-0158-3. Gabba y , Murdo c h and Andrew Pitts (1999). “A New Approach to Abstract Syn tax In volving Binders”. In: Pr o c e e dings of the 14th A nnual IEEE Symp osium on L o gic in Computer Scienc e . LICS ’99. W ashington, DC, USA: IEEE Computer So ciety, pp. 214–224. isbn : 0-7695-0158-3. Gabba y , Murdo c h J. and A. M. Pitts (2001). “A New Approac h to Abstract Syn tax with V ariable Binding”. In: F ormal A sp e cts of Computing 13.3–5, pp. 341–363. doi : http://doi.ieeecomputerso ciety .o rg/10.1109/LICS.1999.782617 . Ghani, Neil and Christoph Lüth (2003). “Rewriting Via Coinserters”. In: Nor d. J. Comput. 10.4, pp. 290–312. REFERENCES 33 Hirsc howitz, André and Marco Maggesi (2007a). “Modules o ver Monads and Linear- it y”. In: W oLLIC . Ed. by Daniel Leiv ant and Ruy J. G. B. de Queiroz. V ol. 4576. Lecture Notes in Computer Science. Springer, pp. 218–237. isbn : 978-3-540-73443- 7. – (2007b). “The algebraicity of the lambda-calculus”. In: CoRR abs/0704.2900. url : http://arxiv.o rg/abs/0704.2900 . – (2010). “Modules ov er monads and initial seman tics”. In: Inf. Comput. 208.5, pp. 545–564. Hirsc howitz, T om (2011). “Cartesian closed 2-categories and permutation equiv alence in higher-order rewriting”. 19 pages, submitted. url : http://hal.a rchives- ouvertes. fr/hal- 00540205/en/ . Hofmann, Martin (1999). “Seman tical Analysis of Higher-Order Syntax”. In: In 14th A nnual Symp osium on L o gic in Computer Scienc e . IEEE Computer So ciety Press, pp. 204–213. Hur, Ch ung-Kil (2010). “Categorical equational systems: algebraic models and equational reasoning”. PhD thesis. Universit y of Cambri dge, UK. Mac Lane, Saunders (1998). Cate gories for the working mathematician . Second. V ol. 5. Graduate T exts in Mathematics. New Y ork: Springer-V erlag, pp. xii+314. isbn : 0-387-98403-8. Miculan, Marino and Iv an Scagnetto (2003). “A framew ork for t yp ed HOAS and seman tics”. In: PPDP . A CM, pp. 184–194. isbn : 1-58113-705-2. Pitts, A. M. (2003). “Nominal Logic, A First Order Theory of Names and Binding”. In: Information and Computation 186, pp. 165–193. P ow er, John (Apr. 2007). “Abstract Syntax: Substitution and Binders”. In: Ele ctr on. Notes The or. Comput. Sci. 173, pp. 3–16. issn : 1571-0661. doi : 10.1016/j.entcs. 2007.02.024 . T anaka, Miki and John P o wer (2005). “A unified category-theoretic formulation of t yp ed binding signatures”. In: Pr o c e e dings of the 3r d A CM SIGPLAN workshop on Me chanized r e asoning ab out languages with variable binding . MERLIN ’05. T allinn, Estonia: A CM, pp. 13–24. isbn : 1-59593-072-8. doi : http: //doi. acm.org/ 10.1145/1088454.1088457 . Zsidó, Julianna (2010). “Typed Abstract Syn tax”. http :/ /tel .archives- ouvertes.fr/tel- 00535944/ . PhD thesis. Univ ersit y of Nice, F rance.
Original Paper
Loading high-quality paper...
Comments & Academic Discussion
Loading comments...
Leave a Comment