Higher-Order Type-Level Programming in Haskell

Total Page:16

File Type:pdf, Size:1020Kb

Higher-Order Type-Level Programming in Haskell Imperial College London Department of Computing Higher-order Type-level Programming in Haskell Supervisor Prof. Susan Eisenbach Csongor Kiss Second Marker Dr. Anthony Field June 18, 2018 1 Abstract Haskell, as implemented by the Glasgow Haskell Compiler (GHC), provides rich facilities for performing computation in the language of types, allowing programmers to ensure sophisticated static properties about their programs. The type language, however, has several limitations, restricting the range of expressible programs. Namely, the type language is first-order: no higher-order type functions are possible. This work aims to lift this restriction, enabling higher-order type-level programming. We motivate the problem by demonstrating the power of the type language, and explain the limitation. Then we describe a solution, and present an implementation in GHC. We discuss the interaction with the current type system, and present novel ways of writing type-level programs. 2 Acknowledgements I would like to thank my supervisor, Susan Eisenbach, and Tony Field for the motivating discussions and for keeping me on track. A particular thanks goes to Richard Eisenberg, who offered help and helpful insights about the topic. 3 Contents 1 Introduction5 1.1 Towards type safety...............................6 1.2 Type-level properties...............................6 1.3 Type families...................................8 1.4 Limitations....................................8 1.5 Contributions................................... 10 2 Background 11 2.1 Types and kinds................................. 11 2.2 Rich kinds..................................... 13 2.3 Equality...................................... 14 2.4 Type functions are first-order.......................... 17 2.5 Singletons..................................... 19 3 Unsaturated type families 21 3.1 A new arrow................................... 21 3.2 Type classes for type families.......................... 23 3.3 Code reuse.................................... 26 3.3.1 Subtyping................................. 26 3.3.2 Matchability polymorphism....................... 27 3.4 Type system................................... 28 3.4.1 Expressions................................ 28 3.4.2 Coercions................................. 29 3.4.3 Matchabilities............................... 30 3.4.4 Types and kinds............................. 30 3.4.5 Axioms.................................. 30 3.5 Type classes.................................... 30 3.5.1 Equality.................................. 32 Contents 4 Roles................................... 35 3.5.2 Instance resolution............................ 37 4 Implementation 41 4.1 Constraint-based type inference......................... 41 4.1.1 Constraints................................ 41 4.1.2 The solver pipeline............................ 42 Canonicalisation............................. 43 Flattening................................. 45 Canonicalising equalities......................... 45 Top-level interaction........................... 45 4.1.3 Unsaturated type families........................ 45 5 Unsaturated type families in practice 47 5.1 Generic programming.............................. 47 5.1.1 Algebraic structure............................ 48 5.1.2 Type-level traversals over the generic structure............ 50 5.2 Type-level Generic programming........................ 51 5.3 Stateful programming.............................. 54 6 Discussion 57 6.1 Dependent Haskell................................ 57 6.2 Future work.................................... 58 5 Chapter 1 Introduction Types help us write programs by making sure that programming errors are detected long before we attempt to execute them. The more sophisticated the type system, the more bad programs it rejects, and the more good programs it allows. Haskell is a purely functional programming language, which means that functions are referentially transparent: they are not allowed to mutate values or perform side-effects. While referential transparency already eliminates a large class of erroneous programs (such as those resulting from careless mutation of global variables), there are still many ways in which programs can fail. For example, consider the head function, which returns the first element of a list: -- pre: non-empty list head :: [a ] ! a head (x : xs) = x When all is well, head readily extracts the item in the first position1: > ghci> head [1 :: 10] > 1 However, if we try to interrogate the empty list, we are faced with a runtime error: > ghci> head [] > ? runtime error : non − exhaustive patterns in function head ? This is rather unfortunate, as the promise of static types was that they help us catch errors like these before we ever get to run our programs. However, not all is lost. We can write a safer version of head, which at least handles empty lists, and is explicit about when no value could be returned. 1Lines beginning with ghci> describe queries typed into the Glasgow Haskell Compiler's interactive REPL, and the following line is the corresponding result Chapter 1. Introduction 6 1.1 Towards type safety saferHead :: [a ] ! Maybe a saferHead (x : xs) = Just x saferHead [ ] = Nothing saferHead returns a value of type Maybe a. Users of the function must then pattern match on the result to discover whether the extraction succeeded (Just x) or not (Nothing). > ghci> saferHead [] > Nothing While we have at least managed to avoid runtime exceptions, this solution is still unsatis- factory. Even if we know that the input list is non-empty, we still have to pattern match on the result every time we wish to use saferHead. Ideally the compiler would just tell us that we tried to call head on the empty list, and reject the program. In vanilla Haskell98 [Peyton Jones, 2003], saferHead is the best we can do. Over the long course of Haskell's history, however, the language has been extended with a plethora of type system features that allow programmers to explain their informal knowledge (such as that the input list has to be non-empty) to the typechecker by means of a more expressive vocabulary in the type language. 1.2 Type-level properties Let us revisit the head function by using the full power of Haskell's type system, as imple- mented in the Glasgow Haskell Compiler (GHC)2. The requirement we wish to enforce is that the input list is non-empty, or in other words, that its length is at least one. For the built-in list type, the length of a list is not apparent in its type. For example, [ ] :: [Int ] and [1; 2] :: [Int ] both have the same type. In order to reason about the lengths, we need to reflect a notion of numeric values in the type system. To illustrate this consider the following inductive definition of natural numbers: data Nat = Zero j Succ Nat 2As of writing, the most recent released version is 8.4.3 Chapter 1. Introduction 7 At the value level, this introduces the data constructors Zero and Succ. The DataKinds language extension [Yorgey et al., 2012] allows the Zero and Succ data constructors also to be used at the type level as type constructors. We can then create a version of list, which we call Vector, that is indexed by the number of elements it contains. data Vector :: Nat ! ? ! ? where VNil :: Vector 'Zero a VCons :: a ! Vector n a ! Vector ('Succ n) a Here, Vector is a generalised algebraic data type (GADT) [Cheney and Hinze, 2003], which means that its constructors refine the type index. The empty constructor VNil simply represents a list whose length is 'Zero3. VCons takes a value of type a and another vector of length n, and constructs a vector of length one bigger than n. Now we can specify the the safeHead function as safeHead :: Vector ('Succ n) a ! a safeHead (VCons x xs) = x That is, it takes a vector whose length is one bigger than some natural number (i.e at least one). Since the only way to produce an index headed by 'Succ is by using VCons, calling safeHead with the empty list is a type error: > ghci> safeHead (VCons 1 (VCons 2 VNil)) > 1 > ghci> safeHead VNil > ? type error : Couldn0t match type 'Zero with ' Succ n0 ∗ Now it's impossible to try to extract the head of the empty list, as the compiler will stop us from doing so. 1.3 Type families GADTs are a powerful tool, but working with them can be awkward. For example, it's not obvious how to carry out certain operations on them that modify their lengths, such as appending two vectors together, as in order for this to be expressible, we need a way of performing computation (addition in this case) at the type level. With the TypeFamilies 3The ' (tick) symbol is used as a syntactic aid to signify that Zero is used in a type context here. Chapter 1. Introduction 8 extension [Chakravarty et al., 2005], we can express type functions (called type families) in a natural way. type family Add (a :: Nat)(b :: Nat) :: Nat where Add 'Zero b = b Add ('Succ a) b = 'Succ (Add a b) The Add type family carries out the addition of two Nats by doing recursion on the first. This allows us to express the type of append, that is: it takes two vectors and returns a new vector whose length is the sum of the two inputs. append :: Vector n a ! Vector m a ! Vector (Add n m) a append VNil v = v append (VCons a as) v = VCons a (append as v) 1.4 Limitations Type families, or type functions, enable a natural and convenient way of expressing type- level computations. However, the current implementation of type families is not ideal, in that there are some computations that can be expressed at the value level that cannot
Recommended publications
  • No-Longer-Foreign: Teaching an ML Compiler to Speak C “Natively”
    Electronic Notes in Theoretical Computer Science 59 No. 1 (2001) URL: http://www.elsevier.nl/locate/entcs/volume59.html 16 pages No-Longer-Foreign: Teaching an ML compiler to speak C “natively” Matthias Blume 1 Lucent Technologies, Bell Laboratories Abstract We present a new foreign-function interface for SML/NJ. It is based on the idea of data- level interoperability—the ability of ML programs to inspect as well as manipulate C data structures directly. The core component of this work is an encoding of the almost 2 complete C type sys- tem in ML types. The encoding makes extensive use of a “folklore” typing trick, taking advantage of ML’s polymorphism, its type constructors, its abstraction mechanisms, and even functors. A small low-level component which deals with C struct and union declarations as well as program linkage is hidden from the programmer’s eye by a simple program-generator tool that translates C declarations to corresponding ML glue code. 1 An example Suppose you are an ML programmer who wants to link a program with some C rou- tines. The following example (designed to demonstrate data-level interoperability rather than motivate the need for FFIs in the first place) there are two C functions: input reads a list of records from a file and findmin returns the record with the smallest i in a given list. The C library comes with a header file ixdb.h that describes this interface: typedef struct record *list; struct record { int i; double x; list next; }; extern list input (char *); extern list findmin (list); Our ml-nlffigen tool translates ixdb.h into an ML interface that corre- sponds nearly perfectly to the original C interface.
    [Show full text]
  • GHC Reading Guide
    GHC Reading Guide - Exploring entrances and mental models to the source code - Takenobu T. Rev. 0.01.1 WIP NOTE: - This is not an official document by the ghc development team. - Please refer to the official documents in detail. - Don't forget “semantics”. It's very important. - This is written for ghc 9.0. Contents Introduction 1. Compiler - Compilation pipeline - Each pipeline stages - Intermediate language syntax - Call graph 2. Runtime system 3. Core libraries Appendix References Introduction Introduction Official resources are here GHC source repository : The GHC Commentary (for developers) : https://gitlab.haskell.org/ghc/ghc https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary GHC Documentation (for users) : * master HEAD https://ghc.gitlab.haskell.org/ghc/doc/ * latest major release https://downloads.haskell.org/~ghc/latest/docs/html/ * version specified https://downloads.haskell.org/~ghc/9.0.1/docs/html/ The User's Guide Core Libraries GHC API Introduction The GHC = Compiler + Runtime System (RTS) + Core Libraries Haskell source (.hs) GHC compiler RuntimeSystem Core Libraries object (.o) (libHsRts.o) (GHC.Base, ...) Linker Executable binary including the RTS (* static link case) Introduction Each division is located in the GHC source tree GHC source repository : https://gitlab.haskell.org/ghc/ghc compiler/ ... compiler sources rts/ ... runtime system sources libraries/ ... core library sources ghc/ ... compiler main includes/ ... include files testsuite/ ... test suites nofib/ ... performance tests mk/ ... build system hadrian/ ... hadrian build system docs/ ... documents : : 1. Compiler 1. Compiler Compilation pipeline 1. compiler The GHC compiler Haskell language GHC compiler Assembly language (native or llvm) 1. compiler GHC compiler comprises pipeline stages GHC compiler Haskell language Parser Renamer Type checker Desugarer Core to Core Core to STG STG to Cmm Assembly language Cmm to Asm (native or llvm) 1.
    [Show full text]
  • Dynamic Extension of Typed Functional Languages
    Dynamic Extension of Typed Functional Languages Don Stewart PhD Dissertation School of Computer Science and Engineering University of New South Wales 2010 Supervisor: Assoc. Prof. Manuel M. T. Chakravarty Co-supervisor: Dr. Gabriele Keller Abstract We present a solution to the problem of dynamic extension in statically typed functional languages with type erasure. The presented solution re- tains the benefits of static checking, including type safety, aggressive op- timizations, and native code compilation of components, while allowing extensibility of programs at runtime. Our approach is based on a framework for dynamic extension in a stat- ically typed setting, combining dynamic linking, runtime type checking, first class modules and code hot swapping. We show that this framework is sufficient to allow a broad class of dynamic extension capabilities in any statically typed functional language with type erasure semantics. Uniquely, we employ the full compile-time type system to perform run- time type checking of dynamic components, and emphasize the use of na- tive code extension to ensure that the performance benefits of static typing are retained in a dynamic environment. We also develop the concept of fully dynamic software architectures, where the static core is minimal and all code is hot swappable. Benefits of the approach include hot swappable code and sophisticated application extension via embedded domain specific languages. We instantiate the concepts of the framework via a full implementation in the Haskell programming language: providing rich mechanisms for dy- namic linking, loading, hot swapping, and runtime type checking in Haskell for the first time. We demonstrate the feasibility of this architecture through a number of novel applications: an extensible text editor; a plugin-based network chat bot; a simulator for polymer chemistry; and xmonad, an ex- tensible window manager.
    [Show full text]
  • What I Wish I Knew When Learning Haskell
    What I Wish I Knew When Learning Haskell Stephen Diehl 2 Version This is the fifth major draft of this document since 2009. All versions of this text are freely available onmywebsite: 1. HTML Version ­ http://dev.stephendiehl.com/hask/index.html 2. PDF Version ­ http://dev.stephendiehl.com/hask/tutorial.pdf 3. EPUB Version ­ http://dev.stephendiehl.com/hask/tutorial.epub 4. Kindle Version ­ http://dev.stephendiehl.com/hask/tutorial.mobi Pull requests are always accepted for fixes and additional content. The only way this document will stayupto date and accurate through the kindness of readers like you and community patches and pull requests on Github. https://github.com/sdiehl/wiwinwlh Publish Date: March 3, 2020 Git Commit: 77482103ff953a8f189a050c4271919846a56612 Author This text is authored by Stephen Diehl. 1. Web: www.stephendiehl.com 2. Twitter: https://twitter.com/smdiehl 3. Github: https://github.com/sdiehl Special thanks to Erik Aker for copyediting assistance. Copyright © 2009­2020 Stephen Diehl This code included in the text is dedicated to the public domain. You can copy, modify, distribute and perform thecode, even for commercial purposes, all without asking permission. You may distribute this text in its full form freely, but may not reauthor or sublicense this work. Any reproductions of major portions of the text must include attribution. The software is provided ”as is”, without warranty of any kind, express or implied, including But not limitedtothe warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authorsor copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, Arising from, out of or in connection with the software or the use or other dealings in the software.
    [Show full text]
  • Idris: a Functional Programming Language with Dependent Types
    Programming Languages and Compiler Construction Department of Computer Science Christian-Albrechts-University of Kiel Seminar Paper Idris: A Functional Programming Language with Dependent Types Author: B.Sc. Finn Teegen Date: 20th February 2015 Advised by: M.Sc. Sandra Dylus Contents 1 Introduction1 2 Fundamentals2 2.1 Universes....................................2 2.2 Type Families..................................2 2.3 Dependent Types................................3 2.4 Curry-Howard Correspondence........................4 3 Language Overview5 3.1 Simple Types and Functions..........................5 3.2 Dependent Types and Functions.......................6 3.3 Implicit Arguments...............................7 3.4 Views......................................8 3.5 Lazy Evaluation................................8 3.6 Syntax Extensions...............................9 4 Theorem Proving 10 4.1 Propositions as Types and Terms as Proofs................. 10 4.2 Encoding Intuitionistic First-Order Logic................... 12 4.3 Totality Checking................................ 14 5 Conclusion 15 ii 1 Introduction In conventional Hindley-Milner based programming languages, such as Haskell1, there is typically a clear separation between values and types. In dependently typed languages, however, this distinction is less clear or rather non-existent. In fact, types can depend on arbitrary values. Thus, they become first-class citizens and are computable like any other value. With types being allowed to contain values, they gain the possibility to describe prop- erties of their own elements. The standard example for dependent types is the type of lists of a given length - commonly referred to as vectors - where the length is part of the type itself. When starting to encode properties of values as types, the elements of such types can be seen as proofs that the stated property is true.
    [Show full text]
  • The Glasgow Haskell Compiler User's Guide, Version 4.08
    The Glasgow Haskell Compiler User's Guide, Version 4.08 The GHC Team The Glasgow Haskell Compiler User's Guide, Version 4.08 by The GHC Team Table of Contents The Glasgow Haskell Compiler License ........................................................................................... 9 1. Introduction to GHC ....................................................................................................................10 1.1. The (batch) compilation system components.....................................................................10 1.2. What really happens when I “compile” a Haskell program? .............................................11 1.3. Meta-information: Web sites, mailing lists, etc. ................................................................11 1.4. GHC version numbering policy .........................................................................................12 1.5. Release notes for version 4.08 (July 2000) ........................................................................13 1.5.1. User-visible compiler changes...............................................................................13 1.5.2. User-visible library changes ..................................................................................14 1.5.3. Internal changes.....................................................................................................14 2. Installing from binary distributions............................................................................................16 2.1. Installing on Unix-a-likes...................................................................................................16
    [Show full text]
  • The Glorious Glasgow Haskell Compilation System User's Guide
    The Glorious Glasgow Haskell Compilation System User’s Guide, Version 7.8.2 i The Glorious Glasgow Haskell Compilation System User’s Guide, Version 7.8.2 The Glorious Glasgow Haskell Compilation System User’s Guide, Version 7.8.2 ii COLLABORATORS TITLE : The Glorious Glasgow Haskell Compilation System User’s Guide, Version 7.8.2 ACTION NAME DATE SIGNATURE WRITTEN BY The GHC Team April 11, 2014 REVISION HISTORY NUMBER DATE DESCRIPTION NAME The Glorious Glasgow Haskell Compilation System User’s Guide, Version 7.8.2 iii Contents 1 Introduction to GHC 1 1.1 Obtaining GHC . .1 1.2 Meta-information: Web sites, mailing lists, etc. .1 1.3 Reporting bugs in GHC . .2 1.4 GHC version numbering policy . .2 1.5 Release notes for version 7.8.1 . .3 1.5.1 Highlights . .3 1.5.2 Full details . .5 1.5.2.1 Language . .5 1.5.2.2 Compiler . .5 1.5.2.3 GHCi . .6 1.5.2.4 Template Haskell . .6 1.5.2.5 Runtime system . .6 1.5.2.6 Build system . .6 1.5.3 Libraries . .6 1.5.3.1 array . .6 1.5.3.2 base . .7 1.5.3.3 bin-package-db . .7 1.5.3.4 binary . .7 1.5.3.5 bytestring . .7 1.5.3.6 Cabal . .8 1.5.3.7 containers . .8 1.5.3.8 deepseq . .8 1.5.3.9 directory . .8 1.5.3.10 filepath . .8 1.5.3.11 ghc-prim . .8 1.5.3.12 haskell98 .
    [Show full text]
  • Constrained Type Families (Extended Version)
    Constrained Type Families (extended version) J. GARRETT MORRIS, e University of Edinburgh and e University of Kansas 42 RICHARD A. EISENBERG, Bryn Mawr College We present an approach to support partiality in type-level computation without compromising expressiveness or type safety. Existing frameworks for type-level computation either require totality or implicitly assume it. For example, type families in Haskell provide a powerful, modular means of dening type-level computation. However, their current design implicitly assumes that type families are total, introducing nonsensical types and signicantly complicating the metatheory of type families and their extensions. We propose an alternative design, using qualied types to pair type-level computations with predicates that capture their domains. Our approach naturally captures the intuitive partiality of type families, simplifying their metatheory. As evidence, we present the rst complete proof of consistency for a language with closed type families. CCS Concepts: •eory of computation ! Type structures; •So ware and its engineering ! Func- tional languages; Additional Key Words and Phrases: Type families, Type-level computation, Type classes, Haskell ACM Reference format: J. Garre Morris and Richard A. Eisenberg. 2017. Constrained Type Families (extended version). PACM Progr. Lang. 1, 1, Article 42 (January 2017), 38 pages. DOI: hp://dx.doi.org/10.1145/3110286 1 INTRODUCTION Indexed type families (Chakravarty et al. 2005; Schrijvers et al. 2008) extend the Haskell type system with modular type-level computation. ey allow programmers to dene and use open mappings from types to types. ese have given rise to further extensions of the language, such as closed type families (Eisenberg et al.
    [Show full text]
  • Freebsd Foundation February 2018 Update
    FreeBSD Foundation February 2018 Update Upcoming Events Message from the Executive Director Dear FreeBSD Community Member, AsiaBSDCon 2018 Welcome to our February Newsletter! In this newsletter you’ll read about FreeBSD Developers Summit conferences we participated in to promote and teach about FreeBSD, March 8-9, 2018 software development projects we are working on, why we need Tokyo, Japan funding, upcoming events, a release engineering update, and more. AsiaBSDCon 2018 Enjoy! March 8-11, 2018 Tokyo, Japan Deb SCALE 16x March 8-11, 2018 Pasadena, CA February 2018 Development Projects FOSSASIA 2018 March 22-25, 2018 Update: The Modern FreeBSD Tool Chain: Singapore, Singapore LLVM's LLD Linker As with other BSD distributions, FreeBSD has a base system that FreeBSD Journal is developed, tested and released by a single team. Included in the base system is the tool chain, the set of programs used to build, debug and test software. FreeBSD has relied on the GNU tool chain suite for most of its history. This includes the GNU Compiler Collection (GCC) compiler, and GNU binutils which include the GNU "bfd" linker. These tools served the FreeBSD project well from 1993 until 2007, when the GNU project migrated to releasing its software under the General Public License, version 3 (GPLv3). A number of FreeBSD developers and users objected to new restrictions included in GPLv3, and the GNU tool chain became increasingly outdated. The January/February issue of the FreeBSD Journal is now available. Don't miss articles on The FreeBSD project migrated to Clang/LLVM for the compiler in Tracing ifconfig Commands, Storage FreeBSD 10, and most of GNU binutils were replaced with the ELF Tool Multipathing, and more.
    [Show full text]
  • Hassling with the IDE - for Haskell
    Hassling with the IDE - for Haskell This guide is for students that are new to the Haskell language and are in need for a proper I​ntegrated ​D​evelopment ​E​nvironment (IDE) to work on the projects of the course. This guide is intended for Windows users. It may work similarly on Linux and iOS. For more information, see: - https://www.haskell.org/platform/mac.html​ for iOS - https://www.haskell.org/downloads/linux/​ for Linux For any errors, please contact me at ​[email protected]​. Changelog 04/09/2019 Initial version 24/08/2020 Updated contents for the year 2020 / 2021, thanks to Bernadet for pointing it out 1. Installing Visual Studio Code Visual Studio Code (VSC) is a lightweight, but rather extensive, code editor. We’ll talk about its features throughout this guide. You can download VSC at: https://code.visualstudio.com/download Once installed, take note of the sidebar on the left. From top to bottom, it contains: - Explorer (Ctrl + Shift + E) - Search (Ctrl + Shift + F) - Source control (Ctrl + Shift + G) - Debug (Ctrl + Shift + D) - Extensions (Ctrl + Shift + X) The ​bold​ options are important to remember. They will be mentioned throughout this document. 2. Installing GHC (and stack / cabal) on Windows Download the Glasgow Haskell Compiler (GHC). This compiler is used throughout this course and any other course that uses Haskell. You can find its download instructions at: https://www.haskell.org/platform/windows.html The download link recommends you to use Chocolatey. ​Chocolatey is an open source project that provides developers and admins alike a better way to manage Windows software.
    [Show full text]
  • Twelf User's Guide
    Twelf User’s Guide Version 1.4 Frank Pfenning and Carsten Schuermann Copyright c 1998, 2000, 2002 Frank Pfenning and Carsten Schuermann Chapter 1: Introduction 1 1 Introduction Twelf is the current version of a succession of implementations of the logical framework LF. Previous systems include Elf (which provided type reconstruction and the operational semantics reimplemented in Twelf) and MLF (which implemented module-level constructs loosely based on the signatures and functors of ML still missing from Twelf). Twelf should be understood as research software. This means comments, suggestions, and bug reports are extremely welcome, but there are no guarantees regarding response times. The same remark applies to these notes which constitute the only documentation on the present Twelf implementation. For current information including download instructions, publications, and mailing list, see the Twelf home page at http://www.cs.cmu.edu/~twelf/. This User’s Guide is pub- lished as Frank Pfenning and Carsten Schuermann Twelf User’s Guide Technical Report CMU-CS-98-173, Department of Computer Science, Carnegie Mellon University, November 1998. Below we state the typographic conventions in this manual. code for Twelf or ML code ‘samp’ for characters and small code fragments metavar for placeholders in code keyboard for input in verbatim examples hkeyi for keystrokes math for mathematical expressions emph for emphasized phrases File names for examples given in this guide are relative to the main directory of the Twelf installation. For example ‘examples/guide/nd.elf’ may be found in ‘/usr/local/twelf/examples/guide/nd.elf’ if Twelf was installed into the ‘/usr/local/’ directory.
    [Show full text]
  • Session Arrows: a Session-Type Based Framework for Parallel Code Generation
    MENG INDIVIDUAL PROJECT IMPERIAL COLLEGE LONDON DEPARTMENT OF COMPUTING Session Arrows: A Session-Type Based Framework For Parallel Code Generation Supervisor: Prof. Nobuko Yoshida Author: Dr. David Castro-Perez Shuhao Zhang Second Marker: Dr. Iain Phillips June 19, 2019 Abstract Parallel code is notorious for its difficulties in writing, verification and maintenance. However, it is of increasing importance, following the end of Moore’s law. Modern pro- grammers are expected to utilize the power of multi-core CPUs and face the challenges brought by parallel programs. This project builds an embedded framework in Haskell to generate parallel code. Combining the power of multiparty session types with parallel computation, we create a session typed monadic language as the middle layer and use Arrow, a general interface to computation as an abstraction layer on top of the language. With the help of the Arrow interface, we convert the data-flow of the computation to communication and generate parallel code according to the communication pattern between participants involved in the computation. Thanks to the addition of session types, not only the generated code is guaranteed to be deadlock-free, but also we gain a set of local types so that it is possible to reason about the communication structure of the parallel computation. In order to show that the framework is as expressive as usual programming lan- guages, we write several common parallel computation patterns and three algorithms to benchmark using our framework. They demonstrate that users can express computa- tion similar to traditional sequential code and gain, for free, high-performance parallel code in low-level target languages such as C.
    [Show full text]