{-# LANGUAGE DataKinds #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS -Wno-unticked-promoted-constructors #-}

-- | "GHC.Generics" definition of '<>'
module Data.DerivingVia.GHC.Generics.Semigroup (
  GSemigroup (..),
)
where

import GHC.Generics
import GHC.TypeLits

class GSemigroup rep where
  gsappend :: rep x -> rep x -> rep x

instance Monoid c => GSemigroup (K1 i c) where
  gsappend :: forall x. K1 i c x -> K1 i c x -> K1 i c x
gsappend (K1 c
l) (K1 c
r) = forall k i c (p :: k). c -> K1 i c p
K1 (c
l forall a. Semigroup a => a -> a -> a
<> c
r)

instance GSemigroup f => GSemigroup (M1 i c f) where
  gsappend :: forall x. M1 i c f x -> M1 i c f x -> M1 i c f x
gsappend (M1 f x
l) (M1 f x
r) = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (forall (rep :: * -> *) x. GSemigroup rep => rep x -> rep x -> rep x
gsappend f x
l f x
r)

instance GSemigroup V1 where
  gsappend :: forall x. V1 x -> V1 x -> V1 x
gsappend = \case {}

instance GSemigroup U1 where
  gsappend :: forall x. U1 x -> U1 x -> U1 x
gsappend U1 x
U1 U1 x
U1 = forall k (p :: k). U1 p
U1

instance (GSemigroup l, GSemigroup r) => GSemigroup (l :*: r) where
  gsappend :: forall x. (:*:) l r x -> (:*:) l r x -> (:*:) l r x
gsappend (l x
l1 :*: r x
r1) (l x
l2 :*: r x
r2) = forall (rep :: * -> *) x. GSemigroup rep => rep x -> rep x -> rep x
gsappend l x
l1 l x
l2 forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: forall (rep :: * -> *) x. GSemigroup rep => rep x -> rep x -> rep x
gsappend r x
r1 r x
r2

instance
  TypeError
    ( Text "No Generics definition of "
        :<>: ShowType Semigroup
        :<>: Text " for types with multiple constructors "
        :<>: ShowType (l :+: r)
    ) =>
  GSemigroup (l :+: r)
  where
  gsappend :: forall x. (:+:) l r x -> (:+:) l r x -> (:+:) l r x
gsappend = forall a. HasCallStack => [Char] -> a
error [Char]
"GSemigroup :+:"