GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

Similar documents
GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

Type families and data kinds

IA014: Advanced Functional Programming

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

GADTs. GADTs in Haskell

Typing Data. Chapter Recursive Types Declaring Recursive Types

Type-indexed functions in Generic Haskell

Programming in Omega Part 1. Tim Sheard Portland State University

Lecture 8: Summary of Haskell course + Type Level Programming

CS 320: Concepts of Programming Languages

Simple Unification-based Type Inference for GADTs

Logic - CM0845 Introduction to Haskell

CS 320: Concepts of Programming Languages

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Lists. Michael P. Fourman. February 2, 2010

Programming with Universes, Generically

Applicative, traversable, foldable

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

Structural polymorphism in Generic Haskell

Shell CSCE 314 TAMU. Higher Order Functions

The type checker will complain that the two branches have different types, one is string and the other is int

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Overloading, Type Classes, and Algebraic Datatypes

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Programming Languages Lecture 14: Sum, Product, Recursive Types

A general introduction to Functional Programming using Haskell

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Advanced features of Functional Programming (Haskell)

Programming Language Concepts, CS2104 Lecture 7

CS 457/557: Functional Languages

Programming Languages Fall 2013

Software System Design and Implementation

Programming with Math and Logic

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

Denotational Semantics. Domain Theory

CS558 Programming Languages

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CS 360: Programming Languages Lecture 12: More Haskell

Introduction to Programming, Aug-Dec 2006

COMP 3141 Software System Design and Implementation

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

CS115 - Module 10 - General Trees

Type Checking and Type Inference

CSCE 314 Programming Languages

Modules and Representation Invariants

Lecture 4: Higher Order Functions

CS 320 Homework One Due 2/5 11:59pm

Graphical Untyped Lambda Calculus Interactive Interpreter

Advances in Programming Languages

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

Pattern Matching and Abstract Data Types

Giving Haskell a Promotion

CS558 Programming Languages

Concepts of programming languages

Applicative, traversable, foldable

Exercise 1 (2+2+2 points)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Combining Programming with Theorem Proving

Abstract Interpretation Using Laziness: Proving Conway s Lost Cosmological Theorem

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Recap: ML s Holy Trinity

CSCI-GA Scripting Languages

An introduction introduction to functional functional programming programming using usin Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell

MoreIntro.v. MoreIntro.v. Printed by Zach Tatlock. Oct 07, 16 18:11 Page 1/10. Oct 07, 16 18:11 Page 2/10. Monday October 10, 2016 lec02/moreintro.

Exercise 1: Graph coloring (10 points)

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Programming Languages 3. Definition and Proof by Induction

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Theorem Proving Principles, Techniques, Applications Recursion

Applying Type-Level and Generic Programming in Haskell

Data types for mcrl2

CSCE 314 Programming Languages

Programming Languages

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Data types à la carte

GADTs gone mild. Code at Gabriel RADANNE 23 Mars 2017

An Introduction to Programming and Proving in Agda (incomplete draft)

Coercion Quantification

INTRODUCTION TO FUNCTIONAL PROGRAMMING

CS 320: Concepts of Programming Languages

Introduction. Wouter Swierstra. Applied functional programming. Faculty of Science Information and Computing Sciences

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

Exercise 1 ( = 24 points)

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

CS 11 Ocaml track: lecture 4. Today: modules

Programming Languages. Datatypes

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages

Transcription:

GADTs AFP Summer School Alejandro Serrano 1

Today s lecture Generalized algebraic data types (GADTs) 2

A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces: 3

A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces: a new datatype Tree of kind * -> *. 3

A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces: a new datatype Tree of kind * -> *. two constructor functions Leaf :: Tree a Node :: Tree a -> a -> Tree a -> Tree a 3

A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces: a new datatype Tree of kind * -> *. two constructor functions Leaf :: Tree a Node :: Tree a -> a -> Tree a -> Tree a 3 the possiblity to use the constructors Leaf and Node in patterns.

