Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data Nat
- toNatural :: Nat -> Natural
- fromNatural :: Natural -> Nat
- cata :: a -> (a -> a) -> Nat -> a
- explicitShow :: Nat -> String
- explicitShowsPrec :: Int -> Nat -> ShowS
- data SNat n where
- snatToNat :: forall n. SNat n -> Nat
- snatToNatural :: forall n. SNat n -> Natural
- class SNatI n where
- reify :: forall r. Nat -> (forall n. SNatI n => Proxy n -> r) -> r
- reflect :: forall n proxy. SNatI n => proxy n -> Nat
- reflectToNum :: forall n m proxy. (SNatI n, Num m) => proxy n -> m
- eqNat :: forall n m. (SNatI n, SNatI m) => Maybe (n :~: m)
- type family EqNat (n :: Nat) (m :: Nat) where ...
- induction :: forall n f. SNatI n => f Z -> (forall m. SNatI m => f m -> f (S m)) -> f n
- induction1 :: forall n f a. SNatI n => f Z a -> (forall m. SNatI m => f m a -> f (S m) a) -> f n a
- class SNatI n => InlineInduction n where
- inlineInduction :: forall n f. InlineInduction n => f Z -> (forall m. InlineInduction m => f m -> f (S m)) -> f n
- unfoldedFix :: forall n a proxy. InlineInduction n => proxy n -> (a -> a) -> a
- type family Plus (n :: Nat) (m :: Nat) :: Nat where ...
- type family Mult (n :: Nat) (m :: Nat) :: Nat where ...
- type family ToGHC (n :: Nat) :: Nat where ...
- type family FromGHC (n :: Nat) :: Nat where ...
- nat0 :: Nat
- nat1 :: Nat
- nat2 :: Nat
- nat3 :: Nat
- nat4 :: Nat
- nat5 :: Nat
- nat6 :: Nat
- nat7 :: Nat
- nat8 :: Nat
- nat9 :: Nat
- type Nat0 = Z
- type Nat1 = S Nat0
- type Nat2 = S Nat1
- type Nat3 = S Nat2
- type Nat4 = S Nat3
- type Nat5 = S Nat4
- type Nat6 = S Nat5
- type Nat7 = S Nat6
- type Nat8 = S Nat7
- type Nat9 = S Nat8
- proofPlusZeroN :: Plus Nat0 n :~: n
- proofPlusNZero :: SNatI n => Plus n Nat0 :~: n
- proofMultZeroN :: Mult Nat0 n :~: Nat0
- proofMultNZero :: forall n proxy. SNatI n => proxy n -> Mult n Nat0 :~: Nat0
- proofMultOneN :: SNatI n => Mult Nat1 n :~: n
- proofMultNOne :: SNatI n => Mult n Nat1 :~: n
Natural, Nat numbers
Nat natural numbers.
Better than GHC's built-in Nat
for some use cases.
fromNatural :: Natural -> Nat Source #
Showing
explicitShow :: Nat -> String Source #
Singleton
Singleton of Nat
.
snatToNatural :: forall n. SNat n -> Natural Source #
Implicit
Convenience class to get SNat
.
reify :: forall r. Nat -> (forall n. SNatI n => Proxy n -> r) -> r Source #
Reify Nat
.
>>>
reify nat3 reflect
3
reflect :: forall n proxy. SNatI n => proxy n -> Nat Source #
Reflect type-level Nat
to the term level.
reflectToNum :: forall n m proxy. (SNatI n, Num m) => proxy n -> m Source #
Equality
eqNat :: forall n m. (SNatI n, SNatI m) => Maybe (n :~: m) Source #
Decide equality of type-level numbers.
>>>
eqNat :: Maybe (Nat3 :~: Plus Nat1 Nat2)
Just Refl
>>>
eqNat :: Maybe (Nat3 :~: Mult Nat2 Nat2)
Nothing
type family EqNat (n :: Nat) (m :: Nat) where ... Source #
Type family used to implement ==
from Data.Type.Equality module.
Induction
Induction on Nat
.
Useful in proofs or with GADTs, see source of proofPlusNZero
.
Induction on Nat
, functor form. Useful for computation.
>>>
induction1 (Tagged 0) $ retagMap (+2) :: Tagged Nat3 Int
Tagged 6
class SNatI n => InlineInduction n where Source #
The induction will be fully inlined.
See test/Inspection.hs
.
inlineInduction1 :: f Z a -> (forall m. InlineInduction m => f m a -> f (S m) a) -> f n a Source #
InlineInduction Z Source # | |
InlineInduction n => InlineInduction (S n) Source # | |
:: InlineInduction n | |
=> f Z | zero case |
-> (forall m. InlineInduction m => f m -> f (S m)) | induction step |
-> f n |
See InlineInduction
.
Example: unfoldedFix
unfoldedFix :: forall n a proxy. InlineInduction n => proxy n -> (a -> a) -> a Source #
Unfold n
steps of a general recursion.
Note: Always benchmark. This function may give you both bad properties: a lot of code (increased binary size), and worse performance.
For known n
unfoldedFix
will unfold recursion, for example
unfoldedFix
(Proxy
::Proxy
Nat3
) f = f (f (f (fix f)))
Arithmetic
type family Plus (n :: Nat) (m :: Nat) :: Nat where ... Source #
Addition.
>>>
reflect (snat :: SNat (Plus Nat1 Nat2))
3
type family Mult (n :: Nat) (m :: Nat) :: Nat where ... Source #
Multiplication.
>>>
reflect (snat :: SNat (Mult Nat2 Nat3))
6
Conversion to GHC Nat
type family ToGHC (n :: Nat) :: Nat where ... Source #
Convert to GHC Nat
.
>>>
:kind! ToGHC Nat5
ToGHC Nat5 :: GHC.Nat = 5
type family FromGHC (n :: Nat) :: Nat where ... Source #
Convert from GHC Nat
.
>>>
:kind! FromGHC 7
FromGHC 7 :: Nat = 'S ('S ('S ('S ('S ('S ('S 'Z))))))