Why does the WriterT Monad instance require an implicit Monoid and not just a Semigroup?

17 views
Skip to first unread message

Przemysław Pokrywka

unread,
Sep 27, 2017, 6:33:23 AM9/27/17
to scalaz
Hello,

I've set up some monad transformer stack involving WriterT, using some custom data type as the Writer "log".
I've provided a Semigroup instance for that "log" data type.
When I try to call flatMap, the compiler tells me, that there's no implicit Monad instance for my stack type.
The issue disappears when I provide a Monoid for my custom "log" type (instead of the Semigroup).
I think this is related to the following code fragment: https://github.com/scalaz/scalaz/blob/5d6fbf7d4d567cb736a4cc2d46fc9edc071cc5cf/core/src/main/scala/scalaz/WriterT.scala#L192
and I started to wonder, why is the Monoid instance required - wouldn't just Semigroup be sufficient?

Regards,
Przemek

Tony Morris

unread,
Sep 27, 2017, 6:53:12 AM9/27/17
to sca...@googlegroups.com

You need Monoid to write the monad instance for Writer. What would pure look like without the Monoid identity?

However, you only need Semigroup to use flatMap (the Bind typeclass). The Writer instance for Bind should only require Semigroup.

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaz+un...@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.
Visit this group at https://groups.google.com/group/scalaz.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Tomas Mikula

unread,
Sep 27, 2017, 8:03:33 AM9/27/17
to sca...@googlegroups.com
And here is a proof that Semigroup is sufficient for flatMap:


On Wed, Sep 27, 2017 at 12:53 PM, Tony Morris <tonym...@gmail.com> wrote:

You need Monoid to write the monad instance for Writer. What would pure look like without the Monoid identity?

However, you only need Semigroup to use flatMap (the Bind typeclass). The Writer instance for Bind should only require Semigroup.


On 27/09/17 20:30, Przemysław Pokrywka wrote:
Hello,

I've set up some monad transformer stack involving WriterT, using some custom data type as the Writer "log".
I've provided a Semigroup instance for that "log" data type.
When I try to call flatMap, the compiler tells me, that there's no implicit Monad instance for my stack type.
The issue disappears when I provide a Monoid for my custom "log" type (instead of the Semigroup).
I think this is related to the following code fragment: https://github.com/scalaz/scalaz/blob/5d6fbf7d4d567cb736a4cc2d46fc9edc071cc5cf/core/src/main/scala/scalaz/WriterT.scala#L192
and I started to wonder, why is the Monoid instance required - wouldn't just Semigroup be sufficient?

Regards,
Przemek

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaz+unsubscribe@googlegroups.com.

To post to this group, send email to sca...@googlegroups.com.
Visit this group at https://groups.google.com/group/scalaz.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaz+unsubscribe@googlegroups.com.

Przemysław Pokrywka

unread,
Sep 28, 2017, 5:45:26 AM9/28/17
to scalaz
Thanks Tomas, thanks Tony, obviously it makes sense now.
I suspect that the Monad (not just Bind) was required in my case for other reason (the WriterT is just a part of my monad transformer stack and another monad transformer requires a nomen-omen Monad), but would need to check.

Cheers!


On Wednesday, September 27, 2017 at 1:03:33 PM UTC+1, Tomas Mikula wrote:
And here is a proof that Semigroup is sufficient for flatMap:

On Wed, Sep 27, 2017 at 12:53 PM, Tony Morris <tonym...@gmail.com> wrote:

You need Monoid to write the monad instance for Writer. What would pure look like without the Monoid identity?

However, you only need Semigroup to use flatMap (the Bind typeclass). The Writer instance for Bind should only require Semigroup.


On 27/09/17 20:30, Przemysław Pokrywka wrote:
Hello,

I've set up some monad transformer stack involving WriterT, using some custom data type as the Writer "log".
I've provided a Semigroup instance for that "log" data type.
When I try to call flatMap, the compiler tells me, that there's no implicit Monad instance for my stack type.
The issue disappears when I provide a Monoid for my custom "log" type (instead of the Semigroup).
I think this is related to the following code fragment: https://github.com/scalaz/scalaz/blob/5d6fbf7d4d567cb736a4cc2d46fc9edc071cc5cf/core/src/main/scala/scalaz/WriterT.scala#L192
and I started to wonder, why is the Monoid instance required - wouldn't just Semigroup be sufficient?

Regards,
Przemek

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaz+un...@googlegroups.com.

To post to this group, send email to sca...@googlegroups.com.
Visit this group at https://groups.google.com/group/scalaz.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaz+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages