Lab 10: Semantics

  1. Evaluate your TA first if you haven't done so.
  2. Semantics
    1. Ch 23, Exercise 5. Give a natural semantics for each of the following:
      1. The value of a > b is 1 if a is greater than b, 0 otherwise.
      2. Within a compound statement, changes to the environment are cumulative. Each statement in the compound sees any changes to the environment left by the statement before.
      3. An assignment statement v := e changes the value of the variable v in the current environment.
      4. The statement while (e) s evaluates the guard expression e. If the guard is 0, nothing else happens. If the guard is not 0, the body statement s is executed and the whole action repeats.
    2. Ch 23, Exercise 2. In this exercise you will define Language Four, an extension of Language Three listed below:
        <exp>     ::= fn <var> => <exp>   | <addexp>
        <addexp>  ::= <addexp> + <mulexp> | <mulexp>
        <mulexp>  ::= <mulexp> * <funexp> | <funexp>
        <funexp>  ::= <funexp> <rootexp>  | <rootexp>
        <rootexp> ::= let val <var> = <exp> in <exp> end
                      | ( <exp> ) | <var> | <const>
        
      Here is a sample program in Language Four, showing all the new constructs:
        let
          val fact = fn x => if x < 2 then x else x * fact(x-1)
        in
          fact 5
        end
        
      As you can see, Language Four extends Language Three with three new constructs: the < operator for comparison, the - operator for subtraction, and the conditional (if-then-else) expression. The sample program above defines a recursive factorial function and uses it to compute the factorial of 5. Define the syntax of Languague Four by extending the syntax of Language Three and give a natural semantics for Language Four.

    3. Ch 23, Exercise 3. Recursive function are not legal under the static-scoping semantics for Language Three, because the scope of the definition of a variable does not include the function body. ML works the same way. The scope of the definition of f produced by fun f ... includes the function body being defined, but the scope of the definition of f produced by val f = fn... does not. So in ML, only the fun definitions can be recursive. Extend the syntax of Language Four to allow simple definitions with fun, so that, for example, this program is legal:
        let fun f x = x + 1 in f 1 end
        
      Also, give a natual semantics for your extended language.

  3. Prolog: write a Quicksort(L, X) predicate in Prolog where L is a list of unsorted integers and X is the sorted listed from L.