Additional assert function: assert :: Bool -> String -> a -> a
Currently, we have an assert function in GHC.Base:
-- Assertion function. This simply ignores its boolean argument. -- The compiler may rewrite it to @('assertError' line)@. -- | 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. -- SLPJ: in 5.04 etc 'assert' is in GHC.Prim, -- but from Template Haskell onwards it's simply -- defined here in Base.lhs assert :: Bool -> a -> a assert _pred r = r
This get's rewritten by the type checker to assertError:
Note [Adding the implicit parameter to 'assert'] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The typechecker transforms (assert e1 e2) to (assertError "Foo.hs:27" e1 e2). This isn't really the Right Thing because there's no way to "undo" if you want to see the original source code in the typechecker output. We'll have fix this in due course, when we care more about being able to reconstruct the exact original program.
I would like to propose an additional one:
assertStr :: Bool -> String -> a -> a assertStr _pred msg r = r
NOTE: what is a name that is more consistent with the code base?
That would get rewritten as:
assertError ("Foo.hs:27 - " ++ show msg) e1 e2
This makes assertions a bit more meaningful when reading the error message, and they can replace the existing ASSERT2 macros throughout the code base.
|Component||Compiler (Type checker)|