Type Inference - Princeton University Computer Science...Language Design for Type Inference We call...
Transcript of Type Inference - Princeton University Computer Science...Language Design for Type Inference We call...
![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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/33.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/35.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/36.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/37.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/38.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/39.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/40.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/41.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/42.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/43.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/44.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/45.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/46.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/47.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/48.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/49.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/50.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/51.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/52.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/53.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/54.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/55.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/56.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/57.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/58.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/59.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/60.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/61.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/62.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/63.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/64.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/65.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/66.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/67.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/68.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/69.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/70.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/71.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/72.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/73.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/74.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/75.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/76.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/77.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/78.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/79.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052012/6029601193f9f64a1a082d2d/html5/thumbnails/80.jpg)
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