UP | HOME

Operational Semantics
Lecture 4

Table of Contents

Review

  • Brainstorming a language
  • Symbols vs. meaning
  • Informal descriptions (C standard)

Questions about the last class?

Quiz

Quiz Discussion

Control-flow statements

Looked at the assignment statement (review as needed)

What about conditionals?

Recall variable assignment

  • Updates state

    <e> => n    s' = s.put(x, n)
    ----------------------------
    s : <x = e> => s'
    

In this kind of assignment, it returns no value, only a new state.

In this kind of assignment, we evaluate the right-hand side before binding

In this language, variables are mutuable (no scope associated with assignment).

Statements

  • Assignment, if statements, loops, etc.
  • Can change state, e.g.,

    if (c > 2)
      x = 10;
    
    • if-statement changes state because it contains an assignment

Constructs that change state are said to have "side-effects" on the state

Ouput of statement indirectly found in the state (rather than in an output value)

Changing state

  • s : <statement> => s'
    • Takes state (e.g., RAM) as input, evaluates to new state
  • Side-effect: constructs that changes state

    x = 2;
    if (c > 2)
      x = 10;
    
    • x is modified
    • What's an alternative to mutation?

f(c) { // x is 3 if c is positive, otherwise it's 10 if (c>0) x = 3; else x = 10;

x = c > 0 ? 3 : 10; }

if (c>0) { x = 3; print(x); while ()… }

  1. evaluate the conditional expression
  2. if true: execute whatever statement is in the body of the if state
  3. if false: do nothing

Conditional statements

  • What is the semantics?

    if (c > 2)
      x = 10;
    

Informal semantics

  • Evaluate condition
  • If true, evaluate the if-branch
  • if false, do nothing

Another option is to include an else branch?

Can we rewrite if-then-else as if-then?

Conditional statement example





s : <if (c > 2) x = 10;> => s'

s  = {x=2,c=3}
s' = ??

Conditional statement example



s : <(c > 2)> => ??        s : <x = 10> =>  s'
----------------------------------------------
s : <if (c > 2) x = 10;> => s'

s  = {x=2,c=3}
s' = ??

Conditional statement example

// relational op           // assignment
---------------------      -------------------
s : <(c > 2)> => true      s : <x = 10> =>  s'
----------------------------------------------
s : <if (c > 2) x = 10;> => s'

s  = {x=2,c=3}
s' = ??

Conditional statement example

// relational op           // assignment
---------------------      -------------------
s : <(c > 2)> => true      s : <x = 10> =>  s'
----------------------------------------------
s : <if (c > 2) x = 10;> => s'

s  = {x=2,c=3}
s' = {x=10,c=3}

Conditional statement semantics

e  ::=  // expressions

st ::=  x = e    // assignment
     |  if e st  // conditional


s : <e> => true     s : st => s'
--------------------------------  [if-true]
s : <if e st> => s'


s : <e> => false
-------------------               [if-false]
s : <if e st> => s'

Side-effect freedom

  • s : <statement> => v, s
  • State is the same before and after
  • Output captured via v
  • Trade-offs
    • Ease of writing
    • Ease of analysis
  • Loops?

Without variable reassignment, how do we write a loop?

Side effect free version of conditionals?

C's ternary operators: x = c > 2 ? 10 : x;

Operational semantics as interpreter

node:

  • left
  • right
  • data

count_nodes_in_tree(node): // base if node is null: return

/ recursive print(node) / preorder count_nodes_in_tree(node.left) print(node) // in order count_nodes_in_tree(node.right) print(node) // postorder

node:

  • child[]
  • data

count_nodes_in_tree(node): // base if node is null: return

/ recursive print(node) / preorder for all cnode in child: count_nodes_in_tree(cnode) print(node) // postorder

example of binary tree for 1+2*3 trace postorder traversal as evaluation

Side effect examples

int x;

int f(c) { x = 2; if (c>0) { x = 3; } else { x = 4; } // what's the state here? print(x) }

int main() { // s = {x=0} print(f(3)); // s' = {x=3 or 4} }

Summary of design choices so far

  • Statements or no statements?
  • Boolean type or repurpose integers?
  • Side effects or no side effects?
  • Expressing iteration?

Iteration

  • What choices do we have?

Possible constructs

  • goto statements
  • while loops
  • recursive functions

While loops

  • What is the behavior of a while loop?

    x = 1;
    i = input();
    while (i > 0) {
      x = x * 2;
      i = i - 1;
    }
    

Informal semantics

  • Evaluate condition
  • If true, evaluate the body, return to top
  • If false, exit loop

    Note that this is very similar to the conditional statement

    Nearly identical syntax, keyword allows interpreting it differently

Formal semantics

e  ::=  // expressions

st ::=  x = e       // assignment
     |  if e st     // conditional
     |  while e st  // while
     |  { st* }     // compound statement


s : <e> => true
s : <st> => s'
s' : <while e st> => s''
------------------------  [while-true]
s  : <while e st> => s''


s : <e> => false
----------------------    [while-false]
s : <while e st> => s

Two possible matches, based on premises: one for when the conditional expression evaluates to true and one for false.

Note that the premises for while-true has the same syntax <while e st> evaluated again, but under a new state, s', i.e., the state after evaluating the body once, s : <st> => s'.

Executing the while loop again results in yet another potentially-new state, s''

Homework

  1. Write a semantic rule for an if-then-else statement,

    s : <if e then st1 else st2> => s'.

    Assume you already have rules for evaluating boolean expressions, i.e., s : <e> => true or s : <e> => false, and for statements, i.e., s : <st> => s'.

    // if, then, and else are keywords
    // e is the conditional expression metavariable
    // st1 and st2 are metavariables for the if- and else-branches
    st ::= if e then st1 else st2
    
  2. (Optional) Write a semantic rule for a side-effect free if-then-else statement, that evaluates to to a value instead, i.e., s : <if e1 then e2 else e3> => v, assuming there are already rules for expression evaluation.

Author: Paul Gazzillo

Created: 2022-02-14 Mon 11:49