-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use all constructors listed in a pattern matching to resolve ambiguity #6784
Description
Original bug ID: 6784
Reporter: @alainfrisch
Status: acknowledged (set by @damiendoligez on 2015-02-18T22:54:20Z)
Resolution: open
Priority: normal
Severity: feature
Target version: later
Category: typing
Child of: #5759 #7409
Monitored by: @diml @yallop @hcarty
Bug description
In a record pattern or expression, when no type information is available, the type-checker looks at all listed labels to find the record type. It would be useful to apply a similar strategy for pattern matching on sum types.
Typically, with a declaration such as:
type kind1 =
| Var of string
| Foo of kind1 * kind2
and kind2 =
| Var of string
| Bar of kind1 option * kind2 option
one would be able to write directly:
let rec eval1 = function
| Var _ -> ...
| Foo _ -> ...
and eval2 = function
| Var _ -> ...
| Bar _ -> ...
This would make the code more robust w.r.t. changing the ordering between the declarations for kind1 and kind2 and also w.r.t. changing the ordering of pattern matching clauses. Moreover, it's not syntactically convenient to provide a type annotation on the input type for eval1 or eval2 ("let rec eval1 : t -> _ = function ...").
A first step would be to apply the strategy only when the constructors appear at top-level (type-based lookup would then often do the job for sub-terms, except for parametrized sum types). This should be simple to implement and explain. We could go further and do the same e.g. under tuples.