title: Trying to get bind working for combination of State and Delayed monad
body: <p>If the answer to this is "you are going about it all wrong", by all means let me know a more proper way. I had code which was structured like this:</p>
  
  <pre><code>type Res&lt;'T&gt; =
      | E of (DC -&gt; 'T)
      | V of 'T
  </code></pre>
  
  <p>Now this type was primarily used directly, with a lot of inline code that did manual per-case binding, which is a lot of boilerplate I am trying to get rid of, so I figured I turn it into a computation expression. It wasn't too hard to get <em>map</em>, <em>bind</em> and <em>apply</em> right.</p>
  
  <p>State was carried along through DC but this was cumbersome to get right, so I changed it to the following, which is a common signature I see in discussions with the state monad.</p>
  
  <pre><code>type Res&lt;'T&gt; =
      | E of (DC -&gt; DC * 'T)
      | V of 'T
  </code></pre>
  
  <p>Then I decided to take it to the next level and introduce the Delayed monad (or Eventually or whatever its name is). So I changed my type as follows, making it recursive:</p>
  
  <pre><code>type Res&lt;'T&gt; =
      | E of (DC -&gt; DC * Res&lt;'T&gt;)
      | V of 'T
  </code></pre>
  
  <p>I tried some variants of the following, but keep getting:</p>
  
  <blockquote>
    <p>The resulting type would be infinite when unifying ''a' and 'Res&lt;'a> -> Res&lt;'b>'</p>
  </blockquote>
  
  <p>(usually happens when I do not invoke the <em>return</em>, i.e. the <code>Res.E &lt;|</code> below)</p>
  
  <p>Or I can't seem to get my types right, the following (understandably) throws a type error, but I'm having a blind spot fixing it:</p>
  
  <blockquote>
    <p>This expression was expected to have type 'Res&lt;'a>' but here has type 'DC * Res&lt;'a>'.</p>
  </blockquote>
  
  <pre><code>let rec bind k res =
      match res with
      | Res.V v -&gt; k v      // not delayed, should it be?
      | Res.E e -&gt; 
          Res.E &lt;| fun dc -&gt; bind k (e dc)   // here's my error, on 'e dc'
           //(fun dc -&gt; dc, bind f (e dc))
  bind k res
  </code></pre>
  
  <p>I understand that the signature should be <code>('a -&gt; Res&lt;'b&gt;) -&gt; Res&lt;'a&gt; -&gt; XRes&lt;'b&gt;</code>. The problem comes from getting the recursion right, at least in my head. I'm not even sure why I am feeding the result to <code>Res.E</code>, as in my understanding, the <code>k</code> continuation should already return this type anyway.</p>
  
  <p>Also, I currently have the <em>return</em> as follows (following <a href="https://stevehorsfield.wordpress.com/2009/09/03/f-custom-monad-workflow-development/" rel="nofollow">Steve Horsfield</a>):</p>
  
  <pre><code>let result v = Res.V v
  </code></pre>
  
  <p>But also noticed in some posts (notably the <a href="http://fsharpforfunandprofit.com/monadster/" rel="nofollow">hilarious Frankenfunctor</a>) this:</p>
  
  <pre><code>let result v = Res.E &lt;| fun dc -&gt; dc, Res.V v
  </code></pre>
  
  <p>Maybe I am following a pattern I shouldn't have, but it seemed a good idea at the time, especially in trying to refactor zillions of boilerplate code and replace it with computation expressions, but perhaps I should stick to the direct evaluation, it fit in my head ;).</p>
  
  <p>But I feel like I'm close... Any ideas? </p>