Survey of Programming Language Concepts, COSC-3308-01 Tutorial 7 Exercise 1. (Where is the error?) This exercise shows a failure at run-time: % example of failure declare proc {ExampleOfFailure M N ?Z} if M == 0 then Z = 1 end if N == 0 then Z = 2 end end local R in {ExampleOfFailure 0 0 R} end Explain why? Extend the program to be able to catch the exception. Exercise 2. (multiple variable declaration) Write the semantics of the execution of a local statement that introduces multiple variables simultaneously. For example, the following Oz program local P in local Y in local Z in Z=1 proc {P X} Y=X end {P Z} end end end can be transformed into an equivalent program for which all the declarations have been done altogether: local P Y Z in Z=1 proc {P X} Y=X end {P Z} end This assignment extends the kernel language by a declaration statement that introduces multiple variables simultaneously. The statement under consideration is thus local <x>1 … <x>n in <s> end Give an operational semantics rule where the statement to be pushed is <s>. Exercise 3. (Length of an abstract list) A trivial Oz code for returning the number of elements of a list can be given by: declare fun {Length L} case L of nil then 0 [] H|T then 1+{Length T} end end For example, the call {Browse {Length [1 f 2 3 s 5]}} will display 6. Explain why this function is not tail recursive. Write an equivalent tail recursive function. Write also an invariant for this tail recursive function. Using the invariant, prove the correctness for this program. Exercise 4. (evaluation with declarations) An obvious way to extend our arithmetic expressions is by also allowing the declaration of variables with immediately assigning a value computed by an expression. This type of expression is commonly referred to as let-expression. So we have additionally: Syntax: A let-expression is a tuple let(X Y Z) where X is an atom giving the variable name and Y and Z are expressions. Semantics: The first expression inside a let-expression defines the value to be assigned to the variable introduced in the let-expression. The second expression can refer to the variable introduced. In some sense, a let-expression is similar with a local statement, except the fact that the former returns the value of an expression, whereas the latter is a statement (it just does some computations). Example: {Eval let(x mult(int(2) int(4)) add(var(x) int(3))) env} should return 11. The variable x will be bound to the value of the expression mult(int(2) int(4)), which is evaluated to 8. After that, the second expression, add(var(x) int(3)), is evaluated to 8+3, and its result, 11, is returned. Hints: One needs environment adjunction as well here which is implemented by record adjunction. The Mozart system provides a specific function for adding features and values to the records, namely: {AdjoinAt R F X} takes a record R, a feature F, and a new field X and returns a record which has a feature F with the value X in addition to all features and fields from R except F. Examples: (of using AdjoinAt) {AdjoinAt a(x:1 y:2) z 7} returns a(x:1 y:2 z:7) {AdjoinAt a(x:1 y:2) x 7} returns a(x:7 y:2)