{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_HADDOCK hide #-}

-- | This module contains all the classes eventually exported by
-- "Control.Functor.Linear". Together with related operations.
module Control.Functor.Linear.Internal.Class
  ( -- * Functors
    Functor (..),
    dataFmapDefault,
    (<$>),
    (<&>),
    (<$),
    void,

    -- * Applicative Functors
    Applicative (..),
    dataPureDefault,

    -- * Monads
    Monad (..),
    MonadFail (..),
    return,
    join,
    ap,
    foldM,
  )
where

import qualified Control.Monad as NonLinear ()
import Data.Functor.Compose
import Data.Functor.Identity
import qualified Data.Functor.Linear.Internal.Applicative as Data
import qualified Data.Functor.Linear.Internal.Functor as Data
import Data.Functor.Sum
import Data.Monoid.Linear hiding (Sum)
import Data.Type.Bool
import Data.Unrestricted.Linear.Internal.Consumable
import GHC.TypeLits
import GHC.Types (Type)
import Generics.Linear
import Prelude.Linear.Generically
import Prelude.Linear.Internal
import Prelude.Linear.Unsatisfiable (Unsatisfiable, unsatisfiable)
import Prelude (Bool (..), String)

-- # Control Functors
-------------------------------------------------------------------------------

-- TODO: explain that the category of linear function is self-enriched, and that
-- this is a hierarchy of enriched monads. In order to have some common
-- vocabulary.

-- There is also room for another type of functor where map has type `(a %1->b)
-- -> f a %1-> f b`. `[]` and `Maybe` are such functors (they are regular
-- (endo)functors of the category of linear functions whereas `LFunctor` are
-- control functors). A Traversable hierarchy would start with non-control
-- functors.

-- TODO: make the laws explicit

-- | Control linear functors. The functor of type
-- @f a@ holds only one value of type @a@ and represents a computation
-- producing an @a@ with an effect. All control functors are data functors,
-- but not all data functors are control functors.
class Data.Functor f => Functor f where
  -- | Map a linear function @g@ over a control functor @f a@.
  -- Note that @g@ is used linearly over the single @a@ in @f a@.
  fmap :: (a %1 -> b) %1 -> f a %1 -> f b

-- | Apply the control @fmap@ over a data functor.
dataFmapDefault :: Functor f => (a %1 -> b) -> f a %1 -> f b
dataFmapDefault :: forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
dataFmapDefault a %1 -> b
f = (a %1 -> b) %1 -> f a %1 -> f b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f

(<$>) :: Functor f => (a %1 -> b) %1 -> f a %1 -> f b
<$> :: forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
(<$>) = (a %1 -> b) %1 -> f a %1 -> f b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap
{-# INLINE (<$>) #-}

infixl 4 <$> -- same fixity as base.<$>

-- |  @
--    ('<&>') = 'flip' 'fmap'
--    @
(<&>) :: Functor f => f a %1 -> (a %1 -> b) %1 -> f b
<&> :: forall (f :: * -> *) a b.
Functor f =>
f a %1 -> (a %1 -> b) %1 -> f b
(<&>) f a
a a %1 -> b
f = a %1 -> b
f (a %1 -> b) %1 -> f a %1 -> f b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
<$> f a
a
{-# INLINE (<&>) #-}

infixl 1 <&> -- same fixity as base.<&>

-- | Linearly typed replacement for the standard '(Prelude.<$)' function.
(<$) :: (Functor f, Consumable b) => a %1 -> f b %1 -> f a
a
a <$ :: forall (f :: * -> *) b a.
(Functor f, Consumable b) =>
a %1 -> f b %1 -> f a
<$ f b
fb = (b %1 -> a) %1 -> f b %1 -> f a
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap (b %1 -> a %1 -> a
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` a
a) f b
fb

infixl 4 <$ -- same fixity as base.<$

-- | Discard a consumable value stored in a control functor.
void :: (Functor f, Consumable a) => f a %1 -> f ()
void :: forall (f :: * -> *) a. (Functor f, Consumable a) => f a %1 -> f ()
void = (a %1 -> ()) %1 -> f a %1 -> f ()
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume

-- # Control Applicatives
-------------------------------------------------------------------------------

-- | Control linear applicative functors. These represent effectful
-- computations that could produce continuations that can be applied with
-- '<*>'.
class (Data.Applicative f, Functor f) => Applicative f where
  {-# MINIMAL pure, ((<*>) | liftA2) #-}

  -- | Inject (and consume) a value into an applicative control functor.
  pure :: a %1 -> f a

  -- | Apply the linear function in a control applicative functor to the value
  -- of type @a@ in another functor. This is essentialy composing two effectful
  -- computations, one that produces a function @f :: a %1-> b@ and one that
  -- produces a value of type @a@ into a single effectful computation that
  -- produces a value of type @b@.
  (<*>) :: f (a %1 -> b) %1 -> f a %1 -> f b
  (<*>) = ((a %1 -> b) %1 -> a %1 -> b)
%1 -> f (a %1 -> b) %1 -> f a %1 -> f b
forall (f :: * -> *) a b c.
Applicative f =>
(a %1 -> b %1 -> c) %1 -> f a %1 -> f b %1 -> f c
liftA2 (a %1 -> b) %1 -> a %1 -> b
forall a (q :: Multiplicity). a %q -> a
id

  infixl 4 <*> -- same fixity as base.<*>

  -- | @liftA2 g@ consumes @g@ linearly as it lifts it
  -- over two functors: @liftA2 g :: f a %1-> f b %1-> f c@.
  liftA2 :: (a %1 -> b %1 -> c) %1 -> f a %1 -> f b %1 -> f c
  liftA2 a %1 -> b %1 -> c
f f a
x f b
y = a %1 -> b %1 -> c
f (a %1 -> b %1 -> c) %1 -> f a %1 -> f (b %1 -> c)
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
<$> f a
x f (b %1 -> c) %1 -> f b %1 -> f c
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
<*> f b
y

-- | Apply the control @pure@ over a data applicative.
dataPureDefault :: Applicative f => a -> f a
dataPureDefault :: forall (f :: * -> *) a. Applicative f => a -> f a
dataPureDefault a
x = a %1 -> f a
forall (f :: * -> *) a. Applicative f => a %1 -> f a
pure a
x

instance Monoid a => Applicative ((,) a) where
  pure :: forall a. a %1 -> (a, a)
pure a
x = (a
forall a. Monoid a => a
mempty, a
x)
  (a
a, a %1 -> b
f) <*> :: forall a b. (a, a %1 -> b) %1 -> (a, a) %1 -> (a, b)
<*> (a
b, a
x) = (a
a a %1 -> a %1 -> a
forall a. Semigroup a => a %1 -> a %1 -> a
<> a
b, a %1 -> b
f a
x)

instance (Monoid a, Monoid b) => Applicative ((,,) a b) where
  pure :: forall a. a %1 -> (a, b, a)
pure a
x = (a
forall a. Monoid a => a
mempty, b
forall a. Monoid a => a
mempty, a
x)
  (a
a1, b
a2, a %1 -> b
f) <*> :: forall a b. (a, b, a %1 -> b) %1 -> (a, b, a) %1 -> (a, b, b)
<*> (a
b1, b
b2, a
x) = (a
a1 a %1 -> a %1 -> a
forall a. Semigroup a => a %1 -> a %1 -> a
<> a
b1, b
a2 b %1 -> b %1 -> b
forall a. Semigroup a => a %1 -> a %1 -> a
<> b
b2, a %1 -> b
f a
x)

instance (Monoid a, Monoid b, Monoid c) => Applicative ((,,,) a b c) where
  pure :: forall a. a %1 -> (a, b, c, a)
pure a
x = (a
forall a. Monoid a => a
mempty, b
forall a. Monoid a => a
mempty, c
forall a. Monoid a => a
mempty, a
x)
  (a
a1, b
a2, c
a3, a %1 -> b
f) <*> :: forall a b.
(a, b, c, a %1 -> b) %1 -> (a, b, c, a) %1 -> (a, b, c, b)
<*> (a
b1, b
b2, c
b3, a
x) = (a
a1 a %1 -> a %1 -> a
forall a. Semigroup a => a %1 -> a %1 -> a
<> a
b1, b
a2 b %1 -> b %1 -> b
forall a. Semigroup a => a %1 -> a %1 -> a
<> b
b2, c
a3 c %1 -> c %1 -> c
forall a. Semigroup a => a %1 -> a %1 -> a
<> c
b3, a %1 -> b
f a
x)

deriving via
  Generically1 Identity
  instance
    Functor Identity

instance Applicative Identity where
  pure :: forall a. a %1 -> Identity a
pure = a %1 -> Identity a
forall a. a -> Identity a
Identity
  Identity a %1 -> b
f <*> :: forall a b. Identity (a %1 -> b) %1 -> Identity a %1 -> Identity b
<*> Identity a
x = b %1 -> Identity b
forall a. a -> Identity a
Identity (a %1 -> b
f a
x)

instance Monad Identity where
  Identity a
x >>= :: forall a b. Identity a %1 -> (a %1 -> Identity b) %1 -> Identity b
>>= a %1 -> Identity b
f = a %1 -> Identity b
f a
x

-- # Control Monads
-------------------------------------------------------------------------------

-- | Control linear monads.
-- A linear monad is one in which you sequence linear functions in a context,
-- i.e., you sequence functions of the form @a %1-> m b@.
class Applicative m => Monad m where
  {-# MINIMAL (>>=) #-}

  -- | @x >>= g@ applies a /linear/ function @g@ linearly (i.e., using it
  -- exactly once) on the value of type @a@ inside the value of type @m a@
  (>>=) :: m a %1 -> (a %1 -> m b) %1 -> m b

  infixl 1 >>= -- same fixity as base.>>=

  (>>) :: m () %1 -> m a %1 -> m a
  m ()
m >> m a
k = m ()
m m () %1 -> (() %1 -> m a) %1 -> m a
forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
>>= (\() -> m a
k)
  infixl 1 >> -- same fixity as base.>>

-- | This class handles pattern-matching failure in do-notation.
-- See "Control.Monad.Fail" for details.
class Monad m => MonadFail m where
  fail :: String -> m a

return :: Monad m => a %1 -> m a
return :: forall (m :: * -> *) a. Monad m => a %1 -> m a
return a
x = a %1 -> m a
forall (f :: * -> *) a. Applicative f => a %1 -> f a
pure a
x
{-# INLINE return #-}

-- | Given an effect-producing computation that produces an effect-producing computation
-- that produces an @a@, simplify it to an effect-producing
-- computation that produces an @a@.
join :: Monad m => m (m a) %1 -> m a
join :: forall (m :: * -> *) a. Monad m => m (m a) %1 -> m a
join m (m a)
action = m (m a)
action m (m a) %1 -> (m a %1 -> m a) %1 -> m a
forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
>>= m a %1 -> m a
forall a (q :: Multiplicity). a %q -> a
id

-- | Use this operator to define Applicative instances in terms of Monad instances.
ap :: Monad m => m (a %1 -> b) %1 -> m a %1 -> m b
ap :: forall (m :: * -> *) a b.
Monad m =>
m (a %1 -> b) %1 -> m a %1 -> m b
ap m (a %1 -> b)
f m a
x = m (a %1 -> b)
f m (a %1 -> b) %1 -> ((a %1 -> b) %1 -> m b) %1 -> m b
forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
>>= (\a %1 -> b
f' -> (a %1 -> b) %1 -> m a %1 -> m b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f' m a
x)

-- | Fold from left to right with a linear monad.
-- This is a linear version of 'NonLinear.foldM'.
foldM :: forall m a b. Monad m => (b %1 -> a %1 -> m b) -> b %1 -> [a] %1 -> m b
foldM :: forall (m :: * -> *) a b.
Monad m =>
(b %1 -> a %1 -> m b) -> b %1 -> [a] %1 -> m b
foldM b %1 -> a %1 -> m b
_ b
i [] = b %1 -> m b
forall (m :: * -> *) a. Monad m => a %1 -> m a
return b
i
foldM b %1 -> a %1 -> m b
f b
i (a
x : [a]
xs) = b %1 -> a %1 -> m b
f b
i a
x m b %1 -> (b %1 -> m b) %1 -> m b
forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
>>= \b
i' -> (b %1 -> a %1 -> m b) -> b %1 -> [a] %1 -> m b
forall (m :: * -> *) a b.
Monad m =>
(b %1 -> a %1 -> m b) -> b %1 -> [a] %1 -> m b
foldM b %1 -> a %1 -> m b
f b
i' [a]
xs

---------------
-- Instances --
---------------

deriving via
  Generically1 ((,) a)
  instance
    Functor ((,) a)

deriving via
  Generically1 ((,,) a b)
  instance
    Functor ((,,) a b)

deriving via
  Generically1 ((,,,) a b c)
  instance
    Functor ((,,,) a b c)

deriving via
  Generically1 ((,,,,) a b c d)
  instance
    Functor ((,,,,) a b c d)

instance Monoid a => Monad ((,) a) where
  (a
a, a
x) >>= :: forall a b. (a, a) %1 -> (a %1 -> (a, b)) %1 -> (a, b)
>>= a %1 -> (a, b)
f = a %1 -> (a, b) %1 -> (a, b)
forall b. a %1 -> (a, b) %1 -> (a, b)
go a
a (a %1 -> (a, b)
f a
x)
    where
      go :: a %1 -> (a, b) %1 -> (a, b)
      go :: forall b. a %1 -> (a, b) %1 -> (a, b)
go a
b1 (a
b2, b
y) = (a
b1 a %1 -> a %1 -> a
forall a. Semigroup a => a %1 -> a %1 -> a
<> a
b2, b
y)

deriving via
  Generically1 (Sum f g)
  instance
    (Functor f, Functor g) => Functor (Sum f g)

deriving via
  Generically1 (Compose f g)
  instance
    (Functor f, Functor g) => Functor (Compose f g)

------------------------
-- Generics instances --
------------------------

instance (Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f) where
  fmap :: forall a b.
(a %1 -> b) %1 -> Generically1 f a %1 -> Generically1 f b
fmap a %1 -> b
f = f b %1 -> Generically1 f b
forall (f :: * -> *) a. f a -> Generically1 f a
Generically1 (f b %1 -> Generically1 f b)
-> (Generically1 f a %1 -> f b)
%1 -> Generically1 f a
%1 -> Generically1 f b
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. Rep1 f b %1 -> f b
forall {k} (f :: k -> *) (p :: k) (m :: Multiplicity).
Generic1 f =>
Rep1 f p %m -> f p
to1 (Rep1 f b %1 -> f b)
-> (Generically1 f a %1 -> Rep1 f b)
%1 -> Generically1 f a
%1 -> f b
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. (a %1 -> b) %1 -> Rep1 f a %1 -> Rep1 f b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f (Rep1 f a %1 -> Rep1 f b)
%1 -> (Generically1 f a %1 -> Rep1 f a)
-> Generically1 f a
%1 -> Rep1 f b
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. f a %1 -> Rep1 f a
forall {k} (f :: k -> *) (p :: k) (m :: Multiplicity).
Generic1 f =>
f p %m -> Rep1 f p
from1 (f a %1 -> Rep1 f a)
-> (Generically1 f a %1 -> f a) -> Generically1 f a %1 -> Rep1 f a
forall b c a (q :: Multiplicity) (m :: Multiplicity)
       (n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. Generically1 f a %1 -> f a
forall (f :: * -> *) a. Generically1 f a %1 -> f a
unGenerically1

-- True if the generic type does not contain 'Par1', i.e. it does not use its parameter.
type family NoPar1 (f :: Type -> Type) :: Bool where
  NoPar1 U1 = 'True
  NoPar1 (K1 i v) = 'True
  NoPar1 (l :*: r) = NoPar1 l && NoPar1 r
  NoPar1 (l :+: r) = NoPar1 l && NoPar1 r
  NoPar1 (l :.: r) = NoPar1 l || NoPar1 r
  NoPar1 (M1 i c f) = NoPar1 f
  NoPar1 Par1 = 'False

-- If the generic type does not use its parameter, we can linearly coerce the parameter to any other type.
class NoPar1 f ~ 'True => Unused f where
  unused :: f a %1 -> f b

instance Unused U1 where
  unused :: forall a b. U1 a %1 -> U1 b
unused U1 a
U1 = U1 b
forall k (p :: k). U1 p
U1

instance Unused (K1 i v) where
  unused :: forall a b. K1 i v a %1 -> K1 i v b
unused (K1 v
c) = v %1 -> K1 i v b
forall k i c (p :: k). c -> K1 i c p
K1 v
c

instance (Unused l, Unused r) => Unused (l :*: r) where
  unused :: forall a b. (:*:) l r a %1 -> (:*:) l r b
unused (l a
l :*: r a
r) = l a %1 -> l b
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused l a
l l b %1 -> r b %1 -> (:*:) l r b
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: r a %1 -> r b
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused r a
r

instance (Unused l, Unused r) => Unused (l :+: r) where
  unused :: forall a b. (:+:) l r a %1 -> (:+:) l r b
unused (L1 l a
l) = l b %1 -> (:+:) l r b
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (l a %1 -> l b
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused l a
l)
  unused (R1 r a
r) = r b %1 -> (:+:) l r b
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (r a %1 -> r b
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused r a
r)

instance Unused f => Unused (M1 i c f) where
  unused :: forall a b. M1 i c f a %1 -> M1 i c f b
unused (M1 f a
a) = f b %1 -> M1 i c f b
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f a %1 -> f b
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused f a
a)

instance (Unused' (NoPar1 l) l r, (NoPar1 l || NoPar1 r) ~ 'True) => Unused (l :.: r) where
  unused :: forall a b. (:.:) l r a %1 -> (:.:) l r b
unused (Comp1 l (r a)
a) = l (r b) %1 -> (:.:) l r b
forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (x :: k1).
f (g x) -> (:.:) f g x
Comp1 (forall (left_unused :: Bool) (l :: * -> *) (r :: * -> *) a b.
Unused' left_unused l r =>
l (r a) %1 -> l (r b)
unused' @(NoPar1 l) l (r a)
a)

class Unused' (left_unused :: Bool) l r where
  unused' :: l (r a) %1 -> l (r b)

instance Unused l => Unused' 'True l r where
  unused' :: forall a b. l (r a) %1 -> l (r b)
unused' = l (r a) %1 -> l (r b)
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused

instance (Functor l, Unused r) => Unused' 'False l r where
  unused' :: forall a b. l (r a) %1 -> l (r b)
unused' = (r a %1 -> r b) %1 -> l (r a) %1 -> l (r b)
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap r a %1 -> r b
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused

-- A linear map on a pair is only possible if only one side uses its parameter.
-- To get the right type, the other side can then be coerced (instead of mapped) using `unused`.
class (noPar1l ~ NoPar1 l, noPar1r ~ NoPar1 r) => EitherNoPar1 (noPar1l :: Bool) (noPar1r :: Bool) l r where
  eitherNoPar1Map :: (a %1 -> b) %1 -> (l :*: r) a %1 -> (l :*: r) b

instance (Unused l, Functor r, NoPar1 r ~ 'False) => EitherNoPar1 'True 'False l r where
  eitherNoPar1Map :: forall a b. (a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
eitherNoPar1Map a %1 -> b
f (l a
l :*: r a
r) = l a %1 -> l b
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused l a
l l b %1 -> r b %1 -> (:*:) l r b
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: (a %1 -> b) %1 -> r a %1 -> r b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f r a
r

instance (Unused r, Functor l, NoPar1 l ~ 'False) => EitherNoPar1 'False 'True l r where
  eitherNoPar1Map :: forall a b. (a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
eitherNoPar1Map a %1 -> b
f (l a
l :*: r a
r) = (a %1 -> b) %1 -> l a %1 -> l b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f l a
l l b %1 -> r b %1 -> (:*:) l r b
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: r a %1 -> r b
forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused r a
r

type MessageMany =
  'Text "Can't derive an instance of Functor. One of the constructors"
    ':$$: 'Text "of your datatype uses the type parameter more than once."

instance ('False ~ NoPar1 l, 'False ~ NoPar1 r, Unsatisfiable MessageMany) => EitherNoPar1 'False 'False l r where
  eitherNoPar1Map :: forall a b. (a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
eitherNoPar1Map = (a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
forall a. Bottom => a
unsatisfiable

type MessageZero =
  'Text "Can't derive an instance of Functor. One of the constructors"
    ':$$: 'Text "of your datatype does not use the type parameter."

instance ('True ~ NoPar1 l, 'True ~ NoPar1 r, Unsatisfiable MessageZero) => EitherNoPar1 'True 'True l r where
  eitherNoPar1Map :: forall a b. (a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
eitherNoPar1Map = (a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
forall a. Bottom => a
unsatisfiable

instance (Functor l, Functor r) => Functor (l :+: r) where
  fmap :: forall a b. (a %1 -> b) %1 -> (:+:) l r a %1 -> (:+:) l r b
fmap a %1 -> b
f (L1 l a
a) = l b %1 -> (:+:) l r b
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 ((a %1 -> b) %1 -> l a %1 -> l b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f l a
a)
  fmap a %1 -> b
f (R1 r a
a) = r b %1 -> (:+:) l r b
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 ((a %1 -> b) %1 -> r a %1 -> r b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f r a
a)

instance Functor f => Functor (M1 j c f) where
  fmap :: forall a b. (a %1 -> b) %1 -> M1 j c f a %1 -> M1 j c f b
fmap a %1 -> b
f (M1 f a
a) = f b %1 -> M1 j c f b
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 ((a %1 -> b) %1 -> f a %1 -> f b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f f a
a)

instance Functor Par1 where
  fmap :: forall a b. (a %1 -> b) %1 -> Par1 a %1 -> Par1 b
fmap a %1 -> b
f (Par1 a
a) = b %1 -> Par1 b
forall p. p -> Par1 p
Par1 (a %1 -> b
f a
a)

instance (Functor f, Functor g) => Functor (f :.: g) where
  fmap :: forall a b. (a %1 -> b) %1 -> (:.:) f g a %1 -> (:.:) f g b
fmap a %1 -> b
f (Comp1 f (g a)
fga) = f (g b) %1 -> (:.:) f g b
forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (x :: k1).
f (g x) -> (:.:) f g x
Comp1 ((g a %1 -> g b) %1 -> f (g a) %1 -> f (g b)
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap ((a %1 -> b) %1 -> g a %1 -> g b
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f) f (g a)
fga)

instance (Data.Functor l, Data.Functor r, EitherNoPar1 b1 b2 l r) => Functor (l :*: r) where
  fmap :: forall a b. (a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
fmap = (a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
forall (noPar1l :: Bool) (noPar1r :: Bool) (l :: * -> *)
       (r :: * -> *) a b.
EitherNoPar1 noPar1l noPar1r l r =>
(a %1 -> b) %1 -> (:*:) l r a %1 -> (:*:) l r b
eitherNoPar1Map