title: F# too many calls to nested Computation Expression
body: <p>This questions is an evolution of <a href="https://stackoverflow.com/questions/66702741/f-saying-value-not-defined-in-computation-expression">this question</a>. I am trying to find out why when I run <code>State.exec</code> on the CE that I appear to be getting undesired nesting behavior of the CEs. It seems to be calling them many times. Here is what I have:</p>
  <pre class="lang-ml prettyprint-override"><code>type State&lt;'a, 's&gt; = ('s -&gt; 'a * 's)
  
  module State =
      // Explicit
      // let result x : State&lt;'a, 's&gt; = fun s -&gt; x, s
      // Less explicit but works better with other, existing functions:
      let result x s = 
          x, s
  
      let bind (f:'a -&gt; State&lt;'b, 's&gt;) (m:State&lt;'a, 's&gt;) : State&lt;'b, 's&gt; =
          // return a function that takes the state
          fun s -&gt;
              // Get the value and next state from the m parameter
              let a, s' = m s
              // Get the next state computation by passing a to the f parameter
              let m' = f a
              // Apply the next state to the next computation
              m' s'
  
      /// Evaluates the computation, returning the result value.
      let eval (m:State&lt;'a, 's&gt;) (s:'s) = 
          m s 
          |&gt; fst
  
      /// Executes the computation, returning the final state.
      let exec (m:State&lt;'a, 's&gt;) (s:'s) = 
          m s
          |&gt; snd
  
      /// Returns the state as the value.
      let getState (s:'s) = 
          s, s
  
      /// Ignores the state passed in favor of the provided state value.
      let setState (s:'s) = 
          fun _ -&gt; 
              (), s
  
  
  type StateBuilder() =
      member __.Return(value) : State&lt;'a, 's&gt; = 
          State.result value
      member __.Bind(m:State&lt;'a, 's&gt;, f:'a -&gt; State&lt;'b, 's&gt;) : State&lt;'b, 's&gt; = 
          State.bind f m
      member __.ReturnFrom(m:State&lt;'a, 's&gt;) = 
          m
      member __.Zero() =
          State.result ()
      member __.Delay(f) = 
          State.bind f (State.result ())
  
  
  let rng = System.Random(123)
  type StepId = StepId of int
  type Food =
      | Chicken
      | Rice
  type Step =
    | GetFood of StepId * Food
    | Eat of StepId * Food
    | Sleep of StepId * duration:int
  type PlanAcc = PlanAcc of lastStepId:StepId * steps:Step list
  
  let state = StateBuilder()
  
  let getFood =
      state {
          printfn &quot;GetFood&quot;
          let randomFood = 
              if rng.NextDouble() &gt; 0.5 then Food.Chicken
              else Food.Rice
          let! (PlanAcc (StepId lastStepId, steps)) = State.getState
          let nextStepId = StepId (lastStepId + 1)
          let newStep = GetFood (nextStepId, randomFood)
          let newAcc = PlanAcc (nextStepId, newStep::steps)
          do! State.setState newAcc
          return randomFood
      }
  
  type StateBuilder with
  
      [&lt;CustomOperation(&quot;sleep&quot;, MaintainsVariableSpaceUsingBind=true)&gt;]
      member this.Sleep (st:State&lt;_,PlanAcc&gt;, [&lt;ProjectionParameter&gt;] (duration: 'a -&gt; int)) =
          printfn $&quot;Sleep&quot;
          let program d =
              state {
                  let! x = st
                  printfn &quot;Sleep: %A&quot; duration
                  let! (PlanAcc (StepId lastStepId, steps)) = State.getState
                  let nextStepId = StepId (lastStepId + 1)
                  let newStep = Sleep (nextStepId, d)
                  let newAcc = PlanAcc (nextStepId, newStep::steps)
                  do! State.setState newAcc
                  return x 
              }
  
          State.bind (fun x -&gt; program (duration x)) st
  
  
      [&lt;CustomOperation(&quot;eat&quot;, MaintainsVariableSpaceUsingBind=true)&gt;]
      member this.Eat (st:State&lt;_,PlanAcc&gt;, [&lt;ProjectionParameter&gt;] (food: 'a -&gt; Food)) =
          printfn $&quot;Eat&quot;
          let program e =
              state {
                  let! x = st
                  printfn &quot;Eat: %A&quot; food
                  let! (PlanAcc (StepId lastStepId, steps)) = State.getState
                  let nextStepId = StepId (lastStepId + 1)
                  let newStep = Eat (nextStepId, e)
                  let newAcc = PlanAcc (nextStepId, newStep::steps)
                  do! State.setState newAcc
                  return x
              }
          State.bind (fun x -&gt; program (food x)) st
  
  
  let simplePlan =
      state {
          let! f1 = getFood
          sleep 1
          eat f1
          sleep 2
          eat f1
          sleep 3
      }
  
  let initalAcc = PlanAcc(StepId 0, [])
  
  let x = State.exec simplePlan initalAcc
  </code></pre>
  <p>Here is what I expect to get for <code>x</code>:</p>
  <pre><code>&gt; x;;
  val it : PlanAcc =
  PlanAcc
  (StepId 6,
    [Sleep (StepId 6, 3); GetFood (StepId 5, Chicken);
        Sleep (StepId 4, Chicken); EatFood (StepId 3, Chicken);
        Sleep (StepId 2, 1); GetFood (StepId 1, Chicken)])
  </code></pre>
  <p>Here is what I get:</p>
  <pre><code>&gt; x;;
  val it : PlanAcc =
    PlanAcc
      (StepId 63,
       [Sleep (StepId 63, 3); Eat (StepId 62, Rice); Sleep (StepId 61, 2);
        Eat (StepId 60, Rice); Sleep (StepId 59, 1);
        GetFood (StepId 58, Chicken); GetFood (StepId 57, Chicken);
        Sleep (StepId 56, 1); GetFood (StepId 55, Rice);
        GetFood (StepId 54, Chicken); Eat (StepId 53, Chicken);
        Sleep (StepId 52, 1); GetFood (StepId 51, Chicken);
        GetFood (StepId 50, Chicken); Sleep (StepId 49, 1);
        GetFood (StepId 48, Chicken); GetFood (StepId 47, Chicken);
        Sleep (StepId 46, 2); Eat (StepId 45, Rice); Sleep (StepId 44, 1);
        GetFood (StepId 43, Rice); GetFood (StepId 42, Chicken);
        Sleep (StepId 41, 1); GetFood (StepId 40, Rice);
        GetFood (StepId 39, Rice); Eat (StepId 38, Rice); Sleep (StepId 37, 1);
        GetFood (StepId 36, Chicken); GetFood (StepId 35, Rice);
        Sleep (StepId 34, 1); GetFood (StepId 33, Rice);
        GetFood (StepId 32, Chicken); Eat (StepId 31, Rice);
        Sleep (StepId 30, 2); Eat (StepId 29, Rice); Sleep (StepId 28, 1);
        GetFood (StepId 27, Chicken); GetFood (StepId 26, Rice);
        Sleep (StepId 25, 1); GetFood (StepId 24, Rice);
        GetFood (StepId 23, Rice); Eat (StepId 22, Chicken);
        Sleep (StepId 21, 1); GetFood (StepId 20, Rice);
        GetFood (StepId 19, Chicken); Sleep (StepId 18, 1);
        GetFood (StepId 17, Chicken); GetFood (StepId 16, Rice);
        Sleep (StepId 15, 2); Eat (StepId 14, Rice); Sleep (StepId 13, 1);
        GetFood (StepId 12, Rice); GetFood (StepId 11, Rice);
        Sleep (StepId 10, 1); GetFood (StepId 9, Rice);
        GetFood (StepId 8, Chicken); Eat (StepId 7, Chicken);
        Sleep (StepId 6, 1); GetFood (StepId 5, Chicken);
        GetFood (StepId 4, Chicken); Sleep (StepId 3, 1);
        GetFood (StepId 2, Chicken); GetFood (StepId 1, Chicken)])
  </code></pre>
  <p>I'm fairly certain it has to do with how the <code>State</code> is being bound in the <code>program</code> CEs since I don't have a problem if I just call <code>let! f = getFood</code> a bunch of times.</p>
  <p>I tried removing the <code>let! x = st</code> and <code>return x</code> calls in the <code>program</code> CEs thinking that was what was causing the issue, but the compiler complained in the <code>simplePlan</code> CE saying, &quot;The expression was expected to have type 'Food' but here has type 'unit'&quot;.</p>
  <p>Image of error:
  <a href="https://i.stack.imgur.com/Hk8zZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hk8zZ.png" alt="enter image description here" /></a></p>