caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: maf@microsoft.com, caml-list@inria.fr
Subject: RE: variant filtering
Date: Mon, 21 Feb 2000 11:44:55 +0900	[thread overview]
Message-ID: <20000221114455F.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: Your message of "Sun, 20 Feb 2000 15:58:13 -0800" <783D93998201D311B0CF00805FEAA07B7E9101@RED-MSG-42>

From: Manuel Fahndrich <maf@microsoft.com>

> yes, I see that one has to be able to capture the row variable and it would
> be part of two different types. The work-around you suggest is fine when the
> number of variant cases is small and known. However, one nice thing about
> polymorphic variants is their openness. Thus, enumerating all the other
> cases is not really an option in many cases.

There are two different problems here. One of expressiveness (all
cases have to be known, clearly variants in ocaml are not as
expressive as in Wallace), and another of conciseness (the need to
explicitely write all cases).

I think that expressiveness is not the main problem. The added
expressive power is in my view too complex to be used in most
programs. And in general open pattern matching on variants is not a
good idea, beacuse it weakens the type checking (typos do not cause
type errors).

There are solutions for the conciseness problem, like allowing one to
write a name of variant type instead of the complete case list:

type failures = [`FailureA | `FailureB | `FailureC]

let g () =
  match f () with
    `Success s -> `Success 1
  | #failures as other -> other

Would it be enough?

Remark also that most examples that would use the extra expressiveness
of explicit row variables can also be rewritten in this formalism:

# let f0 = function
      `SuccessA x -> x
    | `SuccessB x -> x*2;;
val f0 : [<`SuccessA int|`SuccessB int] -> int

With row variables:
# let protect f x =
    match x with
      `FailureA -> -1
    | `FailureB -> -2
    | `FailureC -> -3
    | other -> f other;;
val protect : (['r] -> int) -> [<`FailureA|`FailureB|`FailureC|'r] -> int
# let g = protect f0;;
g : [<`FailureA|`FailureB|`FailureC|`SuccessA int|`SuccessB int] -> int

Without row variables:
# let handler x =
    match x with
      `FailureA -> -1
    | `FailureB -> -2
    | `FailureC -> -3;;
val handler : [<`FailureA|`FailureB|`FailureC] -> int
# let g = function
    | #success as x -> f0 x
    | #failures as x -> handler x;;
g : [<`FailureA|`FailureB|`FailureC|`SuccessA int|`SuccessB int] -> int

As you can see, the main difference is that you have to add some glue
by hand. This is less abstract than only applying a function, but the
advantage is that you stay at a simpler level of reasonning. In
practice I believe you do not get more verbose.

Jacques
---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>




       reply	other threads:[~2000-02-21 17:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <783D93998201D311B0CF00805FEAA07B7E9101@RED-MSG-42>
2000-02-21  2:44 ` Jacques Garrigue [this message]
2000-02-18  1:15 Manuel Fahndrich
2000-02-18 14:41 ` Jacques Garrigue

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=20000221114455F.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=maf@microsoft.com \
    /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).