type checking - princeton university computer science · it describes a relaon between three things...
Post on 23-May-2020
1 Views
Preview:
TRANSCRIPT
TypeChecking
COS326DavidWalker
PrincetonUniversity
slidescopyright2017DavidWalkerpermissiongrantedtoreusetheseslidesfornon-commercialeducaEonalpurposes
ImplemenEnganInterpreter
letx=3inx+x
Let(“x”,Num3,Binop(Plus,Var“x”,Var“x”))
Num6
6
Parsing
EvaluaEon
PreTyPrinEng
2
ImplemenEnganInterpreter
letx=3inx+x
Let(“x”,Num3,Binop(Plus,Var“x”,Var“x”))
Num6
6
Parsing
EvaluaEon
PreTyPrinEng
3
TypeChecking
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
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.
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
RecallInferenceRuleNotaEonWhendefininghowevaluaEonworked,weusedthisnotaEon:
e1-->λx.ee2-->v2e[v2/x]-->ve1e2-->v
“ife1evaluatestoafuncEonwithargumentxandbodyeande2evaluatestoavaluev2andewithv2subsEtutedforxevaluatestovthene1appliedtoe2evaluatestov”
InEnglish:
Andwewerealsoabletotranslateeachruleinto1caseofafuncEoninOCaml.Togetheralltherulesformedthebasisforaninterpreterforthelanguage.
TheevaluaEonjudgementThisnotaEon:wasreadinEnglishas"eevaluatestov."ItdescribedarelaEonbetweentwothings–anexpressioneandavaluev.(Andewasrelatedtovwhenevereevaluatedtov.)Notealsothatweusuallythoughtofeonthelejas"given"andthevontherightascomputedfrome(accordingtotherules).
e-->v
ThetypingjudgementThisnotaEon:isreadinEnglishas"ehastypetincontextG."Itisgoingtodefinehowtypecheckingworks.ItdescribesarelaEonbetweenthreethings–atypecheckingcontextG,anexpressione,andatypet.WearegoingtothinkofGandeasgiven,andwearegoingtocomputet.Thetypingrulesaregoingtotellushow.
G|-e:t
TypingContexts
WhatisthetypecheckingcontextG?Technically,I'mgoingtotreatGasifitwerea(parEal)funcEonthatmapsvariablenamestotypes.NotaEon:G(x) --lookupx'stypeinGG,x:t --extendGsothatxmapstotWhenGisempty,I'mjustgoingtoomitit.SoI'llsomeEmesjustwrite:|-e:t
ExampleTypingContextsHere'sanexamplecontext:x:int,y:bool,z:intThinkofacontextasan"assumpEon"or"hypothesis"ReaditastheassumpEonthat"xhastypeint,yhastypeboolandzhastypeint"InthesubsituEonmodel,ifyouassumedxhastypeint,thatmeansthatwhenyourunthecode,youhadbeTeractuallywindupsubsEtuEnganintegerforx.
TypingContextsandFreeVariablesOnemorebitofintuiEon:Ifanexpressionecontainsfreevariablesx,y,andzthenweneedtosupplyacontextGthatcontainstypesforatleastx,yandz.Ifwedon't,wewon'tbeabletotypechecke.
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.)
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:
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:
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:
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
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:
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:
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:
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:
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
KeyProperEesGoodtypesystemsaresound.Inotherwords,ifthetypesystemsaysthatehastypettheneshouldhave"well-defined"evaluaEon(ie,ourinterpretershouldnotraiseanexcepEonpart-waythroughbecauseitdoesn'tknowhowtoconEnueevaluaEon).Also,ifehastypetanditterminatesandproducesavalue,thenitshouldproduceavalueofthattype.eg,iftisint,thenitshouldproduceavaluewithtypeint.
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.:-)
Thetypingrulesalsodefineanalgorithmfor...typechecking...
top related