{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Refined
(
Refined
, refine
, refine_
, refineThrow
, refineFail
, refineError
, refineEither
, refineTH
, refineTH_
, unrefine
, Predicate (validate)
, reifyPredicate
, Not(..)
, And(..)
, type (&&)
, Or(..)
, type (||)
, Xor(..)
, IdPred(..)
, LessThan(..)
, GreaterThan(..)
, From(..)
, To(..)
, FromTo(..)
, NegativeFromTo(..)
, EqualTo(..)
, NotEqualTo(..)
, Odd(..)
, Even(..)
, DivisibleBy(..)
, NaN(..)
, Infinite(..)
, Positive
, NonPositive
, Negative
, NonNegative
, ZeroToOne
, NonZero
, SizeLessThan(..)
, SizeGreaterThan(..)
, SizeEqualTo(..)
, Empty
, NonEmpty
, Ascending(..)
, Descending(..)
, Weaken (weaken)
, andLeft
, andRight
, leftOr
, rightOr
, weakenAndLeft
, weakenAndRight
, weakenOrLeft
, weakenOrRight
, strengthen
, RefineException
( RefineNotException
, RefineAndException
, RefineOrException
, RefineXorException
, RefineOtherException
, RefineSomeException
)
, displayRefineException
, throwRefineOtherException
, throwRefineSomeException
, success
) where
import Control.Exception (Exception (displayException))
import Data.Coerce (coerce)
import Data.Either (isRight, rights)
import Data.Foldable (foldl')
import Data.Functor.Contravariant ((>$<))
import Data.Proxy (Proxy(Proxy))
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Lazy as TextLazy
import qualified Data.Text.Lazy.Builder as TextBuilder
import qualified Data.Text.Lazy.Builder.Int as TextBuilder
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Typeable (TypeRep, Typeable, typeRep)
import Control.Monad.Catch (MonadThrow, SomeException)
import qualified Control.Monad.Catch as MonadThrow
import Control.Monad.Error.Class (MonadError)
import qualified Control.Monad.Error.Class as MonadError
#if !MIN_VERSION_base(4,13,0)
import Control.Monad.Fail (MonadFail, fail)
import Prelude hiding (fail)
#endif
import GHC.Exts (Proxy#, proxy#)
import GHC.Generics (Generic, Generic1)
import GHC.TypeLits (type (<=), KnownNat, Nat, natVal')
import Refined.Unsafe.Type (Refined(Refined))
import qualified Language.Haskell.TH.Syntax as TH
#if HAVE_AESON
import Control.Monad ((<=<))
import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
import qualified Data.Aeson as Aeson
#endif
#if HAVE_QUICKCHECK
import Test.QuickCheck (Arbitrary, Gen)
import qualified Test.QuickCheck as QC
import Data.Typeable (showsTypeRep)
#endif
import "these-skinny" Data.These (These(This,That,These))
infixl 0 |>
infixl 9 .>
(|>) :: a -> (a -> b) -> b
a
x |> :: forall a b. a -> (a -> b) -> b
|> a -> b
f = forall a b. a -> (a -> b) -> b
apply a
x a -> b
f
{-# INLINE (|>) #-}
(.>) :: (a -> b) -> (b -> c) -> a -> c
a -> b
f .> :: forall a b c. (a -> b) -> (b -> c) -> a -> c
.> b -> c
g = forall a b c. (a -> b) -> (b -> c) -> a -> c
compose a -> b
f b -> c
g
{-# INLINE (.>) #-}
apply :: a -> (a -> b) -> b
apply :: forall a b. a -> (a -> b) -> b
apply a
x a -> b
f = a -> b
f a
x
compose :: (a -> b) -> (b -> c) -> (a -> c)
compose :: forall a b c. (a -> b) -> (b -> c) -> a -> c
compose a -> b
f b -> c
g a
x = b -> c
g (a -> b
f a
x)
data Ordered a = Empty | Decreasing a | Increasing a
inc :: Ordered a -> Bool
inc :: forall a. Ordered a -> Bool
inc (Decreasing a
_) = Bool
False
inc Ordered a
_ = Bool
True
{-# INLINE inc #-}
dec :: Ordered a -> Bool
dec :: forall a. Ordered a -> Bool
dec (Increasing a
_) = Bool
False
dec Ordered a
_ = Bool
True
{-# INLINE dec #-}
increasing :: (Foldable t, Ord a) => t a -> Bool
increasing :: forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> Bool
increasing = forall a. Ordered a -> Bool
inc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall {a}. Ord a => Ordered a -> a -> Ordered a
go forall a. Ordered a
Empty where
go :: Ordered a -> a -> Ordered a
go Ordered a
Empty a
y = forall a. a -> Ordered a
Increasing a
y
go (Decreasing a
x) a
_ = forall a. a -> Ordered a
Decreasing a
x
go (Increasing a
x) a
y
| a
x forall a. Ord a => a -> a -> Bool
<= a
y = forall a. a -> Ordered a
Increasing a
y
| Bool
otherwise = forall a. a -> Ordered a
Decreasing a
y
{-# INLINABLE increasing #-}
decreasing :: (Foldable t, Ord a) => t a -> Bool
decreasing :: forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> Bool
decreasing = forall a. Ordered a -> Bool
dec forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall {a}. Ord a => Ordered a -> a -> Ordered a
go forall a. Ordered a
Empty where
go :: Ordered a -> a -> Ordered a
go Ordered a
Empty a
y = forall a. a -> Ordered a
Decreasing a
y
go (Increasing a
x) a
_ = forall a. a -> Ordered a
Increasing a
x
go (Decreasing a
x) a
y
| a
x forall a. Ord a => a -> a -> Bool
>= a
y = forall a. a -> Ordered a
Decreasing a
y
| Bool
otherwise = forall a. a -> Ordered a
Increasing a
y
{-# INLINABLE decreasing #-}
instance (Read x, Predicate p x) => Read (Refined p x) where
readsPrec :: Int -> ReadS (Refined p x)
readsPrec Int
d = forall a. Bool -> ReadS a -> ReadS a
readParen (Int
d forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$ \String
r1 -> do
(String
"Refined", String
r2) <- ReadS String
lex String
r1
(x
raw, String
r3) <- forall a. Read a => Int -> ReadS a
readsPrec Int
11 String
r2
case forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine x
raw of
Right Refined p x
val -> [(Refined p x
val, String
r3)]
Left RefineException
_ -> []
#if HAVE_AESON
instance (FromJSON a, Predicate p a) => FromJSON (Refined p a) where
parseJSON :: Value -> Parser (Refined p a)
parseJSON = forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON
instance (FromJSONKey a, Predicate p a) => FromJSONKey (Refined p a) where
fromJSONKey :: FromJSONKeyFunction (Refined p a)
fromJSONKey = case forall a. FromJSONKey a => FromJSONKeyFunction a
Aeson.fromJSONKey @a of
FromJSONKeyFunction a
Aeson.FromJSONKeyCoerce -> forall a. (Text -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyTextParser forall a b. (a -> b) -> a -> b
$ forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
Aeson.FromJSONKeyText Text -> a
f -> forall a. (Text -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyTextParser forall a b. (a -> b) -> a -> b
$ forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> a
f
Aeson.FromJSONKeyTextParser Text -> Parser a
f -> forall a. (Text -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyTextParser forall a b. (a -> b) -> a -> b
$ forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Text -> Parser a
f
Aeson.FromJSONKeyValue Value -> Parser a
f -> forall a. (Value -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyValue forall a b. (a -> b) -> a -> b
$ forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Value -> Parser a
f
fromJSONKeyList :: FromJSONKeyFunction [Refined p a]
fromJSONKeyList = case forall a. FromJSONKey a => FromJSONKeyFunction [a]
Aeson.fromJSONKeyList @a of
Aeson.FromJSONKeyText Text -> [a]
f -> forall a. (Text -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyTextParser forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [a]
f
Aeson.FromJSONKeyTextParser Text -> Parser [a]
f -> forall a. (Text -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyTextParser forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Text -> Parser [a]
f
Aeson.FromJSONKeyValue Value -> Parser [a]
f -> forall a. (Value -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyValue forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Value -> Parser [a]
f
instance (ToJSON a, Predicate p a) => ToJSON (Refined p a) where
toJSON :: Refined p a -> Value
toJSON = forall a. ToJSON a => a -> Value
Aeson.toJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (p :: k) x. Refined p x -> x
unrefine
instance (ToJSONKey a, Predicate p a) => ToJSONKey (Refined p a) where
toJSONKey :: ToJSONKeyFunction (Refined p a)
toJSONKey = forall {k} (p :: k) x. Refined p x -> x
unrefine forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
>$< forall a. ToJSONKey a => ToJSONKeyFunction a
Aeson.toJSONKey
toJSONKeyList :: ToJSONKeyFunction [Refined p a]
toJSONKeyList = forall a b. (a -> b) -> [a] -> [b]
map forall {k} (p :: k) x. Refined p x -> x
unrefine forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
>$< forall a. ToJSONKey a => ToJSONKeyFunction [a]
Aeson.toJSONKeyList
#endif /* HAVE_AESON */
#if HAVE_QUICKCHECK
instance forall p a. (Arbitrary a, Typeable a, Typeable p, Predicate p a) => Arbitrary (Refined p a) where
arbitrary :: Gen (Refined p a)
arbitrary = Int -> Gen a -> Gen (Refined p a)
loop Int
0 forall a. Arbitrary a => Gen a
QC.arbitrary
where
loop :: Int -> Gen a -> Gen (Refined p a)
loop :: Int -> Gen a -> Gen (Refined p a)
loop !Int
runs Gen a
gen
| Int
runs forall a. Ord a => a -> a -> Bool
< Int
100 = do
Maybe (Refined p a)
m <- forall {k} (p :: k) a.
Predicate p a =>
Gen a -> Gen (Maybe (Refined p a))
suchThatRefined Gen a
gen
case Maybe (Refined p a)
m of
Just Refined p a
x -> do
forall (f :: * -> *) a. Applicative f => a -> f a
pure Refined p a
x
Maybe (Refined p a)
Nothing -> do
Int -> Gen a -> Gen (Refined p a)
loop (Int
runs forall a. Num a => a -> a -> a
+ Int
1) Gen a
gen
| Bool
otherwise = forall a. HasCallStack => String -> a
error (forall {k} {k} (p :: k) (a :: k).
(Typeable p, Typeable a) =>
Proxy p -> Proxy a -> String
refinedGenError (forall {k} (t :: k). Proxy t
Proxy @p) (forall {k} (t :: k). Proxy t
Proxy @a))
shrink :: Refined p a -> [Refined p a]
shrink = forall a b. [Either a b] -> [b]
rights forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
QC.shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (p :: k) x. Refined p x -> x
unrefine
refinedGenError :: (Typeable p, Typeable a)
=> Proxy p -> Proxy a -> String
refinedGenError :: forall {k} {k} (p :: k) (a :: k).
(Typeable p, Typeable a) =>
Proxy p -> Proxy a -> String
refinedGenError Proxy p
p Proxy a
a = String
"arbitrary :: Refined ("
forall a. [a] -> [a] -> [a]
++ forall {k} (a :: k). Typeable a => Proxy a -> String
typeName Proxy p
p
forall a. [a] -> [a] -> [a]
++ String
") ("
forall a. [a] -> [a] -> [a]
++ forall {k} (a :: k). Typeable a => Proxy a -> String
typeName Proxy a
a
forall a. [a] -> [a] -> [a]
++ String
"): Failed to generate a value that satisfied"
forall a. [a] -> [a] -> [a]
++ String
" the predicate after 100 tries."
suchThatRefined :: forall p a. (Predicate p a)
=> Gen a -> Gen (Maybe (Refined p a))
suchThatRefined :: forall {k} (p :: k) a.
Predicate p a =>
Gen a -> Gen (Maybe (Refined p a))
suchThatRefined Gen a
gen = do
Maybe a
m <- forall a. Gen a -> (a -> Bool) -> Gen (Maybe a)
QC.suchThatMaybe Gen a
gen (forall {k} (p :: k) a. Predicate p a => a -> Bool
reifyPredicate @p @a)
case Maybe a
m of
Maybe a
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
Just a
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. a -> Maybe a
Just (forall k (p :: k) x. x -> Refined p x
Refined a
x))
typeName :: Typeable a => Proxy a -> String
typeName :: forall {k} (a :: k). Typeable a => Proxy a -> String
typeName = forall a b c. (a -> b -> c) -> b -> a -> c
flip TypeRep -> ShowS
showsTypeRep String
"" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep
#endif /* HAVE_QUICKCHECK */
refine :: forall p x. (Predicate p x) => x -> Either RefineException (Refined p x)
refine :: forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine x
x = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. b -> Either a b
Right (forall k (p :: k) x. x -> Refined p x
Refined x
x)) forall a b. a -> Either a b
Left (forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate (forall {k} (t :: k). Proxy t
Proxy @p) x
x)
{-# INLINABLE refine #-}
refine_ :: forall p x. (Predicate p x) => x -> Either RefineException x
refine_ :: forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException x
refine_ = forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine @p @x forall a b c. (a -> b) -> (b -> c) -> a -> c
.> coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINABLE refine_ #-}
refineThrow :: (Predicate p x, MonadThrow m) => x -> m (Refined p x)
refineThrow :: forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadThrow m) =>
x -> m (Refined p x)
refineThrow = forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
MonadThrow.throwM forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINABLE refineThrow #-}
refineFail :: (Predicate p x, MonadFail m) => x -> m (Refined p x)
refineFail :: forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadFail m) =>
x -> m (Refined p x)
refineFail = forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall e. Exception e => e -> String
displayException forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall (m :: * -> *) a. MonadFail m => String -> m a
fail) forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINABLE refineFail #-}
refineError :: (Predicate p x, MonadError RefineException m)
=> x -> m (Refined p x)
refineError :: forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, MonadError RefineException m) =>
x -> m (Refined p x)
refineError = forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall e (m :: * -> *) a. MonadError e m => e -> m a
MonadError.throwError forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINABLE refineError #-}
refineEither :: forall p x. (Predicate p x) => x -> Either (Refined (Not p) x) (Refined p x)
refineEither :: forall {k} (p :: k) x.
Predicate p x =>
x -> Either (Refined (Not p) x) (Refined p x)
refineEither x
x =
case forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate (forall {k} (t :: k). Proxy t
Proxy @p) x
x of
Maybe RefineException
Nothing -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall k (p :: k) x. x -> Refined p x
Refined x
x
Just RefineException
_ -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall k (p :: k) x. x -> Refined p x
Refined x
x
{-# INLINABLE refineEither #-}
#if MIN_VERSION_template_haskell(2,17,0)
refineTH :: forall p x m. (Predicate p x, TH.Lift x, TH.Quote m, MonadFail m)
=> x
-> TH.Code m (Refined p x)
refineTH :: forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, Lift x, Quote m, MonadFail m) =>
x -> Code m (Refined p x)
refineTH =
let showException :: RefineException -> Code m a
showException = RefineException -> ExceptionTree RefineException
refineExceptionToTree
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> Bool -> ExceptionTree RefineException -> String
showTree Bool
True
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall (m :: * -> *) a. MonadFail m => String -> m a
fail
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a (m :: * -> *). m (TExp a) -> Code m a
TH.liftCode
in forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine @p @x
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall {a}. RefineException -> Code m a
showException forall t (m :: * -> *). (Lift t, Quote m) => t -> Code m t
TH.liftTyped
#else
refineTH :: forall p x. (Predicate p x, TH.Lift x)
=> x
-> TH.Q (TH.TExp (Refined p x))
refineTH =
let showException = refineExceptionToTree
.> showTree True
.> fail
in refine @p @x
.> either showException TH.lift
.> fmap TH.TExp
#endif
#if MIN_VERSION_template_haskell(2,17,0)
refineTH_ :: forall p x m. (Predicate p x, TH.Lift x, TH.Quote m, MonadFail m)
=> x
-> TH.Code m x
refineTH_ :: forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, Lift x, Quote m, MonadFail m) =>
x -> Code m x
refineTH_ =
forall {k} (p :: k) x (m :: * -> *).
(Predicate p x, Lift x, Quote m, MonadFail m) =>
x -> Code m (Refined p x)
refineTH @p @x
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall (m :: * -> *) a. Code m a -> m (TExp a)
TH.examineCode
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} (p :: k) x. TExp (Refined p x) -> TExp x
unsafeUnrefineTExp
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a (m :: * -> *). m (TExp a) -> Code m a
TH.liftCode
#else
refineTH_ :: forall p x. (Predicate p x, TH.Lift x)
=> x
-> TH.Q (TH.TExp x)
refineTH_ = refineTH @p @x .> fmap unsafeUnrefineTExp
#endif
unsafeUnrefineTExp :: TH.TExp (Refined p x) -> TH.TExp x
unsafeUnrefineTExp :: forall {k} (p :: k) x. TExp (Refined p x) -> TExp x
unsafeUnrefineTExp (TH.TExp Exp
e) = forall a. Exp -> TExp a
TH.TExp
(Name -> Exp
TH.VarE 'unrefine Exp -> Exp -> Exp
`TH.AppE` Exp
e)
unrefine :: Refined p x -> x
unrefine :: forall {k} (p :: k) x. Refined p x -> x
unrefine = coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINE unrefine #-}
class (Typeable p) => Predicate p x where
{-# MINIMAL validate #-}
validate :: Proxy p -> x -> Maybe RefineException
reifyPredicate :: forall p a. Predicate p a => a -> Bool
reifyPredicate :: forall {k} (p :: k) a. Predicate p a => a -> Bool
reifyPredicate = forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine @p @a forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a b. Either a b -> Bool
isRight
{-# INLINABLE reifyPredicate #-}
data IdPred
= IdPred
deriving
( forall x. Rep IdPred x -> IdPred
forall x. IdPred -> Rep IdPred x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IdPred x -> IdPred
$cfrom :: forall x. IdPred -> Rep IdPred x
Generic
)
instance Predicate IdPred x where
validate :: Proxy IdPred -> x -> Maybe RefineException
validate Proxy IdPred
_ x
_ = forall a. Maybe a
Nothing
{-# INLINE validate #-}
data Not p
= Not
deriving
( forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (p :: k) x. Rep (Not p) x -> Not p
forall k (p :: k) x. Not p -> Rep (Not p) x
$cto :: forall k (p :: k) x. Rep (Not p) x -> Not p
$cfrom :: forall k (p :: k) x. Not p -> Rep (Not p) x
Generic
, forall k (a :: k). Rep1 Not a -> Not a
forall k (a :: k). Not a -> Rep1 Not a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall k (a :: k). Rep1 Not a -> Not a
$cfrom1 :: forall k (a :: k). Not a -> Rep1 Not a
Generic1
)
instance (Predicate (p :: k) x, Typeable p, Typeable k) => Predicate (Not p) x where
validate :: Proxy (Not p) -> x -> Maybe RefineException
validate Proxy (Not p)
p x
x = do
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. a -> Maybe a
Just (TypeRep -> RefineException
RefineNotException (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (Not p)
p)))
(forall a b. a -> b -> a
const forall a. Maybe a
Nothing)
(forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate @p forall a. HasCallStack => a
undefined x
x)
data And l r
= And
deriving
( forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (l :: k) k (r :: k) x. Rep (And l r) x -> And l r
forall k (l :: k) k (r :: k) x. And l r -> Rep (And l r) x
$cto :: forall k (l :: k) k (r :: k) x. Rep (And l r) x -> And l r
$cfrom :: forall k (l :: k) k (r :: k) x. And l r -> Rep (And l r) x
Generic
, forall k k (l :: k) (a :: k). Rep1 (And l) a -> And l a
forall k k (l :: k) (a :: k). And l a -> Rep1 (And l) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall k k (l :: k) (a :: k). Rep1 (And l) a -> And l a
$cfrom1 :: forall k k (l :: k) (a :: k). And l a -> Rep1 (And l) a
Generic1
)
infixr 3 &&
type (&&) = And
instance ( Predicate (l :: k) x, Predicate (r :: k) x, Typeable l, Typeable r, Typeable k
) => Predicate (And l r) x where
validate :: Proxy (And l r) -> x -> Maybe RefineException
validate Proxy (And l r)
p x
x = do
let a :: Maybe RefineException
a = forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate @l forall a. HasCallStack => a
undefined x
x
let b :: Maybe RefineException
b = forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate @r forall a. HasCallStack => a
undefined x
x
let throw :: These RefineException RefineException -> Maybe RefineException
throw These RefineException RefineException
err = forall a. a -> Maybe a
Just (TypeRep -> These RefineException RefineException -> RefineException
RefineAndException (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (And l r)
p) These RefineException RefineException
err)
case (Maybe RefineException
a, Maybe RefineException
b) of
(Just RefineException
e, Just RefineException
e1) -> These RefineException RefineException -> Maybe RefineException
throw (forall a b. a -> b -> These a b
These RefineException
e RefineException
e1)
(Just RefineException
e, Maybe RefineException
_) -> These RefineException RefineException -> Maybe RefineException
throw (forall a b. a -> These a b
This RefineException
e)
(Maybe RefineException
Nothing, Just RefineException
e) -> These RefineException RefineException -> Maybe RefineException
throw (forall a b. b -> These a b
That RefineException
e)
(Maybe RefineException
Nothing, Maybe RefineException
Nothing) -> forall a. Maybe a
Nothing
data Or l r
= Or
deriving
( forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (l :: k) k (r :: k) x. Rep (Or l r) x -> Or l r
forall k (l :: k) k (r :: k) x. Or l r -> Rep (Or l r) x
$cto :: forall k (l :: k) k (r :: k) x. Rep (Or l r) x -> Or l r
$cfrom :: forall k (l :: k) k (r :: k) x. Or l r -> Rep (Or l r) x
Generic
, forall k k (l :: k) (a :: k). Rep1 (Or l) a -> Or l a
forall k k (l :: k) (a :: k). Or l a -> Rep1 (Or l) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall k k (l :: k) (a :: k). Rep1 (Or l) a -> Or l a
$cfrom1 :: forall k k (l :: k) (a :: k). Or l a -> Rep1 (Or l) a
Generic1
)
infixr 2 ||
type (||) = Or
instance ( Predicate (l :: k) x, Predicate (r :: k) x, Typeable l, Typeable r, Typeable k
) => Predicate (Or l r) x where
validate :: Proxy (Or l r) -> x -> Maybe RefineException
validate Proxy (Or l r)
p x
x = do
let left :: Maybe RefineException
left = forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate @l forall a. HasCallStack => a
undefined x
x
let right :: Maybe RefineException
right = forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate @r forall a. HasCallStack => a
undefined x
x
case (Maybe RefineException
left, Maybe RefineException
right) of
(Just RefineException
l, Just RefineException
r) -> forall a. a -> Maybe a
Just (TypeRep -> RefineException -> RefineException -> RefineException
RefineOrException (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (Or l r)
p) RefineException
l RefineException
r)
(Maybe RefineException, Maybe RefineException)
_ -> forall a. Maybe a
Nothing
data Xor l r
= Xor
deriving
( forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (l :: k) k (r :: k) x. Rep (Xor l r) x -> Xor l r
forall k (l :: k) k (r :: k) x. Xor l r -> Rep (Xor l r) x
$cto :: forall k (l :: k) k (r :: k) x. Rep (Xor l r) x -> Xor l r
$cfrom :: forall k (l :: k) k (r :: k) x. Xor l r -> Rep (Xor l r) x
Generic
, forall k k (l :: k) (a :: k). Rep1 (Xor l) a -> Xor l a
forall k k (l :: k) (a :: k). Xor l a -> Rep1 (Xor l) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall k k (l :: k) (a :: k). Rep1 (Xor l) a -> Xor l a
$cfrom1 :: forall k k (l :: k) (a :: k). Xor l a -> Rep1 (Xor l) a
Generic1
)
instance ( Predicate (l :: k) x, Predicate (r :: k) x, Typeable l, Typeable r, Typeable k
) => Predicate (Xor l r) x where
validate :: Proxy (Xor l r) -> x -> Maybe RefineException
validate Proxy (Xor l r)
p x
x = do
let left :: Maybe RefineException
left = forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate @l forall a. HasCallStack => a
undefined x
x
let right :: Maybe RefineException
right = forall {k} (p :: k) x.
Predicate p x =>
Proxy p -> x -> Maybe RefineException
validate @r forall a. HasCallStack => a
undefined x
x
case (Maybe RefineException
left, Maybe RefineException
right) of
(Maybe RefineException
Nothing, Maybe RefineException
Nothing) -> forall a. a -> Maybe a
Just (TypeRep
-> Maybe (RefineException, RefineException) -> RefineException
RefineXorException (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (Xor l r)
p) forall a. Maybe a
Nothing)
(Just RefineException
l, Just RefineException
r) -> forall a. a -> Maybe a
Just (TypeRep
-> Maybe (RefineException, RefineException) -> RefineException
RefineXorException (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (Xor l r)
p) (forall a. a -> Maybe a
Just (RefineException
l, RefineException
r)))
(Maybe RefineException, Maybe RefineException)
_ -> forall a. Maybe a
Nothing
data SizeLessThan (n :: Nat)
= SizeLessThan
deriving
( forall (n :: Nat) x. Rep (SizeLessThan n) x -> SizeLessThan n
forall (n :: Nat) x. SizeLessThan n -> Rep (SizeLessThan n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (SizeLessThan n) x -> SizeLessThan n
$cfrom :: forall (n :: Nat) x. SizeLessThan n -> Rep (SizeLessThan n) x
Generic
)
instance (Foldable t, KnownNat n) => Predicate (SizeLessThan n) (t a) where
validate :: Proxy (SizeLessThan n) -> t a -> Maybe RefineException
validate Proxy (SizeLessThan n)
p t a
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeLessThan n)
p (t a
x, Text
"Foldable") forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. Ord a => a -> a -> Bool
(<), Text
"less than")
instance (KnownNat n) => Predicate (SizeLessThan n) Text where
validate :: Proxy (SizeLessThan n) -> Text -> Maybe RefineException
validate Proxy (SizeLessThan n)
p Text
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeLessThan n)
p (Text
x, Text
"Text") Text -> Int
Text.length (forall a. Ord a => a -> a -> Bool
(<), Text
"less than")
instance (KnownNat n) => Predicate (SizeLessThan n) BS.ByteString where
validate :: Proxy (SizeLessThan n) -> ByteString -> Maybe RefineException
validate Proxy (SizeLessThan n)
p ByteString
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeLessThan n)
p (ByteString
x, Text
"ByteString") ByteString -> Int
BS.length (forall a. Ord a => a -> a -> Bool
(<), Text
"less than")
instance (KnownNat n) => Predicate (SizeLessThan n) BL.ByteString where
validate :: Proxy (SizeLessThan n) -> ByteString -> Maybe RefineException
validate Proxy (SizeLessThan n)
p ByteString
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeLessThan n)
p (ByteString
x, Text
"ByteString") (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int64
BL.length) (forall a. Ord a => a -> a -> Bool
(<), Text
"less than")
data SizeGreaterThan (n :: Nat)
= SizeGreaterThan
deriving
( forall (n :: Nat) x. Rep (SizeGreaterThan n) x -> SizeGreaterThan n
forall (n :: Nat) x. SizeGreaterThan n -> Rep (SizeGreaterThan n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (SizeGreaterThan n) x -> SizeGreaterThan n
$cfrom :: forall (n :: Nat) x. SizeGreaterThan n -> Rep (SizeGreaterThan n) x
Generic
)
instance (Foldable t, KnownNat n) => Predicate (SizeGreaterThan n) (t a) where
validate :: Proxy (SizeGreaterThan n) -> t a -> Maybe RefineException
validate Proxy (SizeGreaterThan n)
p t a
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeGreaterThan n)
p (t a
x, Text
"Foldable") forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. Ord a => a -> a -> Bool
(>), Text
"greater than")
instance (KnownNat n) => Predicate (SizeGreaterThan n) Text where
validate :: Proxy (SizeGreaterThan n) -> Text -> Maybe RefineException
validate Proxy (SizeGreaterThan n)
p Text
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeGreaterThan n)
p (Text
x, Text
"Text") Text -> Int
Text.length (forall a. Ord a => a -> a -> Bool
(>), Text
"greater than")
instance (KnownNat n) => Predicate (SizeGreaterThan n) BS.ByteString where
validate :: Proxy (SizeGreaterThan n) -> ByteString -> Maybe RefineException
validate Proxy (SizeGreaterThan n)
p ByteString
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeGreaterThan n)
p (ByteString
x, Text
"ByteString") ByteString -> Int
BS.length (forall a. Ord a => a -> a -> Bool
(>), Text
"greater than")
instance (KnownNat n) => Predicate (SizeGreaterThan n) BL.ByteString where
validate :: Proxy (SizeGreaterThan n) -> ByteString -> Maybe RefineException
validate Proxy (SizeGreaterThan n)
p ByteString
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeGreaterThan n)
p (ByteString
x, Text
"ByteString") (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int64
BL.length) (forall a. Ord a => a -> a -> Bool
(>), Text
"greater than")
data SizeEqualTo (n :: Nat)
= SizeEqualTo
deriving
( forall (n :: Nat) x. Rep (SizeEqualTo n) x -> SizeEqualTo n
forall (n :: Nat) x. SizeEqualTo n -> Rep (SizeEqualTo n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (SizeEqualTo n) x -> SizeEqualTo n
$cfrom :: forall (n :: Nat) x. SizeEqualTo n -> Rep (SizeEqualTo n) x
Generic
)
instance (Foldable t, KnownNat n) => Predicate (SizeEqualTo n) (t a) where
validate :: Proxy (SizeEqualTo n) -> t a -> Maybe RefineException
validate Proxy (SizeEqualTo n)
p t a
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeEqualTo n)
p (t a
x, Text
"Foldable") forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. Eq a => a -> a -> Bool
(==), Text
"equal to")
instance (KnownNat n) => Predicate (SizeEqualTo n) Text where
validate :: Proxy (SizeEqualTo n) -> Text -> Maybe RefineException
validate Proxy (SizeEqualTo n)
p Text
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeEqualTo n)
p (Text
x, Text
"Text") Text -> Int
Text.length (forall a. Eq a => a -> a -> Bool
(==), Text
"equal to")
instance (KnownNat n) => Predicate (SizeEqualTo n) BS.ByteString where
validate :: Proxy (SizeEqualTo n) -> ByteString -> Maybe RefineException
validate Proxy (SizeEqualTo n)
p ByteString
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeEqualTo n)
p (ByteString
x, Text
"ByteString") ByteString -> Int
BS.length (forall a. Eq a => a -> a -> Bool
(==), Text
"equal to")
instance (KnownNat n) => Predicate (SizeEqualTo n) BL.ByteString where
validate :: Proxy (SizeEqualTo n) -> ByteString -> Maybe RefineException
validate Proxy (SizeEqualTo n)
p ByteString
x = forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (SizeEqualTo n)
p (ByteString
x, Text
"ByteString") (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int64
BL.length) (forall a. Eq a => a -> a -> Bool
(==), Text
"equal to")
data Ascending
= Ascending
deriving
( forall x. Rep Ascending x -> Ascending
forall x. Ascending -> Rep Ascending x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Ascending x -> Ascending
$cfrom :: forall x. Ascending -> Rep Ascending x
Generic
)
instance (Foldable t, Ord a) => Predicate Ascending (t a) where
validate :: Proxy Ascending -> t a -> Maybe RefineException
validate Proxy Ascending
p t a
x = do
if forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> Bool
increasing t a
x
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy Ascending
p)
Text
"Foldable is not in ascending order."
data Descending
= Descending
deriving
( forall x. Rep Descending x -> Descending
forall x. Descending -> Rep Descending x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Descending x -> Descending
$cfrom :: forall x. Descending -> Rep Descending x
Generic
)
instance (Foldable t, Ord a) => Predicate Descending (t a) where
validate :: Proxy Descending -> t a -> Maybe RefineException
validate Proxy Descending
p t a
x = do
if forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> Bool
decreasing t a
x
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy Descending
p)
Text
"Foldable is not in descending order."
data LessThan (n :: Nat)
= LessThan
deriving
( forall (n :: Nat) x. Rep (LessThan n) x -> LessThan n
forall (n :: Nat) x. LessThan n -> Rep (LessThan n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (LessThan n) x -> LessThan n
$cfrom :: forall (n :: Nat) x. LessThan n -> Rep (LessThan n) x
Generic
)
instance (Ord x, Num x, KnownNat n) => Predicate (LessThan n) x where
validate :: Proxy (LessThan n) -> x -> Maybe RefineException
validate Proxy (LessThan n)
p x
x = do
let n :: Integer
n = forall (n :: Nat). KnownNat n => Integer
nv @n
let x' :: x
x' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
if x
x forall a. Ord a => a -> a -> Bool
< x
x'
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (LessThan n)
p)
(Text
"Value is not less than " forall a. Semigroup a => a -> a -> a
<> forall a. Integral a => a -> Text
i2text Integer
n)
data GreaterThan (n :: Nat)
= GreaterThan
deriving
( forall (n :: Nat) x. Rep (GreaterThan n) x -> GreaterThan n
forall (n :: Nat) x. GreaterThan n -> Rep (GreaterThan n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (GreaterThan n) x -> GreaterThan n
$cfrom :: forall (n :: Nat) x. GreaterThan n -> Rep (GreaterThan n) x
Generic
)
instance (Ord x, Num x, KnownNat n) => Predicate (GreaterThan n) x where
validate :: Proxy (GreaterThan n) -> x -> Maybe RefineException
validate Proxy (GreaterThan n)
p x
x = do
let n :: Integer
n = forall (n :: Nat). KnownNat n => Integer
nv @n
let x' :: x
x' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
if x
x forall a. Ord a => a -> a -> Bool
> x
x'
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (GreaterThan n)
p)
(Text
"Value is not greater than " forall a. Semigroup a => a -> a -> a
<> forall a. Integral a => a -> Text
i2text Integer
n)
data From (n :: Nat)
= From
deriving
( forall (n :: Nat) x. Rep (From n) x -> From n
forall (n :: Nat) x. From n -> Rep (From n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (From n) x -> From n
$cfrom :: forall (n :: Nat) x. From n -> Rep (From n) x
Generic
)
instance (Ord x, Num x, KnownNat n) => Predicate (From n) x where
validate :: Proxy (From n) -> x -> Maybe RefineException
validate Proxy (From n)
p x
x = do
let n :: Integer
n = forall (n :: Nat). KnownNat n => Integer
nv @n
let x' :: x
x' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
if x
x forall a. Ord a => a -> a -> Bool
>= x
x'
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (From n)
p)
(Text
"Value is less than " forall a. Semigroup a => a -> a -> a
<> forall a. Integral a => a -> Text
i2text Integer
n)
data To (n :: Nat)
= To
deriving
( forall (n :: Nat) x. Rep (To n) x -> To n
forall (n :: Nat) x. To n -> Rep (To n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (To n) x -> To n
$cfrom :: forall (n :: Nat) x. To n -> Rep (To n) x
Generic
)
instance (Ord x, Num x, KnownNat n) => Predicate (To n) x where
validate :: Proxy (To n) -> x -> Maybe RefineException
validate Proxy (To n)
p x
x = do
let n :: Integer
n = forall (n :: Nat). KnownNat n => Integer
nv @n
let x' :: x
x' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
if x
x forall a. Ord a => a -> a -> Bool
<= x
x'
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (To n)
p)
(Text
"Value is greater than " forall a. Semigroup a => a -> a -> a
<> forall a. Integral a => a -> Text
i2text Integer
n)
data FromTo (mn :: Nat) (mx :: Nat)
= FromTo
deriving
( forall (mn :: Nat) (mx :: Nat) x.
Rep (FromTo mn mx) x -> FromTo mn mx
forall (mn :: Nat) (mx :: Nat) x.
FromTo mn mx -> Rep (FromTo mn mx) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (mn :: Nat) (mx :: Nat) x.
Rep (FromTo mn mx) x -> FromTo mn mx
$cfrom :: forall (mn :: Nat) (mx :: Nat) x.
FromTo mn mx -> Rep (FromTo mn mx) x
Generic
)
instance ( Ord x, Num x, KnownNat mn, KnownNat mx, mn <= mx
) => Predicate (FromTo mn mx) x where
validate :: Proxy (FromTo mn mx) -> x -> Maybe RefineException
validate Proxy (FromTo mn mx)
p x
x = do
let mn' :: Integer
mn' = forall (n :: Nat). KnownNat n => Integer
nv @mn
let mx' :: Integer
mx' = forall (n :: Nat). KnownNat n => Integer
nv @mx
if x
x forall a. Ord a => a -> a -> Bool
>= forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
mn' Bool -> Bool -> Bool
&& x
x forall a. Ord a => a -> a -> Bool
<= forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
mx'
then forall a. Maybe a
Nothing
else
let msg :: Text
msg = [ Text
"Value is out of range (minimum: "
, forall a. Integral a => a -> Text
i2text Integer
mn'
, Text
", maximum: "
, forall a. Integral a => a -> Text
i2text Integer
mx'
, Text
")"
] forall a b. a -> (a -> b) -> b
|> forall a. Monoid a => [a] -> a
mconcat
in TypeRep -> Text -> Maybe RefineException
throwRefineOtherException (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (FromTo mn mx)
p) Text
msg
data EqualTo (n :: Nat)
= EqualTo
deriving
( forall (n :: Nat) x. Rep (EqualTo n) x -> EqualTo n
forall (n :: Nat) x. EqualTo n -> Rep (EqualTo n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (EqualTo n) x -> EqualTo n
$cfrom :: forall (n :: Nat) x. EqualTo n -> Rep (EqualTo n) x
Generic
)
instance (Eq x, Num x, KnownNat n) => Predicate (EqualTo n) x where
validate :: Proxy (EqualTo n) -> x -> Maybe RefineException
validate Proxy (EqualTo n)
p x
x = do
let n :: Integer
n = forall (n :: Nat). KnownNat n => Integer
nv @n
let x' :: x
x' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
if x
x forall a. Eq a => a -> a -> Bool
== x
x'
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (EqualTo n)
p)
(Text
"Value does not equal " forall a. Semigroup a => a -> a -> a
<> forall a. Integral a => a -> Text
i2text Integer
n)
data NotEqualTo (n :: Nat)
= NotEqualTo
deriving
( forall (n :: Nat) x. Rep (NotEqualTo n) x -> NotEqualTo n
forall (n :: Nat) x. NotEqualTo n -> Rep (NotEqualTo n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (NotEqualTo n) x -> NotEqualTo n
$cfrom :: forall (n :: Nat) x. NotEqualTo n -> Rep (NotEqualTo n) x
Generic
)
instance (Eq x, Num x, KnownNat n) => Predicate (NotEqualTo n) x where
validate :: Proxy (NotEqualTo n) -> x -> Maybe RefineException
validate Proxy (NotEqualTo n)
p x
x = do
let n :: Integer
n = forall (n :: Nat). KnownNat n => Integer
nv @n
let x' :: x
x' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
if x
x forall a. Eq a => a -> a -> Bool
/= x
x'
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (NotEqualTo n)
p)
(Text
"Value does equal " forall a. Semigroup a => a -> a -> a
<> forall a. Integral a => a -> Text
i2text Integer
n)
data NegativeFromTo (n :: Nat) (m :: Nat)
= NegativeFromTo
deriving
( forall (n :: Nat) (m :: Nat) x.
Rep (NegativeFromTo n m) x -> NegativeFromTo n m
forall (n :: Nat) (m :: Nat) x.
NegativeFromTo n m -> Rep (NegativeFromTo n m) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) (m :: Nat) x.
Rep (NegativeFromTo n m) x -> NegativeFromTo n m
$cfrom :: forall (n :: Nat) (m :: Nat) x.
NegativeFromTo n m -> Rep (NegativeFromTo n m) x
Generic
)
instance (Ord x, Num x, KnownNat n, KnownNat m) => Predicate (NegativeFromTo n m) x where
validate :: Proxy (NegativeFromTo n m) -> x -> Maybe RefineException
validate Proxy (NegativeFromTo n m)
p x
x = do
let n' :: Integer
n' = forall (n :: Nat). KnownNat n => Integer
nv @n
let m' :: Integer
m' = forall (n :: Nat). KnownNat n => Integer
nv @m
if x
x forall a. Ord a => a -> a -> Bool
>= forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Num a => a -> a
negate Integer
n') Bool -> Bool -> Bool
&& x
x forall a. Ord a => a -> a -> Bool
<= forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
m'
then forall a. Maybe a
Nothing
else
let msg :: Text
msg = [ Text
"Value is out of range (minimum: "
, forall a. Integral a => a -> Text
i2text (forall a. Num a => a -> a
negate Integer
n')
, Text
", maximum: "
, forall a. Integral a => a -> Text
i2text Integer
m'
, Text
")"
] forall a b. a -> (a -> b) -> b
|> forall a. Monoid a => [a] -> a
mconcat
in TypeRep -> Text -> Maybe RefineException
throwRefineOtherException (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (NegativeFromTo n m)
p) Text
msg
data DivisibleBy (n :: Nat)
= DivisibleBy
deriving
( forall (n :: Nat) x. Rep (DivisibleBy n) x -> DivisibleBy n
forall (n :: Nat) x. DivisibleBy n -> Rep (DivisibleBy n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (n :: Nat) x. Rep (DivisibleBy n) x -> DivisibleBy n
$cfrom :: forall (n :: Nat) x. DivisibleBy n -> Rep (DivisibleBy n) x
Generic
)
instance (Integral x, KnownNat n) => Predicate (DivisibleBy n) x where
validate :: Proxy (DivisibleBy n) -> x -> Maybe RefineException
validate Proxy (DivisibleBy n)
p x
x = do
let n :: Integer
n = forall (n :: Nat). KnownNat n => Integer
nv @n
let x' :: x
x' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
if x
x forall a. Integral a => a -> a -> a
`mod` x
x' forall a. Eq a => a -> a -> Bool
== x
0
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (DivisibleBy n)
p)
(Text
"Value is not divisible by " forall a. Semigroup a => a -> a -> a
<> forall a. Integral a => a -> Text
i2text Integer
n)
data Odd
= Odd
deriving
( forall x. Rep Odd x -> Odd
forall x. Odd -> Rep Odd x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Odd x -> Odd
$cfrom :: forall x. Odd -> Rep Odd x
Generic
)
instance (Integral x) => Predicate Odd x where
validate :: Proxy Odd -> x -> Maybe RefineException
validate Proxy Odd
p x
x = do
if forall a. Integral a => a -> Bool
odd x
x
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy Odd
p)
Text
"Value is not odd."
data NaN
= NaN
deriving
( forall x. Rep NaN x -> NaN
forall x. NaN -> Rep NaN x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep NaN x -> NaN
$cfrom :: forall x. NaN -> Rep NaN x
Generic
)
instance (RealFloat x) => Predicate NaN x where
validate :: Proxy NaN -> x -> Maybe RefineException
validate Proxy NaN
p x
x = do
if forall a. RealFloat a => a -> Bool
isNaN x
x
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy NaN
p)
Text
"Value is not IEEE \"not-a-number\" (NaN)."
data Infinite
= Infinite
deriving
( forall x. Rep Infinite x -> Infinite
forall x. Infinite -> Rep Infinite x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Infinite x -> Infinite
$cfrom :: forall x. Infinite -> Rep Infinite x
Generic
)
instance (RealFloat x) => Predicate Infinite x where
validate :: Proxy Infinite -> x -> Maybe RefineException
validate Proxy Infinite
p x
x = do
if forall a. RealFloat a => a -> Bool
isInfinite x
x
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy Infinite
p)
Text
"Value is not IEEE infinity or negative infinity."
data Even
= Even
deriving
( forall x. Rep Even x -> Even
forall x. Even -> Rep Even x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Even x -> Even
$cfrom :: forall x. Even -> Rep Even x
Generic
)
instance (Integral x) => Predicate Even x where
validate :: Proxy Even -> x -> Maybe RefineException
validate Proxy Even
p x
x = do
if forall a. Integral a => a -> Bool
even x
x
then forall a. Maybe a
Nothing
else TypeRep -> Text -> Maybe RefineException
throwRefineOtherException
(forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy Even
p)
Text
"Value is not even."
type Positive = GreaterThan 0
type NonPositive = To 0
type Negative = LessThan 0
type NonNegative = From 0
type ZeroToOne = FromTo 0 1
type NonZero = NotEqualTo 0
type Empty = SizeEqualTo 0
type NonEmpty = SizeGreaterThan 0
class Weaken from to where
weaken :: Refined from x -> Refined to x
weaken = coerce :: forall a b. Coercible a b => a -> b
coerce
instance (n <= m) => Weaken (LessThan n) (LessThan m)
instance (n <= m) => Weaken (LessThan n) (To m)
instance (n <= m) => Weaken (To n) (To m)
instance (m <= n) => Weaken (GreaterThan n) (GreaterThan m)
instance (m <= n) => Weaken (GreaterThan n) (From m)
instance (m <= n) => Weaken (From n) (From m)
instance (p <= n, m <= q) => Weaken (FromTo n m) (FromTo p q)
instance (p <= n) => Weaken (FromTo n m) (From p)
instance (m <= q) => Weaken (FromTo n m) (To q)
instance (n <= m) => Weaken (SizeLessThan n) (SizeLessThan m)
instance (m <= n) => Weaken (SizeGreaterThan n) (SizeGreaterThan m)
andLeft :: Refined (And l r) x -> Refined l x
andLeft :: forall {k} {k} (l :: k) (r :: k) x.
Refined (And l r) x -> Refined l x
andLeft = coerce :: forall a b. Coercible a b => a -> b
coerce
andRight :: Refined (And l r) x -> Refined r x
andRight :: forall {k} {k} (l :: k) (r :: k) x.
Refined (And l r) x -> Refined r x
andRight = coerce :: forall a b. Coercible a b => a -> b
coerce
leftOr :: Refined l x -> Refined (Or l r) x
leftOr :: forall {k} {k} (l :: k) x (r :: k).
Refined l x -> Refined (Or l r) x
leftOr = coerce :: forall a b. Coercible a b => a -> b
coerce
rightOr :: Refined r x -> Refined (Or l r) x
rightOr :: forall {k} {k} (r :: k) x (l :: k).
Refined r x -> Refined (Or l r) x
rightOr = coerce :: forall a b. Coercible a b => a -> b
coerce
weakenAndLeft :: Weaken from to => Refined (And from x) a -> Refined (And to x) a
weakenAndLeft :: forall {k} {k} {k} (from :: k) (to :: k) (x :: k) a.
Weaken from to =>
Refined (And from x) a -> Refined (And to x) a
weakenAndLeft = coerce :: forall a b. Coercible a b => a -> b
coerce
weakenAndRight :: Weaken from to => Refined (And x from) a -> Refined (And x to) a
weakenAndRight :: forall {k} {k} {k} (from :: k) (to :: k) (x :: k) a.
Weaken from to =>
Refined (And x from) a -> Refined (And x to) a
weakenAndRight = coerce :: forall a b. Coercible a b => a -> b
coerce
weakenOrLeft :: Weaken from to => Refined (And from x) a -> Refined (And to x) a
weakenOrLeft :: forall {k} {k} {k} (from :: k) (to :: k) (x :: k) a.
Weaken from to =>
Refined (And from x) a -> Refined (And to x) a
weakenOrLeft = coerce :: forall a b. Coercible a b => a -> b
coerce
weakenOrRight :: Weaken from to => Refined (And x from) a -> Refined (And x to) a
weakenOrRight :: forall {k} {k} {k} (from :: k) (to :: k) (x :: k) a.
Weaken from to =>
Refined (And x from) a -> Refined (And x to) a
weakenOrRight = coerce :: forall a b. Coercible a b => a -> b
coerce
strengthen :: forall p p' x. (Predicate p x, Predicate p' x)
=> Refined p x
-> Either RefineException (Refined (p && p') x)
strengthen :: forall {k} {k} (p :: k) (p' :: k) x.
(Predicate p x, Predicate p' x) =>
Refined p x -> Either RefineException (Refined (p && p') x)
strengthen Refined p x
r = do
Refined x
x <- forall {k} (p :: k) x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine @p' @x (forall {k} (p :: k) x. Refined p x -> x
unrefine Refined p x
r)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall k (p :: k) x. x -> Refined p x
Refined x
x)
{-# inlineable strengthen #-}
data RefineException
=
RefineNotException
{ RefineException -> TypeRep
_RefineException_typeRep :: !TypeRep
}
|
RefineAndException
{ _RefineException_typeRep :: !TypeRep
, RefineException -> These RefineException RefineException
_RefineException_andChild :: !(These RefineException RefineException)
}
|
RefineOrException
{ _RefineException_typeRep :: !TypeRep
, RefineException -> RefineException
_RefineException_orLChild :: !RefineException
, RefineException -> RefineException
_RefineException_orRChild :: !RefineException
}
|
RefineXorException
{ _RefineException_typeRep :: !TypeRep
, RefineException -> Maybe (RefineException, RefineException)
_RefineException_children :: !(Maybe (RefineException, RefineException))
}
|
RefineSomeException
{ _RefineException_typeRep :: !TypeRep
, RefineException -> SomeException
_RefineException_Exception :: !SomeException
}
|
RefineOtherException
{ _RefineException_typeRep :: !TypeRep
, RefineException -> Text
_RefineException_message :: !Text
}
deriving
( forall x. Rep RefineException x -> RefineException
forall x. RefineException -> Rep RefineException x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RefineException x -> RefineException
$cfrom :: forall x. RefineException -> Rep RefineException x
Generic
)
instance Show RefineException where
show :: RefineException -> String
show = RefineException -> String
displayRefineException
data ExceptionTree a
= NodeNone
| NodeSome !TypeRep SomeException
| NodeOther !TypeRep !Text
| NodeNot !TypeRep
| NodeOr !TypeRep [ExceptionTree a]
| NodeAnd !TypeRep [ExceptionTree a]
| NodeXor !TypeRep [ExceptionTree a]
showTree :: Bool -> ExceptionTree RefineException -> String
showTree :: Bool -> ExceptionTree RefineException -> String
showTree Bool
inGhc
| Bool
inGhc = String
-> String -> String -> ExceptionTree RefineException -> [String]
showOne String
"" String
"" String
""
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a. (a -> a) -> [a] -> [a]
mapOnTail (Int -> ShowS
indent Int
6)
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> [String] -> String
unlines
| Bool
otherwise = String
-> String -> String -> ExceptionTree RefineException -> [String]
showOne String
" " String
"" String
"" forall a b c. (a -> b) -> (b -> c) -> a -> c
.> [String] -> String
unlines
where
mapOnTail :: (a -> a) -> [a] -> [a]
mapOnTail :: forall a. (a -> a) -> [a] -> [a]
mapOnTail a -> a
f = \case
[] -> []
(a
a : [a]
as) -> a
a forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map a -> a
f [a]
as
indent :: Int -> String -> String
indent :: Int -> ShowS
indent Int
n String
s = forall a. Int -> a -> [a]
replicate Int
n Char
' ' forall a. [a] -> [a] -> [a]
++ String
s
showOne :: String -> String -> String -> ExceptionTree RefineException -> [String]
showOne :: String
-> String -> String -> ExceptionTree RefineException -> [String]
showOne String
leader String
tie String
arm = \case
ExceptionTree RefineException
NodeNone ->
[
]
NodeSome TypeRep
tr SomeException
e ->
[ String
leader
forall a. Semigroup a => a -> a -> a
<> String
arm
forall a. Semigroup a => a -> a -> a
<> String
tie
forall a. Semigroup a => a -> a -> a
<> String
"The predicate ("
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show TypeRep
tr
forall a. Semigroup a => a -> a -> a
<> String
") failed with the exception: "
forall a. Semigroup a => a -> a -> a
<> forall e. Exception e => e -> String
displayException SomeException
e
]
NodeOther TypeRep
tr Text
p ->
[ String
leader
forall a. Semigroup a => a -> a -> a
<> String
arm
forall a. Semigroup a => a -> a -> a
<> String
tie
forall a. Semigroup a => a -> a -> a
<> String
"The predicate ("
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show TypeRep
tr
forall a. Semigroup a => a -> a -> a
<> String
") failed with the message: "
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
p
]
NodeNot TypeRep
tr ->
[ String
leader
forall a. Semigroup a => a -> a -> a
<> String
arm
forall a. Semigroup a => a -> a -> a
<> String
tie
forall a. Semigroup a => a -> a -> a
<> String
"The predicate ("
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show TypeRep
tr
forall a. Semigroup a => a -> a -> a
<> String
") does not hold"
]
NodeOr TypeRep
tr [ExceptionTree RefineException]
rest -> TypeRep -> String
nodeRep TypeRep
tr forall a. a -> [a] -> [a]
: [ExceptionTree RefineException] -> String -> [String]
showChildren [ExceptionTree RefineException]
rest (String
leader forall a. Semigroup a => a -> a -> a
<> String
extension)
NodeAnd TypeRep
tr [ExceptionTree RefineException]
rest -> TypeRep -> String
nodeRep TypeRep
tr forall a. a -> [a] -> [a]
: [ExceptionTree RefineException] -> String -> [String]
showChildren [ExceptionTree RefineException]
rest (String
leader forall a. Semigroup a => a -> a -> a
<> String
extension)
NodeXor TypeRep
tr [] ->
[ String
leader
forall a. Semigroup a => a -> a -> a
<> String
arm
forall a. Semigroup a => a -> a -> a
<> String
tie
forall a. Semigroup a => a -> a -> a
<> String
"The predicate ("
forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show TypeRep
tr
forall a. Semigroup a => a -> a -> a
<> String
") does not hold, because both predicates were satisfied"
]
NodeXor TypeRep
tr [ExceptionTree RefineException]
rest -> TypeRep -> String
nodeRep TypeRep
tr forall a. a -> [a] -> [a]
: [ExceptionTree RefineException] -> String -> [String]
showChildren [ExceptionTree RefineException]
rest (String
leader forall a. Semigroup a => a -> a -> a
<> String
extension)
where
nodeRep :: TypeRep -> String
nodeRep :: TypeRep -> String
nodeRep TypeRep
tr = String
leader forall a. Semigroup a => a -> a -> a
<> String
arm forall a. Semigroup a => a -> a -> a
<> String
tie forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show TypeRep
tr
extension :: String
extension :: String
extension = case String
arm of
String
"" -> String
""
String
"└" -> String
" "
String
_ -> String
"│ "
showChildren :: [ExceptionTree RefineException] -> String -> [String]
showChildren :: [ExceptionTree RefineException] -> String -> [String]
showChildren [ExceptionTree RefineException]
children String
leader =
let arms :: [String]
arms = forall a. Int -> a -> [a]
replicate (forall (t :: * -> *) a. Foldable t => t a -> Int
length [ExceptionTree RefineException]
children forall a. Num a => a -> a -> a
- Int
1) String
"├" forall a. Semigroup a => a -> a -> a
<> [String
"└"]
in forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (String
-> String -> String -> ExceptionTree RefineException -> [String]
showOne String
leader String
"── ") [String]
arms [ExceptionTree RefineException]
children)
refineExceptionToTree :: RefineException -> ExceptionTree RefineException
refineExceptionToTree :: RefineException -> ExceptionTree RefineException
refineExceptionToTree = forall {k} {a :: k}. RefineException -> ExceptionTree a
go
where
go :: RefineException -> ExceptionTree a
go = \case
RefineSomeException TypeRep
tr SomeException
e -> forall {k} (a :: k). TypeRep -> SomeException -> ExceptionTree a
NodeSome TypeRep
tr SomeException
e
RefineOtherException TypeRep
tr Text
p -> forall {k} (a :: k). TypeRep -> Text -> ExceptionTree a
NodeOther TypeRep
tr Text
p
RefineNotException TypeRep
tr -> forall {k} (a :: k). TypeRep -> ExceptionTree a
NodeNot TypeRep
tr
RefineOrException TypeRep
tr RefineException
l RefineException
r -> forall {k} (a :: k).
TypeRep -> [ExceptionTree a] -> ExceptionTree a
NodeOr TypeRep
tr [RefineException -> ExceptionTree a
go RefineException
l, RefineException -> ExceptionTree a
go RefineException
r]
RefineAndException TypeRep
tr (This RefineException
l) -> forall {k} (a :: k).
TypeRep -> [ExceptionTree a] -> ExceptionTree a
NodeAnd TypeRep
tr [RefineException -> ExceptionTree a
go RefineException
l]
RefineAndException TypeRep
tr (That RefineException
r) -> forall {k} (a :: k).
TypeRep -> [ExceptionTree a] -> ExceptionTree a
NodeAnd TypeRep
tr [RefineException -> ExceptionTree a
go RefineException
r]
RefineAndException TypeRep
tr (These RefineException
l RefineException
r) -> forall {k} (a :: k).
TypeRep -> [ExceptionTree a] -> ExceptionTree a
NodeAnd TypeRep
tr [RefineException -> ExceptionTree a
go RefineException
l, RefineException -> ExceptionTree a
go RefineException
r]
RefineXorException TypeRep
tr Maybe (RefineException, RefineException)
Nothing -> forall {k} (a :: k).
TypeRep -> [ExceptionTree a] -> ExceptionTree a
NodeXor TypeRep
tr []
RefineXorException TypeRep
tr (Just (RefineException
l, RefineException
r)) -> forall {k} (a :: k).
TypeRep -> [ExceptionTree a] -> ExceptionTree a
NodeXor TypeRep
tr [RefineException -> ExceptionTree a
go RefineException
l, RefineException -> ExceptionTree a
go RefineException
r]
displayRefineException :: RefineException -> String
displayRefineException :: RefineException -> String
displayRefineException = RefineException -> ExceptionTree RefineException
refineExceptionToTree forall a b c. (a -> b) -> (b -> c) -> a -> c
.> Bool -> ExceptionTree RefineException -> String
showTree Bool
False
instance Exception RefineException where
displayException :: RefineException -> String
displayException = forall a. Show a => a -> String
show
throwRefineOtherException
:: TypeRep
-> Text
-> Maybe RefineException
throwRefineOtherException :: TypeRep -> Text -> Maybe RefineException
throwRefineOtherException TypeRep
rep
= TypeRep -> Text -> RefineException
RefineOtherException TypeRep
rep forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a. a -> Maybe a
Just
throwRefineSomeException
:: TypeRep
-> SomeException
-> Maybe RefineException
throwRefineSomeException :: TypeRep -> SomeException -> Maybe RefineException
throwRefineSomeException TypeRep
rep
= TypeRep -> SomeException -> RefineException
RefineSomeException TypeRep
rep forall a b c. (a -> b) -> (b -> c) -> a -> c
.> forall a. a -> Maybe a
Just
success
:: Maybe RefineException
success :: Maybe RefineException
success
= forall a. Maybe a
Nothing
sized :: forall p n a. (Typeable (p n), KnownNat n)
=> Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized :: forall {k} (p :: Nat -> k) (n :: Nat) a.
(Typeable (p n), KnownNat n) =>
Proxy (p n)
-> (a, Text)
-> (a -> Int)
-> (Int -> Int -> Bool, Text)
-> Maybe RefineException
sized Proxy (p n)
p (a
x, Text
typ) a -> Int
lenF (Int -> Int -> Bool
cmp, Text
cmpDesc) = do
let x' :: Int
x' = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall (n :: Nat). KnownNat n => Integer
nv @n)
let sz :: Int
sz = a -> Int
lenF a
x
if Int -> Int -> Bool
cmp Int
sz Int
x'
then forall a. Maybe a
Nothing
else
let msg :: Text
msg =
[ Text
"Size of ", Text
typ, Text
" is not ", Text
cmpDesc, Text
" "
, forall a. Integral a => a -> Text
i2text Int
x'
, Text
". "
, Text
"Size is: "
, forall a. Integral a => a -> Text
i2text Int
sz
] forall a b. a -> (a -> b) -> b
|> forall a. Monoid a => [a] -> a
mconcat
in TypeRep -> Text -> Maybe RefineException
throwRefineOtherException (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep Proxy (p n)
p) Text
msg
nv :: forall n. KnownNat n => Integer
nv :: forall (n :: Nat). KnownNat n => Integer
nv = forall (n :: Nat). KnownNat n => Proxy# n -> Integer
natVal' (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# n)
i2text :: Integral a => a -> Text
i2text :: forall a. Integral a => a -> Text
i2text = forall a. Integral a => a -> Builder
TextBuilder.decimal
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> Builder -> Text
TextBuilder.toLazyText
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> Text -> Text
TextLazy.toStrict
{-# SPECIALISE i2text :: Int -> Text #-}
{-# SPECIALISE i2text :: Integer -> Text #-}