English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
signature and 'a generic types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-06-20 (15:59)
From: Virgile Prevosto <virgile.prevosto@m...>
Subject: Re: [Caml-list] signature and 'a generic types
Le 06/20/2005, à 05:20:40 PM, Damien Bobillot a écrit:
> Look at the following code :
>      module Module : (sig val func : 'a -> 'b end) =
>      struct let func a = a end

>      Values do not match:
>        val func : 'a -> 'a
>      is not included in
>        val func : 'a -> 'b
> Why ?

because the implementation of func is less general than what the
signature expects: if this was accepted, then you could use Module.func
in a context where 'a and 'b would get different instantiations.

> I also try to use the reverse match :
>      module Module : (sig val func : 'a -> 'a end) = struct
>          let l = ref []
>          let func a = List.assoc a !l
>      end

This is not the "reverse" match, which would rather be
module Module: (sig val func: 'a -> 'a end) = 
  let l = [] 
  let func a = List.assoc a l

which is a perfectly well defined module. In your case, problems come
from the ref: the type of l (hence of func) is not really polymorphic:
it uses what is called weak type variables (that you recognize because
of the '_' prefix in '_a and '_b), that on the contrary to normal
type variables can not be generalized. Otherwise, it would be possible
to add to l pairs of different types, such as in the following example:
module Module = 
  let l = ref [] (* type: ('_a * '_b) list *)
  let func a = List.assoc a !l
  let _ = l:= (0, "foo")::!l 
   (* after that '_a is bound to int and '_b to string. *)
  let _ = l:= ("bar", 1)::!l (* type error. *)
E tutto per oggi, a la prossima volta