caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] F#
Date: Thu, 8 Mar 2007 12:41:52 +0000	[thread overview]
Message-ID: <200703081241.52814.jon@ffconsultancy.com> (raw)
In-Reply-To: <20070308131255.44cc175b.mle+ocaml@mega-nerd.com>

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


  reply	other threads:[~2007-03-08 12:47 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-08  1:13 Interactive technical computing Jon Harrop
2007-03-08  1:49 ` [Caml-list] " Jim Miller
2007-03-08  2:52   ` skaller
2007-03-08  3:00     ` Jim Miller
2007-03-08  3:10       ` skaller
     [not found]         ` <beed19130703071919g1f537f59o93ce06871fba8f3a@mail.gmail.com>
2007-03-08  3:27           ` skaller
2007-03-08  3:36             ` Jim Miller
2007-03-08 21:16               ` Richard Jones
     [not found]                 ` <45F10E90.5000707@laposte.net>
2007-03-09  7:43                   ` Matthieu Dubuget
2007-03-10 14:58                     ` Richard Jones
2007-03-08 12:22             ` Gerd Stolpmann
2007-03-08 14:24               ` Christophe TROESTLER
2007-03-08 19:34                 ` Jon Harrop
2007-03-08 20:34                   ` Christophe TROESTLER
2007-03-09 10:22                     ` Jon Harrop
2007-03-09 10:45                       ` Christophe TROESTLER
2007-03-08  2:12 ` Erik de Castro Lopo
2007-03-08 12:41   ` Jon Harrop [this message]
2007-03-08 11:12 ` Andrej Bauer
2007-03-08 11:59 ` Vu Ngoc San
2007-03-08 12:43   ` Jon Harrop
2007-03-08 21:28     ` Vu Ngoc San
2007-03-09  0:14       ` skaller
  -- strict thread matches above, loose matches on Subject: below --
2007-03-08 14:41 [Caml-list] F# Robert Fischer
2007-03-08 15:10 ` Jon Harrop
2007-03-08 17:30   ` Roland Zumkeller
2007-03-08 17:54     ` Brian Hurt
2007-03-08 23:07       ` skaller
2002-06-10 14:04 Don Syme
     [not found] <BCDB2C3F59F5744EBE37C715D66E779C0481E208@red-msg-04.redmon d.corp.microsoft.com>
2002-06-09 17:49 ` Chris Hecker
2002-06-09 12:26 Don Syme
2002-06-10  6:22 ` Michael Vanier
     [not found] <BCDB2C3F59F5744EBE37C715D66E779C0481E206@red-msg-04.redmon d.corp.microsoft.com>
2002-06-09  1:30 ` Chris Hecker
2002-06-08 23:04 Don Syme
2002-06-08 15:05 Vincent Foley
2002-06-08 16:01 ` Xavier Leroy

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=200703081241.52814.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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;
as well as URLs for NNTP newsgroup(s).