keigoi / hlist-ocaml

Heterogeneously-typed lists for OCaml

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hlist: Heterogeneously-typed lists for OCaml

An implementation of heterogeneously-typed lists in (pure) OCaml.

Usage

open Hlist

(* A heterogeneously-typed list *)
let x = Hetero.[123; "abc"; true]
(*
val x :
  [ `cons of
      int *
      [ `cons of string * [ `cons of bool * ([ `cons of unit * 'a ] as 'a) ] ]
  ] seq
*)

let a = seq_head x
(* 123 *)

let b = seq_head x
(* ["abc"; true] *)

(** Getting elements by index *)

let x0 = seq_get Zero x 
(* 123 *)

let x1 = seq_get (Succ Zero) x
(* "abc" *)

let x2 = seq_get (Succ (Succ Zero)) x
(* true *)

(* Returns the unit value for the index beyond the end *)
let x3 = seq_get (Succ (Succ (Succ Zero))) x
(* () *)

(** Updating an element by index, polymorphically *)

let y0 = seq_put Zero x `Hello_world
(*
val y0 :
  [ `cons of
      _[> `Hello_world ] *
      [ `cons of string * [ `cons of bool * ([ `cons of unit * 'a ] as 'a) ] ]
  ] seq
*)

let y1 = seq_put (Succ (Succ Zero)) y0 ["this"; "is"; "a"; "homogeneous"; "list"]
(*
val y1 :
  [ `cons of
      _[> `Hello_world ] *
      [ `cons of string * [ `cons of string list * ([ `cons of unit * 'a ] as 'a) ] ]
  ] seq
*)

Installation and development

Trying with utop:

git clone http://github.com/keigoi/hlist-ocaml.git
cd hlist-ocaml
dune utop

Installation:

opam install http://github.com/keigoi/hlist-ocaml.git

Uninstall:

opam remove hlist

Contact

References

The Peano-number based index is introduced in Fig. 6 in the following paper:

And heterogeneously-typed lists are originally introduced in:

  • Oleg Kiselyov, Ralf Lämmel and Keean Schupke: Strongly typed heterogeneous collections. Haskell 2004, pp. 96-107, 2004.

About

Heterogeneously-typed lists for OCaml

License:MIT License


Languages

Language:OCaml 98.4%Language:Makefile 1.6%