olymorphic ype Inference Mic hael I
129K - views

olymorphic ype Inference Mic hael I

Sc artzbac httpwwwdaimiaudkmis Mar ch 1995 Preface In this lecture will presen tin functional language and gradually enric its yp e system shall co er the basic CurryHindley system and ands constrain tbased algorithm for monomorphic yp e

Download Pdf

olymorphic ype Inference Mic hael I

Download Pdf - The PPT/PDF document "olymorphic ype Inference Mic hael I" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation on theme: "olymorphic ype Inference Mic hael I"— Presentation transcript:

Page 1
olymorphic ype Inference Mic hael I. Sc artzbac http://www.daimi.au.dk/~mis Mar ch 1995 Preface In this lecture will presen tin functional language and gradually enric its yp e system. shall co er the basic Curry-Hindley system and and's constrain t-based algorithm for monomorphic yp e inference; brie y observ the Curry-Ho ard isomorphism and notice that logical for- malism ma serv as the inspiration for new yp e rules; presen the p oly- morphic Milner system and the Damas-Milner algorithm for p olymorphic yp e inference; see the Milner-Mycroft system for p olymorphic

recursion; and sk etc the dev elopmen of higher yp e systems. will touc up on the relationship bet een yp es and logic and sho ho rules from logic ma giv inspiration for new yp e rules. En route shall encoun ter the curious disco ery that algorithmic problems for yp e systems, whic ha b een implemen ted in p opular programming languages, ha turned out to be resp ectiv ely complete for exp onen tial time and undecidable.
Page 2
ype Chec king and ype Inference Tw main reasons for in tro ducing yp es in to programming language are safety and adability Let us rst consider the safet y asp

ect. Not all expressions in an un yp ed language are though to be sensible. or example, computing the quotien of text and function or the square ro ot of banana is plain nonsense. Let us agree up on the existence of subset of ad expr essions the run- time b eha viors of whic are undesirable or unde ned. An non-trivial hoice of badness leads to an uncomputable subset. Th us, the compiler cannot distinguish the good from the bad. o ensure safet y of programs w ein tro duce a t yp e system and reluctan tly sacri ce some go o d expressions. This happ ens b ecause e require our t yp e system to

ensure that only good expressions are ypable. Ho ev er, since yp e hec king m ust b e decidable, it is clear that some go o d expressions are not ypable. The follo wing diagram illustrates this situation.   slack typ able expr essions ad expr essions The slack in yp e system is the set of good expressions that it unfairly rejects. The desire to minimize this slac is driving force in the dev elop- men of yp e systems, whic leads to ards ev er more complex yp e rules. This trend is curb ed the fact that the yp e discipline should not en- cum ber the actual programming to o uc h. This last

concern is the reason wh arithmetical errors suc as index out of range and division b zero are not prev en ted most yp e systems. The issue of readabilit wishes for the yp es to be succinct hin ts to the seman tics of expressions. or example, when trying to understand the orkings of a function, it is quite nice to kno win adv ance that it computes b o oleans from lists of in tegers. This leads to ards more expressiv and in tuitiv yp es|limited the need for decidabilit of yp e hec king. The ideal situation seems to arise when our yp e system admits au- tomatic inference of yp es. Then obtain

