type-level-numbers-0.1.1.1: Type level numbers implemented using type families.

Portabilityunportable (GHC only)
Stabilityunstable
MaintainerAlexey Khudyakov <alexey.skladnoy@gmail.com>
Safe HaskellSafe-Inferred

TypeLevel.Number.Classes

Contents

Description

This module contain interface type classes for operations with type level numbers.

Synopsis

Comparison of numbers

type family Compare n m :: *Source

Type family for comparing two numbers. It's expected that for any two valid n and m 'Compare n m' is equal to IsLess when 'n<m', IsEqual when 'n=m' and IsGreater when 'n>m'.

compareN :: n -> m -> Compare n mSource

Data labels for types comparison

data IsLesser Source

Instances

data IsEqual Source

Instances

Specialized type classes

These type classes are meant to be used in contexts to ensure relations between numbers. For example:

 someFunction :: Lesser n m => Data n -> Data m -> Data n
 someFunction = ...

They have generic instances and every number which is instance of Compare type family is instance of these type classes.

These instance could have problems. They weren't exensively tested. Also error messages are really unhelpful.

class Lesser n m Source

Numbers n and m are instances of this class if and only is n < m.

Instances

~ * (Compare n m) IsLesser => Lesser n m 

class LesserEq n m Source

Numbers n and m are instances of this class if and only is n <= m.

Instances

OneOfTwo (Compare n m) IsLesser IsEqual => LesserEq n m 

class Greater n m Source

Numbers n and m are instances of this class if and only is n > m.

Instances

~ * (Compare n m) IsGreater => Greater n m 

class GreaterEq n m Source

Numbers n and m are instances of this class if and only is n >= m.

Instances

OneOfTwo (Compare n m) IsGreater IsEqual => GreaterEq n m 

Special traits

class Positive n Source

Positive number.

Instances

Nat (O n) => Positive (O n) 
Nat (I n) => Positive (I n) 

class NonZero n Source

Non-zero number. For naturals it's same as positive

Instances

Nat (O n) => NonZero (O n) 
Nat (I n) => NonZero (I n) 

Arithmetic operations on numbers

type family Next n :: *Source

Next number.

nextN :: n -> Next nSource

type family Prev n :: *Source

Previous number

prevN :: n -> Prev nSource

type family Negate n :: *Source

Negate number.

type family Add n m :: *Source

Sum of two numbers.

addN :: n -> m -> Add n mSource

type family Sub n m :: *Source

Difference of two numbers.

subN :: n -> m -> Sub n mSource

type family Mul n m :: *Source

Product of two numbers.

mulN :: n -> m -> Mul n mSource

type family Div n m :: *Source

Division of two numbers. n and m should be instances of this class only if remainder of 'n/m' is zero.

divN :: n -> m -> Div n mSource

Special classes

type family Normalized n :: *Source

Usually numbers have non-unique representation. This type family is canonical representation of number.