This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Design of Programming Languages  Fall 2006 Second Exam : Nov. 14, 2006 Student Name: 1. , 2. , 3. , 4. , 5. , 6. , 7. Do any 5. 1. (10 pts) ML and Types. Derive the types, or point out the type errors, of (a) (3 pts) fun F1 g = g (F1 g) (b) (3 pts) fun F2 g x = g (F2 g) x (c) (2 pts) val g = fn f => fn x => if x = 0 then 1 else f(x + 1) (d) (2 pts) val g = fn f : real => fn x => if x = 0 then 1 else f(x + 1); Hint: RUN them and then try to reconstruct why... 1 2. (10 pts) Fixed Points and Evaluation Strategies . (a) (5 pts) Using the Tarski (i.e., approximation) construction, find the least fixed point for the function g = f.n. if n = 0 then 1 else f ( n + 1) . Soln.: g = , g = { ( n, )  n } . g 1 = g ( g ) = n. if n = 0 then 1 else ( n + 1), g 1 = { (0 , 1) } { ( n, )  n > } . g 2 = g ( g 1 ) = n. if n = 0 then 1 else g 1 ( n + 1), g 2 = { (0 , 1) } { ( n, )  n > } . . . . g k = g ( g k 1 ) = n. if n = 0 then 1 else g k 1 ( n + 1), g k = { (0 , 1) } { ( n, )  n > } . . . . Since g 1 = g k k 1, we must have the limit g = { (0 , 1) } { ( n, )  n > } , (b) Taking the ML functions F1 and F2 of the previous problem, explain what should be returned, and why, by the calls F1 g 0 F1 g 1 F2 g 0 F2 g 1 Hint (for where it may be relevant): recall that ML implements proper tailrecursion. Soln.: RUN them in mosml or SML/NJ and explain the behavior by thinking about the way ML evaluates parameters. Lecture6, slides 48 and following should be helpful. 2 3. (10 pts) Type Judgments and ML code . Consider the two judgments for Array Formation and Array Get. is a type ARRAY( ) is a type , , e 1 : ARRAY( ) , , e 2 : INT , , ARRAYGET( e 1 , e 2 ) : Within the function fun typeof (e, globals, functions, formals) of typed Impcore, 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(AGET(a, i)) where AGET(a, i) is the appropriate form of the expression e . Soln.: check your homework. One of the more common errors had to do with the ex pectation that the type of the elements of the array had to be of INTTY, BOOLTY or UNITTY... there is NO reason why it could not be an array......
View Full
Document
 Fall '09
 Giam

Click to edit the document details