collect-errors: Error monad with a Float instance

[ bsd3, library, math ] [ Propose Tags ]

Versions [RSS] [faq] 0.1.0.0, 0.1.1.0, 0.1.2.0, 0.1.3.0, 0.1.4.0, 0.1.4.1, 0.1.5.0 ChangeLog.md base (>=4.7 && <5), containers, QuickCheck (>=2.7) [details] BSD-3-Clause 2021 Michal Konecny Michal Konecny mikkonecny@gmail.com Math https://github.com/michalkonecny/collect-errors#readme https://github.com/michalkonecny/collect-errors/issues head: git clone https://github.com/michalkonecny/collect-errors by MichalKonecny at 2021-05-08T08:47:56Z LTSHaskell:0.1.5.0, NixOS:0.1.5.0, Stackage:0.1.5.0 680 total (40 in the last 30 days) (no votes yet) [estimated by Bayesian average] λ λ λ Docs available Last success reported on 2021-05-08

Maintainer's Corner

For package maintainers and hackage trustees

Candidates

• No Candidates

[back to package description]

collect-errors

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))
NaN

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.