Tail-call Optimization not working in Python 3.6 or 2.7
DarkPhoenix6 opened this issue ยท 6 comments
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
?
Python 2.7 is not maintained anymore, but TCO is working in master with Python 3.5+
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`?
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
@azazel75 please explain what to do differently googling and reading the tutorials doesn't help.
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.
I made TCO work without macropy in case you're interested:
https://github.com/rebcabin/rebcabin.github.io/blob/main/LambdaThePynultimateImperative002.ipynb