Type Systems for Nominal Terms Elliot Fairweather Department of Informatics King’s College London a thesis submitted for the degree of Doctor of Philosophy 2014 for my father Acknowledgements On a personal note, I should like to thank Ben for his true and enduring friendship. Professionally, I thank Jamie Gabbay for the use of his LATEXcommand for the ‘new’ symbol, James Cheney for directing me to the reference concerning de Bruijn-style systems and José Meseguer for his explanation of the type system of Maude. I am most grateful to Jesús Domı́nguez-Álvarez for his help in testing both my theoretical and practical work and also to my examiners, Andrew Pitts and Zhaohui Luo, for their helpful and enjoyable assessment of my thesis. Most importantly, I should like to express my utmost gratitude to Maribel, without whose insight and encouragement I should not have succeeded. Abstract This thesis concerns types systems for nominal terms, a new syntax, close to informal practice, for representing and reasoning about formal languages that use binders. Nominal techniques allow a first-order approach to binding in formal languages, providing direct access to both binders and bound variables and a formal axiomatisation of α-equivalence. This approach is promising, not least because it has been shown that unification and matching of nominal representations is both decidable and tractable, giving rise to nominal models of computation and programming languages. Nominal terms, a nominal extension of first-order terms, are now well studied, particularly in the context of equational reasoning. However, type systems for nominal terms have not yet been extensively researched. The development of type systems for nominal terms allows the application of the nominal approach to binding to the areas of specification and verification. Programming languages and environments based upon certifying type systems facilitate formal descriptions of operational semantics and the implementation of efficient compilers. Such features are increasingly important, particularly in critical domains where mathematical certainty is a necessity, such as medicine, telecommunications, transport and defence. This work first defines three variations on a simple type system for nominal terms in the style of Church’s simply typed lambda calculus. An ML-style polymorphic type system is then studied for which a type inference algorithm is provided and implemented. This type system is then applied to equational theories. Two formulations of typed rewriting are presented, one more expressive and one more efficient. Finally, a dependent type system is given for nominal terms extended with atom substitution, with a view to developing a nominal logical framework. Contents 1 Introduction 1 2 Preliminaries 3 2.1 Lambda Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.2 2.3 Type Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Rewriting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 7 2.4 Nominal Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3 Simple Types for Nominal Terms 21 3.1 Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.2 Type System in the Style of Church . . . . . . . . . . . . . . . . . . . . . . 23 3.3 Type System in a Church-like Style . . . . . . . . . . . . . . . . . . . . . . . 25 3.4 Type System in the Style of de Bruijn . . . . . . . . . . . . . . . . . . . . . 27 4 Polymorphic Types for Nominal Terms 32 4.1 Polymorphic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 4.2 Type System in the Style of Curry . . . . . . . . . . . . . . . . . . . . . . . 35 4.3 Inference of Principal Solutions . . . . . . . . . . . . . . . . . . . . . . . . . 41 5 Typed Nominal Equational Theories 47 5.1 Typed Nominal Rewriting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.2 Typed Closed Nominal Rewriting . . . . . . . . . . . . . . . . . . . . . . . . 51 5.3 Typed Nominal Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 5.4 Further Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6 Dependent Types for Nominal Terms with Atom Substitutions 63 6.1 Nominal Terms with Atom Substitutions and Dependent Types . . . . . . . 64 6.2 Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 6.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 6.4 Adequacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 iv CONTENTS 7 Implementation 91 7.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 7.2 Simple Types for Nominal Terms . . . . . . . . . . . . . . . . . . . . . . . . 92 7.3 Polymorphic Types for Nominal Terms . . . . . . . . . . . . . . . . . . . . . 92 7.4 Typed Nominal Equational Theories . . . . . . . . . . . . . . . . . . . . . . 93 7.5 Dependent Types for Nominal Terms with Atom Substitutions . . . . . . . 93 8 Related Work 95 8.1 Syntax with Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 8.2 Nominal Systems and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 8.3 Rewriting and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 9 Conclusions and Future Work References 99 101 v Chapter 1 Introduction his thesis concerns the intersection of two areas of research, that of syntax with T binding and type systems. The concept of binders is pervasive in the study of formal languages. Existing methodologies in this area are often founded upon higher-order abstract syntax, using lambda calculi to encode each binder as a functional abstraction, and have several disadvantages. Bound names may not be directly accessible in such systems and inductive reasoning over expressions containing binders is not easily supported. Nominal techniques, [Gabbay, 2011a; Gabbay and Pitts, 1999], offer a new approach to reasoning about name binding in formal languages, allowing direct access to binders and named bound variables and use the properties of permutations of names to simplify reasoning about object language terms equivalent up to the renaming of bound names. This ‘α-equivalence’ over terms can be provided as a formal, built-in axiomatisation and allows an approach to structural induction modulo α-equivalence similar to that used in informal practice, [Pitts, 2006]. The approach has already enjoyed success in both theory and practice. Unification and matching of nominal representations has been shown to be decidable, [Urban et al., 2004] and linear time algorithms for matching have been developed, [Calvès and Fernández, 2010]. As a result, both logic-based and functional programming languages that integrate nominal techniques have already been implemented, [Cheney and Urban, 2003; Shinwell, 2005]. Nominal terms, first introduced in [Urban et al., 2004], are a nominal extension of first-order terms and as such constitute a meta-language for reasoning about named variables of an object language in the presence of meta-level variables. They have been studied extensively, particularly in the context of equational reasoning, [Fernández and Gabbay, 2007b]. Type systems are another important area within the study of formal languages. Types are not only employed for many reasons as a practical aid in programming, [Pierce, 2002], but also, on a theoretical level, are intrinsically linked to the specification and verification 1 of program properties through constructivist logics and the Curry-Howard isomorphism. This is an area of increasing importance; critical domain software, such as that used in the fields of medicine, telecommunications, transport and defence, often demands mathematical certainty of program correctness. Thus there is a corresponding demand for the development of type systems of increasing power and sophistication for use in certified programming environments. However, most applications using a nominal approach to abstract syntax have so far made use of quite simple type systems. This thesis contributes several more advanced type systems for nominal terms. Firstly, three variations on a simple type system for nominal terms in the style of Church’s simply typed lambda calculus are considered in chapter 3. An ML-style polymorphic type system is then studied in chapter 4 for which a type inference algorithm is provided. This type system is then applied to equational theories in chapter 5. There, two formulations of typed nominal rewriting are presented, one more expressive and one more efficient, and also a formulation of typed nominal algebra. An earlier version of material presented in chapters 4 and 5 was collaborated on with Murdoch J. Gabbay and Maribel Fernández and published in [Fairweather et al., 2011]. Finally, in chapter 6, a dependent type system is given for nominal terms extended with atom substitution, with a view to defining a nominal logical framework in the future. Material in this chapter was developed as part of a Royal Society International Joint Project and in collaboration with Maribel Fernández and Nora Szasz and Alvaro Tasistro of University ORT Uruguay. Alongside these theoretical contributions, a majority of the type systems and other material covered here has been implemented by the author; this practical aspect of the thesis is described in more detail in chapter 7. Chapter 8 discusses the content of this thesis in the context of other related research. Chapter 9 consists of some concluding remarks together with proposed directions for future work based upon the work of this thesis. The next chapter, chapter 2, gives introductions to the theory of type systems, rewriting and the nominal approach to syntax with binding. 2 Chapter 2 Preliminaries I n the interest of completeness, this chapter gives brief introductions to four areas of theory, frequently referenced in the text below. Section 2.1 discusses the lambda calculus, one of the archetypical models of computation and a standard example of a formal system that uses binding. Section 2.2, introduces the theory of type systems and section 2.3 that of rewriting. Finally, section 2.4 gives a detailed description of nominal terms. 2.1 Lambda Calculus The lambda calculus was originally conceived by Church as a description of higher-order logic, [Church, 1932, 1933]. After being proved inconsistent and unfit for such a purpose by Church’s students, Kleene and Rosser, [Kleene and Rosser, 1935], a fragment of the calculus was subsequently used by Church to capture the idea of computable functions and answer negatively Hilbert’s Entscheidungsproblem, [Church, 1936]. The subject of extensive research ever since, the conceptual simplicity of the lambda calculus has made it the de facto starting point for the study of computation. The lambda calculus and its many extensions also form the foundation of the school of functional programming, increasingly popular due to the power derived from its formal mathematical basis. A brief introduction to some of the theory of lambda calculus follows below. 2.1.1 Syntax Let x, y, z, . . . range over a countably infinite set of variables, X, and then define lambda terms using the following grammar. 3 2.1 Lambda Calculus Definition 2.1 (Lambda Terms) M, N ::= x | λx.M | (M N ) Call these second and third constructions respectively, an abstraction and an application. Following convention, let application have precedence over abstraction, and let abstractions associate to the right and applications to the left; repeated abstractions may be contracted and bracketing omitted accordingly. The following example gives lambda terms representing the S, K and I combinators of combinatory logic, [Curry et al., 1972]. Example 2.1 (Lambda Terms) λx.λy.λz.(x y (x z)) λx.λy.x λx.x Call a variable occurring under an abstraction of a variable of the same name bound and otherwise free. The sets of bound and free variables occurring in a term, M , written bvars(M ) and fvars(M ), respectively, can be defined by simple inductive definitions. Substitution is a primitive operation of the lambda calculus. Read M [N/x] as ‘N replaces x in M ’. Note that only free variables are replaced. Here and below, ,, is used to denote definitional equality. Definition 2.2 (Substitution) x [S/x] , S y [S/x] , y (M N ) [S/x] , M [S/x] N [S/x] (λx.M ) [S/x] , λx.M (λy.M ) [S/x] , λy.(M [S/x]) (y 6∈ fvars(S)) One of Church’s original axioms for the theory of the lambda calculus was that of α-equivalence. Definition 2.3 (α-equivalence) λx.M =α λy.M [y/x] (y 6∈ fvars(M )) The condition upon the final rule in definition 2.2 above is the cause of many problems, not least the inability to use structural induction over the lambda calculus, [Pitts, 2006], which led to the development of the nominal approach to abstract syntax, [Gabbay, 2001; Gabbay and Pitts, 1999]. 4 2.2 Type Systems In the study of the lambda calculus, it is usual to circumvent this issue either by taking the quotient of the set of lambda terms by α-equivalence, that is, to consider terms equivalent modulo the choice of bound names, or to use Barendregt’s ‘variable convention’, whereby the names of all bound variables in a term are chosen to be different from those that are free. The standard lambda calculus theory is that of β-equality or βη-equality. Definition 2.4 (βη-equivalence) (λx.M ) N =β M [N/x] λx.(M x) =η M 2.2 x 6∈ fvars(M ) Type Systems Type systems were first studied as a means to overcome the paradoxes of set theory, [Russell and Whitehead, 1910, 1912, 1913] and were later incorporated into the lambda calculus by Church, [Church, 1940]. In addition to extensive subsequent research from this theoretical standpoint, type systems were later applied to the area of programming languages and remain one of the greatest achievements of theoretical computer science in practical use. Type systems provide a tractable, syntactic method to define and check specifications of programs, allowing certain of their behaviours to be reasoned about even before execution, [Pierce, 2002, 2004]. Further advances in the theoretical study of type systems, particularly those arising from the ‘Curry-Howard isomorphism’, [Howard, 1980], a direct correspondence between type systems and logical systems, has allowed the demand to be met for more powerful type systems for programming languages, capable of more advanced specification and verification. Such more advanced type systems are famously summarised for the lambda calculus by Barendregt in [Barendregt, 1991]. There, Barendregt represents the possible interactions between the terms and types of a language in a cube-shaped diagram, fig. 2.1, where each vertex is a type system and each edge represents a directed inclusion relationship. The type system, λ2, is that allowing the representation of generic polymorphism (in which terms may depend upon types), the System F of Girard, [Girard et al., 1989] and Polymorphic Lambda Calculus of Reynolds, [Reynolds, 1974]. λP , is a type system allowing ‘dependent’ types, (types that may depend upon terms), a powerful notion, allowing for the representation of more complex logics, and as such is used as the foundation of the logical framework, LF, [Harper et al., 1987]. The culmination of the cube, the type system, λC, or Calculus of Constructions, [Coquand and Huet, 1988], allows all possible depen5 2.2 Type Systems λ2 λω >6 - λC >6 - λP 2 6 6 λ→ λω > > λP ω - λP Figure 2.1: Barendregt’s ‘Lambda Cube’ dencies between terms and types and is the basis of the theorem prover, Coq, [Chlipala, 2011]. There now follows, as means of an introduction to the notation of type systems, a short description of two approaches to the simply typed lambda calculus, λ→. The simply typed lambda calculus has a number of properties, not least of which is the strong normalization of β-reduction. The first is in the style of Church, [Barendregt et al., 2013; Church, 1940], where terms are typed from the outset, and in fact only well-typed terms are considered to exist, over which any further properties are defined. The second is in the style of Curry, [Barendregt et al., 2013; Curry and Feys, 1958], where elements of the term language are deemed typeable or not only after the definition of other properties of the language. Note that the set of well-typed Church lambda terms is provably equivalent to the set of typeable Curry terms, [Barendregt et al., 2013]. 2.2.1 Simply Typed Lambda Calculus Let B represent a countably infinite set of base types, then let the set of types, T be generated by the following grammar, definition 2.5. Call σ → τ , a function type and let ≡ denote syntactic equality over types. Definition 2.5 (Types) σ, τ ::= B | σ → τ 6 2.3 Rewriting Church-style System Let there be a countably infinite number of countably infinite sets of variables, Xτ , one for each type, τ ∈ T . Let xτ , y τ , z τ , . . . range over elements in Xτ . Then let the set of well-typed lambda terms of type, τ , written Λτ , be constructed as in definition 2.6. Definition 2.6 (Lambda Terms) xτ ∈ Λ τ M ∈ Λτ M ∈ Λσ→τ , N ∈ Λσ ⇒ (λxσ .M ) ∈ Λσ→τ ⇒ (M N ) ∈ Λτ Now, let the set of well-typed lambda terms be defined as S Λτ . τ ∈T Curry-style System Recall the set of untyped lambda terms as defined in definition 2.1. Definition 2.7 (Lambda Terms) M, N ::= x | λx.M | (M N ) Let a typing environment, Γ, be a finite list of pairs of a variable and a type, written (x : τ ) and called type associations. Let Γx represent the right-most occurrence of an association for x in Γ; if no such association exists, Γx is undefined. Let Γ o n (a : τ ) denote the addition of (a : τ ) to the right-hand side of Γ. A typing judgement is a triple, Γ ` M : τ , that says that M is of type τ in the environment, Γ. Derivable judgements are inferred by the rules given in definition 2.8. Definition 2.8 (Typing Judgements) Γx = τ Γ`x: τ Γo n (x : σ) ` M : τ Γ ` (λx.M ) : σ → τ Γ`M : σ→τ Γ`N : τ Γ ` (M N ) : τ Let the set of typeable lambda terms be those for which there exists a derivable typing judgement. 2.3 Rewriting Rewriting is a method of equational reasoning over first-order languages that is based upon the theory of abstract reduction systems. Sets of oriented equational identities on terms of a language or term rewriting systems are used to specify and reason about 7 2.3 Rewriting the dynamic or computational behaviour of those terms. The properties of confluence and termination are of particular interest. Like the lambda calculus, rewriting is a complete model of computation but arguably one that is much more intuitively understandable. An excellent introduction to rewriting can be found in [Baader and Nipkow, 1998]. At the same time, rewriting can also be seen to represent the dynamics of logics and used as a logical framework. This side of rewriting and its applications, many based upon the highly developed rewriting logic programming language Maude, is summarised in [Meseguer, 2012]. There also exist several interpretations of rewriting for languages with binding, called higher-order rewriting. Such systems use the lambda calculus as a meta-language to subsume the issues that arise from binding. The most established system of higher-order rewriting, is that of Combinatory Reduction Systems, [Klop et al., 1993]. There now a follows a brief, semi-formal introduction to some of the concepts and syntax of rewriting. 2.3.1 Term Rewriting Systems Consider pairwise disjoint, countably infinite sets of variables, X, Y, Z, . . . and termformers, f, g, . . .. A signature, Σ, is a set of term-formers, which associates a nonnegative integer arity to each term-former. Term-formers of zero arity can be considered to be constants. The set of terms is generated from the following grammar, where, f n , is a term-former of arity, n. Definition 2.9 (First-order Terms) s, t ::= X | f n (t1 , . . . , tn ) Let, ≡, denote syntactic equality over terms and vars(t), the set of variables occurring in t. Write C [t] to represent a term, C, in which, at one specific position of its syntax tree, occurs the sub-term, t. A substitution, θ, is a finite mapping from variables to terms. The application of a substitution to a term, written as juxtaposition to the right, thus t θ, represents the simultaneous instantiation of those variables of the term, t, in the domain of θ. A matching problem, asks whether given two terms, l and s, such that vars(l) ∩ vars(s) = ∅, there exists a substitution, θ, such that, l θ ≡ s. A rewrite rule is a pair of terms, (l, r), written, l → r, such that, vars(r) ⊆ vars(l). A term rewriting system, R, is a set of rewrite rules over a particular signature. The one-step rewrite relation, generated by a term rewriting system, R, is the least relation of tuples, s →R t, such that, for any rewrite rule, (l → r) of R, and any 8 2.4 Nominal Terms substitution, θ, then, s ≡ C [s0 ], l θ ≡ s0 and C [r θ] ≡ t. Write s R t for the reflexive, transitive closure of this one-step relation. Informally, this says that if a term or part of a term matches the left-hand side of a rewrite rule then it can be rewritten to a corresponding instance of the right-hand side. Consider the following term-formers, 00 , suc1 and add2 , to represent addition over the numbers. The following example gives some first-order terms constructed using this signature. Example 2.2 (First-order terms) 0 suc (0) add (suc (0), suc (suc (0))) suc (suc (0)) suc (add (0, add (suc (0), suc (0)))) Anticipating example 5.4.1, one can give the following term rewriting system to represent primitive recursive addition over the natural numbers. Example 2.3 (Term Rewriting Systems) add (X, 0) → X add (X, suc (Y )) → suc (add (X, Y )) Then, one has the following sequence of rewrite steps to compute ‘2 + 2’, where one applies the second rule of example 2.3 in the first three steps and the second rule in the final step. Example 2.4 (Rewriting) add (suc (suc (0)), suc (suc (0))) → suc (add (suc (suc (0)), suc (0))) → suc (suc (add (suc (suc (0)), 0))) → suc (suc (suc (suc (0)))) Thus, add (suc (suc (0)), suc (suc (0))) suc (suc (suc (suc (0)))), or ‘4’. 2.4 Nominal Terms Nominal terms are a meta-language designed to specify and reason about formal languages that use names and binding, such as logics or programming languages. Introduced in, [Urban et al., 2004], they constitute a first-order syntax extended by the nominal approach to binding in abstract syntax first studied in [Gabbay, 2001; Gabbay and Pitts, 1999]. Unification and matching for nominal terms has been shown to be both decidable [Urban et al., 2004] and tractable, [Calvès and Fernández, 2010]. Equational reasoning for nominal terms has also been extensively studied, [Clouston, 2010; Fernández and Gabbay, 2007b; Gabbay and Mathijssen, 2009]. 9 2.4 Nominal Terms 2.4.1 Atoms Atoms represent names or named variables at the object-language level. Let there be a countably infinite set of atoms, A, and following the permutative convention of [Gabbay, 2011b], let a, b, c, . . ., range over distinct atoms in A. 2.4.2 Permutations A permutation, denoted π, is a bijective function on A, such that {a | π(a) 6= a, a ∈ A} is a finite set. Permutations are represented as a list of transpositions or ‘swappings’ of atoms, where id represents the identity permutation, that is, the permutation, π, which, for all a ∈ A, π(a) = a. π ::= id | π ∗ (a b) The final id and ‘list cons’ operators in the representation of a permutation, π, will most often be omitted; for example, the permutation (id ∗ (c d)) ∗ (a b) will be written simply as (c d) (a b). Below, π ranges over such syntactic representations of permutations as finite lists of pairs of atoms. Due to redundancy in this presentation a permutation need not have a unique representation; for example, (a b)(b c)(a b) represents the same permutation as (a c), and (a a), the same as the identity permutation. Write π 0 @ π for the composition of the permutation π followed by the permutation π 0 , which, as a list of transpositions corresponds to the concatenation of π 0 onto the end of π. Write π -1 for the inverse of π, which is obtained by reversing the order of the list of -1 transpositions used to represent π; for example (e f )(c d)(a b) = (a b)(c d)(e f ). The disagreement set of two permutations, π and π 0 , written, ds(π, π 0 ) is defined as {a | π(a) 6= π 0 (a)}; that is, the set of atoms for which π and π 0 have differing values. 2.4.3 Nominal Terms Consider countably infinite, pairwise disjoint sets of variables, X, Y, Z, . . . ∈ X and term-formers, f, g, . . . ∈ F, and then following [Fernández et al., 2004; Urban et al., 2004], let nominal terms be defined using the grammar described in definition 2.10 immediately below. Definition 2.10 (Nominal Terms) s, t ::= a | [a] t | f t | (t1 , . . . , tn ) | π · X Let these constructions be called respectively atom terms, abstractions, function applications, tuples and moderated variables. 10 2.4 Nominal Terms Note that variables are variables at the level of the language of nominal terms. Permutations are said to suspend on variables to form moderated variables; read (a b) · X as ‘swap a and b in X’. Whilst a variable, X, is not by itself a nominal term, the moderated variable id · X is a term; in this situation the identity permutation will most often be omitted and the term be written as just X. Call occurrences of an atom, a, abstracted when they are in the scope of an abstraction on a, [a] t, and otherwise call them unabstracted. Write vars(t) for the variables occurring in t and atms(t) for the atoms occurring in t (note that this includes all atoms mentioned in suspended permutations on moderated variables); for example, a, b, c ∈ atms([a] [b] (a c) · X) and X ∈ vars([a] [b] (a c) · X). These notations are extended to other elements of syntax and tuples of elements in the obvious way. If one supposes term-formers, lam and app, then one can build nominal terms that represent terms of the lambda calculus, section 2.1 and [Barendregt, 1984]. Example 2.5 (Nominal Terms) lam [a] a lam [a] X app (lam [a] a, a) app (lam [a] X, X) In this example the nominal terms in the first line represent, respectively, the lambda terms, λx.x and (λx.x) x and those in the second line, the lambda term ‘contexts’, λx.M (or λx.[−]) and (λx.M ) M . The second term in the second line contains two occurrences of the variable, X; note that these represent the same unknown part of the term, and that each occurs under different abstracted atoms. This is a generalisation of the traditional notion of a lambda term ‘context’, in which usually only a single unknown part of a term is allowed. 2.4.4 Permutation Actions The application of a permutation, π, to an atom, a, is written as a standard function application, π(a), and is defined inductively over a list of transpositions as described in definition 2.11. Definition 2.11 (Permutation Action on Atoms) id(a) , a (π ∗ (a b))(a) , π(b) (π ∗ (a b))(b) , π(a) (π ∗ (a b))(c) , π(c) 11 2.4 Nominal Terms Permutations are most commonly applied to terms at the level of the object-language. The permutation action of a permutation, π, on a term, t, is written π · t and defined inductively, as follows below in definition 2.12. Definition 2.12 (Permutation Action on Terms) π · a , π(a) 0 π · (π · X) , (π @ π 0 ) · X π · [a] t , [π(a)] π · t π · f t , f (π · t) π · (t1 , . . . , tn ) , (π · t1 , . . . , π · tn ) Note that this is the same notation as that used to represent a permutation suspended upon a variable in a moderated variable; the two ideas are conceptually similar and no ambiguity arises. Example 2.6 (Permutation Action on Terms) (a b) · f [a] (a, b, (b c) · X) = f [b] (b, a, (a b)(b c) · X) At times, it is necessary to apply a permutation at the level of nominal terms rather than of an object language; this is called the meta-level permutation action on terms. Define π t, the meta-level permutation action of a permutation, π, on a term, t, inductively, as follows. Definition 2.13 (Meta-level Permutation Action on Terms) π π (π 0 · X) , (π @ π 0 @ π -1 ) · X π ([a] t) , [π(a)] π t π π a , π(a) (f t) , f (π t) (t1 , . . . , tn ) , (π t1 , . . . , π tn ) Example 2.7 (Meta-level Permutation Action on Terms) (a b) 2.4.5 f [a] (a, b, (b c) · X) = f [b] (b, a, (a b)(b c)(a b) · X) Variable Substitutions Variable substitutions are substitutions that operate at the level of nominal terms. 12 2.4 Nominal Terms Definition 2.14 (Variable Substitutions) A variable substitution is a mapping from variables to terms, equal to the identity mapping but for finitely many arguments, and written as a set of bindings [X1 7→ s1 ] . . . [Xn 7→ sn ], such that the variables, X1 , . . . , Xn , are pairwise distinct. Let θ range over variable substitutions and write the value of a substitution, θ, for a variable, X, as θ(X). The domain of a substitution θ, written dom(θ), is the set of variables, {X | θ(X) 6≡ X}. id denotes the identity substitution, that is, the substitution whose domain is the empty set. The action of a variable substitution, θ, upon a term, t, is written as juxtaposition to the right thus, t θ, and is defined inductively as follows. Definition 2.15 (Variable Substitution Action) aθ , a (π · X) θ , π · (θ(X)) (π · X) θ , π · X X ∈ dom(θ) X 6∈ dom(θ) ([a] t) θ , [a] (t θ) (f t) θ , f (t θ) (t1 , . . . , tn ) θ , (t1 θ, . . . , tn θ) It is important to note that variable substitutions are grafting onto syntax trees and do not avoid the capture of atoms by abstractions. Example 2.8 (Atom Capture by Variable Subsitution) ([a] X, [b] X) [X 7→ a] = ([a] a, [b] a) Notice how in the above example, the atom, a, is captured in the first abstraction following the action of the variable substitution. 2.4.6 Positions Say that a term, C, has a position at the variable, X, when C contains only one occurrence of X and the identity permutation, id, is suspended on this one occurrence. Write C [s] for C [X 7→ s]. One would usually call such a term, C, a ‘context’ but this terminology is not used below, because it has already been employed for a different purpose. A position could also be defined by a path in the abstract syntax tree of a term as in [Fernández et al., 2004]. 13 2.4 Nominal Terms Example 2.9 (Positions) The terms, X and [a] X, both have positions at X, whereas, (X, X) and (a b) · X do not. 2.4.7 Freshness and Alpha-equivalence The freshness and alpha-equivalence relations now presented are key concepts with regard to nominal terms and coincide, respectively, with the traditional notions of ‘free variables’ and ‘α-equivalence’ in systems with binding, such as the lambda calculus. Call a # t a freshness constraint. Let ∆, ∇ range over sets of freshness constraints of the form a # X; call such sets freshness contexts. The shorthand notation, A # X , where, A and X are arbitrary sets of atoms and variables respectively, may be used to represent the freshness context {a # X | a ∈ A, X ∈ X }. The brackets of singleton sets may be omitted in such notation. The meta-level permutation action is extended to freshness contexts, thus π ∆ , {π(a) # X | a # X ∈ ∆}. Write ∆ ` a # t when a derivation exists using the rules given in definition 2.16 below. Definition 2.16 (Freshness Relation) ∆`a#b ∆ ` a # [a] t π -1 (a) # X ∈ ∆ (var)# ∆`a#π·X (atm)# (abs : aa)# ∆`a#t (abs : ab)# ∆ ` a # [b] t ∆ ` a # t1 . . . ∆ ` a # tn (tpl)# ∆ ` a # (t1 , . . . , tn ) ∆`a#t (app)# ∆`a#ft Call s ≈α t an alpha-equality constraint and write ∆ ` s ≈α t when a derivation exists using the rules given in definition 2.17 below. Definition 2.17 (Alpha-equivalence Relation) ∆ ` a ≈α a (atm)α ∆ ` s ≈α t (abs : aa)α ∆ ` [a] s ≈α [a] t ∀a ∈ ds(π, π 0 ) a # X ∈ ∆ (var)α ∆ ` π · X ≈α π 0 · X ∆ ` s ≈α (a b) · t ∆ ` a # t (abs : ab)α ∆ ` [a] s ≈α [b] t ∆ ` s1 ≈α t1 . . . ∆ ` sn ≈α tn (tpl)α ∆ ` (s1 , . . . , sn ) ≈α (t1 , . . . , tn ) ∆ ` s ≈α t (app)α ∆ ` f s ≈α f t Example 2.10 (Alpha-equivalence) ∅ ` [a] a ≈α [b] b ∅ ` [a] c ≈α [b] c ∅ ` [a] c 6≈α [a] a a # X, b # X ` (a b) · X ≈α X 14 2.4 Nominal Terms Let C range over freshness and alpha-equality constraints; a constraint problem, C, is an arbitrary set of such constraints. Extend the above notation for the derivability of constraints element-wise to constraint problems; thus, write ∆ ` {C1 , . . . , Cn } for ∆ ` C1 , . . . , ∆ ` Cn . The action of variable substitutions extends to constraints and thus constraint problems in the natural way. If solvable, a solution to a constraint problem, C, is a freshness context, ∆, such that ∆ ` C. A constraint problem has a unique, minimal solution [Urban et al., 2004]. See [Fernández and Gabbay, 2005; Urban et al., 2004] for algorithms to check and solve constraint problems. 2.4.8 Properties of Nominal Terms The freshness and alpha-equivalence relations are closed under the action of permutations. That is, for any permutation, π, if ∆ ` a # t, then ∆ ` π(a) # π · t, and if ∆ ` s ≈α t, then ∆ ` π · s ≈α π · t. The following result is one of the main technical correctness properties of nominal terms. Theorem 2.1 If ∆ ` a # s and ∆ ` s ≈α t then ∆ ` a # t. Proof 2.1 See [Urban et al., 2004]. Further properties of nominal terms together with proofs can be found in [Fernández and Gabbay, 2007b; Urban et al., 2004]. 2.4.9 Terms-in-context Definition 2.18 (Terms-in-context) A term-in-context is a pair ∆ ` s of a freshness context, ∆, and a term, s. 2.4.10 Nominal Matching Definition 2.19 (Nominal Matching Problem) Given a constraint problem, C, . . . , si ≈α ti , . . . , a corresponding nominal matching S S problem is defined when vars(si ) ∩ vars(ti ) = ∅ and is written . . . , si ? ≈α ti , . . . . A solution to such a problem, if one exists, is a pair, (∆, θ), of a freshness context, ∆, S and a variable substitution, θ, such that dom(θ) ⊆ vars(si ) and ∆ ` C θ. Informally, this says that a nominal matching problem is a constraint problem in which one adds the restriction that the variables in the left-hand sides of alpha-equality constraints are disjoint from the variables in the right-hand sides and that only variables in the left-hand sides of equality constraints may be instantiated. 15 2.4 Nominal Terms Nominal matching is decidable [Urban et al., 2004] and can be solved in linear time [Calvès and Fernández, 2010]. Definition 2.20 (Nominal Pattern Matching Problem) A nominal pattern matching problem, consists of two terms-in-context, ∇ ` l and ∆ ` s, to be matched, where vars(∇ ` l) ∩ vars(∆ ` s) = ∅ and is written: (∇ ` l) ? ≈α (∆ ` s) A solution to a nominal pattern matching problem, sol((∇ ` l) ? ≈α (∆ ` s)), if one exists, is a variable substitution, θ, such that (∇0 , θ) is a solution to the nominal matching problem ∇ ∪ {l ? ≈α s} and ∆ ` ∇0 . Example 2.11 (Nominal Pattern Matching Problems) (a # X ` [a] X) ? ≈α (b # Y ` [a] (a b) · Y ) (a # X ` [a] X) ? ≈α (∅ ` [a] a) In the example above, the first pattern matching problem has the solution, [X 7→ (a b) · Y ], whereas the second has none. 2.4.11 Nominal Rewriting Definition 2.21 A nominal rewrite rule is a 3-tuple, consisting of a freshness context, ∆, and two terms, s and t, such that vars(∆) ∪ vars(t) ⊆ vars(s), and is written, ∆ ` s → t. A nominal term rewriting system or rewrite theory, R, is a pair of a set of termformers and a possibly infinite set of rewrite rules generated from those term-formers. Expanding upon example 2.5, consider again the term-formers, lam and app, together with the term-former, sub, where sub ([a] M, N ), represents the substitution, M [N/a], in the lambda calculus. One then has the following rewrite rules defining the β-reduction of lambda terms. Example 2.12 (Rewrite Theory) ∅ ` app (lam [a] X, Y ) → sub ([a] X, Y ) a # X ` sub ([a] X, Z) → X ∅ ` sub ([a] a, Z) → Z ∅ ` sub ([a] (app (X, Y )), Z) → app (sub ([a] X, Z), sub ([a] Y, Z)) b # Z ` sub ([a] lam [b] X, Z) → lam [b] sub ([a] X, Z) 16 2.4 Nominal Terms A rewrite rule, ∇ ` l → r, matches a term-in-context, ∆ ` s, when there exists a solution to the nominal pattern matching problem (∇ ` l) ? ≈α (∆ ` s). Definition 2.22 (Nominal Rewriting) The one-step nominal rewriting relation, generated by a rewrite theory, R, is the least relation of tuples, ∆ ` s →R t, such that for any rewrite rule, ∇ ` l → r belonging to R, freshness context, ∆, terms, C and s0 , permutation, π and variable substitution, θ, then: s ≡ C [s0 ] ∆ ` ∇ θ ∪ π · (l θ) ≈α s0 , ∆ ` s →R t C [π · (r θ)] ≈α t The nominal rewriting relation, over a rewrite theory, R, written ∆ ` s R t, is the reflexive, transitive closure of the one-step rewriting relation. The nominal rewriting relation is equivariant at the level of nominal terms. The permutation, π, in definition 2.22, makes it equivalent to the approach in [Fernández and Gabbay, 2007b] where the set of rules in a rewrite theory is made closed under the action of meta-level permutation. For example, a rewrite rule, ∅ ` a → a, will rewrite any atom. Equivariant nominal matching is NP-complete, [Cheney, 2004], and thus the notion of closed rewriting definition 2.29 is often preferred, where possible. Using the rewrite theory of example 2.12, one has the following example rewrite sequences. Example 2.13 (Nominal Rewriting) app (lam [a] a, lam [b] b) → sub ([a] a, lam [b] b) → lam [b] b app (lam [a] b, lam [b] b) → sub ([a] b, lam [b] b) → b app (lam [a] lam [b] a, b) → sub ([a] lam [b] a, b) → lam [b0 ] sub ([a] a, b) → lam [b0 ] b 2.4.12 Nominal Algebra Nominal algebra is to be understood in the sense of a nominal approach to universal algebra, or the study of the logic of equality, as in [Gabbay and Mathijssen, 2009] or [Clouston, 2010]. See [Fernández and Gabbay, 2010] for a detailed treatment of the connection between nominal rewriting and nominal algebra. Definition 2.23 A nominal axiom is a 3-tuple, consisting of a freshness context, ∆, and two terms, s and t, and written, ∆ ` s = t. An algebraic theory, A, is a pair of a set of term-formers and a possibly infinite set of axioms generated from those term-formers. 17 2.4 Nominal Terms Definition 2.24 (Nominal Algebra) The nominal algebra relation, generated by an algebraic theory, A, is the least reflexive, symmetric, transitive relation of tuples, ∆ ` s =A t, such that for any axiom, ∇ ` l = r belonging to A, freshness context, ∆, term, C, permutation, π, variable substitution, θ, and freshness context, ∆# (where, if a # X ∈ ∆# , then a 6∈ atms(∆) ∪ atms(s) ∪ atms(t)), then: ∆ ∪ ∆# ` ∇ θ ∪ C [π · (l θ)] ≈α s, ∆ ` s =A t 2.4.13 C [π · (r θ)] ≈α t Newly-freshened Variants A newly-freshened variant of a term, t, is a term, written tN , in which all the atoms and variables have been replaced by newly generated atoms and variables with respect to those occurring in t; that is, such that, atms(tN ) ∩ atms(t) = ∅ and vars(tN ) ∩ vars(t) = ∅. An inductive definition is trivial and thus omitted. The notion of newly-freshened variants is extended to other syntax, such as freshness contexts, rewrite rules and axioms, in the obvious way. Atoms and variables in a newly-freshened variant may, on occasion, need to be disjoint not only from those atoms and variables occurring in one particular element of syntax itself but also from those occurring in other elements; in such situations, these extra constraints will be explicitly specified. Note that in practice the creation of newly-freshened variants is often accomplished by the generation of new atoms and variables with respect to the entire system under consideration. Example 2.14 (Newly-freshened Variants) N ([a] [b] X) = [aN ] [bN ] X N N ({a # X, b # Z}) = {aN # X N , bN # Z N } N (c # Y ` [a] X → b) = cN # Y N ` [aN ] X N → bN 2.4.14 Closedness Intuitively, a closed term has no unabstracted atoms and all occurrences of a variable must appear under the same abstracted atoms. Thus, ∅ ` f ([a] X, X) is not closed because the atom, a, is abstracted in the first occurrence of the variable, X and not in the second, but a # X ` f ([a] X, X) is closed, because the freshness context ensures that a cannot occur in X. Closedness was introduced in [Fernández et al., 2004]. Definition 2.25 tests for closedness using newly-freshened variants and nominal matching, section 2.4.10 but inductive definitions are also possible, [Clouston, 2007; Gabbay, 2011b]. 18 2.4 Nominal Terms Definition 2.25 (Term Closedness Problems) Given a term, t, if the nominal matching problem, tN ? ≈α t , has a solution, (∇, θ), then the solution to the term closedness problem for t is ∇ \ (atms(tN ) # vars(t)). If a solution to a term closedness problem exists, then it is unique and minimal; this follows from the properties of solutions to nominal matching problems. It is also independent of the newly-freshened variant generated (any freshness constraint mentioning the atoms of tN having been removed.) Definition 2.26 (Closed Terms) A term, t, is closed, if the solution to the term closedness problem for t is the empty set. Definition 2.27 (Closed Terms-in-context) A term-in-context, ∆ ` t, is closed, if ∇ is the solution to the term closedness problem for t and ∆ ` ∇. Definition 2.28 (Closed Equations) Call a rewrite rule, ∆ ` l → r, or an axiom, ∆ ` l = r, closed, if the term-in-context, ∆ ` (l, r), is closed. 2.4.15 Closed Nominal Rewriting As mentioned in section 2.4.11, for reasons of efficiency, if possible, one should make use of closed nominal rewriting. Definition 2.29 (Closed Nominal Rewriting) The one-step, closed nominal rewriting relation, generated by a rewrite theory, R, is the least relation of tuples, ∆ ` s →C R t, such that for every rewrite rule, R ∈ R of the form, ∇ ` l → r, term-in-context, ∆ ` s, terms, C and s0 , and variable substitution, θ, such that for a newly-freshened variant RN of R (fresh for ∆, s and t, in addition to R), where ∆# = atms(RN ) # vars(R) ∪ vars(∆) ∪ vars(s) ∪ vars(t), then: s ≡ C [s0 ] ∆ ∪ ∆# ` ∇N θ ∪ lN θ ≈α s0 , C [rN θ] ≈α t ∆ ` s →C R t The closed nominal rewriting relation, ∆ ` s C R t is the reflexive, transitive closure of the one-step, closed nominal rewriting relation. Closed rewriting is sound and complete for nominal algebra, if all axioms are closed [Fernández and Gabbay, 2010] and given closed rewrite rules closed nominal rewriting reduces to nominal rewriting. The notion of closed nominal rewriting is valuable for two reasons. Firstly, consider a rewrite rule, a # X ` X → X. The term, Z, will not rewrite using this rule under nominal 19 2.4 Nominal Terms rewriting, because no freshness information for Z is available. Although, intuitively, the rule says only that there exists some atom that is fresh for Z, which is always true. However, Z will rewrite using closed nominal rewriting, because when matched against a newly-freshened variant of the rule, aN # X N ` X N → X N , the extra freshness constraint, aN # Z, may be used. Secondly, because the use of a newly-freshened rule makes matching against unabstracted atoms impossible, closed nominal rewriting is used with sets of closed rewrite rules, that is, rules that do not contain unabstracted atoms. This means that closed nominal rewriting can use standard nominal matching to match rules to terms; meta-level equivariance need not be considered. This reduces the complexity of the matching problem from exponential time to linear time. 20 Chapter 3 Simple Types for Nominal Terms his chapter studies three simple type systems for nominal terms, taking inspira- T tion from the simply typed lambda calculus in the styles of both Church and de Bruijn, [Barendregt, 2000]. The systems considered are all Church-style in the sense that the sets of well-typed atoms, well-typed permutations and well-typed terms are defined before considering the actions of permutations, and the freshness and alpha-equivalence relations. Firstly, a true Church-style system, [Church, 1940], with an ornamented syntax for typed atoms, is considered in section 3.2. The notational burden of such systems is usually alleviated in type systems for the lambda calculus by introducing a typing environment for variables and only annotating variables at the point of binding, a style attributed to de Bruijn, [Barendregt et al., 2013]. Integrating such elements from the practice of Currystyle systems is much more difficult when typing nominal terms, because the separation of atoms from types interacts problematically with capturing substitution. A second system, detailed in section 3.3 is suggested as an alternative presentation of a true Church-style system and uses a typing environment to type atoms in a ‘global’ fashion. The third system in section 3.4 considers the de Bruijn-style approach in preparation for the study of a Curry-style system in chapter 4. 3.1 Simple Types This section outlines the syntax of simple types and other syntactic structures common to the type systems that follow. The notion of types built using constructors and that of products of types is standard. This is augmented with a type for the abstraction 21 3.1 Simple Types construction of nominal terms. 3.1.1 Types and Declarations Consider a countably infinite set of type-formers, C, and write a typical element thus, C. For example, Nat or List might be type-formers. Define simple types, σ, τ , and type declarations, ρ, ξ, using the grammar described in definition 3.1. Definition 3.1 (Types and Declarations) σ, τ ::= [σ] τ | C τ | (τ1 × . . . × τn ) ρ, ξ ::= hσ ,→ τ i Call [σ] τ , an abstraction type, C τ , a constructed type and (τ1 × . . . × τn ), a product type. Base data types may be represented by constructed types built from an empty product type; for example, Nat (). In such cases, the product type will, more often than not, be omitted; for example, the type Nat () will be written simply as Nat. Declarations are used below to type term-formers. Let ≡ denote syntactic equality over types and extend this to type declarations in the obvious fashion. 3.1.2 Environments A type association is a pair of a variable, X, and a type, σ, written (X : σ). A typing environment, Γ, is a set of type associations subject to the condition that if (X : σ) ∈ Γ and (X : τ ) ∈ Γ then σ ≡ τ . Let ΓX be the type associated with the variable, X, in the environment, Γ. If there is no association for X in Γ, ΓX is undefined, written ⊥. The equality relation on types is extended to environments in the obvious way. Bracketing in environments may often be omitted for the sake of clarity. 3.1.3 Signatures A declaration association is a pair of a term-former, f, and a declaration, ρ, written (f : ρ); for example, (0 : h() ,→ Nati) and (suc : hNat ,→ Nati) might be declaration associations. A typed signature, Σ, is a set of declaration associations, again subject to the condition that, if (f : ρ) ∈ Σ and (f : ξ) ∈ Σ then ρ ≡ ξ. Σf denotes the declaration associated with the term-former f in Σ. If no associated declaration for f exists in Σ, then Σf is undefined, written ⊥. 22 3.2 Type System in the Style of Church 3.2 Type System in the Style of Church Following Church, [Church, 1940], and in the style of nominal systems using sorted atoms, this type system considers typed atoms and extends the traditional syntax of nominal terms by ornamenting atoms with their type. Variables are typed using an environment to reduce the notational burden and to maintain consistency with systems described later, whilst also drawing attention to the typing of atoms. 3.2.1 Simply Typed Nominal Terms Whilst atoms are ornamented by types in this system, variables and term-formers are not and so, firstly a grammar for pseudo-terms is described in definition 3.2 and then the set of well-typed terms defined in definition 3.3. However, note that permutations are well-typed by construction. Definition 3.2 (Permutations and Pseudo-terms) π ::= id | π ∗ (aτ bτ ) s, t ::= aσ | [aσ ] t | f t | (t1 , . . . , tn ) | π · X Note that if σ 6≡ τ , then aσ 6≡ aτ ; aσ and aτ are different atoms. Superficially, this appears to destroy the ‘atomic’ nature of atoms, namely that atoms are distinguishable by name alone and their internal structure is opaque. It is however in fact equivalent to the approach where one partitions the set of atoms into a countably infinite number of countably infinite sets of atoms, one for each type, if one concatenates a textual representation of an atom’s type to its name. Definition 3.3 (Well-typed Terms) σ≡τ (atm)τ Γ Σ aσ : τ ΓX ≡ τ (var)τ Γ Σ π · X : τ Γ Σ t : τ (abs)τ Γ Σ [aσ ] t : [σ] τ Γ Σ t1 : τ1 . . . Γ Σ tn : τn (tpl)τ Γ Σ (t1 , . . . , tn ) : (τ1 × . . . × τn ) Σf ≡ hσ ,→ τ i Γ Σ t : σ (app)τ Γ Σ f t : τ Consider a formalisation of first-order logic over the domain of the natural numbers. Let Nat and o, be base types for the natural numbers and propositions respectively. Then, considering a signature, Σ, of the form, {0 : h() ,→ Nati, suc : hNat ,→ Nati, eql : h(Nat × Nat) ,→ oi, and : h(o, o) ,→ oi, not : ho ,→ oi, all : h[Nat] o ,→ oi}, one has the following examples of well-typed terms. 23 3.2 Type System in the Style of Church Example 3.1 (Well-typed Terms) X : Nat Σ suc suc X : Nat P : o Σ all [aNat ] P : o Σ not eql (0, suc suc aNat ) : o Σ and (all [aNat ] P, not eql (0, suc suc aNat )) : o 3.2.2 Permutation Actions, Freshness and Alpha-equivalence The following definitions are defined over permutations and well-typed terms, where σ 6≡ τ . The alpha-equivalence relation is defined only for well-typed terms of the same type in the same environment. Notice that the possibility that atoms may have the same name but be of different types, results in the proliferation of rules required to define the permutation action on atoms and the freshness relation over atom terms and abstractions, in comparison with definitions 2.11 and 2.16. Definition 3.4 (Permutation Action on Atoms) id(aτ ) , aτ (π ∗ (aτ bτ ))(cτ ) , π(cτ ) (π ∗ (aτ bτ ))(cσ ) , π(cσ ) (π ∗ (aτ bτ ))(aτ ) , π(bτ ) (π ∗ (aτ bτ ))(aσ ) , π(aσ ) (π ∗ (aτ bτ ))(bτ ) , π(aτ ) (π ∗ (aτ bτ ))(bσ ) , π(bσ ) Definition 3.5 (Permutation Action on Terms) π · aτ , π(aτ ) π · (π 0 · X) , (π @ π 0 ) · X π · (t1 , . . . , tn ) , (π · t1 , . . . , π · tn ) π · [aτ ] t , [π(aτ )] π · t π · f t , f (π · t) 24 3.3 Type System in a Church-like Style Definition 3.6 (Freshness Relation) (atm : aa)# ∆ ` aσ # aτ ∆ ` aσ # bτ (atm : ab0 )# ∆ ` aσ # bσ (atm : ab)# π -1 (aσ ) # X ∈ ∆ (var)# ∆ ` aσ # π · X (abs : aa)# ∆ ` aσ # t (abs : aa0 )# ∆ ` aσ # [aτ ] t ∆ ` aσ # t (abs : ab)# ∆ ` aσ # [bσ ] t ∆ ` aσ # t (abs : ab0 )# ∆ ` aσ # [bτ ] t ∆ ` aσ # [aσ ] t ∆ ` aσ # t1 . . . ∆ ` aσ # tn # (tpl) ∆ ` aσ # (t1 , . . . , tn ) ∆ ` aσ # t (app)# ∆ ` aσ # f t Definition 3.7 (Alpha-equivalence Relation) ∆ ` aτ ≈α aτ (atm)α ∆ ` s ≈α t (abs : aa)α ∆ ` [aτ ] s ≈α [aτ ] t ∀a ∈ ds(π, π 0 ) a # X ∈ ∆ (var)α ∆ ` π · X ≈α π 0 · X ∆ ` s ≈α (aτ bτ ) · t ∆ ` aτ # t (abs : ab)α ∆ ` [aτ ] s ≈α [bτ ] t ∆ ` s1 ≈α t1 . . . ∆ ` sn ≈α tn (tpl)α ∆ ` (s1 , . . . , sn ) ≈α (t1 , . . . , tn ) ∆ ` s ≈α t (app)α ∆ ` f s ≈α f t If one considers once again the signature defined for example 3.1 then one has the following examples of freshness and alpha-equivalence over well-typed terms. Example 3.2 (Freshness and Alpha-equivalence) ∅ ` aNat # suc bNat ∅ ` aNat # suc aNat⇒Nat ∅ ` aNat 6# suc aNat bNat # P ` all [aNat ] P ≈α all [bNat ] (aNat bNat ) · P ∅ ` all [aNat ] X 6≈α all [bo ] X 3.3 Type System in a Church-like Style The type system described in this section is a variant of the previous Church-style system, in which the notational burden of ornamenting atoms with their types is alleviated by using a ‘global’ typing environment for atoms. This has the additional of advantage of removing the confusion created by atoms of the same name but of different types. The syntax of pseudo-permutations and pseudo-terms in this system is that of standard nominal terms as defined in 2.10 but is recalled below in definition 3.8. Notice that 25 3.3 Type System in a Church-like Style permutations are now not necessarily well-typed. Definition 3.8 (Pseudo-permutations and Pseudo-terms) π ::= id | π ∗ (a b) 3.3.1 s, t ::= a | [a] t | f t | (t1 , . . . , tn ) | π · X Environments Let environments now include associations for atoms, (a : σ), in addition to those for variables and subject to the same condition of uniqueness. Extend the notation, ΓX , to atoms, Γa . 3.3.2 Simply Typed Nominal Terms Definition 3.9 (Well-typed Permutations) Γ typP(id) (id)τ Γa ≡ τ ≡ Γb Γ typP(π) (prm)τ Γ typP(π ∗ (a b)) This syntax-directed definition for well-typed permutations is stronger than necessary, due to redundancy present in the representation used. For a given permutation, π, and environment, Γ, it suffices to check that for each atom, a, such that π(a) 6= a, Γa ≡ Γπ(a) . Definition 3.10 (Well-typed Terms) Γa ≡ τ (atm)τ Γ Σ ∆ ` a : τ ΓX ≡ τ Γ typP(π) (var)τ Γ Σ ∆ ` π · X : τ Γ a ≡ σ Γ Σ ∆ ` t : τ (abs)τ Γ Σ ∆ ` [a] t : [σ] τ Γ Σ ∆ ` t1 : τ1 . . . Γ Σ ∆ ` tn : τn (tpl)τ Γ Σ ∆ ` (t1 , . . . , tn ) : (τ1 × . . . × τn ) Σf ≡ hσ ,→ τ i Γ Σ ∆ ` t : σ (app)τ Γ Σ ∆ ` f t : τ Consider again example 3.1. In this variant of the system, those same well-typed terms are defined as follows. Example 3.3 (Well-typed Terms) X : Nat Σ suc suc X : Nat a : Nat, P : o Σ all [a] P : o a : Nat Σ not eql (0, suc suc a) : o a : Nat Σ and (all [a] P, not eql (0, suc suc a)) : o 26 3.4 Type System in the Style of de Bruijn Notice that both abstracted and unabstracted atoms must be typed from the environment and that, in the final term, because well-typedness is decided before the definition of alpha-equivalence, both the abstracted and unabstracted occurrences of the atom, a, are typed identically. 3.3.3 Permutation Actions, Freshness and Alpha-equivalence For this type system, given well-typed permutations and well-typed terms in a single environment, the actions of permutations, freshness relation and alpha-equivalence relation (again, defined only for two well-typed terms of the same type, typed in the same environment) are defined as in section 2.4.4, definition 2.16 and definition 2.17 respectively. This represents an appreciable decrease in complexity from the pure Church-style system defined in section 3.2. 3.4 Type System in the Style of de Bruijn The type system in this section adopts the practices of a Curry-style system by annotating the abstracted atom in each abstraction with a type and using an environment in which type associations for atoms are updated. The atom-capturing nature of variable substitution over nominal terms combined with the updating of type associations, produces a conflict with the desire to type multiple occurrences of the instantiation of a variable in the same environment and thus results in a more complex system. However, this system is presented as a bridge to the Curry-style system detailed in chapter 4 where this separation of atoms from their types becomes necessary. Definition 3.11 (Pseudo-permutations and Pseudo-terms) π ::= id | π ∗ (a b) 3.4.1 s, t ::= a | [a : τ ] t | f t | (t1 , . . . , tn ) | π · X Environments Let the association for an atom, a, now be updated in an environment, Γ, written Γ o n (a : τ ) where this means either Γ ∪ {(a : τ )} or (Γ \ {(a : σ)}) ∪ {(a : τ )} as well-formedness demands. This notation is extended element-wise to sets of updates. For example, if Γ is the environment {(a : σ1 ), (c : σ2 )} then Γ o n {(a : τ1 ), (b : τ2 )} denotes the environment {(a : τ1 ), (b : τ2 ), (c : σ2 )}. The associations for variables in an environment may not be updated. 27 3.4 Type System in the Style of de Bruijn 3.4.2 Simply Typed Nominal Terms Definition 3.12 (Well-typed Permutations) Γ typP(id) 3.4.3 (id)τ Γa ≡ τ ≡ Γb Γ typP(π) (prm)τ Γ typP(π ∗ (a b)) Typing Judgements A typing judgement is a 4-tuple, (Γ, Σ, s, τ ), of an environment, Γ, a signature, Σ, a pseudo-term, s and a type, τ , and is written Γ Σ s : τ . Definition 3.13 (Quasi-derivable Typing Judgements) Γa ≡ τ (atm)τ Γ Σ a : τ ΓX ≡ τ Γ typP(π) (var)τ Γ Σ π · X : τ Γo n (a : σ) Σ t : τ (abs)τ Γ Σ [a : σ] t : [σ] τ Γ Σ t1 : τ1 , . . . , Γ Σ tn : τn (tpl)τ Γ Σ (t1 , . . . , tn ) : (τ1 × . . . × τn ) Σf ≡ hσ ,→ τ i Γ Σ t : σ (app)τ Γ Σ f t : τ Consider again the signature of examples 3.1 and 3.3. The examples of well-typed terms given there, have the following quasi-derivable judgements in this de Bruijn-style system. Example 3.4 (Quasi-derivable Typing Judgements) X : Nat Σ suc suc X : Nat P : o Σ all [a : Nat] P : o a : Nat Σ not eql (0, suc suc a) : o a : Nat Σ and (all [a : Nat] P, not eql (0, suc suc a)) : o Note that only unabstracted atoms need now be typed in the environment, and that in the last term, the type given to the abstracted occurrence of the atom, a, is dependent of that given in the environment for the unabstracted occurrence. Definition 3.14 (Variable Judgement) Suppose Γ Σ t : τ . All ‘sub-judgements’ of the quasi-derivation of Γ Σ t : τ have the form, Γ0 Σ t0 : τ 0 where t0 is a sub-term of t and Γ0 is Γ updated with associations for abstracted atoms. Call leaves of the form, Γ0 Σ π · X : τ 0 the variable judgements of the quasi-derivation of Γ Σ t : τ . 28 3.4 Type System in the Style of de Bruijn Note that for a given quasi-derivation of a judgement, there is one variable judgement for each separate occurrence of a variable in a term and it is unique. A property called the ‘diamond’ property, over quasi-derivations is now defined. A quasi-derivation of a judgement for which this property holds is a derivation. (The name ‘diamond’ is used for historical reasons; the first definition proposed to capture the idea behind this property being denoted by the symbol, .) Definition 3.15 (Diamond Property) The diamond property holds for a quasi-derivation of Γ Σ t : τ , if, for each variable, X ∈ vars(t), the environments of all variable judgements for that variable throughout the quasi-derivation are equal. Definition 3.16 (Derivable Typing Judgements) A typing judgement Γ Σ t : τ is derivable, if there exists a quasi-derivation of the judgement for which the diamond property holds. Informally, given a quasi-derivation of a judgement, if each occurrence of a variable, X ∈ vars(t), is typed in the same environment, it is a derivable judgement. For all of the example quasi-derivable well-typed terms in example 3.4, the diamond property holds. However, consider the following example, of a quasi-derivable judgement. Example 3.5 (Diamond Property) and (all [a : Nat] P, P ) Here, the variable judgements for the two occurrences of the variable, P , have different environments, the first, {a : Nat}, and the second, ∅. A variable substitution for P , would thus have to be typed in multiple, inconsistent environments. Therefore this judgement is not derivable. The need to check the equivalence of the environments for each separate occurrence of a variable in a quasi-derivation is a result of the atom-capturing nature of variable substitution. Atoms occurring in a term substituted for a variable may be subject to abstractions of differing types that are reflected in updates in the environment. One could also check the well-typedness of permutations as part of this property as described in the polymorphic type system detailed in chapter 4. However, it was decided to enforce earlier and stronger checks on permutations in this system to draw attention to its Church-style nature and to the separate causes of the difficulties arising in these systems. 29 3.4 Type System in the Style of de Bruijn 3.4.4 Permutation Actions, Freshness and Alpha-equivalence Definition 3.17 (Permutation Action on Atoms) (π ∗ (a b))(c) , π(c) id(a) , a (π ∗ (a b))(a) , π(b) (π ∗ (a b))(b) , π(a) Definition 3.18 (Permutation Action on Terms) π · a , π(a) π · (π 0 · X) , (π @ π 0 ) · X π · f t , f (π · t) π · [a : τ ] t , [π(a) : τ ] π · t π · (t1 , . . . , tn ) , (π · t1 , . . . , π · tn ) Definition 3.19 (Freshness Relation) ∆`a#b ∆ ` a # [a : τ ] t (atm)# π -1 (a) # X ∈ ∆ (var)# ∆`a#π·X ∆`a#t (abs : ab)# ∆ ` a # [b : τ ] t (abs : aa)# ∆ ` a # t1 . . . ∆ ` a # tn (tpl)# ∆ ` a # (t1 , . . . , tn ) ∆`a#t (app)# ∆`a#ft Definition 3.20 (Alpha-equivalence Relation) ∆ ` a ≈α a (atm)α ∀a ∈ ds(π, π 0 ) a # X ∈ ∆ (var)α ∆ ` π · X ≈α π 0 · X ∆ ` s ≈α t (abs : aa)α ∆ ` [a : τ ] s ≈α [a : τ ] t ∆ ` s ≈α (a b) · t ∆ ` a # t (abs : ab)α ∆ ` [a : τ ] s ≈α [b : τ ] t ∆ ` s1 ≈α t1 . . . ∆ ` sn ≈α tn (tpl)α ∆ ` (s1 , . . . , sn ) ≈α (t1 , . . . , tn ) ∆ ` s ≈α t (app)α ∆ ` f s ≈α f t The following examples of freshness and alpha-equivalence correspond to those given for the Church-style system in example 3.2. Example 3.6 (Freshness and Alpha-equivalence) ∅ ` a # suc b ∅ ` a 6# suc a b # P ` all [a : Nat] P ≈α all [b : Nat] (a b) · P ∅ ` all [a : Nat] X 6≈α all [b : o] X 30 3.4 Type System in the Style of de Bruijn Conclusions This chapter has presented three simple type systems for nominal terms, all in the style of Church. A fourth simply typed system, in the style of Curry, is easily developed from the de Bruijn-style system described in section 3.4, in which atoms, permutations and terms are neither ornamented nor annotated and follow the traditional syntax of nominal terms as defined in definition 2.10. In such a Curry-style system, the actions of permutations, freshness relation and alpha-equivalence relation are defined over untyped atoms, untyped permutations and untyped terms, that is before the set of typeable terms is decided. In such a system, constructed types would be used as unknown constants to type abstracted atoms and be internally unified during type inference. This system is not explored here; it was preferred to demonstrate such features in a richer language of polymorphic types as discussed in chapter 4. 31 Chapter 4 Polymorphic Types for Nominal Terms his chapter describes a polymorphic type system for nominal terms in the style T of Curry [Barendregt, 2000; Curry and Feys, 1958]. It extends the syntax of simple types with type variables and quantified declarations in order to implement a ML-style polymorphic type system, [Damas and Milner, 1982], in which multiple applications of a term-former within a term may each be given a different type. The notion of a principal solution to a typeability problems for a term is defined and an algorithm presented to compute such solutions. This algorithm is shown to be sound and complete with respect to the derivable judgements of the type system. Such a type system is powerful enough to be of practical interest in programming language design. Its polymorphism allows for stronger specifications than those possible using the simple type systems of chapter 3, whilst its Curry-style nature and the decidability of type inference are of great importance in terms of the reduction of notational burden. A selection of interesting examples using this system is given in chapter 5. 4.1 Polymorphic Types In this section, the syntax of simple types is extended with the addition of type variables and quantified declarations for term-formers. The former necessitates a formalisation of type substitutions and the associated action of type instantiation and the latter a notion of generic type instantiation. 32 4.1 Polymorphic Types 4.1.1 Types and Declarations Consider a countably infinite set of type variables, T, pairwise disjoint from the set of type-formers, C, and let α, β, γ, . . ., range over elements in T. Extending the syntax of the simple types given in section 3.1.1, let polymorphic types and type declarations be defined by the grammar described below in definition 4.1. Definition 4.1 (Types and Declarations) σ, τ ::= α | [σ] τ | C τ | (τ1 × . . . × τn ) ρ, ξ ::= ∀(α) . hσ ,→ τ i Call the first, new construction, a variable type. In the rule for a declaration, α denotes any finite set of type variables (if empty, the initial quantification may be omitted entirely); these type variables are generic type variables and are bound throughout the declaration. Write tvars(σ) for the set of type variables occurring in the type, σ. Extend this notation to declarations, tvars(ρ), to represent the set of type variables which occur in ρ but are not bound; that is, for a declaration, ∀(α).hσ ,→τ i, the set, (tvars(σ)∪tvars(τ ))\α. Call the elements of this set the free type variables of ρ. Write ≡ for syntactic equality over types and extend this to declarations, modulo αequivalence of bound type variables. Note that nominal syntax is not used for the definition of declarations. Example 4.1 (Equality of Declarations) ∀(α) . hList α ,→ Nati ≡ ∀(β) . hList β ,→ Nati 6≡ ∀(α) . hList γ ,→ Nati 4.1.2 Type Substitutions and Instantiation Definition 4.2 (Type Substitution) A type substitution, S, is a mapping from type variables to types, equal to the identity mapping but for finitely many arguments, and written as a set of bindings [α1 7→ τ1 ] . . . [αn 7→ τn ], such that the type variables, α1 , . . . , αn , are pairwise distinct. Let S, T, U, . . . range over type substitutions. The domain of a type substitution S, written dom(S), is the set of type variables, {α | α S 6≡ α}. Let id denote the identity substitution; that is the substitution whose domain is the empty set. Composition of type substitutions is written, S ◦ T , that is, the substitution equivalent to applying S followed by T . The action of a type substitution, S, upon a type, τ , called instantiation, is written to the right thus, τ S, and is defined in the usual inductive way as described in definition 4.3. Instantiation is extended to declarations in the usual capture-avoiding manner. 33 4.1 Polymorphic Types Definition 4.3 (Instantiation) α S , S(α) ([σ] τ ) S , [σ S] τ S (α ∈ dom(S)) (C τ ) S , C (τ S) αS , α (α 6∈ dom(S)) (τ1 × . . . × τn ) S , (τ1 S × . . . × τn S) Example 4.2 (Instantiation of Types and Declarations) ([α] α) [α 7→ β] = [β] β (∀(β) . h(α × β) ,→ αi) [α 7→ β] = ∀(β 0 ) . h(β × β 0 ) ,→ βi Substitutions are partially ordered by instantiation. Define the most general unifier of two types, written mgu(σ, τ ), as a least element, S, if one exists, in an ordering of substitutions, such that, σ S ≡ τ S. [Baader and Snyder, 2001] is an excellent reference to detailed algorithms for the calculation of most general unifiers. Extend the notion of the ordering of types by instantiation and the most general unifier of two types to declarations in the obvious way by considering the declaration as a tuple. 4.1.3 Generic Instantiation Generic instantiation acts only upon generic type variables and therefore only upon declarations. Definition 4.4 (Generic Instantiation) A declaration, ρ, of the form ∀(α) . hσ ,→ τ i, has a generic instance, written ρ < ξ, where ξ, is a declaration of the form ∀() . hσ 0 ,→ τ 0 i and σ S ≡ σ 0 and τ S ≡ τ 0 for some type substitution, S, where S instantiates all but only the generic type variables in ρ. Example 4.3 (Generic Type Instantiation) ∀(α, β) . h(α × β) ,→ αi < h(Nat × Bool) ,→ Nati ∀(α) . h(α × β) ,→ αi < 6 h(Nat × Bool) ,→ Nati ∀(α, β) . h(α × β) ,→ αi < h(α × β) ,→ αi ∀(α) . h(α × β) ,→ αi < h(Nat × β) ,→ Nati Definition 4.5 (Most General Generic Instance) The most general generic instance, ξ, of a declaration, ρ, is the generic instance, such that for any other generic instance, ξ 0 , ξ ≤ ξ 0 . The following useful technical result follows by an easy induction. Lemma 4.1 If ρ < ξ then ρ S < ξ S. 34 4.2 Type System in the Style of Curry Proof 4.1 By induction over the syntax of the pair of types forming the declaration. A similar result is proven in [Damas and Milner, 1982]. 4.2 Type System in the Style of Curry The following section starts by recalling and conflating those concepts relating to environments and signatures given in chapter 3 and adding definitions relating to type variables and type substitutions. Following that a description of the type system is given, together with proofs of a number of properties over derivable typing judgements, the most important of which is that alpha-equivalence respects typeability. 4.2.1 Environments A type association is a pair of a variable, X, and a type, σ, written (X : σ) or a pair of an atom, a, and a type σ, written (a : σ). A typing environment, Γ, is a set of type associations subject to the condition that if (X : σ) ∈ Γ and (X : τ ) ∈ Γ then σ ≡ τ (and similarly for atom associations.) Let ΓX be the type associated with the variable, X, in the environment, Γ (and similarly for an atom.) If there is no association for X in Γ, ΓX is undefined, written ⊥. The equality relation on types is extended to environments in the obvious way. Bracketing in environments may often be omitted for the sake of clarity. Write π Γ to denote the environment obtained by applying the permutation, π, to the atoms in the environment, Γ. Let the association for an atom, a, be updated in an environment, Γ, written Γ o n (a : τ ) where this means either Γ∪{(a : τ )} or (Γ\{(a : σ)})∪{(a : τ )} as well-formedness demands. This notation is extended element-wise to sets of associations. For example, if Γ is the environment {(a : σ1 ), (c : σ2 )} then Γ o n {(a : τ1 ), (b : τ2 )} denotes the environment {(a : τ1 ), (b : τ2 ), (c : σ2 )}. The associations for variables in an environment may not be updated. Write Γ S for the environment obtained by applying the type substitution, S, to the types in the environment, Γ. Let tvars(Γ) denote the set of type variables occurring in the environment, Γ, and S |Γ , the restriction of the domain of the type substitution, S, to those type variables occurring in Γ. 4.2.2 Signatures A declaration association is a pair of a term-former, f, and a declaration, ρ, written (f : ρ); for example, (suc : hNat ,→ Nati) and (0 : h() ,→ Nati). 35 4.2 Type System in the Style of Curry A typed signature, Σ, is a set of declaration associations, again subject to the condition that, if (f : ρ) ∈ Σ and (f : ξ) ∈ Σ then ρ ≡ ξ. Σf denotes the declaration associated with the term-former, f, in Σ. If no associated declaration for f exists in Σ, then Σf is undefined, written ⊥. All type variables occurring in declarations in a signature are considered to be generic type variables; that is, for every declaration, ρ, tvars(ρ) = ∅. 4.2.3 Typing Judgements First, recall the definition of nominal terms given in definition 2.10. Definition 4.6 (Nominal Terms) π ::= id | π ∗ (a b) s, t ::= a | [a] t | f t | (t1 , . . . , tn ) | π · X A typing judgement is a 5-tuple, (Γ, Σ, ∆, s, τ ), of an environment, Γ, a signature, Σ, a freshness context, ∆, a term, s and a type, τ , and is written Γ Σ ∆ ` s : τ . Quasi-derivable typing judgements are defined inductively as follows below. Definition 4.7 (Quasi-derivable Typing Judgements) Γa ≡ τ (atm)τ Γ Σ ∆ ` a : τ Σf < hσ ,→ τ i Γ Σ ∆ ` t : σ (app)τ Γ Σ ∆ ` f t : τ ΓX ≡ τ (var)τ Γ Σ ∆ ` π · X : τ Γo n (a : τ ) Σ ∆ ` t : τ 0 (abs)τ Γ Σ ∆ ` [a] t : [τ ] τ 0 Γ Σ ∆ ` t1 : τ1 . . . Γ Σ ∆ ` tn : τn (tpl)τ Γ Σ ∆ ` (t1 , . . . , tn ) : (τ1 × . . . × τn ) The rule, (var)τ differs from that of the same name in definition 3.13 in that, here, the permutation, π, is not checked as an assumption of the rule. The correctness of the permutation with respect to the typing of variables is delayed until the definition of derivable typing judgements in definition 4.11. This is true to the Curry-style nature of the system, in that terms involving a single occurrence of a variable need not be peremptorily rejected as untypeable on the basis of an associated suspended permutation. It is only in the case of multiple occurrences of the same variable in a term, that one truly needs to check that the permutation swaps atoms of compatible types. The notion of quasiderivations for this system also allows terms to be typed irrespective of the action of variable substitutions, if so desired. 36 4.2 Type System in the Style of Curry 4.2.4 Essential Environments for Variable Judgements Definition 4.8 (Variable Judgement) Suppose a quasi-derivation exists for the judgement, Γ Σ ∆ ` t : τ . All the ‘subjudgements’ of this quasi-derivation have the form Γ0 Σ ∆ ` t0 : τ 0 where t0 is a sub-term of t and Γ0 is Γ updated with associations for abstracted atoms. Call leaves of the form, Γ0 Σ ∆ ` π · X : τ 0 the variable judgements of the quasi-derivation of Γ Σ ∆ ` t : τ . Note that for a given quasi-derivation of a judgement, there is one variable judgement for each separate occurrence of a variable in a term and each one is uniquely defined modulo the names of type variables. Definition 4.9 (Essential Environment) Let Γ0 Σ ∆ ` π · X : τ 0 be a variable judgement of a quasi-derivation of Γ Σ ∆ ` t : τ . Then, call π -1 Γ0 \ {(a : σ) | ∆ ` a # X} the essential environment of the variable judgement, Γ0 Σ ∆ ` π · X : τ 0 . In the above definition, type associations for atoms that are fresh for a variable are removed because those atoms can not appear in any instantiation of that variable and so need not be typed. Example 4.4 (Essential Environments) The variable judgements of a quasi-derivation of the judgement, a : Int, b : Int, X : τ ∅ ∆ ` ((a b) · X, [a] X) : (τ × [Bool] τ ) are as follows. a : Int, b : Int, X : τ ∅ ∆ ` (a b) · X : τ (1) a : Bool, b : Int, X : τ ∅ ∆ ` id · X : τ (2) If one takes the freshness context, ∆ to be the empty set, ∅, then the corresponding essential environments for these variable judgements are as follows. {b : Int, a : Int, X : τ } (1) {a : Bool, b : Int, X : τ } (2) However, if ∆ is taken to be the freshness context, {a # X}, the corresponding essential environments are thus. {b : Int, X : τ } (1) {b : Int, X : τ } (2) 37 4.2 Type System in the Style of Curry 4.2.5 Typing Judgements A property, called the diamond property, is now defined for quasi-derivations of judgements. A quasi-derivation of judgement for which this property holds is a derivation of the judgement. Definition 4.10 (Diamond Property) The diamond property holds for a quasi-derivation of Γ Σ ∆ ` t : τ , if, for each variable, X ∈ vars(t), the essential environments of all variable judgements for that variable throughout the quasi-derivation are equal. Definition 4.11 (Derivable Typing Judegements) A typing judgement Γ Σ ∆ ` t : τ is derivable, if there exists a quasi-derivation of the judgement for which the diamond property holds. Informally a quasi-derivation of a judgement is a derivable judgement if each occurrence of a variable, X ∈ vars(t), is typed in the same environment. Γ Σ ∆ ` s : τ may sometimes be written to express ‘Γ Σ ∆ ` s : τ is derivable’. The following are examples of derivable typing judgements: Example 4.5 (Derivable Typing Judgements) a : α, X : β ∅ ∅ ` (a, X) : (α × β) ∅ ∅ ∅ ` [a] a : [α] α a : β ∅ ∅ ` [a] a : [α] α ∅ ∅ ∅ ` [a] a : [γ] γ a : α, b : β ∅ ∅ ` ([a] [b] [b] (a, b), a, b) : ([α0 ] [β 0 ] [γ] (α0 × γ) × α × β) a : τ1 , b : τ2 , X : τ ∅ ∅ ` (a b) · X : τ a : τ1 , b : τ1 , X : τ ∅ ∅ ` ((a b) · X, id · X) : (τ × τ ) X : τ ∅ a # X ` ([a] id · X, id · X) : (τ × τ ) a : α, b : β, X : τ ∅ ∅ ` [a] ((a b) · X, id · X) : [β] (τ × τ ) Notice that for the sixth term above, a type is still derivable, despite the incompatible types of the atoms occurring in the permutation, because there is only one occurrence of the variable, X, whereas the seventh, requires the types of the atoms, a and b, to be equal. For the eighth term, the freshness condition equates the conflicting environments of the two variable judgements, whilst for the ninth, the type association for a is updated in the environment to produce equal environments for the two variable judgements. 4.2.6 Properties of Derivable Typing Judgements Lemma 4.2 is standard. Lemma 4.4 is a key property for this type system. 38 4.2 Type System in the Style of Curry Lemma 4.2 (Instantiation of Typing Judgements) If Γ Σ ∆ ` t : τ then Γ S Σ ∆ ` t : τ S. Proof 4.2 By induction on the derivation of the judgement. Again, a similar result is shown in [Damas and Milner, 1982]. Lemma 4.3 (Meta-level Equivariance of Typing Judgements) Given a permutation, π, if Γ Σ ∆ ` t : τ , then π Γ Σ π ∆ ` π t : τ . Proof 4.3 By induction on the derivation of the judgement, Γ Σ ∆ ` t : τ . • For an atom term, a, if Γ Σ ∆ ` a : τ , then Γa = τ follows, as does π Γπ a = τ and thus π Γ Σ π ∆ ` π a : τ . • For a moderated variable, π 0 · X, if Γ Σ ∆ ` π 0 · X : τ , then ΓX = π ΓX = τ follows, π and thus π Γ Σ π ∆ ` (π 0 · X) : τ . • For a function application, f t, if Γ Σ ∆ ` f t : τ , then by the induction hypothesis, π Γ Σ π π ∆ ` π t : τ 0 and because Σ is fixed under permutation, thus π Γ Σ (f t) : τ . π ∆` • For a tuple, (t1 , . . . , tn ), each element follows from the inductive hypothesis but one must check that the diamond property still holds. In the pathological case, which it suffices to prove, (π1 ·X, . . . , πn ·X), one knows that the essential environments for each variable judgment Γi Σ ∆ ` πi ·X : τi are equal, π but now one must prove that the essential environments of π Γi Σ π ∆ ` (πi ·X) : τi are also equal. π Observe that (πi · X) = π @ πi @ π -1 · X and thus, if are equal, then, because π@πi-1 (π@πi @π -1 )-1 @π Γi = πi-1 π@πi-1 @π -1 @π Γi \ {(a : σ) | ∆ ` a # X} -1 Γi = π@πi Γi , it follows that Γi \ {(a : σ) | ∆ ` a # X} are all equal. • For an abstraction, [a] t, if Γ Σ ∆ ` [a] t : τ , then τ ≡ [τ 0 ] τ 00 and Γ o n (a : τ 0 ) Σ π ∆ ` t : τ 00 . By the induction hypothesis, (Γ o n (a : τ 0 )) Σ π ∆ ` π t : τ 00 . Now, π π π (Γ o n (a : τ 0 )) ≡ Γ o n (π a : τ 0 ) and so Γ o n (π a : τ 0 ) Σ π ∆ ` π t : τ 00 . One can π now infer that π Γ Σ π ∆ ` [π a] π t : [τ 0 ] τ 00 and so π Γ Σ π ∆ ` [a] t : τ . Lemma 4.4 (Object-level Equivariance of Typing Judgements) Given a permutation, π, if Γ Σ ∆ ` t : τ then π Γ Σ ∆ ` π · t : τ . 39 4.2 Type System in the Style of Curry Proof 4.4 The cases for an atom term, a moderated variable and a function application proceed as in proof 4.3 for meta-level equivariance. • For a tuple, (t1 , . . . , tn ), Γ Σ ∆ ` (t1 , . . . , tn ) : τ , again each element follows by the induction hypothesis and one must check that the diamond property continues to hold. Similarly, in the pathological case where (π1 · X, . . . , πn · X), one knows that the essential environments for each variable judgment Γi Σ ∆ ` πi ·X : τi are equal, but now one must prove that the essential environments of π Γi Σ ∆ ` : π · (πi · X)τi are equal, which follows from the fact that (π@πi )-1 @π -1 Γi = π i @π -1 @π -1 Γi = π i Γi . • For an abstraction, [a] t, if Γ Σ ∆ ` [a] t : τ , then τ ≡ [τ 0 ] τ 00 and Γ o n (a : τ 0 ) Σ π ∆ ` t : τ 00 . By the induction hypothesis, (Γ o n (a : τ 0 )) Σ ∆ ` π · t : τ 00 . One still π knows that (Γ o n (a : τ 0 )) ≡ π Γ o n (π a : τ 0 ) but now π Γ o n (π a : τ 0 ) Σ ∆ ` π · t : τ 00 π and Γ o n (π · a : τ 0 ) Σ ∆ ` π · t : τ 00 follows from the fact that π a = π(a) = π · a. One can now infer that π Γ Σ ∆ ` [π · a] π · t : [τ 0 ] τ 00 and so π Γ Σ ∆ ` π · [a] t : τ . 4.2.7 Types and Alpha-equivalence It is now proved that alpha-equivalence respects types; most importantly, this requires that the defined static semantics for derivable typing judgements correctly handles the action of suspended permutations and also the possibility that the application of a variable substitution may result in the capture of atoms by abstractions. The following lemma must first be defined and proved. Lemma 4.5 (Type Weakening and Strengthening) • If Γ Σ ∆ ` t : τ then Γ o n (a : τ 0 ) Σ ∆ ` t : τ , provided that either (a : τ 0 ) 6∈ Γ or a 6∈ atms(t) or ∆ ` a # t. Call this type weakening. • If Γ Σ ∆ ` t : τ and (a : τ 0 ) ∈ Γ then Γ \ {(a : τ 0 )} Σ ∆ ` t : τ provided that either a 6∈ atms(t) or ∆ ` a # t. Call this type strengthening. Proof 4.5 By induction on the typing judgement derivation. For strengthening, if an atom, a ∈ atms(t), then, because ∆ ` a # t, a must either be abstracted and (a : τ ) is updated or occur in a suspended permutation in which case (a : τ ) is removed from the essential environment. Theorem 4.6 (Types and Alpha-equivalence) If ∆ ` s ≈α t and Γ Σ ∆ ` s : τ then Γ Σ ∆ ` t : τ . 40 4.3 Inference of Principal Solutions Proof 4.6 By induction on the size of the pair, (s, t). The form of (s, t), is rather restricted by the assumption that ∆ ` s ≈α t; for example, if s ≡ π ·X then t ≡ π 0 ·X for some permutation, π 0 . This information is used without comment in the proof. • If ∆ ` a ≈α a and Γ Σ ∆ ` a : τ , then there is nothing to prove. • If ∆ ` π · X ≈α π 0 · X and Γ Σ ∆ ` π · X : τ , then ΓX = τ and it follows that Γ Σ ∆ ` π 0 · X : τ . • If ∆ ` f s ≈α f t and Γ Σ ∆ ` f s : τ , then Γ Σ ∆ ` s : τ 0 , and by the inductive hypothesis, one deduces that Γ Σ ∆ ` t : τ 0 and concludes that Γ Σ ∆ ` f t : τ . • If ∆ ` (s1 , . . . , sn ) ≈α (t1 , . . . , tn ) and Γ Σ ∆ ` (s1 , . . . , sn ) : τ , then each element again follows by the inductive hypothesis but one must check the diamond property still holds. Once again it suffices to prove this for the pathological case, s ≡ (π1 · X, . . . , πn · X) ≈α (π10 · X, . . . , πn0 · X) ≡ t. Now, πi · X ≈α πi0 · X when ∆ ` ds(πi , πi0 ) # X, and so if the essential environments of each variable judgement in s are equal, then they must also be equal for t. • If ∆ ` [a] s ≈α [a] t and Γ Σ ∆ ` [a] s : τ , then ∆ ` s ≈α t and τ ≡ [τ 0 ] τ 00 and Γ o n (a : τ 0 ) Σ ∆ ` s : τ 00 . Use the inductive hypothesis to deduce that Γo n (a : τ 0 ) Σ ∆ ` t : τ 00 and conclude that Γ Σ ∆ ` [a] t : [τ 0 ] τ 00 . • If ∆ ` [a] s ≈α [b] t and Γ Σ ∆ ` [a] s : τ , then again, τ ≡ [τ 0 ] τ 00 and Γ o n (a : τ 0 ) Σ ∆ ` s : τ 00 and by the properties of ≈α [Urban et al., 2004, theorem 2.11], ∆ ` s ≈α (a b) · t, a # t, (a b) · s ≈α t, b # s . By type weakening, lemma 4.5, Γ o n {(a : τ 0 ), (b : τ 0 )} Σ ∆ ` s : τ 00 and by object-level equivariance, lemma 4.4, Γ o n {(a : τ 0 ), (b : τ 0 )} Σ ∆ ` (a b) · s : τ 00 . From the fact that ∆ ` (a b) · s ≈α t, deduce by the inductive hypothesis that Γo n {(a : τ 0 ), (b : τ 0 )} Σ ∆ ` t : τ 00 and then that Γ o n {(a : τ 0 )} Σ ∆ ` [b] t : [τ 0 ] τ 00 . From the fact that ∆ ` a#[b] t, conclude by type strengthening, lemma 4.5, that Γ Σ ∆ ` [b] t : [τ 0 ] τ 00 . 4.3 Inference of Principal Solutions This section first presents the notions of a term typeability problem and a principal solution to such a problem. There then follows a description of an algorithm for the computation of principal solutions and a proof that this algorithm is sound and complete with respect to derivable typing judgements within the system. 41 4.3 Inference of Principal Solutions 4.3.1 Typeability Problems Definition 4.12 (Typeability Problem) A typeability problem is a 4-tuple (Γ, Σ, ∆, t), written, Γ Σ ∆ ` t, which asks whether the term, t, is typeable using the type and declaration associations given in the environment, Γ, and signature, Σ, respectively, and the freshness constraints given in the freshness context, ∆. Given a typeability problem, Γ Σ ∆ ` t, if there exists a type, τ , such that Γ Σ ∆ ` t : τ is derivable, then say that Γ Σ ∆ ` t is typeable. Otherwise, say that Γ Σ ∆ ` t is untypeable. More generally, say that a typeability problem is solvable if there exists a type substitution, S, that when applied to the environment, Γ, allows a type, τ , to be derived for the term, t. This is stated formally in definition 4.13. Definition 4.13 (Typeability Problem Solution) A solution to a typeability problem, Γ Σ ∆ ` t, is a pair, (S, τ ), consisting of a type substitution, S, and a type, τ , such that the typing judgement, Γ S Σ ∆ ` t : τ , is derivable. Note, that a typeability problem may have a solution but still not be typeable. A problem is only typeable when there exists a solution of the form, (id, τ ). Example 4.6 (Typeability Problem Solutions) The typeability problem, a : Bool, b : Bool, X : α ∅ ∅ ` (a b) · X, has the solutions (id, α) and ([α 7→ Nat], Nat) amongst others, and is both solvable and typeable. 4.3.2 Principal Solutions and Principal Types Solutions have a natural ordering given by the instantiation of substitutions. (S, σ) ≤ (T, τ ) when ∃U. {T = S ◦ U ∧ τ ≡ σ U } A minimal element in a set of typing solutions is a most general solution to a problem and is called a principal solution. The type component of a principal solution for a given typing problem is a most general type or principal type for that problem. By these definitions, there may be many principal solutions and thus many principal types for a given typeability problem. For example, if one lets Σ be the signature, {f : ∀(α) . hNat ,→ αi}, then both (id, α0 ) and (id, β) are principal solutions to the typeability problem, X : Nat Σ ∅ ` f X. However, principal solutions for a solvable typeability problem, Γ Σ ∆ ` s, are unique modulo the names of those type variables that are not elements of the set, tvars(Γ). In fact, principal types can be viewed as principal schemes, as in the type system described in [Damas and Milner, 1982] for the language, ML. 42 4.3 Inference of Principal Solutions 4.3.3 Inference of Principal Types A partial function, pt, is now presented that given a typeability problem, Γ Σ ∆ ` t, computes its principal solution. It is defined in two phases, firstly, ptQ constructs a most general quasi-derivation for a given typeability problem, Γ Σ ∆ ` t, and then, ptE checks the essential environments of the variable judgements of this quasi-derivation and outputs a solution, if one exists. During the computation of ptQ, it is necessary to preserve the full quasi-derivation tree constructed in order to compute ptE. The preservation of this tree is verbose and relatively straightforward and is omitted in the outline of the algorithm for ptQ given below, in order to maintain clarity. The only point of interest is that one must remember to apply the type substitution output to the preserved quasi-derivation tree, because typing function applications may instantiate previously computed sub-trees, due to the computation of most general unifiers over types returned in those sub-trees. 4.3.4 ptQ Phase ptQ, takes as input a typeability problem and outputs a typeability problem solution, if one exists, together with the quasi-derivation tree constructed for that solution. Definition 4.14 (Algorithm for ptQ) • ptQ(Γ Σ ∆ ` a) = (id, τ ) where Γa = τ . • ptQ(Γ Σ ∆ ` π · X) = (id, τ ) where ΓX = τ . • ptQ(Γ Σ ∆ ` [a] s) = (S |Γ , [τ 0 ] τ ) where ptQ(Γ o n (a : α) Σ ∆ ` s) = (S, τ ), the type variable, α, is newly generated and τ 0 = α S. • ptQ(Γ Σ ∆ ` f t) = (SS 0 |Γ , φ S 0 ) where ptQ(Γ Σ ∆ ` t) = (S, τ ), Σf = ρ and hφ0 ,→ φi is the most general generic instance of ρ, where every type variable, α ∈ tvars(hφ0 ,→ φi), is newly generated and S 0 = mgu(φ0 , τ ). • ptQ(Γ Σ ∆ ` (t1 , . . . , tn )) = (S1 . . . Sn , (τ1 S2 . . . Sn × . . . × τn−1 Sn × τn )) where ptQ(Γ Σ ∆ ` t1 ) = (S1 , τ1 ), ptQ(Γ S1 Σ ∆ ` t2 ) = (S2 , τ2 ), . . . , ptQ(Γ S1 . . . Sn−1 Σ ∆ ` tn ) = (Sn , τn ). 4.3.5 ptE Phase ptE takes the typeability problem solution and quasi-derivation output by ptQ and, if defined, returns a typeability problem solution, more specific than or equal to the input solution. 43 4.3 Inference of Principal Solutions The functions to determine variable judgements within a quasi-derivation tree and to compute the essential environments that then arise are straightforward. The function to compute the most general unifier of two types, mgu(σ, τ ), is extended to sets of pairs of types in the obvious way, using the transitivity of equality. The most general unifier of two environments, Γ and Υ, is defined such that, for all a ∈ Γ ∪ Υ, both Γa and Υa must be defined, and that set of pairs of types be unifiable by mgu. The notion is extended further to sets of environments in the obvious way. ptE, is easily defined using these subcomponents. Given the quasi-derivation for the typeability problem, Γ Σ ∆ ` t, a most general unifier for the essential environments for each variable, X ∈ vars(t), is computed and, if defined, this unifier is composed with the substitution component and applied to the type component of the input solution from ptQ to produce the solution to pt. 4.3.6 Soundness and Completeness of pt Given a typeability problem, P , it must now be shown that pt(P ) is a solution to P , and that every solution to P , is an instance of pt(P ). This is now stated and proved formally. Lemma 4.7 is required for the proof. Lemma 4.7 (Newly-generated Environments and pt) If pt(Γ o n (a : τ ) Σ ∆ ` t) = (S, σ) and α is a newly-generated type variable with respect to Γ o n (a : τ ), then there exists a type substitution, T , and a type, τ , such that pt(Γ o n (a : α) Σ ∆ ` t) = (T, τ ) and (T, τ ) ≤ (S, σ). Proof 4.7 By induction on the term, t. The only interesting case is that for an atom term, a. There, pt(Γ o n (a : τ ) Σ ∆ ` a) = (id, τ ) and so it follows that pt(Γ o n (a : α) Σ ∆ ` a) = (id, α) and, because α is newly-generated, (id, α) ≤ (id, τ ). Theorem 4.8 (Soundness and Completeness of pt) • Soundness: if pt(Γ Σ ∆ ` t) is defined and equal to (S, τ ) then Γ S Σ ∆ ` t : τ is derivable. • Completeness: let S be a substitution such that dom(S) ⊆ tvars(Γ); if Γ U Σ ∆ ` t : σ is derivable then pt(Γ Σ ∆ ` t) is defined and equal to (S, τ ) and (S, τ ) ≤ (U, σ). Proof 4.8 The argument for soundness is by a routine induction on the derivation of pt(Γ Σ ∆ ` t) = (S, τ ), using lemma 4.2 and is omitted. That for completeness is divided into two parts corresponding to the two phases of pt. The first part is thus for ptQ and is by induction on the abstract syntax tree of the term, t, with the help of lemma 4.7. 44 4.3 Inference of Principal Solutions Completeness of ptQ • If Γ U Σ ∆ ` a : σ then σ = (Γ U )a = Γa U . Let Γa = τ , then by definition ptQ(Γ Σ ∆ ` a) = (id, τ ), which is clearly the principal solution. • The case for a moderated variable follows that for an atom term. • If Γ U Σ ∆ ` [a] t : σ, then σ ≡ [σ 0 ] σ 00 , and Γ U o n (a : σ 0 ) Σ ∆ ` t : σ 00 . By the inductive hypothesis, ptQ(Γ o n (a : σ 0 ) Σ ∆ ` t) = (S, τ 00 ), of which, (U, σ 00 ), is an instance; that is, there is a substitution, T , such that U = S ◦ T and σ 00 ≡ τ 00 T . Now also, by definition, ptQ(Γ Σ ∆ ` [a] t) = (V |Γ , [µ0 ] µ00 ), where µ0 = α V and ptQ(Γ o n (a : α) Σ ∆ ` t) = (V, µ00 ), thus, by lemma 4.7, there exists a substitution, W , such that, V ◦ W = S and µ00 W ≡ τ 00 . However, because α is newly generated, it follows that for all β ∈ tvars(Γ) then β V |Γ ≡ β S. The fact that α 6∈ dom(S) gives µ0 ≡ α V ≡ σ 0 and α 6∈ tvars(µ00 ), gives µ00 ≡ τ 00 . Thus, (U, [σ 0 ] σ 00 ) is an instance of (V |Γ , [µ0 ] µ00 ). • If Γ U Σ ∆ ` f t : σ, then Γ U Σ ∆ ` t : σ 0 , Σf = ρ ≡ hρ0 ,→ ρ00 i and hρ0 ,→ ρ00 i < hσ 0 ,→ σi. By the inductive hypothesis, ptQ(Γ Σ ∆ ` t) = (S, τ 0 ), of which, (U, σ 0 ), is an instance; that is, there is a substitution, T , such that U = S T , σ 0 ≡ τ 0 T . Thus by definition, ptQ(Γ Σ ∆ ` f t) = ((S ◦ V ) |Γ , φ00 V ), where hφ0 ,→ φ00 i, is a newly generated most general generic instance of ρ, and V = mgu(φ0 , τ 0 ). Now, because φ0 is part of the most general generic instance of ρ, there exists a substitution W , such that φ0 W ≡ σ 0 ≡ τ 0 T but τ 0 V ≡ φ0 V , thus V ≤ W ◦ T , otherwise V would not be the mgu of φ0 and τ 0 . • If Γ U Σ ∆ ` (t1 , . . . , tn ) : (σ1 × . . . × σn ), then Γ U Σ ∆ ` t1 : σ1 , . . . , Γ U Σ ∆ ` tn : σn . By definition ptQ(Γ Σ ∆ ` (t1 , . . . , tn )) = (S1 . . . Sn , (τ1 S2 . . . Sn × . . . × τn−1 Sn × τn )) where ptQ(Γ Σ ∆ ` t1 ) = (S1 , τ1 ), ptQ(Γ S1 Σ ∆ ` t2 ) = (S2 , τ2 ), . . . , ptQ(Γ S1 . . . Sn−1 Σ ∆ ` tn ) = (Sn , τn ). This is equivalent to computing, first, ptQ(Γ Σ ∆ ` t1 ) = (S1 , τ1 ), . . . , ptQ(Γ Σ ∆ ` tn ) = (Sn , τn ), and then the most general unifier of these solutions. Elementwise each is provable by induction and ((t1 , . . . , tn ), (σ1 × . . . × σn )), must be an instance of the most general unifier of the solutions. 45 4.3 Inference of Principal Solutions Completeness of ptE If Γ U Σ ∆ ` t : σ is derivable then there exists a derivation in which the essential environments of the variable judgements of that derivation are equal for each variable, X ∈ vars(t). Given the completeness of ptQ, shown immediately above, then ptQ(Γ Σ ∆ ` t) is defined and equal to (S, τ ) and (S, τ ) ≤ (U, σ). By the fact that ptE computes a most general unifier and it is known that one exists, then pt(Γ Σ ∆ ` t) is defined and must remain more general than (S, τ ). The following two properties follow as corollaries of theorem 4.8. Corollary 4.9 (Empty Type Substitution) A typeability problem, Γ Σ ∆ ` t, is typeable, if and only if pt(Γ Σ ∆ ` t) = (id, τ ) for some type, τ . Corollary 4.10 (Alpha-equivalence and Principal Solutions) ∆ ` s ≈α t implies pt(Γ Σ ∆ ` s) = pt(Γ Σ ∆ ` t) modulo the names of type variables, for any environment, Γ, and signature, Σ, such that pt is defined for either one of the terms, s or t. Conclusions This chapter has presented a Curry-style polymorphic type system for nominal terms. It has been proved that the derivation of typeable terms respects the notion of alphaequivalence defined in definition 2.17. That is, that the type system gives derivations compatible with the action of suspended permutations on variables and the atom-capturing nature of variable substitution. The system has been shown to have principal solutions modulo the renaming of those type variables not occurring in the environment. An algorithm to compute such principal solutions for a given term typeability problem has been given in detail and its soundness and completeness with respect to the derivable typing judgements of the type system proven. This algorithm has been implemented by the author, for more details of which see section 7.3. 46 Chapter 5 Typed Nominal Equational Theories his chapter explores the application of the polymorphic type system of chapter 4 T to nominal equational theories. A formalization of typed nominal rewriting is given by first defining the notion of a typeable rewrite rule and then providing conditions upon nominal rewriting sufficient to guarantee the preservation of types. A simplification, using the more efficient approach of closed nominal rewriting, over typeable terms, is then investigated and a corresponding system of typed nominal algebra defined. 5.1 Typed Nominal Rewriting This section begins by defining a typeable rewrite rule. This is followed by conditions upon nominal rewriting that ensure the property of subject reduction. These conditions are formulated in section 5.1.2, as typed nominal pattern matching. 5.1.1 Typeable Rewrite Rules Definition 5.1 (Typeable Rewrite Rule) A typeable rewrite rule, written Φ Σ ∇ ` l → r : τ , is a tuple of a freshness context, ∇, an environment, Φ, a signature, Σ, two terms, l and r and a type, τ , such that the following conditions hold. 1. vars(Φ) ∪ vars(∇) ∪ vars(r) ⊆ vars(l) 2. pt(Φ Σ ∇ ` (l, r)) = (id, (τ × τ )) 47 5.1 Typed Nominal Rewriting The first condition is a standard condition for untyped rewrite rules, namely that variables mentioned in the rule must be contained in the left-hand term. The second says that l and r are typeable and have the same principal type in the context of ∇, Φ and Σ. Most importantly, this condition is so framed to ensure that the essential environments of both sides are the same, which is key to proving the property of subject reduction. Let Σ be the signature, {lam : ∀(α, β) . h[α] β ,→ α ⇒ βi, app : ∀(α, β) . h(α ⇒ β × α) ,→ βi, sub : ∀(α, β) . h([α] β × α) ,→ βi}, where ⇒, is a type-former, written infix, to construct function types. One then has the following typeable rules representing βη-reduction for a polymorphically typed lambda calculus. Example 5.1 (Typeable Rewrite Rules) X : α, Y : β Σ ∅ ` app ((lam [a] X), Y ) → sub ([a] X, Y ) : α X : α ⇒ β Σ a # X ` lam [a] (app (X, a)) → X : α ⇒ β X : α, Z : γ Σ a # X ` sub ([a] X, Z) → X : α Z : γ Σ ∅ ` sub ([a] a, Z) → Z : γ X : β ⇒ α, Y : β, Z : γ Σ ∅ ` sub ([a] (app (X, Y )), Z) → app (sub ([a] X, Z), sub ([a] Y, Z)) : α X : α, Z : γ Σ b # Z ` sub ([a] (lam [b] X), Z) → lam [b] (sub ([a] X, Z)) : α0 ⇒ α 5.1.2 Typed Nominal Pattern Matching Given a typeable rewrite rule and a typeable term for which a solution to an equivariant nominal pattern problem exists, it does not necessarily hold that their respective types be compatible. Thus, this section introduces the concept of typed nominal pattern patching. Remark 5.1 Typed nominal pattern matching, as defined below, is an equivariant matching. Definition 5.2 (Typed Nominal Pattern Matching) A typed nominal pattern matching problem, (Φ Σ ∇ ` l) ? ≈α (Γ Σ ∆ ` s) is a pair of tuples, where Φ and Γ are environments, Σ is a signature, ∇ and ∆ are freshness contexts, and l and s are terms, such that the variables and type variables occurring on the left-hand side are disjoint from those occurring on the right-hand side, that is, vars(Φ Σ ∇ ` l) ∩ vars(Γ Σ ∆ ` s) = ∅ and tvars(Φ) ∩ tvars(Γ) = ∅. 48 5.1 Typed Nominal Rewriting A solution to a typed nominal pattern matching problem, if it exists, is the least 3tuple, (S, θ, π), of a type substitution, a variable substitution and a permutation such that the following conditions are met. 1. ∆ ` ∇ θ, π · (l θ) ≈α s 2. For each a ∈ atms(Φ), (π Φ)a S ≡ Γa . 3. For each Φ0 Σ ∇ ` id · X : τ 0 , a variable judgement of Φ Σ ∇ ` l : τ , then π Γo n ( Φ0 ) S Σ ∆ ` π · ((id · X) θ) : τ 0 S. Condition 1 defines a solution, to an untyped equivariant nominal matching problem and determines the substitution, θ, and the permutation, π, of the solution to the typed nominal matching problem. Condition 2 checks that any type association for an atom given in Φ is defined and compatible with that given in Γ. Note that these associations type the unabstracted atoms in the pattern and term. Condition 3 effectively ‘types’ the variable substitution, θ. It ensures that the term substituted for each variable is typeable in the essential environment for that variable in the pattern with the addition of any extra associations in the term’s environment. Note that the second condition entails that associations for atoms in Γ are not updated with incompatible types from Φ0 . To see why it is necessary to check the variable substitution, θ, consider the signature, Σ, where Σf = ∀(α) . hα ,→ Nati and Σtrue = h() ,→ Booli. Given the pattern, f X, and the term, f true, then the untyped pattern matching problem ∅ ` f X ? ≈α ∅ ` f true has a solution [X 7→ true], but the typed pattern matching problem (X : Nat Σ ∅ ` f X) ? ≈α (∅ Σ ∅ ` f true) has none: the variable substitution, [X 7→ true], is not ‘typeable’, because X is required to have the type, Nat, but it is instantiated with a term of type Bool. The following proof shows that given a typeable pattern, then a typeability problem that is equal by typed nominal pattern matching is typeable with an instance of the type of the pattern. Property 5.1 (Typed Nominal Pattern Matching) If Φ Σ ∇ ` l : σ, and (Φ Σ ∇ ` l) ? ≈α (Γ Σ ∆ ` s) = (S, θ, π), then Γ Σ ∆ ` π · (l θ) : σ S. Proof 5.1 By induction on the term, l. • In the case of an atom term, a, by condition 2 of typed nominal pattern matching, (π Φ)a S ≡ Γa . • The case for a variable, π 0 · X, follows from condition 3 on typed nominal pattern matching, whereby the substitution is ‘typeable’. 49 5.1 Typed Nominal Rewriting • In the case of an abstraction, [a] l0 , then Γ Σ ∆ ` l θ : σ S, if Γ o n {(a : σ 00 S)} Σ ∆ ` l0 θ : σ 0 S, where σ S ≡ [σ 00 S] σ 0 S. The inductive hypothesis can be applied directly and the result follows immediately. • The case for a tuple follows by induction element-wise and also form the from the fact that because the pattern, l, is typeable, then the essential environments are equal for all variable judgements of each variable, X ∈ vars(l). • The case for a function application follows directly by induction. 5.1.3 Typed Nominal Rewriting The conditions of typed nominal rewriting are inherited from nominal rewriting, section 2.4.11, [Fernández and Gabbay, 2007b; Fernández et al., 2004] and extended with types. Definition 5.3 (Typed Nominal Rewriting) Given a rewrite theory, R, of which, R is a typeable rewrite rule of the form, Φ Σ ∇ ` l → r : σ, a typeability problem, Γ Σ ∆ ` s, and a term, t, such that vars(R) ∩ vars(Γ Σ ∆ ` s) ∪ vars(t) = ∅ and tvars(R) ∩ tvars(Γ) = ∅ (renaming variables or type variables in R, if necessary), then the one-step typed nominal rewriting relation, ∆ ` s →τR t, is the least relation of tuples generated over the theory R such that the following conditions are met. 1. s ≡ C [s0 ] 2. (Φ Σ ∇ ` l) ? ≈α (Γ0 Σ ∆ ` s0 ) has the solution (S, θ, π). 3. ∆ ` C [π · (r θ)] ≈α t. The typed nominal rewriting relation over a rewrite theory, R, written ∆ ` s τR t, is the reflexive, transitive closure of the one-step typed nominal rewriting relation. 5.1.4 Subject Reduction It is now proved that typed nominal rewriting enjoys the property of subject reduction; that is, given typeable rules, the type of a term is preserved under rewriting. Theorem 5.2 (Subject Reduction) Given a typeable rewrite rule, Φ Σ ∇ ` l → r : σ, let R be the rewrite theory, (Σ, {∇ ` l → r}); if Γ Σ ∆ ` s : τ and ∆ ` s τR t then Γ Σ ∆ ` t : τ . 50 5.2 Typed Closed Nominal Rewriting Proof 5.2 By corollary 4.9, it suffices to prove that, if pt(Γ Σ ∆ ` s) = (id, τ ) and ∆ ` s τR t then Γ Σ ∆ ` t : τ . The proof proceeds by induction on the length of the finite rewrite sequence, ∆ ` s τR t and thus it also suffices to prove that subject reduction holds for the one-step nominal rewriting relation, ∆ ` s →τR t. From this base case, it is known that s ≡ C [s0 ] and that ∆ ` ∇ θ ∪ π · (l θ) ≈α s0 , C [π · (r θ)] ≈α t . From the assumption that the term, s, is typeable, one deduces that there exists a typing judgement, Γ0 Σ ∆ ` s0 : τ 0 , for the sub-term, s0 . However, pt(Φ Σ ∇ ` l) = (id, σ) and by property 5.1, then Γ0 Σ ∆ ` s0 : σ S and therefore there exists some type substitution, T , such that Γ0 T ≡ Γ0 and σ S T ≡ τ 0 . By theorem 4.6, that is, that alpha-equivalence respects types, it follows now that 0 Γ Σ ∆ ` π · l θ : σ S T . By the assumptions on typeable rewrite rules, Φ Σ ∇ ` r : σ, and by lemma 4.2, the instantiation of types, it follows that Φ S T Σ ∇ ` r : σ S T . By the second condition of definition 5.1, (that l has the same essential environments for each variable judgement as those that occur in r), Γ0 Σ ∆ ` π · r θ : σ S. Γ0 T ≡ Γ0 , and so again by lemma 4.2, it also holds that Γ0 Σ ∆ ` π · (r θ) : σ S T . Hence, Γ Σ ∆ ` C [π · (r θ)] : τ , and then, again by theorem 4.6, Γ Σ ∆ ` t : τ , as required. 5.2 Typed Closed Nominal Rewriting As previously noted, closed nominal rewriting, definition 2.29 can be solved in linear time [Calvès and Fernández, 2010], and so is markedly more efficient than the equivariant matching required in definition 5.2, which is an NP-complete problem, [Cheney, 2004]. Thus a system of typed closed nominal rewriting is highly desirable. In addition, when using typed nominal pattern matching, each attempted matching is type-checked and this represents a significant overhead when used at each rewrite step. Thus, the system presented here also adds conditions upon the typing of function applications to allow typed nominal pattern matching to be made unnecessary. Rewrite rules may then be typed statically, whilst still ensuring subject reduction. 5.2.1 Typeable-closed Rewrite Rules Definition 5.4 (Typeable-closed Rewrite Rule) A typeable-closed rewrite rule, R, written, Φ Σ ∇ ` l → r : τ is a tuple of a freshness context, ∇, an environment, Φ, which has type associations for variables only, a signature, Σ, two terms, l and r and a type, τ such that the following conditions are met. 51 5.2 Typed Closed Nominal Rewriting 1. vars(Φ) ∪ vars(∇) ∪ vars(r) ⊆ vars(l) 2. pt(Φ Σ ∇ ` (l, r)) = (id, (τ × τ )) 3. The rule, ∇ ` l → r, is closed. 4. For every function application, Γ0 Σ ∆ ` f t : τ 0 , in either l or r, where Σf = hσ ,→τ i then the type of t must be at least as general as σ. Conditions 1 and 2 are the same as in definition 5.1. Condition 3 ensures that the rule is closed. Condition 4 says that the sub-term that forms the argument to an occurrence of a term-former has the most general type possible. This imposes the rigidity of generic type variables in rewrite rules using function applications. Assume a signature, Σ, of the following form, {fst : ∀(α β) . h(α × β) ,→ αi, snd : ∀(α β) . h(α × β) ,→ βi}. One then has the following set of typeable-closed rules, defining surjective pairing (which cannot be implemented by a compositional translation to the lambda calculus, [Barendregt, 1974].) Example 5.2 (Typeable-closed Rewrite Rules) X : α, Y : β Σ ∅ ` fst (X, Y ) → X : α X : α, Y : β Σ ∅ ` snd (X, Y ) → Y : β Z : (α × β) Σ ∅ ` (fst Z, snd Z) → Z : (α × β) Note that rewrite rules may be closed but not typeable-closed. The conditions on typeable-closed rules, given in definition 5.4 are such that rewrite rules can be typed statically and typed nominal pattern matching not be required. A closed rule that is not typeable-closed may still be typeable by definition 5.1. 5.2.2 Typed Closed Nominal Rewriting The conditions given in definition 5.4 allow typed closed nominal rewriting to be defined using nominal matching and closed nominal rewriting; that is, neither equivariant matching nor typed matching is required. However typed closed nominal rewriting still has the property of subject reduction; that is, given typeable-closed rules, the type of a typeable term is preserved under rewriting. Theorem 5.3 (Subject Reduction) Given a typeable-closed rewrite rule, Φ Σ ∇ ` l → r : σ, let R be the rewrite theory, (Σ, {∇ ` l → r}); if Γ Σ ∆ ` s : τ and ∆ ` s C R t then Γ Σ ∆ ` t : τ . Note that closed nominal rewriting reduces to nominal rewriting when using closed rules. Therefore, in order to prove subject reduction for typed closed nominal rewriting, it 52 5.2 Typed Closed Nominal Rewriting is sufficient to show that the conditions on typeable-closed rewrite rules ensure property 5.1 of typed nominal pattern matching. One may then proceed as in proof 5.2. Proof 5.3 Condition 1 of definition 5.2 is trivially satisfied, because any solution to a nominal matching problem is solution to an equivariant nominal matching problem. Condition 2 of definition 5.2 is also trivially satisfied. Here, condition 3 states that the rewrite rule must be closed and thus for the environment, Φ, of a typeable-closed rewrite rule, it may be considered that atms(Φ) = ∅. Therefore the condition is vacuously true. Condition 3 of definition 5.2, that the variable substitution is ‘typeable’, is here a consequence of condition 4 and the fact that only typeable terms are subject to rewriting. Condition 4 states that the argument term of each occurrence of an application in a rule is typed with the most general type possible. This ensures that the problem regarding different instantiations of generic type variables during the ‘typing’ of substitutions discussed in definition 5.2 does not arise. Consider again the set of rewrite rules for βη-reduction for a polymorphically typed lambda calculus given in example 5.1. Whilst all the rules in this set are typeable rewrite rules, only some are typeable-closed. Specifically, the rules for substitution over abstractions and applications, do not comply to condition 4 of that definition 5.4. In the case of the former, the variable, X, on the left-hand side has the type, β ⇒ α which is too specific an ‘argument’ type for the declaration for the substitution term-former, sub and similarly for X on the right-hand side in the latter case. This brings to attention the fact that these rules do not exhibit genuine generic polymorphism but are rather making use of an ad-hoc polymorphism where a rewrite depends on the type of the type of the term being rewritten. It also highlights a conflict between the types for the lambda calculus being modelled and the type system for nominal terms explored here that specifies the syntactic representations of the lambda terms. The rules for substitution give behaviour to the syntax trees defined for lambda terms and do not require the information provided by the lambda calculus types being represented. Subject reduction for both typed nominal rewriting and typed closed nominal rewriting should allow a hybrid approach where the greater efficiency of closed nominal rewriting is harnessed whenever it is possible to do so. The author also conjectures that techniques borrowed from research into generalized algebraic data types (GADTs), [Peyton Jones et al., 2006], may allow such ad-hoc polymorphism in rules to be made less problematic. 53 5.3 Typed Nominal Algebra 5.3 Typed Nominal Algebra Closed nominal rewriting is complete for nominal algebra when using closed axioms, [Fernández and Gabbay, 2010]. Thus, it is straightforward to extend the result in section 5.2 to define a system of typed nominal algebra in which types are preserved under equational reasoning. The definition of a typeable-closed axiom follows closely that for a typeable closed rewrite rule, given in definition 5.4. Definition 5.5 (Typeable-closed Axiom) A typeable-closed axiom, A, written, Φ Σ ∇ ` l = r : τ is a tuple of a freshness context, ∇, an environment, Φ, which has type associations for variables only, a signature, Σ, two terms, l and r and a type, τ , such that the following conditions are met. 1. pt(Φ Σ ∇ ` (l, r)) = (id, (τ × τ )) 2. The axiom, ∇ ` l = r, is closed. 3. For every function application, Γ0 Σ ∆ ` f t : τ 0 , in either l or r, where Σf = hσ ,→τ i then the type of t must be at least as general as σ. From the aforementioned result, [Fernández and Gabbay, 2010, Theorem 5.19], that nominal algebra is complete for closed nominal rewriting when using closed axioms, it is again straightforward to prove that typed nominal algebra preserves types under equational reasoning, by demonstrating that the conditions on typeable-closed axiom given in definition 5.5 are sufficient to guarantee typed nominal pattern matching, property 5.1. The following axioms defining quantifier exchange for a fragment of first-order logic provide a good example of typeable-closed axioms. Assume Σ to be the signature, {and : h(o, o),→oi, or : h(o, o),→oi, not : ho,→oi, all : h[i] o ,→ oi, some : h[i] o ,→ oi}, where the base types, i and o , represent individuals and propositions respectively. 54 5.3 Typed Nominal Algebra Example 5.3 (Typeable-closed Axioms) P : o, Q : o Σ a # P ` and (P, all [a] Q) = all [a] and (P, Q) : o P : o, Q : o Σ a # P ` or (P, all [a] Q) = all [a] or (P, Q) : o P : o, Q : o Σ a # P ` and (Q, all [a] P ) = all [a] and (Q, P ) : o P : o, Q : o Σ a # P ` or (Q, all [a] P ) = all [a] or (Q, P ) : o P : o, Q : o Σ a # P ` and (P, some [a] Q) = some [a] and (P, Q) : o P : o, Q : o Σ a # P ` or (P, some [a] Q) = some [a] or (P, Q) : o P : o, Q : o Σ a # P ` and (Q, some [a] P ) = some [a] and (Q, P ) : o P : o, Q : o Σ a # P ` or (Q, some [a] P ) = some [a] or (Q, P ) : o P : o Σ ∅ ` not all [a] P = all [a] not P : o P : o Σ ∅ ` not some [a] P = some [a] not P : o Recall again example 5.1. One may also axiomatize βη-reduction for that polymorphically typed lambda calculus as follows, [Gabbay and Mathijssen, 2010]. Example 5.4 (Axiomatized Polymorphic Lambda Calculus) X : α, Y : β Σ ∅ ` app ((lam [a] X), Y ) = sub ([a] X, Y ) : α X : α ⇒ β Σ a # X ` lam [a] (app (X, a)) = X : α ⇒ β X : α, Z : γ Σ a # X ` sub ([a] X, Z) = X : α Z : γ Σ ∅ ` sub ([a] a, Z) = Z : γ X : β ⇒ α, Y : β, Z : γ Σ ∅ ` sub ([a] (app (X, Y )), Z) = app (sub ([a] X, Z), sub ([a] Y, Z)) : α X : α, Z : γ Σ b # Z ` sub ([a] (lam [b] X), Z) = lam [b] (sub ([a] X, Z)) : α0 ⇒ α X : α, a : β Σ ∅ ` sub ([a] X, a) = X : α The axioms for substitution over abstractions and applications are again not typeableclosed for the same reasons as those outlined above in section 5.2.2. However, in addition, the axiom, X : α, a : β Σ ∅ ` sub ([a] X, a) = X : α is not typeable-closed because it violates condition 2 of definition 5.5. The remaining axioms are typeable-closed. Non-closed rules are not common (and are not accepted by the standard higher-order 55 5.4 Further Examples rewriting formalisms), but interesting examples do exist, such as for models of the πcalculus; here, in this case, the rule is needed for completeness with respect to the models, [Gabbay and Mathijssen, 2010]. A formalism of ‘non-closed’ typed nominal algebra, following that for typed nominal rewriting, given in section 5.1, using typed nominal pattern matching would allow this axiom to be typed. Again, a hybrid approach, over both formalisms of typed nominal algebra, would allow reasoning with this set of rules. The study of such a system remains an area for future work. Note that nominal algebra is also complete for nominal rewriting given the ability to generate new freshness constraints when necessary, [Fernández and Gabbay, 2010]. 5.4 Further Examples This section gives a number of further examples of sets rewrite rules and axioms that are typeable or typeable-closed. All these examples, as well as those others given in this chapter, have been machine-checked using the author’s implementation. 5.4.1 Arithmetic Given a base type for natural numbers, Nat, and a signature, Σ, thus, {0 : h() ,→ Nati, suc : hNat ,→ Nati, add : h(Nat × Nat) ,→ Nati, mul : h(Nat × Nat) ,→ Nati, exp : h(Nat × Nat) ,→ Nati, ack : h(Nat × Nat) ,→ Nati}, then one has the following typeable-closed rewrite rules for primitive recursive arithmetic. X : Nat Σ ∅ ` add (X, 0) → X : Nat X : Nat, Y : Nat Σ ∅ ` add (X, suc Y ) → suc (add (X, Y )) : Nat X : Nat Σ ∅ ` mul (X, 0) → 0 : Nat X : Nat, Y : Nat Σ ∅ ` mul (X, suc Y ) → add (X, mul (X, Y )) : Nat X : Nat Σ ∅ ` exp (X, 0) → suc 0 : Nat X : Nat, Y : Nat Σ ∅ ` exp (X, suc Y ) → mul (X, exp (X, Y )) : Nat Y : Nat Σ ∅ ` ack (0, Y ) → suc Y : Nat X : Nat Σ ∅ ` ack (suc X, 0) → ack (X, suc 0) : Nat X : Nat, Y : Nat Σ ∅ ` ack (suc X, suc Y ) → ack (X, ack (suc X, Y )) : Nat 56 5.4 Further Examples 5.4.2 Higher-order Logic Let o, be a base type representing propositions and Σ be the signature, {> : h() ,→ oi, ⊥ : h() ,→ oi, ∧ : h(o × o) ,→ oi, ⇒ : h(o × o) ,→ oi, ⇔ : h(o × o) ,→ oi, ≈ : ∀(α) . h(α × α) ,→ oi, ∀ : ∀(α) . h[α] o ,→ oi, σ : ∀(α) . h([α] o × α) ,→ oi}. Write function applications for substitution, σ ([a] s, t), as s [a/t], and use infix notation for all the binary type-formers. The set of rewrite rules defined below, representing equality and simplification for (a fragment of) higher-order logic, are typeable-closed. Z : α Σ ∅ ` Z ≈ Z → > : o X : o Σ a # X ` ∀ [a] X → X : o X : o, Y : o Σ ∅ ` ∀[a] (X ∧ Y ) → ∀ [a] X ∧ ∀ [a] Y : o One also has the following typeable-closed axioms. P : o, R : α Σ ∅ ` ∀ [a] P ⇒ P [a/R] = > : o P : o, Q : o Σ a # P ` ∀ [a] (P ⇒ Q) ⇔ (P ⇒ ∀ [a] Q) = > : o P : o, R : α, S : α Σ ∅ ` R ≈ S ∧ P [a/R] ⇒ P [a/S] = > : o 5.4.3 Gödel’s System T Consider an extension of example 5.1 for the polymorphically typed lambda calculus to represent Gödel’s System T of total higher-order functions over the natural numbers. Let Nat be a base type for the natural numbers and let ⇒, again be a type-former, written infix, for the construction of function types. Given the extended signature, Σ, of the form, {lam : ∀(α, β) . h[α] β ,→ α ⇒ βi, app : ∀(α, β) . h(α ⇒ β × α) ,→ βi, sub : ∀(α, β) . h([α] β × α) ,→ βi, 0 : h() ,→ Nati, suc : hNat ,→ Nati, rec : ∀(α) . h(α × α ⇒ (Nat ⇒ α) × Nat) ,→ αi} then one has the following typeable rewrite rules to define System T. 57 5.4 Further Examples X : α, Y : β Σ ∅ ` app ((lam [a] X), Y ) → sub ([a] X, Y ) : α X : α, Z : γ Σ a # X ` sub ([a] X, Z) → X : α Z : γ Σ ∅ ` sub ([a] a, Z) → Z : γ X : β ⇒ α, Y : β, Z : γ Σ ∅ ` sub ([a] (app (X, Y )), Z) → app (sub ([a] X, Z), sub ([a] Y, Z)) : α X : α, Z : γ Σ b # Z ` sub ([a] (λb.X), Z) → lam [b] (sub ([a] X, Z)) : α0 ⇒ α Z : γ Σ ∅ ` sub ([a] 0, Z) → 0 : Nat X : Nat, Z : γ Σ ∅ ` sub ([a] (suc X), Z) → suc (sub ([a] X, Z)) : Nat U : α, V : α ⇒ (Nat ⇒ α), X : Nat, Z : γ Σ ∅ ` sub ([a] (rec (U, V, X)), Z) → rec (sub ([a] U, Z), sub ([a] V, Z), sub ([a] X, Z)) : α U : α, V : α ⇒ (Nat ⇒ α), X : Nat, Z : γ Σ ∅ ` sub ([a] (rec (U, V, X)), Z) → rec (sub ([a] U, Z), sub ([a] V, Z), sub ([a] X, Z)) : α U : α, V : α ⇒ (Nat ⇒ α) Σ ∅ ` rec (U, V, 0) → U : α U : α, V : α ⇒ (Nat ⇒ α), X : Nat Σ ∅ ` rec (U, V, suc X) → (U (rec (U, V, X))) X : α For the additional rules, the specification of the declarations for the term-formers, 0, suc and rec, to the domain of natural numbers, results in the violation of condition 4 of definition 5.4, with respect to the declarations for sub and app in all cases except for the rule, Γ Σ ∅ ` rec (U, V, 0) → U : α, which is typeable-closed. One can define a lambda term for addition (and for the other arithmetic functions in example 5.4.1) over Church numerals, thus: ∅ Σ ∅ ` lam [m] lam [n] rec (m, lam [a] lam [b] suc a, n) : Nat ⇒ (Nat ⇒ Nat) 58 5.4 Further Examples 5.4.4 Untyped Lambda Calculus Suppose a base type, Lam, representing terms of an untyped lambda calculus and let Σ be the signature, {lam : h[Lam] Lam ,→ Lami, app : h(Lam × Lam) ,→ Lami, sub : h([Lam] Lam × Lam) ,→ Lami}. Let the applications, lam [a] s, app (s, t) and sub ([a] s, t), be written as λa.s, s t, and s [t/a], respectively. A set of rewrite rules, which define βη-reduction for the untyped lambda calculus, and all of which satisfy the conditions given for a typeable-closed rule in definition 5.4, are given below. X : Lam, Y : Lam Σ ∅ ` (λa.X) Y → X [Y /a] : Lam X : Lam Σ a#X ` λa.(X a) → X : Lam X : Lam, Z : Lam Σ a#X ` X [Z/a] → X : Lam Z : Lam Σ ∅ ` a [Z/a] → Z : Lam X : Lam, Y : Lam, Z : Lam Σ ∅ ` (X Y ) [Z/a] → X [Z/a] Y [Z/a] : Lam X : Lam, Z : Lam Σ b#Z ` (λb.X) [Z/a] → λb.(X [Z/a]) : Lam 5.4.5 Untyped Linear Lambda Calculus with Unbounded Recursion Consider an extension of example 5.4.4 for the untyped lambda calculus to an untyped version of the linear lambda calculus with unbounded recursion presented in in [Alves et al., 2011]. Suppose, again, a base type, Lam, representing untyped lambda terms and add to the signature, Σ, the following declaration associations: 0 : h() ,→ Lami, suc : hLam ,→ Lami, let : h([Lam] [Lam] Lam × Lam) ,→ Lami, par : h(Lam × Lam) ,→ Lami and rec : h(Lam × Lam × Lam × Lam) ,→ Lami. One can now represent closed reduction within the system using the following typeable-closed rewrite rules. X : Lam, Y : Lam Σ ∅ ` (λa.X) Y → X [Y /a] : Lam X : Lam Σ a#X ` λa.(X a) → X : Lam X : Lam, Z : Lam Σ a#X ` X [Z/a] → X : Lam Z : Lam Σ ∅ ` a [Z/a] → Z : Lam X : Lam, Y : Lam, Z : Lam Σ ∅ ` (X Y ) [Z/a] → X [Z/a] Y [Z/a] : Lam X : Lam, Z : Lam Σ b#Z ` (λb.X) [Z/a] → λb.(X [Z/a]) : Lam 59 5.4 Further Examples Z : Lam Σ ∅ ` 0 [a/Z] → 0 : Lam X : Lam, Z : Lam Σ ∅ ` (suc X) [a/Z] → suc (X [a/Z]) : Lam X : Lam, Y : Lam, Z : Lam Σ b # Z, c # Z ` (let ([b] [c] X, Y )) [a/Z] → let ([b] [c] (X [a/Z]), Y [a/Z]) : Lam X : Lam, Y : Lam, Z : Lam Σ ∅ ` (par (X, Y )) [a/Z] → par (X [a/Z], Y [a/Z]) : Lam V : Lam, W : Lam, X : Lam, Y : Lam, Z : Lam Σ ∅ ` (rec (V, W, X, Y )) [a/Z] → rec (V [a/Z], W [a/Z], X [a/Z], Y [a/Z]) : Lam V : Lam, W : Lam, X : Lam, Y : Lam Σ ∅ ` rec (par (0, V ), W, X, Y ) → W : Lam U : Lam, V : Lam, W : Lam, X : Lam, Y : Lam Σ ∅ ` rec (par (suc U, V ), W, X, Y ) → X (rec ((Y, par (U, V )), W, X, Y ) : Lam Extend this model again with base types, stkT, elemT and cfgT, representing stacks, stack elements and configurations, respectively, and let Σ0 be the signature, Σ ∪ {cfg : h(Lam×stkT),→cfgTi, elem : hLam,→elemTi, cns : h(elemT×stkT),→stkTi, recS1 : h(Lam× Lam × Lam) ,→ elemTi, recS2 : h(Lam × Lam × Lam × Lam) ,→ elemTi, letS : h[Lam] [Lam] Lam ,→ elemTi}. One now has the following typeable-closed rewrite rules to describe the stack machine for the extended lambda calculus defined above, as detailed in [Alves et al., 2011, Table 6]. 60 5.4 Further Examples M : Lam, N : Lam, S : stkT Σ0 ∅ ` cfg ((M N ), S) → cfg (M, cns (elem N, S)) : cfgT M : Lam, N : Lam, S : stkT Σ0 ∅ ` cfg (λa.M, cns (elem N, S)) → cfg (M [a/N ], S) : cfgT M : Lam, N : Lam, S : stkT Σ0 ∅ ` cfg (let ([a] [b] M, N ), S) → cfg (N, cns (letS [a] [b] M, S)) : cfgT N 1 : Lam, N 2 : Lam, M : Lam, S : stkT Σ0 b # N 1 ` cfg (par (N 1, N 2), cns (letS [a] [b] M, S) → cfg ((M [a/N 1]) [b/N 2], S) : cfgT N : Lam, U : Lam, V : Lam, W : Lam, S : stkT Σ0 ∅ ` cfg (rec (N, U, V, W ), S) → cfg (N, cns (recS1 (U, V, W ), S) : cfgT N 1 : Lam, N 2 : Lam, U : Lam, V : Lam, W : Lam, S : stkT Σ0 ∅ ` cfg (par (N 1, N 2), cns (recS1 (U, V, W ), S)) → cfg (N 1, cns (recS2 (N 2, U, V, W ), S) : cfgT T : Lam, U : Lam, V : Lam, W : Lam, S : stkT Σ0 ∅ ` cfg (0, cns (recS2 (T, U, V, W ), S)) → cfg (U, S) : cfgT N : Lam, T : Lam, U : Lam, V : Lam, W : Lam, S : stkT Σ0 ∅ ` cfg (suc N, cns (recS2 (T, U, V, W ), S)) → cfg (V, cns (elem rec ((W par (N, T )), U, V, W ), S)) : cfgT The translation of the rules for this stack machine to a system of typeable-closed nominal rewrite rules and their subsequent machine-checking by the author’s implementation identified a previously unidentified freshness condition needed to ensure closedness in one of the rules for the reduction of a let-expression (b # N 1). 61 5.4 Further Examples Conclusions This chapter has described two systems of typed nominal rewriting and one of typed nominal algebra. The first, typed nominal rewriting, is a general formulation that uses an equivariant nominal matching extended with conditions on types. Subject reduction is ensured and it allows non-closed rules to be typed. The second, closed typed nominal rewriting is much more efficient, allowing rules to be statically typed and uses standard nominal matching but is much more restrictive as to which rules are considered typeable. A formulation of typed nominal algebra follows from this second system as a corollary. The use of closedness is less central to the system of closed typed nominal rewriting presented here than in [Fairweather et al., 2011]. The use of closedness checks for rules annotated with types was later found to be insufficient and is superseded here by a refinement of the idea of essential typings proposed in [Fernández and Gabbay, 2007a]. As noted above, a hybrid system using typed and closed typed nominal rewriting, providing a maximum combination of expressiveness and efficiency remains an area of future work, as does a similar approach for typed nominal algebra. 62 Chapter 6 Dependent Types for Nominal Terms with Atom Substitutions his chapter aims to define a type system for nominal terms with a view to the T future development of a logical framework based on nominal techniques. A logical framework is a formal system that provides the facility to define a number of logical systems by abstraction of their common features. Commonalities such as name binding and substitution operations, the definition of formulae and rule schemes, and the representation, construction and checking of proofs can all form part of the logical framework. Such a system can thus greatly ease the development of certified programming environments, because the above concepts need then not be implemented for each logical system but just once for the abstracting framework. The Edinburgh Logical Framework (LF), [Harper et al., 1987], is one such system and a source of inspiration for some of the work in this chapter. The now well-known ‘propositions-as-types’ principle, following from the Curry-Howard isomorphism, [Howard, 1980], whereby types are viewed as propositions and terms of those types as proofs of the corresponding propositions, allows the checking of proofs of a logical system to be reduced to the checking of typing derivations of a type system. The minimum expressive power desirable of a type system used in a logical framework, is that types be allowed to depend upon terms; such types are known as ‘dependent’ types. Most importantly this lets types be built that depend upon object level variables of encoded logical systems. The facility to instantiate such variables is provided by the addition of atom substitutions to the language of nominal terms. Thus, this chapter develops a dependently typed syntax for nominal terms with atom 63 6.1 Nominal Terms with Atom Substitutions and Dependent Types substitutions. Decidability of freshness, alpha-equivalence and matching over this syntax is proved. The type system is then described followed by various example encodings including an extended example for first-order logic together with a proof of adequacy. 6.1 Nominal Terms with Atom Substitutions and Dependent Types This section first specifies the syntax of dependently typed nominal terms with atom substitutions, which comprises permutations, atom substitutions, pseudo-terms and pseudotypes. This is then followed by definitions for the actions of permutations and variable substitutions over these constructions. Note that although the definitions of this section all consider dependently typed nominal terms with atom substitutions, the untyped fragment of this language is worthy of equal interest and definitions are presented accordingly. 6.1.1 Syntax Consider countably infinite, pairwise disjoint sets of atoms, a, b, c, . . . ∈ A, variables, X, Y, Z, . . . ∈ X, term-formers, f, g, . . . ∈ F, and type-formers, C, C0 , . . . ∈ C. The syntax of permutations, π, atom substitutions, ϑ, pseudo-terms, s, t, and pseudo-types, σ, τ , is defined by mutual induction and generated by the grammar in definition 6.1. Definition 6.1 (Syntax) π ::= id | π ∗ (a b) ϑ ::= id | [a 7→ t] ∗ ϑ s, t ::= a | [a : σ] t | f t | (t1 , . . . , tn ) | ϑπ · X σ, τ ::= [a : σ] τ | C t | (τ1 × . . . × τn ) Recall that a permutation is a bijection on the set of atoms, A, such that π(a) 6= a for finitely many atoms, a ∈ A. An atom substitution is a mapping from atoms to pseudo-terms, equal to the identity mapping but for finitely many arguments. This mapping is represented as a list of pairs, ϑ, of the form, [ai 7→ ti ] such that the atoms, ai , are pairwise distinct. id represents the identity substitution. Note however that this list is interpreted as a set of simultaneous bindings and not as a sequence. Thus the value of an atom substitution, ϑ, for an atom, a, written ϑ(a) is uniquely defined. For a given atom substitution, ϑ, the domain of ϑ, written dom(ϑ), is the finite set of atoms, {a | ϑ(a) 6≡ a, a ∈ A} and the image of ϑ, img(ϑ), is the finite multi-set of pseudo-terms, {ϑ(a) | a ∈ dom(θ)}. Note that as for 64 6.1 Nominal Terms with Atom Substitutions and Dependent Types permutations, the final id and ‘list cons’ operators for atom substitutions will commonly be omitted. Atom substitutions act upon pseudo-terms and pseudo-types by instantiating atoms and are ‘capture-avoiding’. Like permutations, atom substitutions suspend upon variables and are applied after a suspended permutation. The addition of atom substitutions to the syntax of nominal terms in this system is motivated by the need to instantiate object level variables in encoded logical systems. Let the constructions for pseudo-terms be called respectively atom terms, abstractions, function applications, tuples and moderated variables and those for pseudotypes, an abstraction type, a constructed type and a product type. Let M, N, . . . range over elements of the union of the set of pseudo-terms and set of pseudo-types. Here, a moderated variable is a variable, X, together with a suspended permutation, π followed by a suspended atom substitution, ϑ. Due to the fact that constructed types may now depend upon terms, it is possible to build types that are not well-formed. Thus one must now ‘type’ types. This is achieved by a system of kinds, that define well-formed types. In this system, there is only one kind of well-formed types, written type. 6.1.2 Permutation Action The object-level action of permutations is defined for atom substitutions, pseudo-terms and pseudo-types in definition 6.2. Definition 6.2 (Permutation Action) π · id , id π · ([a 7→ t] ∗ ϑ) , [π(a) 7→ π · t] ∗ (π · ϑ) π · a , π(a) 0 π · (ϑπ · X) , π · ϑ(π @ π 0 ) · X π · [a : σ] t , [π(a) : π · σ] (π · t) π · f t , f (π · t) π · (t1 , . . . , tn ) , (π · t1 , . . . , π · tn ) π · [a : σ] τ , [π · a : π · σ] (π · τ ) π · C t , C (π · t) π · (τ1 × . . . × τn ) , (π · τ1 × . . . × π · τn ) 65 6.1 Nominal Terms with Atom Substitutions and Dependent Types 6.1.3 Freshness and Alpha-equivalence Call a # M a freshness constraint. Let ∆, ∇ range over sets of freshness constraints of the form a # X; call such sets freshness contexts. The shorthand notation, A # X , where, A and X are arbitrary sets of atoms and variables respectively, may be used to represent the freshness context {a # X | a ∈ A, X ∈ X }. The brackets of singleton sets may be omitted in such notation. Write ∆ ` a # M when a derivation exists using the rules given in definition 6.3 below. Definition 6.3 (Freshness Relation) ∆`a#b (atm)# img# (∆, a, ϑπ · X) a ∈ dom(ϑ) (var : aa)# ∆ ` a # ϑ π · X img# (∆, a, ϑπ · X) a 6∈ dom(ϑ) π -1 (a) # X ∈ ∆ (var : ab)# ∆ ` a # ϑ π · X ∆`a#τ (abs : aa)# ∆ ` a # [a : τ ] s ∆`a#s ∆`a#τ (abs : ab)# ∆ ` a # [b : τ ] s ∆ ` a # s1 . . . ∆ ` a # sn (tpl)# ∆ ` a # (s1 , . . . , sn ) ∆`a#σ (abt : aa)# ∆ ` a # [a : σ] τ ∆`a#s (app)# ∆`a#fs ∆`a#τ ∆`a#σ (abt : ab)# ∆ ` a # [b : σ] τ ∆ ` a # τ1 . . . ∆ ` a # τn (prd)# ∆ ` a # (τ1 × . . . × τn ) ∆`a#t (cns)# ∆ ` a # Ct The main differences with respect to the freshness relation for nominal terms given in definition 2.16 are the introduction of new rules for types, (abt : aa)# , (abt : ab)# , (prd)# and (cns)# , and the rules for moderated variables, (var : aa)# and (var : ab)# , which take into account suspended atom substitutions as well as suspended permutations. The notation, img# (∆, a, ϑπ ·X), in these rules, defines the conditions necessary for freshness with regard to the suspended atom substitution and is an abbreviation of the following finite set of hypotheses. ∆ ` a # ϑ(π(b)) ∨ b # X ∈ ∆ | b ∈ A, π(b) ∈ dom(ϑ) Informally this specifies that for each atom, π(b), in the domain of the atom substitution, ϑ, either a must be fresh for its value under ϑ or that atom must not occur in the variable, X, so that it cannot be instantiated. This ensures that the substitution will not 66 6.1 Nominal Terms with Atom Substitutions and Dependent Types introduce the atom a when it is applied to an instance of X. However this disjunction of conditions results in the possibility of multiple derivations for a given freshness constraint, as seen in the below example, where img# may be satisfied either by the constraint b # Y or by a # X. Example 6.1 (Non-determinacy of Freshness) b # X, b # Y, a # X ` b # [a 7→ Y ] · X If one considers the suspended atom substitution, ϑ, to be id, the conditions upon ϑ are satisfied vacuously and the two rules clearly reduce to that for nominal terms, given in definition 2.16. Call M ≈α N an alpha-equality constraint and write ∆ ` M ≈α N when a derivation exists using the rules given in definition 6.4 below. Note that because alphaequivalence is defined using freshness, again multiple possible derivations may exist for a given constraint. Definition 6.4 (Alpha-equivalence Relation) ∆ ` a ≈α a (atm)α ∀a ∈ ds(ϑ1 π1 , ϑ2 π2 ) a # X ∈ ∆ (var)α ∆ ` ϑ1 π1 · X ≈α ϑ2 π2 · X ∆ ` σ ≈α τ ∆ ` s ≈α t (abs : aa)α ∆ ` [a : σ] s ≈α [a : τ ] t ∆ ` σ ≈α τ ∆ ` a # t ∆ ` s ≈α (a b) · t (abs : ab)α ∆ ` [a : σ] s ≈α [b : τ ] t ∆ ` s1 ≈α t1 . . . ∆ ` sn ≈α tn ∆ ` (s1 , . . . , sn ) ≈α (t1 , . . . , tn ) (tpl)α ∆ ` s ≈α t (app)α ∆ ` f s ≈α f t ∆ ` σ1 ≈α τ1 ∆ ` σ2 ≈α τ2 (abt : aa)α ∆ ` [a : σ1 ] σ2 ≈α [a : τ1 ] τ2 ∆ ` σ1 ≈α τ1 ∆ ` a # τ2 ∆ ` σ2 ≈α (a b) · τ2 (abt : ab)α ∆ ` [a : σ1 ] σ2 ≈α [b : τ1 ] τ2 ∆ ` σ1 ≈α τ1 . . . ∆ ` σn ≈α τn (prd)α ∆ ` (σ1 × . . . × σn ) ≈α (τ1 × . . . × τn ) ∆ ` s ≈α t (cns)α ∆ ` C s ≈α C t This presentation of alpha-equivalence is defined by induction on the size of the pair, (M, N ), and is both syntax-directed and decidable when considered as a recursive predicate. It is a generalisation of the notion of alpha-equivalence on nominal terms presented in definition 2.17. 67 6.1 Nominal Terms with Atom Substitutions and Dependent Types The only case that is not straightforward is again that of a moderated variable, (var)α . Here it is important to remember that both permutations and atom substitutions are finite mappings and that the image of a suspended atom substitution is given as a sub-term of the syntax of the moderated variable. Thus, the disagreement set of two suspensions, ϑ1 π1 and ϑ2 π2 , written ds(ϑ1 π1 , ϑ2 π2 ), is also finite and may be defined as {a | ϑ1 (π1 (a)) 6≈α ϑ2 (π2 (a)), a ∈ A}, which although a recursive step is of decreasing size. Therefore, informally, this rule says that two moderated variables of the same variable are alpha-equivalent if for every atom, the value under either the images of the atom under the two suspensions are alpha-equivalent, ϑ1 (π1 (a)) ≈α ϑ2 (π2 (a)), or the atom is fresh for that variable, a # X. Example 6.2 (Alpha-equivalence) b # X ` [a 7→ c] · X ≈α [b 7→ c](a b) · X In this example, the condition of rule (var)α holds, because a [a 7→ c] = c ≈α c = ((a b) · a) [b 7→ c], c [a 7→ c] = c ≈α c = ((a b) · c) [b 7→ c] and although b [a 7→ c] = b 6≈α a = ((a b) · b) [b 7→ c], there is a freshness constraint, b # X. The reflexivity, symmetry and transitivity of the alpha-equivalence relation can be proved by adapting the proofs given in [Fernández and Gabbay, 2007b]. 6.1.4 Atom Substitution Action The action of an atom substitution, ϑ, upon a pseudo-term or pseudo-type, M , written, M ϑ, is defined by induction in the presence of a freshness context, ∆, in definition 6.6. For the sake of clarity of presentation this freshness context is not explicitly written throughout the definition. Let ϑ−a denote the atom substitution ϑ restricted to the domain, dom(ϑ) \ {a}. The composition of two atom substitutions, written ϑ1 ◦ ϑ2 , is defined as the atom substitution equivalent to applying ϑ1 followed by ϑ2 . The syntactic construction of such a composition built from two substitutions represented as sets of bindings is given in definition 6.5 by adapting the algorithm described in [Baader and Snyder, 2001, 2.1]. Note that this operation itself uses the action of an atom substitution upon pseudo-term and so is defined simultaneously with definition 6.6 and is also parameterised by the freshness context, ∆. 68 6.1 Nominal Terms with Atom Substitutions and Dependent Types Definition 6.5 (Composition of Atom Substitutions) 1. For each binding, [a 7→ t], in ϑ1 , apply ϑ2 to t; call this substitution ϑ01 . 2. Let ϑ2 0 be ϑ2 restricted to the domain, dom(ϑ2 ) \ dom(ϑ1 ). 3. Let ϑ1 00 be ϑ1 0 restricted to the domain, {a | ϑ1 0 (a) 6≡ a, a ∈ dom(ϑ1 0 )}. 4. Return the concatenation of ϑ2 0 onto ϑ1 00 . Definition 6.6 (Action of Atom Substitution) a ϑ , ϑ(a) a ∈ dom(ϑ) aϑ,a (ϑ0 π · X) ϑ , (ϑ0 ◦ ϑ)π · X a 6∈ dom(ϑ) ([a : σ] s) ϑ , [c : σ ϑ] ((a c) · s) ϑ−c ∆ ` c # s, c # img(ϑ) (f s) ϑ , f (s ϑ) (t1 , . . . , tn ) ϑ , (t1 ϑ, . . . , tn ϑ) ([a : σ] τ ) ϑ , [c : σ ϑ] ((a c) · τ ) ϑ−c ∆ ` c # τ, c # img(ϑ) (C s) ϑ , C (s ϑ) (τ1 × . . . × τn ) ϑ , (τ1 ϑ × . . . × τn ϑ) The capture-avoidance of unabstracted atoms is ensured by the fact that when an atom substitution acts upon an abstraction or abstraction type, a suitable alpha-equivalent representative is first chosen with respect to the freshness context, ∆. In practice, this presentation will result in the creation of freshness constraints for atoms, newly-generated with respect to the system as a whole, and is similar to the approach taken in [Fernández and Gabbay, 2010]. A suitable ‘even fresher’ atom always exists, and it is one’s right to add constraints for that atom to the freshness context, a fact which is taken advantage of below in theorem 6.1 and definition 6.11. Any implementation of this definition as a recursive function must accommodate a suitable mechanism for the generation of such names; this is most easily achieved by the threading of global state throughout the function or by the use of a global choice function that returns the last used or next available name. Atom substitutions work uniformly on alpha-equivalence classes of pseudo-terms and pseudo-types. Theorem 6.1 Let M and N be two pseudo-terms or two pseudo-types; if ∆ ` M ≈α N then there exists a freshness context, ∆# such that for each constraint, a # X ∈ ∆# , a 6∈ atms(M ) ∪ atms(N ) ∪ atms(∆) and ∆ ∪ ∆# ` M ϑ ≈α N ϑ. 69 6.1 Nominal Terms with Atom Substitutions and Dependent Types Proof 6.1 By induction on definition 6.4 for alpha-equivalence together with definition 6.6 for atom substitution. For the cases of an abstraction and abstraction type, the freshness constraint holds by the conditions imposed upon the choice of alpha-equivalent representative. 6.1.5 Variable Substitutions Definition 6.7 (Variable Substitutions) A variable substitution is a mapping from variables to pseudo-terms, equal to the identity mapping but for finitely many arguments, and written as a set of bindings [X1 7→ s1 ] . . . [Xn 7→ sn ], such that the variables, X1 , . . . , Xn , are pairwise distinct. The action of variable substitutions upon atom substitutions, pseudo-terms and pseudotypes is given in definition 6.8. A variable substitution, θ, acts upon an atom substitution, ϑ, by instantiating the variables occurring in the pseudo-terms of the image of ϑ, and is written ϑ θ. Note that the instantiation of a variable requires the application of an atom substitution to a pseudo-term and thus the action of variable substitutions is also parameterised by a freshness context, which again is left implicit in the definition below. Definition 6.8 (Variable Substitution Action) id θ , id ([a 7→ t] ∗ ϑ) θ , [a 7→ t θ] ∗ (ϑ θ) aθ,a (ϑ π · X) θ , (π · θ(X)) (ϑ θ) (ϑπ · X) θ , (ϑ θ)π · X X ∈ dom(θ) X 6∈ dom(θ) ([a : σ] t) θ , [a : σ θ] (t θ) (f t) θ , f (t θ) (t1 , . . . , tn ) θ , (t1 θ, . . . , tn θ) ([a : σ] τ ) θ , [a : σ θ] (τ θ) (C t) θ , C (t θ) (τ1 × . . . × τn ) θ , (τ1 θ × . . . × τn θ) 6.1.6 Constraint Problems Let C range over freshness and alpha-equality constraints; a constraint problem, C, is an arbitrary set of such constraints. Extend the above notations for the derivability of constraints element-wise to constraint problems; thus, write ∆ ` {C1 , . . . , Cn } for ∆ ` C1 , . . . , ∆ ` Cn . Due to the action of atom substitution, the action of variable 70 6.1 Nominal Terms with Atom Substitutions and Dependent Types substitutions upon constraints and constraint problems is now also subject to the fact that if ∆ ` C θ then ∆ may contain constraints for atoms, a 6∈ atms(C). If solvable, a solution to a constraint problem, C, is a set of freshness contexts, F, such that for each element, ∆ ∈ F then ∆ ` C. Notice that this is in contrast to nominal terms, where if a constraint problem is solvable, the solution is a unique, minimal freshness context, as described in section 2.4.7. This is a result of the conditions for rules (var : aa)# and (var : ab) of definition 6.3, which allow a given freshness constraint to have more than one derivation. Due to this non-determinacy, in order to solve constraint problems, it is first necessary to define a notion of combining such solutions. Given two sets of freshness contexts, F1 and F2 , define their combination, F1 t F2 as follows. Definition 6.9 F1 t F2 , {∆1 ∪ ∆2 | (∆1 , ∆2 ) ∈ F1 × F2 } Given the notion of how to combine two sets of freshness contexts, given in definition 6.9, the definition of an algorithm to build the set of freshness contexts required to satisfy a freshness constraint, a # M , is straightforward by the inductive and syntaxdirected nature of definition 6.3 and has been implemented. The only cases that are not trivial, are those for a moderated variable, a # ϑπ · X, namely, rules (var : aa)# and (var : ab)# of definition 6.3. Here, for each atom, b, such that ϑ(π(b)) 6≡ b, one recursively constructs the solution to a # ϑ(π(b)) and takes its union with the additional freshness context, {b # X}. One now combines these sets of freshness contexts in a pairwise fashion to form a single set of freshness contexts. Finally if the atom, a, itself is such that ϑ(π(a)) ≡ a then for each freshness context in the solution so far one takes its union with the additional freshness constraint a # X. Termination is guaranteed because as previously discussed in section 6.1.3, the domain of the suspension ϑπ is finite and the image of ϑ is given as a sub-term of the moderated variable. Given this algorithm to build solutions to freshness constraints, and the notion of how to combine solutions, further algorithms to solve alpha-equivalence constraints and constraint problems in general can easily be defined, based upon that for constraint problems given in [Urban et al., 2004]. 6.1.7 Matching The type system introduced in section 6.2 requires a formalisation of matching to check that term-formers and type-formers are used in a way that is consistent with their respective type declarations. The concepts of nominal matching, section 2.4.10, are now therefore extended to nominal terms with atom substitutions. 71 6.1 Nominal Terms with Atom Substitutions and Dependent Types Definition 6.10 (Matching Problem) Given a constraint problem, C, . . . , Mi ≈α Ni , . . . , a corresponding matching prob S S lem is defined when vars(Mi ) ∩ vars(Ni ) = ∅ and is written . . . , Mi ? ≈α Ni , . . . . A solution to such a problem, if one exists, is a pair, (F, θ), of a set of freshness S contexts, F, and a variable substitution, θ, such that dom(θ) ⊆ vars(Mi ) and for each element ∆ ∈ F, ∆ ` C θ. Informally, this says that a matching problem is a constraint problem in which one adds the restriction that the variables in the left-hand sides of alpha-equality constraints are disjoint from the variables in the right-hand sides and that only variables in the left-hand sides of equality constraints may be instantiated. Remember that for nominal terms with atom substitutions each possible freshness context of a solution may contain constraints for atoms, a 6∈ atms(C). Definition 6.11 (Pattern Matching Problem) A pattern matching problem, consists of two pseudo-terms-in-context or two pseudotypes-in-context, ∇ ` M and ∆ ` N , to be matched, where vars(∇ ` M ) ∩ vars(∆ ` N ) = ∅ and is written (∇ ` M ) ? ≈α (∆ ` N ). A solution to such a problem, if one exists, is a variable substitution, θ, such that (F, θ) is a solution to the matching problem ∇ ∪ {M ? ≈α N } and there exist freshness contexts, ∆# of which each constraint, a # X, a 6∈ atms(M ) ∪ atms(N ) ∪ atms(∇), and ∇0 ∈ F, such that ∆ ∪ ∆# ` ∇0 . Write sol((∇ ` M ) ? ≈α (∆ ` N )) to denote such a solution. Nominal matching problems enjoy uniqueness of most general solutions but this is not the case for matching problems here, as one can see from example 6.3. Example 6.3 (Non-determinancy of Matching) [a 7→ b] · X ? ≈α b ∅ ` ([a 7→ b] · X) [X 7→ a] ≈α b ∅ ` ([a 7→ b] · X) [X 7→ b] ≈α b The situation is similar for logical frameworks based on the lambda calculus. The solution there is to restrict the form of matching problems. Inspired by LF, [Pfenning and Schürmann, 1999], the type system in section 6.2 is designed so that one only needs to match against a pattern term, t, such that for any variable, X ∈ vars(t), then idπ · X is a sub-term of t. Thus, the value of X is uniquely determined. Note that a solution may only instantiate variables in the pattern and so if an atom substitution occurs in the matched term then it can be treated as a constant sub-term. Using this assumption, the algorithm is similar to that used to check alpha-equivalence, except that when a variable sub-term, ϑπ · X, of the pattern is being matched against, if 72 6.2 Type System ϑ is not id then that constraint is postponed until after the constraint for the occurrence of the idπ · X sub-term in the pattern has been solved and a unique variable substitution generated. As noted in section 6.1.4, one must also take into account the creation of freshness constraints for atoms newly-generated with respect to the entire system under consideration. Such an algorithm has been implemented by the author. 6.1.8 Unification The type system also requires a notion of unification in order to ensure that given type declarations do not overlap. Definition 6.12 (Unification Problem) Given a constraint problem, C, . . . , Mi ≈α Ni , . . . , the corresponding unification problem is written . . . , Mi ? ≈α ? Ni , . . . . A solution to such a problem, if one exists, is a pair, (F, θ), of a set of freshness contexts, F, and a variable substitution, θ, such that for each element ∆ ∈ F, ∆ ` C θ. Definition 6.13 (Pattern Unification Problem) A pattern unification problem, consists of two pseudo-terms-in-context or two pseudotypes-in-context, ∇1 ` M and ∇2 ` N , to be unified, where vars(∇1 ` M )∩vars(∇2 ` N ) = ∅ and is written (∇1 ` M ) ? ≈α ? (∇2 ` N ). A solution to such a problem, if one exists, is a variable substitution, θ, such that (F, θ) is a solution to the unification problem ∇1 ∪ ∇2 ∪ {M ? ≈α ? N }. 6.1.9 Closedness The definition of a newly-freshened variant, as specified in section 2.4.13, is extended to nominal terms with atom substitutions in the obvious way and given definition 6.10, specifying matching problems and their solutions for nominal terms with atom substitutions, a corresponding notion of closedness may be defined in a manner similar to section 2.4.14. An algorithm to check closedness in this way has been implemented by the author. It is sometimes useful to be able to check the overall closedness of a set of pseudo-terms and pseudo-types, ∆ ` {. . . , s, σ, t, τ, . . .}. This can easily be simulated by checking the closedness of the product type, ( . . . × C s × σ × C t × τ × . . . ). 6.2 Type System This section starts by introducing the syntax of environments, declarations and judgements used in this type system. The validity of environments (Definition 6.17), validity of sets of declarations (Definition 6.16), and derivability of typing judgements (Definition 6.19), 73 6.2 Type System are then defined by mutual induction. An incremental, ‘boot-strapping’ approach allows the derivation of complex type judgement derivations without circularity, as can be seen from the examples in section 6.3.1. 6.2.1 Syntax A type association is a pair of a variable, X, and a type, σ, written (X : σ) or an atom and a type, written (a : σ). A pseudo-environment is an ordered list of type associations. A pseudo-environment may contain at most one type association for each variable but note that unlike in chapters 3 to 5, it may contain more than one association for a given atom. Here, let Γ o n (a : τ ), denote the result of appending (a : τ ) to the end of the list that represents the pseudo-environment, Γ (similarly for a variable association) and let this notation be extended element-wise to lists of associations. Thus, when searching for the type of an atom in Γ, one starts from the end. The endmost association available for a given atom, a, in Γ is denoted by Γa . If there is no type association for a in Γ then Γa is undefined, written ⊥ (similarly for a variable.) It is important to note that type associations for variables are never appended by the typing rules, however the rules for abstractions and abstraction types do append type associations for atoms. A pseudo-declaration states the type associations and freshness constraints that a term must satisfy in order that that an application or constructed type built from that term is well-formed. Definition 6.14 (Pseudo-declarations) Γ ∆ ` f t : hσ ,→ τ i Γ ∆ ` C t : hσ ,→ typei Thus, informally, this definition says that if under the type associations in Γ, and the freshness constraints in ∆, t has type σ then f t has type τ , or similarly that C t is of the kind, type. In practice, users need not give complete declarations; it is sufficient to write Γ∆`ft : τ Γ ∆ ` C t : type and the system will infer the complete declaration by computing the type of t. Pseudo-declarations are given for a term-former or type-former together with an argument term in order to allow the use of atoms of that argument in the type of the application 74 6.2 Type System or constructed type. This necessitates the use of unification over the syntax to eliminate the possibility of overlapping declarations. Pseudo-judgements are finally presented in definition 6.15. A pseudo-judgement specifies that under a given environment, set of declarations and freshness context, either a term has a particular type or a type is well-formed. Definition 6.15 (Pseudo-judgements) Γ Σ ∆ ` t : τ Γ Σ ∆ ` τ : type 6.2.2 Valid Sets of Declarations A valid set of declarations, Σ, written validD(Σ), is defined inductively as follows. Definition 6.16 (Valid Set of Declarations) • The empty set of declarations, ∅, is valid; validD(∅). • If validD(Σ), Γ ∆ ` f t : hσ ,→ τ i is a valid declaration under the following conditions. – Γ Σ ∆ ` t : σ, where vars(σ) ⊆ vars(t) ∪ vars(τ ) – Γ Σ ∆ ` τ : type, where τ is not an abstraction type – ∆ ` {t, τ, σ} is closed – for any variable, X ∈ vars(t) ∪ vars(τ ), then the sub-term, idπ · X, occurs in either t or τ . Then, provided that there is no declaration in Σ of the form Γ0 ∆0 ` f t0 : hσ 0 ,→ τ 0 i such that there is a solution to the pattern unification problem, (∆ ` t) ? ≈α ? (∆0 ` t0 ), as described in definition 6.13, it holds that validD(Σ ∪ {Γ ∆ ` f t : hσ ,→ τ i}). • If validD(Σ), then Γ ∆ ` C t : hσ ,→ typei is a valid declaration under the following conditions. – Γ Σ ∆ ` t : σ, where vars(σ) ⊆ vars(t) – ∆ ` {t, σ} is closed – for any variable, X ∈ vars(t) then the sub-term, idπ · X, occurs in t Then, provided that there is no declaration in Σ of the form Γ0 ∆0 ` C t0 : hσ 0 ,→ typei such that ∆ ` t and ∆0 ` t0 are unifiable, validD(Σ∪{Γ ∆ ` C t : hσ,→typei}). 75 6.2 Type System Example 6.4 (Valid Sets of Declarations) Let lam be a term-former to build functions on natural numbers and let Nat and FunNat be type-formers representing the natural numbers and functions from Nat to Nat, respectively. ∅ ` Nat : type ∅ ` FunNat : type X : Nat ∅ ` lam [a : Nat] X : h[a : Nat] Nat ,→ FunNat i 6.2.3 Valid Environments Assuming a freshness context, ∆, and a valid set of declarations, Σ, a valid environment, written validE(Γ, Σ, ∆), is defined as follows in definition 6.17. Definition 6.17 (Valid Environments) • The empty list of type associations, −, is a valid environment; validE(−, Σ, ∆). • If validE(Γ, Σ, ∆), then validE(Γ o n (a : τ ), Σ, ∆), provided that Γ Σ ∆ ` τ : type. • If validE(Γ, Σ, ∆), then validE(Γ o n (X : τ ), Σ, ∆), provided that Γ Σ ∆ ` τ : type and for any atom, a, such that ∆ ` a # X then ∆ ` a # τ . Informally, the final condition upon variable associations says that in a valid environment, if an atom cannot occur in an instance of a variable, X, then it cannot occur in the type given that variable either. 6.2.4 Typing Judgements Typing judgements are derived inductively using the rules given in definition 6.19. The declarations provided by the user are required in the rules for term-formers, (app)τ , and type-formers, (cns)τ . Declarations are matched to pseudo-terms and pseudo-types using pattern matching and therefore each time an application or constructed type is typed a newly-freshened variant of the declaration for that term-former or type-former is generated. Write ΓN ∆N ` f tN : hσ N ,→ τ N i ∈ ΣN (similarly for a constructed type) to emphasise the fact that such a newly-freshened variant of a declaration is being used. In the rule for a variable, (var)τ , a predicate, typS is used. Definition 6.18 Write typV(Γ, Σ, ∆, ϑπ, X) if for any atom a such that ∆ 6` a # X and ϑ(π(a)) 6≡ a then Γa ≡ τ and Γ Σ ∆ ` ϑ(π(a)) : (π · τ ) ϑ. 76 6.2 Type System This definition indicates that for any atom, a, that can occur in the variable, X, and such that ϑ(π(a)) 6≡ a then ϑ(π(a)) must be typeable with a type compatible with the type of a. Definition 6.19 (Typing rules) validE(Γ, Σ, ∆) Γa ≡ τ (atm)τ Γ Σ ∆ ` a : τ validE(Γ, Σ, ∆) ΓX ≡ τ typV(Γ, Σ, ∆, ϑπ, X) (var)τ Γ Σ ∆ ` ϑπ · X : (π · τ ) ϑ Γo n (a : σ) Σ ∆ ` t : τ (abs)τ Γ Σ ∆ ` [a : τ ] t : [a : σ] τ validE(Γ, Σ, ∆) (tpl : 0)τ Γ Σ ∆ ` () : () Γ Σ ∆ ` t1 : τ1 . . . Γ Σ ∆ ` tn : τn (tpl : n)τ Γ Σ ∆ ` (t1 , . . . , tn ) : (τ1 × . . . × τn ) Γ Σ ∆ ` t : σ N θ Γ Σ ∆ ` τ : type (app)τ Γ Σ ∆ ` f t : τ ΓN ∆N ` f tN : hσ N ,→ τ N i ∈ ΣN θ = sol((∆N ` (C tN × τ N )) ? ≈α (∆ ` (C t × τ ))) Γo n (a : σ) Σ ∆ ` τ : type (abt)τ Γ Σ ∆ ` [a : σ] τ : type validE(Γ, Σ, ∆) (prd : 0)τ Γ Σ ∆ ` () : type Γ Σ ∆ ` τ1 : type . . . Γ Σ ∆ ` τn : type (prd : n)τ Γ Σ ∆ ` (τ1 × . . . × τn ) : type Γ Σ ∆ ` t : σ N θ (cns)τ Γ Σ ∆ ` C t : type ΓN ∆N ` C tN : hσ N ,→ typei ∈ ΣN ! θ = sol((∆N ` tN ) ? ≈α (∆ ` t)) Γ Σ ∆ ` t : τ ∆ ` σ ≈ α τ (α)τ Γ Σ ∆ ` t : σ Note that in the rules, (atm)τ , (var)τ , (tpl : 0)τ and (prd : 0)τ , the validity of the environment is needed as a premise because the environment, Γ, is not assumed to be valid and also, that in the rule, (var)τ , the suspension, ϑπ, must be applied to the type, τ . 77 ! 6.3 Examples 6.3 Examples In this section, examples are given to show how the type system given previously in section 6.2 can be used to specify various formal systems. 6.3.1 First-order Logic First-order logic is a proto-typical system with binding. Start the specification by defining type-formers for natural numbers, propositions and proofs, and term-formers to build numbers, 0 (zero) and s (successor) and propositions, bot (⊥), imp (⇒) and all (∀). − ∅ ` N : type − ∅ ` Prop : type P : Prop ∅ ` Proof P : type −∅`0: N X : N ∅ ` sX : N − ∅ ` bot : Prop P1 : Prop, P2 : Prop ∅ ` imp (P1 , P2 ) : Prop P : Prop ∅ ` all [x : N] P : Prop Now, define declarations for the predicates used to build proofs; the introduction and elimination of imp and all, impi , impe , alli and alle and the elimination of bot, bote . P1 : Prop, P2 : Prop, Q : Proof P2 x # P1 , x # P2 ` impi [x : Proof P1 ] Q : Proof imp (P1 , P2 ) P1 : Prop, P2 : Prop, Q : Proof imp (P1 , P2 ), Q1 : Proof P1 ∅ ` impe (Q, Q1 ) : Proof P2 P : Prop, Q : Proof bot ∅ ` bote (P, Q) : Proof P P : Prop, Q : [x : N] Proof P ∅ ` alli Q : Proof all [x : N] P P : Prop, Q : Proof all [x : N] P, N : N x # N ` alle (Q, N ) : Proof [x 7→ N ] · P 78 6.3 Examples Note that in the declaration for bote one must use variables, P and Q, as arguments because of the restriction that all variables in the type of bote should occur in its arguments or in its types. Also, note that in the declaration for alli above, the variable, Q, of type [x : N] Proof P , is used, in other words, n is not unabstracted, as expected. The full declaration, which can be inferred is P : Prop, Q : [x : N] Proof P ∅ ` alli Q : h[x : N] Proof P ,→ Proof all ([x : N] P )i. When this declaration is used to type terms built with alli , pattern matching is used to obtain the values of P and Q (examples are given below.) Finally, define the predicates ge (greater than or equal to, ≥) and ev (even) on natural numbers. X : N, Y : N ∅ ` ge (X, Y ) : Prop X : N ∅ ` g0 X : Proof ge (X, 0) X : N, Y : N, P : Proof ge (X, Y ) ∅ ` gs P : Proof ge (s X, s Y ) X : N ∅ ` ev X : Prop − ∅ ` e0 : Proof ev 0 X : N, P : Proof ev X ∅ ` ess P : Proof ev s s X Note that, similarly to the declaration of bote , one needs to use the variable, X, in the declaration for g0 . Examples now follow that show how to derive proofs using the declarations given above. In these derivations uses of the rule, (app)τ , are numbered and the solution of the pattern matching problem noted afterward, together with the declaration used and the type inferred for the argument to the term-former or type-former. Example 6.5 The first example demonstrates that all [x : N] ge (x, 0) is a well-formed formula. First, note that the full declaration of all, P : Prop ∅ ` all [x : N] P : h[x : N] Prop ,→ Propi, can be obtained from the abbreviated declaration by inferring the type of [x : N] P , as follows in the first derivation below. The second derivation then shows that − Σ ∅ ` all [x : N] ge (x, 0) : Prop. validE((P : Prop, x : N), Σ, ∅) typV((P : Prop, x : N), Σ, ∅, idid, P ) (var) P : Prop, x : N Σ ∅ ` P : Prop (abs) P : Prop Σ ∅ ` [x : N] P : [x : N] Prop 79 6.3 Examples .. validE((x : N), Σ, ∅) . (atm) (app) x : N Σ ∅ ` x : N x : N Σ ∅ ` 0 : N (tpl : n) x : N Σ ∅ ` (x, 0) : (N × N) (app)2 x : N Σ ∅ ` ge (x, 0) : Prop (abs) − Σ ∅ ` [x : N] ge (x, 0 ) : [x : N] Prop (α) − Σ ∅ ` [x : N] ge (x, 0 ) : [xN : N] Prop (app)1 − Σ ∅ ` all [x : N] ge (x, 0) : Prop 1. P N : Prop ∅ ` all [xN : N] P N : Prop θ = [P N 7→ ge (x, 0)] σ N = [xN : N] Prop 2. X N : N, Y N : N ∅ ` ge (X N , Y N ) : Prop θ = [X N 7→ x][Y N 7→ 0] σ N = (N × N) Example 6.6 It is now shown that alli [x : N] g0 x is a proof of all [x : N] ge (x, 0). The full declaration of alli , P1 : Prop, P2 : [x : N] Proof P1 ∅ ` alli P2 : h[x : N] Proof P1 ,→Proof (all [x : N] P1 )i, can be easily inferred from the abbreviated declaration. validE((x : N), Σ, ∅) (atm) x : N Σ ∅ ` x : N (app)2 x : N Σ ∅ ` g0 x : Proof ge (x, 0) (abs) − Σ ∅ ` [x : N] g0 x : [x : N] Proof ge (x, 0) (α) − Σ ∅ ` [x : N] g0 x : [xN : N] Proof ge (xN , 0) (app)1 − Σ ∅ ` alli ([x : N] g0 x) : Proof all [x : N] ge (x, 0) 1. P N : Prop, QN : [xN : N] Proof P N ∅ ` alli QN : Proof all [xN : N] P N θ = [P N 7→ ge (xN , 0)][QN 7→ [x : N] g0 x] σ N = [xN : N] Proof P N 2. X N : N ∅ ` g0 X N : Proof ge (X N , 0) θ = [X N 7→ x] σ N = N Example 6.7 In the final example of this group, the proof of the previous example is instantiated at x to give a proof of Proof ge (0, 0). 80 6.3 Examples .. . (app) x : N Σ ∅ ` g0 x : Proof ge (x, 0 ) (abs) − Σ ∅ ` [x : N] g0 x : [x : N] Proof ge (x, 0 ) (α) − Σ ∅ ` [x : N] g0 x : [xN : N] Proof ge (xN , 0 ) .. (app)2 − Σ ∅ ` alli [x : N] g0 x : Proof all [x : N] ge (x, 0 ) . (α) (app) N N − Σ ∅ ` alli [x : N] g0 x : Proof all [x : N] ge (x , 0 ) − Σ ∅ ` 0 : N (tpl : n) − Σ ∅ ` (alli [x : N] g0 x, 0) : (Proof all [xN : N] ge (xN , 0 ) × N) 1 (app) − Σ ∅ ` alle (alli [x : N] g0 x, 0) : Proof ge (0, 0) 1. P N : Prop, QN : Proof all [xN : N] P N , N N : N xN # N N ` alle (QN , N N ) : Proof [xN 7→ N N ] · P N θ = [P N 7→ ge (xN , 0)][QN 7→ alli ([x : N] g0 x)][N N 7→ 0] σ N = (Proof all ([xN : N] P N ) × N) 2. P N : Prop, QN : [xN : N] Proof P N ∅ ` alli QN : Proof all [xN : N] P N θ = [P N 7→ ge (xN , 0)][QN 7→ [x : N] g0 x] σ N = [xN : N] Proof P N Example 6.8 This example demonstrates that the term, ev s s 0 , is a well-formed formula. validE(−, Σ, ∅) (tpl : 0) − Σ ∅ ` () : () (app) − Σ ∅ ` 0 : N (app) − Σ ∅ ` s 0 : N (app)2 − Σ ∅ ` s s 0 : N (app)1 − Σ ∅ ` ev s s 0 : Prop 1. X N : N ∅ ` ev X N : Prop θ = [X N 7→ s s 0 ] σ N = N 2. XN : N ∅ ` s XN : N θ = [X N 7→ s 0] σ N = N Example 6.9 It is now shown that the term, ess e0 , is a proof of the formula in the previous example. .. . (app) − Σ ∅ ` e0 : Proof (ev 0) (app)1 − Σ ∅ ` ess e0 : Proof ev s s 0 1. X N : N, P N : Proof ev X N ∅ ` ess P N : Proof ev s s X N θ = [P N 7→ e0 ][X N 7→ 0] σ N = Proof (ev X N ) 81 6.3 Examples Example 6.10 In the first of the final pair of examples, it is demonstrated that all [x : N] imp (ev x, ev s s x) is a well-formed formula. .. .. . . (app) (app) x : N Σ ∅ ` ev x : Prop x : N Σ ∅ ` ev s s x : Prop (tpl : n) x : N Σ ∅ ` (ev x, ev s s x) : (Prop × Prop) 2 (app) x : N Σ ∅ ` imp (ev x, ev s s x) : Prop (abs) x : N Σ ∅ ` [x : N] imp (ev x, ev s s x) : [x : N] Prop (α) x : N Σ ∅ ` [x : N] imp (ev x, ev s s x) : [xN : N] Prop (app)1 − Σ ∅ ` all [x : N] imp (ev x, ev s s x) : Prop 1. P N : Prop ∅ ` all [xN : N] P N : Prop θ = [P N 7→ imp (ev x, ev (s s x))] σ N = [xN : N] Prop 2. P1 N : Prop, P2 N : Prop ∅ ` imp (P1 N , P2 N ) : Prop θ = [P1 N 7→ ev x][P2 N 7→ ev (s s x)] σ N = (Prop × Prop) Example 6.11 Now it is shown that alli [x : N] impi [p : Proof ev x] ess p is a proof of the previous formula. .. . (app) x : N, p : Proof ev x Σ ∅ ` ess p : Proof ev s s x (abs) x : N Σ ∅ ` [p : Proof ev x] ess p : [p : Proof ev x] Proof ev s s x (α) x : N Σ ∅ ` [p : Proof ev x] ess p : [xN : Proof ev x] Proof ev s s x (app)2 x : N Σ ∅ ` impi [p : Proof ev x] ess p : Proof imp (ev x, ev s s x) (abs) − Σ ∅ ` [x : N] impi [p : Proof ev x] ess p : [x : N] Proof imp (ev x, ev s s x) (α) − Σ ∅ ` [x : N] impi [p : Proof ev x] ess p : [xN : N] Proof imp (ev xN , ev s s xN ) (app)1 − Σ ∅ ` alli [x : N] impi [p : Proof ev x] ess p : Proof all [x : N] imp (ev x, ev s s x) 1. P N : Prop, QN : [xN : N] Proof P N ∅ ` alli QN : Proof all [xN : N] P N θ = [P N 7→ imp (ev xN , ev s s xN )][QN 7→ [x : N] impi [p : Proof ev x] ess p] σ N = [xN : N] Proof P N 2. P1 N : Prop, P2 N : Prop, QN : Proof P2 N xN # P1 N , xN # P2 N ` impi [xN : Proof P1 N ] QN : Proof imp (P1 N , P2 N ) θ = [P1 N 7→ ev x][P2 N 7→ ev s s x][QN 7→ ess xN ] σ N = [xN : Proof P1 N ] Proof P2 N 82 6.3 Examples 6.3.2 Simply Typed Lambda Calculus One can formalise the simply typed lambda calculus in this system using the following declarations. oType denotes the types of the object language, and there is a ground type i and function types: − ∅ ` oType : type − ∅ ` i : oType A : oType, B : oType ∅ ` fun (A, B) : oType The lambda terms of a certain type, T , will be encoded as terms of type Trm T , where Trm is a type-former: T : oType ∅ ` Trm T : type The representation of terms of the simply typed lambda calculus is given below. Note that lambda calculus variables are represented directly as atoms. A : oType, B : oType, M : Trm (fun (A, B)), N : Trm A ∅ ` app (M, N ) : Trm B A : oType, B : oType, X : Trm B x # B ` lam ([x : oType] X) : Trm fun (A, B) β-reduction is specified as a relation between terms of the same type, with the usual reduction rule: A : oType, B : oType, X : Trm B, Y : Trm A x # Y ` beta (app (lam [x : Trm A] X, Y ), [x 7→ Y ] · X) : BRed (app (lam [x : Trm A] X, Y ), [x 7→ Y ] · X) Similarly, one can define arithmetic operations as relations: X : N, Y : N, Z : N ∅ ` Sum (X, Y, Z) : type X : N ∅ ` s0 : Sum (X, 0, X) X : N, Y : N, Z : N, P : Sum (X, Y, Z) ∅ ` ss P : Sum (X, s Y, s Z) 6.3.3 Families of Data Types One can define a family of data types with the following declarations. 83 6.4 Adequacy ∅ ` Datum : type X : Datum ∅ ` El X : type ∅ ` int : Datum ∅ ` bool : Datum X : Datum ∅ ` list X : Datum X : Datum ∅ ` nil : El list X X : Datum, H : El X, T : El list X ∅ ` cons (H, T ) : El list X Here, the type-former, El, is used to build the types of the elements of a datum. One cannot define list X to be both a datum and a type, so the type-former El is introduced to transform a term into a type , thus list X is a term, and El list X is a type. 6.4 Adequacy An encoding of a system in a logical framework is adequate if it faithfully reflects the properties of the encoded system. For instance, in the case of an encoding of first-order logic, one needs to show that the terms used in the dependent type system represent first-order terms, that formulae and proofs correspond to their standardly acknowledged notions, and that only provable propositions have a proof in the system. In this section, adequacy is shown for the encoding of first-order logic given in section 6.3.1. The goal is to prove that there is a bijection between proofs in first-order logic and the corresponding terms in this system. A formal specification of first-order logic terms, formulae and proofs is given and then it is shown that these are encoded by terms of the correct type and that encoded terms represent only well-formed terms, formulae and proofs. The theorems and proofs presented here follow closely those given in [Harper et al., 1987] to prove the adequacy of LF but are much simpler due to the fact that here lambda calculus β-reduction is not involved and therefore all terms are of canonical form. 6.4.1 Terms and Formulae The following grammars define the syntax of the sets of terms (Trm) and formulae (Frm) of first-order logic. 84 6.4 Adequacy Definition 6.20 (First-order Logic Terms and Formulae) T, T0 ::= 0 | s(T) | x F, F0 ::= ⊥ | F → F0 | ∀x. F | Even(T) | T ≥ T0 Let fvars(T) and fvars(F) denote respectively the set of free variables in the term, T, and the formula, F. Extend this notation element-wise to sets of formulae. A translation function JTK is defined by induction on T, from Trm to terms in the system using the term-formers, 0 and s, for which, see section 6.3.1. Note that the free variables of T are encoded as unabstracted atoms in JTK. =0 J0K Js(T)K = s JTK =x JxK A corresponding translation function is defined over the elements of Frm. = bot J⊥K JF1 → F2 K = imp (JF1 K, JF2 K) J∀x. FK = all [x : N] JFK JEven(T)K = ev JTK JT1 ≥ T2 K = ge (JT1 K, JT2 K) It is now proved that translated terms and formulae are typeable terms of the system using the declarations given in section 6.3.1. Theorem 6.2 For any term, T ∈ Trm, such that fvars(T) = x1 , . . . , xn and freshness context, ∆, if x1 : N, . . . , xn : N Σ ∆ ` JTK : N. Similarly for any F ∈ Frm, such that fvars(F) = x1 , . . . , xn , and freshness context, ∆, x1 : N, . . . , xn : N Σ ∆ ` JFK : Prop, Proof 6.2 By structural induction on the syntax of elements of Trm and Frm. x : N Σ ∆ ` x : N Γ Σ ∆ ` 0 : N (atm) (app) Γ Σ ∆ ` JTK : N Γ Σ ∆ ` N : type (app)1 Γ Σ ∆ ` s JTK : N 85 6.4 Adequacy 1. XN : N ∅ ` s XN : N θ = [X N 7→ JTK] σ N = N In the last derivation, Γ is of the form x1 : N, . . . , xn : N where x1 , . . . , xn are the free variables of T and Γ Σ ∆ ` JTK : N holds by the inductive hypothesis. For the translation function over elements of Frm, the cases for all [x : N] JFK and imp (JF1 K, JF2 K) are given below. Γo n (x : N) Σ ∆ ` JFK : Prop .. (abs) Γ Σ ∆ ` [x : N] JFK : [x : N] Prop . (α) N Γ Σ ∆ ` [x : N] JFK : [x : N] Prop Γ Σ ∆ ` Prop : type (app)1 Γ Σ ∆ ` all [x : N] JFK : Prop 1. P N : Prop all [xN : N] P N : Prop θ = [P N 7→ JFK] σ N = [xN : N] Prop where Γ contains type associations for all the free variables of F and Γ o n (x : N) Σ ∆ ` JFK : Prop holds by the induction hypothesis. .. Γ Σ ∆ ` JF1 K : Prop Γ Σ ∆ ` JF2 K : Prop . (tpl : n) Γ Σ ∆ ` (JF1 K, JF2 K) : (Prop × Prop) Γ Σ ∆ ` Prop : type (app)1 Γ Σ ∆ ` imp (JF1 K, JF2 K) : Prop 1. P1 N : Prop, P2 N : Prop imp (P1 N , P2 N ) : Prop θ = [P1 N 7→ JF1 K][P2 N 7→ JF2 K] σ N = (Prop × Prop) where, again, Γ is the environment, x1 : N, . . . , xn : N, containing type associations for each element of fvars(F1 ) ∪ fvars(F2 ) and both Γ Σ ∆ ` JF1 K : Prop and Γ Σ ∆ ` JF2 K : Prop hold by the induction hypothesis. It is now shown that typeable encoded terms correspond exactly to well-formed firstorder logic terms and formulae. Theorem 6.3 If Γ Σ ∆ ` s : N is a derivable typing judgement and the environment, Γ, contains only type associations for unabstracted atoms of the form (a : N), then s ≡ JTK for some term T ∈ Trm. Similarly, if Γ Σ ∆ ` s : Prop is a derivable typing judgement and the environment, Γ, contains only type associations for unabstracted atoms of the form (a : N), then s ≡ JFK for some formula F ∈ Frm. 86 6.4 Adequacy Proof 6.3 By induction on typing judgement derivations. The only applicable cases are when the first step of the derivation is by one of the rules, (atm), (app) or (α). The two most interesting possibilities, where s ≡ all [x : N] t ≡ J∀x. FK and s ≡ imp (t1 , t2 ) ≡ JF1 → F2 K are shown below; t ≡ JFK, t1 ≡ JF1 K and t2 ≡ JF2 K follow by the inductive hypothesis. Γo n (x : N) Σ ∆ ` t : Prop .. (abs) . Γ Σ ∆ ` [x : N] t : [x : N] Prop (α) Γ Σ ∆ ` Prop : type Γ Σ ∆ ` [x : N] t : [xN : N] Prop (app)1 Γ Σ ∆ ` all [x : N] t : Prop 1. P N : Prop all [xN : N] P N : Prop θ = [P N 7→ t] σ N = [xN : N] Prop .. Γ Σ ∆ ` t1 : Prop Γ Σ ∆ ` t2 : Prop . (tpl : n) Γ Σ ∆ ` (t1 , t2 ) : (Prop × Prop) Γ Σ ∆ ` Prop : type (app)1 Γ Σ ∆ ` imp (t1 , t2 ) : Prop 1. P1 N : Prop, P2 N : Prop imp (P1 N , P2 N ) : Prop θ = [P1 N 7→ t1 ][P2 N 7→ t2 ] σ N = (Prop × Prop) This shows that the encoding of terms and formulae is adequate; it remains to prove the encoding of proofs in first-order logic is also adequate. 6.4.2 Proofs In order to show the adequacy of the encoding of proofs of first-order formulae, first, a natural deduction presentation is given for first-order logic, inspired by the one used in [Harper et al., 1987] to prove the adequacy of the encoding in LF. Let judgements have the form E `V P : F, indicating that there is a proof P of the formula F, using the list of hypotheses, E, and the set of free variables, V, where (fvars(E) ⊆ V). The introduction rules for implication and universal quantification are shown below. E, (vF1 : F1 ) `V P : F2 E `V →i (P \ vF1 ) : F1 → F2 E `V∪{x} P : F E `V ∀i (x. P) : ∀x. F Here, vF1 is the variable name of a proof of F1 , the notation (P \ vF1 ) denotes the proof P where vF1 is discharged, and in the rule for ∀i , the condition fvars(E) ⊆ V on judgements implies that x is not used in E. 87 6.4 Adequacy A natural deduction judgement, J, of the form E `V P : F is translated to a typing judgement of the system, JJK, as follows. ∆ JE `V P : FK = JVK o n JEK Σ JPKvars(JJK) ` JPK : Proof JFK Here, if V is {x1 , . . . , xn } then JVK = x1 : N, . . . , xn : N and JEK contains vFi : Proof JFi K for each (vFi : Fi ) in Γ. The translation function from proofs to terms, JPK, is defined inductively; two cases are given. J∀i (x. P)K = alli [x : N] JPK J→i (P \ vF1 )K = impi [vF1 : Proof JF1 K] JPK A second translation function from a proof, P, to a freshness context, parameterised ∆ by a set of variables, X , and written JPKX is also required; again two cases are given. ∆ ∆ J∀i (x. P)KX ∆ = JP KX ∆ J→i (P \ vF1 )KX = {vF1 # X | X ∈ X } ∪ JP KX One can now prove the following property relating natural deduction proofs in firstorder logic and their encoding in the system presented here. Theorem 6.4 If E `V ∆ P : F is a derivable judgement, J, of natural deduction then JVK o n JEK Σ JPKvars(JJK) ` JPK : Proof JFK is a derivable typing judgement. Proof 6.4 By induction on the syntax of the proof, P. The same two cases, where P is of the form ∀i (x. P0 ) or →i (P0 \ vF1 ) are given here as an illustration. In the second of these, the use ∆ of the rule (α) follows from the fact that JPKvars(JJK) ` vF1 # JF1 K, vF1 # JF2 K. ∆ JVK o n JEK o n (x : N) Σ JPKvars(JJK) ` JPK : Proof JFK (abs) ∆ JVK o n JEK Σ JPKvars(JJK) ` [x : N] JPK : [x : N] Proof JFK (α) ∆ JVK o n JEK Σ JPKvars(JJK) ` [x : N] JPK : [xN : N] Proof (xN x) · JFK ∆ JVK o n JEK Σ JPKvars(JJK) ` alli [x : N] JPK : Proof all [x : N] JFK 1. .. . (app)1 P N : Prop, QN : [xN : N] Proof P N ∅ ` alli QN : Proof (all [xN : N] P N ) θ = [P N 7→ (xN x) · JFK][QN 7→ [x : N] JPK] σ N = [xN : N] Proof P N 88 6.4 Adequacy ∆ JVK o n JEK o n (vF1 : Proof JF1 K) Σ JPKvars(JJK) ` JPK : Proof JF2 K (abs) ∆ JVK o n JEK Σ JPKvars(JJK) ` [vF1 : Proof JF1 K] JPK : [vF1 : Proof JF1 K] Proof JF2 K (α) ∆ JVK o n JEK Σ JPKvars(JJK) ` [vF1 : Proof JF1 K] JPK : [xN : Proof JF1 K] Proof JF2 K ∆ JVK o n JEK Σ JPKvars(JJK) ` impi [vF1 : Proof JF1 K] JPK : Proof imp (JF1 K, JF2 K) P1 N : Prop, P2 N : Prop, QN : Proof P2 N 1. xN # P1 N , xN # P2 N ` impi [xN : Proof P1 N ] QN : Proof imp (P1 N , P2 N ) θ = [P1 N 7→ JF1 K][P2 N 7→ JF2 K][QN 7→ (xN vF1 ) · JPK] σ N = [xN : Proof P1 N ] Proof P2 N Finally, in order to complete the adequacy proof, it is shown that only provable firstorder formulae are encoded by terms of type Proof t. Theorem 6.5 If Γ Σ ∆ ` s : Proof t where the environment, Γ, contains only type associations for atoms either of the form (a : N ) or (vF1 : Proof ti ) where ti ≡ JFi K is an encoding of some formula Fi , then s ≡ JPK where P is a proof by natural deduction of some formula F such that t ≡ JFK. Proof 6.5 By induction on typing judgement derivations; again, the only applicable cases are those for which the first step of the derivations is by one of the rules, (atm), (app) or (α). The possibilities where s ≡ alli [x : N] s0 ≡ J∀i (x. P0 )K and s ≡ impi [vF1 : Proof t1 ] s0 ≡ J→i (P0 \ vF1 )K are given below; s0 ≡ JP0 K follows by the inductive hypothesis in both cases. In the derivation for impi , ∆ ` vF1 # t1 , vF1 # t2 , vF1 # impi [vF1 : Proof t1 ] s0 and so vF1 cannot be a free variable of P or F. Γo n (x : N) Σ ∆ ` s0 : Proof t (abs) Γ Σ ∆ ` [x : N] s0 : [x : N] Proof t (α) Γ Σ ∆ ` [x : N] s0 : [xN : N] Proof (xN x) · t Γ Σ ∆ ` alli [x : N] s0 : Proof all [x : N] t 1. .. . (app)1 P N : Prop, QN : [xN : N] Proof P N ∅ ` alli QN : Proof (all [xN : N] P N ) θ = [P N 7→ (xN x) · t][QN 7→ [x : N] s0 ] σ N = [xN : N] Proof P N 89 .. . (app)1 6.4 Adequacy Γo n (vF1 : Proof t1 ) Σ ∆ ` s0 : Proof t2 (abs) Γ Σ ∆ ` [vF1 : Proof t1 ] s0 : [vF1 : Proof t1 ] Proof t2 (α) Γ Σ ∆ ` [vF1 : Proof t1 ] s0 : [xN : Proof t1 ] Proof t2 Γ Σ ∆ ` impi [vF1 : Proof t1 ] s0 : Proof imp (t1 , t2 ) 1. .. . (app)1 P1 N : Prop, P2 N : Prop, QN : Proof P2 N xN # P1 N , xN # P2 N ` impi [xN : Proof P1 N ] QN : Proof imp (P1 N , P2 N ) θ = [P1 N 7→ t1 ][P2 N 7→ t2 ][QN 7→ (xN vF1 ) · s0 ] σ N = [xN : Proof P1 N ] Proof P2 N Conclusions This chapter has presented a dependent type system for nominal terms with atom substitutions. Definitions of constraint and matching problems over this syntax have been given together with algorithms for solving such problems. These algorithms have been implemented. A set of axioms and rules was then defined for determining the typeability of pseudo-terms and pseudo-types in this system in the presence of user-defined declarations for term-formers and type-formers. An extended example for first-order logic was presented and its adequacy proven. There remains much further work to be done for this system. The meta-theory is not fully developed; properties such as the unicity of types, meta-level equivariance of valid sets of declarations, type weakening for disjoint associations and the validity of environments for derivable typing judgements remain to be fully explored. The type system itself has not been implemented. In its present form, the inclusion of the rule, (α)τ , means that the inference of derivations is not completely syntax-directed. This property, that derivable typing judgements hold for alpha-equivalent classes of types, may be derivable and if so should help in the development of a type inference algorithm for the system. It is also possible that the type system could also benefit from the two-stage approach to derivability considered in chapter 4 in place of the typS predicate used in the rule, (var)τ . The addition of rewriting to the system is also of interest and may be aided by a restriction on the types of the arguments of applications and constructed types, as used in section 5.2. 90 Chapter 7 Implementation he author has implemented all of the type systems defined in chapters 2 to 5, as well T many other algorithms for nominal terms, nominal equational theories and nominal terms with atom substitutions as described in chapter 6. The programming language used was Haskell, [Peyton Jones, 2003], and all code was compiled using version 7.6.3 of the Glasgow Haskell Compiler, [GHC Team, The, 2013]. A browser-based user interface, accessing CGI binaries, is provided to allow the use of a number of tools related to typing, constraint problem solving and rewriting. The user may input queries using an ASCII-based concrete syntax similar to that given in the definitions above. 7.1 Preliminaries A slightly simplified listing of the translation of the main syntactic elements given in section 2.4 to Haskell is given below. import q u a l i f i e d Data . Map a s M import q u a l i f i e d Data . S e t a s S newtype Atm = Atm String newtype Var = Var String type Cxt = S . S e t (Atm, Var ) type Frm = String type Prm = [ ( Atm, Atm ) ] data Trm = AtmTrm Atm | AbsTrm Atm Trm | AppTrm Frm Trm 91 7.2 Simple Types for Nominal Terms | TplTrm [ Trm ] | VarTrm Prm Var type VSub = M. Map Var Trm Listing 7.1: Nominal Terms Functions and other accompanying data structures for the following concepts from chapter 2 were programmed. • permutation actions • freshness • alpha-equivalence • constraint problems • variable substitution action • nominal matching • nominal unification • closedness • closed nominal rewriting In addition to those features mentioned above, prototype implementations were written of algorithms for the determination of critical pairs in sets of rewrite rules, [Fernández and Gabbay, 2007b], and the ordering of nominal terms and completion of sets of axioms, [Fernández and Rubio, 2012]. 7.2 Simple Types for Nominal Terms Type checking and type inference algorithms were implemented for each of the three systems of simple types defined in chapter 3. Derivable typing judgements may be output as proof trees, formatted in LATEX. The solving of constraint problems over typeable terms in these systems was also implemented, together with the prerequisite functionality. 7.3 Polymorphic Types for Nominal Terms The following simplified code listing shows the translation of the grammar for types and declarations given in definition 4.1 of chapter 4. 92 7.4 Typed Nominal Equational Theories type TFrm = String type TVar = String data Typ = | | | PrdTyp CnsTyp AbsTyp VarTyp [ Typ ] TFrm Typ Typ Typ TVar newtype Dec = Dec ( Typ , Typ ) Listing 7.2: Polymorphic Types Here, as for the systems of simple types, both a type checking and a type inference algorithm were written. Derivable typing judgements may again be output in LATEXformat. Implementations of matching and unification of types were a necessary subcomponent. 7.4 Typed Nominal Equational Theories From chapter 5, predicate functions were written to check the conditions for typeable rewrite rules, given in definition 5.1 and for typeable-closed rewrite rules, in definition 5.4. All the sets of rewrite rules and axioms given as examples in chapter 5 have been machinechecked using these functions. The implementation of the check for typeable-closed rewrite rules together that for closed nominal rewriting, section 7.1, gives an implementation of typeable-closed nominal rewriting. However, equivariant nominal matching was not implemented and hence a system of typeable rewriting not completed. 7.5 Dependent Types for Nominal Terms with Atom Substitutions From chapter 6 only the syntax of nominal terms with atom substitutions was developed. The following code listing is a simplified version of the translation of the grammar for pseudo-terms given in definition 6.1. import q u a l i f i e d Data . Map a s M import q u a l i f i e d Data . S e t a s S newtype Atm = Atm String newtype Var = Var String type Cxt = S . S e t (Atm, Var ) 93 7.5 Dependent Types for Nominal Terms with Atom Substitutions type Frm = String type Prm = [ ( Atm, Atm ) ] type ASub = M. Map data Trm = AtmTrm | AbsTrm | AppTrm | TplTrm | VarTrm Atm Trm Atm Atm Trm Frm Trm [ Trm ] ASub Prm Var type VSub = M. Map Var Trm Listing 7.3: Nominal Terms with Atom Substitutions Algorithms for the following syntactic concepts were implemented. • permutation actions • freshness • alpha-equivalence • atom substitution action • constraint problems • variable substitution action • matching • closedness The definition of a type inference algorithm for the type system of this chapter and a subsequent implementation remains a key area for future work. 94 Chapter 8 Related Work his chapter gives a summary of areas of research relevant to the topic of this thesis. T Comparisons between these differing approaches and the material studied here are drawn where appropriate. 8.1 Syntax with Binders This is an extremely large area of research, in which many approaches exist. The first solution to the problems arising from α-equivalent classes of terms was that of de Bruijn, [de Bruijn, 1972] and used in the theorem prover AUTOMATH. There, variables are nameless and are instead represented by and manipulated as integers or ‘indices’, where the value of a variable denotes that it is associated with the binding point that number of abstractions above in the syntax tree. An α-equivalence class of terms then has a unique representative. This approach is excellent from a mechanical point of view but is very far removed from the traditional, human-friendly representation of term languages. An intermediate approach is that of ‘locally nameless’ systems, such as described in [Aydemir et al., 2008]. Here, de Bruijn indices are used for bound variables and names for free variables. α-equivalence classes still have a unique representative but the use of names for free variables makes the system more intuitive to the user. This approach has been used in the implementation of many more modern theorem provers including Coq, [Chlipala, 2011; Coq Development Team, The, 2012], LEGO, [Luo and Pollack, 1992], and Isabelle, [Wenzel, 2013]. Perhaps the most popular approach is that of ‘higher-order abstract syntax’, [Pfenning and Elliott, 1988]. In such systems, binding in term languages is represented using the lambda calculus and thus, issues of α-equivalence and capture-avoiding substitution are implicitly handled at the meta-level. The undecidable properties of the lambda calculus, do cause problems such as the undecidability of unification, [Huet, 1973], for which, however, 95 8.2 Nominal Systems and Types practical solutions do exist. The Edinburgh Logical Framework, LF, [Harper et al., 1987], a dependently typed extension of the lambda calculus, using higher-order abstract syntax, has been highly developed both theoretically and practically as the Twelf proof assistant, [Pfenning and Schürmann, 1999]. Most rewriting systems for languages involving binding, such as Combinatory Reduction Systems, [Klop, 1980; Klop et al., 1993], and the later Higher-order Rewrite Systems, [Mayr and Nipkow, 1998], take an approach similar to that of higher-order abstract syntax. Another approach, is that taken by Luo, in the Pal+ logical framework, [Luo, 2003]. There, ‘let-expressions’, representing parametric families of types and type elements, form the meta-language, in place of the lambda calculus, and are used to define a logical framework for specifying type theories. The nominal approach taken in this thesis, is a named approach (indeed, the use of names is eponymous of ‘nominal’ practice), and allows the formalisation of α-equivalence classes of terms, whilst remaining intuitive and close to traditional language representations. Substitution is not implicit but can be specified simply via rewrite rules, as shown in example 5.4.4. There may exist similarities between the nominal approach and that of ‘contextual modal type theory’, [Nanevski et al., 2008] and the dependent type system of chapter 6 may benefit from a study of the handling of type environments and substitutions therein. 8.2 Nominal Systems and Types Whilst the author is unaware of any other work upon type systems for nominal terms directly, a number of systems of nominal abstract syntax, a more fundamental approach to the incorporation of the ideas of nominal sets to syntax, have been typed. In [Pitts, 2010], Pitts extends Gödel’s System T, itself, an extension of the lambda calculus with a notion of bounded recursion, by adding a nominal name restriction construction to allow the definition of local names and structural induction modulo α-equivalence over lambda terms. This is subsequently generalised to other inductive data structures. The System T component of the language is a simply typed lambda calculus with a base type representing natural numbers. With respect to the α-structural induction component, there is one type for names (atoms) and one for lambda terms. Similar to [Pitts, 2010], is [Cheney, 2009]. There, Cheney, defines a simple nominal type theory by extending the lambda calculus with nominal name abstraction and concretion operators and again then generalises to other recursive data structures modulo α-equivalence. The type system is a conservative extension of the simply typed lambda calculus together with a set of base types specifically for names and abstraction type for name abstractions. In [Cheney, 2012], Cheney builds on this approach, adding a dependent name abstraction type to an LF-style extension of the lambda calculus. 96 8.2 Nominal Systems and Types Urban describes in [Urban, 2008] how the nominal package for Isabelle, [Wenzel, 2013], a higher-order logic theorem proving environment, is typed. Within the Isabelle type system, atoms are given a single type. Permutations are typed simply as lists of pairs of that type. The action of a permutation is an overloaded operator allowing a definition to be given for each built-in ‘permutation type’. Such a type is an instance of a type class that guarantees that certain properties of elements of the type are maintained under the permutation operation. However, in later work, [Huffman and Urban, 2010], it is explained that atoms may be given arbitrary sorts (‘multi-sorted concrete atoms’) at the term level which can be enforced by the Isabelle type-checker by use of sub-typing. In, [Shinwell, 2005], Shinwell describes the implementation of a language Fresh OCaml, (successor to the FreshML of [Gabbay, 2001]), an extension of OCaml for meta-programming with binding and data structures. Expressions for generating fresh atoms, atom abstraction, swapping atoms and freshness conditions are added to the language. Atoms are given a name type but one that is polymorphic over all types. Abstractions need not necessarily abstract atoms and the abstracted expression need not be of a ‘name’ type. However this language is not fully formalised. Instead a much smaller subset, MiniFreshML, is studied in which the type system is extended with a single type for names and an abstraction type, of the type of names over types. As previously remarked, these are systems based on nominal abstract syntax rather than nominal terms. Nominal abstract syntax generally just adds the nominal semantics of abstractions to a formalism, often as in [Pitts, 2010] and [Cheney, 2009, 2012], a lambda calculus or maybe first-order logic as in [Pitts, 2003]. Nominal terms, however, are distinctly an extension of first-order terms, complete with term-formers and variables. Nominal terms must represent binders as an abstraction combined with a term-former ([Clouston, 2010], takes another approach whereby a certain set of term-formers are identified as ‘binding’) whereas in nominal abstract syntax, binding may still be achieved with other non- first-order syntactic elements of the formalism, such as the functional abstraction of the lambda calculus. From the point of view of types, in most of the systems described above, atoms are either all typed with a single type or a set of base types restricted to atoms. Abstracted atoms are also subsequently required to inhabit an ‘atom’ type. These restrictions deny to the nominal parts of the syntax any of the more complex types or features present in the type systems. In the type systems presented here, in chapters 3, 4 and 6, both unabstracted and abstracted atoms may inhabit any type. 97 8.3 Rewriting and Types 8.3 Rewriting and Types Type systems for first-order rewriting are well studied. For example, [van Bakel and Fernández, 2003] defines a system of intersection types for term rewriting systems, and [van Bakel et al., 1999], a system of second rank polymorphic intersection types. The rewriting logic language, Maude, [Clavel et al., 2002, 2011] has a developed type system, providing sorts, sub-sorts, operator overloading and more. Type systems for formalisms of higher-order rewriting are less well developed. Higherorder Rewrite Systems, [Mayr and Nipkow, 1998], use a typed lambda calculus as a metalanguage together with restrictions on patterns to ensure the decidability of higher-order matching, however, rewrite rules may only be typed with base types. Richer types for function symbols in this system are considered in [Jouannaud et al., 2005]. In ‘plain’ higher-order rewrite systems, the lambda calculus is extended with first-order rewriting together with first-order matching. A mixed approach of standard and plain higher-order rewriting is proposed in [Jouannaud, 2005], using a system of polymorphic types. This makes the study of translations between systems such as combinatory reduction systems and nominal rewriting, both past, [Fernández and Gabbay, 2007b] and ongoing by Domı́nguez-Álvarez and Fernández, interesting, as translated systems will be able to benefit from features developed for nominal rewriting such as the polymorphic type system of chapter 4 and the completion algorithm of [Fernández and Rubio, 2012]. 98 Chapter 9 Conclusions and Future Work his thesis has surveyed three styles of type system for nominal terms; Church-style T simple types, ML-style polymorphic types in the style of Curry and dependent types for nominal terms with atom substitutions. The simple type systems, whilst maybe not of sufficient power to be as useful as the later systems, are instructive from a theoretical point of view. In particular, comparison of these systems with the Curry-style polymorphic system, aided in the understanding of the interaction between typing and the atom-capturing nature of variable substitution. Of these systems that for polymorphic types is the most developed and has been successfully applied to the area of equational reasoning. Formalisations of typed nominal rewriting and typed nominal algebra have been described and used to model a range of interesting formal systems. The dependent type system first required the theory of nominal terms to be extended to include a notion of atom substitution. It was then shown to be capable of modelling a number of formal systems and its adequacy proven for a formalisation of first-order logic. However this system remains in its infancy and has much scope for further study both in terms of theoretical advances and practical implementation. Future Work There exist many possibilities for future work arising directly from the material studied here. The simple type systems of chapter 3 could be applied to systems of equational reasoning as was done for the polymorphic type system in chapter 5. A more detailed study of the expressive power of equational theories using these type systems would be of interest. 99 It was conjectured, in chapter 5 that a hybrid system whereby rewrites from typed and typed closed nominal rewriting could be used together, to allow rewrite theories containing both typeable and typeable-closed rules to be used. A rewrite using a more permissive, typeable rule would require a typed nominal rewrite step using typed nominal pattern matching and equivariant nominal matching whereas one using a typeable-closed rule would only need a typed closed nominal rewrite step using the more efficient concept of closed nominal rewriting. Such a system could also be developed for nominal algebra. Such type systems of equational reasoning, once combined with the completion algorithm of [Fernández and Rubio, 2012] for orienting sets of axioms to form sets of rewrite rules would be of use in the operational side of a nominal certified programming environment. It is also possible that less restrictive forms of typed closed nominal rewriting could be developed by changing the way term-formers are typed, possibly using ideas from research into Generalized Algebraic Data Types, [Peyton Jones et al., 2006], as previously asserted. This could also form part of research into a method of deriving the type declarations of term-formers from rewrite rules or axioms. The dependent type system of chapter 6 is the area with the greatest scope for future work. The study of nominal terms with atom substitution could be developed in its own right, exploring in depth the complexity of solving constraint, matching and unification problems over this extended syntax. As previously stated there remains much work to be done as regards the meta-theory of this type system. Most importantly, proof of the derivability of the typing rule, (α), would allow the definition and implementation of a syntax-directed inference algorithm for the type system. This would constitute a rudimentary automated proof environment. This type system is also yet to be studied in the context of equational theories. More generally, the work on types for nominal terms presented here could also be developed as a nominal package or extension to one or more of the existing existing specification languages such as Twelf, [Pfenning and Schürmann, 1999], or Maude, [Clavel et al., 2002]. 100 References Sandra Alves, Maribel Fernández, Mário Florido, and Ian Mackie. Linearity and Recursion in a Typed Lambda-calculus. In Proceedings of the 13th ACM SIGPLAN International Symposium on Principles and Practice of Declarative Programming, 2011. 59, 60 Brian Aydemir, Arthur Charguéraud, Benjamin C. Pierce, Randy Pollack, and Stephanie Weirich. Engineering Formal Metatheory. In Proceedings of the 35th ACM SIGPLANSIGACT Symposium on Principles of Programming Languages, 2008. 95 Franz Baader and Tobias Nipkow. Term Rewriting and All That. Cambridge University Press, 1998. 8 Franz Baader and Wayne Snyder. Unification Theory. In Handbook of Automated Reasoning. Elsevier, 2001. 34, 68 Henk P. Barendregt. Pairing without Conventional Constraints. Zeitschrift für mathematischen Logik und Grundlagen der Mathematik, 20, 1974. 52 Henk P. Barendregt. The Lambda Calculus: its Syntax and Semantics. North-Holland, 1984. 11 Henk P. Barendregt. Introduction to Generalized Type Systems. Journal of Functional Programming, 1, 1991. 5 Henk P. Barendregt. Lambda Calculi With Types. In Handbook of Logic in Computer Science. Oxford University Press, 2000. 21, 32 Henk P. Barendregt, Will Dekkers, and Richard Statman. Lambda Calculus With Types. Cambridge University Press, 2013. 6, 21 Christophe Calvès and Maribel Fernández. Matching and Alpha-equivalence Check for Nominal Terms. Journal of Computer System Sciences, 76, 2010. 1, 9, 16, 51 James Cheney. The Complexity of Equivariant Unification. In Proceedings of the 31st International Colloquium on Automata, Languages and Programming, 2004. 17, 51 101 REFERENCES James Cheney. A Simple Nominal Type Theory. Electronic Notes in Theoretical Computer Science, 228, 2009. 96, 97 James Cheney. A Dependent Nominal Type Theory. Logical Methods in Computer Science, 8, 2012. 96, 97 James Cheney and Christian Urban. System Description: Alpha-Prolog, a Fresh Approach to Logic Programming Modulo Alpha-equivalence. In Proceedings of the 17th International Workshop on Unification, 2003. 1 Adam Chlipala. Certified Programming with Dependent Types. MIT Press, 2011. 6, 95 Alonzo Church. A Set of Postulates for the Foundation of Logic. Annals of Mathematics, 33, 1932. 3 Alonzo Church. A Set of Postulates for the Foundation of Logic (Second Paper). Annals of Mathematics, 34, 1933. 3 Alonzo Church. An Unsolvable Problem of Elementary Number Theory. American Journal of Mathematics, 58, 1936. 3 Alonzo Church. A Formulation of the Simple Theory of Types. Journal of Symbolic Logic, 5, 1940. 5, 6, 21, 23 Manuel Clavel, Francisco Durán, Steven Eker, Patrick Lincoln, Narciso Martı́-Oliet, José Meseguer, and Jose F. Quesada. Maude: Specification and Programming in Rewriting Logic. Theoretical Computer Science, 285, 2002. 98, 100 Manuel Clavel, Francisco Durán, Steven Eker, Patrick Lincoln, Narciso Martı́-Oliet, José Meseguer, and Carolyn Talcott. Maude Manual, 2011. 98 Ranald A. Clouston. Closed Terms. Unpublished, 2007. 18 Ranald A. Clouston. Equational Logic for Names and Binding. PhD thesis, University of Cambridge, 2010. 9, 17, 97 Coq Development Team, The. The Coq Proof Assistant: Reference Manual, 2012. 95 Thierry Coquand and Gerard Huet. The Calculus of Constructions. Information and Computation, 76, 1988. 5 Haskell B. Curry and Robert Feys. Combinatory Logic. North-Holland, 1958. 6, 32 Haskell. B. Curry, J. Roger Hindley, and Jonathan P. Seldin. Combinatory Logic, Volume II. North-Holland, 1972. 4 102 REFERENCES Luis Damas and Robin Milner. Principal Type-schemes for Functional Programs. In Proceedings of the 9th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 1982. 32, 35, 39, 42 Nicolaas G. de Bruijn. Lambda Calculus Notation With Nameless Dummies, A Tool For Automatic Formula Manipulation, With Application To The Church-Rosser Theorem. Indagationes Mathematicae, 5, 1972. 95 Elliot Fairweather, Maribel Fernández, and Murdoch J. Gabbay. Principal Types for Nominal Theories. In Proceedings of the 18th International Symposium on Fundamentals of Computation Theory, 2011. 2, 62 Maribel Fernández and Murdoch J. Gabbay. Nominal Rewriting with Name Generation: Abstraction vs. Locality. In Proceedings of the 7th ACM SIGPLAN International Symposium on Principles and Practice of Declarative Programming, 2005. 15 Maribel Fernández and Murdoch J. Gabbay. Curry-style Types for Nominal Terms. In Types for Proofs and Programs. Springer, 2007a. 62 Maribel Fernández and Murdoch J. Gabbay. Nominal Rewriting. Information and Computation, 205, 2007b. 1, 9, 15, 17, 50, 68, 92, 98 Maribel Fernández and Murdoch J. Gabbay. Closed Nominal Rewriting and Efficiently Computable Nominal Algebra Equality. In Proceedings of the 5th International Workshop on Logical Frameworks and Meta-languages, 2010. 17, 19, 54, 56, 69 Maribel Fernández and Albert Rubio. Nominal Completion for Rewrite Systems with Binders. In Proceedings of the 39th International Colloquium on Automata, Languages, and Programming, 2012. 92, 98, 100 Maribel Fernández, Murdoch J. Gabbay, and Ian Mackie. Nominal Rewriting Systems. In Proceedings of the 6th ACM SIGPLAN International Symposium on Principles and Practice of Declarative Programming, 2004. 10, 13, 18, 50 Murdoch J. Gabbay. A Theory of Inductive Definitions with Alpha-equivalence. PhD thesis, University of Cambridge, 2001. 4, 9, 97 Murdoch J. Gabbay. Foundations of Nominal Techniques: Logic and Semantics of Variables in Abstract Syntax. Bulletin of Symbolic Logic, 17, 2011a. 1 Murdoch J. Gabbay. Nominal Terms and Nominal Logics: From Foundations to Metamathematics. In Handbook of Philosphical Logic. Kluwer, 2011b. 10, 18 Murdoch J. Gabbay and Aad Mathijssen. Nominal Universal Algebra: Equational Logic with Names and Binding. Journal of Logic and Computation, 19, 2009. 9, 17 103 REFERENCES Murdoch J. Gabbay and Aad Mathijssen. A Nominal Axiomatisation of the Lambdacalculus. Journal of Logic and Computation, 20, 2010. 55, 56 Murdoch J. Gabbay and Andrew M. Pitts. A New Approach to Abstract Syntax Involving Binders. In Proceedings of the 14th Annual Symposium on Logic in Computer Science, 1999. 1, 4, 9 GHC Team, The. The Glorious Glasgow Haskell Compilation System User’s Guide, 2013. 91 Jean-Yves Girard, Paul Taylor, and Yves Lafont. Proofs and Types. Cambridge University Press, 1989. 5 Robert Harper, Furio Honsell, and Gordon Plotkin. A Framework for Defining Logics. In Proceedings of the 2nd IEEE Symposium on Logic in Computer Science. IEEE Computer Society Press, 1987. 5, 63, 84, 87, 96 William A. Howard. The Formulae-as-types Notion of Construction. In To H. B. Curry: Essays on Combinatory Logic, Lambda Calculus, and Formalism. Academic Press, 1980. 5, 63 Gérard P. Huet. The Undecidability of Unification in Third Order Logic. Information and Control, 22, 1973. 95 Brian Huffman and Christian Urban. Proof Pearl: A New Foundation for Nominal Isabelle. In Proceedings of the 1st Conference on Interactive Theorem Proving, 2010. 97 Jean-Pierre Jouannaud. Higher-Order Rewriting: Framework, Confluence and Termination. In Processes, Terms and Cycles: Steps on the Road to Infinity. Springer-Verlag, 2005. 98 Jean-Pierre Jouannaud, Femke van Raamsdonk, and Albert Rubio. Higher-Order Rewriting with Types and Arities. Unpublished, 2005. 98 Stephen C. Kleene and J. B. Rosser. The Inconsistency of Certain Formal Logics. Annals of Mathematics, 36, 1935. 3 Jan-Willem Klop. Combinatory Reduction Systems. Mathematischen Centrum, 1980. 96 Jan-Willem Klop, Vincent van Oostrom, and Femke van Raamsdonk. Combinatory Reduction Systems: Introduction and Survey. Theoretical Computer Science, 121, 1993. 8, 96 Zhaohui Luo. PAL+ : A Lambda-free Logical Framework. Journal of Functional Programming, 13, 2003. 96 104 REFERENCES Zhaohui Luo and Randy Pollack. LEGO Proof Development System: User’s Manual. Technical report, University of Edinburgh, 1992. 95 Richard Mayr and Tobias Nipkow. Higher-order Rewrite Systems and their Confluence. Theoretical Computer Science, 192, 1998. 96, 98 José Meseguer. Twenty Years of Rewriting Logic. Journal of Logic and Algebraic Programming, 81, 2012. 8 Aleksandar Nanevski, Frank Pfenning, and Brigitte Pientka. Contextual Modal Type Theory. ACM Transactions on Computational Logic, 9, 2008. 96 Simon L. Peyton Jones, editor. Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press, 2003. 91 Simon L. Peyton Jones, Dimitrios Vytiniotis, Stephanie Weirich, and Geoffrey Washburn. Simple Unification-based Type Inference for GADTs. ACM SIGPLAN Notices, 41, 2006. 53, 100 Frank Pfenning and Conal Elliott. Higher-order Abstract Syntax. In Proceedings of the ACM SIGPLAN 1988 Conference on Programming Language Design and Implementation, 1988. 95 Frank Pfenning and Carsten Schürmann. System description: Twelf — A Meta-logical Framework for Deductive Systems. In Proceedings of the 16th International Conference on Automated Deduction, 1999. 72, 96, 100 Benjamin C. Pierce. Types and Programming Languages. MIT Press, 2002. 1, 5 Benjamin C. Pierce. Advanced Topics in Types and Programming Languages. MIT Press, 2004. 5 Andrew M. Pitts. Nominal Logic, A First Order Theory of Names and Binding. Information and Computation, 186, 2003. 97 Andrew M. Pitts. Alpha-structural Recursion and Induction. Journal of the ACM, 53, 2006. 1, 4 Andrew M. Pitts. Nominal System T. In Proceedings of the 37th ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages, 2010. 96, 97 John C. Reynolds. Towards a Theory of Type Structure. In Programming Symposium, 1974. 5 Bertrand Russell and Alfred North Whitehead. Principia Mathematica. Cambidge University Press, 1910, 1912, 1913. 5 105 REFERENCES Mark Shinwell. The Fresh Approach: Functional Programming With Names and Binders. Technical report, University of Cambridge, 2005. 1, 97 Christian Urban. Nominal Techniques in Isabelle/HOL. Journal of Automated Reasoning, 40, 2008. 97 Christian Urban, Andrew M. Pitts, and Murdoch J. Gabbay. Nominal Unification. Theoretical Computer Science, 323, 2004. 1, 9, 10, 15, 16, 41, 71 Steffen van Bakel and Maribel Fernández. Normalization, Approximation and Semantics for Combinator Systems. Theoretical Computer Science, 290, 2003. 98 Steffen van Bakel, Franco Barbanera, and Maribel Fernández. Polymorphic Intersection Type Assignment for Rewrite Systems with Abstractions and β-rule. In Types for Proofs and Programs. Springer, 1999. 98 Makarius Wenzel. The Isabelle Reference Manual, 2013. 95, 97 106