all the adv an tages describ ed ab o e, without the need to b other with the yp e rules ourselv es.
Page 3
In the follo wing sections w e study the basic dev elopmen tof t yp e systems for functional languages. Tin unctional Language de ne tin anilla- a ored functional language to serv as the basis for this presen tation. It is designed to resem ble the core of languages suc as Scheme Mirand or ML. Exp ::= true false red( Exp succ( Exp iszero( Exp cons( Exp Exp ca r( Exp cdr( Exp nil null( Exp if Exp then Exp else Exp fun(x) Exp end Exp Exp let Exp in Exp end Th us, w eha ein tegers,

b o oleans, lists and functions. The let -construct de- nes lo cal scop e and at the same time p ermits us to giv e simple recursiv de nitions. The seman tics of this language is en tirely standard and will not be formalized. Note, though, that there are arious expressions that in tu- itiv ely recognize as meaningless, suc h as: succ(true) false(0) , and ca r(fun(x) end) These are the bad expressions that our yp e system ust exclude. Exercise 2.1 Pr gr am length function on lists. Simple ype System ho ose language of yp es that describ es the di eren alues of our expressions: in tegers, b o

oleans, lists and functions. Type ::= Int Bo ol list( Type Type Type ust no decide on the rules for assigning yp es to expressions. Some are ery easy; for example, true is of yp e Bo ol and fun(x) succ(x) end is of yp e Int Int Bad expressions are excluded b y imp osing t yp e restrictions; for example, cdr only orks on lists, the branc hes of an if -expression ust ha the same yp e, and in function calls the formal and actual argumen ts yp es ust be equal.
Page 4
It ould not be hard to write do wn few man ual pages describing this yp e system. Ho ev er, ho ose an extremely compact

notation that is inspired connection bet een yp es and logic that shall explore further in later section. yp e judgment is of the form: Here is sym bol table, i.e., mapping from iden ti ers to yp es; is an expression; and is yp e. Its meaning is simply: relativ ely to the sym bol table the expression has yp e yp e judgmen ts are com bined in to yp e rules of the form: ...; Its meaning is: if the judgmen ts all hold, then so do es the judgmen Using these notational con en tions, can express all asp ects of our yp e system in the follo wing manner. Int true Bo ol false Bo ol Int red(e) Int Int

succ(e) Int Int iszero(e) Bo ol list( cons(e ,e list( list( ca r(e) list( cdr(e) list( nil list( list( null(e) Bo ol Bo ol; if then else ,x fun(x) end (e ..., ... ,f ,f let in end
Page 5
This is the Curry-Hindley yp e system dating bac to the 1960s. Exercise 3.1 What is the r^ ole of the rule in the lower left-hand orner? Exercise 3.2 How an we tel l that the let -c onstruct al lows cursive de - nitions? The claim that giv en expression has particular yp e can be eri ed systematic (and seemingly p edan tic) application of the ab o rules. Consider for example the function: fun(g)

fun(x) succ(g(x)) end end wish to sho that it has yp e (Bo ol Int) (Bo ol Int) formal deriv ation lo oks as follo ws. Bool Int,x: Bool Bool Int g: Bool Int, x: Bool x: Bool g: Bool Int, x: Bool g(x): Int g: Bool Int, x: Bool succ(g(x)) Int g: Bool Int fun(x)succ(g(x)) end: Bool Int fun(g)fun(x)succ(g(x))end end (Bool Int) (Bool Int) Exercise 3.3 Go thr ough the ab ove derivation b ottom-up and explain which typ rules ar use d. It is quite easy to implemen t a top-do wn t yp e c hec er based on suc h deriv a- tions, since the outermost t yp e constructor and expression op erator alw ys

determine the yp e rule that ust be applied. this p oin should pause to consider whether our yp e system is sound in the sense that only good expressions are ypable. sa an ything ab out this, ould need formal seman tics of our language and sp eci cation of go o dness. The usual strategy is then to sho prop ert y called subje ct duction an expression of t yp e can only ev aluate to alue of yp e Soundness then follo ws, since bad expressions do not ev aluate to alues of an yp e.
Page 6
Simple ype Inference As explained, do not an to pro vide the yp es explicitly Rather, wish for the

compiler to nd appropriate ypings of our expressions or to inform us if none exists. This spares us the trouble of writing do wn complicated yp e expression that often only clutter up our nice programs. Another often cited adv an tage is the hop e that clev er yp e inference algorithm ma come up with more general yp es than ourselv es could, th us eeping the slac to an absolute minim um. An ob vious idea is to use b ottom-up pro cedure that tra erses the parse tree and computes the yp e of ev ery sub expression. But this only orks for constan expressions without ariables. As trivial coun ter-

example consider the term: succ(x) In strict b ottom-up pro cedure ust rst ho ose yp e for But unless happ en to select Int will mistak enly conclude that the expression is not ypable. e can still use a b ottom-up pro cedure, but w e should collect al l yp es, rather than just single sp ecimen. Ho ev er, ust someho deal with the p ossibilit y that expressions ma yha in nitely man yt yp es. or exam- ple, the function: fun(g) fun(x) succ(g(x)) end end for whic earlier deriv ed the yp e (Bo ol Int) (Bo ol Int) has in fact ev ery p ossible yp e of the form Int) Int) But this example ha already giv

en the solution to our problem: an in nite set of yp es sometimes has nite sym b olic represen tation. de ne typ scheme to be yp e that ma con tain o ccurrences of yp e ariables. ormally the syn tax is as follo ws. Types ::= Int Bo ol list( Types Types Types ar ar ::= ... yp e sc heme de nes set of yp es: those that can be obtained sub- stituting t yp es for ariables in consisten manner. or example, the yp e
Page 7
sc heme list( de nes the follo wing in nite set: (Int Int) list(Int) (Bo ol Int) list(Bo ol) (Bo ol Bo ol) list(Bo ol) (Int Bo ol) list(Int) ((Int Bo ol) list(Bo ol ))

list(Int Bo ol) The only requiremen is that b oth o ccurrences of ust be substituted with the same yp e. If also allo ariables to be substituted with other yp e sc hemes, then obtain preorder on yp e sc hemes, whic holds exactly when is obtained from through substitution. Exercise 4.1 What is pr or der? Exercise 4.2 Which airs among the fol lowing typ schemes ar or der d? list( Exercise 4.3 gue that when then the set of typ es determine by is sup erset of that determine by The ey to an ecien yp e inference algorithm is the follo wing fundamen- tal observ ation: for ev ery t ypable expression

there is a t yp e sc heme that exactly de nes all p ossible yp es of call the princip al typ scheme for The task of yp e inference is then to construct the principal yp e sc heme (for ev ery sub expression) or to fail if the expression is un ypable. e presen t the algorithm in the st yle of W and [11 ] from 1987. or ev ery parse tree no de in tro duce yp e ariable whic denotes the (as et) unkno wn yp e sc heme for the sub expression ro oted in capture scop e rules correctly require that all parse tree no des corresp onding to o ccurrences of the same iden ti er in scop e ust share the same yp

e ariable. In examples b elo w, use the sligh tly am biguous notation
Page 8
for the yp e ariable corresp onding to the sub expression Exercise 4.4 Why is this notation ambiguous? Ev ery kind of expression imp oses its wn kind of constrain ts on yp e ari- ables. Consider the case of function application (e kno from the yp e rule for application that the follo wing equation ust hold. ]= (e That is, ust be function that accepts argumen ts of the same yp e as The full collection of constrain ts is as follo ws. Int true true Bo ol false false Bo ol red(e) red(e) ]= Int succ(e) succ(e)

Int iszero(e) iszero(e) Bo ol Int cons(e ,e cons(e ,e list( ca r(e) list( ca r(e) cdr(e) cdr(e) ]= list( nil nil list( null(e) null(e) Bo ol list( if then else if then else ]= ]= Bo ol fun(x) end fun(x) end (e (e let in end let in end ]= In the ab o e, assume that eac is fresh ariable distinct from all others. no kno ho to generate the constrain ts for an expression. or example, the expression: fun(g) fun(x) succ(g(x)) end end
Page 9
yields the follo wing constrain ts. fun(g) fun(x) succ(g(x)) end end fun(x) succ(g(x)) end fun(x) succ(g(x)) end succ(g(x)) succ(g(x)) g(x) Int g(x)

Exercise 4.5 Explain the origin of ach of the ab ove onstr aints. are then left with the problem of solving these constrain ts. ust assign to eac yp e ariable yp e sc heme suc that all constrain ts are satis ed. ortunately this is exactly the ell-kno wn uni c ation pr oblem It has an ecien linear-time algorithm that ev en computes solution consisting of unique principal yp e sc hemes. The ab o example has the follo wing solution. fun(g) fun(x) succ(g(x)) end end Int) Int) fun(x) succ(g(x)) end Int succ(g(x)) Int g(x) Int Int Exercise 4.6 erify that the ab ove is inde solution. no presen

simple algorithm for solving constrain ts. The constrain system is giv en as The solution is function from t yp e ariables to yp e sc hemes. use the notation to indicate the yp e sc heme obtained from substituting ev ery yp e ariable with its alue in If and are yp e sc hemes, then is the set of subtrees on whic they rst di er in preorder tra ersal. let (x while is not solution do let s,t (S(x ),S( )), where S(x S( ); if and re b oth yp e constructo rs then fail ; Assume s=x if o ccurs in then fail ; let (x 7! (x )) end
Page 10
This algorithm, whic as presen ted Robinson [10 in 1965,

runs in exp onen tial time. The linear time algorithm is from 1978 aterson and egman [9 ]. Exercise 4.7 Gener ate and solve the typ onstr aints for the fol lowing expr ession. fun(x) fun(y) x((ca r(y))(x)) end end When an expression is un ypable, then obtain an unsolv able collection of constrain ts. Consider the susp ect expression: cons(succ(x),ca r(x)) It generates these constrain ts. cons(succ(x),ca r(x)) list succ(x) ca r(x) list succ(x) list ca r(x) succ(x) Int Int Exercise 4.8 gue that the ab ove onstr aints ar unsolvable. ypes and Logic Earlier, the p eculiar notation for t yp e rules

w as blamed on their connection with lo gic or ery pure yp e systems, this connection is kno wn as the Curry-Howar isomorphism It is so famous that shall frame it. yp es ar formulas, and expr essions ar pr ofs. The isomorphism bet een yp es and form ulas is simply syn tactic: Int and Bo ol corresp ond to facts and the yp e constructor corresp onds to the logical connectiv The relationship b et een expressions and pro ofs is 10
Page 11
bit more subtle, but w e only need to kno w its main consequence: a form ula is alid if and only if the corresp onding yp e is not empt urthermore, our

yp e rules mirror exactly the inference rules from logic. or example, there are rules in olving the function yp e: ,x fun(x) end (e If ignore the expressions and apply the isomorphism, then immedi- ately recognize familiar rules from logic: These are resp ectiv ely de duction and mo dus onens or example, consider the form ula ( (( )). Is this a tautology? es, b ecause the expression: fun(x) fun(y) fun(z) x(y(z)) end end end has the principal yp e sc heme )) Exercise 5.1 Why must typ scheme inferr for an expr ession ne es- sarily tautolo gy? Exercise 5.2 Complete the pr of that (( )) is tautolo

gy. In con trast, common sense tells us that the form ula is not tautology whic means that no expression has principal yp e sc heme Exercise 5.3 gue that no expr ession has the princip al typ scheme oncile this with the fact that e.g. the expr es- sion: 11
Page 12
fun(x) fun(y) end end has typ (Int Bo ol) (Bo ol Int) Notice that, from logical p ersp ectiv e, yp e inference is rather bizarre activit y: ha pro of and are lo oking for the corresp onding form ula. Our tin functional language so far excludes pro duct and sum yp es. Using the Curry-Ho ard isomorphism, can easily gure out

ho to add them. The usual rules from logic for conjunction giv us the rules for pro ducts: pair(e ,e fst(e) snd(e) There are certainly no surprises here. Similarly ha an isomorphism bet een sums and disjunction: left(e right(e decide(e,e ,e This should b e rather familiar from a programming language p oin t of view. Ho ev er, closer scrutin rev eals that ha obtained some un usual logi- cal rules: pro of of disjunction ust necessarily disclose whic disjunct is b eing pro ed. This is not true for standard logic, whic allo ws pro ofs to be considerably more indirect. The somewhat eak er logic

that corre- sp onds exactly to yp e theory is kno wn as intuitionistic lo gic and has in fact b een prop osed logicians for purely philosophical reasons. Exercise 5.4 What ar the typ infer enc onstr aints for these new ex- pr essions? Exercise 5.5 Find typ whose tr anslation into lo gic is classic al tau- tolo gy but which do not have any expr essions and thus do es not yield an intuitionistic tautolo gy. It is harder to see ho w the t yp e rules for lists resem ble logic in an yw But 12
Page 13
this is only b ecause ha hosen the wrong op erators. In the follo wing ersion see clearly

the logical origins of lists. list( cons(e ,e list( base step induct(base,step) list Th us, lists are all ab out ounting and induction The induct op erator is astly more general than ca and cdr or example, the length op erator is easily de ned as: induct(0,fun(x) succ(fst(x)) end) Exercise 5.6 Use induct to de ne the op er ators ca and cdr Still, the practical adv an tage of the connection bet een yp es and logic is that ha source of inspiration for missing yp e rules. Consider no el yp e constructor stream( whic de nes in nite lists of -v alues. Suc streams are in certain formal sense the

dual concept of lists. Cor- resp ondingly the dual concept of induction is called oinduction Lo oking up its de nition, magically get the follo wing yp e rules for streams. base step coinduct(base,step) stream stream ca r(e) cdr(e) stream The coinduct op erator constructs streams|not inductiv ely but coinduc- tiv ely And w e migh tas w ell use the term ounting to describ e the actions of ca and cdr Similar (co)induction principles are de ned for arbitrary (monotone) recursiv yp es. Exercise 5.7 Use coinduct to onstruct the str am (0,1,2,3,4,. . . ) Exercise 5.8 De ne the typ rules for nite and

in nite binary tr es. olymorphic ypes The expression b elo seems at rst glance quite reasonable, but it is not ypable. It is in fact an example of slac in our yp e system. 13
Page 14
let fun(x) cons(x,nil) end in pair(f(0),f(true)) The problem is of course that ust assign single yp e to the function need instead the usual notion of p olymorphism. or this purp ose extend our yp e sc hemes as follo ws. Types ::= Int Bo ol list( Types Types Types ar ar ::= ... Pol ::= ar Pol Types The p olymorphic t yp e : list describ es function that will accept an argumen of an yp e and yield as

result list of alues of that giv en yp e. This is exactly the yp e an for the function ab o e. will only allo p olymorphism in connection with let -de nitions. The use of a univ ersal quan ti er hin ts at another connection with logic. Indeed, the yp es rules for p olymorphic functions are those from logic, except that the la er of syn tax is rather thic er this time. ,f ,f : let in end 62 : Here indicates sev eral t yp e v ariables. The requiremen 62 means that ust not con tain an assumptions ab out The requiremen means that is sp ecialization of as describ ed in Section (except that only

ariables from ma be substituted). This yp e system as in tro duced Milner [7 in 1978. Exercise 6.1 What is the onne ction with lo gic? Here is formal deriv ation sho wing that the earlier example expression is ypable. Unfortunately it is so large that w em ust cut it in to sev eral pieces. 14
Page 15
f: list( fun(x) ...end list( f: list( pair( ...) list(Int) list(Bool) let f= fun(x)cons(x,nil)end in pair(f(0),f(true)): list(Int) list(Bool) The -piece is concerned with yping the function list( ),x: x: f: list( ),x: nil: list( f: list( ),x: cons(x,nil): list( The -piece is concerned

with yping the pair -expression. f: list( list( f: list( f: Int list(Int) f: list( 0:Int f: list( f(0): list(Int) Exercise 6.2 Complete the -pie and verify ach step in the ab ove derivation. Wh is the requiremen 62 necessary? If remo it, the function: fun(x) let in end can be sho wn to ha yp e sc heme Int This is manifestly false, since the function acts as the iden tit The purp orted deriv ation is as follo ws. x: ,f x: x: ,f: : : x: ,f: : Int let f=x inf: Int fun(x)let f= xinf end Int Exercise 6.3 Catch the err or in the ab ove derivation. Exercise 6.4 What is the orr esp onding lo gic al

fal lacy? 15
Page 16
olymorphic ype Inference The task of yp e inference seems to b ecome astly more complex with the in tro duction of the p olymorphic let -construct. Ho ev er, there is certainly a ery na v e idea that w can fall bac k on. Using a simple syn tactic transfor- mation that unfolds let -de nitions, can expand p olymorphic program in to an equiv alen monomorphic ersion. or the expression: let fun(x) cons(x,nil) end in pair(f(0),f(true)) obtain the equiv alen ersion: pair let fun(x) cons(x,nil) end in f(0) let fun(x) cons(x,nil) end in f(true) whic certainly is ypable

in the monomorphic yp e system. The only disadv an tage is that the expanded ersion of an expression ma be exp o- nen tially larger than the original. Exercise 7.1 Find an example wher pr gr am of size exp ands to one of size (2 Exercise 7.2 gue that the exp ande version is typ able in the monomor- phic system if and only if the original version is typ able in the olymorphic system. Can do b etter than this exp onen tial algorithm? An ob vious idea is to use form of dynamic programming where the principal yp e sc hemes of let -de nitions are sa ed, so that they need only be computed once. This

is essen tially the Damas-Milner [1 ] algorithm from 1982 whic h is implemen ted in the ML system. or almost ten ears it as folklore that this algorithm had lo p olynomial time complexit This also corresp onded ell with the practical exp eriences of ML programmers. This b elief w as thorougly shattered when Kfoury , Tiuryn, and Urzyczyn [4 and Mairson [6 in 1989 sim ultaneously pro ed that the p olymorphic yp e inference problem is complete for exp onen tial time. This means that an correct implemen tation ust use an exp onen tial amoun of time on 16
Page 17
in nitely man inputs. His

pro of is an esome construction of an ML program that directly sim ulates a giv en deterministic T uring mac hine run- ning in exp onen tial time, and where ypabilit of the program coincides with acceptance the mac hine. PSP CE-hardness alone as pro ed Kanellakis and Mitc hell [3 earlier in 1989. In spite of this nast result, implemen tations of the ML language seem to be running ery ell in ev eryda life. But danger lurks b eneath the surface. As concrete example, regard the follo wing ML program. fun pair fn => y; let val x1=fn => pair in let val x2=fn => x1(x1(y)) in let val x3=fn =>

x2(x2(y)) in let val x4=fn => x3(x3(y)) in x4(fn z=>z) end end end end; Its principal yp e sc heme is: (((((((((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> ((((((((('a -> 'a) -> ('a

-> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> 'f) -> 'f) -> ((((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) ->

'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> ((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 17
Page 18
'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) ->

'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> 'f) -> 'f) -> 'g) -> 'g) -> ((((((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) ->

'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> ((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> 'f) -> 'f) -> ((((((((((('a -> 'a)

-> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> ((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) ->

((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> 'f) -> 'f) -> 'g) -> 'g) -> 'h) -> 'h) -> ((((((((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a)

-> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> ((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) ->

((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 18
Page 19
'e) -> 'e) -> 'f) -> 'f) -> ((((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> ((((((((('a

-> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> 'f) -> 'f) -> 'g) -> 'g) -> ((((((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a

-> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> ((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a ->

'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> 'f) -> 'f) -> ((((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) ->

'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> ((((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> ((((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> ((((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> ((('a -> 'a) -> ('a -> 'a) -> 'b) -> 'b) -> 'c) -> 'c) -> 'd) -> 'd) -> 'e) -> 'e) -> 'f) -> 'f) -> 'g) -> 'g) -> 'h) -> 'h) ->

'i) -> 'i Note that the analogy describ ed in Section 5, the ab o is gigan tic tautology in prop ositional logic. 19
Page 20
olymorphism and Recursion ha only allo ed p olymorphism in let -de nitions, and ev en there ha some limitations. or example, the follo wing reasonable expression is not ypable|w ha some slac in the p olymorphic yp e system. let fun(i) fun(x) if iszero(i) then else f(p red(i))(pair(x,x)) end end in f(87)(true) Exercise 8.1 gue that the ab ove expr ession is not typ able in the oly- morphic system. Exercise 8.2 What is the esult of the ab ove expr ession? There

are also less con triv ed examples sho wing that this restriction is practical uisance. The general problem is that function is only p oly- morphic from the outside, so to sp eak. Ho ev er, this can be remedied minor hange in our yp e rule. ,f : ,f : let in end 62 Exercise 8.3 What has en change in the typ rule? This is the Milner-Mycroft [8 yp e system from 1984. corresp onding yp e inference algorithm m ust necessarily be more obscure than the earlier one, since the na v expansion of let -de nitions ma no yield in nite monomorphic ersions. et, implemen tatio ns did exist for ersions of

the ML language. Th us it caused some concern when Henglein [2 and Kfoury Tiuryn, and Urzyczyn [5 in 1990 sim ultaneously pro ed that the yp e inference problem is in fact undecidable. The problem seems to ob ey the la ws of carto on ph ysics, since p eople ere p erfectly happ with the prop osed implemen tations b efore the unde- cidabilit yw as kno wn. The reason is that the semi-algorithm terminates for 20
Page 21
all ypable expressions and only fails to terminate for ery small fraction of un ypable expressions. Higher ype Systems By no means do es the dev elopmen of yp e systems

stop here. There are at least three good reasons to push on. Firstly there is alw ys more slac to pic up. Man in teresting yp e rules ha b een motiv ated anno ying examples of unfairly rejected ex- pressions. One example is the onjunctive yp e written whic means that sim ultaneously has yp es and Exercise 9.1 Suggest rules for onjunctive typ es. How do they di er fr om pr ducts? olymorphism ma also be included as an orthogonal feature in the yp e system. Th us yp es ma lo ok lik e: : : This is the system F2 studied Girard and Reynolds. Suc extensions are naturally bey ond the reac of yp e

inference. Exercise 9.2 Suggest an expr ession that ould have the olymorphic typ : : Secondly migh stra from purely functional language. Man e orts ha e b een directed to ards incorp orating non-functional features in to ML- st yle t yp e systems. or example, the mixture of p olymorphism and p oin ters is ery complicated and often requires subtle yp e rules. Exercise 9.3 What is wr ong with these simple rules for ointers (assume the obvious semantics for the expr essions)? ref(e) p ointer( e: p ointer( deref(e) p ointer( ); p ointer( x:=e p ointer( Thirdly it is p ossible to ha yp es that

capture more of the seman tics of expressions. This is done emphasizing the logical connection and building yp e systems so ric that one can de ne e.g. yp e whose alues are just the sorting functions on in teger lists. this p oin yp e hec king 21
Page 22
is equiv alen to program eri cation and, hence, undecidable. This means that eac h program m ust include a hand-written but compiler-c hec ed pro of of its yp e-correctness. References [1] Luis Damas and Robin Milner. Principal yp e sc hemes for functional programming. In 9th Symp osium on Principles of Pr gr amming an- guages 1982.

[2] F. Henglein. yp e inference with p olymorphic recursion. CM ans- actions on Pr gr amming anguages and Systems 15, 1993. [3] . Kanellakis and J. Mitc hell. olymorphic uni cation and ML t yping. In 16th Symp osium on Principles of Pr gr amming anguages CM Press, Jan uary 1989. [4] A. J. Kfoury J. Tiuryn, and Urzyczyn. Ml ypabilit is DEXPTIME-complete. In 15th Col lo quium on es in lgebr and Pr gr amming Springer-V erlag, Ma 1990. [5] A. J. Kfoury J. Tiuryn, and Urzyczyn. yp e reconstruction in the presence of p olymorphic recursion. CM ansactions on Pr gr am- ming anguages and Systems 15,

1993. [6] Harry G. Mairson. Decidabilit of ML yping is complete for de- terministic exp onen tial time. In 17th Symp osium on Principles of Pr gr amming anguages CM Press, Jan uary 1990. [7] Robin Milner. theory of t yp e p olymorphism in programming. Jour- nal of Computer and System Scienc es 17, 1978. [8] A. Mycroft. olymorphic yp e sc hemes and recursiv de nitions. In 6th International Confer enc on Pr gr amming Springer-V erlag, 1984. [9] M. S. aterson and M. N. egman. Linear uni cation. Journal of Computer and System Scienc es 16, 1978. [10] J. A. Robinson. mac hine-orien ted logic based

on the resolution principle. Journal of the CM 12, 1965. [11] M. and. simple algorithm and pro of for yp e inference. unda- mentae Informatic ae X, 1987. 22
Page 23
10 Problems Problem 10.1 Giv formal deriv ation sho wing that the expression: let Double fun(x) if iszero(x) then else succ(succ(Double(p red(x)))) end in Double(succ(0)) has yp e Int Problem 10.2 Infer the principal yp e sc heme for this expression: fun(x) fun(y) cons(ca r(x),y(true)) end end Problem 10.3 List all constan expressions ha ving these p olymorphic yp es. : : list( : : : Problem 10.4 Oyster of the eek. Here

are the rules for the existen tial quan ti er in logic. : : 62 A; 62 The notation means with all o ccurrences of substituted with Explain the logical con ten of these rules. In tro duce an existen tial quan ti er in to our yp e system, along with reasonable set of expressions, and recognize an imp ortan concept from programming languages. 23
Page 24
Selection of Solutions Exercise 3.1 It allo ws the yp e hec er to p erform lo okups in the sym bol table. Exercise 3.2 In the judgmen for the expression b eing let -de ned, include in the sym bol table: ,f Exercise 4.1 It is relation

that is re exiv and transitiv e, but fails to be an ti-symmetric, e.g. and but Exercise 4.2 ; ; ; ; list( ; ; ; ; list( ; ; list( Exercise 4.3 Simply comp ose the substitutions. Exercise 4.4 Tw o sub expressions with iden tical syn tax ma y yield di eren yp e ariables, suc as e.g. nil ]. Ho ev er, from the con text of eac yp e constrain it is clear to whic sub expression yp e ariable corresp onds. Exercise 4.8 The constrain ts Int and list ca r(x) are con tradictory Exercise 5.1 The rules for inferring yp es corresp ond to alid inference rules of logic. Exercise 5.3 A function with t yp e sc

heme ust lo ok lik e: fun(x) fun(y) end end where has t yp e An exhaustiv e searc h through the t yp e rules con vinces us that w cannot nd suc an expression. The fact that an expression has yp e (Int Bo ol) (Bo ol Int) is not problem, since this is merely one instance of the yp e sc heme. 24
Page 25
Exercise 5.4 pair(e ,e pair(e ,e fst(e) fst(e) snd(e) snd(e) left(e left(e ]+ right(e right(e ]= +[ decide(e,e ,e ]= decide(e,e ,e ]= decide(e,e ,e ]= decide(e,e ,e Exercise 5.5 The form ula (( is classical tautology but translates in to an empt yp e. Exercise 5.6 can only solv this

problem, if allo an expression erro that generates run-time error. Then ha ca induct(erro r,fun(x) snd(x) end) cdr induct(erro r,fun(x) fst(x) end) Exercise 5.7 (0,1,2,3,4, ...) coinduct(0,fun(x) pair(succ(x),x) end Exercise 5.8 Let nbin( be the yp e of nite binary trees with -v alues at all no des. then ha e: nbin( ); nbin( bin(e,e ,e nbin( base step induct(base,step) nbin( Let infbin( be the yp e of in nite binary trees with -v alues at all no des. then ha e: base step coinduct(base,step) infbin( infbin( ro ot(e) leftson(e) infbin( ); rightson(e) infbin( 25
Page 26
Exercise 6.1 The

logical rules for univ ersal quan ti cation are: : 62 : Exercise 6.3 It is not legal to mak p olymorphic in since ha the assumption Exercise 7.1 The ML program on page 17 is lik this (with 4). Exercise 7.2 In b oth approac hes are allo ed to ho ose fresh yp e ariables for the yp es of the let -de ned expressions. Exercise 8.1 The yp e ariable ust satisfy ], whic is not p ossible. Exercise 8.2 It computes complete binary tree of heigh 87 with the alue true at eac leaf. Exercise 8.3 ha univ ersally quan ti ed the yp e of in the judgmen of Exercise 9.3 The yp e rules allo the yping of bad

expressions, suc as: let ref(fun(x) end) in let r:=ref(fun(x) succ(x) end) in deref(r)(true) This is yp e correct, since is giv en yp e : p ointer( and th us can be instan tiated to b oth p ointer(Int Int) and p ointer(Bo ol Bo ol) Ho w- ev er, its computation leads to succ(true) Problem10.3 There are no constan t expressions of t yp e : ; for : list( there is only nil for : ha functions: fun(x) fun(y) end end and fun(x) fun(y) end end nally for : : there are in nitely man functions of the form: fun(f ) fun(g) fun(x) f(g (x)) end end end for 0. 26
Page 27
Problem 10.4 If can pro with

an sp eci c alue in place of the ariable then is witness to the truth of : Con ersely if this existen tial quan ti cation holds and can pro using but without an assumptions on then can conclude This corresp onds to abstract data yp es, where the implemen tatio yp e is hidden. only need to kno that is exists! The yp e rules are as follo ws: abs(e) : : ,x use in 62 A; 62 The side conditions exactly state that the implemen tatio yp e is hidden during use. The yp e of an abstract implemen tation of stac of b o oleans could be: : Int Bo ol Bo ol Bo ol corresp onding to the stac op erations Init

Empt Push and op In concrete implemen tatio the hidden yp e could be list(Bo ol) p olymorphic, abstract stac ould then ha the yp e: : : Int Bo ol 27