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
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
  • 3,643
  • 3
  • 32
  • 40
4
votes
1 answer

What are some benefits and pitfalls of ODT (Observation Driven Testing)?

We just came upon the whitepaper "Observation Driven Testing: Yes, the code is doing what you want. By the way, what else is it doing?", and were intrigued. However, Google doesn't seem to reveal much about how it works in practice (1, 2).…
ms-tg
  • 2,688
  • 23
  • 18
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,686
  • 18
  • 71
  • 126
4
votes
2 answers

Enforcing invariants with scope on child entity of aggregate root - DDD

I´m trying to understand how to represent certain DDD (Domain Driven Design) rules. Following the Blue Book convention we have: The root Entity has global identity and is responsible for checking invariants. The root entity controls access and…
Fabio Marreco
  • 2,186
  • 2
  • 23
  • 24
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,867
  • 2
  • 21
  • 42
4
votes
1 answer

How can class invariant strengthen pre and post-conditions?

Link You can think of the class invariant as a health criterion, which must be fulfilled by all objects in between operations. As a precondition of every public operation of the class, it can therefore be assumed that the class invariant…
4
votes
1 answer

I'm using daikon Chicory to do invariant detect, but meets java.lang.VerifyError

I'm learning how to use daikon.Chicory do some invariant detect. But on the java-examples part, I meet this error after this command java daikon.Chicory --daikon DataStructures.StackArTester I'm using cygwin shell on windows 7. -ea -Xmx128M…
riowww
  • 123
  • 1
  • 10
4
votes
3 answers

Are preconditions and postconditions needed in addition to invariants in member functions if doing design by contract?

I understand that in the DbC method, preconditions and postconditions are attached to a function. What I'm wondering is if that applies to member functions as well. For instance, assuming I use invariants at the beginning at end of each public…
3
votes
2 answers

Loop Invariant for function to compute factorials

I'm having a hard time correctly identifying a loop invariant for the following function: F(y) X <-- 1 while (y > 1) do x <-- x * y y <-- y - 1 return (x) I've identified the loop invariant to be x = 1 OR x = y! as…
Brownbay
  • 5,400
  • 3
  • 25
  • 30
3
votes
2 answers

Can this statement be regarded as a class invariant?

This is a highly general thought, but let's use C# in this example. Given that I have a disposable class Foo, i.e., it implements IDisposable. Foo has a boolean flag disposed that is false until Dispose is called, after which it's true. All public…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
3
votes
2 answers

Remove invariants from some prolog list?

I am searching some predicate: reduce_2n_invariant(+I, +F, -O) based on: some input list I some input operator F of form fx, which generates some output list O, that satisfies following general condition: ∀x:(x ∈ O ↔ ∀ n ∈ ℕ ∀ y ∈ O: x ≠…
Martin Kunze
  • 995
  • 6
  • 16
3
votes
3 answers

Can I get Code Contracts to warn me about "illegal" subtyping?

Sorry if this question seems too long. Before I can ask it, I need to show where it's coming from. Set-up: Given the following immutable type Rectangle: class Rectangle { public Rectangle(double width, double height) { … } public double…
3
votes
3 answers

DDD - how to enforce invariants but specific to the client requirements?

I am trying to figure out how to keep the invariants still consistent for a few consumers (business clients) of the project who have their own requirements on the same version of the aggregate root. Let's take the Customer as an example and ask…
3
votes
2 answers

Ada GNATprove insints that 1 is not >= 0

I am trying to prove, that my algorithm for finding second largest value in array works as it should. This is my code: function FindMax2 (V : Vector) return Integer is Max : Natural := 0; SecondMax : Natural := 0; begin for I in V'Range…
pucikplay
  • 101
  • 4
3
votes
1 answer

Proving Select Sort algorithm using SPARK

I am trying to prove that my implementation of Select Sort in Ada is correct. I have tried a few loop invariants, but using gnatprove only proves inner loop's invariant: package body Selection with SPARK_Mode is procedure Sort (A : in out Arr) is …
pucikplay
  • 101
  • 4