CS 330 Home Schedule Resources

Assignment 3
Kernel Language Semantics

Estimated time: 4 hours

The objective of this homework is to familiarize yourself with the declarative model and its semantics using the kernel language.

Exercises

  1. Free and bound identifiers. (5 points) Consider the following statement:
       proc {P X}
          if X>0 then {P X-1} end
       end
    
    Is the second occurrence of the identifier P free or bound? Justify your answer by first translating to kernel syntax.

    (Tangential hint: To translate infix operators into the kernel language, you can use the online Mozart Documentation's alphabetical index under 'Symbols', before 'A', to find out which module defines each operator.)

  2. Contextual environment. (10 points) Section 2.4 explains how a procedure call is executed. Consider the following procedure MulByN and its call:
        declare MulByN A=10 B N in
        N=3                                % *
        proc {MulByN X ?Y}                 % *
             Y=N*X                         % *
        end                                % *
        {MulByN A B}
        {Browse B}
    
    What value, if any, is printed in the Browser? Now modify the code so that the lines marked with an asterisk form the body of a local statement that declares a new identifier N. Consider two scenarios: one where the identifier N is removed from the declare line, and another where it remains and is bound to 4. How does the overall execution change? Explain why in terms of what is happening to the environment when MulByN is called.

    [Clarification: in the second part of this question, "Now modify...," you're comparing two different versions of the above code:

    1. modified code with a local N and no N in the declare statement
    2. modified code with one local N and one N in the declare.
    -Max]

  3. Functions and procedures. (5 points) If a function body has an if statement with a missing else case, then an exception is raised if the if condition is false. Why is this behavior correct? This situation does not occur for procedures. Explain why it should not.

  4. Tuples and lists represented as records. (20 points) For each of the following records, indicate whether it is also a tuple and/or a list, or else invalid.
    1. a(1:a 2:b 3:c)
    2. a(1:a 2:b 4:c)
    3. a(0:a 1:b 2:c)
    4. a(1:a 2:b 3:c d)
    5. a(a 2:b 3:c 4:d)
    6. a(2:b 3:c 4:d a)
    7. a(1:a 2:b 3:c foo:d)
    8. '|'(1:a 2:'|'(1:b 2:nil))
    9. '|'(1:a 2:'|'(1:b 3:nil))
    10. '|'(1:a(2:aa) 2:'|'(1:b 2:nil))

  5. The declarative model's type hierarchy. (20 points) For each of the following values, what is its most specific type from the types in Figure 2.16 (page 51), and what are all its parent types in the hierarchy? Translate the syntactic sugar notation into the basic kernel notation. (Hint for strings: Browsing them will show the character codes.)
    1. person
    2. true
    3. bintree(key:foo val:5 nil nil)
    4. rec( )
    5. sen(time flies)
    6. [2 4 6]
    7. [2 4 [6]]
    8. [[2 4] 6]
    9. &z
    10. "Hello\nWorld!"
    11. key#6
    12. val#": "#N#".\n"
    13. ~4
    14. fun {$ X} X end

    [Clarification: for purposes of this question, it's okay to use values as well as identifiers where the kernel syntax on page 50 has only variables like < x1>. For instance, instead of "local X in X=21 person(age:X)", you should write "person(age:21)".]

  6. if and case statements. (10 points) This exercise explores the relationship between the if statement and the case statement.
  7. The case statement. (20 points) This exercise tests your understanding of the full case statement. Given the following procedure:
       proc {Test X}   
         case X
           of a|Z then {Browse 'case'(l)}
           [] f(a) then {Browse    'case'(2)}
           [] Y|Z andthen Y==Z then {Browse 'case'(3)}
           [] Y|Z then {Browse 'case'(4)}
           [] f(y) then {Browse 'case'(5)}
           else {Browse 'case'(6)} end   
       end
    
    Report what happens when you execute each of the following:

    For your own benefit, make sure you understand exactly what happens in each case.

  8. The case statement again. (10 points) Given the following procedure:

        proc {Test X}
            case X of f(a Y c) then {Browse 'case'(l)}
            else {Browse 'case'(2)} end
        end 

    Without executing any code, predict what will happen when you feed each of the following:

    Use the kernel translation and the semantics if necessary to make the prediction. After making the predictions, check your understanding by running the examples in Mozart. Now run the following example:

        declare X Y
        if f(X Y d)==f(a Y c) then {Browse 'case'(l)}
        else {Browse 'case'(2)} end
    
    Does this give the same result as the previous problem, or a different one? Explain the result.

  9. Lexically scoped closures. (10 points) Given the following code:
       declare Max3 Max5
       proc {SpecialMax Value ?SMax}
         fun {SMax X}
           if X>Value then X else Value end
         end
       end
       {SpecialMax 3 Max3}
       {SpecialMax 5 Max5}
    
    See what happens when you execute:
    {Browse [{Max3 4} {Max5 4}]}
    What is Max3? How is it different from Max5?

  10. Control abstraction. (10 points) This exercise explores the relationship between linguistic abstractions and higher-order programming.

    1. Define the function AndThen as follows:
          fun {AndThen BP1 BP2}
             if {BP1} then {BP2} else false end
          end
      
      Does the call {AndThen fun{$} expression1 end fun {$} expression2 end} give the same result as expression1 andthen expression2? Does it avoid the evaluation of (expression)2 in the same situations?

    2. Write a function OrElse that is to orelse as AndThen is to andthen. Explain its behavior.

  11. Feedback.
    Scale
    1. Not at all
    2. Not very
    3. Slightly
    4. Somewhat
    5. Mostly
    6. Very
    7. Extremely