Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of OCaml as a programming language behind Hacklang and Flow? [closed]

Tags:

ocaml

Recently, Facebook announced Flow, a static type checker for JavaScript, which is implemented mainly in OCaml (https://code.facebook.com/posts/1505962329687926/flow-a-new-static-type-checker-for-javascript/).

Hacklang (PHP with static type checker) is also mainly written in OCaml. So, I wonder that what are the distinctive benefits of OCaml for making a static type checker?

like image 602
Supasate Avatar asked Nov 19 '14 17:11

Supasate


People also ask

What is OCaml good for?

Object-oriented programming: OCaml allows for writing programs in an object-oriented style. In keeping with the language's philosophy, the object-oriented layer obeys the "strong typing" paradigm, which makes it impossible to send a message to an object that cannot answer it.

What is Hacklang used for?

Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Meta as a dialect of PHP. The language implementation is open-source, licensed under the MIT License. Hack allows programmers to use both dynamic typing and static typing.

Is OCaml functional programming?

OCaml unifies functional, imperative, and object-oriented programming under an ML-like type system. Thus, programmers need not be highly familiar with the pure functional language paradigm to use OCaml.

What type of language is OCaml?

OCaml is a functional (applicative) programming language, but also an imperative language, and also an object-oriented language. This means you can mix and match paradigms at will.


1 Answers

"What are the benefits of OCaml as a programming language" is indeed an opinion-based question, and one that I'm not going to tackle here. However, I work on the Hack team at Facebook, and have worked closely with the Flow team, so I can answer the question I think you intended to ask: "Why did Facebook pick OCaml to build Hack and Flow?"

  • The biggest reason is that OCaml has brilliant support for defining your own datatypes, and then pattern matching on them. Most of what Hack and Flow do are operations over various forms of an AST, and having a really nice way to express "if you see this kind of node with that kind of node inside it, do this thing" is invaluable. Take a look at the definition of subtyping in Hack -- it's certainly complicated, but at its heart it's just a big pattern match over a pair of types, and would be unreadably complex in any language without pattern matching.
  • OCaml is largely a functional language, with great support for first-class functions and immutable data structures. Similarly to the above, when you're doing typechecking, you end up doing a lot of different kinds of maps and folds over AST nodes, and functional languages express that really concisely. For example, typing a block of code is literally just a fold_left over the statements it contains.
  • But it's not purely functional. It's "impure" -- sometimes, mutable state, using exceptions, or similar is just the nicest way to express something. But most importantly, it means that you don't need any sort of complicated acrobatics or mental model switch or anything to call into C functions via an FFI. Both Hack and Flow use the same model for multithreading: a specially mmap'd region shared between different fork'd processes, containing a shared, lockless hash table. That's the sort of thing I wouldn't want to express in any language except C -- and that's exactly what we do. The OCaml code can call a couple magic functions without being any the wiser that it's actually C under the hood. (As an aside, I'm going to do a tech talk in January about how exactly our multithreading works, along with some other bits of Hack implementation details -- it's really cool, but hard to grasp without an intro, even if the code is open source!)
like image 51
Josh Watzman Avatar answered Sep 21 '22 13:09

Josh Watzman