Data structures for describing changes to other data structures.
Go to file
2021-12-17 17:52:08 -05:00
.github/workflows ci: Try to fix cabal test invocation; remove travis 2021-11-14 22:16:11 -05:00
bench-cbits Earlier 'base' versions for CrossImpl and earlier RTS compatability for checkCapability. 2018-10-17 16:25:23 -04:00
dep/reflex-platform Fix release.nix so TH doesn't break 2020-01-17 13:34:33 -05:00
src/Data Merge branch 'module-for-additive' into proper-orphans-dep 2021-11-17 00:58:43 -05:00
test Add hunit and hegdehog test suite with roundtrip test for patchThatChangesMap 2021-04-11 20:10:39 +02:00
.ghci Remove some old cruft from the .ghci file 2016-05-27 23:36:50 -04:00
.gitignore Merge remote-tracking branch 'origin/wip-optimizer' into develop 2016-11-09 11:39:46 -05:00
.stylish-haskell.yaml Run stylish-haskell 2018-07-13 23:12:07 -04:00
cabal.project Fix lint failures; Remove overridden dependent-map 2019-08-05 00:04:17 -04:00
ChangeLog.md Fix merge, the top one header should be "unreleased" 2021-12-17 17:52:08 -05:00
CONTRIBUTING.md Remove patch-platform 2020-06-19 13:05:12 +02:00
LICENSE Change license to BSD3 and prepare cabal file for preliminary release 2015-03-13 21:05:19 -04:00
patch.cabal Merge remote-tracking branch 'origin/release/0.0.5.0' into develop 2021-12-17 17:51:08 -05:00
README.md Get rid of backticks on title in README 2020-06-15 18:03:49 -04:00
release.nix Fix release.nix so TH doesn't break 2020-01-17 13:34:33 -05:00
Setup.hs Fix cabal file warnings 2015-03-13 21:46:26 -04:00
stylize Add stylize script 2018-07-13 23:26:39 -04:00

patch

Haskell Hackage Hackage CI Travis CI BSD3 License

Data structures for describing changes to other data structures.

A Patch type represents a kind of change made to a data structure.

class Patch p where
  type PatchTarget p :: *
  -- | Apply the patch p a to the value a.  If no change is needed, return
  -- 'Nothing'.
  apply :: p -> PatchTarget p -> Maybe (PatchTarget p)

Patching Maps

For example, Data.Patch.Map defines the PatchMap type which can be used to patch Maps. A PatchMap represents updates to a Map that can insert, remove, or replace items in the Map. In this example, the Map is the PatchTarget and the PatchMap is the Patch. Keep in mind that there are many other possible Patches that can be applied to a Map (i.e., Map can be the PatchTarget for many different Patch instances).

PatchMap is defined as:

newtype PatchMap k v = PatchMap { unPatchMap :: Map k (Maybe v) }

The Maybe around the value is used to represent insertion/updates or deletion of the element at a given key.

Its Patch instance begins with:

instance Ord k => Patch (PatchMap k v) where
  type PatchTarget (PatchMap k v) = Map k v
  ...

When a PatchMap is applied to its PatchTarget, the following changes can occur:

  • If the key is present in the Patch and the PatchTarget...

    • And the Patch value at that key is Nothing: delete that key from the PatchTarget.

    • And the Patch value at that key is Just x: update the value at that key in the PatchTarget to x.

  • If the key is present in the Patch and not present in the PatchTarget...

    • And the Patch value at that key is Nothing: do nothing because we're trying to delete a key that doesn't exist in the target in the first place.

    • And the Patch value at that key is Just x: insert the key and the value x into the PatchTarget

  • If the key is not present in the Patch but present in the PatchTarget: do nothing.

There are, of course, more complicated ways of patching maps involving, for example, moving values from one key to another. You can find the code for that in Data.Patch.PatchMapWithMove. Note that the PatchTarget type associated with the PatchMapWithMove patch instance is still Map k v!