collect-errors: Error monad with a Float instance

[ bsd3, library, math ] [ Propose Tags ]

Please see the README on GitHub at

[Skip to Readme]
Versions [RSS] [faq],,,,,,
Change log
Dependencies base (>=4.7 && <5), containers, deepseq, QuickCheck (>=2.7) [details]
License BSD-3-Clause
Copyright 2021 Michal Konecny
Author Michal Konecny
Category Math
Home page
Bug tracker
Source repo head: git clone
Uploaded by MichalKonecny at 2021-05-12T01:53:07Z
Distributions LTSHaskell:, NixOS:, Stackage:
Downloads 710 total (10 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2021-05-12 [all 1 reports]


[Index] [Quick Jump]


Maintainer's Corner

For package maintainers and hackage trustees


Readme for collect-errors-

[back to package description]


CollectErrors es t is a monad wrapper around values of type t which can accommodate (a list of) (potential) errors of type es that have (maybe) occurred during the computation of a value. A value may be missing, leaving only the error(s).

The wrapper CN t is a special case of CollectErrors es t with es = NumErrors.

The CN wrapper also propagates instances of Floating, allowing us to write expressions with partial functions (ie functions that fail for some inputs) instead of branching after each application of such function:

*Numeric.CollectErrors> a = 1 :: CN Double
*Numeric.CollectErrors> (1/(a-1))+(sqrt (a-2))
{{ERROR: division by 0; ERROR: out of domain: sqrt for negative arg -1.0}}

as opposed to:

*Prelude> a = 1 :: Double
*Prelude> (1/(a-1))+(sqrt (a-2))

Dealing with the errors can be moved outside the expression:

*Numeric.CollectErrors> a = 1 :: CN Double
*Numeric.CollectErrors> toEither $ 1/(a-1)
Left {ERROR: division by 0}

*Numeric.CollectErrors> toEither $ 1/a+(sqrt a)
Right 2.0

The CN wrapper has support for potential errors so that it can be applied to a set arithmetic such as interval arithmetic.

The Floating instance cannot be used with a set arithmetic since the instance relies on true/false comparisons but a set arithmetic has only three-valued (true/false/undecided) comparisons. Package mixed-types-num provides alternative numerical type classes in which three-valued (ie Kleenean) comparisons are available.