{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE InstanceSigs #-}
#if __GLASGOW_HASKELL__ >= 810
{-# LANGUAGE StandaloneKindSignatures #-}
#endif
-- | This module exports 'Generically' and 'Generically' newtypes
-- meant to be used with "GHC.Generics" and @DerivingVia@.
--
-- These types are re-exported from "GHC.Generics" on @base-4.17@ and later,
-- and defined here for older @base@ versions.
--
module GHC.Generics.Generically (
    Generically (..),
    Generically1 (..),
) where

#if MIN_VERSION_base(4,17,0)
import GHC.Generics
#if !MIN_VERSION_base(4,18,0)
import Data.Orphans () -- To bring Eq/Ord instances for Generically1 into scope
#endif
#else

#if __GLASGOW_HASKELL__ >= 810
import Data.Kind (Type)
#endif

#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup (..))
#endif

import GHC.Generics
import Control.Applicative (liftA2)
import Control.Applicative (Alternative (..))
import Data.Functor.Classes (Ord1 (..), Eq1 (..))

-------------------------------------------------------------------------------
-- Generically
-------------------------------------------------------------------------------

-- | A type whose instances are defined generically, using the
-- 'Generic' representation.
newtype Generically a = Generically a

instance (Generic a, Semigroup (Rep a ())) => Semigroup (Generically a) where
  (<>) :: Generically a -> Generically a -> Generically a
  Generically a
a <> :: Generically a -> Generically a -> Generically a
<> Generically a
b = forall a. a -> Generically a
Generically (forall a x. Generic a => Rep a x -> a
to (forall a x. Generic a => a -> Rep a x
from a
a forall a. Semigroup a => a -> a -> a
<> forall a x. Generic a => a -> Rep a x
from a
b :: Rep a ()))

instance (Generic a, Monoid (Rep a ())) => Monoid (Generically a) where
  mempty :: Generically a
  mempty :: Generically a
mempty = forall a. a -> Generically a
Generically (forall a x. Generic a => Rep a x -> a
to (forall a. Monoid a => a
mempty :: Rep a ()))

  mappend :: Generically a -> Generically a -> Generically a
#if MIN_VERSION_base(4,11,0)
  mappend :: Generically a -> Generically a -> Generically a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
#else
  mappend (Generically a) (Generically b) = Generically (to (mappend (from a) (from b) :: Rep a ()))
#endif

-------------------------------------------------------------------------------
-- Generically1
-------------------------------------------------------------------------------

#if __GLASGOW_HASKELL__ >= 810
type    Generically1 :: forall k. (k -> Type) -> (k -> Type)
#endif

-- | A datatype whose instances are defined generically, using the
-- 'Generic' representation. 'Generically1' is a higher-kinded version
-- of 'Generically' that uses 'Generic1'.
newtype Generically1 f a = Generically1 (f a)

instance (Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f) where
  fmap :: (a -> a') -> (Generically1 f a -> Generically1 f a')
  fmap :: forall a b. (a -> b) -> Generically1 f a -> Generically1 f b
fmap a -> a'
f (Generically1 f a
as) = forall k (f :: k -> *) (a :: k). f a -> Generically1 f a
Generically1 (forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a'
f (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
as)))

  (<$) :: a -> Generically1 f b -> Generically1 f a
  a
a <$ :: forall a b. a -> Generically1 f b -> Generically1 f a
<$ Generically1 f b
as = forall k (f :: k -> *) (a :: k). f a -> Generically1 f a
Generically1 (forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (a
a forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f b
as))

instance (Generic1 f, Applicative (Rep1 f)) => Applicative (Generically1 f) where
  pure :: a -> Generically1 f a
  pure :: forall a. a -> Generically1 f a
pure a
a = forall k (f :: k -> *) (a :: k). f a -> Generically1 f a
Generically1 (forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a))

  (<*>) :: Generically1 f (a -> b) -> Generically1 f a -> Generically1 f b
  Generically1 f (a -> b)
fs <*> :: forall a b.
Generically1 f (a -> b) -> Generically1 f a -> Generically1 f b
<*> Generically1 f a
as = forall k (f :: k -> *) (a :: k). f a -> Generically1 f a
Generically1 (forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f (a -> b)
fs forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
as))

#if MIN_VERSION_base(4,10,0)
  liftA2 :: (a -> b -> c) -> (Generically1 f a -> Generically1 f b -> Generically1 f c)
  liftA2 :: forall a b c.
(a -> b -> c)
-> Generically1 f a -> Generically1 f b -> Generically1 f c
liftA2 a -> b -> c
f (Generically1 f a
as) (Generically1 f b
bs) = forall k (f :: k -> *) (a :: k). f a -> Generically1 f a
Generically1 (forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
as) (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f b
bs)))
#endif

instance (Generic1 f, Alternative (Rep1 f)) => Alternative (Generically1 f) where
  empty :: Generically1 f a
  empty :: forall a. Generically1 f a
empty = forall k (f :: k -> *) (a :: k). f a -> Generically1 f a
Generically1 (forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 forall (f :: * -> *) a. Alternative f => f a
empty)

  (<|>) :: Generically1 f a -> Generically1 f a -> Generically1 f a
  Generically1 f a
as1 <|> :: forall a. Generically1 f a -> Generically1 f a -> Generically1 f a
<|> Generically1 f a
as2 = forall k (f :: k -> *) (a :: k). f a -> Generically1 f a
Generically1 (forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
as1 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
as2))

instance (Generic1 f, Eq (Rep1 f a)) => Eq (Generically1 f a) where
   Generically1 f a
x == :: Generically1 f a -> Generically1 f a -> Bool
== Generically1 f a
y = forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
x forall a. Eq a => a -> a -> Bool
== forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
y
   Generically1 f a
x /= :: Generically1 f a -> Generically1 f a -> Bool
/= Generically1 f a
y = forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
x forall a. Eq a => a -> a -> Bool
/= forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
y

instance (Generic1 f, Ord (Rep1 f a)) => Ord (Generically1 f a) where
   Generically1 f a
x compare :: Generically1 f a -> Generically1 f a -> Ordering
`compare` Generically1 f a
y = forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
x forall a. Ord a => a -> a -> Ordering
`compare` forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
y

instance (Generic1 f, Eq1 (Rep1 f)) => Eq1 (Generically1 f) where
  liftEq :: (a -> b -> Bool) -> (Generically1 f a -> Generically1 f b -> Bool)
  liftEq :: forall a b.
(a -> b -> Bool) -> Generically1 f a -> Generically1 f b -> Bool
liftEq a -> b -> Bool
eq (Generically1 f a
as1) (Generically1 f b
as2) = forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
as1) (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f b
as2)

instance (Generic1 f, Ord1 (Rep1 f)) => Ord1 (Generically1 f) where
  liftCompare :: (a -> b -> Ordering) -> (Generically1 f a -> Generically1 f b -> Ordering)
  liftCompare :: forall a b.
(a -> b -> Ordering)
-> Generically1 f a -> Generically1 f b -> Ordering
liftCompare a -> b -> Ordering
cmp (Generically1 f a
as1) (Generically1 f b
as2) = forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
cmp (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f a
as1) (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 f b
as2)

-- MIN_VERSION_base(4,17,0)
#endif