Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

code compatibility between OCaml and F#

Tags:

f#

ocaml

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

like image 788
user515232 Avatar asked Nov 21 '10 17:11

user515232


People also ask

Is F# the same as OCaml?

OCaml is statically typed where there is no need of writing explicit type declaration as the compiler will intimate your types. F# is a strongly typed functional language that uses type inference which means this also does not need a declaration of the type and this is done by the compiler.

Is F# based on OCaml?

Let's start with the obvious, F# is OCaml. It's OCaml backed by the world's largest and most experienced creators of programming languages. And in the areas that OCaml is great, F# is also great! Sum types, static typing, eager execution, pipelines, immutable values, all of this is really great.


2 Answers

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.

like image 149
J D Avatar answered Oct 12 '22 02:10

J D


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.

like image 20
Brian Avatar answered Oct 12 '22 02:10

Brian