Docsity

Midterm Solutions - Programming Languages Compilers | CS 421, Exams of Computer Science

Toggle sidebar
bg1
CS 421—Summer 2006 1 CS421 Summer 2006 Midterm Solutions
CS421 Summer 2006 Midterm Solutions
CS 421 — Introduction to Programming Languages
Summer Term 2006
11:30am–12:45pm, Thursday, June 29, 2006
The total time for this exam is 75 minutes.
Print your name and netid below. Also write your name at the bottom of each subsequent page.
Name:
Netid:
•This is a closed-book exam. You are allowed one 3 inch by 5 inch card of notes prepared
by yourself. You may write on both sides of the card. This card is not to be shared. All
other materials, besides pens, pencils, and erasers, are to be put away.
•Do not share anything with other students. Do not talk to other students. Do not look at
another student’s exam. Also, be careful to not expose your exam to easy viewing by other
students. Violation of any of these rules will count as cheating.
•If you believe there is an error, or an ambiguous question, you must document your assump-
tions about what the question means. We are not allowed to answer questions about the exam
in class, and obviously cannot answer these questions for students taking the exam elsewhere.
•Including this cover sheet, rules at the end, and scratch pages, there are 15 pages to the exam.
Please verify that you have all 15 pages. You are allowed to tear off the last three (rules and
scratch) if this makes it easier to take the test.
Question Total points Score
1 10
2 9
3 16
4 19
5 12
6 20
7 8
8 6
SUBTOTAL 100
Extra Credit 12
TOTAL 112
University of Illinois at Urbana-Champaign Netid:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Midterm Solutions - Programming Languages Compilers | CS 421 and more Exams Computer Science in PDF only on Docsity!

CS421 Summer 2006 Midterm Solutions

CS 421 — Introduction to Programming Languages

Summer Term 2006

11:30am–12:45pm, Thursday, June 29, 2006

The total time for this exam is 75 minutes.

Print your name and netid below. Also write your name at the bottom of each subsequent page.

Name:

Netid:

  • This is a closed-book exam. You are allowed one 3 inch by 5 inch card of notes prepared by yourself. You may write on both sides of the card. This card is not to be shared. All other materials, besides pens, pencils, and erasers, are to be put away.
  • Do not share anything with other students. Do not talk to other students. Do not look at another student’s exam. Also, be careful to not expose your exam to easy viewing by other students. Violation of any of these rules will count as cheating.
  • If you believe there is an error, or an ambiguous question, you must document your assump- tions about what the question means. We are not allowed to answer questions about the exam in class, and obviously cannot answer these questions for students taking the exam elsewhere.
  • Including this cover sheet, rules at the end, and scratch pages, there are 15 pages to the exam. Please verify that you have all 15 pages. You are allowed to tear off the last three (rules and scratch) if this makes it easier to take the test.

Question Total points Score 1 10 2 9 3 16 4 19 5 12 6 20 7 8 8 6 SUBTOTAL 100 Extra Credit 12 TOTAL 112

  1. Short Answer Questions (10 points):

(a) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): A statically-typed language: i. assigns types to all expressions at compile time TRUE ii. may require run-time checks to guarantee type errors do not occur TRUE iii. always has a strong type system FALSE iv. always requires explicit type declarations FALSE

(b) In the following, state TRUE or FALSE for each part separately; each part is 1 point (write your answers to the left): If a language is strongly typed: i. an operation that could produce a type error may be detected at compile-time or at run-time TRUE ii. the use of type inference to determine types of expressions is required FALSE iii. the language must be statically typed FALSE iv. the language must be dynamically typed FALSE

(c) (2 points) What value is produced by the following program? let x = 10 in let y = 5 + x in let f y = y + x in let x = 20 + y in f x;; 45

  1. Higher-Order Functions and Data Types (16 points):

Given the following OCaml datatype:

type int_pair = Empty | LPair of int_pair * int | RPair of int * int_pair;;

(a) (6 points) Write a recursive function fold pair: val fold_pair : (int -> ’a -> ’a) -> (int -> ’a -> ’a) -> ’a -> int_pair -> ’a = that folds a function over an int pair. It should take a function that works on LPair (typed between the first set of parens in the type signature for fold pair), a function that works on RPair (typed between the second set of parens in the type signature for fold pair), and an identity for Empty, as well as the int pair, returning the value of the fold computation. 1 let rec fold_pair f g z p = 2 match p with 3 | Empty -> z 4 | LPair (ps,n) -> f n (fold_pair f g z ps) 5 | RPair (n,ps) -> g n (fold_pair f g z ps);; 6 val fold_pair : 7 (int -> ’a -> ’a) -> (int -> ’a -> ’a) -> ’a -> int_pair -> ’a =

(b) (5 points) Write a function sum rpair, using fold pair, that will calculate the sum using only the integer value stored in RPair items – in other words, the int that is part of an LPair will be disregarded for the sum (but the int pair that is part of the LPair still needs to be processed, since it may contain RPair items that will contribute to the sum). 1 # let sum_rpair p = 2 fold_pair (fun x y -> y) (fun x y -> x + y) 0 p;; 3 val sum_rpair : int_pair -> int = 4 # p4;; 5 - : int_pair = RPair (1, LPair (RPair (3, Empty), 5)) 6 # sum_rpair p4;; 7 - : int = 4 8

(c) (5 points) Write a function flip lpair, using fold pair, that will turn an LPair into an RPair, but will leave RPair and Empty items as they are. 1 # let flip_lpair p = 2 fold_pair (fun x y -> RPair (x,y)) (fun x y -> RPair (x,y)) Empty p;; 3 val flip_lpair : int_pair -> int_pair = 4 # p4;; 5 - : int_pair = RPair (1, LPair (RPair (3, Empty), 5)) 6 # flip_lpair p4;; 7 - : int_pair = RPair (1, RPair (5, RPair (3, Empty)))

  1. Type Derivations (19 points): Below is an outline of the type derivation of the following expression:

let f = (fun x −> (g x)) in f 10

Please complete the derivation by giving the results to go in the numbered blanks. Put your answers next to the corresponding numbers below the type derivation outline.

(#14) ⊢ (#15) : (#16) (#17) ⊢ (#18) : (#19) (#11) ⊢ (#12) : (#13) (#1) ⊢ (fun x −> (g x)) : (#2)

(#5) ⊢ (#6) : (#7) (#8) ⊢ (#9) : (#10) (#3) ⊢ f 10 : (#4) {g : int → bool} ⊢ let f = (fun x −> (g x)) in f 10 : bool

#1 {g : int → bool}

#2 int → bool

#3 {g : int → bool; f : int → bool}

#4 bool

#5 {g : int → bool; f : int → bool} #6 f

#7 int → bool

#8 {g : int → bool; f : int → bool}

#9 10

#10 int

#11 {g : int → bool, x : int}

#12 g x

#13 bool

#14 {g : int → bool, x : int}

#15 g

#16 int → bool

#17 {g : int → bool, x : int} #18 x

#19 int

  1. Lambda Calculus (20 points):

(a) (5 points) Use β-reduction with a lazy evaluation order to reduce the following term as fully as possible: (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t)) (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t)) →β (λc d.(λx y.x) c d)(λs.s)((λf.f f)(λt.t)) →β (λd.(λx y.x) (λs.s) d)((λf.f f)(λt.t)) →β (λx y.x) (λs.s) ((λf.f f)(λt.t)) →β (λy.(λs.s)) ((λf.f f)(λt.t)) →β λs.s

(b) (5 points) Use β-reduction with an eager evaluation order to reduce the following term as fully as possible (note: this is the same term as above): (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t)) (λb c d.b c d)(λx y.x)(λs.s)((λf.f f)(λt.t)) →β (λc d.(λx y.x) c d)(λs.s)((λf.f f)(λt.t)) →β (λd.(λx y.x) (λs.s) d)((λf.f f)(λt.t)) →β (λd.(λx y.x) (λs.s) d)((λt.t)(λt.t)) →β (λd.(λx y.x) (λs.s) d)(λt.t) →β (λx y.x) (λs.s) (λt.t) →β (λy.(λs.s)) (λt.t) →β λs.s

(c) (5 points) Use β-reduction to reduce this term as fully as possible. If you need α- conversion, show explicitly where it is needed. Use an eager evaluation order. (λa b.b b a)((λx y.y)(λw.a w)(λv.b v))(λt.t) (λa b.b b a)((λx y.y)(λw.a w)(λv.b v))(λt.t) →β (λa b.b b a)((λy.y)(λv.b v))(λt.t) →β (λa b.b b a)(λv.b v)(λt.t) →α (λa c.c c a)(λv.b v)(λt.t) →β (λc.c c(λv.b v))(λt.t) →β (λt.t)(λt.t)(λv.b v) →β (λt.t)(λv.b v) →β (λv.b v)

(d) (5 points) Recall our definition of pairs in the λ calculus – a pair can be represented as a term λx.x a b where x is the constructor tag (here we only have one constructor, so we only need one tag) and a and b are the two elements in the pair. The pair constructor abstraction is then λa b x.x a b, which will take the two elements of the pair and “plug them in” to the correct spots. Using this definition of pairs, provide a λ-abstraction that, given a pair and a term, will return a new pair where the first element is the same as that of the input pair and the second element is the input term. In other words, given pair (a, b) and term t, the pair (a, t) should be returned.

For the solution, just having the following would be sufficient: λp t.(λa b x.x a b)(p(λx y.x)) t This is a function that takes a pair and a term and then has three parts: the first is the pair constructor, (λa b x.x a b); the second is the π 1 or left projection function for the pair, taking out the first element, (p(λx y.x)); the third is the term to put in, t. Remember, a “loaded” pair with data in it is of the form λx.x a b where a and b are the left and right members of the pair. If you substitute the function (λx y.x) for x here, you will see that this will then return the first element of the pair. You could also have reduced this a bit, to λp t x.x(p(λx y.x)) t but any valid solution was accepted.

  1. Finite Automata (6 points, 1 point each): For the given finite automata over the alphabet {a, b}, several possible input strings are provided. For each string, determine if it is accepted by the automata. If so, circle it. If not, cross it out.

a ǫ (^) a

a

b (^) ǫ

(a) a (b) baab (c) aaaab (d) b (e) aaaaa (f) ba

  1. Extra Credit (12 points): The following type may be used to represent terms of the lambda calculus as abstract syntax trees.

type lterm = Var of string (* x ) | Abs of string * lterm ( \lambda x. t ) | App of lterm * lterm ( t t’ *);;

(a) (3 points) Write a function bound vars which, given a term of type lterm, returns a list of strings (string list) containing all the names of variables having a bound occurrence in the term. It is acceptable if a name appears in the list more than once. 1 let bound_vars lterm = 2 let rec bound_aux lterm binders = 3 match lterm with 4 | Var x -> if (List.mem x binders) then [x] else [] 5 | Abs (x,t) -> bound_aux t (x::binders) 6 | App (t,t’) -> (bound_aux t binders) @ (bound_aux t’ binders) 7 in bound_aux lterm [];;

(b) (4 points) Write a function gen church numeral which, given a non-negative integer (type int), returns an lterm representing the equivalent Church Numeral. Remember our definitions: 0 ≡ λf x.x, 1 ≡ λf x.f x, and in general Suc n ≡ λf x.f(n f x). 1 let gen_church_numeral n = 2 let rec church_aux n = 3 match n with 4 | 0 -> Var "x" 5 | _ -> App (Var "f", church_aux (n-1)) 6 in Abs ("f", Abs("x", church_aux n));; 7 8 # gen_church_numeral 3;; 9 - : lterm = 10 Abs ("f", Abs ("x", App (Var "f", App (Var "f", App (Var "f", Var "x")))))

Rules for type derivations:

Constants

⊢ n : int (assuming n is an int)

⊢ true : bool

⊢ false : bool

Variables

Γ ⊢ x : τ if(x : τ ) ∈ Γ

Arithmetic Operators Γ ⊢ e 1 : int Γ ⊢ e 2 : int Γ ⊢ e 1 ⊕ e 2 : int

(⊕ ∈ {+, −, ∗, /, · · ·})

Relational Operators Γ ⊢ e 1 : int Γ ⊢ e 2 : int Γ ⊢ e 1 ∼ e 2 : bool

(∼ ∈ {<, >, ≤, ≥, =, 6 =, · · ·})

Booleans Γ ⊢ e 1 : bool Γ ⊢ e 2 : bool Γ ⊢ e 1 && e 2 : bool Γ ⊢ e 1 : bool Γ ⊢ e 2 : bool Γ ⊢ e 1 || e 2 : bool Γ ⊢ e 1 : bool Γ ⊢! e 1 : bool If Γ ⊢ e 1 : bool Γ ⊢ e 2 : τ Γ ⊢ e 3 : τ Γ ⊢ if e 1 then e 2 else e 3 : τ Function Abstraction Γ ∪ [x : τ 1 ] ⊢ e : τ 2 Γ ⊢ fun x−>e : τ 1 → τ 2 Function Application Γ ⊢ e 1 : τ 1 → τ 2 Γ ⊢ e 2 : τ 1 Γ ⊢ e 1 e 2 : τ 2 Let Γ ⊢ e 1 : τ Γ ∪ [x : τ ] ⊢ e 2 : τ ′ Γ ⊢ let x = e 1 in e 2 : τ ′ Letrec Γ ∪ [x : τ ] ⊢ e 1 : τ Γ ∪ [x : τ ] ⊢ e 2 : τ ′ Γ ⊢ let rec x = e 1 in e 2 : τ ′