Top Banner
Programming in Vinyl Jon Sterling jonmsterling.com May 18, 2014
173

Programming in Vinyl (BayHac 2014)

Jun 23, 2015

Download

Technology

jonsterling

Introduction to Vinyl 0.4 and its denotation in Type Theory.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming in Vinyl (BayHac 2014)

Programming in Vinyl

Jon Sterlingjonmsterling.com

May 18, 2014

Page 2: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

Structural Typing

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

Page 8: Programming in Vinyl (BayHac 2014)

Structural Typing

I Sharing field names and accessors

I Record types may be characterized structurally

Page 9: Programming in Vinyl (BayHac 2014)

Structural Typing

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

Page 10: Programming in Vinyl (BayHac 2014)

Row Polymorphism

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

Page 11: Programming in Vinyl (BayHac 2014)

Row Polymorphism

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

Page 12: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

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: Programming in Vinyl (BayHac 2014)

Roll Your Own in Haskell

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

Page 25: Programming in Vinyl (BayHac 2014)

Roll Your Own in Haskell

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

Page 26: Programming in Vinyl (BayHac 2014)

Universes a la Tarski

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

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

Page 27: Programming in Vinyl (BayHac 2014)

Universes a la Tarski

I A type U of codes for types.

I Function ElU : U → Type.

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

Page 28: Programming in Vinyl (BayHac 2014)

Universes a la Tarski

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

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

Page 29: Programming in Vinyl (BayHac 2014)

Universes a la Tarski

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

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

Page 30: Programming in Vinyl (BayHac 2014)

Universes a la Tarski

Type

Page 31: Programming in Vinyl (BayHac 2014)

Universes a la Tarski

Type

Page 32: Programming in Vinyl (BayHac 2014)

Universes a la Tarski

Type

Page 33: Programming in Vinyl (BayHac 2014)

Universes a la Tarski

Type

Page 34: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 35: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:

I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 36: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 37: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type

Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 38: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type

Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 39: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 40: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A

