Semantics

The Importance of Semantics

  • Semantics refers to the meaning of a programing language, including statements, expressions, and entire programs
  • Programmers must know the meaning of a language in order to use it
  • Compiler writers need to reflect meaning of a langauge in the implementation of compilers
  • If we had a perfect description of semantics
    • Programs could be proven correct without execution
    • Compilers could be automatically generated from langauge description

Operational Semantics

  • Describe the meaning of a statement or entire program by describing the values of memory in a machine before and after execution
  • Writing out the state of every type of memory in a modern computer is way to cumbersome
    • We use an intermediate language as a proxy

Operational Semantics Example

for (expr1;expr2;expr3) {
    < loop_body > 
}

expr1;
loop: if expr2 == 0 goto out
      < loop_body>
      expr3;
      goto loop
out:  ...

Operational Semantics Practice

Give the operational semantics description for the following:

expr1;
do{
< body >
expr2;
}while(expr3);
switch(expr1)
{
    case const1:
        < body1 >
        break;
    case const2:
        < body2 >
        break;
    default:
        < body3 >
        break;
}

Operational Semantics Problems

  • Intermediate language is not mathmateically rigourous
  • Can have situations with circularities
  • Can become so complex that they are not practical

Denotational Semantics

  • A mathmateical model of semantics
  • Each part of a language , usually a syntax rule is associated with:
    • A mathmatical object, like an integer
    • A function mapping an instance of that sytnax rule to the mathmatical object
  • The state of a program is the values of all its variables
    • A state, $s$, can be represented as $ s = \{ < i_1, v_1 >, < i_2, v_2 > , ... , < i_n, v_n> \} $
    • $i_x $ is a variable and $ v_x $ is it's corresponding value
    • We define a function VARMAP($i_j,s) = v_j$

Denotational Semantics Example

For the grammar:

  • $ < dec\_num > \to$ '0' | '1' | '2'| '3' | '4' | '5' | '6' | '7' | '8' | '9'
  • $ \qquad \qquad \quad \, \, \,$ | $< dec\_num>$ ('0' | '1' | '2'| '3' | '4' | '5' | '6' | '7' | '8' | '9')

The denotational mappings are:

  • $M_{dec}$('0') = 0, $M_{dec}$('1') = 1, $M_{dec}$('2') = 2, ... , $M_{dec}$('9') = 9
  • $M_{dec}$( $< dec\_num >$ '0') = 10 * $M_{dec}$( $< dec\_num >$)
  • $M_{dec}$( $< dec\_num >$ '1') = 10 * $M_{dec}$( $< dec\_num >$) + 1
  • ...
  • $M_{dec}$( $< dec\_num >$ '9') = 10 * $M_{dec}$( $< dec\_num >$) + 9

Denotational Semantics Example

For the grammar:

  • $< expr > \to < dec\_num > | < var > | < binary\_expr > $
  • $< binary\_expr > \to < left\_expr > < operator > < right\_expr> $
  • $< left\_expr > \to < dec\_num > | < var > $
  • $< right\_expr > \to < dec\_num > | < var > $
  • $< operator > \to +$ | $* $

The denotational mapping is:

  • $M_e(< expr > , s) \; \Delta\!\!=$ case $< expr >$ of
  • $\qquad \qquad \qquad< dec\_num > \Rightarrow M_{dec}( < dec\_num >, s)$
  • $\qquad \qquad \qquad< var > \Rightarrow$ if VARMAP( $< var > , s $) $ == $ undef
  • $\qquad \qquad \qquad \qquad \qquad$ then error
  • $\qquad \qquad \qquad \qquad \qquad$ else VARMAP( $< var > $ , s)
  • $\qquad \qquad \qquad< binary\_expr > \Rightarrow$
  • $\qquad \qquad \qquad \quad$ if ($M_e ( < binary\_expr >.< left\_expr >, s) == $ undef OR
  • $ \qquad \qquad \qquad \qquad M_e ( < binary\_expr >.< right\_expr >, s) == $ undef)
  • $ \qquad \qquad \qquad \quad $ then error
  • $ \qquad \qquad \qquad \quad $ else if ($ < binary\_expr >.< operator > == $ '+')
  • $ \qquad \qquad \qquad \qquad $ then $M_e( < binary\_expr >.< left\_expr > ,s) + $
  • $ \qquad \qquad \qquad \qquad \quad \, \, \,$ $M_e( < binary\_expr >.< right\_expr > ,s) $
  • $ \qquad \qquad \qquad \qquad $else $M_e( < binary\_expr >.< right\_expr > ,s) * $
  • $ \qquad \qquad \qquad \qquad \quad \, \, \,$ $M_e( < binary\_expr >.< right\_expr > ,s) $

Denotational Semantics Continued

  • $M_a( x = E, s) \; \Delta\!\!=$ if $M_e(E,s) == $ error
  • $\qquad \qquad \qquad$ then error
  • $\qquad \qquad \qquad$ else $s' = \{ < i_1, v_1' > , < i_2, v_2' >, ... , < i_n, v_n' >, $ where
  • $\qquad \qquad \qquad \qquad$ for $j = 1,2,...,n$
  • $\qquad \qquad \qquad \qquad \quad$ if $i_j == x$
  • $\qquad \qquad \qquad \qquad \qquad$ then $v_j' = M_e(E,s)$
  • $\qquad \qquad \qquad \qquad \qquad$ else $v_j' = $VARMAP($i_j,s)$

Axiomatic Semantics

  • Based on logic
  • No model of actual vaulues, on the relationship between variables
  • Used for both specificying programing meaning and program verification

Assertions

  • Logic expressions that describe constraints on statements
  • Ones that are before a statement are called Preconditions
  • Ones that are after a statement are called Postconditions

Weakest Precondition

  • Least restrictive precondition that garuntees postcondition will always be true
  • We can work backwards from the last statment of a program to get the weakest precondition for the entire program
    • First find the weakest precondition for the last statement
    • Use this as the post condition to the second to last statement
    • Continue until we have the precondition for the first statement
    • The precondition of the first statement describes under what inputs the program will always be correct

Inference Rules

Writen as
$$\frac{S1,S2,S3,...Sn}{S}$$

Meaning if $S1,S2,...,$ and $Sn$ are all true, then we can infer $S$ is true.

An axiom is a rule that is assumed to be true. For example just $S$.

Rule of Consqeuence

Precondition can be stregthed, post condition can always be weakened.

$$\frac{\{P\}S\{Q\}, P' \Rightarrow P, Q \Rightarrow Q'}{\{P'\}S\{Q'\}}$$

For example, given:

{ x > 3} x = x $-$ 3 {x > 0}

We can prove that { x > 5} x = x $-$ 3 {x > 0} is valid

$$\frac{\{x>3\}x=x-3\{x>0\}, \{x > 5\} \Rightarrow \{x > 3\}, \{x > 0\} \Rightarrow \{x > 0 \}}{\{x > 5\}x=x-3\{x > 0\}}$$

Sequences of Statements

Given the statements
{P1} S1 {P2}
{P2} S2 {P3}

We can use the following inference rule:

$$\frac{\{P1\}S1\{P2\}, \{P2\}S2\{P3\}}{\{P1\}S1,S2\{P3\}}$$

Weakest Precondition Example

Compute weakest precondition

  • a = 3 * ( 2 * b + a);
  • b = 2 * a - 1;
  • { b > 5}
  • x = 2 * y + x - 1;
  • { x > 11}

Conditionals

The inference rule for a conditional statement such as

if B then S1 else S2

is as follows

$$\frac{\{B \, \textrm{and} \, P\}S1\{Q\}, \{(\textrm{not}\, B) \, \textrm{and} \, P\}S2\{Q\}}{\{P\}\textrm{if B then S1 else S2}\{Q\}}$$

Conditionals Practice

Find the weakest precondition for the statement below

if x > 0 then
    y = y - 1
else
    y = y + 1

{y > 0}

Wrap Up

  • Semantics is complex and there is no one system
  • Different representations have advantages for different purposes