What is the functional computational model? It was the computational model you learned first, right?
What is referential transparency?
Your Questions?
Value Bindings
res0_0: Int = 7res0_1: Int = 10
Value Environments
How do we evaluate an expression with variable uses?
Value Environments
We need a way of capturing what the variable names in scope are bound to — a value environment.
Value Environments
Substitution
How do we evaluate an expression with variable uses?
Value Environments: Take-Home Points
We know how to introduce bindings from variable names to values in Scala (i.e., val)
A value environment is a map from variable names to values that stores the bindings.
In order to evaluate an expression containing variable uses, we “apply” a value environment using substitution.
Conceptually, evaluating a sequence of val declarations yields a value environment.
Scoping
Shadowing
How do we read this?
a: Int = 1b: Int = 2c: Int = 6
Shadowing
Let’s pair up and find the binding positions for every variable use in the program below. What is the final environment? Can you rename variable bindings and uses consistently to eliminate the shadowing?
a: Int = 2c: Int = 54d: Int = 324
Shadow-Respecting Substitution
What if we “naively” applied substitution of env1: \([\mathtt{a} \mapsto 2]\) to the rest of the expression?
a: Int = 2c: Int = 54d: Int = 324
Free versus Bound Variables
:
Compilation Failed
Free versus Bound Variables
A closed expression is one that has no free variables:
res10: Int = 7
Functions
Closures
An expression defining a function can refer to variables bound in an outer scope: