Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006122OCamlOCaml typingpublic2013-08-06 16:322014-09-22 21:43
Reporterjpdeplaix 
Assigned To 
PrioritynormalSeveritytweakReproducibilityalways
StatusacknowledgedResolutionopen 
PlatformOSOS Version
Product Version4.00.1 
Target Versionafter-4.02.1Fixed in Version 
Summary0006122: A polymorphic variant type is not recognized as a polymorphic variant
DescriptionThe following test case fails to compile with the following error:
  Error: The type [< `A | `B ] is not a polymorphic variant type

The second one (almost the same) fails with:
  Error: The type [> ] test is not a polymorphic variant type

As far as I know, those two cases should be allowed.
Steps To Reproduceexternal id : ([< `A | `B ] as 'a) -> [> 'a ] = "%identity"

type 'a test = unit constraint 'a = [>]
let test : [< 'a test ] -> [> 'a ] = fun x -> x
Tagstyping
Attached Files

- Relationships

-  Notes
(0010128)
lpw25 (developer)
2013-08-06 16:42
edited on: 2013-08-06 17:54

This is not a bug, since neither of these examples is intended to be allowed.

> external id : ([< `A | `B ] as 'a) -> [> 'a ] = "%identity"

in this example 'a is a polymorphic variant type, however only type constructors that are defined as polymorphic variants are supposed to be allowed. The problem here is that the error message is very confusing.

EDIT: Actually, it doesn't have to be a type constructor, but it does have to be an exact polymorphic variant type.

> type 'a test = unit constraint 'a = [>]
> let test : [< 'a test ] -> [> 'a ] = fun x -> x

In the second example 'a test is defined as equal to unit, which is not a polymorphic variant type, and the error message is fine. Also note that the [> 'a] would also be rejected by the type checker.

(0010130)
jpdeplaix (reporter)
2013-08-06 16:55

Oh yes, the second is false. But even if we replace « fun x -> x » into « Obj.magic », is fails with the same error.

For the first one, I don't see why it shouldn't be allowed. Because if we remove the « < » in « [< `A | `B ] », it compiles and works fine.
(0010132)
lpw25 (developer)
2013-08-06 17:39
edited on: 2013-08-06 18:07

> Oh yes, the second is false. But even if we replace « fun x -> x » into « Obj.magic », is fails with the same error.

The "fun x -> x" isn't causing the error. The error is cause by the fact that 'a test is defined as unit, and so is not a polymorphic variant type. This means that [< 'a test] does not make any sense.

> For the first one, I don't see why it shouldn't be allowed. Because if we remove the « < » in « [< `A | `B ] », it compiles and works fine.

It can't be accepted because 'a is not an exact polymorphic variant type.

Note that [> foo] is translated by checking that foo is a polymorphic variant type, and then copying the constructors from that type. So the constructors must be known at compile-time.

  ([< `A | `B ] as 'a) -> [> 'a ]

cannot work because we do not know the constructors of 'a at compile-time.

(0010135)
jpdeplaix (reporter)
2013-08-06 18:56

Mmmh I don't see the difference between [< `A | `B ] and the closed type [`A | `B] in the compilation process in this context :/

Why allowing to give lower type changes the ability to know the constructors of 'a ?
(0010136)
lpw25 (developer)
2013-08-06 19:48

> Why allowing to give lower type changes the ability to know the constructors of 'a ?

Because a lower type will have fewer constructors.

The thing to bear in mind is that using type expressions within polymorphic variants is not really part of the type system.

  val f : ([`A | `B] as 'a) -> [> 'a]

is just a convenient short-hand for:

  val f : [`A | `B] -> [> `A | `B]

OCaml's row polymorphism does not have a way to express the type:

  [< `A | `B] as 'a -> [> 'a]

and it cannot expand it out like it did for the type of `f` because it does not know exactly what constructors 'a has.

More sophisticated forms of row polymorphism can express types like the one above, but OCaml's can't.
(0011314)
frisch (developer)
2014-04-23 17:37

Downgrading severity, since this is only about providing a better error message.

- Issue History
Date Modified Username Field Change
2013-08-06 16:32 jpdeplaix New Issue
2013-08-06 16:42 lpw25 Note Added: 0010128
2013-08-06 16:43 lpw25 Note Edited: 0010128 View Revisions
2013-08-06 16:55 jpdeplaix Note Added: 0010130
2013-08-06 16:56 jpdeplaix Tag Attached: typing
2013-08-06 17:39 lpw25 Note Added: 0010132
2013-08-06 17:41 lpw25 Note Edited: 0010128 View Revisions
2013-08-06 17:41 lpw25 Note Edited: 0010128 View Revisions
2013-08-06 17:41 lpw25 Note Edited: 0010128 View Revisions
2013-08-06 17:54 lpw25 Note Edited: 0010132 View Revisions
2013-08-06 17:54 lpw25 Note Edited: 0010128 View Revisions
2013-08-06 18:07 lpw25 Note Edited: 0010132 View Revisions
2013-08-06 18:56 jpdeplaix Note Added: 0010135
2013-08-06 19:48 lpw25 Note Added: 0010136
2013-08-19 17:35 doligez Status new => feedback
2013-08-19 17:35 doligez Target Version => 4.01.1+dev
2014-04-23 17:37 frisch Note Added: 0011314
2014-04-23 17:37 frisch Severity major => tweak
2014-04-23 17:37 frisch Status feedback => acknowledged
2014-05-25 20:20 doligez Target Version 4.01.1+dev => 4.02.0+dev
2014-07-16 20:35 doligez Target Version 4.02.0+dev => 4.02.1+dev
2014-09-04 00:25 doligez Target Version 4.02.1+dev => undecided
2014-09-22 21:43 doligez Target Version undecided => after-4.02.1


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker