Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a principled way to compose two monad transformers if they are of different type, but their underlying monad is of the same type?
    primarykey
    data
    text
    <p>Not much I can do to expand the question. But here is a use case: let's say you have two monad transformers, <code>t</code> and <code>s</code>, transforming over the same monad <code>m</code>:</p> <pre><code>master :: (MonadTrans t, Monad m) =&gt; t m a b slave :: (MonadTrans t, Monad m) =&gt; s m a b </code></pre> <p>And I want to compose <code>master</code> and <code>slave</code> such that they can communicate with each other when <code>m</code> primitives are lifted into <code>t</code> and <code>s</code>. The signature might be:</p> <pre><code>bound :: (MonadTrans t, MonadTrans s, Monad m, Monoid a) =&gt; t m a b -&gt; s m a b -&gt; (...) But what is the type of (...) ? </code></pre> <p>A use case, in sugared notation:</p> <pre><code>master :: Monoid a =&gt; a -&gt; t m a b master a = do a &lt;- lift . send $ (a,False) -- * here master is passing function param to slave ... -- * do some logic with a b &lt;- lift . send $ (mempty,True) -- * master terminates slave, and get back result slave :: Monoid a =&gt; (a -&gt; b) -&gt; s m a b slave g = do (a,end) &lt;- lift receive case end of True -&gt; get &gt;&gt;= \b -&gt; exit b _ -&gt; (modify (++[g a])) &gt;&gt; slave g </code></pre> <p>Update: <code>send</code> and <code>receive</code> are primitives of type <code>m</code>.</p> <p>I apologize if this example looks contrived, or resemble coroutines too much, the spirit of the question really has nothing to do with it so please ignore all similarities. But main point is that monads <code>t</code> and <code>s</code> couldn't be sensibly composed with each other before, but after both wrap some underlying monad <code>m</code>, they now could be composed and run as a single function. As for the type of the composed function, I'm really not sure so some direction is appreciated. Now if this abstraction already exist and I just don't know about it, then that would be best.</p>
    singulars
    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