Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading error message with GADTs and polymorphic variants #7181

Closed
vicuna opened this issue Mar 14, 2016 · 4 comments
Closed

Misleading error message with GADTs and polymorphic variants #7181

vicuna opened this issue Mar 14, 2016 · 4 comments
Assignees
Milestone

Comments

@vicuna
Copy link

vicuna commented Mar 14, 2016

Original bug ID: 7181
Reporter: @chambart
Assigned to: @garrigue
Status: closed (set by @xavierleroy on 2017-09-24T15:32:09Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 4.03.0+dev / +beta1
Target version: 4.03.1+dev
Fixed in version: 4.03.0+dev / +beta1
Category: typing
Monitored by: @hcarty

Bug description

The example is

type 'a tree =
| Root : [>Root] tree | Leaf : [Root | Leaf] tree -> [>Leaf] tree

let rec root (type x) (tree : x tree) : [`Root] tree =
match tree with
| Root -> tree
| Leaf parent -> root parent

which fails with

| Root -> tree
^^^^
Error: This expression has type x tree but an expression was expected of type [ Root ] tree Type x = [> Root ] is not compatible with type [ `Root ]
The first variant type does not allow tag(s)

Additional information

By the way, I'm not completely certain that this type should not be accepted

@vicuna
Copy link
Author

vicuna commented Mar 15, 2016

Comment author: @gasche

There is something fishy in trying to take an (exists 'r. [ Root | 'r ]) and turn it into a [ Root ]. In this case, you have no parameter to the Root constructor, so it seems harmless, but you could equally write the following which type-checks:

let rec root (type x) (tree : x tree) : [`Root] tree =
match tree with
| Root -> Root
| Leaf parent -> root parent

if you added a parameter in the Root case, if this parameter does not depend on 'a then you can reuse the trick above, and if it does then your version seems unsound:

type 'a tree =
| Root : ([> Root] as 'a) -> 'a tree | Leaf : [Root | Leaf] tree -> [>Leaf] tree

let rec root (type x) (tree : x tree) : [`Root] tree =
match tree with
| Root _ -> tree
| Leaf parent -> root parent

if I could type-check that I could use it to coerce (Root Leaf) into a ([Root] tree).

@vicuna
Copy link
Author

vicuna commented Mar 15, 2016

Comment author: @garrigue

Indeed, the error message is confusing.
I suppose you should get something like
Type x = [> Root ] is not compatible with type [ Root ]
The second variant type does not allow tag(s) `AnyOtherTag

Here type x has an existential row, which means that it could contain
anything, so you cannot coerce it to just [`Root].

@vicuna
Copy link
Author

vicuna commented Mar 15, 2016

Comment author: @chambart

By the way this also cannot be coerced to [>`Root] for a different reason. The row variable will escape:

| Root -> tree
          ^^^^

Error: This expression has type x tree but an expression was expected of type
([> Root ] as 'a) tree Type x = [> Root ] is not compatible with type [> `Root ] as 'a
The type constructor $0 would escape its scope

@vicuna
Copy link
Author

vicuna commented Mar 30, 2016

Comment author: @garrigue

Fixed in 4.03 by commit 56b337.
Just removed the incorrect last line in that case.

@vicuna vicuna closed this as completed Sep 24, 2017
@vicuna vicuna added the typing label Mar 14, 2019
@vicuna vicuna added this to the 4.03.1 milestone Mar 14, 2019
@vicuna vicuna added the bug label Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants