Introduction to Dependently Types: Idris

25
Idris Abdulsattar - http://bimorphic.com

Transcript of Introduction to Dependently Types: Idris

Page 1: Introduction to Dependently Types: Idris

IdrisAbdulsattar - http://bimorphic.com

Page 2: Introduction to Dependently Types: Idris

FeaturesStrictly evaluated purely functional language

Has backends to LLVM, C, Javascript and even Erlang!

Full dependent types with dependent pattern matching

Simple foreign function interface (to C)

Compiler-supported interactive editing: the compiler helps you write code using the types

where clauses, with rule, simple case expressions, pattern matching let and lambda bindings

Dependent records with projection and update

Type classes

Type-driven overloading resolution

do notation and idiom brackets

Indentation significant and Extensible syntax (very suitable for writing DSLs)

Cumulative universes

Totality checking & a REPL

Page 3: Introduction to Dependently Types: Idris

Motivationdef first(arr) arr[0]end

Points of Failure:1. arr is not an array2. arr is null3. arr is empty

Page 4: Introduction to Dependently Types: Idris

Motivationpublic int first(int[] list) { return list[0];}

Point of Failures:1. arr is not an array2. arr is null3. arr is empty

Page 5: Introduction to Dependently Types: Idris

Motivationfirst :: [Int] -> Int first xs = xs !! 0

Point of Failures:1. arr is not an array2. arr is null3. arr is empty

Page 6: Introduction to Dependently Types: Idris

Actual Requirement

arr must be an array of at least 1 length

Page 7: Introduction to Dependently Types: Idris

Problem

first :: [Int] -> Int first xs = xs !! 0

public int first(int[] list) { return list[0];}

def first(arr) arr[0]end

anything → anything (or runtime error)

array or null → int (or runtime error)

array → int (or runtime error)

Page 8: Introduction to Dependently Types: Idris

Problem● Types don’t capture all the invariants● Functions work for only a subset of inputs ‒ they are not

total

Page 9: Introduction to Dependently Types: Idris

Dependent TypesDependent Types allow types to depend on their values.e.g. length of a list can be part of the type of the list

Page 10: Introduction to Dependently Types: Idris

Natural Numbersdata Nat = Z | S Nat -- peano numbers

zero = Z

one = S Z

two = S (S Z)…

Page 11: Introduction to Dependently Types: Idris

Operationsplus : Nat -> Nat -> Natplus Z y = yplus (S k) y = S (plus k y)

0 + y = y(1 + x) + y = 1 + (x + y)

0 ✕ y = 0(1 + x) ✕ y = y + (x ✕ y)

mult : Nat -> Nat -> Natmult Z y = Zmult (S k) y = plus y (mult k y)

Page 12: Introduction to Dependently Types: Idris

Vectorsdata Vect : Nat -> Type -> Type where Nil : Vect Z a (::) : a -> Vect k a -> Vect (S k) a

zeroVect : Vect 0 Int zeroVect = Nil

oneVect : Vect 1 Int oneVect = 3 :: Nil

threeVect : Vect 3 String threeVect = "I" :: "hope" :: "i'm not confusing you" :: Nil

Page 13: Introduction to Dependently Types: Idris

Solutionfirst : Vect (S k) a -> afirst (x::xs) = x

Points of Failure:1. arr is not an array2. arr is null3. arr is empty

Page 14: Introduction to Dependently Types: Idris

Concatenation(++) : Vect n a -> Vect m a -> Vect (n + m) a(++) Nil ys = ys(++) (x :: xs) ys = x :: xs ++ ys

(++) : Vect n a -> Vect m a -> Vect (n + m) a(++) Nil ys = ys(++) (x :: xs) ys = x :: xs ++ xs

Error: Expected Vect (n + m) a; Got Vect (n + n) a

Page 15: Introduction to Dependently Types: Idris

Another problemGetting an element in an array by index

Goal: No more ArrayIndexOutOfBoundsException!

Page 16: Introduction to Dependently Types: Idris

elemAtelemAt : Fin n -> Vect n a -> a

elemAt FZ (x :: xs) = xelemAt (FS k) (x :: xs) = elemAt k xs

data Fin : Nat -> Type where FZ : Fin (S k) FS : Fin k -> Fin (S k)

Page 17: Introduction to Dependently Types: Idris

Even Numbersdata Even : Nat -> Type where EZ : Even Z ES : Even k -> Even (S (S k))

zeroIsEven : Even 0zeroIsEven = EZ

twoIsEven : Even 2twoIsEven = ES EZ

Page 18: Introduction to Dependently Types: Idris

ProofsTheorem: 4 is even

0 is even0 + 2 is even

(0 + 2) + 2 is even

Proof by Mathematical Induction

fourIsEven : Even 4fourIsEven = ES (ES EZ)

Idris

Page 19: Introduction to Dependently Types: Idris

Curry Howard Correspondence

Programs and mathematical proofs are the same thing, basically

Page 20: Introduction to Dependently Types: Idris

Equalitydata (=) : a -> b -> Type where Refl : x = x

twoIsTwo : 2 = 2twoIsTwo = Refl

3Plus2IsFive : 3 + 2 = 53Plus2IsFive = Refl

Page 21: Introduction to Dependently Types: Idris

Falsity or the Empty Type

disjoint : (n : Nat) -> Z = S n -> Voiddisjoint n p = replace {P = disjointTy} p () where disjointTy : Nat -> Type disjointTy Z = () disjointTy (S k) = Void

data Void : Type where

Page 22: Introduction to Dependently Types: Idris

Interactive Editing

DEMO

Page 23: Introduction to Dependently Types: Idris

Types as First-Class citizens

DEMO

Page 24: Introduction to Dependently Types: Idris

Type Safe printf

DEMO

Page 25: Introduction to Dependently Types: Idris

Thank you!Q & A