Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:50:31+00:00 2026-05-23T15:50:31+00:00

Disclosure: this came up in FsCheck, an F# random testing framework I maintain. I

  • 0

Disclosure: this came up in FsCheck, an F# random testing framework I maintain. I have a solution, but I do not like it. Moreover, I do not understand the problem – it was merely circumvented.

A fairly standard implementation of (monadic, if we’re going to use big words) sequence is:

let sequence l = 
    let k m m' = gen { let! x = m
                       let! xs = m'
                       return (x::xs) }
    List.foldBack k l (gen { return [] })

Where gen can be replaced by a computation builder of choice. Unfortunately, that implementation consumes stack space, and so eventually stack overflows if the list is long enough.The question is: why? I know in principle foldBack is not tail recursive, but the clever bunnies of the F# team have circumvented that in the foldBack implementation. Is there a problem in the computation builder implementation?

If I change the implementation to the below, everything is fine:

let sequence l =
    let rec go gs acc size r0 = 
        match gs with
        | [] -> List.rev acc
        | (Gen g)::gs' ->
            let r1,r2 = split r0
            let y = g size r1
            go gs' (y::acc) size r2
    Gen(fun n r -> go l [] n r)

For completeness, the Gen type and computation builder can be found in the FsCheck source

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:50:32+00:00Added an answer on May 23, 2026 at 3:50 pm

    Building on Tomas’s answer, let’s define two modules:

    module Kurt = 
        type Gen<'a> = Gen of (int -> 'a)
    
        let unit x = Gen (fun _ -> x)
    
        let bind k (Gen m) =     
            Gen (fun n ->       
                let (Gen m') = k (m n)       
                m' n)
    
        type GenBuilder() =
            member x.Return(v) = unit v
            member x.Bind(v,f) = bind f v
    
        let gen = GenBuilder()
    
    
    module Tomas =
        type Gen<'a> = Gen of (int -> ('a -> unit) -> unit)
    
        let unit x = Gen (fun _ f -> f x)
    
        let bind k (Gen m) =     
            Gen (fun n f ->       
                m n (fun r ->         
                    let (Gen m') = k r        
                    m' n f))
    
        type GenBuilder() =
            member x.Return v = unit v
            member x.Bind(v,f) = bind f v
    
        let gen = GenBuilder()
    

    To simplify things a bit, let’s rewrite your original sequence function as

    let rec sequence = function
    | [] -> gen { return [] }
    | m::ms -> gen {
        let! x = m
        let! xs = sequence ms
        return x::xs }
    

    Now, sequence [for i in 1 .. 100000 -> unit i] will run to completion regardless of whether sequence is defined in terms of Kurt.gen or Tomas.gen. The issue is not that sequence causes a stack overflow when using your definitions, it’s that the function returned from the call to sequence causes a stack overflow when it is called.

    To see why this is so, let’s expand the definition of sequence in terms of the underlying monadic operations:

    let rec sequence = function
    | [] -> unit []
    | m::ms ->
        bind (fun x -> bind (fun xs -> unit (x::xs)) (sequence ms)) m
    

    Inlining the Kurt.unit and Kurt.bind values and simplifying like crazy, we get

    let rec sequence = function
    | [] -> Kurt.Gen(fun _ -> [])
    | (Kurt.Gen m)::ms ->
        Kurt.Gen(fun n ->
                let (Kurt.Gen ms') = sequence ms
                (m n)::(ms' n))
    

    Now it’s hopefully clear why calling let (Kurt.Gen f) = sequence [for i in 1 .. 1000000 -> unit i] in f 0 overflows the stack: f requires a non-tail-recursive call to sequence and evaluation of the resulting function, so there will be one stack frame for each recursive call.

    Inlining Tomas.unit and Tomas.bind into the definition of sequence instead, we get the following simplified version:

    let rec sequence = function
    | [] -> Tomas.Gen (fun _ f -> f [])
    | (Tomas.Gen m)::ms ->
        Tomas.Gen(fun n f ->  
            m n (fun r ->
                let (Tomas.Gen ms') = sequence ms
                ms' n (fun rs ->  f (r::rs))))
    

    Reasoning about this variant is tricky. You can empirically verify that it won’t blow the stack for some arbitrarily large inputs (as Tomas shows in his answer), and you can step through the evaluation to convince yourself of this fact. However, the stack consumption depends on the Gen instances in the list that’s passed in, and it is possible to blow the stack for inputs that aren’t themselves tail recursive:

    // ok
    let (Tomas.Gen f) = sequence [for i in 1 .. 1000000 -> unit i]
    f 0 (fun list -> printfn "%i" list.Length)
    
    // not ok...
    let (Tomas.Gen f) = sequence [for i in 1 .. 1000000 -> Gen(fun _ f -> f i; printfn "%i" i)]
    f 0 (fun list -> printfn "%i" list.Length)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would you define testing? In the interest of full disclosure, I'm posting this
I have a small progressive disclosure (text expand and collapse) but the toggle text
I have this old jquery script for progressive disclosure: (notice the $(this).text('more...') code changes
Disclosure: This is university work. I am not expecting code to accomplish my task
full disclosure - this is for a homework assignment. And I normally would not
Full disclosure : This is for a homework assignment. This is driving me nuts.
First, in order to provide full disclosure, I want to point out that this
Is there any way to rotate a disclosure button? I'd like it to point
I would like to use a custom version of the standard disclosure accessory image
First, some disclosure: I am not a Linux admin, and my Linux admin is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.