Covariance and contravariance
Covariance and contravariance are fundamental concepts in the field of type theory and category theory, which are branches of mathematics and computer science. These concepts describe how the types of objects and functions change in response to transformations in their input and output types.
Covariance[edit | edit source]
In type theory, a type constructor is said to be covariant if it preserves the order of types. This means that if type A is a subtype of type B, then the constructed type F(A) is a subtype of F(B). Covariance is often encountered in the context of object-oriented programming and generic programming.
For example, in a programming language with inheritance, if class Dog is a subclass of class Animal, then an array of Dogs can be considered a subtype of an array of Animals. This is because the array type constructor is covariant.
Contravariance[edit | edit source]
Contravariance is the opposite of covariance. A type constructor is contravariant if it reverses the order of types. In other words, if type A is a subtype of type B, then the constructed type F(B) is a subtype of F(A).
Contravariance is often used in the context of function types. For instance, if a function type takes an argument of type A and returns a result of type B, then a function that takes an argument of type B and returns a result of type A can be considered a subtype of the original function type. This is because the function type constructor is contravariant in its argument type and covariant in its return type.
Invariance[edit | edit source]
A type constructor is said to be invariant if it does not exhibit either covariance or contravariance. This means that there is no subtype relationship between the constructed types, regardless of the subtype relationship between the original types.
Applications[edit | edit source]
Covariance and contravariance are crucial in the design of type systems for programming languages. They help ensure type safety and enable more flexible and reusable code. These concepts are also important in category theory, where they relate to the behavior of functors and natural transformations.
Related Concepts[edit | edit source]
- Type theory
- Category theory
- Object-oriented programming
- Generic programming
- Inheritance (object-oriented programming)
- Function type
- Type safety
- Functor
- Natural transformation
See Also[edit | edit source]
Search WikiMD
Ad.Tired of being Overweight? Try W8MD's physician weight loss program.
Semaglutide (Ozempic / Wegovy and Tirzepatide (Mounjaro / Zepbound) available.
Advertise on WikiMD
WikiMD's Wellness Encyclopedia |
Let Food Be Thy Medicine Medicine Thy Food - Hippocrates |
Translate this page: - East Asian
中文,
日本,
한국어,
South Asian
हिन्दी,
தமிழ்,
తెలుగు,
Urdu,
ಕನ್ನಡ,
Southeast Asian
Indonesian,
Vietnamese,
Thai,
မြန်မာဘာသာ,
বাংলা
European
español,
Deutsch,
français,
Greek,
português do Brasil,
polski,
română,
русский,
Nederlands,
norsk,
svenska,
suomi,
Italian
Middle Eastern & African
عربى,
Turkish,
Persian,
Hebrew,
Afrikaans,
isiZulu,
Kiswahili,
Other
Bulgarian,
Hungarian,
Czech,
Swedish,
മലയാളം,
मराठी,
ਪੰਜਾਬੀ,
ગુજરાતી,
Portuguese,
Ukrainian
Medical Disclaimer: WikiMD is not a substitute for professional medical advice. The information on WikiMD is provided as an information resource only, may be incorrect, outdated or misleading, and is not to be used or relied on for any diagnostic or treatment purposes. Please consult your health care provider before making any healthcare decisions or for guidance about a specific medical condition. WikiMD expressly disclaims responsibility, and shall have no liability, for any damages, loss, injury, or liability whatsoever suffered as a result of your reliance on the information contained in this site. By visiting this site you agree to the foregoing terms and conditions, which may from time to time be changed or supplemented by WikiMD. If you do not agree to the foregoing terms and conditions, you should not enter or use this site. See full disclaimer.
Credits:Most images are courtesy of Wikimedia commons, and templates Wikipedia, licensed under CC BY SA or similar.
Contributors: Prab R. Tumpati, MD