Browse thread
[Caml-list] infix functions
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2005-05-27 (10:54) |
From: | Richard Jones <rich@a...> |
Subject: | Re: [Caml-list] infix functions |
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 = <fun> # 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