lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.37k stars 156 forks source link

Adding support for is_integer attribute #2686

Closed anutosh491 closed 1 month ago

anutosh491 commented 1 month ago

This Pr is trying to support the following case (and is also required for the gruntz algorithm)

from lpython import s
from sympy import Symbol

def main0():
    a: S = Symbol('x')
    b: S = S(10)
    x: bool = a.is_integer
    y: bool = b.is_integer
    print(x)
    print(y)

(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython --enable-symengine examples/expr2.py 
False
True
anutosh491 commented 1 month ago

Ready !

anutosh491 commented 1 month ago

There is a failure at the CI. Apart from that this looks good.

Hmm, pretty weird, I wasn't expecting any test to fail. You can quickly check the results on sympy live (https://live.sympy.org/), A WebAssembly-powered Python kernel backed by Pyodide

image
anutosh491 commented 1 month ago

@certik could you maybe try having a look ?

Shaikh-Ubaid commented 1 month ago

@anutosh491 The following diff fixes the issue for me:

diff --git a/src/runtime/lpython/lpython.py b/src/runtime/lpython/lpython.py
index 9f23b02e9..a8b88ac6f 100644
--- a/src/runtime/lpython/lpython.py
+++ b/src/runtime/lpython/lpython.py
@@ -15,6 +15,10 @@ __slots__ = ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64",

 # data-types

+def get_sympy_S(x):
+    from sympy import S
+    return S(x)
+
 type_to_convert_func = {
     "i1": bool,
     "i8": int,
@@ -34,7 +38,7 @@ type_to_convert_func = {
     "Callable": lambda x: x,
     "Allocatable": lambda x: x,
     "Pointer": lambda x: x,
-    "S": lambda x: x,
+    "S": get_sympy_S,
 }

 class Type:
anutosh491 commented 1 month ago

Hey thanks Ubaid, I think this might fix the issue for us.

Shaikh-Ubaid commented 1 month ago

We should clean the commit history or squash merge this.