Meeting 11 - Operational Semantics

Bor-Yuh Evan Chang

Tuesday, October 1, 2024

\(\newcommand{\TirName}[1]{\text{#1}} \newcommand{\inferrule}[3][]{ \let\and\qquad \begin{array}{@{}l@{}} \TirName{#1} \\ \displaystyle \frac{#2}{#3} \end{array} } \newcommand{\infer}[3][]{\inferrule[#1]{#2}{#3}} \)

Meeting 11 - Operational Semantics

What questions does your neighbor have?

In-Class Slides
In-Class Jupyter
Book Chapter

Announcements

  • HW1 scores to be released later today
  • HW3 due Friday (or Monday?) 6pm
  • Peer Reviews on Unit 2 Thu-Fri Oct 3-4 to prepare for the exam
  • Exam on Units 1-2 next Tue Oct 8
    • 50 minutes in class
    • Accommodation letters confirmed (some weeks ago)?
      • Look out for note on Piazza about the extended-time location
      • During class time
    • Practice are all of the learning activities: HWs, Labs, Peer Review

Today

Feedback Rubric

4 - Exceeding (E)

Student has met and exceeded the learning objective.

3 - Proficient (P)

Student has proficiently met the learning objective.

2 - Approaching (A)

Student is approaching the learning objective.

1 - Novice (N)

Student is far from meeting the learning objective.

0 - Zero (Z)

Student has made no attempt towards the learning objective.

Feedback Rubric

4 - Exceeding (E)

Student demonstrates mastery and shows the ability to apply and transfer learning with depth and complexity.

Student demonstrates synthesis of the underlying concepts. Student can go beyond merely describing the solution to explaining the underlying reasoning and discussing generalizations.

3 - Proficient (P)

Student demonstrates competence.

Student is able to explain the overall solution and can answer specific questions. While the student is capable of explaining their solution, they may not be able to confidently extend their explanation beyond the immediate context.

2 - Approaching (A)

Student demonstrates partial understanding.

Student may able to describe the solution but has difficulty answering specific questions about it. Student has difficulty explaining the reasoning behind their solution.

Grading Scale

Level Percentage Range Letter Grade
Exceeding (E) 88% – 100% A
82% – 88% A-
Proficient (P) 75% – 82% B+
68% – 75% B
61% – 68% B-
56% – 61% C+
51% – 56% C
Approaching (A) 46% – 51% C-
42% – 46% D+
38% – 42% D
34% – 38% D-

Peer Reviews

Goal: Staff-organized 30-minute review session to prepare for the exam.

  • A day or two before the the sessions, the instructors will release a set of review questions on the unit (think: practice exam questions).
  • You sign up for a 30-minute slot to take turns asking each other these questions and giving feedback on where your partner is at on their learning journey (Novice, Approaching, Proficient, Exceeding).
  • Show up at your slot with the review questions and your laptop ready. No upfront prep other than completing the homework and lab.
  • A member of the Feedback Staff will join most sessions to offer guidance but will not be an interviewer. They are a third-party participant.
  • You do not “grade” your partner on their learning level but only assess to what extent your partner was an active participant. The Feedback Staff will do the same.

Questions?

  • Review:
    • What is relation between judgment forms, inference rules, and judgments?

Questions?

Operational Semantics

Big Picture

What is the purpose of a language specification?








What is the difference between syntax and semantics?

Lab 2: JavaScript is Bananas

JavaScript
JavaScript
JavaScript

How can we describe how to implement a interpreter for all JavaScripty programs?

Evaluation Judgment Form

\[ e \Downarrow v \]

“Expression \(e\) evaluates to value \(v\).”

A set of inference rules defining such an evaluation judgment form is called a big-step operational semantics for expressions \(e\).

Closed Expressions

\[ \begin{array}{rrrl} \text{expressions} & e& \mathrel{::=}& v\mid e_1 \mathbin{\mathit{bop}} e_2 \\ \text{values} & v& \mathrel{::=}& n \\ \text{binary operators} & \mathit{bop}& \mathrel{::=}& \texttt{+} \\ \text{numbers} & n \end{array} \]

defined trait Expr
defined trait Bop
defined class Binary
defined class N
defined object Plus
defined function isValue
e_oneplustwo: Binary = Binary(bop = Plus, e1 = N(n = 1.0), e2 = N(n = 2.0))

EvalNum and EvalPlus Rules

\[ \inferrule[EvalNum]{ }{ ??? } \]

\[ \inferrule[EvalPlus]{ }{ ??? } \]

EvalNum and EvalPlus Code

defined function eval

Dynamic Typing

Booleans

\[ \begin{array}{rrrl} \text{values} & v& \mathrel{::=}& b \\ \text{booleans} & b \end{array} \]

defined class B
defined function isValue
e_true: B = B(b = true)
e_trueplustwo: Binary = Binary(bop = Plus, e1 = B(b = true), e2 = N(n = 2.0))

EvalVal

\[ \inferrule[EvalVal]{ }{ v \Downarrow v } \]

defined function eval

MatchError

scala.MatchError: B(true) (of class ammonite.$sess.cmd3$Helper$B)
  ammonite.$sess.cmd5$Helper.eval(cmd5.sc:6)
  ammonite.$sess.cmd6$Helper.<init>(cmd6.sc:2)
  ammonite.$sess.cmd6$.<clinit>(cmd6.sc:7)

DynamicTypeError

defined class DynamicTypeError
defined function eval
TypeError: in expression Binary(Plus,B(true),N(2.0))
  ammonite.$sess.cmd9$Helper.eval(cmd9.sc:9)
  ammonite.$sess.cmd10$Helper.<init>(cmd10.sc:2)
  ammonite.$sess.cmd10$.<clinit>(cmd10.sc:7)

Coercions

toNumber

\(\fbox{$v \rightsquigarrow n$}\)

\(\inferrule[ToNumberNum]{ }{ n \rightsquigarrow n }\)

\(\inferrule[ToNumberTrue]{ }{ \mathbf{true} \rightsquigarrow 1 }\)

\(\inferrule[ToNumberFalse]{ }{ \mathbf{true} \rightsquigarrow 0 }\)

defined function toNumber

EvalVal and EvalPlus

\[ \inferrule[EvalVal]{ }{ ??? } \]

\[ \inferrule[EvalPlus]{ }{ ??? } \]

Variables

Syntax

\[ \begin{array}{rrrl} \text{expressions} & e& \mathrel{::=}& x\mid\mathbf{const}\;x\;\texttt{=}\;e_1\texttt{;}\;e_2 \\ \text{variables} & x, y \end{array} \]

defined class Var
defined class ConstDecl
e_const_i_two_trueplusi: ConstDecl = ConstDecl(
  x = "i",
  e1 = N(n = 2.0),
  e2 = Binary(bop = Plus, e1 = B(b = true), e2 = Var(x = "i"))
)
defined type Env
empty: Env = Map()
defined function lookup
defined function extend

Value Environments

\[ \begin{array}{rrrl} \text{value environments} & E, \mathit{env}& \mathrel{::=}& \cdot \mid E[\mathop{}x \mapsto v] \end{array} \]

Semantics

\[ \inferrule[EvalVar]{ }{ ??? } \]

\[ \inferrule[EvalConstDecl]{ }{ ??? } \]

Variables, Numbers, and Booleans

\(\inferrule[EvalVar]{ \phantom{\Downarrow} }{ E \vdash x \Downarrow E(x) }\)

\(\inferrule[EvalConstDecl]{ E \vdash e_1 \Downarrow v_1 \and E[\mathop{}x \mapsto v_1] \vdash e_2 \Downarrow v_2 }{ E \vdash \mathbf{const}\;x\;\texttt{=}\;e_1\texttt{;}\;e_2 \Downarrow v_2 }\)

\(\inferrule[EvalVal]{ \phantom{\Downarrow} }{ E \vdash v \Downarrow v }\)

\(\inferrule[EvalNeg]{ E \vdash e_1 \Downarrow v_1 \and v_1 \rightsquigarrow n_1 }{ E \vdash \mathop{\texttt{-}} e_1 \Downarrow - n_1 }\)

\(\inferrule[EvalNot]{ E \vdash e_1 \Downarrow v_1 \and v_1 \rightsquigarrow b_1 }{ E \vdash \mathop{\texttt{!}} e_1 \Downarrow \lnot b_1 }\)

\(\inferrule[EvalArith]{ E \vdash e_1 \Downarrow v_1 \and E \vdash e_2 \Downarrow v_2 \and v_1 \rightsquigarrow n_1 \and v_2 \rightsquigarrow n_2 \and \mathit{bop}\in \left\{ \texttt{+}, \texttt{-}, \texttt{*}, \texttt{/} \right\} }{ E \vdash e_1 \mathbin{\mathit{bop}} e_2 \Downarrow n_1 \mathbin{\mathit{bop}} n_2 }\)

\(\inferrule[EvalAndTrue]{ E \vdash e_1 \Downarrow v_1 \and v_1 \rightsquigarrow \mathbf{true} \and E \vdash e_2 \Downarrow v_2 }{ E \vdash e_1 \mathbin{\texttt{\&\&}} e_2 \Downarrow v_2 }\)

\(\inferrule[EvalAndFalse]{ E \vdash e_1 \Downarrow v_1 \and v_1 \rightsquigarrow \mathbf{false} }{ E \vdash e_1 \mathbin{\texttt{\&\&}} e_2 \Downarrow v_1 }\)

\(\inferrule[EvalOrTrue]{ E \vdash e_1 \Downarrow v_1 \and v_1 \rightsquigarrow \mathbf{true} }{ E \vdash e_1 \mathbin{\texttt{||}} e_2 \Downarrow v_1 }\)

\(\inferrule[EvalOrFalse]{ E \vdash e_1 \Downarrow v_1 \and v_1 \rightsquigarrow \mathbf{false} \and E \vdash e_2 \Downarrow v_2 }{ E \vdash e_1 \mathbin{\texttt{||}} e_2 \Downarrow v_2 }\)

\(\inferrule[EvalIfTrue]{ E \vdash e_1 \Downarrow v_1 \and v_1 \rightsquigarrow \mathbf{true} \and E \vdash e_2 \Downarrow v_2 }{ E \vdash e_1\;\texttt{?}\;e_2\;\texttt{:}\;e_3 \Downarrow v_2 }\)

\(\inferrule[EvalIfFalse]{ E \vdash e_1 \Downarrow v_1 \and v_1 \rightsquigarrow \mathbf{false} \and E \vdash e_3 \Downarrow v_3 }{ E \vdash e_1\;\texttt{?}\;e_2\;\texttt{:}\;e_3 \Downarrow v_3 }\)

\(\inferrule[EvalEquality]{ E \vdash e_1 \Downarrow v_1 \and E \vdash e_2 \Downarrow v_2 \and \mathit{bop}\in \left\{ \texttt{===}, \texttt{!==} \right\} }{ E \vdash e_1 \mathbin{\mathit{bop}} e_2 \Downarrow v_1 \mathbin{\mathit{bop}} v_2 }\)

\(\inferrule[EvalInequality]{ E \vdash e_1 \Downarrow v_1 \and E \vdash e_2 \Downarrow v_2 \and v_1 \rightsquigarrow n_1 \and v_2 \rightsquigarrow n_2 \and \mathit{bop}\in \left\{ \texttt{<}, \texttt{<=}, \texttt{>}, \texttt{>=} \right\} }{ E \vdash e_1 \mathbin{\mathit{bop}} e_2 \Downarrow n_1 \mathbin{\mathit{bop}} n_2 }\)