package gmap

  1. Overview
  2. Docs
Heterogenous maps over a GADT

Install

Dune Dependency

Authors

Maintainers

Sources

gmap-0.3.0.tbz
sha256=04dd9e6226ac8f8fb4ccb6021048702e34a482fb9c1d240d3852829529507c1c
sha512=71616981f5a15d6b2a47e18702083e52e81f6547076085b1489f676f50b0cc47c7c2c4fa19cb581e2878dc3d4f7133d0c50d8b51a8390be0e6e30318907d81d3

Description

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.

Published: 21 Apr 2019

README

Gmap - heterogenous maps over a GADT

0.3.0

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 B) since the type system knows that looking for A 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

API documentation is available online.

Installation

You need opam installed on your system. The command

opam install gmap

will install this library.

Dependencies (2)

  1. dune
  2. ocaml >= "4.04.2"

Dev Dependencies (2)

  1. fmt with-test
  2. alcotest with-test

Used by (3)

  1. dns >= "4.0.0"
  2. icalendar >= "0.1.1"
  3. x509 >= "0.7.0"

Conflicts

None