CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

CS450

Structure of Higher Level Languages

Lecture 16: Challenges specifying Racket's define

Tiago Cogumbreiro

1 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

How do we add support for definitions?

2 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

How do we add support for definitions?

  • We extend our language (λE) with define
  • We introduce the AST
  • We discuss parsing our language
3 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Understanding definitions

Syntax

$${ \color{purple} t ::= e \mid t;t \mid (\mathtt{define}\ x\ e) } \\[1em] e ::= v \mid x \mid (e_1\ e_2) \mid \lambda x.t \qquad v ::= n \mid \{E,\lambda x.t\} \mid {\color{purple} \mathtt{void}} $$
  • New grammar rule: terms
  • A program is now a non-empty sequence of terms
  • Since we are describing the abstract syntax, there is no distinction between a basic and a function definition
  • Since evaluating a definition returns a void, we need to update values
4 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Values

We add void to values.

$$v ::= n \mid \{E,\lambda x.t\} \mid \mathtt{void} $$

Racket implementation

;; Values
(define (f:value? v) (or (f:number? v) (f:closure? v) (f:void? v)))
(struct f:number (value))
(struct f:closure (env decl))
(struct f:void ())
5 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Expressions

Expressions remain unchanged.

$$e ::= v \mid x \mid (e_1\ e_2) \mid \lambda x.t $$

Racket implementation

(define (f:expression? e) (or (f:value? e) (f:variable? e) (f:apply? e) (f:lambda? e)))
(struct f:variable (name))
(struct f:apply (func args))
(struct f:lambda (params body))
6 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Terms

We implement terms below.

$$t ::= e \mid t;t \mid (\mathtt{define}\ x\ e) $$

Racket implementation

(define (f:term? t) (or (f:expression? t) (f:seq? t) (f:define? t)))
(struct f:seq (fst snd))
(struct f:define (var body))

The body of a function declaration is a single term

The body is no longer a list of terms!

A sequence is not present in the concrete syntax, but it simplifies the implementation and formalism (see reduction)

7 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

$\lambda_F$ semantics

The incorrect way of implementing define

8 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

$\lambda_F$ semantics

The incorrect way of implementing define

Semantics $t \Downarrow_E \langle E, v \rangle$

$$\frac{ e \Downarrow_E v }{ e \Downarrow_E \langle E, v \rangle } \quad \texttt{(E-exp)} \\[2em] \frac{ e \Downarrow_E v }{ (\mathtt{define}\ x\ e) \Downarrow_{E} \langle E{\color{green}[x \mapsto v]}, \mathtt{void} \rangle } \quad \texttt{(E-def)} \\[2em] \frac{ t_1 \Downarrow_{\color{blue}E_1} \langle {\color{purple}E_2}, v_1 \rangle \qquad t_2 \Downarrow_{\color{purple}E_2} \langle {\color{red}E_3}, v_2 \rangle }{ t_1; t_2 \Downarrow_{\color{blue}E_1} \langle {\color{red}E_3}, v_2 \rangle } \quad \texttt{(E-seq)} $$
  • Evaluating a define extends the environment with a new binding
  • Sequencing must thread the environments
9 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Implementing defines with environments

$$v \Downarrow_{E} v \qquad (\texttt{E-val}) \\[.7em] x \Downarrow_{E} {E}(x) \qquad (\texttt{E-var}) $$ $$\lambda x.t \Downarrow_{E} ({E},\lambda x.t) \qquad (\texttt{E-lam}) \\[.7em] \frac{ e_f \Downarrow_{E} ({E_b},\lambda x. t_b) \qquad e_a \Downarrow_{E} v_a \qquad t_b \Downarrow_{\mathbf{E_b[x\mapsto v_a]}} (E_c, v_b) }{ (e_f\ e_a) \Downarrow_{E} v_b } \qquad \texttt{(E-app)} \\[.7em] \frac{ e \Downarrow_E v }{ e \Downarrow_E (E, v) } \quad \texttt{(E-exp)} \\[1em] \frac{ e \Downarrow_E v }{ (\mathtt{define}\ x\ e) \Downarrow_{E} (E{[x \mapsto v]}, \mathtt{void}) } \quad \texttt{(E-def)} \\[1em] \frac{ t_1 \Downarrow_{E_1} ({E_2}, v_1) \qquad t_2 \Downarrow_{E_2} ({E_3}, v_2) }{ t_1; t_2 \Downarrow_{E_1} ({E_3}, v_2) } \quad \texttt{(E-seq)} $$
10 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Why is $\lambda_F$ incorrect?

11 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Evaluating define

Example 1

Consider the following program

(define a 20)
(define b (lambda (y) a))
(b 1)

What is the output of this program?

12 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Evaluating define

Example 1

Consider the following program

(define a 20)
(define b (lambda (y) a))
(b 1)

What is the output of this program? The output is: 20

Let us try to evaluate this program with our $\lambda_F$ semantics!

12 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 1

Input

Environment: []
Term: (define a 20)
13 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 1

Input

Environment: []
Term: (define a 20)

Output

Environment: [ `(a . 20)` ]
Value: `void`

Evaluating

13 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 1

Input

Environment: []
Term: (define a 20)

Output

Environment: [ `(a . 20)` ]
Value: `void`

Evaluating

$$\frac{ 20 \Downarrow_{\{\}} 20 \quad \texttt{(E-val)} }{ (\mathtt{define}\ a\ 20) \Downarrow_{\{\}} ({\color{purple}\{a:20\}}, \mathtt{void}) } \ \texttt{E-def} $$
13 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 2

Input

Environment: [ (a . 20) ]
Term: (define b (lambda (y) a))
14 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 2

Input

Environment: [ (a . 20) ]
Term: (define b (lambda (y) a))

Output

Environment: [
  (a . 20)
  (b . (closure `[(a . 20)]` (lambda (y) a)))
]
Value: void

14 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 2

Input

Environment: [ (a . 20) ]
Term: (define b (lambda (y) a))

Output

Environment: [
  (a . 20)
  (b . (closure `[(a . 20)]` (lambda (y) a)))
]
Value: void

Evaluating

$$\frac{ \lambda y.a \Downarrow_{\{a:20\}} (\{a:20\}, \lambda y.a) \quad \texttt{(E-lam)} }{ (\mathtt{define}\ b\ {\lambda y.a}) \Downarrow_{\{a:20\}} ({\{a:20,{\color{purple} b:(\{a:20\}, \lambda y.a)}\}}, \mathtt{void}) } \ \texttt{E-def} $$
14 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 3

Input

Environment: [
  (a . 20)
  (b . (closure [(a . 20)] (lambda (y) a)))
]
Term: (b 1)
15 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 3

Input

Environment: [
  (a . 20)
  (b . (closure [(a . 20)] (lambda (y) a)))
]
Term: (b 1)

Output

Environment: [
  (a . 20)
  (b . (closure [(a . 20)] (lambda (y) a)))
]
Value: 20

Evaluation

15 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 1: step 3

Input

Environment: [
  (a . 20)
  (b . (closure [(a . 20)] (lambda (y) a)))
]
Term: (b 1)

Output

Environment: [
  (a . 20)
  (b . (closure [(a . 20)] (lambda (y) a)))
]
Value: 20

Evaluation

$$\small \cfrac{ \cfrac{ \qquad \cfrac{ {\color{blue}E}(b) = (\{a:20\}, \lambda y.a) }{ b \Downarrow_{\color{blue}E} (\{a:20\}, \lambda y.a) } \texttt{E-var} \qquad \cfrac{}{ 1 \Downarrow_{\color{blue}E} 1 } \texttt{E-val} \quad \cfrac{ {\color{purple}F}(a) = 20 }{ a \Downarrow_{{\color{purple}F}} ({\color{purple}F}, 20) } \texttt{E-var,E-exp} }{ (b\ 1) \Downarrow_{\color{blue}E} 20 } \texttt{E-app} }{ (b\ 1) \Downarrow_{\color{blue}E} ({\color{blue}E}, 20) } \texttt{E-exp} $$

