[GHC] #9794: Additional assert function: assert :: Bool -> String -> a -> a

GHC ghc-devs at haskell.org
Wed Nov 12 12:14:12 UTC 2014


#9794: Additional assert function:  assert :: Bool -> String -> a -> a
-------------------------------------+-------------------------------------
       Reporter:  rodlogic           |                   Owner:
           Type:  feature request    |                  Status:  new
       Priority:  normal             |               Milestone:
      Component:  Compiler (Type     |                 Version:  7.8.3
  checker)                           |        Operating System:
       Keywords:                     |  Unknown/Multiple
   Architecture:  Unknown/Multiple   |         Type of failure:
     Difficulty:  Unknown            |  None/Unknown
     Blocked By:                     |               Test Case:
Related Tickets:                     |                Blocking:
                                     |  Differential Revisions:
-------------------------------------+-------------------------------------
 Currently, we have an assert function in GHC.Base:

 {{{#!hs
 -- 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:
 {{{#!hs
 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:
 {{{#!hs
 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:
 {{{#!hs
 assertError ("Foo.hs:27 - " ++ show msg) e1 e2
 }}}

 Why?

 This makes assertions a bit more meaningful when reading the error
 message, and they can replace the existing ASSERT2 macros throughout the
 code base.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/9794>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list