on 584334f help sign in
patch/src/Data/Patch.hs
108 lines 3.6 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE StandaloneDeriving #-}
4{-# LANGUAGE TypeFamilies #-}
5{-# LANGUAGE TypeOperators #-}
6-- |
7-- Module:
8-- Data.Patch
9-- Description:
10-- This module defines the 'Patch' class.
11module Data.Patch
12 ( module Data.Patch
13 , module X
14 ) where
16import Control.Applicative
17import Data.Functor.Const (Const (..))
18import Data.Functor.Identity
19import Data.Map.Monoidal (MonoidalMap)
20import Data.Proxy
21#if !MIN_VERSION_base(4,9,0)
22import Data.Semigroup (Semigroup (..), (<>))
23#endif
24import GHC.Generics
26import Data.Patch.Class as X
27import Data.Patch.DMap as X hiding (getDeletions)
28import Data.Patch.DMapWithMove as X
29 ( PatchDMapWithMove, const2PatchDMapWithMoveWith, mapPatchDMapWithMove
30 , patchDMapWithMoveToPatchMapWithMoveWith
31 , traversePatchDMapWithMoveWithKey, unPatchDMapWithMove
32 , unsafePatchDMapWithMove, weakenPatchDMapWithMoveWith
33 )
34import Data.Patch.IntMap as X hiding (getDeletions)
35import Data.Patch.Map as X
36import Data.Patch.MapWithMove as X
37 ( PatchMapWithMove, patchMapWithMoveNewElements
38 , patchMapWithMoveNewElementsMap, unPatchMapWithMove
39 , unsafePatchMapWithMove
40 )
42-- | A 'Group' is a 'Monoid' where every element has an inverse.
43class (Semigroup q, Monoid q) => Group q where
44 negateG :: q -> q
45 (~~) :: q -> q -> q
46 r ~~ s = r <> negateG s
48-- | An 'Additive' 'Semigroup' is one where (<>) is commutative
49class Semigroup q => Additive q where
51-- | The elements of an 'Additive' 'Semigroup' can be considered as patches of their own type.
52newtype AdditivePatch p = AdditivePatch { unAdditivePatch :: p }
54instance Additive p => PatchHet (AdditivePatch p) where
55 type PatchSource (AdditivePatch p) = p
56 type PatchTarget (AdditivePatch p) = p
57instance Additive p => Patch (AdditivePatch p) where
58 apply (AdditivePatch p) q = Just $ p <> q
60instance (Ord k, Group q) => Group (MonoidalMap k q) where
61 negateG = fmap negateG
63instance (Ord k, Additive q) => Additive (MonoidalMap k q)
65-- | Trivial group.
66instance Group () where
67 negateG ~() = ()
68 ~() ~~ ~() = ()
69instance Additive ()
71-- | Product group. A Pair of groups gives rise to a group
72instance (Group a, Group b) => Group (a, b) where
73 negateG (a, b) = (negateG a, negateG b)
74 (a, b) ~~ (c, d) = (a ~~ c, b ~~ d)
75instance (Additive a, Additive b) => Additive (a, b)
77-- See https://gitlab.haskell.org/ghc/ghc/issues/11135#note_111802 for the reason Compose is not also provided.
78-- Base does not define Monoid (Compose f g a) so this is the best we can
79-- really do for functor composition.
80instance Group (f (g a)) => Group ((f :.: g) a) where
81 negateG (Comp1 xs) = Comp1 (negateG xs)
82 Comp1 xs ~~ Comp1 ys = Comp1 (xs ~~ ys)
83instance Additive (f (g a)) => Additive ((f :.: g) a)
85-- | Product of groups, Functor style.
86instance (Group (f a), Group (g a)) => Group ((f :*: g) a) where
87 negateG (a :*: b) = negateG a :*: negateG b
88 (a :*: b) ~~ (c :*: d) = (a ~~ c) :*: (b ~~ d)
89instance (Additive (f a), Additive (g a)) => Additive ((f :*: g) a)
91-- | Trivial group, Functor style
92instance Group (Proxy x) where
93 negateG ~Proxy = Proxy
94 ~Proxy ~~ ~Proxy = Proxy
95instance Additive (Proxy x)
97-- | Const lifts groups into a functor.
98deriving instance Group a => Group (Const a x)
99instance Additive a => Additive (Const a x)
100-- | Ideitnty lifts groups pointwise (at only one point)
101deriving instance Group a => Group (Identity a)
102instance Additive a => Additive (Identity a)
104-- | Functions lift groups pointwise.
105instance Group b => Group (a -> b) where
106 negateG f = negateG . f
107 (~~) = liftA2 (~~)
108instance Additive b => Additive (a -> b)