where ${\color{blue}E}= {\{a:20,b:(\{a:20\}, \lambda y.a)\}}$
${\color{purple}F} = {\{a:20\}}[y \mapsto 1] = \{a:20, {\color{red}y:1}\}$

15 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Evaluating define

Example 2

16 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Evaluating define

Example 2

Consider the following program

(define b (lambda (y) a))
(define a 20)
(b 1)

What is the output of this program?

17 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Evaluating define

Example 2

Consider the following program

(define b (lambda (y) a))
(define a 20)
(b 1)

What is the output of this program? The output is: 20

Let us try to evaluate this program with our $\lambda_F$ semantics!

17 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 1

Input

Environment: []
Term: (define b (lambda (y) a))
18 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 1

Input

Environment: []
Term: (define b (lambda (y) a))

Output

Environment: [
  (b . (closure `[]` (lambda (y) a)))
]
Value: void

Evaluation

18 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 1

Input

Environment: []
Term: (define b (lambda (y) a))

Output

Environment: [
  (b . (closure `[]` (lambda (y) a)))
]
Value: void

Evaluation

$$\frac{ \lambda y.a \Downarrow_{\{\}} (\{\}, \lambda y.a) \quad \texttt{(E-lam)} }{ (\mathtt{define}\ b\ {\lambda y.a}) \Downarrow_{\{\}} ({\color{purple}\{b:(\{\}, \lambda y.a)\}}, \mathtt{void}) } \ \texttt{E-def} $$
18 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 2

Input

Environment: [
  (b . (closure [] (lambda (y) a)))
]
Term: (define a 20)
19 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 2

Input

Environment: [
  (b . (closure [] (lambda (y) a)))
]
Term: (define a 20)

Output

Environment: [
  `(a . 20)`
  (b . (closure [] (lambda (y) a)))
]
Value: void

Evaluation

19 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 2

Input

Environment: [
  (b . (closure [] (lambda (y) a)))
]
Term: (define a 20)

Output

Environment: [
  `(a . 20)`
  (b . (closure [] (lambda (y) a)))
]
Value: void

Evaluation

$$\frac{ 20 \Downarrow_{{\{b:(\{\}, \lambda y.a)\}}} 20 \quad \texttt{(E-val)} }{ (\mathtt{define}\ a\ 20) \Downarrow_{{\{b:(\{\}, \lambda y.a)\}}} (\{b:(\{\}, \lambda y.a),{\color{purple}a:20}\}, \mathtt{void}) } \ \texttt{E-def} $$
19 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 3

Input

Environment: [
  (a . 20)
  (b . (closure [] (lambda (y) a)))
]
Term: (b 1)
20 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 3

Input

Environment: [
  (a . 20)
  (b . (closure [] (lambda (y) a)))
]
Term: (b 1)

Output

Environment: [
  (a . 20)
  (b . (closure `[]` (lambda (y) a)))
]
Value: `error! a is undefined`

Insight

When creating a closure we copied the existing environment, and therefore any future updates are forgotten.

The semantics of $\lambda_F$ is not enough! We need to introduce a notion of mutation.

20 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

CS450

Structure of Higher Level Languages

Lecture 16: Challenges specifying Racket's define

Tiago Cogumbreiro

21 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Introducing $\lambda_D$

22 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Language $\lambda_D$: Terms

We highlight in $\color{red}\text{red}$ an operation that produces a side-effect: mutating an environment.

$$\frac{ e \Downarrow_{E} v \qquad {\color{red} E \leftarrow [ x := v]} }{ (\mathtt{define}\ x\ e) \Downarrow_{E} \mathtt{void} } \ \texttt{(E-def)} \\[2em] \frac{ t_1 \Downarrow_{E} v_1 \qquad t_2 \Downarrow_{E} v_2 }{ t_1; t_2 \Downarrow_{E} v_2 } \ \texttt{(E-seq)} $$
23 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Language $\lambda_D$: Expressions

Because we have side-effects, the order in which we evaluate each sub-expression is important.

$$v \Downarrow_{E} v \qquad (\texttt{E-val}) $$ $$x \Downarrow_{E} {E}(x) \qquad (\texttt{E-var}) $$ $$\lambda x.t \Downarrow_{E} ({E},\lambda x.t) \qquad (\texttt{E-lam}) $$ $$\frac{ e_f \Downarrow_{E} (E_f,\lambda x. t_b) \qquad e_a \Downarrow_{E} v_a \qquad {\color{red} E_b \leftarrow E_f + [x := v_a] } \qquad t_b \Downarrow_{E_b} v_b }{ (e_f\ e_a) \Downarrow_E v_b } \ \texttt{(E-app)} $$

Can you explain why the order is important?

24 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Language $\lambda_D$: Expressions

Because we have side-effects, the order in which we evaluate each sub-expression is important.

$$v \Downarrow_{E} v \qquad (\texttt{E-val}) $$ $$x \Downarrow_{E} {E}(x) \qquad (\texttt{E-var}) $$ $$\lambda x.t \Downarrow_{E} ({E},\lambda x.t) \qquad (\texttt{E-lam}) $$ $$\frac{ e_f \Downarrow_{E} (E_f,\lambda x. t_b) \qquad e_a \Downarrow_{E} v_a \qquad {\color{red} E_b \leftarrow E_f + [x := v_a] } \qquad t_b \Downarrow_{E_b} v_b }{ (e_f\ e_a) \Downarrow_E v_b } \ \texttt{(E-app)} $$

Can you explain why the order is important? Otherwise, we might evaluate the body of the function $t_b$ without observing the assignment $x := v_a$ in $E_b$.

24 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Mutable operations on environments

25 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Mutable operations on environments

Put

$E \leftarrow [ x := v]$
Take a reference to an environment $E$ and mutate its contents, by adding a new binding.

Push

$E \leftarrow E' + [x := v]$

Create a new environment referenced by $E$ which copies the elements of $E'$ and also adds a new binding.

26 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Making side-effects explicit

27 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Mutation as a side-effect

Let us use a triangle $\blacktriangleright$ to represent the order of side-effects.

$$\frac{ % \blacktriangleright e \Downarrow_{E} v \quad\blacktriangleright\quad {\color{red} E \leftarrow [ x := v]} }{ % \blacktriangleright (\mathtt{define}\ x\ e) \Downarrow_{E} \mathtt{void} } \texttt{(E-def)} \\[2em] \frac{ % \blacktriangleright t_1 \Downarrow_{E} v_1 \quad\blacktriangleright\quad t_2 \Downarrow_{E} v_2 }{ % \blacktriangleright t_1; t_2 \Downarrow_{E} v_2 } \texttt{(E-seq)} \\[2em] \frac{ % \blacktriangleright e_f \Downarrow_{E} (E_f,\lambda x. t_b) \ \blacktriangleright \ e_a \Downarrow_{E} v_a \ \blacktriangleright \ {\color{red} E_b \leftarrow E_f + [x := v_a] } \ \blacktriangleright \ t_b \Downarrow_{E_b} v_b }{ %\blacktriangleright (e_f\ e_a) \Downarrow_E v_b } \texttt{(E-app)} $$
28 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Implementing side-effect mutation

Making the heap explicit

We can annotate each triangle with a heap, to make explicit how the global heap should be passed from one operation to the next. In this example, defining a variable takes an input global heap $H$ and produces an output global heap $H_2$.

$$\frac{ \blacktriangleright_H \quad e \Downarrow_{E} v \quad \blacktriangleright_{H_1} \quad {\color{red} E \leftarrow [ x := v]} \quad \blacktriangleright_{H_2} }{ \blacktriangleright_H\ (\mathtt{define}\ x\ e) \Downarrow_{E} \mathtt{void} \blacktriangleright_{H_2} } \ \texttt{(E-def)} $$
29 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Let us use our rule sheet!

$$\frac{ % \blacktriangleright e \Downarrow_{E} v \quad\blacktriangleright\quad {E \leftarrow [ x := v]} }{ % \blacktriangleright (\mathtt{define}\ x\ e) \Downarrow_{E} \mathtt{void} } \texttt{(E-def)} \\[2em] \frac{ % \blacktriangleright t_1 \Downarrow_{E} v_1 \quad\blacktriangleright\quad t_2 \Downarrow_{E} v_2 }{ % \blacktriangleright t_1; t_2 \Downarrow_{E} v_2 } \texttt{(E-seq)} \\[2em] \frac{ % \blacktriangleright e_f \Downarrow_{E} (E_f,\lambda x. t_b) \ \ \ \blacktriangleright \ \ \ e_a \Downarrow_{E} v_a \ \ \ \blacktriangleright \ \ \ { E_b \leftarrow E_f + [x := v_a] } \ \ \ \blacktriangleright \ \ \ t_b \Downarrow_{E_b} v_b }{ %\blacktriangleright (e_f\ e_a) \Downarrow_E v_b } \texttt{(E-app)} $$ $$v \Downarrow_{E} v \qquad (\texttt{E-val}) $$ $$x \Downarrow_{E} {E}(x) \qquad (\texttt{E-var}) $$ $$\lambda x.t \Downarrow_{E} ({E},\lambda x.t) \qquad (\texttt{E-lam}) $$
30 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Examples

31 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Evaluating Example 2

(define b (lambda (y) a))
(define a 20)
(b 1)

Input

E0: []
---
Env: E0
Term: (define b (lambda (y) a))
32 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Evaluating Example 2

(define b (lambda (y) a))
(define a 20)
(b 1)

Input

E0: []
---
Env: E0
Term: (define b (lambda (y) a))

Output

E0: [
  (b . (closure `E0` (lambda (y) a)))
]
Value: void

$$\frac{ % \blacktriangleright \cfrac{ }{ \lambda y.a \Downarrow_{E_0} ({E_0},\lambda y.a) } \quad\blacktriangleright\quad \cfrac{}{\color{red} E_0 \leftarrow [ b := ({E_0},\lambda y.a)]} }{ % \blacktriangleright (\mathtt{define}\ b\ \lambda y.a) \Downarrow_{E_0} \mathtt{void} } $$
32 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 2

Input

E0: [
  (b . (closure E0 (lambda (y) a)))
]
---
Env: E0
Term: (define a 20)
33 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 2

Input

E0: [
  (b . (closure E0 (lambda (y) a)))
]
---
Env: E0
Term: (define a 20)

Output

E0: [
  `(a . 20)`
  (b . (closure E0 (lambda (y) a)))
]
Value: void

$$\frac{ % \blacktriangleright \cfrac{ }{ 20 \Downarrow_{E_0} 20 } \quad\blacktriangleright\quad \cfrac{}{\color{red} E_0 \leftarrow [ a := 20]} }{ % \blacktriangleright (\mathtt{define}\ a\ 20) \Downarrow_{E_0} \mathtt{void} } $$
33 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 3

Input

E0: [
  (a . 20)
  (b . (closure E0 (lambda (y) a)))
]
---
Env: E0
Term: (b 1)
34 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 2: step 3

Input

E0: [
  (a . 20)
  (b . (closure E0 (lambda (y) a)))
]
---
Env: E0
Term: (b 1)

Output

E0: [
  (a . 20)
  (b . (closure E0 (lambda (y) a)))
]
*E1: [ E0
* (y . 1)
*]
*Value: 20

$$\frac{ % \blacktriangleright b \Downarrow_{E_0} (E_0,\lambda y. a) \ \blacktriangleright \ 1 \Downarrow_{E_0} 1 \ \blacktriangleright \ {\color{red} E_1 \leftarrow E_0 + [y := 1] } \ \blacktriangleright \ a \Downarrow_{E_1} 20 }{ %\blacktriangleright (b\ 1) \Downarrow_{E_0} 20 } $$
34 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 3

*(define (f x) (lambda (y) x))
(f 10)

Input

E0: []
---
Env: E0
Term: (define (f x) (lambda (y) x))
35 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 3

*(define (f x) (lambda (y) x))
(f 10)

Input

E0: []
---
Env: E0
Term: (define (f x) (lambda (y) x))

Output

E0: [
  (f . (closure E0
          (lambda (x) (lambda (y) x))))
]
Value: void
35 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 3

*(define (f x) (lambda (y) x))
(f 10)

Input

E0: []
---
Env: E0
Term: (define (f x) (lambda (y) x))

Output

E0: [
  (f . (closure E0
          (lambda (x) (lambda (y) x))))
]
Value: void

$$\frac{ % \blacktriangleright \lambda x.\lambda y.x \Downarrow_{E_0} (E_0, \lambda x.\lambda y.x) \qquad \phantom{ \blacktriangleright\quad \color{red} E_0 \leftarrow [ f := (E_0, \lambda x.\lambda y.x)] } }{ %\blacktriangleright (\texttt{define}\ f\ \lambda x.\lambda y.x) \Downarrow_{E_0} \texttt{void} } $$
35 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 3

(define (f x) (lambda (y) x))
(f 10)

Input

E0: []
---
Env: E0
Term: (define (f x) (lambda (y) x))

Output

E0: [
  (f . (closure E0
          (lambda (x) (lambda (y) x))))
]
Value: void

$$\frac{ % \blacktriangleright \lambda x.\lambda y.x \Downarrow_{E_0} (E_0, \lambda x.\lambda y.x) \quad\blacktriangleright\quad \color{red} E_0 \leftarrow [ f := (E_0, \lambda x.\lambda y.x)] }{ %\blacktriangleright (\texttt{define}\ f\ \lambda x.\lambda y.x) \Downarrow_{E_0} \texttt{void} } $$
36 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 3

Input

E0: [
  (f . (closure E0
          (lambda (x) (lambda (y) x))))
]
---
Env: E0
Term: (f 10)
37 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 3

Input

E0: [
  (f . (closure E0
          (lambda (x) (lambda (y) x))))
]
---
Env: E0
Term: (f 10)

Output

E0: [
  (f . (closure E0
          (lambda (x) (lambda (y) x))))
]
E1: [ E0 (x . 10) ]
Value: (closure E1 (lambda (y) x))
37 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Example 3

Input

E0: [
  (f . (closure E0
          (lambda (x) (lambda (y) x))))
]
---
Env: E0
Term: (f 10)

Output

E0: [
  (f . (closure E0
          (lambda (x) (lambda (y) x))))
]
E1: [ E0 (x . 10) ]
Value: (closure E1 (lambda (y) x))

$$\frac{ % \blacktriangleright \cfrac{ E_0(f) = (E_0, {\color{blue}\lambda x}.{\color{green}\lambda y.x}) }{ f \Downarrow_{E_0} (E_0, {\color{blue}\lambda x}.{\color{green}\lambda y.x}) } \quad \cfrac{ }{ 10 \Downarrow_{E_0} 10 } \quad \cfrac{}{ E_1 \leftarrow E_0 + [{\color{blue}x} := 10] } \quad \cfrac{ }{ {\color{green}\lambda y.x} \Downarrow_{E_1} \color{purple}(E_1, \lambda y.x) } }{ %\blacktriangleright (f\ 10) \Downarrow_{E_0} \color{purple}(E_1, \lambda y.x) } $$
37 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

How to implement mutation without mutable constructs?

38 / 39

CS450  ☽  Challenges specifying Racket's define  ☽  Lecture 16  ☽  Tiago Cogumbreiro

Motivating example

  • Calling function b must somehow access variable a which is defined after its creation.
; Env: `[]`
(define b (lambda (x) a))
; Env: `[(b . (closure ?? (lambda (x) a)))]`
(define a 20)
; Env: `[(b . (closure ?? (lambda (x) a))) (a . 20)]`
(b 1)
39 / 39
Rotate for a larger view