<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2005/05/3911702a49feba6f0afb3ecbb9030cff"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2005-05-27T10:54:29"
  subject="Re: [Caml-list] infix functions"
  prev="2005/05/b3e1a86c65afa1b9438f0c6828902fae"
  next="2005/05/a75422b52de2316685268a49e63d92b7"
  prev-in-thread="2005/05/b3e1a86c65afa1b9438f0c6828902fae"
  next-in-thread="2005/05/ec338a887ac5c965576076298f0bdb53"
  prev-thread="2005/05/71cd7e87cf18096774a9be24b165c5d0"
  next-thread="2005/05/0863ff06ae4cdb1a42c798a087362566"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] infix functions">
<msg 
  url="2005/05/b3e1a86c65afa1b9438f0c6828902fae"
  from="Jonathan Roewen &lt;jonathan.roewen@g...&gt;"
  author="Jonathan Roewen"
  date="2005-05-27T10:06:11"
  subject="[Caml-list] infix functions">
<msg 
  url="2005/05/3911702a49feba6f0afb3ecbb9030cff"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2005-05-27T10:54:29"
  subject="Re: [Caml-list] infix functions">
<msg 
  url="2005/05/ec338a887ac5c965576076298f0bdb53"
  from="padiolea@i..."
  author="padiolea@i..."
  date="2005-05-27T12:46:16"
  subject="Re: [Caml-list] infix functions">
<msg 
  url="2005/05/3405c44764e476f00f816b68d6cb19a6"
  from="William D. Neumann &lt;wneumann@c...&gt;"
  author="William D. Neumann"
  date="2005-05-27T14:20:06"
  subject="Re: [Caml-list] infix functions">
</msg>
</msg>
</msg>
<msg 
  url="2005/05/a75422b52de2316685268a49e63d92b7"
  from="Jean-Christophe Filliatre &lt;Jean-Christophe.Filliatre@l...&gt;"
  author="Jean-Christophe Filliatre"
  date="2005-05-27T11:12:56"
  subject="Re: [Caml-list] infix functions">
</msg>
<msg 
  url="2005/05/ecd76077d2f949effe289b3db4889044"
  from="Vincenzo Ciancia &lt;vincenzo_yahoo_addressguard-gmane@y...&gt;"
  author="Vincenzo Ciancia"
  date="2005-05-27T11:14:07"
  subject="Re: infix functions">
</msg>
</msg>
</thread>

<contents>
On Fri, May 27, 2005 at 10:06:10PM +1200, Jonathan Roewen wrote:
&gt; Hi,
&gt; 
&gt; I see some pervasive functions are infix, and I'm wondering if there's
&gt; any plan to support making any arbitrary infix functions?
&gt; 
&gt; For instance, the Int32 (etc) modules are horrible to use cause of the
&gt; prefix functions. These are perfect candidates for being infix. And
&gt; being an OS project, there are a lot of instances where we need the
&gt; extra precision, and having to do things like add some_int32
&gt; another_int32 complex. Especially when you have to throw in
&gt; 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 -&gt; int32 -&gt; int32 = &lt;fun&gt;
	# 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 -&gt;
	           &lt;:expr&lt; List.map $e2$ $e1$ &gt;&gt;
	       ]
	   ];
	END

So using that extension you could write code like:

	list map_with (fun elem -&gt; ...)

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 $&lt;

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 $&lt;

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

</contents>

</message>

