mixed-types-num: Alternative Prelude with numeric and logic expressions typed bottom-up

[ bsd3, library, math ] [ Propose Tags ]

Main purpose

This package provides a version of Prelude where unary and binary operations such as not, +, == have their result type derived from the parameter type(s), allowing, e.g.:

let n = 1 :: Integer in n/(n+1) :: Rational
1/2 :: Rational

(The type Rational would be derived automatically because integer literals are always of type Integer, not Num t => t.)

(length [x])+1/3 :: Rational
2^2 :: Integer
2.0^(-2) :: Rational
(double 2)^(1/2) :: Double

The following examples require package aern2-real:

2^(1/2) :: CauchyReal
pi :: CauchyReal
sqrt 2 :: CauchyReal
... x :: CauchyReal ... if (isCertainlyTrue (x > 1)) then ...

Type classes

Arithmetic operations are provided via multi-parameter type classes and the result type is given by associated type families. For example:

(+) :: (CanAddAsymmetric t1 t2) => t1 -> t2 -> AddType t1 t2

The type constraint CanAdd t1 t2 implies both CanAddAsymmetric t1 t2 and CanAddAsymmetric t2 t1.

For convenience there are other aggregate type constraints such as CanAddThis t1 t2, which implies that the result is of type t1, and CanAddSameType t, which is a shortcut for CanAddThis t t.

Testable specification

The arithmetic type classes are accompanied by generic hspec test suites, which are specialised to concrete instance types for their testing. These test suites include the expected algebraic properties of operations, such as commutativity and associativity of addition.


f: (CanMulAsymmetric t1 t2, CanAddAsymmetric t4 (MulType t1 t2),
CanAddAsymmetric (AddType t4 (MulType t1 t2)) Integer,
CanSqrt (AddType (AddType t4 (MulType t1 t2)) Integer)) =>
-> t1
-> t2
-> SqrtType (AddType (AddType t4 (MulType t1 t2)) Integer)

Further reading

To find out more, please read the documentation for the modules in the order specified in Numeric.MixedTypes.


The idea of having numeric expressions in Haskell with types derived bottom-up was initially suggested and implemented by Pieter Collins. This version is a fresh rewrite by Michal Konečný.

Versions [faq],, 0.2,, 0.3,, 0.3.1,,,,,, 0.3.2, 0.4.0, (info)
Dependencies base (==4.*), convertible (>= && <1.2), hspec (>=2.1 && <2.5), hspec-smallcheck (>=0.3 && <0.5), QuickCheck (>=2.7 && <2.10), smallcheck (==1.1.*) [details]
License BSD-3-Clause
Copyright (c) 2015-2017 Michal Konecny
Author Michal Konecny
Maintainer Michal Konecny <mikkonecny@gmail.com>
Category Math
Home page https://github.com/michalkonecny/mixed-types-num
Source repo head: git clone https://github.com/mikkonecny/mixed-types-num
Uploaded by MichalKonecny at Wed Mar 8 23:10:17 UTC 2017
Distributions LTSHaskell:, NixOS:, Stackage:
Downloads 3049 total (257 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2017-03-08 [all 1 reports]




Maintainer's Corner

For package maintainers and hackage trustees