@@ -1,4 +1,7 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+{-# LANGUAGE StandaloneDeriving #-}
13{-# LANGUAGE TypeFamilies #-}
4+{-# LANGUAGE TypeOperators #-}
25-- |
36-- Module:
47-- Reflex.Patch
@@ -23,6 +26,11 @@ import Reflex.Patch.MapWithMove as X (PatchMapWithMove, patchMapWithMoveNewEleme
2326 unsafePatchMapWithMove)
2427import Data.Map.Monoidal (MonoidalMap)
2528import Data.Semigroup (Semigroup (..), (<>))
29+import GHC.Generics
30+import Data.Functor.Identity
31+import Data.Functor.Const
32+import Data.Proxy
33+import Control.Applicative
2634
2735-- | A 'Group' is a 'Monoid' where every element has an inverse.
2836class (Semigroup q, Monoid q) => Group q where
@@ -44,3 +52,39 @@ instance (Ord k, Group q) => Group (MonoidalMap k q) where
4452 negateG = fmap negateG
4553
4654instance (Ord k, Additive q) => Additive (MonoidalMap k q)
55+
56+instance Group () where
57+ negateG _ = ()
58+ _ ~~ _ = ()
59+instance Additive ()
60+
61+instance (Group a, Group b) => Group (a, b) where
62+ negateG (a, b) = (negateG a, negateG b)
63+ (a, b) ~~ (c, d) = (a ~~ c, b ~~ d)
64+instance (Additive a, Additive b) => Additive (a, b) where
65+
66+-- See https://gitlab.haskell.org/ghc/ghc/issues/11135#note_111802 for the reason Compose is not also provided.
67+-- Base does not define Monoid (Compose f g a) so this has no "home"
68+instance Group (f (g a)) => Group ((f :.: g) a) where
69+ negateG (Comp1 xs) = Comp1 (negateG xs)
70+ Comp1 xs ~~ Comp1 ys = Comp1 (xs ~~ ys)
71+
72+instance Additive (f (g a)) => Additive ((f :.: g) a) where
73+
74+instance (Group (f a), Group (g a)) => Group ((f :*: g) a) where
75+ negateG (a :*: b) = negateG a :*: negateG b
76+ (a :*: b) ~~ (c :*: d) = (a ~~ c :*: b ~~ d)
77+instance (Additive (f a), Additive (g a)) => Additive ((f :*: g) a) where
78+
79+instance Group (Proxy x) where
80+ negateG _ = Proxy
81+ _ ~~ _ = Proxy
82+instance Additive (Proxy x)
83+
84+deriving instance Group a => Group (Const a x)
85+deriving instance Group a => Group (Identity a)
86+
87+instance Group b => Group (a -> b) where
88+ negateG f = negateG . f
89+ (~~) = liftA2 (~~)
90+instance Additive b => Additive (a -> b)