package yuujinchou

  1. Overview
  2. Docs
Name pattern combinators

Install

Dune Dependency

Authors

Maintainers

Sources

2.0.0.tar.gz
md5=4e111316943fcd39707bfbb47897c46e
sha512=d53f08a462ea26b59af3e8e1f8bf1b643f57e23d89b5ddf9c9091ec8341023e4600067c3dd7965173c2819f702c9df7668975ad0abe54b31d7684cfa92328c21

Description

This package implements a pattern language for selecting names. It intends to facilitate the implementation of the "import" statement or any feature that allows users to select a group of names by patterns.

Published: 08 Mar 2022

README

README.mkd

# Yuujinchou: Name Pattern Combinators

_Yuujinchou_ is an OCaml package of name patterns for implementing import statements. Please consult the [generated documentation](https://redprl.org/yuujinchou/yuujinchou/Yuujinchou) for more details.

## How to Use It

```ocaml
open Yuujinchou

module Data =
struct
  type t = int
  let equal n1 n2 = n1 = n2
  let merge ~rev_path x y =
    if equal x y then
      Result.ok x
    else
      Result.error @@ `Inconsistent (List.rev rev_path)
  let shadow ~rev_path:_ _x y = y
  let compare : t -> t -> int = compare
end

(** An environment is a mapping from paths to data. *)
type env = Data.t Trie.t

(** [remap pattern env] uses the [pattern] to massage
    the environment [env]. *)
let remap pattern env =
  let pp_path = function [] -> "(root)" | path -> String.concat "." path in
  match Action.run ~union:Data.merge pattern env with
  | Ok env -> env
  | Error (`Inconsistent path) ->
    failwith ("Inconsistent data assigned to the same path " ^ pp_path path)
  | Error (`BindingNotFound path) ->
    failwith ("Expected binding(s) not found within the subtree at " ^ pp_path path)

(** [import env pattern imported] imports the environment
    [imported] massaged by [pattern] into [env]. *)
let import env pattern imported =
  Trie.union Data.shadow env @@ remap pattern imported

module DataSet = Set.Make (Data)

(** [select env pattern] returns the set of matched data. *)
let select env pattern =
  DataSet.of_seq @@ Trie.to_seq_values @@ remap pattern env
```

## Installation

You need OCaml 4.08.0 or newer. The package is available on the OPAM repository:
```
opam install yuujinchou
```

You could also check out the source repository and install the latest version in development:
```
git clone https://github.com/RedPRL/yuujinchou.git
opam install ./yuujinchou
```

Dependencies (2)

  1. ocaml >= "4.12.0"
  2. dune >= "2.7"

Dev Dependencies (4)

  1. odoc with-doc
  2. qcheck-alcotest >= "0.18" & with-test
  3. qcheck >= "0.18" & with-test
  4. alcotest >= "1.5.0" & with-test

Used by

None

Conflicts

None