caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] infix functions
@ 2005-05-27 10:06 Jonathan Roewen
  2005-05-27 10:58 ` Richard Jones
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jonathan Roewen @ 2005-05-27 10:06 UTC (permalink / raw)
  To: caml-list

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.

Jonathan


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] infix functions
  2005-05-27 10:06 [Caml-list] infix functions Jonathan Roewen
@ 2005-05-27 10:58 ` Richard Jones
  2005-05-27 12:46   ` padiolea
  2005-05-27 11:12 ` Jean-Christophe Filliatre
  2005-05-27 11:13 ` Vincenzo Ciancia
  2 siblings, 1 reply; 6+ messages in thread
From: Richard Jones @ 2005-05-27 10:58 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] infix functions
  2005-05-27 10:06 [Caml-list] infix functions Jonathan Roewen
  2005-05-27 10:58 ` Richard Jones
@ 2005-05-27 11:12 ` Jean-Christophe Filliatre
  2005-05-27 11:13 ` Vincenzo Ciancia
  2 siblings, 0 replies; 6+ messages in thread
From: Jean-Christophe Filliatre @ 2005-05-27 11:12 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list


Jonathan Roewen writes:
 > 
 > 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.

In some simple cases, it can help to insert

	let (+) = Int32.add
	let (-) = Int32.sub
	...

at the  beginning of your files  (or better to  put these declarations
within  a small  module that  you open  only when  you need  the infix
notation).  You  can even  adopt  other  notations,  such as  +!,  -!,
etc.  Only the  first  character  is used  to  determine the  operator
precedence.

Beware of the lexical issue with multiplication :-)

Hope this helps,
-- 
Jean-Christophe


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: infix functions
  2005-05-27 10:06 [Caml-list] infix functions Jonathan Roewen
  2005-05-27 10:58 ` Richard Jones
  2005-05-27 11:12 ` Jean-Christophe Filliatre
@ 2005-05-27 11:13 ` Vincenzo Ciancia
  2 siblings, 0 replies; 6+ messages in thread
From: Vincenzo Ciancia @ 2005-05-27 11:13 UTC (permalink / raw)
  To: caml-list

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.

Not arbitrary, but there are some "free" symbols that can be defined (I
don't know exactly how many and what, but I guess it's on the manual or in
the lexer :) ), and all the infix operators can be redefined. Example of
redefining "+":

########
# module InfixInt32 = struct
    let (+) = Int32.add
  end;;
module InfixInt32 : sig val ( + ) : int32 -> int32 -> int32 end
# 3+3;;
- : int = 6
# open InfixInt32;;
# 3+3;;
This expression has type int but is here used with type int32
#######

example of defining "+*"

#######
# let (+*) = Int32.add;;
val ( +* ) : int32 -> int32 -> int32 = <fun>
# 30l +* 20l;;
- : int32 = 50l
#######

I don't know if there is a way to force inlining of "+*" though, but I
suppose that you could finally resort to defining your operators in camlp4

http://pauillac.inria.fr/caml/camlp4/manual/manual006.html

see section 5.3.1

Bye

Vincenzo

-- 
Please note that I do not read the e-mail address used in the from field but
I read vincenzo_ml at yahoo dot it
Attenzione: non leggo l'indirizzo di posta usato nel campo from, ma leggo
vincenzo_ml at yahoo dot it



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] infix functions
  2005-05-27 10:58 ` Richard Jones
@ 2005-05-27 12:46   ` padiolea
  2005-05-27 14:20     ` William D. Neumann
  0 siblings, 1 reply; 6+ messages in thread
From: padiolea @ 2005-05-27 12:46 UTC (permalink / raw)
  To: Richard Jones; +Cc: Jonathan Roewen, caml-list

> 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,

Can we access the code of this OS project ?
I know that there is another OS project with caml called funk at
  http://perso.ens-lyon.fr/samuel.mimram/docs/funk_doc/
and another one called House but with haskell
 http://www.cse.ogi.edu/~hallgren/House/
that you might be interested in.

> 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 -> ...)

I think it is simpler for such cases to have a generic operator such as
let (+>) o f = f o

and then just do
 [1;2;3;4] +> map (fun x -> x+1)
which is reminescent of object notation.






^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] infix functions
  2005-05-27 12:46   ` padiolea
@ 2005-05-27 14:20     ` William D. Neumann
  0 siblings, 0 replies; 6+ messages in thread
From: William D. Neumann @ 2005-05-27 14:20 UTC (permalink / raw)
  To: padiolea; +Cc: Richard Jones, Jonathan Roewen, caml-list

On Fri, 27 May 2005 padiolea@irisa.fr wrote:

> Can we access the code of this OS project ?
> I know that there is another OS project with caml called funk at
>  http://perso.ens-lyon.fr/samuel.mimram/docs/funk_doc/
> and another one called House but with haskell
> http://www.cse.ogi.edu/~hallgren/House/
> that you might be interested in.

You can take a look at it ever at <http://dst.purevoid.org/>.  The Apirl 
27th entry on the main page contains a link to the SVN repository.  I know 
I've been hoping to get a chance to play around with what they've got and 
see if I can't contribute some for a while now -- hopefully life will play 
nice with me in that regard soon.

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers.  We don't need them, we're not doing 
anything with them.

Tigers are noble and sleek; children are loud and messy."

         -- Neko Case

  Think of XML as Lisp for COBOL programmers.

 	-- Tony-A (some guy on /.)


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-05-27 14:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-27 10:06 [Caml-list] infix functions Jonathan Roewen
2005-05-27 10:58 ` Richard Jones
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

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).