Learning OCaml by writing bindings for a matrix data type written in C

Hello everyone,

I’m new to OCaml and as a start I wanted to write bindings for my C fixed size vector and matrix library. I got the basics working after that I’m not sure where to continue. Any ideas would be appreciated. This is my first functional language, coming from C++ I have a hard time thinking the OCaml way.

  • Are operators overloading supported. How do I do it?
  • Any ideas of what I should do next, What kind of bigger project should I work on to improve my skills?
  • Are there any open source libraries I should look closely at to either get inspired or contribute to? for example Owl seems interesting to me.
  • I’m very much interested in compilers and ML so any ideas of projects along the lines of compilers or scientific computing in OCaml would be appreciated.

I’m trying to prepare myself for learning and using OCaml and C at university where I would major in computer science and maths. And I learnt that OCaml is very much used here in France.
Also sorry if my english sound weird, I’m not a native speaker.

Here is my code: matrice/bindings/ocaml/matrice at main · istmarc/matrice · GitHub

1 Like

Hello Marc,

Welcome, and I’m happy to hear that you’re interested in OCaml (and your English is great!). Writing C bindings is, I would say, more of an intermediate-level task than a beginner exercise, so congratulations on managing to solve it on your own.

Best regards and don’t hesitate to ask more questions.

1 Like

Thank you for your reply.

  • After looking at the different I would say weird operators defined, I would rather keep it simple and not use operator overloading at all. Maybe only for elementwise operations like add, sub, mul and div.
  • I will look at both Owl and Raven.I will try to join the next meeting and see how it is and if there are some open issues for beginners to work on.
2 Likes

Marc,

Perhaps you might be interested in Jean-Christophe Filliatre’s book “Learn Programming with OCaml” ( https://usr.lmf.cnrs.fr/lpo/ ) ? Originally written in French, but I don’t have a link handy for that.

1 Like

Thank you for the link.

I think only the English version is available for free but that it also great. I will start with the English version and I will try to get a copy of the book in French.

One way this is sometimes done (e.g. see the Zarith documentation) is to define the operators so that they can be used with local open, such as:

let f x y z =
  let open MyModule in
   x + y * z (* using those defined in MyModule *)

let g x y z = MyModule.(x + y * z)

The goal here is not true overloading (i.e. selection based on type of the argument) but rather syntactic convenience. Variations around these include:

  • putting the operators in a dedicated “Ops” submodule (let open MyModule.Ops in ) if you don’t want to put everything in MyModule in scope
  • still using special operators (+%, *%, …) if you need to mix e.x. integer and your data type in expressions (e.g. MyModule.(mix (4*3) *% x)) since otherwise, the * in my module shadows the one from the standard library.
1 Like

Just to mention that approaching the language by writing C bindings may be unusual, but it is far from being uninteresting :slight_smile: In particular, this will immediately force you to go into the details of the data representation of the OCaml runtime, which is one of keys to understanding the language.

A few excellent resources that I recommend to ambitious beginners:

A few more specialized resources:

And finally:

This book is 1) outdated (written at the time of CAML Light), 2) in French. However, the differences with OCaml are mostly cosmetic, and it remains, in my opinion, the best book to learn the language, full of insight and little gems. (Just to mention one: this is the only source I know that explains the use of “levels” in the OCaml typechecker for beginners.)

Cheers,
Nicolas

4 Likes