Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
This module exposes the inner workings of type-level arithmetic for further extensions.
Synopsis
- type family AddK k1 k2 where ...
- type family SubK k1 k2 where ...
- type family MulK k1 k2 where ...
- type family IntDivK k where ...
- type family ExpK k1 k2 where ...
- type family NegK k where ...
- type family Abs (x :: k) :: k where ...
- type family Negate (x :: k) :: NegK k where ...
- type family Recip (x :: k) :: Rat where ...
- type family Simplify (x :: Rat) :: Rat where ...
- type family Truncate (x :: k) :: TInt where ...
- type family Floor (x :: k) :: TInt where ...
- type family Ceiling (x :: k) :: TInt where ...
- type family Add (x :: k1) (y :: k2) :: AddK k1 k2 where ...
- type family Sub (x :: k1) (y :: k2) :: SubK k1 k2 where ...
- type family Mul (x :: k1) (y :: k2) :: MulK k1 k2 where ...
- type family RatDiv (x :: k1) (y :: k2) :: Rat where ...
- type family DivMod (x :: k) (y :: Nat) :: (IntDivK k, IntDivK k) where ...
- type family QuotRem (x :: k) (y :: Nat) :: (IntDivK k, IntDivK k) where ...
- type family Div (x :: k) (y :: Nat) :: IntDivK k where ...
- type family Mod (x :: k) (y :: Nat) :: IntDivK k where ...
- type family Quot (x :: k) (y :: Nat) :: IntDivK k where ...
- type family Rem (x :: k) (y :: Nat) :: IntDivK k where ...
- type family GCD (x :: k1) (y :: k2) :: Nat where ...
- type family LCM (x :: k1) (y :: k2) :: Nat where ...
- type family Exp (x :: k1) (y :: k2) :: ExpK k1 k2 where ...
- type family IntLog (n :: Nat) (x :: k) :: TInt where ...
Kind Results
type family IntDivK k where ... Source #
The kind of the result of division by a natural number
Since: 0.1.4
Unary operations
type family Abs (x :: k) :: k where ... Source #
The absolute value of a type-level number
Since: 0.1.4
type family Recip (x :: k) :: Rat where ... Source #
The reciprocal of a type-level number
Since: 0.1.4
type family Simplify (x :: Rat) :: Rat where ... Source #
Reduce a type-level rational into its canonical form
Since: 0.1.4
Rounding operations
type family Truncate (x :: k) :: TInt where ... Source #
Round a type-level number towards zero
Since: 0.1.4
type family Floor (x :: k) :: TInt where ... Source #
Round a type-level number towards negative infinity
Since: 0.1.4
type family Ceiling (x :: k) :: TInt where ... Source #
Round a type-level number towards positive infinity
Since: 0.1.4
Binary operations
type family Add (x :: k1) (y :: k2) :: AddK k1 k2 where ... Source #
The sum of two type-level numbers.
Since: 0.1.2
Add (x :: Nat) (y :: Nat) = x + y | |
Add ('Pos x) ('Pos y) = 'Pos (x + y) | |
Add ('Neg x) ('Pos y) = If (x <=? y) ('Pos (y - x)) ('Neg (x - y)) | |
Add ('Pos x) ('Neg y) = If (y <=? x) ('Pos (x - y)) ('Neg (y - x)) | |
Add ('Neg x) ('Neg y) = 'Neg (x + y) | |
Add ('Pos x) (y :: Nat) = 'Pos (x + y) | |
Add ('Neg x) (y :: Nat) = If (x <=? y) ('Pos (y - x)) ('Neg (x - y)) | |
Add (x :: Nat) ('Pos y) = 'Pos (x + y) | |
Add (x :: Nat) ('Neg y) = If (y <=? x) ('Pos (x - y)) ('Neg (y - x)) | |
Add (x :: Rat) (y :: Rat) = Simplify (AddRat x y) | |
Add (x :: Rat) y = Simplify (AddRat x (y :% 1)) | |
Add x (y :: Rat) = Simplify (AddRat (x :% 1) y) |
type family Sub (x :: k1) (y :: k2) :: SubK k1 k2 where ... Source #
The difference of two type-level numbers
For the difference of two naturals a
and b
, a-b
is also a natural,
so only exists for a
>= b
.
@since 0.1.2
Sub (x :: Nat) (y :: Nat) = x - y | |
Sub ('Pos x) ('Pos y) = Add x ('Neg y) | |
Sub ('Neg x) ('Pos y) = Add ('Neg x) ('Neg y) | |
Sub ('Pos x) ('Neg y) = Add ('Pos x) ('Pos y) | |
Sub ('Neg x) ('Neg y) = Add ('Neg x) ('Pos y) | |
Sub ('Pos x) (y :: Nat) = Add ('Pos x) ('Neg y) | |
Sub ('Neg x) (y :: Nat) = Add ('Neg x) ('Neg y) | |
Sub (x :: Nat) ('Pos y) = Add x ('Neg y) | |
Sub (x :: Nat) ('Neg y) = Add x ('Pos y) | |
Sub (x :: Rat) (y :: Rat) = Simplify (SubRat x y) | |
Sub (x :: Rat) y = Simplify (SubRat x (y :% 1)) | |
Sub x (y :: Rat) = Simplify (SubRat (x :% 1) y) |
type family Mul (x :: k1) (y :: k2) :: MulK k1 k2 where ... Source #
The product of two type-level numbers
Since: 0.1.2
Mul (x :: Nat) (y :: Nat) = x * y | |
Mul ('Pos x) ('Pos y) = 'Pos (x * y) | |
Mul ('Neg x) ('Pos y) = 'Neg (x * y) | |
Mul ('Pos x) ('Neg y) = 'Neg (x * y) | |
Mul ('Neg x) ('Neg y) = 'Pos (x * y) | |
Mul ('Pos x) (y :: Nat) = 'Pos (x * y) | |
Mul ('Neg x) (y :: Nat) = 'Neg (x * y) | |
Mul (x :: Nat) ('Pos y) = 'Pos (x * y) | |
Mul (x :: Nat) ('Neg y) = 'Neg (x * y) | |
Mul (x :: Rat) (y :: Rat) = Simplify (MulRat x y) | |
Mul (x :: Rat) y = Simplify (MulRat x (y :% 1)) | |
Mul x (y :: Rat) = Simplify (MulRat (x :% 1) y) |
type family RatDiv (x :: k1) (y :: k2) :: Rat where ... Source #
The result of dividing two type-level numbers.
Since: 0.1.4
RatDiv (x :: Nat) (y :: Nat) = Simplify (x :% y) | |
RatDiv (x :: TInt) (y :: Nat) = Simplify (x :% y) | |
RatDiv (x :: Nat) ('Pos y) = Simplify (x :% y) | |
RatDiv (x :: Nat) ('Neg y) = Simplify ('Neg x :% y) | |
RatDiv (x :: TInt) ('Neg y) = Simplify (Negate x :% y) | |
RatDiv (x :: TInt) ('Pos y) = Simplify (x :% y) | |
RatDiv (x :: Rat) (y :: Rat) = Mul x (Recip y) | |
RatDiv x (y :: Rat) = Mul x (Recip y) | |
RatDiv (x :: Rat) y = Mul x (RatDiv 1 y) |
type family DivMod (x :: k) (y :: Nat) :: (IntDivK k, IntDivK k) where ... Source #
The quotient and remainder of a type-level integer and a natural number. For a negative dividend, the remainder part is positive such that x = q*y + r @since 0.1.4
type family QuotRem (x :: k) (y :: Nat) :: (IntDivK k, IntDivK k) where ... Source #
The quotient and remainder of a type-level integer and a natural number. For a negative dividend, the remainder part is negative such that x = q*y + r @since 0.1.4
type family Div (x :: k) (y :: Nat) :: IntDivK k where ... infixl 7 Source #
The quotient of a type-level integer and a natural number.
Since: 0.1.4
type family Mod (x :: k) (y :: Nat) :: IntDivK k where ... infixl 7 Source #
The remainder of a type-level integer and a natural number
For a negative number, behaves similarly to mod
.
@since 0.1.4
type family Quot (x :: k) (y :: Nat) :: IntDivK k where ... infixl 7 Source #
The integer part of the result of dividing an integer by a natural number
Since: 0.1.4
type family Rem (x :: k) (y :: Nat) :: IntDivK k where ... infixl 7 Source #
The remainder of the result of dividing an integer by a natural number
Since: 0.1.4
type family GCD (x :: k1) (y :: k2) :: Nat where ... Source #
The greatest common divisor of two type-level integers
Since: 0.1.4
type family LCM (x :: k1) (y :: k2) :: Nat where ... Source #
The least common multiple of two type-level integers
Since: 0.1.4
type family Exp (x :: k1) (y :: k2) :: ExpK k1 k2 where ... Source #
Exponentiation of a type-level number by an integer
Since: 0.1.4
Exp (x :: Nat) (y :: Nat) = x ^ y | |
Exp ('Pos x) (y :: Nat) = 'Pos (x ^ y) | |
Exp ('Neg x) (y :: Nat) = ExpAux ('Pos 1) ('Neg x) y | |
Exp (x :: Rat) (y :: Nat) = Simplify (ExpAux (1 :% 1) x y) | |
Exp (x :: Rat) ('Pos y) = Simplify (ExpAux (1 :% 1) x y) | |
Exp (x :: Rat) ('Neg y) = Simplify (Recip (ExpAux (1 :% 1) x y)) | |
Exp x ('Pos y) = Simplify (ExpAux (1 :% 1) (x :% 1) y) | |
Exp x ('Neg y) = Simplify (Recip (ExpAux (1 :% 1) (x :% 1) y)) |
type family IntLog (n :: Nat) (x :: k) :: TInt where ... Source #
The floor of the logarithm of a type-level number
NB. unlike Log2
, Log n 0
here is a type error.
Since: 0.1.4
IntLog 0 _ = TypeError ('Text "Invalid IntLog base: 0") | |
IntLog 1 _ = TypeError ('Text "Invalid IntLog base: 1") | |
IntLog _ 0 = TypeError ('Text "IntLog n 0 is infinite") | |
IntLog _ ('Pos 0) = TypeError ('Text "IntLog n 0 is infinite") | |
IntLog _ ('Neg 0) = TypeError ('Text "IntLog n 0 is infinite") | |
IntLog _ (0 :% _) = TypeError ('Text "IntLog n 0 is infinite") | |
IntLog _ (_ :% 0) = TypeError ('Text "IntLog parameter has zero denominator") | |
IntLog _ ('Neg _) = TypeError ('Text "IntLog of a negative does not exist") | |
IntLog n (x :: Nat) = IntLog n ('Pos x) | |
IntLog n ('Pos x) = IntLogAux n ('Pos x) | |
IntLog n (x :: Rat) = If (Floor x == 'Pos 0) (NegLogFudge n x (Negate (IntLogAux n (Floor (Recip x))))) (IntLog n (Floor x)) |