From: Virgile Prevosto <virgile.prevosto@m4x.org>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] signature and 'a generic types
Date: Mon, 20 Jun 2005 17:59:42 +0200 [thread overview]
Message-ID: <20050620175942.2287baed@mpiat2305> (raw)
In-Reply-To: <C6B8033B-6F2E-490D-85BB-55B37407448E@m4x.org>
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
next prev parent reply other threads:[~2005-06-20 15:59 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-06-20 15:20 Damien Bobillot
2005-06-20 15:59 ` Virgile Prevosto [this message]
2005-06-20 16:06 ` [Caml-list] " Stephane Glondu
2005-06-20 16:19 ` Marcin 'Qrczak' Kowalczyk
2005-06-20 22:31 ` Michael Alexander Hamburg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050620175942.2287baed@mpiat2305 \
--to=virgile.prevosto@m4x.org \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox