on c99f2d2 help sign in
13 files changed+808−216
since splitsnapshots
unifiedsplit
ChangeLog.md+4 patch.cabal+6−1 src/Control/Category/DecidablyEmpty.hs+9 src/Data/Functor/Misc.hs+28−5 src/Data/Patch.hs+7−5 src/Data/Patch/Class.hs+133−6 src/Data/Patch/DMap.hs+3−1 src/Data/Patch/DMapWithMove.hs+406−152 src/Data/Patch/DMapWithMove/By.hs+91 src/Data/Patch/IntMap.hs+3−1 src/Data/Patch/Map.hs+3−1 src/Data/Patch/MapWithMove.hs+70−44 src/Data/Patch/Patchable.hs+45
ChangeLog.md+4
@@ -4,6 +4,10 @@
44
55* `PatchMapWithMove` supports moves with a patch.
66
7+* Add `PatchDMapWithReset`
8+
9+* Add `Patchable`
10+
711## 0.0.1.0
812
913* Support older GHCs with `split-these` flag.
patch.cabal+6−1
@@ -39,19 +39,24 @@ library
3939 , transformers >= 0.5.6.0 && < 0.6
4040 , witherable >= 0.3 && < 0.3.2
4141
42− exposed-modules: Data.Functor.Misc
42+ exposed-modules: Control.Category.DecidablyEmpty
43+ , Data.Functor.Misc
44+ , Data.Monoid.DecidablyEmpty
4345 , Data.Patch
4446 , Data.Patch.Class
4547 , Data.Patch.DMap
4648 , Data.Patch.DMapWithMove
49+ , Data.Patch.DMapWithMove.By
4750 , Data.Patch.IntMap
4851 , Data.Patch.Map
4952 , Data.Patch.MapWithMove
53+ , Data.Patch.Patchable
5054
5155 ghc-options: -Wall -fwarn-redundant-constraints -fwarn-tabs
5256
5357 if flag(split-these)
5458 build-depends: these >= 1 && <1.1
59+ , these-lens >= 1 && <1.1
5560 , semialign >=1 && <1.2
5661 , monoidal-containers >= 0.6 && < 0.7
5762 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/Patch.hs+7−5
@@ -51,8 +51,10 @@ class Semigroup q => Additive q where
5151-- | The elements of an 'Additive' 'Semigroup' can be considered as patches of their own type.
5252newtype AdditivePatch p = AdditivePatch { unAdditivePatch :: p }
5353
54−instance Additive p => Patch (AdditivePatch p) where
54+instance Additive p => PatchHet (AdditivePatch p) where
55+ type PatchSource (AdditivePatch p) = p
5556 type PatchTarget (AdditivePatch p) = p
57+instance Additive p => Patch (AdditivePatch p) where
5658 apply (AdditivePatch p) q = Just $ p <> q
5759
5860instance (Ord k, Group q) => Group (MonoidalMap k q) where
@@ -62,8 +64,8 @@ instance (Ord k, Additive q) => Additive (MonoidalMap k q)
6264
6365-- | Trivial group.
6466instance Group () where
65− negateG _ = ()
66− _ ~~ _ = ()
67+ negateG ~() = ()
68+ ~() ~~ ~() = ()
6769instance Additive ()
6870
6971-- | Product group. A Pair of groups gives rise to a group
@@ -88,8 +90,8 @@ instance (Additive (f a), Additive (g a)) => Additive ((f :*: g) a)
8890
8991-- | Trivial group, Functor style
9092instance Group (Proxy x) where
91− negateG _ = Proxy
92− _ ~~ _ = Proxy
93+ negateG ~Proxy = Proxy
94+ ~Proxy ~~ ~Proxy = Proxy
9395instance Additive (Proxy x)
9496
9597-- | Const lifts groups into a functor.
src/Data/Patch/Class.hs+133−6
@@ -1,18 +1,54 @@
1+{-# LANGUAGE DefaultSignatures #-}
2+{-# LANGUAGE DeriveTraversable #-}
3+{-# LANGUAGE DerivingStrategies #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE MultiParamTypeClasses #-}
7+{-# LANGUAGE PolyKinds #-}
18{-# LANGUAGE TypeFamilies #-}
9+{-# LANGUAGE TypeOperators #-}
210-- | The interface for types which represent changes made to other types
311module Data.Patch.Class where
412
13+import qualified Data.Semigroupoid as Cat
514import Data.Functor.Identity
15+import Data.Functor.Misc
616import Data.Maybe
717import Data.Semigroup (Semigroup(..))
818import Data.Proxy
19+import Data.Type.Equality ((:~:) (..))
20+
21+class PatchHet p where
22+ type PatchSource p :: *
23+ type PatchTarget p :: *
24+ -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
25+ -- 'Nothing'.
26+ applyHet
27+ :: p
28+ -> PatchSource p
29+ -> Either (PatchSource p :~: PatchTarget p) (PatchTarget p)
30+ default applyHet
31+ :: Patch p
32+ => p
33+ -> PatchSource p
34+ -> Either (PatchSource p :~: PatchTarget p) (PatchTarget p)
35+ applyHet p a = case apply p a of
36+ Nothing -> Left Refl
37+ Just a' -> Right a'
38+
39+-- | Apply a 'PatchHet'; if it does nothing, return the original value
40+applyAlwaysHet :: PatchHet p => p -> PatchSource p -> PatchTarget p
41+applyAlwaysHet p t = case applyHet p t of
42+ Left Refl -> t
43+ Right t' -> t'
944
1045-- | A 'Patch' type represents a kind of change made to a datastructure.
1146--
1247-- If an instance of 'Patch' is also an instance of 'Semigroup', it should obey
1348-- the law that @applyAlways (f <> g) == applyAlways f . applyAlways g@.
14−class Patch p where
15− type PatchTarget p :: *
49+class ( PatchHet p
50+ , PatchSource p ~ PatchTarget p
51+ ) => Patch p where
1652 -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
1753 -- 'Nothing'.
1854 apply :: p -> PatchTarget p -> Maybe (PatchTarget p)
@@ -22,19 +58,110 @@ applyAlways :: Patch p => p -> PatchTarget p -> PatchTarget p
2258applyAlways p t = fromMaybe t $ apply p t
2359
2460-- | 'Identity' can be used as a 'Patch' that always fully replaces the value
25−instance Patch (Identity a) where
61+instance PatchHet (Identity a) where
62+ type PatchSource (Identity a) = a
2663 type PatchTarget (Identity a) = a
64+instance Patch (Identity a) where
2765 apply (Identity a) _ = Just a
2866
29−-- | 'Identity' can be used as a 'Patch' that always fully replaces the value
30−instance Patch (Proxy a) where
67+-- | 'Proxy' can be used as a 'Patch' that always fully replaces the value
68+instance PatchHet (Proxy (a :: *)) where
69+ type PatchSource (Proxy a) = a
3170 type PatchTarget (Proxy a) = a
71+instance Patch (Proxy (a :: *)) where
3272 apply ~Proxy _ = Nothing
3373
3474-- | Like '(.)', but composes functions that return patches rather than
3575-- functions that return new values. The Semigroup instance for patches must
3676-- apply patches right-to-left, like '(.)'.
37−composePatchFunctions :: (Patch p, Semigroup p) => (PatchTarget p -> p) -> (PatchTarget p -> p) -> PatchTarget p -> p
77+composePatchFunctions
78+ :: (Patch p, Semigroup p)
79+ => (PatchTarget p -> p)
80+ -> (PatchTarget p -> p)
81+ -> PatchTarget p -> p
3882composePatchFunctions g f a =
3983 let fp = f a
4084 in g (applyAlways fp a) <> fp
85+
86+
87+class PatchHet2Base (p :: k -> k -> *) where
88+ type PatchSource1 p :: k -> *
89+ type PatchTarget1 p :: k -> *
90+
91+class ( PatchHet2Base p
92+ , PatchHet (p from to)
93+ , PatchSource1 p from ~ PatchSource (p from to)
94+ , PatchTarget1 p to ~ PatchTarget (p from to)
95+ ) => PatchHet2Locally (p :: k -> k -> *) from to where
96+instance ( PatchHet2Base p
97+ , PatchHet (p from to)
98+ , PatchSource1 p from ~ PatchSource (p from to)
99+ , PatchTarget1 p to ~ PatchTarget (p from to)
100+ ) => PatchHet2Locally (p :: k -> k -> *) from to where
101+
102+applyHet2Locally
103+ :: PatchHet2Locally p from to
104+ => p from to
105+ -> PatchSource1 p from
106+ -> Either (PatchSource1 p from :~: PatchTarget1 p to) (PatchTarget1 p to)
107+applyHet2Locally = applyHet
108+
109+applyAlwaysHet2Locally
110+ :: PatchHet2Locally p from to
111+ => p from to
112+ -> PatchSource1 p from
113+ -> PatchTarget1 p to
114+applyAlwaysHet2Locally = applyAlwaysHet
115+
116+-- TODO once we can use quantified constraints, perhaps combine PatchHet2Base and
117+-- PatchHet2Locally, or at least get rid of this.
118+class PatchHet2Base p => PatchHet2 (p :: k -> k -> *) where
119+ applyHet2
120+ :: p from to
121+ -> PatchSource1 p from
122+ -> Either (PatchSource1 p from :~: PatchTarget1 p to) (PatchTarget1 p to)
123+
124+applyAlwaysHet2
125+ :: PatchHet2 p
126+ => p from to
127+ -> PatchSource1 p from
128+ -> PatchTarget1 p to
129+applyAlwaysHet2 p t = case applyHet2 p t of
130+ Left Refl -> t
131+ Right t' -> t'
132+
133+-- | Connect the classes without quanitified constraints
134+newtype ProjectLocal p from to = ProjectLocal { unProjectLocal :: p from to }
135+ deriving newtype Cat.Semigroupoid
136+
137+instance PatchHet2 p => PatchHet (ProjectLocal p from to) where
138+ type PatchSource (ProjectLocal p from to) = PatchSource1 p from
139+ type PatchTarget (ProjectLocal p from to) = PatchTarget1 p to
140+ applyHet (ProjectLocal p) src = applyHet2 p src
141+
142+instance PatchHet2 p => PatchHet2Base (ProjectLocal p) where
143+ type PatchSource1 (ProjectLocal p) = PatchSource1 p
144+ type PatchTarget1 (ProjectLocal p) = PatchTarget1 p
145+
146+class ( PatchHet2Base p
147+ , PatchSource1 p ~ PatchTarget1 p
148+ ) => Patch2 p
149+instance ( PatchHet2Base p
150+ , PatchSource1 p ~ PatchTarget1 p
151+ ) => Patch2 p
152+
153+-- | 'First2' can be used as a 'Patch' that always fully replaces the value
154+instance PatchHet (First2 (t :: k -> *) (from :: k) (to :: k)) where
155+ type PatchSource (First2 t from to) = t from
156+ type PatchTarget (First2 t from to) = t to
157+ applyHet (First2 val) _ = Right val
158+
159+-- | 'Proxy3' can be used as a 'Patch' that always does nothing
160+instance PatchHet (Proxy3 (t :: k -> *) (a :: k) (a :: k)) where
161+ type PatchSource (Proxy3 t a a) = t a
162+ type PatchTarget (Proxy3 t a a) = t a
163+ applyHet ~Proxy3 _ = Left Refl
164+
165+instance PatchHet2Base (Proxy3 (t :: k -> *) :: k -> k -> *) where
166+ type PatchSource1 (Proxy3 t) = t
167+ 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+406−152
@@ -1,101 +1,223 @@
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 #-}
14+{-# LANGUAGE TypeApplications #-}
1115{-# LANGUAGE TypeFamilies #-}
1216{-# LANGUAGE UndecidableInstances #-}
17+{-# LANGUAGE UndecidableSuperClasses #-}
1318
1419-- |Module containing @'PatchDMapWithMove' k v@ and associated functions, which represents a 'Patch' to a @'DMap' k v@ which can insert, update, delete, and
1520-- move values between keys.
1621module Data.Patch.DMapWithMove where
1722
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 (..))
23+import qualified Control.Category as Cat
24+--import qualified Control.Category.DecidablyEmpty as Cat
25+import Data.Constraint.Extras (Has')
26+import Data.Dependent.Map (DMap)
27+import Data.Dependent.Sum (DSum (..))
2428import qualified Data.Dependent.Map as DMap
25−import Data.Functor.Constant
29+import Data.Functor.Constant (Constant (..))
2630import Data.Functor.Misc
27−import Data.Functor.Product
28−import Data.GADT.Compare (GEq (..))
29−import Data.GADT.Show (GShow, gshow)
31+ ( Const2 (..), Proxy3 (..)
32+ , weakenDMapWith
33+ , dmapToMapWith
34+ )
35+import Data.Functor.Product (Product (..))
36+import Data.GADT.Compare (GEq (..), GCompare (..))
37+import Data.GADT.Show (GRead, GShow, gshow)
3038import qualified Data.Map as Map
3139import Data.Maybe
3240import Data.Semigroup (Semigroup (..), (<>))
41+import Data.Semigroupoid as Cat
3342import Data.Some (Some(Some))
34−import Data.Proxy
35−import Data.These
43+import Data.Proxy (Proxy (..))
44+import Data.These (These (..))
3645
37−-- | 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
38−-- destinations.
46+import Data.Patch.Class
47+ ( Patch (..), PatchHet (..)
48+ , PatchHet2 (..), PatchSource1, PatchTarget1
49+ , applyAlwaysHet2
50+ )
51+import Data.Patch.MapWithMove (PatchMapWithMove (..))
52+import qualified Data.Patch.MapWithMove as MapWithMove
53+
54+-- | Like 'PatchMapWithMove', but for 'DMap'. Each key carries a 'NodeInfo'
55+-- which describes how it will be changed by the patch and connects move sources
56+-- and destinations.
3957--
4058-- Invariants:
4159--
42−-- * A key should not move to itself.
43−-- * A move should always be represented with both the destination key (as a 'From_Move') and the source key (as a @'ComposeMaybe' ('Just' destination)@)
60+-- * A key should not move to itself.
61+--
62+-- * A move should always be represented with both the destination key (as a
63+-- 'From_Move') and the source key (as a @'ComposeMaybe' ('Just'
64+-- destination)@)
4465newtype PatchDMapWithMove k v = PatchDMapWithMove (DMap k (NodeInfo k v))
4566
46−-- |Structure which represents what changes apply to a particular key. @_nodeInfo_from@ specifies what happens to this key, and in particular what other key
47−-- the current key is moving from, while @_nodeInfo_to@ specifies what key the current key is moving to if involved in a move.
48−data NodeInfo k v a = NodeInfo
49− { _nodeInfo_from :: !(From k v a)
50− -- ^Change applying to the current key, be it an insert, move, or delete.
51− , _nodeInfo_to :: !(To k a)
52− -- ^Where this key is moving to, if involved in a move. Should only be @ComposeMaybe (Just k)@ when there is a corresponding 'From_Move'.
67+--deriving instance ( GShow k
68+-- , HasZip Show k p
69+-- , Has' Show k (PatchTarget1 p)
70+-- ) => Show (PatchDMapWithMove k p)
71+--deriving instance ( GRead k
72+-- , HasZip Read k p
73+-- , Has' Read k (PatchTarget1 p)
74+-- ) => Read (PatchDMapWithMove k p)
75+--deriving instance ( GEq k
76+-- , HasZip Eq k p
77+-- , Has' Eq k (PatchTarget1 p)
78+-- ) => Eq (PatchDMapWithMove k p)
79+--deriving instance ( GCompare k
80+-- , HasZip Ord k p
81+-- , Has' Ord k (PatchTarget1 p)
82+-- ) => Ord (PatchDMapWithMove k p)
83+
84+-- | Structure which represents what changes apply to a particular key.
85+-- @_nodeInfo_from@ specifies what happens to this key, and in particular what
86+-- other key the current key is moving from, while @_nodeInfo_to@ specifies what
87+-- key the current key is moving to if involved in a move.
88+data NodeInfo k p a = NodeInfo
89+ { _nodeInfo_from :: !(From k p a)
90+ -- ^ Change applying to the current key, be it an insert, move, or delete.
91+ , _nodeInfo_to :: !(To k p a)
92+ -- ^ Where this key is moving to, if involved in a move. Should only be
93+ -- @ComposeMaybe (Just k)@ when there is a corresponding 'From_Move'.
5394 }
54− deriving (Show)
55−
56−-- |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
57−-- key (@From_Delete@), or moving a key (@From_Move@).
58−data From (k :: a -> *) (v :: a -> *) :: a -> * where
59− -- |Insert a new or update an existing key with the given value @v a@
60− From_Insert :: v a -> From k v a
61− -- |Delete the existing key
62− From_Delete :: From k v a
63− -- |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@,
64− -- usually but not necessarily with @From_Delete@.
65− From_Move :: !(k a) -> From k v a
66− deriving (Show, Read, Eq, Ord)
67−
68−-- |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
69−-- operation.
70−type To = ComposeMaybe
95+
96+--deriving instance ( Show (k a)
97+-- , Show (p a a)
98+-- , Show (PatchTarget1 p a)
99+-- ) => Show (NodeInfo k p a)
100+--deriving instance ( Read (k a)
101+-- , Read (p a a)
102+-- , Read (PatchTarget1 p a)
103+-- ) => Read (NodeInfo k p a)
104+--deriving instance ( Eq (k a)
105+-- , Eq (p a a)
106+-- , Eq (PatchTarget1 p a)
107+-- ) => Eq (NodeInfo k p a)
108+--deriving instance ( Ord (k a)
109+-- , Ord (p a a)
110+-- , Ord (PatchTarget1 p a)
111+-- ) => Ord (NodeInfo k p a)
112+
113+-- | Structure describing a particular change to a key, be it inserting a new
114+-- key (@From_Insert@), updating an existing key (@From_Insert@ again), deleting
115+-- a key (@From_Delete@), or moving a key (@From_Move@).
116+--
117+-- This type isn't used directly as the from field patch, but is instead wrapped
118+-- in an existential. However, it is nice to be able to reason about this in
119+-- isolation as it is itself a @Semigroupoid@ when the underlying patch is.
120+data From (k :: a -> *) (p :: a -> a -> *) :: a -> * where
121+ -- | Insert a new or update an existing key with the given value @PatchTarget1
122+ -- p a@
123+ From_Insert :: PatchTarget1 p to -> From k p to
124+ -- | Delete the existing key
125+ From_Delete :: From k p to
126+ -- | Move the value from the given key @k a@ to this key. The source key
127+ -- should also have an entry in the patch giving the current key as
128+ -- @_nodeInfo_to@, usually but not necessarily with @From_Delete@.
129+ From_Move :: !(DSum k (Flip p to)) -> From k p to
130+
131+deriving instance ( Show (k a), GShow k
132+ , Has' Show k (Flip p a)
133+ , Show (PatchTarget1 p a)
134+ ) => Show (From k p a)
135+deriving instance ( Read (k a), GRead k
136+ , Has' Read k (Flip p a)
137+ , Read (PatchTarget1 p a)
138+ ) => Read (From k p a)
139+deriving instance ( GEq k
140+ , Has' Eq k (Flip p a)
141+ , Eq (PatchTarget1 p a)
142+ ) => Eq (From k p a)
143+deriving instance ( GCompare k
144+ , Has' Eq k (Flip p a) -- superclass bug
145+ , Has' Ord k (Flip p a)
146+ , Ord (PatchTarget1 p a)
147+ ) => Ord (From k p a)
148+
149+newtype Flip p to from = Flip (p from to)
150+
151+instance Cat.Category p => Cat.Category (Flip (p :: k -> k -> *)) where
152+ id = Flip Cat.id
153+ Flip y . Flip x = Flip $ x Cat.. y
154+
155+-- | The "to" part of a 'NodeInfo'. Rather than be built out of @From@ like @From@
156+-- is, we store just the information necessary to compose a @To@ and @From@ like
157+-- @oLocal@ composes two @From@s.
158+data To (k :: a -> *) (p :: a -> a -> *) :: a -> * where
159+ -- | Delete or leave in place
160+ To_NonMove :: To k p from
161+ -- | Move the value from the given key @k a@ to this key. The target key
162+ -- should also have an entry in the patch giving the current key in
163+ -- @_nodeInfo_from@, usually but not necessarily with @To_Delete@.
164+ To_Move :: !(DSum k (p from)) -> To k p from
165+
166+deriving instance ( Show (k a), GShow k
167+ , Has' Show k (p a)
168+ , Show (PatchTarget1 p a)
169+ ) => Show (To k p a)
170+deriving instance ( Read (k a), GRead k
171+ , Has' Read k (p a)
172+ , Read (PatchTarget1 p a)
173+ ) => Read (To k p a)
174+deriving instance ( GEq k
175+ , Has' Eq k (p a)
176+ , Eq (PatchTarget1 p a)
177+ ) => Eq (To k p a)
178+deriving instance ( GCompare k
179+ , Has' Eq k (p a) -- superclass bug
180+ , Has' Ord k (p a)
181+ , Ord (PatchTarget1 p a)
182+ ) => Ord (To k p a)
71183
72184-- |Test whether a 'PatchDMapWithMove' satisfies its invariants.
73−validPatchDMapWithMove :: forall k v. (GCompare k, GShow k) => DMap k (NodeInfo k v) -> Bool
185+validPatchDMapWithMove
186+ :: forall k v
187+ . (GCompare k, GShow k)
188+ => DMap k (NodeInfo k v)
189+ -> Bool
74190validPatchDMapWithMove = not . null . validationErrorsForPatchDMapWithMove
75191
76192-- |Enumerate what reasons a 'PatchDMapWithMove' doesn't satisfy its invariants, returning @[]@ if it's valid.
77−validationErrorsForPatchDMapWithMove :: forall k v. (GCompare k, GShow k) => DMap k (NodeInfo k v) -> [String]
193+validationErrorsForPatchDMapWithMove
194+ :: forall k v
195+ . (GCompare k, GShow k)
196+ => DMap k (NodeInfo k v)
197+ -> [String]
78198validationErrorsForPatchDMapWithMove m =
79199 noSelfMoves <> movesBalanced
80200 where
81201 noSelfMoves = mapMaybe selfMove . DMap.toAscList $ m
82− selfMove (dst :=> NodeInfo (From_Move src) _) | Just _ <- dst `geq` src = Just $ "self move of key " <> gshow src <> " at destination side"
83− selfMove (src :=> NodeInfo _ (ComposeMaybe (Just dst))) | Just _ <- src `geq` dst = Just $ "self move of key " <> gshow dst <> " at source side"
202+ selfMove (dst :=> NodeInfo (From_Move (src :=> _)) _)
203+ | Just _ <- dst `geq` src = Just $ "self move of key " <> gshow src <> " at destination side"
204+ selfMove (src :=> NodeInfo _ (To_Move (dst :=> _)))
205+ | Just _ <- src `geq` dst = Just $ "self move of key " <> gshow dst <> " at source side"
84206 selfMove _ = Nothing
85207 movesBalanced = mapMaybe unbalancedMove . DMap.toAscList $ m
86− unbalancedMove (dst :=> NodeInfo (From_Move src) _) =
208+ unbalancedMove (dst :=> NodeInfo (From_Move (src :=> _)) _) =
87209 case DMap.lookup src m of
88210 Nothing -> Just $ "unbalanced move at destination key " <> gshow dst <> " supposedly from " <> gshow src <> " but source key is not in the patch"
89− Just (NodeInfo _ (ComposeMaybe (Just dst'))) ->
211+ Just (NodeInfo _ (To_Move (dst' :=> _))) ->
90212 if isNothing (dst' `geq` dst)
91213 then Just $ "unbalanced move at destination key " <> gshow dst <> " from " <> gshow src <> " is going to " <> gshow dst' <> " instead"
92214 else Nothing
93215 _ ->
94216 Just $ "unbalanced move at destination key " <> gshow dst <> " supposedly from " <> gshow src <> " but source key has no move to key"
95− unbalancedMove (src :=> NodeInfo _ (ComposeMaybe (Just dst))) =
217+ unbalancedMove (src :=> NodeInfo _ (To_Move (dst :=> _))) =
96218 case DMap.lookup dst m of
97219 Nothing -> Just $ " unbalanced move at source key " <> gshow src <> " supposedly going to " <> gshow dst <> " but destination key is not in the patch"
98− Just (NodeInfo (From_Move src') _) ->
220+ Just (NodeInfo (From_Move (src' :=> _)) _) ->
99221 if isNothing (src' `geq` src)
100222 then Just $ "unbalanced move at source key " <> gshow src <> " to " <> gshow dst <> " is coming from " <> gshow src' <> " instead"
101223 else Nothing
@@ -104,35 +226,49 @@ validationErrorsForPatchDMapWithMove m =
104226 Just $ "unbalanced move at source key " <> gshow src <> " supposedly going to " <> gshow dst <> " but destination key is not moving"
105227 unbalancedMove _ = Nothing
106228
107−-- |Test whether two @'PatchDMapWithMove' k v@ contain the same patch operations.
108−instance (GEq k, Has' Eq k (NodeInfo k v)) => Eq (PatchDMapWithMove k v) where
109− PatchDMapWithMove a == PatchDMapWithMove b = a == b
110−
111229-- |Higher kinded 2-tuple, identical to @Data.Functor.Product@ from base ≥ 4.9
112230data Pair1 f g a = Pair1 (f a) (g a)
113231
114232-- |Helper data structure used for composing patches using the monoid instance.
115−data Fixup k v a
233+data Fixup k p a
116234 = Fixup_Delete
117− | Fixup_Update (These (From k v a) (To k a))
118−
119−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
120−instance GCompare k => Semigroup (PatchDMapWithMove k v) where
235+ | Fixup_Update (These (From k p a) (To k p a))
236+
237+-- | Compose patches having the same effect as applying the patches in turn:
238+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
239+instance ( GCompare k
240+ , Cat.Semigroupoid p
241+ -- , Cat.DecidablyEmpty p
242+ , PatchHet2 p
243+ , PatchSource1 p ~ PatchTarget1 p
244+ ) => Semigroup (PatchDMapWithMove k p) where
121245 PatchDMapWithMove ma <> PatchDMapWithMove mb = PatchDMapWithMove m
122246 where
123− connections = DMap.toList $ DMap.intersectionWithKey (\_ a b -> Pair1 (_nodeInfo_to a) (_nodeInfo_from b)) ma mb
124− h :: DSum k (Pair1 (ComposeMaybe k) (From k v)) -> [DSum k (Fixup k v)]
125− h (_ :=> Pair1 (ComposeMaybe mToAfter) editBefore) = case (mToAfter, editBefore) of
126− (Just toAfter, From_Move fromBefore)
127− | isJust $ fromBefore `geq` toAfter
128− -> [toAfter :=> Fixup_Delete]
129− | otherwise
130− -> [ toAfter :=> Fixup_Update (This editBefore)
131− , fromBefore :=> Fixup_Update (That (ComposeMaybe mToAfter))
132− ]
133− (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
134− (Just toAfter, _) -> [toAfter :=> Fixup_Update (This editBefore)]
135− (Nothing, _) -> []
247+ connections :: [DSum k (Pair1 (To k p) (From k p))]
248+ connections = DMap.toList $ DMap.intersectionWithKey
249+ (\_ a b -> Pair1 (_nodeInfo_to a) (_nodeInfo_from b))
250+ ma
251+ mb
252+ h :: DSum k (Pair1 (To k p) (From k p)) -> [DSum k (Fixup k p)]
253+ h ((_ :: k between) :=> Pair1 editAfter editBefore) = case (editAfter, editBefore) of
254+ (To_Move ((toAfter :: k after) :=> p1), From_Move ((fromBefore :: k before) :=> Flip p0)) ->
255+ --case toAfter `geq` fromBefore of
256+ -- Just Refl | Just Refl <- Cat.isId p0 ->
257+ -- [ toAfter :=> Fixup_Delete ]
258+ -- _ ->
259+ [ toAfter :=> Fixup_Update (This $ From_Move $ fromBefore :=> (Flip $ p1 `o` p0))
260+ , fromBefore :=> Fixup_Update (That $ To_Move $ toAfter :=> (p1 `o` p0))
261+ ]
262+ (To_NonMove, From_Move (fromBefore :=> _)) ->
263+ -- The item is destroyed in the second patch, so indicate that it is
264+ -- destroyed in the source map
265+ [fromBefore :=> Fixup_Update (That To_NonMove)]
266+ (To_Move (toAfter :=> p), From_Insert val) ->
267+ [toAfter :=> Fixup_Update (This $ From_Insert $ applyAlwaysHet2 p val)]
268+ (To_Move (toAfter :=> _), From_Delete) ->
269+ [toAfter :=> Fixup_Update (This From_Delete)]
270+ (To_NonMove, _) ->
271+ []
136272 mergeFixups _ Fixup_Delete Fixup_Delete = Fixup_Delete
137273 mergeFixups _ (Fixup_Update a) (Fixup_Update b)
138274 | This x <- a, That y <- b
@@ -163,13 +299,19 @@ instance GCompare k => Semigroup (PatchDMapWithMove k v) where
163299 These _ b -> Just b
164300 That b -> Just b
165301
166−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
167−instance GCompare k => Monoid (PatchDMapWithMove k v) where
302+-- | Compose patches having the same effect as applying the patches in turn:
303+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
304+instance ( GCompare k
305+ , Cat.Semigroupoid p
306+ -- , DecidablyEmpty p
307+ , PatchHet2 p
308+ , PatchSource1 p ~ PatchTarget1 p
309+ ) => Monoid (PatchDMapWithMove k p) where
168310 mempty = PatchDMapWithMove mempty
169311 mappend = (<>)
170312
171313{-
172−mappendPatchDMapWithMoveSlow :: forall k v. (ShowTag k v, GCompare k) => PatchDMapWithMove k v -> PatchDMapWithMove k v -> PatchDMapWithMove k v
314+mappendPatchDMapWithMoveSlow :: forall k v. (ShowTag k v, GCompare k) => PatchDMapWithMove k v -> PatchDMapWithMove k v -> PatchDMapWithMove k v
173315PatchDMapWithMove dstAfter srcAfter `mappendPatchDMapWithMoveSlow` PatchDMapWithMove dstBefore srcBefore = PatchDMapWithMove dst src
174316 where
175317 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
@@ -190,25 +332,31 @@ PatchDMapWithMove dstAfter srcAfter `mappendPatchDMapWithMoveSlow` PatchDMapWith
190332 src = DMap.mapMaybeWithKey g $ DMap.union srcAfter srcBefore
191333-}
192334
193−-- |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'.
194−insertDMapKey :: k a -> v a -> PatchDMapWithMove k v
335+-- | Make a @'PatchDMapWithMove' k v@ which has the effect of inserting or
336+-- updating a value @PatchTarget1 p a@ to the given key @k a@, like
337+-- 'DMap.insert'.
338+insertDMapKey :: k a -> PatchTarget1 p a -> PatchDMapWithMove k p
195339insertDMapKey k v =
196− PatchDMapWithMove . DMap.singleton k $ NodeInfo (From_Insert v) (ComposeMaybe Nothing)
340+ PatchDMapWithMove . DMap.singleton k $ NodeInfo (From_Insert v) To_NonMove
197341
198−-- |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:
342+-- | Make a @'PatchDMapWithMove' k v@ which has the effect of moving the value
343+-- from the first key @k a@ to the second key @k a@, equivalent to:
199344--
200345-- @
201346-- 'DMap.delete' src (maybe dmap ('DMap.insert' dst) (DMap.lookup src dmap))
202347-- @
203−moveDMapKey :: GCompare k => k a -> k a -> PatchDMapWithMove k v
348+moveDMapKey
349+ :: GCompare k
350+ => k a -> k a -> PatchDMapWithMove k (Proxy3 v)
204351moveDMapKey src dst = case src `geq` dst of
205352 Nothing -> PatchDMapWithMove $ DMap.fromList
206− [ dst :=> NodeInfo (From_Move src) (ComposeMaybe Nothing)
207− , src :=> NodeInfo From_Delete (ComposeMaybe $ Just dst)
353+ [ dst :=> NodeInfo (From_Move (src :=> Flip Proxy3)) To_NonMove
354+ , src :=> NodeInfo From_Delete (To_Move $ dst :=> Proxy3)
208355 ]
209− Just _ -> mempty
356+ Just _ -> PatchDMapWithMove DMap.empty
210357
211−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of swapping two keys in the mapping, equivalent to:
358+-- | Make a @'PatchDMapWithMove' k v@ which has the effect of swapping two keys
359+-- in the mapping, equivalent to:
212360--
213361-- @
214362-- let aMay = DMap.lookup a dmap
@@ -217,17 +365,18 @@ moveDMapKey src dst = case src `geq` dst of
217365-- . maybe id (DMap.insert b) (aMay `mplus` bMay)
218366-- . DMap.delete a . DMap.delete b $ dmap
219367-- @
220−swapDMapKey :: GCompare k => k a -> k a -> PatchDMapWithMove k v
368+swapDMapKey :: GCompare k => k a -> k a -> PatchDMapWithMove k (Proxy3 v)
221369swapDMapKey src dst = case src `geq` dst of
222370 Nothing -> PatchDMapWithMove $ DMap.fromList
223− [ dst :=> NodeInfo (From_Move src) (ComposeMaybe $ Just src)
224− , src :=> NodeInfo (From_Move dst) (ComposeMaybe $ Just dst)
371+ [ dst :=> NodeInfo (From_Move (src :=> Flip Proxy3)) (To_Move $ src :=> Proxy3)
372+ , src :=> NodeInfo (From_Move (dst :=> Flip Proxy3)) (To_Move $ dst :=> Proxy3)
225373 ]
226− Just _ -> mempty
374+ Just _ -> PatchDMapWithMove DMap.empty
227375
228−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of deleting a key in the mapping, equivalent to 'DMap.delete'.
376+-- | Make a @'PatchDMapWithMove' k v@ which has the effect of deleting a key in
377+-- the mapping, equivalent to 'DMap.delete'.
229378deleteDMapKey :: k a -> PatchDMapWithMove k v
230−deleteDMapKey k = PatchDMapWithMove $ DMap.singleton k $ NodeInfo From_Delete $ ComposeMaybe Nothing
379+deleteDMapKey k = PatchDMapWithMove $ DMap.singleton k $ NodeInfo From_Delete To_NonMove
231380
232381{-
233382k1, k2 :: Const2 Int () ()
@@ -253,17 +402,20 @@ dst (PatchDMapWithMove x _) = x
253402src (PatchDMapWithMove _ x) = x
254403-}
255404
256−-- |Extract the 'DMap' representing the patch changes from the 'PatchDMapWithMove'.
405+-- | Extract the 'DMap' representing the patch changes from the
406+-- 'PatchDMapWithMove'.
257407unPatchDMapWithMove :: PatchDMapWithMove k v -> DMap k (NodeInfo k v)
258408unPatchDMapWithMove (PatchDMapWithMove p) = p
259409
260−-- |Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove', without checking any invariants.
410+-- | Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove',
411+-- without checking any invariants.
261412--
262413-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchDMapWithMove' are preserved; they will not be checked.
263414unsafePatchDMapWithMove :: DMap k (NodeInfo k v) -> PatchDMapWithMove k v
264415unsafePatchDMapWithMove = PatchDMapWithMove
265416
266−-- |Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove' while checking invariants. If the invariants are satisfied, @Right p@ is returned
417+-- | Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove' while
418+-- checking invariants. If the invariants are satisfied, @Right p@ is returned
267419-- otherwise @Left errors@.
268420patchDMapWithMove :: (GCompare k, GShow k) => DMap k (NodeInfo k v) -> Either [String] (PatchDMapWithMove k v)
269421patchDMapWithMove dm =
@@ -271,94 +423,196 @@ patchDMapWithMove dm =
271423 [] -> Right $ unsafePatchDMapWithMove dm
272424 errs -> Left errs
273425
274−-- |Map a natural transform @v -> v'@ over the given patch, transforming @'PatchDMapWithMove' k v@ into @'PatchDMapWithMove' k v'@.
275−mapPatchDMapWithMove :: forall k v v'. (forall a. v a -> v' a) -> PatchDMapWithMove k v -> PatchDMapWithMove k v'
276−mapPatchDMapWithMove f (PatchDMapWithMove p) = PatchDMapWithMove $
277− DMap.map (\ni -> ni { _nodeInfo_from = g $ _nodeInfo_from ni }) p
278− where g :: forall a. From k v a -> From k v' a
279− g = \case
426+-- | Map a natural transform @v -> v'@ over the given patch, transforming
427+-- @'PatchDMapWithMove' k v@ into @'PatchDMapWithMove' k v'@.
428+mapPatchDMapWithMove
429+ :: forall k p p'
430+ . (forall a. PatchTarget1 p a -> PatchTarget1 p' a)
431+ -> (forall from to. p from to -> p' from to)
432+ -> PatchDMapWithMove k p
433+ -> PatchDMapWithMove k p'
434+mapPatchDMapWithMove f g (PatchDMapWithMove m) =
435+ PatchDMapWithMove $ DMap.map (\ni -> NodeInfo
436+ { _nodeInfo_from = h $ _nodeInfo_from ni
437+ , _nodeInfo_to = j $ _nodeInfo_to ni
438+ }) m
439+ where h :: forall a. From k p a -> From k p' a
440+ h = \case
280441 From_Insert v -> From_Insert $ f v
281442 From_Delete -> From_Delete
282− From_Move k -> From_Move k
283−
284−-- |Traverse an effectful function @forall a. v a -> m (v ' a)@ over the given patch, transforming @'PatchDMapWithMove' k v@ into @m ('PatchDMapWithMove' k v')@.
285−traversePatchDMapWithMove :: forall m k v v'. Applicative m => (forall a. v a -> m (v' a)) -> PatchDMapWithMove k v -> m (PatchDMapWithMove k v')
286−traversePatchDMapWithMove f = traversePatchDMapWithMoveWithKey $ const f
287−
288−-- |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')@.
289−traversePatchDMapWithMoveWithKey :: forall m k v v'. Applicative m => (forall a. k a -> v a -> m (v' a)) -> PatchDMapWithMove k v -> m (PatchDMapWithMove k v')
290−traversePatchDMapWithMoveWithKey f (PatchDMapWithMove p) = PatchDMapWithMove <$> DMap.traverseWithKey (nodeInfoMapFromM . g) p
291− where g :: forall a. k a -> From k v a -> m (From k v' a)
292− g k = \case
443+ From_Move (k :=> (Flip p)) -> From_Move $ k :=> Flip (g p)
444+ j :: forall a. To k p a -> To k p' a
445+ j = \case
446+ To_NonMove -> To_NonMove
447+ To_Move (k :=> p) -> To_Move $ k :=> g p
448+
449+-- | Traverse an effectful function @forall a. PatchTarget1 p a -> m (v ' a)@
450+-- over the given patch, transforming @'PatchDMapWithMove' k v@ into @m
451+-- ('PatchDMapWithMove' k v')@.
452+traversePatchDMapWithMove
453+ :: forall m k p p'
454+ . Applicative m
455+ => (forall a. PatchTarget1 p a -> m (PatchTarget1 p' a))
456+ -> (forall from to. p from to -> m (p' from to))
457+ -> PatchDMapWithMove k p
458+ -> m (PatchDMapWithMove k p')
459+traversePatchDMapWithMove f g = traversePatchDMapWithMoveWithKey
460+ (\_ -> f)
461+ (\_ _ -> g)
462+
463+-- | Map an effectful function @forall a. k a -> PatchTarget1 p a -> m (v ' a)@
464+-- over the given patch, transforming @'PatchDMapWithMove' k v@ into @m
465+-- ('PatchDMapWithMove' k v')@.
466+traversePatchDMapWithMoveWithKey
467+ :: forall m k p p'
468+ . Applicative m
469+ => (forall a. k a -> PatchTarget1 p a -> m (PatchTarget1 p' a))
470+ -> (forall from to. k from -> k to -> p from to -> m (p' from to))
471+ -> PatchDMapWithMove k p
472+ -> m (PatchDMapWithMove k p')
473+traversePatchDMapWithMoveWithKey f g (PatchDMapWithMove m) =
474+ fmap PatchDMapWithMove $ DMap.traverseWithKey (\k ni -> NodeInfo
475+ <$> (h k $ _nodeInfo_from ni)
476+ <*> (j k $ _nodeInfo_to ni)) m
477+ where h :: forall a. k a -> From k p a -> m (From k p' a)
478+ h k = \case
293479 From_Insert v -> From_Insert <$> f k v
294480 From_Delete -> pure From_Delete
295− From_Move fromKey -> pure $ From_Move fromKey
296−
297−-- |Map a function which transforms @'From' k v a@ into a @'From' k v' a@ over a @'NodeInfo' k v a@.
298−nodeInfoMapFrom :: (From k v a -> From k v' a) -> NodeInfo k v a -> NodeInfo k v' a
299−nodeInfoMapFrom f ni = ni { _nodeInfo_from = f $ _nodeInfo_from ni }
300−
301−-- |Map an effectful function which transforms @'From' k v a@ into a @f ('From' k v' a)@ over a @'NodeInfo' k v a@.
302−nodeInfoMapFromM :: Functor f => (From k v a -> f (From k v' a)) -> NodeInfo k v a -> f (NodeInfo k v' a)
303−nodeInfoMapFromM f ni = fmap (\result -> ni { _nodeInfo_from = result }) $ f $ _nodeInfo_from ni
304−
305−-- |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
306−-- values.
307−weakenPatchDMapWithMoveWith :: forall k v v'. (forall a. v a -> v') -> PatchDMapWithMove k v -> PatchMapWithMove (Some k) (Proxy v')
308−weakenPatchDMapWithMoveWith f (PatchDMapWithMove p) = PatchMapWithMove $ weakenDMapWith g p
309− where g :: forall a. NodeInfo k v a -> MapWithMove.NodeInfo (Some k) (Proxy v')
310− g ni = MapWithMove.NodeInfo
481+ From_Move (fromKey :=> Flip p) -> From_Move . (fromKey :=>) . Flip <$> g fromKey k p
482+ j :: forall a. k a -> To k p a -> m (To k p' a)
483+ j k = \case
484+ To_NonMove -> pure To_NonMove
485+ To_Move (toKey :=> p) -> To_Move . (toKey :=>) <$> g k toKey p
486+
487+-- | Map a function which transforms @'From k PatchTarget1 p a@ into a @'From k
488+-- PatchTarget1 p' a@ over a @'NodeInfo' k PatchTarget1 p a@.
489+nodeInfoMapFrom
490+ :: (From k p a -> From k p' a)
491+ -> (To k p a -> To k p' a)
492+ -> NodeInfo k p a
493+ -> NodeInfo k p' a
494+nodeInfoMapFrom f g ni = NodeInfo
495+ { _nodeInfo_from = f $ _nodeInfo_from ni
496+ , _nodeInfo_to = g $ _nodeInfo_to ni
497+ }
498+
499+-- | Map an effectful function which transforms @'From k PatchTarget1 p a@ into
500+-- a @f ('From k PatchTarget1 p' a)@ over a @'NodeInfo' k PatchTarget1 p a@.
501+nodeInfoMapFromM
502+ :: Applicative f
503+ => (From k p a -> f (From k p' a))
504+ -> (To k p a -> f (To k p' a))
505+ -> NodeInfo k p a
506+ -> f (NodeInfo k p' a)
507+nodeInfoMapFromM f g ni = NodeInfo
508+ <$> (f $ _nodeInfo_from ni)
509+ <*> (g $ _nodeInfo_to ni)
510+
511+-- | Weaken a 'PatchDMapWithMove' to a 'PatchMapWithMove' by weakening the keys
512+-- from @k a@ to @'Some' k@ and applying a given weakening function
513+-- @PatchTarget1 p a -> v'@ to values.
514+weakenPatchDMapWithMoveWith
515+ :: forall k p p'
516+ . (forall a. PatchTarget1 p a -> PatchTarget p')
517+ -> (forall from to. p from to -> p')
518+ -> PatchDMapWithMove k p
519+ -> PatchMapWithMove (Some k) p'
520+weakenPatchDMapWithMoveWith f g (PatchDMapWithMove m) =
521+ PatchMapWithMove $ weakenDMapWith h m
522+ where h :: forall a. NodeInfo k p a -> MapWithMove.NodeInfo (Some k) p'
523+ h ni = MapWithMove.NodeInfo
311524 { MapWithMove._nodeInfo_from = case _nodeInfo_from ni of
312525 From_Insert v -> MapWithMove.From_Insert $ f v
313526 From_Delete -> MapWithMove.From_Delete
314− From_Move k -> MapWithMove.From_Move (Some k) Proxy
315− , MapWithMove._nodeInfo_to = Some <$> getComposeMaybe (_nodeInfo_to ni)
527+ From_Move (k :=> Flip p) -> MapWithMove.From_Move (Some k) $ g p
528+ , MapWithMove._nodeInfo_to = case _nodeInfo_to ni of
529+ To_NonMove -> MapWithMove.To_NonMove
530+ To_Move (k :=> p) -> MapWithMove.To_Move (Some k) $ g p
316531 }
317532
318−-- |"Weaken" a @'PatchDMapWithMove' (Const2 k a) v@ to a @'PatchMapWithMove' k v'@. Weaken is in scare quotes because the 'Const2' has already disabled any
319−-- dependency in the typing and all points are already @a@, hence the function to map each value to @v'@ is not higher rank.
320−patchDMapWithMoveToPatchMapWithMoveWith :: forall k v v' a. (v a -> v') -> PatchDMapWithMove (Const2 k a) v -> PatchMapWithMove k (Proxy v')
321−patchDMapWithMoveToPatchMapWithMoveWith f (PatchDMapWithMove p) = PatchMapWithMove $ dmapToMapWith g p
322− where g :: NodeInfo (Const2 k a) v a -> MapWithMove.NodeInfo k (Proxy v')
323− g ni = MapWithMove.NodeInfo
533+-- | "Weaken" a @'PatchDMapWithMove' (Const2 k a) v@ to a @'PatchMapWithMove' k
534+-- v'@. Weaken is in scare quotes because the 'Const2' has already disabled any
535+-- dependency in the typing and all points are already @a@, hence the function
536+-- to map each value to @v'@ is not higher rank.
537+patchDMapWithMoveToPatchMapWithMoveWith
538+ :: forall k p p' a
539+ . (PatchTarget1 p a -> PatchTarget p')
540+ -> (p a a -> p')
541+ -> PatchDMapWithMove (Const2 k a) p
542+ -> PatchMapWithMove k p'
543+patchDMapWithMoveToPatchMapWithMoveWith f g (PatchDMapWithMove m) =
544+ PatchMapWithMove $ dmapToMapWith h m
545+ where h :: NodeInfo (Const2 k a) p a -> MapWithMove.NodeInfo k p'
546+ h ni = MapWithMove.NodeInfo
324547 { MapWithMove._nodeInfo_from = case _nodeInfo_from ni of
325548 From_Insert v -> MapWithMove.From_Insert $ f v
326549 From_Delete -> MapWithMove.From_Delete
327− From_Move (Const2 k) -> MapWithMove.From_Move k Proxy
328− , MapWithMove._nodeInfo_to = unConst2 <$> getComposeMaybe (_nodeInfo_to ni)
550+ From_Move (Const2 k :=> Flip p) -> MapWithMove.From_Move k $ g p
551+ , MapWithMove._nodeInfo_to = case _nodeInfo_to ni of
552+ To_NonMove -> MapWithMove.To_NonMove
553+ To_Move (Const2 k :=> p) -> MapWithMove.To_Move k $ g p
329554 }
330555
331−-- |"Strengthen" a @'PatchMapWithMove' k v@ into a @'PatchDMapWithMove ('Const2' k a)@; that is, turn a non-dependently-typed patch into a dependently typed
332−-- one but which always has a constant key type represented by 'Const2'. Apply the given function to each @v@ to produce a @v' a@.
333−-- Completemented by 'patchDMapWithMoveToPatchMapWithMoveWith'
334−const2PatchDMapWithMoveWith :: forall k v v' a. (v -> v' a) -> PatchMapWithMove k (Proxy v) -> PatchDMapWithMove (Const2 k a) v'
335−const2PatchDMapWithMoveWith f (PatchMapWithMove p) = PatchDMapWithMove $ DMap.fromDistinctAscList $ g <$> Map.toAscList p
336− where g :: (k, MapWithMove.NodeInfo k (Proxy v)) -> DSum (Const2 k a) (NodeInfo (Const2 k a) v')
556+-- | "Strengthen" a @'PatchMapWithMove' k v@ into a @'PatchDMapWithMove
557+-- ('Const2' k a)@; that is, turn a non-dependently-typed patch into a
558+-- dependently typed one but which always has a constant key type represented by
559+-- 'Const2'. Apply the given function to each @v@ to produce a @PatchTarget1 p'
560+-- a@. Completemented by 'patchDMapWithMoveToPatchMapWithMoveWith'
561+const2PatchDMapWithMoveWith
562+ :: forall k v v' a
563+ . (v -> v' a)
564+ -> PatchMapWithMove k (Proxy v)
565+ -> PatchDMapWithMove (Const2 k a) (Proxy3 v')
566+const2PatchDMapWithMoveWith f (PatchMapWithMove p) =
567+ PatchDMapWithMove $ DMap.fromDistinctAscList $ g <$> Map.toAscList p
568+ where g :: (k, MapWithMove.NodeInfo k (Proxy v))
569+ -> DSum (Const2 k a) (NodeInfo (Const2 k a) (Proxy3 v'))
337570 g (k, ni) = Const2 k :=> NodeInfo
338571 { _nodeInfo_from = case MapWithMove._nodeInfo_from ni of
339572 MapWithMove.From_Insert v -> From_Insert $ f v
340573 MapWithMove.From_Delete -> From_Delete
341− MapWithMove.From_Move fromKey Proxy -> From_Move $ Const2 fromKey
342− , _nodeInfo_to = ComposeMaybe $ Const2 <$> MapWithMove._nodeInfo_to ni
574+ MapWithMove.From_Move fromKey Proxy -> From_Move $ Const2 fromKey :=> Flip Proxy3
575+ , _nodeInfo_to = case MapWithMove._nodeInfo_to ni of
576+ MapWithMove.To_NonMove -> To_NonMove
577+ MapWithMove.To_Move toKey Proxy -> To_Move $ Const2 toKey :=> Proxy3
343578 }
344579
345580-- | Apply the insertions, deletions, and moves to a given 'DMap'.
346−instance GCompare k => Patch (PatchDMapWithMove k v) where
347− type PatchTarget (PatchDMapWithMove k v) = DMap k v
348− 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?
349− where insertions = DMap.mapMaybeWithKey insertFunc p
350− insertFunc :: forall a. k a -> NodeInfo k v a -> Maybe (v a)
581+instance ( GCompare k
582+ , PatchHet2 p
583+ , PatchSource1 p ~ PatchTarget1 p
584+ ) => PatchHet (PatchDMapWithMove k p) where
585+ type PatchSource (PatchDMapWithMove k p) = DMap k (PatchSource1 p)
586+ type PatchTarget (PatchDMapWithMove k p) = DMap k (PatchTarget1 p)
587+
588+instance ( GCompare k
589+ , PatchHet2 p
590+ , PatchSource1 p ~ PatchTarget1 p
591+ ) => Patch (PatchDMapWithMove k p) where
592+ apply (PatchDMapWithMove m) old = Just $! insertions `DMap.union` (old `DMap.difference` deletions)
593+ -- TODO: return Nothing sometimes --Note: the strict application here is
594+ -- critical to ensuring that incremental merges don't hold onto all their
595+ -- prerequisite events forever; can we make this more robust?
596+ where insertions = DMap.mapMaybeWithKey insertFunc m
597+ insertFunc :: forall a. k a -> NodeInfo k p a -> Maybe (PatchTarget1 p a)
351598 insertFunc _ ni = case _nodeInfo_from ni of
352599 From_Insert v -> Just v
353− From_Move k -> DMap.lookup k old
600+ From_Move (k :=> Flip p) -> applyAlwaysHet2 p <$> DMap.lookup k old
354601 From_Delete -> Nothing
355− deletions = DMap.mapMaybeWithKey deleteFunc p
356− deleteFunc :: forall a. k a -> NodeInfo k v a -> Maybe (Constant () a)
602+ deletions = DMap.mapMaybeWithKey deleteFunc m
603+ deleteFunc :: forall a. k a -> NodeInfo k p a -> Maybe (Constant () a)
357604 deleteFunc _ ni = case _nodeInfo_from ni of
358605 From_Delete -> Just $ Constant ()
359606 _ -> Nothing
360607
361−-- | Get the values that will be replaced, deleted, or moved if the given patch is applied to the given 'DMap'.
362−getDeletionsAndMoves :: GCompare k => PatchDMapWithMove k v -> DMap k v' -> DMap k (Product v' (ComposeMaybe k))
608+-- | Get the values that will be replaced, deleted, or moved if the given patch
609+-- is applied to the given 'DMap'.
610+getDeletionsAndMoves
611+ :: ( GCompare k
612+ , PatchSource1 p ~ PatchTarget1 p
613+ )
614+ => PatchDMapWithMove k p
615+ -> DMap k (PatchSource1 p)
616+ -> DMap k (Product (PatchTarget1 p) (To k p))
363617getDeletionsAndMoves (PatchDMapWithMove p) m = DMap.intersectionWithKey f m p
364618 where f _ v ni = Pair v $ _nodeInfo_to ni
src/Data/Patch/DMapWithMove/By.hsnew file+91
@@ -0,0 +1,91 @@
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 PatternGuards #-}
10+{-# LANGUAGE PolyKinds #-}
11+{-# LANGUAGE Rank2Types #-}
12+{-# LANGUAGE ScopedTypeVariables #-}
13+{-# LANGUAGE StandaloneDeriving #-}
14+{-# LANGUAGE TypeApplications #-}
15+{-# LANGUAGE TypeFamilies #-}
16+{-# LANGUAGE UndecidableInstances #-}
17+{-# LANGUAGE UndecidableSuperClasses #-}
18+
19+module Data.Patch.DMapWithMove.By where
20+
21+import Data.Semigroupoid as Cat
22+
23+import Data.Patch.Class
24+
25+-- | Structure describing a particular change to a key, be it inserting a new
26+-- key (@By_Insert@), updating an existing key (@By_Insert@ again), deleting
27+-- a key (@By_Delete@), or moving a key (@By_Move@).
28+--
29+-- This type isn't used directly as the from field patch, but is instead wrapped
30+-- in an existential. However, it is nice to be able to reason about this in
31+-- isolation as it is itself a @Semigroupoid@ when the underlying patch is.
32+data By (k :: a -> *) (p :: a -> a -> *) :: a -> a -> * where
33+ -- | Insert a new or update an existing key with the given value @PatchTarget1
34+ -- p a@
35+ By_Insert :: PatchTarget1 p to -> By k p from to
36+ -- | Delete the existing key
37+ By_Delete :: By k p from to
38+ -- | Move the value from the given key @k a@ to this key. The source key
39+ -- should also have an entry in the patch giving the current key as
40+ -- @_nodeInfo_to@, usually but not necessarily with @By_Delete@.
41+ By_Move :: !(k from) -> p from to -> By k p from to
42+
43+deriving instance ( Show (k from), Show (k to)
44+ , Show (p from to)
45+ , Show (PatchTarget1 p to)
46+ ) => Show (By k p from to)
47+deriving instance ( Read (k from), Read (k to)
48+ , Read (p from to)
49+ , Read (PatchTarget1 p to)
50+ ) => Read (By k p from to)
51+deriving instance ( Eq (k from), Eq (k to)
52+ , Eq (p from to)
53+ , Eq (PatchTarget1 p to)
54+ ) => Eq (By k p from to)
55+deriving instance ( Ord (k from), Ord (k to)
56+ , Ord (p from to)
57+ , Ord (PatchTarget1 p to)
58+ ) => Ord (By k p from to)
59+
60+mapByPatch
61+ :: PatchTarget1 p0 ~ PatchTarget1 p1
62+ => ((p0 from to) -> (p1 from to))
63+ -> By k p0 from to
64+ -> By k p1 from to
65+mapByPatch f = \case
66+ By_Insert v -> By_Insert v
67+ By_Delete -> By_Delete
68+ By_Move k p -> By_Move k $ f p
69+
70+-- | Compose patches having the same effect as applying the patches in turn:
71+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
72+instance ( PatchSource1 p ~ PatchTarget1 p
73+ , Cat.Semigroupoid p
74+ , PatchHet2 p
75+ ) => Cat.Semigroupoid (By k p) where
76+ o p0 p1 = mapByPatch unProjectLocal $
77+ oLocal (mapByPatch ProjectLocal p0) (mapByPatch ProjectLocal p1)
78+
79+oLocal
80+ :: ( PatchSource1 p ~ PatchTarget1 p
81+ , PatchHet2Locally p between after
82+ , Cat.Semigroupoid p
83+ )
84+ => By k p between after
85+ -> By k p before between
86+ -> By k p before after
87+By_Insert new `oLocal` _ = By_Insert new
88+By_Delete `oLocal` _ = By_Delete
89+By_Move _ x `oLocal` By_Insert y = By_Insert $ applyAlwaysHet2Locally x y
90+By_Move _ x `oLocal` By_Move src y = By_Move src $ x `o` y
91+By_Move _ _ `oLocal` By_Delete = By_Delete
src/Data/Patch/IntMap.hs+3−1
@@ -19,8 +19,10 @@ import Data.Patch.Class
1919newtype PatchIntMap a = PatchIntMap (IntMap (Maybe a)) deriving (Functor, Foldable, Traversable, Monoid)
2020
2121-- | Apply the insertions or deletions to a given 'IntMap'.
22−instance Patch (PatchIntMap a) where
22+instance PatchHet (PatchIntMap a) where
23+ type PatchSource (PatchIntMap a) = IntMap a
2324 type PatchTarget (PatchIntMap a) = IntMap a
25+instance Patch (PatchIntMap a) where
2426 apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
2527 let removes = IntMap.filter isNothing p
2628 adds = IntMap.mapMaybe id p
src/Data/Patch/Map.hs+3−1
@@ -18,8 +18,10 @@ newtype PatchMap k v = PatchMap { unPatchMap :: Map k (Maybe v) }
1818 deriving (Show, Read, Eq, Ord)
1919
2020-- | Apply the insertions or deletions to a given 'Map'.
21−instance Ord k => Patch (PatchMap k v) where
21+instance Ord k => PatchHet (PatchMap k v) where
22+ type PatchSource (PatchMap k v) = Map k v
2223 type PatchTarget (PatchMap k v) = Map k v
24+instance Ord k => Patch (PatchMap k v) where
2325 {-# INLINABLE apply #-}
2426 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?
2527 where insertions = Map.mapMaybeWithKey (const id) p
src/Data/Patch/MapWithMove.hs+70−44
@@ -41,7 +41,7 @@ deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (PatchMapWithMove k
4141data NodeInfo k p = NodeInfo
4242 { _nodeInfo_from :: !(From k p)
4343 -- ^ Where do we get the new value for this key?
44− , _nodeInfo_to :: !(To k)
44+ , _nodeInfo_to :: !(To k p)
4545 -- ^ If the old value is being kept (i.e. moved rather than deleted or
4646 -- replaced), where is it going?
4747 }
@@ -64,14 +64,23 @@ deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (From k p)
6464-- | Describe where a key's old value will go. If this is 'Just', that means
6565-- the key's old value will be moved to the given other key; if it is 'Nothing',
6666-- that means it will be deleted.
67−type To = Maybe
67+data To k p
68+ = To_NonMove
69+ | To_Move !k !p
70+
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)
6875
6976-- | Create a 'PatchMapWithMove', validating it
7077patchMapWithMove
7178 :: Ord k => Map k (NodeInfo k p) -> Maybe (PatchMapWithMove k p)
7279patchMapWithMove m = if valid then Just $ PatchMapWithMove m else Nothing
7380 where valid = forwardLinks == backwardLinks
74− forwardLinks = Map.mapMaybe _nodeInfo_to m
81+ forwardLinks = flip Map.mapMaybe m $ \x -> case _nodeInfo_to x of
82+ To_NonMove -> Nothing
83+ To_Move to _ -> Just to
7584 backwardLinks = Map.fromList $ catMaybes $ flip fmap (Map.toList m) $ \(to, p) ->
7685 case _nodeInfo_from p of
7786 From_Move from _ -> Just (from, to)
@@ -82,7 +91,7 @@ patchMapWithMoveInsertAll
8291 :: Map k (PatchTarget p) -> PatchMapWithMove k p
8392patchMapWithMoveInsertAll m = PatchMapWithMove $ flip fmap m $ \v -> NodeInfo
8493 { _nodeInfo_from = From_Insert v
85− , _nodeInfo_to = Nothing
94+ , _nodeInfo_to = To_NonMove
8695 }
8796
8897-- | Extract the internal representation of the 'PatchMapWithMove'
@@ -93,7 +102,7 @@ unPatchMapWithMove (PatchMapWithMove p) = p
93102-- | Make a @'PatchMapWithMove' k p@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
94103insertMapKey
95104 :: k -> PatchTarget p -> PatchMapWithMove k p
96−insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) Nothing
105+insertMapKey k v = PatchMapWithMove . Map.singleton k $ NodeInfo (From_Insert v) To_NonMove
97106
98107-- |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:
99108--
@@ -106,8 +115,8 @@ moveMapKey src dst
106115 | src == dst = mempty
107116 | otherwise =
108117 PatchMapWithMove $ Map.fromList
109− [ (dst, NodeInfo (From_Move src mempty) Nothing)
110− , (src, NodeInfo From_Delete (Just dst))
118+ [ (dst, NodeInfo (From_Move src mempty) To_NonMove)
119+ , (src, NodeInfo From_Delete (To_Move dst mempty))
111120 ]
112121
113122-- |Make a @'PatchMapWithMove' k p@ which has the effect of swapping two keys in the mapping, equivalent to:
@@ -120,19 +129,21 @@ moveMapKey src dst
120129-- . Map.delete a . Map.delete b $ map
121130-- @
122131swapMapKey
123− :: (DecidablyEmpty p, Patch p) => Ord k => k -> k -> PatchMapWithMove k p
132+ :: (DecidablyEmpty p, Patch p)
133+ => Ord k => k -> k -> PatchMapWithMove k p
124134swapMapKey src dst
125135 | src == dst = mempty
126136 | otherwise =
127137 PatchMapWithMove $ Map.fromList
128− [ (dst, NodeInfo (From_Move src mempty) (Just src))
129− , (src, NodeInfo (From_Move dst mempty) (Just dst))
138+ [ (dst, NodeInfo (From_Move src mempty) (To_Move dst mempty))
139+ , (src, NodeInfo (From_Move dst mempty) (To_Move dst mempty))
130140 ]
131141
132−-- |Make a @'PatchMapWithMove' k v@ which has the effect of deleting a key in the mapping, equivalent to 'Map.delete'.
142+-- | Make a @'PatchMapWithMove' k v@ which has the effect of deleting a key in
143+-- the mapping, equivalent to 'Map.delete'.
133144deleteMapKey
134145 :: k -> PatchMapWithMove k v
135−deleteMapKey k = PatchMapWithMove . Map.singleton k $ NodeInfo From_Delete Nothing
146+deleteMapKey k = PatchMapWithMove . Map.singleton k $ NodeInfo From_Delete To_NonMove
136147
137148-- | Wrap a @'Map' k (NodeInfo k v)@ representing patch changes into a @'PatchMapWithMove' k v@, without checking any invariants.
138149--
@@ -142,9 +153,16 @@ unsafePatchMapWithMove
142153unsafePatchMapWithMove = PatchMapWithMove
143154
144155-- | Apply the insertions, deletions, and moves to a given 'Map'
145−instance (Ord k, Patch p) => Patch (PatchMapWithMove k p) where
156+instance (Ord k, Patch p) => PatchHet (PatchMapWithMove k p) where
157+ type PatchSource (PatchMapWithMove k p) = Map k (PatchSource p)
146158 type PatchTarget (PatchMapWithMove k p) = Map k (PatchTarget p)
147− apply (PatchMapWithMove m) 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?
159+
160+instance (Ord k, Patch p) => Patch (PatchMapWithMove k p) where
161+ -- TODO: return Nothing sometimes
162+ -- Note: the strict application here is critical to ensuring that incremental
163+ -- merges don't hold onto all their prerequisite events forever; can we make
164+ -- this more robust?
165+ apply (PatchMapWithMove m) old = Just $! insertions `Map.union` (old `Map.difference` deletions)
148166 where insertions = flip Map.mapMaybeWithKey m $ \_ ni -> case _nodeInfo_from ni of
149167 From_Insert v -> Just v
150168 From_Move k p -> applyAlways p <$> Map.lookup k old
@@ -182,7 +200,7 @@ patchThatSortsMapWith cmp m = PatchMapWithMove $ Map.fromList $ catMaybes $ zipW
182200 reverseMapping = Map.fromList $ catMaybes $ zipWith f unsorted sorted
183201 g (to, _) (from, _) = if to == from then Nothing else
184202 let Just movingTo = Map.lookup from reverseMapping
185− in Just (to, NodeInfo (From_Move from mempty) $ Just movingTo)
203+ in Just (to, NodeInfo (From_Move from mempty) $ To_Move movingTo mempty)
186204
187205-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
188206-- will produce a 'Map' with the same values as the second 'Map' but with the
@@ -209,25 +227,32 @@ patchThatChangesMap oldByIndex newByIndex = patch
209227 then Map.delete v remainingValues
210228 else Map.insert v remainingKeys remainingValues
211229 case Map.lookup v remainingValues of
212− Nothing -> return $ NodeInfo (From_Insert v) $ Just undefined -- There's no existing value we can take
230+ Nothing -> return $ NodeInfo (From_Insert v) $ To_Move undefined undefined -- There's no existing value we can take
213231 Just fromKs ->
214232 if k `Set.member` fromKs
215233 then do
216234 putRemainingKeys $ Set.delete k fromKs
217− return $ NodeInfo (From_Move k mempty) $ Just undefined -- There's an existing value, and it's here, so no patch necessary
235+ return $ NodeInfo (From_Move k mempty) $ To_Move undefined undefined -- There's an existing value, and it's here, so no patch necessary
218236 else do
219237 (fromK, remainingKeys) <- return . fromJust $ Set.minView fromKs -- There's an existing value, but it's not here; move it here
220238 putRemainingKeys remainingKeys
221− return $ NodeInfo (From_Move fromK mempty) $ Just undefined
239+ return $ NodeInfo (From_Move fromK mempty) $ To_Move undefined undefined
222240 Map.traverseWithKey f newByIndex
223241 unusedOldKeys = fold unusedValuesByValue
224242 pointlessMove k = \case
225243 From_Move k' _ | k == k' -> True
226244 _ -> False
227245 keyWasMoved k = if k `Map.member` oldByIndex && not (k `Set.member` unusedOldKeys)
228− then Just undefined
229− else Nothing
230− 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
246+ then To_Move undefined undefined
247+ else To_NonMove
248+ patch = unsafePatchMapWithMove $ Map.filterWithKey
249+ (\k -> not . pointlessMove k . _nodeInfo_from)
250+ $ Map.mergeWithKey
251+ (\k a _ -> Just $ nodeInfoSetTo (keyWasMoved k) a)
252+ (Map.mapWithKey $ \k -> nodeInfoSetTo $ keyWasMoved k)
253+ (Map.mapWithKey $ \k _ -> NodeInfo From_Delete $ keyWasMoved k)
254+ insertsAndMoves
255+ oldByIndex
231256
232257-- | Change the 'From' value of a 'NodeInfo'
233258nodeInfoMapFrom
@@ -242,35 +267,41 @@ nodeInfoMapMFrom f ni = fmap (\result -> ni { _nodeInfo_from = result }) $ f $ _
242267
243268-- | Set the 'To' field of a 'NodeInfo'
244269nodeInfoSetTo
245− :: To k -> NodeInfo k v -> NodeInfo k v
270+ :: To k p -> NodeInfo k p -> NodeInfo k p
246271nodeInfoSetTo to ni = ni { _nodeInfo_to = to }
247272
248−-- |Helper data structure used for composing patches using the monoid instance.
249−data Fixup k v
273+-- | Helper data structure used for composing patches using the monoid instance.
274+data Fixup k p
250275 = Fixup_Delete
251− | Fixup_Update (These (From k v) (To k))
276+ | Fixup_Update (These (From k p) (To k p))
252277
253−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
278+-- | Compose patches having the same effect as applying the patches in turn:
279+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
254280instance ( Ord k
255− , Monoid p
256281 , DecidablyEmpty p
257282 , Patch p
258283 ) => Semigroup (PatchMapWithMove k p) where
259284 PatchMapWithMove ma <> PatchMapWithMove mb = PatchMapWithMove m
260285 where
261286 connections = Map.toList $ Map.intersectionWithKey (\_ a b -> (_nodeInfo_to a, _nodeInfo_from b)) ma mb
262− h :: (k, (Maybe k, From k p)) -> [(k, Fixup k p)]
263− h (_, (mToAfter, editBefore)) = case (mToAfter, editBefore) of
264− (Just toAfter, From_Move fromBefore p)
265− | fromBefore == toAfter && isNull p
287+ h :: (k, (To k p, From k p)) -> [(k, Fixup k p)]
288+ h (_, (editAfter, editBefore)) = case (editAfter, editBefore) of
289+ (To_Move toAfter p1, From_Move fromBefore p0)
290+ | fromBefore == toAfter && isNull p0
266291 -> [(toAfter, Fixup_Delete)]
267292 | otherwise
268− -> [ (toAfter, Fixup_Update (This editBefore))
269− , (fromBefore, Fixup_Update (That mToAfter))
293+ -> [ (toAfter, Fixup_Update (This $ From_Move fromBefore $ p1 <> p0))
294+ , (fromBefore, Fixup_Update (That $ To_Move toAfter $ p1 <> p0))
270295 ]
271− (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
272− (Just toAfter, _) -> [(toAfter, Fixup_Update (This editBefore))]
273− (Nothing, _) -> []
296+ (To_NonMove, From_Move fromBefore _) ->
297+ -- The item is destroyed in the second patch, so indicate that it is
298+ -- destroyed in the source map
299+ [(fromBefore, Fixup_Update (That editAfter))]
300+ (To_Move toAfter p, From_Insert val) ->
301+ [(toAfter, Fixup_Update $ This $ From_Insert $ applyAlways p val)]
302+ (To_Move toAfter _, From_Delete) ->
303+ [(toAfter, Fixup_Update $ This From_Delete)]
304+ (To_NonMove, _) -> []
274305 mergeFixups _ Fixup_Delete Fixup_Delete = Fixup_Delete
275306 mergeFixups _ (Fixup_Update a) (Fixup_Update b)
276307 | This x <- a, That y <- b
@@ -286,13 +317,7 @@ instance ( Ord k
286317 applyFixup _ ni = \case
287318 Fixup_Delete -> Nothing
288319 Fixup_Update u -> Just $ NodeInfo
289− { _nodeInfo_from = case _nodeInfo_from ni of
290− f@(From_Move _ p') -> case getHere u of -- The `from` fixup comes from the "old" patch
291− Nothing -> f -- If there's no `from` fixup, just use the "new" `from`
292− Just (From_Insert v) -> From_Insert $ applyAlways p' v
293− Just From_Delete -> From_Delete
294− Just (From_Move oldKey p) -> From_Move oldKey $ p' <> p
295− _ -> error "PatchMapWithMove: fixup for non-move From"
320+ { _nodeInfo_from = fromMaybe (_nodeInfo_from ni) $ getHere u
296321 , _nodeInfo_to = fromMaybe (_nodeInfo_to ni) $ getThere u
297322 }
298323 m = Map.differenceWithKey applyFixup (Map.unionWithKey combineNodeInfos ma mb) fixups
@@ -308,7 +333,8 @@ instance ( Ord k
308333 That b -> Just b
309334
310335--TODO: Figure out how to implement this in terms of PatchDMapWithMove rather than duplicating it here
311−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
336+-- | Compose patches having the same effect as applying the patches in turn:
337+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
312338instance ( Ord k
313339 , Monoid p
314340 , DecidablyEmpty p
src/Data/Patch/Patchable.hsnew file+45
@@ -0,0 +1,45 @@
1+{-# LANGUAGE DeriveGeneric #-}
2+{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE LambdaCase #-}
4+{-# LANGUAGE StandaloneDeriving #-}
5+{-# LANGUAGE TypeFamilies #-}
6+
7+-- The derived instances are undecidable in the case of a pathological instance like
8+-- instance Patch x where
9+-- type PatchTarget x = Patchable x
10+{-# LANGUAGE UndecidableInstances #-}
11+
12+module Data.Patch.Patchable where
13+
14+-- import Data.Aeson
15+import GHC.Generics
16+
17+import Data.Patch
18+
19+-- | Like SemiMap/PartialMap but for anything patchable
20+data Patchable p
21+ = Patchable_Patch p
22+ | Patchable_Complete (PatchTarget p)
23+ deriving (Generic)
24+
25+completePatchable :: Patchable p -> Maybe (PatchTarget p)
26+completePatchable = \case
27+ Patchable_Complete t -> Just t
28+ Patchable_Patch _ -> Nothing
29+
30+deriving instance (Eq p, Eq (PatchTarget p)) => Eq (Patchable p)
31+deriving instance (Ord p, Ord (PatchTarget p)) => Ord (Patchable p)
32+deriving instance (Show p, Show (PatchTarget p)) => Show (Patchable p)
33+deriving instance (Read p, Read (PatchTarget p)) => Read (Patchable p)
34+-- instance (ToJSON p, ToJSON (PatchTarget p)) => ToJSON (Patchable p)
35+-- instance (FromJSON p, FromJSON (PatchTarget p)) => FromJSON (Patchable p)
36+
37+instance (Monoid p, Patch p) => Monoid (Patchable p) where
38+ mempty = Patchable_Patch mempty
39+ mappend = (<>)
40+
41+instance (Semigroup p, Patch p) => Semigroup (Patchable p) where
42+ (<>) = curry $ \case
43+ (Patchable_Patch a, Patchable_Patch b) -> Patchable_Patch $ a <> b
44+ (Patchable_Patch a, Patchable_Complete b) -> Patchable_Complete $ applyAlways a b
45+ (Patchable_Complete a, _) -> Patchable_Complete a