@@ -0,0 +1,46 @@
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE LambdaCase #-}
6+{-# LANGUAGE StandaloneDeriving #-}
7+{-# LANGUAGE UndecidableInstances #-}
9+module Data.Patch.Patchable where
12+#if !MIN_VERSION_base(4,11,0)
13+import Data.Semigroup (Semigroup (..))
17+-- | Like SemiMap/PartialMap but for anything patchable
20+ | Patchable_Complete (PatchTarget p)
23+deriving instance (Eq p, Eq (PatchTarget p)) => Eq (Patchable p)
24+deriving instance (Ord p, Ord (PatchTarget p)) => Ord (Patchable p)
25+deriving instance (Show p, Show (PatchTarget p)) => Show (Patchable p)
26+deriving instance (Read p, Read (PatchTarget p)) => Read (Patchable p)
28+completePatchable :: Patchable p -> Maybe (PatchTarget p)
29+completePatchable = \case
30+ Patchable_Complete t -> Just t
31+ Patchable_Patch _ -> Nothing
34+#if !MIN_VERSION_base(4,11,0)
38+ ) => Monoid (Patchable p) where
39+ mempty = Patchable_Patch mempty
42+instance (Semigroup p, Patch p) => Semigroup (Patchable p) where
44+ (Patchable_Patch a, Patchable_Patch b) -> Patchable_Patch $ a <> b
45+ (Patchable_Patch a, Patchable_Complete b) -> Patchable_Complete $ applyAlways a b
46+ (Patchable_Complete a, _) -> Patchable_Complete a