Skip to content

Support GADTs in or-patterns #5736

@vicuna

Description

@vicuna

Original bug ID: 5736
Reporter: omion
Assigned to: @garrigue
Status: confirmed (set by @garrigue on 2012-08-22T23:27:03Z)
Resolution: open
Priority: normal
Severity: feature
Version: 4.00.0
Target version: later
Category: typing
Related to: #7319
Child of: #5998
Monitored by: @hcarty

Bug description

Currently, if you have a GADT with multiple branches sharing the same type, eg:

type _ gadt =
| Float : float -> float gadt
| Float2 : float -> float gadt
| Int : int -> int gadt

then try to use Float and Float2 in one branch of a function:

let type_of_gadt : type t. t gadt -> string = function
| Float _ | Float2 _ -> "float"
| Int _ -> "int"

this will not pass the type checker, even though Float and Float2 have the same type:

Error: This pattern matches values of type float gadt
       but a pattern was expected which matches values of type t gadt

You would have to have each variant:

let type_of_gadt : type t. t gadt -> string = function
| Float _ -> "float"
| Float2 _ -> "float"
| Int _ -> "int"

or, matching with the default "_" will work since it's not an or-pattern, but this only works well if there is only one type in the GADT that has the problem:

let type_of_gadt : type t. t gadt -> string = function
| Int _ -> "int"
| _ -> "float"

It would be handy to be able to combine the branches for GADT constructors that refer to the same types.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions