Is there a reason why OCaml is not able to unroll intermediate parametrized types in a signature ?
For instance:
(* foo.ml *)
type 'a internal = Foo of 'a
type t = string internal
and:
(* foo.mli *)
type t = Foo of string
Give an error.
I guess this is releated to the fact that the memory representation can be different sometimes, but I was wondering if there is any deeper reason before submitting a bug report to the OCaml bug-tracker…
This is not a memory representation question. When matching a type declaration against a type signature, or more generally when checking if a declaration
t1is less general than a declarationt2, the type-checker currently considers only those three cases:t2is an abstract type or type abbreviationt1andt2are both sum typest1andt2are both recordsThe other cases fail with an error. In your case,
t1(the type being checked) is a type abbreviation, andt2(the specification) is a sum type. This fail with a type error.See the source code:
type_declarationsin typing/includemod.ml.This is not a memory representation consideration as
foo.mlwould fail with this as well:Perhaps this check could be refined. You should ask on the bugtracker.
Edit: it’s not trivial to tell how far this check should be refined. It is not correct in general to expand abbreviations on the signature side when checking signature matching, for example the following should not be accepted:
The other way around (an internal equality is hidden from the outside) is correct and already possible:
Now, memory representation also come into account when considering abbreviation expansion, as the dynamic semantics (memory representation choices) of the type system is not preserved by such expansions: