Slide 1

Slide 1 text

Modern Haskell Making sense of the Type System @ryanlemmer Nov, 2017

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

TYPES TERMS ‘a’ [‘a’,’b’,’c’] [10,11,12] 2 3 [Char] Char [Int] Maybe Char Just ‘a’ Int 4 ‘b’ ‘c’ Types classify Terms Nothing

Slide 4

Slide 4 text

TYPES TERMS x = 1 Types NOT 1st class x = “hello” x = String if x == 2 … if x == String … f x = “hey” f x = Int f String = “string” (NOT ALLOWED)

Slide 5

Slide 5 text

What Classifies Types?

Slide 6

Slide 6 text

KINDS TYPES TERMS Maybe * Show (Maybe Int) * * Constraint Show Int [Char] Int Maybe Char [‘a’,’b’,’c’] 2 Just ‘a’ Show Maybe Maybe Maybe

Slide 7

Slide 7 text

KINDS TYPES Maybe * Show (Maybe Int) * * Constraint Show Int [Char] Int Maybe Char Show Maybe Maybe Maybe The Haskell 98 Kind-system is * “untyped” * not “kind-safe” * only “Arity-safe”

Slide 8

Slide 8 text

KINDS TYPES TERMS Vect n a Nil Cons x (Vect n a) * Zero Succ n ADT

Slide 9

Slide 9 text

KINDS TYPES TERMS Vect n a Nil Cons x (Vect n a) * Zero Succ n GADT

Slide 10

Slide 10 text

Type Functions Add :: ? -> ? -> ? Add Zero b = b Add (Succ n) b = Succ (Add n b) append :: Vect n a -> Vect m a -> Vect (Add n m) a

Slide 11

Slide 11 text

Type Functions Add :: * -> * -> * Add Zero Zero Add (Succ Zero) (Succ Zero) not “kind-safe” Add Int Bool Add Zero (Succ Bool)

Slide 12

Slide 12 text

Type Functions FAMILIES Add Zero (Succ n) = Succ n Add Zero (Succ n) ~ Succ n “Equality Axiom” Type Family instance …compiles to There is no “type computation”! (only solving of equality constraints) Add Int Bool ~ Add Int Bool

Slide 13

Slide 13 text

KINDS TYPES TERMS Vect n a Nil Cons x (Vect n a) * Zero Succ n GADT

Slide 14

Slide 14 text

KINDS TYPES TERMS Vect n a Nil Cons x (Vect n a) * Zero Succ n Custom Kinds? Nat

Slide 15

Slide 15 text

KINDS TYPES TERMS Succ n Zero Nat * ‘Succ n ‘Zero Nat Bool False True ‘False ‘True Bool Type Promotion

Slide 16

Slide 16 text

KINDS TYPES TERMS Vect n a Nil Cons x (Vect n a) Add::Nat -> Nat -> Nat Zero Succ n Kind-safety! Nat

Slide 17

Slide 17 text

still non-dependent lengthV :: Vect n a -> n lengthV :: Vect n a -> Nat replicateV :: n -> a -> Vect n a replicateV :: Nat -> a -> Vect n a

Slide 18

Slide 18 text

KINDS TYPES TERMS SNat (n::Nat) SZero SSucc Zero Succ n Hacking it with Singletons Nat

Slide 19

Slide 19 text

Faking it gets messy… lengthV :: Vect n a -> n lengthV {n} _ = n lengthV :: forall a n. SingI n => Vect n a -> Sing n lengthV _ = (sing :: SNat n) HASKELL 9.* ? HASKELL

Slide 20

Slide 20 text

Why bother? Increased Precision append :: List a -> List a -> List a append :: Vect n a -> Vect m a -> Vect (n + m) a index :: n -> List a -> a index :: Upto n -> Vect n a -> a transpose :: Matrix a -> Matrix a transpose :: Matrix m n a -> Matrix n m a take :: Nat -> List a -> List a take :: (n::Nat) -> Vect (n + m) a -> Vect m a

Slide 21

Slide 21 text

Why bother? Flexible Type Signatures printf "Name=%s" :: String -> String printf "Name=%s, Age=%d" :: String -> Int -> String zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]

Slide 22

Slide 22 text

IDRIS! data Nat = Zero | Succ Nat add :: Nat -> Nat -> Nat add Zero n = Zero add (Succ n) m = Succ (add n m) length :: Vect n a -> n length {n} _ = n replicate :: n -> a -> Vect n a replicate Zero _ = Nil replicate (Succ n) x = Cons x (replicate n x)

Slide 23

Slide 23 text

Fully Dependently Typed Languages • 1984 Coq - interactive theorem prover, TOTAL • 1999/2007 AGDA - Haskell like, TOTAL • 2008/2013 IDRIS - Haskell like, TOTALity not enforced, proof assistant, “type driven” • F*, … FULLY Dependently Typed

Slide 24

Slide 24 text

Totality • Type Level Functions => Type Checking involves running programs • may not terminate (Halting problem at compile time!) • Totality => type check WILL halt TOTALITY

Slide 25

Slide 25 text

• Curry-Howard Correspondence • TERMS level ~ TYPE level • a PROGRAM is a PROOF of its Type (a THEOREM) • TESTS => PRESENCE of errors, PROOFS show ABSENCE PROGRAM ~ PROOF

Slide 26

Slide 26 text

1998 2010 you are here! 2020 2030 Haskell 98 Functional Dependencies GADTs Type-class Type functions Type Families Haskell 2010 Kind Polymorphism, Type Promotion Type Promotion ++, Singletons Dependent Haskell: Eisenberg/Weirich GHC Dependent Haskell? Static Typing and Dependent Typing become synonymous? 2017

Slide 27

Slide 27 text

the beginning is NEAR!