Safe Haskell | None |
---|---|
Language | Haskell2010 |
Top-level module exporting the key DependentLiterals functionality.
Synopsis
- class HasIntLiterals a
- type AllowsIntLiteral n a = (LitAssertion a a (ToInteger n), ReprAssertion a a (ToInteger n))
- type IntLiteral n a = (HasIntLiterals a, AllowsIntLiteral n a)
- valueOf :: forall n a. (IntLiteral (ToInteger n) a, KnownInteger (ToInteger n)) => a
- type HasBasicLiterals a = (HasIntLiterals a, LitAssertion a ~ NoAssertion, ReprAssertion a ~ NoAssertion)
- newtype StockLit a = StockLit a
- class SNum a where
- type SNumRepr a :: Type
- type SNumConstraint a :: Integer -> Constraint
- fromSNum :: Satisfying (SNumConstraint a) (SNumber (SNumRepr a)) -> a
- intoSNum :: a -> Satisfying (SNumConstraint a) (SNumber (SNumRepr a))
- newtype SNumLit (c :: Type -> Integer -> Constraint) a = SNumLit a
Int Literals
class HasIntLiterals a Source #
The main class providing dependent-literals functionality.
Instances of this can have integral literals and numeric patterns with
-fplugin=DependentLiterals.Plugin
.
This class exposes the internal unsafe machinery of the library; a safer and
less error-prone way to get instances for it is via SNum
or StockLit
.
Instances
type AllowsIntLiteral n a = (LitAssertion a a (ToInteger n), ReprAssertion a a (ToInteger n)) Source #
A constraint alias showing that the particular value n
is valid for a
.
With this in context along with
, you can use an
integral literal value HasIntLiterals
an
at type a
. See also IntLiteral
.
type IntLiteral n a = (HasIntLiterals a, AllowsIntLiteral n a) Source #
A convenient form of IntLiteral
when only one value is needed.
This is a constraint tuple, so using this multiple times in a signature
creates a bit of constraint pollution; for tidier signatures, use one
HasIntLiterals
and several AllowsIntLiteral
s.
valueOf :: forall n a. (IntLiteral (ToInteger n) a, KnownInteger (ToInteger n)) => a Source #
Get the value of a type-level number at runtime, as if it were a literal.
That is, when DependentLiterals is enabled, 42
and valueOf @42
are the
same thing. (When it's not enabled, 42
is just fromInteger 42
).
Basic Literals
type HasBasicLiterals a = (HasIntLiterals a, LitAssertion a ~ NoAssertion, ReprAssertion a ~ NoAssertion) Source #
A constraint alias that asserts you can use any integral literal.
This can be useful in polymorphic contexts when you don't want to list out constraints for every literal value you need, and are willing to accept that some types with stronger compile-time validation will be excluded.
Use
directly instead.Wrapped
Num
The HasIntLiterals
instance of
results in integral
literals and patterns that behave like the stock literals would have,
according to the underlying type's Wrapped
NumNum
instance.
For use with -XDerivingVia
. This calls through to the fromIntegral
of
the underlying Num
instance for the final conversion. If the type in
question has a Num
instance and it's acceptable for literals to overflow
(or if the type is supported by -Woverflowed-literals), this is a good way
to get an instance.
This is suitable for DerivingVia
and tends to work as a deriving clause,
so:
newtype MyType = MyType Int deriving (Eq, Num) deriving HasIntLiterals via Wrapped Num MyType
Note in this case you could just as well say deriving HasIntLiterals
to
get a GeneralizedNewtypeDeriving
instance that consumes Int literals and
coerces them, but if you wrote a custom Num instance, via Wrapped Num MyType
will respect it.
StockLit a |
Instances
(Eq a, Num a) => HasIntLiterals (StockLit a) Source # | |
Defined in DependentLiterals.Int type ReprAssertion (StockLit a) :: Type -> Integer -> Constraint Source # type LitConstraint (StockLit a) :: Integer -> Constraint Source # type LitAssertion (StockLit a) :: Type -> Integer -> Constraint Source # unsafeFromInteger :: forall (n :: Integer) b. (LitAssertion (StockLit a) b n, ReprAssertion (StockLit a) b n) => Proxy b -> Tagged n Integer -> StockLit a Source # unsafeMatchInteger :: forall (n :: Integer) b. ReprAssertion (StockLit a) b n => Proxy b -> Tagged n Integer -> StockLit a -> CMaybe (LitConstraint (StockLit a) n) Source # | |
type ReprAssertion (StockLit a) Source # | |
Defined in DependentLiterals.Int | |
type LitConstraint (StockLit a) Source # | |
Defined in DependentLiterals.Int | |
type LitAssertion (StockLit a) Source # | |
Defined in DependentLiterals.Int |
Dependent Literals
A user-facing dependent numeric typeclass.
This gives an isomorphism between a
and exists n. SNumber (SNumRepr a) n
given that SNumConstraint a n
holds.
To get instances of HasIntLiterals
without interacting with the unsafe
parts of the library, implement this class and lift it to HasIntLiterals
with deriving HasIntLiterals via SNumLit MyAssertionClass MyType
.
type SNumRepr a :: Type Source #
The underlying numerical representation type.
type SNumConstraint a :: Integer -> Constraint Source #
The constraint on literal values to validate/refine a
.
fromSNum :: Satisfying (SNumConstraint a) (SNumber (SNumRepr a)) -> a Source #
Conversion from SNumber
with a proof of SNumConstraint
into a
.
Used for interpreting integral literals.
intoSNum :: a -> Satisfying (SNumConstraint a) (SNumber (SNumRepr a)) Source #
Conversion from a
into SNumber
with a proof of 'SNumConstraint.'
Used for providing proofs on successful pattern matches.
Instances
newtype SNumLit (c :: Type -> Integer -> Constraint) a Source #
A newtype carrying a HasIntLiterals
instance for use with DerivingVia
.
SNumLit a |
Instances
(Eq (SNumRepr a), Num (SNumRepr a), SNum a) => HasIntLiterals (SNumLit c a) Source # | |
Defined in DependentLiterals.Int type ReprAssertion (SNumLit c a) :: Type -> Integer -> Constraint Source # type LitConstraint (SNumLit c a) :: Integer -> Constraint Source # type LitAssertion (SNumLit c a) :: Type -> Integer -> Constraint Source # unsafeFromInteger :: forall (n :: Integer) b. (LitAssertion (SNumLit c a) b n, ReprAssertion (SNumLit c a) b n) => Proxy b -> Tagged n Integer -> SNumLit c a Source # unsafeMatchInteger :: forall (n :: Integer) b. ReprAssertion (SNumLit c a) b n => Proxy b -> Tagged n Integer -> SNumLit c a -> CMaybe (LitConstraint (SNumLit c a) n) Source # | |
type ReprAssertion (SNumLit c a) Source # | |
Defined in DependentLiterals.Int | |
type LitConstraint (SNumLit c a) Source # | |
Defined in DependentLiterals.Int | |
type LitAssertion (SNumLit c a) Source # | |
Defined in DependentLiterals.Int |