Alternative syntax Observation The types of the constructor functions contain sufficient information to describe the datatype. data Tree :: * -> * where Leaf :: Tree a Node :: Tree a -> a -> Tree a -> Tree a Question What are the restrictions regarding the types of the constructors? 4

Algebraic datatypes Constructors of an algebraic datatype T must: target the type T, result in a simple type of kind *, i.e., T a1... an where a1,..., an are distinct type variables. 5

Another example data Either :: * -> * -> * Left :: a -> Either a b Right :: b -> Either a b where Both constructors produce values of type Either a b. Does it make sense to lift these restrictions? 6

Excursion: Expression language Imagine we re implementing a small programming language in Haskell: data Expr = LitI Int LitB Bool IsZero Expr Plus Expr Expr If Expr Expr Expr 7

Excursion: Expression language Alternatively, we could redefine the data type as follows: data Expr :: * where LitI :: Int -> Expr LitB :: Bool -> Expr IsZero :: Expr -> Expr Plus :: Expr -> Expr -> Expr If :: Expr -> Expr -> Expr -> Expr 8

Syntax: concrete vs abstract Imagined concrete syntax: if iszero (0 + 1) then False else True How is it represented in abstract syntax? 9

Syntax: concrete vs abstract Imagined concrete syntax: if iszero (0 + 1) then False else True How is it represented in abstract syntax? If (IsZero (Plus (LitI 0) (LitI 1))) (LitB False) (LitB True) 9

Evaluation Try it yourself 10

Evaluation Before we write an interpreter, we need to choose the type that it returns. Our expressions may evaluate to booleans or integers: data Val = VInt Int VBool Bool Defining an interpreter now boils down to defining a function: eval :: Expr a -> Val 11

Evaluation eval :: Expr a -> Val eval (LitI n) = VInt n eval (LitB b) = VBool b eval (IsZero e) = case eval e of VInt n -> VBool (n == 0) _ -> error "type error" eval (Plus e1 e2) = case (eval e1, eval e2) of (VInt n1, VInt n2) -> VInt (n1 + n2) _ -> error "type error" 12

Problems with evaluation Evaluation code is mixed with code for handling type errors. The evaluator uses tags (i.e., constructors) to dinstinguish values these tags are maintained and checked at run time. Run-time type errors can, of course, be prevented by writing a type checker or using phantom types. 13

Type errors It is all too easy to write ill-typed expressions such as: If (LitI 0) (LitB False) (LitI 1) How can we prevent programmers from writing such terms? 14

Phantom types At the moment, all expressions have the same type: data Expr = LitI Int LitB Bool... We would like to distinguish between expressions of different types. 15

Phantom types At the moment, all expressions have the same type: data Expr = LitI Int LitB Bool... We would like to distinguish between expressions of different types. To do so, we add an additional type parameter to our expression data type. 15

Phantom types data Expr a = LitI Int LitB Bool IsZero (Expr Int) Plus (Expr Int) (Expr Int) If (Expr Bool) (Expr a) (Expr a) Note: the type variable a is never actually used in the data type for expressions. We call such type variables phantom types. 16

Constructing well typed terms Rather than expose the constructors of our expression language, we can instead provide a well-typed API for users to write terms: liti :: Int -> Expr Int liti = LitI plus :: Expr Int -> Expr Int -> Expr Int plus = Plus iszero :: Expr Int -> Expr Bool iszero = IsZero 17 This guarantees that users will only ever construct well-typed terms! But what about writing an interpreter

More problems with evaluation Even if we know that we only have type-correct terms, the Haskell compiler does not enforce this. We still need to write all the error cases. 18

Beyond phantom types What if we encode the type of the term in the Haskell type? data Expr :: * -> * where LitI :: Int -> Expr Int LitB :: Bool -> Expr Bool IsZero :: Expr Int -> Expr Bool Plus :: Expr Int -> Expr Int -> Expr Int If :: Expr Bool -> Expr a -> Expr a -> Expr a Each expression has an additional type argument, representing the type of values it stores. 19

GADTs GADTs lift the restriction that all constructors must produce values of the same type. Constructors can have more specific return types. Interesting consequences for pattern matching: when case-analyzing an Expr Int, it could not be constructed by Bool or IsZero; when case-analyzing an Expr Bool, it could not be constructed by Int or Plus; when case-analyzing an Expr a, once we encounter the constructor IsZero in a pattern, we know that we must be dealing with an Expr Bool; 20

Evaluation revisited eval :: Expr a -> a eval (LitI n) = n eval (LitB b) = b eval (IsZero e) = (eval e) == 0 eval (Plus e1 e2) = eval e1 + eval e2 eval (If e1 e2 e3) eval e1 = eval e2 otherwise = eval e3 No possibility for run-time failure (modulo undefined); no tags required on our values. Pattern matching on a GADT requires a type signature. Why? 21

Limitation: type signatures are required data X :: * -> * where C :: Int -> X Int D :: X a E :: Bool -> X Bool f (C n) = [n] -- (1) f D = [] -- (2) f (E n) = [n] -- (3) 22

Limitation: type signatures are required data X :: * -> * where C :: Int -> X Int D :: X a E :: Bool -> X Bool f (C n) = [n] -- (1) f D = [] -- (2) f (E n) = [n] -- (3) What is the type of f, with/without (3)? What is the (probable) desired type? 22 f :: X a -> [Int] -- (1) only f :: X b -> [c] -- (2) only f :: X a -> [Int] -- (1) + (2)

Extending our language Let us extend the expression types with pair construction and projection: data Expr :: * -> * where... Pair :: Expr a -> Expr b -> Expr (a, b) Fst :: Expr (a,b) -> Expr a Snd :: Expr (a,b) -> Expr b For Fst and Snd, the type of the non-projected component is hidden that is, it is not visible from the type of the compound expression. 23

Evaluation again eval :: Expr a -> a eval... eval (Pair x y) = (eval x, eval y) eval (Fst p) = fst (eval p) eval (Snd p) = snd (eval p) 24

GADTs GADTs have become one of the more popular Haskell extensions. The classic example for motivating GADTs is interpreters for expression languages, such as the one we have seen here. However, these richer data types offer many other applications. In particular, they let us program with types in interesting new ways. 25

Prelude.head: empty list > mycomplicatedfunction 42 "inputfile.csv" *** Exception: Prelude.head: empty list Can we use the type system to rule out such exceptions before a program is run? 26

Prelude.head: empty list > mycomplicatedfunction 42 "inputfile.csv" *** Exception: Prelude.head: empty list Can we use the type system to rule out such exceptions before a program is run? To do so, we ll introduce a new list-like datatype that records the length of the list in its type. 26

Natural numbers and vectors Natural numbers can be encoded as types no constructors are required. data Zero data Succ a Vectors are lists with a fixed number of elements: data Vec :: * -> * -> * where Nil :: Vec a Zero Cons :: a -> Vec a n -> Vec a (Succ n) 27

Type-safe head and tail head :: Vec a (Succ n) -> a head (Cons x xs) = x tail :: Vec a (Succ n) -> Vec a n tail (Cons x xs) = xs Question Why is there no case for Nil is required? 28

Type-safe head and tail head :: Vec a (Succ n) -> a head (Cons x xs) = x tail :: Vec a (Succ n) -> Vec a n tail (Cons x xs) = xs Question Why is there no case for Nil is required? Actually, a case for Nil results in a type error. 28

More functions on vectors map :: (a -> b) -> Vec a n -> Vec b n map f Nil = Nil map f (Cons x xs) = Cons (f x) (map f xs) zipwith :: (a -> b -> c) -> Vec a n -> Vec b n -> Vec c n zipwith op Nil Nil = Nil zipwith op (Cons x xs) (Cons y ys) = Cons (op x y) (zipwith op xs ys) We can require that the two vectors have the same length! This lets us rule out bogus cases. 29

Yet more functions on vectors snoc :: Vec a n -> a -> Vec a (Succ n) snoc Nil y = Cons y Nil snoc (Cons x xs) y = Cons x (snoc xs y) reverse :: Vec a n -> Vec a n reverse Nil = Nil reverse (Cons x xs) = snoc xs x What about appending two vectors, analogous to the (++) operation on lists? 30

Problematic functions What is the type of our append function? vappend :: Vec a m -> Vec a n -> Vec a??? 31

Problematic functions What is the type of our append function? vappend :: Vec a m -> Vec a n -> Vec a??? How can we add two types, n and m? 31

Problematic functions What is the type of our append function? vappend :: Vec a m -> Vec a n -> Vec a??? How can we add two types, n and m? Suppose we want to convert from lists to vectors: fromlist :: [a] -> Vec a n Where does the type variable n come from? What possible values can it have? 31

Writing vector append There are multiple options to solve that problem: construct explicit evidence, use a type family (more on that in the next lecture). 32

Explicit evidence Given two types n and m, what is their sum? We can define a GADT describing the graph of addition: data Sum :: * -> * -> * -> * where SumZero :: Sum Zero n n SumSucc :: Sum m n s -> Sum (Succ m) n (Succ s) 33

Explicit evidence Given two types n and m, what is their sum? We can define a GADT describing the graph of addition: data Sum :: * -> * -> * -> * where SumZero :: Sum Zero n n SumSucc :: Sum m n s -> Sum (Succ m) n (Succ s) Using this function, we can now define append as follows: append :: Sum m n s -> Vec a m -> Vec a n -> Vec a s append SumZero Nil ys = ys append (SumSucc p) (Cons x xs) ys = Cons x (append p xs ys) 33

Passing explicit evidence This approach has one major disadvantage: we must construct the evidence, the values of type Sum n m p, by hand every time we wish to call append. We could use a multi-parameter type class with functional dependencies to automate this construction 34

Converting between lists and vectors It is easy enough to convert from a vector to a list: tolist :: Vec a n -> [a] tolist Nil = [] tolist (Cons x xs) = x : tolist xs This simply discards the type information we have carefully constructed. 35

Converting between lists and vectors It is easy enough to convert from a vector to a list: tolist :: Vec a n -> [a] tolist Nil = [] tolist (Cons x xs) = x : tolist xs This simply discards the type information we have carefully constructed. 35

Converting between lists and vectors Converting in the other direction, however is not as easy: fromlist :: [a] -> Vec a n fromlist [] = Nil fromlist (x:xs) = Cons x (fromlist xs) Question Why doesn t this definition type check? 36

Converting between lists and vectors Converting in the other direction, however is not as easy: fromlist :: [a] -> Vec a n fromlist [] = Nil fromlist (x:xs) = Cons x (fromlist xs) Question Why doesn t this definition type check? The type says that the result must be polymorphic in n, that is, it returns a vector of any length, rather than a vector of a specific (unknown) length. 36

From lists to vectors We can specify the length of the vector being constructed in a separate argument, hide the length using an existential type. 37

From lists to vectors (contd.) Suppose we simply pass in a regular natural number, Nat: fromlist :: Nat -> [a] -> Vec a n fromlist Zero [] = Nil fromlist (Succ n) (x:xs) = Cons x (fromlist n xs) fromlist = error "wrong length!" 38

From lists to vectors (contd.) Suppose we simply pass in a regular natural number, Nat: fromlist :: Nat -> [a] -> Vec a n fromlist Zero [] = Nil fromlist (Succ n) (x:xs) = Cons x (fromlist n xs) fromlist = error "wrong length!" This still does not solve our problem there is no connection between the natural number that we are passing and the n in the return type. 38

Singletons We need to reflect type-level natural numbers on the value level. To do so, we define a (yet another) variation on natural numbers: data SNat :: * -> * where SZero :: SNat Zero SSucc :: SNat n -> SNat (Succ n) This is a singleton type for any n, the type SNat n has a single inhabitant (the number n). 39

From lists to vectors data SNat :: * -> * where SZero :: SNat Zero SSucc :: SNat n -> SNat (Succ n) fromlist :: SNat n -> [a] -> Vec a n fromlist SZero [] = Nil fromlist (SSucc n) (x:xs) = Cons x (fromlist n xs) fromlist = error "wrong length!" Question This function may still fail dynamically. Why? 40

From lists to vectors We can specify the length of the vector being constructed in a separate argument, hide the length using an existential type. What about the second alternative? 41

From lists to vectors We can define a wrapper around vectors, hiding their length: data VecAnyLen :: * -> * where VecAnyLen :: Vec a n -> VecAnyLen a A value of type VecAnyLen a stores a vector of some length with values of type a. 42

From lists to vectors We can convert any list to a vector of some length as follows: fromlist :: [a] -> VecAnyLen a fromlist [] = VecAnyLen Nil fromlist (x:xs) = case fromlist xs of VecAnyLen ys -> VecAnyLen (Cons x ys) 43

From lists to vectors We can combine the two approaches and include a SNat in the packed type: data VecAnyLen :: * -> * where VecAnyLen :: SNat n -> Vec a n -> VecAnyLen a Question How does the conversion function change? 44

Comparing the length of vectors We can define a boolean function that checks when two vectors have the same length equallength :: Vec a m -> Vec b n -> Bool equallength Nil Nil = True equallength (Cons _ xs) (Cons _ ys) = equallength xs equallength = False 45

Comparing the length of vectors Suppose I want to use this to check the lengths of my vectors: if equallength xs ys then zipvec xs ys else error "Wrong lengths" Question Will this type check? 46

Comparing the length of vectors Suppose I want to use this to check the lengths of my vectors: if equallength xs ys then zipvec xs ys else error "Wrong lengths" Question Will this type check? No! Just because equallength xs ys returns True, does not guarantee that m and n are equal How can we enforce that two types are indeed equal? 46

Equality type Just as we saw for the Sum type, we can introduce a GADT that represents a proof that two types are equal: data Equal :: * -> * -> * where Refl :: Equal a a 47

Properties of the equality relation refl :: Equal a a sym :: Equal a b -> Equal b a trans :: Equal a b -> Equal b c -> Equal a c How are these functions defined? 48

Properties of the equality relation refl :: Equal a a sym :: Equal a b -> Equal b a trans :: Equal a b -> Equal b c -> Equal a c How are these functions defined? refl = Refl sym Refl = Refl trans Refl Refl = Refl What happens if you don t pattern match on the Refl constructor? 48

Build an equality proof Instead of returning a boolean, we can now provide evidence that the length of two vectors is equal: eqlength :: Vec a m -> Vec b n -> Maybe (Equal m n) eqlength Nil Nil = Just Refl eqlength (Cons x xs) (Cons y ys) = case eqlength xs ys of Just Refl -> Just Refl Nothing -> Nothing eqlength = Nothing You have to pattern match on Refl above! 49

Using equality test :: Vec a m -> Vec b (Succ n) -> Maybe (a,b) test xs ys = case eqlength xs ys Just Refl -> head (zipvec xs ys) _ -> Nothing Question Why does this type check? 50

Expressive power of equality The equality type can be used to encode other GADTs. Recall our expression example using phantom types: data Expr a = LitI Int LitB Bool IsZero (Expr Int) Plus (Expr Int) (Expr Int) If (Expr Bool) (Expr a) (Expr a) 51

Expressive power of equality We can use equality proofs and phantom types to implement GADTs: data Expr a = LitI (Equal a Int) Int LitB (Equal a Bool) Bool IsZero (Equal a Bool) (Equal b Int) Plus (Equal a Int) (Expr Int) (Expr Int) If (Expr Bool) (Expr a) (Expr a) 52

Summary GADTs can be used to encode advanced properties of types in the type language. We end up mirroring expression-level concepts on the type level (e.g. natural numbers). GADTs can also represent data that is computationally irrelevant and just guides the type checker (equality proofs, evidence for addition). Such information could ideally be erased, but in Haskell, we can always cheat via undefined :: Equal Int Bool 53

Tomorrow Wouter will introduce functional dependencies and type families as another way to perform computation over types -- No need to build 'Sum' by hand append :: Sum m n s => Vec a m -> Vec a n -> Vec a s -- No need to have a result argument append :: Vec a m -> Vec a n -> Vec a ( 54