caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: Jonathan Roewen <jonathan.roewen@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] infix functions
Date: Fri, 27 May 2005 11:58:24 +0100	[thread overview]
Message-ID: <20050527105824.GA6650@furbychan.cocan.org> (raw)
In-Reply-To: <ad8cfe7e050527030652565b3@mail.gmail.com>

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


  reply	other threads:[~2005-05-27 10:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-27 10:06 Jonathan Roewen
2005-05-27 10:58 ` Richard Jones [this message]
2005-05-27 12:46   ` padiolea
2005-05-27 14:20     ` William D. Neumann
2005-05-27 11:12 ` Jean-Christophe Filliatre
2005-05-27 11:13 ` Vincenzo Ciancia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050527105824.GA6650@furbychan.cocan.org \
    --to=rich@annexia.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=jonathan.roewen@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).