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 SAA15082; Mon, 5 Jul 2004 18:39:48 +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 SAA15148 for ; Mon, 5 Jul 2004 18:39:47 +0200 (MET DST) Received: from fichte.ai.univie.ac.at (fichte.ai.univie.ac.at [131.130.174.156]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i65GdjEV006869 for ; Mon, 5 Jul 2004 18:39:46 +0200 Received: from fichte.ai.univie.ac.at (markus@localhost [127.0.0.1]) by fichte.ai.univie.ac.at (8.12.3/8.12.3/Debian-6.6) with ESMTP id i65GdhDu009468; Mon, 5 Jul 2004 18:39:43 +0200 Received: (from markus@localhost) by fichte.ai.univie.ac.at (8.12.3/8.12.3/Debian-6.6) id i65Gdhe9009467; Mon, 5 Jul 2004 18:39:43 +0200 Date: Mon, 5 Jul 2004 18:39:43 +0200 From: Markus Mottl To: "Jojgov, G.I." Cc: caml-list@inria.fr Subject: Re: [Caml-list] Translation between datatypes with binding Message-ID: <20040705163943.GA8625@fichte.ai.univie.ac.at> Mail-Followup-To: "Jojgov, G.I." , caml-list@inria.fr References: <9F38CF35D80CAE409B979F3EB5242B4A014B1CCF@winex2.campus.tue.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9F38CF35D80CAE409B979F3EB5242B4A014B1CCF@winex2.campus.tue.nl> User-Agent: Mutt/1.4.1i X-Miltered: at nez-perce with ID 40E98451.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 datatypes:01 ideally:01 functors:01 -rectypes:01 expr:01 expr:01 datatypes:01 higher-order:01 model:01 val:01 reuse:01 higher-order:01 reuse:01 compiler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Mon, 05 Jul 2004, Jojgov, G.I. wrote: > I am trying to use OCAML as a logical framework to implement two systems > with binding and to define a translation from one to the other. Ideally, > I would like to be able to write code like this It may be more convenient to use polymorphic variants here (see below for example), because you can keep the code more abstract. Another possibility would be to use type functors. To make the latter convenient, this would however require the use of the compiler flag "-rectypes" so I won't go into details. Here is my solution using polymorphic variants: --------------------------------------------------------------------------- let rec trans trans_id inv_trans = function | `SomeValue id -> `SomeValue (trans_id id) | `SomeBinder (id, body) -> let trans_body v = inv_trans (body (trans trans_id inv_trans v)) in `SomeBinder (trans_id id, trans_body) let rec trans_1_2 expr1 = trans string_of_int trans_2_1 expr1 and trans_2_1 expr2 = trans int_of_string trans_1_2 expr2 --------------------------------------------------------------------------- > (this is a simplified version of the real datatypes and the translation > that I want to use) It's not correct: you also need a conversion function from expr2 to expr1 in order to implement translations for higher-order stuff (i.e. functions like "body"). Objects won't help here either, because they, too, cannot have a model for the following in the general case (you'd always be stuck with a finite amount of translations): val invert : ('a -> 'b) -> ('b -> 'a) > My questions are: Is there a natural way to define a function like the > translation function above on datatypes? Is there another way to > implement these structures? I guess the only solution is to use classes, > but I already have some code dealing with the two concrete types and > preferably I would like to reuse the code. If you can't do something with algebraic datatypes, you won't be able to do it with objects either. If you are not afraid of higher-order functions, code reuse is also easy with algebraic datatypes. Regards, Markus -- Markus Mottl http://www.oefai.at/~markus markus@oefai.at ------------------- 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