introduction to dependently types: idris

Post on 26-Jan-2017

334 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

IdrisAbdulsattar - http://bimorphic.com

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

Motivationdef first(arr) arr[0]end

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

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

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

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

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

Actual Requirement

arr must be an array of at least 1 length

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)

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

total

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

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

zero = Z

one = S Z

two = S (S Z)…

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)

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

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

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

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

Another problemGetting an element in an array by index

Goal: No more ArrayIndexOutOfBoundsException!

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)

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

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

Curry Howard Correspondence

Programs and mathematical proofs are the same thing, basically

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

twoIsTwo : 2 = 2twoIsTwo = Refl

3Plus2IsFive : 3 + 2 = 53Plus2IsFive = Refl

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

Interactive Editing

DEMO

Types as First-Class citizens

DEMO

Type Safe printf

DEMO

Thank you!Q & A

top related