r/ocaml • • Jul 14 '22

F# (in)compatibility

I'm just playing with F# coming from an OCaml background. There's lots of cool stuff in both languages but I cannot help but wonder why so many functions common to both languages have different names.

  F#            OCaml
  Seq.tryPick   Seq.find_map
  Seq.truncate  Seq.take
  Seq.skip      Seq.drop
  Seq.skipWhile Seq.drop_while
  Seq.cache     Seq.memoize
  Seq.collect   Seq.concat_map or Seq.flat_map
  Seq.fold      Seq.fold_left
  Seq.choose    Seq.filter_map

Why is this?

5 Upvotes

39 comments sorted by

21

u/yawaramin Jul 14 '22

F# and OCaml are different languages maintained by different teams. There is no compatibility goal between them.

0

u/PurpleUpbeat2820 Jul 14 '22

F# and OCaml are different languages maintained by different teams. There is no compatibility goal between them.

I get what you're saying but the intersection between OCaml and F# is relatively huge. I've been playing with lots of basic ML code using algebraic datatypes, tuples, pattern matching, functions including lambdas and so on and it all runs in both languages with zero changes. So there appears to have been a great deal of compatability, perhaps historically and it is no longer an on-going concern. Guessing: maybe because F# was bootstrapped from an initial OCaml implementation?

10

u/yawaramin Jul 14 '22

Perhaps initially but it very quickly became a non-goal. Don Syme, the creator of F#, very explicitly wanted to avoid OCaml's type-erased implementation of generics, and went for reified generics instead: https://inbox.ocaml.org/caml-list/D5DD834CDA73854DA0ADC8B3C953C681066F91A5@EUR-MSG-11.europe.corp.microsoft.com/

There is some similarity in the core language implementation, but the more you explore, you notice a lot more differences. Just assuming that everything will be compatible will lead to surprises down the road.

1

u/PurpleUpbeat2820 Jul 15 '22

There is some similarity in the core language implementation, but the more you explore, you notice a lot more differences. Just assuming that everything will be compatible will lead to surprises down the road.

Sound advice. Thank you!

2

u/vorotato Jul 14 '22

Some members of the community have argued for similarity where it makes sense, which is a significant part of why many parts "just work", but there is no guarantee that you can paste code in and it "works", or that things won't work differently. There was an effort at one time to ensure compatibility. I believe it was argued that not enough people used both and that it was a lot of effort and sometimes lead to awkwardness with C# interop. I personally have argued for similarity but not compatibility, there are a lot of ML languages and in my opinion changing the syntax to look more like C/C++/C# lineage only confuses people into a false sense of familiarity. They are still close enough that translating between the two is often quite achievable, which in my opinion is a benefit to both ecosystems.

1

u/PurpleUpbeat2820 Jul 14 '22

They are still close enough that translating between the two is often quite achievable, which in my opinion is a benefit to both ecosystems.

Absolutely. I'm having lots of fun playing with them both right now.

You wouldn't happen to know if there is a "cheat sheet" that gives all of the translations like the examples I gave?

2

u/vorotato Jul 14 '22

I haven't heard of one, but if you document these as you find them it will make it much easier to move between.

1

u/PurpleUpbeat2820 Jul 14 '22

Will do. Thanks!

14

u/emaphis Jul 14 '22

F# is meant to be a .Net language. It's supposed to fit into the .Net ecosystem, consuming and producing .Net libraries along with standard functional programming. 'Seq' implements one of .Net's Collection framework interfaces so .Net programmers can easily use 'Seq' and 'Seq' can be integrated with .Net collections code.

-3

u/PurpleUpbeat2820 Jul 14 '22 edited Jul 14 '22

Absolutely. And OCaml's Seq module implements a similar data structures to F#'s Seq module with many identical functions and, yet, they appear to have been deliberately given different names. Is there a reason for this?

11

u/ct075 Jul 14 '22 edited Jul 14 '22

You say "deliberately" as if they made the conscious decision to do something different than F#, but I don't think that's fair.

For one, OCaml predates F#, so if anything, F# is the one diverging -- why should OCaml use the names from a framework that it doesn't target? Secondly, from a functional programming perspective, F# is the one with weird names -- the take and dropWhile language is mirrored in many other functional languages, so why is F# deliberately using a different name from the standard?

EDIT: Sorry, on rereading your original post, it seems that I've gotten the direction of your complaint wrong. I think the main answer is that the F# names were chosen to be more familiar to non-functional programmers (as the names would be reflected in other .NET languages, not just F#).

1

u/PurpleUpbeat2820 Jul 14 '22 edited Jul 14 '22

You say "deliberately" as if they made the conscious decision to do something different than F#, but I don't think that's fair.

I mean that in the sense that OCaml appears to have copied Seq from F# but changed the names of many things.

OCaml predates F#

OCaml is older than F# but OCaml only just got most of this functionality in 4.14 and it appears to have been heavily inspired by F#. Another example is the pipeline operator |> which apparently comes from F# but they did not change the name of that.

EDIT: Sorry, on rereading your original post, it seems that I've gotten the direction of your complaint wrong. I think the main answer is that the F# names were chosen to be more familiar to non-functional programmers (as the names would be reflected in other .NET languages, not just F#).

Right. That makes sense. I know nothing about .NET or C# so I guess I cannot tell.

9

u/ct075 Jul 14 '22

OCaml is older than F# but OCaml only just got most of this functionality and it appears to have been taken directly from F#.

The functions use the same names as the equivalents from the List module, which uses names that long predate both OCaml and F#. I'd certainly rather that Seq match another container structure in its own language rather than some F# standard.

1

u/PurpleUpbeat2820 Jul 14 '22 edited Jul 14 '22

The functions use the same names as the equivalents from the List module

Do they? OCaml's List module doesn't appear to contain most of these functions, e.g. skip, drop, take. Others like memoize don't make sense in any other data structure.

I'd certainly rather that Seq match another container structure in its own language rather than some F# standard.

I agree completely. If these functions already existed in any other OCaml module then it would absolutely make sense to reuse the nomenclature. Principle of least surprise, and all that. However, all of this stuff appears to have been recently copied from F#, not just the functions but the Seq module itself including most of the concept it implements: on-demand sequences, i.e. not lazy. The only conceptual difference appears to be that an F# seq allows arbitrary code to be executed at the start and finish of enumeration (e.g. to open and close a database connection) where as OCaml's collection does not.

2

u/ct075 Jul 14 '22

Do they? OCaml's List module doesn't appear to contain most of these functions, e.g. skip , drop , take . Others like memoize don't make sense in any other data structure.

You're right, I was thinking of List from Jane Street Core. Insofar as principle of least surprise, as I've said before, these names have a rich history in other functional languages (other than memoize, which I agree seems like a weird rename for the sake of it).

2

u/PurpleUpbeat2820 Jul 14 '22

Thanks. I think I need to learn more languages!

8

u/elliottcable Jul 14 '22

I think he answered your question? F# is always stuck between two choices, because it’s standing between two ancient behemoths:

  • breaking with C# patterns to support OCamleer expectations, or

  • breaking with OCaml patterns to support C#er expectations.

Because it’s made by-and-for the .NET ecosystem, you can probably safely assume it will always choose the latter of those two.

0

u/PurpleUpbeat2820 Jul 14 '22 edited Jul 14 '22

Right but these functions all appear to have existed in F# first and then been ported verbatim to OCaml later. Most of them were new in OCaml v4.14. If so, I don't see how .NET or C# compatibility could be the reason.

EDIT As /u/octochron points out, they existed in OCaml before they were in the Stdlib in the form of Jane St's Base library and it looks like that is where the names come from.

4

u/elliottcable Jul 14 '22

I can’t tell what you’re suggesting. You think OCaml contributors, creating a different language, should include the first camel-cased function in OCaml’s history, just for compatibility with … another language? (Which, by the way, isn’t compatibility: it’s not like you can copy-paste code between the two, or share libraries.)

It’s a different language. With different conventions. And thus different naming.

Hell, if you’re gonna make an argument for consistency across completely different programming languages, a brief survey shows me about a dozen languages with drop_while, and literally only .NET calling it skip_while. Maybe all of the entire Microsoft ecosystem should switch names for ‘compatibility with OCaml?’

I apologize if that came off a little snarky. I think, perhaps, you don’t understand how strange it is to come to one language and tell them they should have matched the decisions an (arbitrary) other language made — especially when language-design is, quite often, about backwards-compatibility and internal consistency much more than other domains or software-engineering. ¯_(ツ)_/¯

1

u/PurpleUpbeat2820 Jul 14 '22 edited Jul 14 '22

I understand what you're saying but let me push back on some of it.

You think OCaml contributors, creating a different language

The languages are extremely similar and concepts including names appear to have been flowing back and forth between them for many years, e.g. OCaml copied |> from F#.

it’s not like you can copy-paste code between the two, or share libraries

You can actually copy-paste a lot of code between them. Pretty much the whole core of ML is identical between OCaml and F#. Even the syntax used for printf is virtually identical.

completely different programming languages

In the grand scheme of things OCaml and F# are very similar programming languages. They differ mostly in exotic features like GADTs and type providers.

come to one language and tell them they should have matched the decisions an (arbitrary) other language made

I'm not telling anyone they should have matched the decisions an (arbitrary) other language made. I'm asking why, when they appear to have copied everything else, they changed the names of some things.

I apologize if that came off a little snarky.

Not at all. I'm finding all of the answers really interesting.

7

u/octachron Jul 14 '22

There was no deliberate divergence here, just independent choices that lead to different path taken. In particular, the standard library tries to share naming convention that are common within the OCaml ecosystem. Typically, when functions already exist in base or containers with a common name, the standard library tries to preserve those names.

1

u/PurpleUpbeat2820 Jul 14 '22

There was no deliberate divergence here, just independent choices that lead to different path taken. In particular, the standard library tries to share naming convention that are common within the OCaml ecosystem. Typically, when functions already exist in base or containers with a common name, the standard library tries to preserve those names.

Ah, I see. I hadn't realised that this comes from Jane St's libraries. They do indeed use this nomenclature.

Thanks!

2

u/emaphis Jul 14 '22

I doubt there is any other reason for their naming choices beyond following .Net conventions. They meant F# to be easy to convert from C# and Visual Basic. .Net already had naming conventions for API so F# just stuck to them.

1

u/PurpleUpbeat2820 Jul 14 '22

Ah yeah. F#'s Seq appears to be just a wrapper over a .NET collection called IEnumerable.

2

u/thedufer Jul 15 '22

Pretty much every OCaml function you named (maybe not memoize) sounds like an exact match for Haskell functions, and many other strongly-typed functional languages, whereas the F# ones sound like they'd be familiar to .NET folks. The languages might share a lot of similarities, but they fit in different ecosystems.

1

u/PurpleUpbeat2820 Jul 15 '22

Haskell has drop, dropwhile and take but not find_map (which is a weird name, IMO). The name cache is a misnomer because it isn't caching (there is no bound and no eviction) so I prefer memoize even though this isn't exactly memoizing either...

Thanks for the pointer to Haskell though. I'll have a look at its stdlib in more detail too...

2

u/thedufer Jul 15 '22

Fair point on find_map. Haskell calls that firstJust, which honestly is a pretty confusing name (and would make no sense in OCaml, since we call that constructor Some).

1

u/PurpleUpbeat2820 Jul 15 '22 edited Jul 18 '22

Yeah. I must say that the worst name I have come across in the FPL world though is snoc which means "append" and was chose because it is the opposite of "prepend" which was called "cons" so they took C O N S and reversed the order of the letters to create S N O C. The lunacy!

3

u/Leonidas_from_XIV Jul 18 '22

I somewhat disagree on find_map being confusing. find in OCaml is usually about "finding" an element based on some equality. So you would expect a signature like, well, find:

