From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA09989; Mon, 25 Mar 2002 16:47:26 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA10845 for ; Mon, 25 Mar 2002 16:47:25 +0100 (MET) Received: from kiefer.ai.univie.ac.at (kiefer.ai.univie.ac.at [131.130.174.157]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g2PFlNP09360 for ; Mon, 25 Mar 2002 16:47:24 +0100 (MET) Received: (from markus@localhost) by kiefer.ai.univie.ac.at (8.9.3/8.9.3/Debian 8.9.3-21) id QAA29863 for caml-list@inria.fr; Mon, 25 Mar 2002 16:47:22 +0100 Date: Mon, 25 Mar 2002 16:47:22 +0100 From: Markus Mottl To: OCAML Subject: [Caml-list] pattern-matching on partially matched values Message-ID: <20020325154722.GA29124@kiefer.ai.univie.ac.at> Mail-Followup-To: OCAML Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.26i Organization: Austrian Research Institute for Artificial Intelligence Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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