{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Patch.Class where
import Data.Functor.Identity
import Data.Maybe
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup(..))
#endif
import Data.Proxy
class Patch p where
type PatchTarget p :: *
apply :: p -> PatchTarget p -> Maybe (PatchTarget p)
applyAlways :: Patch p => p -> PatchTarget p -> PatchTarget p
applyAlways p t = fromMaybe t $ apply p t
instance Patch (Identity a) where
type PatchTarget (Identity a) = a
apply (Identity a) _ = Just a
instance Patch (Proxy a) where
type PatchTarget (Proxy a) = a
apply ~Proxy _ = Nothing
composePatchFunctions :: (Patch p, Semigroup p) => (PatchTarget p -> p) -> (PatchTarget p -> p) -> PatchTarget p -> p
composePatchFunctions g f a =
let fp = f a
in g (applyAlways fp a) <> fp