6
6
{-# LANGUAGE PatternGuards #-}
7
7
{-# LANGUAGE ScopedTypeVariables #-}
8
8
{-# LANGUAGE StandaloneDeriving #-}
9
+ {-# LANGUAGE TemplateHaskell #-}
9
10
{-# LANGUAGE TypeFamilies #-}
10
11
{-# LANGUAGE UndecidableInstances #-}
11
12
@@ -16,14 +17,14 @@ module Data.Patch.MapWithPatchingMove where
16
17
import Data.Patch.Class
17
18
18
19
import Control.Arrow
20
+ import Control.Lens.TH (makeWrapped )
19
21
import Control.Monad.Trans.State
20
22
import Data.Foldable
21
23
import Data.Function
22
24
import Data.List
23
25
import Data.Map (Map )
24
26
import qualified Data.Map as Map
25
27
import Data.Maybe
26
- -- import Data.Proxy
27
28
#if !MIN_VERSION_base(4,10,0)
28
29
import Data.Semigroup (Semigroup (.. ))
29
30
#endif
@@ -39,6 +40,7 @@ newtype PatchMapWithPatchingMove k p = PatchMapWithPatchingMove
39
40
{ -- | Extract the internal representation of the 'PatchMapWithPatchingMove'
40
41
unPatchMapWithPatchingMove :: Map k (NodeInfo k p )
41
42
}
43
+
42
44
deriving instance (Show k , Show p , Show (PatchTarget p )) => Show (PatchMapWithPatchingMove k p )
43
45
deriving instance (Ord k , Read k , Read p , Read (PatchTarget p )) => Read (PatchMapWithPatchingMove k p )
44
46
deriving instance (Eq k , Eq p , Eq (PatchTarget p )) => Eq (PatchMapWithPatchingMove k p )
@@ -58,12 +60,33 @@ deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (NodeInfo k p)
58
60
deriving instance (Eq k , Eq p , Eq (PatchTarget p )) => Eq (NodeInfo k p )
59
61
deriving instance (Ord k , Ord p , Ord (PatchTarget p )) => Ord (NodeInfo k p )
60
62
63
+ bitraverseNodeInfo
64
+ :: Applicative f
65
+ => (k0 -> f k1 )
66
+ -> (p0 -> f p1 )
67
+ -> (PatchTarget p0 -> f (PatchTarget p1 ))
68
+ -> NodeInfo k0 p0 -> f (NodeInfo k1 p1 )
69
+ bitraverseNodeInfo fk fp fpt (NodeInfo from to) = NodeInfo
70
+ <$> bitraverseFrom fk fp fpt from
71
+ <*> traverse fk to
72
+
61
73
-- | Describe how a key's new value should be produced
62
74
data From k p
63
75
= From_Insert (PatchTarget p ) -- ^ Insert the given value here
64
76
| From_Delete -- ^ Delete the existing value, if any, from here
65
77
| From_Move ! k ! p -- ^ Move the value here from the given key, and apply the given patch
66
78
79
+ bitraverseFrom
80
+ :: Applicative f
81
+ => (k0 -> f k1 )
82
+ -> (p0 -> f p1 )
83
+ -> (PatchTarget p0 -> f (PatchTarget p1 ))
84
+ -> From k0 p0 -> f (From k1 p1 )
85
+ bitraverseFrom fk fp fpt = \ case
86
+ From_Insert pt -> From_Insert <$> fpt pt
87
+ From_Delete -> pure From_Delete
88
+ From_Move k p -> From_Move <$> fk k <*> fp p
89
+
67
90
deriving instance (Show k , Show p , Show (PatchTarget p )) => Show (From k p )
68
91
deriving instance (Read k , Read p , Read (PatchTarget p )) => Read (From k p )
69
92
deriving instance (Eq k , Eq p , Eq (PatchTarget p )) => Eq (From k p )
@@ -326,3 +349,5 @@ instance ( Ord k
326
349
) => Monoid (PatchMapWithPatchingMove k p ) where
327
350
mempty = PatchMapWithPatchingMove mempty
328
351
mappend = (<>)
352
+
353
+ makeWrapped ''PatchMapWithPatchingMove
0 commit comments