{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_HADDOCK hide #-}

module Data.Functor.Linear.Internal.Applicative
  ( Applicative (..),
    genericPure,
    genericLiftA2,
  )
where

import qualified Control.Monad.Trans.Reader as NonLinear
import Data.Functor.Compose
import Data.Functor.Const
import Data.Functor.Identity
import Data.Functor.Linear.Internal.Functor
import Data.Functor.Product
import Data.Monoid (Ap (..))
import Data.Monoid.Linear hiding (Product)
import Data.Unrestricted.Linear.Internal.Ur (Ur (..))
import GHC.TypeLits
import Generics.Linear
import Prelude.Linear.Generically
import Prelude.Linear.Internal
import Prelude.Linear.Unsatisfiable
import qualified Prelude

-- # Applicative definition
-------------------------------------------------------------------------------

-- | Data 'Applicative'-s can be seen as containers which can be zipped
-- together. A prime example of data 'Applicative' are vectors of known length
-- ('ZipList's would be, if it were not for the fact that zipping them together
-- drops values, which we are not allowed to do in a linear container).
--
-- In fact, an applicative functor is precisely a functor equipped with (pure
-- and) @liftA2 :: (a %1-> b %1-> c) -> f a %1-> f b %1-> f c@. In the case where
-- @f = []@, the signature of 'liftA2' would specialise to that of 'zipWith'.
--
-- Intuitively, the type of 'liftA2' means that 'Applicative's can be seen as
-- containers whose "number" of elements is known at compile-time. This
-- includes vectors of known length but excludes 'Maybe', since this may
-- contain either zero or one value.  Similarly, @((->) r)@ forms a Data
-- 'Applicative', since this is a (possibly infinitary) container indexed by
-- @r@, while lists do not, since they may contain any number of elements.
--
-- == Remarks for the mathematically inclined
--
-- An 'Applicative' is, as in the restricted case, a lax monoidal endofunctor of
-- the category of linear types. That is, it is equipped with
--
-- * a (linear) function @() %1-> f ()@
-- * a (linear) natural transformation @(f a, f b) %1-> f (a, b)@
--
-- It is a simple exercise to verify that these are equivalent to the definition
-- of 'Applicative'. Hence that the choice of linearity of the various arrow is
-- indeed natural.
class Functor f => Applicative f where
  {-# MINIMAL pure, (liftA2 | (<*>)) #-}
  pure :: a -> f a
  (<*>) :: f (a %1 -> b) %1 -> f a %1 -> f b
  infixl 4 <*> -- same fixity as base.<*>
  f (a %1 -> b)
f <*> f a
x = ((a %1 -> b) %1 -> a %1 -> b) -> f (a %1 -> b) %1 -> f a %1 -> f b
forall (f :: * -> *) a b c.
Applicative f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
liftA2 (a %1 -> b) %1 -> a %1 -> b
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
($) f (a %1 -> b)
f f a
x
  liftA2 :: (a %1 -> b %1 -> c) -> 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) -> f a %1 -> f (b %1 -> c)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> 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

-- # Instances
-------------------------------------------------------------------------------

deriving via
  Generically1 (Const x)
  instance
    Monoid x => Applicative (Const x)

deriving via
  Generically1 Ur
  instance
    Applicative Ur

deriving via
  Generically1 ((,) a)
  instance
    Monoid a => Applicative ((,) a)

deriving via
  Generically1 (Product f g)
  instance
    (Applicative f, Applicative g) => Applicative (Product f g)

deriving via
  Generically1 (f :*: g)
  instance
    (Applicative f, Applicative g) => Applicative (f :*: g)

deriving via
  Generically1 ((,,) a b)
  instance
    (Monoid a, Monoid b) => Applicative ((,,) a b)

deriving via
  Generically1 ((,,,) a b c)
  instance
    (Monoid a, Monoid b, Monoid c) => Applicative ((,,,) a b c)

deriving via
  Generically1 Identity
  instance
    Applicative Identity

instance (Applicative f, Applicative g) => Applicative (Compose f g) where
  pure :: forall a. a -> Compose f g a
pure a
x = f (g a) -> Compose f g a
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (g a -> f (g a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> g a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x))
  (Compose f (g (a %1 -> b))
f) <*> :: forall a b.
Compose f g (a %1 -> b) %1 -> Compose f g a %1 -> Compose f g b
<*> (Compose f (g a)
x) = f (g b) %1 -> Compose f g b
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose ((g (a %1 -> b) %1 -> g a %1 -> g b)
-> f (g (a %1 -> b)) %1 -> f (g a) %1 -> f (g b)
forall (f :: * -> *) a b c.
Applicative f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
liftA2 g (a %1 -> b) %1 -> g a %1 -> g b
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
(<*>) f (g (a %1 -> b))
f f (g a)
x)
  liftA2 :: forall a b c.
(a %1 -> b %1 -> c)
-> Compose f g a %1 -> Compose f g b %1 -> Compose f g c
liftA2 a %1 -> b %1 -> c
f (Compose f (g a)
x) (Compose f (g b)
y) = f (g c) %1 -> Compose f g c
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose ((g a %1 -> g b %1 -> g c) -> f (g a) %1 -> f (g b) %1 -> f (g c)
forall (f :: * -> *) a b c.
Applicative f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
liftA2 ((a %1 -> b %1 -> c) -> g a %1 -> g b %1 -> g c
forall (f :: * -> *) a b c.
Applicative f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
liftA2 a %1 -> b %1 -> c
f) f (g a)
x f (g b)
y)

instance Applicative m => Applicative (NonLinear.ReaderT r m) where
  pure :: forall a. a -> ReaderT r m a
pure a
x = (r -> m a) -> ReaderT r m a
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
NonLinear.ReaderT (\r
_ -> a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
  NonLinear.ReaderT r -> m (a %1 -> b)
f <*> :: forall a b.
ReaderT r m (a %1 -> b) %1 -> ReaderT r m a %1 -> ReaderT r m b
<*> NonLinear.ReaderT r -> m a
x = (r -> m b) %1 -> ReaderT r m b
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
NonLinear.ReaderT (\r
r -> r -> m (a %1 -> b)
f r
r m (a %1 -> b) %1 -> m a %1 -> m b
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
<*> r -> m a
x r
r)

instance (Applicative f, Semigroup a) => Semigroup (Ap f a) where
  (Ap f a
x) <> :: Ap f a %1 -> Ap f a %1 -> Ap f a
<> (Ap f a
y) = f a %1 -> Ap f a
forall {k} (f :: k -> *) (a :: k). f a -> Ap f a
Ap (f a %1 -> Ap f a) -> f a %1 -> Ap f a
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ (a %1 -> a %1 -> a) -> f a %1 -> f a %1 -> f a
forall (f :: * -> *) a b c.
Applicative f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
liftA2 a %1 -> a %1 -> a
forall a. Semigroup a => a %1 -> a %1 -> a
(<>) f a
x f a
y

instance (Applicative f, Monoid a) => Monoid (Ap f a) where
  mempty :: Ap f a
mempty = f a -> Ap f a
forall {k} (f :: k -> *) (a :: k). f a -> Ap f a
Ap (f a -> Ap f a) -> f a -> Ap f a
forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty

-- ----------------
-- Generic deriving
-- ----------------

instance
  (Generic1 f, Functor (Rep1 f), GApplicative ('ShowType f) (Rep1 f)) =>
  Applicative (Generically1 f)
  where
  pure :: forall a. a -> Generically1 f a
pure = f a -> Generically1 f a
forall (f :: * -> *) a. f a -> Generically1 f a
Generically1 (f a -> Generically1 f a) -> (a -> f a) -> a -> Generically1 f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. a -> f a
forall (f :: * -> *) a.
(Generic1 f, GApplicative ('ShowType f) (Rep1 f)) =>
a -> f a
genericPure
  liftA2 :: forall a b c.
(a %1 -> b %1 -> c)
-> Generically1 f a %1 -> Generically1 f b %1 -> Generically1 f c
liftA2 a %1 -> b %1 -> c
f (Generically1 f a
x) (Generically1 f b
y) = f c %1 -> Generically1 f c
forall (f :: * -> *) a. f a -> Generically1 f a
Generically1 ((a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
forall (f :: * -> *) a b c.
(Generic1 f, GApplicative ('ShowType f) (Rep1 f)) =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
genericLiftA2 a %1 -> b %1 -> c
f f a
x f b
y)

genericPure ::
  forall f a.
  (Generic1 f, GApplicative ('ShowType f) (Rep1 f)) =>
  a ->
  f a
genericPure :: forall (f :: * -> *) a.
(Generic1 f, GApplicative ('ShowType f) (Rep1 f)) =>
a -> f a
genericPure = Rep1 f a -> f a
forall {k} (f :: k -> *) (p :: k) (m :: Multiplicity).
Generic1 f =>
Rep1 f p %m -> f p
to1 (Rep1 f a -> f a) -> (a -> Rep1 f a) -> a -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (s :: ErrorMessage) (f :: * -> *) a.
GApplicative s f =>
a -> f a
gpure @('ShowType f)

genericLiftA2 ::
  forall f a b c.
  (Generic1 f, GApplicative ('ShowType f) (Rep1 f)) =>
  (a %1 -> b %1 -> c) ->
  f a %1 ->
  f b %1 ->
  f c
genericLiftA2 :: forall (f :: * -> *) a b c.
(Generic1 f, GApplicative ('ShowType f) (Rep1 f)) =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
genericLiftA2 a %1 -> b %1 -> c
f f a
x f b
y = Rep1 f c %1 -> f c
forall {k} (f :: k -> *) (p :: k) (m :: Multiplicity).
Generic1 f =>
Rep1 f p %m -> f p
to1 (forall (s :: ErrorMessage) (f :: * -> *) a b c.
GApplicative s f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
gliftA2 @('ShowType f) a %1 -> b %1 -> c
f (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
x) (f b %1 -> Rep1 f b
forall {k} (f :: k -> *) (p :: k) (m :: Multiplicity).
Generic1 f =>
f p %m -> Rep1 f p
from1 f b
y))

class GApplicative (s :: ErrorMessage) f where
  gpure :: a -> f a
  gliftA2 :: (a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c

instance
  Unsatisfiable
    ( 'Text "Cannot derive a data Applicative instance for" ':$$: s
        ':$$: 'Text "because empty types cannot implement pure."
    ) =>
  GApplicative s V1
  where
  gpure :: forall a. a -> V1 a
gpure = a -> V1 a
forall a. Bottom => a
unsatisfiable
  gliftA2 :: forall a b c. (a %1 -> b %1 -> c) -> V1 a %1 -> V1 b %1 -> V1 c
gliftA2 = (a %1 -> b %1 -> c) -> V1 a %1 -> V1 b %1 -> V1 c
forall a. Bottom => a
unsatisfiable

instance GApplicative s U1 where
  gpure :: forall a. a -> U1 a
gpure a
_ = U1 a
forall k (p :: k). U1 p
U1
  gliftA2 :: forall a b c. (a %1 -> b %1 -> c) -> U1 a %1 -> U1 b %1 -> U1 c
gliftA2 a %1 -> b %1 -> c
_ U1 a
U1 U1 b
U1 = U1 c
forall k (p :: k). U1 p
U1
  {-# INLINE gpure #-}
  {-# INLINE gliftA2 #-}

instance GApplicative s f => GApplicative s (M1 i c f) where
  gpure :: forall a. a -> M1 i c f a
gpure = f a -> M1 i c f a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f a -> M1 i c f a) -> (a -> f a) -> a -> M1 i c f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (s :: ErrorMessage) (f :: * -> *) a.
GApplicative s f =>
a -> f a
gpure @s
  gliftA2 :: forall a b c.
(a %1 -> b %1 -> c) -> M1 i c f a %1 -> M1 i c f b %1 -> M1 i c f c
gliftA2 a %1 -> b %1 -> c
f (M1 f a
x) (M1 f b
y) = f c %1 -> M1 i c f c
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (forall (s :: ErrorMessage) (f :: * -> *) a b c.
GApplicative s f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
gliftA2 @s a %1 -> b %1 -> c
f f a
x f b
y)
  {-# INLINE gpure #-}
  {-# INLINE gliftA2 #-}

instance GApplicative s Par1 where
  gpure :: forall a. a -> Par1 a
gpure = a -> Par1 a
forall a. a -> Par1 a
Par1
  gliftA2 :: forall a b c.
(a %1 -> b %1 -> c) -> Par1 a %1 -> Par1 b %1 -> Par1 c
gliftA2 a %1 -> b %1 -> c
f (Par1 a
x) (Par1 b
y) = c %1 -> Par1 c
forall a. a -> Par1 a
Par1 (a %1 -> b %1 -> c
f a
x b
y)
  {-# INLINE gpure #-}
  {-# INLINE gliftA2 #-}

instance (GApplicative s f, Applicative g) => GApplicative s (f :.: g) where
  gpure :: forall a. a -> (:.:) f g a
gpure = f (g a) -> (:.:) f g a
forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (x :: k1).
f (g x) -> (:.:) f g x
Comp1 (f (g a) -> (:.:) f g a) -> (a -> f (g a)) -> a -> (:.:) f g a
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (s :: ErrorMessage) (f :: * -> *) a.
GApplicative s f =>
a -> f a
gpure @s (g a -> f (g a)) -> (a -> g a) -> a -> f (g a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. a -> g a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  gliftA2 :: forall a b c.
(a %1 -> b %1 -> c)
-> (:.:) f g a %1 -> (:.:) f g b %1 -> (:.:) f g c
gliftA2 a %1 -> b %1 -> c
f (Comp1 f (g a)
x) (Comp1 f (g b)
y) = f (g c) %1 -> (:.:) f g c
forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (x :: k1).
f (g x) -> (:.:) f g x
Comp1 (forall (s :: ErrorMessage) (f :: * -> *) a b c.
GApplicative s f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
gliftA2 @s ((a %1 -> b %1 -> c) -> g a %1 -> g b %1 -> g c
forall (f :: * -> *) a b c.
Applicative f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
liftA2 a %1 -> b %1 -> c
f) f (g a)
x f (g b)
y)
  {-# INLINE gpure #-}
  {-# INLINE gliftA2 #-}

instance (GApplicative s f, GApplicative s g) => GApplicative s (f :*: g) where
  gpure :: forall a. a -> (:*:) f g a
gpure a
a = forall (s :: ErrorMessage) (f :: * -> *) a.
GApplicative s f =>
a -> f a
gpure @s a
a f a -> g a -> (:*:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: forall (s :: ErrorMessage) (f :: * -> *) a.
GApplicative s f =>
a -> f a
gpure @s a
a
  gliftA2 :: forall a b c.
(a %1 -> b %1 -> c)
-> (:*:) f g a %1 -> (:*:) f g b %1 -> (:*:) f g c
gliftA2 a %1 -> b %1 -> c
f (f a
a1 :*: g a
a2) (f b
b1 :*: g b
b2) = forall (s :: ErrorMessage) (f :: * -> *) a b c.
GApplicative s f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
gliftA2 @s a %1 -> b %1 -> c
f f a
a1 f b
b1 f c %1 -> g c %1 -> (:*:) f g c
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: forall (s :: ErrorMessage) (f :: * -> *) a b c.
GApplicative s f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
gliftA2 @s a %1 -> b %1 -> c
f g a
a2 g b
b2
  {-# INLINE gpure #-}
  {-# INLINE gliftA2 #-}

instance
  Unsatisfiable
    ( 'Text "Cannot derive a data Applicative instance for" ':$$: s
        ':$$: 'Text "because sum types do not admit a uniform Applicative definition."
    ) =>
  GApplicative s (x :+: y)
  where
  gpure :: forall a. a -> (:+:) x y a
gpure = a -> (:+:) x y a
forall a. Bottom => a
unsatisfiable
  gliftA2 :: forall a b c.
(a %1 -> b %1 -> c)
-> (:+:) x y a %1 -> (:+:) x y b %1 -> (:+:) x y c
gliftA2 = (a %1 -> b %1 -> c)
-> (:+:) x y a %1 -> (:+:) x y b %1 -> (:+:) x y c
forall a. Bottom => a
unsatisfiable

instance GApplicative s f => GApplicative s (MP1 m f) where
  gpure :: forall a. a -> MP1 m f a
gpure a
a = f a %m -> MP1 m f a
forall {k} (b :: k -> *) (c :: k) (a :: Multiplicity).
b c %a -> MP1 a b c
MP1 (forall (s :: ErrorMessage) (f :: * -> *) a.
GApplicative s f =>
a -> f a
gpure @s a
a)
  gliftA2 :: forall a b c.
(a %1 -> b %1 -> c) -> MP1 m f a %1 -> MP1 m f b %1 -> MP1 m f c
gliftA2 a %1 -> b %1 -> c
f (MP1 f a
a) (MP1 f b
b) = f c %m -> MP1 m f c
forall {k} (b :: k -> *) (c :: k) (a :: Multiplicity).
b c %a -> MP1 a b c
MP1 (forall (s :: ErrorMessage) (f :: * -> *) a b c.
GApplicative s f =>
(a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
gliftA2 @s a %1 -> b %1 -> c
f f a
a f b
b)
  {-# INLINE gpure #-}
  {-# INLINE gliftA2 #-}

instance Monoid c => GApplicative s (K1 i c) where
  gpure :: forall a. a -> K1 i c a
gpure a
_ = c -> K1 i c a
forall k i c (p :: k). c -> K1 i c p
K1 c
forall a. Monoid a => a
mempty
  gliftA2 :: forall a b c.
(a %1 -> b %1 -> c) -> K1 i c a %1 -> K1 i c b %1 -> K1 i c c
gliftA2 a %1 -> b %1 -> c
_ (K1 c
x) (K1 c
y) = c %1 -> K1 i c c
forall k i c (p :: k). c -> K1 i c p
K1 (c
x c %1 -> c %1 -> c
forall a. Semigroup a => a %1 -> a %1 -> a
<> c
y)
  {-# INLINE gpure #-}
  {-# INLINE gliftA2 #-}

instance
  Unsatisfiable
    ( 'Text "Cannot derive a data Applicative instance for" ':$$: s
        ':$$: 'Text "because it contains one or more primitive unboxed fields."
        ':$$: 'Text "Such unboxed types lack canonical monoid operations."
    ) =>
  GApplicative s (URec a)
  where
  gpure :: forall a. a -> URec a a
gpure = a -> URec a a
forall a. Bottom => a
unsatisfiable
  gliftA2 :: forall a b c.
(a %1 -> b %1 -> c) -> URec a a %1 -> URec a b %1 -> URec a c
gliftA2 = (a %1 -> b %1 -> c) -> URec a a %1 -> URec a b %1 -> URec a c
forall a. Bottom => a
unsatisfiable