Class and Companion Object

Examples

Examples of uses of object and companion objects in a large Scala project:

Expressions, Conditionals

Examples

Examples of uses of expressions and non-strict evaluation in a large Scala project:

Contracts

Examples

Examples of uses of contracts in a large Scala project:

Formal Semantics

Syntax

Below is a grammar for integer expressions. The non-terminal symbol Number is not expanded further, it is assumed to define the usual syntax of signed integer literals.

Expr ::= Number
       | Expr '+' Expr 
       | Expr '-' Expr 
       | Expr '*' Expr 
       | '(' Expr ')'

Rules

Below are reduction rules for our language of integer expressions. The store \(\xi\) is irrelevant in this language without variables and just passed on unmodified.

Numbers

\(\frac{}{\langle\mathtt{n},\xi\rangle \Downarrow \langle n,\xi\rangle} \text{\tiny(Num)}\)

Parentheses

\(\frac{\langle\mathtt{e},\xi\rangle \Downarrow \langle v, \xi_1\rangle}{\langle\mathtt{(e)},\xi\rangle \Downarrow \langle v,\xi_1\rangle} \text{\tiny(Par)}\)

  • Operations

\(\frac{\langle \mathtt{e}_1,\xi \rangle \Downarrow \langle v_1,\xi \rangle ~~ \langle \mathtt{e}_2,\xi \rangle \Downarrow \langle v_2,\xi \rangle}{\langle \mathtt{e}_1 \mathtt{+} \mathtt{e}_2,\xi\rangle \Downarrow \langle v_1 + v_2,\xi \rangle}\text{\tiny(Add)}\)

\(\frac{\langle \mathtt{e}_1,\xi \rangle \Downarrow \langle v_1,\xi \rangle ~~ \langle \mathtt{e}_2,\xi \rangle \Downarrow \langle v_2,\xi \rangle}{\langle \mathtt{e}_1 \mathtt{-} \mathtt{e}_2,\xi\rangle \Downarrow \langle v_1 - v_2,\xi\rangle}\text{\tiny(Sub)}\)

Define the Semantics of Multiplication

Analogous to addition and subtraction, define the reduction rule for multiplication.

\(\frac{}{\langle \mathtt{e}_1 \mathtt{*} \mathtt{e}_2,\xi\rangle \Downarrow \langle ~~~~~~~~~~~~ \rangle}\text{\tiny(Mul)}\)

Solution: Semantics of Multiplication

Annotate Rules

Annotate the missing rule names in the reduction of \(\mathtt{(2*3)}\) to its integer value.

\[\begin{prooftree} \AxiomC{} \UnaryInfC{$\langle \mathtt{2},\xi \rangle \Downarrow \langle 2,\xi\rangle$} \AxiomC{} \UnaryInfC{$\langle \mathtt{3},\xi \rangle \Downarrow \langle 3,\xi\rangle$} \BinaryInfC{$\langle \mathtt{2*3},\xi \rangle \Downarrow \langle 6,\xi\rangle$} \UnaryInfC{$\langle \mathtt{(2*3)},\xi \rangle \Downarrow \langle 6,\xi\rangle$} \end{prooftree}\]

Solution: Annotate Rules

Fill in the Proof

Reduce the expression \(\mathtt{(2*3) * 4}\) to its integer value.

\[\begin{prooftree} \AxiomC{} \UnaryInfC{$\langle \mathtt{(2*3) * 4},\xi \rangle \Downarrow \langle 24,\xi\rangle$} \end{prooftree}\]

Solution: Evaluate Expression

Pattern Matching

Pattern matching works with assignments:

(result: List[Int]) =>
  val x1 :: x2 :: x3 :: _ = result
  x1 <= x2 && x2 <= x3

Explicit match and case pattern matching is more commonplace:

(result: List[Int]) => result match
  case x1 :: x2 :: x3 :: _ =>
    x1 <= x2 && x2 <= x3

In a pattern matching expression, the first matching case is evaluated:

def f (xs: List[Int]) = xs match
  case _ => "Any list"
  case x :: Nil => s"One element $x$
end f
  
f(List(1, 2, 3)) // "Any list"
f(List(1))       // "Any list"! The first case matches.

Solutions

Solution: Semantics of Multiplication

\(\frac{\langle \mathtt{e}_1,\xi \rangle \Downarrow \langle v_1,\xi \rangle ~~ \langle \mathtt{e}_2,\xi \rangle \Downarrow \langle v_2,\xi \rangle}{\langle \mathtt{e}_1 \mathtt{*} \mathtt{e}_2,\xi\rangle \Downarrow \langle v_1 \cdot v_2,\xi\rangle}\text{\tiny(Mul)}\)

Solution: Annotate Rules

\[\begin{prooftree} \AxiomC{} \RL{(Num)} \UnaryInfC{$\langle \mathtt{2},\xi \rangle \Downarrow \langle 2,\xi\rangle$} \AxiomC{} \RL{(Num)} \UnaryInfC{$\langle \mathtt{3},\xi \rangle \Downarrow \langle 3,\xi\rangle$} \RL{(Mul)} \BinaryInfC{$\langle \mathtt{2*3},\xi \rangle \Downarrow \langle 6,\xi\rangle$} \RL{(Par)} \UnaryInfC{$\langle \mathtt{(2*3)},\xi \rangle \Downarrow \langle 6,\xi\rangle$} \end{prooftree}\]

Solution: Evaluate Expression

\[\begin{prooftree} \AXC{By $\mathtt{(2*3)}$ from above } \UIC{$\langle \mathtt{(2*3)},\xi \rangle \Downarrow \langle 6,\xi \rangle$} \AXC{} \RL{(Num)} \UIC{$\langle \mathtt{4},\xi \rangle \Downarrow \langle 4,\xi \rangle$} \RL{(Mul)} \BIC{$\langle \mathtt{(2*3)*4},\xi \rangle \Downarrow \langle 24,\xi \rangle$} \end{prooftree}\]