1{-# LANGUAGE TypeFamilies #-}
2-- | The interface for types which represent changes made to other types
3module Data.Patch.Class where
5import Data.Functor.Identity
7import Data.Semigroup (Semigroup(..))
10-- | A 'Patch' type represents a kind of change made to a datastructure.
12-- If an instance of 'Patch' is also an instance of 'Semigroup', it should obey
13-- the law that @applyAlways (f <> g) == applyAlways f . applyAlways g@.
15 type PatchTarget p :: *
16 -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
18 apply :: p -> PatchTarget p -> Maybe (PatchTarget p)
20-- | Apply a 'Patch'; if it does nothing, return the original value
21applyAlways :: Patch p => p -> PatchTarget p -> PatchTarget p
22applyAlways p t = fromMaybe t $ apply p t
24-- | 'Identity' can be used as a 'Patch' that always fully replaces the value
25instance Patch (Identity a) where
26 type PatchTarget (Identity a) = a
27 apply (Identity a) _ = Just a
29-- | 'Proxy' can be used as a 'Patch' that does nothing.
30instance Patch (Proxy a) where
31 type PatchTarget (Proxy a) = a
32 apply ~Proxy _ = Nothing
34-- | Like '(.)', but composes functions that return patches rather than
35-- functions that return new values. The Semigroup instance for patches must
36-- apply patches right-to-left, like '(.)'.
37composePatchFunctions :: (Patch p, Semigroup p) => (PatchTarget p -> p) -> (PatchTarget p -> p) -> PatchTarget p -> p
38composePatchFunctions g f a =
40 in g (applyAlways fp a) <> fp
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list