This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

A confusing module question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2008-01-11 (22:55) From: Zheng Li Subject: Re: A confusing module question
```
"Yaron Minsky" <yminsky@gmail.com> writes:
> I've been programming in OCaml for along time, and I still don't have
> a really good mental model to understand when some module trick I try
> is going to work.  How do people think about things like this?

OCaml distinguish "type representation" from "type equation", you may
want to check the "module type" section of the manual for the
definitions.

Now consider the example:

module type S = sig
type exposed_t = {foo : int}
type t = exposed_t
end

We shouldn't expect type exposed_t and type t to be semantically
equivelent, as

module type S = sig
type exposed_t = {foo : int}
type t = {foo : int} (* both syntactically and semantically invalid *)
end

Notice the problem here: the constructors ("foo" here) of type
representations are _nominally_ and _exclusive_ bound, which leads to
the fact that any type equivelences can only be defined through type
equations. Though we can define as many type equivelences (anywhere) as
we want, you won't be able to define a single identical type
represetation. In other words, type representation is _principle_ and
type equation isn't. So we shouldn't expect them to be _exchangable_
just because of type arithmetic.

Back to the example, module type S requires a type representation
exposed_t and it's type equivelence t. If given

module M:S = struct
type t = {foo : int}
type exposed_t = t
end

The compiler will fail to find exposed_t has any kind of "representation"
and t is not a type equivelence of anyone. You can re-export exposed_t's
representation as

module M:S = struct
type t = {foo: int}
type exposed_t = t = {foo: int}
end

HTH.

--
Zheng Li
http://www.pps.jussieu.fr/~li

```