@@ -13,45 +13,14 @@
1313
1414-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
1515-- another
16−module Data.Patch.MapWithPatchingMove
17− ( PatchMapWithPatchingMove (..)
18− , patchMapWithPatchingMove
19− , patchMapWithPatchingMoveInsertAll
20− , insertMapKey
21− , moveMapKey
22− , swapMapKey
23− , deleteMapKey
24− , unsafePatchMapWithPatchingMove
25− , patchMapWithPatchingMoveNewElements
26− , patchMapWithPatchingMoveNewElementsMap
27− , patchThatSortsMapWith
28− , patchThatChangesAndSortsMapWith
29− , patchThatChangesMap
30−
31− -- * Node Info
32− , NodeInfo (..)
33− , bitraverseNodeInfo
34− , nodeInfoMapFrom
35− , nodeInfoMapMFrom
36− , nodeInfoSetTo
37−
38− -- * From
39− , From(..)
40− , bitraverseFrom
41−
42− -- * To
43− , To
44−
45− -- TODO internals module
46− , Fixup (..)
47− ) where
16+module Data.Patch.MapWithPatchingMove where
4817
4918import Data.Patch.Class
5019
51−import Control.Lens hiding (from, to)
20+import Control.Arrow
5221import Control.Lens.TH (makeWrapped)
53−import Data.Align (align)
54−import Data.Foldable (toList)
22+import Control.Monad.Trans.State
23+import Data.Foldable
5524import Data.Function
5625import Data.List
5726import Data.Map (Map)
@@ -61,9 +30,9 @@ import Data.Maybe
6130import Data.Semigroup (Semigroup (..))
6231#endif
6332import Data.Monoid.DecidablyEmpty
64−import Data.Set (Set)
6533import qualified Data.Set as Set
6634import Data.These (These (..))
35+import Data.Tuple
6736
6837-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
6938-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
@@ -86,6 +55,57 @@ deriving instance ( Ord k
8655 , Patch p
8756 ) => DecidablyEmpty (PatchMapWithPatchingMove k p)
8857
58+-- | Holds the information about each key: where its new value should come from,
59+-- and where its old value should go to
60+data NodeInfo k p = NodeInfo
61+ { _nodeInfo_from :: !(From k p)
62+ -- ^ Where do we get the new value for this key?
63+ , _nodeInfo_to :: !(To k)
64+ -- ^ If the old value is being kept (i.e. moved rather than deleted or
65+ -- replaced), where is it going?
66+ }
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)
71+
72+bitraverseNodeInfo
73+ :: Applicative f
74+ => (k0 -> f k1)
75+ -> (p0 -> f p1)
76+ -> (PatchTarget p0 -> f (PatchTarget p1))
77+ -> NodeInfo k0 p0 -> f (NodeInfo k1 p1)
78+bitraverseNodeInfo fk fp fpt (NodeInfo from to) = NodeInfo
79+ <$> bitraverseFrom fk fp fpt from
80+ <*> traverse fk to
81+
82+-- | Describe how a key's new value should be produced
83+data From k p
84+ = From_Insert (PatchTarget p) -- ^ Insert the given value here
85+ | From_Delete -- ^ Delete the existing value, if any, from here
86+ | From_Move !k !p -- ^ Move the value here from the given key, and apply the given patch
87+
88+bitraverseFrom
89+ :: Applicative f
90+ => (k0 -> f k1)
91+ -> (p0 -> f p1)
92+ -> (PatchTarget p0 -> f (PatchTarget p1))
93+ -> From k0 p0 -> f (From k1 p1)
94+bitraverseFrom fk fp fpt = \case
95+ From_Insert pt -> From_Insert <$> fpt pt
96+ From_Delete -> pure From_Delete
97+ From_Move k p -> From_Move <$> fk k <*> fp p
98+
99+deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (From k p)
100+deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (From k p)
101+deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (From k p)
102+deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (From k p)
103+
104+-- | Describe where a key's old value will go. If this is 'Just', that means
105+-- the key's old value will be moved to the given other key; if it is 'Nothing',
106+-- that means it will be deleted.
107+type To = Maybe
108+
89109-- | Create a 'PatchMapWithPatchingMove', validating it
90110patchMapWithPatchingMove
91111 :: Ord k => Map k (NodeInfo k p) -> Maybe (PatchMapWithPatchingMove k p)
@@ -136,8 +156,8 @@ moveMapKey src dst
136156-- @
137157-- let aMay = Map.lookup a map
138158-- bMay = Map.lookup b map
139−-- in maybe id (Map.insert a) (bMay <> aMay)
140−-- . maybe id (Map.insert b) (aMay <> bMay)
159+-- in maybe id (Map.insert a) (bMay `mplus` aMay)
160+-- . maybe id (Map.insert b) (aMay `mplus` bMay)
141161-- . Map.delete a . Map.delete b $ map
142162-- @
143163swapMapKey
@@ -229,70 +249,39 @@ patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatCha
229249
230250-- | Create a 'PatchMapWithPatchingMove' that, if applied to the first 'Map' provided,
231251-- will produce the second 'Map'.
232−-- Note: this will never produce a patch on a value.
233252patchThatChangesMap
234− :: forall k p
235− . (Ord k, Ord (PatchTarget p), Monoid p)
253+ :: (Ord k, Ord (PatchTarget p), Monoid p)
236254 => Map k (PatchTarget p) -> Map k (PatchTarget p) -> PatchMapWithPatchingMove k p
237255patchThatChangesMap oldByIndex newByIndex = patch
238− where invert :: Map k (PatchTarget p) -> Map (PatchTarget p) (Set k)
239− invert = Map.fromListWith (<>) . fmap (\(k, v) -> (v, Set.singleton k)) . Map.toList
240− -- In the places where we use unionDistinct, a non-distinct key indicates a bug in this function
241− unionDistinct :: forall k' v'. Ord k' => Map k' v' -> Map k' v' -> Map k' v'
242− unionDistinct = Map.unionWith (error "patchThatChangesMap: non-distinct keys")
243− unionPairDistinct :: (Map k (From k v), Map k (To k)) -> (Map k (From k v), Map k (To k)) -> (Map k (From k v), Map k (To k))
244− unionPairDistinct (oldFroms, oldTos) (newFroms, newTos) = (unionDistinct oldFroms newFroms, unionDistinct oldTos newTos)
245− -- Generate patch info for a single value
246− -- Keys that are found in both the old and new sets will not be patched
247− -- Keys that are found in only the old set will be moved to a new position if any are available; otherwise they will be deleted
248− -- Keys that are found in only the new set will be populated by moving an old key if any are available; otherwise they will be inserted
249− patchSingleValue :: PatchTarget p -> Set k -> Set k -> (Map k (From k p), Map k (To k))
250− patchSingleValue v oldKeys newKeys = foldl' unionPairDistinct mempty $ align (toList $ oldKeys `Set.difference` newKeys) (toList $ newKeys `Set.difference` oldKeys) <&> \case
251− This oldK -> (mempty, Map.singleton oldK Nothing) -- There's nowhere for this value to go, so we know we are deleting it
252− That newK -> (Map.singleton newK $ From_Insert v, mempty) -- There's nowhere fo this value to come from, so we know we are inserting it
253− These oldK newK -> (Map.singleton newK $ From_Move oldK mempty, Map.singleton oldK $ Just newK)
254− -- Run patchSingleValue on a These. Missing old or new sets are considered empty
255− patchSingleValueThese :: PatchTarget p -> These (Set k) (Set k) -> (Map k (From k p), Map k (To k))
256− patchSingleValueThese v = \case
257− This oldKeys -> patchSingleValue v oldKeys mempty
258− That newKeys -> patchSingleValue v mempty newKeys
259− These oldKeys newKeys -> patchSingleValue v oldKeys newKeys
260− -- Generate froms and tos for all values, then merge them together
261− (froms, tos) = foldl' unionPairDistinct mempty $ Map.mapWithKey patchSingleValueThese $ align (invert oldByIndex) (invert newByIndex)
262− patch = unsafePatchMapWithPatchingMove $ align froms tos <&> \case
263− This from -> NodeInfo from Nothing -- Since we don't have a 'to' record for this key, that must mean it isn't being moved anywhere, so it should be deleted.
264− That to -> NodeInfo From_Delete to -- Since we don't have a 'from' record for this key, it must be getting deleted
265− These from to -> NodeInfo from to
266−
267−--
268−-- NodeInfo
269−--
270−
271−-- | Holds the information about each key: where its new value should come from,
272−-- and where its old value should go to
273−data NodeInfo k p = NodeInfo
274− { _nodeInfo_from :: !(From k p)
275− -- ^ Where do we get the new value for this key?
276− , _nodeInfo_to :: !(To k)
277− -- ^ If the old value is being kept (i.e. moved rather than deleted or
278− -- replaced), where is it going?
279− }
280−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (NodeInfo k p)
281−deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (NodeInfo k p)
282−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (NodeInfo k p)
283−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (NodeInfo k p)
284−
285−-- | Traverse the 'NodeInfo' over the key, patch, and patch target. Because of
286−-- the type families here, this doesn't it any bi- or tri-traversal class.
287−bitraverseNodeInfo
288− :: Applicative f
289− => (k0 -> f k1)
290− -> (p0 -> f p1)
291− -> (PatchTarget p0 -> f (PatchTarget p1))
292− -> NodeInfo k0 p0 -> f (NodeInfo k1 p1)
293−bitraverseNodeInfo fk fp fpt (NodeInfo from to) = NodeInfo
294− <$> bitraverseFrom fk fp fpt from
295− <*> traverse fk to
256+ where oldByValue = Map.fromListWith Set.union $ swap . first Set.singleton <$> Map.toList oldByIndex
257+ (insertsAndMoves, unusedValuesByValue) = flip runState oldByValue $ do
258+ let f k v = do
259+ remainingValues <- get
260+ let putRemainingKeys remainingKeys = put $ if Set.null remainingKeys
261+ then Map.delete v remainingValues
262+ else Map.insert v remainingKeys remainingValues
263+ case Map.lookup v remainingValues of
264+ Nothing -> return $ NodeInfo (From_Insert v) $ Just undefined -- There's no existing value we can take
265+ Just fromKs ->
266+ if k `Set.member` fromKs
267+ then do
268+ putRemainingKeys $ Set.delete k fromKs
269+ return $ NodeInfo (From_Move k mempty) $ Just undefined -- There's an existing value, and it's here, so no patch necessary
270+ else do
271+ (fromK, remainingKeys) <- return $
272+ fromMaybe (error "PatchMapWithPatchingMove.patchThatChangesMap: impossible: fromKs was empty") $
273+ Set.minView fromKs -- There's an existing value, but it's not here; move it here
274+ putRemainingKeys remainingKeys
275+ return $ NodeInfo (From_Move fromK mempty) $ Just undefined
276+ Map.traverseWithKey f newByIndex
277+ unusedOldKeys = fold unusedValuesByValue
278+ pointlessMove k = \case
279+ From_Move k' _ | k == k' -> True
280+ _ -> False
281+ keyWasMoved k = if k `Map.member` oldByIndex && not (k `Set.member` unusedOldKeys)
282+ then Just undefined
283+ else Nothing
284+ patch = unsafePatchMapWithPatchingMove $ Map.filterWithKey (\k -> not . pointlessMove k . _nodeInfo_from) $ Map.mergeWithKey (\k a _ -> Just $ nodeInfoSetTo (keyWasMoved k) a) (Map.mapWithKey $ \k -> nodeInfoSetTo $ keyWasMoved k) (Map.mapWithKey $ \k _ -> NodeInfo From_Delete $ keyWasMoved k) insertsAndMoves oldByIndex
296285
297286-- | Change the 'From' value of a 'NodeInfo'
298287nodeInfoMapFrom
@@ -310,47 +299,6 @@ nodeInfoSetTo
310299 :: To k -> NodeInfo k v -> NodeInfo k v
311300nodeInfoSetTo to ni = ni { _nodeInfo_to = to }
312301
313−--
314−-- From
315−--
316−
317−-- | Describe how a key's new value should be produced
318−data From k p
319− = From_Insert (PatchTarget p) -- ^ Insert the given value here
320− | From_Delete -- ^ Delete the existing value, if any, from here
321− | From_Move !k !p -- ^ Move the value here from the given key, and apply the given patch
322−
323−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (From k p)
324−deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (From k p)
325−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (From k p)
326−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (From k p)
327−
328−-- | Traverse the 'From' over the key, patch, and patch target. Because of
329−-- the type families here, this doesn't it any bi- or tri-traversal class.
330−bitraverseFrom
331− :: Applicative f
332− => (k0 -> f k1)
333− -> (p0 -> f p1)
334− -> (PatchTarget p0 -> f (PatchTarget p1))
335− -> From k0 p0 -> f (From k1 p1)
336−bitraverseFrom fk fp fpt = \case
337− From_Insert pt -> From_Insert <$> fpt pt
338− From_Delete -> pure From_Delete
339− From_Move k p -> From_Move <$> fk k <*> fp p
340−
341−--
342−-- To
343−--
344−
345−-- | Describe where a key's old value will go. If this is 'Just', that means
346−-- the key's old value will be moved to the given other key; if it is 'Nothing',
347−-- that means it will be deleted.
348−type To = Maybe
349−
350−--
351−-- Fixup
352−--
353−
354302-- | Helper data structure used for composing patches using the monoid instance.
355303data Fixup k v
356304 = Fixup_Delete