@@ -1,11 +1,14 @@
1−{-# LANGUAGE DeriveFoldable #-}
2−{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE DeriveTraversable #-}
1{-# LANGUAGE DeriveTraversable #-}
4{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE LambdaCase #-}
4{-# LANGUAGE LambdaCase #-}
5+{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE PatternGuards #-}
6{-# LANGUAGE PatternGuards #-}
7{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8+{-# LANGUAGE TemplateHaskell #-}
9+{-# LANGUAGE TypeApplications #-}
8{-# LANGUAGE TypeFamilies #-}
10{-# LANGUAGE TypeFamilies #-}
9-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
12-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
11module Data.Patch.MapWithMove where
14module Data.Patch.MapWithMove where
@@ -13,6 +16,7 @@ module Data.Patch.MapWithMove where
13import Data.Patch.Class
16import Data.Patch.Class
15import Control.Arrow
18import Control.Arrow
19+import Control.Lens hiding (from, to)
16import Control.Monad.Trans.State
20import Control.Monad.Trans.State
17import Data.Foldable
21import Data.Foldable
18import Data.Function
22import Data.Function
@@ -28,7 +32,13 @@ import Data.Tuple
28-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
32-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
29-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
33-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
30-- and vice versa. There should never be any unpaired From/To keys.
34-- and vice versa. There should never be any unpaired From/To keys.
31−newtype PatchMapWithMove k v = PatchMapWithMove (Map k (NodeInfo k v)) deriving (Show, Eq, Ord, Functor, Foldable, Traversable)
35+newtype PatchMapWithMove k v = PatchMapWithMove
36+ { -- | Extract the internal representation of the 'PatchMapWithMove'
37+ unPatchMapWithMove :: Map k (NodeInfo k v)
39+ deriving ( Show, Read, Eq, Ord
40+ , Functor, Foldable, Traversable
33-- | Holds the information about each key: where its new value should come from,
43-- | Holds the information about each key: where its new value should come from,
34-- and where its old value should go to
44-- and where its old value should go to
@@ -53,6 +63,13 @@ data From k v
53-- that means it will be deleted.
63-- that means it will be deleted.
54type To = Maybe
64type To = Maybe
66+makeWrapped ''PatchMapWithMove
68+instance FunctorWithIndex k (PatchMapWithMove k)
69+instance FoldableWithIndex k (PatchMapWithMove k)
70+instance TraversableWithIndex k (PatchMapWithMove k) where
71+ itraverse f (PatchMapWithMove x) = PatchMapWithMove <$> itraverse (traverse . f) x
56-- | Create a 'PatchMapWithMove', validating it
73-- | Create a 'PatchMapWithMove', validating it
57patchMapWithMove :: Ord k => Map k (NodeInfo k v) -> Maybe (PatchMapWithMove k v)
74patchMapWithMove :: Ord k => Map k (NodeInfo k v) -> Maybe (PatchMapWithMove k v)
58patchMapWithMove m = if valid then Just $ PatchMapWithMove m else Nothing
75patchMapWithMove m = if valid then Just $ PatchMapWithMove m else Nothing
@@ -70,10 +87,6 @@ patchMapWithMoveInsertAll m = PatchMapWithMove $ flip fmap m $ \v -> NodeInfo
70 , _nodeInfo_to = Nothing
87 , _nodeInfo_to = Nothing
73−-- | Extract the internal representation of the 'PatchMapWithMove'
74−unPatchMapWithMove :: PatchMapWithMove k v -> Map k (NodeInfo k v)
75−unPatchMapWithMove (PatchMapWithMove p) = p
77-- | Make a @'PatchMapWithMove' k v@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
90-- | Make a @'PatchMapWithMove' k v@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
78insertMapKey :: k -> v -> PatchMapWithMove k v
91insertMapKey :: k -> v -> PatchMapWithMove k v
79insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) Nothing
92insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) Nothing