numeric-prelude: An experimental alternative hierarchy of numeric type classes

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.


The package provides an experimental alternative hierarchy of numeric type classes. The type classes are more oriented at mathematical structures and their methods come with laws that the instances must fulfill.

[Skip to ReadMe]


Versions0.0.2, 0.0.4, 0.0.5, 0.1, 0.1.1, 0.1.2, 0.1.3,,,,, 0.2, 0.2.1, 0.2.2,, 0.3,,, 0.4,,,, 0.4.1, 0.4.2, 0.4.3,,
Change logNone available
Dependenciesarray (>=0.1 && <0.6), base (>=4.5 && <5), containers (>=0.1 && <0.7), deepseq (>=1.1 && <1.5), gnuplot (==0.5.*), HTam (>=0.0.2 && <0.1), non-negative (>=0.0.5 && <0.2), numeric-prelude, parsec (>=1 && <4), QuickCheck (>=1 && <3), random (>=1.0 && <1.2), semigroups (>=0.1 && <1.0), storable-record (>=0.0.1 && <0.1), utility-ht (>=0.0.6 && <0.1) [details]
AuthorDylan Thurston <>, Henning Thielemann <>, Mikael Johansson
MaintainerHenning Thielemann <>
Home page
Source repositorythis: darcs clone --tag
head: darcs clone
Executablesnumeric-prelude-gaussian, numeric-prelude-demo
UploadedSat Sep 1 10:24:43 UTC 2018 by HenningThielemann





Build example executables


Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Maintainers' corner

For package maintainers and hackage trustees

Readme for numeric-prelude-

[back to package description]

Revisiting the Numeric Classes


The Prelude for Haskell 98 offers a well-considered set of numeric classes which covers the standard numeric types (Integer, Int, Rational, Float, Double, Complex) quite well. But they offer limited extensibility and have a few other flaws. In this proposal we will revisit these classes, addressing the following concerns:

  1. The current Prelude defines no semantics for the fundamental operations. For instance, presumably addition should be associative (or come as close as feasible), but this is not mentioned anywhere.

  2. There are some superfluous superclasses. For instance, Eq and Show are superclasses of Num. Consider the data type data IntegerFunction a = IF (a -> Integer). One can reasonably define all the methods of Algebra.Ring.C for IntegerFunction a (satisfying good semantics), but it is impossible to define non-bottom instances of Eq and Show. In general, superclass relationship should indicate some semantic connection between the two classes.

  3. In a few cases, there is a mix of semantic operations and representation-specific operations. toInteger, toRational, and the various operations in RealFloating (decodeFloat, ...) are the main examples.

  4. In some cases, the hierarchy is not finely-grained enough: Operations that are often defined independently are lumped together. For instance, in a financial application one might want a type "Dollar", or in a graphics application one might want a type "Vector". It is reasonable to add two Vectors or Dollars, but not, in general, reasonable to multiply them. But the programmer is currently forced to define a method for (*) when she defines a method for (+).

In specifying the semantics of type classes, I will state laws as follows:

    (a + b) + c === a + (b + c)

The intended meaning is extensional equality: The rest of the program should behave in the same way if one side is replaced with the other. Unfortunately, the laws are frequently violated by standard instances; the law above, for instance, fails for Float:

    (1e20 + (-1e20)) + 1.0  = 1.0
     1e20 + ((-1e20) + 1.0) = 0.0

For inexact number types like floating point types, thus these laws should be interpreted as guidelines rather than absolute rules. In particular, the compiler is not allowed to use them for optimization. Unless stated otherwise, default definitions should also be taken as laws.

Thanks to Brian Boutel, Joe English, William Lee Irwin II, Marcin Kowalczyk, Ketil Malde, Tom Schrijvers, Ken Shan, and Henning Thielemann for helpful comments.


Write modules in the following style:

    {-# LANGUAGE RebindableSyntax #-}
    module MyModule where

    ... various specific imports ...

    import NumericPrelude

Importing NumericPrelude is almost the same as

    import NumericPrelude.Numeric
    import NumericPrelude.Base   .

Instead of the NoImplicitPrelude pragma you could also write import Prelude () but this will yield problems with numeric literals.

There are two wrapper types that allow types to be used with both Haskell98 and NumericPrelude type classes that are initially implemented for only one of them.

Scope & Limitations/TODO

Additional standard libraries might include Enum, IEEEFloat (including the bulk of the functions in Haskell 98's RealFloat class), VectorSpace, Ratio, and Lattice.