Copyright | Alexey Khudyakov |
---|---|
License | BSD3-style (see LICENSE) |
Maintainer | Alexey Khudyakov <alexey.skladnoy@gmail.com> |
Stability | unstable |
Portability | unportable (GHC only) |
Safe Haskell | Safe-Inferred |
Language | Haskell98 |
This module contain interface type classes for operations with type level numbers.
- type family Compare n m :: *
- compareN :: n -> m -> Compare n m
- data IsLesser
- data IsEqual
- data IsGreater
- class Lesser n m
- class LesserEq n m
- class Greater n m
- class GreaterEq n m
- class Positive n
- class NonZero n
- type family Next n :: *
- nextN :: n -> Next n
- type family Prev n :: *
- prevN :: n -> Prev n
- type family Negate n :: *
- negateN :: n -> Negate n
- type family Add n m :: *
- addN :: n -> m -> Add n m
- type family Sub n m :: *
- subN :: n -> m -> Sub n m
- type family Mul n m :: *
- mulN :: n -> m -> Mul n m
- type family Div n m :: *
- divN :: n -> m -> Div n m
- type family Normalized n :: *
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'.
Data labels for types comparison
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.
Numbers n and m are instances of this class if and only is n < m.
Numbers n and m are instances of this class if and only is n <= m.
Numbers n and m are instances of this class if and only is n > m.
Numbers n and m are instances of this class if and only is n >= m.
Special traits
Non-zero number. For naturals it's same as positive
Arithmetic operations on numbers
type family Add n m :: * Source
Sum of two numbers.
type Add ZZ ZZ = ZZ | |
type Add Z Z | |
type Add ZZ (D1 n) = Normalized (D1 n) | |
type Add ZZ (D0 n) = Normalized (D0 n) | |
type Add ZZ (Dn n) = Normalized (Dn n) | |
type Add Z (O n) | |
type Add Z (I n) | |
type Add (D1 n) ZZ = Normalized (D1 n) | |
type Add (D0 n) ZZ = Normalized (D0 n) | |
type Add (Dn n) ZZ = Normalized (Dn n) | |
type Add (O n) Z | |
type Add (I n) Z | |
type Add (D1 n) (D1 m) | |
type Add (D1 n) (D0 m) | |
type Add (D1 n) (Dn m) | |
type Add (D0 n) (D1 m) | |
type Add (D0 n) (D0 m) | |
type Add (D0 n) (Dn m) | |
type Add (Dn n) (D1 m) | |
type Add (Dn n) (D0 m) | |
type Add (Dn n) (Dn m) | |
type Add (O n) (I m) | |
type Add (O n) (O m) | |
type Add (I n) (I m) | |
type Add (I n) (O m) |
type family Sub n m :: * Source
Difference of two numbers.
type Sub ZZ ZZ = ZZ | |
type Sub Z Z | |
type Sub ZZ (D1 n) = Negate (D1 n) | |
type Sub ZZ (D0 n) = Negate (D0 n) | |
type Sub ZZ (Dn n) = Negate (Dn n) | |
type Sub (D1 n) ZZ = D1 n | |
type Sub (D0 n) ZZ = D0 n | |
type Sub (Dn n) ZZ = Dn n | |
type Sub (O n) Z | |
type Sub (I n) Z | |
type Sub (D1 n) (D1 m) = Add (D1 n) (Negate (D1 m)) | |
type Sub (D1 n) (D0 m) = Add (D1 n) (Negate (D0 m)) | |
type Sub (D1 n) (Dn m) = Add (D1 n) (Negate (Dn m)) | |
type Sub (D0 n) (D1 m) = Add (D0 n) (Negate (D1 m)) | |
type Sub (D0 n) (D0 m) = Add (D0 n) (Negate (D0 m)) | |
type Sub (D0 n) (Dn m) = Add (D0 n) (Negate (Dn m)) | |
type Sub (Dn n) (D1 m) = Add (Dn n) (Negate (D1 m)) | |
type Sub (Dn n) (D0 m) = Add (Dn n) (Negate (D0 m)) | |
type Sub (Dn n) (Dn m) = Add (Dn n) (Negate (Dn m)) | |
type Sub (O n) (I m) | |
type Sub (O n) (O m) | |
type Sub (I n) (I m) | |
type Sub (I n) (O m) |
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.
Special classes
type family Normalized n :: * Source
Usually numbers have non-unique representation. This type family is canonical representation of number.
type Normalized ZZ = ZZ | |
type Normalized Z = Z | |
type Normalized (D1 n) = D1 (Normalized n) | |
type Normalized (D0 n) | |
type Normalized (Dn n) = Dn (Normalized n) | |
type Normalized (O n) | |
type Normalized (I n) = I (Normalized n) |