Given these types
type a = [ `A ]
type b = [ a | `B | `C ]
and this function
let pp: [< b] -> string =
function | `A -> "A"
| `B -> "B"
| `C -> "C"
applying a value of type a works without issue, as expected:
let a: a = `A
let _ = pp a
However, if the function is modified to include a wildcard pattern
let pp: [< b] -> string =
function | `A -> "A"
| `B -> "B"
| _ -> "?"
even though everything else remains the same, it now yields the following error (on let _ = pp a):
This expression has type b -> string but an expression was expected of type a -> 'a Type b = [ `A | `B ] is not compatible with type a = [ `A ] The second variant type does not allow tag(s) `B
Questions:
- Why is it no longer able to accept a subtype? I understand the wildcard means it now CAN accept a supertype, but that shouldn't mean it MUST.
- Is there some way of getting around this, to avoid having to enumerate a million or so variants that aren't relevant?
The underlying question is why the type of
is infered as
[> `A| `B] -> stringand not as[< `A| `B | ... ] -> string(where...stands for any constructor). The answer is that is a design choice and a question of compromise between false positive and false negative : https://www.math.nagoya-u.ac.jp/~garrigue/papers/matching.pdf .More precisely, the second type was deemed too weak since it was too easy to lose the information that
`Aand`Bwere present inpp. For instance, consider the following code where`bis a spelling mistake and should have been`B:Currently, this code fails with
At this point, if
`bwas a spelling mistake, it becomes possible to catch the mistake here. Ifpphad been typed[< `A|`B |..], the type of dual would have been restricted to[`A] -> unit * stringsilently, with no chance of catching this mistake. Moreover, with the current typing, if`bwas not a spelling mistake, it is perfectly possible to makedualvalid by adding some coercionsmaking it very explicit that
restrictandppworks on different sets of polymorphic variants.