Meeting 27 - Procedural Abstraction

Bor-Yuh Evan Chang

Tuesday, December 3, 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 27 - Procedural Abstraction

What questions does your neighbor have?

Announcements

  • Remainder of the Semester
    • HW 5 and Lab 5 before Thanksgiving break
    • Unit 6 (probably one combined assignment) Lab 5 after Thanksgiving break
    • Exam 5-6 in the last week of classes before the Final
  • Come see us to make a study plan
    • e.g., via the redo policy
    • see the Final Exam as an opportunity to show growth from mid-semester exams.

Today

Questions?

  • Review:
    • What is the essence of imperative computation?

Procedures

What are procedures?

Assignment

\[ \begin{array}{rrrl} \text{expressions} & e& \mathrel{::=}& \cdots \mid x \mathrel{\texttt{=}}e_1 \end{array} \]

What if we applied substitution as before?

Static Memory

Without procedure call, dynamically-allocated memory addresses seems overkill. \[ \begin{array}{rrrl} \text{memories} & m& \mathrel{::=}& \cdot\mid m[x \mapsto v] \end{array} \]

Procedures: Syntax

\[ \begin{array}{rrrl} \text{types} & \tau& \mathrel{::=}& \texttt{number}\mid\texttt{(}x\texttt{:}\, \mathbf{var}\,\tau \texttt{)} \mathrel{\texttt{=}\!\texttt{>}} \tau' \\ \text{values} & v& \mathrel{::=}& n\mid\texttt{(}x\texttt{:}\, \mathbf{var}\,\tau \texttt{)} \mathrel{\texttt{=}\!\texttt{>}} e_1 \\ \text{expressions} & e& \mathrel{::=}& n\mid\texttt{(}x\texttt{:}\, \mathbf{var}\,\tau \texttt{)} \mathrel{\texttt{=}\!\texttt{>}} e_1 \mid x \mid e_1\texttt{(}e_2\texttt{)} \mid x \mathrel{\texttt{=}}e_1 \mid\mathord{\texttt{*}}a \end{array} \]

Figure 1: Syntax of TypeScripty with number literals, procedure literals, procedure calls, and mutable variable assignment.

Procedures: Semantics

Procedures: Implementation

defined trait Expr
defined class N
defined class Var
defined class Assign
defined class Deref
defined class A
defined function isValue
defined class Mem
defined object Mem
defined class DoWith
defined object DoWith
import DoWith._
defined function memalloc
defined function substitute
defined function step

Parameter-Passing Modes

Small changes in \(\TirName{DoCall}\).

Call-By-Name Parameters: Syntax

\[ \begin{array}{rrrl} \text{types} & \tau& \mathrel{::=}& \texttt{number}\mid\texttt{(}x\texttt{:}\, m\,\tau \texttt{)} \mathrel{\texttt{=}\!\texttt{>}} \tau' \\ \text{values} & v& \mathrel{::=}& n\mid\texttt{(}x\texttt{:}\, m\,\tau \texttt{)} \mathrel{\texttt{=}\!\texttt{>}} e_1 \\ \text{expressions} & e& \mathrel{::=}& n\mid\texttt{(}x\texttt{:}\, m\,\tau \texttt{)} \mathrel{\texttt{=}\!\texttt{>}} e_1 \mid x \mid e_1\texttt{(}e_2\texttt{)} \mid m\;x\;\texttt{=}\;e_1\texttt{;}\;e_2 \\ \text{parameter modes} & m& \mathrel{::=}& \mathbf{const}\mid\mathbf{name} \end{array} \]

Figure 3: Syntax of TypeScripty with number literals, function literals with parameter modes, and variable declarations, and function call expressions.

Call-By-Name Parameters: Semantics

Exotic Parameter-Passing Modes

Reference parameters (as in C++ and C#)?

Out parameters (as in C#)?

In-out parameters (as in Ada)?

Pointers

First-class addresses (i.e., when “addresses are values”).

Dynamically-Allocated Mutable Objects: Syntax

\[ \begin{array}{rrrl} \text{expressions} & e& \mathrel{::=}& n \mid\texttt{\{} \overline{ f\texttt{:}\,e } \texttt{\}} \mid e_1 \mathrel{\texttt{=}}e_2 \mid e_1\texttt{.}f \mid x \mid\mathbf{const}\;x\;\texttt{=}\;e_1\texttt{;}\;e_2 \\ \text{values} & v& \mathrel{::=}& n\mid a \\ \text{location values} & l& \mathrel{::=}& a\texttt{.}f \\ \text{addresses} & a \end{array} \]

Figure 5: Syntax of TypeScripty with number literals and dynamically-allocated mutable objects.

Dynamically-Allocated Mutable Objects: Semantics

Dynamically-Allocated Mutable Objects: Semantics

Aliasing

const a = { val: 1 };
const b = a;
b.val = 42;
console.log(a.val)