on 4da27a8 help sign in
14 files changed+924−22
since splitsnapshots
unifiedsplit
ChangeLog.md+4 patch.cabal+5−1 src/Control/Category/DecidablyEmpty.hs+9 src/Data/Functor/Misc.hs+28−5 src/Data/Patch.hs+13−5 src/Data/Patch/Class.hs+131−5 src/Data/Patch/DMap.hs+3−1 src/Data/Patch/DMapWithMove.hs+4−1 src/Data/Patch/DMapWithPatchingMove.hs+624 src/Data/Patch/DMapWithPatchingMove/By.hs+89 src/Data/Patch/IntMap.hs+3−1 src/Data/Patch/Map.hs+3−1 src/Data/Patch/MapWithMove.hs+4−1 src/Data/Patch/MapWithPatchingMove.hs+4−1
ChangeLog.md+4
@@ -2,6 +2,10 @@
22
33## Unreleased
44
5+* `PatchMapWithMove` supports moves with a patch.
6+
7+* `PatchDMapWithMove` supports moves with a patch.
8+
59* Rewrite `PatchMapWithMove` in terms of `PatchMapWithPatchingMove`.
610 Care is taken to make this not a breaking change.
711 In particular, `PatchMapWithMove` is a newtype of `PatchMapWithPatchingMove`, as is the `NodeInfo` and `From` of `PatchMapWithPatchingMove`'s versions of those.
patch.cabal+5−1
@@ -43,12 +43,15 @@ library
4343 , transformers >= 0.5.6.0 && < 0.6
4444 , witherable >= 0.3 && < 0.4
4545
46− exposed-modules: Data.Functor.Misc
46+ exposed-modules: Control.Category.DecidablyEmpty
47+ , Data.Functor.Misc
4748 , Data.Monoid.DecidablyEmpty
4849 , Data.Patch
4950 , Data.Patch.Class
5051 , Data.Patch.DMap
5152 , Data.Patch.DMapWithMove
53+ , Data.Patch.DMapWithPatchingMove
54+ , Data.Patch.DMapWithPatchingMove.By
5255 , Data.Patch.IntMap
5356 , Data.Patch.Map
5457 , Data.Patch.MapWithMove
@@ -59,6 +62,7 @@ library
5962
6063 if flag(split-these)
6164 build-depends: these >= 1 && <1.2
65+ , these-lens >= 1 && <1.1
6266 , semialign >=1 && <1.2
6367 , monoidal-containers >= 0.6 && < 0.7
6468 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 #-}
@@ -16,6 +16,8 @@
1616module Data.Functor.Misc
1717 ( -- * Const2
1818 Const2 (..)
19+ , Proxy3 (..)
20+ , First2 (..)
1921 , unConst2
2022 , dmapToMap
2123 , dmapToIntMap
@@ -36,6 +38,7 @@ module Data.Functor.Misc
3638 , ComposeMaybe (..)
3739 ) where
3840
41+import qualified Control.Category as Cat
3942import Control.Applicative ((<$>))
4043import Data.Dependent.Map (DMap)
4144import qualified Data.Dependent.Map as DMap
@@ -47,6 +50,7 @@ import Data.IntMap (IntMap)
4750import qualified Data.IntMap as IntMap
4851import Data.Map (Map)
4952import qualified Data.Map as Map
53+import qualified Data.Semigroupoid as Cat
5054import Data.Some (Some, mkSome)
5155import Data.These
5256import Data.Type.Equality ((:~:)(Refl))
@@ -62,15 +66,15 @@ data Const2 :: * -> x -> x -> * where
6266 Const2 :: k -> Const2 k v v
6367 deriving (Typeable)
6468
65−-- | Extract the value from a Const2
66−unConst2 :: Const2 k v v' -> k
67−unConst2 (Const2 k) = k
68−
6969deriving instance Eq k => Eq (Const2 k v v')
7070deriving instance Ord k => Ord (Const2 k v v')
7171deriving instance Show k => Show (Const2 k v v')
7272deriving instance Read k => Read (Const2 k v v)
7373
74+-- | Extract the value from a Const2
75+unConst2 :: Const2 k v v' -> k
76+unConst2 (Const2 k) = k
77+
7478instance Show k => GShow (Const2 k v) where
7579 gshowsPrec n x@(Const2 _) = showsPrec n x
7680
@@ -86,6 +90,25 @@ instance Ord k => GCompare (Const2 k v) where
8690 EQ -> GEQ
8791 GT -> GGT
8892
93+data Proxy3 :: x -> y -> z -> * where
94+ Proxy3 :: Proxy3 vx vy vz
95+ deriving ( Show, Read, Eq, Ord
96+ , Functor, Foldable, Traversable
97+ , Typeable
98+ )
99+
100+instance Cat.Category (Proxy3 x) where
101+ id = Proxy3
102+ ~Proxy3 . ~Proxy3 = Proxy3
103+
104+newtype First2 (t :: k -> *) (a :: k) (b :: k) = First2 (t b)
105+ deriving ( Show, Read, Eq, Ord
106+ , Functor, Foldable, Traversable
107+ )
108+
109+instance Cat.Semigroupoid (First2 x) where
110+ First2 x `o` ~(First2 _) = First2 x
111+
89112-- | Convert a 'DMap' to a regular 'Map'
90113dmapToMap :: DMap (Const2 k v) Identity -> Map k v
91114dmapToMap = Map.fromDistinctAscList . map (\(Const2 k :=> Identity v) -> (k, v)) . DMap.toAscList
src/Data/Patch.hs+13−5
@@ -31,6 +31,12 @@ import Data.Patch.DMapWithMove as X
3131 , traversePatchDMapWithMoveWithKey, unPatchDMapWithMove
3232 , unsafePatchDMapWithMove, weakenPatchDMapWithMoveWith
3333 )
34+import Data.Patch.DMapWithPatchingMove as X
35+ ( PatchDMapWithPatchingMove, const2PatchDMapWithPatchingMoveWith, mapPatchDMapWithPatchingMove
36+ , patchDMapWithPatchingMoveToPatchMapWithPatchingMoveWith
37+ , traversePatchDMapWithPatchingMoveWithKey, unPatchDMapWithPatchingMove
38+ , unsafePatchDMapWithPatchingMove, weakenPatchDMapWithPatchingMoveWith
39+ )
3440import Data.Patch.IntMap as X hiding (getDeletions)
3541import Data.Patch.Map as X
3642import Data.Patch.MapWithMove as X
@@ -51,8 +57,10 @@ class Semigroup q => Additive q where
5157-- | The elements of an 'Additive' 'Semigroup' can be considered as patches of their own type.
5258newtype AdditivePatch p = AdditivePatch { unAdditivePatch :: p }
5359
54−instance Additive p => Patch (AdditivePatch p) where
60+instance Additive p => PatchHet (AdditivePatch p) where
61+ type PatchSource (AdditivePatch p) = p
5562 type PatchTarget (AdditivePatch p) = p
63+instance Additive p => Patch (AdditivePatch p) where
5664 apply (AdditivePatch p) q = Just $ p <> q
5765
5866instance (Ord k, Group q) => Group (MonoidalMap k q) where
@@ -62,8 +70,8 @@ instance (Ord k, Additive q) => Additive (MonoidalMap k q)
6270
6371-- | Trivial group.
6472instance Group () where
65− negateG _ = ()
66− _ ~~ _ = ()
73+ negateG ~() = ()
74+ ~() ~~ ~() = ()
6775instance Additive ()
6876
6977-- | Product group. A Pair of groups gives rise to a group
@@ -88,8 +96,8 @@ instance (Additive (f a), Additive (g a)) => Additive ((f :*: g) a)
8896
8997-- | Trivial group, Functor style
9098instance Group (Proxy x) where
91− negateG _ = Proxy
92− _ ~~ _ = Proxy
99+ negateG ~Proxy = Proxy
100+ ~Proxy ~~ ~Proxy = Proxy
93101instance Additive (Proxy x)
94102
95103-- | Const lifts groups into a functor.
src/Data/Patch/Class.hs+131−5
@@ -1,23 +1,58 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE DerivingStrategies #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE MultiParamTypeClasses #-}
7+{-# LANGUAGE PolyKinds #-}
28{-# LANGUAGE ScopedTypeVariables #-}
39{-# LANGUAGE TypeFamilies #-}
10+{-# LANGUAGE TypeOperators #-}
411-- | The interface for types which represent changes made to other types
512module Data.Patch.Class where
613
14+import qualified Data.Semigroupoid as Cat
715import Data.Functor.Identity
16+import Data.Functor.Misc
817import Data.Kind (Type)
918import Data.Maybe
1019#if !MIN_VERSION_base(4,11,0)
1120import Data.Semigroup (Semigroup(..))
1221#endif
1322import Data.Proxy
23+import Data.Type.Equality ((:~:) (..))
24+
25+class PatchHet p where
26+ type PatchSource p :: Type
27+ type PatchTarget p :: Type
28+ -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
29+ -- 'Nothing'.
30+ applyHet
31+ :: p
32+ -> PatchSource p
33+ -> Either (PatchSource p :~: PatchTarget p) (PatchTarget p)
34+ default applyHet
35+ :: Patch p
36+ => p
37+ -> PatchSource p
38+ -> Either (PatchSource p :~: PatchTarget p) (PatchTarget p)
39+ applyHet p a = case apply p a of
40+ Nothing -> Left Refl
41+ Just a' -> Right a'
42+
43+-- | Apply a 'PatchHet'; if it does nothing, return the original value
44+applyAlwaysHet :: PatchHet p => p -> PatchSource p -> PatchTarget p
45+applyAlwaysHet p t = case applyHet p t of
46+ Left Refl -> t
47+ Right t' -> t'
1448
1549-- | A 'Patch' type represents a kind of change made to a datastructure.
1650--
1751-- If an instance of 'Patch' is also an instance of 'Semigroup', it should obey
1852-- the law that @applyAlways (f <> g) == applyAlways f . applyAlways g@.
19−class Patch p where
20− type PatchTarget p :: Type
53+class ( PatchHet p
54+ , PatchSource p ~ PatchTarget p
55+ ) => Patch p where
2156 -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
2257 -- 'Nothing'.
2358 apply :: p -> PatchTarget p -> Maybe (PatchTarget p)
@@ -27,19 +62,110 @@ applyAlways :: Patch p => p -> PatchTarget p -> PatchTarget p
2762applyAlways p t = fromMaybe t $ apply p t
2863
2964-- | 'Identity' can be used as a 'Patch' that always fully replaces the value
30−instance Patch (Identity a) where
65+instance PatchHet (Identity a) where
66+ type PatchSource (Identity a) = a
3167 type PatchTarget (Identity a) = a
68+instance Patch (Identity a) where
3269 apply (Identity a) _ = Just a
3370
3471-- | 'Proxy' can be used as a 'Patch' that does nothing.
35−instance forall (a :: Type). Patch (Proxy a) where
72+instance forall (a :: Type). PatchHet (Proxy a) where
73+ type PatchSource (Proxy a) = a
3674 type PatchTarget (Proxy a) = a
75+instance forall (a :: Type). Patch (Proxy a) where
3776 apply ~Proxy _ = Nothing
3877
3978-- | Like '(.)', but composes functions that return patches rather than
4079-- functions that return new values. The Semigroup instance for patches must
4180-- apply patches right-to-left, like '(.)'.
42−composePatchFunctions :: (Patch p, Semigroup p) => (PatchTarget p -> p) -> (PatchTarget p -> p) -> PatchTarget p -> p
81+composePatchFunctions
82+ :: (Patch p, Semigroup p)
83+ => (PatchTarget p -> p)
84+ -> (PatchTarget p -> p)
85+ -> PatchTarget p -> p
4386composePatchFunctions g f a =
4487 let fp = f a
4588 in g (applyAlways fp a) <> fp
89+
90+
91+class PatchHet2Base (p :: k -> k -> *) where
92+ type PatchSource1 p :: k -> *
93+ type PatchTarget1 p :: k -> *
94+
95+class ( 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+instance ( PatchHet2Base p
101+ , PatchHet (p from to)
102+ , PatchSource1 p from ~ PatchSource (p from to)
103+ , PatchTarget1 p to ~ PatchTarget (p from to)
104+ ) => PatchHet2Locally (p :: k -> k -> *) from to where
105+
106+applyHet2Locally
107+ :: PatchHet2Locally p from to
108+ => p from to
109+ -> PatchSource1 p from
110+ -> Either (PatchSource1 p from :~: PatchTarget1 p to) (PatchTarget1 p to)
111+applyHet2Locally = applyHet
112+
113+applyAlwaysHet2Locally
114+ :: PatchHet2Locally p from to
115+ => p from to
116+ -> PatchSource1 p from
117+ -> PatchTarget1 p to
118+applyAlwaysHet2Locally = applyAlwaysHet
119+
120+-- TODO once we can use quantified constraints, perhaps combine PatchHet2Base and
121+-- PatchHet2Locally, or at least get rid of this.
122+class PatchHet2Base p => PatchHet2 (p :: k -> k -> *) where
123+ applyHet2
124+ :: p from to
125+ -> PatchSource1 p from
126+ -> Either (PatchSource1 p from :~: PatchTarget1 p to) (PatchTarget1 p to)
127+
128+applyAlwaysHet2
129+ :: PatchHet2 p
130+ => p from to
131+ -> PatchSource1 p from
132+ -> PatchTarget1 p to
133+applyAlwaysHet2 p t = case applyHet2 p t of
134+ Left Refl -> t
135+ Right t' -> t'
136+
137+-- | Connect the classes without quanitified constraints
138+newtype ProjectLocal p from to = ProjectLocal { unProjectLocal :: p from to }
139+ deriving newtype Cat.Semigroupoid
140+
141+instance PatchHet2 p => PatchHet (ProjectLocal p from to) where
142+ type PatchSource (ProjectLocal p from to) = PatchSource1 p from
143+ type PatchTarget (ProjectLocal p from to) = PatchTarget1 p to
144+ applyHet (ProjectLocal p) = applyHet2 p
145+
146+instance PatchHet2 p => PatchHet2Base (ProjectLocal p) where
147+ type PatchSource1 (ProjectLocal p) = PatchSource1 p
148+ type PatchTarget1 (ProjectLocal p) = PatchTarget1 p
149+
150+class ( PatchHet2Base p
151+ , PatchSource1 p ~ PatchTarget1 p
152+ ) => Patch2 p
153+instance ( PatchHet2Base p
154+ , PatchSource1 p ~ PatchTarget1 p
155+ ) => Patch2 p
156+
157+-- | 'First2' can be used as a 'Patch' that always fully replaces the value
158+instance PatchHet (First2 (t :: k -> *) (from :: k) (to :: k)) where
159+ type PatchSource (First2 t from to) = t from
160+ type PatchTarget (First2 t from to) = t to
161+ applyHet (First2 val) _ = Right val
162+
163+-- | 'Proxy3' can be used as a 'Patch' that always does nothing
164+instance PatchHet (Proxy3 (t :: k -> *) (a :: k) (a :: k)) where
165+ type PatchSource (Proxy3 t a a) = t a
166+ type PatchTarget (Proxy3 t a a) = t a
167+ applyHet ~Proxy3 _ = Left Refl
168+
169+instance PatchHet2Base (Proxy3 (t :: k -> *) :: k -> k -> *) where
170+ type PatchSource1 (Proxy3 t) = t
171+ type PatchTarget1 (Proxy3 t) = t
src/Data/Patch/DMap.hs+3−1
@@ -42,8 +42,10 @@ instance GCompare k => DecidablyEmpty (PatchDMap k v) where
4242 isEmpty (PatchDMap m) = DMap.null m
4343
4444-- | Apply the insertions or deletions to a given 'DMap'.
45−instance GCompare k => Patch (PatchDMap k v) where
45+instance GCompare k => PatchHet (PatchDMap k v) where
46+ type PatchSource (PatchDMap k v) = DMap k v
4647 type PatchTarget (PatchDMap k v) = DMap k v
48+instance GCompare k => Patch (PatchDMap k v) where
4749 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?
4850 where insertions = DMap.mapMaybeWithKey (const $ getComposeMaybe) diff
4951 deletions = DMap.mapMaybeWithKey (const $ nothingToJust . getComposeMaybe) diff
src/Data/Patch/DMapWithMove.hs+4−1
@@ -350,8 +350,11 @@ const2PatchDMapWithMoveWith f (PatchMapWithMove p) = PatchDMapWithMove $ DMap.fr
350350 }
351351
352352-- | Apply the insertions, deletions, and moves to a given 'DMap'.
353−instance GCompare k => Patch (PatchDMapWithMove k v) where
353+instance GCompare k => PatchHet (PatchDMapWithMove k v) where
354+ type PatchSource (PatchDMapWithMove k v) = DMap k v
354355 type PatchTarget (PatchDMapWithMove k v) = DMap k v
356+
357+instance GCompare k => Patch (PatchDMapWithMove k v) where
355358 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?
356359 where insertions = DMap.mapMaybeWithKey insertFunc p
357360 insertFunc :: forall a. k a -> NodeInfo k v a -> Maybe (v a)
src/Data/Patch/DMapWithPatchingMove.hsnew file+624
@@ -0,0 +1,624 @@
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 TypeFamilies #-}
15+{-# LANGUAGE UndecidableInstances #-}
16+{-# LANGUAGE UndecidableSuperClasses #-}
17+
18+-- |Module containing @'PatchDMapWithPatchingMove' k v@ and associated functions, which represents a 'Patch' to a @'DMap' k v@ which can insert, update, delete, and
19+-- move values between keys.
20+module Data.Patch.DMapWithPatchingMove where
21+
22+import qualified Control.Category as Cat
23+--import qualified Control.Category.DecidablyEmpty as Cat
24+
25+import Data.Constraint.Extras (Has')
26+import Data.Dependent.Map (DMap)
27+import Data.Dependent.Sum (DSum (..))
28+import qualified Data.Dependent.Map as DMap
29+import Data.Functor.Constant (Constant (..))
30+import Data.Functor.Misc
31+ ( Const2 (..), Proxy3 (..)
32+ , weakenDMapWith
33+ , dmapToMapWith
34+ )
35+import Data.Functor.Const (Const (..))
36+import Data.Functor.Product (Product (..))
37+import Data.GADT.Compare (GEq (..), GCompare (..))
38+import Data.GADT.Show (GRead, GShow, gshow)
39+import qualified Data.Map as Map
40+import Data.Maybe
41+import Data.Monoid.DecidablyEmpty
42+import Data.Semigroupoid as Cat
43+#if !MIN_VERSION_base(4,11,0)
44+import Data.Semigroup (Semigroup (..), (<>))
45+#endif
46+import Data.Some (Some (Some), mkSome)
47+import Data.Proxy (Proxy (..))
48+import Data.These (These (..))
49+import Data.Type.Equality ((:~:)(..))
50+
51+import Data.Patch.Class
52+ ( Patch (..), PatchHet (..)
53+ , PatchHet2 (..), PatchSource1, PatchTarget1
54+ , applyAlwaysHet2
55+ )
56+import Data.Patch.MapWithPatchingMove (PatchMapWithPatchingMove (..))
57+import qualified Data.Patch.MapWithPatchingMove as MapWithPatchingMove
58+
59+-- | Like 'PatchMapWithPatchingMove', but for 'DMap'. Each key carries a 'NodeInfo'
60+-- which describes how it will be changed by the patch and connects move sources
61+-- and destinations.
62+--
63+-- Invariants:
64+--
65+-- * A key should not move to itself.
66+--
67+-- * A move should always be represented with both the destination key (as a
68+-- 'From_Move') and the source key (as a @'ComposeMaybe' ('Just'
69+-- destination)@)
70+newtype PatchDMapWithPatchingMove k v = PatchDMapWithPatchingMove (DMap k (NodeInfo k v))
71+
72+--deriving instance ( GShow k
73+-- , HasZip Show k p
74+-- , Has' Show k (PatchTarget1 p)
75+-- ) => Show (PatchDMapWithPatchingMove k p)
76+--deriving instance ( GRead k
77+-- , HasZip Read k p
78+-- , Has' Read k (PatchTarget1 p)
79+-- ) => Read (PatchDMapWithPatchingMove k p)
80+--deriving instance ( GEq k
81+-- , HasZip Eq k p
82+-- , Has' Eq k (PatchTarget1 p)
83+-- ) => Eq (PatchDMapWithPatchingMove k p)
84+--deriving instance ( GCompare k
85+-- , HasZip Ord k p
86+-- , Has' Ord k (PatchTarget1 p)
87+-- ) => Ord (PatchDMapWithPatchingMove k p)
88+
89+-- It won't let me derive for some reason
90+instance ( GCompare k
91+ , Cat.Semigroupoid v
92+ , PatchHet2 v
93+ , PatchSource1 v ~ PatchTarget1 v
94+ ) => DecidablyEmpty (PatchDMapWithPatchingMove k v) where
95+ isEmpty (PatchDMapWithPatchingMove m) = DMap.null m
96+
97+-- | Structure which represents what changes apply to a particular key.
98+-- @_nodeInfo_from@ specifies what happens to this key, and in particular what
99+-- other key the current key is moving from, while @_nodeInfo_to@ specifies what
100+-- key the current key is moving to if involved in a move.
101+data NodeInfo k p a = NodeInfo
102+ { _nodeInfo_from :: !(From k p a)
103+ -- ^ Change applying to the current key, be it an insert, move, or delete.
104+ , _nodeInfo_to :: !(To k)
105+ -- ^ Where this key is moving to, if involved in a move. Should only be
106+ -- @ComposeMaybe (Just k)@ when there is a corresponding 'From_Move'.
107+ }
108+
109+--deriving instance ( Show (k a)
110+-- , Show (p a a)
111+-- , Show (PatchTarget1 p a)
112+-- ) => Show (NodeInfo k p a)
113+--deriving instance ( Read (k a)
114+-- , Read (p a a)
115+-- , Read (PatchTarget1 p a)
116+-- ) => Read (NodeInfo k p a)
117+--deriving instance ( Eq (k a)
118+-- , Eq (p a a)
119+-- , Eq (PatchTarget1 p a)
120+-- ) => Eq (NodeInfo k p a)
121+--deriving instance ( Ord (k a)
122+-- , Ord (p a a)
123+-- , Ord (PatchTarget1 p a)
124+-- ) => Ord (NodeInfo k p a)
125+
126+-- | Structure describing a particular change to a key, be it inserting a new
127+-- key (@From_Insert@), updating an existing key (@From_Insert@ again), deleting
128+-- a key (@From_Delete@), or moving a key (@From_Move@).
129+--
130+-- This type isn't used directly as the from field patch, but is instead wrapped
131+-- in an existential. However, it is nice to be able to reason about this in
132+-- isolation as it is itself a @Semigroupoid@ when the underlying patch is.
133+data From (k :: a -> *) (p :: a -> a -> *) :: a -> * where
134+ -- | Insert a new or update an existing key with the given value @PatchTarget1
135+ -- p a@
136+ From_Insert :: PatchTarget1 p to -> From k p to
137+ -- | Delete the existing key
138+ From_Delete :: From k p to
139+ -- | Move the value from the given key @k a@ to this key. The source key
140+ -- should also have an entry in the patch giving the current key as
141+ -- @_nodeInfo_to@, usually but not necessarily with @From_Delete@.
142+ From_Move :: !(DSum k (Flip p to)) -> From k p to
143+
144+deriving instance ( Show (k a), GShow k
145+ , Has' Show k (Flip p a)
146+ , Show (PatchTarget1 p a)
147+ ) => Show (From k p a)
148+deriving instance ( Read (k a), GRead k
149+ , Has' Read k (Flip p a)
150+ , Read (PatchTarget1 p a)
151+ ) => Read (From k p a)
152+deriving instance ( GEq k
153+ , Has' Eq k (Flip p a)
154+ , Eq (PatchTarget1 p a)
155+ ) => Eq (From k p a)
156+deriving instance ( GCompare k
157+ , Has' Eq k (Flip p a) -- superclass bug
158+ , Has' Ord k (Flip p a)
159+ , Ord (PatchTarget1 p a)
160+ ) => Ord (From k p a)
161+
162+newtype Flip p to from = Flip (p from to)
163+
164+instance Cat.Category p => Cat.Category (Flip (p :: k -> k -> *)) where
165+ id = Flip Cat.id
166+ Flip y . Flip x = Flip $ x Cat.. y
167+
168+-- | The "to" part of a 'NodeInfo'. Rather than be built out of @From@ like @From@
169+-- is, we store just the information necessary to compose a @To@ and @From@ like
170+-- @oLocal@ composes two @From@s.
171+data To (k :: a -> *) where
172+ -- | Delete or leave in place
173+ To_NonMove :: To k
174+ -- | Move the value from the given key @k a@ to this key. The target key
175+ -- should also have an entry in the patch giving the current key in
176+ -- @_nodeInfo_from@, usually but not necessarily with @To_Delete@.
177+ To_Move :: !(Some k) -> To k
178+
179+deriving instance GShow k => Show (To k)
180+deriving instance GRead k => Read (To k)
181+deriving instance GEq k => Eq (To k)
182+deriving instance GCompare k => Ord (To k)
183+
184+-- |Test whether a 'PatchDMapWithPatchingMove' satisfies its invariants.
185+validPatchDMapWithPatchingMove
186+ :: forall k v
187+ . (GCompare k, GShow k)
188+ => DMap k (NodeInfo k v)
189+ -> Bool
190+validPatchDMapWithPatchingMove = not . null . validationErrorsForPatchDMapWithPatchingMove
191+
192+-- |Enumerate what reasons a 'PatchDMapWithPatchingMove' doesn't satisfy its invariants, returning @[]@ if it's valid.
193+validationErrorsForPatchDMapWithPatchingMove
194+ :: forall k v
195+ . (GCompare k, GShow k)
196+ => DMap k (NodeInfo k v)
197+ -> [String]
198+validationErrorsForPatchDMapWithPatchingMove m =
199+ noSelfMoves <> movesBalanced
200+ where
201+ noSelfMoves = mapMaybe selfMove . DMap.toAscList $ m
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 (Some dst)))
205+ | Just _ <- src `geq` dst = Just $ "self move of key " <> gshow dst <> " at source side"
206+ selfMove _ = Nothing
207+ movesBalanced = mapMaybe unbalancedMove . DMap.toAscList $ m
208+ unbalancedMove (dst :=> NodeInfo (From_Move (src :=> _)) _) =
209+ case DMap.lookup src m of
210+ Nothing -> Just $ "unbalanced move at destination key " <> gshow dst <> " supposedly from " <> gshow src <> " but source key is not in the patch"
211+ Just (NodeInfo _ (To_Move (Some dst'))) ->
212+ if isNothing (dst' `geq` dst)
213+ then Just $ "unbalanced move at destination key " <> gshow dst <> " from " <> gshow src <> " is going to " <> gshow dst' <> " instead"
214+ else Nothing
215+ _ ->
216+ Just $ "unbalanced move at destination key " <> gshow dst <> " supposedly from " <> gshow src <> " but source key has no move to key"
217+ unbalancedMove (src :=> NodeInfo _ (To_Move (Some dst))) =
218+ case DMap.lookup dst m of
219+ Nothing -> Just $ " unbalanced move at source key " <> gshow src <> " supposedly going to " <> gshow dst <> " but destination key is not in the patch"
220+ Just (NodeInfo (From_Move (src' :=> _)) _) ->
221+ if isNothing (src' `geq` src)
222+ then Just $ "unbalanced move at source key " <> gshow src <> " to " <> gshow dst <> " is coming from " <> gshow src' <> " instead"
223+ else Nothing
224+
225+ _ ->
226+ Just $ "unbalanced move at source key " <> gshow src <> " supposedly going to " <> gshow dst <> " but destination key is not moving"
227+ unbalancedMove _ = Nothing
228+
229+
230+data ToFrom k p a = ToFrom (To k) (From k p a)
231+
232+-- | Helper data structure used for composing patches using the monoid instance.
233+data Fixup k p
234+ = Fixup_Delete
235+ | Fixup_Update (These (DSum k (From k p)) (To k))
236+
237+-- | Compose patches having the same effect as applying the patches in turn:
238+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
239+instance forall k p
240+ . ( GCompare k
241+ , Cat.Semigroupoid p
242+ -- , Cat.DecidablyEmpty p
243+ , PatchHet2 p
244+ , PatchSource1 p ~ PatchTarget1 p
245+ )
246+ => Semigroup (PatchDMapWithPatchingMove k p) where
247+ PatchDMapWithPatchingMove ma <> PatchDMapWithPatchingMove mb = PatchDMapWithPatchingMove m
248+ where
249+ connections :: [DSum k (ToFrom k p)]
250+ connections = DMap.toList $ DMap.intersectionWithKey
251+ (\_ a b -> ToFrom (_nodeInfo_to a) (_nodeInfo_from b))
252+ ma
253+ mb
254+ h :: DSum k (ToFrom k p) -> [DSum k (Const (Fixup k p))]
255+ h ((between :: k between) :=> ToFrom editAfter editBefore) = case (editAfter, editBefore) of
256+ (To_Move (Some (toAfter :: k after)), From_Move ((fromBefore :: k before) :=> Flip p) :: From k p between) ->
257+ --case toAfter `geq` fromBefore of
258+ -- Just Refl | Just Refl <- Cat.isId p0 ->
259+ -- [ toAfter :=> Fixup_Delete ]
260+ -- _ ->
261+ [ toAfter :=> Const (Fixup_Update $ This $ between :=> From_Move (fromBefore :=> Flip p))
262+ , fromBefore :=> Const (Fixup_Update $ That $ To_Move $ Some toAfter)
263+ ]
264+ (To_NonMove, From_Move (fromBefore :=> _)) ->
265+ -- The item is destroyed in the second patch, so indicate that it is
266+ -- destroyed in the source map
267+ [fromBefore :=> Const ( Fixup_Update $ That To_NonMove)]
268+ --(To_Move (Some toAfter), From_Insert val) ->
269+ -- [toAfter :=> Fixup_Update (This $ From_Insert $ applyAlwaysHet2 p val)]
270+ --(To_Move (Some toAfter), From_Delete) ->
271+ -- [toAfter :=> Fixup_Update (This From_Delete)]
272+ (To_Move (Some toAfter), _) ->
273+ [toAfter :=> Const (Fixup_Update $ This $ between :=> editBefore)]
274+ (To_NonMove, _) ->
275+ []
276+ mergeFixups _ (Const Fixup_Delete) (Const Fixup_Delete) = Const $ Fixup_Delete
277+ mergeFixups _ (Const (Fixup_Update a)) (Const (Fixup_Update b))
278+ | This x <- a, That y <- b
279+ = Const $ Fixup_Update $ These x y
280+ | That y <- a, This x <- b
281+ = Const $ Fixup_Update $ These x y
282+ mergeFixups _ _ _ = error "PatchDMapWithPatchingMove: incompatible fixups"
283+ fixups = DMap.fromListWithKey mergeFixups $ concatMap h connections
284+ combineNodeInfos _ nia nib = NodeInfo
285+ { _nodeInfo_from = _nodeInfo_from nia
286+ , _nodeInfo_to = _nodeInfo_to nib
287+ }
288+ applyFixup :: k a -> NodeInfo k p a -> Const (Fixup k p) a -> Maybe (NodeInfo k p a)
289+ applyFixup _ ni (Const fixup) = case fixup of
290+ Fixup_Delete -> Nothing
291+ Fixup_Update u -> Just $ NodeInfo
292+ { _nodeInfo_from = case _nodeInfo_from ni of
293+ f@(From_Move ((between0 :: k between0) :=> Flip (p' :: p between0 a))) -> case getHere u of -- The `from` fixup comes from the "old" patch
294+ Nothing -> f -- If there's no `from` fixup, just use the "new" `from`
295+ Just ((between1 :: k between1) :=> frm) -> case geq between0 between1 of
296+ Nothing -> error "fixup joined-on key did not match"
297+ Just Refl -> case frm of
298+ From_Insert v -> From_Insert $ applyAlwaysHet2 p' v
299+ From_Delete -> From_Delete
300+ From_Move (oldKey :=> Flip (p :: p oldKey between1)) -> From_Move $ oldKey :=> Flip (p' `o` p :: p oldKey a)
301+ _ -> error "PatchMapWithPatchingMove: fixup for non-move From"
302+ , _nodeInfo_to = fromMaybe (_nodeInfo_to ni) $ getThere u
303+ }
304+ m = DMap.differenceWithKey applyFixup (DMap.unionWithKey combineNodeInfos ma mb) fixups
305+ getHere :: These a b -> Maybe a
306+ getHere = \case
307+ This a -> Just a
308+ These a _ -> Just a
309+ That _ -> Nothing
310+ getThere :: These a b -> Maybe b
311+ getThere = \case
312+ This _ -> Nothing
313+ These _ b -> Just b
314+ That b -> Just b
315+
316+-- | Compose patches having the same effect as applying the patches in turn:
317+-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
318+instance ( GCompare k
319+ , Cat.Semigroupoid p
320+ -- , DecidablyEmpty p
321+ , PatchHet2 p
322+ , PatchSource1 p ~ PatchTarget1 p
323+ ) => Monoid (PatchDMapWithPatchingMove k p) where
324+ mempty = PatchDMapWithPatchingMove mempty
325+ mappend = (<>)
326+
327+{-
328+mappendPatchDMapWithPatchingMoveSlow :: forall k v. (ShowTag k v, GCompare k) => PatchDMapWithPatchingMove k v -> PatchDMapWithPatchingMove k v -> PatchDMapWithPatchingMove k v
329+PatchDMapWithPatchingMove dstAfter srcAfter `mappendPatchDMapWithPatchingMoveSlow` PatchDMapWithPatchingMove dstBefore srcBefore = PatchDMapWithPatchingMove dst src
330+ where
331+ 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
332+ removeRedundantDst toKey (From_Move fromKey) | isJust (toKey `geq` fromKey) = Nothing
333+ removeRedundantDst _ a = Just a
334+ f :: forall a. k a -> From k v a -> Maybe (From k v a)
335+ f toKey _ = removeRedundantDst toKey $ case getDstAction toKey dstAfter of
336+ From_Move fromKey -> getDstAction fromKey dstBefore
337+ nonMove -> nonMove
338+ dst = DMap.mapMaybeWithKey f $ DMap.union dstAfter dstBefore
339+ getSrcAction k m = fromMaybe (ComposeMaybe $ Just k) $ DMap.lookup k m
340+ removeRedundantSrc fromKey (ComposeMaybe (Just toKey)) | isJust (fromKey `geq` toKey) = Nothing
341+ removeRedundantSrc _ a = Just a
342+ g :: forall a. k a -> ComposeMaybe k a -> Maybe (ComposeMaybe k a)
343+ g fromKey _ = removeRedundantSrc fromKey $ case getSrcAction fromKey srcBefore of
344+ ComposeMaybe Nothing -> ComposeMaybe Nothing
345+ ComposeMaybe (Just toKeyBefore) -> getSrcAction toKeyBefore srcAfter
346+ src = DMap.mapMaybeWithKey g $ DMap.union srcAfter srcBefore
347+-}
348+
349+-- | Make a @'PatchDMapWithPatchingMove' k v@ which has the effect of inserting or
350+-- updating a value @PatchTarget1 p a@ to the given key @k a@, like
351+-- 'DMap.insert'.
352+insertDMapKey :: k a -> PatchTarget1 p a -> PatchDMapWithPatchingMove k p
353+insertDMapKey k v =
354+ PatchDMapWithPatchingMove . DMap.singleton k $ NodeInfo (From_Insert v) To_NonMove
355+
356+-- | Make a @'PatchDMapWithPatchingMove' k v@ which has the effect of moving the value
357+-- from the first key @k a@ to the second key @k a@, equivalent to:
358+--
359+-- @
360+-- 'DMap.delete' src (maybe dmap ('DMap.insert' dst) (DMap.lookup src dmap))
361+-- @
362+moveDMapKey
363+ :: GCompare k
364+ => k a -> k a -> PatchDMapWithPatchingMove k (Proxy3 v)
365+moveDMapKey src dst = case src `geq` dst of
366+ Nothing -> PatchDMapWithPatchingMove $ DMap.fromList
367+ [ dst :=> NodeInfo (From_Move (src :=> Flip Proxy3)) To_NonMove
368+ , src :=> NodeInfo From_Delete (To_Move $ Some dst)
369+ ]
370+ Just _ -> PatchDMapWithPatchingMove DMap.empty
371+
372+-- | Make a @'PatchDMapWithPatchingMove' k v@ which has the effect of swapping two keys
373+-- in the mapping, equivalent to:
374+--
375+-- @
376+-- let aMay = DMap.lookup a dmap
377+-- bMay = DMap.lookup b dmap
378+-- in maybe id (DMap.insert a) (bMay <> aMay)
379+-- . maybe id (DMap.insert b) (aMay <> bMay)
380+-- . DMap.delete a . DMap.delete b $ dmap
381+-- @
382+swapDMapKey :: GCompare k => k a -> k a -> PatchDMapWithPatchingMove k (Proxy3 v)
383+swapDMapKey src dst = case src `geq` dst of
384+ Nothing -> PatchDMapWithPatchingMove $ DMap.fromList
385+ [ dst :=> NodeInfo (From_Move (src :=> Flip Proxy3)) (To_Move $ Some src)
386+ , src :=> NodeInfo (From_Move (dst :=> Flip Proxy3)) (To_Move $ Some dst)
387+ ]
388+ Just _ -> PatchDMapWithPatchingMove DMap.empty
389+
390+-- | Make a @'PatchDMapWithPatchingMove' k v@ which has the effect of deleting a key in
391+-- the mapping, equivalent to 'DMap.delete'.
392+deleteDMapKey :: k a -> PatchDMapWithPatchingMove k v
393+deleteDMapKey k = PatchDMapWithPatchingMove $ DMap.singleton k $ NodeInfo From_Delete To_NonMove
394+
395+{-
396+k1, k2 :: Const2 Int () ()
397+k1 = Const2 1
398+k2 = Const2 2
399+p1, p2 :: PatchDMapWithPatchingMove (Const2 Int ()) Identity
400+p1 = moveDMapKey k1 k2
401+p2 = moveDMapKey k2 k1
402+p12 = p1 <> p2
403+p21 = p2 <> p1
404+p12Slow = p1 `mappendPatchDMapWithPatchingMoveSlow` p2
405+p21Slow = p2 `mappendPatchDMapWithPatchingMoveSlow` p1
406+
407+testPatchDMapWithPatchingMove = do
408+ print p1
409+ print p2
410+ print $ p12 == deleteDMapKey k1
411+ print $ p21 == deleteDMapKey k2
412+ print $ p12Slow == deleteDMapKey k1
413+ print $ p21Slow == deleteDMapKey k2
414+
415+dst (PatchDMapWithPatchingMove x _) = x
416+src (PatchDMapWithPatchingMove _ x) = x
417+-}
418+
419+-- | Extract the 'DMap' representing the patch changes from the
420+-- 'PatchDMapWithPatchingMove'.
421+unPatchDMapWithPatchingMove :: PatchDMapWithPatchingMove k v -> DMap k (NodeInfo k v)
422+unPatchDMapWithPatchingMove (PatchDMapWithPatchingMove p) = p
423+
424+-- | Wrap a 'DMap' representing patch changes into a 'PatchDMapWithPatchingMove',
425+-- without checking any invariants.
426+--
427+-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchDMapWithPatchingMove' are preserved; they will not be checked.
428+unsafePatchDMapWithPatchingMove :: DMap k (NodeInfo k v) -> PatchDMapWithPatchingMove k v
429+unsafePatchDMapWithPatchingMove = PatchDMapWithPatchingMove
430+
431+-- | Wrap a 'DMap' representing patch changes into a 'PatchDMapWithPatchingMove' while
432+-- checking invariants. If the invariants are satisfied, @Right p@ is returned
433+-- otherwise @Left errors@.
434+patchDMapWithPatchingMove :: (GCompare k, GShow k) => DMap k (NodeInfo k v) -> Either [String] (PatchDMapWithPatchingMove k v)
435+patchDMapWithPatchingMove dm =
436+ case validationErrorsForPatchDMapWithPatchingMove dm of
437+ [] -> Right $ unsafePatchDMapWithPatchingMove dm
438+ errs -> Left errs
439+
440+-- | Map a natural transform @v -> v'@ over the given patch, transforming
441+-- @'PatchDMapWithPatchingMove' k v@ into @'PatchDMapWithPatchingMove' k v'@.
442+mapPatchDMapWithPatchingMove
443+ :: forall k p p'
444+ . (forall a. PatchTarget1 p a -> PatchTarget1 p' a)
445+ -> (forall from to. p from to -> p' from to)
446+ -> PatchDMapWithPatchingMove k p
447+ -> PatchDMapWithPatchingMove k p'
448+mapPatchDMapWithPatchingMove f g (PatchDMapWithPatchingMove m) =
449+ PatchDMapWithPatchingMove $ DMap.map (\ni -> NodeInfo
450+ { _nodeInfo_from = h $ _nodeInfo_from ni
451+ , _nodeInfo_to = _nodeInfo_to ni
452+ }) m
453+ where h :: forall a. From k p a -> From k p' a
454+ h = \case
455+ From_Insert v -> From_Insert $ f v
456+ From_Delete -> From_Delete
457+ From_Move (k :=> Flip p) -> From_Move $ k :=> Flip (g p)
458+
459+-- | Traverse an effectful function @forall a. PatchTarget1 p a -> m (v ' a)@
460+-- over the given patch, transforming @'PatchDMapWithPatchingMove' k v@ into @m
461+-- ('PatchDMapWithPatchingMove' k v')@.
462+traversePatchDMapWithPatchingMove
463+ :: forall m k p p'
464+ . Applicative m
465+ => (forall a. PatchTarget1 p a -> m (PatchTarget1 p' a))
466+ -> (forall from to. p from to -> m (p' from to))
467+ -> PatchDMapWithPatchingMove k p
468+ -> m (PatchDMapWithPatchingMove k p')
469+traversePatchDMapWithPatchingMove f g = traversePatchDMapWithPatchingMoveWithKey
470+ (\_ -> f)
471+ (\_ _ -> g)
472+
473+-- | Map an effectful function @forall a. k a -> PatchTarget1 p a -> m (v ' a)@
474+-- over the given patch, transforming @'PatchDMapWithPatchingMove' k v@ into @m
475+-- ('PatchDMapWithPatchingMove' k v')@.
476+traversePatchDMapWithPatchingMoveWithKey
477+ :: forall m k p p'
478+ . Applicative m
479+ => (forall a. k a -> PatchTarget1 p a -> m (PatchTarget1 p' a))
480+ -> (forall from to. k from -> k to -> p from to -> m (p' from to))
481+ -> PatchDMapWithPatchingMove k p
482+ -> m (PatchDMapWithPatchingMove k p')
483+traversePatchDMapWithPatchingMoveWithKey f g (PatchDMapWithPatchingMove m) =
484+ PatchDMapWithPatchingMove <$> DMap.traverseWithKey (\k ni -> NodeInfo
485+ <$> h k (_nodeInfo_from ni)
486+ <*> pure (_nodeInfo_to ni)) m
487+ where h :: forall a. k a -> From k p a -> m (From k p' a)
488+ h k = \case
489+ From_Insert v -> From_Insert <$> f k v
490+ From_Delete -> pure From_Delete
491+ From_Move (fromKey :=> Flip p) -> From_Move . (fromKey :=>) . Flip <$> g fromKey k p
492+
493+-- | Map a function which transforms @'From k PatchTarget1 p a@ into a @'From k
494+-- PatchTarget1 p' a@ over a @'NodeInfo' k PatchTarget1 p a@.
495+nodeInfoMapFrom
496+ :: (From k p a -> From k p' a)
497+ -> (To k -> To k)
498+ -> NodeInfo k p a
499+ -> NodeInfo k p' a
500+nodeInfoMapFrom f g ni = NodeInfo
501+ { _nodeInfo_from = f $ _nodeInfo_from ni
502+ , _nodeInfo_to = g $ _nodeInfo_to ni
503+ }
504+
505+-- | Map an effectful function which transforms @'From k PatchTarget1 p a@ into
506+-- a @f ('From k PatchTarget1 p' a)@ over a @'NodeInfo' k PatchTarget1 p a@.
507+nodeInfoMapFromM
508+ :: Applicative f
509+ => (From k p a -> f (From k p' a))
510+ -> (To k -> f (To k))
511+ -> NodeInfo k p a
512+ -> f (NodeInfo k p' a)
513+nodeInfoMapFromM f g ni = NodeInfo
514+ <$> f (_nodeInfo_from ni)
515+ <*> g (_nodeInfo_to ni)
516+
517+-- | Weaken a 'PatchDMapWithPatchingMove' to a 'PatchMapWithPatchingMove' by weakening the keys
518+-- from @k a@ to @'Some' k@ and applying a given weakening function
519+-- @PatchTarget1 p a -> v'@ to values.
520+weakenPatchDMapWithPatchingMoveWith
521+ :: forall k p p'
522+ . (forall a. PatchTarget1 p a -> PatchTarget p')
523+ -> (forall from to. p from to -> p')
524+ -> PatchDMapWithPatchingMove k p
525+ -> PatchMapWithPatchingMove (Some k) p'
526+weakenPatchDMapWithPatchingMoveWith f g (PatchDMapWithPatchingMove m) =
527+ PatchMapWithPatchingMove $ weakenDMapWith h m
528+ where h :: forall a. NodeInfo k p a -> MapWithPatchingMove.NodeInfo (Some k) p'
529+ h ni = MapWithPatchingMove.NodeInfo
530+ { MapWithPatchingMove._nodeInfo_from = case _nodeInfo_from ni of
531+ From_Insert v -> MapWithPatchingMove.From_Insert $ f v
532+ From_Delete -> MapWithPatchingMove.From_Delete
533+ From_Move (k :=> Flip p) -> MapWithPatchingMove.From_Move (mkSome k) $ g p
534+ , MapWithPatchingMove._nodeInfo_to = case _nodeInfo_to ni of
535+ To_NonMove -> Nothing
536+ To_Move (Some k) -> Just (mkSome k)
537+ }
538+
539+-- | "Weaken" a @'PatchDMapWithPatchingMove' (Const2 k a) v@ to a @'PatchMapWithPatchingMove' k
540+-- v'@. Weaken is in scare quotes because the 'Const2' has already disabled any
541+-- dependency in the typing and all points are already @a@, hence the function
542+-- to map each value to @v'@ is not higher rank.
543+patchDMapWithPatchingMoveToPatchMapWithPatchingMoveWith
544+ :: forall k p p' a
545+ . (PatchTarget1 p a -> PatchTarget p')
546+ -> (p a a -> p')
547+ -> PatchDMapWithPatchingMove (Const2 k a) p
548+ -> PatchMapWithPatchingMove k p'
549+patchDMapWithPatchingMoveToPatchMapWithPatchingMoveWith f g (PatchDMapWithPatchingMove m) =
550+ PatchMapWithPatchingMove $ dmapToMapWith h m
551+ where h :: NodeInfo (Const2 k a) p a -> MapWithPatchingMove.NodeInfo k p'
552+ h ni = MapWithPatchingMove.NodeInfo
553+ { MapWithPatchingMove._nodeInfo_from = case _nodeInfo_from ni of
554+ From_Insert v -> MapWithPatchingMove.From_Insert $ f v
555+ From_Delete -> MapWithPatchingMove.From_Delete
556+ From_Move (Const2 k :=> Flip p) -> MapWithPatchingMove.From_Move k $ g p
557+ , MapWithPatchingMove._nodeInfo_to = case _nodeInfo_to ni of
558+ To_NonMove -> Nothing
559+ To_Move (Some (Const2 k)) -> Just k
560+ }
561+
562+-- | "Strengthen" a @'PatchMapWithPatchingMove' k v@ into a @'PatchDMapWithPatchingMove
563+-- ('Const2' k a)@; that is, turn a non-dependently-typed patch into a
564+-- dependently typed one but which always has a constant key type represented by
565+-- 'Const2'. Apply the given function to each @v@ to produce a @PatchTarget1 p'
566+-- a@. Completemented by 'patchDMapWithPatchingMoveToPatchMapWithPatchingMoveWith'
567+const2PatchDMapWithPatchingMoveWith
568+ :: forall k v v' a
569+ . (v -> v' a)
570+ -> PatchMapWithPatchingMove k (Proxy v)
571+ -> PatchDMapWithPatchingMove (Const2 k a) (Proxy3 v')
572+const2PatchDMapWithPatchingMoveWith f (PatchMapWithPatchingMove p) =
573+ PatchDMapWithPatchingMove $ DMap.fromDistinctAscList $ g <$> Map.toAscList p
574+ where g :: (k, MapWithPatchingMove.NodeInfo k (Proxy v))
575+ -> DSum (Const2 k a) (NodeInfo (Const2 k a) (Proxy3 v'))
576+ g (k, ni) = Const2 k :=> NodeInfo
577+ { _nodeInfo_from = case MapWithPatchingMove._nodeInfo_from ni of
578+ MapWithPatchingMove.From_Insert v -> From_Insert $ f v
579+ MapWithPatchingMove.From_Delete -> From_Delete
580+ MapWithPatchingMove.From_Move fromKey Proxy -> From_Move $ Const2 fromKey :=> Flip Proxy3
581+ , _nodeInfo_to = case MapWithPatchingMove._nodeInfo_to ni of
582+ Nothing -> To_NonMove
583+ Just toKey -> To_Move $ Some (Const2 toKey)
584+ }
585+
586+-- | Apply the insertions, deletions, and moves to a given 'DMap'.
587+instance ( GCompare k
588+ , PatchHet2 p
589+ , PatchSource1 p ~ PatchTarget1 p
590+ ) => PatchHet (PatchDMapWithPatchingMove k p) where
591+ type PatchSource (PatchDMapWithPatchingMove k p) = DMap k (PatchSource1 p)
592+ type PatchTarget (PatchDMapWithPatchingMove k p) = DMap k (PatchTarget1 p)
593+
594+instance ( GCompare k
595+ , PatchHet2 p
596+ , PatchSource1 p ~ PatchTarget1 p
597+ ) => Patch (PatchDMapWithPatchingMove k p) where
598+ apply (PatchDMapWithPatchingMove m) old = Just $! insertions `DMap.union` (old `DMap.difference` deletions)
599+ -- TODO: return Nothing sometimes --Note: the strict application here is
600+ -- critical to ensuring that incremental merges don't hold onto all their
601+ -- prerequisite events forever; can we make this more robust?
602+ where insertions = DMap.mapMaybeWithKey insertFunc m
603+ insertFunc :: forall a. k a -> NodeInfo k p a -> Maybe (PatchTarget1 p a)
604+ insertFunc _ ni = case _nodeInfo_from ni of
605+ From_Insert v -> Just v
606+ From_Move (k :=> Flip p) -> applyAlwaysHet2 p <$> DMap.lookup k old
607+ From_Delete -> Nothing
608+ deletions = DMap.mapMaybeWithKey deleteFunc m
609+ deleteFunc :: forall a. k a -> NodeInfo k p a -> Maybe (Constant () a)
610+ deleteFunc _ ni = case _nodeInfo_from ni of
611+ From_Delete -> Just $ Constant ()
612+ _ -> Nothing
613+
614+-- | Get the values that will be replaced, deleted, or moved if the given patch
615+-- is applied to the given 'DMap'.
616+getDeletionsAndMoves
617+ :: ( GCompare k
618+ , PatchSource1 p ~ PatchTarget1 p
619+ )
620+ => PatchDMapWithPatchingMove k p
621+ -> DMap k (PatchSource1 p)
622+ -> DMap k (Product (PatchTarget1 p) (Const (To k)))
623+getDeletionsAndMoves (PatchDMapWithPatchingMove p) m = DMap.intersectionWithKey f m p
624+ where f _ v ni = Pair v $ Const $ _nodeInfo_to ni
src/Data/Patch/DMapWithPatchingMove/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.DMapWithPatchingMove.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
@@ -38,8 +38,10 @@ deriving instance Semigroup (PatchIntMap v)
3838makeWrapped ''PatchIntMap
3939
4040-- | Apply the insertions or deletions to a given 'IntMap'.
41−instance Patch (PatchIntMap a) where
41+instance PatchHet (PatchIntMap a) where
42+ type PatchSource (PatchIntMap a) = IntMap a
4243 type PatchTarget (PatchIntMap a) = IntMap a
44+instance Patch (PatchIntMap a) where
4345 apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
4446 let removes = IntMap.filter isNothing p
4547 adds = IntMap.mapMaybe id p
src/Data/Patch/Map.hs+3−1
@@ -45,8 +45,10 @@ instance Ord k => Semigroup (PatchMap k v) where
4545 stimes = stimesIdempotentMonoid
4646
4747-- | Apply the insertions or deletions to a given 'Map'.
48−instance Ord k => Patch (PatchMap k v) where
48+instance Ord k => PatchHet (PatchMap k v) where
49+ type PatchSource (PatchMap k v) = Map k v
4950 type PatchTarget (PatchMap k v) = Map k v
51+instance Ord k => Patch (PatchMap k v) where
5052 {-# INLINABLE apply #-}
5153 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?
5254 where insertions = Map.mapMaybeWithKey (const id) p
src/Data/Patch/MapWithMove.hs+4−1
@@ -178,8 +178,11 @@ unsafePatchMapWithMove :: Map k (NodeInfo k v) -> PatchMapWithMove k v
178178unsafePatchMapWithMove = PatchMapWithMove' . PM.unsafePatchMapWithPatchingMove . coerce
179179
180180-- | Apply the insertions, deletions, and moves to a given 'Map'
181−instance Ord k => Patch (PatchMapWithMove k v) where
181+instance Ord k => PatchHet (PatchMapWithMove k v) where
182+ type PatchSource (PatchMapWithMove k v) = Map k v
182183 type PatchTarget (PatchMapWithMove k v) = Map k v
184+
185+instance Ord k => Patch (PatchMapWithMove k v) where
183186 apply (PatchMapWithMove' p) = apply p
184187
185188-- | Returns all the new elements that will be added to the 'Map'.
src/Data/Patch/MapWithPatchingMove.hs+4−1
@@ -170,8 +170,11 @@ unsafePatchMapWithPatchingMove
170170unsafePatchMapWithPatchingMove = PatchMapWithPatchingMove
171171
172172-- | Apply the insertions, deletions, and moves to a given 'Map'
173−instance (Ord k, Patch p) => Patch (PatchMapWithPatchingMove k p) where
173+instance (Ord k, Patch p) => PatchHet (PatchMapWithPatchingMove k p) where
174+ type PatchSource (PatchMapWithPatchingMove k p) = Map k (PatchSource p)
174175 type PatchTarget (PatchMapWithPatchingMove k p) = Map k (PatchTarget p)
176+
177+instance (Ord k, Patch p) => Patch (PatchMapWithPatchingMove k p) where
175178 -- TODO: return Nothing sometimes
176179 -- Note: the strict application here is critical to ensuring that incremental
177180 -- merges don't hold onto all their prerequisite events forever; can we make