Dependent types in Idris Lukasz Hanuszczak April 02, 2015 Why? - - PowerPoint PPT Presentation

dependent types in idris
SMART_READER_LITE
LIVE PREVIEW

Dependent types in Idris Lukasz Hanuszczak April 02, 2015 Why? - - PowerPoint PPT Presentation

Dependent types in Idris Lukasz Hanuszczak April 02, 2015 Why? Static typing If it compiles, it runs. some Haskell guy Static typing If it compiles, it runs. some Haskell guy Its a lie. The night is dark and full of


slide-1
SLIDE 1

Dependent types in Idris

  • Lukasz Hanuszczak

April 02, 2015

slide-2
SLIDE 2

Why?

slide-3
SLIDE 3

Static typing

If it compiles, it runs. – some Haskell guy

slide-4
SLIDE 4

Static typing

If it compiles, it runs. – some Haskell guy

It’s a lie.

slide-5
SLIDE 5

The night is dark and full of terrors

import System.IO.Unsafe unsafePerformIO :: IO a -> a

slide-6
SLIDE 6

The night is dark and full of terrors

import System.IO.Unsafe unsafePerformIO :: IO a -> a Not that bad.

slide-7
SLIDE 7

The night is dark and full of terrors

import System.IO.Unsafe unsafePerformIO :: IO a -> a Not that bad. head :: [a] -> a tail :: [a] -> [a] (!!) :: Int -> [a] -> a

slide-8
SLIDE 8

The night is dark and full of terrors

import System.IO.Unsafe unsafePerformIO :: IO a -> a Not that bad. head :: [a] -> a tail :: [a] -> [a] (!!) :: Int -> [a] -> a Dreadful.

slide-9
SLIDE 9

The night is dark and full of terrors

import System.IO.Unsafe unsafePerformIO :: IO a -> a Not that bad. head :: [a] -> Maybe a tail :: [a] -> Maybe [a] (!!) :: Int -> [a] -> Maybe a Useless.

slide-10
SLIDE 10
  • Previously. . .
slide-11
SLIDE 11

Tagged lists

data Empty data NonEmpty data List t a where Nil :: List Empty a Cons :: a -> List t a -> List NonEmpty a

slide-12
SLIDE 12

Tagged lists

data Empty data NonEmpty data List t a where Nil :: List Empty a Cons :: a -> List t a -> List NonEmpty a head :: List NonEmpty a -> a head (Cons x _) = x

slide-13
SLIDE 13

Tagged lists

data Empty data NonEmpty data List t a where Nil :: List Empty a Cons :: a -> List t a -> List NonEmpty a head :: List NonEmpty a -> a head (Cons x _) = x tail :: List NonEmpty a -> ??? tail (Cons _ t) = t

slide-14
SLIDE 14

Vectors (first attempt)

{-# LANGUAGE GADTs #-}

slide-15
SLIDE 15

Vectors (first attempt)

{-# LANGUAGE GADTs #-} data Zero data Succ n where Succ :: Succ n

slide-16
SLIDE 16

Vectors (first attempt)

{-# LANGUAGE GADTs #-} data Zero data Succ n where Succ :: Succ n data Vect n a where Nil :: Vect Zero a Cons :: a -> Vect n a -> Vect (Succ n) a

slide-17
SLIDE 17

Vectors (first attempt)

{-# LANGUAGE GADTs #-} data Zero data Succ n where Succ :: Succ n data Vect n a where Nil :: Vect Zero a Cons :: a -> Vect n a -> Vect (Succ n) a head :: Vector (Succ n) a -> a head (Cons x xs) = x tail :: Vector (Succ n) a -> Vector n a tail (Cons x xs) = xs

slide-18
SLIDE 18

Vectors (first attempt)

{-# LANGUAGE GADTs #-} data Zero data Succ n where Succ :: Succ n data Vect n a where Nil :: Vect Zero a Cons :: a -> Vect n a -> Vect (Succ n) a head :: Vector (Succ n) a -> a head (Cons x xs) = x tail :: Vector (Succ n) a -> Vector n a tail (Cons x xs) = xs (++) :: Vector n a -> Vector m a -> ??? (++) Nil ys = ys (++) (Cons x xs) ys = Cons x (xs ++ ys)

slide-19
SLIDE 19

Vectors (first+ attempt)

{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances, FlexibleContexts #-} {-# LANGUAGE UndecidableInstances #-}

slide-20
SLIDE 20

Vectors (first+ attempt)

{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances, FlexibleContexts #-} {-# LANGUAGE UndecidableInstances #-} class Plus n m nm instance Plus Zero m m instance (Plus n m nm) => Plus (Succ n) m (Succ nm)

slide-21
SLIDE 21

Vectors (first+ attempt)

{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances, FlexibleContexts #-} {-# LANGUAGE UndecidableInstances #-} class Plus n m nm instance Plus Zero m m instance (Plus n m nm) => Plus (Succ n) m (Succ nm) (++) :: Plus n m nm => Vect n a -> Vect m a -> Vect nm a (++) Nil ys = ys (++) (Cons x xs) ys = Cons x (xs ++ ys)

slide-22
SLIDE 22

Vectors (first+ attempt)

{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances, FlexibleContexts #-} {-# LANGUAGE UndecidableInstances #-} class Plus n m nm instance Plus Zero m m instance (Plus n m nm) => Plus (Succ n) m (Succ nm) (++) :: Plus n m nm => Vect n a -> Vect m a -> Vect nm a (++) Nil ys = ys (++) (Cons x xs) ys = Cons x (xs ++ ys) . . . but it doesn’t work.

slide-23
SLIDE 23

Vectors (second attempt)

{-# LANGUAGE GADTs, DataKinds #-} {-# LANGUAGE TypeFamilies, TypeOperators #-}

slide-24
SLIDE 24

Vectors (second attempt)

{-# LANGUAGE GADTs, DataKinds #-} {-# LANGUAGE TypeFamilies, TypeOperators #-} data Nat = Zero | Succ Nat

slide-25
SLIDE 25

Vectors (second attempt)

{-# LANGUAGE GADTs, DataKinds #-} {-# LANGUAGE TypeFamilies, TypeOperators #-} data Nat = Zero | Succ Nat type family Plus (n :: Nat) (m :: Nat) :: Nat type instance Plus Zero m = m type instance Plus (Succ n) m = Succ (Plus n m)

slide-26
SLIDE 26

Vectors (second attempt)

{-# LANGUAGE GADTs, DataKinds #-} {-# LANGUAGE TypeFamilies, TypeOperators #-} data Nat = Zero | Succ Nat type family Plus (n :: Nat) (m :: Nat) :: Nat type instance Plus Zero m = m type instance Plus (Succ n) m = Succ (Plus n m) data Vect n a where Nil :: Vect Zero a Cons :: a -> Vect n a -> Vect (Succ n) a

slide-27
SLIDE 27

Vectors (second attempt)

{-# LANGUAGE GADTs, DataKinds #-} {-# LANGUAGE TypeFamilies, TypeOperators #-} data Nat = Zero | Succ Nat type family Plus (n :: Nat) (m :: Nat) :: Nat type instance Plus Zero m = m type instance Plus (Succ n) m = Succ (Plus n m) data Vect n a where Nil :: Vect Zero a Cons :: a -> Vect n a -> Vect (Succ n) a (++) :: Vect n a -> Vect m a -> Vect (Plus n m) a (++) Nil ys = ys (++) (Cons x xs) ys = Cons x (xs ++ ys)

slide-28
SLIDE 28

Idris

slide-29
SLIDE 29

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax

slide-30
SLIDE 30

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax ◮ totality checking

slide-31
SLIDE 31

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax ◮ totality checking ◮ eager evaluation

slide-32
SLIDE 32

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax ◮ totality checking ◮ eager evaluation ◮ foreign function interface (C, JavaScript)

slide-33
SLIDE 33

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax ◮ totality checking ◮ eager evaluation ◮ foreign function interface (C, JavaScript) ◮ type classes

slide-34
SLIDE 34

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax ◮ totality checking ◮ eager evaluation ◮ foreign function interface (C, JavaScript) ◮ type classes ◮ function overloading

slide-35
SLIDE 35

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax ◮ totality checking ◮ eager evaluation ◮ foreign function interface (C, JavaScript) ◮ type classes ◮ function overloading ◮ a lot of sugar

slide-36
SLIDE 36

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax ◮ totality checking ◮ eager evaluation ◮ foreign function interface (C, JavaScript) ◮ type classes ◮ function overloading ◮ a lot of sugar ◮ modules, namespaces

slide-37
SLIDE 37

Idris

It is a general purpose pure functional programming language with dependent types, featuring:

◮ Haskell-like syntax ◮ totality checking ◮ eager evaluation ◮ foreign function interface (C, JavaScript) ◮ type classes ◮ function overloading ◮ a lot of sugar ◮ modules, namespaces ◮ . . . a lot more

slide-38
SLIDE 38

Dependent types

What does it mean to be functional?

slide-39
SLIDE 39

Dependent types

What does it mean to be functional?

To have functions as first class values.

slide-40
SLIDE 40

Dependent types

What does it mean to be functional?

To have functions as first class values.

What does it mean to be dependently typed?

slide-41
SLIDE 41

Dependent types

What does it mean to be functional?

To have functions as first class values.

What does it mean to be dependently typed?

To have types as first class values.

slide-42
SLIDE 42

Types as first class values

Haskell

type Foo = [Int]

slide-43
SLIDE 43

Types as first class values

Haskell

type Foo = [Int]

Idris

Foo : Type Foo = List Int

slide-44
SLIDE 44

Types as first class values

Haskell

type Foo = [Int]

Idris

Foo : Type Foo = List Int foo : Bool -> Type foo True = Int foo False = String

slide-45
SLIDE 45

Peano numbers

data Nat : Type where Z : Nat S : Nat -> Nat

slide-46
SLIDE 46

Peano numbers

data Nat : Type where Z : Nat S : Nat -> Nat (+) : Nat -> Nat -> Nat (+) Z m = m (+) (S n) m = S (n + m)

slide-47
SLIDE 47

Vectors (finally, the right way!)

data Vect : Nat -> Type -> Type where Nil : Vect Z a (::) : Vect n a -> Vect (S n) a

slide-48
SLIDE 48

Vectors (finally, the right way!)

data Vect : Nat -> Type -> Type where Nil : Vect Z a (::) : Vect n a -> Vect (S n) a head : Vect (S n) a -> a head (x :: _) = x tail : Vect (S n) a -> Vect n a tail (_ :: xs) = xs

slide-49
SLIDE 49

Vectors (finally, the right way!)

data Vect : Nat -> Type -> Type where Nil : Vect Z a (::) : Vect n a -> Vect (S n) a head : Vect (S n) a -> a head (x :: _) = x tail : Vect (S n) a -> Vect n a tail (_ :: xs) = xs (heavy breathing)

slide-50
SLIDE 50

Vectors (finally, the right way!)

data Vect : Nat -> Type -> Type where Nil : Vect Z a (::) : Vect n a -> Vect (S n) a head : Vect (S n) a -> a head (x :: _) = x tail : Vect (S n) a -> Vect n a tail (_ :: xs) = xs (heavy breathing) (++) : Vect n a -> Vect m a -> Vect (n + m) a (++) Nil ys = ys (++) (x :: xs) ys = x :: (xs ++ ys)

slide-51
SLIDE 51

A new roadblock. . .

index : Nat -> Vect n a -> a index Z (x :: _) = x index (S n) (_ :: xs) = index n xs index (S n) Nil = ???

slide-52
SLIDE 52

. . . and a solution

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

slide-53
SLIDE 53

. . . and a solution

data Fin : Nat -> Type where FZ : Fin (S n) FS : Fin n -> Fin (S n) index : Fin n -> Vect n a -> a index FZ (x :: _) = x index (FS n) (_ :: xs) = index n xs

slide-54
SLIDE 54

Pairs

slide-55
SLIDE 55

Pairs

Boring ones. . .

data Pair a b = MkPair a b foo : (String, Int) foo = ("Foo", 42)

slide-56
SLIDE 56

Pairs

Boring ones. . .

data Pair a b = MkPair a b foo : (String, Int) foo = ("Foo", 42)

. . . and dependent ones

data Sigma : (A : Type) -> (P : A -> Type) -> Type where MkSigma : {A : Type} -> {P : A -> Type}

  • > (a : A) -> P a -> Sigma A P

bar : Sigma Nat (\n => Vect n String) bar = MkSigma 2 ["a", "b"]

slide-57
SLIDE 57

Pairs

Boring ones. . .

data Pair a b = MkPair a b foo : (String, Int) foo = ("Foo", 42)

. . . and dependent ones

data Sigma : (A : Type) -> (P : A -> Type) -> Type where MkSigma : {A : Type} -> {P : A -> Type}

  • > (a : A) -> P a -> Sigma A P

bar : (n : Nat ** Vect n String) bar = (2 ** ["a", "b"])

slide-58
SLIDE 58

Pairs

Boring ones. . .

data Pair a b = MkPair a b foo : (String, Int) foo = ("Foo", 42)

. . . and dependent ones

data Sigma : (A : Type) -> (P : A -> Type) -> Type where MkSigma : {A : Type} -> {P : A -> Type}

  • > (a : A) -> P a -> Sigma A P

bar : (n ** Vect n String) bar = (_ ** ["a", "b"])

slide-59
SLIDE 59

Filtering

filter : (a -> Bool) -> Vect n a -> ??? filter _ [] = [] filter p (x :: xs) = let xs’ = filter p xs in if p x then x :: xs’ else xs’

slide-60
SLIDE 60

Filtering

filter : (a -> Bool) -> Vect n a -> (m ** Vect m a) filter _ [] = (0 ** []) filter p (x :: xs) = let (m ** xs’) = filter p xs in if p x then (S m ** x :: xs’) else (m ** xs’)

slide-61
SLIDE 61

Filtering

filter : (a -> Bool) -> Vect n a -> (m ** Vect m a) filter _ [] = (_ ** []) filter p (x :: xs) = let (_ ** xs’) = filter p xs in if p x then (_ ** x :: xs’) else (_ ** xs’)

slide-62
SLIDE 62

Predicates

Program is a proof of its type.

slide-63
SLIDE 63

Predicates

Program is a proof of its type. data Elem : a -> Vect n a -> Type where Here : Elem x (x :: xs) There : Elem x xs -> Elem x (y :: xs)

slide-64
SLIDE 64

Predicates

Program is a proof of its type. data Elem : a -> Vect n a -> Type where Here : Elem x (x :: xs) There : Elem x xs -> Elem x (y :: xs) numbers : Vect 6 Int numbers = [4, 8, 15, 16, 23, 42] test : Elem 15 numbers test = There (There Here)

slide-65
SLIDE 65

Predicates

mapEl : {xs : Vect n a} -> {f : a -> b}

  • > Elem x xs -> Elem (f x) (map f xs)

mapEl Here = Here mapEl (There e) = There (mapEl e)

slide-66
SLIDE 66

Predicates

mapEl : {xs : Vect n a} -> {f : a -> b}

  • > Elem x xs -> Elem (f x) (map f xs)

mapEl Here = Here mapEl (There e) = There (mapEl e) numbers’ : Vect 6 Int numbers’ = map (* 2) numbers test’ : Elem 30 numbers’ test’ = mapEl test { f = (* 2) }

slide-67
SLIDE 67

Predicates

mapEl : {xs : Vect n a} -> {f : a -> b}

  • > Elem x xs -> Elem (f x) (map f xs)

mapEl Here = Here mapEl (There e) = There (mapEl e) numbers’ : Vect 6 Int numbers’ = map (* 2) numbers test’ : Elem 30 numbers’ test’ = mapEl test { f = (* 2) } replaceEl : (xs : Vect k t) -> Elem x xs -> (y : t)

  • > (ys : Vect k t ** Elem y ys)

replaceEl (_ :: xs) Here y = (y :: xs ** Here) replaceEl (x :: xs) (There ex) y = let (ys ** ey) = replaceEl xs ex y in (x :: ys ** There ey)

slide-68
SLIDE 68

Lambda calculus interpreter

slide-69
SLIDE 69

Goals

◮ simple types (integers, booleans and functions) ◮ variables ◮ if-then-else construct ◮ compile-time rejection of ill-typed expressions

slide-70
SLIDE 70

Types

data Ty : Type where TyInt : Ty TyBool : Ty TyFun : Ty -> Ty -> Ty

slide-71
SLIDE 71

Types

data Ty : Type where TyInt : Ty TyBool : Ty TyFun : Ty -> Ty -> Ty data HasType : Fin n -> Vect n Ty -> Ty -> Type where Stop : HasType FZ (t :: _) t Pop : HasType i G t -> HasType (FS i) (_ :: G) t

slide-72
SLIDE 72

Types

data Ty : Type where TyInt : Ty TyBool : Ty TyFun : Ty -> Ty -> Ty data HasType : Fin n -> Vect n Ty -> Ty -> Type where Stop : HasType FZ (t :: _) t Pop : HasType i G t -> HasType (FS i) (_ :: G) t interpTy : Ty -> Type interpTy TyInt = Int interpTy TyBool = Bool interpTy (TyFun a r) = interpTy a -> interpTy r

slide-73
SLIDE 73

Expressions

data Expr : Vect n Ty -> Ty -> Type where Var : HasType i G t -> Expr G t Val : Int -> Expr G TyInt Lam : Expr (a :: G) r -> Expr G (TyFun a r) App : Expr G (TyFun a r) -> Expr G a -> Expr G r Op : (interpTy a -> interpTy b -> interpTy c)

  • > Expr G a -> Expr G b -> Expr G c

If : Expr G TyBool

  • > Lazy (Expr G t) -> Lazy (Expr G t)
  • > Expr G t
slide-74
SLIDE 74

Expression examples

plus : Expr G (TyFun TyInt (TyFun TyInt TyInt)) plus = Lam (Lam (Op (+) (Var Stop) (Var (Pop Stop))))

slide-75
SLIDE 75

Expression examples

plus : Expr G (TyFun TyInt (TyFun TyInt TyInt)) plus = Lam (Lam (Op (+) (Var Stop) (Var (Pop Stop)))) fact : Expr G (TyFun TyInt TyInt) fact = (Lam (If (Op (==) (Var Stop) (Val 0)) (Val 1) (Op (*) (App fact (Op (-) (Var Stop) (Val 1))) (Var Stop))))

slide-76
SLIDE 76

Environment

data Env : Vect n Ty -> Type where Nil : Env Nil (::) : interpTy t -> Env G -> Env (t :: G)

slide-77
SLIDE 77

Environment

data Env : Vect n Ty -> Type where Nil : Env Nil (::) : interpTy t -> Env G -> Env (t :: G) lookup : HasType i G t -> Env G -> interpTy t lookup Stop (t :: _) = t lookup (Pop i) (_ :: ts) = lookup i ts

slide-78
SLIDE 78

Interpreter

interp : Env G -> Expr G t -> interpTy t interp env (Var x) = lookup x env interp env (Val c) = c interp env (Lam e) = \a => interp (a :: env) e interp env (App f e) = (interp env f) (interp env e) interp env (Op f l r) = f (interp env l) (interp env r) interp env (If c t f) = if interp env c then interp env t else interp env f

slide-79
SLIDE 79

Interpreter

interp : Env G -> Expr G t -> interpTy t interp env (Var x) = lookup x env interp env (Val c) = c interp env (Lam e) = \a => interp (a :: env) e interp env (App f e) = (interp env f) (interp env e) interp env (Op f l r) = f (interp env l) (interp env r) interp env (If c t f) = if interp env c then interp env t else interp env f interp’ : Expr [] t -> interpTy t interp’ = interp []

slide-80
SLIDE 80

Theorem proving

slide-81
SLIDE 81

Equality

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

slide-82
SLIDE 82

Equality

data (=) : a -> b -> Type where Refl : x = x fiveIsFive : 5 = 5 fiveIsFive = Refl

slide-83
SLIDE 83

Equality

data (=) : a -> b -> Type where Refl : x = x fiveIsFive : 5 = 5 fiveIsFive = Refl twoTwosIsFour : 2 + 2 = 4 twoTwosIsFour = Refl

slide-84
SLIDE 84

Equality

data (=) : a -> b -> Type where Refl : x = x fiveIsFive : 5 = 5 fiveIsFive = Refl twoTwosIsFour : 2 + 2 = 4 twoTwosIsFour = Refl eqVectLength : (xs : Vect n a) -> (ys : Vect m a)

  • > (xs = ys) -> n = m

eqVectLength _ _ Refl = Refl

slide-85
SLIDE 85

Induction

cong : {f : t -> u} -> (a = b) -> f a = f b cong Refl = Refl

slide-86
SLIDE 86

Induction

cong : {f : t -> u} -> (a = b) -> f a = f b cong Refl = Refl plusReducesS : (n : Nat) -> (m : Nat)

  • > S (n + m) = n + (S m)

plusReducesS Z m = Refl plusReducesS (S n) m = cong (plusReducesS n m)

slide-87
SLIDE 87

Impossible

Usually, when particular pattern match is not possible we just omit the clause. However, sometimes it is useful (as we will see in a moment) to mark it explicitly - Idris provides us with impossible keyword for such cases.

slide-88
SLIDE 88

Impossible

Usually, when particular pattern match is not possible we just omit the clause. However, sometimes it is useful (as we will see in a moment) to mark it explicitly - Idris provides us with impossible keyword for such cases. foo : (n : Nat) -> Vect n a -> Nat foo Z (_::_) impossible foo (S _) [] impossible foo n _ = n

slide-89
SLIDE 89

Negation

data Void

slide-90
SLIDE 90

Negation

data Void Having ⊥ everything is possible: void : Void -> a

slide-91
SLIDE 91

Negation

data Void Having ⊥ everything is possible: void : Void -> a Not : Type -> Type Not a = a -> Void

slide-92
SLIDE 92

Negation

data Void Having ⊥ everything is possible: void : Void -> a Not : Type -> Type Not a = a -> Void data IsZero : Nat -> Type where Zero : IsZero Z succNonZero : (n : Nat) -> IsZero (S n) -> Void succNonZero _ Zero impossible

slide-93
SLIDE 93

Negation

data Void Having ⊥ everything is possible: void : Void -> a Not : Type -> Type Not a = a -> Void data IsZero : Nat -> Type where Zero : IsZero Z succNonZero : (n : Nat) -> Not (IsZero (S n)) succNonZero _ Zero impossible

slide-94
SLIDE 94

Decidability

data Dec : Type -> Type where Yes : {A : Type} -> A -> Dec A No : {A : Type} -> Not A -> Dec A

slide-95
SLIDE 95

Decidability

data Dec : Type -> Type where Yes : {A : Type} -> A -> Dec A No : {A : Type} -> Not A -> Dec A decIsZero : (n : Nat) -> Dec (IsZero n) decIsZero Z = Yes Zero decIsZero (S n) = No (succNonZero n)

slide-96
SLIDE 96

Interactive proving

Idris has many facilities to help with theorem proving:

◮ good editor suport (Emacs and Vim) ◮ set of builtin syntax rules ◮ tactics language

slide-97
SLIDE 97

Dependently typed lambda calculus

slide-98
SLIDE 98

Syntax

e, T ::= x | λx.e | e1e2 | (x : T1) → T2 | T where e stands for expressions, T for types (they are the same thing but are distinguished here for clarity), ∗ → ∗ for functional type and T for type of types.

slide-99
SLIDE 99

Syntax

e, T ::= x | λx.e | e1e2 | (x : T1) → T2 | T where e stands for expressions, T for types (they are the same thing but are distinguished here for clarity), ∗ → ∗ for functional type and T for type of types. Having something like type of types is bad but makes everything simple.

slide-100
SLIDE 100

Typechecking rules

Γ ⊢ T : T type

slide-101
SLIDE 101

Typechecking rules

Γ ⊢ T : T type Γ, x : T ⊢ x : T var

slide-102
SLIDE 102

Typechecking rules

Γ ⊢ T : T type Γ, x : T ⊢ x : T var Γ, x : T1 ⊢ e : T2 Γ ⊢ T1 : T Γ ⊢ λx.e : (x : T1) → T2 lambda

slide-103
SLIDE 103

Typechecking rules

Γ ⊢ T : T type Γ, x : T ⊢ x : T var Γ, x : T1 ⊢ e : T2 Γ ⊢ T1 : T Γ ⊢ λx.e : (x : T1) → T2 lambda Γ ⊢ T1 : T Γ, x : T1 ⊢ T2 : T Γ ⊢ (x : T1) → T2 : T pi

slide-104
SLIDE 104

Typechecking rules

Γ ⊢ T : T type Γ, x : T ⊢ x : T var Γ, x : T1 ⊢ e : T2 Γ ⊢ T1 : T Γ ⊢ λx.e : (x : T1) → T2 lambda Γ ⊢ T1 : T Γ, x : T1 ⊢ T2 : T Γ ⊢ (x : T1) → T2 : T pi Γ ⊢ e1 : (x : T1) → T2 Γ ⊢ e2 : T1 Γ ⊢ e1e2 : T2 [x → e2] app

slide-105
SLIDE 105

Examples

id : (t : T ) → (x : t) → t id = λt.λx.x

slide-106
SLIDE 106

Examples

id : (t : T ) → (x : t) → t id = λt.λx.x idid : (t : T ) → (x : t) → t idid = id ((t : T ) → (x : t) → t) id

slide-107
SLIDE 107

Examples

bool : T bool = (t : T ) → (bt : t) → (bf : t) → t

slide-108
SLIDE 108

Examples

bool : T bool = (t : T ) → (bt : t) → (bf : t) → t true : bool true = λt.λbt.λbf .bt false : bool false = λt.λbt.λbf .bf

slide-109
SLIDE 109

Examples

bool : T bool = (t : T ) → (bt : t) → (bf : t) → t true : bool true = λt.λbt.λbf .bt false : bool false = λt.λbt.λbf .bf if : bool → (t : T) → (bt : t) → (bf : t) → t if = λb.b

slide-110
SLIDE 110

References

◮ Idris language

◮ tutorial: http://docs.idris-lang.org/en/latest/

tutorial/index.html

◮ FAQ: https://github.com/idris-lang/Idris-dev/wiki/

Unofficial-FAQ

◮ “Pi-Forall: How to use and implement a dependently-typed

language”, Stephanie Weirich (Compose Conference)

◮ video: https://www.youtube.com/watch?v=6klfKLBnz9k ◮ notes: https://github.com/sweirich/pi-forall

◮ “Idris: Practical Dependent Types with Practical Examples”,

Brian McKenna (Strange Loop 2014)

◮ video: https://www.youtube.com/watch?v=4i7KrG1Afbk