In programming, some data acts as a resource (e.g., file handles, channels) subject to usage constraints. This poses a challenge to software correctness as most languages are agnostic to constraints on data. The approach of linear types provides a partial remedy, delineating data into resources to be used but never copied or discarded, and unconstrained values. Bounded Linear Logic provides a more fine-grained approach, quantifying non-linear use via an indexed-family of modalities. Recent work on coeffect types generalises this idea to graded comonads, providing type systems which can capture various program properties. Here, we propose the umbrella notion of graded modal types, encompassing coeffect types and dual notions of type-based effect reasoning via graded monads. In combination with linear and indexed types, we show that graded modal types provide an expressive type theory for quantitative program reasoning, advancing the reach of type systems to capture and verify a broader set of program properties. We demonstrate this approach via a type system embodied in a fully-fledged functional language called Granule, exploring various examples.Quantitative Program Reasoning with Graded Modal Types 110:3[2016]. Our graded modalities can thus be considered to be computationally trivial, i.e., not requiring additional underlying semantics, though we provide a graded possibility encapsulating I/O effects.At the moment, Granule is not designed as a general-use surface-level language. Rather, our aim is to demonstrate the reasoning power provided by combining linear, graded, and indexed types in the context of standard language features like data types, pattern matching, and recursion.
A TASTE OF GRANULEWe begin with various example programs in Granule, building from the established concept of linear types up to the graded modalities of this paper. We show how linear and graded modal types allow us to document, discover, and enforce program properties, complementing and extending the reasoning provided by parametric polymorphism and indexed types.Granule syntactically resembles Haskell. Programs comprise mutually recursive definitions, with functions given by sequences of equations, using pattern matching to distinguish their cases. Toplevel definitions must have a type signature (inference and principal types is further work, ğ10). The T E X source of this section is a literate Granule file; everything here is real code. Ill-typed definitions are marked by ✗. We invite the reader to run the type checker and interpreter themselves. 1
LinearityTo ease into the syntax, the following are two well-typed polymorphic functions in Granule:Polymorphic type variables are explicit, given with their kind. These functions are both linear: they use their inputs exactly once. The id function is the linear function par excellence and flip switches around the order in which a function takes its arguments. From flip we can deduce that the structural exchange rule is allowed. However, the other two structural rules, weakening and con...