Live data from Hacker News

Real World OCaml

realworldocaml.org

61–70 of 83 posts

Re: Real World OCaml

#61

Earlier quoted context omitted.

Is OCaml, then, a good fit for the kinds of programs many people have started writing in Go? Native, stand alone binaries. High performance (not sure how the Go vs. OCaml benchmarks look right now). Good networking. More productive and less error prone than C or C++. Less verbose than Java. Haven't written any OCaml programs, but seems to check the same boxes. Go seems to have a much better concurrency story with cha…

My understanding is that Go has a focus on concurrency which OCaml presently lacks, and so it probably has an edge there (possibly a big one). Otherwise, yes, OCaml is a good fit for the kinds of problems I understand Go to be used for.

If you only need concurrency for I/O, and in particular networking then OCaml is pretty good here:

- you can use system threads. Yes you'll only be able to run one OCaml thread at once, but when one OCaml thread gets blocked on I/O it switches to another.

- you can do co-operative threading if you write your code in a monadic style (there are a few libraries providing this: Lwt, Async, and in some sense Equeue). Then you don't need to worry about the thread/context-switching overhead, and the event-driven architecture should allow you to scale to thousands of concurrent events. While this may sound scary at first, the syntax is quite easy to understand, and it really is a lot easier than trying to write non-blocking code in C with lots of callbacks.

In particular have a look at Ocsigen, and Eliom that provide a web-server and framework: http://ocsigen.org/tutorial/

Regarding the book have a look at the 'Concurrent Programming with Async' chapter that shows how you can write a TCP server/client that doesn't block.

Re: Real World OCaml

#62
post #35

Earlier quoted context omitted.

It depends on what you are working on. OCaml lets you interact more closely with Unix and with C libraries, if you need that. It also has a lot faster startup time and lower memory overhead, making it somewhat more suitable for writing Unix-style programs that need to run quickly with low overhead. I also moved from OCaml to Scala for my primary programming, and have really enjoyed it. The functional goodness on top…

Is OCaml, then, a good fit for the kinds of programs many people have started writing in Go? Native, stand alone binaries. High performance (not sure how the Go vs. OCaml benchmarks look right now). Good networking. More productive and less error prone than C or C++. Less verbose than Java. Haven't written any OCaml programs, but seems to check the same boxes. Go seems to have a much better concurrency story with cha…

Go does have a much better concurrency story, OCaml's is rather weak (as others have observed).

For the work I do (data analysis and scientific research, particularly on recommender systems, with system-building to support that), if I need concurrency, it's suitable to run the program on the JVM. So I would use Scala in that case, and OCaml would be fine for the other systems-y stuff.

I personally have taken to writing such code in Haskell these days, but that's largely to practice my FP skills in a manner that's easier to transfer back to Scala.

Re: Real World OCaml

#63
post #16

Also interesting read, "Unix system programming in OCaml", http://ocamlunix.forge.ocamlcore.org/

Although not a book per-se, the tutorials on the OCaml.org site are based on the old ocaml-tutorial.org ones, which is among the ones I used to get started (there was no Real World OCaml, or OCaml from the Very Beginning at the time): http://ocaml.org/tutorials/

For future reference, all books should be listed here: http://ocaml.org/books.html

Re: Real World OCaml

#64
post #33

For the early adopters and experimenters amongst you, you might like Felix http://felix-lang.org/share/src/web/tutorial.fdoc It is a whole program optimized, strongly typed, polymorphic, ML like language that can interact effortlessly with C and C++ code and has coroutines baked in. Its own demo webserver is based on coroutines. It uses a mix of lazy and eager evaluation for performance and compiles down to C++. Exec…

Hrm, what do you think about rust vs Felix? They seem to be targeting the same problem.

Rust and Felix both try to be general languages so they're both targeting that. Felix has a better type system. Rust provides more secure but restricted protocol for concurrency, Felix has no such restrictions, it's specifically designed to support shared memory concurrency, which Rust specifically doesn't allow. Rust uses message passing but organises via the memory management mechanism to do it very fast.

Re: Real World OCaml

#65
post #33

For the early adopters and experimenters amongst you, you might like Felix http://felix-lang.org/share/src/web/tutorial.fdoc It is a whole program optimized, strongly typed, polymorphic, ML like language that can interact effortlessly with C and C++ code and has coroutines baked in. Its own demo webserver is based on coroutines. It uses a mix of lazy and eager evaluation for performance and compiles down to C++. Exec…

I had a look at it and it sounded pretty cool. The one thing I found unfortunate is the lack of separation between safe and unsafe code, but it certainly has a lot going for it.

There's no such thing as safe code. So to do as you suggest requires some set of suitable concepts of relative safety, together with some way to enforce them. I am very interested in implementing mechanisms that provide guarantees. And not just safety. Another would be licence management (e.g allow only BSD licenced code to be used) .. that's a legal safety guarantee :)

Re: Real World OCaml

#66
post #20
post #7

Where is OCaml's place in the current world? I find it interesting as it seems to generate little "buzz", but has two new books this year, and is a pre-cursor to another functional language that itself seems to be gaining traction, and is yet produced my Microsoft: F# My observational / untested impression is OCaml seems to be more practical, and maybe a little easier to transition to for someone like me who uses mos…

It's a great teaching tool (so in a university/academic setting at the very least)

I have been taught fundamental concepts of programming and programming paradigms with OCaml, and I am very grateful for that. To me, it is an expressive, clean, powerful multi-paradigm language that deserves a lot of love.

Re: Real World OCaml

#67

Earlier quoted context omitted.

My understanding is that Go has a focus on concurrency which OCaml presently lacks, and so it probably has an edge there (possibly a big one). Otherwise, yes, OCaml is a good fit for the kinds of problems I understand Go to be used for.

If you only need concurrency for I/O, and in particular networking then OCaml is pretty good here: - you can use system threads. Yes you'll only be able to run one OCaml thread at once, but when one OCaml thread gets blocked on I/O it switches to another. - you can do co-operative threading if you write your code in a monadic style (there are a few libraries providing this: Lwt, Async, and in some sense Equeue). Then…

Good points.

Re: Real World OCaml

#68

Earlier quoted context omitted.

If you want to use a functional language on Unix, _and_ have a type system, _and_ avoid JVM/.Net then you don't have many choices. There is Haskell of course, but it is lazy by default, which makes it harder to reason about how your program will execute, and how much space it'll use. Rust is something interesting to keep an eye on, but AFAIK it is not ready yet for production use. Hence I prefer OCaml.

From the little I know of OCaml, Rust isn't really in the same category. While it has functional elements, it's really aimed at being a cleaner C++ (for instance, it doesn't have TCO).

Rust was originally implemented in OCaml, though. You're correct theyre not exactly in the same category, but there is influence there.

Re: Real World OCaml

#69
post #33

For the early adopters and experimenters amongst you, you might like Felix http://felix-lang.org/share/src/web/tutorial.fdoc It is a whole program optimized, strongly typed, polymorphic, ML like language that can interact effortlessly with C and C++ code and has coroutines baked in. Its own demo webserver is based on coroutines. It uses a mix of lazy and eager evaluation for performance and compiles down to C++. Exec…

This looks really awesome! Thanks for bringing it to my attention. Why is this so much under the radar? It seems like it would have a ton going for it.

The historical answer is that originally Felix programs were being written for the Shootout and the compiler was upgraded so it performed well. In fact it trashed everything. Then control of the Shootout changed hands and Felix got dropped by the new manager. Today, there are no forums for developing new languages.

Felix "targets" people that would like to use Haskell or Ocaml but have a ton of code in C and C++ to interface with. Felix is a C++ upgrade: it discard the syntax, but retains ABI compatibility, at quite some cost to things like safety for example.

Felix is more or less guaranteed to perform on par with C/C++ or better for the simple reason you can embed C++ directly into Felix, this works because Felix generates C++. And of course you can link to your favourite libraries with minimal syntax.

  type mytype = "My::Type";
  ctor mytype : int = "My::Type ($1)";
  fun addup : mytype * mytype -> mytype = "$1.addup($2)";
Unlike Ocaml which requires a lot of hard to write glue logic, Felix and C++ share types and functions. Typically only type glue is required to create a bridge.

Re: Real World OCaml

#70
post #54

Earlier quoted context omitted.

Lack of marketing I guess, not many users, and may be because people panic when they cannot immediately find the thing they are familiar with, for example OO hierarchies, dynamic types. Plus I found it hard to understand till I got a rudimentary understanding of OCaML, and Haskell's typeclasses. So its not ready to be used by everyone. To do something nontrivial with it you would need to be on the mailing list though…

OCaml doesn't have typeclasses, does it? It's been a while since I've used it, so things may've changed or my memory may be failing...

It now has first-order modules, which can be used to accomplish almost the same thing. The biggest difference is first-order modules are explicit while typeclasses are implicit. Whether this is a good or bad thing is up for debate.
Post reply on HN