CMSC330

Operational Semantics

Operational Semantics

Semantics
Operational Semantics
OpSem of OCaml

Semantics

Semantics

Semantics: The meaning of a phrase


//java
int x = 2 + 3;
(* ocaml *)
let x = 2 + 3;;
# ruby
x = 2 + 3;;
// go
x := 2 + 3
// javascript
var x = 2 +3;
          

5 Idioms, 1 'semantic'

  • Denotational Semantics: Describe meanings through mathematical constructs
  • Axiomatic Semantics: Describe meanings through promises
  • Operational Semantics: Describe meanings through how things execute
  • Denotational Semantics: Describe meanings through mathematical constructs
  • Axiomatic Semantics: Describe meanings through promises
  • Operational Semantics: Describe meanings through how things execute
    • Helpful for making interpreters

Operational Semantics

OpSem ultimately creates a proof of correctness or properties

Syntax for this class:

  • Value: \(v\)
  • Expression: \(e\)
  • Environment: \(A\)

Goal: create a definitional interpreter

Opsem of OCaml

We will create rules for how an ocaml program will execute

Suppose our languge is small: only numbers


(* Grammar *)
E -> n
          

An interpreter needs a rule of what an expression returns

An interpreter needs a rule of what an expression returns

\(e \Rightarrow v\)

  • Where \(e := n\)
  • Where \(v := n\)

Let us add addition to our language


(* Grammar *)
E -> n| E + E
          

\(e \Rightarrow v\)

  • Where \(e := n|e+e\)
  • Where \(v := n\)

(* Grammar *)
E -> n| E + E
          

\(e \Rightarrow v\)

  • If \(e\) is a number \(n\) then \(n \Rightarrow n\)
  • If \(e\) is an expression of \(e_1 + e_2\) then
    • if \(e_1 \Rightarrow n_1\)
    • if \(e_2 \Rightarrow n_2\)
    • if \(n_1 + n_2 = n_3\)
    • then \(e \Rightarrow n_3\)

This is an argument structure

  • if \(e_1 \Rightarrow n_1\)
  • if \(e_2 \Rightarrow n_2\)
  • if \(n_1 + n_2 = n_3\)
  • then \(e \Rightarrow n_3\)

\[\begin{array}{rl} & e_1 \Rightarrow n_1\\ & e_2 \Rightarrow n_2\\ & n_1 + n_2 = n_3\\\hline \therefore & e_1 + e_2 \Rightarrow n_3\\ \end{array}\]

Rules of inference:

\[\frac{H_1 ... H_n}{C}\]

  • If the conditions \(H_1\) ...\(H_n\) ("hypotheses") are true, then the condition C ("conclusion") is true
  • If \(n = 0\) (no hypotheses) then the conclusion automatically holds; this is called an axiom

Syntax for the class:

\[\frac{H_1 ... H_n}{C}\]

\[\frac{e1 \Rightarrow n1\qquad e2 \Rightarrow n2\qquad n3\ \text{is}\ n1+n2}{e1+e2 \Rightarrow n3}\]


(* Grammar *)
E -> n|E + E
          

Suppose \(e\) is a number \(n\):

\[\frac{}{n \Rightarrow n}\]

Suppose \(e\) is a an expression of \(e1 + e2\):

\[\frac{e1 \Rightarrow n1\qquad e2 \Rightarrow n2\qquad n3\ \text{is}\ n1+n2}{e1+e2 \Rightarrow n3}\]

Let's add more to the language


(* Grammar *)
E -> x|n|E + E|let x = E in E
          
  • Where \(x\) is a variable name (identifier)
  • Where \(x \Rightarrow v\)

We need an enviroment \(A\) to store variables and their values


(* Grammar *)
E -> x|n|E + E|let x = E in E
          

Suppose \(e\) is \(x\):

\[\frac{A(x) = v}{A; x \Rightarrow v}\]


(* Grammar *)
E -> x|n|E + E|let x = E in E
          

Suppose \(e\) is \(x\):

\[\frac{A(x) = v}{A; x \Rightarrow v}\]

Suppose \(e\) is \(\text{let }x = e_1\ \text{in}\ e_2\):

\[\frac{A;e_1\Rightarrow v_1 \qquad A,x:v_1;e_2\Rightarrow v_2}{A;\text{let }x = e_1\ \text{in}\ e_2 \Rightarrow v2}\]


(* Grammar *)
E -> x|n|E + E|let x = E in E
          

Putting it all together:

Suppose \(e\) is a number \(n\):

\[\frac{}{A;n \Rightarrow n}\]

Suppose \(e\) is a an expression of \(e1 + e2\):

\[\frac{A;e1 \Rightarrow n1\qquad A;e2 \Rightarrow n2\qquad A;n3\ \text{is}\ n1+n2}{A;e1+e2 \Rightarrow n3}\]

Suppose \(e\) is \(x\):

\[\frac{A(x) = v}{A; x \Rightarrow v}\]

Suppose \(e\) is \(\text{let }x = e_1\ \text{in}\ e_2\):

\[\frac{A;e_1\Rightarrow v_1 \qquad A,x:v_1;e_2\Rightarrow v_2}{A;\text{let }x = e_1\ \text{in}\ e_2 \Rightarrow v2}\]

Putting it all together:

Suppose \(e\) is a number \(n\):

\[\frac{}{A;n \Rightarrow n}\]

Suppose \(e\) is a an expression of \(e1 + e2\):

\[\frac{A;e1 \Rightarrow n1\qquad A;e2 \Rightarrow n2\qquad A;n3\ \text{is}\ n1+n2}{A;e1+e2 \Rightarrow n3}\]

Suppose \(e\) is \(x\):

\[\frac{A(x) = v}{A; x \Rightarrow v}\]

Suppose \(e\) is \(\text{let }x = e_1\ \text{in}\ e_2\):

\[\frac{A;e_1\Rightarrow v_1 \qquad A,x:v_1;e_2\Rightarrow v_2}{A;\text{let }x = e_1\ \text{in}\ e_2 \Rightarrow v2}\]

Time to derive/create proofs

Suppose \(e\) is a number \(n\):

\[\frac{}{A;n \Rightarrow n}\]

Suppose \(e\) is a an expression of \(e1 + e2\):

\[\frac{A;e1 \Rightarrow n1\qquad A;e2 \Rightarrow n2\qquad A;n3\ \text{is}\ n1+n2}{A;e1+e2 \Rightarrow n3}\]

Suppose \(e\) is \(x\):

\[\frac{A(x) = v}{A; x \Rightarrow v}\]

Suppose \(e\) is \(\text{let }x = e_1\ \text{in}\ e_2\):

\[\frac{A;e_1\Rightarrow v_1 \qquad A,x:v_1;e_2\Rightarrow v_2}{A;\text{let }x = e_1\ \text{in}\ e_2 \Rightarrow v2}\]

If these are the rules of our language: prove that 2+4 is both valid in the language and evaluates to 6

If these are the rules of our language: prove that 2+4 is both valid in the language and evaluates to 6

2+4 is a an expression of \(e1 + e2\):

\[\frac{A;e1 \Rightarrow n1\qquad A;e2 \Rightarrow n2\qquad A;n3\ \text{is}\ n1+n2}{A;e1+e2 \Rightarrow n3}\]

\[\cfrac{\cfrac{}{A;2 \Rightarrow 2}\qquad \cfrac{}{A;4 \Rightarrow 4}\qquad A;6\ \text{is}\ 2+4}{A;2+4 \Rightarrow 6}\]

If these are the rules of our language: prove that 2+4 is both valid in the language and evaluates to 6

2+4 is a an expression of \(e1 + e2\):

\[\frac{A;e1 \Rightarrow n1\qquad A;e2 \Rightarrow n2\qquad A;n3\ \text{is}\ n1+n2}{A;e1+e2 \Rightarrow n3}\]

\[\frac{\cfrac{}{A;2 \Rightarrow 2}\qquad \cfrac{}{A;4 \Rightarrow 4}\qquad A;6\ \text{is}\ 2+4}{A;2+4 \Rightarrow 6}\]

Now prove that let x = 3 in x + 4 is both valid in the language and evaluates to 7

Now prove that let x = 3 in x + 4 is both valid in the language and evaluates to 7

\[\frac{}{A;\text{let }x = 3\ \text{in}\ x+4 \Rightarrow 7}\]

Suppose \(e\) is a number \(n\):

\[\frac{}{A;n \Rightarrow n}\]

Suppose \(e\) is a an expression of \(e1 + e2\):

\[\frac{A;e1 \Rightarrow n1\qquad A;e2 \Rightarrow n2\qquad A;n3\ \text{is}\ n1+n2}{A;e1+e2 \Rightarrow n3}\]

Suppose \(e\) is \(x\):

\[\frac{A(x) = v}{A; x \Rightarrow v}\]

Suppose \(e\) is \(\text{let }x = e_1\ \text{in}\ e_2\):

\[\frac{A;e_1\Rightarrow v_1 \qquad A,x:v_1;e_2\Rightarrow v_2}{A;\text{let }x = e_1\ \text{in}\ e_2 \Rightarrow v2}\]

Now prove that let x = 3 in x + 4 is both valid in the language and evaluates to 7

\[\frac{\cfrac{}{A;3\Rightarrow 3}\qquad \cfrac{\cfrac{A,x:3(x)=3}{A,x:3;x\Rightarrow 3}\qquad\cfrac{}{A,x:3;4\Rightarrow 4}\qquad 7\text{ is }3+4}{A,x:3;x+4\Rightarrow 7}}{A;\text{let }x = 3\ \text{in}\ x+4 \Rightarrow 7}\]

As our language gets more complicated, the more rules we need to have


(* Grammar *)
E -> x|n|E + E|let x = E in E
    |true|false|eq0 E
          

\[\frac{}{A;true \Rightarrow true}\]

\[\frac{}{A;false \Rightarrow false}\]

\[\frac{A;e \Rightarrow 0}{A;\text{eq0 } e \Rightarrow true}\]

\[\frac{A;e \Rightarrow v\qquad v \neq 0}{A;\text{eq0 } e \Rightarrow false}\]