howdowe bind type variables? · tick (mkt = mkt newval upd toint where val upd toint) newval = upd...
TRANSCRIPT
![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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/1.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/2.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/3.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/4.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/5.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/7.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/8.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/9.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/10.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/14.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/15.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/16.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/17.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/18.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/19.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/20.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/21.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/22.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/23.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/24.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/25.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/26.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/27.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/28.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/29.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/30.jpg)
UniversalsvsExistentialsdataUnivExawhereMkUE::a!b!UnivExa
A ?. D ? .
caseueofMkUE@a@bxy!...
::UnivE
xτ
�. . ?� �τ� �� � � �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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/31.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/32.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/33.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/34.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/35.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/36.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/37.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/38.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/39.jpg)
Γ⊢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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/40.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/41.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/42.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022050603/5faa27de45f9735b240b1faa/html5/thumbnails/43.jpg)
TypeVariablesinPatternsRichardA.EisenbergBrynMawrCollege
Friday,September28,2018HaskellSymposiumSt.Louis,MO,USA
SimonPeytonJonesMicrosoftResearch,Cambridge