Copyright | (c) Justin Le 2015 |
---|---|
License | MIT |
Maintainer | justin@jle.im |
Stability | unstable |
Portability | non-portable |
Safe Haskell | Safe |
Language | Haskell2010 |
Provides the KnownNats
and KnownSymbols
typeclasses in analogy to
KnownNat
and KnownSymbol
from GHC.TypeLits. Also provides
singleton-esque structures for traversing over type-level lists of
Nat
s and Symbol
s. Comes with continuation-style reifiers and
existential types for dependent typing usage, and as an analogy with
SomeNat
and SomeSymbol
.
See typeclass documentations for more information.
- class KnownNats ns where
- data SomeNats :: * where
- data NatList :: [Nat] -> * where
- someNatsVal :: [Integer] -> Maybe SomeNats
- someNatsVal' :: [Integer] -> SomeNats
- reifyNats :: [Integer] -> (forall ns. KnownNats ns => NatList ns -> r) -> r
- traverseNatList :: forall f ns. Applicative f => (forall n. KnownNat n => Proxy n -> f SomeNat) -> NatList ns -> f SomeNats
- traverseNatList' :: forall f. Applicative f => (SomeNat -> f SomeNat) -> SomeNats -> f SomeNats
- traverseNatList_ :: forall f a ns. Applicative f => (forall n. KnownNat n => Proxy n -> f a) -> NatList ns -> f ()
- mapNatList :: (forall n. KnownNat n => Proxy n -> SomeNat) -> NatList ns -> SomeNats
- class KnownSymbols ns where
- symbolsVal :: p ns -> [String]
- symbolsList :: SymbolList ns
- data SomeSymbols :: * where
- SomeSymbols :: KnownSymbols ns => !(SymbolList ns) -> SomeSymbols
- data SymbolList :: [Symbol] -> * where
- ØSL :: SymbolList `[]`
- (:<$) :: (KnownSymbol n, KnownSymbols ns) => !(Proxy n) -> !(SymbolList ns) -> SymbolList (n : ns)
- someSymbolsVal :: [String] -> SomeSymbols
- reifySymbols :: [String] -> (forall ns. KnownSymbols ns => SymbolList ns -> r) -> r
- traverseSymbolList :: forall f ns. Applicative f => (forall n. KnownSymbol n => Proxy n -> f SomeSymbol) -> SymbolList ns -> f SomeSymbols
- traverseSymbolList' :: forall f. Applicative f => (SomeSymbol -> f SomeSymbol) -> SomeSymbols -> f SomeSymbols
- traverseSymbolList_ :: forall f ns. Applicative f => (forall n a. KnownSymbol n => Proxy n -> f a) -> SymbolList ns -> f ()
- mapSymbolList :: (forall n. KnownSymbol n => Proxy n -> SomeSymbol) -> SymbolList ns -> SomeSymbols
KnownNats
class KnownNats ns where Source
is intended to represent that every KnownNats
nsNat
in the
type-level list ns
is itself a KnownNat
(meaning, you can use
natVal
to get its corresponding Integer
).
In practice, just knowing that every item has a KnownNat
instance is
not enough; it's nice, but unless you're able to "iterate" over every
Nat
in the list, it's of limited use. That's why this class also
provides a constructor for
, so that you can produce
a NatList
nsNatList
for every
, which you can iterate over to get
KnownNat
ns
s for every Proxy
nn
in ns
along with the
instances.KnownNat
n
It also has an analogy to natVal
, natsVal
, which lets you get a list
of the represented Integer
s for, say,
.Proxy
[1,2,3]
data SomeNats :: * where Source
Represents unknown type-level lists of type-level natural numbers.
It's a NatList
, but you don't know what the list contains at
compile-time.
data NatList :: [Nat] -> * where Source
Singleton-esque type for "traversing" over type-level lists of Nat
s.
Essentially contains a (value-level) list of
s, but each Proxy
nn
has a KnownNat
instance for you to use. At runtime (after type
erasure), is more or less equivalent to a [
.Integer
]
Typically generated using natsList
.
someNatsVal :: [Integer] -> Maybe SomeNats Source
List equivalent of someNatVal
. Convert a list of integers into an
unknown type-level list of naturals. Will return Nothing
if any of
the given Integer
s is negative.
someNatsVal' :: [Integer] -> SomeNats Source
Like someNatsVal
, but will also go ahead and produce KnownNat
s
whose integer values are negative. It won't ever error on producing
them, but extra care must be taken when using the produced SomeNat
s.
reifyNats :: [Integer] -> (forall ns. KnownNats ns => NatList ns -> r) -> r Source
List equivalent of reifyNat
. Given a list of integers, takes
a function in an "environment" with a
corresponding to
the given list, where every NatList
nsn
in ns
has a KnownNat
instance.
Essentially a continuation-style version of SomeNats
.
For compatability with reifyNat
, be aware that this also produces
s where KnownNat
nn
is negative, without complaining.
traverseNatList :: forall f ns. Applicative f => (forall n. KnownNat n => Proxy n -> f SomeNat) -> NatList ns -> f SomeNats Source
traverseNatList' :: forall f. Applicative f => (SomeNat -> f SomeNat) -> SomeNats -> f SomeNats Source
Like traverseNatList
, but literally actually a
Traversal'
, so is usable with lens-library
machinery.SomeNat
SomeNats
traverseNatList_ :: forall f a ns. Applicative f => (forall n. KnownNat n => Proxy n -> f a) -> NatList ns -> f () Source
KnownSymbols
class KnownSymbols ns where Source
is intended to represent that every KnownSymbols
nsSymbol
in the
type-level list ns
is itself a KnownSymbol
(meaning, you can use
symbolVal
to get its corresponding String
).
You can use symbolsVal
to get the corresponding [
from
String
]
.KnownSymbols
ns
For reasons discussed further in the documentation for KnownNats
, this
also lets you generate a
, in order to iterate over the
type-level list of SymbolList
nsSymbol
s and take advantage of their KnownSymbol
instances.
symbolsVal :: p ns -> [String] Source
symbolsList :: SymbolList ns Source
KnownSymbols ([] Symbol) Source | |
(KnownSymbol n, KnownSymbols ns) => KnownSymbols ((:) Symbol n ns) Source |
data SomeSymbols :: * where Source
Represents unknown type-level lists of Symbol
s. It's a SymbolList
,
but you don't know what the list contains at compile-time.
SomeSymbols :: KnownSymbols ns => !(SymbolList ns) -> SomeSymbols |
data SymbolList :: [Symbol] -> * where Source
Singleton-esque type for "traversing" over type-level lists of
Symbol
s. Essentially contains a (value-level) list of
s,
but each Proxy
nn
has a KnownSymbol
instance for you to use. At runtime
(after type erasure), is more or less equivalent to a [
.String
]
Typically generated using symbolsList
.
ØSL :: SymbolList `[]` | |
(:<$) :: (KnownSymbol n, KnownSymbols ns) => !(Proxy n) -> !(SymbolList ns) -> SymbolList (n : ns) infixr 5 |
Show (SymbolList ns) Source |
someSymbolsVal :: [String] -> SomeSymbols Source
List equivalent of someNatVal
. Convert a list of integers into an
unknown type-level list of naturals. Will return Nothing
if any of
the given Integer
s is negative.
reifySymbols :: [String] -> (forall ns. KnownSymbols ns => SymbolList ns -> r) -> r Source
List equivalent of reifyNat
. Given a list of integers, takes
a function in an "environment" with a
corresponding to
the given list, where every NatList
nsn
in ns
has a KnownNat
instance.
Essentially a continuation-style version of SomeSymbols
.
traverseSymbolList :: forall f ns. Applicative f => (forall n. KnownSymbol n => Proxy n -> f SomeSymbol) -> SymbolList ns -> f SomeSymbols Source
Utility function for traversing over all of the
s in
a Proxy
nSymbolList
, each with the corresponding KnownSymbol
instance
available. Gives the the ability to "change" the represented natural
number to a new one, in a SomeSymbol
.
Can be considered a form of a Traversal'
.SomeSymbol
SomeSymbols
traverseSymbolList' :: forall f. Applicative f => (SomeSymbol -> f SomeSymbol) -> SomeSymbols -> f SomeSymbols Source
Like traverseSymbolList
, but literally actually a
Traversal'
, so is usable with lens-library
machinery.SomeSymbol
SomeSymbols
traverseSymbolList_ :: forall f ns. Applicative f => (forall n a. KnownSymbol n => Proxy n -> f a) -> SymbolList ns -> f () Source
Utility function for traversing over all of the
s in
a Proxy
nSymbolList
, each with the corresponding KnownSymbol
instance
available. Results are ignored.
mapSymbolList :: (forall n. KnownSymbol n => Proxy n -> SomeSymbol) -> SymbolList ns -> SomeSymbols Source
Utility function for "mapping" over each of the Symbol
s in the
SymbolList
.