{-# 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 #-}
module Control.Functor.Linear.Internal.Class
(
Functor (..),
dataFmapDefault,
(<$>),
(<&>),
(<$),
void,
Applicative (..),
dataPureDefault,
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.Kind (FUN)
import Data.Monoid.Linear hiding (Sum)
import Data.Type.Bool
import Data.Unrestricted.Linear.Internal.Consumable
import GHC.TypeLits
import GHC.Types (Multiplicity (..), Type)
import Generics.Linear
import Prelude.Linear.Generically
import Prelude.Linear.Internal
import Prelude.Linear.Unsatisfiable (Unsatisfiable, unsatisfiable)
import Prelude (Bool (..), String)
class (Data.Functor f) => Functor f where
fmap :: (a %1 -> b) %1 -> f a %1 -> f b
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 = 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
(<$>) = forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap
{-# INLINE (<$>) #-}
infixl 4 <$>
(<&>) :: (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 forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
<$> f a
a
{-# INLINE (<&>) #-}
infixl 1 <&>
(<$) :: (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 = forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap (forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` a
a) f b
fb
infixl 4 <$
void :: (Functor f, Consumable a) => f a %1 -> f ()
void :: forall (f :: * -> *) a. (Functor f, Consumable a) => f a %1 -> f ()
void = forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap forall a. Consumable a => a %1 -> ()
consume
class (Data.Applicative f, Functor f) => Applicative f where
{-# MINIMAL pure, ((<*>) | liftA2) #-}
pure :: a %1 -> f a
(<*>) :: 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 forall a (q :: Multiplicity). a %q -> a
id
infixl 4 <*>
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 forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
<$> f a
x forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
<*> f b
y
dataPureDefault :: (Applicative f) => a -> f a
dataPureDefault :: forall (f :: * -> *) a. Applicative f => a -> f a
dataPureDefault a
x = 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 = (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 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 = (forall a. Monoid a => a
mempty, 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 forall a. Semigroup a => a %1 -> a %1 -> a
<> a
b1, b
a2 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 = (forall a. Monoid a => a
mempty, forall a. Monoid a => a
mempty, 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 forall a. Semigroup a => a %1 -> a %1 -> a
<> a
b1, b
a2 forall a. Semigroup a => a %1 -> a %1 -> a
<> b
b2, c
a3 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 = 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 = 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
class (Applicative m) => Monad m where
{-# MINIMAL (>>=) #-}
(>>=) :: m a %1 -> (a %1 -> m b) %1 -> m b
infixl 1 >>=
(>>) :: m () %1 -> m a %1 -> m a
m ()
m >> m a
k = m ()
m forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
>>= (\() -> m a
k)
infixl 1 >>
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 = forall (f :: * -> *) a. Applicative f => a %1 -> f a
pure a
x
{-# INLINE return #-}
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 forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
>>= forall a (q :: Multiplicity). a %q -> a
id
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 forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
>>= (\a %1 -> b
f' -> forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f' m a
x)
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 [] = 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 forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
>>= \b
i' -> 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
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 = 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 forall a. Semigroup a => a %1 -> a %1 -> a
<> a
b2, b
y)
deriving via
Generically1 (Sum (f :: Type -> Type) g)
instance
(Functor f, Functor g) => Functor (Sum f g)
deriving via
Generically1 (Compose (f :: Type -> Type) (g :: Type -> Type))
instance
(Functor f, Functor g) => Functor (Compose f g)
instance Functor (FUN 'One a) where
fmap :: forall a b. (a %1 -> b) %1 -> (a %1 -> a) %1 -> a %1 -> b
fmap = forall b c a (q :: Multiplicity) (m :: Multiplicity)
(n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
(.)
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 = forall (f :: * -> *) a. f a -> Generically1 f a
Generically1 forall b c a (q :: Multiplicity) (m :: Multiplicity)
(n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. forall {k} (f :: k -> *) (p :: k) (m :: Multiplicity).
Generic1 f =>
Rep1 f p %m -> f p
to1 forall b c a (q :: Multiplicity) (m :: Multiplicity)
(n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f forall b c a (q :: Multiplicity) (m :: Multiplicity)
(n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. forall {k} (f :: k -> *) (p :: k) (m :: Multiplicity).
Generic1 f =>
f p %m -> Rep1 f p
from1 forall b c a (q :: Multiplicity) (m :: Multiplicity)
(n :: Multiplicity).
(b %1 -> c) %q -> (a %1 -> b) %m -> a %n -> c
. forall (f :: * -> *) a. Generically1 f a %1 -> f a
unGenerically1
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
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 = 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) = 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) = forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused l a
l forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: 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) = forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused l a
l)
unused (R1 r a
r) = forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (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) = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (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) = 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' = 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' = forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap forall (f :: * -> *) a b. Unused f => f a %1 -> f b
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) = forall (f :: * -> *) a b. Unused f => f a %1 -> f b
unused l a
l forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: 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) = forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap a %1 -> b
f l a
l forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: 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 = 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 = 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) = forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (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) = forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (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) = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (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) = 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) = forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (x :: k1).
f (g x) -> (:.:) f g x
Comp1 (forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
fmap (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 = 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