From: Martin Jambon <martin.jambon@ens-lyon.org>
To: David Teller <David.Teller@univ-orleans.fr>
Cc: OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] Auto-closing polymorphic variants ?
Date: Thu, 21 Feb 2008 01:29:23 +0100 (CET) [thread overview]
Message-ID: <Pine.LNX.4.64.0802210121210.17606@martin.ec.wink.com> (raw)
In-Reply-To: <1203541049.11853.36.camel@Blefuscu>
On Wed, 20 Feb 2008, David Teller wrote:
> Dear list,
>
> There are still a number of things I don't quite understand about
> polymorphic variants. For instance, polymorphic variants seem to be open
> by default.
>
> # let a = `a ;;
> val a : [> `a ] = `a
> # let b = `b ;;
> val b : [> `b ] = `b
>
> I can only assume this was done to keep the property that in
> if ... then some_p else some_q
> it must be possible to unify the types of some_p and some_q, which
> wouldn't be possible with closed types.
>
> However, as mentioned in the discussion regarding exceptionless error
> management, in conjunction with wildcards, sanity checks become
> irrelevant, which may lead to hard-to-track errors, e.g.
>
> # let safe_div x = function
> | 0. -> `Div_by_zero
> | y -> `Ok (x /. y) ;;
> val safe_div : float -> float -> [> `Div_by_zero | `Ok of float ] = <fun>
>
> # let idiv x y =
> match safe_div (float_of_int x) (float_of_int y) with
> | `Success x -> x
> | _ -> nan ;;
> val idiv : int -> int -> float = <fun>
>
>
> Here, because of the wildcard, ocamlc didn't notice that we wrote
> `Success where no such variant could happen.
>
> Now, it seems to me that this wouldn't a real problem if we had a way to
> auto-close safe_div during the match, i.e. something like
>
> # let idiv x y =
> match close (safe_div (float_of_int x) (float_of_int y)) with
> | `Success x -> x
> ^^^^^^^^^
> | _ -> nan ;;
>
> This pattern matches values of type [> `Success of 'a ]
> but is here used to match values of type [ `Div_by_zero | `Ok of
> float ]
> The second variant type does not allow tag(s) `Success
>
> Of course, we could do that by manually closing the type of safe_div,
> but this would essentially mean duplicating information.
>
> Either
> # let idiv x y =
> match (safe_div (float_of_int x) (float_of_int y) :
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> [ `Success of float | `Div_by_zero ] ) with
> | `Success x -> x
> | _ -> nan ;;
> This expression has type [> `Div_by_zero | `Ok of float ]
> but is here used with type [ `Div_by_zero | `Success of float ]
> The second variant type does not allow tag(s) `Ok
>
> or
> # let idiv x y =
> match(safe_div (float_of_int x) (float_of_int y) :
> [`Div_by_zero | `Ok of float]) with
> | `Success x -> x
> ^^^^^^^^^^^
> | _ -> nan ;;
> This pattern matches values of type [> `Success of 'a ]
> but is here used to match values of type [ `Div_by_zero | `Ok of
> float ]
> The second variant type does not allow tag(s) `Success
>
> Unfortunately, I can't seem to find anything comparable to that "close"
> operator in the documentation, nor any design pattern which would attain
> the same effect.
>
> Does anyone have ideas on this subject ?
Yes: don't use wildcards mixed with concrete cases:
Good: function `A -> ... | `B -> ...
Good: function _ -> ...
Bad: function `A -> ... | _ -> ...
Martin
--
http://wink.com/profile/mjambon
http://martin.jambon.free.fr
next prev parent reply other threads:[~2008-02-21 0:29 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-02-20 20:57 David Teller
2008-02-21 0:29 ` Martin Jambon [this message]
2008-02-21 10:46 ` [Caml-list] " David Teller
2008-02-21 1:54 ` Jacques Garrigue
2008-02-21 12:21 ` David Teller
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=Pine.LNX.4.64.0802210121210.17606@martin.ec.wink.com \
--to=martin.jambon@ens-lyon.org \
--cc=David.Teller@univ-orleans.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