| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Refined
- data Refined p x
- refine :: Predicate p x => x -> Either String (Refined p x)
- refineTH :: (Predicate p x, Lift x) => x -> Q (TExp (Refined p x))
- unrefine :: Refined p x -> x
- class Predicate p x where
- data Not r
- data And l r
- data Or l r
- data LessThan (n :: Nat)
- data GreaterThan (n :: Nat)
- data From (n :: Nat)
- data To (n :: Nat)
- data FromTo (mn :: Nat) (mx :: Nat)
- data EqualTo (n :: Nat)
- type Positive = GreaterThan 0
- type NonPositive = To 0
- type Negative = LessThan 0
- type NonNegative = From 0
- type ZeroToOne = FromTo 0 1
Documentation
A refinement type,
which wraps a value of type x,
ensuring that it satisfies a type-level predicate p.
refine :: Predicate p x => x -> Either String (Refined p x) #
A smart constructor of a Refined value.
Checks the input value at runtime.
refineTH :: (Predicate p x, Lift x) => x -> Q (TExp (Refined p x)) #
Constructs a Refined value with checking at compile-time using Template Haskell.
E.g.,
>>>$$(refineTH 23) :: Refined Positive IntRefined 23
Here's an example of an invalid value:
>>>$$(refineTH 0) :: Refined Positive Int<interactive>:6:4: Value is not greater than 0 In the Template Haskell splice $$(refineTH 0) In the expression: $$(refineTH 0) :: Refined Positive Int In an equation for ‘it’: it = $$(refineTH 0) :: Refined Positive Int
If it's not evident, the example above indicates a compile-time failure, which means that the checking was done at compile-time, thus introducing a zero runtime overhead compared to a plain value construction.
Predicate Interface
A class which defines a runtime interpretation of
a type-level predicate p for type x.
Minimal complete definition
Methods
validate :: p -> x -> Maybe String #
Check the value x according to the predicate p,
producing an error string if the value does not satisfy.
Instances
| (Ord x, Num x, KnownNat n) => Predicate (EqualTo n) x # | |
| (Ord x, Num x, KnownNat n) => Predicate (To n) x # | |
| (Ord x, Num x, KnownNat n) => Predicate (From n) x # | |
| (Ord x, Num x, KnownNat n) => Predicate (GreaterThan n) x # | |
| (Ord x, Num x, KnownNat n) => Predicate (LessThan n) x # | |
| Predicate r x => Predicate (Not r) x # | |
| (Ord x, Num x, KnownNat mn, KnownNat mx, (<=) mn mx) => Predicate (FromTo mn mx) x # | |
| (Predicate l x, Predicate r x) => Predicate (Or l r) x # | |
| (Predicate l x, Predicate r x) => Predicate (And l r) x # | |
Standard Predicates
Logical
A logical conjunction predicate, composed of two other predicates.
A logical disjunction predicate, composed of two other predicates.
Numeric
A predicate, which ensures that a value is less than the specified type-level number.
data GreaterThan (n :: Nat) #
A predicate, which ensures that a value is greater than the specified type-level number.
A predicate, which ensures that a value is greater than or equal to the specified type-level number.
A predicate, which ensures that a value is less than or equal to the specified type-level number.
data FromTo (mn :: Nat) (mx :: Nat) #
A predicate, which ensures that a value is between or equal to either of the specified type-level numbers.
A predicate, which ensures that a value equals to the specified type-level number.
type Positive = GreaterThan 0 #
A predicate, which ensures that the value is greater than zero.
type NonPositive = To 0 #
A predicate, which ensures that the value is less than or equal to zero.
type NonNegative = From 0 #
A predicate, which ensures that the value is greater than or equal to zero.