24

I know roughly how F# and OCaml differ in terms of "features" (e.g. functors, camlp4, units of measure...).

I wonder about the following: Concerning code that doesn't contain said features, is there a difference in coding style (other than say naming conventions) between F# and OCaml? To put it another way, if some (idiomatic) F# code can be translated in a straight forward way (maybe trivially) to OCaml, will this transformation necessarily lead to idiomatic Ocaml?

Edit: From the links provided by Guy Coder I guess that some "idiomatic" OCaml code might not directly translate to "idiomatic" F# code due to the fact that exceptions are much slower in F# (and more widely used in OCaml). What about the other direction? Will some F#ish OCaml code ever provoke a reaction in terms of "Don't do XX that way, the OCaml way to do XX is with exceptions instead of XY.."

Generally, what are the differences in using exceptions in OCaml when compared to the typical usage in F#. Are there other constructs that are differently used in the two languages (e.g. because of performance penalties in one language)?

objmagic
  • 1,028
  • 1
  • 9
  • 18
D.F.F
  • 914
  • 1
  • 6
  • 17
  • Thanks for taking the time to really explore this question more than a simple glance and looking at the links I provided. I do not actively use OCaml so there are holes in what I can provide however I don't believe that using option types instead of exceptions would not be idiomatic, I see the use of exceptions as a hold over from ML. – Guy Coder Apr 21 '16 at 10:35
  • To really understand this question in more depth if you look at it with the knowledge I have, then you have to not look at just F# and OCaml but the entire ML family, ML, CAML, OCaml, and F#. I believe the CAML is not active or even used in main stream anymore, but I included it for historical reasons. – Guy Coder Apr 21 '16 at 10:36
  • If you really want more details then try reaching out to the members of [ICFP](http://www.icfpconference.org/) or [CUFP](http://cufp.org/2016/) as I am not a member of either. – Guy Coder Apr 21 '16 at 10:39
  • More links you might find of interest are from the questions I have asked related to converting OCaml to F#. See my [questions](http://stackoverflow.com/users/1243762/guy-coder?tab=questions) that start with `Converting OCaml to F#:` – Guy Coder Apr 21 '16 at 10:46
  • `Are there other constructs that are differently used in the two languages` as I noted in my answer the biggest ones are the differences in the libraries and environments, e.g. `.Net` and `Visual Studio` vs. `batteries included`. The type system is also something that one must understand in more detail. I know of a good detailed page on this but I can't find my reference to it at present, but it is in a few of my SO comments. Also [Jon Harrop](http://stackoverflow.com/users/13924/jon-harrop) can give a much better answer as he has used both languages for years in consulting. – Guy Coder Apr 21 '16 at 11:01
  • All of the F# code I ported from OCaml and ML did port and fixing the compiler errors navigated the F# code to idiomatic F# code. I would not be surprised if the reverse were also true. However porting C# to F# code by just fixing the compiler errors does not, in my opinion, always produce idiomatic F# code. Some of the code I see ported from C# should never see the day of light, especially for people learning functional programming. – Guy Coder Apr 21 '16 at 11:10
  • @Guy Coder: Thanks for your further comments! They are very much appreciated. It will take me some time to go through the provided material/links, meanwhile I will accept your answer. – D.F.F Apr 21 '16 at 11:20
  • Another area when going from F# to OCaml to pay attention to is the use of the F# word `inline`. This is added/needed with F# for both performance reasons but what may be unknown is it's use for fixing specific typing problems. – Guy Coder Apr 21 '16 at 11:23
  • Also I tend to add `Of interest` comments to certain questions as I find info in the future, and this is one of those questions are good for dropping such references, so refer back in a year or more and you might find more. – Guy Coder Apr 21 '16 at 11:25
  • Of interest: [F# changes to OCaml](http://stackoverflow.com/questions/179492/f-changes-to-ocaml) – Guy Coder Apr 21 '16 at 18:50
  • Of interest: [F# specification 3.0 Chapter 18. Features for ML Compatibility](http://fsharp.org/specs/language-spec/3.0/FSharpSpec-3.0-final.pdf) – Guy Coder May 02 '16 at 14:28

1 Answers1

23

Yes the translation will be considered idiomatic by many OCaml users for the most part. Since idiomatic is not an exact standard there is no way to measure how idiomatic.

The caveat is that you will have to modify some of the code to make use of features in one language that are not in the other. In particular if a functor is the idiomatic way to do it in OCaml, then you would have to convert the F# code to a Functor. Or if F# made extensive use of the .Net library code, then you would have to recreate those functions in OCaml or find an equivalent.

I have translated my share of ML and OCaml to F# and as you note, and what I refer to as the environment and ethos are what are really the big differences, e.g. functors, camlp4, units of measure, Visual Studio, time travel, NUnit, WPF, etc.

Remember that F# started as OCaml when being ported so for the most part there is still a lot in common.

If you want to see some significant amount of code done in OCaml and F# translated almost line for line take a look at the code for "Handbook of Practical Logic and Automated Reasoning" by John Harrison

F#

OCaml

Then you can judge for yourself.

I do have to note that the code for "Handbook of Practical Logic and Automated Reasoning" does not make use of classes, records, events, and the like. It is almost purely functions.

Another way to compare any two languages in general for idomaticness (OK so I coined that word) is to use Rosetta Code. Rosetta Code contains solutions to programming task written in many languages. So find a programming task, e.g. create a class and then look at the OCaml and F# versions.

Not all of the task are done in all of the languages, but any one can contribute or even suggest new task.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • 2
    The word *idiomaticness* is not that accepted, but it seems to be roughly twice as popular on Google than the word *idiomacity*, which I have come to use. The two words seem to be used mainly by linguists and computer scientists, so I would not frown upon its use within these circles, although *idiomaticness* sounds like it lacks an *-itude*. :-P – sshine Apr 21 '16 at 06:19
  • 1
    Of Interest: [Generalized algebraic data type](https://en.wikipedia.org/wiki/Generalized_algebraic_data_type) and [OCaml Language extensions: Generalized algebraic datatypes](http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc85) – Guy Coder Jun 01 '16 at 20:42