2 ms·
It seems like "union" is often nonsensical for function types. They say it's useful for representing overloaded functions, but then give this example: let f |
by skybrian 5y ago
It seems like "union" is often nonsensical for function types. They say it's useful for representing overloaded functions, but then give this example:
let f | ( Positive → Positive )
∪ ( Positive → NonPositive )
= fun x ⇒ x - 7 in
(f 10) + ( f 5)
Here a function is supposed to take a positive number and then return a number that's both positive and negative. That is, the intersection of the return types is the empty set and there's no possible implementation.
So it seems like arbitrary unions of multiple function types isn't a good way to represent a function type. But it's unclear why you'd expect this to work since taking the union of two functions doesn't work either. Considered as a set of (input, output) pairs, the union of two functions is often a nonfunction, mapping the same input to multiple outputs.
Consider how this works for merging dictionaries. Each key can only be mapped to one value, so you can't just take the union without deciding which input takes priority when keys overlap.
Similarly, for function overloading, either the input types don't overlap or there is some way of deciding which takes priority.