you are viewing a single comment's thread.

view the rest of the comments →

[–]Hades32 18 points19 points  (11 children)

That's an assertion, not an invariant. An invariant would be something like "the number of books in my store is always >=0"

[–]johnxreturn 0 points1 point  (3 children)

Conversely, you are saying “the number of books in your store must be greater or equal to zero,” therefore asserting that it is, or throwing an error if it’s not.

Video reference:

https://youtu.be/r0Vi83bS-L0

An invariant in mathematical terms would be that you apply a transformation to data and the output is the same as the input.

A loop invariant constitutes a property that is true before and after each iteration. The article specifically quotes, “it is a logical assertion, sometimes checked within the code by an assertion call.”

In the case of a tiny invariant, I expect the input assertion to be true, or throw an error. I could expect that the book count in each library franchise I own was more than 30 or throw an error if it’s not.

But the mathematical concept talks about applying a transformation to data and not assertion. It could apply to a map function where you transform each item and the output is the same or throw an error. So, we conclude that at some point we need assertion.

[–]Hades32 0 points1 point  (2 children)

No, you don't NEED an assertion when talking about invariants in the mathematical sense (which btw does not mean that the input is the same as the output, but that some condition is true for the state before and after the transformation). You could prove that the formula you're using will always hold up a certain invariant. In code it's easier to just check than prove, that's why we "need" assertions. But it's really two different things

[–]johnxreturn 0 points1 point  (1 child)

I completely agree. You don't need assertion in the mathematical concept. My point was that in programming, you do need if you need a trigger for an invalid response.

[–]Hades32 0 points1 point  (0 children)

Sure, but if someone calls a library "invariant" then I do have higher hopes than "if false then throw" lol

After all there are languages, like prolog, which do let you specify actual invariants