Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd...

43
type variables? ? How do we bind

Transcript of Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd...

Page 1: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

typevariables??How do webind

Page 2: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

typevariables??How webindshould

Page 3: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

xconsys=x:ys

prefix::prefixxyss=mapxconsysswhere

a![[a]]![[a]]

Page 4: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

prefix::prefixxyss=mapxconsysswhere

xconsys=x:ysxcons::[a]![a]

a![[a]]![[a]]

Page 5: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

prefix::prefixxyss=mapxconsysswhere

xconsys=x:ysxcons::[a]![a]

Couldn'tmatch‘a1’with‘a’‘a1’isboundinxcons::∀a1.[a1]->[a1]

A � � . �a � �a1!

a![[a]]![[a]]

Page 6: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

prefix::prefixxyss=mapxconsysswhere

xconsys=x:ysxcons::[a]![a]

a![[a]]![[a]]∀a.

{-#LANGUAGEScopedTypeVariables#-}

Ok,onemoduleloaded.

Page 7: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful

Goal:Allowatypesignatureon

anyexpression

Page 8: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful•type-classambiguity

show::Showa⇒a!Stringread::Reada⇒String!a

normalize::String!Stringnormalize=show.read

. � � � . ? �

Page 9: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful•type-classambiguity•polymorphicrecursion

dataTa=Leafa|Node(T[a])(T[a])

leaves::Ta![a]leaves(Leafx)=[x]leaves(Nodet1t2)=concat(leavest1++leavest2)

�? . A � ?� ??.

Page 10: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful•type-classambiguity•polymorphicrecursion•higher-ranktypes

ScrapYourBoilerplate[TLDI'03]:

everywhere::(∀a.Dataa⇒a!a)

∀a.Dataa⇒a!a!�? . A � ?� ??.

Page 11: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful•type-classambiguity•polymorphicrecursion•higher-ranktypes•GADTs dataGawhere

MkInt::GIntMkFun::G(Int!Int)

matchG::Ga!amatchGMkInt=5matchGMkFun=(10+)

Page 12: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful•type-classambiguity•polymorphicrecursion•higher-ranktypes•GADTs dataGawhere

MkInt::GIntMkFun::G(Int!Int)

matchG::Ga!amatchGMkInt=5matchGMkFun=(10+)

�? . A � ?� ??.

Page 13: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful•type-classambiguity•polymorphicrecursion•higher-ranktypes•GADTs•inherentambiguitytypefamilyFaambig::Typeablea⇒Fa!Inttest::Char!Inttestx=ambigx

� . �� �a

Page 14: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful

Goal:Allowatypesignatureon

anyexpression

Page 15: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Solution:ScopedTypeVariableScopedTypeVariables

Page 16: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

ScopedTypeVariablesprefix::prefixxyss=mapxconsysswhere

xconsys=x:ysxcons::[a]![a]

a![[a]]![[a]]∀a.

prefix(x::a)yss=mapxconsysswherexcons::[a]![a]xconsys=x:ys

. �? . A

Page 17: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

ScopedTypeVariablesprefix(x::a)yss=mapxconsysswherexcons::[a]![a]xconsys= x:ys

Ok,onemoduleloaded.λ>:tprefixprefix::

1:

Numa⇒a![[a]]![[a]]

Page 18: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

ScopedTypeVariablesprefix(x::a)yss=mapxconsysswherexcons::[a]![a]xconsys= x:ysTrue:

Couldn'tmatchawithBool

Rule:typevariablesmustbevariables

Arbitrary?

Page 19: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

ScopedTypeVariablesWhatisthespecificationof

anyway?

Typingrules!Contribution:

Page 20: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

ExistentialsdataTickerwhereMkT::∀a.a!(a!a)!(a!Int)!TickerD ? .

tick::Ticker!Tickertick(MkT=MkTnewValupdtoIntwhere

val updtoInt)

newVal=updval

Page 21: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

dataTickerwhereMkT::∀a.a!(a!a)!(a!Int)!Ticker

tick::Ticker!Tickertick(MkT=MkTnewValupdtoIntwhere

val updtoInt)

newVal=updvalnewVal::a

. � ?� ?

Existentials

Page 22: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

dataTickerwhereMkT::∀a.a!(a!a)!(a!Int)!Ticker

tick::Ticker!Tickertick(MkT=MkTnewValupdtoIntwhere

val updtoInt)

newVal=updvalnewVal::a

( ::a)

� � . � � �a

Existentials

Page 23: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

ExistentialsdataElabwhereMkE::Showa⇒[Maybe(Tree(a,Int))]!Elab

.� . �? . A � ��a� A � �

Page 24: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Existentials

typefamilyFadataExFwhereMkF::Typeablea⇒Fa!ExF

.� . �? . A � ��a� A � �

??

Page 25: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Typesignaturesareuseful

Goal:Allowatypesignatureon

anyexpression

Page 26: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Solution:ScopedTypeVariableScopedTypeVariables

Page 27: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Solution:ScopedTypeVariableScopedTypeVariables

Partial

Contribution:Patterntypeapplications

Page 28: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

PatterntypeapplicationsdataTickerwhereMkT::∀a.a!(a!a)!(a!Int)!Ticker

tick::Ticker!Tickertick(MkT=MkTnewValupdtoIntwhere

val updtoInt)

newVal=updvalnewVal::a

@a

Page 29: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Patterntypeapplications

Explicitbindingoftypevariablesalwaysworks

Page 30: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

UniversalsvsExistentialsdataUnivExawhereMkUE::a!b!UnivExa

A ?. D ? .

caseueofMkUE@a@bxy!...

::UnivE

�. . ?� �τ� �� � � �a

Page 31: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

UniversalsvsExistentials

�. . ?� �τ� �� � � �a

Uniformity

dataConfusedawhereMkC::a~b⇒b!Confuseda

. � ?� D ? . ¯\_(ϑ)_/¯

Page 32: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Universals&Existentials

Γ⊢K@τ1..mp1..n:Tσ1..j

...K:∀a1..m.Q⇒η1..n→Tφ1..jΓ,Q,φ1..j~σ1..j⊩τ1..m~a1..m

Page 33: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Universals&Existentials

Γ⊢K@τ1..mp1..n:Tσ1..j

...K:∀a1..m.Q⇒η1..n→Tφ1..jΓ,Q,φ1..j~σ1..j⊩τ1..m~a1..m

�. . ?� �.� .

Page 34: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Universals&Existentials

Γ⊢K@τ1..mp1..n:Tσ1..j

...K:∀a1..m.Q⇒η1..n→Tφ1..jΓ,Q,φ1..j~σ1..j⊩τ1..m~a1..m

D � ?A � �. A ?

Page 35: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Universals&Existentials

Γ⊢K@τ1..mp1..n:Tσ1..j

...K:∀a1..m.Q⇒η1..n→Tφ1..jΓ,Q,φ1..j~σ1..j⊩τ1..m~a1..m

A. � � . . ?

Page 36: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Universals&Existentials

Γ⊢K@τ1..mp1..n:Tσ1..j

...K:∀a1..m.Q⇒η1..n→Tφ1..jΓ,Q,φ1..j~σ1..j⊩τ1..m~a1..m

? A � ? .

Page 37: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Universals&Existentials

Γ⊢K@τ1..mp1..n:Tσ1..j

...K:∀a1..m.Q⇒η1..n→Tφ1..jΓ,Q,φ1..j~σ1..j⊩τ1..m~a1..m

?A � �. A ?

Page 38: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Universals&Existentials

Γ⊢K@τ1..mp1..n:Tσ1..j

...K:∀a1..m.Q⇒η1..n→Tφ1..jΓ,Q,φ1..j~σ1..j⊩τ1..m~a1..m

".??A � � (),� A. ? "

" � � � � �� �. . ?"

Page 39: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Γ⊢K@τ1..mp1..n:Tσ1..j

...K:∀a1..m.Q⇒η1..n→Tφ1..jΓ,Q,φ1..j~σ1..j⊩τ1..m~a1..m

".??A � � (),� A. ? "

" � � � � �� �. . ?"

dataExamplewhereMkEx::∀ab.(a~Maybeb)⇒Example

Example

casex::ExampleofMkEx@a@b!...MkEx@(Maybeb)@b!...MkEx@(Maybeb)!...MkEx@a@(Maybeb)!...

Page 40: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Whythisbehavior?

It'sexactlyhowpatternsignatureswouldwork.

Page 41: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

Inthepaper:fullspecificationwithtypingrules

Upshot:wecaneasilydropthevariablerestriction

Page 42: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

NextSteps

Implementation:MyNguyen

Bindingtypevariablesinλ-expressions� . �. D

Page 43: Howdowe bind type variables? · tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a. Pattern type applications Explicit binding of ...

TypeVariablesinPatternsRichardA.EisenbergBrynMawrCollege

[email protected]

Friday,September28,2018HaskellSymposiumSt.Louis,MO,USA

[email protected]

SimonPeytonJonesMicrosoftResearch,Cambridge

[email protected]