We extend the language from Sect. 4.2 with functional values. These
require lexical closures, so we assume symbol tables are fully persistent. We add the
following productions to Grammar 4.1:
TypeId → fun id
Exp → Exp Exp
Exp → fn id => Exp
The notation is taken from Standard ML.
Evaluating fn x => e in an environment vtable produces a functional value f .
When f is applied to an argument v, it is checked that v is an integer. If this is the
case, e is evaluated in vtable extended with a binding that binds x to v. We then
check if the result w of this evaluation is an integer, and if so use it as the result of
the function application.
When evaluating e1 e2, we evaluate e1 to a functional value f and e2 to an integer
v and then apply f to v as described above.
Extend the interpreter from Fig. 4.3 to handle these new constructions as described above. Represent a lexical closures as a pair of (the abstract syntax of) an
expression and an environment.