packagegmap
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=04dd9e6226ac8f8fb4ccb6021048702e34a482fb9c1d240d3852829529507c1c
sha512=71616981f5a15d6b2a47e18702083e52e81f6547076085b1489f676f50b0cc47c7c2c4fa19cb581e2878dc3d4f7133d0c50d8b51a8390be0e6e30318907d81d3
Description
Published:21 Apr 2019
README
Gmap - heterogenous maps over a GADT
0.3.0
Gmap exposes the functorMake
which takes a key type (aGADT '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 stdlibMap data structure.
type _ key = | I : int key | S : string keymodule 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 -> Eqendmodule 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 constructorB
) since the type system knows that looking forA
will result in anint
.
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'shmap 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 needopam installed on your system. The command
opam install gmap
will install this library.