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

Slightly odd polymorphic variant types #6743

Closed
vicuna opened this issue Jan 6, 2015 · 3 comments
Closed

Slightly odd polymorphic variant types #6743

vicuna opened this issue Jan 6, 2015 · 3 comments
Assignees
Milestone

Comments

@vicuna
Copy link

vicuna commented Jan 6, 2015

Original bug ID: 6743
Reporter: @lpw25
Assigned to: @garrigue
Status: closed (set by @xavierleroy on 2016-12-07T10:47:06Z)
Resolution: won't fix
Priority: normal
Severity: tweak
Target version: 4.03.0+dev / +beta1
Category: typing

Bug description

It seems that conjunctive polymorphic variant types are not normalized, which leads to some slightly odd types. For example:

# let f (x : [< `A of _]) = x;;
val f : ([< `A of 'b ] as 'a) -> 'a = <fun>

# let g (x : [< `A of int]) = f x;;
val g : ([< `A of int & 'b ] as 'a) -> 'a = <fun>

and

# let h (type a) (type b) (x : [< `A of a]) : [< `A of b] = x;;
val h : ([< `A of 'b & 'c ] as 'a) -> 'a = <fun>

Both these cases include unnecessary type variables. I don't think there are issues with soundness of these types or anything, but it would probably be better if the unnecessary type variables were unified away.

@vicuna
Copy link
Author

vicuna commented Jan 16, 2015

Comment author: @garrigue

For principality reasons, in general it is not correct to unify argument types in the upper bound. It is of course correct in some cases, but detecting them is non-trivial: it amounts to checking an equivalence between two quantified logical formulae. I tried once, but this was really too much work to be worth it.
The specific case you describe here is simpler than the general one, since it would be sufficient to check that a type variable only occurs as argument of a single variant type, but this is still some work.

@vicuna
Copy link
Author

vicuna commented Jan 16, 2015

Comment author: @lpw25

I think the first example is the only one that is actually confusing because you start with two types which have no conjunctions and, even though the types can unify, you end up with a conjunction.

Would it not be safe (and easy to implement) to attempt unification when faced with two types which do not have any conjunctions (e.g. [< Foo of 'a] and [< Foo of int]) and then only fallback to a conjunction (e.g. [< `Foo of 'a & int]) if the unification fails?

@vicuna
Copy link
Author

vicuna commented Jan 16, 2015

Comment author: @lpw25

Would it not be safe (and easy to implement) to attempt unification when faced with two types which do not have any conjunctions (e.g. [< Foo of 'a] and [< Foo of int]) and then only fallback to a conjunction (e.g. [< `Foo of 'a & int]) if the unification fails?

Actually, I guess that would be less general since it would not allow:

# let f (x : [< `A of 'b | `B of string]) (y : 'b) = x;;
val f : ([< `A of 'b | `B of string ] as 'a) -> 'b -> 'a = <fun>

# let g (x : [< `A of int | `B of string]) (y : float) = f x y;;
val g : ([< `A of int & float | `B of string ] as 'a) -> float -> 'a = <fun>

# let h (x : [`B of string]) (y : float) = g x y;;
val h : [ `B of string ] -> float -> [ `B of string ] = <fun>

@vicuna vicuna closed this as completed Dec 7, 2016
@vicuna vicuna added this to the 4.03.0 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