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

Modern Haskell: making sense of the type system

ryan lemmer
November 20, 2017

Modern Haskell: making sense of the type system

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

ryan lemmer

November 20, 2017
Tweet

More Decks by ryan lemmer

Other Decks in Programming

Transcript

  1. 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
  2. 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)
  3. KINDS TYPES TERMS Maybe * Show (Maybe Int) * *

    Constraint Show Int [Char] Int Maybe Char [‘a’,’b’,’c’] 2 Just ‘a’ Show Maybe Maybe Maybe
  4. 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”
  5. 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
  6. Type Functions Add :: * -> * -> * Add

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

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

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

    n a) Add::Nat -> Nat -> Nat Zero Succ n Kind-safety! Nat
  11. 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
  12. 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
  13. 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
  14. 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]
  15. 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)
  16. 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
  17. Totality • Type Level Functions => Type Checking involves running

    programs • may not terminate (Halting problem at compile time!) • Totality => type check WILL halt TOTALITY
  18. • 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
  19. 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