Docsity

CS3723 Midterm Exam 2: Programming Languages and Memory Management, Exams of Programming Languages

Toggle sidebar
bg1
CS3723 Midterm Exam 2
Apr 13, 2009
1. (30pts) Finish the following sentences by filling in the blanks.
(a) (5pts) Give two fundamental differences between the ML and the Scheme languages.
(1) Scheme is interpreted, ML is both compiled and interpreted (2) Scheme supports ad-hoc polymorphism, ML does not.
Give two fundamental differences between the ML and the C languages.
(1) ML is both compiled and interpreted; C is purely compiled (2) ML variables cannot be modified unless it is a reference cell; all C variables can be modified.
If we were to allow function definitions to be nested and function pointers to be returned as results
in the C language, what additional support do we need to add to C? managing activation records in the heap and do garbage collection
(b) (5pts) A type is a collection of computable values that share some structural properties. There
are two kinds of type declarations to introduced new type names, transparent declarations and
opaque declarations. There are two ways to determine whether two types are equivalent, name equivalence
and structural equivalence.
(c) (5pts) A type error occurs when a value is misinterpreted at runtime. A language is type safe if it
does not allow type errors at runtime. An example language that is type safe is ML/Scheme/Java;
an example language that is not type safe is C/C++/Pascal/Fortran. Give a key difference be-
tween compile-time and runtime type checking (aside from that one is done at compile time and the
other done at runtime): Every variable can have only a single type in compile-time type checking; Variables can have different types dynamically in runtime type checking.
(d) (5pts) A function is polymorphic if it can operate on different types of input parameters. There
are three kinds of polymorphism: (1) ad-hoc polymorphism, (2) parametric polymorphism and (3)
subtype polymorphism. A compiler is said to perform type inference if it automatically determine the type of each variable based on how the variable is used.
(e) (5pts) A block is a code fragment that introduces new variables. The runtime memory of a
program includes three components, the code space (which contains the instructions to be eval-
uated), the runtime stack, and the heap. There are three ways to implement parameter passing
for function calls: pass-by-name, pass-by-reference, and pass-by-value.
(f) (5pts) A tail call is a function call that there is no more computation in the caller after the call returns.
In particular, tail recursion is equivalent to loops because The callee can reuse the activation record of the caller by modifying values of parameters and local variables.
An exception is when something unexpected happens, we want the program to jump out of the current block(pop out its activation record)
until a caller block can handle the exception. The concept of continuation means .
Both continuation and exceptions can be used to support dynamic jumps in programs.
2. (10pts) Type inference. Translate the Scheme code to ML. Determine a unique type for each of the
variables in the given Scheme code or the translated ML code.
(define Append (lambda (x y)
(if (null? x) y (cons (car x) (Append (cdr x) y)))))
x: ’a list; y: ’a list; return type: ’a list
fun Append([], y) = y
| Append(x1::x2, y) = x1 :: Append(x2, y);
3. (10pts) Translate ad-hoc polymorphism to subtype polymorphism. Translate the following Scheme
code to ML.
1
pf3
pf4

Partial preview of the text

Download CS3723 Midterm Exam 2: Programming Languages and Memory Management and more Exams Programming Languages in PDF only on Docsity!

CS3723 Midterm Exam 2

Apr 13, 2009

  1. (30pts) Finish the following sentences by filling in the blanks.

(a) (5pts) Give two fundamental differences between the ML and the Scheme languages. (1) Scheme is interpreted, ML is both compiled and interpreted (2) Scheme supports ad-hoc polymorphism, ML Give two fundamental differences between the ML and the C languages. (1) ML is both compiled and interpreted; C is purely compiled (2) ML variables cannot be modified unless it is If we were to allow function definitions to be nested and function pointers to be returned as results in the C language, what additional support do we need to add to C? managing activation records in the heap and d (b) (5pts) A type is a collection of computable values that share some structural properties. There are two kinds of type declarations to introduced new type names, transparent declarations and opaque declarations. There are two ways to determine whether two types are equivalent, name equivalence and structural equivalence. (c) (5pts) A type error occurs when a value is misinterpreted at runtime. A language is type safe if it does not allow type errors at runtime. An example language that is type safe is ML/Scheme/Java; an example language that is not type safe is C/C++/Pascal/Fortran. Give a key difference be- tween compile-time and runtime type checking (aside from that one is done at compile time and the other done at runtime): Every variable can have only a single type in compile-time type checking; Variables can (d) (5pts) A function is polymorphic if it can operate on different types of input parameters. There are three kinds of polymorphism: (1) ad-hoc polymorphism, (2) parametric polymorphism and (3) subtype polymorphism. A compiler is said to perform type inference if it automatically determine the type of each (e) (5pts) A block is a code fragment that introduces new variables. The runtime memory of a program includes three components, the code space (which contains the instructions to be eval- uated), the runtime stack, and the heap. There are three ways to implement parameter passing for function calls: pass-by-name, pass-by-reference, and pass-by-value. (f) (5pts) A tail call is a function call that there is no more computation in the caller after the call returns. In particular, tail recursion is equivalent to loops because The callee can reuse the activation record of the caller b An exception is when something unexpected happens, we want the program to jump out of the current block(pop until a caller block can handle the exception. The concept of continuation means. Both continuation and exceptions can be used to support dynamic jumps in programs.

  1. (10pts) Type inference. Translate the Scheme code to ML. Determine a unique type for each of the variables in the given Scheme code or the translated ML code.

(define Append (lambda (x y) (if (null? x) y (cons (car x) (Append (cdr x) y)))))

x: ’a list; y: ’a list; return type: ’a list fun Append([], y) = y | Append(x1::x2, y) = x1 :: Append(x2, y);

  1. (10pts) Translate ad-hoc polymorphism to subtype polymorphism. Translate the following Scheme code to ML.

(define Add (lambda (x y) (cond ((number? y) (+ x y)) ((null? y) ’()) (else (cons (Add x (car y)) (Add x (cdr y)))))))

Use the following ML datatype in your translation (Hint: x and y do not have the same type)

datatype Value = Number of int | Tree of Value*Value | NULL;

fun Add(x, Number(y)) = Number(x + y) | Add(x, NULL) = NULL | Add(x, Tree(y1,y2)) = Tree(Add(x,y1), Add(x,y2));

  1. (16pts) Memory management for blocks. Draw a snapshot of the runtime stack for the following ML code immediately before the call to add returns.

1: let 2: fun mk_x(x) = 3: let fun add1(y) = x + y 4: in 5: let val x = 7 in add1(5) end 6: end 7: fun apply(f,x) = f(x) 8: in 9: apply(mk_x,10)- 10:end;

Global: Control Link -> NULL Access Link -> NULL mk_x : <Global, impl of mk_x> apply: <Global, impl of apply> apply(mk_x,10):?

apply(mk_x,10): Control Link -> Global Access Link -> Global Return Addr -> line 9 Return Result Addr -> Global.apply(mk_x,10) f -> <Global, impl of mk_x> x : 10 mk_x(10) :

mk_x(10): Control Link -> apply(mk_x,10) Access Link -> Global Return Addr -> line 7 Return Result Addr -> apply(mk_x,10).mk_x(10) x : 10 add1: <mk_x(10), impl of add1>

Inline1: Control Link -> mk_x(10)

main(3,[]) called; line 7: insert(3,[]) called line 6: find(3,[]) called line 3: exception Missing(3) raised line 6: exception Missing(3) handled line 6: [3] insert(3,[]) returns with result [3] line 7: main(3,[]) returns with result [3]