package dunolint-lib
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=1b064927c9e1ef5352a1886ae34a206fef0ce6a913c19a77b0162acc108e0e50
sha512=6cbc08ba318bef6584d15a4491e3dde1bf436109ce0f8b7c400a9f91bbcee64c5785bc924df11eafe98243ec2f188a7f92c58c5062729f3e2af1e9977f1a5e67
doc/dunolint-lib.vendor_blang/Blang/index.html
Module Blang
Source
Boolean expressions.
A blang is a boolean expression built up by applying the usual boolean operations to properties that evaluate to true or false in some context.
Usage
For example, imagine writing a config file for an application that filters a stream of integers. Your goal is to keep only those integers that are multiples of either -3 or 5. Using Blang
for this task, the code might look like:
module Property = struct
type t =
| Multiple_of of int
| Positive
| Negative
[@@deriving sexp]
let eval t num =
match t with
| Multiple_of n -> num % n = 0
| Positive -> num > 0
| Negative -> num < 0
end
type config = {
keep : Property.t Blang.t;
} [@@deriving sexp]
let config = {
keep =
Blang.t_of_sexp
Property.t_of_sexp
(Sexp.of_string
"(or (and negative (multiple_of 3)) (and positive (multiple_of 5)))";
}
let keep config num : bool =
Blang.eval config.keep (fun p -> Property.eval p num)
Note how positive
and negative
and multiple_of
become operators in a small, newly-defined boolean expression language that allows you to write statements like (and negative (multiple_of 3))
.
Blang sexp syntax
The blang sexp syntax is almost exactly the derived one, except that:
1. Base properties are not marked explicitly. Thus, if your base property type has elements FOO, BAR, etc., then you could write the following Blang s-expressions:
FOO (and FOO BAR) (if FOO BAR BAZ)
and so on. Note that this gets in the way of using the blang "keywords" in your value language.
2. And
and Or
take a variable number of arguments, so that one can (and probably should) write
(and FOO BAR BAZ QUX)
instead of
(and FOO (and BAR (and BAZ QUX)))
If you want to see the derived sexp, use Raw.sexp_of_t
.
Note that the sexps are not directly inferred from the type below -- there are lots of fancy shortcuts. Also, the sexps for 'a
must not look anything like blang sexps. Otherwise t_of_sexp
will fail. The directly inferred sexps are available via Raw.sexp_of_t
.