Exam2S06Hints

# Exam2S06Hints - Design of Programming Languages - Spring...

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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Design of Programming Languages - Spring 2006 Second Exam : April 5, 2006 Do any 5. 1. (10 pts) ML and Types. Derive the types of (a) fun f x = f x; (b) fun F g = g (F g) (c) fun F g x = g (F g) x and show your derivation steps. Use the ideas from the class discussion on universally quan- tified types to begin your type inference. Solutions: (a) f is a function of one argument, so, from the left hand side we can only conclude that f : ’a -> ’b , with x having type ’a . Since the right hand side has f a , we have no further restrictions on types. Thus f : ’a -> ’b . Notice that any calls to this function will never terminate. (b) From the left hand side, we conclude that F : ’a -> ’b with g : ’a . From the right hand side we must conclude that g : ’b -> ’b , since (F g) : ’b because of the left hand side, and since the type of F g must equal the type of g (F g) (the range types of both sides must match) we must have that both the domain and range types of g are ’b . Thus g : ’a = ’b -> ’b . Finally, F : (’b -> ’b) -> ’b . (c) See Exam2Hints.pdf , problem 1 a, on the course site - the Fall 05 exam. 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 Γ ξ , Γ φ , Γ ρ ⊢ e 1 : ARRAY( τ ) Γ ξ , Γ φ , Γ ρ ⊢ e 2 : INT Γ ξ , Γ φ , Γ ρ ⊢ e 2 : τ Γ ξ , Γ φ , Γ ρ ⊢ ARRAY-SET( e 1 , e 2 , e 3 ) : τ 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 ac- cording to the type judgment above. Write the code completing ty(ASET(a, i, e)) where ASET(a, i, e) is the appropriate form of the expression e . Solution: a possible solution (awkward but usable) is given by the code fragment: 1 ty (ASET (a, i, e)) = let val t1 = ty e in case (ty a) of ARRAYTY(t2) => if (ty i) = INTTY then (if t2 = t1 then t1 else raise TypeError("array and element type mismatch")) else raise TypeError("array index not an int")| _ => raise TypeError("attempt to access non-array") end 3. (10 pts) Fixed Points ....
View Full Document

## This note was uploaded on 02/13/2012 for the course CS 91.531 taught by Professor Giam during the Fall '09 term at UMass Lowell.

### Page1 / 5

Exam2S06Hints - Design of Programming Languages - Spring...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online