On Tue, 19 Sep 2000, Sven LUTHER wrote:
> What is the problem with the following ?
>
> # module type T_A = sig
> val string_of_a : 'a -> string
> end;;
> module type T_A = sig val string_of_a : 'a -> string end
> # module A : T_A = struct
> let string_of_a = string_of_int
> end;;
> Signature mismatch:
> Modules do not match:
> sig val string_of_a : int -> string end
> is not included in
> T_A
> Values do not match:
> val string_of_a : int -> string
> is not included in
> val string_of_a : 'a -> string
The reaction of OCaml looks correct to me: you have a signature that
demands that there be a function "string_of_a" which can convert a string
out of anything (I'd really love to have something like this! ;)
The implementation, however, can only manage integers, which means: it is
not general enough to cope with all input. Just imagine that somebody,
believing that your function can handle all input as specified in the
interface, passed a float to the implementation "A.string_of_a": it might
crash on this input. Therefore, this is not allowed.
You could do the opposite: have a signature that restricts the type to
integers, but whose implementation could handle much more than this. This
would be safe.
Best regards,
Markus Mottl
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl