Signature hiding option

Is there a reason why OCaml cannot deploy intermediate parameterized 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 suppose this is freed from the fact that the memory representation can sometimes be different, but I was wondering if there was any deeper reason before sending the error report to OCaml error-error ...

+3
source share
2 answers

This is not a matter of representing memory. When comparing a type declaration with a type signature, or, in general, when checking to see if the declaration is t1less general than the declaration t2, currently, type checking only checks for three cases:

  • t2 - abbreviation for abstract type or type
  • t1 t2 -
  • t1 t2 -

. t1 ( ) , t2 () . .

: type_declarations /includemod.ml.

, foo.ml :

type u = Foo of string
type t = u

, . .

: , , . , , :

module Test : sig
  type t = Foo
  type u = t (* to the outside, t and u would be equal *)
end = struct
  type t = Foo (* while internally they are different *)
  type u = Foo (* sum/records are generative/nominative *)
end

( ) :

module Test : sig
  type t = Foo
  type u = Foo
end = struct
  type t = Foo
  type u = t = Foo
end;;

fun (x : Test.t) -> (x : Test.u);;
(* Error: This expression has type Test.t but an expression
   was expected of type Test.u *)

, ( ) :

module Test : sig
  type r = { x : float; y : float; z : float } (* boxed float record *)
end = struct
  type 'a t = { x : 'a; y : 'a; z : 'a } (* polymorphic record *)
  type r = float t
end
+4

, , :

type 'a t = 'a * int
type u = string t

type u = string * int

( ) OCaml. , . , . , . ( OCaml. , , .)

FWIW, :

type 'a t = Foo of 'a
type 'a u = 'a t = Foo of 'a

type 'a u = Foo of 'a

, .

+5

All Articles