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
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Type Theo pe Theoris rists ts HA HATE Him Him!

Ryan Scott

rgscott@indiana.edu github.com/RyanGlScott

September 1, 2017

Learn this ONE WEIRD TRICK to fake dependent types in a language that doesn’t support them

LEARN THE TRUTH NOW

slide-2
SLIDE 2

Depende Dependent types t types

slide-3
SLIDE 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 = []

slide-4
SLIDE 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 = []

slide-5
SLIDE 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’

:(

slide-6
SLIDE 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

slide-7
SLIDE 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 #-}

slide-8
SLIDE 8

Si Singl gleton types

  • n types

data family Sing :: k -> Type

slide-9
SLIDE 9

Si Singl gleton types

  • n types

data family Sing :: k -> Type

data instance Sing :: Bool -> Type where SFalse :: Sing False STrue :: Sing True

slide-10
SLIDE 10

Si Singl gleton types

  • n types

data family Sing :: k -> Type

data instance Sing :: Bool -> Type where SFalse :: Sing False STrue :: Sing True

slide-11
SLIDE 11

Si Singl gleton types

  • n types

data family Sing :: k -> Type

data instance Sing :: Bool -> Type where SFalse :: Sing False STrue :: Sing True

slide-12
SLIDE 12

Si Singl gleton types

  • n 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

slide-13
SLIDE 13

Si Singl gleton types

  • n 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

slide-14
SLIDE 14

Si Singl gleton types

  • n types

data family Sing :: k -> Type

data instance Sing :: Nat -> Type where SZ :: Sing Z SS :: Sing (n :: Nat) -> Sing (S n) data Nat = Z | S Nat

slide-15
SLIDE 15

Si Singl gleton types

  • n types

data family Sing :: k -> Type

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 data Nat = Z | S Nat

slide-16
SLIDE 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 = []

slide-17
SLIDE 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

slide-18
SLIDE 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 = []

slide-19
SLIDE 19

Depende Dependent types t types.. ... . in in Hask Haskell ll? (He (Heck ck yeah!) eah!)

type Π = Sing

slide-20
SLIDE 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 = []

slide-21
SLIDE 21

Wh What e else e can w can we fak ake e emul ulat ate?

λ Dependent pattern matching

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 25

Wh What e else e can w can we fak ake e emul ulat ate?

λ Dependent pattern matching... in Haskell!

slide-26
SLIDE 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

slide-27
SLIDE 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

slide-28
SLIDE 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

slide-29
SLIDE 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

slide-30
SLIDE 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)

slide-31
SLIDE 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?