type checking - princeton university computer science · it describes a relaon between three things...
TRANSCRIPT
![Page 1: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/1.jpg)
TypeChecking
COS326DavidWalker
PrincetonUniversity
slidescopyright2017DavidWalkerpermissiongrantedtoreusetheseslidesfornon-commercialeducaEonalpurposes
![Page 2: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/2.jpg)
ImplemenEnganInterpreter
letx=3inx+x
Let(“x”,Num3,Binop(Plus,Var“x”,Var“x”))
Num6
6
Parsing
EvaluaEon
PreTyPrinEng
2
![Page 3: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/3.jpg)
ImplemenEnganInterpreter
letx=3inx+x
Let(“x”,Num3,Binop(Plus,Var“x”,Var“x”))
Num6
6
Parsing
EvaluaEon
PreTyPrinEng
3
TypeChecking
![Page 4: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/4.jpg)
LanguageSyntax
typet=IntT|BoolT|ArrToft*ttypex=string(*variables*)typec=Intofint|Boolofbooltypeo=Plus|Minus|LessThantypee=Constofc|Opofe*o*e|Varofx|Ifofe*e*e|Funofx*typ*e|Callofe*e|Letofx*e*e
![Page 5: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/5.jpg)
LanguageSyntax
typet=IntT|BoolT|ArrToft*ttypex=string(*variables*)typec=Intofint|Boolofbooltypeo=Plus|Minus|LessThantypee=Constofc|Opofe*o*e|Varofx|Ifofe*e*e|Funofx*typ*e|Callofe*e|Letofx*e*e
NoEcethatwerequireatypeannotaEonhere.We'llseewhythisisrequiredforourtypecheckingalgorithmlater.
![Page 6: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/6.jpg)
LanguageSyntax(BNFDefiniEon)
t::=int|bool|t->tb--rangesoverbooleansn--rangesoverintegersx--rangesovervariablenamesc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
typet=IntT|BoolT|ArrToft*ttypex=string(*variables*)typec=Intofint|Boolofbooltypeo=Plus|Minus|LessThantypee=Constofc|Opofe*o*e|Varofx|Ifofe*e*e|Funofx*typ*e|Callofe*e|Letofx*e*e
![Page 7: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/7.jpg)
RecallInferenceRuleNotaEonWhendefininghowevaluaEonworked,weusedthisnotaEon:
e1-->λx.ee2-->v2e[v2/x]-->ve1e2-->v
“ife1evaluatestoafuncEonwithargumentxandbodyeande2evaluatestoavaluev2andewithv2subsEtutedforxevaluatestovthene1appliedtoe2evaluatestov”
InEnglish:
Andwewerealsoabletotranslateeachruleinto1caseofafuncEoninOCaml.Togetheralltherulesformedthebasisforaninterpreterforthelanguage.
![Page 8: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/8.jpg)
TheevaluaEonjudgementThisnotaEon:wasreadinEnglishas"eevaluatestov."ItdescribedarelaEonbetweentwothings–anexpressioneandavaluev.(Andewasrelatedtovwhenevereevaluatedtov.)Notealsothatweusuallythoughtofeonthelejas"given"andthevontherightascomputedfrome(accordingtotherules).
e-->v
![Page 9: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/9.jpg)
ThetypingjudgementThisnotaEon:isreadinEnglishas"ehastypetincontextG."Itisgoingtodefinehowtypecheckingworks.ItdescribesarelaEonbetweenthreethings–atypecheckingcontextG,anexpressione,andatypet.WearegoingtothinkofGandeasgiven,andwearegoingtocomputet.Thetypingrulesaregoingtotellushow.
G|-e:t
![Page 10: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/10.jpg)
TypingContexts
WhatisthetypecheckingcontextG?Technically,I'mgoingtotreatGasifitwerea(parEal)funcEonthatmapsvariablenamestotypes.NotaEon:G(x) --lookupx'stypeinGG,x:t --extendGsothatxmapstotWhenGisempty,I'mjustgoingtoomitit.SoI'llsomeEmesjustwrite:|-e:t
![Page 11: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/11.jpg)
ExampleTypingContextsHere'sanexamplecontext:x:int,y:bool,z:intThinkofacontextasan"assumpEon"or"hypothesis"ReaditastheassumpEonthat"xhastypeint,yhastypeboolandzhastypeint"InthesubsituEonmodel,ifyouassumedxhastypeint,thatmeansthatwhenyourunthecode,youhadbeTeractuallywindupsubsEtuEnganintegerforx.
![Page 12: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/12.jpg)
TypingContextsandFreeVariablesOnemorebitofintuiEon:Ifanexpressionecontainsfreevariablesx,y,andzthenweneedtosupplyacontextGthatcontainstypesforatleastx,yandz.Ifwedon't,wewon'tbeabletotypechecke.
![Page 13: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/13.jpg)
TypeCheckingRules
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
Goal:GiverulesthatdefinetherelaEon"G|-e:t".Todothat,wearegoingtogiveoneruleforeverysortofexpression.(WecanturneachruleintoacaseofarecursivefuncEonthattakesanexpressionasaninputandimplementrulespreTydirectly.)
![Page 14: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/14.jpg)
TypingContextsandFreeVariables
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
G|-b:bool
“booleanconstantsbalwayshavetypebool,nomaTerwhatthecontextGis"
English:
Ruleforconstantbooleans:
![Page 15: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/15.jpg)
TypingContextsandFreeVariables
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
G|-n:int
“integerconstantsnalwayshavetypeint,nomaTerwhatthecontextGis"
English:
Ruleforconstantintegers:
![Page 16: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/16.jpg)
TypingContextsandFreeVariables
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
G|-e1:t1G|-e2:t2optype(o)=(t1,t2,t3)G|-e1oe2:t3
“e1oe2hastypet3,ife1hastypet1,e2hastypet2andoisanoperatorthattakesargumentsoftypet1andt2andreturnsavalueoftypet3"
where
Ruleforconstantintegers:
optype(+)=(int,int,int)optype(-)=(int,int,int)optype(<)=(int,int,bool)
English:
![Page 17: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/17.jpg)
TypingContextsandFreeVariables
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
G(x)=tG|-x:t
“variablexhasthetypegivenbythecontext"
Ruleforvariables:
English:
Note:thisisruleexplains(part)ofwhythecontextneedstoprovidetypesforallofthefreevariablesinanexpression
![Page 18: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/18.jpg)
TypingContextsandFreeVariables
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
G|-e1:boolG|-e2:tG|-e3:tG|-ife1thene2elsee3:t
“ife1hastypeboolande2hastypetande3has(thesame)typetthene1thene2elsee3hastypet"
Ruleforif:
English:
![Page 19: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/19.jpg)
TypingContextsandFreeVariables
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
G,x:t|-e:t2G|-λx:t.e:t->t2
“ifGextendedwithx:tprovesehastypet2thenλx:t.ehastypet->t2"
RuleforfuncEons:
English:
![Page 20: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/20.jpg)
TypingContextsandFreeVariables
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
G|-e1:t1->t2G|-e2:t1G|-e1e2:t2
“ifGextendedwithx:tprovesehastypet2thenλx:t.ehastypet->t2"
RuleforfuncEoncall:
English:
![Page 21: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/21.jpg)
TypingContextsandFreeVariables
t::=int|bool|t->tc::=n|bo::=+|-|<e::=c|eoe|x|ifetheneelsee|λx:t.e|ee|letx=eine
G|-e1:t1G,x:t1|-e2:t2G|-letx=e1ine2:t2
“ife1hastypet1andGextendedwithx:t1provese2hastypet2thenletx=e1ine2hastypet2"
Ruleforlet:
English:
![Page 22: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/22.jpg)
ATypingDerivaEonAtypingderivaEonisa"proof"thatanexpressioniswell-typedinaparEcularcontext.Suchproofsconsistofatreeofvalidrules,withnoobligaEonslejunfulfilledatthetopofthetree.(ie:noaxiomslejover).
G,x:int(x)=intG,x:int|-x:intG,x:int|-2:intG,x:int|-x+2:intG|-λx:int.x+2:int->int
![Page 23: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/23.jpg)
KeyProperEesGoodtypesystemsaresound.Inotherwords,ifthetypesystemsaysthatehastypettheneshouldhave"well-defined"evaluaEon(ie,ourinterpretershouldnotraiseanexcepEonpart-waythroughbecauseitdoesn'tknowhowtoconEnueevaluaEon).Also,ifehastypetanditterminatesandproducesavalue,thenitshouldproduceavalueofthattype.eg,iftisint,thenitshouldproduceavaluewithtypeint.
![Page 24: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/24.jpg)
Soundness=Progress+PreservaEonProvingsoundnessboilsdowntotwotheorems:ProgressTheorem:If|-e:ttheneither:(1)eisavalue,or(2)e-->e'Preserva.onTheorem:If|-e:tande-->e'then|-e':tSeeCOS510forproofsofthesetheorems.Butyouhavemostofthenecessarytechniques:ProofbyinducEononthestructureof...variousinducEvedatatypes.:-)
![Page 25: Type Checking - Princeton University Computer Science · It describes a relaon between three things – a type checking context G, an expression e, and a type t. We are going to think](https://reader033.vdocuments.site/reader033/viewer/2022042300/5ecaaa23691ccf233957ba7a/html5/thumbnails/25.jpg)
Thetypingrulesalsodefineanalgorithmfor...typechecking...