Skip to content

Lazier Semigroup instance for Maybe

Mailing list discussion here: https://mail.haskell.org/pipermail/libraries/2018-May/028818.html

The existing Semigroup instance for Maybe is:

instance Semigroup a => Semigroup (Maybe a) where
    Nothing <> b       = b
    a       <> Nothing = a
    Just a  <> Just b  = Just (a <> b)

It has been proposed that it be replaced by:

instance Semigroup a => Semigroup (Maybe a) where
    Nothing <> b = b
    Just a  <> b = Just (maybe a (a<>) b)

This is lazier in the second argument, making it more consistent with the strictness of the Semigroup instances for And,Or,Ord,Either,Proxy and (). Equivalently, we could write:

instance Semigroup a => Semigroup (Maybe a) where
    Nothing <> b = b
    Just a  <> Nothing = Just a
    Just a  <> Just b  = Just (a <> b)

This makes it a little more clear that we aren't building a closure for partial function application, but it should have the same behavior.

Trac metadata
Trac field Value
Version 8.2.2
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC ekmett
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information