Skip to content

Commit a7b9f43

Browse files
committed
Tweak names more, add patch instance, and haddock
1 parent b74bdc7 commit a7b9f43

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Data/Patch/PatchOrReplacement.hs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE FlexibleInstances #-}
55
{-# LANGUAGE LambdaCase #-}
66
{-# LANGUAGE StandaloneDeriving #-}
7+
{-# LANGUAGE TypeFamilies #-}
78
{-# LANGUAGE UndecidableInstances #-}
89

910
module Data.Patch.PatchOrReplacement where
@@ -14,10 +15,16 @@ import Data.Semigroup (Semigroup (..))
1415
#endif
1516
import GHC.Generics
1617

17-
-- | Like SemiMap/PartialMap but for anything patchable
18+
-- | Either a patch or a replacement value.
19+
--
20+
-- A good patch type will describe small changes very efficiently, but
21+
-- that often comes at the cost of describing large change rather
22+
-- inefficiently. 'PatchOrReplacement' can be used as an escape hatch:
23+
-- when the change as a patch would be too big, just provide a new value
24+
-- to replace the old one with instead.
1825
data PatchOrReplacement p
1926
= PatchOrReplacement_Patch p
20-
| PatchOrReplacement_Complete (PatchTarget p)
27+
| PatchOrReplacement_Replacement (PatchTarget p)
2128
deriving (Generic)
2229

2330
deriving instance (Eq p, Eq (PatchTarget p)) => Eq (PatchOrReplacement p)
@@ -27,9 +34,17 @@ deriving instance (Read p, Read (PatchTarget p)) => Read (PatchOrReplacement p)
2734

2835
completePatchOrReplacement :: PatchOrReplacement p -> Maybe (PatchTarget p)
2936
completePatchOrReplacement = \case
30-
PatchOrReplacement_Complete t -> Just t
37+
PatchOrReplacement_Replacement t -> Just t
3138
PatchOrReplacement_Patch _ -> Nothing
3239

40+
-- | 'PatchOrReplacement p' is a patch when we can apply the patch or
41+
-- replace the old value with the new replacement value.
42+
instance Patch p => Patch (PatchOrReplacement p) where
43+
type PatchTarget (PatchOrReplacement p) = PatchTarget p
44+
apply = \case
45+
PatchOrReplacement_Patch p -> apply p
46+
PatchOrReplacement_Replacement v -> \_ -> Just v
47+
3348
instance ( Monoid p
3449
#if !MIN_VERSION_base(4,11,0)
3550
, Semigroup p
@@ -42,5 +57,5 @@ instance ( Monoid p
4257
instance (Semigroup p, Patch p) => Semigroup (PatchOrReplacement p) where
4358
(<>) = curry $ \case
4459
(PatchOrReplacement_Patch a, PatchOrReplacement_Patch b) -> PatchOrReplacement_Patch $ a <> b
45-
(PatchOrReplacement_Patch a, PatchOrReplacement_Complete b) -> PatchOrReplacement_Complete $ applyAlways a b
46-
(PatchOrReplacement_Complete a, _) -> PatchOrReplacement_Complete a
60+
(PatchOrReplacement_Patch a, PatchOrReplacement_Replacement b) -> PatchOrReplacement_Replacement $ applyAlways a b
61+
(PatchOrReplacement_Replacement a, _) -> PatchOrReplacement_Replacement a

0 commit comments

Comments
 (0)