From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id B3C0BBC68 for ; Mon, 13 Nov 2006 08:29:39 +0100 (CET) Received: from mail.kaba.or.jp (ipb3l.ipl.t.u-tokyo.ac.jp [133.11.138.179]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id kAD7TVCO032448 for ; Mon, 13 Nov 2006 08:29:39 +0100 Received: from [127.0.0.1] (localhost [127.0.0.1]) by mail.kaba.or.jp (Postfix) with ESMTP id E55F72D8C78 for ; Mon, 13 Nov 2006 16:23:58 +0900 (JST) Mime-Version: 1.0 (Apple Message framework v752.2) Content-Transfer-Encoding: 7bit Message-Id: <1EB857FD-F706-419C-9181-D45DEA88B756@mist.i.u-tokyo.ac.jp> Content-Type: text/plain; charset=US-ASCII; format=flowed To: caml-list@inria.fr From: Keisuke Nakano Subject: Simple idea for making a function infix Date: Mon, 13 Nov 2006 16:23:57 +0900 X-Mailer: Apple Mail (2.752.2) X-Miltered: at concorde with ID 45581EDB.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; u-tokyo:01 infix:01 haskell:01 ocaml:01 infix:01 haskell:01 ocaml's:01 syntax:01 ocaml:01 haskell's:01 notation:01 ocaml's:01 syntax:01 blog:98 arbitrary:01 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