Closure

2 views
Skip to first unread message
Message has been deleted

llarsson

unread,
Mar 11, 2010, 12:31:51 PM3/11/10
to proglang-course-2010
Since I am a bit stubbern I would like to know how the closure should
look in haskell or in java/pseudocode

my first idea was (not considering the application stack)

type Var = (Env, Exp)
type Env = [(Ident, Var)]

However this is not valid haskell so one needs to make a data type
instead that can be viewed as above but in either case one has a
chicken & the egg problem. If one wants to add an element/variable to
the enviroment one wants the Env pointer of Var to point to the
resulting Env. Only way I can come up with is very ugly so I think
there is a fundamental error.

Arnar Birgisson

unread,
Mar 11, 2010, 12:39:55 PM3/11/10
to proglang-c...@googlegroups.com
A closure is a pair of an expression (possibly with free variables)
and an environment, mapping variables to values (or other closures).

In Haskell you could use something like

type Closure = (Exp, Env)
type Env = Ident -> Value -- or [(Ident, Value)] if you want to use lookup

The "tricky" part is that Values need (for call-by-need) to be either
concrete values *or* closures.

data Value = V ConcreteValue | C Closure
data ConcreteValue = IntV Integer | BoolV Bool | ...

This is one possible way of structuring things.

hth,
Arnar

llarsson

unread,
Mar 11, 2010, 1:13:55 PM3/11/10
to proglang-course-2010
One could as well use one of the basic primitives for expression like
EInt or some other of the basic values for expressions. The problem is
the chicken & egg problem that when you add something example chuch0
the enviroment it points to should be the result of the addition
otherwise recusion would be imposible.


a = \x -> \y -> x+y

Example of adding a to the enviroment
Env = [(a, A]

Closure for:
A = ((\x -> \y -> x+y), [])
|
----------------------------|
|
Env for it to be evaluated should of course contain itself otherwise
recusion would be impossible but this creates a chicken & egg problem

llarsson

unread,
Mar 12, 2010, 5:49:58 AM3/12/10
to proglang-course-2010
I realized how to solve that, I add a function call with same
parameters in the Env thus it solves the chicken & egg problem with
lazy evaluation
Reply all
Reply to author
Forward
0 new messages