Skip to content

Recursive sequence expression containing recursive definition doesn't run in finite space #996

Open

Description

@kbattocchi

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.

Activity

  1. dsyme commented on Mar 8, 2016

    @dsyme
    Contributor

    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.

  2. added
    Impact-Low
    on Mar 8, 2016
  3. added
    Impact-Medium
    and removed
    Impact-Low
    on Jun 29, 2017
  4. dsyme commented on Sep 1, 2017

    @dsyme
    Contributor

    @kbattocchi I finally fixed this one in #3536, please take a look

  5. reopened this on Oct 24, 2017
  6. dsyme commented on Oct 24, 2017

    @dsyme
    Contributor

    Reopening as the fix was wrong, see #3749

  7. added this to the 16.0 milestone on Aug 29, 2018
  8. modified the milestones: 16.0, 16.1 on Feb 21, 2019
  9. 7 remaining items

  10. added
    Area-Compiler-StateMachines
    and removed on Mar 31, 2022
  11. 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
  12. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-StateMachinesFeature ImprovementImpact-Low

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions