Questions tagged [invariants]

In computer science, a predicate is called an invariant to a sequence of operations provided that: if the predicate is true before starting the sequence, then it is true at the end of the sequence.

In loops, invariants are data structures referenced within the loop that do not change during any iteration. In design-by-contract, invariants are invariants are properties of a class than must be satisfied at the end of any method call that is invoked from outside of the class itself.

References

218 questions
7
votes
5 answers

Preserving invariants while allowing destructuring

I want to define a type so that all construction goes through module members that can preserve invariants, but allow destructuring for pattern matching. I'm just learning OCaml but the following almost works for an int pair with the invariant that…
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
6
votes
2 answers

Is there a static invariant discovery tool for C programs?

I'm looking for a tool that can statically discover invariants in C programs. I checked out Daikon but it discovers invariants only dynamically. Is there a tool available for what I'm looking for? Thanks!
6
votes
2 answers

DDD Aggregate with potentially large collection with important invariant

I understand that Aggregates should be small and they should protect invariants. I also know that keeping large collections in Aggregates impacts performance. I have a usecase, that needs to protect its invariants, but also will lead to large…
Maciej Pszczolinski
  • 1,623
  • 1
  • 19
  • 38
6
votes
2 answers

Does it make sense to throw a private exception?

I want to throw a runtime exception in case my class invariants are invalidated. Since this is a programming error (similar to a NullPointerException), clients should not catch that exception. Should the exception class be declared private or public…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
6
votes
1 answer

Using Loop invariant to prove correctness of merge sort (Initialization , Maintenance , Termination)

How would you go about proving the correctness of merge sort with reasoning over the states of loop invariants?.The only thing that i can visualize is that during the merge step the subarrays(invariants) when combined maintain their states i-e they…
With A SpiRIT
  • 478
  • 3
  • 6
  • 17
6
votes
4 answers

Adding Invariants to Interfaces in Java

I've been thinking about creating a Java framework that would allow programmers to specify invariants (pre- and post-conditions) on interfaces. The purpose would be to make code more robust and reduce the number of unit tests that would need to be…
Tarski
  • 5,360
  • 4
  • 38
  • 47
6
votes
8 answers

Loop invariants (Specifically Ch.3 of "Accelerated C++")

I'm currently working my way through "Accelerated C++" and just came across this in chapter 3: // invariant: // we have read count grades so far, and // sum is the sum of the first count grades while (cin >> x) { ++count; sum += x; } The…
Owen
  • 63
  • 6
5
votes
1 answer

"dict[str, Unknown]" is incompatible with my custom TypedDict

Given this custom TypedDict TMyDict: class TMyDict(TypedDict, total=False): prop_a: int prop_b: int This is ok: def get_my_dict_works() -> TMyDict: return { 'prop_a': 0, 'prop_b': 1 } But this don't: def get_my_dict_fail() ->…
Aero WuBook
  • 173
  • 1
  • 6
5
votes
1 answer

How to Java doc an invariant for a class?

I'm wanting to know where exactly the comment should go and what keyword I should use as I cant really seem to find an example online, should I for example do this? /** * @invariant invariant example */ public class Example { }
Moulie415
  • 317
  • 3
  • 13
5
votes
1 answer

Invariant and precise keywords in GLSL

I am trying to understand these two concepts. The manual I am reading is very brief on them and things like multipass algorithm are new to me. I would like to have some examples (not code) of where would I need to use invariant or precise variables,…
ali
  • 10,927
  • 20
  • 89
  • 138
5
votes
1 answer

Immutable class in Eiffel

I'm trying to make an immutable POINT class in Eiffel. Is the code below defines one? The {NONE} accessibility for the x and y fields is enough for it? Can I write something to the class invariant like x = x', or how else can I achieve…
Ferenc Dajka
  • 1,052
  • 3
  • 17
  • 45
5
votes
3 answers

Do you use invariants when you program?

I am taking an intermediate programming course which stresses the use of invariants. I have never used them before and they seem to take up more time to create. Does the software engineering industry stress the use of invariants?
Brandon Tiqui
  • 1,429
  • 3
  • 17
  • 35
5
votes
4 answers

maintaining rep invariants in Objective-C

I'm new to Objective-C and trying to figure out what the best way of maintaining the rep invariant of a class is, given that exceptions aren't really an appropriate way of enforcing them. A good example of where this would come up is in the Fraction…
5
votes
2 answers

For OOP, are immutable and invariant synonymous?

For OOP, are immutable and invariant synonymous? I have a vague feeling that a difference exists, but I'm not sure. I believe that immutable typically is applied to objects, while invariant is typically applied to attributes/values. In both cases…
JustADude
  • 2,619
  • 7
  • 31
  • 45
5
votes
3 answers

Array of different generics

Long story short, I would like to be able to store generics using different type parameters in an array, by using a parent type to all the types used. MSDN mentioned it was impossible, as generics were invariant types, but a comment stated that this…
1 2
3
14 15