module GHC.Generics.Extended
  ( module GHC.Generics,
    constrName,
  )
where

import Data.Kind (Type)
import GHC.Generics
import Prelude

-- | Return the constructor name of some data with a Generic instance, as a
-- string. Useful when constructing error or trace messages, or debugging.
constrName :: (HasConstructor (Rep a), Generic a) => a -> String
constrName :: a -> String
constrName a
a = Rep a Any -> String
forall (f :: * -> *) x. HasConstructor f => f x -> String
genericConstrName (Rep a Any -> String) -> Rep a Any -> String
forall a b. (a -> b) -> a -> b
$ a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
a
{-# INLINE constrName #-}

-- for constrName, see: https://stackoverflow.com/a/48179707/176841
class HasConstructor (f :: Type -> Type) where
  genericConstrName :: f x -> String

instance HasConstructor f => HasConstructor (D1 c f) where
  genericConstrName :: D1 c f x -> String
genericConstrName (M1 f x
x) = f x -> String
forall (f :: * -> *) x. HasConstructor f => f x -> String
genericConstrName f x
x
  {-# INLINE genericConstrName #-}

instance (HasConstructor x, HasConstructor y) => HasConstructor (x :+: y) where
  genericConstrName :: (:+:) x y x -> String
genericConstrName (L1 x x
l) = x x -> String
forall (f :: * -> *) x. HasConstructor f => f x -> String
genericConstrName x x
l
  genericConstrName (R1 y x
r) = y x -> String
forall (f :: * -> *) x. HasConstructor f => f x -> String
genericConstrName y x
r
  {-# INLINE genericConstrName #-}

instance Constructor c => HasConstructor (C1 c f) where
  genericConstrName :: C1 c f x -> String
genericConstrName C1 c f x
x = C1 c f x -> String
forall k (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> String
conName C1 c f x
x
  {-# INLINE genericConstrName #-}