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

[ bsd3, library, math ] [ Propose Tags ] [ Report a vulnerability ]

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]

Modules

[Index] [Quick Jump]

Flags

Automatic Flags
NameDescriptionDefault
buildexamples

Build example executables

Disabled

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

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.0.2, 0.0.4, 0.0.5, 0.1, 0.1.1, 0.1.2, 0.1.3, 0.1.3.1, 0.1.3.2, 0.1.3.3, 0.1.3.4, 0.2, 0.2.1, 0.2.2, 0.2.2.1, 0.3, 0.3.0.1, 0.3.0.2, 0.4, 0.4.0.1, 0.4.0.2, 0.4.0.3, 0.4.1, 0.4.2, 0.4.3, 0.4.3.1, 0.4.3.2, 0.4.3.3, 0.4.4
Dependencies array (>=0.1 && <0.6), base (>=4.5 && <5), containers (>=0.1 && <0.8), deepseq (>=1.1 && <1.6), gnuplot (>=0.5 && <0.6), HTam (>=0.0.2 && <0.2), non-negative (>=0.0.5 && <0.2), numeric-prelude, parsec (>=1 && <4), QuickCheck (>=2.10 && <3), random (>=1.0 && <1.3), semigroups (>=0.1 && <1.0), storable-record (>=0.0.1 && <0.1), utility-ht (>=0.0.13 && <0.1) [details]
Tested with ghc ==7.4.2, ghc ==7.6.3, ghc ==7.8.4, ghc ==7.10.3, ghc ==8.4.4, ghc ==8.6.5, ghc ==9.0.1
License BSD-3-Clause
Author Dylan Thurston <dpt@math.harvard.edu>, Henning Thielemann <numericprelude@henning-thielemann.de>, Mikael Johansson
Maintainer Henning Thielemann <numericprelude@henning-thielemann.de>
Revised Revision 2 made by HenningThielemann at 2023-12-22T22:32:38Z
Category Math
Home page http://www.haskell.org/haskellwiki/Numeric_Prelude
Source repo this: darcs get https://hub.darcs.net/thielema/numeric-prelude/ --tag 0.4.4
head: darcs get https://hub.darcs.net/thielema/numeric-prelude/
Uploaded by HenningThielemann at 2022-05-28T21:21:57Z
Distributions LTSHaskell:0.4.4, NixOS:0.4.4, Stackage:0.4.4
Reverse Dependencies 32 direct, 22 indirect [details]
Executables numeric-prelude-gaussian, numeric-prelude-demo
Downloads 24435 total (65 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
All reported builds failed as of 2022-05-28 [all 1 reports]

Readme for numeric-prelude-0.4.4

[back to package description]

Revisiting the Numeric Classes

Introduction

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.

Usage

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

  • It might be desireable to split Ord up into Poset and Ord (a total ordering). This is not addressed here.

  • In some cases, this hierarchy may not yet be fine-grained enough. For instance, time spans ("5 minutes") can be added to times ("12:34"), but two times are not addable. ("12:34 + 8:23") As it stands, users have to use a different operator for adding time spans to times than for adding two time spans. Similar issues arise for vector space et al. This is a consciously-made tradeoff, but might be changed. This becomes most serious when dealing with quantities with units like length/distance^2, for which (*) as defined here is useless. (One way to see the issue: should f x y = iterate (x *) y have principal type (Ring.C a) => a -> a -> [a] or something like (Ring.C a, Module a b) => a -> b -> [b] ?)

  • I stuck with the Haskell 98 names. In some cases I find them lacking. Neglecting backwards compatibility, we have renamed classes as follows:

    Num           --> Additive, Ring, Absolute
    Integral      --> ToInteger, IntegralDomain, RealIntegral
    Fractional    --> Field
    Floating      --> Algebraic, Transcendental
    Real          --> ToRational
    RealFrac      --> RealRing, RealField
    RealFloat     --> RealTranscendental
    

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