` : LabelPhone[`],Email[`] : A

s : AElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 41: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : A

s : AElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 42: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 43: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 44: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string

ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 45: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 46: Programming in Vinyl (BayHac 2014)

A Closed Universe

Let A be a universe of address books:I Statics:

A : Type Label : Type Home,Office : Label

Name : A` : Label

Phone[`],Email[`] : As : A

ElA(s) : Type

I Dynamics:

ElA(Name) string ElA(Email[`]) string

ElA(Phone[`]) list(N)

Page 47: Programming in Vinyl (BayHac 2014)

Records as Products

Page 48: Programming in Vinyl (BayHac 2014)

Records as Products

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

Page 49: Programming in Vinyl (BayHac 2014)

Records as Products

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

recordU ∑V⊆U

∏V

ElU |V

Page 50: Programming in Vinyl (BayHac 2014)

Records as Products

Type

Page 51: Programming in Vinyl (BayHac 2014)

Records as Products

Type

×× ×

×

Page 52: Programming in Vinyl (BayHac 2014)

Records as Products

recordU ∑V⊆U

∏V

ElU |V

Page 53: Programming in Vinyl (BayHac 2014)

Example Record

recordU ∑V⊆U

∏V

ElU |V

A′ {Name,Email Work}ex : recordU

ex 〈A′, λ.{Name 7→ ”Robert Harper”;Email Work 7→ ”[email protected]”}〉

Page 54: Programming in Vinyl (BayHac 2014)

Example Record

recordU ∑V⊆U

∏V

ElU |V

A′ {Name,Email Work}

ex : recordU

ex 〈A′, λ.{Name 7→ ”Robert Harper”;Email Work 7→ ”[email protected]”}〉

Page 55: Programming in Vinyl (BayHac 2014)

Example Record

recordU ∑V⊆U

∏V

ElU |V

A′ {Name,Email Work}ex : recordU

ex 〈A′, λ.{Name 7→ ”Robert Harper”;Email Work 7→ ”[email protected]”}〉

Page 56: Programming in Vinyl (BayHac 2014)

Example Record

recordU ∑V⊆U

∏V

ElU |V

A′ {Name,Email Work}ex : recordU

ex 〈A′, λ.{Name 7→ ”Robert Harper”;Email Work 7→ ”[email protected]”}〉

Page 57: Programming in Vinyl (BayHac 2014)

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 58: Programming in Vinyl (BayHac 2014)

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 59: Programming in Vinyl (BayHac 2014)

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 60: Programming in Vinyl (BayHac 2014)

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 61: Programming in Vinyl (BayHac 2014)

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 62: Programming in Vinyl (BayHac 2014)

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 V ⊆ X R(V ) :≡∏V

ElX |V : Type

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

Page 63: Programming in Vinyl (BayHac 2014)

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 V ⊆ X R(V ) :≡∏V

ElX |V : Type

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

Page 64: Programming in Vinyl (BayHac 2014)

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 V ⊆ X R(V ) :≡∏V

ElX |V : Type

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

Page 65: Programming in Vinyl (BayHac 2014)

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 V ⊆ X R(V ) :≡∏V

ElX |V : Type

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

Page 66: Programming in Vinyl (BayHac 2014)

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 V ⊆ X R(V ) :≡∏V

ElX |V : Type

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

Page 67: Programming in Vinyl (BayHac 2014)

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 68: Programming in Vinyl (BayHac 2014)

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 69: Programming in Vinyl (BayHac 2014)

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 70: Programming in Vinyl (BayHac 2014)

Corecords as Sums

Page 71: Programming in Vinyl (BayHac 2014)

Corecords as Sums

Corecords (extensible variants): the sum of the image ofElU in Type restricted to a subset of U .

Page 72: Programming in Vinyl (BayHac 2014)

Corecords as Sums

Corecords (extensible variants): the sum of the image ofElU in Type restricted to a subset of U .

corecordU ∑V⊆U

∑V

ElU |V

Page 73: Programming in Vinyl (BayHac 2014)

Corecords as Sums

Type

Page 74: Programming in Vinyl (BayHac 2014)

Corecords as Sums

Type

++ +

+

Page 75: Programming in Vinyl (BayHac 2014)

Corecords as Sums

corecordU ∑V⊆U

∑V

ElU |V

Page 76: Programming in Vinyl (BayHac 2014)

Doing it in Haskell

I Create a universe U at the type-levelI Use type families to approximate ElUI Parameterize Rec by U , ElU?

Page 77: Programming in Vinyl (BayHac 2014)

Doing it in Haskell

I Create a universe U at the type-level

I Use type families to approximate ElUI Parameterize Rec by U , ElU?

Page 78: Programming in Vinyl (BayHac 2014)

Doing it in Haskell

I Create a universe U at the type-levelI Use type families to approximate ElU

I Parameterize Rec by U , ElU?

Page 79: Programming in Vinyl (BayHac 2014)

Doing it in Haskell

I Create a universe U at the type-levelI Use type families to approximate ElUI Parameterize Rec by U , ElU?

Page 80: Programming in Vinyl (BayHac 2014)

Records in Haskell

data Rec :: (U → ∗)→ [ U ]→ ∗ whereRNil :: Rec elU ’[](:&) :: !(elU r)→ !(Rec elU rs)→ Rec elU (r ’: rs)

Page 81: Programming in Vinyl (BayHac 2014)

Records in Haskell

data Rec :: (U → ∗)→ [ U ]→ ∗ where

RNil :: Rec elU ’[](:&) :: !(elU r)→ !(Rec elU rs)→ Rec elU (r ’: rs)

Page 82: Programming in Vinyl (BayHac 2014)

Records in Haskell

data Rec :: (U → ∗)→ [ U ]→ ∗ whereRNil :: Rec elU ’[]

(:&) :: !(elU r)→ !(Rec elU rs)→ Rec elU (r ’: rs)

Page 83: Programming in Vinyl (BayHac 2014)

Records in Haskell

data Rec :: (U → ∗)→ [ U ]→ ∗ whereRNil :: Rec elU ’[](:&) :: !(elU r)→ !(Rec elU rs)→ Rec elU (r ’: rs)

Page 84: Programming in Vinyl (BayHac 2014)

Records in Haskell (Actually)

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

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

Page 85: Programming in Vinyl (BayHac 2014)

Records in Haskell (Actually)

data TyFun :: ∗ → ∗ → ∗

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

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

Page 86: Programming in Vinyl (BayHac 2014)

Records in Haskell (Actually)

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

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

Page 87: Programming in Vinyl (BayHac 2014)

Records in Haskell (Actually)

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

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

Page 88: Programming in Vinyl (BayHac 2014)

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 89: Programming in Vinyl (BayHac 2014)

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 90: Programming in Vinyl (BayHac 2014)

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 91: Programming in Vinyl (BayHac 2014)

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 92: Programming in Vinyl (BayHac 2014)

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 93: Programming in Vinyl (BayHac 2014)

Validating Records

bob :: Rec ElA [Name, Email Work]

bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

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

*unnnnnhhh...*

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

Page 94: Programming in Vinyl (BayHac 2014)

Validating Records

bob :: Rec ElA [Name, Email Work]bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

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

*unnnnnhhh...*

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

Page 95: Programming in Vinyl (BayHac 2014)

Validating Records

bob :: Rec ElA [Name, Email Work]bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

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

*unnnnnhhh...*

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

Page 96: Programming in Vinyl (BayHac 2014)

Validating Records

bob :: Rec ElA [Name, Email Work]bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

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

*unnnnnhhh...*

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

Page 97: Programming in Vinyl (BayHac 2014)

Validating Records

bob :: Rec ElA [Name, Email Work]bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

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

*unnnnnhhh...*

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

Page 98: Programming in Vinyl (BayHac 2014)

Welp.

Page 99: Programming in Vinyl (BayHac 2014)

Effects inside records

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

Page 100: Programming in Vinyl (BayHac 2014)

Effects inside records

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

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

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

Page 101: Programming in Vinyl (BayHac 2014)

Effects inside records

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

(=:) : Applicative f⇒ sing r→ elU $ r→ Rec elU f ’[r]

k =: x = pure x :& RNil

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

Page 102: Programming in Vinyl (BayHac 2014)

Effects inside records

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

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

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

Page 103: Programming in Vinyl (BayHac 2014)

Effects inside records

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

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

(⇐\): sing r→ f (elU $ r)→ Rec elU f ’[r]

k⇐\ x = x :& RNil

Page 104: Programming in Vinyl (BayHac 2014)

Effects inside records

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

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

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

Page 105: Programming in Vinyl (BayHac 2014)

Compositional Validation

type RecA = Rec ElA

bob :: RecA Identity [Name, Email Work]bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

Page 106: Programming in Vinyl (BayHac 2014)

Compositional Validation

type RecA = Rec ElAbob :: RecA Identity [Name, Email Work]

bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

Page 107: Programming in Vinyl (BayHac 2014)

Compositional Validation

type RecA = Rec ElAbob :: RecA Identity [Name, Email Work]bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

Page 108: Programming in Vinyl (BayHac 2014)

Compositional Validation

type RecA = Rec ElAbob :: RecA Identity [Name, Email Work]bob = Name =: ”Robert Harper”⊕ Email Work =: ”[email protected]

Page 109: Programming in Vinyl (BayHac 2014)

Compositional Validation

type Validator a = a→ Either Error a

validateName :: RecA Validator ’[Name]validatePhone :: ∀`. RecA Validator ’[Phone `]validateEmail :: ∀`. RecA Validator ’[Email `]

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

validateContact :: RecA Validator TotalContactvalidateContact = validateName

⊕ validateEmail⊕ validateEmail⊕ validatePhone⊕ validatePhone

Page 110: Programming in Vinyl (BayHac 2014)

Compositional Validation

type Validator a = a→ Either Error avalidateName :: RecA Validator ’[Name]validatePhone :: ∀`. RecA Validator ’[Phone `]validateEmail :: ∀`. RecA Validator ’[Email `]

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

validateContact :: RecA Validator TotalContactvalidateContact = validateName

⊕ validateEmail⊕ validateEmail⊕ validatePhone⊕ validatePhone

Page 111: Programming in Vinyl (BayHac 2014)

Compositional Validation

type Validator a = a→ Either Error avalidateName :: RecA Validator ’[Name]validatePhone :: ∀`. RecA Validator ’[Phone `]validateEmail :: ∀`. RecA Validator ’[Email `]

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

validateContact :: RecA Validator TotalContactvalidateContact = validateName

⊕ validateEmail⊕ validateEmail⊕ validatePhone⊕ validatePhone

Page 112: Programming in Vinyl (BayHac 2014)

Compositional Validation

type Validator a = a→ Either Error avalidateName :: RecA Validator ’[Name]validatePhone :: ∀`. RecA Validator ’[Phone `]validateEmail :: ∀`. RecA Validator ’[Email `]

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

validateContact :: RecA Validator TotalContactvalidateContact = validateName

⊕ validateEmail⊕ validateEmail⊕ validatePhone⊕ validatePhone

Page 113: Programming in Vinyl (BayHac 2014)

Record Operators

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }

type Validator = Lift (→) Identity (Either Error)

( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rs

rdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

Page 114: Programming in Vinyl (BayHac 2014)

Record Operators

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }

type Validator = Lift (→) Identity (Either Error)

( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rs

rdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

Page 115: Programming in Vinyl (BayHac 2014)

Record Operators

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }

type Validator = Lift (→) Identity (Either Error)

( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rs

rdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

Page 116: Programming in Vinyl (BayHac 2014)

Record Operators

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }

type Validator = Lift (→) Identity (Either Error)

( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rs

rdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

Page 117: Programming in Vinyl (BayHac 2014)

Record Operators

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }

type Validator = Lift (→) Identity (Either Error)

( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rs

rdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

Page 118: Programming in Vinyl (BayHac 2014)

Compositional Validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rsrdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

validateContact :: RecA Validator TotalContact

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

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

Page 119: Programming in Vinyl (BayHac 2014)

Compositional Validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rsrdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

validateContact :: RecA Validator TotalContact

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

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

Page 120: Programming in Vinyl (BayHac 2014)

Compositional Validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rsrdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

validateContact :: RecA Validator TotalContact

bobValid :: RecA (Either Error) [Name, Email Work]

bobValid = cast validateContact ? bob

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

Page 121: Programming in Vinyl (BayHac 2014)

Compositional Validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rsrdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

validateContact :: RecA Validator TotalContact

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

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

Page 122: Programming in Vinyl (BayHac 2014)

Compositional Validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rsrdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

validateContact :: RecA Validator TotalContact

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

validBob :: Either Error (RecA Identity [Name, Email Work])

validBob = rdist bobValid

Page 123: Programming in Vinyl (BayHac 2014)

Compositional Validation

newtype Lift o f g x = Lift { runLift :: f x ‘o‘ g x }type Validator = Lift (→) Identity (Either Error)( ? ) :: RecU (Lift (→) f g) rs→ RecU f rs→ RecU g rsrdist :: Applicative f⇒ RecU f rs→ f (RecU Identity rs)

validateContact :: RecA Validator TotalContact

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

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

Page 124: Programming in Vinyl (BayHac 2014)

Laziness as an effect

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

type PlainRecU rs = RecU Identity rstype LazyRecU rs = RecU Thunk rs

Page 125: Programming in Vinyl (BayHac 2014)

Laziness as an effect

newtype Identity a = Identity { runIdentity :: a }

data Thunk a = Thunk { unThunk :: a }

type PlainRecU rs = RecU Identity rstype LazyRecU rs = RecU Thunk rs

Page 126: Programming in Vinyl (BayHac 2014)

Laziness as an effect

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

type PlainRecU rs = RecU Identity rstype LazyRecU rs = RecU Thunk rs

Page 127: Programming in Vinyl (BayHac 2014)

Laziness as an effect

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

type PlainRecU rs = RecU Identity rs

type LazyRecU rs = RecU Thunk rs

Page 128: Programming in Vinyl (BayHac 2014)

Laziness as an effect

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

type PlainRecU rs = RecU Identity rstype LazyRecU rs = RecU Thunk rs

Page 129: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchName :: RecA IO ’[Name]fetchName = Name⇐\ someOperation

fetchWorkEmail :: RecA IO ’[Email Work]fetchWorkEmail = Email Work⇐\ anotherOperation

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

Page 130: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchName :: RecA IO ’[Name]

fetchName = Name⇐\ someOperation

fetchWorkEmail :: RecA IO ’[Email Work]fetchWorkEmail = Email Work⇐\ anotherOperation

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

Page 131: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchName :: RecA IO ’[Name]fetchName = Name⇐\ someOperation

fetchWorkEmail :: RecA IO ’[Email Work]fetchWorkEmail = Email Work⇐\ anotherOperation

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

Page 132: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchName :: RecA IO ’[Name]fetchName = Name⇐\ someOperation

fetchWorkEmail :: RecA IO ’[Email Work]

fetchWorkEmail = Email Work⇐\ anotherOperation

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

Page 133: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchName :: RecA IO ’[Name]fetchName = Name⇐\ someOperation

fetchWorkEmail :: RecA IO ’[Email Work]fetchWorkEmail = Email Work⇐\ anotherOperation

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

Page 134: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchName :: RecA IO ’[Name]fetchName = Name⇐\ someOperation

fetchWorkEmail :: RecA IO ’[Email Work]fetchWorkEmail = Email Work⇐\ anotherOperation

fetchBob :: RecA IO [Name, Email Work]

fetchBob = fetchName ⊕ fetchWorkEmail

Page 135: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchName :: RecA IO ’[Name]fetchName = Name⇐\ someOperation

fetchWorkEmail :: RecA IO ’[Email Work]fetchWorkEmail = Email Work⇐\ anotherOperation

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

Page 136: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

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

( $ ) :: (∀ a. f a→ g a)→ RecU f rs→ RecU g rs

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

concurrentBob :: Concurrently (RecA Identity [...])concurrentBob = rdist bobConcurrently

Page 137: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

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

( $ ) :: (∀ a. f a→ g a)→ RecU f rs→ RecU g rs

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

concurrentBob :: Concurrently (RecA Identity [...])concurrentBob = rdist bobConcurrently

Page 138: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

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

( $ ) :: (∀ a. f a→ g a)→ RecU f rs→ RecU g rs

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

concurrentBob :: Concurrently (RecA Identity [...])concurrentBob = rdist bobConcurrently

Page 139: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

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

( $ ) :: (∀ a. f a→ g a)→ RecU f rs→ RecU g rs

bobConcurrently :: RecA Concurrently [Name, Email Work]

bobConcurrently = Concurrently $ fetchBob

concurrentBob :: Concurrently (RecA Identity [...])concurrentBob = rdist bobConcurrently

Page 140: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

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

( $ ) :: (∀ a. f a→ g a)→ RecU f rs→ RecU g rs

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

concurrentBob :: Concurrently (RecA Identity [...])concurrentBob = rdist bobConcurrently

Page 141: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

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

( $ ) :: (∀ a. f a→ g a)→ RecU f rs→ RecU g rs

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

concurrentBob :: Concurrently (RecA Identity [...])

concurrentBob = rdist bobConcurrently

Page 142: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

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

( $ ) :: (∀ a. f a→ g a)→ RecU f rs→ RecU g rs

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

concurrentBob :: Concurrently (RecA Identity [...])concurrentBob = rdist bobConcurrently

Page 143: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchBob :: RecA IO [Name, Email Work]bobConcurrently :: RecA Concurrently [Name, Email Work]concurrentBob :: Concurrently (RecA Identity [...])

bob :: IO (RecA Identity [Name, Email Work])bob = runConcurrently concurrentBob

Page 144: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchBob :: RecA IO [Name, Email Work]bobConcurrently :: RecA Concurrently [Name, Email Work]concurrentBob :: Concurrently (RecA Identity [...])

bob :: IO (RecA Identity [Name, Email Work])bob = runConcurrently concurrentBob

Page 145: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchBob :: RecA IO [Name, Email Work]bobConcurrently :: RecA Concurrently [Name, Email Work]concurrentBob :: Concurrently (RecA Identity [...])

bob :: IO (RecA Identity [Name, Email Work])

bob = runConcurrently concurrentBob

Page 146: Programming in Vinyl (BayHac 2014)

Concurrent Records with Async

fetchBob :: RecA IO [Name, Email Work]bobConcurrently :: RecA Concurrently [Name, Email Work]concurrentBob :: Concurrently (RecA Identity [...])

bob :: IO (RecA Identity [Name, Email Work])bob = runConcurrently concurrentBob

Page 147: Programming in Vinyl (BayHac 2014)

Containers: The Syntax for Data Types

container : Type

U : Type ElU : U → TypeU / ElU : container

C : containerC.Sh : Type

C U / ElUC.Sh U

C : containerC.Po : C.Sh→ Type

C U / ElUC.Po ElU

Page 148: Programming in Vinyl (BayHac 2014)

Containers: The Syntax for Data Types

container : TypeU : Type ElU : U → TypeU / ElU : container

C : containerC.Sh : Type

C U / ElUC.Sh U

C : containerC.Po : C.Sh→ Type

C U / ElUC.Po ElU

Page 149: Programming in Vinyl (BayHac 2014)

Containers: The Syntax for Data Types

container : TypeU : Type ElU : U → TypeU / ElU : container

C : containerC.Sh : Type

C U / ElUC.Sh U

C : containerC.Po : C.Sh→ Type

C U / ElUC.Po ElU

Page 150: Programming in Vinyl (BayHac 2014)

Containers: The Syntax for Data Types

container : TypeU : Type ElU : U → TypeU / ElU : container

C : containerC.Sh : Type

C U / ElUC.Sh U

C : containerC.Po : C.Sh→ Type

C U / ElUC.Po ElU

Page 151: Programming in Vinyl (BayHac 2014)

Containers: The Syntax for Data Types

container : TypeU : Type ElU : U → TypeU / ElU : container

C : containerC.Sh : Type

C U / ElUC.Sh U

C : containerC.Po : C.Sh→ Type

C U / ElUC.Po ElU

Page 152: Programming in Vinyl (BayHac 2014)

Containers: The Syntax for Data Types

container : TypeU : Type ElU : U → TypeU / ElU : container

C : containerC.Sh : Type

C U / ElUC.Sh U

C : containerC.Po : C.Sh→ Type

C U / ElUC.Po ElU

Page 153: Programming in Vinyl (BayHac 2014)

Restricting Containers

C : container V ⊆ C.ShC|V : container

C U / ElUC|V V / ElU |V

Page 154: Programming in Vinyl (BayHac 2014)

Restricting Containers

C : container V ⊆ C.ShC|V : container

C U / ElUC|V V / ElU |V

Page 155: Programming in Vinyl (BayHac 2014)

Container Lifting

C : container F : Type→ TypeC ↑ F : container

C U / ElUC ↑ F U / F ◦ ElU

Page 156: Programming in Vinyl (BayHac 2014)

Container Lifting

C : container F : Type→ TypeC ↑ F : container

C U / ElUC ↑ F U / F ◦ ElU

Page 157: Programming in Vinyl (BayHac 2014)

A Menagerie of Quantifiers

Page 158: Programming in Vinyl (BayHac 2014)

A Menagerie of Quantifiers

Dependent Products:

Γ ` A : Type Γ, x : A ` B : TypeΓ `

∏AB : Type

Γ, x : A ` e : B[x]

Γ ` λx.e :∏

AB

Page 159: Programming in Vinyl (BayHac 2014)

A Menagerie of Quantifiers

Dependent Sums:

Γ ` A : Type Γ, x : A ` B : TypeΓ `

∑AB : Type

Γ ` a : A Γ ` b : B[a]

Γ ` 〈a, b〉 :∑

AB

Page 160: Programming in Vinyl (BayHac 2014)

A Menagerie of Quantifiers

Inductive Types:

Γ ` A : Type Γ, x : A ` B : TypeΓ `WAB : Type

Γ ` a : A Γ, v : B[a] ` b : WAB

Γ ` sup(a; v. b) : WAB

Page 161: Programming in Vinyl (BayHac 2014)

A Menagerie of Quantifiers

Coinductive Types:

Γ ` A : Type Γ, x : A ` B : TypeΓ ` MAB : Type

Γ ` a : A Γ, v : B[a] ` b :∞ (MAB)

Γ ` inf(a; v. b) : MAB

Page 162: Programming in Vinyl (BayHac 2014)

A Scheme for Quantifiers

Γ, A : Type, (x : A ` B : Type) ` QAB : TypeΓ ` Q quantifier

Page 163: Programming in Vinyl (BayHac 2014)

A Scheme for Quantifiers

Γ, A : Type, (x : A ` B : Type) ` QAB : TypeΓ ` Q quantifier

Page 164: Programming in Vinyl (BayHac 2014)

Quantifiers Give Containers Semantics

Γ, A : Type, (x : A ` B : Type) ` QAB : TypeΓ ` Q quantifier

C : container Q quantifier

JCKQ : Type

C U / ElUJCKQ QUElU

Page 165: Programming in Vinyl (BayHac 2014)

Quantifiers Give Containers Semantics

Γ, A : Type, (x : A ` B : Type) ` QAB : TypeΓ ` Q quantifier

C : container Q quantifier

JCKQ : Type

C U / ElUJCKQ QUElU

Page 166: Programming in Vinyl (BayHac 2014)

Quantifiers Give Containers Semantics

Γ, A : Type, (x : A ` B : Type) ` QAB : TypeΓ ` Q quantifier

C : container Q quantifier

JCKQ : Type

C U / ElUJCKQ QUElU

Page 167: Programming in Vinyl (BayHac 2014)

Vinyl Records as Containers

Records and corecords are finite products and sumsrespectively.

Rec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΠ

CoRec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΣ

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KW

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KM

Page 168: Programming in Vinyl (BayHac 2014)

Vinyl Records as Containers

Records and corecords are finite products and sumsrespectively.

Rec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΠ

CoRec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΣ

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KW

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KM

Page 169: Programming in Vinyl (BayHac 2014)

Vinyl Records as Containers

Records and corecords are finite products and sumsrespectively.

Rec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΠ

CoRec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΣ

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KW

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KM

Page 170: Programming in Vinyl (BayHac 2014)

Vinyl Records as Containers

Records and corecords are finite products and sumsrespectively.

Rec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΠ

CoRec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΣ

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KW

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KM

Page 171: Programming in Vinyl (BayHac 2014)

Vinyl Records as Containers

Records and corecords are finite products and sumsrespectively.

Rec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΠ

CoRec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΣ

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KW

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KM

Page 172: Programming in Vinyl (BayHac 2014)

Vinyl Records as Containers

Records and corecords are finite products and sumsrespectively.

Rec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΠ

CoRec ElU F rs ∼= J(U / ElU)|rs3− ↑ F KΣ

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KW

??? ElU F rs ∼= J(U / ElU)|rs3− ↑ F KM

Page 173: Programming in Vinyl (BayHac 2014)

Questions