lihaoyi / macropy

Macros in Python: quasiquotes, case classes, LINQ and more!
3.28k stars 176 forks source link

Tail-call Optimization not working in Python 3.6 or 2.7 #80

Closed DarkPhoenix6 closed 6 years ago

DarkPhoenix6 commented 7 years ago

from macropy.experimental.tco import macros, tco

@tco def fact(n, acc=0): if n == 0: return acc else: return fact(n-1, n * acc)

print(fact(10000))

gives:

Traceback (most recent call last): File "", line 2, in File "C:\Program Files\Python36\lib\site-packages\macropy-1.0.3-py3.6.egg\macropy\core\macros.py", line 31, in call return self.func(*args, **kwargs) File "C:\Program Files\Python36\lib\site-packages\macropy-1.0.3-py3.6.egg\macropy\experimental\tco.py", line 131, in tco tree.decorator_list = ([hq[trampoline_decorator]] + File "C:\Program Files\Python36\lib\site-packages\macropy-1.0.3-py3.6.egg\macropy\core\macros.py", line 34, in getitem raise TypeError(self.msg.replace("%s", self.func.name)) TypeError: Macro hq illegally invoked at runtime; did you import it properly using from ... import macros, hq?

from macropy.experimental.tco import macros, tco

@tco ... def fact(n, acc=0): ... if n == 0: ... return acc ... else: ... return fact(n-1, n acc) ... Traceback (most recent call last): File "", line 2, in File "C:\Python27\lib\site-packages\macropy\core\macros.py", line 28, in call return self.func(args, **kwargs) File "C:\Python27\lib\site-packages\macropy\experimental\tco.py", line 131, in tco tree.decorator_list = ([hq[trampoline_decorator]] + File "C:\Python27\lib\site-packages\macropy\core\macros.py", line 31, in getitem raise TypeError(self.msg.replace("%s", self.func.name)) TypeError: Macro hq illegally invoked at runtime; did you import it properly using from ... import macros, hq?

azazel75 commented 6 years ago

Python 2.7 is not maintained anymore, but TCO is working in master with Python 3.5+

21IDEAS commented 6 years ago

The example from the documentation is not working in Python 3.5, 3.6 for me.

from macropy.experimental.tco import macros, tco

@tco
def fact(n, acc=1): 
    if n == 0:
        return acc
    else:
        return fact(n-1, n * acc)
    print(fact(10000))

instead returns the same error as above:

Traceback (most recent call last):
  File "fib_tco.py", line 5, in <module>
    def fact(n, acc=1): 
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/macropy/core/macros.py", line 29, in __call__
    return self.func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/macropy/experimental/tco.py", line 147, in tco
    tree.decorator_list = ([hq[trampoline_decorator]] +
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/macropy/core/macros.py", line 32, in __getitem__
    raise TypeError(self.msg.replace("%s", self.func.__name__))
TypeError: Macro `hq` illegally invoked at runtime; did you import it properly using `from ... import macros, hq`?
azazel75 commented 6 years ago

As the text for the error states, you are running it in the wrong way, i.e. without enabling macro expansion. Please follow the tutorials

glassresistor commented 5 years ago

@azazel75 please explain what to do differently googling and reading the tutorials doesn't help.

generic-github-user commented 1 year ago

I'm having the same issue (have tried multiple recent versions of Python), and was also unable to find any relevant information in the documentation.

rebcabin commented 1 year ago

I made TCO work without macropy in case you're interested:

https://github.com/rebcabin/rebcabin.github.io/blob/main/LambdaThePynultimateImperative002.ipynb