Re: [Haskell-cafe] Haskell-Cafe Digest, Vol 124, Issue 39

On Sat, Dec 28, 2013 at 5:13 PM, Tom Ellis <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk> wrote:
data Wrap m n a = Wrap (m (n a))
This is Compose [0] from traversable. It doesn't have a Monad instance, though...
Erik
[0] http://hackage.haskell.org/package/transformers-0.3.0.0/docs/Data-Functor-Co... According to [1], Wrap/Compose does indeed have a Monad instance terms of traverse (here called 'swap'). As far as I understand the paper includes a proof that all the Monad laws holds for such as composition, however there is no mention of transformers as the paper predates them. Personally I am not sure that the transformer composition is useful.
Would it be as simple as adding this instance to Data.Functor.Compose:
instance (Functor m, Traversable n, Monad m, Monad n) => Monad (Compose m n)
or can anyone spot a problem with this approach? Hans [1]: http://web.cecs.pdx.edu/~mpj/pubs/RR-1004.pdf, page 9 [2]: http://strictlypositive.org/IdiomLite.pdf

On 29 dec 2013, at 18:40, Hans Höglund wrote:
On Sat, Dec 28, 2013 at 5:13 PM, Tom Ellis <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk> wrote:
data Wrap m n a = Wrap (m (n a))
This is Compose [0] from traversable. It doesn't have a Monad instance, though...
Erik
[0] http://hackage.haskell.org/package/transformers-0.3.0.0/docs/Data-Functor-Co... According to [1], Wrap/Compose does indeed have a Monad instance terms of traverse (here called 'swap'). As far as I understand the paper includes a proof that all the Monad laws holds for such as composition, however there is no mention of transformers as the paper predates them. Personally I am not sure that the transformer composition is useful.
Would it be as simple as adding this instance to Data.Functor.Compose:
instance (Functor m, Traversable n, Monad m, Monad n) => Monad (Compose m n)
or can anyone spot a problem with this approach?
Hans
[1]: http://web.cecs.pdx.edu/~mpj/pubs/RR-1004.pdf, page 9 [2]: http://strictlypositive.org/IdiomLite.pdf

On Sun, Dec 29, 2013 at 06:50:15PM +0100, Hans Höglund wrote:
According to [1], Wrap/Compose does indeed have a Monad instance terms of traverse (here called 'swap'). As far as I understand the paper includes a proof that all the Monad laws holds for such as composition
[1]: http://web.cecs.pdx.edu/~mpj/pubs/RR-1004.pdf, page 9
Careful, they mention in Section 6.4 that "this construction only yields a composite monad if m has a certain commutativity property". This is the same reason that 'ListT m' is not a monad unless 'm' is commutative. What is your use case for these combinators? Perhaps you are using them exactly in a commutative case. Tom

On Sun, Dec 29, 2013 at 07:06:26PM +0000, Tom Ellis wrote:
On Sun, Dec 29, 2013 at 06:50:15PM +0100, Hans Höglund wrote:
According to [1], Wrap/Compose does indeed have a Monad instance terms of traverse (here called 'swap'). As far as I understand the paper includes a proof that all the Monad laws holds for such as composition
[1]: http://web.cecs.pdx.edu/~mpj/pubs/RR-1004.pdf, page 9
Careful, they mention in Section 6.4 that "this construction only yields a composite monad if m has a certain commutativity property".
This is the same reason that 'ListT m' is not a monad unless 'm' is commutative.
What is your use case for these combinators? Perhaps you are using them exactly in a commutative case.
Having thought about your original question some more, I think the best approach would be to use a free monad like this import Control.Monad.Free data F m n a = L (m a) | R (n a) type Wrap m n = Free (F m n) so then 'Wrap m n' genuinely is a monad, and you can write a function condense :: (Monad m, Monad n, Traversable n, ...) => Wrap m n a -> m (n a) to collect up the results at the end. Then we need not bother to check whether anything satisfies any laws. 'condense' is similar to your original 'mjoin'. Tom (PS This is overkill for the current discussion, but: since you're starting with 'm' and 'n' monads anyway, using a free monad transformer (Control.Monad.Trans.Free) would probably reduce the boilerplate needed in 'condense'.)
participants (2)
-
Hans Höglund
-
Tom Ellis