Invariant (computer science)

Invariant (computer science)

In computer science, a predicate that, if true, will remain true throughout a specific sequence of operations, is called (an) invariant to that sequence.

Use

Although computer programs are typically mainly specified in terms of what they change, it's equally important to know or specify the invariants of a program. This is especially useful when reasoning about the program. The theory of optimizing compilers, the methodology of design by contract, and formal methods for determining program correctness, all pay close attention to invariants in computer programs.

Programmers often make use of assertions in their code to make invariants explicit. Some object oriented programming languages have a special syntax for specifying class invariants.

Example

The MU puzzle is a good example of a logical problem where determining an invariant is useful. The puzzle:
# If a string ends with an I, a U may be appended ("x"I → "x"IU)
# Any string after an M may be completely duplicated (M"x" → M"xx")
# Any three consecutive Is (III) may be replaced with a single U ("x"III"y" → "x"U"y")
# And two consecutive Us may be removed ("x"UU"y" → "xy")

Is it possible to convert MI into MU using these four transformation rules only?

One could spend many hours applying these transformation rules to strings. However, it might be quicker to find a predicate that's invariant to all rules, and makes getting to "MU" impossible. Logically looking at the puzzle, the only way to get rid of any Is is to have three consecutive Is in the string. The only way to get rid of all Is is when there are exactly three consecutive Is. This makes the following invariant interesting to consider:

:"The number of Is in the string is not a multiple of 3".

This is an invariant to the problem if for each of the transformation rules the following holds: if the invariant held before applying the rule, it will also hold after applying it. If we look at the net effect of applying the rules on the number of Is and Us we can see this actually is the case for all rules:

:

Above table shows clearly that the invariant holds for each of the possible transformation rules. Which basically means that whichever rule we pick, at whatever state, if the number of Is was not a multiple of three before applying the rule, it won't be afterwards either.

Given that there is a single I in the starting string MI, and one is not a multiple of three, it's impossible to go from MI to MU as zero is a multiple of three.

References


* C. A. R. Hoare. "An axiomatic basis for computer programming". "Communications of the ACM", 12(10):576–585, October 1969. doi|10.1145/363235.363259 [http://www.spatial.maine.edu/~worboys/processes/hoare%20axiomatic.pdf | Download PDF ]
* J.D. Fokker, H. Zantema, S.D. Swierstra (1991). "Iteratie en invariatie", Programmeren en Correctheid. Academic Service. ISBN 90-6233-681-7.

ee also

* Hoare logic
* Loop invariant
* Object invariant
* Const correctness

External links

* [http://www.samdurak.com/software/VisualInvariants/ Applet: Visual Invariants in Sorting Algorithms ]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Institution (computer science) — The notion of institution has been created by Joseph Goguen and Rod Burstall in the late 1970 sin order to deal with the population explosion among the logical systems used in computer science . The notion tries to capture the essence of the… …   Wikipedia

  • Topic outline of computer science — Computer science, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems. One well known subject classification system for computer science is… …   Wikipedia

  • List of important publications in computer science — This is a list of important publications in computer science, organized by field. Some reasons why a particular publication might be regarded as important: Topic creator – A publication that created a new topic Breakthrough – A publication that… …   Wikipedia

  • Covariance and contravariance (computer science) — Within the type system of a programming language, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations (such as parameters, generics, and return… …   Wikipedia

  • Invariant — may have several meanings, among which are:* Invariant (computer science), an expression whose value doesn t change during program execution * In computer science, a type in overriding that is neither covariant nor contravariant * Invariant… …   Wikipedia

  • Garbage collection (computer science) — This article is about garbage collection in memory management. For garbage collection in an SSD, see garbage collection (SSD). For other uses, see garbage collection. In computer science, garbage collection (GC) is a form of automatic memory… …   Wikipedia

  • Class (computer science) — In object oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.More technically, a class is a cohesive… …   Wikipedia

  • Constructor (computer science) — In object oriented programming, a constructor (sometimes shortened to ctor) in a class is a special block of statements called when an object is created, either when it is declared (statically constructed on the stack, possible in C++ but not in… …   Wikipedia

  • Method (computer science) — In object oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). Like a procedure in… …   Wikipedia

  • Computer simulation — This article is about computer model within a scientific context. For artistic usage, see 3d modeling. For simulating a computer on a computer, see emulator. A 48 hour computer simulation of Typhoon Mawar using the Weather Research and… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”