Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The difference</h2> <p>The difference between assert() and assume() is that </p> <ul> <li>assert() is a way to document and dynamically check invariants, while </li> <li>assume() is intended to be consumed by static analysis tools</li> </ul> <p>The intended consumer / context of assert() is testing, such as a Scala JUnit test, while that of assume() is "as a means of design-by-contract style specification of pre- and post-conditions on functions, with the intention that these specifications could be consumed by a static analysis tool" (excerpted from the <a href="http://www.scala-lang.org/api/current/index.html#scala.Predef%24">scaladoc</a>).</p> <h2>Static analysis / model checking</h2> <p>In the context of static analysis, as Adam Zalcman has pointed out, assert() is an all-execution-paths assertion, to check a global invariant, while assume() works locally to pare down the amount of checking that the analyzer needs to do. assume() is used in the context of assume-guarantee reasoning, which is a divide and conquer mechanism to help model checkers assume something about the method so as to tackle the state explosion problem that arises when one attempts to check all paths that the program may take. For example, if you knew that in the design of your program, a function f1(n1 : Int, n2:Int) is NEVER passed n2 &lt; n1, then stating this assumption explicitly would help the analyzer not have to check a LOT of combinations of n1 and n2.</p> <h2>In practice</h2> <p>In practice, since such whole-program model checkers are still mostly theory, let's look at what the scala compiler and interpreter does:</p> <ol> <li>both assume() and assert() expressions are checked at runtime</li> <li>-Xdisable-assertions disables both assume() and assert() checking</li> </ol> <h2>More</h2> <p>More from the excellent scaladoc on this topic:</p> <p><strong>Assertions</strong></p> <p>A set of <code>assert</code> functions are provided for use as a way to document and dynamically check invariants in code. assert statements can be elided at runtime by providing the command line argument <code>-Xdisable-assertions</code> to the <code>scala</code> command.</p> <p>Variants of <code>assert</code> intended for use with static analysis tools are also provided: <code>assume</code>, <code>require</code> and <code>ensuring</code>. <code>require</code> and ensuring are intended for use as a means of design-by-contract style specification of pre- and post-conditions on functions, with the intention that these specifications could be consumed by a static analysis tool. For instance,</p> <pre><code>def addNaturals(nats: List[Int]): Int = { require(nats forall (_ &gt;= 0), "List contains negative numbers") nats.foldLeft(0)(_ + _) } ensuring(_ &gt;= 0) </code></pre> <p>The declaration of addNaturals states that the list of integers passed should only contain natural numbers (i.e. non-negative), and that the result returned will also be natural. require is distinct from assert in that if the condition fails, then the caller of the function is to blame rather than a logical error having been made within addNaturals itself. ensures is a form of assert that declares the guarantee the function is providing with regards to it's return value. )</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload