ternary-lattice
Lattice structures for ternary values — partial orders, semilattice operations, lattice maps, and morphisms over the domain {-1, 0, +1}.
Why This Exists
Ternary logic shows up in fuzzy systems, three-valued logics, signal processing, and decision models — but the algebraic backbone (lattices, orders, morphisms) rarely gets a dedicated treatment. This crate provides that foundation: well-defined partial orders, meet/join operations, pointwise maps, and order-preserving morphisms, all on the compact ternary domain. It's no_std and forbid(unsafe_code) so it works in embedded and safety-critical contexts.
Core Concepts
- Ternary values:
Neg(-1),Zero(0),Pos(+1) — the three-element domain. - Flat ordering (information ordering):
Zerois bottom (unknown),NegandPosare incomparable concrete values. - Linear ordering (numeric):
-1 ≤ 0 ≤ +1— a total order. - Meet / Join: Greatest lower bound and least upper bound under either ordering.
- MeetSemilattice / JoinSemilattice: Collect elements and compute the meet or join of the entire set.
- LatticeMap: A BTreeMap keyed by ternary lattice points, with pointwise merge operations.
- LatticeMorphism: Order-preserving (monotone) functions between ternary lattices, with composition and monotonicity checking.
- Structural metrics:
chain_heightandlattice_widthfor each ordering.
Quick Start
# Cargo.toml
[]
= "0.1"
use ;
API Overview
| Type | Description |
|---|---|
Ternary |
The fundamental value: Neg, Zero, Pos |
LatticeOrder |
Flat (information) or Linear (numeric) ordering |
TernaryLattice |
Lattice with le, meet, join, bottom, top |
MeetSemilattice |
Accumulate values, compute global meet |
JoinSemilattice |
Accumulate values, compute global join |
LatticeMap<V> |
Map from ternary points to values, with merge_meet |
LatticeMorphism |
Monotone map between lattices, with compose and is_monotone |
chain_height / lattice_width |
Structural metrics |
How It Works
The crate defines two partial orders on the three-element set. Under flat ordering, Zero acts as bottom (unknown/undefined) while Neg and Pos are incomparable — this mirrors three-valued logic (Kleene). Under linear ordering, the natural numeric total order is used.
Meet and join are computed pointwise under the chosen ordering. The MeetSemilattice and JoinSemilattice types fold over collections of ternary values. LatticeMap stores arbitrary values keyed by ternary positions and supports merge with conflict detection. LatticeMorphism encodes a function from one lattice to another (identity, constant, negation, or custom) and verifies monotonicity by checking all pairs under the source ordering.
Use Cases
- Three-valued logic engines: Use the flat ordering as the algebraic backbone for Kleene-style logics where
0means "unknown." - Fuzzy/uncertainty aggregation: Merge multiple ternary signals (approve/abstain/reject) using meet/join with conflict detection.
- Formal verification: Lattice morphisms and monotonicity checking provide a lightweight framework for proving properties of ternary transformations.
- Embedded signal classification:
no_stdcompatible — use lattice operations for ternary sensor fusion on microcontrollers.
Known Limitations
-
Flat ordering treats conflicting inputs as bottom: Under
LatticeOrder::Flat, the meet ofNegandPosisZero(bottom). This means if two sources disagree (one says Neg, one says Pos), the result is "unknown" — information is discarded rather than preserved as a conflict flag. UseLatticeMap::merge_meet()and check for key collisions if you need conflict detection. -
LatticeMorphism::is_monotone()is O(n²): Monotonicity checking tests all pairs of input values under the source ordering. For the ternary domain this is fine (3² = 9 pairs), but the implementation doesn't generalize — it hardcodes the three-element lattice, so composing morphisms withcompose()and re-checking monotonicity always runs the same 9 checks regardless of the composed function's structure. -
LatticeMapusesBTreeMap, not a flat array: Since there are only 3 possible keys (Neg, Zero, Pos), usingBTreeMap<Ternary, V>allocates heap memory for tree nodes. A flat[Option<V>; 3]array would be more efficient and avoid allocation, which matters inno_stdenvironments. -
No Galois connection support: The crate provides morphisms (functions between lattices) but not Galois connections (adjoint pairs of monotone functions), which are the standard tool for abstract interpretation and program analysis.
-
MeetSemilatticeandJoinSemilatticedon't track insertion order: Themeet_all()/join_all()results depend only on the set of inserted values, not their order. If you need to know when a conflict occurred, you must track it externally.
Ecosystem
Part of the SuperInstance ternary computing suite:
ternary-lattice— this crateternary-codes— error-correcting codes for ternary dataternary-gradient— gradient-free optimization on ternary landscapesternary-language— ternary NLP and grammar processingternary-trees— ternary decision trees and foreststernary-transform— wavelet, Fourier, and kernel transformsternary-planning— planning and scheduling with ternary prioritiesternary-rl— reinforcement learning with ternary actionsternary-som— self-organizing maps for ternary dataternary-failure— failure analysis with ternary classification
License
MIT
See Also
- ternary-ring — related
- ternary-algebra — related
- ternary-topology — related
- ternary-tuple — related
- ternary-logic — related