Previous week Up Next week


Here is the latest OCaml Weekly News, for the week of May 19 to 26, 2015.

  1. GADTs and JSON
  2. Job: Ocaml Engineer in Paris
  3. Other OCaml News



Trevor Smith asked and Rodolphe Lepigre replied:
> Is it possible to encode a recursive, heterogenous map and list datastructure
> with GADTs?
> I want to encode JSON (there are already a couple of great libraries out there
> so this is kind of an academic question). I would like to have functions that
> can only take a JSON map type, for example to take a json map and return a
> value. The key here is that the map can hold values of type int,string and also
> maps. Is this possible?
> eg get : (map : map json) -> key:string -> 'a json
> Thank you.
> Trevor

You should probably have a look at the examples given for GADTs on the page
about language extensions (advanced examples section):

Not so long ago I used equality types to encode collections of object of
different types. Here is an example with lists and a function for obtaining
the n-th element.

type (_,_) eq = Eq : ('a,'a) eq

type 'a typ =
  | Int   : int typ
  | Float : float typ
  | Pair  : 'a typ * 'b typ -> ('a * 'b) typ

let rec equal : type a b. a typ -> b typ -> (a,b) eq option = fun t1 t2 ->
  match (t1,t2) with
  | (Int       , Int      ) -> Some Eq
  | (Float     , Float    ) -> Some Eq
  | (Pair(a,b) , Pair(c,d)) ->
        match (equal a c, equal b d) with
        | (Some Eq, Some Eq)  -> Some Eq 
        | _                   -> None
  | _                       -> None

type _ tlist' =
  | Nil  : unit tlist'
  | Cons : ('a typ * 'a * unit tlist') -> unit tlist'

type tlist = unit tlist'

let rec nth : type a. tlist -> int -> a typ -> a = fun l n ty ->
  match (l, n) with
  | (Nil          , _) -> raise Not_found
  | (Cons(ty',t,_), 0) ->
        match equal ty ty' with
        | Some Eq -> t
        | None    -> invalid_arg "Ill type in nth"
  | (Cons(_,_,l)  , _) -> nth l (n-1) ty

let l = Cons(Int, 3, Cons(Float, 4.2, Nil))

let n : int   = nth l 0 Int
let f : float = nth l 1 Float

Job: Ocaml Engineer in Paris


Vincent Balat announced:
The Ocsigen team is looking for a full time OCaml developer with good OCaml
skills to work on the development of a Web application using Ocsigen (Eliom +
Js_of_ocaml) in a start-up located in Paris.

Please contact me for more information!

Other OCaml News

From the ocamlcore planet blog:
Thanks to Alp Mestan, we now include in the OCaml Weekly News the links to the
recent posts from the ocamlcore planet blog at

ICFP programming contest 2014: a retrospective (part 2/2):

Church Numerals:

On the book "More OCaml":

ICFP programming contest 2014: a retrospective (part 1/2):

Labeled and optional arguments:

The troublesome reflection rule (TYPES 2015 slides):

Old cwn

If you happen to miss a CWN, you can send me a message and I'll mail it to you, or go take a look at the archive or the RSS feed of the archives.

If you also wish to receive it every week by mail, you may subscribe online.

Alan Schmitt