Mathics3 / mathics-core

An open-source Mathematica. This repository contains the Python modules for WL Built-in functions, variables, core primitives, e.g. Symbol, a parser to create Expressions, and an evaluator to execute them.
https://mathics.org
Other
786 stars 48 forks source link

Python IndexError possibly on `Delete[]` of an empty list (but not #1105) #1106

Closed rocky closed 1 month ago

rocky commented 2 months ago

Description

Mathics3 is exiting with an Index error in a situation I haven't fully narrowed down yet.

How to Reproduce

I am getting an IndexError exception to get Greg Chaitin's small Lisp interpreter running.

Steps to reproduce the behavior.

In Mathics3, after cd'ing to directories that have rlist.mathics and sets.l:

In[1]:= << "rlisp.mathics" 
Out[1]= None

In[2]:= run@"sets" 

Output Given

Below, I've run with post-mortem debugging so I might better understand what is going on.

$ trepan3k --post-mortem ../mathics/main.py
(/tmp/Mathics3/mathics-core/mathics/main.py:1): <module>
-> 1 #!/usr/bin/env python
Evaluate unrecognized is on.
Macro "kilL!" defined.
Rocky's Python Trepan Python code startup loaded
(trepan3k) c
Fast continue...

Mathics 7.0.1dev0
on CPython 3.12.6 (main, Sep 12 2024, 13:05:57) [GCC 13.2.0]
using SymPy 1.12.1, mpmath 1.3.0, numpy 1.26.4, cython Not installed

Copyright (C) 2011-2024 The Mathics3 Team.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
See the documentation for the full license.

Quit by evaluating Quit[] or by pressing CONTROL-D.

In[1]:= << "rlisp.mathics"
Out[1]= None

In[2]:= run@"sets"
LISP Interpreter Run

[[[[[
 Elementary Set Theory in LISP (finite sets)
]]]]]
[Set membership predicate:]
define (member? e[lement] set)
   [Is set empty?]
   if atom set [then] false [else]
   [Is the element that we are looking for the first element?]
   if = e car set [then] true [else]
   [recursion step!]
   [return] (member? e cdr set)

define      member?
value       (lambda (e set) (if (atom set) false (if (= e (car
             set)) true (member? e (cdr set)))))

(member? 1 '(1 2 3))

expression  (member? 1 (' (1 2 3)))
Traceback (most recent call last):
  File "/home/rocky/.pyenv/versions/3.12.6/bin/trepan3k", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/tmp/rocky/python3-trepan/trepan/__main__.py", line 241, in main
    normal_termination = dbg.run_script(mainpyfile)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
....
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1021, in expression_pattern_match_element_process_items
    pattern.get_wrappings(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 519, in get_wrappings
    yield_func(items[0])
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1010, in yield_wrapping
    parms["element"].match(
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns.py", line 1022, in match
    self.pattern.match(yield_func, expression, new_vars, evaluation)
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns.py", line 1253, in match
    yield_func(vars, None)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 991, in match_yield
    pattern.match_element(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 641, in match_element
    expression_pattern_match_element_process_items(items, items_rest, parms)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1021, in expression_pattern_match_element_process_items
    pattern.get_wrappings(
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 519, in get_wrappings
    yield_func(items[0])
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1010, in yield_wrapping
    parms["element"].match(
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns.py", line 1022, in match
    self.pattern.match(yield_func, expression, new_vars, evaluation)
  File "/tmp/Mathics3/mathics-core/mathics/builtin/patterns.py", line 1362, in match
    yield_func(vars, None)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 1007, in match_yield
    yield_func(new_vars, items_rest)
  File "/tmp/Mathics3/mathics-core/mathics/core/pattern.py", line 981, in element_yield
    yield_func(
  File "/tmp/Mathics3/mathics-core/mathics/core/rules.py", line 134, in yield_match
    new_expression = apply_fn(expression, vars, options, evaluation)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/core/rules.py", line 339, in apply_function
    return self.function(evaluation=evaluation, **vars_noctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/Mathics3/mathics-core/mathics/builtin/list/eol.py", line 374, in eval
    if positions.elements[0].has_form("List", None)
       ~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range
Uncaught exception. Entering post-mortem debugger...
(/tmp/Mathics3/mathics-core/mathics/builtin/list/eol.py:374 @350): eval
!! 374             if positions.elements[0].has_form("List", None)
R=> (<class 'IndexError'>, IndexError('tuple index out of range'), <traceback object at 0x711ca8d414c0>)
(Trepan3k:pm) positions
<ListExpression: ()>
(Trepan3k:pm) load trepan3k_mathics3
WARNING:root:``import_and_load_builtins`` should be called just once...
loaded command: "mathics3"
loaded command: "mbacktrace"
loaded command: "mup"
loaded command: "printelement"
(Trepan3k:pm) mbt -b
B>0 (0) Delete[expr_, positions___]

         Delete[expr_, positions___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/list/eol.py' at line 374
B#1 (35) CompoundExpression[expr___]

          CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#2 (55) Block[vars_, expr_]

          Block[vars_, expr_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/scoping.py' at line 205
B#3 (86) CompoundExpression[expr___]

          CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#4 (106) Block[vars_, expr_]

           Block[vars_, expr_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/scoping.py' at line 205
B#5 (137) CompoundExpression[expr___]

           CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#6 (157) Block[vars_, expr_]

           Block[vars_, expr_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/scoping.py' at line 205
B#7 (184) Catch[expr_]

           Catch[expr_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 133
B#8 (203) CompoundExpression[expr___]

           CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#9 (230) CompoundExpression[expr___]

           CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#10 (249) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#11 (268) While[test_, body_]

            While[test_, body_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 645
B#12 (299) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B>13 (326) Delete[expr_, positions___]

            Delete[expr_, positions___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/list/eol.py' at line 374
B#14 (361) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#15 (381) Block[vars_, expr_]

            Block[vars_, expr_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/scoping.py' at line 205
B#16 (412) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#17 (432) Block[vars_, expr_]

            Block[vars_, expr_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/scoping.py' at line 205
B#18 (463) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#19 (483) Block[vars_, expr_]

            Block[vars_, expr_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/scoping.py' at line 205
B#20 (510) Catch[expr_]

            Catch[expr_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 134
B#21 (529) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#22 (556) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#23 (575) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
B#24 (594) While[test_, body_]

            While[test_, body_]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 650
B#25 (625) CompoundExpression[expr___]

            CompoundExpression[expr___]
     called from file '/tmp/Mathics3/mathics-core/mathics/builtin/procedural.py' at line 185
(Trepan3k:pm) positions.elements
()
(Trepan3k:pm) break /tmp/Mathics3/mathics-core/mathics/builtin/list/eol.py:281

Additional context

1105 was opened as an attempt to recreate the problem. This problem while similar may be different.