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 JAA14811; Sat, 11 Sep 2004 09:49:31 +0200 (MET DST) 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 JAA15967 for ; Sat, 11 Sep 2004 09:49:30 +0200 (MET DST) Received: from smtp1.adl2.internode.on.net (smtp1.adl2.internode.on.net [203.16.214.181]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i8B7nR4W025457 for ; Sat, 11 Sep 2004 09:49:29 +0200 Received: from [192.168.1.200] (ppp210-32.lns2.syd3.internode.on.net [203.122.210.32]) by smtp1.adl2.internode.on.net (8.12.9/8.12.9) with ESMTP id i8B7nP4Y032251 for ; Sat, 11 Sep 2004 17:19:26 +0930 (CST) Subject: [Caml-list] polymorphic variant typing question From: skaller Reply-To: skaller@users.sourceforge.net To: caml users Content-Type: text/plain Message-Id: <1094888964.27775.39.camel@pelican.wigram> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 11 Sep 2004 17:49:25 +1000 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4142AE07.003 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; sourceforge:01 char:01 seq:01 kleene:01 gre:99 gcode:99 gre:99 gcode:99 incr:01 seq:01 char:01 combinator:01 compiles:01 functor:01 9660:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Consider this: type ('x, 'a) regexp_t' = [ | `REGEXP_char of int | `REGEXP_seq of 'x * 'x (** concatenation *) | `REGEXP_alt of 'x * 'x (** alternation *) | `REGEXP_aster of 'x (** Kleene closure *) | `REGEXP_epsilon (** epsilon: null string *) | `REGEXP_code of 'a (** associated code *) ] type 'a regexp_t = ('x, 'a) regexp_t' as 'x (* extend the regexp type -- polymorpic variants are wonderful! *) type 'a gre_t = [ | ('b,'a) regexp_t' | `REGEXP_group of 'b ] as 'b ;; type 'b gcode = [`User of 'b | `Left of int | `Right of int ] ;; let groupify (re: 'a gre_t) : 'a gcode regexp_t = let counter = ref 0 in let rec aux (re: 'a gre_t) : 'a gcode regexp_t = match re with | `REGEXP_group a -> let n = !counter in incr counter; let a = aux a in seq (seq (code (`Left n)) a) (code (`Right n)) | `REGEXP_code a -> `REGEXP_code (`User a) | `REGEXP_alt (a,b) -> let a = aux a in let b = aux b in `REGEXP_alt (a,b) | `REGEXP_seq (a,b) -> let a = aux a in let b = aux b in `REGEXP_alt (a,b) | `REGEXP_aster a -> `REGEXP_aster (aux a) | `REGEXP_epsilon -> `REGEXP_epsilon | `REGEXP_char c -> `REGEXP_char c in aux re What this does is extend the regular expression type to allow a group combinator, but then reduce it by using left/right bracket codes. This typing compiles. Question: how can I replace the last two lines of the match with something which just handles 'all the rest of the cases'? Those cases are invariant, but a coercion | (x : 'a gre_t ) -> ( x : 'a gre_t :> 'a gcode regexp_t) doesn't work because in general, 'a gre_t isn't a subtype of 'a gcode regexp_t. I need to reduce the type functor from gre_t to regexp_t, and 'a to 'a gcode. Do I have to factor the original regexp_t into invariant and variant parts?? [BTW: the typing of all this is awesome!] -- John Skaller, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ------------------- 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