Using let module for matching

In OCaml, referring to constructors defined in other modules can be somewhat awkward. Suppose we have a module like the following.

  1. module Example = struct
  2. type t = Foo | Bar | Baz
  3. end

To write a function that pattern matches on values of type Example.t we could directly refer to the variants as follows.

» Continue Reading.

Extracting an exception from a module

The Unix module defines the Unix_error exception constructor.

  1. module Unix : sig
  2. exception Unix_error of error * string * string
  3. ...
  4. end

Suppose you want to create your own My_unix module that defines some Unix utility functions and exports the same Unix_error. How would you do it? You can’t redeclare

» Continue Reading.

A universal type?

Is it possible in OCaml to implement a universal type, into which any other type can be embedded? More concretely, is possible to implement the following signature?

  1. module type Univ = sig
  2. type t
  3. val embed: unit -> ('a -> t) * (t -> 'a option)
  4. end

The idea is that t is the universal type and that embed () returns a pair (inj, prj), which inject to and project from the universal type. Projection is partial (returns an option) because injection is not surjective.

» Continue Reading.

Talking at Penn

I just got back from an enjoyable visit at Penn. I gave a version of my POPL talk for an audience consisting in large part of students taking Benjamin Pierce’s advanced programming class, which is being done in Haskell with a little bit of ML. I also got a chance to chat with some of the PL faculty and grad students and to hear what people are up to on the research front.

It was a fun afternoon. I hope among other things that it stirs up some more interest (and proposals) for this year’s OCaml Summer Project.

» Continue Reading.

HOWTO: Static access control using phantom types

We thought that phantom types would be an appropriate topic for our first real post because they are a good example of a powerful and useful feature of OCaml that is little used in practice.

In this post, I’ll cover a fairly simple use of phantom types: enforcing a capability-style access-control policy. In particular, I’ll describe how you can create easy to use read-only handles to a mutable data structure. We’ll explore this using the example of an int ref. The int ref is a toy example, but the same approach can be used for more realistic cases, such as a string library or a database interface.

» Continue Reading.

We’ve got a blog!

Jane Street finally has a blog! Jane Street is one of the biggest commercial users of OCaml, and we like to think that we’ve picked up a few tricks over the years. In addition to putting down our random musings, we plan to use this space to share what we’ve learned. We hope that over time this turns into a useful resource for the larger OCaml community.

We’ll get our first real post out soon.