implicit_predicates_gen/lib.rs
1//! Code generator for exact geometric predicates.
2//!
3//! Turns the definition of a predicate or of a point type into specialized
4//! Rust code for the `implicit-predicates` crates.
5//!
6//! **Status: early development.** [`generate`] takes the definitions of a
7//! `.pred` file and returns, for each predicate, the Rust module of its
8//! stages: a floating-point filter, exact stages on floating-point
9//! expansions, and the chain that runs them (see [`generate`] for the items
10//! of the module), and the module of its checks for tests: an exact
11//! evaluator and inputs at the edge of the error bound of the filter. The
12//! `implicit-predicates-gen` command line tool writes those modules for a
13//! directory of definitions, formatted with `rustfmt`, or checks with
14//! `--check` that the files are up to date; its README gives the command
15//! that regenerates the code of the `implicit-predicates` crate.
16//!
17//! The steps of the generation are public as well. [`parse`] takes a `.pred`
18//! file, written in a small subset of Rust, and returns each [`Predicate`] as
19//! an expression [`Graph`] in which equal subexpressions share a node.
20//! Mistakes in the file are reported as an [`Error`] that carries the position
21//! of the offending token.
22//!
23//! A definition is a function over `Point3` parameters returning `Sign`. Its
24//! body holds `let` bindings and a final expression built from `+`, `-` (binary
25//! and unary), `*`, parentheses, integer literals, the coordinates `p.x`,
26//! `p.y`, `p.z` of a parameter and the names of earlier bindings. A second
27//! function with the same name and parameters, marked `#[exact]`, gives the
28//! form used for exact evaluation. Comments cost nothing; doc comments
29//! (`///`, `//!`, `/** */`, `/*! */`) are rejected, and so are predicate
30//! names that cannot name a module, such as keywords.
31//!
32//! The only limit of [`parse`] is the nesting of brackets, at most 32 deep,
33//! which keeps the stack small: sums, products and runs of unary minus are
34//! read in loops, so [`parse`] cannot panic or overflow the stack of the
35//! calling thread, even in a debug build with a 1 MiB stack, and it accepts
36//! a sum of any length. The later steps have budgets instead: a sum written
37//! as one chain of more than about 1,560 terms is rejected by
38//! [`filter_bound`], because the exact coefficients of its error bound grow
39//! with the length of the chain, an expression too large to expand is
40//! rejected by [`canonical`], and [`generate`] rejects main forms of more
41//! than 256 distinct subexpressions, for which the search of the edge cases
42//! of the checks would be slow. Each reports the limit it hit, before doing
43//! the work; the work section of [`generate`] lists every budget.
44//!
45//! Two analyses work on the graphs. [`check_same_polynomial`] expands the
46//! main and the exact form of a predicate with [`canonical`] and requires
47//! them to be the same, nonzero polynomial. [`difference_graph`] checks that
48//! a form is homogeneous in differences of point coordinates, that is, built
49//! from differences of one coordinate of two points, combined by sums of
50//! terms of equal degree and by products, and returns it as a [`DiffGraph`]
51//! with the degree of every node.
52//!
53//! [`filter_bound`] derives, in exact arithmetic, the error bound of
54//! evaluating a [`DiffGraph`] in `f64`: with `D` the largest computed
55//! difference plus the two largest errors of the points, and `E` the sum of
56//! those two errors, the computed value differs from the exact one by at most
57//! `(epsilon * D + kappa * E) * D^(k - 1)`, where `k` is the degree.
58//! [`FilterBound::constants`] gives `epsilon` and `kappa` rounded up to `f64`,
59//! and the range of `D`, between two powers of two, in which overflow and
60//! underflow cannot invalidate the bound.
61//!
62//! [`exact_plan_raw`] and [`exact_plan_differences`] derive, for each exact
63//! stage, the range of input magnitudes for which every exact product of the
64//! arithmetic core stays in its domain and no value overflows, and the
65//! worst-case length of every temporary; [`generate`] sizes the stack
66//! buffers of the emitted stages from them.
67
68#![forbid(unsafe_code)]
69
70mod bound;
71mod cursor;
72mod differences;
73mod dyadic;
74mod emit;
75mod error;
76mod expr;
77mod graph;
78mod limits;
79mod parse;
80mod polynomial;
81mod range;
82
83pub use bound::{FilterBound, FilterConstants, filter_bound};
84pub use differences::{DiffGraph, DiffNode, difference_graph};
85pub use emit::{Generated, generate};
86pub use error::Error;
87pub use graph::{Graph, Node, NodeId};
88pub use parse::{Position, Predicate, parse};
89pub use polynomial::{Polynomial, canonical, canonical_of_differences, check_same_polynomial};
90pub use range::{ExactPlan, ExactRange, Leaves, exact_plan_differences, exact_plan_raw};