Skip to content
vic

keigoi/hlist-ocaml

Heterogeneously-typed lists for OCaml

keigoi/hlist-ocaml.json
{
"createdAt": "2022-02-18T23:41:18Z",
"defaultBranch": "main",
"description": "Heterogeneously-typed lists for OCaml",
"fullName": "keigoi/hlist-ocaml",
"homepage": null,
"language": "OCaml",
"name": "hlist-ocaml",
"pushedAt": "2022-05-04T15:22:42Z",
"stargazersCount": 13,
"topics": [],
"updatedAt": "2025-11-10T09:19:17Z",
"url": "https://github.com/keigoi/hlist-ocaml"
}

hlist: Heterogeneously-typed lists for OCaml

Section titled “hlist: Heterogeneously-typed lists for OCaml”

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

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
*)

Trying with utop:

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

Installation:

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

Uninstall:

Terminal window
opam remove hlist

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.