This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Design of Programming Languages  Fall 2005 Second Exam : Nov. 15, 2005 Do any 5. 1. (10 pts) ML, Types and Fixed Points. (4 pts) Derive the type of fun F g x = g (F g) x and show your derivation steps. Solution. From the left hand side, we can immediately conclude that F has type a b c , for some (as yet unspecified) types a , b , and c . Furthermore (F g) must have type b c . Moving to the righthandside, we have that g must now have type (b c) b c . But this must also be the original type a . Replacing in the original, we have F : ((b c) b c) b c . (6 pts) Given the function val g = fn f => fn x => if x = 0 then 1 else x*f(x  1) derive its type (2 pts) and trace through the computation F g 3 (4 pts). Solution. The type of g should be fairly simple (if you have trouble just run it in ML and trace it back). For the rest: F g 3 = g (F g) 3 = if 3 = 0 then 1 else 3*((F g) 2) = 3*((F g) 2) = 3*(g (F g) 2) = 3*(if 2 = 0 then 1 else 2*((F g) 1)) = etc... No Y combinators: this is a way to exploit the recursion provided by ML to get around the typing problems of the Y combinator. 2. (10 pts) Type Judgments and ML code . Consider the two judgments for Array Formation and Array Make. is a type ARRAY( ) is a type 1 , , e 1 : INT , , e 2 : , , ARRAYMAKE( e 1 , e 2 ) : ARRAY( ) Within the function fun typeof (e, globals, functions, formals) of typed Im pcore, you have the function ty , which takes only the expression e as an argument, using the remaining arguments of typeof as free variables and returns the type of its argument according to the type judgment above. Write the code completing ty(AMAKE(len, init)) where AMAKE(len, init) is the appropriate form of the ex pression e ....
View Full
Document
 Fall '09
 Giam

Click to edit the document details