assert-failure-0.1.2.3: Syntactic sugar improving 'assert' and 'error'
Safe HaskellNone
LanguageHaskell2010

Control.Exception.Assert.Sugar

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
  • assert :: Bool -> a -> a
  • blame :: Show a => Bool -> a -> Bool
  • showFailure :: Show v => String -> v -> String
  • swith :: String -> v -> (String, v)
  • allB :: Show a => (a -> Bool) -> [a] -> Bool
  • failure :: Show a => (forall x. Bool -> x -> x) -> a -> b
  • twith :: Text -> b -> (Text, b)
  • forceEither :: Show a => (forall x. Bool -> x -> x) -> Either a b -> b

Documentation

assert :: Bool -> a -> a #

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])

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")

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.

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