Jedi Environment Diagrams

Introduction

·       Executing blocks and applying closures in Jedi x.0 (for x > 1) requires creating a local environment.

·       In the case of a block, the local environment extends the calling environment and will hold the bindings created by any local declarations.

val localEnv = new Environment(env)

·       In the case of applying a closure the local environment extends the defining environment of the closure and will hold the bindings of parameters to arguments.

val localEnv = new Environment(defEnv)

·       (Recall that to implement the static scope rule functions/closures defined by executing a lambda must retain the execution environment of the lambda.)

·       When lambdas and blocks are nested, this can create a complex network of hash tables called environment diagrams.

·       Drawing environment diagrams is a good way to make sure you understand how blocks and function calls work.

Sample Problems with Solutions

·       Samples

Sample Problems

Assume the following declarations have been made in Jedi:

-> def x = 10
ok
-> def y = 20
ok
-> def z = 100
ok

(Since these declarations are made at the Jedi prompt, the bindings they create will be stored in the global environment.)

In the following examples, draw and environment diagram and supply the result of executing the following Jedi expressions.

Block-Block

-> {def y = 2; x + y + {def x = 5; def z = 1; x + y + z}}
?

Block-Lambda

-> def foo = {def y = 2; lambda(z) x + y + z}
ok
-> foo(y)
?
-> foo(z / 2)
?

Lambda-Block

-> def foo = lambda(z) { def y = x + z; 2 * y }
OK
-> foo(y)
?
-> foo(z / 2)
?

Lambda-Lambda

-> def foo = lambda(z) lambda(x) (x + y + z)
ok
-> def bar = foo(z / 2)
ok
-> bar(x * 2)
?