Fully Grounding Inference and Scoped Effects
Author:
Keywords:
C14/20/079#55685055
Abstract:
Formal methods allow for the modelling of systems to ensure that a system behaves as expected. Type systems (or when accompanied with a reduction: calculi) are the particular formal method at the center of this thesis. In particular, in this thesis we utilize calculi to formally reason about two areas. Part I: Type Inference & Elaboration In the first part we utilize calculi to model type inference and elaboration for the Hindley-Damas-Milner (HDM) system, one of the first and most well known type systems of its kind. Type inference algorithms for the HDM system, most notably algorithm W, has received a lot of attention, and their soundness and completeness have been well-studied and proven both manually (on paper) and mechanically (in a proof assistant). However, elaboration algorithms (i.e. type checking algorithms that, in addition to verifying if the input program is well-formed, output a translation of the input program into an often explicitly typed output language such as System F) have not received the same amount of attention, and their implementation in compilers such as GHC is often much more ad-hoc. One key difference between these two algorithms is the treatment of underconstrained types. For type checking, unification variables that do not affect the overall type need not be instantiated. However, for elaboration, instantiating all unification variables is essential, because unification variables are internal to the algorithm and should not leak into the output. In Part I we put elaboration algorithms on a formal footing by presenting fully formalized elaboration algorithm for the HDM system. We do so in two steps. First, we formalize a fully grounding type checking algorithm, and present its (mechanically verified) correctness proof. The novel concept of fully grounding encompasses the notion that at any point any unification variable can be instantiated, which is essential for a type checking algorithm to be extended with elaboration. Second, we perform exactly this extension by adding elaboration to the inference algorithm to obtain an elaboration algorithm. We extend the correctness theorems, which requires an equivalence relation on elaborated terms, which we define by using logical relations. Both algorithms implement input and output contexts, as well as the novel concept of full contexts, which significantly simplifies the state-passing of traditional algorithms. Part II: Scoped Effects In the second part, we utilize calculi to reason about effects. Algebraic effects & handlers have become a standard approach for side-effects in functional programming. Their modular composition between different effects and clean separation of syntax and semantics make them attractive to a wide audience. However, not all effects can be classified as algebraic; some effects need a more sophisticated handling. In particular, effects that have or create a delimited scope need special care, as their continuation consists of two parts—in and out of the scope—and their modular composition introduces additional complexity. These effects are called scoped and have gained attention by their growing applicability and adoption in popular libraries. While calculi have been designed with algebraic effects & handlers built in to facilitate their use, a calculus that supports scoped effects & handlers in a similar manner does not yet exist. Here we fill this gap: we present λsc, a calculus with native support for both algebraic and scoped effects & handlers. The most novel part of λsc is the support for modular composition of different scoped effects & handlers by extending handlers with forwarding clauses, which make λsc much more expressive than existing calculi with algebraic effects & handlers. Our calculus is based on Eff, an existing calculus for algebraic effects, extended with Koka-style row polymorphism, and consists of a formal grammar, operational semantics, a (type-safe) type-and-effect system. We give a prototype implementation of λsc with type inference and demonstrate λsc on a range of examples.