In coursera functional programming course, I came across a subtle concept.
If A2 <: A1 and B1 <: B2, then (A1 => B1) <: (A2 => B2)
Justification
If we draw a Venn diagram for this,
diagram 1
diagram 2
Reference : Youtube video
Thanks
For example, in one study, people were more likely to change their stereotype that lawyers are extraverted if they learned about an introverted lawyer who seemed otherwise typical of the group (e.g., was White), rather than one who seemed deviant on multiple dimensions (e.g., was Black).
In programming language theory, subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which a subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability, meaning that program elements, typically subroutines or functions, ...
Subtyping is a notion in programming language theory where a subtype, which is a data type, is related to a supertype based on the notion of substitutability, where program elements such as functions and subroutines that are written for the supertype will still operate if given the subtype instead.
A subtype of a given type is a combination of the type, a constraint on values of the type, and certain attributes specific to the subtype. The given type is called the type of the subtype. Similarly, the associated constraint is called the constraint of the subtype.
Let's call (A1 => B1) for F1 and (A2 => B2) for F2
For a function F1 to be a subtype of another function F2, we need the type system to accept it in the place of F2.
You can pass any subtype of an argument A to a function that accepts A, but no supertype. This means that, for F1 to be a subtype of F2, it must accept at least everything that F2 accepts as argument, hence A1 must be a supertype of A2.
The output of F1, on the other hand, must be at least as detailed as the output of F2 so it can be used wherever the output of F2 can be used. This means that B1 must be a subtype of B2.
I'm not sure that diagrams are a good way of visualizing how this fits together, but I'd say that, of the two, diagram 1 is the most accurate one.
Let's look at an example:
Say you have the function f1(s: Set): Set
Then f2(s: Iterable): SortedSet
is a subtype of f1, since it can be used in place of f1.
f1 requires its arguments to be of type Set
or any subtype of Set
. All these arguments are also valid in f2.
The output of f1 is a Set
, so the output of f2 must be usable as a Set
. Since SortedSet
is a subtype of Set
, this is also true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With