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?
Applicative functors aren't explicitly modelled in F#, but they're easy enough to add if you need them. F# doesn't have typeclasses, so implementing applicative functors tend to be more on a case-by-case basis.
If we want to make a type constructor an instance of Functor, it has to have a kind of * -> *, which means that it has to take exactly one concrete type as a type parameter. For example, Maybe can be made an instance because it takes one type parameter to produce a concrete type, like Maybe Int or Maybe String.
As a variation, an applicative functor maps objects in an 'elevated world' using functions from the same 'elevated world'. In Haskell, an applicative functor is defined like this:
So what's a functor F: BG → BH F: B G → B H? It's precisely a group homomorphism from G G to H H! So in this example, a functor is just a function (which happens to be compatible with the group structure).
I don't have 50 reputation to comment here, so I'll try to do it as an answer:
however, for any concrete type T, one can make ((,) T) an applicative.
...
There's a theorem in mathematics that any collection with at least 2 elements can be made into a monoid. So for any concrete type T, it could in principle be made a member of Monoid, and then could in principle be made Applicative. What's wrong with this reasoning?
What about the tuple from the uninhabited type? (,) Void
It is a Functor
,right?
Could you derive Applicative
for it? How would pure
be implemented?
There is nice example in reactive-banana library.
It features Event a
types, which represents a single simultaneous event in time (think, an impulse), and Behavior 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With