Skip to content
heterogenous Map over a GADT
OCaml
Branch: master
Clone or download
Latest commit cf40ba7 Jun 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore dune Feb 15, 2019
.travis.yml
CHANGES.md breaking the API, it's more safe now :) Apr 20, 2019
LICENSE.md initial Jun 16, 2018
README.md fix README May 7, 2019
dune
dune-project dune Feb 15, 2019
gmap.ml fix signature of fold2 Jun 18, 2019
gmap.mli fix signature of fold2 Jun 18, 2019
gmap.opam breaking the API, it's more safe now :) Apr 20, 2019
tests.ml breaking the API, it's more safe now :) Apr 20, 2019

README.md

Gmap - heterogenous maps over a GADT

%%VERSION%%

Gmap exposes the functor Make which takes a key type (a GADT 'a key) and outputs a type-safe Map where each 'a key is associated with a 'a value. This removes the need for additional packing. It uses OCaml's stdlib Map data structure.

type _ key =
  | I : int key
  | S : string key

module K = struct
  type 'a t = 'a key

  let compare : type a b. a t -> b t -> (a, b) Gmap.Order.t = fun t t' ->
    let open Gmap.Order in
    match t, t' with
    | I, I -> Eq | I, _ -> Lt | _, I -> Gt
    | S, S -> Eq
end

module M = Gmap.Make(K)


let () =
  let m = M.empty in
  ...
  match M.find I m with
  | Some x -> Printf.printf "got %d\n" x
  | None -> Printf.printf "found nothing\n"

This is already an exhaustive pattern match: there is no need for another case (for the constructor S) since the type system knows that looking for I will result in an int.

Motivation came from parsing of protocols which usually specify optional values and extensions via a tag-length-value (TLV) mechanism: for a given tag the structure of value is different - see for example IP options, TCP options, DNS resource records, TLS hello extensions, etc.

Discussing this problem with Justus Matthiesen during summer 2017, we came up with this design. Its main difference to Daniel C. Bünzli's hmap is that in gmap the key-value GADT type must be provided when instantiating the functor. In hmap, keys are created dynamically.

Documentation

Build Status

API documentation is available online.

Installation

You need opam installed on your system. The command

opam install gmap

will install this library.

You can’t perform that action at this time.