How to draw

Example:

function d_map(fun, xs) {
	if(!is_null(xs)) {
		const h = head(xs);
		set_head(xs, fun(h));
		d_map(fun, tail(xs));
	}
}
const L = list(5);
let R = d_map(x => y => x+y, L);
R;

Step 1

Start with the Program Environment

  • The arrow upwards indicates it is enclosed by the global environment

    Environment Model 2024-10-16 05.18.11.excalidraw

    ⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

    Excalidraw Data

    Text Elements

    Program env

    Link to original

Step 2

Write down all declarations in the current frame

  • function declarations function xxx()
  • const and let declarations
    • use := to denote a const declaration
    • use : to denote a let (variable) declaration

      Environment Model 2024-10-16 05.18.12.excalidraw

      ⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

      Excalidraw Data

      Text Elements

      Program env

      d_map :=

      L :=

      R :

      Link to original

Step 3

Evaluate the program at the current level/scope Add to the diagram based on How to draw In this example:

  1. Declare function d_map
  2. Evaluate value of L
  3. Evaluate value of R

    Environment Model 2024-10-16 05.18.15.excalidraw

    ⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

    Excalidraw Data

    Text Elements

    Program env

    d_map :=

    L :=

    R :

    params: (fun, xs) body: { if (!is_null…

    5

    Link to original

Draw call to d_map (i.e. evaluate arguments)

let R = d_map(x => y => x+y, L);

Environment Model 2024-10-16 05.18.16.excalidraw

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

Excalidraw Data

Text Elements

Program env

d_map :=

L :=

R :

params: (fun, xs) body: { if (!is_null…

5

fun :

xs :

d_map

h := 5

Block

params: (x) body: { return y x+y; }

Link to original

Draw execution of of d_map

set_head(xs, fun(h)); // on this line, we will need to draw the call to fun(h), and change the head of xs

Environment Model 2024-10-16 05.18.17.excalidraw

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

Excalidraw Data

Text Elements

Program env

d_map :=

L :=

R :

params: (fun, xs) body: { if (!is_null…

fun :

xs :

d_map

h := 5

Block

params: (x) body: { return y x+y; }

h : 5

fun

params: (y) body: { return x+y; }

Link to original

d_map(fun, tail(xs)); // tail is a primitive function, so no frames are created
// since tail(xs) is null, the if statement causes there to be no declarations in the body of d_map, thus no need for a body environment
// finally fill in the return value into R

Environment Model 2024-10-16 05.18.18.excalidraw

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

Excalidraw Data

Text Elements

Program env

d_map :=

L :=

R :

params: (fun, xs) body: { if (!is_null…

fun :

xs :

d_map

h := 5

Block

params: (x) body: { return y x+y; }

h : 5

fun

params: (y) body: { return x+y; }

fun :

xs : null

d_map

Link to original

How to Draw

Note: the drawings here are just examples

General Rules

  • When accessing a name (constant, variable or function), start at the current frame, and keep searching enclosing frames until the name is found
    • If the search reaches the global environment without finding the name, an error will be thrown

Function Declaration

Draw two circles

  • Left circle points to text describing the function: params and body
  • Right circle points to the environment the function was declared (and thus the environment that will be extended when the function is called)
  • Any names that refer to the function will point to the two circles

    Environment Model 2024-10-16 05.18.13.excalidraw

    ⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

    Excalidraw Data

    Text Elements

    Program env

    d_map :=

    L :=

    R :

    params: (fun, xs) body: { if (!is_null…

    Link to original

Pair Structure

Draw an arrow pointing to the pair structure. The pair structure must be outside the frame box

Environment Model 2024-10-16 05.18.14.excalidraw

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

Excalidraw Data

Text Elements

Program env

d_map :=

L :=

R :

5

6

Link to original

Function Call

  1. If the function has parameters, extend the environment that the function was declared in (the frame that the arrow from the right circle points to)
    • The contents of this frame will be the function parameters
    • Label this frame with the name of the function (d_map), or the lambda expression (x => x+1)
    • The parameters will be taken as variables, so use :
    • Repeat Step 2 to fill this in
    • If the function has no parameters, do not draw this frame
  2. If the function has declarations in its body (const, let or function), then extend either the environment created in 1., or the environment that the function was declared in

Anonymous Lambda Declaration (Lambda With no Name)

Same as Function Declaration, but without the arrow pointing to it

  • If the lambda B is declared inside lambda A, the environment that lambda B points to will be the environment of lambda A (either the environment containing lambda A’s parameters, or lambda A’s body)
const hi = a => b => c => a + b + c;

Environment Model 2024-10-16 05.18.20.excalidraw

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

Excalidraw Data

Text Elements

Program env

hi :=

params: (a) body: { return b c a + b + c; }

a : 1

hi

params: (b) body: { return c a + b + c; }

b : 2

b c

params: (c) body: { return a + b + c; }

c : 3

c a +…

Link to original

Terms

Frame

A container of name bindings In a diagram: a single box that contains names and their values note: frames cannot be empty

Environment

A sequence of frames In a diagram: the whole chain of frames

Shadow

When a variable is declared in more than one frame in an environment, the other declarations are shadowed by the nearest declaration