Skip to main content

sim_shape/
lib.rs

1#![forbid(unsafe_code)]
2#![allow(deprecated)]
3#![deny(missing_docs)]
4//! Shape algebra, comparison, and match-hook helpers.
5//!
6//! `sim-shape` supplies concrete `Shape` implementations while the kernel owns
7//! only the open shape protocol.
8//!
9//! ```rust
10//! use std::sync::Arc;
11//!
12//! use sim_kernel::{Cx, DefaultFactory, Expr, NoopEvalPolicy};
13//! use sim_shape::{
14//!     AnyShape, ExactExprShape, ExprKind, ExprKindShape, HookedShape, Shape,
15//!     ShapeRelationKind, TraceMarkHook, relate_shapes,
16//! };
17//!
18//! let mut cx = Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
19//! let exact_true = ExactExprShape::new(Expr::Bool(true));
20//! let bool_expr = ExprKindShape::new(ExprKind::Bool);
21//! let relation = relate_shapes(&mut cx, &exact_true, &bool_expr, &[]).unwrap();
22//!
23//! assert_eq!(relation.kind, ShapeRelationKind::LeftSubshape);
24//! assert!(relation.proven);
25//!
26//! let hooked = HookedShape::new(Arc::new(AnyShape), vec![Arc::new(TraceMarkHook)]);
27//! let matched = hooked.check_expr(&mut cx, &Expr::String("trace me".to_owned())).unwrap();
28//! assert!(matched.accepted);
29//! assert!(matched.diagnostics.iter().any(|diagnostic| {
30//!     diagnostic.message.starts_with("shape-hook:mark")
31//! }));
32//! ```
33//!
34//! # Surface
35//!
36//! The engine is organized into private module groups whose public items are
37//! re-exported at the crate root (the modules themselves are not public):
38//!
39//! - `primitives` -- atomic shapes (`AnyShape`, `ExactExprShape`,
40//!   `ExprKindShape`, `ClassShape`, `NumberValueShape`, `ListShape`,
41//!   `FieldShape`) and the combinators and object-grammar parsers built on
42//!   them.
43//! - `algebra` -- boolean and collection shape algebra: `AndShape`,
44//!   `OrShape`, `NotShape`, `RepeatShape`, and `TableShape` with its field and
45//!   extra-field policies.
46//! - `compare` -- shape comparison and subsumption: normalization
47//!   (`normalize_shape`, `ShapeNormalForm`), relation analysis
48//!   (`relate_shapes`, `ShapeRelation`), and `VennShapeSet` set reasoning.
49//! - `citizen` -- citizen integration: class registration, codec, and
50//!   construction for shape objects, plus the `*_class_symbol` accessors.
51//! - `functions` -- the callable shape object: `FunctionObject`,
52//!   `FunctionCase`, overload selection, and shape-as-value wrapping.
53//! - `hooks` -- match-extension hooks: `HookedShape` and the `MatchHook`
54//!   protocol with the built-in hook implementations.
55//! - `parse` -- the shape grammar parser that turns an `Expr` into a `Shape`
56//!   and runs checks against expressions and values.
57//! - `base` -- the base shape vocabulary re-exported from the kernel
58//!   (`Shape`, `ShapeMatch`, `ShapeDoc`, `Bindings`, `ShapeReport`).
59
60mod algebra;
61mod base;
62mod citizen;
63#[cfg(test)]
64mod citizen_tests;
65mod compare;
66mod diagnostics;
67mod functions;
68mod hooks;
69mod parse;
70mod primitives;
71mod recursion;
72#[cfg(test)]
73mod tests;
74
75pub use algebra::{
76    AndShape, NotShape, OrShape, OrStrategy, RepeatShape, TableExtraPolicy, TableFieldSpec,
77    TableShape,
78};
79pub use base::{
80    Bindings, ExprKind, MatchScore, Shape, ShapeBindings, ShapeDoc, ShapeMatch, ShapeReport,
81    check_value_report, insert_shape_satisfaction_claim, satisfies_shape_predicate,
82    shape_report_from_match,
83};
84pub use citizen::{
85    and_shape_class_symbol, any_shape_class_symbol, class_shape_class_symbol,
86    exact_expr_shape_class_symbol, expr_kind_shape_class_symbol, hooked_shape_class_symbol,
87    list_shape_class_symbol, not_shape_class_symbol, or_shape_class_symbol,
88    repeat_shape_class_symbol, table_shape_class_symbol, venn_shape_set_class_symbol,
89};
90pub use compare::{
91    ShapeNormalForm, ShapeNormalKind, ShapeProbe, ShapeRelation, ShapeRelationKind, ShapeWitness,
92    VennShapeSet, normalize_shape, relate_shapes,
93};
94pub use diagnostics::{
95    binding_failure_diagnostic, callable_mismatch_diagnostic, expected_shape_diagnostic,
96    overload_selection_diagnostic,
97};
98pub use functions::{
99    FunctionCase, FunctionObject, NativeFunctionImpl, SelectedCase, ShapeObject, case_result_shape,
100    case_shape, function_cases, overload, shape_value, shape_value_with_encoding,
101};
102pub use hooks::{
103    AcceptOnNoDiagnosticsHook, DiscardOnDiagnosticPrefixHook, HookedShape, MatchHook,
104    MatchHookContext, MatchHookDecision, MatchHookKind, MatchHookObject, MatchHookPhase,
105    MatchHookTargetKind, ScoreFloorHook, TraceMarkHook, accept_on_no_diagnostics_hook_class_symbol,
106    discard_on_diagnostic_prefix_hook_class_symbol, hook_ref_arc, hook_value,
107    score_floor_hook_class_symbol, trace_mark_hook_class_symbol,
108};
109pub use parse::{check_shape_on_expr, check_shape_on_value, parse_shape_expr, shape_error};
110pub use primitives::{
111    AnyShape, CaptureShape, ClassShape, EffectfulShape, ExactExprShape, ExprKindShape, FieldShape,
112    FieldSpec, ListShape, NumberValueShape, ObjectExpr, OneOfShape, PrattShape, ShapeExprParser,
113};