I\'m still getting familiar with all this category theory stuff, and just about every example I see is with a Maybe or an Array. But I haven\'t found any examples that discrimin
Cactus gives a great example of a semigroup that's not a monoid. Non-empty (finite) lists of any type represent the free semigroup over that type. Another example is Data.Void.Void
, which isn't a Monoid
because it doesn't have any elements and therefore doesn't have an identity element. Yet another example is the set of positive integers under addition.
One Functor
that's not an Apply
is Handler
.
data Handler a where
Handler :: Exception e => (e -> IO a) -> Handler a
instance Functor Handler where
fmap f (Handler h) = Handler (\e -> f <$> h e)
Given Handler f :: Handler (a -> b)
and Handler g :: Handler a
, you have
f :: e1 -> IO (a -> b)
g :: e2 -> IO a
Where e1
and e2
are (possibly different) types of exception. You need to create h :: e3 -> IO b
for some exception type e3
. There is no really sensible way to do this**.
It seems harder to find Functor
s that can't be made into law-abiding Apply
instances, because Apply
has just one law and therefore admits all sorts of weird things that Applicative
would reject.
Map k
and IntMap
. Also, (,) a
and Const a
when a
is a Semigroup
but not a Monoid
. Similarly, some other types fit the pattern of accepting a weaker context for Apply
and/or Bind
than for Applicative
or Monad
, respectively.
ZipList
is an Apply
but not a Bind
. I don't know what a Chain
is.
** One semi-sensible way might look like this:
data P x y = P x y deriving (Show, Typeable)
instance (Exception x, Exception y) =>
Exception (P x y)
instance Apply Handler where
Handler f <.> Handler g =
Handler (\(P e1 e2) -> f e1 <*> g e2)
I think this obeys the Apply
law, but I'm not yet completely certain.
Non-empty lists, defined as data NEList a = Cons a [a]
are semigroups but not monoids.