Skip to content

Fix behavior of $Line , In[...] and Out[...]. - #1988

Open
mmatera wants to merge 1 commit into
masterfrom
fix_Line_In_Out
Open

mmatera wants to merge 1 commit into
masterfrom
fix_Line_In_Out

Conversation

@mmatera

@mmatera mmatera commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Fix behavior of $Line, In[...] and Out[...] always start at 1, and the first evaluation should be stored in In[1] and Out[1].

…rst evaluation should be stored in In[1] and Out[1].
>> Definition[In]
= Attributes[In] = {Listable, Protected}
.
. In[6] = Definition[In]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the order was wrong because the rules were not properly built in mathics.core.evaluation. Adding the evaluation parameter when the rules are created, the rules can determine that that the patterns are literal patterns, and therefore the right precedence is used.


name = "$Line"
summary_text = "current line number"
rules = {"$Line": "1"}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This automatically sets the initial value of $Line after we reset the Definitions object.

line_number = self.get_line_no()
if line_number is not None:
self.set_config_value("$Line", +increment)
self.set_config_value("$Line", line_number + increment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we erase it by mistake at some point...

@rocky rocky Oct 8, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably during Dialog[] addition. We should make sure that this isn't broken by these changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could use in Evaluation.evaluation, in the finally block. Right now, it is not used anywhere

output_forms = self.definitions.outputforms

line_no = self.definitions.get_line_no()
line_no += 1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should happend at the end of the evaluation, not before.

Comment thread mathics/session.py
"""
try:
self.definitions = Definitions(add_builtin)
except KeyError:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have worked before I fixed the MakeBoxes rule. The proper way to decide if we need to load the built-ins is to check if they were already loaded...

Comment thread test/test_session.py
def test_session_evaluation_as_in_cli():
# `evaluation_as_in_cli` returns a `Result` object
session.reset()
assert session.evaluate("$Line") is Symbol("$Line")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fits better with the WMA behaviour.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Please enlighten. In what way is it better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is now in master,

In[1]:= $Line
Out[1]= 2

which is weird. Also, this is not compatible with the result using the MathicsSession object:

>>> from mathics.core.load_builtin  import import_and_load_builtins
>>> from mathics.session import MathicsSession
>>> import_and_load_builtins()
>>> session=MathicsSession()
>>> session.evaluate("$Line")
<Symbol: System`$Line>
>>> session.evaluate_as_in_cli("$Line").result
'1'

which looks inconsistent: three ways to evaluate the same expression in the same condition, and three different results.
In WMA,

In[1]:= $Line                                                                                                                                                                 

Out[1]= 1

@rocky

rocky commented Oct 9, 2026

Copy link
Copy Markdown
Member

When I added Dialog, I was sloppy and didn't add tests to ensure the Dialog numbering would work correctly. I also believe I didn't fully implement all the WMA Dialog features.

(I think at that time I was more focused on diagnosing a Rubi problem we have/had).

Also, we did not, and still don't, have sufficient line numbering tests.

I think what we need to do here is write examples and then tests that show how we're currently broken and demonstrates how this fixes it.

@mmatera

mmatera commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

When I added Dialog, I was sloppy and didn't add tests to ensure the Dialog numbering would work correctly. I also believe I didn't fully implement all the WMA Dialog features.

(I think at that time I was more focused on diagnosing a Rubi problem we have/had).

Also, we did not, and still don't, have sufficient line numbering tests.

I think what we need to do here is write examples and then tests that show how we're currently broken and demonstrates how this fixes it.

The tests were already there; it's just that they were not testing the right behavior...

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants