Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is assert used?

Tags:

haskell

I'm having trouble understanding how to use the assert function (Control.Exception.Assert)

I did read the documentation (http://hackage.haskell.org/package/assert-0.0.1.2/docs/Control-Exception-Assert.html) but I still don't seem to understand how it's used. The currying really doesn't help in this case because of how unexplicit it is. Examples and explanation of what it's for would be lovely.

(For context, I am trying to figure out how to use assert in this code to make sure n is always non-negative) [No answers please, I'd like to figure it out on my own]

power :: Int -> Int -> Int
power x n =
  if n == 0 then
    1
  else
    x * power x (n - 1)
like image 296
HayashiEsme Avatar asked Sep 07 '18 17:09

HayashiEsme


People also ask

When should I use assert?

You can use an assert to check if your logical assumption is correct. You can also use assert statements to check if the control flow is correct or not. For example, if you have a function that returns a value, you may want to put an assert statement.

How assert is used in Java?

assert is a Java keyword used to define an assert statement. An assert statement is used to declare an expected boolean condition in a program. If the program is running with assertions enabled, then the condition is checked at runtime. If the condition is false, the Java runtime system throws an AssertionError .

Why do we use assert in testing?

Assertions are used for validating a test case and helps us understand if a test case has passed or failed. The assertion is considered to be met if the actual result of an application matches with that of the expected result.

When should I use assert in C++?

Assertion should be used for anticipating error in the way a programmer is using an API/function/class/whatever. These bugs need to be fixed quickly at debug time.


1 Answers

The assert exported from that library (which I haven't used) is actually defined in base here. You could use it like this:

power :: Int -> Int -> Int
power x n = assert (n >= 0) $
  if n == 0 then
    1
  else
    x * power x (n - 1)

But this isn't really what the function is meant for. It would be better in the case above to either raise your own error with a friendly message, or (better yet), return a Maybe Int.

assert is intended for checking internal invariants in your functions, where a violation indicates a bug. It can be really useful to use assertions in tandem with tests (tests excercise invariants which are checked by internal assert calls).

You have to make sure to compile either without optimizations or with -fno-ignore-asserts though, as assertions are optimized away when compiling with optimizations (another great feature of assert).

I've taken to including the following in the library code in order to have a test that assertions are on in my testsuite (very important, and an issue I've run into before):

assertionCanary :: IO Bool
assertionCanary = do
    assertionsWorking <- try $ assert False $ return ()
    return $
      case assertionsWorking of
           Left (AssertionFailed _) -> True
           _                        -> False
like image 162
jberryman Avatar answered Sep 27 '22 21:09

jberryman