Bugs: handling of closure variables (why I found out)

8 messages Options
Embed this post
Permalink
Erik Huelsmann

Bugs: handling of closure variables (why I found out)

Reply Threaded More More options
Print post
Permalink
The reason I found out about our issues in closure variables handling
is the fact that I tried to evaluate this code:


(funcall (compile nil
  '(lambda ()
       (flet ((%f (f a)
                 (funcall (lambda () a))
                 (when (a < 2)
                    (funcall #'%f #'%f (1+ a)))
                 a))
         (%f #'%f 1))))

Which should return 1, because the last statement of the first
(level-0 recursion) call ends by returning the 'a' value which is
passed to that call. The actual answer returned should be 2, if the
code compiles, because I wrote it off the top of my head.


I'll leave it as an excercise to the reader to explain why it returns
2, based on their analysis of the compiler and my earlier description
of the way our compiler works.


Bye,


Erik.

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel
Russell McManus

Re: Bugs: handling of closure variables (why I found out)

Reply Threaded More More options
Print post
Permalink
Erik Huelsmann <[hidden email]> writes:

> (funcall (compile nil
>   '(lambda ()
>        (flet ((%f (f a)
>                  (funcall (lambda () a))
>                  (when (a < 2)
>                     (funcall #'%f #'%f (1+ a)))
>                  a))
>          (%f #'%f 1))))

I know you wrote the above without the benefit of a lisp environment,
but when I change your example above in the obvious way, I get this:

(funcall
 (compile nil
          '(lambda ()
            (labels ((%f (f a)
                       (funcall (lambda () a))
                       (when (< a 2)
                         (funcall #'%f #'%f (1+ a)))
                       a))
              (%f #'%f 1)))))

Which when run with a fresh 'svn up' version of abcl returns 1.

-russ


------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel
Alessio Stalla

Re: Bugs: handling of closure variables (why I found out)

Reply Threaded More More options
Print post
Permalink
On Wed, May 13, 2009 at 9:04 PM, Russell McManus
<[hidden email]> wrote:

> Erik Huelsmann <[hidden email]> writes:
>
>> (funcall (compile nil
>>   '(lambda ()
>>        (flet ((%f (f a)
>>                  (funcall (lambda () a))
>>                  (when (a < 2)
>>                     (funcall #'%f #'%f (1+ a)))
>>                  a))
>>          (%f #'%f 1))))
>
> I know you wrote the above without the benefit of a lisp environment,
> but when I change your example above in the obvious way, I get this:
>
> (funcall
>  (compile nil
>          '(lambda ()
>            (labels ((%f (f a)
>                       (funcall (lambda () a))
>                       (when (< a 2)
>                         (funcall #'%f #'%f (1+ a)))
>                       a))
>              (%f #'%f 1)))))
>
> Which when run with a fresh 'svn up' version of abcl returns 1.
>
> -russ
>

I think Erik meant

(funcall (compile nil '(lambda ()
        (flet ((%f (f a)
                  (funcall (lambda () a))
                  (when (< a 2)
                     (funcall f f (1+ a)))
                  a))
         (%f #'%f 1)))))

i.e. use the f parameter and not the %f function inside the body of
%f. This correctly evaluates to 1 in sbcl, but gives 2 in abcl fresh
from svn.

Alessio

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel
Peter Tsenter

JSR223Example.java got broken

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
Statement
 
    ((Invocable) lispEngine).invokeFunction("hello", "world");
 
invokes abcl interactive debugger with a message
 
 
Debugger invoked on condition of type UNBOUND-VARIABLE:
  The variable ABCL-SCRIPT-USER::SOMEVARIABLE is unbound.
 
I don't rememeber how far back it used to work.


Hotmail® goes with you. Get it on your BlackBerry or iPhone.
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel
Alessio Stalla

Re: JSR223Example.java got broken

Reply Threaded More More options
Print post
Permalink
On Mon, May 18, 2009 at 7:15 AM, Peter Tsenter <[hidden email]> wrote:

> Statement
>
>     ((Invocable) lispEngine).invokeFunction("hello", "world");
>
> invokes abcl interactive debugger with a message
>
>
> Debugger invoked on condition of type UNBOUND-VARIABLE:
>   The variable ABCL-SCRIPT-USER::SOMEVARIABLE is unbound.
>
> I don't rememeber how far back it used to work.

Thanks for the report, I'll investigate it this evening. I haven't run
the example in a while, and even if I have some simple unit tests to
check the AbclScriptEngine, these tests are far from complete and the
example might do something which is not covered by them.

Alessio

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel
Alessio Stalla

Re: JSR223Example.java got broken

Reply Threaded More More options
Print post
Permalink
On Mon, May 18, 2009 at 9:12 AM, Alessio Stalla <[hidden email]> wrote:

> On Mon, May 18, 2009 at 7:15 AM, Peter Tsenter <[hidden email]> wrote:
>> Statement
>>
>>     ((Invocable) lispEngine).invokeFunction("hello", "world");
>>
>> invokes abcl interactive debugger with a message
>>
>>
>> Debugger invoked on condition of type UNBOUND-VARIABLE:
>>   The variable ABCL-SCRIPT-USER::SOMEVARIABLE is unbound.
>>
>> I don't rememeber how far back it used to work.
>
> Thanks for the report, I'll investigate it this evening. I haven't run
> the example in a while, and even if I have some simple unit tests to
> check the AbclScriptEngine, these tests are far from complete and the
> example might do something which is not covered by them.
>

I've given a quick look at the AbclScriptEngine on Trac (I don't have
my local working copy available now) and I've understood the problem.

With my last commit I changed how the Lisp code is executed in a
JSR-223 context. Before it went like this:

The code string was parsed with read-from-string (with *package* bound
to :abcl-script-user), the resulting Lisp code was enclosed in a (let
...) form with all the bindings passed through the ScriptContext, and
finally eval'ed.

This worked fine with snippets of code like those in the example and
in my unit tests, but hadn't the correct behavior when loading a Lisp
file, since the built-in load function behaves differently - it reads
and evaluates a form at a time, so that e.g. in-package forms are
allowed to change the current package and affect the reading of
subsequent forms.

So now I changed it to use a modified version of load that returns the
value of the last form loaded. This solves the problem above. However,
the let-form trick for establishing a set of bindings does not work
anymore, since load always runs in a null lexical environment. So I
also made bindings declared with ScriptContext special, so they can be
seen by load. However, I forgot to change how function invocation
works - it should now re-establish bindings, since those are now
special and thus are not resolved at function declaration time but at
function call time. It's not a hard thing to do, I should be able to
fix it before tomorrow.

Bye,
Alessio

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel
Alessio Stalla

Re: JSR223Example.java got broken

Reply Threaded More More options
Print post
Permalink
On Mon, May 18, 2009 at 10:13 AM, Alessio Stalla
<[hidden email]> wrote:

> On Mon, May 18, 2009 at 9:12 AM, Alessio Stalla <[hidden email]> wrote:
>> On Mon, May 18, 2009 at 7:15 AM, Peter Tsenter <[hidden email]> wrote:
>>> Statement
>>>
>>>     ((Invocable) lispEngine).invokeFunction("hello", "world");
>>>
>>> invokes abcl interactive debugger with a message
>>>
>>>
>>> Debugger invoked on condition of type UNBOUND-VARIABLE:
>>>   The variable ABCL-SCRIPT-USER::SOMEVARIABLE is unbound.
>>>
>>> I don't rememeber how far back it used to work.
>>
>> Thanks for the report, I'll investigate it this evening. I haven't run
>> the example in a while, and even if I have some simple unit tests to
>> check the AbclScriptEngine, these tests are far from complete and the
>> example might do something which is not covered by them.
>>
>
> I've given a quick look at the AbclScriptEngine on Trac (I don't have
> my local working copy available now) and I've understood the problem.
>
> With my last commit I changed how the Lisp code is executed in a
> JSR-223 context. Before it went like this:
>
> The code string was parsed with read-from-string (with *package* bound
> to :abcl-script-user), the resulting Lisp code was enclosed in a (let
> ...) form with all the bindings passed through the ScriptContext, and
> finally eval'ed.
>
> This worked fine with snippets of code like those in the example and
> in my unit tests, but hadn't the correct behavior when loading a Lisp
> file, since the built-in load function behaves differently - it reads
> and evaluates a form at a time, so that e.g. in-package forms are
> allowed to change the current package and affect the reading of
> subsequent forms.
>
> So now I changed it to use a modified version of load that returns the
> value of the last form loaded. This solves the problem above. However,
> the let-form trick for establishing a set of bindings does not work
> anymore, since load always runs in a null lexical environment. So I
> also made bindings declared with ScriptContext special, so they can be
> seen by load. However, I forgot to change how function invocation
> works - it should now re-establish bindings, since those are now
> special and thus are not resolved at function declaration time but at
> function call time. It's not a hard thing to do, I should be able to
> fix it before tomorrow.

Ok, I have hopefully fixed it. For details, here's the commit comment:

"
Fixed function evaluation using invokeFunction. It was broken since last
commit on JSR-223. Now invokeFunction uses the same "eval-in-script-context"
macro that is used to evaluate interpreted and compiled code in the right
environment, including special variables from the ScriptContext.
In passing, the invokeFunction() method has also been fixed so that
javaInstance() is called on its return value, like it happens in all other
kinds of Lisp calls from Java.
"

I add that the javaInstance() thing means that
scriptEngine.evalFunction("random", 10) will return an Integer and not
a Fixnum like it did before, which is conforming to how the rest of
ABCL works in the general case.

Cheers,
Ale

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel
Peter Tsenter

Re: JSR223Example.java got broken

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
I've re-tested and happy to report that everything works.
But I'd like to make a few points.

1. Explicit (manual) registration of AbclScriptEngineFactory requires one more statement:
 
import org.armedbear.lisp.scripting.AbclScriptEngineFactory;

2. Registration and configuration runs very slooooooow.
Before I had just enough time to drink my coffee.
Now, I can execute the following procedure:
 
   run to the local store-buy coffee-run back-drink it
 
twice.
 
OK, it took 94236 msec. For comparison, 42 interpretation - 875, compilation - 2485, evaluation - 31 and 0.


 

> Date: Mon, 18 May 2009 21:41:34 +0200
> Subject: Re: [j-devel] JSR223Example.java got broken
> From: [hidden email]
> To: [hidden email]
> CC: [hidden email]
>
> On Mon, May 18, 2009 at 10:13 AM, Alessio Stalla
> <[hidden email]> wrote:
> > On Mon, May 18, 2009 at 9:12 AM, Alessio Stalla <[hidden email]> wrote:
> >> On Mon, May 18, 2009 at 7:15 AM, Peter Tsenter <[hidden email]> wrote:
> >>> Statement
> >>>
> >>>     ((Invocable) lispEngine).invokeFunction("hello", "world");
> >>>
> >>> invokes abcl interactive debugger with a message
> >>>
> >>>
> >>> Debugger invoked on condition of type UNBOUND-VARIABLE:
> >>>   The variable ABCL-SCRIPT-USER::SOMEVARIABLE is unbound.
> >>>
> >>> I don't rememeber how far back it used to work.
> >>
> >> Thanks for the report, I'll investigate it this evening. I haven't run
> >> the example in a while, and even if I have some simple unit tests to
> >> check the AbclScriptEngine, these tests are far from complete and the
> >> example might do something which is not covered by them.
> >>
> >
> > I've given a quick look at the AbclScriptEngine on Trac (I don't have
> > my local working copy available now) and I've understood the problem.
> >
> > With my last commit I changed how the Lisp code is executed in a
> > JSR-223 context. Before it went like this:
> >
> > The code string was parsed with read-from-string (with *package* bound
> > to :abcl-script-user), the resulting Lisp code was enclosed in a (let
> > ...) form with all the bindings passed through the ScriptContext, and
> > finally eval'ed.
> >
> > This worked fine with snippets of code like those in the example and
> > in my unit tests, but hadn't the correct behavior when loading a Lisp
> > file, since the built-in load function behaves differently - it reads
> > and evaluates a form at a time, so that e.g. in-package forms are
> > allowed to change the current package and affect the reading of
> > subsequent forms.
> >
> > So now I changed it to use a modified version of load that returns the
> > value of the last form loaded. This solves the problem above. However,
> > the let-form trick for establishing a set of bindings does not work
> > anymore, since load always runs in a null lexical environment. So I
> > also made bindings declared with ScriptContext special, so they can be
> > seen by load. However, I forgot to change how function invocation
> > works - it should now re-establish bindings, since those are now
> > special and thus are not resolved at function declaration time but at
> > function call time. It's not a hard thing to do, I should be able to
> > fix it before tomorrow.
>
> Ok, I have hopefully fixed it. For details, here's the commit comment:
>
> "
> Fixed function evaluation using invokeFunction. It was broken since last
> commit on JSR-223. Now invokeFunction uses the same "eval-in-script-context"
> macro that is used to evaluate interpreted and compiled code in the right
> environment, including special variables from the ScriptContext.
> In passing, the invokeFunction() method has also been fixed so that
> javaInstance() is called on its return value, like it happens in all other
> kinds of Lisp calls from Java.
> "
>
> I add that the javaInstance() thing means that
> scriptEngine.evalFunction("random", 10) will return an Integer and not
> a Fixnum like it did before, which is conforming to how the rest of
> ABCL works in the general case.
>
> Cheers,
> Ale



Hotmail® has a new way to see what's up with your friends. Check it out.
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel