#571 – Covariance in Programming Languages
April 27, 2012 Leave a comment
In programming languages, the idea of covariance has to do with whether the ordering of a set of elements is preserved after calling some function that transforms each element.
Consider a set of elements and a function F that accepts as input a member of the set and returns a member of the set. I.e. X’ = F(X), where both X and X’ are members of the set.
We describe a function as covariant if preserves the ordering of elements of the set passed to it. If we have two members of our set, X and Y, and X <= Y, then the function F is covariant if F(X) <= F(Y).
For example, the function F(X) = 2X is covariant with respect to the set of integers. If X <= Y, then 2X <= 2Y, for any X and Y integer values that you pick.