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
3
votes
1 answer

How to apply function to value defined in data class constructor before init method?

Let's say I have a data class like this: data class MyData(val something: Int, val somethingElse : String) { init { require(something > 20) { "Something must be > 20" } require(StringUtils.isNotEmtpy(somethingElse)) { "Something…
Johan
  • 37,479
  • 32
  • 149
  • 237
3
votes
1 answer

What are read-models within Aggregates in DDD and where to use them?

I know in DDD that deleting an Aggregate root would mean removing everything within the Aggregate boundary at once. But I have noticed that read-models(read-only properties) are used within aggregates as Lev Gorodinski stated in this blog:…
Simple Code
  • 2,354
  • 2
  • 27
  • 56
3
votes
1 answer

If an invariant between two datum must hold on all threads, does it forces read/write on these datum to be in critical section?

I have tried to establish an invariant between two atomic counters in one thread while ensuring that this invariant was maintained during read on an other thread without using a mutex. Nevertheless looking at the code, it appears that I just…
Oliv
  • 17,610
  • 1
  • 29
  • 72
3
votes
2 answers

What is an invariant varying in OpenGL Shader Language

I was reading the OpenGL ES 2 Shading Language specification (PDF), when I went through this code: invariant varying mediump vec3 Color; I think understand the invariance concept, but the meaning of an "invariant varying" seems quite…
Antzi
  • 12,831
  • 7
  • 48
  • 74
3
votes
0 answers

C++ Use Function Preconditions Or Wrapper Classes with Invariants?

I find myself writing a lot of functions that begin with many preconditions, and then I have to figure out how to handle all the invalid inputs and write tests for them. Note that the codebase I work in does not allow throwing exceptions, in case…
Russell Taylor
  • 141
  • 1
  • 5
3
votes
2 answers

Domain Driven Design; Can ValueObject contains invariants or specifications?

I'm starting to play with Domain Driven Design and have a question about ValueObjects : Can they contains invariants or other specifications ? Consider an immutable ValueObject : ValueObject ( prop integer: Int prop string: String // Value…
gervais.b
  • 2,294
  • 2
  • 22
  • 46
3
votes
4 answers

Managing invariants in Clojure / Haskell

I have been comparing OOP and FP methodologies and I could not put my head around one thing in functional programming - keeping invariants in the data structure. For example imagine the following requirements. We have a list of projects and every…
3
votes
1 answer

Is 'invariant' property part of the definition of Abstraction?

As part of my learning i think the best answer(with meaning) for definition of abstraction that i found is from stackoverflow: What is abstraction? Besides that, As part of current online course cs61B Fall 2006, Berkeley, i learnt the similar below…
overexchange
  • 15,768
  • 30
  • 152
  • 347
3
votes
2 answers

Scala BigInt Array

I'm trying to solve this problem http://projecteuler.net/problem=62 and I am getting hung up on this error: euler.scala:10: error: type mismatch; found : Array[Any] required: Array[Int] Note: Any >: Int, but class Array is invariant in type…
David Melin
  • 302
  • 2
  • 11
3
votes
1 answer

meaning of Hu seven invariant moments in object recognition

My question is about the meaning of Hu's seven invariant moments. As far as I know, some moments have a meaning; i.e. the zeroth order refers to the area of the image. Also as stated in http://mathworld.wolfram.com/topics/Moments.html a lot of…
Omid
  • 31
  • 1
  • 4
2
votes
2 answers

Finding a loop invariant in a program to compute sums of cubes?

I'm not 100% sure what the invariant in a triple power summation is. Note: n is always a non-negative value. Pseudocode: triplePower(n) i=0 tot=0 while i <= n LI1 j = 0 while j < i LI2 k = 0 while…
Michael Schilling
  • 341
  • 3
  • 5
  • 16
2
votes
2 answers

Hoare Logic, while loop with '<= '

I'm working on some Hoare logic and I am wondering whether my approach is the right one. I have the following program P: s = 0 i = 1 while (i <= n) { s = s + i i = i + 1 } It should satisfy the hoare triple {n >= 0}P{s = n*(n+1)/2} (so it…
bambinoh
  • 63
  • 1
  • 6
2
votes
1 answer

D class invariant calling const functions

Why does the following throw a compiler error: class A { public: int f() const { return 5; } protected: invariant() { assert (f() == 5); } } main.d(14): Error: cannot call public/export function f from…
Taco de Wolff
  • 1,682
  • 3
  • 17
  • 34
2
votes
1 answer

Optimal placement of assert statements to assure correctness using invariant

I'm trying to understand invariants in programming via real examples written in Python. I'm confused about where to place assert statements to check for invariants. My research has shown different patterns for where to check for invariants. For…
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
2
votes
4 answers

Enforce use of Getter / Setter within same class (C++)

Is there a way in C++ to enforce the use of getters or setters WITHIN the class? class C{ private: int x; // should only be Changed by setX(); private: setX(int i){ (...) // enforce some complicated invariantes …
Hugo
  • 23
  • 5