From: Markus Mottl <markus@oefai.at>
To: OCAML <caml-list@inria.fr>
Subject: [Caml-list] pattern-matching on partially matched values
Date: Mon, 25 Mar 2002 16:47:22 +0100 [thread overview]
Message-ID: <20020325154722.GA29124@kiefer.ai.univie.ac.at> (raw)
Hello,
I wonder whether this idea has ever been considered for pattern-matching:
Let's say that I have the following (somewhat artificial) code:
type t = A of a | B of b | C of c
match foo with
| A a -> Simply a
| B b -> This (Is (Very (Complicated (WithB b))))
| C c -> This (Is (Very (Complicated (WithC c))))
What I'd like to do is factor out the commonality in such a way:
match foo with
| A a -> Simply a
| _ ->
let f x = This (Is (Very (Complicated x))) in
match foo with
| B b -> f (WithB b)
| C c -> f (WithC c)
When you see "this is very complicated", think of a tree containing
hundreds of (automatically generated) nodes - this is just a small
example.
I could certainly place "let f x = ..." above of the match of "foo",
but this would have several disadvantages: the closure for "f" would
have to be built even when it is not required as in the match case "A a".
Secondly, the definition becomes further removed from the point of use,
making human reasoning more difficult.
It seems to me that it would be rather trivial to maintain a list of
the tried cases for all variables on which a match has been performed,
thus preventing spurious warnings about inexhaustive subsequent matches
on the same. Another example would be:
match foo with
| None -> do_something ()
| _ ->
let cond = do_something_else () in
if cond then
let Some x = foo in
...
else ...
It would then be unnecessary to match all cases immediately to bind
variables. This is not really a problem in this simple example, but may
make a difference when performing superfluous matches on more complicated
patterns.
Would some analysis of this kind make sense in the OCaml-compiler?
Regards,
Markus Mottl
--
Markus Mottl markus@oefai.at
Austrian Research Institute
for Artificial Intelligence http://www.oefai.at/~markus
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
reply other threads:[~2002-03-25 15:47 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20020325154722.GA29124@kiefer.ai.univie.ac.at \
--to=markus@oefai.at \
--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