1
Vote

Issue with environment updating.

description

For some reason, when using parameters which would hide parameters of a higher scope, malformed expression errors occur.
 
For example, take this definition of fold:
 
(define fold (lambda (f a xs) (if (not? xs) a (fold f (f (car xs) a) (cdr xs)))))
 
Now, let's define map using fold:
 
(define map (lambda (f xs) (reverse (fold (lambda (x a) (cons (f x) a)) nil xs))))
 
Then when using map like so:
 
(map (lambda (x) (* x x)) '(1 2 3)))
 
A malformed expression error is raised by the eval function (the one defined in F#, not FScheme).
 
Interestingly enough, if we change the parameter "f" in map to anything else, this error doesn't occur.

comments

Steellworks wrote Jan 27, 2012 at 5:06 AM

I actually found the bug: the issue is that the environment created for the execution of a closure body contains the environment frames of the calling environment before the frames of the definition environment.

Simply changing

let env'' = List.rev acc |> extend (env' @ env)

to

let env'' = List.rev acc |> extend (env @ env')

fixes this bug.

wrote Feb 1, 2013 at 1:01 AM

MonkeyGuru wrote Nov 18, 2014 at 10:53 PM

Wow, thanks!
Very sorry that I haven't been looking at CodePlex (in years!)
I'll integrate the fix in the GitHub repo (https://github.com/AshleyF/FScheme)