The laws as given in the current documentation do rely on parametricity to connect to fmap
.
Without parametricity, we lose that connection, as we cannot even prove the uniqueness of fmap
, and indeed there are models/extensions of System F where fmap
is not unique.
A simple example of breaking parametricity is to add type-case (pattern-matching on types), this allows you to define the following twist
which inspects the type of its argument and flip any boolean it finds:
twist :: forall a. a -> a
twist @Bool = not
twist @(a -> b) = \f -> (\x -> twist @b (f (twist @a x)))
twist @a = id -- default case
It has the type of polymorphic identity, but it is not
the identity function.
You can then define the following "twisted identity" functor, where pure
applies twist
to its argument:
newtype I a = I { runI :: a }
pure :: a -> I a
pure = I . twist
(<*>) :: I (a -> b) -> I a -> I b -- The usual, no twist
(<*>) (I f) (I x) = I (f x)
A key property of twist
is that twist . twist = id
. This allows it to cancel out with itself when composing values embedded using pure
, thus guaranteeing the four laws of Control.Applicative
. (Proof sketch in Coq)
This twisted functor also yields a different definition of fmap
, as \u -> pure f <*> u
. Unfolded definition:
fmap :: (a -> b) -> I a -> I b
fmap f (I x) = I (twist (f (twist x)))
This does not contradict duplode's answer, which translates the usual argument for the uniqueness of the identity of monoids to the setting of monoidal functors, but it undermines its approach. The issue is that view assumes that you have a given functor already and that the monoidal structure is compatible with it. In particular, the law fmap f u = pure f <*> u
is implied from defining pure
as \x -> fmap (const x) funit
(and (<*>)
also accordingly). That argument breaks down if you haven't fixed fmap
in the first place, so you don't have any coherence laws to rely on.
<*>
being not really the “fundamental” method of applicative, i.e. monoidal functors – it's ratherfzip :: f a -> f b -> f (a,b)
. However, I just don't quite get what you mean by “feels like a cheat”. You're just using the laws, how can that be a cheat. – Claimantfmap
(which implies thefmap
/Applicative
law) flows from parametricity. So if you take these laws out of a context where parametricity holds, that raises the question of whetherpure
is still determined by<*>
or whether it's actually determined only by the combination offmap
with<*>
. – Selfpityfzip
andfunit :: f ()
might play into this sort of question. – Selfpity