{-# LANGUAGE RankNTypes #-}
module Control.Exception.Assert.Sugar
( assert, blame, showFailure, swith, allB
, twith, failure, forceEither
) where
import Control.Exception (assert)
import Data.Text (Text)
import Debug.Trace (trace)
import Prelude
import qualified Text.Show.Pretty as Show.Pretty (ppShow)
infix 1 `blame`
blame :: Show v => Bool -> v -> Bool
{-# NOINLINE blame #-}
blame :: Bool -> v -> Bool
blame Bool
True v
_ = Bool
True
blame Bool
False v
blamed = String -> Bool -> Bool
forall a. String -> a -> a
trace (v -> String
forall v. Show v => v -> String
blameMessage v
blamed) Bool
False
blameMessage :: Show v => v -> String
blameMessage :: v -> String
blameMessage v
blamed = String
"Contract failed and the following is to blame:\n "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ v -> String
forall v. Show v => v -> String
Show.Pretty.ppShow v
blamed
infix 2 `showFailure`
showFailure :: Show v => String -> v -> String
{-# NOINLINE showFailure #-}
showFailure :: String -> v -> String
showFailure String
s v
blamed =
String
"Internal failure occurred and the following is to blame:\n "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ v -> String
forall v. Show v => v -> String
Show.Pretty.ppShow v
blamed
infix 2 `swith`
swith :: String -> v -> (String, v)
{-# INLINE swith #-}
swith :: String -> v -> (String, v)
swith String
s v
blamed = (String
s, v
blamed)
allB :: Show v => (v -> Bool) -> [v] -> Bool
{-# NOINLINE allB #-}
allB :: (v -> Bool) -> [v] -> Bool
allB v -> Bool
predicate [v]
l = case (v -> Bool) -> [v] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all v -> Bool
predicate [v]
l of
Bool
True -> Bool
True
Bool
False -> String -> Bool -> Bool
forall a. String -> a -> a
trace ((v -> Bool) -> [v] -> String
forall v. Show v => (v -> Bool) -> [v] -> String
allBMessage v -> Bool
predicate [v]
l) Bool
False
allBMessage :: Show v => (v -> Bool) -> [v] -> String
allBMessage :: (v -> Bool) -> [v] -> String
allBMessage v -> Bool
predicate [v]
l =
String
"The following items on the list don't respect the contract:\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [v] -> String
forall v. Show v => v -> String
Show.Pretty.ppShow ((v -> Bool) -> [v] -> [v]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (v -> Bool) -> v -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> Bool
predicate) [v]
l)
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nout of all the list items below:\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [v] -> String
forall v. Show v => v -> String
Show.Pretty.ppShow [v]
l
infix 2 `twith`
{-# DEPRECATED twith
"consider using 'swith' instead, for simplicity, because GHC optimizes lazy 'String' constants very well." #-}
twith :: Text -> b -> (Text, b)
{-# INLINE twith #-}
twith :: Text -> b -> (Text, b)
twith Text
t b
b = (Text
t, b
b)
infix 1 `failure`
{-# DEPRECATED failure
"use 'error' and 'showFailure' instead, now that 'error' prints source positions." #-}
failure :: Show a => (forall x. Bool -> x -> x) -> a -> b
{-# NOINLINE failure #-}
failure :: (forall x. Bool -> x -> x) -> a -> b
failure forall x. Bool -> x -> x
asrt a
blamed =
let s :: String
s = String
"Internal failure occurred and the following is to blame:\n "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall v. Show v => v -> String
Show.Pretty.ppShow a
blamed
in String -> b -> b
forall a. String -> a -> a
trace String
s
(b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ Bool -> b -> b
forall x. Bool -> x -> x
asrt Bool
False
(b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ String -> b
forall a. HasCallStack => String -> a
error String
"Control.Exception.Assert.Sugar.failure"
infix 1 `forceEither`
forceEither :: Show a => (forall x. Bool -> x -> x) -> Either a b -> b
{-# DEPRECATED forceEither
"use 'either (error . show) id' instead, now that 'error' prints source positions." #-}
{-# NOINLINE forceEither #-}
forceEither :: (forall x. Bool -> x -> x) -> Either a b -> b
forceEither forall x. Bool -> x -> x
asrt (Left a
a) = forall x. Bool -> x -> x
asrt (forall x. Bool -> x -> x) -> a -> b
forall a b. Show a => (forall x. Bool -> x -> x) -> a -> b
`failure` a
a
forceEither forall x. Bool -> x -> x
_ (Right b
b) = b
b