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=AWL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 9538CBC69 for ; Thu, 8 Mar 2007 13:47:46 +0100 (CET) Received: from pih-relay04.plus.net (pih-relay04.plus.net [212.159.14.131]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l28CljGE013980 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Thu, 8 Mar 2007 13:47:46 +0100 Received: from [80.229.56.224] (helo=beast.local) by pih-relay04.plus.net with esmtp (Exim) id 1HPI25-0005SQ-6W for caml-list@yquem.inria.fr; Thu, 08 Mar 2007 12:47:45 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] F# Date: Thu, 8 Mar 2007 12:41:52 +0000 User-Agent: KMail/1.9.5 References: <200703080113.22721.jon@ffconsultancy.com> <20070308131255.44cc175b.mle+ocaml@mega-nerd.com> In-Reply-To: <20070308131255.44cc175b.mle+ocaml@mega-nerd.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200703081241.52814.jon@ffconsultancy.com> X-Miltered: at discorde with ID 45F005F1.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; compiler:01 ocaml:01 overloading:01 overloading:01 overload:01 constructors:01 inference:01 overload:01 apis:01 uglier:01 inference:01 val:01 ocaml:01 val:01 sml:01 On Thursday 08 March 2007 02:12, Erik de Castro Lopo wrote: > I'm mainly a Linux guy so the chances of me getting up close and personal > with F# are exactly zero :-). Actually the F# compiler and tools are freely available and run under Mono (on Linux). I've had a little play with C# from Mono and was really impressed, but I haven't tried F# from Mono yet. > However, I am interested in hearing about > the differences between F# and Ocaml wrt operator overloading. Care to > clue me (and everyone else) in? I can try. Basically, overloading is very popular in languages like C++ and C#. Programmers even overload functions and constructors in those languages. General overloading doesn't sit well with type inference because the code no longer conveys a sufficient amount of type information to resolve the overload and infer all of the types. So general overloading is bad for type inferred languages like F# and .NET APIs that use overloading make for uglier F# code that uses them. However, operator overloading (i.e. overloading symbols) makes numerical code so much easier to read that it is worth sacrificing some inference for it. The arithmetic operators default to int: > let add a b = a + b;; val add : int -> int -> int So you keep OCaml compatibility. But the operators can be applied to floats: > 1.1 + 2.2;; val it : float = 3.3 SML provides the same capability up to this point but F# allows the overloading to be extended. It already works for vectors: > let a = vector [1.; 2.; 3.] and b = vector [2.; 3.; 4.];; val a : vector val b : vector > a + b;; val it : vector = [3.; 5.; 7.] and you can even extend it to work on your own types by augmenting the type constructor with static member functions: > type expr = | Int of int | Add of expr * expr | Mul of expr * expr with static member ( + ) (f, g) = Add(f, g) static member ( * ) (f, g) = Mul(f, g) end;; Then you can even use + to add symbolic expressions: > Int 3 + Int 4;; val it : expr = Int 3 + Int 4 However, the operator must always be statically resolved, so the code is always fast because it doesn't incur run-time dispatch. That's the basic idea. There are some subtlties, like is the type of + 'a->'a->'a or 'a->'b->'c? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. OCaml for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists