From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id C5E5AD45F for ; Mon, 24 Oct 2005 20:26:55 +0200 (CEST) Received: from web26808.mail.ukl.yahoo.com (web26808.mail.ukl.yahoo.com [217.146.176.84]) by nez-perce.inria.fr (8.13.0/8.13.0) with SMTP id j9OIQtoY004305 for ; Mon, 24 Oct 2005 20:26:55 +0200 Received: (qmail 24836 invoked by uid 60001); 24 Oct 2005 18:26:54 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.de; h=Message-ID:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=oRd+tCSg2G4slNmdI0ZM/WcrPpLCFZl9MyLKtfDTxJcCtU0OzUYerACRJAMGpuICKOAO41nongKvZtOd58B74osAvEb0WFiaqQKJezpwHmpENcEaiukKDYTzSo8EiIMMSP97j9l0KBgY0DC2qcCnhzjRxBbkYbFpvSB3gPxtwiE= ; Message-ID: <20051024182654.24834.qmail@web26808.mail.ukl.yahoo.com> Received: from [83.176.53.249] by web26808.mail.ukl.yahoo.com via HTTP; Mon, 24 Oct 2005 20:26:54 CEST Date: Mon, 24 Oct 2005 20:26:54 +0200 (CEST) From: Martin Chabr Subject: Ant: [Caml-list] how to implement generic operators To: skaller Cc: caml-list@yquem.inria.fr In-Reply-To: <1130131250.28443.79.camel@rosella> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Miltered: at nez-perce with ID 435D276F.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 compiler:01 ocaml:01 ocaml:01 inference:01 iter:01 iter:01 compiler:01 converts:01 combinators:01 combinators:01 binary:01 binary:01 parsing:01 variants:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=DNS_FROM_RFC_ABUSE autolearn=disabled version=3.0.3 I am not an expert and I am not a compiler or language designer. I only use languages and I can only tell you how I feel when I use one language or move to another one. I did some Prolog and some Python programming before I moved to OCaml. In both Prolog and Python you can use lists with mixed types as you suggest. I thing the problem is not mixing types in a list, but what you want to do with the data afterwards. Operators do restrict data types. I was surprised at first when I found out how OCaml restricts lists. I was even more surprised when I learned about the distinction between the integer and floating operators (+ and * vs +. and *. etc.): Then I learned about some problems with type inference in Standard ML which uses overloaded operators. You have to declare the types more in Standard ML than in OCaml. Now I got used to it and appreciate the clarity: types in mixed expressions must always be converted to be compatible with each other. No automatic conversion where you wonder what will happen. C# had collections of mixed types as well in the 1.x version. Now, in the version 2.0 they have generics and strong type checking and everybody seems to like it, not having to do any type detection and casting. Martin --- skaller wrote: > I am looking for some ideas on how to implement > generic > operators. > > To explain what I mean by that: consider some > operations > such as: fold, map, iter. In Ocaml these are > polymorphic. > > A generic iter would be something like: > > iter f [1; 2.0; "hello"] > > --> > > f 1; f 2.0; f "hello" > > Now, within Ocaml itself this doesn't make sense. > However > my question relates to how to *implement* such an > operation > in a compiler. In that case, the list above is a > list > of *terms* not values -- so there is no typing > problem, > provided the reduction rules ground at compile time. > > Now for the problem: there are a LOT of such > operators. > Here is another example: > > compose [f; g; h; k] > > and another > > ravel [f; g; h; k] > > which converts a product of functions to a function > of products. > > I want the USER to be able to define these > operations, > and provide only a minimal set of combinators in the > compiler. > > Obviously, one of the key combinators is fold, since > all of > the above operations suffer from the problem that > whilst > the user can .. as in Ocaml .. easily define a > binary version > of the operator .. there is no way in the target > language > to handle *lists* of arguments: the compiler itself > can do > so easily by using Ocaml lists of terms, but the > user > cannot modify the compiler to add new combinators. > > To make the problem concrete: suppose I would like > to avoid: > > Compose of term list > Ravel of term list > ..... > > each of which uses much the same reduction rules: it > expands > to some tree or using more basic binary primitives > .. > I would clearly need some representation like: > > Assoc of op * term list > > and then some kind of fold which works for all > different binary op, > where now 'op' is defined by the user. > > So crudely the question is: what is the > representation of 'op' > required so the generic fold can create a wide class > of > generic n-ary operations from binary ones? > > Note the data for op will be obtained by parsing in > the input > program, so cannot consist of just a fixed list of > variants. > > Crudely .. I am looking for a way to declare a > function > which instead of having 'two' arguments, can have n. > All the arguments must be the same KIND so their > terms > have the same type, but the arguments obviously will > not > have the same type. > > Hope this makes sense .. I have a feeling MetaOcaml > can do this already. > > -- > John Skaller > Felix, successor to C++: http://felix.sf.net > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: > http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > ___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de