CMSC330

Operational Semantics

Operational Smeantics

Semantics
Operational Semantics
OpSem of OCaml
Definitional Interpreter

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}\]

Syntax for the class:

\[\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 thier 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}\]

\[\frac{\frac{}{A;2 \Rightarrow 2}\qquad \frac{}{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{\frac{}{A;2 \Rightarrow 2}\qquad \frac{}{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

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{\frac{}{A;3\Rightarrow 3}\qquad \frac{\frac{A,x:3(x)=3}{A,x:3;x\Rightarrow 3}\qquad\frac{}{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}\]

Definitional Interpreter

Let's go back to our simple language


(* E -> n|E + E *)
type expr = Num of int|Plus of expr * expr
          

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

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


let rec eval e =
  match e with
   Num(n) -> n
  |Plus(e1,e2) -> 
    let n1 = eval e1 in 
    let n2 = eval e2 in 
    let n3 = n1+n2 in 
    n3
  |_ -> failwith "error"
          

Environment: a mapping from Identifiers to values


(* E -> x|n|E + E|let x = E in E *)
type expr = Iden of string|Num of int|Plus of expr * expr
            |Let of string * expr *expr

let rec lookup env x = match env with
   []-> failwith "undefined variable"
  |(id,v)::t -> if x = id then v else 
                lookup t x    

let extend env x v = (x,v)::env
          

let rec eval env e =
  match e with
   Iden(x) -> lookup env x
  |Num(n) -> n
  |Plus(e1,e2) -> 
    let n1 = eval env e1 in 
    let n2 = eval env e2 in 
    let n3 = n1+n2 in 
    n3
  |Let (x,e1,e2) -> 
    let v1 = eval env e1 in
    let menv = extend env x v1 in
    let v2 = eval menv e2 in
    v2
  |_ -> failwith "error"