F# changes to OCaml [closed]
Asked Answered
M

3

131

F# is derived from OCaml, but what major items are missing or added? Specifically I'm curious as to whether the resources available for learning OCaml are also useful to someone who wants to learn F#.

Moonwort answered 7/10, 2008 at 17:21 Comment(2)
Attention: if you're interested in the fate of this question, please join the discussion on Meta Stack Overflow or discuss it in chat - the comments here were extraordinarily unproductive, so I have removed them.Litterbug
@JonHarrop I think this link might be the updated one. (By Keiko Nakata)Glyph
H
94

The main differences are that F# does not support:

  • functors
  • OCaml-style objects
  • polymorphic variants
  • the camlp4/5 preprocessor or extension points (ppx)

In addition, F# has a different syntax for labeled and optional parameters.

In theory, OCaml programs that don't use these features can be compiled with F#. Learning OCaml is a perfectly reasonable introduction to F# (and vice versa, I'd imagine).

The complete list of differences is here (note: archive.org replacement of dead link).

Hols answered 7/10, 2008 at 19:27 Comment(4)
Is there a camlp4 equivalent for F#?Jaimiejain
Don't think so. But you could pipe your program through camlp4 and into the F# compiler, probably. (Wouldn't recommend it.)Hols
F# does have named and optional arguments but it uses a different syntax and semantics. You will have trouble using Camlp4 with F# because F# is almost always indentation sensitive so it will need a new lexer and the Camlp4 documentation on lexers has remained unwritten for almost two years.Tseng
I heard somewhere that Microsoft didn't want to add camlp4-like functionality to F# because they were worried that such functionality might encourage people to start using development environments other than Microsoft Visual Studio.Dollydolman
H
127

This question has been answered for some time now, but I was quite surprised that most of the answers say what OCaml features are missing in F# - this is definitely good to know if you want to port existing OCaml programs to F# (which is probably the motivation of most of the referenced articles). However, there are many features that make F# a different language (not just a limited version of OCaml for .NET!) Here is a couple of things that are added in F#:

  • Units of measure that allow you to type-check code dealing with numerical calculations
  • Meta-programming using quotations (which makes it possible to use LINQ in F# and is also essential for promissing projects like the WebSharper platform)
  • Active patterns for creating abstractions for functional data types (and generally very useful feature for more complicated pattern matching applications)
  • Computation expressions which is a language feature behind asynchronous workflows (a library for asynchronous I/O/web service/GUI programming)
  • .NET compatible object-system that makes it possible to fully interoperate with the .NET platform (OCaml also has a support for objects but different - there are of course some benefits in both of the systems).
  • Overloaded operators - As far as I know, OCaml doesn't have overloaded operators - in F# you can use + for all numeric types as well as your types that support it.

And, honestly, I think that it is also worth mentioning the Visual Studio IDE. This is not a part of the language, but it really improves the user experience (IntelliSense support in Visual Studio is really good!)

If you look at the list, there are many things that largely contributed to the popularity of F#, so it's much more than just "OCaml without functors". F# is definitely based on OCaml (and takes ideas from other languages such as Haskell) and shares many aspects with them, however there is also a lot of other things. I guess that without things like asynchronous workflows, .NET style OO and meta-programming, the Microsoft Developer Division would never include F# in Visual Studio 2010.

Horrified answered 21/3, 2010 at 0:10 Comment(9)
Woah! OCaml was specifically bred for metaprogramming it is vastly superior to F# and .NET in that context. Camlp4 is a far more powerful quotation system than F#'s. OCaml's lexers and parsers are way better than anything available for .NET. Also, Camlp4 macros are available that implement active patterns and computation expressions. OCaml also has IDEs that provide the same benefits that Visual Studio does for F#.Tseng
F# quotations have completely different goal than what's available in Camlp4 though (aside, this isn't OCaml language - it is a system on top of it), so it is not fair to compare them. The existence of Camlp4 is certainly an advantage of OCaml, but it has nothing to do with quotations (that allow things like WebSharper, running F# on GPU etc.)Horrified
@Tomas: How do you reconcile your claim that Camlp4 "is a system on top of" OCaml when Camlp4 is integrated with OCaml at the binary level and Camlp4 macros can be defined and used on-the-fly in a running OCaml REPL? How do you reconcile your claim that Camlp4 "has nothing to do with quotations" with the fact that Camlp4 provides a quotation mechanism? How do you reconcile your implication that Camlp4 cannot facilitate the likes of Websharper when SkyDeck's ocamljs tool has been compiling OCaml code quoted using Camlp4 to Javascript since 2007?Tseng
@Tomas: F# cannot even quote expressions with unbound variables like <@ a @>, let alone type definitions like <@ type t = int @> and it cannot handle arbitrary grammars much less extensible lexers and parsers as Camlp4 does. Lack of a decent macro system is one deficiency but, IMHO, the lack of any decent lexers and parsers for F# is a far more serious impediment. I actually advise developers to create their lexers and parsers using OCaml, restricting themselves to the subset that F# supports and porting it back to F# in order to benefit from OCaml's superior tool support!Tseng
As a WebSharper developer, I agree with Jon here. OCAML language and tooling for syntax manipulation are ahead of F#, even quotations considered.Chincapin
@Erik: "Most people focus on what's missing from F#, but I also wanted to see what was missing from OCaml". Note that the active patterns, metaprogramming, computation expressions and asynchronous workflows were available in OCaml before they made it into F#.Tseng
Someone should also mention the OCaml global interpreter lock. I'm not sure how far along they are in fixing this, but it seems to be one of the language's biggest weaknesses.Duwalt
@RickMinerich: OCaml's global lock was removed by the OC4MC project in 2009 but the result is of little practical use because it is so slow and scales so poorly.Tseng
I think Type Providers should be added to listDelmardelmer
H
94

The main differences are that F# does not support:

  • functors
  • OCaml-style objects
  • polymorphic variants
  • the camlp4/5 preprocessor or extension points (ppx)

In addition, F# has a different syntax for labeled and optional parameters.

In theory, OCaml programs that don't use these features can be compiled with F#. Learning OCaml is a perfectly reasonable introduction to F# (and vice versa, I'd imagine).

The complete list of differences is here (note: archive.org replacement of dead link).

Hols answered 7/10, 2008 at 19:27 Comment(4)
Is there a camlp4 equivalent for F#?Jaimiejain
Don't think so. But you could pipe your program through camlp4 and into the F# compiler, probably. (Wouldn't recommend it.)Hols
F# does have named and optional arguments but it uses a different syntax and semantics. You will have trouble using Camlp4 with F# because F# is almost always indentation sensitive so it will need a new lexer and the Camlp4 documentation on lexers has remained unwritten for almost two years.Tseng
I heard somewhere that Microsoft didn't want to add camlp4-like functionality to F# because they were worried that such functionality might encourage people to start using development environments other than Microsoft Visual Studio.Dollydolman
E
12

F# and OCaml are taxonimically classes in the ML family of languages, which includes a whole passle of other weird animals too. F# is newer than OCaml, and it doesn't have either functors [functions of module -> module] or row types [object classes and polymorphic variants] yet. Between them, those two simplifications probably make the learning curve easier for someone developing on the .Net platform. Sadly, those two language features are hugely powerful in OCaml, so reading the OCaml literature to gain insights into how to code for F# will probably lead to premature frustration with the latter when it's probably an excellent alternative to C# where both are available.

Egeria answered 25/5, 2009 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.