Copyright | (c) Artem Chirkin |
---|---|
License | BSD3 |
Maintainer | [email protected] |
Safe Haskell | None |
Language | Haskell2010 |
Numeric.Dim
Contents
Description
This module is based on TypeLits
and re-exports its functionality.
It provides KnownDim
class that is similar to KnownNat
, but keeps
Int
s instead of Integer
s;
Also it provides Dim
data family serving as a customized Proxy
type
and a singleton suitable for recovering an instance of the KnownDim
class.
A set of utility functions provide inference functionality, so
that KnownDim
can be preserved over some type-level operations.
- data XNat
- type XN (n :: Nat) = XN n
- type N (n :: Nat) = N n
- data XNatType :: XNat -> Type where
- data Dim (x :: k) where
- pattern Dim :: forall (n :: k). KnownDim n => Dim n
- pattern D :: forall (n :: Nat). KnownDim n => Dim n
- pattern Dn :: forall (xn :: XNat). KnownXNatType xn => forall (n :: Nat). (KnownDim n, xn ~ N n) => Dim n -> Dim xn
- pattern Dx :: forall (xn :: XNat). KnownXNatType xn => forall (n :: Nat) (m :: Nat). (KnownDim n, MinDim m n, xn ~ XN m) => Dim n -> Dim xn
- type SomeDim = Dim (XN 0)
- class KnownDim (n :: k) where
- class KnownXNatType (n :: XNat) where
- dimVal :: Dim (x :: k) -> Word
- dimVal' :: forall n. KnownDim n => Word
- someDimVal :: Word -> SomeDim
- sameDim :: forall (x :: Nat) (y :: Nat). Dim x -> Dim y -> Maybe (Evidence (x ~ y))
- sameDim' :: forall (x :: Nat) (y :: Nat) p q. (KnownDim x, KnownDim y) => p x -> q y -> Maybe (Evidence (x ~ y))
- compareDim :: Dim a -> Dim b -> Ordering
- compareDim' :: forall a b p q. (KnownDim a, KnownDim b) => p a -> q b -> Ordering
- constrain :: forall (m :: Nat) x. KnownDim m => Dim x -> Maybe (Dim (XN m))
- constrainBy :: forall m x. Dim m -> Dim x -> Maybe (Dim (XN m))
- relax :: forall (m :: Nat) (n :: Nat). MinDim m n => Dim (XN n) -> Dim (XN m)
- plusDim :: Dim n -> Dim m -> Dim (n + m)
- minusDim :: MinDim m n => Dim n -> Dim m -> Dim (n - m)
- minusDimM :: Dim n -> Dim m -> Maybe (Dim (n - m))
- timesDim :: Dim n -> Dim m -> Dim (* n m)
- powerDim :: Dim n -> Dim m -> Dim ((^) n m)
- data Nat :: *
- type family CmpNat (a :: Nat) (b :: Nat) :: Ordering where ...
- type family (a :: Nat) + (b :: Nat) :: Nat where ...
- type family (a :: Nat) - (b :: Nat) :: Nat where ...
- type family (a :: Nat) * (b :: Nat) :: Nat where ...
- type family (a :: Nat) ^ (b :: Nat) :: Nat where ...
- type family MinDim (m :: Nat) (n :: Nat) :: Constraint where ...
- type family FixedDim (x :: XNat) (n :: Nat) :: Constraint where ...
- inferDimLE :: forall m n. MinDim m n => Evidence (m <= n)
- class KnownDimKind k where
- data DimKind :: Type -> Type where
Type level numbers that can be unknown.
Either known or unknown at compile-time natural number
type XN (n :: Nat) = XN n Source #
Unknown natural number, known to be not smaller than the given Nat
Term level dimension
data Dim (x :: k) where Source #
Singleton type to store type-level dimension value.
On the one hand, it can be used to let type-inference system know
relations between type-level naturals.
On the other hand, this is just a newtype wrapper on the Word
type.
Usually, the type parameter of Dim
is either Nat
or XNat
.
If dimensionality of your data is known in advance, use Nat
;
if you know the size of some dimensions, but do not know the size
of others, use XNat
s to represent them.
Bundled Patterns
pattern Dim :: forall (n :: k). KnownDim n => Dim n | Independently of the kind of type-level number,
construct an instance of Match against this pattern to bring |
pattern D :: forall (n :: Nat). KnownDim n => Dim n | Same as |
pattern Dn :: forall (xn :: XNat). KnownXNatType xn => forall (n :: Nat). (KnownDim n, xn ~ N n) => Dim n -> Dim xn | Statically known |
pattern Dx :: forall (xn :: XNat). KnownXNatType xn => forall (n :: Nat) (m :: Nat). (KnownDim n, MinDim m n, xn ~ XN m) => Dim n -> Dim xn |
|
class KnownDim (n :: k) where Source #
This class provides the Dim
associated with a type-level natural.
Minimal complete definition
Methods
Get value of type-level dim at runtime.
Note, this function is supposed to be used with TypeApplications
,
and the KnownDim
class has varying kind of the parameter;
thus, the function has two type paremeters (kind and type of n
).
For example, you can type:
>>>
:set -XTypeApplications
>>>
:set -XDataKinds
>>>
:t dim @Nat @3
dim @Nat @3 :: Dim 3
>>>
:set -XTypeOperators
>>>
:t dim @_ @(13 - 6)
dim @_ @(13 - 6) :: Dim 7
>>>
:t dim @_ @(N 17)
dim @_ @(N 17) :: Dim (N 17)
Instances
KnownNat n => KnownDim Nat n Source # | |
KnownDim Nat 0 Source # | |
KnownDim Nat 1 Source # | |
KnownDim Nat 2 Source # | |
KnownDim Nat 3 Source # | |
KnownDim Nat 4 Source # | |
KnownDim Nat 5 Source # | |
KnownDim Nat 6 Source # | |
KnownDim Nat 7 Source # | |
KnownDim Nat 8 Source # | |
KnownDim Nat 9 Source # | |
KnownDim Nat 10 Source # | |
KnownDim Nat 11 Source # | |
KnownDim Nat 12 Source # | |
KnownDim Nat 13 Source # | |
KnownDim Nat 14 Source # | |
KnownDim Nat 15 Source # | |
KnownDim Nat 16 Source # | |
KnownDim Nat 17 Source # | |
KnownDim Nat 18 Source # | |
KnownDim Nat 19 Source # | |
KnownDim Nat 20 Source # | |
KnownDim Nat n => KnownDim XNat (N n) Source # | |
class KnownXNatType (n :: XNat) where Source #
Find out the type of XNat
constructor
Minimal complete definition
Methods
xNatType :: XNatType n Source #
Pattern-match against this to out the type of XNat
constructor
Instances
KnownXNatType (XN n) Source # | |
KnownXNatType (N n) Source # | |
someDimVal :: Word -> SomeDim Source #
Similar to someNatVal
from TypeLits
.
sameDim :: forall (x :: Nat) (y :: Nat). Dim x -> Dim y -> Maybe (Evidence (x ~ y)) Source #
We either get evidence that this function was instantiated with the same type-level numbers, or Nothing.
Note, this function works on Nat
-indexed dimensions only,
because Dim (XN x)
does not have runtime evidence to infer x
and `KnownDim x` does not imply `KnownDim (XN x)`.
sameDim' :: forall (x :: Nat) (y :: Nat) p q. (KnownDim x, KnownDim y) => p x -> q y -> Maybe (Evidence (x ~ y)) Source #
We either get evidence that this function was instantiated with the same type-level numbers, or Nothing.
compareDim' :: forall a b p q. (KnownDim a, KnownDim b) => p a -> q b -> Ordering Source #
Ordering of dimension values.
constrain :: forall (m :: Nat) x. KnownDim m => Dim x -> Maybe (Dim (XN m)) Source #
Change the minimum allowed size of a Dim (XN x)
,
while testing if the value inside satisfies it.
constrainBy :: forall m x. Dim m -> Dim x -> Maybe (Dim (XN m)) Source #
constrain
with explicitly-passed constraining Dim
to avoid AllowAmbiguousTypes
.
relax :: forall (m :: Nat) (n :: Nat). MinDim m n => Dim (XN n) -> Dim (XN m) Source #
Decrease minimum allowed size of a Dim (XN x)
.
Simple Dim arithmetics
Re-export part of TypeLits
for convenience
(Kind) This is the kind of type-level natural numbers.
Instances
KnownDimKind Nat Source # | |
KnownNat n => KnownDim Nat n Source # | |
KnownDim Nat 0 Source # | |
KnownDim Nat 1 Source # | |
KnownDim Nat 2 Source # | |
KnownDim Nat 3 Source # | |
KnownDim Nat 4 Source # | |
KnownDim Nat 5 Source # | |
KnownDim Nat 6 Source # | |
KnownDim Nat 7 Source # | |
KnownDim Nat 8 Source # | |
KnownDim Nat 9 Source # | |
KnownDim Nat 10 Source # | |
KnownDim Nat 11 Source # | |
KnownDim Nat 12 Source # | |
KnownDim Nat 13 Source # | |
KnownDim Nat 14 Source # | |
KnownDim Nat 15 Source # | |
KnownDim Nat 16 Source # | |
KnownDim Nat 17 Source # | |
KnownDim Nat 18 Source # | |
KnownDim Nat 19 Source # | |
KnownDim Nat 20 Source # | |
Eq (Dim Nat n) # | |
Ord (Dim Nat n) # | |
type (==) Nat a b | |
type family CmpNat (a :: Nat) (b :: Nat) :: Ordering where ... #
Comparison of type-level naturals, as a function.
Since: 4.7.0.0
type family (a :: Nat) - (b :: Nat) :: Nat where ... infixl 6 #
Subtraction of type-level naturals.
Since: 4.7.0.0
type family (a :: Nat) * (b :: Nat) :: Nat where ... infixl 7 #
Multiplication of type-level naturals.
type family (a :: Nat) ^ (b :: Nat) :: Nat where ... infixr 8 #
Exponentiation of type-level naturals.
type family MinDim (m :: Nat) (n :: Nat) :: Constraint where ... Source #
Friendly error message if `m <= n` constraint is not satisfied.
Use this type family instead of (<=)
if possible
or try inferDimLE
function as the last resort.
type family FixedDim (x :: XNat) (n :: Nat) :: Constraint where ... Source #
Constraints given by an XNat type on possible values of a Nat hidden inside.
inferDimLE :: forall m n. MinDim m n => Evidence (m <= n) Source #
MinDim
implies (<=)
, but this fact is not so clear to GHC.
This function assures the type system that the relation takes place.
Inferring kind of type-level dimension
class KnownDimKind k where Source #
Figure out whether the type-level dimension is Nat
or XNat
.
Useful for generalized inference functions.
Minimal complete definition
Instances