@@ -1,4 +1,7 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+{-# LANGUAGE StandaloneDeriving #-}
1{-# LANGUAGE TypeFamilies #-}
3{-# LANGUAGE TypeFamilies #-}
4+{-# LANGUAGE TypeOperators #-}
4-- Reflex.Patch
7-- Reflex.Patch
@@ -23,6 +26,11 @@ import Reflex.Patch.MapWithMove as X (PatchMapWithMove, patchMapWithMoveNewEleme
23 unsafePatchMapWithMove)
26 unsafePatchMapWithMove)
24import Data.Map.Monoidal (MonoidalMap)
27import Data.Map.Monoidal (MonoidalMap)
25import Data.Semigroup (Semigroup (..), (<>))
28import Data.Semigroup (Semigroup (..), (<>))
30+import Data.Functor.Identity
31+import Data.Functor.Const
33+import Control.Applicative
27-- | A 'Group' is a 'Monoid' where every element has an inverse.
35-- | A 'Group' is a 'Monoid' where every element has an inverse.
28class (Semigroup q, Monoid q) => Group q where
36class (Semigroup q, Monoid q) => Group q where
@@ -44,3 +52,39 @@ instance (Ord k, Group q) => Group (MonoidalMap k q) where
44 negateG = fmap negateG
52 negateG = fmap negateG
46instance (Ord k, Additive q) => Additive (MonoidalMap k q)
54instance (Ord k, Additive q) => Additive (MonoidalMap k q)
56+instance Group () where
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
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)
72+instance Additive (f (g a)) => Additive ((f :.: g) a) where
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
79+instance Group (Proxy x) where
82+instance Additive (Proxy x)
84+deriving instance Group a => Group (Const a x)
85+deriving instance Group a => Group (Identity a)
87+instance Group b => Group (a -> b) where
88+ negateG f = negateG . f
90+instance Additive b => Additive (a -> b)