{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeOperators #-}

#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE DefaultSignatures #-}
#endif

#if __GLASGOW_HASKELL__ >= 705
{-# LANGUAGE PolyKinds #-}
#endif

#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif

module Generics.Deriving.Copoint (
  -- * GCopoint class
    GCopoint(..)

  -- * Default method
  , gcopointdefault

  -- * Internal class
  , GCopoint'(..)

  ) where

import           Control.Applicative (WrappedMonad)

import           Data.Monoid (Dual)
import qualified Data.Monoid as Monoid (Sum)

import           Generics.Deriving.Base

#if MIN_VERSION_base(4,6,0)
import           Data.Ord (Down)
#else
import           GHC.Exts (Down)
#endif

#if MIN_VERSION_base(4,8,0)
import           Data.Functor.Identity (Identity)
import           Data.Monoid (Alt)
#endif

#if MIN_VERSION_base(4,9,0)
import qualified Data.Functor.Sum as Functor (Sum)
import           Data.Semigroup (Arg, First, Last, Max, Min, WrappedMonoid)
#endif

--------------------------------------------------------------------------------
-- Generic copoint
--------------------------------------------------------------------------------

-- General copoint may return 'Nothing'

class GCopoint' t where
    gcopoint' :: t a -> Maybe a

instance GCopoint' V1 where
    gcopoint' :: forall a. V1 a -> Maybe a
gcopoint' V1 a
_ = forall a. Maybe a
Nothing

instance GCopoint' U1 where
    gcopoint' :: forall a. U1 a -> Maybe a
gcopoint' U1 a
U1 = forall a. Maybe a
Nothing

instance GCopoint' Par1 where
    gcopoint' :: forall a. Par1 a -> Maybe a
gcopoint' (Par1 a
a) = forall a. a -> Maybe a
Just a
a

instance GCopoint' (K1 i c) where
    gcopoint' :: forall a. K1 i c a -> Maybe a
gcopoint' K1 i c a
_ = forall a. Maybe a
Nothing

instance GCopoint' f => GCopoint' (M1 i c f) where
    gcopoint' :: forall a. M1 i c f a -> Maybe a
gcopoint' (M1 f a
a) = forall (t :: * -> *) a. GCopoint' t => t a -> Maybe a
gcopoint' f a
a

instance (GCopoint' f, GCopoint' g) => GCopoint' (f :+: g) where
    gcopoint' :: forall a. (:+:) f g a -> Maybe a
gcopoint' (L1 f a
a) = forall (t :: * -> *) a. GCopoint' t => t a -> Maybe a
gcopoint' f a
a
    gcopoint' (R1 g a
a) = forall (t :: * -> *) a. GCopoint' t => t a -> Maybe a
gcopoint' g a
a

-- Favours left "hole" for copoint
instance (GCopoint' f, GCopoint' g) => GCopoint' (f :*: g) where
    gcopoint' :: forall a. (:*:) f g a -> Maybe a
gcopoint' (f a
a :*: g a
b) = case (forall (t :: * -> *) a. GCopoint' t => t a -> Maybe a
gcopoint' f a
a) of
                            Just a
x -> forall a. a -> Maybe a
Just a
x
                            Maybe a
Nothing -> forall (t :: * -> *) a. GCopoint' t => t a -> Maybe a
gcopoint' g a
b

instance (GCopoint f) => GCopoint' (Rec1 f) where
    gcopoint' :: forall a. Rec1 f a -> Maybe a
gcopoint' (Rec1 f a
a) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (d :: * -> *) a. GCopoint d => d a -> a
gcopoint f a
a

instance (GCopoint f, GCopoint' g) => GCopoint' (f :.: g) where
    gcopoint' :: forall a. (:.:) f g a -> Maybe a
gcopoint' (Comp1 f (g a)
x) = forall (t :: * -> *) a. GCopoint' t => t a -> Maybe a
gcopoint' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (d :: * -> *) a. GCopoint d => d a -> a
gcopoint forall a b. (a -> b) -> a -> b
$ f (g a)
x

class GCopoint d where
  gcopoint :: d a -> a
#if __GLASGOW_HASKELL__ >= 701
  default gcopoint :: (Generic1 d, GCopoint' (Rep1 d))
                   => (d a -> a)
  gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault
#endif

gcopointdefault :: (Generic1 d, GCopoint' (Rep1 d))
                => d a -> a
gcopointdefault :: forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault d a
x = case (forall (t :: * -> *) a. GCopoint' t => t a -> Maybe a
gcopoint' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 forall a b. (a -> b) -> a -> b
$ d a
x) of
                      Just a
x' -> a
x'
                      Maybe a
Nothing -> forall a. HasCallStack => [Char] -> a
error [Char]
"Data type is not copointed"

-- instance (Generic1 d, GCopoint' (Rep1 d)) => GCopoint d

-- Base types instances
instance GCopoint ((,) a) where
  gcopoint :: forall a. (a, a) -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint ((,,) a b) where
  gcopoint :: forall a. (a, b, a) -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint ((,,,) a b c) where
  gcopoint :: forall a. (a, b, c, a) -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint ((,,,,) a b c d) where
  gcopoint :: forall a. (a, b, c, d, a) -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint ((,,,,,) a b c d e) where
  gcopoint :: forall a. (a, b, c, d, e, a) -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint ((,,,,,,) a b c d e f) where
  gcopoint :: forall a. (a, b, c, d, e, f, a) -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

#if MIN_VERSION_base(4,8,0)
instance GCopoint f => GCopoint (Alt f) where
  gcopoint :: forall a. Alt f a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault
#endif

#if MIN_VERSION_base(4,9,0)
instance GCopoint (Arg a) where
  gcopoint :: forall a. Arg a a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault
#endif

instance GCopoint Down where
  gcopoint :: forall a. Down a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint Dual where
  gcopoint :: forall a. Dual a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

#if MIN_VERSION_base(4,9,0)
instance GCopoint First where
  gcopoint :: forall a. First a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault
#endif

#if MIN_VERSION_base(4,8,0)
instance GCopoint Identity where
  gcopoint :: forall a. Identity a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault
#endif

#if MIN_VERSION_base(4,9,0)
instance GCopoint Last where
  gcopoint :: forall a. Last a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint Max where
  gcopoint :: forall a. Max a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint Min where
  gcopoint :: forall a. Min a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance (GCopoint f, GCopoint g) => GCopoint (Functor.Sum f g) where
  gcopoint :: forall a. Sum f g a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault
#endif

instance GCopoint Monoid.Sum where
  gcopoint :: forall a. Sum a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

instance GCopoint m => GCopoint (WrappedMonad m) where
  gcopoint :: forall a. WrappedMonad m a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault

#if MIN_VERSION_base(4,9,0)
instance GCopoint WrappedMonoid where
  gcopoint :: forall a. WrappedMonoid a -> a
gcopoint = forall (d :: * -> *) a.
(Generic1 d, GCopoint' (Rep1 d)) =>
d a -> a
gcopointdefault
#endif