galois tech talk / vinyl: records in haskell and type theory

136
Vinyl: Records in Haskell and Type Theory Jon Sterling jonmsterling.com August 12, 2014

Upload: jonsterling

Post on 28-May-2015

927 views

Category:

Technology


1 download

DESCRIPTION

Records in Haskell are notoriously difficult to compose; many solutions have been proposed. Vinyl lies in the space of library-level approaches, and addresses polymorphism, extensibility, effects and strictness. I describe how Vinyl approaches record families over arbitrary key spaces using a Tarski universe construction, as well as a method for immersing each field of a record in a chosen effect modality. Also discussed are presheaves, sheaves and containers.

TRANSCRIPT

Page 1: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: Records in Haskelland Type Theory

Jon Sterlingjonmsterling.com

August 12, 2014

Page 2: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records in GHC 7.8

I Haskell records are nominally typedI They may not share field names

data R = R { x :: X }data R’ = R’ { x :: X } −− ˆ Error

Page 3: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records in GHC 7.8

I Haskell records are nominally typed

I They may not share field names

data R = R { x :: X }data R’ = R’ { x :: X } −− ˆ Error

Page 4: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records in GHC 7.8

I Haskell records are nominally typedI They may not share field names

data R = R { x :: X }data R’ = R’ { x :: X } −− ˆ Error

Page 5: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records in GHC 7.8

I Haskell records are nominally typedI They may not share field names

data R = R { x :: X }

data R’ = R’ { x :: X } −− ˆ Error

Page 6: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records in GHC 7.8

I Haskell records are nominally typedI They may not share field names

data R = R { x :: X }data R’ = R’ { x :: X } −− ˆ Error

Page 7: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Structural typing

I Sharing field names and accessorsI Record types may be characterized structurally

Page 8: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Structural typing

I Sharing field names and accessors

I Record types may be characterized structurally

Page 9: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Structural typing

I Sharing field names and accessorsI Record types may be characterized structurally

Page 10: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Row polymorphism

How do we express the type of a function which adds afield to a record?

Page 11: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Row polymorphism

How do we express the type of a function which adds afield to a record?

Page 12: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Row polymorphism

How do we express the type of a function which adds afield to a record?

x : {foo : A}f(x) : {foo : A, bar : B}

Page 13: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Row polymorphism

How do we express the type of a function which adds afield to a record?

x : {foo : A; ~rs}f(x) : {foo : A, bar : B; ~rs}

Page 14: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 15: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 16: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ where

RNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 17: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[]

(:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 18: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 19: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])

class ss ⊆ (rs :: [∗]) wherecast :: Rec rs→ Rec ss

(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 20: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss

(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 21: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss(=:) : s ::: t→ t→ Rec ’[s ::: t]

(⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 22: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)

lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 23: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

data (s :: Symbol) ::: (t :: ∗) = Field

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

class s ∈ (rs :: [∗])class ss ⊆ (rs :: [∗]) where

cast :: Rec rs→ Rec ss(=:) : s ::: t→ t→ Rec ’[s ::: t](⊕) : Rec ss→ Rec ts→ Rec (ss ++ ts)lens : s ::: t ∈ rs⇒ s ::: t→ Lens’ (Rec rs) t

Page 24: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

f :: Rec (”foo” ::: A ’: rs)→ Rec (”bar” ::: B ’: ”foo” ::: A ’: rs)

Page 25: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Roll your own in Haskell

f :: Rec (”foo” ::: A ’: rs)→ Rec (”bar” ::: B ’: ”foo” ::: A ’: rs)

Page 26: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Why be stringly typed?

I Let’s generalize our key space

Page 27: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Why be stringly typed?

I Let’s generalize our key space

Page 28: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Old

We relied on a single representation for keys as pairs ofstrings and types.

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

Page 29: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Proposed

We put the keys in an arbitrary type (kind) and describetheir semantics with a function el (for elements).

data Rec :: [∗]→ ∗ whereRNil :: Rec ’[](:&) :: !t→ !(Rec rs)→ Rec ((s ::: t) ’: rs)

Page 30: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Proposed

We put the keys in an arbitrary type (kind) and describetheir semantics with a function el (for elements).

data Rec :: (el :: k→ ∗)→ (rs :: [k])→ ∗ whereRNil :: Rec el ’[](:&) :: ∀(r :: k) (rs’:: [k]). !(el r)→ !(Rec el rs)→ Rec (r ’: rs’)

Page 31: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Proposed

We put the keys in an arbitrary type (kind) and describetheir semantics with a function el (for elements).

data Rec :: (k→ ∗)→ [k]→ ∗ whereRNil :: Rec el ’[](:&) :: !(el r)→ !(Rec el rs)→ Rec (r ’: rs)

Page 32: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Actual

Type families are not functions, but in many cases cansimulate them using Richard Eisenberg’s techniqueoutlined in Defunctionalization for the win.

data TyFun :: ∗ → ∗ → ∗

type family (f :: TyFun k l→ ∗) $ (x :: k) :: l

data Rec :: (TyFun k ∗ → ∗)→ [k]→ ∗ whereRNil :: Rec el ’[](:&) :: !(el $ r)→ !(Rec el rs)→ Rec el (r ’: rs)

Page 33: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Actual

Type families are not functions, but in many cases cansimulate them using Richard Eisenberg’s techniqueoutlined in Defunctionalization for the win.

data TyFun :: ∗ → ∗ → ∗type family (f :: TyFun k l→ ∗) $ (x :: k) :: l

data Rec :: (TyFun k ∗ → ∗)→ [k]→ ∗ whereRNil :: Rec el ’[](:&) :: !(el $ r)→ !(Rec el rs)→ Rec el (r ’: rs)

Page 34: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Actual

Type families are not functions, but in many cases cansimulate them using Richard Eisenberg’s techniqueoutlined in Defunctionalization for the win.

data TyFun :: ∗ → ∗ → ∗type family (f :: TyFun k l→ ∗) $ (x :: k) :: l

data Rec :: (TyFun k ∗ → ∗)→ [k]→ ∗ whereRNil :: Rec el ’[](:&) :: !(el $ r)→ !(Rec el rs)→ Rec el (r ’: rs)

Page 35: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email Label

data SLabel :: Label→ ∗ whereSHome :: SLabel HomeSWork :: SLabel Work

Page 36: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email Label

data SLabel :: Label→ ∗ whereSHome :: SLabel HomeSWork :: SLabel Work

Page 37: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email Label

data SLabel :: Label→ ∗

data SAddrKeys :: AddrKeys→ ∗ whereSName :: SAddrKeys NameSPhone :: SLabel l→ SAddrKeys (Phone l)SEmail :: SLabel l→ SAddrKeys (Email l)

Page 38: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email Label

data SLabel :: Label→ ∗data SAddrKeys :: AddrKeys→ ∗ where

SName :: SAddrKeys NameSPhone :: SLabel l→ SAddrKeys (Phone l)SEmail :: SLabel l→ SAddrKeys (Email l)

Page 39: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email Label

data SLabel :: Label→ ∗data SAddrKeys :: AddrKeys→ ∗

data ElAddr :: (TyFun AddrKeys ∗)→ ∗ whereElAddr :: ElAddr el

type instance ElAddr $ Name = Stringtype instance ElAddr $ (Phone l) = [N]type instance ElAddr $ (Email l) = String

type AddrRec rs = Rec ElAddr rs

Page 40: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email Label

data SLabel :: Label→ ∗data SAddrKeys :: AddrKeys→ ∗

data ElAddr :: (TyFun AddrKeys ∗)→ ∗ whereElAddr :: ElAddr el

type instance ElAddr $ Name = Stringtype instance ElAddr $ (Phone l) = [N]type instance ElAddr $ (Email l) = String

type AddrRec rs = Rec ElAddr rs

Page 41: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email Label

data SLabel :: Label→ ∗data SAddrKeys :: AddrKeys→ ∗

data ElAddr :: (TyFun AddrKeys ∗)→ ∗ whereElAddr :: ElAddr el

type instance ElAddr $ Name = Stringtype instance ElAddr $ (Phone l) = [N]type instance ElAddr $ (Email l) = String

type AddrRec rs = Rec ElAddr rs

Page 42: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Sugared example

import Data.Singletons as S

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email LabelS.genSingletons [ ’’Label, ’’AddrKeys ]

makeUniverse ’’AddrKeys ”ElAddr”

type instance ElAddr $ Name = Stringtype instance ElAddr $ (Phone l) = [N]type instance ElAddr $ (Email l) = String

type AddrRec rs = Rec ElAddr rs

Page 43: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Sugared example

import Data.Singletons as S

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email LabelS.genSingletons [ ’’Label, ’’AddrKeys ]

makeUniverse ’’AddrKeys ”ElAddr”

type instance ElAddr $ Name = Stringtype instance ElAddr $ (Phone l) = [N]type instance ElAddr $ (Email l) = String

type AddrRec rs = Rec ElAddr rs

Page 44: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Sugared example

import Data.Singletons as S

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email LabelS.genSingletons [ ’’Label, ’’AddrKeys ]

makeUniverse ’’AddrKeys ”ElAddr”

type instance ElAddr $ Name = Stringtype instance ElAddr $ (Phone l) = [N]type instance ElAddr $ (Email l) = String

type AddrRec rs = Rec ElAddr rs

Page 45: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Sugared example

import Data.Singletons as S

data Label = Home |Workdata AddrKeys = Name | Phone Label | Email LabelS.genSingletons [ ’’Label, ’’AddrKeys ]

makeUniverse ’’AddrKeys ”ElAddr”

type instance ElAddr $ Name = Stringtype instance ElAddr $ (Phone l) = [N]type instance ElAddr $ (Email l) = String

type AddrRec rs = Rec ElAddr rs

Page 46: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example records

bob :: AddrRec [Name, Email Work]bob = SName =: ”Robert W. Harper”⊕ SEmail SWork =: ”[email protected]

jon :: AddrRec [Name, Email Work, Email Home]jon = SName =: ”Jon M. Sterling”⊕ SEmail SWork =: ”[email protected]”⊕ SEmail SHome =: ”[email protected]

Page 47: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Example records

bob :: AddrRec [Name, Email Work]bob = SName =: ”Robert W. Harper”⊕ SEmail SWork =: ”[email protected]

jon :: AddrRec [Name, Email Work, Email Home]jon = SName =: ”Jon M. Sterling”⊕ SEmail SWork =: ”[email protected]”⊕ SEmail SHome =: ”[email protected]

Page 48: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Recovering HList

data Id :: (TyFun k k)→ ∗ wheretype instance Id $ x = x

type HList rs = Rec Id rs

ex :: HList [Z, Bool, String]ex = 34 :& True :& ”vinyl” :& RNil

Page 49: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Recovering HList

data Id :: (TyFun k k)→ ∗ wheretype instance Id $ x = x

type HList rs = Rec Id rs

ex :: HList [Z, Bool, String]ex = 34 :& True :& ”vinyl” :& RNil

Page 50: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Recovering HList

data Id :: (TyFun k k)→ ∗ wheretype instance Id $ x = x

type HList rs = Rec Id rs

ex :: HList [Z, Bool, String]ex = 34 :& True :& ”vinyl” :& RNil

Page 51: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Recovering HList

data Id :: (TyFun k k)→ ∗ wheretype instance Id $ x = x

type HList rs = Rec Id rs

ex :: HList [Z, Bool, String]ex = 34 :& True :& ”vinyl” :& RNil

Page 52: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Validating records

validateName :: String→ Either Error StringvalidateEmail :: String→ Either Error StringvalidatePhone :: [N]→ Either Error [N]

*unnnnnhhh...*

validateContact:: AddrRec [Name, Email Work]→ Either Error (AddrRec [Name, Email Work])

Page 53: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Validating records

validateName :: String→ Either Error StringvalidateEmail :: String→ Either Error StringvalidatePhone :: [N]→ Either Error [N]

*unnnnnhhh...*

validateContact:: AddrRec [Name, Email Work]→ Either Error (AddrRec [Name, Email Work])

Page 54: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Validating records

validateName :: String→ Either Error StringvalidateEmail :: String→ Either Error StringvalidatePhone :: [N]→ Either Error [N]

*unnnnnhhh...*

validateContact:: AddrRec [Name, Email Work]→ Either Error (AddrRec [Name, Email Work])

Page 55: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Welp.

Page 56: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Effects inside records

data Rec :: (TyFun k ∗ → ∗)→ [k]→ ∗ whereRNil :: Rec el ’[](:&) :: !(el $ r)→ !(Rec el rs)→ Rec el (r ’: rs)

Page 57: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Effects inside records

data Rec :: (TyFun k ∗ → ∗)→ (∗ → ∗)→ [k]→ ∗ whereRNil :: Rec el f ’[](:&) :: !(f (el $ r))→ !(Rec el f rs)→ Rec el f (r ’: rs)

Page 58: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Effects inside records

data Rec :: (TyFun k ∗ → ∗)→ (∗ → ∗)→ [k]→ ∗ whereRNil :: Rec el f ’[](:&) :: !(f (el $ r))→ !(Rec el f rs)→ Rec el f (r ’: rs)

(=:) : Applicative f⇒ sing r→ el $ r→ Rec el f ’[r]k =: x = pure x :& RNil

(⇐ \): sing r→ f (el $ r)→ Rec el f ’[r]k⇐ \ x = x :& RNil

Page 59: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Effects inside records

data Rec :: (TyFun k ∗ → ∗)→ (∗ → ∗)→ [k]→ ∗ whereRNil :: Rec el f ’[](:&) :: !(f (el $ r))→ !(Rec el f rs)→ Rec el f (r ’: rs)

(=:) : Applicative f⇒ sing r→ el $ r→ Rec el f ’[r]k =: x = pure x :& RNil

(⇐ \): sing r→ f (el $ r)→ Rec el f ’[r]k⇐ \ x = x :& RNil

Page 60: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Effects inside records

data Rec :: (TyFun k ∗ → ∗)→ (∗ → ∗)→ [k]→ ∗ whereRNil :: Rec el f ’[](:&) :: !(f (el $ r))→ !(Rec el f rs)→ Rec el f (r ’: rs)

(=:) : Applicative f⇒ sing r→ el $ r→ Rec el f ’[r]k =: x = pure x :& RNil

(⇐ \): sing r→ f (el $ r)→ Rec el f ’[r]k⇐ \ x = x :& RNil

Page 61: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

type Validator a = a→ Either Error a

Page 62: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)

Page 63: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)

validateName :: AddrRec Validator ’[Name]validatePhone :: ∀l. AddrRec Validator ’[Phone l]validateEmail :: ∀l. AddrRec Validator ’[Email l]

type TotalContact =[ Name, Email Home, Email Work, Phone Home, Phone Work ]

validateContact :: AddrRec Validator TotalContactvalidateContact = validateName

⊕ validateEmail ⊕ validateEmail⊕ validatePhone ⊕ validatePhone

Page 64: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)

validateName :: AddrRec Validator ’[Name]validatePhone :: ∀l. AddrRec Validator ’[Phone l]validateEmail :: ∀l. AddrRec Validator ’[Email l]

type TotalContact =[ Name, Email Home, Email Work, Phone Home, Phone Work ]

validateContact :: AddrRec Validator TotalContactvalidateContact = validateName

⊕ validateEmail ⊕ validateEmail⊕ validatePhone ⊕ validatePhone

Page 65: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)

validateName :: AddrRec Validator ’[Name]validatePhone :: ∀l. AddrRec Validator ’[Phone l]validateEmail :: ∀l. AddrRec Validator ’[Email l]

type TotalContact =[ Name, Email Home, Email Work, Phone Home, Phone Work ]

validateContact :: AddrRec Validator TotalContactvalidateContact = validateName

⊕ validateEmail ⊕ validateEmail⊕ validatePhone ⊕ validatePhone

Page 66: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)

validateName :: AddrRec Validator ’[Name]validatePhone :: ∀l. AddrRec Validator ’[Phone l]validateEmail :: ∀l. AddrRec Validator ’[Email l]

type TotalContact =[ Name, Email Home, Email Work, Phone Home, Phone Work ]

validateContact :: AddrRec Validator TotalContactvalidateContact = validateName

⊕ validateEmail ⊕ validateEmail⊕ validatePhone ⊕ validatePhone

Page 67: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Record effect operators

( ? ) :: Rec el (Lift (→) f g) rs→ Rec el f rs→ Rec el g rs

rtraverse :: Applicative h⇒(∀ x. f x→ h (g x))→Rec el f rs→ h (Rec el g rs)

rdist :: Applicative f⇒ Rec el f rs→ f (Rec el Identity rs)rdist = rtraverse (fmap Identity)

Page 68: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Record effect operators

( ? ) :: Rec el (Lift (→) f g) rs→ Rec el f rs→ Rec el g rs

rtraverse :: Applicative h⇒(∀ x. f x→ h (g x))→Rec el f rs→ h (Rec el g rs)

rdist :: Applicative f⇒ Rec el f rs→ f (Rec el Identity rs)rdist = rtraverse (fmap Identity)

Page 69: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Record effect operators

( ? ) :: Rec el (Lift (→) f g) rs→ Rec el f rs→ Rec el g rs

rtraverse :: Applicative h⇒(∀ x. f x→ h (g x))→Rec el f rs→ h (Rec el g rs)

rdist :: Applicative f⇒ Rec el f rs→ f (Rec el Identity rs)rdist = rtraverse (fmap Identity)

Page 70: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Record effect operators

( ? ) :: Rec el (Lift (→) f g) rs→ Rec el f rs→ Rec el g rs

rtraverse :: Applicative h⇒(∀ x. f x→ h (g x))→Rec el f rs→ h (Rec el g rs)

rdist :: Applicative f⇒ Rec el f rs→ f (Rec el Identity rs)rdist = rtraverse (fmap Identity)

Page 71: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

bob :: AddrRec [Name, Email Work]validateContact :: AddrRec Validator TotalContact

bobValid :: AddrRec (Either Error) [Name, Email Work]bobValid = cast validateContact ? bob

validBob :: Either Error (AddrRec Identity [Name, Email Work])validBob = rdist bobValid

Page 72: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

bob :: AddrRec [Name, Email Work]validateContact :: AddrRec Validator TotalContact

bobValid :: AddrRec (Either Error) [Name, Email Work]bobValid = cast validateContact ? bob

validBob :: Either Error (AddrRec Identity [Name, Email Work])validBob = rdist bobValid

Page 73: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Compositional validation

bob :: AddrRec [Name, Email Work]validateContact :: AddrRec Validator TotalContact

bobValid :: AddrRec (Either Error) [Name, Email Work]bobValid = cast validateContact ? bob

validBob :: Either Error (AddrRec Identity [Name, Email Work])validBob = rdist bobValid

Page 74: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Laziness as an effect

newtype Identity a = Identity { runIdentity :: a }data Thunk a = Thunk { unThunk :: a }

type PlainRec el rs = Rec el Identity rstype LazyRec el rs = Rec el Thunk rs

Page 75: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Laziness as an effect

newtype Identity a = Identity { runIdentity :: a }data Thunk a = Thunk { unThunk :: a }

type PlainRec el rs = Rec el Identity rstype LazyRec el rs = Rec el Thunk rs

Page 76: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Laziness as an effect

newtype Identity a = Identity { runIdentity :: a }data Thunk a = Thunk { unThunk :: a }

type PlainRec el rs = Rec el Identity rs

type LazyRec el rs = Rec el Thunk rs

Page 77: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Laziness as an effect

newtype Identity a = Identity { runIdentity :: a }data Thunk a = Thunk { unThunk :: a }

type PlainRec el rs = Rec el Identity rstype LazyRec el rs = Rec el Thunk rs

Page 78: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

fetchName :: AddrRec IO ’[Name]fetchName = SName⇐\ runDB nameQuery

fetchWorkEmail :: AddrRec IO ’[Email Work]fetchWorkEmail = SEmail SWork⇐\ runDB emailQuery

fetchBob :: AddrRec IO [Name, Email Work]fetchBob = fetchName ⊕ fetchWorkEmail

Page 79: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

fetchName :: AddrRec IO ’[Name]fetchName = SName⇐\ runDB nameQuery

fetchWorkEmail :: AddrRec IO ’[Email Work]fetchWorkEmail = SEmail SWork⇐\ runDB emailQuery

fetchBob :: AddrRec IO [Name, Email Work]fetchBob = fetchName ⊕ fetchWorkEmail

Page 80: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

fetchName :: AddrRec IO ’[Name]fetchName = SName⇐\ runDB nameQuery

fetchWorkEmail :: AddrRec IO ’[Email Work]fetchWorkEmail = SEmail SWork⇐\ runDB emailQuery

fetchBob :: AddrRec IO [Name, Email Work]fetchBob = fetchName ⊕ fetchWorkEmail

Page 81: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

fetchName :: AddrRec IO ’[Name]fetchName = SName⇐\ runDB nameQuery

fetchWorkEmail :: AddrRec IO ’[Email Work]fetchWorkEmail = SEmail SWork⇐\ runDB emailQuery

fetchBob :: AddrRec IO [Name, Email Work]fetchBob = fetchName ⊕ fetchWorkEmail

Page 82: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

newtype Concurrently a= Concurrently { runConcurrently :: IO a }

( $ ) :: (∀ a. f a→ g a)→ Rec el f rs→ Rec el g rs

bobConcurrently :: Rec el Concurrently [Name, Email Work]bobConcurrently = Concurrently $ fetchBob

concurrentBob :: IO (PlainRec el [Name, Email Work])concurrentBob = runConcurrently (rdist bobConcurrently)

Page 83: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

newtype Concurrently a= Concurrently { runConcurrently :: IO a }

( $ ) :: (∀ a. f a→ g a)→ Rec el f rs→ Rec el g rs

bobConcurrently :: Rec el Concurrently [Name, Email Work]bobConcurrently = Concurrently $ fetchBob

concurrentBob :: IO (PlainRec el [Name, Email Work])concurrentBob = runConcurrently (rdist bobConcurrently)

Page 84: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

newtype Concurrently a= Concurrently { runConcurrently :: IO a }

( $ ) :: (∀ a. f a→ g a)→ Rec el f rs→ Rec el g rs

bobConcurrently :: Rec el Concurrently [Name, Email Work]bobConcurrently = Concurrently $ fetchBob

concurrentBob :: IO (PlainRec el [Name, Email Work])concurrentBob = runConcurrently (rdist bobConcurrently)

Page 85: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

newtype Concurrently a= Concurrently { runConcurrently :: IO a }

( $ ) :: (∀ a. f a→ g a)→ Rec el f rs→ Rec el g rs

bobConcurrently :: Rec el Concurrently [Name, Email Work]bobConcurrently = Concurrently $ fetchBob

