Note that there are some explanatory texts on larger screens.

plurals
  1. POIterated substitution in Haskell via the Identity Monad
    primarykey
    data
    text
    <p>As far as I can tell, one of the big uses of the monadic bind operation is to do variable substitution such as</p> <pre><code>do x &lt;- maybeComputeSomething y &lt;- maybeComputeSomethingElse maybeDoStuff x y </code></pre> <p>This is all well and good if maybeComputeSomething, maybeComputeSomethingElse, and maybeDoStuff all return Maybe values, but I feel like this form of iterated substitution would be useful in computations even if there wasn't the possibility of a returned Nothing. Since the identity is a monad, I would in some sense expect to be able to do something such as</p> <pre><code>do x &lt;- computeSomething y &lt;- computeSomethingElse doStuff x y </code></pre> <p>which would expand to something looking like</p> <pre><code>computeSomething &gt;&gt;= (\x -&gt; computeSomethingElse &gt;&gt;= (\y -&gt; doStuff x y)) </code></pre> <p>and since bind in the identity monad is simply x >>= f = f x, this would act as</p> <pre><code>doStuff computeSomething computeSomethingElse </code></pre> <p>if >>= were treated as the identity monad's bind, but this (unsurprisingly) fails, because after all I never explicitly stated a monad (unlike the Maybe example when the types are clearly of the form Maybe a) and if Haskell assumed the identity monad everywhere the majority of cases would get cluttered up quite fast by the required disambiguations.</p> <p>So this leads me to my question. I want some code that feels like</p> <pre><code>do x &lt;- computeSomething y &lt;- computeSomethingElse doStuff x y </code></pre> <p>and that does the same thing as</p> <pre><code>doStuff computeSomething computeSomthingElse </code></pre> <p>I understand that I could do this by explicitly defining an Id monad which looks like Maybe except without the Nothing possibility, but this takes types a -> Id a, and so doesn't really define the identity monad. Furthermore, my functions now have to have types a -> Id b, whereas I would really like them to still be of the form a -> b. Is there a way to create the repeated substitution feel without introducing complexity in the types involved?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload