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 Statements¶

  • The book suggests the following statements are enough to write Operational Semantics of control structures
    • ident = var
    • ident = ident + 1
    • ident = ident -1
    • goto label
    • if var relop var goto label

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);

Answer

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

or

expr1;
loop:   <body>
        expr2;
        if expr3 == 1 goto loop;
        ....

Operational Semantics Practice¶

Give the operational semantics description for the following:

switch(expr1)
{
    case const1:
        < body1 >
        break;
    case const2:
        < body2 >
        break;
    default:
        < body3 >
        break;
}

Answer:

if expr1 == const1 goto L1;
        if expr2 == const2 goto L2;
        <body3>;
        goto out;
L1:     <body1>
        goto out;
L2:     <body2>
        goto out;
out:    ....

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 >.< left\_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}
Answer: $$ 2 \times a - 1 > 5\\ 2 \times a > 6\\ a > 3 $$ So a>3 is the precondition for the last line and the post condition for the first line. $$ 3 \times ( 2 \times b + a) > 3\\ 2 \times b + a > 1\\ 2 \times b > 1 - a\\ b > \frac{1 - a}{2} $$ The precondition for the entire sequence of statements is $b > \frac{1 -a}{2}$
  • x = 2 * y + x - 1;
  • { x > 11}
$$ 2 \times y + x - 1 > 11\\ 2 \times y + x > 12\\ 2 \times y > 12 - x\\ y > 6 - \frac{x}{2}\\ $$

The precondition is then $y > 6 - \frac{x}{2}$

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}

Because there is an if statement, we need to evaluate the postcondition for all possible paths of execution

$$ y = y - 1\\ \{y > 0\}\\ y - 1 > 0\\ y > 1 $$

AND

$$ y = y + 1\\ \{y > 0\}\\ y + 1 > 0\\ y > -1 $$

Our two possible preconditions are then $y > 1$ or $y > -1$. Our job is to pick the one that will make the postcondition of $\{y > 0\}$ no matter which branch of the if-statement is evaluated.

By substituting in each possible value in each branch, we find that only $y > 1$ causes the postcondition to always be true, so that is the precondition for the entire if-statement

Wrap Up¶

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