@@ -1,11 +1,17 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveTraversable #-}
23{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleInstances #-}
35{-# LANGUAGE LambdaCase #-}
6+{-# LANGUAGE MultiParamTypeClasses #-}
47{-# LANGUAGE PatternGuards #-}
58{-# LANGUAGE ScopedTypeVariables #-}
69{-# LANGUAGE StandaloneDeriving #-}
10+{-# LANGUAGE TemplateHaskell #-}
11+{-# LANGUAGE TypeApplications #-}
712{-# LANGUAGE TypeFamilies #-}
813{-# LANGUAGE UndecidableInstances #-}
14+
915-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
1016-- another
1117module Data.Patch.MapWithMove where
@@ -13,6 +19,7 @@ module Data.Patch.MapWithMove where
1319import Data.Patch.Class
1420
1521import Control.Arrow
22+import Control.Lens hiding (from, to)
1623import Control.Monad.Trans.State
1724import Data.Foldable
1825import Data.Function
@@ -31,8 +38,12 @@ import Data.Tuple
3138-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
3239-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
3340-- and vice versa. There should never be any unpaired From/To keys.
34−newtype PatchMapWithMove k p = PatchMapWithMove (Map k (NodeInfo k p))
41+newtype PatchMapWithMove k p = PatchMapWithMove
42+ { -- | Extract the internal representation of the 'PatchMapWithMove'
43+ unPatchMapWithMove :: Map k (NodeInfo k p)
44+ }
3545deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (PatchMapWithMove k p)
46+deriving instance (Ord k, Read k, Read p, Read (PatchTarget p)) => Read (PatchMapWithMove k p)
3647deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (PatchMapWithMove k p)
3748deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (PatchMapWithMove k p)
3849
@@ -67,11 +78,11 @@ deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (From k p)
6778data To k p
6879 = To_NonMove
6980 | To_Move !k !p
81+ deriving ( Show, Read, Eq, Ord
82+ , Functor, Foldable, Traversable
83+ )
7084
71−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (To k p)
72−deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (To k p)
73−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (To k p)
74−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (To k p)
85+makeWrapped ''PatchMapWithMove
7586
7687-- | Create a 'PatchMapWithMove', validating it
7788patchMapWithMove
@@ -94,12 +105,7 @@ patchMapWithMoveInsertAll m = PatchMapWithMove $ flip fmap m $ \v -> NodeInfo
94105 , _nodeInfo_to = To_NonMove
95106 }
96107
97−-- | Extract the internal representation of the 'PatchMapWithMove'
98−unPatchMapWithMove
99− :: PatchMapWithMove k p -> Map k (NodeInfo k p)
100−unPatchMapWithMove (PatchMapWithMove p) = p
101−
102−-- | Make a @'PatchMapWithMove' k p@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
108+-- | Make a @'PatchMapWithMove' k p@ which has the effect of inserting or replacing a value @v@ at the given key @k@, like 'Map.insert'.
103109insertMapKey
104110 :: k -> PatchTarget p -> PatchMapWithMove k p
105111insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) To_NonMove