type inference - princeton university computer science...language design for type inference we call...

80
Type Inference COS 326 David Walker Princeton University slides copyright 2017 David Walker permission granted to reuse these slides for non-commercial educaFonal purposes

Upload: others

Post on 04-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TypeInference

COS326DavidWalker

PrincetonUniversity

slidescopyright2017DavidWalkerpermissiongrantedtoreusetheseslidesfornon-commercialeducaFonalpurposes

Page 2: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

MidtermExam

WedOct25,2017InClass(11:00-12:20)

MidtermWeek

Bethereorbesquare!

2

Page 3: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TYPEINFERENCE

3

Page 4: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceTheMLlanguageandtypesystemisdesignedtosupportaverystrongformoftypeinference.

Page 5: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceTheMLlanguageandtypesystemisdesignedtosupportaverystrongformoftypeinference.MLfindsthistypeformap:

let rec map f l = match l with [ ] -> [ ] | hd::tl -> f hd :: map f tl

map : ('a -> 'b) -> 'a list -> 'b list

Page 6: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceTheMLlanguageandtypesystemisdesignedtosupportaverystrongformoftypeinference.MLfindsthistypeformap:whichisreallyanabbreviaFonforthistype:

let rec map f l = match l with [ ] -> [ ] | hd::tl -> f hd :: map f tl

map : ('a -> 'b) -> 'a list -> 'b list

map : forall 'a,'b.('a -> 'b) -> 'a list -> 'b list

Page 7: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceWecallthistypetheprincipletype(scheme)formap.AnyotherML-styletypeyoucangivemapisaninstanceofthistype,meaningwecanobtaintheothertypesviasubs3tu3onoftypesforparametersfromtheprincipletype.Eg:

('a -> 'a) -> 'a list -> 'a list

map : ('a -> 'b) -> 'a list -> 'b list

(bool -> int) -> bool list -> int list

('a -> int) -> 'a list -> int list

Page 8: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferencePrincipletypesaregreat:•  thetypeinferenceenginecanmakeabestchoiceforthetypeto

giveanexpression•  theenginedoesn'thavetoguess(andwon'thavetoguesswrong)

ThefactthatprincipletypesexistissurprisinglybriYle.IfyouchangeML'stypesystemaliYlebitineitherdirecFon,itcanfallapart.

Page 9: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceSupposewetakeoutpolymorphictypesandneedatypeforid:Thenthecompilermightguessthatidhasone(andonlyone)ofthesetypes:

id : bool -> bool

let id x = x

id : int -> int

Page 10: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceSupposewetakeoutpolymorphictypesandneedatypeforid:Thenthecompilermightguessthatidhasone(andonlyone)ofthesetypes:Butlateron,oneofthefollowingcodesnippetswon'ttypecheck:Sowhateverchoiceismade,adifferentonemighthavebeenbeYer.

id true

id : bool -> bool

let id x = x

id : int -> int

id 3

Page 11: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceWeshowedthatremovingtypesfromthelanguagecausesafailureofprincipletypes.Doesaddingmoretypesalwaysmaketypeinferenceeasier?

Page 12: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceWeshowedthatremovingtypesfromthelanguagecausesafailureofprincipletypes.Doesaddingmoretypesalwaysmaketypeinferenceeasier?

Page 13: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceOCamlonlyhasuniversaltypesontheoutside:Considerthisprogram:Itwon'ttypecheckinOCaml.Wemightwanttogiveitthistype:NoFcethattheuniversalquanFfierappearsunderan->.

f : (forall a.a->a) -> bool * int

forall 'a,'b. ('a -> 'b) -> 'a list -> 'b list

let f g = (g true, g 3)

Page 14: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceSystemFisalotlikeOCaml,exceptthatitallowsuniversalquanFfiersinanyposiFon.Itcouldtypecheckf.Unfortunately,typeinferenceinSystemFisundecideable..

f : (forall a.a->a) -> bool * int

let f g = (g true, g 3)

Page 15: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceSystemFisalotlikeOCaml,exceptthatitallowsuniversalquanFfiersinanyposiFon.Itcouldtypecheckf.Unfortunately,typeinferenceinSystemFisundecideable.Developedin1972bylogicianJeanYves-Girardwhowasinterestedintheconsistencyofalogicof2nd-orderarithemeFc.RediscoveredasprogramminglanguagebyJohnReynoldsin1974..

f : (forall a.a->a) -> bool * int

let f g = (g true, g 3)

Page 16: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceEvenseeminglysmallchangescaneffecttypeinference.Suppose"+"operatedonbothfloatsandints.Whattypeforthis?

let f x = x + x

Page 17: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceEvenseeminglysmallchangescaneffecttypeinference.Suppose"+"operatedonbothfloatsandints.Whattypeforthis?

f : int -> int ?

let f x = x + x

f : float -> float ?

Page 18: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceEvenseeminglysmallchangescaneffecttypeinference.Suppose"+"operatedonbothfloatsandints.Whattypeforthis?

f : int -> int ?

let f x = x + x

f : float -> float ?

f : 'a -> 'a ?

Page 19: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

LanguageDesignforTypeInferenceEvenseeminglysmallchangescaneffecttypeinference.Suppose"+"operatedonbothfloatsandints.Whattypeforthis?NotypeinOCaml'stypesystemworks.InHaskell:

f : int -> int ?

let f x = x + x

f : float -> float ?

f : 'a -> 'a ?

f : Num 'a => 'a -> 'a

Page 20: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

INFERRINGSIMPLETYPES

20

Page 21: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TypeSchemesAtypeschemecontainstypevariablesthatmaybefilledinduringtypeinference

s::=a|int|bool|s->s

Atermschemeisatermthatcontainstypeschemesratherthanpropertypes.eg,forfuncFons:

fun(x:s)->e

letrecf(x:s):s=e

Page 22: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TheGenericTypeInferenceAlgorithm1)AdddisFnctvariablesinallplacestypeschemesareneeded

22

Page 23: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TheGenericTypeInferenceAlgorithm1)AdddisFnctvariablesinallplacestypeschemesareneeded2)Generateconstraints(equaFonsbetweentypes)thatmustbesaFsfiedinorderforanexpressiontotypecheck

•  NoFcethedifferencebetweenthisandthetypecheckingalgorithmfromlastFme.LastFme,wetriedto:•  eagerlydeducetheconcretetypewhencheckingeveryexpression•  rejectprogramswhentypesdidn'tmatch.eg:

•  ThisFme,we'llcollectupequaFonslike:

23

fe--f'sargumenttypemustequale

a->b=c

Page 24: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TheGenericTypeInferenceAlgorithm1)AdddisFnctvariablesinallplacestypeschemesareneeded2)Generateconstraints(equaFonsbetweentypes)thatmustbesaFsfiedinorderforanexpressiontotypecheck

•  NoFcethedifferencebetweenthisandthetypecheckingalgorithmfromlastFme.LastFme,wetriedto:•  eagerlydeducetheconcretetypewhencheckingeveryexpression•  rejectprogramswhentypesdidn'tmatch.eg:

•  ThisFme,we'llcollectupequaFonslike:

3)SolvetheequaFons,generaFngsubsFtuFonsoftypesforvar's

24

fe--f'sargumenttypemustequale

a->b=c

Page 25: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Example:Inferringtypesformap

let rec map f l = match l with

[] -> [] | hd::tl -> f hd :: map f tl

Page 26: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Step1:Annotate

let rec map (f:a) (l:b) : c = match l with

[] -> [] | hd::tl -> f hd :: map f tl

Page 27: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Step2:GenerateConstraints

let rec map (f:a) (l:b) : c = match l with

[] -> [] | hd::tl -> f hd :: map f tl b = d list

a = d -> f

...

Page 28: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Step2:GenerateConstraints

let rec map (f:a) (l:b) : c = match l with

[] -> [] | hd::tl -> f hd :: map f tl

b = b’ list b = b’’ list

b = b’’’ list

a = a

b = b’’’ list

a = b’’ -> a’

c = c’ list

a’ = c’

d list = c’ list

d list = c

finalconstraints:

Page 29: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Step3:SolveConstraints

let rec map (f:a) (l:b) : c = match l with

[] -> [] | hd::tl -> f hd :: map f tl

b = b’ list b = b’’ list

b = b’’’ list

a = a

b = b’’’ list

a = b’’ -> a’

c = c’ list

a’ = c’

d list = c’ list

d list = c

