1- {-# LANGUAGE DeriveFoldable #-}
2- {-# LANGUAGE DeriveFunctor #-}
31{-# LANGUAGE DeriveTraversable #-}
42{-# LANGUAGE FlexibleContexts #-}
3+ {-# LANGUAGE FlexibleInstances #-}
54{-# LANGUAGE LambdaCase #-}
5+ {-# LANGUAGE MultiParamTypeClasses #-}
66{-# LANGUAGE PatternGuards #-}
77{-# LANGUAGE ScopedTypeVariables #-}
8+ {-# LANGUAGE TemplateHaskell #-}
9+ {-# LANGUAGE TypeApplications #-}
810{-# LANGUAGE TypeFamilies #-}
11+
912-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
1013-- another
1114module Data.Patch.MapWithMove where
1215
1316import Data.Patch.Class
1417
1518import Control.Arrow
19+ import Control.Lens hiding (from , to )
1620import Control.Monad.Trans.State
1721import Data.Foldable
1822import Data.Function
@@ -28,7 +32,13 @@ import Data.Tuple
2832-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
2933-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
3034-- 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 )
38+ }
39+ deriving ( Show , Read , Eq , Ord
40+ , Functor , Foldable , Traversable
41+ )
3242
3343-- | Holds the information about each key: where its new value should come from,
3444-- and where its old value should go to
@@ -53,6 +63,13 @@ data From k v
5363-- that means it will be deleted.
5464type To = Maybe
5565
66+ makeWrapped ''PatchMapWithMove
67+
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
72+
5673-- | Create a 'PatchMapWithMove', validating it
5774patchMapWithMove :: Ord k => Map k (NodeInfo k v ) -> Maybe (PatchMapWithMove k v )
5875patchMapWithMove m = if valid then Just $ PatchMapWithMove m else Nothing
@@ -70,10 +87,6 @@ patchMapWithMoveInsertAll m = PatchMapWithMove $ flip fmap m $ \v -> NodeInfo
7087 , _nodeInfo_to = Nothing
7188 }
7289
73- -- | Extract the internal representation of the 'PatchMapWithMove'
74- unPatchMapWithMove :: PatchMapWithMove k v -> Map k (NodeInfo k v )
75- unPatchMapWithMove (PatchMapWithMove p) = p
76-
7790-- | Make a @'PatchMapWithMove' k v@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
7891insertMapKey :: k -> v -> PatchMapWithMove k v
7992insertMapKey k v = PatchMapWithMove . Map. singleton k $ NodeInfo (From_Insert v) Nothing
0 commit comments