martin-saurer / jkernel

Jupyter Notebook / J Integration
GNU General Public License v3.0
44 stars 15 forks source link

Old variables persist, causing misleading results #10

Closed jonahx closed 5 years ago

jonahx commented 6 years ago

This is best demonstrated with a simple example:

1. create some erroneous definition

step1

The error is correct, since the definition of f is a syntax error.

2. now create a correct definition and "run cell" again

step2 Still working as expected, since we've re-bound f to a valid definition.

3. now delete the correct definition and "run cell" again

step3

The result here should be the same as in step 1, but the correct definition of f is still hanging around...

martin-saurer commented 6 years ago

@jonahx J sentences in a notebook cell are feed to the J interpreter line-by-line. So the cell contents:

f=. & @ +
2 f 3

result in a value error because f has not evaluated to a definition. At the time when "2 f 3" is executed, f does not exist. Try only the sentence "f=. & @ +" in a cell. This results in a "syntax error", not in a "value error". Yes, this behaviour can be misleading because only the error message of the last line is displayed. I will investigate in that as soon as my time permits.

 f=. & @ +         <-- syntax error, but not displayed
 f=. +             <-- correct definition *1
 2 f 3             <-- correct evaluation

 f=. & @ +         <-- syntax error, but not displayed
 2 f 3             <-- correct evaluation because f from *1 is still defined

So the good news are that old definitions/variables do NOT persist.

jonahx commented 6 years ago

@martin-saurer

Thanks for looking into it.

Yes, this behaviour can be misleading because only the error message of the last line is displayed. I will investigate in that as soon as my time permits.

Fwiw, I think either of these 2 options would avoid any confusion:

  1. Display only the first error
  2. Display all the errors

So the good news are that old definitions/variables do NOT persist.

What about the behavior I'm seeing in item 3 from my OP?

martin-saurer commented 6 years ago

@jonahx The behavior of item 3 in your OP is what I tried to describe. The last “good” definition of f stays in effect until a new “good” definition is made. A “bad” definition of f (syntax error) does not overwrite or clear the last “good” definition of f.

jonahx commented 6 years ago

@martin-saurer

The behavior of item 3 in your OP is what I tried to describe. The last “good” definition of f stays in effect until a new “good” definition is made. A “bad” definition of f (syntax error) does not overwrite or clear the last “good” definition of f.

I think less confusing behavior would be for a "run cell" to wipe out the previous memory and behave as if the cell you see in front of you, right now, was all that ever existed. Essentially, we want the code itself to be referentially transparent. This is how it works for python notebooks, eg.

martin-saurer commented 5 years ago

Python also keeps variables from previous cells. Wipe out the memory for all previous cells, when running a new cell, is a bad idea.