Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modern Haskell: making sense of the type system

D2ab8b4fe9ddebc6774777e5f4549304?s=47 ryan lemmer
November 20, 2017

Modern Haskell: making sense of the type system

FuConf 2017 - Bangalore (Haskell, Functional Programming, Dependently Typed Programming)

D2ab8b4fe9ddebc6774777e5f4549304?s=128

ryan lemmer

November 20, 2017
Tweet

Transcript

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

    2017
  2. None
  3. 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
  4. 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)
  5. What Classifies Types?

  6. KINDS TYPES TERMS Maybe * Show (Maybe Int) * *

    Constraint Show Int [Char] Int Maybe Char [‘a’,’b’,’c’] 2 Just ‘a’ Show Maybe Maybe Maybe
  7. 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”
  8. KINDS TYPES TERMS Vect n a Nil Cons x (Vect

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

    n a) * Zero Succ n GADT
  10. 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
  11. Type Functions Add :: * -> * -> * Add

    Zero Zero Add (Succ Zero) (Succ Zero) not “kind-safe” Add Int Bool Add Zero (Succ Bool)
  12. 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
  13. KINDS TYPES TERMS Vect n a Nil Cons x (Vect

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

    n a) * Zero Succ n Custom Kinds? Nat
  15. KINDS TYPES TERMS Succ n Zero Nat * ‘Succ n

    ‘Zero Nat Bool False True ‘False ‘True Bool Type Promotion
  16. KINDS TYPES TERMS Vect n a Nil Cons x (Vect

    n a) Add::Nat -> Nat -> Nat Zero Succ n Kind-safety! Nat
  17. 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
  18. KINDS TYPES TERMS SNat (n::Nat) SZero SSucc Zero Succ n

    Hacking it with Singletons Nat
  19. 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
  20. 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
  21. 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]
  22. 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)
  23. 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
  24. Totality • Type Level Functions => Type Checking involves running

    programs • may not terminate (Halting problem at compile time!) • Totality => type check WILL halt TOTALITY
  25. • 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
  26. 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
  27. the beginning is NEAR!