List Info

Thread: "ocaml_beginners"::[] modules, module types and signatures




"ocaml_beginners"::[] modules, module types and signatures
country flaguser name
United States
2007-04-06 10:09:33

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I am seeing some unintuitive behavior that I can't understand. I have a
simple module type signature:

module type Mytest = sig
val id : int -> 'a
end

Then I define a simple module that seems like it should match that signature:

# module Foo = struct
let id (a:int) = a
end;;
module Foo : sig val id : int -> int end

However it does not match:

# module X = (Foo:Mytest);;
Signature mismatch:
Modules do not match: sig val id : int -> int end is not included inMytest
Values do not match:
val id : int -> int
is not included in
val id : int -> 'a

It seems to me however, that a function of (int -> int) is a subtype of
(int -> 'a). Why wouldn't it unify?

Thanks in advance,

Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.2 (GNU/Linux)

iD8DBQFGFmKxfIRcEFL/JewRAmpXAJwJ5CA8ZfN8UZD5NSMhKj8hF1Q5lACgu7Ed
bFmvLQzpre6lDzV8I9owv6s=
=oXuE
-----END PGP SIGNATURE-----

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: modules, module types and signatures
country flaguser name
United States
2007-04-06 12:19:23

This is a bit confusing, but module signatures and function signatures
are different beasts.

A module signature is its interface... the module signature defines
how the outside world has access to the module's functions and types.
Therefore it can restrict function types to make them conform to the
module signature, but it cannot generalize them. A function of type
int -> int cannot be used as a function from type int -> 'a, but a
function of type 'a -> 'b could be used as a function from type int -> 'a.

Note that a function of type 'a -> 'a can't be used as a function from
type int -> 'a because 'a -> 'a would always unify to int -> int,
which doesn't match the module signature.

The ocaml manual explains it this way:

"Signatures are interfaces for structures. A signature specifies which
components of a structure are accessible from the outside, and with
which type. It can be used to hide some components of a structure
(e.g. local function definitions) or export some components with a
restricted type.";

http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )