type theo pe theoris rists ts ha hate him him
play

Type Theo pe Theoris rists ts HA HATE Him Him! Learn this ONE - PowerPoint PPT Presentation

Type Theo pe Theoris rists ts HA HATE Him Him! Learn this ONE WEIRD TRICK to fake dependent types in a language that doesnt support them LEARN THE TRUTH NOW Ryan Scott rgscott@indiana.edu github.com/RyanGlScott September 1, 2017


  1. Type Theo pe Theoris rists ts HA HATE Him Him! Learn this ONE WEIRD TRICK to fake dependent types in a language that doesn’t support them LEARN THE TRUTH NOW Ryan Scott rgscott@indiana.edu github.com/RyanGlScott September 1, 2017

  2. Depende Dependent types t types

  3. Depende Dependent types t types.. ... . in in Idris is mkSingle : (x : Bool) -> if x then Nat else List Nat mkSingle True = 0 mkSingle False = []

  4. Depende Dependent types t types.. ... . in in Hask Haskell ll? mkSingle :: (x :: Bool) -> if x then Nat else [Nat] mkSingle True = 0 mkSingle False = []

  5. Depende Dependent types t types.. ... . in in Hask Haskell ll? mkSingle :: (x :: Bool) -> if x then Nat else [Nat] mkSingle True = 0 mkSingle False = [] <interactive>:1:28: error: parse error on input ‘if’ :(

  6. Ther here’s hope y e’s hope yet λ Haskell can support dependent types, if you’re willing to squint λ We’ll need to enable a modest number of GHC extensions

  7. Ther here’s hope y e’s hope yet λ Haskell can support dependent types, if you’re willing to squint λ We’ll need to enable a modest number of GHC extensions {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE EmptyCase #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE InstanceSigs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeInType #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-}

  8. Si Singl gleton types on types data family Sing :: k -> Type

  9. Si Singl gleton types on types data family Sing :: k -> Type data instance Sing :: Bool -> Type where SFalse :: Sing False STrue :: Sing True

  10. Si Singl gleton types on types data family Sing :: k -> Type data instance Sing :: Bool -> Type where SFalse :: Sing False STrue :: Sing True

  11. Si Singl gleton types on types data family Sing :: k -> Type data instance Sing :: Bool -> Type where SFalse :: Sing False STrue :: Sing True

  12. Si Singl gleton types on types data family Sing :: k -> Type data instance Sing :: Bool -> Type where SFalse :: Sing False STrue :: Sing True data instance Sing (z :: Bool) = (z ~ False) => SFalse | (z ~ True) => STrue

  13. Si Singl gleton types on types data family Sing :: k -> Type data instance Sing :: Bool -> Type where SFalse :: Sing False STrue :: Sing True data instance Sing (z :: Bool) = (z ~ False) => SFalse | (z ~ True) => STrue

  14. Si Singl gleton types on types data family Sing :: k -> Type data Nat = Z | S Nat data instance Sing :: Nat -> Type where SZ :: Sing Z SS :: Sing (n :: Nat) -> Sing (S n)

  15. Si Singl gleton types on types data family Sing :: k -> Type data Nat = Z | S Nat data instance Sing :: Nat -> Type where SZ :: Sing Z SS :: Sing (n :: Nat) -> Sing (S n) data instance Sing (z :: Nat) = (z ~ Z) => SZ | forall (n :: Nat). (z ~ S n) => SS n

  16. Depende Dependent types t types.. ... . in in Idris (r is (redu edux) mkSingle : (x : Bool) -> if x then Nat else List Nat mkSingle True = 0 mkSingle False = []

  17. Depende Dependent types t types.. ... . in in Hask Haskell ll? (He (Heck ck yeah!) eah!) type family If (c :: Bool) (t :: k) (f :: k) :: k where If True t f = t If False t f = f

  18. Depende Dependent types t types.. ... . in in Hask Haskell ll? (He (Heck ck yeah!) eah!) type family If (c :: Bool) (t :: k) (f :: k) :: k where If True t f = t If False t f = f mkSingle :: Sing (x :: Bool) -> If x Nat [Nat] mkSingle STrue = 0 mkSingle SFalse = []

  19. Depende Dependent types t types.. ... . in in Hask Haskell ll? (He (Heck ck yeah!) eah!) type Π = Sing

  20. Depende Dependent types t types.. ... . in in Hask Haskell ll? (He (Heck ck yeah!) eah!) type Π = Sing mkSingle :: Π (x :: Bool) -> If x Nat [Nat] mkSingle STrue = 0 mkSingle SFalse = []

  21. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching

  22. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Idris data Vect : Nat -> Type -> Type where Nil : Vect Z a Cons : a -> Vect n a -> Vect (S n) a

  23. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Idris data Vect : Nat -> Type -> Type where Nil : Vect Z a Cons : a -> Vect n a -> Vect (S n) a len : Vect n a -> Nat len Nil = 0 len (Cons x xs) = 1 + len xs

  24. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Idris data Vect : Nat -> Type -> Type where Nil : Vect Z a Cons : a -> Vect n a -> Vect (S n) a len : {n : Nat} -> Vect n a -> Nat len {n=Z} Nil = 0 len {n=S k} (Cons x xs) = 1 + len xs

  25. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Haskell!

  26. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Haskell! data Vect :: Nat -> Type -> Type where Nil :: Vect Z a Cons :: a -> Vect n a -> Vect (S n) a

  27. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Haskell! data Vect :: Nat -> Type -> Type where Nil :: Vect Z a Cons :: a -> Vect n a -> Vect (S n) a len :: Sing (n :: Nat) -> Vect n a -> Nat len SZ Nil = 0 len (SS k) (Cons x xs) = 1 + len xs

  28. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Haskell! class SingI (a :: k) where sing :: Sing (a :: k) instance SingI Z where sing = SZ instance SingI n => SingI (S n) where sing = SS sing

  29. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Haskell! len :: Sing (n :: Nat) -> Vect n a -> Nat len SZ Nil = 0 len (SS k) (Cons x xs) = 1 + len xs

  30. Wh What e else e can w can we fak ake e emul ulat ate? λ Dependent pattern matching... in Haskell! len :: Sing (n :: Nat) -> Vect n a -> Nat len SZ Nil = 0 len (SS k) (Cons x xs) = 1 + len xs len’ :: forall (n :: Nat). SingI n => Vect n a -> Nat len’ = len (sing :: n)

  31. λ With enough elbow grease, one can simulate a great deal of dependently typed code λ Impress your friends at the bar! Be the envy of your family! λ http://hackage.haskell.org/package/singletons An Any ques y questio tions? s?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend