18

Good day all,

I am developing a small hobby project in OCaml. I was wondering how easy it would be to migrate it to F#. I know that F# has some features that OCaml doesn't, but I was hoping that my OCaml code would require little effort to port. I don't necessarily want to migrate, I want to retain / develop on both platforms.

Thanks in advance, Michael

user515232
  • 211
  • 1
  • 5

3 Answers3

15

Writing cross-compiling code looks like a world of pain to me. John Whitington of Coherent PDF is the only person I know of who has tried to do this to any real degree.

I have translated a lot of OCaml code to F# (probably more than anyone else in the world) and the main problems are #light syntax, the use of any non-trivial OCaml features (objects, polymorphic variants, higher-order modules, labelled and optional arguments and so on), libraries (e.g. lablgl, lablgtk, ocamlgraph, laziness), macros (parsing, streams, pattern matching extensions) and changes in basic syntax such as array indexing. For example, I just tried to port the Almabench benchmark from OCaml to F# and it took several hours because I ended up by having to rewrite every a.[i] to an a.(i) by hand due to a multitude of bugs in the F# compiler: its OCaml compatibility mode is quite fragile.

So I would advise you to choose between the languages rather than trying to cross-compile.

J D
  • 48,105
  • 13
  • 171
  • 274
  • Thanks a lot. What I wanted to avoid was learning an extra language. Since I have a working knowledge of .NET (C#) and basic knowledge of OCaml (from my func. programing course) I though that F# was (almost) a given. Apparently it's not. :) – user515232 Nov 21 '10 at 20:24
  • 1
    No problem. You'll probably find it more of a help than a hindrance to learn both OCaml and F# at the same time because they are so similar but you will probably find the differences very enlightening as well. – J D Nov 21 '10 at 23:11
7

You should read the final portion of the spec

Features for ML Compatibility

and be sure to grab FSharp.PowerPack.Compatibility.dll from the PowerPack for various compat libraries.

Brian
  • 117,631
  • 17
  • 236
  • 300
0

I haven't done any OCaml to F#-porting, but I know F# has been designed with OCaml compatibility in mind. As I know, F# is mostly a superset of basic OCaml.

It should support (and without #light be limited to) most OCaml keywords and have equivalents of most standard functions in the core libraries (or in the .Net framework). So I'd guess that - at least for hobby projects - porting should be actually easy.

F# doesn't have OCaml's advanced module system though, so you'll have trouble with implementing e.g. functors.

Dario
  • 48,658
  • 8
  • 97
  • 130
  • This was true several years ago but I don't think it is accurate any more. A lot of basic stuff in F# has long-since diverged from OCaml. For example, indexing an array is `a.(i)` in OCaml but `a.[i]` with a type annotation in F#. OCaml's basic arithmetic operators over floating point numbers (`+.`, `*.` etc.) no longer work correctly in F#. – J D Feb 26 '11 at 13:27
  • 1
    F# is not mostly a superset of OCaml, but a subset with a few extensions (active patterns, etc.). Jon's answer lists some of the features of OCaml that are not supported by F#. – Fabrice Le Fessant Feb 20 '12 at 09:03