on 5bb4430 help sign in
patch/src/Reflex/Patch/Class.hs
22 lines 808 B history raw
1{-# LANGUAGE TypeFamilies #-}
2-- | The interface for types which represent changes made to other types
3module Reflex.Patch.Class where
5import Control.Monad.Identity
6import Data.Maybe
8-- | A 'Patch' type represents a kind of change made to a datastructure.
9class Patch p where
10 type PatchTarget p :: *
11 -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
12 -- 'Nothing'.
13 apply :: p -> PatchTarget p -> Maybe (PatchTarget p)
15-- | Apply a 'Patch'; if it does nothing, return the original value
16applyAlways :: Patch p => p -> PatchTarget p -> PatchTarget p
17applyAlways p t = fromMaybe t $ apply p t
19-- | 'Identity' can be used as a 'Patch' that always fully replaces the value
20instance Patch (Identity a) where
21 type PatchTarget (Identity a) = a
22 apply (Identity a) _ = Just a