{-# LANGUAGE TypeOperators, FlexibleContexts #-}
{- | Pulled from
https://stackoverflow.com/questions/10112733/haskell-simple-constructor-comparison-function
 -}
module SameConstr where

import GHC.Generics
import Data.Function (on)

--class EqC a where
--    eqConstr :: a -> a -> Bool
--    default eqConstr :: (Generic a, GEqC (Rep a)) => a -> a -> Bool
--    eqConstr = geqConstr `on` from

eqConstr :: (Generic a, GEqC (Rep a)) => a -> a -> Bool
eqConstr = geqConstr `on` from

class GEqC f where
  geqConstr :: f p -> f p -> Bool
  {-# INLINE geqConstr #-}
  geqConstr _ _ = True

instance GEqC f => GEqC (M1 i c f) where
  {-# INLINE geqConstr #-}
  geqConstr (M1 x) (M1 y) = geqConstr x y

instance GEqC (K1 i c)
instance GEqC (f :*: g)
instance GEqC U1
instance GEqC V1

instance (GEqC f, GEqC g) => GEqC (f :+: g) where
  {-# INLINE geqConstr #-}
  geqConstr (L1 x) (L1 y) = geqConstr x y
  geqConstr (R1 x) (R1 y) = geqConstr x y
  geqConstr _ _ = False

