9 ms·
OCaml for the Masses (2011)
- cordite 12y agoAs a haskeller, I've heard some disconcerting things about OCaml. Namely, the standard lib is more or less made just to be able to compile itself, and that the standard lib is not pure, which makes developers go to alternate implementations to do the job in a way that is expected. Please do correct me if what I've heard is wrong. My only indirect experience with OCaml is the Opa language compiler--so all I've seen is that it is a language favored for implementing languages.
- yminsky 12y ago(Author of the article here) The stdlib that ships with the compiler is indeed minimal, though it is used for things other than the compiler. It can be used for other projects, but you probably want something more full-featured. Core, and the more minimal and portable Core_kernel (https://github.com/janestreet/core_kernel https://github.com/janestreet/core_kernel) is a full-featured alternative that is growing in popularity, and is what the book I worked on (Real World OCaml, http://realworldocaml.org http://realworldocaml.org) is based on. OCaml (and most of both the stdlib and Core) default to immutable data structures, but OCaml has good support for programming imperatively, to its credit, in my view.
- jzelinskie 12y agoI've heard that Jane Street uses their own extensions to the language. Also, how does one deal with the development of OCaml being largely done in French?
- yminsky 12y agoJane Street makes use of OCaml's built-in metaprogramming facility, camlp4. While these are in some sense "our own extensions", they're wholly shareable without using a hacked version of the compiler, and just because we build our code using these syntax extensions, doesn't mean that users of our libraries need to do the same. You can use Core_kernel, for example, without using our syntax extensions. The French thing is a non-issue. The compiler is written and documented in English, and all the main contributors are fluent English speakers, and the mailing lists are almost entirely in English.
- masklinn 12y ago> Also, how does one deal with the development of OCaml being largely done in French? OCaml is developed by french people, I've never heard of it being done in french (aside from interpersonal banter I guess). The french version of the official site doesn't even work correctly (when you click on the "manual" link you get the english manual)
- kd0amg 12y agoI've never heard of it being done in french (aside from interpersonal banter I guess) And an April Fools joke -- http://gallium.inria.fr/blog/ocaml-5/ http://gallium.inria.fr/blog/ocaml-5/
- plinkplonk 12y agoFWIW: Core doesn't work with Windows (the Real World OCaml book does state this upfront) so you need either an OSX or Linux machine to work with Core, and so you can't work through the book with a Windows machine. Something to be aware of.
- pjmlp 12y agoI was really disappointed, as I only found this out after buying the book. So I end up mostly reading the book as a means to update the knowledge I had from the Caml Light days and differences to F#.
- yminsky 12y agoFor what it's worth, the world has changed since then: Core_kernel (which is the highly portable bits of Core, which is most of it) works on Windows just fine. Indeed, WODI, which is the best windows package manager for OCaml right now, ships with it.
- pjmlp 12y agoThanks for the info. I'll have a look at it.
- tomp 12y agoHi, Yaron! I was recently looking into using Core for one of my projects, but have so far failed to find any good, clear, complete documentation or how-to guide. Is there anything like that available online? I guess I could read the Real World OCaml book to learn parts of it, but I already know OCaml...
- yminsky 12y agoReal World OCaml does indeed cover Core and related libraries, but you can also just read the API documentation: https://ocaml.janestreet.com/ocaml-core/latest/doc/ https://ocaml.janestreet.com/ocaml-core/latest/doc/
- a-nikolaev 12y agoI think, you are misinformed. While the standard library is not huge, it is quite ok. Then, OCaml is steadily gaining strength with Opam and Jane Street's libraries, which become de-facto standard. Personally, I see OCaml as C++ done right (minus pointers), if you need pointers, use Rust.
- eru 12y agoOr D.
- judk 12y agoThe Haskell Prelude is famously awful, so OCaml stdlib isn't much of a needle-mover.
- codygman 12y agoI wouldn't say it's awful, however there are definitely improvements that need to be made.
- orbifold 12y agoThe default string type is terrible, the default file input output introduces space leaks and has terrible performance. The default numerical hierachy, (Num a) in particular, is nothing mathematicians tend to study (it would be much more sensible to define Ring a, for example). It overemphasizes lists and instead of attempting to define polymorphic functions that give access to a wide range of containers, pollutes the global name space with list specific functions, even though lists are rarely the right choice.
- frowaway001 12y agoThanks for this comment. I always wondered if I was the only one who felt that way, or if I was just missing the point when I found these things weird.
- arunix 12y agoAre there any alternative implementations that address these issues?
- judk 12y agoThere are lots of high quality libraries, but no major effort to replace the Prelude's privileged position in the language. So it it kind of a minefield to find the correct module/function for your needs. Haskell Platform is a great standard set of imports, but it is only for the commonest libs, beyond that you have again the problem of sorting out excellent libs from some student's throwaway homework project with the same name.
- GreaterFool 12y agoAnother Haskeller here. I strongly recommend you take a second look at OCaml. I think it has some fantastic features that Haskell doesn't: Modules. They are great for solving major code organisation problems and I really hope Haskell will get a module system soon ("Backpack" paper). Type classes are great for things like Monad and Num since you are likely to use many different Monads or Nums in the same chunk of code. But consider what happens if you want to support both ByteString and Text in your library (let's say a parser library). You may end up with a type class constraint like ListLike and your code will be littered with 'ListLike .. =>'. But when you're parsing some data later on it will be either ByteString or Text. So I'd fix my choice of ListLike at module instantiation time and make the constraint go away. If I need both (e.g. I'm reading some data from a socket and some of it is Text and some of it is ByteString) then I'm just going to instantiate two modules, one for each. To me this is a much cleaner solution. Named/optional function parameters. They solve a bunch of minor but incredibly annoying problems. A simple example: for (aka flip map); It is not in Prelude and whenever it comes up there are plenty of people who are opposed for one reason or another. In OCaml (Core library) it doesn't matter because map takes the function by named parameter so List.map ~f:(fun x -> x + 1) [1;2;3] works fine and so does List.map [1;2;3] (fun x -> moderately long, perhaps few lines long function). Both cases are important for readability. Another example: in Haskell I've been working with APIs where functions have 20+ positional parameters (yeah, it's terrible design but that's out of my hands). If they were named and some of them optional (most of them really are optional) then the code would be a fraction of what it is. OCaml is quick to compile. I've been looking at Eliom web framework recently and rebuilding a website takes seconds; compare that to yesod which takes forever and spins all the cores on my laptop which eats my battery in no time. OCaml code tends to be quite straightforward: from my experience it is longer than Haskell equivalent, uses less abstractions but at the same time it is conceptually simple. Typed printf. (yeah, there are 'solutions' in Haskell but it's a typed printf out of the box!) Polymorphic variants. Can be used to bring information to the level of types. Compare parse :: Parser a -> String -> ParseResult a with parse :: Parser a -> String -> [`Error String | `Result a] Also see OCaml's tyxml package which provides typed html5. Yeah, the types look rather crazy in some places but that's a small price to pay for rooting out invalid HTML.
- shadytrees 12y ago
- skybrian 12y agoThis is from 2011. I wonder how OCaml implementations have changed since then?
- yminsky 12y agoThe biggest change is OPAM, http://opam.ocaml.org/ http://opam.ocaml.org/, a top-notch package management system that makes installing OCaml packages much easier. Shameless plug: there's also a newish O'Reilly book, Real World OCaml http://realworldocaml.org http://realworldocaml.org, which I think is a big help in learning the language. Oh, and there's OCaml Labs, a new lab at Cambridge University that's dedicated to improving the language. And of course the compiler is constantly making progress. The upcoming 4.02 release is a pretty fun one, which I documented here: https://blogs.janestreet.com/ocaml-4-02-everything-else/ https://blogs.janestreet.com/ocaml-4-02-everything-else/ And before that, changes like GADTs and first-class modules landed, which have been quite useful extensions. Really, it's a very active and fun community these days. The language is getting better quite quickly, but mostly in conservative and tasteful ways. The people in charge of the core language have been doing a great job, and the community infrastructure (things like OPAM) have been making big strides as well.
- judk 12y agoGreat to here. I tried wodi years ago and it was a disaster. Made hobby-learning ocaml a nonstarter. Opam sounds promising.
- girvo 12y agoOPAM is awesome, made it super easy to install "Batsh"[0], a neat compile-to-bash-or-BAT language. Makes programming in shell not painful :D [0] https://github.com/BYVoid/Batsh https://github.com/BYVoid/Batsh
- mercurial 12y agoIt looks like the ecosystem is fairly small. Another thing that is off-putting (unless I'm missing something) is that OPAM packages have no online docs per default, something that is standard with Haskell packages, for instance, and is definitely a big help.
- tieTYT 12y agoWhat made me give up on Haskell was all the libraries had native linux bindings. It was hell reusing code if you're developing on windows (and I've heard this is true on macs, too). Does OCaml have this issue?
- tedks 12y agoOcaml has a bytecode compiler and a native code compiler; any library written in OCaml can be compiled to run-anywhere bytecode, though I'm not sure how many libraries are really just wrappers around native GNU/Linux libraries.
- yminsky 12y agoUnix support is definitely better than Windows. That said, the core language and base libraries are pretty portable. Here's one nice retrospective on a move from Python to OCaml for a program that needed portability to Windows as a fundamental concern. http://roscidus.com/blog/blog/2014/06/06/python-to-ocaml-retrospective/ http://roscidus.com/blog/blog/2014/06/06/python-to-ocaml-ret... Probably the biggest problem on Windows is that OPAM, the package manager, doesn't work there. That will come eventually, though.
- shadowfox 12y ago> Probably the biggest problem on Windows is that OPAM, the package manager, doesn't work there. That will come eventually, though Wodi [1] is a reasonable choice until OPAM starts supporting Windows. It includes many of the interesting/useful libraries (including, importantly, batteries [2] and core [3]) [1] http://wodi.forge.ocamlcore.org/ http://wodi.forge.ocamlcore.org/ [2] http://batteries.forge.ocamlcore.org/ http://batteries.forge.ocamlcore.org/ [3] https://github.com/janestreet/core_kernel https://github.com/janestreet/core_kernel
- pjmlp 12y ago> Probably the biggest problem on Windows is that OPAM, the package manager, doesn't work there. That will come eventually, though. Any ETA on it? If I knew about it before buying the book, I would not have done it. The book is great, but as it is I have better luck with F# at work.
- gnuvince 12y agoI recently picked up a project in OCaml that I had begun last summer and never finished. I have to say, compared to my day-to-day tools (mostly Java these days, Python before), OCaml is a breath of fresh air. For instance, today I did a refactoring where I replaced a type that was simply an alias to string with a proper abstract data type. Fixing the code was simply a matter of compiling, going to the file/line number in the error message, fix and repeat. Algebraic data types (sometimes called discriminated unions) are great for properly separating the logic of a data structure instead of sticking everything into one big class. I highly recommend you give it a try. By the way, although we hear about OCaml less often than other languages, it is still used in many well known places such as Facebook (for pfff and Hack), Microsoft (SLAM, a verification tool for drivers), and Airbus (they use the ASTRÉE static verification tool which is written in OCaml).
- brianberns 12y ago> Although we hear about OCaml less often than other languages, it is still used in many well known places such as ... Microsoft Microsoft's F# language is essentially a .NET version of OCaml.
- _pmf_ 12y ago> Microsoft's F# language is essentially a .NET version of OCaml. Oh, how I wish there was a JVM variant of F# (no, Scala does not count).
- simplify 12y agoI considered picking up OCaml at one point, but got scared off by the global interpreter lock. Can an experienced OCaml dev tell me when this would really become an issue, and certain cases where it wouldn't make a difference?
- yminsky 12y agoMy experience has been building parallel and distributed programs using multiple runtime instances that communicate via message passing. There are libraries that help automate this, link our own Async_parallel (https://blogs.janestreet.com/async-parallel/ https://blogs.janestreet.com/async-parallel/). One advantage of this approach is that it scales beyond a single box. There is work going on at OCaml Labs on a parallel runtime. I suspect it will be useful, but in the end, message passing is I think a better idiom than shared memory threads for parallel programming. When the true parallel runtime lands, I'm not sure that we'll actually use it much for running truly parallel threads.
- lobster_johnson 12y agoDoes OCaml have anything resembling Erlang's OTP? Lightweight concurrent processes, easy to use remote processes, supervisor trees etc.?
- yminsky 12y agoAsync and LWT give you lightweight concurrency mechanisms. Libraries like Async.RPC give you lightweight remote invocation, and Async_parallel gives you simple mechanisms for spinning up multiple physical processes and communicating between them. Don't get me wrong: OTP by all accounts has richer support for this kind of stuff. I think OCaml is a better language for many purposes, but OTP is a great runtime and set of libraries whose equal is not yet found in any other language as far as I can tell.
- mercurial 12y ago> I suspect it will be useful, but in the end, message passing is I think a better idiom than shared memory threads for parallel programming. It really depends on the problem you are working with. You wouldn't want to make a high-performance HTTP server this way. Especially if you get to copy 2MB of POST data every time.
- robert_nsu 12y agoI've played with OCaml in the past, but I decided to get comfortable with F# then give OCaml another go.
- jmgrosen 12y agoI've often seen OCaml compared to F#; as someone who knows Haskell and Rust (among others), which is better to learn? I'm not really a Windows guy, which makes me think OCaml, but I've seen that Xamarin now works with F#, which is appealing to my inner app developer.
- chadzawistowski 12y agoF# is essentially OCaml.NET, so knowledge of either will be very transferable to the other. F# can interface with C# types, but it feels cumbersome at times. Idiomatic F# is very similar to idiomatic OCaml.
- bsamuels 12y agoI prefer F#, but only so because it has decent visual studio tooling and .net interop. F# power tools' refactoring is great, and its syntax coloring is very good as well. http://puu.sh/a1vM5/c735f3d319.png http://puu.sh/a1vM5/c735f3d319.png (i swear this looks a lot nicer on the washed out screen i program with) The interop with .net libs is helpful too - but i've found that if there's anything that's really hurting F#, it's all the OOP tacked onto it to make the .net interop work. Decent tradeoff if you ask me, but it's cringeworthy to see people using F# like an oop language when the oop only appears to be there for the interop magic to work.
- stonemetal 12y agoComparing OCaml and F# is about like comparing Java and C#. The MS languages started out very similar to the others, but made much larger strides. F# has changed their syntax to be lighter, with something similar to Haskell's offside rule. They added workflows which are monads but not quite as nice as Haskell's.
- adultSwim 12y agoMy personal preference is OCaml. One big point I would give to F# is tooling. Having a real IDE for OCaml would be nice. When I was writing OCaml I used Emacs with ocamlspotter. The other big F# feature is library support. OCaml has pretty good libraries but F# can leverage the whole .NET framework. This would be noticeable in areas like GUIs. I imagine that not using Microsoft F# would negate the benefits of F# over OCaml. To be honest, go with either. For most people the benefits come from the simple/common/old features. Once you get used to dealing with the compiler, you'll likely fall in love with ML.
- hackerboos 12y agoWe were given this by Dr Grant Malcolm when studying semantics in the University of Liverpool. Although I didn't run off and learn OCaml at the time it did spark my interest in functional programming - eventually picking up Scala and Erlang.
- deleted 12y ago[deleted]
- rrss1122 12y agoI tried OCaml once. It was to figure out the algorithm Yaron Minsky (author of linked article) used for his orbital simulator, Planets. I only learned enough to port the code over to JS, and quickly forgot it. Coming from a procedural programming background, the learning curve was too steep for OCaml. Here is Minsky's Planet's project, by the way: http://planets.homedns.org/ http://planets.homedns.org/
- ALee 12y agoWe're also using OCaml at Esper. Here's our implementation for folks who are interested in using functional programming lagnuages: http://stackoverflow.com/questions/19350590/designing-large-projects-in-ocaml http://stackoverflow.com/questions/19350590/designing-large-...