Hello
Here is the latest OCaml Weekly News, for the week of May 19 to 26, 2015.
Archive: https://sympa.inria.fr/sympa/arc/caml-list/2015-05/msg00081.html
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):
http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc85
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)) ->
begin
match (equal a c, equal b d) with
| (Some Eq, Some Eq) -> Some Eq
| _ -> None
end
| _ -> 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) ->
begin
match equal ty ty' with
| Some Eq -> t
| None -> invalid_arg "Ill type in nth"
end
| (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
##########
Archive: https://sympa.inria.fr/sympa/arc/caml-list/2015-05/msg00083.html
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!
Thanks to Alp Mestan, we now include in the OCaml Weekly News the links to the
recent posts from the ocamlcore planet blog at http://planet.ocaml.org/.
ICFP programming contest 2014: a retrospective (part 2/2):
http://gallium.inria.fr/blog/icfp-contest-2014-retrospective-2
Church Numerals:
http://blog.shaynefletcher.org/2015/05/church-numerals.html
On the book "More OCaml":
http://www.amazon.com/review/R7DMDHZMOKS8X
ICFP programming contest 2014: a retrospective (part 1/2):
http://gallium.inria.fr/blog/icfp-contest-2014-retrospective-1
Labeled and optional arguments:
http://blog.shaynefletcher.org/2015/03/labeled-and-optional-arguments.html
The troublesome reflection rule (TYPES 2015 slides):
http://math.andrej.com/2015/05/19/the-troublesome-reflection-rule-types-2015-slides/
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.