CMSC330

Operational Semantics

Operational Semantics

Semantics
Operational Semantics
OpSem of OCaml
OpSem of Lambda Calc
Interpreter

Semantics

Semantics

Semantics: The meaning of a phrase


//java
int x = 2 + 3;

(* ocaml *)
let x = 2 + 3;;

# ruby/python
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 language 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}\]

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

Note: n3 is n1+n2 is in the meta language

Meta Language: the language used to describe the target language

Target Language: the language we are describing


(* 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 environment \(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}\]

\[\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

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

Lambda Calc


e -> x
    |λx.e
    |e e
          

\[\cfrac{}{A;x \Rightarrow x}\]

\[\cfrac{A;e \Rightarrow e'}{A;\lambda x.e \Rightarrow \lambda x.e'}\]

\[\cfrac{A;e_1 \Rightarrow e'}{A;(e_1\ e_2) \Rightarrow (e' e_2)}\]

\[\cfrac{A;e_2 \Rightarrow e'}{A;(e_1\ e_2) \Rightarrow (e_1\ e')}\]

\[\cfrac{A;e_2 \Rightarrow e'\qquad A,x:e';e_1 \Rightarrow e''}{A;((\lambda x.e_1)\ e_2) \Rightarrow e''}\]

\[\cfrac{}{A;x \Rightarrow x}\]

\[\cfrac{A;e \Rightarrow e'}{A;\lambda x.e \Rightarrow \lambda x.e'}\]

\[\cfrac{A;e_1 \Rightarrow e'}{A;(e_1\ e_2) \Rightarrow (e' e_2)}\]

\[\cfrac{A;e_2 \Rightarrow e'}{A;(e_1\ e_2) \Rightarrow (e_1\ e')}\]

\[\cfrac{A;e_2 \Rightarrow e'\qquad A,x:e';e_1 \Rightarrow e''}{A;((\lambda x.e_1)\ e_2) \Rightarrow e''}\]

Which are Lazy? Which are Eager?

Note: level of abstractness can be Arbitrary

Note: level of abstractness can be Arbitrary


def f(x)
  x = x * x
  return x + 1
          

\[\cfrac{}{A;f(e) \Downarrow v}\]

\[\cfrac{A;e \Rightarrow n \qquad v\ is\ 1+(n*n)}{A;f(e) \Rightarrow v}\]

Like step into vs step over from GDB

Definitional Interpreter

Enter: (a subset of) LOLCODE


          3                 BTW Single number
          SUM OF 3 AN 3     BTW 6
          I HAS A var ITZ 5 BTW var = 5
          SUM OF var AN 3   BTW 8
          

          3                 BTW Single number
          SUM OF 3 AN 3     BTW 6
          I HAS A var ITZ 5 BTW var = 5
          SUM OF var AN 3   BTW 8
          

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

\[\frac{A;e_1 \Rightarrow n_1\qquad A;e_2 \Rightarrow n_2\qquad A;n_3\ \text{is}\ n_1+n_2}{A;SUM\ OF\ e_1\ AN\ e_2 \Rightarrow n_3}\]

\[\frac{A;y \Rightarrow v_1\qquad A,x:y;e2 \Rightarrow v_2}{A;I\ HAS\ A\ x\ ITZ\ y\ \text{\\}n\ e_2 \Rightarrow v_2}\]

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