25
Type Checking COS 326 David Walker Princeton University slides copyright 2017 David Walker permission granted to reuse these slides for non-commercial educaEonal purposes

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

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thetypingrulesalsodefineanalgorithmfor...typechecking...