From: Xavier Clerc <xcforum@free.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Question about polymorphic variants
Date: Mon, 31 Oct 2005 18:08:34 +0100 [thread overview]
Message-ID: <D55E9EA1-986E-4B34-AC3C-CE4C18E356CF@free.fr> (raw)
In-Reply-To: <20051029.092649.55723981.garrigue@math.nagoya-u.ac.jp>
Le 29 oct. 05 à 02:26, Jacques Garrigue a écrit :
[...]
>> In this counter-example, it is not clear to me whether the possible
>> problem is type-related or runtime-related. I mean, would this
>> counter-example still hold if arrays were both boxed or both
>> unboxed ?
>>
>
> No, the difference in representation is essential here.
>
> But at the type level, this problem can be expressed a bit
> differently: whether it is safe to add a "top" to the type system,
> allowing any type to be coerced to it. While some languages might
> allow that, there is no fundamental reason it should be so (an this is
> indeed not the case in ocaml.)
> If we cannot assume the existence of such a type, then we cannot prove
> that polymorphism in this case would be safe.
I must admit that I don't grasp the link between the existence of a
"top" type and the inference of a polymorphic type in the given
examples. I would expect inference of 'a array in arrays example and
'a in "List.map (fun (x, y) -> x + 1)" and don't see in what
circumstances such types would not be safe (modulo the array
representation issue discussed above).
Could you exhibit an example where such inference would be false/
unsafe ?
>>> Actually, since this counter-example wouldn't apply to the above
>>> case
>>> of polymorphic variants, this would probably be safe to leave the
>>> polymorphic variant type as polymorphic...
>>>
>>
>> Does this mean that inferring "[< `Off | `On] list -> int list" would
>> be perfectly safe in the example above ?
>> (by saying so, I am not pleading for any compiler change, I am just
>> trying to organize my thoughts)
>>
>
> I believe so, but I don't have a proof ready for that.
> One way to do it would be to prove that any instance of [< `Off | `On]
> is a subtype of [ `Off | `On ], which sounds trivial in this case.
> So I would say this should apply to both [< ... ] types (closed
> polymorphic variants, including the [< ... > ...] case) and <...; ..>
> types (extensible object types.) But not to [> ...] types (open
> polymorphic variants.)
Well, the only way to get rid of the leading underscore in inferred
type is to "use" all the tags of the type (that is "converge" toward
the upper bound of the variant), as in the following toplevel session :
# let f = function
| `Off -> 0
| `On -> 1;;
val f : [< `Off | `On ] -> int = <fun>
# let mf = List.map f;;
val mf : _[< `Off | `On ] list -> int list = <fun>
# mf [`On];;
- : int list = [1]
# mf;;
- : _[< `Off | `On > `On ] list -> int list = <fun>
# mf [`Off];;
- : int list = [0]
# mf;;
- : [ `Off | `On ] list -> int list = <fun>
Does this mean that [`Off | `On] list -> int list could be inferred
for mf as one can pass [`Off | `On] where [< `Off | `On] is waited
(as [`tag1 | `tag2 ... | `tagn] is shorthand for [< tag1 | `tag2 ...
| `tagn > tag1 | `tag2 ... | `tagn]).
I apologize for my questions if they are trivial but I must confess
that I am having a hard time understanding the subtleties of
polymorphic variants.
Xavier Clerc
next prev parent reply other threads:[~2005-10-31 17:08 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-10-28 9:57 Xavier Clerc
2005-10-28 11:59 ` [Caml-list] " Jacques Garrigue
2005-10-28 12:27 ` Xavier Clerc
2005-10-29 0:26 ` Jacques Garrigue
2005-10-31 17:08 ` Xavier Clerc [this message]
2005-11-01 0:27 ` Jacques Garrigue
2005-11-04 13:20 ` Xavier Clerc
2005-11-07 3:11 ` Jacques Garrigue
2005-11-07 13:39 ` Xavier Clerc
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=D55E9EA1-986E-4B34-AC3C-CE4C18E356CF@free.fr \
--to=xcforum@free.fr \
--cc=caml-list@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;
as well as URLs for NNTP newsgroup(s).