merge: A functor for consistent merging of information

[ data, library, mit ] [ Propose Tags ]

A functor for consistent merging of information.

[Skip to Readme]


[Index] [Quick Jump]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],,,,
Change log
Dependencies base (>=4.6 && <5), profunctors (>=5.6 && <6) [details]
License MIT
Copyright 2020 Samuel Schlesinger
Author Samuel Schlesinger
Category Data
Source repo head: git clone
Uploaded by sgschlesinger at 2021-09-19T00:55:18Z
Distributions NixOS:
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 661 total (14 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2021-09-19 [all 1 reports]

Readme for merge-

[back to package description]


Often, one finds themselves having multiple sources of knowledge for some piece of data, and having to merge these together. Perhaps we have a type representing partial information about a digital friend.

data Friend = Friend
  { name :: Maybe Text
  , email :: Maybe Text
  , age :: Max Int
  , pubKey :: PublicKey

If we learn some information about a friend from someone, and some from someone else, we'll want to merge that information to have a more complete picture. That said, it might not succeed, as we may have inconsistent information like two different names or different public keys. We'll want a function of type:

f :: Friend -> Friend -> Maybe Friend

That's the pattern that this library encapsulates!

mergeFriends :: Merge [String] Friend Friend
mergeFriends =
    <$> optional name   .? ["name"]
    <*> optional email  .? ["email"]
    <*> combine age     .? ["age"]
    <*> required pubKey .? ["pubKey"]

f :: Friend -> Friend -> Validation [String] Friend
f x y = runMerge mergeFriends x y

We didn't get exactly what we thought we wanted, but this Validation type is better: it is an Applicative which accumulates the errors for every field.