The explainable-predicates
package provides a Predicate a
type, which is esssntially "a -> Bool
that can explain itself." The package provides a quite comprehensive library of 52 combinators for building these explainable predicates, which can accept or reject values, describe themselves, and explain why a specific value does or doesn’t match the predicate. For example, if the predicate requires that a container has specific values in it, then the explanation will tell you which value was missing.
The intended use case is for unit testing, where explanations provide delightfully helpful messages when a test fails. For example, I very much hope that some day, HSpec or a successor library will contain a shouldSatisfy
function to assert that a value matches a Predicate
and explain why not. However, it would be very interesting to hear if someone finds other uses for the package, as well!
This was all previously available as part of my HMock library, but I’ve separated it out now so that it can be used separate from HMock.