module Game.LambdaHack.Utils.Assert
( assert, blame, failure, allB, checkM, trueM, falseM
) where
import Control.Exception (assert)
import Debug.Trace (trace)
infix 1 `blame`
blame :: Show a => Bool -> a -> Bool
blame condition blamed
| condition = True
| otherwise =
let s = "Contract failed and the following is to blame:\n" ++
" " ++ show blamed
in trace s False
infix 1 `failure`
failure :: Show a => (Bool -> b -> b) -> a -> b
failure asrt blamed =
let s = "Internal failure occured and the following is to blame:\n" ++
" " ++ show blamed
in trace s $
asrt False
(error "Assert.failure: no error location (upgrade to GHC >= 7.4)")
allB :: Show a => (a -> Bool) -> [a] -> Bool
allB predicate l =
let s = show (filter (not . predicate) l) ++ " in the context of " ++ show l
in blame (all predicate l) s
checkM :: (Show a, Monad m) =>
(Bool -> m () -> m ()) -> (c -> Bool) -> a -> c -> m ()
checkM asrt predicate blamed value
| predicate value = return ()
| otherwise =
let s = "The returned value is wrong and the following is to blame:\n" ++
" " ++ show blamed
in trace s $
asrt False
(error "Assert.checkM: no error location (upgrade to GHC >= 7.4)")
trueM, falseM :: (Show a, Monad m) => (Bool -> m () -> m ()) -> a -> Bool
-> m ()
trueM asrt = checkM asrt id
falseM asrt = checkM asrt not