sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.22k stars 424 forks source link

Magma interface poorly supports strings #37978

Open edgarcosta opened 2 months ago

edgarcosta commented 2 months ago

Steps To Reproduce

sage: foo = "string"
sage: magma(foo)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File /home/sage/sage-10.2/src/sage/interfaces/expect.py:1496, in ExpectElement.__init__(self, parent, value, is_name, name)
   1495 try:
-> 1496     self._name = parent._create(value, name=name)
   1497 # Convert ValueError and RuntimeError to TypeError for
   1498 # coercion to work properly.

File /home/sage/sage-10.2/src/sage/interfaces/interface.py:516, in Interface._create(self, value, name)
    515 name = self._next_var_name() if name is None else name
--> 516 self.set(name, value)
    517 return name

File /home/sage/sage-10.2/src/sage/interfaces/magma.py:626, in Magma.set(self, var, value)
    609 """
    610 Set the variable var to the given value in the Magma interpreter.
    611
   (...)
    624     13/5
    625 """
--> 626 out = self.eval("%s:=%s" % (var, value))
    627 if out.lower().find("error") != -1:

File /home/sage/sage-10.2/src/sage/interfaces/magma.py:559, in Magma.eval(self, x, strip, **kwds)
    558 if 'Runtime error' in ans or 'User error' in ans:
--> 559     raise RuntimeError("Error evaluating Magma code.\nIN:%s\nOUT:%s" % (x, ans))
    560 return ans

RuntimeError: Error evaluating Magma code.
IN:_sage_[1]:=string;
OUT:
>> _sage_[1]:=string;
              ^
User error: Identifier 'string' has not been declared or assigned

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In [2], line 1
----> 1 magma(foo)

File /home/sage/sage-10.2/src/sage/interfaces/magma.py:791, in Magma.__call__(self, x, gens)
    788 except TypeError:  # if x isn't hashable
    789     pass
--> 791 A = Expect.__call__(self, x)
    792 if has_cache:
    793     x._magma_cache[self] = A

File /home/sage/sage-10.2/src/sage/interfaces/interface.py:298, in Interface.__call__(self, x, name)
    295         pass
    297 if isinstance(x, str):
--> 298     return cls(self, x, name=name)
    299 try:
    300     # Special methods do not and should not have an option to
    301     # set the name directly, as the identifier assigned by the
    302     # interface should stay consistent. An identifier with a
    303     # user-assigned name might change its value, so we return a
    304     # new element.
    305     result = self._coerce_from_special_method(x)

File /home/sage/sage-10.2/src/sage/interfaces/expect.py:1501, in ExpectElement.__init__(self, parent, value, is_name, name)
   1499 except (RuntimeError, ValueError) as x:
   1500     self._session_number = -1
-> 1501     raise TypeError(*x.args)
   1502 except BaseException:
   1503     self._session_number = -1

TypeError: Error evaluating Magma code.
IN:_sage_[1]:=string;
OUT:
>> _sage_[1]:=string;
              ^
User error: Identifier 'string' has not been declared or assigned

Checklist

edgarcosta commented 2 months ago

here is a temporary workaround:

sage: bar = magma('"foo"')
sage: magma.eval(f'Type({bar.name()})')
'MonStgElt'
fchapoton commented 1 month ago

how to tell a string from a command ? is "2+2" a string ?

edgarcosta commented 1 month ago

I agree it is tricky for magma(), but the issue also arises for magma.?(),

For example:


sage: magma.HasSignature('"Print"', [])
false
sage: magma.HasSignature("Print", [])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File /Applications/sage-dev/src/sage/interfaces/expect.py:1520, in ExpectElement.__init__(self, parent, value, is_name, name)
   1519 try:
-> 1520     self._name = parent._create(value, name=name)
   1521 # Convert ValueError and RuntimeError to TypeError for
   1522 # coercion to work properly.

File /Applications/sage-dev/src/sage/interfaces/interface.py:517, in Interface._create(self, value, name)
    516 name = self._next_var_name() if name is None else name
--> 517 self.set(name, value)
    518 return name

File /Applications/sage-dev/src/sage/interfaces/magma.py:629, in Magma.set(self, var, value)
    612 """
    613 Set the variable var to the given value in the Magma interpreter.
    614
   (...)
    627     13/5
    628 """
--> 629 out = self.eval("%s:=%s" % (var, value))
    630 if out.lower().find("error") != -1:

File /Applications/sage-dev/src/sage/interfaces/magma.py:562, in Magma.eval(self, x, strip, **kwds)
    561 if 'Runtime error' in ans or 'User error' in ans:
--> 562     raise RuntimeError("Error evaluating Magma code.\nIN:%s\nOUT:%s" % (x, ans))
    563 return ans

RuntimeError: Error evaluating Magma code.
IN:_sage_[4]:=HasSignature(_sage_[2],_sage_[1]);
OUT:
>> _sage_[4]:=HasSignature(_sage_[2],_sage_[1]);
                          ^
Runtime error in 'HasSignature': Bad argument types
Argument types given: Intrinsic, SeqEnum[Any]

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 magma.HasSignature("Print", [])

File /Applications/sage-dev/src/sage/interfaces/magma.py:1802, in MagmaFunction.__call__(self, *args, **kwds)
   1800         del kwds['nvals']
   1801 M = self._parent
-> 1802 return M.function_call(self._name,
   1803                        list(args),
   1804                        params=kwds,
   1805                        nvals=nvals)

File /Applications/sage-dev/src/sage/interfaces/magma.py:1170, in Magma.function_call(self, function, args, params, nvals)
   1165     par = ' : ' + ','.join('%s:=%s' % (a, b.name())
   1166                            for a, b in params.items())
   1168 fun = "%s(%s%s)" % (function, ",".join(s.name() for s in args), par)
-> 1170 return self._do_call(fun, nvals)

File /Applications/sage-dev/src/sage/interfaces/magma.py:1220, in Magma._do_call(self, code, nvals)
   1218     ans = None
   1219 elif nvals == 1:
-> 1220     return self(code)
   1221 else:
   1222     v = [self._next_var_name() for _ in range(nvals)]

File /Applications/sage-dev/src/sage/interfaces/magma.py:797, in Magma.__call__(self, x, gens)
    794 except TypeError:  # if x isn't hashable
    795     pass
--> 797 A = Expect.__call__(self, x)
    798 if has_cache:
    799     x._magma_cache[self] = A

File /Applications/sage-dev/src/sage/interfaces/interface.py:299, in Interface.__call__(self, x, name)
    296         pass
    298 if isinstance(x, str):
--> 299     return cls(self, x, name=name)
    300 try:
    301     # Special methods do not and should not have an option to
    302     # set the name directly, as the identifier assigned by the
    303     # interface should stay consistent. An identifier with a
    304     # user-assigned name might change its value, so we return a
    305     # new element.
    306     result = self._coerce_from_special_method(x)

File /Applications/sage-dev/src/sage/interfaces/expect.py:1525, in ExpectElement.__init__(self, parent, value, is_name, name)
   1523 except (RuntimeError, ValueError) as x:
   1524     self._session_number = -1
-> 1525     raise TypeError(*x.args)
   1526 except BaseException:
   1527     self._session_number = -1

TypeError: Error evaluating Magma code.
IN:_sage_[4]:=HasSignature(_sage_[2],_sage_[1]);
OUT:
>> _sage_[4]:=HasSignature(_sage_[2],_sage_[1]);
                          ^
Runtime error in 'HasSignature': Bad argument types
Argument types given: Intrinsic, SeqEnum[Any]

sage: