assert-failure-0.1.2.5: Syntactic sugar improving 'assert' and 'error'

Control.Exception.Assert.Sugar

Contents

Description

Syntactic sugar that improves the usability of assert and error. The original assert function is here re-exported for convenience.

Make sure to enable assertions for your cabal package, e.g., by setting

ghc-options: -fno-ignore-asserts

in your .cabal file. Otherwise, some of the functions will have no effect at all.

Synopsis

# Documentation

assert :: Bool -> a -> a #

If the first argument evaluates to True, then the result is the second argument. Otherwise an AssertionFailed exception is raised, containing a String with the source file and line number of the call to assert.

Assertions can normally be turned on or off with a compiler flag (for GHC, assertions are normally on unless optimisation is turned on with -O or the -fignore-asserts option is given). When assertions are turned off, the first argument to assert is ignored, and the second argument is returned as the result.

blame :: Show a => Bool -> a -> Bool infix 1 Source #

If the condition fails, display the value blamed for the failure. Used as in

assert (age < 120 blame age) $savings / (120 - age) showFailure :: Show v => String -> v -> String infix 2 Source # A helper function for error. To be used as in case xs of 0 : _ -> error$ "insignificant zero" showFailure xs

Fixing the first argument to String instead of anything Showable prevents warnings about defaulting, even when OverloadedStrings extension is enabled.

swith :: String -> v -> (String, v) infix 2 Source #

Syntactic sugar for the pair operation, to be used for blame as in

assert (age < 120 blame "age too high" swith age) $savings / (120 - age) Fixing the first component of the pair to String prevents warnings about defaulting, even when OverloadedStrings extension is enabled. allB :: Show a => (a -> Bool) -> [a] -> Bool Source # Like all, but if the predicate fails, blame all the list elements and especially those for which it fails. To be used as in assert (allB (<= height) [yf, y1, y2]) # DEPRECATED twith :: Text -> b -> (Text, b) infix 2 Source # Deprecated: consider using swith instead, for simplicity, because GHC optimizes lazy String constants very well. Syntactic sugar for the pair operation, to be used for blame as in assert (age < 120 blame "age too high" twith age)$ savings / (120 - age)

Fixing the first component of the pair to Text prevents warnings about defaulting, even when OverloadedStrings extension is enabled.

failure :: Show a => (forall x. Bool -> x -> x) -> a -> b infix 1 Source #

Deprecated: use error and showFailure instead, now that error prints source positions.

Like error, but shows the source position (in newer GHCs error shows source position as well, hence deprecation) and also the value to blame for the failure. To be used as in

case xs of
0 : _ -> assert failure (xs, "has an insignificant zero")

forceEither :: Show a => (forall x. Bool -> x -> x) -> Either a b -> b infix 1 Source #

Deprecated: use 'either (error . show) id' instead, now that error prints source positions.

Assuming that Left signifies an error condition, check the Either value and, if Left is encountered, fail outright and show the error message (in newer GHCs error shows source position as well, hence deprecation). Used as in

assert forceEither parseOrFailWithMessage code