This question already has an answer here:
From functors that are not applicatives:
A type constructor which is a Functor but not an Applicative. A simple example is a pair:
instance Functor ((,) r) where fmap f (x,y) = (x, f y)
But there is no way how to define its
Applicative
instance without imposing additional restrictions onr
. In particular, there is no way how to definepure :: a -> (r, a)
for an arbitraryr
.
Here, pure
fails to be definable for all types at once; however, for any concrete type T
, one can make ((,) T)
an applicative.
Question: Is there an example of a concrete functor (i.e., no type variables involved) that is a functor but not an applicative?
There is nice example in reactive-banana library.
It features
Event a
types, which represents a single simultaneous event in time (think, an impulse), andBehavior a
type, which represents a value that is available in any moment (for instance, emitting a value from the last event).Behavior is an Applicative, because you can combine two of them - they have a value in any point of time.
Event, though, is a Functor only, because you can't combine them. Given two
Event
s you can't be sure they will happen simultaneosly.The "in principle" doesn't necessarily translate into code. Consider:
As far as I can see, there is no meaningful
Monoid
instance forUserID
. In principle, one might use0
and(+)
on the underlyingInteger
, but that would amount to leaking an implementation detail for no good reason (and which is likely end up being hidden by making the type abstract). That being so,(,) UserID
would be a perfectly good example of aFunctor
which is not anApplicative
.I don't have 50 reputation to comment here, so I'll try to do it as an answer:
...
What about the tuple from the uninhabited type?
(,) Void
It is a
Functor
,right?Could you derive
Applicative
for it? How wouldpure
be implemented?