Environment in lab 4?

2 views
Skip to first unread message

Erik Levin

unread,
Mar 6, 2010, 9:29:15 AM3/6/10
to proglang-c...@googlegroups.com
Hi,

we're a bit confused about lab 4. In the slides for the lab 4 lecture it one one hand says "the environment i.e. mapping from function name to function defi nition" which sounds like it should be a map from identifier to expression. Later it mentions an environment of type [(String,Value)]. So should the environment be from identifier to value or expression?

/ Erik Levin

Krasimir Angelov

unread,
Mar 6, 2010, 10:05:44 AM3/6/10
to proglang-c...@googlegroups.com
There is no contradiction if you have closures:

data Value = VInt Int | VClosure Env Expr

Erik Levin

unread,
Mar 6, 2010, 11:29:40 AM3/6/10
to proglang-c...@googlegroups.com
Ok, so a list of maps from identifier to value?

Also, what is the environment inside closures used for, and what should it contain? In the rule

Γ ⊢ fun  Clos (\x -> body)        Γ ⊢ arg  val             Δ, x := val  body  result

for instance, why isn't it possible to just use Γ in the final rule as well (with x set to val)?

/ Erik


2010/3/6 Krasimir Angelov <kr.an...@gmail.com>

Krasimir Angelov

unread,
Mar 7, 2010, 2:54:26 PM3/7/10
to proglang-c...@googlegroups.com
Because Delta is the environment at the moment when the lambda binding
was closed in the closure. Delta is not necessary the same as Gamma.
For example if you have to evaluate the following expression:

(\x,y -> x+y) 1 2

and the current environment is Gamma then you first have to compute
the left subexpression which is:

(\x,y -> x+y) 1

This is application where function with two arguments is applied to
only one value. The result will be the closure:

Clos (Gamma, x := 1) (\y -> x+y)

The new environment in the closure is our Delta in the evaluation
rule. It has a new binding for x i.e. x := 1 on top of the old
environment Gamma. Now to compute the top-level expression you have to
add the binding for y as well and you will get:

Δ, x := 1, y := 2 ⊢ x+y ⇓ result

If you just use the original environment Gamma you will miss the binding for x.


Krasimir


On Sat, Mar 6, 2010 at 5:29 PM, Erik Levin

Reply all
Reply to author
Forward
0 new messages