From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 68954BCAB for ; Fri, 27 May 2005 12:54:29 +0200 (CEST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j4RAsQHG010437 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Fri, 27 May 2005 12:54:29 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1DbcXo-00029v-00; Fri, 27 May 2005 11:58:24 +0100 Date: Fri, 27 May 2005 11:58:24 +0100 To: Jonathan Roewen Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] infix functions Message-ID: <20050527105824.GA6650@furbychan.cocan.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.28i From: Richard Jones X-Miltered: at nez-perce with ID 4296FC62.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 infix:01 infix:01 ocaml:01 val:01 pcaml:01 expr:01 expr:01 makefile:01 cmo:01 ocamlc:01 -pp:01 cmo:01 mlast:01 -impl:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: On Fri, May 27, 2005 at 10:06:10PM +1200, Jonathan Roewen wrote: > Hi, > > I see some pervasive functions are infix, and I'm wondering if there's > any plan to support making any arbitrary infix functions? > > For instance, the Int32 (etc) modules are horrible to use cause of the > prefix functions. These are perfect candidates for being infix. And > being an OS project, there are a lot of instances where we need the > extra precision, and having to do things like add some_int32 > another_int32 complex. Especially when you have to throw in > bitshifting, AND and OR, and other magic. You can create infix operators in the basic language. You have to use the right first character in the operator - the scanner appears to use the first character to decide whether the operator is infix or prefix. This is rather obliquely documented here: http://caml.inria.fr/pub/docs/manual-ocaml/manual009.html (Look for the section "Prefix and infix symbols"). So: $ ocaml Objective Caml version 3.08.2 # #load "nums.cma";; # let (+^) = Int32.add;; val ( +^ ) : int32 -> int32 -> int32 = # 2000000000l +^ 1l;; - : int32 = 2000000001l It's also possible to create infix functions; however you have to use the camlp4 preprocessor and your functions become reserved words in the language. Here is an example of an infix function which should get you started: open Pcaml EXTEND expr: AFTER "apply" [ LEFTA [ e1 = expr; "map_with"; e2 = expr -> <:expr< List.map $e2$ $e1$ >> ] ]; END So using that extension you could write code like: list map_with (fun elem -> ...) Use the following Makefile rule to compile the extension: operators.cmo: operators.ml4 $(OCAMLC) -c -pp "camlp4o pa_extend.cmo q_MLast.cmo -impl" -I +camlp4 \ -impl $< and the following rule to compile code using this extension: OCAMLPP := -pp "camlp4o ./operators.cmo" OCAMLC := ocamlc.opt OCAMLCFLAGS := -w s -g $(OCAMLPP) .ml.cmo: $(OCAMLC) $(OCAMLCFLAGS) $(OCAMLCINCS) -c $< Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com