@@ -1,11 +1,17 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveTraversable #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE LambdaCase #-}
5{-# LANGUAGE LambdaCase #-}
6+{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE PatternGuards #-}
7{-# LANGUAGE PatternGuards #-}
5{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE ScopedTypeVariables #-}
6{-# LANGUAGE StandaloneDeriving #-}
9{-# LANGUAGE StandaloneDeriving #-}
10+{-# LANGUAGE TemplateHaskell #-}
11+{-# LANGUAGE TypeApplications #-}
7{-# LANGUAGE TypeFamilies #-}
12{-# LANGUAGE TypeFamilies #-}
8{-# LANGUAGE UndecidableInstances #-}
13{-# LANGUAGE UndecidableInstances #-}
9-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
15-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
11module Data.Patch.MapWithMove where
17module Data.Patch.MapWithMove where
@@ -13,6 +19,7 @@ module Data.Patch.MapWithMove where
13import Data.Patch.Class
19import Data.Patch.Class
15import Control.Arrow
21import Control.Arrow
22+import Control.Lens hiding (from, to)
16import Control.Monad.Trans.State
23import Control.Monad.Trans.State
17import Data.Foldable
24import Data.Foldable
18import Data.Function
25import Data.Function
@@ -31,8 +38,12 @@ import Data.Tuple
31-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
38-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
32-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
39-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
33-- and vice versa. There should never be any unpaired From/To keys.
40-- 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)
35deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (PatchMapWithMove k p)
45deriving 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)
36deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (PatchMapWithMove k p)
47deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (PatchMapWithMove k p)
37deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (PatchMapWithMove k p)
48deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (PatchMapWithMove k p)
@@ -67,11 +78,11 @@ deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (From k p)
67data To k p
78data To k p
68 = To_NonMove
79 = To_NonMove
69 | To_Move !k !p
80 | To_Move !k !p
81+ deriving ( Show, Read, Eq, Ord
82+ , Functor, Foldable, Traversable
71−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (To k p)
85+makeWrapped ''PatchMapWithMove
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)
76-- | Create a 'PatchMapWithMove', validating it
87-- | Create a 'PatchMapWithMove', validating it
77patchMapWithMove
88patchMapWithMove
@@ -94,12 +105,7 @@ patchMapWithMoveInsertAll m = PatchMapWithMove $ flip fmap m $ \v -> NodeInfo
94 , _nodeInfo_to = To_NonMove
105 , _nodeInfo_to = To_NonMove
97−-- | Extract the internal representation of the 'PatchMapWithMove'
99− :: PatchMapWithMove k p -> Map k (NodeInfo k p)
100−unPatchMapWithMove (PatchMapWithMove p) = p
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'.
103insertMapKey
109insertMapKey
104 :: k -> PatchTarget p -> PatchMapWithMove k p
110 :: k -> PatchTarget p -> PatchMapWithMove k p
105insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) To_NonMove
111insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) To_NonMove