package refl

  1. Overview
  2. Docs
PPX deriver for reflection

Install

Dune Dependency

Authors

Maintainers

Sources

v0.2.1.tar.gz
sha512=e7eeb7e3a2831db4fccc9e133b63bfac950f231350ff5f726d800bc0a7a2eb1313d38ffff0e1a0b399ca431cd8b9c792b5a2846f537775df8f338b1e1670e5d9

Description

PPX deriver for reflection

Published: 17 Jul 2020

README

refl: Efficient and type-safe type reflection for OCaml

refl provides runtime representations for most OCaml types, and a "one size fits all" deriving plugin. This plugin automatically derives runtime type representations from type declarations, allowing most other deriving plugins to be rewritten as regular OCaml functions parametized by these runtime representations. The library also provides such functions which provides flexible alternatives to ppx_deriving standard plugins: show, compare, eq, map, iter, fold, enum, make.

Once refl is derived for a given type, values of this type can be used on all (compatible) functions. This is the main motivation behind refl: instead of having to decide which derivers to use at the type declaration point, it is sufficient to derive only refl, and then this type can be used with all functions operating on such runtime representations, even functions that are defined after the type declaration.

refl can be used with dune by using the preprocess field.

(executable
  ...
  (preprocess (pps refl.ppx))
  (libraries ... refl ...))

Basic usage

The following example declares a type for binary trees along with its runtime representation.

type 'a binary_tree =
  | Leaf
  | Node of { left : 'a binary_tree; label : 'a; right : 'a binary_tree }
        [@@deriving refl]

The function Refl.show can be used to print such trees, the function Refl.compare can be used to compare two binary trees, the function Refl.map can be used to change labels, etc. For instance:

# Refl.show [%refl: string binary_tree] []
    (Node { left = Leaf; label = "root"; right = Leaf });;
- : string = "Node { left = Leaf; label = \"root\"; right = Leaf }"

Built-in types can be used directly without prior declaration.

# Refl.show [%refl: (string * int) list] [] ["a", 1; "b", 2];;
- : string = "[(\"a\", 1); (\"b\", 2)]"

When a type contains type variables, custom printers for these variables should be listed in the second argument of Refl.show.

# Refl.show [%refl: _ list] [Some Format.pp_print_string] ["a"; "b"; "c"];;
- : string = "[a; b; c]"

The custom printers are optional for skipping "shadow" type variables, that is to say type variables that are not used to type sub-values. This will be particularly useful for GADTs. Typically, shadow type variables appear in GADTs as indexes. For instance, consider the following definition for fixed-length vector.

type ('a, 'length) vector =
  | [] : ('a, [`Zero]) vector
  | (::) : 'a * ('a, 'length) vector -> ('a, [`Succ of 'length]) vector
        [@@deriving refl]

When applying Refl.show to vectors, None can be passed for the printer of the second parameter ('length), since it will never be used.

# Refl.show [%refl: (_, _) vector] [Some Format.pp_print_string; None]
    ["a"; "b"; "c"];;
- : string = "[a; b; c]"

Another example of function is Refl.map, that applies a given function for each type variables.

# Refl.map [%refl: _ binary_tree] [%refl: _ binary_tree] [P string_of_int]
    (Node { left = Leaf; label = 1; right = Leaf });;
  - : string binary_tree = Node {left = Leaf; label = "1"; right = Leaf}

Note that the reflexive representation keeps track whether type variables occur positively or negatively. The P constructor above marks functions appliable to type variables that appear positively (outside arrow types or at the right-hand side of arrow types). For types that appear negatively, functions in the reverse direction should be provided, marked with N. For types that appear both positively and negatively, a pair of functions for both directions should be provided, marked with PN.

Type constraints

Functions can require constraints to be satisfied by the type to be applicable: for instance, the function compare requires that the type contains no arrows unless they are hidden by an [@opaque] attribute.

Dependencies (7)

  1. traverse >= "0.2.0" & < "0.3.0"
  2. metaquot >= "0.2.0" & < "0.3.0"
  3. metapp >= "0.2.0" & < "0.3.0"
  4. fix >= "20200131"
  5. stdcompat >= "14"
  6. dune >= "1.11.0"
  7. ocaml >= "4.03.0" & < "4.12.0"

Dev Dependencies (1)

  1. odoc with-doc

Used by (4)

  1. clangml >= "4.1.0" & < "4.3.0"
  2. clangml-transforms >= "0.25"
  3. override >= "0.3.0"
  4. pattern = "0.2.0"

Conflicts

None

OCaml

Innovation. Community. Security.