Integrating Linear and Dependent Types
Total Page:16
File Type:pdf, Size:1020Kb
rtifact Comple * A t * te n * te A is W s E * e n l l C o L D C o P * * c u e m s O E u e e P n R t v e o d t * y * s E a a l d u e a t Integrating Dependent and Linear Types Neelakantan R. Krishnaswami Pierre Pradic Nick Benton University of Birmingham ENS Lyon Microsoft Research <[email protected]> <[email protected]> <[email protected]> Abstract ative programs, and type dependency permits giving very In this paper, we show how to integrate linear types with precise types to describe the precise functional behavior of type dependency, by extending the linear/non-linear calcu- those programs. The primary difficulty we face is precisely lus of Benton to support type dependency. the interaction between type dependency and the structural Next, we give an application of this calculus by giving a rules. Since linear types ensure a variable occurs only once in proof-theoretic account of imperative programming, which a program, and dependent types permit variables to occur in requires extending the calculus with computationally irrele- both types and terms, how should we count occurrences of vant quantification, proof irrelevance, and a monad of com- variables in types? putations. We show the soundness of our theory by giving The key observation underpinning our approach is that a realizability model in the style of Nuprl, which permits us we do not have to answer this question! We build on the work of to validate not only the β-laws for each type, but also the Benton [7], which formulated models of intuitionistic linear F a G η-laws. logic in terms of a monoidal adjunction between a These extensions permit us to decompose Hoare triples monoidal closed category L and a cartesian closed category A into a collection of simpler type-theoretic connectives, yield- C. The exponential ! then decomposes as the composition G(F(A)) ing a rich equational theory for dependently-typed higher- of the . Syntactically, such a model then corresponds order imperative programs. Furthermore, both the type the- to a pair of lambda calculi, one intuitionistic, and one linear, F G ory and its model are relatively simple, even when all of the which are related by a pair of modal operators and . extensions are considered. In the linear/non-linear (LNL) calculus, we no longer have to view the intuitionistic function space P ! Q through Keywords Linear types, dependent types, intersection types, the lens of the Girard encoding !P ( Q. Instead, the intu- proof irrelevance, separation logic, Hoare triples itionistic arrow, !, is a connective in its own right, inde- pendent of the linear function space, (. Given such a sep- 1. Introduction aration, we can make the intuitionistic part of the language dependent, changing the type X ! Y to the pi-type Πx : X. Y, Two of the most influential research directions in language without altering the linear function space A ( B. We then design are substructural logic and dependent type theory. end up with a type theory in which linear types may depend Substructural logics like linear logic [18] and separation on intuitionistic terms in quite sophisticated ways, but we logic [38] offer fine-grained control over the structural rules never have to form a type dependent on an open linear term. of logic (i.e., contraction and weakening), which means types As an illustration, recall Girard’s [19] classic motivating control not only how a value of a given type is used, but also example of the resource reading of linear logic in which the how often it is used. This permits giving a logical account proposition A represents spending a dollar, B represents ob- resource of the notion of , and has been tremendously useful taining a pack of Camels, and C obtaining a pack of Marl- in giving modular and accurate specifications to stateful and boro. The formula A ( B&C then expresses that one may imperative programs. spend a dollar and receive the option of a pack of Camels On the other hand, dependent type theory extends simple and of a pack of Marlboro, but only of these options may be type theory by permitting terms to occur in types, permitting exercised. In LNLD, from that assumption one can type, for the expression of concepts like equality in types. This enables example, a machine M that takes n dollars and produces its giving types which give a very fine-grained specification of choice of something you can buy: the functional behaviour of their inhabitants. 0 0 We would like to fully integrate dependent and linear M : Πn : N. tuple n A ( Fm : N, m : N, p : m + m = n. type theory. Linear type theory would permit us to extend tuple m B ⊗ tuple m0 C the Curry-Howard correspondence to (for example) imper- where tuple n A is the (definable) n-fold linear tensor A⊗· · ·⊗ A. The return type of the linear function space in the type Permission to make digital or hard copies of all or part of this work for of M is a linear dependent pair, which generalizes LNL’s F personal or classroom use is granted without fee provided that copies are not modality for mapping intuitionistic values into linear ones. made or distributed for profit or commercial advantage and that copies bear So M yields two integers m and m0, a proof that their sum is this notice and the full citation on the first page. Copyrights for components 0 of this work owned by others than the author(s) must be honored. Abstracting n and an m-tuple of Bs together with an m -tuple of Cs. with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request Contributions. In this paper, we make the following contri- permissions from [email protected]. butions: POPL ’15, January 15–17, 2015, Mumbai, India. Copyright is held by the owner/author(s). Publication rights licensed to ACM. • First, we describe a core type theory LNLD that smoothly ACM 978-1-4503-3300-9/15/01. $15.00. integrates both linearity and full type dependency, by ex- http://dx.doi.org/10.1145/2676726.2676969 Γ ok Γ ` Ui : Ui+1 Γ ` Li : Ui+1 Γ ok Γ ` X type Γ ` X : Ui Γ, x : X ` Y : Ui Γ ` X : Ui Γ, x : X ` Y : Ui · ok Γ, x : X ok Γ ` Πx : X. Y : Ui Γ ` Σx : X. Y : Ui Γ ` ∆ ok Γ ` A : Li Γ ` 1 : Ui Γ ` G A : Ui Γ ` ∆ ok Γ ` A linear Γ ` X : U Γ ` e : XΓ ` e0 : X Γ ` · ok Γ ` ∆ a : A ok i , 0 Γ ` e =X e : Ui Γ ` X type Γ ` A linear Γ ` A : Li Γ ` B : Li Γ ` A : Li Γ ` B : Li Γ ` I : Li Γ ` A ⊗ B : Li Γ ` A ( B : Li Γ ` X : Ui Γ ` A : Li Γ ` X type Γ ` A linear Γ ` X : Ui Γ, x : X ` A : Li Γ ` X : Ui Γ, x : X ` A : Li Γ ` Πx : X. A : Li Γ ` Fx : X. A : Li Γ ` X ≡ Y type Γ ` A ≡ B linear Γ ` A : Li Γ ` B : Li Γ ` X ≡ Y : Ui Γ ` A ≡ B : Li Γ ` > : Li Γ ` A & B : Li Γ ` X ≡ Y type Γ ` A ≡ B linear Figure 2. Type Well-formedness Figure 1. Structural judgements Γ ` e : YΓ ` X ≡ Y type tending the linear/non-linear calculus [7, 8]. Our type 0 Γ, x : X, Γ ` x : X Γ ` e : X Γ ` () : 1 theory is an extensional type theory, which means that we consider the full βη-theory of the language, and further- Γ ` e : XΓ ` e0 :[e=x]Y Γ ` e : Σx : X. Y 0 more, it also has a universe hierarchy, meaning that we Γ ` (e, e ) : Σx : X. Y Γ ` π1 e : X support “full-spectrum” dependent types including large eliminations (i.e., computing types from terms). Γ ` e : Σx : X. Y Γ ` π e :[π e=x]Y Our approach to linear dependent type theory gives a 2 1 simple proof-theoretic explanation of how the ad-hoc Γ ` Πx : X. Y type Γ, x : X ` e : Y restrictions on the interaction of linearity in earlier ap- Γ ` λx. e : Πx : X. Y proaches (such as Linear LF [10]) arise, by showing how Γ ` e : Πx : X. YΓ ` e0 : X they can be modelled in LNLD. Γ ` e e0 :[e0=x]Y • Second, we show how to turn this calculus into a lan- guage for dependently-typed imperative programming Γ ` e ≡ e0 : X Γ; · ` e : A 0 (in the style of separation logic). The general idea is to Γ ` refl : e =X e Γ ` G e : G A combine linearity and type dependency to turn separation- logic style specifications into types, and to take impera- Figure 3. Intuitionistic Typing tive programs to be inhabitants of those types. Ensuring that programs have the appropriate operational behavior requires a number of extensions to the type sys- The basic structural judgements are given in Figure 1. The tem, including pointer types, implicit quantification (in judgement Γ ok is the judgement that a context Γ of intuition- the style of intersection and union types), and proof irrel- istic hypotheses is well-formed. As is standard, a well-formed evance for linear types. context is either empty, or a context Γ 0, x : X where Γ 0 is well-formed and X is an intuitionistic type well-formed in • Once this is done, we not only have a very expressive type Γ.