Skip to content

Breaks functions with tail recursion from within loops? #1

@hartwork

Description

@hartwork

Hi @raaidrt! Cool project — thanks for sharing it as software libre!

I read through the design document and noticed the part with the insertion of continue. In nested loops continue would exit the innermost loop in Python, and so I started wondering whether (a) the tacopy algorithm as documented is simplified for educational purposes and handles tail calls from within a loop fine or (b) semantics would change and then that would be a (potentially dangerous) bug. From my experiments with latest main (commit 8f5db70) my impression is (b) is the case. For a reproducer:

The code

from tacopy import tacopy


def tailing(n: int) -> int:
    if n <= 0:
        print("  A", n)
        return 0

    for i in range(3):
        print("  B", n, i)
        return tailing(n - 1)
    
    print("  C", n)
    return 0


print("ORIGINAL")
tailing(2)

print()

print("TACOPIED")
tacopy(tailing)(2)

The output

# python3 demo.py 
ORIGINAL
  B 2 0
  B 1 0
  A 0

TACOPIED
  B 2 0
  B 1 1
  B 0 2
  C -1

I might be missing something here. What do you think?

Best, Sebastian

CC @hannob @TimWolla

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions