* Modules, type de modules et type polymorphes.
@ 2000-09-19 12:29 Sven LUTHER
2000-09-19 18:16 ` Markus Mottl
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Sven LUTHER @ 2000-09-19 12:29 UTC (permalink / raw)
To: caml-list
Bonjour, ...
Hello, ...
quel est le probleme de ceci ?
What is the problem with the following ?
bash-2.04$ ocaml
Objective Caml version 3.00
# 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
Amicalement,
Friendly,
Sven LUTHER
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Modules, type de modules et type polymorphes.
2000-09-19 12:29 Modules, type de modules et type polymorphes Sven LUTHER
@ 2000-09-19 18:16 ` Markus Mottl
2000-09-20 7:31 ` Jean-Christophe Filliatre
2000-09-20 13:45 ` Sylvain BOULM'E
2 siblings, 0 replies; 4+ messages in thread
From: Markus Mottl @ 2000-09-19 18:16 UTC (permalink / raw)
To: Sven LUTHER, caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Modules, type de modules et type polymorphes.
2000-09-19 12:29 Modules, type de modules et type polymorphes Sven LUTHER
2000-09-19 18:16 ` Markus Mottl
@ 2000-09-20 7:31 ` Jean-Christophe Filliatre
2000-09-20 13:45 ` Sylvain BOULM'E
2 siblings, 0 replies; 4+ messages in thread
From: Jean-Christophe Filliatre @ 2000-09-20 7:31 UTC (permalink / raw)
To: luther; +Cc: caml-list
> quel est le probleme de ceci ?
> What is the problem with the following ?
>
> bash-2.04$ ocaml
> Objective Caml version 3.00
>
> # 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
Une fonction de type int -> string ne peut pas être appelée avec un
argument d'un type quelconque. Donc votre module A ne peut avoir
l'interface T_A qui déclare que string_of_a accepte un argument d'un
type quelconque.
Dit autrement, si le module A était effectivement de type T_A, alors
A.string_of_a aurait le type 'a -> string et je pourrais écrire
A.string_of_a "tagada", qui n'a pas de sens.
Plus précisément, et c'est peut-être là l'origine de votre problème,
la quantification universelle sur 'a est faite au niveau du type de
string_of_a (et de manière plus générale pour chaque déclaration dans
la signature) et non au niveau du type de module. En ce sens, il n'y a
pas de polymorphisme au niveau du langage de module.
En revanche, vous pouvez écrire
module type T_A = sig
type a
val string_of_a : a -> string
end
puis
module A : T_A = struct
type a = int
let string_of_a = string_of_int
end
[ No English translation, I apologize. ]
--
Jean-Christophe FILLIATRE
mailto:Jean-Christophe.Filliatre@lri.fr
http://www.lri.fr/~filliatr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Modules, type de modules et type polymorphes.
2000-09-19 12:29 Modules, type de modules et type polymorphes Sven LUTHER
2000-09-19 18:16 ` Markus Mottl
2000-09-20 7:31 ` Jean-Christophe Filliatre
@ 2000-09-20 13:45 ` Sylvain BOULM'E
2 siblings, 0 replies; 4+ messages in thread
From: Sylvain BOULM'E @ 2000-09-20 13:45 UTC (permalink / raw)
To: luther; +Cc: caml-list
> Bonjour, ...
> Hello, ...
>
> quel est le probleme de ceci ?
>
> # 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:
Bonjour,
Le probleme est que dans "T_A", on exige que "string_of_a" soit polymorphe. Or
dans A, tu ne lui fournis qu'une fonction monomorphe !!
Qq'un qui utilise un module A de type T_A a le droit d'ecrire
"A.string_of_a true"
Ce qui provoquerait un segmentation fault avec ton implantation de A...
Je ne sais pas ceux que tu cherches a ecrire, mais qq chose comme ci-dessous
serait peut-etre plus adapte :
module type T_A =
sig type t val string_of_a : t -> string end
module A: (T_A with type t=int) =
struct
type t=int
let string_of_a = string_of_int
end
Sylvain.
--
Tel. prof : 01-44-27-88-35
Page web : http://www-spi.lip6.fr/~boulme/
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2000-09-20 19:53 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-09-19 12:29 Modules, type de modules et type polymorphes Sven LUTHER
2000-09-19 18:16 ` Markus Mottl
2000-09-20 7:31 ` Jean-Christophe Filliatre
2000-09-20 13:45 ` Sylvain BOULM'E
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox