programming type-theory

Contravariance

Let (Cat is subtype of Animal). Consider two functions that consume input:

If a program expects (a handler that deals with cats), can we substitute ? Yes. The program intends to pass a Cat to the function. knows how to handle any Animal. Since , can safely handle the input.

However, the reverse is false. If a program expects (needs to handle any Animal, possibly a Dog), we cannot give it , because crashes if given a Dog.

Therefore, the general function is a subtype of the specific function :

Thus, input parameters are contravariant.