From df626f008230fd55308d8c64d3879d7ec79b44df Mon Sep 17 00:00:00 2001 From: Tobias Haslop <tobiashaslop@hotmail.de> Date: Wed, 1 Nov 2023 01:33:14 +0100 Subject: [PATCH] Elaborate on the quantified superclass of Bifunctor This was requested in the comment https://github.com/haskell/core-libraries-committee/issues/93#issuecomment-1597271700 for when Traversable becomes a superclass of Bitraversable, but similarly applies to Functor/Bifunctor, which already are in a superclass relationship. --- libraries/base/src/Data/Bifunctor.hs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/libraries/base/src/Data/Bifunctor.hs b/libraries/base/src/Data/Bifunctor.hs index d01917ffaa75..e7a776cd995b 100644 --- a/libraries/base/src/Data/Bifunctor.hs +++ b/libraries/base/src/Data/Bifunctor.hs @@ -39,9 +39,23 @@ import GHC.Generics ( K1(..) ) -- Intuitively it is a bifunctor where both the first and second -- arguments are covariant. -- +-- The class definition of a 'Bifunctor' @p@ uses the +-- [QuantifiedConstraints](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/quantified_constraints.html) +-- language extension to quantify over the first type +-- argument @a@ in its context. The context requires that @p a@ +-- must be a 'Functor' for all @a@. In other words a partially +-- applied 'Bifunctor' must be a 'Functor'. This makes 'Functor' a +-- superclass of 'Bifunctor' such that a function with a +-- 'Bifunctor' constraint may use 'fmap' in its implementation. +-- 'Functor' has been a quantified superclass of +-- 'Bifunctor' since base-4.18.0.0. +-- -- You can define a 'Bifunctor' by either defining 'bimap' or by --- defining both 'first' and 'second'. A partially applied 'Bifunctor' --- must be a 'Functor' and the 'second' method must agree with 'fmap'. +-- defining both 'first' and 'second'. The 'second' method must +-- agree with 'fmap': +-- +-- @'second' ≡ 'fmap'@ +-- -- From this it follows that: -- -- @'second' 'id' ≡ 'id'@ @@ -69,8 +83,6 @@ import GHC.Generics ( K1(..) ) -- 'second' (f '.' g) ≡ 'second' f '.' 'second' g -- @ -- --- Since 4.18.0.0 'Functor' is a superclass of 'Bifunctor. --- -- @since 4.8.0.0 class (forall a. Functor (p a)) => Bifunctor p where {-# MINIMAL bimap | first, second #-} -- GitLab