purescript / purescript-control

Common control classes and utility functions
BSD 3-Clause "New" or "Revised" License
56 stars 29 forks source link

Add MonadOr? #35

Closed safareli closed 5 years ago

safareli commented 8 years ago

it would be nice to add MonadOr typeclass:

-- | The `MonadOr` type class has no members of its own but extends
-- | `MonadZero` with an additional law:
-- |
-- | - Left catch: `(return a) <|> b = return a`
class MonadZero m <= MonadOr m

For example IO could be an instance of MonadOr.

But it adding MonadOr will introduce other issue: some structures could conform to MonadOr and MonadPlus and adding empty MonadOr will make it impossible to express that. See example from MonadPlus reform proposal#Instances of both

instance MonadPlus Maybe where
   mplus (Just a) Nothing = Just a
   mplus Nothing (Just a) = Just a
   mplus _ _ = Nothing

instance MonadOr Maybe where
  -- this is same as definition in Alternative
   morelse (Just a) _ = Just a
   morelse _ b = b

instance MonadOr [] where
   morelse [] b = b
   morelse a b = a
instance MonadPlus [] where
  -- this is same as definition in Alternative
   mplus  = (++)

If we add MonadOr it should have function with different name then <|>, haskell proposal uses morelse. but except this, we should also add a function to MonadPlus too, as pointed above some structures supports both (Maybe or List).

Related discussion in Fantasy Land

anilanar commented 7 years ago

Shall this be closed?

hdgarrood commented 5 years ago

Yes, I think we can safely say at this point that this class isn't in enough demand to deserve a spot in the core libraries, so I'm going to close this for now.