Skip to content

OCaml to F# conversions

Eric Taucher edited this page Oct 24, 2013 · 17 revisions

OCaml is written using a syntax which is closer to F# verbose syntax while current main stream F# is typically written using F# lightweight syntax.

OCaml has a different typing system than F# so where some code formatting that works for OCaml will not work for F#. Quite often the only changes needed are in whitespace. e.g. tabs, newlines. At other times, one will need to add |> ignore to keep F# happy, enclose operators in parenthesizes , or change the characters for an operator.

OCaml and F# verbose syntax can handle lines like

let newcps = filter
  (fun (l,(p,q)) ->

but F# light syntax wants

let newcps = filter (fun (l,(p,q)) ->

or

let newcps = 
  filter (fun (l,(p,q)) ->

The use of the Pervasives qualifier is often not needed as most of these functions are now built-in for F#.

The OCaml code here does not rely on a standard set of library functions, yet F# has many built-in functions based on standard OCaml library functions. As such there are many functions in the lib module that can be directly replaced with built-in F# functions. e.g. F# List module functions.

Within functions OCaml will use "let ... in and ..." while F# Light syntax will require "let ... let ...".

OCaml's Read-Eval-Print Loop is known as toplevel while F#'s is known as F# Interactive

To add custom type printers OCaml has the toplevel install_printer directive while F# interactive has AddPrinter

Where the OCaml module system uses include we will use F# open.

The OCaml code uses some F# keywords which can not be used as identifiers. To use an F# keyword as an identifier enclose the keyword within double backtick marks. See: F# 3.0 Language Reference - 3.4 Identifiers and Keywords

OCaml can define the same function more than once in a module while F# will not allow this. To overcome this just rename the first function and change uses of it until reaching the next definition. In class.fs this was done for TAUT.

OCaml has the Format module; for F# we will use Jack's FSharp.Compatibility.OCaml.Format .

OCaml has the Num module; for F# we will use Jack's FSharp.Compatibility.OCaml/Num .

F# uses operator overloading while OCaml uses unique character sequences for many math operators.

Type    OCaml       F#       comment  
Int     +           +        addition  
Float   +.          +        addition  
Num     +/          +        addition; F# version via FSharp.Compatibility.OCaml/Num
String  ^           +        concatenation  
Int     /           /        division  
Float   /.          /        division  
Num     //          /        division; F# version via FSharp.Compatibility.OCaml/Num

Syntax conversions for OCaml to F#

OCaml               F#                Comment
&                   &&                Boolean And
or                  ||                Boolean Or
(o)                 <<                Backward composition operator
land                &&&               Logical And
lor                 |||               Logical Or
lxor                ^^^               Logical Xor
type ('a,'b)func    type func<'a,'b>  Explicit generic construct
string_of_int       string            Conversion to string

10/24/13

See: Languages with Type Inference: OCaml, F#, Scala, Haskell