val find : ('a -> bool) -> 'a t -> 'a option

But then, sometimes you would want to directly transform this value into some other value. This can of course be done in another step, but sometimes when you find_map you are looking up the 'a in some data structure before you decide on true/false. If you now return the 'a directly you have to do the lookup again, so if you just return Some looked_up_value you save one lookup.

How is transforming one value into another usually called? map. Hence, find_map.

0

u/[deleted] Jul 14 '22

[deleted]

2

u/vorotato Jul 14 '22

Much of F#'s design and construction involved non-microsoft people. I have been involved in many language discussion threads, and I am not a microsoft person. We do not know better, but we do sometimes make decisions based on what we think would be best for our community. Sometimes it's just that we don't know that there is an equivalent OCaml construct, many of us only write F# and haven't used OCaml yet. I imagine the same is true for OCaml language designers. At the end of the day the most important thing is that it is useful for your community. We can always make shims or cheat sheets for people who want to migrate between the two.

0

u/PurpleUpbeat2820 Jul 14 '22 edited Jul 14 '22

F# appears to have had a Seq module with these functions for ~15 years whereas OCaml just got this in March 2022. So it looks like the OCaml guys copied most of it from F# but decided to change the names of a bunch of functions.

EDIT So it appears the history of these functions is way more complicated than I had assumed.

4

u/dot-c Jul 14 '22

Well, if they really did copy Seq from F#, they might've changed the names to conform to other FP languages. F# is the outlier in terms of naming, as others have said and OCaml doesn't need any compatibility with it

1

u/PurpleUpbeat2820 Jul 14 '22

Yeah, that makes sense. I have no idea what these functions are called in Clojure, Scala, Haskell and other functional languages.

2

u/yawaramin Jul 14 '22

OCaml Standard Library got the naming and implementation of the Seq data type in 2017, from an external library called 'Batteries Included': https://github.com/ocaml/ocaml/pull/1002

Batteries Included got its implementation of Seq back in 2009: https://github.com/ocaml-batteries-team/batteries-included/commit/d67bfc69c79caa6b712ada4af0c6df604fb823b4

F# got its Seq implementation in 2014, about six years after Batteries Included: https://github.com/dotnet/fsharp/commit/3900d411aeb2d7740f4e9fd72fce81b3a8f04357#diff-9903bb620558639b2065ddb69cf6312476cfe67b9d2099fce94bb4e891796e7bR1

So even looking strictly at timeline, OCaml's Seq predates F#'s one. But then if you look at the actual implementations of both of them, you notice that F#'s Seq is based on .NET's IEnumerable interface, and is implemented in an OOP style, while OCaml's one is implemented in a more functional style (using function and sum types). The only thing they have in common is the naming 'seq' (but then so do Scala and Clojure) and some helpful collection functions which are pretty universal for functional-style collection libraries.

4

u/[deleted] Jul 14 '22

As an outsider, the ocaml naming seems to follow what other FP languages do and f# seems to want to distance itself from that by using more "normal terms that humans understand". There's always been those 2 kinds of camps in FP languages and both have merit.

I may be wrong in my perception since I don't use either language anyway.

1

u/PurpleUpbeat2820 Jul 14 '22

As an outsider, the ocaml naming seems to follow what other FP languages do and f# seems to want to distance itself from that by using more "normal terms that humans understand". There's always been those 2 kinds of camps in FP languages and both have merit.

Yes, I think you've hit the nail on the head.

1

u/vorotato Jul 14 '22

Yeah there's some use of FP terms, but mostly where your average javascript user would know the term. F# attracts a lot of first time FP developers, so it's pretty useful for us.

1

u/vorotato Jul 14 '22 edited Jul 14 '22

I mostly write F# so I can only speak for F#, but if you want you can create a module which extends Seq to add the OCaml names. I suspect the same is possible in OCaml. The authors are not trying to impede compatibility, but compatibility is not a goal. If you value compatibility, or adhering to ML norms, and you use F#, I recommend you (kindly) advocate for it in relevant issues on the F# language discussion repo. https://github.com/fsharp/fslang-suggestions/issues

While I doubt you will change their mind on compatibility as a broad goal, it's likely that you can encourage compatibility where it makes sense for F# or improves the language. Thank you for your interest in our community driven language and I hope that the rising tide of F# and functional programming interest lifts all ML boats. I know many in our community are excited to see the OCaml 5.0 release with the similar mindset that even those of us who don't use OCaml, benefit from having a strong ML ecosystem.

module Seq = 
  let find_map = Seq.tryPick //the underscore feels weird but whatever 
  let take' = Seq.truncate //take already is defined, comes from C# 
  let drop = Seq.skip 
  let drop_while = Seq.skipWhile 
  let memoize = Seq.cache

if you want a more tightly knit approach check out F# type extensions https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/type-extensions

2

u/PurpleUpbeat2820 Jul 14 '22

Thanks. That makes a lot of sense. I didn't want to change anyone's mind, just to understand why things are the way they are.