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

199 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
  • 109,453
  • 27
  • 204
  • 234
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

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
  • 237,063
  • 85
  • 359
  • 638
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,132
  • 4
  • 35
  • 46
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
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…
5
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
  • 380
  • 2
  • 4
  • 14
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
  • 187
  • 1
  • 13
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
  • 981
  • 2
  • 12
  • 39
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,399
  • 3
  • 15
  • 32
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…
4
votes
4 answers

C++: STL: set: stored value constness

Having the following code: #include #include #include #include using namespace std; class Employee { // ... int _id; string _name; string _title; public: Employee(int id): _id(id) {} string const…
nickolay
  • 2,860
  • 2
  • 25
  • 35
4
votes
2 answers

How to implement a Stack class in C#, with pre/postconditions and invariants?

Does anyone have any examples or ideas on how / what is the best way to implement a Stack class in C#? I understand that there is already a Stack class, but I need to understand how to actually implement a Stack class. I also need advice on how to…
Cody
  • 8,061
  • 17
  • 65
  • 120
4
votes
2 answers

What is the inductive invariant of the simple concurrent program?

Here is a simple concurrent program from the article Teaching Concurrency by Leslie Lamport. Consider N processes numbered from 0 through N-1 in which each process i executes x[i] := 1 y[i] := x[(i - 1) % N] and stops, where each x[i] initially…
hengxin
  • 1,633
  • 2
  • 16
  • 38
4
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,569
  • 20
  • 77
  • 128
1 2
3
13 14