on c3cac44 help sign in

develop and c3cac44 have 2 merge bases. This is what c3cac44 changed since the first, 5335f5d, as git diff develop...c3cac44 shows it.

13 files changed+885−266
since splitsnapshots
unifiedsplit
ChangeLog.md+6 patch.cabal+5−1 src/Control/Category/DecidablyEmpty.hs+9 src/Data/Functor/Misc.hs+28−5 src/Data/Monoid/DecidablyEmpty.hs+5 src/Data/Patch.hs+29−19 src/Data/Patch/Class.hs+136−4 src/Data/Patch/DMap.hs+3−1 src/Data/Patch/DMapWithMove.hs+405−151 src/Data/Patch/DMapWithMove/By.hs+89 src/Data/Patch/IntMap.hs+3−1 src/Data/Patch/Map.hs+3−1 src/Data/Patch/MapWithMove.hs+164−83
ChangeLog.md+6
@@ -1,5 +1,11 @@
11# Revision history for patch
22
3+## Unreleased
4+
5+* `PatchMapWithMove` supports moves with a patch.
6+
7+* `PatchDMapWithMove` supports moves with a patch.
8+
39## 0.0.2.0
410
511* Consistently provide:
patch.cabal+5−1
@@ -40,11 +40,14 @@ library
4040 , transformers >= 0.5.6.0 && < 0.6
4141 , witherable >= 0.3 && < 0.3.2
4242
43− exposed-modules: Data.Functor.Misc
43+ exposed-modules: Control.Category.DecidablyEmpty
44+ , Data.Functor.Misc
45+ , Data.Monoid.DecidablyEmpty
4446 , Data.Patch
4547 , Data.Patch.Class
4648 , Data.Patch.DMap
4749 , Data.Patch.DMapWithMove
50+ , Data.Patch.DMapWithMove.By
4851 , Data.Patch.IntMap
4952 , Data.Patch.Map
5053 , Data.Patch.MapWithMove
@@ -53,6 +56,7 @@ library
5356
5457 if flag(split-these)
5558 build-depends: these >= 1 && <1.1
59+ , these-lens >= 1 && <1.1
5660 , semialign >=1 && <1.2
5761 , monoidal-containers >= 0.6 && < 0.7
5862 else
src/Control/Category/DecidablyEmpty.hsnew file+9
@@ -0,0 +1,9 @@
1+{-# LANGUAGE TypeOperators #-}
2+-- TODO upstream somwhere else?
3+module Control.Category.DecidablyEmpty where
4+
5+import Control.Category
6+import Data.Type.Equality
7+
8+class Category c => DecidablyEmpty c where
9+ isId :: c a b -> Maybe (a :~: b)
src/Data/Functor/Misc.hs+28−5
@@ -1,6 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE DeriveDataTypeable #-}
3−{-# LANGUAGE DeriveFunctor #-}
3+{-# LANGUAGE DeriveTraversable #-}
44{-# LANGUAGE FlexibleContexts #-}
55{-# LANGUAGE FlexibleInstances #-}
66{-# LANGUAGE GADTs #-}
@@ -17,6 +17,8 @@
1717module Data.Functor.Misc
1818 ( -- * Const2
1919 Const2 (..)
20+ , Proxy3 (..)
21+ , First2 (..)
2022 , unConst2
2123 , dmapToMap
2224 , dmapToIntMap
@@ -37,6 +39,7 @@ module Data.Functor.Misc
3739 , ComposeMaybe (..)
3840 ) where
3941
42+import qualified Control.Category as Cat
4043import Control.Applicative ((<$>))
4144import Data.Dependent.Map (DMap)
4245import qualified Data.Dependent.Map as DMap
@@ -48,6 +51,7 @@ import Data.IntMap (IntMap)
4851import qualified Data.IntMap as IntMap
4952import Data.Map (Map)
5053import qualified Data.Map as Map
54+import qualified Data.Semigroupoid as Cat
5155import Data.Some (Some(Some))
5256import Data.These
5357import Data.Type.Equality ((:~:)(Refl))
@@ -63,15 +67,15 @@ data Const2 :: * -> x -> x -> * where
6367 Const2 :: k -> Const2 k v v
6468 deriving (Typeable)
6569
66−-- | Extract the value from a Const2
67−unConst2 :: Const2 k v v' -> k
68−unConst2 (Const2 k) = k
69−
7070deriving instance Eq k => Eq (Const2 k v v')
7171deriving instance Ord k => Ord (Const2 k v v')
7272deriving instance Show k => Show (Const2 k v v')
7373deriving instance Read k => Read (Const2 k v v)
7474
75+-- | Extract the value from a Const2
76+unConst2 :: Const2 k v v' -> k
77+unConst2 (Const2 k) = k
78+
7579instance Show k => GShow (Const2 k v) where
7680 gshowsPrec n x@(Const2 _) = showsPrec n x
7781
@@ -87,6 +91,25 @@ instance Ord k => GCompare (Const2 k v) where
8791 EQ -> GEQ
8892 GT -> GGT
8993
94+data Proxy3 :: x -> y -> z -> * where
95+ Proxy3 :: Proxy3 vx vy vz
96+ deriving ( Show, Read, Eq, Ord
97+ , Functor, Foldable, Traversable
98+ , Typeable
99+ )
100+
101+instance Cat.Category (Proxy3 x) where
102+ id = Proxy3
103+ ~Proxy3 . ~Proxy3 = Proxy3
104+
105+newtype First2 (t :: k -> *) (a :: k) (b :: k) = First2 (t b)
106+ deriving ( Show, Read, Eq, Ord
107+ , Functor, Foldable, Traversable
108+ )
109+
110+instance Cat.Semigroupoid (First2 x) where
111+ First2 x `o` ~(First2 _) = First2 x
112+
90113-- | Convert a 'DMap' to a regular 'Map'
91114dmapToMap :: DMap (Const2 k v) Identity -> Map k v
92115dmapToMap = Map.fromDistinctAscList . map (\(Const2 k :=> Identity v) -> (k, v)) . DMap.toAscList
src/Data/Monoid/DecidablyEmpty.hsnew file+5
@@ -0,0 +1,5 @@
1+-- TODO upstream somwhere else?
2+module Data.Monoid.DecidablyEmpty where
3+
4+class Monoid a => DecidablyEmpty a where
5+ isNull :: a -> Bool
src/Data/Patch.hs+29−19
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE GeneralizedNewtypeDeriving #-}
23{-# LANGUAGE StandaloneDeriving #-}
34{-# LANGUAGE TypeFamilies #-}
@@ -12,24 +13,31 @@ module Data.Patch
1213 , module X
1314 ) where
1415
16+import Control.Applicative
17+import Data.Functor.Const (Const (..))
18+import Data.Functor.Identity
19+import Data.Map.Monoidal (MonoidalMap)
20+import Data.Proxy
21+#if !MIN_VERSION_base(4,9,0)
22+import Data.Semigroup (Semigroup (..), (<>))
23+#endif
24+import GHC.Generics
25+
1526import Data.Patch.Class as X
1627import Data.Patch.DMap as X hiding (getDeletions)
17−import Data.Patch.DMapWithMove as X (PatchDMapWithMove, const2PatchDMapWithMoveWith, mapPatchDMapWithMove,
18− patchDMapWithMoveToPatchMapWithMoveWith,
19− traversePatchDMapWithMoveWithKey, unPatchDMapWithMove,
20− unsafePatchDMapWithMove, weakenPatchDMapWithMoveWith)
28+import Data.Patch.DMapWithMove as X
29+ ( PatchDMapWithMove, const2PatchDMapWithMoveWith, mapPatchDMapWithMove
30+ , patchDMapWithMoveToPatchMapWithMoveWith
31+ , traversePatchDMapWithMoveWithKey, unPatchDMapWithMove
32+ , unsafePatchDMapWithMove, weakenPatchDMapWithMoveWith
33+ )
2134import Data.Patch.IntMap as X hiding (getDeletions)
2235import Data.Patch.Map as X
23−import Data.Patch.MapWithMove as X (PatchMapWithMove, patchMapWithMoveNewElements,
24− patchMapWithMoveNewElementsMap, unPatchMapWithMove,
25− unsafePatchMapWithMove)
26−import Data.Map.Monoidal (MonoidalMap)
27−import Data.Semigroup (Semigroup (..), (<>))
28−import GHC.Generics
29−import Data.Functor.Identity
30−import Data.Functor.Const
31−import Data.Proxy
32−import Control.Applicative
36+import Data.Patch.MapWithMove as X
37+ ( PatchMapWithMove, patchMapWithMoveNewElements
38+ , patchMapWithMoveNewElementsMap, unPatchMapWithMove
39+ , unsafePatchMapWithMove
40+ )
3341
3442-- | A 'Group' is a 'Monoid' where every element has an inverse.
3543class (Semigroup q, Monoid q) => Group q where
@@ -43,8 +51,10 @@ class Semigroup q => Additive q where
4351-- | The elements of an 'Additive' 'Semigroup' can be considered as patches of their own type.
4452newtype AdditivePatch p = AdditivePatch { unAdditivePatch :: p }
4553
46−instance Additive p => Patch (AdditivePatch p) where
54+instance Additive p => PatchHet (AdditivePatch p) where
55+ type PatchSource (AdditivePatch p) = p
4756 type PatchTarget (AdditivePatch p) = p
57+instance Additive p => Patch (AdditivePatch p) where
4858 apply (AdditivePatch p) q = Just $ p <> q
4959
5060instance (Ord k, Group q) => Group (MonoidalMap k q) where
@@ -54,8 +64,8 @@ instance (Ord k, Additive q) => Additive (MonoidalMap k q)
5464
5565-- | Trivial group.
5666instance Group () where
57− negateG _ = ()
58− _ ~~ _ = ()
67+ negateG ~() = ()
68+ ~() ~~ ~() = ()
5969instance Additive ()
6070
6171-- | Product group. A Pair of groups gives rise to a group
@@ -80,8 +90,8 @@ instance (Additive (f a), Additive (g a)) => Additive ((f :*: g) a)
8090
8191-- | Trivial group, Functor style
8292instance Group (Proxy x) where
83− negateG _ = Proxy
84− _ ~~ _ = Proxy
93+ negateG ~Proxy = Proxy
94+ ~Proxy ~~ ~Proxy = Proxy
8595instance Additive (Proxy x)
8696
8797-- | Const lifts groups into a functor.
src/Data/Patch/Class.hs+136−4
@@ -1,17 +1,53 @@
1+{-# LANGUAGE DefaultSignatures #-}
2+{-# LANGUAGE DerivingStrategies #-}
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE MultiParamTypeClasses #-}
6+{-# LANGUAGE PolyKinds #-}
17{-# LANGUAGE TypeFamilies #-}
8+{-# LANGUAGE TypeOperators #-}
29-- | The interface for types which represent changes made to other types
310module Data.Patch.Class where
411
12+import qualified Data.Semigroupoid as Cat
513import Data.Functor.Identity
14+import Data.Functor.Misc
615import Data.Maybe
716import Data.Semigroup (Semigroup(..))
17+import Data.Proxy
18+import Data.Type.Equality ((:~:) (..))
19+
20+class PatchHet p where
21+ type PatchSource p :: *
22+ type PatchTarget p :: *
23+ -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
24+ -- 'Nothing'.
25+ applyHet
26+ :: p
27+ -> PatchSource p
28+ -> Either (PatchSource p :~: PatchTarget p) (PatchTarget p)
29+ default applyHet
30+ :: Patch p
31+ => p
32+ -> PatchSource p
33+ -> Either (PatchSource p :~: PatchTarget p) (PatchTarget p)
34+ applyHet p a = case apply p a of
35+ Nothing -> Left Refl
36+ Just a' -> Right a'
37+
38+-- | Apply a 'PatchHet'; if it does nothing, return the original value
39+applyAlwaysHet :: PatchHet p => p -> PatchSource p -> PatchTarget p
40+applyAlwaysHet p t = case applyHet p t of
41+ Left Refl -> t
42+ Right t' -> t'
843
944-- | A 'Patch' type represents a kind of change made to a datastructure.
1045--
1146-- If an instance of 'Patch' is also an instance of 'Semigroup', it should obey
1247-- the law that @applyAlways (f <> g) == applyAlways f . applyAlways g@.
13−class Patch p where
14− type PatchTarget p :: *
48+class ( PatchHet p
49+ , PatchSource p ~ PatchTarget p
50+ ) => Patch p where
1551 -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
1652 -- 'Nothing'.
1753 apply :: p -> PatchTarget p -> Maybe (PatchTarget p)
@@ -21,14 +57,110 @@ applyAlways :: Patch p => p -> PatchTarget p -> PatchTarget p
2157applyAlways p t = fromMaybe t $ apply p t
2258
2359-- | 'Identity' can be used as a 'Patch' that always fully replaces the value
24−instance Patch (Identity a) where
60+instance PatchHet (Identity a) where
61+ type PatchSource (Identity a) = a
2562 type PatchTarget (Identity a) = a
63+instance Patch (Identity a) where
2664 apply (Identity a) _ = Just a
2765
66+-- | 'Proxy' can be used as a 'Patch' that always fully replaces the value
67+instance PatchHet (Proxy (a :: *)) where
68+ type PatchSource (Proxy a) = a
69+ type PatchTarget (Proxy a) = a
70+instance Patch (Proxy (a :: *)) where
71+ apply ~Proxy _ = Nothing
72+
2873-- | Like '(.)', but composes functions that return patches rather than
2974-- functions that return new values. The Semigroup instance for patches must
3075-- apply patches right-to-left, like '(.)'.
31−composePatchFunctions :: (Patch p, Semigroup p) => (PatchTarget p -> p) -> (PatchTarget p -> p) -> PatchTarget p -> p
76+composePatchFunctions
77+ :: (Patch p, Semigroup p)
78+ => (PatchTarget p -> p)
79+ -> (PatchTarget p -> p)
80+ -> PatchTarget p -> p
3281composePatchFunctions g f a =
3382 let fp = f a
3483 in g (applyAlways fp a) <> fp
84+
85+
86+class PatchHet2Base (p :: k -> k -> *) where
87+ type PatchSource1 p :: k -> *
88+ type PatchTarget1 p :: k -> *
89+
90+class ( PatchHet2Base p
91+ , PatchHet (p from to)
92+ , PatchSource1 p from ~ PatchSource (p from to)
93+ , PatchTarget1 p to ~ PatchTarget (p from to)
94+ ) => PatchHet2Locally (p :: k -> k -> *) from to where
95+instance ( PatchHet2Base p
96+ , PatchHet (p from to)
97+ , PatchSource1 p from ~ PatchSource (p from to)
98+ , PatchTarget1 p to ~ PatchTarget (p from to)
99+ ) => PatchHet2Locally (p :: k -> k -> *) from to where
100+
101+applyHet2Locally
102+ :: PatchHet2Locally p from to
103+ => p from to
104+ -> PatchSource1 p from
105+ -> Either (PatchSource1 p from :~: PatchTarget1 p to) (PatchTarget1 p to)
106+applyHet2Locally = applyHet
107+
108+applyAlwaysHet2Locally
109+ :: PatchHet2Locally p from to
110+ => p from to
111+ -> PatchSource1 p from
112+ -> PatchTarget1 p to
113+applyAlwaysHet2Locally = applyAlwaysHet
114+
115+-- TODO once we can use quantified constraints, perhaps combine PatchHet2Base and
116+-- PatchHet2Locally, or at least get rid of this.
117+class PatchHet2Base p => PatchHet2 (p :: k -> k -> *) where
118+ applyHet2
119+ :: p from to
120+ -> PatchSource1 p from
121+ -> Either (PatchSource1 p from :~: PatchTarget1 p to) (PatchTarget1 p to)
122+
123+applyAlwaysHet2
124+ :: PatchHet2 p
125+ => p from to
126+ -> PatchSource1 p from
127+ -> PatchTarget1 p to
128+applyAlwaysHet2 p t = case applyHet2 p t of
129+ Left Refl -> t
130+ Right t' -> t'
131+
132+-- | Connect the classes without quanitified constraints
133+newtype ProjectLocal p from to = ProjectLocal { unProjectLocal :: p from to }
134+ deriving newtype Cat.Semigroupoid
135+
136+instance PatchHet2 p => PatchHet (ProjectLocal p from to) where
137+ type PatchSource (ProjectLocal p from to) = PatchSource1 p from
138+ type PatchTarget (ProjectLocal p from to) = PatchTarget1 p to
139+ applyHet (ProjectLocal p) = applyHet2 p
140+
141+instance PatchHet2 p => PatchHet2Base (ProjectLocal p) where
142+ type PatchSource1 (ProjectLocal p) = PatchSource1 p
143+ type PatchTarget1 (ProjectLocal p) = PatchTarget1 p
144+
145+class ( PatchHet2Base p
146+ , PatchSource1 p ~ PatchTarget1 p
147+ ) => Patch2 p
148+instance ( PatchHet2Base p
149+ , PatchSource1 p ~ PatchTarget1 p
150+ ) => Patch2 p
151+
152+-- | 'First2' can be used as a 'Patch' that always fully replaces the value
153+instance PatchHet (First2 (t :: k -> *) (from :: k) (to :: k)) where
154+ type PatchSource (First2 t from to) = t from
155+ type PatchTarget (First2 t from to) = t to
156+ applyHet (First2 val) _ = Right val
157+
158+-- | 'Proxy3' can be used as a 'Patch' that always does nothing
159+instance PatchHet (Proxy3 (t :: k -> *) (a :: k) (a :: k)) where
160+ type PatchSource (Proxy3 t a a) = t a
161+ type PatchTarget (Proxy3 t a a) = t a
162+ applyHet ~Proxy3 _ = Left Refl
163+
164+instance PatchHet2Base (Proxy3 (t :: k -> *) :: k -> k -> *) where
165+ type PatchSource1 (Proxy3 t) = t
166+ type PatchTarget1 (Proxy3 t) = t
src/Data/Patch/DMap.hs+3−1
@@ -31,8 +31,10 @@ deriving instance GCompare k => Semigroup (PatchDMap k v)
3131deriving instance GCompare k => Monoid (PatchDMap k v)
3232
3333-- | Apply the insertions or deletions to a given 'DMap'.
34−instance GCompare k => Patch (PatchDMap k v) where
34+instance GCompare k => PatchHet (PatchDMap k v) where
35+ type PatchSource (PatchDMap k v) = DMap k v
3536 type PatchTarget (PatchDMap k v) = DMap k v
37+instance GCompare k => Patch (PatchDMap k v) where
3638 apply (PatchDMap diff) old = Just $! insertions `DMap.union` (old `DMap.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
3739 where insertions = DMap.mapMaybeWithKey (const $ getComposeMaybe) diff
3840 deletions = DMap.mapMaybeWithKey (const $ nothingToJust . getComposeMaybe) diff
src/Data/Patch/DMapWithMove.hs+405−151
@@ -1,100 +1,222 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ConstraintKinds #-}
23{-# LANGUAGE FlexibleContexts #-}
34{-# LANGUAGE FlexibleInstances #-}
45{-# LANGUAGE GADTs #-}
6+{-# LANGUAGE InstanceSigs #-}
57{-# LANGUAGE LambdaCase #-}
68{-# LANGUAGE MultiParamTypeClasses #-}
79{-# LANGUAGE PatternGuards #-}
810{-# LANGUAGE PolyKinds #-}
911{-# LANGUAGE Rank2Types #-}
1012{-# LANGUAGE ScopedTypeVariables #-}
13+{-# LANGUAGE StandaloneDeriving #-}
1114{-# LANGUAGE TypeFamilies #-}
1215{-# LANGUAGE UndecidableInstances #-}
16+{-# LANGUAGE UndecidableSuperClasses #-}
1317
1418-- |Module containing @'PatchDMapWithMove' k v@ and associated functions, which represents a 'Patch' to a @'DMap' k v@ which can insert, update, delete, and
1519-- move values between keys.
1620module Data.Patch.DMapWithMove where
1721
18−import Data.Patch.Class
19−import Data.Patch.MapWithMove (PatchMapWithMove (..))
20−import qualified Data.Patch.MapWithMove as MapWithMove
21−
22−import Data.Constraint.Extras
23−import Data.Dependent.Map (DMap, DSum (..), GCompare (..))
22+import qualified Control.Category as Cat
23+--import qualified Control.Category.DecidablyEmpty as Cat
24+import Data.Constraint.Extras (Has')
25+import Data.Dependent.Map (DMap)
26+import Data.Dependent.Sum (DSum (..))
2427import qualified Data.Dependent.Map as DMap
25−import Data.Functor.Constant
28+import Data.Functor.Constant (Constant (..))
2629import Data.Functor.Misc
27−import Data.Functor.Product
28−import Data.GADT.Compare (GEq (..))
29−import Data.GADT.Show (GShow, gshow)
30+ ( Const2 (..), Proxy3 (..)
31+ , weakenDMapWith
32+ , dmapToMapWith
33+ )
34+import Data.Functor.Product (Product (..))
35+import Data.GADT.Compare (GEq (..), GCompare (..))
36+import Data.GADT.Show (GRead, GShow, gshow)
3037import qualified Data.Map as Map
3138import Data.Maybe
3239import Data.Semigroup (Semigroup (..), (<>))
40+import Data.Semigroupoid as Cat
3341import Data.Some (Some(Some))
34−import Data.These
42+import Data.Proxy (Proxy (..))
43+import Data.These (These (..))
3544
36−-- | Like 'PatchMapWithMove', but for 'DMap'. Each key carries a 'NodeInfo' which describes how it will be changed by the patch and connects move sources and
37−-- destinations.
45+import Data.Patch.Class
46+ ( Patch (..), PatchHet (..)
47+ , PatchHet2 (..), PatchSource1, PatchTarget1
48+ , applyAlwaysHet2
49+ )
50+import Data.Patch.MapWithMove (PatchMapWithMove (..))
51+import qualified Data.Patch.MapWithMove as MapWithMove
52+
53+-- | Like 'PatchMapWithMove', but for 'DMap'. Each key carries a 'NodeInfo'
54+-- which describes how it will be changed by the patch and connects move sources
55+-- and destinations.
3856--
3957-- Invariants:
4058--
41−-- * A key should not move to itself.
42−-- * A move should always be represented with both the destination key (as a 'From_Move') and the source key (as a @'ComposeMaybe' ('Just' destination)@)
59+-- * A key should not move to itself.
60+--
61+-- * A move should always be represented with both the destination key (as a
62+-- 'From_Move') and the source key (as a @'ComposeMaybe' ('Just'
63+-- destination)@)
4364newtype PatchDMapWithMove k v = PatchDMapWithMove (DMap k (NodeInfo k v))
4465
45−-- |Structure which represents what changes apply to a particular key. @_nodeInfo_from@ specifies what happens to this key, and in particular what other key
46−-- the current key is moving from, while @_nodeInfo_to@ specifies what key the current key is moving to if involved in a move.
47−data NodeInfo k v a = NodeInfo
48− { _nodeInfo_from :: !(From k v a)
49− -- ^Change applying to the current key, be it an insert, move, or delete.
50− , _nodeInfo_to :: !(To k a)
51− -- ^Where this key is moving to, if involved in a move. Should only be @ComposeMaybe (Just k)@ when there is a corresponding 'From_Move'.
66+--deriving instance ( GShow k
67+-- , HasZip Show k p
68+-- , Has' Show k (PatchTarget1 p)
69+-- ) => Show (PatchDMapWithMove k p)
70+--deriving instance ( GRead k
71+-- , HasZip Read k p
72+-- , Has' Read k (PatchTarget1 p)
73+-- ) => Read (PatchDMapWithMove k p)
74+--deriving instance ( GEq k
75+-- , HasZip Eq k p
76+-- , Has' Eq k (PatchTarget1 p)
77+-- ) => Eq (PatchDMapWithMove k p)
78+--deriving instance ( GCompare k
79+-- , HasZip Ord k p
80+-- , Has' Ord k (PatchTarget1 p)
81+-- ) => Ord (PatchDMapWithMove k p)
82+
83+-- | Structure which represents what changes apply to a particular key.
84+-- @_nodeInfo_from@ specifies what happens to this key, and in particular what
85+-- other key the current key is moving from, while @_nodeInfo_to@ specifies what
86+-- key the current key is moving to if involved in a move.
87+data NodeInfo k p a = NodeInfo
88+ { _nodeInfo_from :: !(From k p a)
89+ -- ^ Change applying to the current key, be it an insert, move, or delete.
90+ , _nodeInfo_to :: !(To k p a)
91+ -- ^ Where this key is moving to, if involved in a move. Should only be
92+ -- @ComposeMaybe (Just k)@ when there is a corresponding 'From_Move'.
5293 }
53− deriving (Show)
54−
55−-- |Structure describing a particular change to a key, be it inserting a new key (@From_Insert@), updating an existing key (@From_Insert@ again), deleting a
56−-- key (@From_Delete@), or moving a key (@From_Move@).
57−data From (k :: a -> *) (v :: a -> *) :: a -> * where
58− -- |Insert a new or update an existing key with the given value @v a@
59− From_Insert :: v a -> From k v a
60− -- |Delete the existing key
61− From_Delete :: From k v a
62− -- |Move the value from the given key @k a@ to this key. The source key should also have an entry in the patch giving the current key as @_nodeInfo_to@,
63− -- usually but not necessarily with @From_Delete@.
64− From_Move :: !(k a) -> From k v a
65− deriving (Show, Read, Eq, Ord)
66−
67−-- |Type alias for the "to" part of a 'NodeInfo'. @'ComposeMaybe' ('Just' k)@ means the key is moving to another key, @ComposeMaybe Nothing@ for any other
68−-- operation.
69−type To = ComposeMaybe
94+
95+--deriving instance ( Show (k a)
96+-- , Show (p a a)
97+-- , Show (PatchTarget1 p a)
98+-- ) => Show (NodeInfo k p a)
99+--deriving instance ( Read (k a)
100+-- , Read (p a a)
101+-- , Read (PatchTarget1 p a)
102+-- ) => Read (NodeInfo k p a)
103+--deriving instance ( Eq (k a)
104+-- , Eq (p a a)
105+-- , Eq (PatchTarget1 p a)
106+-- ) => Eq (NodeInfo k p a)
107+--deriving instance ( Ord (k a)
108+-- , Ord (p a a)
109+-- , Ord (PatchTarget1 p a)
110+-- ) => Ord (NodeInfo k p a)
111+
112+-- | Structure describing a particular change to a key, be it inserting a new
113+-- key (@From_Insert@), updating an existing key (@From_Insert@ again), deleting
114+-- a key (@From_Delete@), or moving a key (@From_Move@).
115+--
116+-- This type isn't used directly as the from field patch, but is instead wrapped
117+-- in an existential. However, it is nice to be able to reason about this in
118+-- isolation as it is itself a @Semigroupoid@ when the underlying patch is.
119+data From (k :: a -> *) (p :: a -> a -> *) :: a -> * where
120+ -- | Insert a new or update an existing key with the given value @PatchTarget1
121+ -- p a@
122+ From_Insert :: PatchTarget1 p to -> From k p to
123+ -- | Delete the existing key
124+ From_Delete :: From k p to
125+ -- | Move the value from the given key @k a@ to this key. The source key
126+ -- should also have an entry in the patch giving the current key as
127+ -- @_nodeInfo_to@, usually but not necessarily with @From_Delete@.
128+ From_Move :: !(DSum k (Flip p to)) -> From k p to
129+
130+deriving instance ( Show (k a), GShow k
131+ , Has' Show k (Flip p a)
132+ , Show (PatchTarget1 p a)
133+ ) => Show (From k p a)
134+deriving instance ( Read (k a), GRead k
135+ , Has' Read k (Flip p a)
136+ , Read (PatchTarget1 p a)
137+ ) => Read (From k p a)
138+deriving instance ( GEq k
139+ , Has' Eq k (Flip p a)
140+ , Eq (PatchTarget1 p a)
141+ ) => Eq (From k p a)
142+deriving instance ( GCompare k
143+ , Has' Eq k (Flip p a) -- superclass bug
144+ , Has' Ord k (Flip p a)
145+ , Ord (PatchTarget1 p a)
146+ ) => Ord (From k p a)
147+
148+newtype Flip p to from = Flip (p from to)
149+
150+instance Cat.Category p => Cat.Category (Flip (p :: k -> k -> *)) where
151+ id = Flip Cat.id
152+ Flip y . Flip x = Flip $ x Cat.. y
153+
154+-- | The "to" part of a 'NodeInfo'. Rather than be built out of @From@ like @From@
155+-- is, we store just the information necessary to compose a @To@ and @From@ like
156+-- @oLocal@ composes two @From@s.
157+data To (k :: a -> *) (p :: a -> a -> *) :: a -> * where
158+ -- | Delete or leave in place
159+ To_NonMove :: To k p from
160+ -- | Move the value from the given key @k a@ to this key. The target key
161+ -- should also have an entry in the patch giving the current key in
162+ -- @_nodeInfo_from@, usually but not necessarily with @To_Delete@.
163+ To_Move :: !(DSum k (p from)) -> To k p from
164+
165+deriving instance ( Show (k a), GShow k
166+ , Has' Show k (p a)
167+ , Show (PatchTarget1 p a)
168+ ) => Show (To k p a)
169+deriving instance ( Read (k a), GRead k
170+ , Has' Read k (p a)
171+ , Read (PatchTarget1 p a)
172+ ) => Read (To k p a)
173+deriving instance ( GEq k
174+ , Has' Eq k (p a)
175+ , Eq (PatchTarget1 p a)
176+ ) => Eq (To k p a)
177+deriving instance ( GCompare k
178+ , Has' Eq k (p a) -- superclass bug
179+ , Has' Ord k (p a)
180+ , Ord (PatchTarget1 p a)
181+ ) => Ord (To k p a)
70182
71183-- |Test whether a 'PatchDMapWithMove' satisfies its invariants.
72−validPatchDMapWithMove :: forall k v. (GCompare k, GShow k) => DMap k (NodeInfo k v) -> Bool
184+validPatchDMapWithMove
185+ :: forall k v
186+ . (GCompare k, GShow k)
187+ => DMap k (NodeInfo k v)
188+ -> Bool
73189validPatchDMapWithMove = not . null . validationErrorsForPatchDMapWithMove
74190
75191-- |Enumerate what reasons a 'PatchDMapWithMove' doesn't satisfy its invariants, returning @[]@ if it's valid.
76−validationErrorsForPatchDMapWithMove :: forall k v. (GCompare k, GShow k) => DMap k (NodeInfo k v) -> [String]
192+validationErrorsForPatchDMapWithMove
193+ :: forall k v
194+ . (GCompare k, GShow k)
195+ => DMap k (NodeInfo k v)
196+ -> [String]
77197validationErrorsForPatchDMapWithMove m =
78198 noSelfMoves <> movesBalanced
79199 where
80200 noSelfMoves = mapMaybe selfMove . DMap.toAscList $ m
81− selfMove (dst :=> NodeInfo (From_Move src) _) | Just _ <- dst `geq` src = Just $ "self move of key " <> gshow src <> " at destination side"
82− selfMove (src :=> NodeInfo _ (ComposeMaybe (Just dst))) | Just _ <- src `geq` dst = Just $ "self move of key " <> gshow dst <> " at source side"
201+ selfMove (dst :=> NodeInfo (From_Move (src :=> _)) _)
202+ | Just _ <- dst `geq` src = Just $ "self move of key " <> gshow src <> " at destination side"
203+ selfMove (src :=> NodeInfo _ (To_Move (dst :=> _)))
204+ | Just _ <- src `geq` dst = Just $ "self move of key " <> gshow dst <> " at source side"
83205 selfMove _ = Nothing
84206 movesBalanced = mapMaybe unbalancedMove . DMap.toAscList $ m
85− unbalancedMove (dst :=> NodeInfo (From_Move src) _) =
207+ unbalancedMove (dst :=> NodeInfo (From_Move (src :=> _)) _) =
86208 case DMap.lookup src m of
87209 Nothing -> Just $ "unbalanced move at destination key " <> gshow dst <> " supposedly from " <> gshow src <> " but source key is not in the patch"
88− Just (NodeInfo _ (ComposeMaybe (Just dst'))) ->
210+ Just (NodeInfo _ (To_Move (dst' :=> _))) ->
89211 if isNothing (dst' `geq` dst)
90212 then Just $ "unbalanced move at destination key " <> gshow dst <> " from " <> gshow src <> " is going to " <> gshow dst' <> " instead"
91213 else Nothing
92214 _ ->
93215 Just $ "unbalanced move at destination key " <> gshow dst <> " supposedly from " <> gshow src <> " but source key has no move to key"
94− unbalancedMove (src :=> NodeInfo _ (ComposeMaybe (Just dst))) =
216+ unbalancedMove (src :=> NodeInfo _ (To_Move (dst :=> _))) =
95217 case DMap.lookup dst m of
96218 Nothing -> Just $ " unbalanced move at source key " <> gshow src <> " supposedly going to " <> gshow dst <> " but destination key is not in the patch"
97− Just (NodeInfo (From_Move src') _) ->
219+ Just (NodeInfo (From_Move (src' :=> _)) _) ->
98220 if isNothing (src' `geq` src)
99221 then Just $ "unbalanced move at source key " <> gshow src <> " to " <> gshow dst <> " is coming from " <> gshow src' <> " instead"
100222 else Nothing
@@ -103,35 +225,49 @@ validationErrorsForPatchDMapWithMove m =
103225 Just $ "unbalanced move at source key " <> gshow src <> " supposedly going to " <> gshow dst <> " but destination key is not moving"
104226 unbalancedMove _ = Nothing
105227
106−-- |Test whether two @'PatchDMapWithMove' k v@ contain the same patch operations.
107−instance (GEq k, Has' Eq k (NodeInfo k v)) => Eq (PatchDMapWithMove k v) where
108− PatchDMapWithMove a == PatchDMapWithMove b = a == b
109−
110228-- |Higher kinded 2-tuple, identical to @Data.Functor.Product@ from base ≥ 4.9
111229data Pair1 f g a = Pair1 (f a) (g a)
112230
113231-- |Helper data structure used for composing patches using the monoid instance.
114−data Fixup k v a
232+data Fixup k p a
115233 = Fixup_Delete
116− | Fixup_Update (These (From k v a) (To k a))
117−
118−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
119−instance GCompare k => Semigroup (PatchDMapWithMove k v) where
234+ | Fixup_Update (These (From k p a) (To k p a))
235+
236+-- | Compose patches having the same effect as applying the patches in turn:
237+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
238+instance ( GCompare k
239+ , Cat.Semigroupoid p
240+ -- , Cat.DecidablyEmpty p
241+ , PatchHet2 p
242+ , PatchSource1 p ~ PatchTarget1 p
243+ ) => Semigroup (PatchDMapWithMove k p) where
120244 PatchDMapWithMove ma <> PatchDMapWithMove mb = PatchDMapWithMove m
121245 where
122− connections = DMap.toList $ DMap.intersectionWithKey (\_ a b -> Pair1 (_nodeInfo_to a) (_nodeInfo_from b)) ma mb
123− h :: DSum k (Pair1 (ComposeMaybe k) (From k v)) -> [DSum k (Fixup k v)]
124− h (_ :=> Pair1 (ComposeMaybe mToAfter) editBefore) = case (mToAfter, editBefore) of
125− (Just toAfter, From_Move fromBefore)
126− | isJust $ fromBefore `geq` toAfter
127− -> [toAfter :=> Fixup_Delete]
128− | otherwise
129− -> [ toAfter :=> Fixup_Update (This editBefore)
130− , fromBefore :=> Fixup_Update (That (ComposeMaybe mToAfter))
131− ]
132− (Nothing, From_Move fromBefore) -> [fromBefore :=> Fixup_Update (That (ComposeMaybe mToAfter))] -- The item is destroyed in the second patch, so indicate that it is destroyed in the source map
133− (Just toAfter, _) -> [toAfter :=> Fixup_Update (This editBefore)]
134− (Nothing, _) -> []
246+ connections :: [DSum k (Pair1 (To k p) (From k p))]
247+ connections = DMap.toList $ DMap.intersectionWithKey
248+ (\_ a b -> Pair1 (_nodeInfo_to a) (_nodeInfo_from b))
249+ ma
250+ mb
251+ h :: DSum k (Pair1 (To k p) (From k p)) -> [DSum k (Fixup k p)]
252+ h ((_ :: k between) :=> Pair1 editAfter editBefore) = case (editAfter, editBefore) of
253+ (To_Move ((toAfter :: k after) :=> p1), From_Move ((fromBefore :: k before) :=> Flip p0)) ->
254+ --case toAfter `geq` fromBefore of
255+ -- Just Refl | Just Refl <- Cat.isId p0 ->
256+ -- [ toAfter :=> Fixup_Delete ]
257+ -- _ ->
258+ [ toAfter :=> Fixup_Update (This $ From_Move $ fromBefore :=> (Flip $ p1 `o` p0))
259+ , fromBefore :=> Fixup_Update (That $ To_Move $ toAfter :=> (p1 `o` p0))
260+ ]
261+ (To_NonMove, From_Move (fromBefore :=> _)) ->
262+ -- The item is destroyed in the second patch, so indicate that it is
263+ -- destroyed in the source map
264+ [fromBefore :=> Fixup_Update (That To_NonMove)]
265+ (To_Move (toAfter :=> p), From_Insert val) ->
266+ [toAfter :=> Fixup_Update (This $ From_Insert $ applyAlwaysHet2 p val)]
267+ (To_Move (toAfter :=> _), From_Delete) ->
268+ [toAfter :=> Fixup_Update (This From_Delete)]
269+ (To_NonMove, _) ->
270+ []
135271 mergeFixups _ Fixup_Delete Fixup_Delete = Fixup_Delete
136272 mergeFixups _ (Fixup_Update a) (Fixup_Update b)
137273 | This x <- a, That y <- b
@@ -162,13 +298,19 @@ instance GCompare k => Semigroup (PatchDMapWithMove k v) where
162298 These _ b -> Just b
163299 That b -> Just b
164300
165−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
166−instance GCompare k => Monoid (PatchDMapWithMove k v) where
301+-- | Compose patches having the same effect as applying the patches in turn:
302+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
303+instance ( GCompare k
304+ , Cat.Semigroupoid p
305+ -- , DecidablyEmpty p
306+ , PatchHet2 p
307+ , PatchSource1 p ~ PatchTarget1 p
308+ ) => Monoid (PatchDMapWithMove k p) where
167309 mempty = PatchDMapWithMove mempty
168310 mappend = (<>)
169311
170312{-
171−mappendPatchDMapWithMoveSlow :: forall k v. (ShowTag k v, GCompare k) => PatchDMapWithMove k v -> PatchDMapWithMove k v -> PatchDMapWithMove k v
313+mappendPatchDMapWithMoveSlow :: forall k v. (ShowTag k v, GCompare k) => PatchDMapWithMove k v -> PatchDMapWithMove k v -> PatchDMapWithMove k v
172314PatchDMapWithMove dstAfter srcAfter `mappendPatchDMapWithMoveSlow` PatchDMapWithMove dstBefore srcBefore = PatchDMapWithMove dst src
173315 where
174316 getDstAction k m = fromMaybe (From_Move k) $ DMap.lookup k m -- Any key that isn't present is treated as that key moving to itself
@@ -189,25 +331,31 @@ PatchDMapWithMove dstAfter srcAfter `mappendPatchDMapWithMoveSlow` PatchDMapWith
189331 src = DMap.mapMaybeWithKey g $ DMap.union srcAfter srcBefore
190332-}
191333
192−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of inserting or updating a value @v a@ to the given key @k a@, like 'DMap.insert'.
193−insertDMapKey :: k a -> v a -> PatchDMapWithMove k v
334+-- | Make a @'PatchDMapWithMove' k v@ which has the effect of inserting or
335+-- updating a value @PatchTarget1 p a@ to the given key @k a@, like
336+-- 'DMap.insert'.
337+insertDMapKey :: k a -> PatchTarget1 p a -> PatchDMapWithMove k p
194338insertDMapKey k v =
195− PatchDMapWithMove . DMap.singleton k $ NodeInfo (From_Insert v) (ComposeMaybe Nothing)
339+ PatchDMapWithMove . DMap.singleton k $ NodeInfo (From_Insert v) To_NonMove
196340
197−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of moving the value from the first key @k a@ to the second key @k a@, equivalent to:
341+-- | Make a @'PatchDMapWithMove' k v@ which has the effect of moving the value
342+-- from the first key @k a@ to the second key @k a@, equivalent to:
198343--
199344-- @
200345-- 'DMap.delete' src (maybe dmap ('DMap.insert' dst) (DMap.lookup src dmap))
201346-- @
202−moveDMapKey :: GCompare k => k a -> k a -> PatchDMapWithMove k v
347+moveDMapKey
348+ :: GCompare k
349+ => k a -> k a -> PatchDMapWithMove k (Proxy3 v)
203350moveDMapKey src dst = case src `geq` dst of
204351 Nothing -> PatchDMapWithMove $ DMap.fromList
205− [ dst :=> NodeInfo (From_Move src) (ComposeMaybe Nothing)
206− , src :=> NodeInfo From_Delete (ComposeMaybe $ Just dst)
352+ [ dst :=> NodeInfo (From_Move (src :=> Flip Proxy3)) To_NonMove
353+ , src :=> NodeInfo From_Delete (To_Move $ dst :=> Proxy3)
207354 ]
208− Just _ -> mempty
355+ Just _ -> PatchDMapWithMove DMap.empty
209356
210−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of swapping two keys in the mapping, equivalent to:
357+-- | Make a @'PatchDMapWithMove' k v@ which has the effect of swapping two keys
358+-- in the mapping, equivalent to:
211359--
212360-- @
213361-- let aMay = DMap.lookup a dmap
@@ -216,17 +364,18 @@ moveDMapKey src dst = case src `geq` dst of
216364-- . maybe id (DMap.insert b) (aMay `mplus` bMay)
217365-- . DMap.delete a . DMap.delete b $ dmap
218366-- @
219−swapDMapKey :: GCompare k => k a -> k a -> PatchDMapWithMove k v
367+swapDMapKey :: GCompare k => k a -> k a -> PatchDMapWithMove k (Proxy3 v)
220368swapDMapKey src dst = case src `geq` dst of
221369 Nothing -> PatchDMapWithMove $ DMap.fromList
222− [ dst :=> NodeInfo (From_Move src) (ComposeMaybe $ Just src)
223− , src :=> NodeInfo (From_Move dst) (ComposeMaybe $ Just dst)
370+ [ dst :=> NodeInfo (From_Move (src :=> Flip Proxy3)) (To_Move $ src :=> Proxy3)
371+ , src :=> NodeInfo (From_Move (dst :=> Flip Proxy3)) (To_Move $ dst :=> Proxy3)
224372 ]
225− Just _ -> mempty
373+ Just _ -> PatchDMapWithMove DMap.empty
226374
227−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of deleting a key in the mapping, equivalent to 'DMap.delete'.
375+-- | Make a @'PatchDMapWithMove' k v@ which has the effect of deleting a key in
376+-- the mapping, equivalent to 'DMap.delete'.
228377deleteDMapKey :: k a -> PatchDMapWithMove k v
229−deleteDMapKey k = PatchDMapWithMove $ DMap.singleton k $ NodeInfo From_Delete $ ComposeMaybe Nothing
378+deleteDMapKey k = PatchDMapWithMove $ DMap.singleton k $ NodeInfo From_Delete To_NonMove
230379
231380{-
232381k1, k2 :: Const2 Int () ()
@@ -252,17 +401,20 @@ dst (PatchDMapWithMove x _) = x
252401src (PatchDMapWithMove _ x) = x
253402-}
254403
255−-- |Extract the 'DMap' representing the patch changes from the 'PatchDMapWithMove'.
404+-- | Extract the 'DMap' representing the patch changes from the
405+-- 'PatchDMapWithMove'.
256406unPatchDMapWithMove :: PatchDMapWithMove k v -> DMap k (NodeInfo k v)
257407unPatchDMapWithMove (PatchDMapWithMove p) = p
258408
259−-- |Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove', without checking any invariants.
409+-- | Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove',
410+-- without checking any invariants.
260411--
261412-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchDMapWithMove' are preserved; they will not be checked.
262413unsafePatchDMapWithMove :: DMap k (NodeInfo k v) -> PatchDMapWithMove k v
263414unsafePatchDMapWithMove = PatchDMapWithMove
264415
265−-- |Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove' while checking invariants. If the invariants are satisfied, @Right p@ is returned
416+-- | Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove' while
417+-- checking invariants. If the invariants are satisfied, @Right p@ is returned
266418-- otherwise @Left errors@.
267419patchDMapWithMove :: (GCompare k, GShow k) => DMap k (NodeInfo k v) -> Either [String] (PatchDMapWithMove k v)
268420patchDMapWithMove dm =
@@ -270,94 +422,196 @@ patchDMapWithMove dm =
270422 [] -> Right $ unsafePatchDMapWithMove dm
271423 errs -> Left errs
272424
273−-- |Map a natural transform @v -> v'@ over the given patch, transforming @'PatchDMapWithMove' k v@ into @'PatchDMapWithMove' k v'@.
274−mapPatchDMapWithMove :: forall k v v'. (forall a. v a -> v' a) -> PatchDMapWithMove k v -> PatchDMapWithMove k v'
275−mapPatchDMapWithMove f (PatchDMapWithMove p) = PatchDMapWithMove $
276− DMap.map (\ni -> ni { _nodeInfo_from = g $ _nodeInfo_from ni }) p
277− where g :: forall a. From k v a -> From k v' a
278− g = \case
425+-- | Map a natural transform @v -> v'@ over the given patch, transforming
426+-- @'PatchDMapWithMove' k v@ into @'PatchDMapWithMove' k v'@.
427+mapPatchDMapWithMove
428+ :: forall k p p'
429+ . (forall a. PatchTarget1 p a -> PatchTarget1 p' a)
430+ -> (forall from to. p from to -> p' from to)
431+ -> PatchDMapWithMove k p
432+ -> PatchDMapWithMove k p'
433+mapPatchDMapWithMove f g (PatchDMapWithMove m) =
434+ PatchDMapWithMove $ DMap.map (\ni -> NodeInfo
435+ { _nodeInfo_from = h $ _nodeInfo_from ni
436+ , _nodeInfo_to = j $ _nodeInfo_to ni
437+ }) m
438+ where h :: forall a. From k p a -> From k p' a
439+ h = \case
279440 From_Insert v -> From_Insert $ f v
280441 From_Delete -> From_Delete
281− From_Move k -> From_Move k
282−
283−-- |Traverse an effectful function @forall a. v a -> m (v ' a)@ over the given patch, transforming @'PatchDMapWithMove' k v@ into @m ('PatchDMapWithMove' k v')@.
284−traversePatchDMapWithMove :: forall m k v v'. Applicative m => (forall a. v a -> m (v' a)) -> PatchDMapWithMove k v -> m (PatchDMapWithMove k v')
285−traversePatchDMapWithMove f = traversePatchDMapWithMoveWithKey $ const f
286−
287−-- |Map an effectful function @forall a. k a -> v a -> m (v ' a)@ over the given patch, transforming @'PatchDMapWithMove' k v@ into @m ('PatchDMapWithMove' k v')@.
288−traversePatchDMapWithMoveWithKey :: forall m k v v'. Applicative m => (forall a. k a -> v a -> m (v' a)) -> PatchDMapWithMove k v -> m (PatchDMapWithMove k v')
289−traversePatchDMapWithMoveWithKey f (PatchDMapWithMove p) = PatchDMapWithMove <$> DMap.traverseWithKey (nodeInfoMapFromM . g) p
290− where g :: forall a. k a -> From k v a -> m (From k v' a)
291− g k = \case
442+ From_Move (k :=> Flip p) -> From_Move $ k :=> Flip (g p)
443+ j :: forall a. To k p a -> To k p' a
444+ j = \case
445+ To_NonMove -> To_NonMove
446+ To_Move (k :=> p) -> To_Move $ k :=> g p
447+
448+-- | Traverse an effectful function @forall a. PatchTarget1 p a -> m (v ' a)@
449+-- over the given patch, transforming @'PatchDMapWithMove' k v@ into @m
450+-- ('PatchDMapWithMove' k v')@.
451+traversePatchDMapWithMove
452+ :: forall m k p p'
453+ . Applicative m
454+ => (forall a. PatchTarget1 p a -> m (PatchTarget1 p' a))
455+ -> (forall from to. p from to -> m (p' from to))
456+ -> PatchDMapWithMove k p
457+ -> m (PatchDMapWithMove k p')
458+traversePatchDMapWithMove f g = traversePatchDMapWithMoveWithKey
459+ (\_ -> f)
460+ (\_ _ -> g)
461+
462+-- | Map an effectful function @forall a. k a -> PatchTarget1 p a -> m (v ' a)@
463+-- over the given patch, transforming @'PatchDMapWithMove' k v@ into @m
464+-- ('PatchDMapWithMove' k v')@.
465+traversePatchDMapWithMoveWithKey
466+ :: forall m k p p'
467+ . Applicative m
468+ => (forall a. k a -> PatchTarget1 p a -> m (PatchTarget1 p' a))
469+ -> (forall from to. k from -> k to -> p from to -> m (p' from to))
470+ -> PatchDMapWithMove k p
471+ -> m (PatchDMapWithMove k p')
472+traversePatchDMapWithMoveWithKey f g (PatchDMapWithMove m) =
473+ PatchDMapWithMove <$> DMap.traverseWithKey (\k ni -> NodeInfo
474+ <$> h k (_nodeInfo_from ni)
475+ <*> j k (_nodeInfo_to ni)) m
476+ where h :: forall a. k a -> From k p a -> m (From k p' a)
477+ h k = \case
292478 From_Insert v -> From_Insert <$> f k v
293479 From_Delete -> pure From_Delete
294− From_Move fromKey -> pure $ From_Move fromKey
295−
296−-- |Map a function which transforms @'From' k v a@ into a @'From' k v' a@ over a @'NodeInfo' k v a@.
297−nodeInfoMapFrom :: (From k v a -> From k v' a) -> NodeInfo k v a -> NodeInfo k v' a
298−nodeInfoMapFrom f ni = ni { _nodeInfo_from = f $ _nodeInfo_from ni }
299−
300−-- |Map an effectful function which transforms @'From' k v a@ into a @f ('From' k v' a)@ over a @'NodeInfo' k v a@.
301−nodeInfoMapFromM :: Functor f => (From k v a -> f (From k v' a)) -> NodeInfo k v a -> f (NodeInfo k v' a)
302−nodeInfoMapFromM f ni = fmap (\result -> ni { _nodeInfo_from = result }) $ f $ _nodeInfo_from ni
303−
304−-- |Weaken a 'PatchDMapWithMove' to a 'PatchMapWithMove' by weakening the keys from @k a@ to @'Some' k@ and applying a given weakening function @v a -> v'@ to
305−-- values.
306−weakenPatchDMapWithMoveWith :: forall k v v'. (forall a. v a -> v') -> PatchDMapWithMove k v -> PatchMapWithMove (Some k) v'
307−weakenPatchDMapWithMoveWith f (PatchDMapWithMove p) = PatchMapWithMove $ weakenDMapWith g p
308− where g :: forall a. NodeInfo k v a -> MapWithMove.NodeInfo (Some k) v'
309− g ni = MapWithMove.NodeInfo
480+ From_Move (fromKey :=> Flip p) -> From_Move . (fromKey :=>) . Flip <$> g fromKey k p
481+ j :: forall a. k a -> To k p a -> m (To k p' a)
482+ j k = \case
483+ To_NonMove -> pure To_NonMove
484+ To_Move (toKey :=> p) -> To_Move . (toKey :=>) <$> g k toKey p
485+
486+-- | Map a function which transforms @'From k PatchTarget1 p a@ into a @'From k
487+-- PatchTarget1 p' a@ over a @'NodeInfo' k PatchTarget1 p a@.
488+nodeInfoMapFrom
489+ :: (From k p a -> From k p' a)
490+ -> (To k p a -> To k p' a)
491+ -> NodeInfo k p a
492+ -> NodeInfo k p' a
493+nodeInfoMapFrom f g ni = NodeInfo
494+ { _nodeInfo_from = f $ _nodeInfo_from ni
495+ , _nodeInfo_to = g $ _nodeInfo_to ni
496+ }
497+
498+-- | Map an effectful function which transforms @'From k PatchTarget1 p a@ into
499+-- a @f ('From k PatchTarget1 p' a)@ over a @'NodeInfo' k PatchTarget1 p a@.
500+nodeInfoMapFromM
501+ :: Applicative f
502+ => (From k p a -> f (From k p' a))
503+ -> (To k p a -> f (To k p' a))
504+ -> NodeInfo k p a
505+ -> f (NodeInfo k p' a)
506+nodeInfoMapFromM f g ni = NodeInfo
507+ <$> f (_nodeInfo_from ni)
508+ <*> g (_nodeInfo_to ni)
509+
510+-- | Weaken a 'PatchDMapWithMove' to a 'PatchMapWithMove' by weakening the keys
511+-- from @k a@ to @'Some' k@ and applying a given weakening function
512+-- @PatchTarget1 p a -> v'@ to values.
513+weakenPatchDMapWithMoveWith
514+ :: forall k p p'
515+ . (forall a. PatchTarget1 p a -> PatchTarget p')
516+ -> (forall from to. p from to -> p')
517+ -> PatchDMapWithMove k p
518+ -> PatchMapWithMove (Some k) p'
519+weakenPatchDMapWithMoveWith f g (PatchDMapWithMove m) =
520+ PatchMapWithMove $ weakenDMapWith h m
521+ where h :: forall a. NodeInfo k p a -> MapWithMove.NodeInfo (Some k) p'
522+ h ni = MapWithMove.NodeInfo
310523 { MapWithMove._nodeInfo_from = case _nodeInfo_from ni of
311524 From_Insert v -> MapWithMove.From_Insert $ f v
312525 From_Delete -> MapWithMove.From_Delete
313− From_Move k -> MapWithMove.From_Move $ Some k
314− , MapWithMove._nodeInfo_to = Some <$> getComposeMaybe (_nodeInfo_to ni)
526+ From_Move (k :=> Flip p) -> MapWithMove.From_Move (Some k) $ g p
527+ , MapWithMove._nodeInfo_to = case _nodeInfo_to ni of
528+ To_NonMove -> MapWithMove.To_NonMove
529+ To_Move (k :=> p) -> MapWithMove.To_Move (Some k) $ g p
315530 }
316531
317−-- |"Weaken" a @'PatchDMapWithMove' (Const2 k a) v@ to a @'PatchMapWithMove' k v'@. Weaken is in scare quotes because the 'Const2' has already disabled any
318−-- dependency in the typing and all points are already @a@, hence the function to map each value to @v'@ is not higher rank.
319−patchDMapWithMoveToPatchMapWithMoveWith :: forall k v v' a. (v a -> v') -> PatchDMapWithMove (Const2 k a) v -> PatchMapWithMove k v'
320−patchDMapWithMoveToPatchMapWithMoveWith f (PatchDMapWithMove p) = PatchMapWithMove $ dmapToMapWith g p
321− where g :: NodeInfo (Const2 k a) v a -> MapWithMove.NodeInfo k v'
322− g ni = MapWithMove.NodeInfo
532+-- | "Weaken" a @'PatchDMapWithMove' (Const2 k a) v@ to a @'PatchMapWithMove' k
533+-- v'@. Weaken is in scare quotes because the 'Const2' has already disabled any
534+-- dependency in the typing and all points are already @a@, hence the function
535+-- to map each value to @v'@ is not higher rank.
536+patchDMapWithMoveToPatchMapWithMoveWith
537+ :: forall k p p' a
538+ . (PatchTarget1 p a -> PatchTarget p')
539+ -> (p a a -> p')
540+ -> PatchDMapWithMove (Const2 k a) p
541+ -> PatchMapWithMove k p'
542+patchDMapWithMoveToPatchMapWithMoveWith f g (PatchDMapWithMove m) =
543+ PatchMapWithMove $ dmapToMapWith h m
544+ where h :: NodeInfo (Const2 k a) p a -> MapWithMove.NodeInfo k p'
545+ h ni = MapWithMove.NodeInfo
323546 { MapWithMove._nodeInfo_from = case _nodeInfo_from ni of
324547 From_Insert v -> MapWithMove.From_Insert $ f v
325548 From_Delete -> MapWithMove.From_Delete
326− From_Move (Const2 k) -> MapWithMove.From_Move k
327− , MapWithMove._nodeInfo_to = unConst2 <$> getComposeMaybe (_nodeInfo_to ni)
549+ From_Move (Const2 k :=> Flip p) -> MapWithMove.From_Move k $ g p
550+ , MapWithMove._nodeInfo_to = case _nodeInfo_to ni of
551+ To_NonMove -> MapWithMove.To_NonMove
552+ To_Move (Const2 k :=> p) -> MapWithMove.To_Move k $ g p
328553 }
329554
330−-- |"Strengthen" a @'PatchMapWithMove' k v@ into a @'PatchDMapWithMove ('Const2' k a)@; that is, turn a non-dependently-typed patch into a dependently typed
331−-- one but which always has a constant key type represented by 'Const2'. Apply the given function to each @v@ to produce a @v' a@.
332−-- Completemented by 'patchDMapWithMoveToPatchMapWithMoveWith'
333−const2PatchDMapWithMoveWith :: forall k v v' a. (v -> v' a) -> PatchMapWithMove k v -> PatchDMapWithMove (Const2 k a) v'
334−const2PatchDMapWithMoveWith f (PatchMapWithMove p) = PatchDMapWithMove $ DMap.fromDistinctAscList $ g <$> Map.toAscList p
335− where g :: (k, MapWithMove.NodeInfo k v) -> DSum (Const2 k a) (NodeInfo (Const2 k a) v')
555+-- | "Strengthen" a @'PatchMapWithMove' k v@ into a @'PatchDMapWithMove
556+-- ('Const2' k a)@; that is, turn a non-dependently-typed patch into a
557+-- dependently typed one but which always has a constant key type represented by
558+-- 'Const2'. Apply the given function to each @v@ to produce a @PatchTarget1 p'
559+-- a@. Completemented by 'patchDMapWithMoveToPatchMapWithMoveWith'
560+const2PatchDMapWithMoveWith
561+ :: forall k v v' a
562+ . (v -> v' a)
563+ -> PatchMapWithMove k (Proxy v)
564+ -> PatchDMapWithMove (Const2 k a) (Proxy3 v')
565+const2PatchDMapWithMoveWith f (PatchMapWithMove p) =
566+ PatchDMapWithMove $ DMap.fromDistinctAscList $ g <$> Map.toAscList p
567+ where g :: (k, MapWithMove.NodeInfo k (Proxy v))
568+ -> DSum (Const2 k a) (NodeInfo (Const2 k a) (Proxy3 v'))
336569 g (k, ni) = Const2 k :=> NodeInfo
337570 { _nodeInfo_from = case MapWithMove._nodeInfo_from ni of
338571 MapWithMove.From_Insert v -> From_Insert $ f v
339572 MapWithMove.From_Delete -> From_Delete
340− MapWithMove.From_Move fromKey -> From_Move $ Const2 fromKey
341− , _nodeInfo_to = ComposeMaybe $ Const2 <$> MapWithMove._nodeInfo_to ni
573+ MapWithMove.From_Move fromKey Proxy -> From_Move $ Const2 fromKey :=> Flip Proxy3
574+ , _nodeInfo_to = case MapWithMove._nodeInfo_to ni of
575+ MapWithMove.To_NonMove -> To_NonMove
576+ MapWithMove.To_Move toKey Proxy -> To_Move $ Const2 toKey :=> Proxy3
342577 }
343578
344579-- | Apply the insertions, deletions, and moves to a given 'DMap'.
345−instance GCompare k => Patch (PatchDMapWithMove k v) where
346− type PatchTarget (PatchDMapWithMove k v) = DMap k v
347− apply (PatchDMapWithMove p) old = Just $! insertions `DMap.union` (old `DMap.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
348− where insertions = DMap.mapMaybeWithKey insertFunc p
349− insertFunc :: forall a. k a -> NodeInfo k v a -> Maybe (v a)
580+instance ( GCompare k
581+ , PatchHet2 p
582+ , PatchSource1 p ~ PatchTarget1 p
583+ ) => PatchHet (PatchDMapWithMove k p) where
584+ type PatchSource (PatchDMapWithMove k p) = DMap k (PatchSource1 p)
585+ type PatchTarget (PatchDMapWithMove k p) = DMap k (PatchTarget1 p)
586+
587+instance ( GCompare k
588+ , PatchHet2 p
589+ , PatchSource1 p ~ PatchTarget1 p
590+ ) => Patch (PatchDMapWithMove k p) where
591+ apply (PatchDMapWithMove m) old = Just $! insertions `DMap.union` (old `DMap.difference` deletions)
592+ -- TODO: return Nothing sometimes --Note: the strict application here is
593+ -- critical to ensuring that incremental merges don't hold onto all their
594+ -- prerequisite events forever; can we make this more robust?
595+ where insertions = DMap.mapMaybeWithKey insertFunc m
596+ insertFunc :: forall a. k a -> NodeInfo k p a -> Maybe (PatchTarget1 p a)
350597 insertFunc _ ni = case _nodeInfo_from ni of
351598 From_Insert v -> Just v
352− From_Move k -> DMap.lookup k old
599+ From_Move (k :=> Flip p) -> applyAlwaysHet2 p <$> DMap.lookup k old
353600 From_Delete -> Nothing
354− deletions = DMap.mapMaybeWithKey deleteFunc p
355− deleteFunc :: forall a. k a -> NodeInfo k v a -> Maybe (Constant () a)
601+ deletions = DMap.mapMaybeWithKey deleteFunc m
602+ deleteFunc :: forall a. k a -> NodeInfo k p a -> Maybe (Constant () a)
356603 deleteFunc _ ni = case _nodeInfo_from ni of
357604 From_Delete -> Just $ Constant ()
358605 _ -> Nothing
359606
360−-- | Get the values that will be replaced, deleted, or moved if the given patch is applied to the given 'DMap'.
361−getDeletionsAndMoves :: GCompare k => PatchDMapWithMove k v -> DMap k v' -> DMap k (Product v' (ComposeMaybe k))
607+-- | Get the values that will be replaced, deleted, or moved if the given patch
608+-- is applied to the given 'DMap'.
609+getDeletionsAndMoves
610+ :: ( GCompare k
611+ , PatchSource1 p ~ PatchTarget1 p
612+ )
613+ => PatchDMapWithMove k p
614+ -> DMap k (PatchSource1 p)
615+ -> DMap k (Product (PatchTarget1 p) (To k p))
362616getDeletionsAndMoves (PatchDMapWithMove p) m = DMap.intersectionWithKey f m p
363617 where f _ v ni = Pair v $ _nodeInfo_to ni
src/Data/Patch/DMapWithMove/By.hsnew file+89
@@ -0,0 +1,89 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE GADTs #-}
6+{-# LANGUAGE InstanceSigs #-}
7+{-# LANGUAGE LambdaCase #-}
8+{-# LANGUAGE MultiParamTypeClasses #-}
9+{-# LANGUAGE PolyKinds #-}
10+{-# LANGUAGE Rank2Types #-}
11+{-# LANGUAGE ScopedTypeVariables #-}
12+{-# LANGUAGE StandaloneDeriving #-}
13+{-# LANGUAGE TypeFamilies #-}
14+{-# LANGUAGE UndecidableInstances #-}
15+{-# LANGUAGE UndecidableSuperClasses #-}
16+
17+module Data.Patch.DMapWithMove.By where
18+
19+import Data.Semigroupoid as Cat
20+
21+import Data.Patch.Class
22+
23+-- | Structure describing a particular change to a key, be it inserting a new
24+-- key (@By_Insert@), updating an existing key (@By_Insert@ again), deleting
25+-- a key (@By_Delete@), or moving a key (@By_Move@).
26+--
27+-- This type isn't used directly as the from field patch, but is instead wrapped
28+-- in an existential. However, it is nice to be able to reason about this in
29+-- isolation as it is itself a @Semigroupoid@ when the underlying patch is.
30+data By (k :: a -> *) (p :: a -> a -> *) :: a -> a -> * where
31+ -- | Insert a new or update an existing key with the given value @PatchTarget1
32+ -- p a@
33+ By_Insert :: PatchTarget1 p to -> By k p from to
34+ -- | Delete the existing key
35+ By_Delete :: By k p from to
36+ -- | Move the value from the given key @k a@ to this key. The source key
37+ -- should also have an entry in the patch giving the current key as
38+ -- @_nodeInfo_to@, usually but not necessarily with @By_Delete@.
39+ By_Move :: !(k from) -> p from to -> By k p from to
40+
41+deriving instance ( Show (k from), Show (k to)
42+ , Show (p from to)
43+ , Show (PatchTarget1 p to)
44+ ) => Show (By k p from to)
45+deriving instance ( Read (k from), Read (k to)
46+ , Read (p from to)
47+ , Read (PatchTarget1 p to)
48+ ) => Read (By k p from to)
49+deriving instance ( Eq (k from), Eq (k to)
50+ , Eq (p from to)
51+ , Eq (PatchTarget1 p to)
52+ ) => Eq (By k p from to)
53+deriving instance ( Ord (k from), Ord (k to)
54+ , Ord (p from to)
55+ , Ord (PatchTarget1 p to)
56+ ) => Ord (By k p from to)
57+
58+mapByPatch
59+ :: PatchTarget1 p0 ~ PatchTarget1 p1
60+ => (p0 from to -> p1 from to)
61+ -> By k p0 from to
62+ -> By k p1 from to
63+mapByPatch f = \case
64+ By_Insert v -> By_Insert v
65+ By_Delete -> By_Delete
66+ By_Move k p -> By_Move k $ f p
67+
68+-- | Compose patches having the same effect as applying the patches in turn:
69+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
70+instance ( PatchSource1 p ~ PatchTarget1 p
71+ , Cat.Semigroupoid p
72+ , PatchHet2 p
73+ ) => Cat.Semigroupoid (By k p) where
74+ o p0 p1 = mapByPatch unProjectLocal $
75+ oLocal (mapByPatch ProjectLocal p0) (mapByPatch ProjectLocal p1)
76+
77+oLocal
78+ :: ( PatchSource1 p ~ PatchTarget1 p
79+ , PatchHet2Locally p between after
80+ , Cat.Semigroupoid p
81+ )
82+ => By k p between after
83+ -> By k p before between
84+ -> By k p before after
85+By_Insert new `oLocal` _ = By_Insert new
86+By_Delete `oLocal` _ = By_Delete
87+By_Move _ x `oLocal` By_Insert y = By_Insert $ applyAlwaysHet2Locally x y
88+By_Move _ x `oLocal` By_Move src y = By_Move src $ x `o` y
89+By_Move _ _ `oLocal` By_Delete = By_Delete
src/Data/Patch/IntMap.hs+3−1
@@ -35,8 +35,10 @@ instance Semigroup (PatchIntMap v) where
3535makeWrapped ''PatchIntMap
3636
3737-- | Apply the insertions or deletions to a given 'IntMap'.
38−instance Patch (PatchIntMap a) where
38+instance PatchHet (PatchIntMap a) where
39+ type PatchSource (PatchIntMap a) = IntMap a
3940 type PatchTarget (PatchIntMap a) = IntMap a
41+instance Patch (PatchIntMap a) where
4042 apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
4143 let removes = IntMap.filter isNothing p
4244 adds = IntMap.mapMaybe id p
src/Data/Patch/Map.hs+3−1
@@ -41,8 +41,10 @@ instance Ord k => Semigroup (PatchMap k v) where
4141makeWrapped ''PatchMap
4242
4343-- | Apply the insertions or deletions to a given 'Map'.
44−instance Ord k => Patch (PatchMap k v) where
44+instance Ord k => PatchHet (PatchMap k v) where
45+ type PatchSource (PatchMap k v) = Map k v
4546 type PatchTarget (PatchMap k v) = Map k v
47+instance Ord k => Patch (PatchMap k v) where
4648 {-# INLINABLE apply #-}
4749 apply (PatchMap p) old = Just $! insertions `Map.union` (old `Map.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
4850 where insertions = Map.mapMaybeWithKey (const id) p
src/Data/Patch/MapWithMove.hs+164−83
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE DeriveTraversable #-}
23{-# LANGUAGE FlexibleContexts #-}
34{-# LANGUAGE FlexibleInstances #-}
@@ -5,9 +6,11 @@
56{-# LANGUAGE MultiParamTypeClasses #-}
67{-# LANGUAGE PatternGuards #-}
78{-# LANGUAGE ScopedTypeVariables #-}
9+{-# LANGUAGE StandaloneDeriving #-}
810{-# LANGUAGE TemplateHaskell #-}
911{-# LANGUAGE TypeApplications #-}
1012{-# LANGUAGE TypeFamilies #-}
13+{-# LANGUAGE UndecidableInstances #-}
1114
1215-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
1316-- another
@@ -24,89 +27,120 @@ import Data.List
2427import Data.Map (Map)
2528import qualified Data.Map as Map
2629import Data.Maybe
30+--import Data.Proxy
31+#if !MIN_VERSION_base(4,9,0)
2732import Data.Semigroup (Semigroup (..), (<>))
33+#endif
34+import Data.Monoid.DecidablyEmpty
2835import qualified Data.Set as Set
29−import Data.These (These(..))
36+import Data.These (These (..))
3037import Data.Tuple
3138
3239-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
3340-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
3441-- and vice versa. There should never be any unpaired From/To keys.
35−newtype PatchMapWithMove k v = PatchMapWithMove
42+newtype PatchMapWithMove k p = PatchMapWithMove
3643 { -- | Extract the internal representation of the 'PatchMapWithMove'
37− unPatchMapWithMove :: Map k (NodeInfo k v)
44+ unPatchMapWithMove :: Map k (NodeInfo k p)
3845 }
39− deriving ( Show, Read, Eq, Ord
40− , Functor, Foldable, Traversable
41− )
46+deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (PatchMapWithMove k p)
47+deriving instance (Ord k, Read k, Read p, Read (PatchTarget p)) => Read (PatchMapWithMove k p)
48+deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (PatchMapWithMove k p)
49+deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (PatchMapWithMove k p)
50+
51+-- -- | Skips the patch so it's 'Functor' and friends
52+-- newtype PatchMapWithMove' k v = PatchMapWithMove' { unPatchMapWithMove' :: PatchMapWithMove k (Proxy v) }
53+--
54+-- deriving instance Functor (PatchMapWithMove' k)
55+-- deriving instance Foldable (PatchMapWithMove' k)
56+-- deriving instance Traversable (PatchMapWithMove' k)
4257
4358-- | Holds the information about each key: where its new value should come from,
4459-- and where its old value should go to
45−data NodeInfo k v = NodeInfo
46− { _nodeInfo_from :: !(From k v)
60+data NodeInfo k p = NodeInfo
61+ { _nodeInfo_from :: !(From k p)
4762 -- ^ Where do we get the new value for this key?
48− , _nodeInfo_to :: !(To k)
63+ , _nodeInfo_to :: !(To k p)
4964 -- ^ If the old value is being kept (i.e. moved rather than deleted or
5065 -- replaced), where is it going?
5166 }
52− deriving (Show, Read, Eq, Ord, Functor, Foldable, Traversable)
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)
5371
5472-- | Describe how a key's new value should be produced
55−data From k v
56− = From_Insert v -- ^ Insert the given value here
73+data From k p
74+ = From_Insert (PatchTarget p) -- ^ Insert the given value here
5775 | From_Delete -- ^ Delete the existing value, if any, from here
58− | From_Move !k -- ^ Move the value here from the given key
59− deriving (Show, Read, Eq, Ord, Functor, Foldable, Traversable)
76+ | From_Move !k !p -- ^ Move the value here from the given key, and apply the given patch
77+
78+deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (From k p)
79+deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (From k p)
80+deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (From k p)
81+deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (From k p)
6082
6183-- | Describe where a key's old value will go. If this is 'Just', that means
6284-- the key's old value will be moved to the given other key; if it is 'Nothing',
6385-- that means it will be deleted.
64−type To = Maybe
86+data To k p
87+ = To_NonMove
88+ | To_Move !k !p
89+ deriving ( Show, Read, Eq, Ord
90+ , Functor, Foldable, Traversable
91+ )
6592
6693makeWrapped ''PatchMapWithMove
94+--makeWrapped ''PatchMapWithMove'
6795
68−instance FunctorWithIndex k (PatchMapWithMove k)
69−instance FoldableWithIndex k (PatchMapWithMove k)
70−instance TraversableWithIndex k (PatchMapWithMove k) where
71− itraverse = itraversed . Indexed
72− itraversed = _Wrapped .> itraversed <. traversed
96+-- instance FunctorWithIndex k (PatchMapWithMove k)
97+-- instance FoldableWithIndex k (PatchMapWithMove k)
98+-- instance TraversableWithIndex k (PatchMapWithMove k) where
99+-- itraverse = itraversed . Indexed
100+-- itraversed = _Wrapped .> itraversed <. traversed
73101
74102-- | Create a 'PatchMapWithMove', validating it
75−patchMapWithMove :: Ord k => Map k (NodeInfo k v) -> Maybe (PatchMapWithMove k v)
103+patchMapWithMove
104+ :: Ord k => Map k (NodeInfo k p) -> Maybe (PatchMapWithMove k p)
76105patchMapWithMove m = if valid then Just $ PatchMapWithMove m else Nothing
77106 where valid = forwardLinks == backwardLinks
78− forwardLinks = Map.mapMaybe _nodeInfo_to m
79− backwardLinks = Map.fromList $ catMaybes $ flip fmap (Map.toList m) $ \(to, v) ->
80− case _nodeInfo_from v of
81− From_Move from -> Just (from, to)
107+ forwardLinks = flip Map.mapMaybe m $ \x -> case _nodeInfo_to x of
108+ To_NonMove -> Nothing
109+ To_Move to _ -> Just to
110+ backwardLinks = Map.fromList $ catMaybes $ flip fmap (Map.toList m) $ \(to, p) ->
111+ case _nodeInfo_from p of
112+ From_Move from _ -> Just (from, to)
82113 _ -> Nothing
83114
84115-- | Create a 'PatchMapWithMove' that inserts everything in the given 'Map'
85−patchMapWithMoveInsertAll :: Map k v -> PatchMapWithMove k v
116+patchMapWithMoveInsertAll
117+ :: Map k (PatchTarget p) -> PatchMapWithMove k p
86118patchMapWithMoveInsertAll m = PatchMapWithMove $ flip fmap m $ \v -> NodeInfo
87119 { _nodeInfo_from = From_Insert v
88− , _nodeInfo_to = Nothing
120+ , _nodeInfo_to = To_NonMove
89121 }
90122
91−-- | Make a @'PatchMapWithMove' k v@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
92−insertMapKey :: k -> v -> PatchMapWithMove k v
93−insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) Nothing
123+-- | Make a @'PatchMapWithMove' k p@ which has the effect of inserting or replacing a value @v@ at the given key @k@, like 'Map.insert'.
124+insertMapKey
125+ :: k -> PatchTarget p -> PatchMapWithMove k p
126+insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) To_NonMove
94127
95−-- |Make a @'PatchMapWithMove' k v@ which has the effect of moving the value from the first key @k@ to the second key @k@, equivalent to:
128+-- |Make a @'PatchMapWithMove' k p@ which has the effect of moving the value from the first key @k@ to the second key @k@, equivalent to:
96129--
97130-- @
98131-- 'Map.delete' src (maybe map ('Map.insert' dst) (Map.lookup src map))
99132-- @
100−moveMapKey :: Ord k => k -> k -> PatchMapWithMove k v
133+moveMapKey
134+ :: (DecidablyEmpty p, Patch p) => Ord k => k -> k -> PatchMapWithMove k p
101135moveMapKey src dst
102136 | src == dst = mempty
103137 | otherwise =
104138 PatchMapWithMove $ Map.fromList
105− [ (dst, NodeInfo (From_Move src) Nothing)
106− , (src, NodeInfo From_Delete (Just dst))
139+ [ (dst, NodeInfo (From_Move src mempty) To_NonMove)
140+ , (src, NodeInfo From_Delete (To_Move dst mempty))
107141 ]
108142
109−-- |Make a @'PatchMapWithMove' k v@ which has the effect of swapping two keys in the mapping, equivalent to:
143+-- |Make a @'PatchMapWithMove' k p@ which has the effect of swapping two keys in the mapping, equivalent to:
110144--
111145-- @
112146-- let aMay = Map.lookup a map
@@ -115,53 +149,70 @@ moveMapKey src dst
115149-- . maybe id (Map.insert b) (aMay `mplus` bMay)
116150-- . Map.delete a . Map.delete b $ map
117151-- @
118−swapMapKey :: Ord k => k -> k -> PatchMapWithMove k v
152+swapMapKey
153+ :: (DecidablyEmpty p, Patch p)
154+ => Ord k => k -> k -> PatchMapWithMove k p
119155swapMapKey src dst
120156 | src == dst = mempty
121157 | otherwise =
122158 PatchMapWithMove $ Map.fromList
123− [ (dst, NodeInfo (From_Move src) (Just src))
124− , (src, NodeInfo (From_Move dst) (Just dst))
159+ [ (dst, NodeInfo (From_Move src mempty) (To_Move dst mempty))
160+ , (src, NodeInfo (From_Move dst mempty) (To_Move dst mempty))
125161 ]
126162
127−-- |Make a @'PatchMapWithMove' k v@ which has the effect of deleting a key in the mapping, equivalent to 'Map.delete'.
128−deleteMapKey :: k -> PatchMapWithMove k v
129−deleteMapKey k = PatchMapWithMove . Map.singleton k $ NodeInfo From_Delete Nothing
163+-- | Make a @'PatchMapWithMove' k v@ which has the effect of deleting a key in
164+-- the mapping, equivalent to 'Map.delete'.
165+deleteMapKey
166+ :: k -> PatchMapWithMove k v
167+deleteMapKey k = PatchMapWithMove . Map.singleton k $ NodeInfo From_Delete To_NonMove
130168
131169-- | Wrap a @'Map' k (NodeInfo k v)@ representing patch changes into a @'PatchMapWithMove' k v@, without checking any invariants.
132170--
133171-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchMapWithMove' are preserved; they will not be checked.
134−unsafePatchMapWithMove :: Map k (NodeInfo k v) -> PatchMapWithMove k v
172+unsafePatchMapWithMove
173+ :: Map k (NodeInfo k p) -> PatchMapWithMove k p
135174unsafePatchMapWithMove = PatchMapWithMove
136175
137176-- | Apply the insertions, deletions, and moves to a given 'Map'
138−instance Ord k => Patch (PatchMapWithMove k v) where
139− type PatchTarget (PatchMapWithMove k v) = Map k v
140− apply (PatchMapWithMove p) old = Just $! insertions `Map.union` (old `Map.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
141− where insertions = flip Map.mapMaybeWithKey p $ \_ ni -> case _nodeInfo_from ni of
177+instance (Ord k, Patch p) => PatchHet (PatchMapWithMove k p) where
178+ type PatchSource (PatchMapWithMove k p) = Map k (PatchSource p)
179+ type PatchTarget (PatchMapWithMove k p) = Map k (PatchTarget p)
180+
181+instance (Ord k, Patch p) => Patch (PatchMapWithMove k p) where
182+ -- TODO: return Nothing sometimes
183+ -- Note: the strict application here is critical to ensuring that incremental
184+ -- merges don't hold onto all their prerequisite events forever; can we make
185+ -- this more robust?
186+ apply (PatchMapWithMove m) old = Just $! insertions `Map.union` (old `Map.difference` deletions)
187+ where insertions = flip Map.mapMaybeWithKey m $ \_ ni -> case _nodeInfo_from ni of
142188 From_Insert v -> Just v
143− From_Move k -> Map.lookup k old
189+ From_Move k p -> applyAlways p <$> Map.lookup k old
144190 From_Delete -> Nothing
145− deletions = flip Map.mapMaybeWithKey p $ \_ ni -> case _nodeInfo_from ni of
191+ deletions = flip Map.mapMaybeWithKey m $ \_ ni -> case _nodeInfo_from ni of
146192 From_Delete -> Just ()
147193 _ -> Nothing
148194
149−-- | Returns all the new elements that will be added to the 'Map'.
150−patchMapWithMoveNewElements :: PatchMapWithMove k v -> [v]
195+-- | Returns all the new elements that will be added to the 'Map'
196+patchMapWithMoveNewElements
197+ :: PatchMapWithMove k p -> [PatchTarget p]
151198patchMapWithMoveNewElements = Map.elems . patchMapWithMoveNewElementsMap
152199
153200-- | Return a @'Map' k v@ with all the inserts/updates from the given @'PatchMapWithMove' k v@.
154−patchMapWithMoveNewElementsMap :: PatchMapWithMove k v -> Map k v
201+patchMapWithMoveNewElementsMap
202+ :: PatchMapWithMove k p -> Map k (PatchTarget p)
155203patchMapWithMoveNewElementsMap (PatchMapWithMove p) = Map.mapMaybe f p
156204 where f ni = case _nodeInfo_from ni of
157205 From_Insert v -> Just v
158− From_Move _ -> Nothing
206+ From_Move _ _ -> Nothing
159207 From_Delete -> Nothing
160208
161209-- | Create a 'PatchMapWithMove' that, if applied to the given 'Map', will sort
162210-- its values using the given ordering function. The set keys of the 'Map' is
163211-- not changed.
164−patchThatSortsMapWith :: Ord k => (v -> v -> Ordering) -> Map k v -> PatchMapWithMove k v
212+patchThatSortsMapWith
213+ :: (Ord k, Monoid p)
214+ => (PatchTarget p -> PatchTarget p -> Ordering)
215+ -> Map k (PatchTarget p) -> PatchMapWithMove k p
165216patchThatSortsMapWith cmp m = PatchMapWithMove $ Map.fromList $ catMaybes $ zipWith g unsorted sorted
166217 where unsorted = Map.toList m
167218 sorted = sortBy (cmp `on` snd) unsorted
@@ -169,20 +220,25 @@ patchThatSortsMapWith cmp m = PatchMapWithMove $ Map.fromList $ catMaybes $ zipW
169220 Just (from, to)
170221 reverseMapping = Map.fromList $ catMaybes $ zipWith f unsorted sorted
171222 g (to, _) (from, _) = if to == from then Nothing else
172− let Just movingTo = Map.lookup to reverseMapping
173− in Just (to, NodeInfo (From_Move from) $ Just movingTo)
223+ let Just movingTo = Map.lookup from reverseMapping
224+ in Just (to, NodeInfo (From_Move from mempty) $ To_Move movingTo mempty)
174225
175226-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
176227-- will produce a 'Map' with the same values as the second 'Map' but with the
177228-- values sorted with the given ordering function.
178−patchThatChangesAndSortsMapWith :: forall k v. (Ord k, Ord v) => (v -> v -> Ordering) -> Map k v -> Map k v -> PatchMapWithMove k v
229+patchThatChangesAndSortsMapWith
230+ :: forall k p. (Ord k, Ord (PatchTarget p), Monoid p)
231+ => (PatchTarget p -> PatchTarget p -> Ordering)
232+ -> Map k (PatchTarget p) -> Map k (PatchTarget p) -> PatchMapWithMove k p
179233patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatChangesMap oldByIndex newByIndex
180234 where newList = Map.toList newByIndexUnsorted
181235 newByIndex = Map.fromList $ zip (fst <$> newList) $ sortBy cmp $ snd <$> newList
182236
183237-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
184238-- will produce the second 'Map'.
185−patchThatChangesMap :: (Ord k, Ord v) => Map k v -> Map k v -> PatchMapWithMove k v
239+patchThatChangesMap
240+ :: (Ord k, Ord (PatchTarget p), Monoid p)
241+ => Map k (PatchTarget p) -> Map k (PatchTarget p) -> PatchMapWithMove k p
186242patchThatChangesMap oldByIndex newByIndex = patch
187243 where oldByValue = Map.fromListWith Set.union $ swap . first Set.singleton <$> Map.toList oldByIndex
188244 (insertsAndMoves, unusedValuesByValue) = flip runState oldByValue $ do
@@ -192,61 +248,81 @@ patchThatChangesMap oldByIndex newByIndex = patch
192248 then Map.delete v remainingValues
193249 else Map.insert v remainingKeys remainingValues
194250 case Map.lookup v remainingValues of
195− Nothing -> return $ NodeInfo (From_Insert v) $ Just undefined -- There's no existing value we can take
251+ Nothing -> return $ NodeInfo (From_Insert v) $ To_Move undefined undefined -- There's no existing value we can take
196252 Just fromKs ->
197253 if k `Set.member` fromKs
198254 then do
199255 putRemainingKeys $ Set.delete k fromKs
200− return $ NodeInfo (From_Move k) $ Just undefined -- There's an existing value, and it's here, so no patch necessary
256+ return $ NodeInfo (From_Move k mempty) $ To_Move undefined undefined -- There's an existing value, and it's here, so no patch necessary
201257 else do
202258 (fromK, remainingKeys) <- return . fromJust $ Set.minView fromKs -- There's an existing value, but it's not here; move it here
203259 putRemainingKeys remainingKeys
204− return $ NodeInfo (From_Move fromK) $ Just undefined
260+ return $ NodeInfo (From_Move fromK mempty) $ To_Move undefined undefined
205261 Map.traverseWithKey f newByIndex
206262 unusedOldKeys = fold unusedValuesByValue
207263 pointlessMove k = \case
208− From_Move k' | k == k' -> True
264+ From_Move k' _ | k == k' -> True
209265 _ -> False
210266 keyWasMoved k = if k `Map.member` oldByIndex && not (k `Set.member` unusedOldKeys)
211− then Just undefined
212− else Nothing
213− patch = unsafePatchMapWithMove $ 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
267+ then To_Move undefined undefined
268+ else To_NonMove
269+ patch = unsafePatchMapWithMove $ Map.filterWithKey
270+ (\k -> not . pointlessMove k . _nodeInfo_from)
271+ $ Map.mergeWithKey
272+ (\k a _ -> Just $ nodeInfoSetTo (keyWasMoved k) a)
273+ (Map.mapWithKey $ \k -> nodeInfoSetTo $ keyWasMoved k)
274+ (Map.mapWithKey $ \k _ -> NodeInfo From_Delete $ keyWasMoved k)
275+ insertsAndMoves
276+ oldByIndex
214277
215278-- | Change the 'From' value of a 'NodeInfo'
216−nodeInfoMapFrom :: (From k v -> From k v) -> NodeInfo k v -> NodeInfo k v
279+nodeInfoMapFrom
280+ :: (From k v -> From k v) -> NodeInfo k v -> NodeInfo k v
217281nodeInfoMapFrom f ni = ni { _nodeInfo_from = f $ _nodeInfo_from ni }
218282
219283-- | Change the 'From' value of a 'NodeInfo', using a 'Functor' (or
220284-- 'Applicative', 'Monad', etc.) action to get the new value
221−nodeInfoMapMFrom :: Functor f => (From k v -> f (From k v)) -> NodeInfo k v -> f (NodeInfo k v)
285+nodeInfoMapMFrom
286+ :: Functor f => (From k v -> f (From k v)) -> NodeInfo k v -> f (NodeInfo k v)
222287nodeInfoMapMFrom f ni = fmap (\result -> ni { _nodeInfo_from = result }) $ f $ _nodeInfo_from ni
223288
224289-- | Set the 'To' field of a 'NodeInfo'
225−nodeInfoSetTo :: To k -> NodeInfo k v -> NodeInfo k v
290+nodeInfoSetTo
291+ :: To k p -> NodeInfo k p -> NodeInfo k p
226292nodeInfoSetTo to ni = ni { _nodeInfo_to = to }
227293
228−-- |Helper data structure used for composing patches using the monoid instance.
229−data Fixup k v
294+-- | Helper data structure used for composing patches using the monoid instance.
295+data Fixup k p
230296 = Fixup_Delete
231− | Fixup_Update (These (From k v) (To k))
297+ | Fixup_Update (These (From k p) (To k p))
232298
233−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
234−instance Ord k => Semigroup (PatchMapWithMove k v) where
299+-- | Compose patches having the same effect as applying the patches in turn:
300+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
301+instance ( Ord k
302+ , DecidablyEmpty p
303+ , Patch p
304+ ) => Semigroup (PatchMapWithMove k p) where
235305 PatchMapWithMove ma <> PatchMapWithMove mb = PatchMapWithMove m
236306 where
237307 connections = Map.toList $ Map.intersectionWithKey (\_ a b -> (_nodeInfo_to a, _nodeInfo_from b)) ma mb
238− h :: (k, (Maybe k, From k v)) -> [(k, Fixup k v)]
239− h (_, (mToAfter, editBefore)) = case (mToAfter, editBefore) of
240− (Just toAfter, From_Move fromBefore)
241− | fromBefore == toAfter
308+ h :: (k, (To k p, From k p)) -> [(k, Fixup k p)]
309+ h (_, (editAfter, editBefore)) = case (editAfter, editBefore) of
310+ (To_Move toAfter p1, From_Move fromBefore p0)
311+ | fromBefore == toAfter && isNull p0
242312 -> [(toAfter, Fixup_Delete)]
243313 | otherwise
244− -> [ (toAfter, Fixup_Update (This editBefore))
245− , (fromBefore, Fixup_Update (That mToAfter))
314+ -> [ (toAfter, Fixup_Update (This $ From_Move fromBefore $ p1 <> p0))
315+ , (fromBefore, Fixup_Update (That $ To_Move toAfter $ p1 <> p0))
246316 ]
247− (Nothing, From_Move fromBefore) -> [(fromBefore, Fixup_Update (That mToAfter))] -- The item is destroyed in the second patch, so indicate that it is destroyed in the source map
248− (Just toAfter, _) -> [(toAfter, Fixup_Update (This editBefore))]
249− (Nothing, _) -> []
317+ (To_NonMove, From_Move fromBefore _) ->
318+ -- The item is destroyed in the second patch, so indicate that it is
319+ -- destroyed in the source map
320+ [(fromBefore, Fixup_Update (That editAfter))]
321+ (To_Move toAfter p, From_Insert val) ->
322+ [(toAfter, Fixup_Update $ This $ From_Insert $ applyAlways p val)]
323+ (To_Move toAfter _, From_Delete) ->
324+ [(toAfter, Fixup_Update $ This From_Delete)]
325+ (To_NonMove, _) -> []
250326 mergeFixups _ Fixup_Delete Fixup_Delete = Fixup_Delete
251327 mergeFixups _ (Fixup_Update a) (Fixup_Update b)
252328 | This x <- a, That y <- b
@@ -278,7 +354,12 @@ instance Ord k => Semigroup (PatchMapWithMove k v) where
278354 That b -> Just b
279355
280356--TODO: Figure out how to implement this in terms of PatchDMapWithMove rather than duplicating it here
281−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
282−instance Ord k => Monoid (PatchMapWithMove k v) where
357+-- | Compose patches having the same effect as applying the patches in turn:
358+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
359+instance ( Ord k
360+ , Monoid p
361+ , DecidablyEmpty p
362+ , Patch p
363+ ) => Monoid (PatchMapWithMove k p) where
283364 mempty = PatchMapWithMove mempty
284365 mappend = (<>)