You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Recursive sequence expression containing recursive definition doesn't run in finite space #996
This small example causes a stack overflow, but shouldn't:
let rec loop r = seq {
if r > 0 then
let rec unused() = unused()
yield r
yield! loop r
}
printfn "%i" (Seq.nth 10000000 (loop 1))
Removing the conditional or replacing the unused recursive definition with an unused non-recursive definition both appear to result in the expected behavior of constant stack space usage.
This appears to be due to the fact that "let rec" causes the sequence expression to be compiled into combinatory form, but the last two portions are still compiled into state-machine form, and the recurrent thunk in-between the two forms causes the stack usage to grow.
One fix is to add Expr.LetRec into the forms compiled by LowerSeqExpr in visualfsharp\src\fsharp\LowerCallsAndSeqs.fs. I'm not sure how hard that is though.
changed the title [-]Sequence expression containing recursive definition is compiled incorrectly[/-][+]Sequence expression containing recursive definition doesn't run in finite space[/+]on Mar 10, 2023
changed the title [-]Sequence expression containing recursive definition doesn't run in finite space[/-][+]Recursive sequence expression containing recursive definition doesn't run in finite space[/+]on Mar 10, 2023
Activity
dsyme commentedon Mar 8, 2016
This appears to be due to the fact that "let rec" causes the sequence expression to be compiled into combinatory form, but the last two portions are still compiled into state-machine form, and the recurrent thunk in-between the two forms causes the stack usage to grow.
One fix is to add Expr.LetRec into the forms compiled by
LowerSeqExprinvisualfsharp\src\fsharp\LowerCallsAndSeqs.fs. I'm not sure how hard that is though.dsyme commentedon Sep 1, 2017
@kbattocchi I finally fixed this one in #3536, please take a look
dsyme commentedon Oct 24, 2017
Reopening as the fix was wrong, see #3749
7 remaining items
[-]Sequence expression containing recursive definition is compiled incorrectly[/-][+]Sequence expression containing recursive definition doesn't run in finite space[/+][-]Sequence expression containing recursive definition doesn't run in finite space[/-][+]Recursive sequence expression containing recursive definition doesn't run in finite space[/+]