ekmett / semigroupoids

http://hackage.haskell.org/package/semigroupoids
Other
75 stars 45 forks source link

derive Alt #135

Closed BebeSparkelSparkel closed 7 months ago

BebeSparkelSparkel commented 8 months ago

It would be nice to be able to derive Alt but I get this error

\> import Data.Functor.Alt as A
\> newtype WE a b = WE (Either a b) deriving (Alt)

<interactive>:2:44: error:
    _ Couldn't match type: Either a
                     with: WE a
        arising from the coercion of the method _many_
          from type _forall a1.
                     Applicative (Either a) =>
                     Either a a1 -> Either a [a1]_
            to type _forall a1. Applicative (WE a) => WE a a1 -> WE a [a1]_
    _ When deriving the instance for (Alt (WE a))

<interactive>:2:44: error:
    _ Couldn't match type: Either a
                     with: WE a
        arising from the coercion of the method _some_
          from type _forall a1.
                     Applicative (Either a) =>
                     Either a a1 -> Either a [a1]_
            to type _forall a1. Applicative (WE a) => WE a a1 -> WE a [a1]_
    _ When deriving the instance for (Alt (WE a))
RyanGlScott commented 8 months ago

This is a known GHC limitation: see https://gitlab.haskell.org/ghc/ghc/-/issues/20815.

BebeSparkelSparkel commented 8 months ago

As a stopgap could be

class Alt' f where
  alt :: f a -> f a -> f a
class Alt' f => Alt f where
  (<!>) :: f a -> f a -> f a
  (<!>) = alt
  some ...
  many ...

Now newtypes can basically derive it with

newtype WE a b = WE (Either a b) deriving (Alt')
instance Alt (WE a)
BebeSparkelSparkel commented 8 months ago

@RyanGlScott Looking at the GHC issue it appears that there has not been progress on them lately. Any thoughts on the stopgap?

RyanGlScott commented 7 months ago

My personal two cents (@ekmett may have different thoughts): I'm not keen on changing the class hierarchy just for the sake of working around a GHC limitation. In my experience, rearranging class APIs almost invariably inflicts pain upon downstream users by breaking existing code. I'd like to avoid that situation unless there is a consensus that that is the design that a bulk of the users want.

ekmett commented 7 months ago

I'm with @RyanGlScott on this one.

RyanGlScott commented 1 day ago

Note that https://gitlab.haskell.org/ghc/ghc/-/issues/20815 has been fixed upstream, so it will be possible to derive Alt using GeneralizedNewtypeDeriving in a future GHC release.