finalconstraints:[b' -> c'/a] [b' list/b]

[c' list/c]

finalsoluFon:

Page 30: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Step3:SolveConstraints

let rec map (f:a) (l:b) : c = match l with

[] -> [] | hd::tl -> f hd :: map f tl

[b' -> c'/a] [b' list/b]

[c' list/c]

finalsoluFon:

let rec map (f:b' -> c') (l:b' list) : c' list = match l with

[] -> [] | hd::tl -> f hd :: map f tl

Page 31: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Step3:SolveConstraints

let rec map (f:a) (l:b) : c = match l with

[] -> [] | hd::tl -> f hd :: map f tl

let rec map (f:a -> b) (l:a list) : b list = match l with

[] -> [] | hd::tl -> f hd :: map f tl

renamingtypevariables:

Page 32: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Step4:GeneratetypesGeneratetypesfromtypeschemes

–  OpFon1:pickaninstanceofthemostgeneraltypewhenwehavecompletedtypeinferenceontheenFreprogram

•  map:(int->int)->intlist->intlist

–  OpFon2:generatepolymorphictypesforprogrampartsandconFnue(polymorphic)typeinference

•  map:foralla,b,c.(a->b)->alist->blist

Page 33: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TypeInferenceDetailsTypeconstraintsaresetsofequaFonsbetweentypeschemes

–  q::={s11=s12,...,sn1=sn2}

–  eg:{b=b’list,a=b->c}

Page 34: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ConstraintGeneraFonSyntax-directedconstraintgeneraFon

–  ouralgorithmcrawlsoverabstractsyntaxofuntypedexpressionsandgenerates

•  atermscheme•  asetofconstraints

Algorithmdefinedassetofinferencerules:

–  G|--u=>e:t,q

contextannotatedexpressionunannotated

expression

type(scheme)

constraintsthatmustbesolved

gen:ctxt->exp->ann_exp*scheme*constraintsinOCaml:

Page 35: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ConstraintGeneraFonSimplerules:

–  G|--x==>x:s,{}(ifG(x)=s)

–  G|--3==>3:int,{}(sameforotherints)

–  G|--true==>true:bool,{}

–  G|--false==>false:bool,{}

Page 36: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Operators

G |-- u1 ==> e1 : t1, q1 G |-- u2 ==> e2 : t2, q2 ------------------------------------------------------------------------ G |-- u1 + u2 ==> e1 + e2 : int, q1 U q2 U {t1 = int, t2 = int}

G |-- u1 ==> e1 : t1, q1 G |-- u2 ==> e2 : t2, q2 ------------------------------------------------------------------------ G |-- u1 < u2 ==> e1 + e2 : bool, q1 U q2 U {t1 = int, t2 = int}

Page 37: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Ifstatements

G |-- u1 ==> e1 : t1, q1 G |-- u2 ==> e2 : t2, q2 G |-- u3 ==> e3 : t3, q3 ---------------------------------------------------------------- G |-- if u1 then u2 else u3 ==> if e1 then e2 else e3 : a, q1 U q2 U q3 U {t1 = bool, a = t2, a = t3}

Page 38: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

FuncFonApplicaFon

G |-- u1 ==> e1 : t1, q1 G |-- u2 ==> e2 : t2, q2 (for a fresh a) ---------------------------------------------------------------- G |-- u1 u2==> e1 e2 : a, q1 U q2 U {t1 = t2 -> a}

Page 39: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

FuncFonDeclaraFon

G, x : a |-- u ==> e : t, q (for fresh a) ---------------------------------------------------------------- G |-- fun x -> e ==> fun (x : a) -> e : a -> b, q U {t = b}

Page 40: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

FuncFonDeclaraFon

G, f : a -> b, x : a |-- u ==> e : t, q (for fresh a,b) ----------------------------------------------------------------------- G |-- rec f(x) = u ==> rec f (x : a) : b = e : a -> b, q U {t = b}

Page 41: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SolvingConstraints

AsoluFontoasystemoftypeconstraintsisasubs3tu3onS–  afuncFonfromtypevariablestotypes–  assumesubsFtuFonsaredefinedonalltypevariables:

•  S(a)=a(foralmostallvariablesa)•  S(a)=s(forsometypeschemes)

–  dom(S)=setofvariabless.t.S(a)≠a

Page 42: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SolvingConstraints

AsoluFontoasystemoftypeconstraintsisasubs3tu3onS–  afuncFonfromtypevariablestotypes–  assumesubsFtuFonsaredefinedonalltypevariables:

•  S(a)=a(foralmostallvariablesa)•  S(a)=s(forsometypeschemes)

–  dom(S)=setofvariabless.t.S(a)≠a

WecanalsoapplyasubsFtuFonStoafulltypeschemes. apply:[int/a,int->bool/b] to:b->a->b returns:(int->bool)->int->(int->bool)

Page 43: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

WecanapplyasubsFtuFonStoafulltypescheme:eg:apply[int/a,int->bool/b]tob->a->breturns:(int->bool)->int->(int->bool)

Page 44: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

WhenisasubsFtuFonSasoluFontoasetofconstraints?Constraints:{s1=s2,s3=s4,s5=s6,...}WhenthesubsFtuFonmakesbothsidesofallequaFonsthesame.Eg:

a=b->cc=int->bool

constraints:

Page 45: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

WhenisasubsFtuFonSasoluFontoasetofconstraints?Constraints:{s1=s2,s3=s4,s5=s6,...}WhenthesubsFtuFonmakesbothsidesofallequaFonsthesame.Eg:

a=b->cc=int->bool

b->(int->bool)/aint->bool/cb/b

constraints:

soluFon:

Page 46: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

WhenisasubsFtuFonSasoluFontoasetofconstraints?Constraints:{s1=s2,s3=s4,s5=s6,...}WhenthesubsFtuFonmakesbothsidesofallequaFonsthesame.Eg:

a=b->cc=int->bool

b->(int->bool)/aint->bool/cb/b

b->(int->bool)=b->(int->bool)int->bool=int->bool

constraints:

soluFon:

constraintswithsoluFonapplied:

Page 47: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

WhenisasubsFtuFonSasoluFontoasetofconstraints?Constraints:{s1=s2,s3=s4,s5=s6,...}WhenthesubsFtuFonmakesbothsidesofallequaFonsthesame.AsecondsoluFon

a=b->cc=int->bool

b->(int->bool)/aint->bool/cb/b

constraints:

soluFon1:

int->(int->bool)/aint->bool/cint/b

soluFon2:

Page 48: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

WhenisonesoluFonbeYerthananothertoasetofconstraints?

a=b->cc=int->bool

b->(int->bool)/aint->bool/cb/b

constraints:

soluFon1:int->(int->bool)/aint->bool/cint/b

soluFon2:

b->(int->bool)

typeb->cwithsoluFonapplied:

int->(int->bool)

typeb->cwithsoluFonapplied:

Page 49: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

SoluFon1is"moregeneral"–thereismoreflex.SoluFon2is"moreconcrete"WeprefersoluFon1.

b->(int->bool)/aint->bool/cb/b

soluFon1:int->(int->bool)/aint->bool/cint/b

soluFon2:

b->(int->bool)

typeb->cwithsoluFonapplied:

int->(int->bool)

typeb->cwithsoluFonapplied:

Page 50: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

SoluFon1is"moregeneral"–thereismoreflex.SoluFon2is"moreconcrete"Wepreferthemoregeneral(lessconcrete)soluFon1.Technically,wepreferTtoSifthereexistsanothersubsFtuFonUandforalltypest,S(t)=U(T(t))

b->(int->bool)/aint->bool/cb/b

soluFon1:int->(int->bool)/aint->bool/cint/b

soluFon2:

b->(int->bool)

typeb->cwithsoluFonapplied:

int->(int->bool)

typeb->cwithsoluFonapplied:

Page 51: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SubsFtuFons

ThereisalwaysabestsoluFon,whichwecanaprinciplesolu3on.ThebestsoluFonis(atleastas)preferredasanyothersoluFon.

b->(int->bool)/aint->bool/cb/b

soluFon1:int->(int->bool)/aint->bool/cint/b

soluFon2:

b->(int->bool)

typeb->cwithsoluFonapplied:

int->(int->bool)

typeb->cwithsoluFonapplied:

Page 52: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

MostGeneralSoluFons

Sistheprincipal(mostgeneral)soluFonofaconstraintqif–  S|=q(itisasoluFon)–  ifT|=qthenT<=S(itisthemostgeneralone)

Lemma:IfqhasasoluFon,thenithasamostgeneraloneWecareaboutprincipalsoluFonssincetheywillgiveusthemostgeneraltypesforterms

Page 53: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ComposiFonofSubsFtuFonsWewillneedtocomparesubsFtuFons:T<=S.eg:

–  T<=SifTis“morespecific”/"less general"thanS–  Ifthereisa

–  Formally:T<=SifandonlyifT=UoSforsomeU

Page 54: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ComposiFonofSubsFtuFonsComposiFon(UoS)appliesthesubsFtuFonSandthenappliesthesubsFtuFonU:

–  (UoS)(a)=U(S(a))WewillneedtocomparesubsFtuFons

–  T<=SifTis“morespecific”thanS–  T<=SifTis“lessgeneral”thanS–  Formally:T<=SifandonlyifT=UoSforsomeU

Page 55: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ComposiFonofSubsFtuFonsExamples:

–  example1:anysubsFtuFonislessgeneralthantheidenFtysubsFtuFonI:

•  S<=IbecauseS=SoI–  example2:

•  S(a)=int,S(b)=c->c•  T(a)=int,T(b)=int->int•  weconclude:T<=S•  ifT(a)=int,T(b)=int->boolthenTisunrelatedtoS(neithermorenorlessgeneral)

Page 56: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

SolvingaConstraint

S|=qifSisasoluFontotheconstraintsq

S(s1) = S(s2) S |= q ----------------------------------- S |= {s1 = s2} U q

---------- S |= { }

anysubsFtuFonisasoluFonfortheemptysetofconstraints

asoluFontoanequaFonisasubsFtuFonthatmakesletandrightsidesequal

Page 57: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ExamplesExample1

–  q={a=int,b=a}–  principalsoluFonS:

Page 58: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ExamplesExample1

–  q={a=int,b=a}–  principalsoluFonS:

•  S(a)=S(b)=int•  S(c)=c(forallcotherthana,b)

Page 59: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ExamplesExample2

–  q={a=int,b=a,b=bool}–  principalsoluFonS:

Page 60: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ExamplesExample2

–  q={a=int,b=a,b=bool}–  principalsoluFonS:

•  doesnotexist(thereisnosoluFontoq)

Page 61: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

UnificaFonUnificaFon:AnalgorithmthatprovidestheprincipalsoluFontoasetofconstraints(ifoneexists)

–  UnificaFonsystemaFcallysimplifiesasetofconstraints,yieldingasubsFtuFon

•  StarFngstateofunificaFonprocess:(I,q)•  FinalstateofunificaFonprocess:(S,{})

Page 62: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

UnificaFonMachineWecanspecifyunificaFonasatransiFonsystem:Basetypes&simplevariables:

(S,{bool=bool} U q) -> (S, q)

(S,{a=a} U q) -> (S, q)

(S,{int=int} U q) -> (S, q)

(S1, q1) -> (S2, q2)

Page 63: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

UnificaFonMachineFuncFons:VariabledefiniFons

(S, {s11 -> s12 = s21 -> s22} U q) -> (S, {s11 = s21, s12 = s22} U q)

(S, {a=s} U q) -> ([a=s] o S, [s/a]q)

(S, {s=a} U q) -> ([a=s] o S, [s/a]q)

when a is not in FreeVars(s)

breaksdownintosmallerconstraints

do S and then do [a=b]

Page 64: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

OccursCheck

Recallthisprogram:Itgeneratesthetheconstraints:a->a=aWhatisthesoluFonto{a=a->a}?

funx->xx

Page 65: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

OccursCheck

Recallthisprogram:Itgeneratesthetheconstraints:a->a=aWhatisthesoluFonto{a=a->a}?Thereisnone!

the"occurscheck"

(S, {s=a} U q) -> ([a=s] o S, [s/a]q)

"when a is not in FreeVars(s)"

funx->xx

Page 66: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

IrreducibleStatesWhenalltheconstraintshavebeenprocessed,wewin!ButsomeFmeswegetstuck,withanequaFonlikethis

–  int=bool–  s1->s2=int–  s1->s2=bool–  a=s(scontainsa)–  orissymmetrictooneoftheabove

Stuckstatesarisewhenconstraintsareunsolvable&theprogramdoesnottypecheck.

(S1, q1) -> (S2, q2) -> (S3, q3) ... -> (Sn, { })

Page 67: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TerminaFonWewantunificaFontoterminate(togiveusatypereconstrucFonalgorithm)Inotherwords,wewanttoshowthatthereisnoinfinitesequenceofstates

–  (S1,q1)->(S2,q2)->...

Page 68: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TerminaFon

Weassociateanorderingwithconstraints–  q<q’ifandonlyif

•  qcontainsfewervariablesthanq’•  qcontainsthesamenumberofvariablesasq’butfewertypeconstructors(ie:feweroccurrencesofint,bool,or“->”)

–  Thisisalexacographicordering•  wecanprove(bycontradicFon)thatthereisnoinfinitedecreasingsequenceofconstraints

Page 69: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

TerminaFon

Lemma:Everystepreducesthesizeofq–  Proof:Bycases(ie:inducFon)onthedefiniFonofthereducFonrelaFon.

-------------------------------- (S,{int=int} U q) -> (S, q) ------------------------------------ (S,{bool=bool} U q) -> (S, q)

----------------------------- (S,{a=a} U q) -> (S, q)

---------------------------------------------- (S,{s11 -> s12= s21 -> s22} U q) -> (S, {s11 = s21, s12 = s22} U q)

------------------------ (a not in FV(s)) (S,{a=s} U q) -> ([a=s] o S, [s/a]q)

Page 70: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

CompleteSoluFonsAcompletesoluFonfor(S,q)isasubsFtuFonTsuchthat

–  T<=S–  T|=q

Page 71: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ProperFesofSoluFonsLemma1:

–  Everyfinalstate(S,{})hasacompletesoluFon.•  ItisS:

–  S<=S–  S|={}

Page 72: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ProperFesofSoluFonsLemma2

–  NostuckstatehasacompletesoluFon(oranysoluFonatall)•  itisimpossibleforasubsFtuFontomakethenecessaryequaFonsequal

–  int≠bool–  int≠t1->t2–  ...

Page 73: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

ProperFesofSoluFonsLemma3

–  If(S,q)->(S’,q’)then•  Tiscompletefor(S,q)iffTiscompletefor(S’,q’)•  proofby?•  intheforwarddirecFon,thisisthepreservaFontheoremfortheunificaFonmachine!

Page 74: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Summary:UnificaFonByterminaFon,(I,q)->*(S,q’)where(S,q’)isirreducible.Moreover:

–  Ifq’={}then•  (S,q’)isfinal(bydefiniFon)•  SisaprincipalsoluFonforq

–  ConsideranyTsuchthatTisasoluFontoq.–  NownoFce,Siscompletefor(S,q’)(bylemma1)–  Siscompletefor(I,q)(bylemma3)–  SinceSiscompletefor(I,q),T<=SandthereforeSisprincipal.

Page 75: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Summary:UnificaFon(cont.)...Moreover:

–  Ifq’isnot{}(and(I,q)->*(S,q’)where(S,q’)isirreducible)then

•  (S,q)isstuck.Consequently,(S,q)hasnocompletesoluFon.Bylemma3,even(I,q)hasnocompletesoluFonandthereforeqhasnosoluFonatall.

Page 76: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

MORETYPEINFERENCE

76

Page 77: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

letGeneralizaFon

Wheredoweintroducepolymorphicvalues?Andplacex:foralla1,...,an.sinthecontext.

let x = v ==> let x : forall a1,..,an.s = v

if v : s and a1,...,an are the variables of s

Page 78: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

letGeneralizaFon

Wheredoweintroducepolymorphicvalues?Andplacex:foralla1,...,an.sinthecontext.Whereandhowdoweuseapolymorphicvalue?

let x = v ==> let x : forall a1,..,an.s = v

G |- x ==> x : s[b1/a1,...,bn/an), {}

if v : s and a1,...,an are the variables of s

when G(x) = forall a1,...,an.s and b1,...,bn are fresh

Page 79: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Whatisthecostoftypeinference?

InpracFce?LinearinthesizeoftheprogramIntheory,DEXPTIME-complete.Why?BecausewecangenerateaprogramthathasatypethatisexponenFallylarge:

let f1 x = x f1 : a -> a let f2 = f1 f1 f2 : (a -> a) -> (a -> a) let f3 = f2 f2 f3 : ((a -> a) -> (a -> a)) ->

((a -> a) -> (a -> a)) let f4 = f3 f3 ...

Page 80: Type Inference - Princeton University Computer Science...Language Design for Type Inference We call this type the principle type (scheme) for map. Any other ML-style type you can give

Summary:TypeInferenceGivenacontextG,anduntypedtermu:

–  Finde,t,qsuchthatG|-u==>e:t,q

–  FindprincipalsoluFonSofqviaunificaFon•  ifnosoluFonexists,thereisnoreconstrucFon

–  ApplyStoe,ieoursoluFonisS(e)•  S(e)containsschemaFctypevariablesa,b,c,etcthatmaybeinstanFatedwithanytype

–  SinceSisprincipal,S(e)characterizesallreconstrucFons.

–  Ifdesired,usethetypecheckingalgorithmtovalidate