@@ -2,364 +2,314 @@
2{-# LANGUAGE DeriveTraversable #-}
2{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE LambdaCase #-}
6{-# LANGUAGE LambdaCase #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE PatternGuards #-}
8{-# LANGUAGE PatternGuards #-}
9+{-# LANGUAGE PatternSynonyms #-}
8{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE StandaloneDeriving #-}
11{-# LANGUAGE StandaloneDeriving #-}
10{-# LANGUAGE TemplateHaskell #-}
12{-# LANGUAGE TemplateHaskell #-}
11{-# LANGUAGE TypeApplications #-}
13{-# LANGUAGE TypeApplications #-}
12{-# LANGUAGE TypeFamilies #-}
14{-# LANGUAGE TypeFamilies #-}
13−{-# LANGUAGE UndecidableInstances #-}
15+{-# LANGUAGE ViewPatterns #-}
15-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
17-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
17−module Data.Patch.MapWithMove where
19+module Data.Patch.MapWithMove
26+ , patchMapWithMoveInsertAll
31+ , unsafePatchMapWithMove
32+ , patchMapWithMoveNewElements
33+ , patchMapWithMoveNewElementsMap
34+ , patchThatSortsMapWith
35+ , patchThatChangesAndSortsMapWith
36+ , patchThatChangesMap
64+import Data.Kind (Type)
19import Data.Patch.Class
65import Data.Patch.Class
66+import Data.Patch.MapWithPatchingMove (PatchMapWithPatchingMove(..), To)
67+import qualified Data.Patch.MapWithPatchingMove as PM -- already a transparent synonym
21−import Control.Arrow
69+import Control.Lens
22−import Control.Lens hiding (from, to)
23−import Control.Monad.Trans.State
26import Data.List
70import Data.List
27import Data.Map (Map)
71import Data.Map (Map)
28import qualified Data.Map as Map
72import qualified Data.Map as Map
29−import Data.Maybe
73+import Data.Proxy
31−#if !MIN_VERSION_base(4,9,0)
74+#if !MIN_VERSION_base(4,11,0)
32−import Data.Semigroup (Semigroup (..), (<>))
75+import Data.Semigroup (Semigroup (..))
34−import Data.Monoid.DecidablyEmpty
77+import Data.Traversable (foldMapDefault)
35−import qualified Data.Set as Set
36−import Data.These (These (..))
39-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
79-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
40-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
80-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
41-- and vice versa. There should never be any unpaired From/To keys.
81-- and vice versa. There should never be any unpaired From/To keys.
42−newtype PatchMapWithMove k p = PatchMapWithMove
82+newtype PatchMapWithMove k (v :: Type) = PatchMapWithMove'
43− { -- | Extract the internal representation of the 'PatchMapWithMove'
83+ { -- | Extract the underlying 'PatchMapWithPatchingMove k (Proxy v)'
44− unPatchMapWithMove :: Map k (NodeInfo k p)
84+ unPatchMapWithMove' :: PatchMapWithPatchingMove k (Proxy v)
46−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (PatchMapWithMove k p)
86+ deriving ( Show, Read, Eq, Ord
47−deriving instance (Ord k, Read k, Read p, Read (PatchTarget p)) => Read (PatchMapWithMove k p)
87+-- Haddock cannot handle documentation here before GHC 8.6
48−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (PatchMapWithMove k p)
88+ ,
49−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (PatchMapWithMove k p)
89+#if __GLASGOW_HASKELL__ >= 806
90+ -- | Compose patches having the same effect as applying the
91+ -- patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p .
51−-- -- | Skips the patch so it's 'Functor' and friends
98+pattern Coerce :: Coercible a b => a -> b
52−-- newtype PatchMapWithMove' k v = PatchMapWithMove' { unPatchMapWithMove' :: PatchMapWithMove k (Proxy v) }
99+pattern Coerce x <- (coerce -> x)
53−--
100+ where Coerce x = coerce x
54−-- deriving instance Functor (PatchMapWithMove' k)
55−-- deriving instance Foldable (PatchMapWithMove' k)
56−-- deriving instance Traversable (PatchMapWithMove' k)
58−-- | Holds the information about each key: where its new value should come from,
102+{-# COMPLETE PatchMapWithMove #-}
59−-- and where its old value should go to
103+pattern PatchMapWithMove :: Map k (NodeInfo k v) -> PatchMapWithMove k v
60−data NodeInfo k p = NodeInfo
104+-- | Extract the representation of the 'PatchMapWithMove' as a map of
61− { _nodeInfo_from :: !(From k p)
105+-- 'NodeInfo'.
62− -- ^ Where do we get the new value for this key?
106+unPatchMapWithMove :: PatchMapWithMove k v -> Map k (NodeInfo k v)
63− , _nodeInfo_to :: !(To k p)
107+pattern PatchMapWithMove { unPatchMapWithMove } = PatchMapWithMove' (PatchMapWithPatchingMove (Coerce unPatchMapWithMove))
64− -- ^ If the old value is being kept (i.e. moved rather than deleted or
65− -- replaced), where is it going?
67−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (NodeInfo k p)
68−deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (NodeInfo k p)
69−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (NodeInfo k p)
70−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (NodeInfo k p)
72−-- | Describe how a key's new value should be produced
109+_PatchMapWithMove
73−data From k p
110+ :: Iso
74− = From_Insert (PatchTarget p) -- ^ Insert the given value here
111+ (PatchMapWithMove k0 v0)
75− | From_Delete -- ^ Delete the existing value, if any, from here
112+ (PatchMapWithMove k1 v1)
76− | From_Move !k !p -- ^ Move the value here from the given key, and apply the given patch
113+ (Map k0 (NodeInfo k0 v0))
77−
114+ (Map k1 (NodeInfo k1 v1))
78−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (From k p)
115+_PatchMapWithMove = iso unPatchMapWithMove PatchMapWithMove
79−deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (From k p)
80−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (From k p)
81−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (From k p)
83−-- | Describe where a key's old value will go. If this is 'Just', that means
84−-- the key's old value will be moved to the given other key; if it is 'Nothing',
85−-- that means it will be deleted.
89− deriving ( Show, Read, Eq, Ord
90− , Functor, Foldable, Traversable
93−makeWrapped ''PatchMapWithMove
117+instance Functor (PatchMapWithMove k) where
118+ fmap f = runIdentity . traverse (Identity . f)
94−--makeWrapped ''PatchMapWithMove'
120+instance Foldable (PatchMapWithMove k) where
121+ foldMap = foldMapDefault
123+instance Traversable (PatchMapWithMove k) where
96−-- instance FunctorWithIndex k (PatchMapWithMove k)
129+instance FunctorWithIndex k (PatchMapWithMove k)
97−-- instance FoldableWithIndex k (PatchMapWithMove k)
130+instance FoldableWithIndex k (PatchMapWithMove k)
98−-- instance TraversableWithIndex k (PatchMapWithMove k) where
131+instance TraversableWithIndex k (PatchMapWithMove k) where
99−-- itraverse = itraversed . Indexed
132+ itraverse = itraversed . Indexed
100−-- itraversed = _Wrapped .> itraversed <. traversed
133+ itraversed =
134+ _PatchMapWithMove .>
102-- | Create a 'PatchMapWithMove', validating it
138-- | Create a 'PatchMapWithMove', validating it
104− :: Ord k => Map k (NodeInfo k p) -> Maybe (PatchMapWithMove k p)
139+patchMapWithMove :: Ord k => Map k (NodeInfo k v) -> Maybe (PatchMapWithMove k v)
105−patchMapWithMove m = if valid then Just $ PatchMapWithMove m else Nothing
140+patchMapWithMove = fmap coerce . PM.patchMapWithPatchingMove . coerce
106− where valid = forwardLinks == backwardLinks
107− forwardLinks = flip Map.mapMaybe m $ \x -> case _nodeInfo_to x of
108− To_NonMove -> Nothing
109− To_Move to _ -> Just to
110− backwardLinks = Map.fromList $ catMaybes $ flip fmap (Map.toList m) $ \(to, p) ->
111− case _nodeInfo_from p of
112− From_Move from _ -> Just (from, to)
115-- | Create a 'PatchMapWithMove' that inserts everything in the given 'Map'
142-- | Create a 'PatchMapWithMove' that inserts everything in the given 'Map'
116−patchMapWithMoveInsertAll
143+patchMapWithMoveInsertAll :: Map k v -> PatchMapWithMove k v
117− :: Map k (PatchTarget p) -> PatchMapWithMove k p
118−patchMapWithMoveInsertAll m = PatchMapWithMove $ flip fmap m $ \v -> NodeInfo
144+patchMapWithMoveInsertAll = PatchMapWithMove' . PM.patchMapWithPatchingMoveInsertAll
119− { _nodeInfo_from = From_Insert v
120− , _nodeInfo_to = To_NonMove
123−-- | Make a @'PatchMapWithMove' k p@ which has the effect of inserting or replacing a value @v@ at the given key @k@, like 'Map.insert'.
146+-- | Make a @'PatchMapWithMove' k v@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
125− :: k -> PatchTarget p -> PatchMapWithMove k p
147+insertMapKey :: k -> v -> PatchMapWithMove k v
126−insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) To_NonMove
148+insertMapKey k v = PatchMapWithMove' $ PM.insertMapKey k v
128−-- |Make a @'PatchMapWithMove' k p@ which has the effect of moving the value from the first key @k@ to the second key @k@, equivalent to:
150+-- |Make a @'PatchMapWithMove' k v@ which has the effect of moving the value from the first key @k@ to the second key @k@, equivalent to:
131-- 'Map.delete' src (maybe map ('Map.insert' dst) (Map.lookup src map))
153-- 'Map.delete' src (maybe map ('Map.insert' dst) (Map.lookup src map))
134− :: (DecidablyEmpty p, Patch p) => Ord k => k -> k -> PatchMapWithMove k p
155+moveMapKey :: Ord k => k -> k -> PatchMapWithMove k v
135−moveMapKey src dst
156+moveMapKey src dst = PatchMapWithMove' $ PM.moveMapKey src dst
136− | src == dst = mempty
157+
138− PatchMapWithMove $ Map.fromList
139− [ (dst, NodeInfo (From_Move src mempty) To_NonMove)
140− , (src, NodeInfo From_Delete (To_Move dst mempty))
143−-- |Make a @'PatchMapWithMove' k p@ which has the effect of swapping two keys in the mapping, equivalent to:
158+-- |Make a @'PatchMapWithMove' k v@ which has the effect of swapping two keys in the mapping, equivalent to:
146-- let aMay = Map.lookup a map
161-- let aMay = Map.lookup a map
147-- bMay = Map.lookup b map
162-- bMay = Map.lookup b map
148−-- in maybe id (Map.insert a) (bMay `mplus` aMay)
163+-- in maybe id (Map.insert a) (bMay <> aMay)
149−-- . maybe id (Map.insert b) (aMay `mplus` bMay)
164+-- . maybe id (Map.insert b) (aMay <> bMay)
150-- . Map.delete a . Map.delete b $ map
165-- . Map.delete a . Map.delete b $ map
153− :: (DecidablyEmpty p, Patch p)
154− => Ord k => k -> k -> PatchMapWithMove k p
167+swapMapKey :: Ord k => k -> k -> PatchMapWithMove k v
155−swapMapKey src dst
168+swapMapKey src dst = PatchMapWithMove' $ PM.swapMapKey src dst
156− | src == dst = mempty
169+
158− PatchMapWithMove $ Map.fromList
159− [ (dst, NodeInfo (From_Move src mempty) (To_Move dst mempty))
160− , (src, NodeInfo (From_Move dst mempty) (To_Move dst mempty))
163−-- | Make a @'PatchMapWithMove' k v@ which has the effect of deleting a key in
170+-- |Make a @'PatchMapWithMove' k v@ which has the effect of deleting a key in the mapping, equivalent to 'Map.delete'.
164−-- the mapping, equivalent to 'Map.delete'.
166− :: k -> PatchMapWithMove k v
171+deleteMapKey :: k -> PatchMapWithMove k v
167−deleteMapKey k = PatchMapWithMove . Map.singleton k $ NodeInfo From_Delete To_NonMove
172+deleteMapKey = PatchMapWithMove' . PM.deleteMapKey
169-- | Wrap a @'Map' k (NodeInfo k v)@ representing patch changes into a @'PatchMapWithMove' k v@, without checking any invariants.
174-- | Wrap a @'Map' k (NodeInfo k v)@ representing patch changes into a @'PatchMapWithMove' k v@, without checking any invariants.
171-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchMapWithMove' are preserved; they will not be checked.
176-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchMapWithMove' are preserved; they will not be checked.
172−unsafePatchMapWithMove
173− :: Map k (NodeInfo k p) -> PatchMapWithMove k p
177+unsafePatchMapWithMove :: Map k (NodeInfo k v) -> PatchMapWithMove k v
174−unsafePatchMapWithMove = PatchMapWithMove
178+unsafePatchMapWithMove = PatchMapWithMove' . PM.unsafePatchMapWithPatchingMove . coerce
176-- | Apply the insertions, deletions, and moves to a given 'Map'
180-- | Apply the insertions, deletions, and moves to a given 'Map'
177−instance (Ord k, Patch p) => PatchHet (PatchMapWithMove k p) where
181+instance Ord k => PatchHet (PatchMapWithMove k v) where
178− type PatchSource (PatchMapWithMove k p) = Map k (PatchSource p)
182+ type PatchSource (PatchMapWithMove k v) = Map k v
179− type PatchTarget (PatchMapWithMove k p) = Map k (PatchTarget p)
183+ type PatchTarget (PatchMapWithMove k v) = Map k v
181−instance (Ord k, Patch p) => Patch (PatchMapWithMove k p) where
185+instance Ord k => Patch (PatchMapWithMove k v) where
182− -- TODO: return Nothing sometimes
183− -- Note: the strict application here is critical to ensuring that incremental
184− -- merges don't hold onto all their prerequisite events forever; can we make
185− -- this more robust?
186− apply (PatchMapWithMove m) old = Just $! insertions `Map.union` (old `Map.difference` deletions)
186+ apply (PatchMapWithMove' p) = apply p
187− where insertions = flip Map.mapMaybeWithKey m $ \_ ni -> case _nodeInfo_from ni of
187+
188− From_Insert v -> Just v
189− From_Move k p -> applyAlways p <$> Map.lookup k old
190− From_Delete -> Nothing
191− deletions = flip Map.mapMaybeWithKey m $ \_ ni -> case _nodeInfo_from ni of
192− From_Delete -> Just ()
195−-- | Returns all the new elements that will be added to the 'Map'
188+-- | Returns all the new elements that will be added to the 'Map'.
196−patchMapWithMoveNewElements
189+patchMapWithMoveNewElements :: PatchMapWithMove k v -> [v]
197− :: PatchMapWithMove k p -> [PatchTarget p]
190+patchMapWithMoveNewElements = PM.patchMapWithPatchingMoveNewElements . unPatchMapWithMove'
198−patchMapWithMoveNewElements = Map.elems . patchMapWithMoveNewElementsMap
200-- | Return a @'Map' k v@ with all the inserts/updates from the given @'PatchMapWithMove' k v@.
192-- | Return a @'Map' k v@ with all the inserts/updates from the given @'PatchMapWithMove' k v@.
201−patchMapWithMoveNewElementsMap
193+patchMapWithMoveNewElementsMap :: PatchMapWithMove k v -> Map k v
202− :: PatchMapWithMove k p -> Map k (PatchTarget p)
203−patchMapWithMoveNewElementsMap (PatchMapWithMove p) = Map.mapMaybe f p
194+patchMapWithMoveNewElementsMap = PM.patchMapWithPatchingMoveNewElementsMap . unPatchMapWithMove'
204− where f ni = case _nodeInfo_from ni of
205− From_Insert v -> Just v
206− From_Move _ _ -> Nothing
207− From_Delete -> Nothing
209-- | Create a 'PatchMapWithMove' that, if applied to the given 'Map', will sort
196-- | Create a 'PatchMapWithMove' that, if applied to the given 'Map', will sort
210-- its values using the given ordering function. The set keys of the 'Map' is
197-- its values using the given ordering function. The set keys of the 'Map' is
211-- not changed.
198-- not changed.
212−patchThatSortsMapWith
199+patchThatSortsMapWith :: Ord k => (v -> v -> Ordering) -> Map k v -> PatchMapWithMove k v
213− :: (Ord k, Monoid p)
214− => (PatchTarget p -> PatchTarget p -> Ordering)
215− -> Map k (PatchTarget p) -> PatchMapWithMove k p
216−patchThatSortsMapWith cmp m = PatchMapWithMove $ Map.fromList $ catMaybes $ zipWith g unsorted sorted
200+patchThatSortsMapWith cmp = PatchMapWithMove' . PM.patchThatSortsMapWith cmp
217− where unsorted = Map.toList m
218− sorted = sortBy (cmp `on` snd) unsorted
219− f (to, _) (from, _) = if to == from then Nothing else
221− reverseMapping = Map.fromList $ catMaybes $ zipWith f unsorted sorted
222− g (to, _) (from, _) = if to == from then Nothing else
223− let Just movingTo = Map.lookup from reverseMapping
224− in Just (to, NodeInfo (From_Move from mempty) $ To_Move movingTo mempty)
226-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
202-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
227-- will produce a 'Map' with the same values as the second 'Map' but with the
203-- will produce a 'Map' with the same values as the second 'Map' but with the
228-- values sorted with the given ordering function.
204-- values sorted with the given ordering function.
229−patchThatChangesAndSortsMapWith
205+patchThatChangesAndSortsMapWith :: forall k v. (Ord k, Ord v) => (v -> v -> Ordering) -> Map k v -> Map k v -> PatchMapWithMove k v
230− :: forall k p. (Ord k, Ord (PatchTarget p), Monoid p)
231− => (PatchTarget p -> PatchTarget p -> Ordering)
232− -> Map k (PatchTarget p) -> Map k (PatchTarget p) -> PatchMapWithMove k p
233patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatChangesMap oldByIndex newByIndex
206patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatChangesMap oldByIndex newByIndex
234 where newList = Map.toList newByIndexUnsorted
207 where newList = Map.toList newByIndexUnsorted
235 newByIndex = Map.fromList $ zip (fst <$> newList) $ sortBy cmp $ snd <$> newList
208 newByIndex = Map.fromList $ zip (fst <$> newList) $ sortBy cmp $ snd <$> newList
237-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
210-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
238-- will produce the second 'Map'.
211-- will produce the second 'Map'.
239−patchThatChangesMap
212+patchThatChangesMap :: (Ord k, Ord v) => Map k v -> Map k v -> PatchMapWithMove k v
240− :: (Ord k, Ord (PatchTarget p), Monoid p)
241− => Map k (PatchTarget p) -> Map k (PatchTarget p) -> PatchMapWithMove k p
242−patchThatChangesMap oldByIndex newByIndex = patch
213+patchThatChangesMap oldByIndex newByIndex = PatchMapWithMove' $
243− where oldByValue = Map.fromListWith Set.union $ swap . first Set.singleton <$> Map.toList oldByIndex
214+ PM.patchThatChangesMap oldByIndex newByIndex
244− (insertsAndMoves, unusedValuesByValue) = flip runState oldByValue $ do
215+
245− let f k v = do
216+--
246− remainingValues <- get
217+-- NodeInfo
247− let putRemainingKeys remainingKeys = put $ if Set.null remainingKeys
218+--
248− then Map.delete v remainingValues
219+
249− else Map.insert v remainingKeys remainingValues
220+-- | Holds the information about each key: where its new value should come from,
250− case Map.lookup v remainingValues of
221+-- and where its old value should go to
251− Nothing -> return $ NodeInfo (From_Insert v) $ To_Move undefined undefined -- There's no existing value we can take
222+newtype NodeInfo k (v :: Type) = NodeInfo' { unNodeInfo' :: PM.NodeInfo k (Proxy v) }
253− if k `Set.member` fromKs
224+deriving instance (Show k, Show p) => Show (NodeInfo k p)
254− then do
225+deriving instance (Read k, Read p) => Read (NodeInfo k p)
255− putRemainingKeys $ Set.delete k fromKs
226+deriving instance (Eq k, Eq p) => Eq (NodeInfo k p)
256− return $ NodeInfo (From_Move k mempty) $ To_Move undefined undefined -- There's an existing value, and it's here, so no patch necessary
227+deriving instance (Ord k, Ord p) => Ord (NodeInfo k p)
258− (fromK, remainingKeys) <- return . fromJust $ Set.minView fromKs -- There's an existing value, but it's not here; move it here
229+{-# COMPLETE NodeInfo #-}
259− putRemainingKeys remainingKeys
230+pattern NodeInfo :: To k -> From k v -> NodeInfo k v
260− return $ NodeInfo (From_Move fromK mempty) $ To_Move undefined undefined
231+_nodeInfo_to :: NodeInfo k v -> To k
261− Map.traverseWithKey f newByIndex
232+_nodeInfo_from :: NodeInfo k v -> From k v
262− unusedOldKeys = fold unusedValuesByValue
233+pattern NodeInfo { _nodeInfo_to, _nodeInfo_from } = NodeInfo'
263− pointlessMove k = \case
234+ PM.NodeInfo
264− From_Move k' _ | k == k' -> True
235+ { PM._nodeInfo_to = _nodeInfo_to
265− _ -> False
236+ , PM._nodeInfo_from = Coerce _nodeInfo_from
266− keyWasMoved k = if k `Map.member` oldByIndex && not (k `Set.member` unusedOldKeys)
237+ }
267− then To_Move undefined undefined
238+
268− else To_NonMove
239+_NodeInfo
269− patch = unsafePatchMapWithMove $ Map.filterWithKey
240+ :: Iso
270− (\k -> not . pointlessMove k . _nodeInfo_from)
241+ (NodeInfo k0 v0)
271− $ Map.mergeWithKey
242+ (NodeInfo k1 v1)
272− (\k a _ -> Just $ nodeInfoSetTo (keyWasMoved k) a)
243+ (PM.NodeInfo k0 (Proxy v0))
273− (Map.mapWithKey $ \k -> nodeInfoSetTo $ keyWasMoved k)
244+ (PM.NodeInfo k1 (Proxy v1))
274− (Map.mapWithKey $ \k _ -> NodeInfo From_Delete $ keyWasMoved k)
245+_NodeInfo = iso unNodeInfo' NodeInfo'
276− oldByIndex
247+instance Functor (NodeInfo k) where
248+ fmap f = runIdentity . traverse (Identity . f)
250+instance Foldable (NodeInfo k) where
251+ foldMap = foldMapDefault
253+instance Traversable (NodeInfo k) where
254+ traverse = bitraverseNodeInfo pure
260+ -> NodeInfo k0 v0 -> f (NodeInfo k1 v1)
261+bitraverseNodeInfo fk fv = fmap coerce
262+ . PM.bitraverseNodeInfo fk (\ ~Proxy -> pure Proxy) fv
278-- | Change the 'From' value of a 'NodeInfo'
265-- | Change the 'From' value of a 'NodeInfo'
280− :: (From k v -> From k v) -> NodeInfo k v -> NodeInfo k v
266+nodeInfoMapFrom :: (From k v -> From k v) -> NodeInfo k v -> NodeInfo k v
281−nodeInfoMapFrom f ni = ni { _nodeInfo_from = f $ _nodeInfo_from ni }
267+nodeInfoMapFrom f = coerce . PM.nodeInfoMapFrom (unFrom' . f . From') . coerce
283-- | Change the 'From' value of a 'NodeInfo', using a 'Functor' (or
269-- | Change the 'From' value of a 'NodeInfo', using a 'Functor' (or
284-- 'Applicative', 'Monad', etc.) action to get the new value
270-- 'Applicative', 'Monad', etc.) action to get the new value
285nodeInfoMapMFrom
271nodeInfoMapMFrom
273+ => (From k v -> f (From k v))
286− :: Functor f => (From k v -> f (From k v)) -> NodeInfo k v -> f (NodeInfo k v)
274+ -> NodeInfo k v -> f (NodeInfo k v)
287−nodeInfoMapMFrom f ni = fmap (\result -> ni { _nodeInfo_from = result }) $ f $ _nodeInfo_from ni
275+nodeInfoMapMFrom f = fmap coerce
276+ . PM.nodeInfoMapMFrom (fmap unFrom' . f . From')
289-- | Set the 'To' field of a 'NodeInfo'
279-- | Set the 'To' field of a 'NodeInfo'
291− :: To k p -> NodeInfo k p -> NodeInfo k p
280+nodeInfoSetTo :: To k -> NodeInfo k v -> NodeInfo k v
292−nodeInfoSetTo to ni = ni { _nodeInfo_to = to }
281+nodeInfoSetTo = coerce . PM.nodeInfoSetTo
294−-- | Helper data structure used for composing patches using the monoid instance.
283+--
295−data Fixup k p
284+-- From
296− = Fixup_Delete
285+--
297− | Fixup_Update (These (From k p) (To k p))
286+
298−
287+-- | Describe how a key's new value should be produced
299−-- | Compose patches having the same effect as applying the patches in turn:
288+newtype From k (v :: Type) = From' { unFrom' :: PM.From k (Proxy v) }
300−-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
289+
301−instance ( Ord k
290+{-# COMPLETE From_Insert, From_Delete, From_Move #-}
302− , DecidablyEmpty p
291+
303− , Patch p
292+-- | Insert the given value here
304− ) => Semigroup (PatchMapWithMove k p) where
293+pattern From_Insert :: v -> From k v
305− PatchMapWithMove ma <> PatchMapWithMove mb = PatchMapWithMove m
294+pattern From_Insert v = From' (PM.From_Insert v)
307− connections = Map.toList $ Map.intersectionWithKey (\_ a b -> (_nodeInfo_to a, _nodeInfo_from b)) ma mb
296+-- | Delete the existing value, if any, from here
308− h :: (k, (To k p, From k p)) -> [(k, Fixup k p)]
297+pattern From_Delete :: From k v
309− h (_, (editAfter, editBefore)) = case (editAfter, editBefore) of
298+pattern From_Delete = From' PM.From_Delete
310− (To_Move toAfter p1, From_Move fromBefore p0)
299+
311− | fromBefore == toAfter && isNull p0
300+-- | Move the value here from the given key
312− -> [(toAfter, Fixup_Delete)]
301+pattern From_Move :: k -> From k v
313− | otherwise
302+pattern From_Move k = From' (PM.From_Move k Proxy)
314− -> [ (toAfter, Fixup_Update (This $ From_Move fromBefore $ p1 <> p0))
303+
315− , (fromBefore, Fixup_Update (That $ To_Move toAfter $ p1 <> p0))
304+bitraverseFrom
316− ]
305+ :: Applicative f
317− (To_NonMove, From_Move fromBefore _) ->
306+ => (k0 -> f k1)
318− -- The item is destroyed in the second patch, so indicate that it is
307+ -> (v0 -> f v1)
319− -- destroyed in the source map
308+ -> From k0 v0 -> f (From k1 v1)
320− [(fromBefore, Fixup_Update (That editAfter))]
309+bitraverseFrom fk fv = fmap coerce
321− (To_Move toAfter p, From_Insert val) ->
310+ . PM.bitraverseFrom fk (\ ~Proxy -> pure Proxy) fv
322− [(toAfter, Fixup_Update $ This $ From_Insert $ applyAlways p val)]
311+ . coerce
323− (To_Move toAfter _, From_Delete) ->
312+
324− [(toAfter, Fixup_Update $ This From_Delete)]
313+makeWrapped ''PatchMapWithMove
325− (To_NonMove, _) -> []
314+makeWrapped ''NodeInfo
326− mergeFixups _ Fixup_Delete Fixup_Delete = Fixup_Delete
315+makeWrapped ''From
327− mergeFixups _ (Fixup_Update a) (Fixup_Update b)
328− | This x <- a, That y <- b
329− = Fixup_Update $ These x y
330− | That y <- a, This x <- b
331− = Fixup_Update $ These x y
332− mergeFixups _ _ _ = error "PatchMapWithMove: incompatible fixups"
333− fixups = Map.fromListWithKey mergeFixups $ concatMap h connections
334− combineNodeInfos _ nia nib = NodeInfo
335− { _nodeInfo_from = _nodeInfo_from nia
336− , _nodeInfo_to = _nodeInfo_to nib
338− applyFixup _ ni = \case
339− Fixup_Delete -> Nothing
340− Fixup_Update u -> Just $ NodeInfo
341− { _nodeInfo_from = fromMaybe (_nodeInfo_from ni) $ getHere u
342− , _nodeInfo_to = fromMaybe (_nodeInfo_to ni) $ getThere u
344− m = Map.differenceWithKey applyFixup (Map.unionWithKey combineNodeInfos ma mb) fixups
345− getHere :: These a b -> Maybe a
350− getThere :: These a b -> Maybe b
356−--TODO: Figure out how to implement this in terms of PatchDMapWithMove rather than duplicating it here
357−-- | Compose patches having the same effect as applying the patches in turn:
358−-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
363− ) => Monoid (PatchMapWithMove k p) where
364− mempty = PatchMapWithMove mempty