Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Keisuke Nakano <ksk@mist.i.u-tokyo.ac.jp>
To: caml-list@inria.fr
Subject: Simple idea for making a function infix
Date: Mon, 13 Nov 2006 16:23:57 +0900	[thread overview]
Message-ID: <1EB857FD-F706-419C-9181-D45DEA88B756@mist.i.u-tokyo.ac.jp> (raw)

Hi all,

Haskell people sometimes complain about that OCaml cannot make an
arbitrary function infix. For example, they can write (3 `min` 4)
to get the result of (min 3 4) in Haskell. Can we satisfy them
without changing OCaml's syntax?

Here is a simple idea for making a function infix in OCaml.
I hope it will be useful for those who like Haskell's backquote
notation `function_name`.
The idea doesn't require any change of OCaml's syntax.

We use the following two infix operators.

   let ( /* ) x y = y x
   and ( */ ) x y = x y

Then we can make an infix operator /*f*/ for a binary function f.
For example, using binary functions 'min' and 'max', we can write

   3 /*min*/ 4 + 6 /*max*/ 8

to get 11 as 'min 3 4 + max 5 8'. Note that the infix operator
( */ ) may conflict with Num.( */ ) if the Num module is loaded
and opened. You can use other definitions in a similar manner, though.

You have to take care of the precedence. For example,

   3 /*min*/ 4 * 6 /*max*/ 8

will return 18 as 'max ((min 3 4) * 6) 8'. So we should write

   (3 /*min*/ 4) * (6 /*max*/ 8)

to get 24 as 'min 3 4 * max 6 8'.


The original idea was introduced in my blog a few months ago
(written in Japanese, though). At that time, I used other definitions:

   let ( <| ) x y = y x
   and ( |> ) x y = x y

or

   let ( @^ ) x y = y x
   and ( ^@ ) x y z = x z y

where the definition of ( ^@ ) should be given in a different way
because of the precedence of infix operaters starting with '^' or '@'.
These operators perform a different behavior because of the precedences
of operators.

   3 <|min|> 4 + 6 <|max|> 8 (* = max (min 3 (4 + 6)) 8 => 8 *)
   3 @^min^@ 4 + 6 @^max^@ 8 (* = min 3 (max (4 + 6) 8) => 3 *)

So you have to write

   (3 <|min|> 4) + (6 <|max|> 8)
   (3 @^min^@ 4) + (6 @^max^@ 8)

to get 11 as min 3 4 + max 5 8'.


Sincerely,

-----------------------------------------------------------------------
Keisuke Nakano
Department of Mathematical Informatics,
University of Tokyo


             reply	other threads:[~2006-11-13  7:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-13  7:23 Keisuke Nakano [this message]
2006-11-13 14:31 ` [Caml-list] " Chris King
2006-11-13 16:19 ` Till Varoquaux
2006-11-13 17:45   ` Jean-Christophe Filliatre
2006-11-13 17:58   ` skaller
2006-11-13 18:29     ` Till Varoquaux
2006-11-13 20:31       ` Karl Zilles
2006-11-13 21:06         ` Till Varoquaux
2006-11-14  4:29 oleg

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=1EB857FD-F706-419C-9181-D45DEA88B756@mist.i.u-tokyo.ac.jp \
    --to=ksk@mist.i.u-tokyo.ac.jp \
    --cc=caml-list@inria.fr \
    /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