{-# LANGUAGE EmptyCase, PolyKinds, UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-unticked-promoted-constructors #-}
-- | Derive @generics-sop@ boilerplate instances from GHC's 'GHC.Generic'.
--
-- The technique being used here is described in the following paper:
--
--   * José Pedro Magalhães and Andres Löh.
--     <http://www.andres-loeh.de/GenericGenericProgramming Generic Generic Programming>.
--     Practical Aspects of Declarative Languages (PADL) 2014.
--
module Generics.SOP.GGP
  ( GCode
  , GFrom
  , GTo
  , GDatatypeInfo
  , GDatatypeInfoOf
  , gfrom
  , gto
  , gdatatypeInfo
  ) where

import Data.Proxy (Proxy (..))
import Data.Kind (Type)
import GHC.Generics as GHC
import Generics.SOP.NP as SOP
import Generics.SOP.NS as SOP
import Generics.SOP.BasicFunctors as SOP
import qualified Generics.SOP.Type.Metadata as SOP.T
import Generics.SOP.Metadata as SOP

type family ToSingleCode (a :: Type -> Type) :: Type
type instance ToSingleCode (K1 _i a) = a

type family ToProductCode (a :: Type -> Type) (xs :: [Type]) :: [Type]
type instance ToProductCode (a :*: b)   xs = ToProductCode a (ToProductCode b xs)
type instance ToProductCode U1          xs = xs
type instance ToProductCode (M1 S _c a) xs = ToSingleCode a ': xs

type family ToSumCode (a :: Type -> Type) (xs :: [[Type]]) :: [[Type]]
type instance ToSumCode (a :+: b)   xs = ToSumCode a (ToSumCode b xs)
type instance ToSumCode V1          xs = xs
type instance ToSumCode (M1 D _c a) xs = ToSumCode a xs
type instance ToSumCode (M1 C _c a) xs = ToProductCode a '[] ': xs

data InfoProxy (c :: Meta) (f :: Type -> Type) (x :: Type) = InfoProxy

type family ToInfo (a :: Type -> Type) :: SOP.T.DatatypeInfo
type instance ToInfo (M1 D (MetaData n m p False) a) =
  SOP.T.ADT m n (ToSumInfo a '[]) (ToStrictnessInfoss a '[])
type instance ToInfo (M1 D (MetaData n m p True) a) =
  SOP.T.Newtype m n (ToSingleConstructorInfo a)

type family ToStrictnessInfoss (a :: Type -> Type) (xss :: [[SOP.T.StrictnessInfo]]) :: [[SOP.T.StrictnessInfo]]
type instance ToStrictnessInfoss (a :+: b)  xss = ToStrictnessInfoss a (ToStrictnessInfoss b xss)
type instance ToStrictnessInfoss V1         xss = xss
type instance ToStrictnessInfoss (M1 C _ a) xss = ToStrictnessInfos a '[] ': xss

type family ToStrictnessInfos (a :: Type -> Type) (xs :: [SOP.T.StrictnessInfo]) :: [SOP.T.StrictnessInfo]
type instance ToStrictnessInfos (a :*: b)  xs = ToStrictnessInfos a (ToStrictnessInfos b xs)
type instance ToStrictnessInfos U1         xs = xs
type instance ToStrictnessInfos (M1 S s a) xs = ToStrictnessInfo s ': xs

type family ToStrictnessInfo (s :: Meta) :: SOP.T.StrictnessInfo
type instance ToStrictnessInfo (MetaSel _ su ss ds) = 'SOP.T.StrictnessInfo su ss ds

type family ToSumInfo (a :: Type -> Type) (xs :: [SOP.T.ConstructorInfo]) :: [SOP.T.ConstructorInfo]
type instance ToSumInfo (a :+: b)  xs = ToSumInfo a (ToSumInfo b xs)
type instance ToSumInfo V1         xs = xs
type instance ToSumInfo (M1 C c a) xs = ToSingleConstructorInfo (M1 C c a) ': xs

type family ToSingleConstructorInfo (a :: Type -> Type) :: SOP.T.ConstructorInfo
type instance ToSingleConstructorInfo (M1 C (MetaCons n PrefixI False) a) =
  SOP.T.Constructor n
type instance ToSingleConstructorInfo (M1 C (MetaCons n (InfixI assoc fix) False) a) =
  SOP.T.Infix n assoc fix
type instance ToSingleConstructorInfo (M1 C (MetaCons n f True) a) =
  SOP.T.Record n (ToProductInfo a '[])

type family ToProductInfo (a :: Type -> Type) (xs :: [SOP.T.FieldInfo]) :: [SOP.T.FieldInfo]
type instance ToProductInfo (a :*: b)  xs = ToProductInfo a (ToProductInfo b xs)
type instance ToProductInfo U1         xs = xs
type instance ToProductInfo (M1 S c a) xs = ToSingleInfo (M1 S c a) ': xs

type family ToSingleInfo (a :: Type -> Type) :: SOP.T.FieldInfo
type instance ToSingleInfo (M1 S (MetaSel (Just n) _su _ss _ds) a) = 'SOP.T.FieldInfo n

class GFieldInfos (a :: Type -> Type) where
  gFieldInfos :: proxy a -> NP FieldInfo xs -> NP FieldInfo (ToProductCode a xs)

instance (GFieldInfos a, GFieldInfos b) => GFieldInfos (a :*: b) where
  gFieldInfos :: forall (proxy :: (* -> *) -> *) (xs :: [*]).
proxy (a :*: b)
-> NP FieldInfo xs -> NP FieldInfo (ToProductCode (a :*: b) xs)
gFieldInfos proxy (a :*: b)
_ NP FieldInfo xs
xs = Proxy a
-> NP FieldInfo (ToProductCode b xs)
-> NP FieldInfo (ToProductCode a (ToProductCode b xs))
forall (a :: * -> *) (proxy :: (* -> *) -> *) (xs :: [*]).
GFieldInfos a =>
proxy a -> NP FieldInfo xs -> NP FieldInfo (ToProductCode a xs)
forall (proxy :: (* -> *) -> *) (xs :: [*]).
proxy a -> NP FieldInfo xs -> NP FieldInfo (ToProductCode a xs)
gFieldInfos (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a) (Proxy b -> NP FieldInfo xs -> NP FieldInfo (ToProductCode b xs)
forall (a :: * -> *) (proxy :: (* -> *) -> *) (xs :: [*]).
GFieldInfos a =>
proxy a -> NP FieldInfo xs -> NP FieldInfo (ToProductCode a xs)
forall (proxy :: (* -> *) -> *) (xs :: [*]).
proxy b -> NP FieldInfo xs -> NP FieldInfo (ToProductCode b xs)
gFieldInfos (Proxy b
forall {k} (t :: k). Proxy t
Proxy :: Proxy b) NP FieldInfo xs
xs)

instance GFieldInfos U1 where
  gFieldInfos :: forall (proxy :: (* -> *) -> *) (xs :: [*]).
proxy U1 -> NP FieldInfo xs -> NP FieldInfo (ToProductCode U1 xs)
gFieldInfos proxy U1
_ NP FieldInfo xs
xs = NP FieldInfo xs
NP FieldInfo (ToProductCode U1 xs)
xs

instance (Selector c) => GFieldInfos (M1 S c a) where
  gFieldInfos :: forall (proxy :: (* -> *) -> *) (xs :: [*]).
proxy (M1 S c a)
-> NP FieldInfo xs -> NP FieldInfo (ToProductCode (M1 S c a) xs)
gFieldInfos proxy (M1 S c a)
_ NP FieldInfo xs
xs = FieldName -> FieldInfo (ToSingleCode a)
forall a. FieldName -> FieldInfo a
FieldInfo (InfoProxy c a (ZonkAny 2) -> FieldName
forall {k} (s :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Selector s =>
t s f a -> FieldName
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
       (a :: k1).
t c f a -> FieldName
selName InfoProxy c a (ZonkAny 2)
forall x. InfoProxy c a x
p) FieldInfo (ToSingleCode a)
-> NP FieldInfo xs -> NP FieldInfo (ToSingleCode a : xs)
forall {k} (a :: k -> *) (x :: k) (xs :: [k]).
a x -> NP a xs -> NP a (x : xs)
:* NP FieldInfo xs
xs
    where
      p :: InfoProxy c a x
      p :: forall x. InfoProxy c a x
p = InfoProxy c a x
forall (c :: Meta) (f :: * -> *) x. InfoProxy c f x
InfoProxy

class GSingleFrom (<