Safe Haskell | None |
---|---|
Language | Haskell2010 |
simple refinement type with only one type and a predicate
Synopsis
- data Refined (opts :: Opt) p a
- unRefined :: forall k (opts :: Opt) (p :: k) a. Refined opts p a -> a
- data Msg0 = Msg0 {}
- showMsg0 :: Msg0 -> String
- type RefinedC opts p a = (OptC opts, PP p a ~ Bool, P p a)
- newRefined :: forall opts p a. RefinedC opts p a => a -> Either Msg0 (Refined opts p a)
- newRefined' :: forall opts p a m. (MonadEval m, RefinedC opts p a) => a -> m (Either Msg0 (Refined opts p a))
- genRefined :: forall opts p a. (RefinedC opts p a, HasCallStack) => Gen a -> Gen (Refined opts p a)
- unsafeRefined :: forall opts p a. a -> Refined opts p a
- unsafeRefined' :: forall opts p a. (RefinedC opts p a, HasCallStack) => a -> Refined opts p a
Refined
data Refined (opts :: Opt) p a Source #
a simple refinement type that ensures the predicate p
holds for the type a
Instances
Lift a => Lift (Refined opts p a :: Type) Source # | |
Eq a => Eq (Refined opts p a) Source # | |
Ord a => Ord (Refined opts p a) Source # | |
Defined in Predicate.Refined compare :: Refined opts p a -> Refined opts p a -> Ordering # (<) :: Refined opts p a -> Refined opts p a -> Bool # (<=) :: Refined opts p a -> Refined opts p a -> Bool # (>) :: Refined opts p a -> Refined opts p a -> Bool # (>=) :: Refined opts p a -> Refined opts p a -> Bool # max :: Refined opts p a -> Refined opts p a -> Refined opts p a # min :: Refined opts p a -> Refined opts p a -> Refined opts p a # | |
(RefinedC opts p a, Read a) => Read (Refined opts p a) Source # |
|
Show a => Show (Refined opts p a) Source # | |
RefinedC opts p String => IsString (Refined opts p String) Source # |
|
Defined in Predicate.Refined fromString :: String -> Refined opts p String # | |
(Arbitrary a, RefinedC opts p a, Show a) => Arbitrary (Refined opts p a) Source # |
|
(RefinedC opts p a, Hashable a) => Hashable (Refined opts p a) Source # | |
Defined in Predicate.Refined | |
ToJSON a => ToJSON (Refined opts p a) Source # | |
Defined in Predicate.Refined | |
(RefinedC opts p a, FromJSON a) => FromJSON (Refined opts p a) Source # |
|
(RefinedC opts p a, Binary a) => Binary (Refined opts p a) Source # |
|
NFData a => NFData (Refined opts p a) Source # | |
Defined in Predicate.Refined |
unRefined :: forall k (opts :: Opt) (p :: k) a. Refined opts p a -> a Source #
extract the value from Refined
type RefinedC opts p a = (OptC opts, PP p a ~ Bool, P p a) Source #
the constraints that Refined
must adhere to
create methods
newRefined :: forall opts p a. RefinedC opts p a => a -> Either Msg0 (Refined opts p a) Source #
returns a Refined
value if a
is valid for the predicate p
>>>
newRefined @OL @(ReadP Int Id > 99) "123"
Right (Refined "123")
>>>
left m0Long $ newRefined @OL @(ReadP Int Id > 99) "12"
Left "False (12 > 99)"
>>>
newRefined @OZ @(Between 10 14 Id) 13
Right (Refined 13)
>>>
left m0BoolE $ newRefined @OZ @(Between 10 14 Id) 99
Left (Right False)
>>>
newRefined @OZ @(Last >> Len == 4) ["one","two","three","four"]
Right (Refined ["one","two","three","four"])
>>>
newRefined @OZ @(Re "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$") "141.213.1.99"
Right (Refined "141.213.1.99")
>>>
left m0BoolE $ newRefined @OZ @(Re "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$") "141.213.1"
Left (Right False)
>>>
left m0BoolE $ newRefined @OZ @(Map' (ReadP Int Id) (Resplit "\\.") >> GuardBool (PrintF "bad length: found %d" Len) (Len == 4)) "141.213.1"
Left (Left "bad length: found 3")
>>>
left m0BoolE $ newRefined @OZ @(Map' (ReadP Int Id) (Resplit "\\.") >> GuardBool (PrintF "bad length: found %d" Len) (Len == 4) && BoolsN (PrintT "octet %d out of range %d" Id) 4 (0 <..> 0xff)) "141.213.1.444"
Left (Left "Bool(3) [octet 3 out of range 444]")
>>>
left m0BoolE $ newRefined @OZ @(Map' (ReadP Int Id) (Resplit "\\.") >> GuardBool (PrintF "bad length: found %d" Len) (Len == 4) && BoolsN (PrintT "octet %d out of range %d" Id) 4 (0 <..> 0xff)) "141.213.1x34.444"
Left (Left "ReadP Int (1x34)")
>>>
newRefined @OZ @(Map ('[Id] >> ReadP Int Id) >> IsLuhn) "12344"
Right (Refined "12344")
>>>
left m0BoolE $ newRefined @OZ @(Map ('[Id] >> ReadP Int Id) >> IsLuhn) "12340"
Left (Right False)
>>>
newRefined @OZ @(Any IsPrime) [11,13,17,18]
Right (Refined [11,13,17,18])
>>>
left m0BoolE $ newRefined @OZ @(All IsPrime) [11,13,17,18]
Left (Right False)
>>>
newRefined @OZ @(Snd !! Fst >> Len > 5) (2,["abc","defghij","xyzxyazsfd"])
Right (Refined (2,["abc","defghij","xyzxyazsfd"]))
>>>
left m0BoolE $ newRefined @OZ @(Snd !! Fst >> Len > 5) (27,["abc","defghij","xyzxyazsfd"])
Left (Left "(!!) index not found")
>>>
left m0BoolE $ newRefined @OZ @(Snd !! Fst >> Len <= 5) (2,["abc","defghij","xyzxyazsfd"])
Left (Right False)
>>>
newRefined @OU @((Id $$ 13) > 100) (\x -> x * 14) ^? _Right . to unRefined . to ($ 99)
Just 1386
newRefined' :: forall opts p a m. (MonadEval m, RefinedC opts p a) => a -> m (Either Msg0 (Refined opts p a)) Source #
QuickCheck method
genRefined :: forall opts p a. (RefinedC opts p a, HasCallStack) => Gen a -> Gen (Refined opts p a) Source #
create Refined
generator using a generator to restrict the values
unsafe create methods
unsafeRefined :: forall opts p a. a -> Refined opts p a Source #
create an unsafe Refined
value without running the predicate
unsafeRefined' :: forall opts p a. (RefinedC opts p a, HasCallStack) => a -> Refined opts p a Source #
create an unsafe Refined
value and also run the predicate