TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Assertions are pessimistic, assumptions are optimistic (2014)

56 pointsby pantalaimonabout 2 years ago

7 comments

Slixabout 2 years ago
Rust has unreachable_unchecked and intrinsics::assume. And a clever crate that provides an assume!() macro for stating assumptions.<p>I toyed with them while learning some unsafe Rust by calling unsafe Windows APIs. Sometimes there were obvious relationships between function parameters and return values that my code couldn&#x27;t know because the Windows API was an opaque FFI to C. Like if I ask a Windows API to fill an 8-element array, my code can assume it returns an int &lt;= 8 instead of considering INT_MAX as a possibility.<p>But anything more advanced like assuming that an array&#x27;s values are sorted seemed questionable. At worst, it looked like LLVM preserved expensive assumes even if they weren&#x27;t useful to the optimizer.<p>Since it was a toy project, I didn&#x27;t care. It&#x27;s neat to tell my code facts about the world and imagine that a perfect code optimizer can use all those facts. Even if there&#x27;s a C API in the way.<p><a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;core&#x2F;hint&#x2F;fn.unreachable_unchecked.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;core&#x2F;hint&#x2F;fn.unreachable_unchecked...</a><p><a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;core&#x2F;intrinsics&#x2F;fn.assume.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;core&#x2F;intrinsics&#x2F;fn.assume.html</a><p><a href="https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;assume" rel="nofollow">https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;assume</a>
dangabout 2 years ago
Discussed (a bit) at the time (sort of):<p><i>Assertions Are Pessimistic, Assumptions Are Optimistic (2014)</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9828805" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=9828805</a> - July 2015 (4 comments)
bytewareabout 2 years ago
I don&#x27;t expect the compiler to produce &quot;optimal&quot; code in non-trivial cases, but being able to prove to the compiler that some high level code corresponds to some optimized assembly is high on my wishlist (obligatory list: coq, lean, framac, k-framework, f* and many others)
raldiabout 2 years ago
Brilliant. I’m actually surprised to see, nine years later, that this isn’t on track to make it into the next version of the C standard.
评论 #35061694 未加载
SideburnsOfDoomabout 2 years ago
Is this just a different term for precondition, postcondition and invariants in &quot;Design by Contract&quot;?<p>It&#x27;s not a new idea, this has been integrated into programming languages before, notably Eiffel (1985)<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Design_by_contract" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Design_by_contract</a>
dvhabout 2 years ago
Assertions were a mistake. The story is always the same. Someone uses assertions to check for things that can never happen. It works as expected. Then someone else turns off assertions. Then things that cannot happen, happen, and the program thinks everything is OK.
评论 #35067430 未加载
fiddlerwoaroofabout 2 years ago
Common Lisp type declarations work sort of like assumptions: if you DECLARE that a variable is of a specific type, the compiler is permitted to trust you on that.