concurrentBob :: IO (PlainRec el [Name, Email Work])concurrentBob = runConcurrently (rdist bobConcurrently)

Page 86: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Concurrent records with Async

newtype Concurrently a= Concurrently { runConcurrently :: IO a }

( $ ) :: (∀ a. f a→ g a)→ Rec el f rs→ Rec el g rs

bobConcurrently :: Rec el Concurrently [Name, Email Work]bobConcurrently = Concurrently $ fetchBob

concurrentBob :: IO (PlainRec el [Name, Email Work])concurrentBob = runConcurrently (rdist bobConcurrently)

Page 87: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Type Theoretic Semantics for Records

Page 88: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Universes a la Tarski

I A type U of codes for types.I Function ElU : U → Type.

Γ ` s : UΓ ` ElU(s) : Type

Page 89: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Universes a la Tarski

I A type U of codes for types.

I Function ElU : U → Type.

Γ ` s : UΓ ` ElU(s) : Type

Page 90: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Universes a la Tarski

I A type U of codes for types.I Function ElU : U → Type.

Γ ` s : UΓ ` ElU(s) : Type

Page 91: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Universes a la Tarski

I A type U of codes for types.I Function ElU : U → Type.

Γ ` s : UΓ ` ElU(s) : Type

Page 92: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Universes a la Tarski

Type

Page 93: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Universes a la Tarski

Type

Page 94: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Universes a la Tarski

Type

Page 95: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Universes a la Tarski

Type

Page 96: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records as products

Page 97: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records as products

Records: the product of the image of ElU in Typerestricted to a subset of U .

Page 98: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records as products

Type

Page 99: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records as products

Type

×× ×

×

Page 100: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

Records and corecords are finite products and sumsrespectively.

Rec(ElU ;F ; rs) :≡∏U|rs

F ◦ ElU

CoRec(ElU ;F ; rs) :≡∑U|rs

F ◦ ElU

Data(ElU ;F ; rs) :≡WU|rs

F ◦ ElU

Codata(ElU ;F ; rs) :≡MU|rs

F ◦ ElU

Page 101: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

Records and corecords are finite products and sumsrespectively.

Rec(ElU ;F ; rs) :≡∏U|rs

F ◦ ElU

CoRec(ElU ;F ; rs) :≡∑U|rs

F ◦ ElU

Data(ElU ;F ; rs) :≡WU|rs

F ◦ ElU

Codata(ElU ;F ; rs) :≡MU|rs

F ◦ ElU

Page 102: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

Records and corecords are finite products and sumsrespectively.

Rec(ElU ;F ; rs) :≡∏U|rs

F ◦ ElU

CoRec(ElU ;F ; rs) :≡∑U|rs

F ◦ ElU

Data(ElU ;F ; rs) :≡WU|rs

F ◦ ElU

Codata(ElU ;F ; rs) :≡MU|rs

F ◦ ElU

Page 103: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

Records and corecords are finite products and sumsrespectively.

Rec(ElU ;F ; rs) :≡∏U|rs

F ◦ ElU

CoRec(ElU ;F ; rs) :≡∑U|rs

F ◦ ElU

Data(ElU ;F ; rs) :≡WU|rs

F ◦ ElU

Codata(ElU ;F ; rs) :≡MU|rs

F ◦ ElU

Page 104: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

Records and corecords are finite products and sumsrespectively.

Rec(ElU ;F ; rs) :≡∏U|rs

F ◦ ElU

CoRec(ElU ;F ; rs) :≡∑U|rs

F ◦ ElU

Data(ElU ;F ; rs) :≡WU|rs

F ◦ ElU

Codata(ElU ;F ; rs) :≡MU|rs

F ◦ ElU

Page 105: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

Records and corecords are finite products and sumsrespectively.

Rec(ElU ;F ; rs) :≡∏U|rs

F ◦ ElU

CoRec(ElU ;F ; rs) :≡∑U|rs

F ◦ ElU

Data(ElU ;F ; rs) :≡WU|rs

F ◦ ElU

Codata(ElU ;F ; rs) :≡MU|rs

F ◦ ElU

Page 106: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

(U|rs) / (F ◦ ElU)

Page 107: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

J(U|rs) / (F ◦ ElU)KΠ

Page 108: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

J(U|rs) / (F ◦ ElU)KW

Page 109: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Vinyl: beyond records?

J(U|rs) / (F ◦ ElU)KM

Page 110: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Presheaves

A presheaf on some space X is a functorO(X)op → Type, where O is the category of open sets ofX for whatever topology you have chosen.

Page 111: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Presheaves

A presheaf on some space X is a functorO(X)op → Type, where O is the category of open sets ofX for whatever topology you have chosen.

Page 112: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Topologies on some space X

I What are the open sets on X?I The empty set and X are open setsI The union of open sets is openI Finite intersections of open sets are open

Page 113: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Topologies on some space X

I What are the open sets on X?

I The empty set and X are open setsI The union of open sets is openI Finite intersections of open sets are open

Page 114: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Topologies on some space X

I What are the open sets on X?I The empty set and X are open sets

I The union of open sets is openI Finite intersections of open sets are open

Page 115: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Topologies on some space X

I What are the open sets on X?I The empty set and X are open setsI The union of open sets is open

I Finite intersections of open sets are open

Page 116: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Topologies on some space X

I What are the open sets on X?I The empty set and X are open setsI The union of open sets is openI Finite intersections of open sets are open

Page 117: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records are presheaves

I Let O = P, the discrete topologyI Then records on a universe X give rise to a presheafR: subset inclusions are taken to casts from larger tosmaller records

for U ∈ P(X) R(U) :≡∏U

ElX |U : Type

for i : V ↪→ U R(i) :≡ cast : R(U)→ R(V )

Page 118: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records are presheaves

I Let O = P, the discrete topology

I Then records on a universe X give rise to a presheafR: subset inclusions are taken to casts from larger tosmaller records

for U ∈ P(X) R(U) :≡∏U

ElX |U : Type

for i : V ↪→ U R(i) :≡ cast : R(U)→ R(V )

Page 119: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records are presheaves

I Let O = P, the discrete topologyI Then records on a universe X give rise to a presheafR: subset inclusions are taken to casts from larger tosmaller records

for U ∈ P(X) R(U) :≡∏U

ElX |U : Type

for i : V ↪→ U R(i) :≡ cast : R(U)→ R(V )

Page 120: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records are presheaves

I Let O = P, the discrete topologyI Then records on a universe X give rise to a presheafR: subset inclusions are taken to casts from larger tosmaller records

for U ∈ P(X) R(U) :≡∏U

ElX |U : Type

for i : V ↪→ U R(i) :≡ cast : R(U)→ R(V )

Page 121: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records are presheaves

I Let O = P, the discrete topologyI Then records on a universe X give rise to a presheafR: subset inclusions are taken to casts from larger tosmaller records

for U ∈ P(X) R(U) :≡∏U

ElX |U : Type

for i : V ↪→ U R(i) :≡ cast : R(U)→ R(V )

Page 122: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records are sheaves

For a cover U =⋃

i Ui on X, then:

e = λr.λi. castUi(r)

p = λf.λi.λj. castUi∩Uj(f(i))

q = λf.λi.λj. castUi∩Uj(f(j))

Page 123: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records are sheaves

For a cover U =⋃

i Ui on X, then:

R(U)∏

iR(Ui)∏

i,jR(Ui ∩ Uj)e p

q

is an equalizer, where

e = λr.λi. castUi(r)

p = λf.λi.λj. castUi∩Uj(f(i))

q = λf.λi.λj. castUi∩Uj(f(j))

Page 124: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records are sheavesFor a cover U =

⋃i Ui on X, then:

R(U)∏

iR(Ui)∏

i,jR(Ui ∩ Uj)

Γ

e

m!u

p

q

where

e = λr.λi. castUi(r)

p = λf.λi.λj. castUi∩Uj(f(i))

q = λf.λi.λj. castUi∩Uj(f(j))

Page 125: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records with non-trivial topology

