Homework 2 

Operational Semantics

Due: Sep 20 2 PM (in class or by email to instructor).

Problem 1 (25%)

Reduce the following program using operational semantics.  You do not need to show all reductions, and may skip steps below the statement level as long as you clearly indicate where you are skipping steps.  However, you should show every step for the labeled statement.  In all cases, you should indicate which rules you are applying (using the rule numbers from Louden).

You may use either proof trees or the more linear method of the text; however, you should be able to understand both since they are really just two different ways to visualize the same proof.

x := 2;
y := x+34;  -- Show all steps for this statement
while x do
  if (y-5) then
    z := y*2
  else
    z := y+1
  fi;
  x := x-1
od

Problem 2 (25%)

Modify Louden's toy language to support division. Also define operational semantics rules to support division. Be careful to think about special cases!

Problem 3 (25%)

Consider the alternate statement with abstract syntax, alt S1 S2 tla. The informal semantics of this statement is as follows: It executes S1 the first time it is executed, and alternates on successive executions.
  1. Modify the operational semantics to support the alternate statement, without changing the set of legal program identifiers. You may assume that there is only one alternate statement in the program.
    Hint: You need to keep additional information to support alt. Create a pair Env+ = <Env,___> where the second element of the pair stores this extra information (I'll let you figure out the type of the element). Then, determine any modification of the existing operational semantics rules. Finally, write a new rule for alt.
  2. Now suppose there are no restrictions on the number of alternate statements in the program (and the alternate statements are independent). If your above solution still works, justify; otherwise, outline the modifications you would need to make (you don't need to actually make these changes)

Problem 4 (25%)

We wish to add another form of iteration statement, for which we will use the keywords repeat and until. It has two components:

  1. A loop body between the two keywords

  2. An identifier after the repeat. This identifier is initialized to 0 before entering the loop the first time.
  3. An expression after the until. If the value of this expression is positive, we terminate the loop; otherwise, we branch back to the loop.
For example, the program:
y := 3;
repeat x
  y := y+1;
  x := x+1;
until x-5
would execute the body 6 times, with y becoming 9 upon termination.
  1. Modify the abstract syntax of our language to support this construct.

  2. Write an operational semantics rule (or rules) for it.

Hint: You can write this construct in terms of constructs you already know.  You can then determine the semantics rule for this construct by determining the semantics of the translated program. However, you’re not allowed to take the easy way out by writing something like:

<repeat ... until...|Env> ==> <some-program|Env>

(even though that would be a technically correct answer).

Hints, Corrections, and Clarifications

  1. I forgot to include a terminator in my original posting of problem 3. It is now there, in red.