Browse thread
signature and 'a generic types
-
Damien Bobillot
- Virgile Prevosto
- Stephane Glondu
- Marcin 'Qrczak' Kowalczyk
- Michael Alexander Hamburg
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ 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 |
Hi, 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) = struct let l = [] let func a = List.assoc a l end;; 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 = struct 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. *) end -- E tutto per oggi, a la prossima volta Virgile