NameType :≡ {F,M,L}A :≡ {Name[t] | t ∈ NameType} ∪ {Phone[`] | ` ∈ Label}

ElA :≡ λ .String

T :≡ {U ∈ P(A) | Name[M ] ∈ U⇒ {Name[F ],Name[L]} ⊆ U}

ex : RT ({Name[t] | t ∈ NameType})ex :≡ {Name[F ] 7→ ”Robert”;

Name[M ] 7→ ”W”;Name[L] 7→ ”Harper”}

nope : RT ({Name[M ],Phone[Work]})nope :≡ {Name[M ] 7→ ”W”;

Phone[Work] 7→ ”5555555555”}

Page 126: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records with non-trivial topology

NameType :≡ {F,M,L}

A :≡ {Name[t] | t ∈ NameType} ∪ {Phone[`] | ` ∈ Label}ElA :≡ λ .String

T :≡ {U ∈ P(A) | Name[M ] ∈ U⇒ {Name[F ],Name[L]} ⊆ U}

ex : RT ({Name[t] | t ∈ NameType})ex :≡ {Name[F ] 7→ ”Robert”;

Name[M ] 7→ ”W”;Name[L] 7→ ”Harper”}

nope : RT ({Name[M ],Phone[Work]})nope :≡ {Name[M ] 7→ ”W”;

Phone[Work] 7→ ”5555555555”}

Page 127: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records with non-trivial topology

NameType :≡ {F,M,L}A :≡ {Name[t] | t ∈ NameType} ∪ {Phone[`] | ` ∈ Label}

ElA :≡ λ .String

T :≡ {U ∈ P(A) | Name[M ] ∈ U⇒ {Name[F ],Name[L]} ⊆ U}

ex : RT ({Name[t] | t ∈ NameType})ex :≡ {Name[F ] 7→ ”Robert”;

Name[M ] 7→ ”W”;Name[L] 7→ ”Harper”}

nope : RT ({Name[M ],Phone[Work]})nope :≡ {Name[M ] 7→ ”W”;

Phone[Work] 7→ ”5555555555”}

Page 128: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records with non-trivial topology

NameType :≡ {F,M,L}A :≡ {Name[t] | t ∈ NameType} ∪ {Phone[`] | ` ∈ Label}

ElA :≡ λ .String

T :≡ {U ∈ P(A) | Name[M ] ∈ U⇒ {Name[F ],Name[L]} ⊆ U}

ex : RT ({Name[t] | t ∈ NameType})ex :≡ {Name[F ] 7→ ”Robert”;

Name[M ] 7→ ”W”;Name[L] 7→ ”Harper”}

nope : RT ({Name[M ],Phone[Work]})nope :≡ {Name[M ] 7→ ”W”;

Phone[Work] 7→ ”5555555555”}

Page 129: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records with non-trivial topology

NameType :≡ {F,M,L}A :≡ {Name[t] | t ∈ NameType} ∪ {Phone[`] | ` ∈ Label}

ElA :≡ λ .String

T :≡ {U ∈ P(A) | Name[M ] ∈ U⇒ {Name[F ],Name[L]} ⊆ U}

ex : RT ({Name[t] | t ∈ NameType})ex :≡ {Name[F ] 7→ ”Robert”;

Name[M ] 7→ ”W”;Name[L] 7→ ”Harper”}

nope : RT ({Name[M ],Phone[Work]})nope :≡ {Name[M ] 7→ ”W”;

Phone[Work] 7→ ”5555555555”}

Page 130: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records with non-trivial topology

NameType :≡ {F,M,L}A :≡ {Name[t] | t ∈ NameType} ∪ {Phone[`] | ` ∈ Label}

ElA :≡ λ .String

T :≡ {U ∈ P(A) | Name[M ] ∈ U⇒ {Name[F ],Name[L]} ⊆ U}

ex : RT ({Name[t] | t ∈ NameType})ex :≡ {Name[F ] 7→ ”Robert”;

Name[M ] 7→ ”W”;Name[L] 7→ ”Harper”}

nope : RT ({Name[M ],Phone[Work]})nope :≡ {Name[M ] 7→ ”W”;

Phone[Work] 7→ ”5555555555”}

Page 131: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Records with non-trivial topology

NameType :≡ {F,M,L}A :≡ {Name[t] | t ∈ NameType} ∪ {Phone[`] | ` ∈ Label}

ElA :≡ λ .String

T :≡ {U ∈ P(A) | Name[M ] ∈ U⇒ {Name[F ],Name[L]} ⊆ U}

ex : RT ({Name[t] | t ∈ NameType})ex :≡ {Name[F ] 7→ ”Robert”;

Name[M ] 7→ ”W”;Name[L] 7→ ”Harper”}

nope : RT ({Name[M ],Phone[Work]})nope :≡ {Name[M ] 7→ ”W”;

Phone[Work] 7→ ”5555555555”}

Page 132: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Future work

I Complete: formalization of records with topologies aspresheaves in Coq

I In Progress: formalization of records as sheavesI Future: extension to dependent records using

extensional type theory and realizability (Nuprl,MetaPRL)

Page 133: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Future work

I Complete: formalization of records with topologies aspresheaves in Coq

I In Progress: formalization of records as sheavesI Future: extension to dependent records using

extensional type theory and realizability (Nuprl,MetaPRL)

Page 134: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Future work

I Complete: formalization of records with topologies aspresheaves in Coq

I In Progress: formalization of records as sheaves

I Future: extension to dependent records usingextensional type theory and realizability (Nuprl,MetaPRL)

Page 135: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Future work

I Complete: formalization of records with topologies aspresheaves in Coq

I In Progress: formalization of records as sheavesI Future: extension to dependent records using

extensional type theory and realizability (Nuprl,MetaPRL)

Page 136: Galois Tech Talk / Vinyl: Records in Haskell and Type Theory

Questions