|
| 1 | +# Design: Reduce Exported Functions (#77) |
| 2 | + |
| 3 | +## Goal |
| 4 | + |
| 5 | +Ensure every exported item has clear meaning, is well-documented, and relates to the package's vision of NP-hard problem reductions. Items that are implementation details become `pub(crate)`. |
| 6 | + |
| 7 | +## Approach: Moderate (Approach B) |
| 8 | + |
| 9 | +v0.1.x allows breaking changes — items are simply made `pub(crate)` with no deprecation. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## 1. Internalize Reduction Structs & Implementation Details |
| 14 | + |
| 15 | +### 1a. Reduction structs (`src/rules/mod.rs`) |
| 16 | + |
| 17 | +Remove all `pub use` for individual `ReductionXToY` structs. Users interact via the `ReduceTo` trait or `ReductionGraph` — they never need the named struct types. |
| 18 | + |
| 19 | +**Items to internalize (~30+):** |
| 20 | +- `ReductionCircuitToSG`, `ReductionKColoringToQUBO`, `ReductionFactoringToCircuit` |
| 21 | +- `Reduction3SATToQUBO`, `ReductionKSatToQUBO` |
| 22 | +- `ReductionISSimpleToGrid`, `ReductionISUnitDiskToGrid` |
| 23 | +- `ReductionISToSP`, `ReductionSPToIS`, `ReductionISToQUBO` |
| 24 | +- `ReductionISSimpleToTriangular` |
| 25 | +- `ReductionMatchingToSP`, `ReductionSPToQUBO` |
| 26 | +- `ReductionVCToIS`, `ReductionISToVC`, `ReductionVCToSC`, `ReductionVCToQUBO` |
| 27 | +- `ReductionSATToCircuit`, `ReductionSATToColoring` |
| 28 | +- `ReductionKSATToSAT`, `ReductionSATToKSAT` |
| 29 | +- `ReductionSATToIS`, `ReductionSATToDS` |
| 30 | +- `ReductionMaxCutToSG`, `ReductionSGToMaxCut` |
| 31 | +- `ReductionQUBOToSG`, `ReductionSGToQUBO` |
| 32 | +- All ILP reduction structs (feature-gated) |
| 33 | + |
| 34 | +### 1b. Circuit gadgets |
| 35 | + |
| 36 | +Internalize: `and_gadget`, `or_gadget`, `not_gadget`, `xor_gadget`, `set0_gadget`, `set1_gadget`, `LogicGadget` |
| 37 | + |
| 38 | +### 1c. Helper types |
| 39 | + |
| 40 | +Internalize: `BoolVar` (from `sat_maximumindependentset`), `ReductionAutoCast` struct |
| 41 | + |
| 42 | +### 1d. Unitdiskmapping internals (`src/rules/unitdiskmapping/mod.rs`) |
| 43 | + |
| 44 | +Internalize: `CopyLine`, `CellState`, `MappingGrid`, `Pattern`, `PatternCell`, `apply_gadget`, `pattern_matches`, `unapply_gadget`, `Weightable`, `map_weights`, `trace_centers`, `create_copylines`, `remove_order`, `mis_overhead_copyline`, `copyline_weighted_locations_triangular`, `mis_overhead_copyline_triangular` |
| 45 | + |
| 46 | +Keep public: `ksg`, `triangular` submodules, `MappingResult`, `GridKind` |
| 47 | + |
| 48 | +### 1e. Other internals |
| 49 | + |
| 50 | +- `src/polynomial.rs` -> `pub(crate) mod polynomial` in lib.rs |
| 51 | +- `src/truth_table.rs` -> `pub(crate) mod truth_table` in lib.rs |
| 52 | +- `NodeJson`, `EdgeJson`, `ReductionGraphJson` -> `pub(crate)` in `src/rules/graph.rs` |
| 53 | +- `ReductionEntry`, `ReductionOverhead` -> `pub(crate)` in `src/rules/registry.rs` (already internal to proc macro + graph) |
| 54 | + |
| 55 | +**Keep public in `src/rules/mod.rs`:** |
| 56 | +- Traits: `ReduceTo`, `ReductionResult` |
| 57 | +- Graph API: `ReductionGraph`, `ReductionPath`, `ExecutablePath`, `ChainedReduction`, `ReductionStep` |
| 58 | +- Cost: `CustomCost`, `Minimize`, `MinimizeSteps`, `PathCostFn` |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## 2. Consolidate Validation Functions into Problem Methods |
| 63 | + |
| 64 | +Add `is_valid_solution(&self, config: &[usize]) -> bool` inherent methods to each problem type. The existing free functions become `pub(crate)`. |
| 65 | + |
| 66 | +| Free function | Problem type | New method | |
| 67 | +|---|---|---| |
| 68 | +| `is_independent_set` | `MaximumIndependentSet` | `is_valid_solution` | |
| 69 | +| `is_clique` | `MaximumClique` | `is_valid_solution` | |
| 70 | +| `is_vertex_cover` | `MinimumVertexCover` | `is_valid_solution` | |
| 71 | +| `is_dominating_set` | `MinimumDominatingSet` | `is_valid_solution` | |
| 72 | +| `is_matching` | `MaximumMatching` | `is_valid_solution` | |
| 73 | +| `is_valid_coloring` | `KColoring` | `is_valid_solution` | |
| 74 | +| `is_maximal_independent_set` | `MaximalIS` | `is_valid_solution` | |
| 75 | +| `is_hamiltonian_cycle` | `TravelingSalesman` | `is_valid_solution` | |
| 76 | +| `is_satisfying_assignment` | `Satisfiability` | `is_valid_solution` | |
| 77 | +| `is_biclique_cover` | `BicliqueCover` | `is_valid_solution` | |
| 78 | +| `is_circuit_satisfying` | `CircuitSAT` | `is_valid_solution` | |
| 79 | +| `is_factoring` | `Factoring` | `is_valid_solution` | |
| 80 | +| `is_set_packing` | `MaximumSetPacking` | `is_valid_solution` | |
| 81 | +| `is_set_cover` | `MinimumSetCovering` | `is_valid_solution` | |
| 82 | + |
| 83 | +Value-returning functions also become methods: |
| 84 | + |
| 85 | +| Free function | Problem type | New method | |
| 86 | +|---|---|---| |
| 87 | +| `cut_size` | `MaxCut` | `cut_size(&self, config)` | |
| 88 | +| `count_paint_switches` | `PaintShop` | `count_switches(&self, config)` | |
| 89 | + |
| 90 | +General matrix utilities become `pub(crate)`: |
| 91 | +- `boolean_matrix_product` — not owned by BMF type, internal utility |
| 92 | +- `matrix_hamming_distance` — same |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## 3. Config & Module Consolidation |
| 97 | + |
| 98 | +### 3a. Config utilities (`src/config.rs`) |
| 99 | + |
| 100 | +Make `pub(crate)`: `config_to_bits`, `bits_to_config` |
| 101 | + |
| 102 | +Keep public: `ConfigIterator`, `DimsIterator`, `index_to_config`, `config_to_index` |
| 103 | + |
| 104 | +### 3b. Remove `graph_types` module |
| 105 | + |
| 106 | +`src/graph_types.rs` is redundant with `src/topology/`. Delete and update all internal `use crate::graph_types::X` to `use crate::topology::X`. |
| 107 | + |
| 108 | +### 3c. Circuit support types |
| 109 | + |
| 110 | +- Keep public: `Circuit`, `BooleanExpr`, `BooleanOp` (needed to construct `CircuitSAT`) |
| 111 | +- Make `pub(crate)`: `Assignment` (internal evaluation detail) |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## 4. Updated Prelude |
| 116 | + |
| 117 | +```rust |
| 118 | +pub mod prelude { |
| 119 | + // Problem types |
| 120 | + pub use crate::models::graph::{ |
| 121 | + KColoring, MaxCut, MaximalIS, MaximumClique, MaximumIndependentSet, |
| 122 | + MaximumMatching, MinimumDominatingSet, MinimumVertexCover, TravelingSalesman, |
| 123 | + }; |
| 124 | + pub use crate::models::optimization::{SpinGlass, QUBO}; |
| 125 | + pub use crate::models::satisfiability::{CNFClause, KSatisfiability, Satisfiability}; |
| 126 | + pub use crate::models::set::{MaximumSetPacking, MinimumSetCovering}; |
| 127 | + pub use crate::models::specialized::{BicliqueCover, BMF, CircuitSAT, Factoring, PaintShop}; |
| 128 | + |
| 129 | + // Core traits |
| 130 | + pub use crate::traits::{OptimizationProblem, Problem, SatisfactionProblem}; |
| 131 | + pub use crate::rules::{ReduceTo, ReductionResult}; |
| 132 | + pub use crate::solvers::{BruteForce, Solver}; |
| 133 | + |
| 134 | + // Types |
| 135 | + pub use crate::types::{Direction, One, SolutionSize, Unweighted}; |
| 136 | + pub use crate::error::{ProblemError, Result}; |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +**Removed from prelude** (still accessible via full path): |
| 141 | +- `config::*` — power-user utilities |
| 142 | +- `registry::{ComplexityClass, ProblemInfo, ProblemMetadata}` — metadata introspection |
| 143 | +- `variant::{CastToParent, KValue, VariantParam, K1..KN}` — type-level machinery |
| 144 | +- `types::{NumericSize, ProblemSize, WeightElement}` — trait bounds users rarely write |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## 5. Final Public Module Summary |
| 149 | + |
| 150 | +| Module | Visibility | Key public items | |
| 151 | +|--------|-----------|-----------------| |
| 152 | +| `models` | `pub` | All problem types, `Circuit`, `BooleanExpr`, `BooleanOp` | |
| 153 | +| `rules` | `pub` | `ReduceTo`, `ReductionResult`, `ReductionGraph`, path types, cost types, `unitdiskmapping::{ksg, triangular, MappingResult, GridKind}` | |
| 154 | +| `solvers` | `pub` | `Solver`, `BruteForce`, `ILPSolver` (feature-gated) | |
| 155 | +| `traits` | `pub` | `Problem`, `OptimizationProblem`, `SatisfactionProblem` | |
| 156 | +| `types` | `pub` | `Direction`, `SolutionSize`, `One`/`Unweighted`, `NumericSize`, `WeightElement`, `ProblemSize` | |
| 157 | +| `error` | `pub` | `ProblemError`, `Result` | |
| 158 | +| `config` | `pub` | `ConfigIterator`, `DimsIterator`, `index_to_config`, `config_to_index` | |
| 159 | +| `topology` | `pub` | Graph types, `Graph` trait, `small_graphs` | |
| 160 | +| `export` | `pub` | JSON export types (used by examples) | |
| 161 | +| `io` | `pub` | `read_problem`, `write_problem`, `to_json`, `from_json` | |
| 162 | +| `registry` | `pub` | `ProblemInfo`, `ComplexityClass`, `ProblemMetadata`, `collect_schemas` | |
| 163 | +| `variant` | `pub` | `VariantParam`, `CastToParent`, `KValue`, K markers | |
| 164 | +| `testing` | `pub` | Test macros & helpers | |
| 165 | +| `polynomial` | `pub(crate)` | Internal | |
| 166 | +| `truth_table` | `pub(crate)` | Internal | |
| 167 | +| `graph_types` | **deleted** | Consolidated into `topology` | |
0 commit comments