1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Scoped index newtypes for the two variable spaces this crate mixes.
//!
//! # Why these exist, and why they are not everywhere
//!
//! gh#764 item 3 proposed `VarX`/`FullX`/`KktRow`/`UserG` across the
//! crate, then measured the cost: ~342 touch points across 12 files,
//! `pub trait SensBacksolver` re-exported from the crate root (so
//! changing `solve_released` is a breaking API change), and 31 sites in
//! `pounce-py` where indices cross into Python as `i64`. That sweep is
//! not worth its price, and this module is not it.
//!
//! What survived the measurement is a much sharper rule:
//!
//! > **Table lookup fails loudly; direct array indexing fails
//! > silently.**
//!
//! A released row resolved through `rows.iter().find(|b| b.row == r)?`
//! turns a space-swap into `SensComputationFailed`, immediately and
//! every time. A direct index into a `Vec` does not: it returns a
//! *neighbouring variable's* answer, which is in range, plausible, and
//! wrong. That is gh#450, and gh#672 finding 1 shipped it again as
//! `report.var_status.get(var_row)` — a var-x row indexing a full-x
//! array.
//!
//! So the newtype earns its cost at exactly one shape: **a conversion
//! whose result feeds a direct array index, in a scope where the other
//! space is also live.** Measured over `pounce-sensitivity`, that is
//! two sites, both in `solver.rs`:
//!
//! * the kappa computation's `var_sigma` read, whose `unwrap_or(0.0)`
//! turns a miss into a zero that silently drops the row from the
//! engaged set;
//! * `weakly_active_bounds`, whose loop body indexes `ctx.x_curr` /
//! `ctx.lo` / `ctx.hi` by var-x **and** `report.var_status` by
//! full-x, with a bound row `b.row` in scope as a third space.
//!
//! The other nine conversion sites do not earn it, and are left alone
//! deliberately:
//!
//! * `activity.rs` has four scatter loops (`var_full[full_of(i)] = e`)
//! where the index is converted at the point of use and only one
//! space is live — there is nothing to mix up.
//! * `algorithm_backsolver.rs:871` resolves through a checked
//! `get_mut(..).ok_or_else(..)` and then sweeps for an unfilled NaN
//! sentinel; it fails loudly twice over.
//! * `solver.rs:1686` and the `full_x_to_var_x` accessors *produce*
//! indices for a caller rather than indexing with them.
//!
//! # What this does and does not buy
//!
//! On the typed path the swap is not merely discouraged, it is
//! **unrepresentable**: [`FullX`] has no public constructor, so the
//! only way to obtain one is to put a [`VarX`] through [`VarToFull`],
//! and `FullXSlice::at` accepts nothing else.
//!
//! It does **not** follow that the swap is gone. `ActivityReport`'s
//! `Vec` fields are public and must stay so, and nothing stops a
//! caller writing `report.var_status[row.get()]` — which compiles, and
//! is caught by leg 3 rather than by the compiler. A newtype closes
//! the path that goes through it; it cannot close one whose public API
//! is a bare `Vec`.
//!
//! The guard that closes the known site is still
//! `sens_invariance_legs.rs` leg 3, whose fixture puts a fixed variable
//! *ahead* of the kink so the two spaces actually diverge. This module
//! is what makes the *next* such site a compile error instead of a
//! fixture someone has to think to write.
//!
//! # Mutation evidence
//!
//! What the type actually closes, measured by reintroducing the swap
//! at each converted site rather than by argument:
//!
//! | mutation | outcome |
//! |---|---|
//! | mint a `FullX` from the var-x row at the kappa read | **compile error**, `E0624: associated function 'new' is private` |
//! | the same at `weakly_active_bounds` | **compile error**, same |
//! | bypass the type: `report.var_status.get(row.get())` | **compiles.** Caught at runtime instead, by `sens_invariance_legs.rs` leg 3 -- 3 legs go red (`leg_fixed_the_weak_set_...`, `leg_fixed_the_directional_derivative_...`, `the_legs_compose_at_the_fixed_and_scaled_corner`) |
//!
//! The third row is the honest limit and the reason leg 3 is not
//! retired: the public `Vec` fields are still reachable, so the type
//! covers the typed path and the leg fences the untyped one. Neither
//! alone covers this site.
//!
//! The first draft of this module failed its own first mutation --
//! `FullX::new` was `pub`, so `FullX::new(var_row.get())` typechecked
//! and the swap was one short line away. That is why the constructor
//! is private now, and why the table above exists at all: a newtype
//! whose guarantee is not mutation-checked is a comment with a
//! `struct` around it.
use Index;
/// A row in the algorithm's **free-variable** space: the primal block
/// the solver actually optimizes, with `make_parameter`-removed
/// variables absent.
///
/// This is the space of `BoundMultiplier::var_row`, `WeakBound::var_row`,
/// and every array whose length is the primal block width.
///
/// It coincides with [`FullX`] until the first removed variable and
/// diverges after it — which is why a corpus of fixtures with no fixed
/// variables cannot see a swap, and why leg 3 puts one ahead of the
/// kink on purpose.
///
/// ```
/// use pounce_sensitivity::index::VarX;
/// assert_eq!(VarX::new(3).get(), 3);
/// ```
;
/// An index into the **model's** variable space, fixed variables
/// included: the space of every `ActivityReport` per-variable array,
/// and the length [`crate::solver::Solver::n_full_x`] reports.
///
/// A `FullX` is obtainable only by converting a [`VarX`] through
/// [`VarToFull`] -- there is no public constructor, by design:
///
/// ```
/// use pounce_sensitivity::index::{VarToFull, VarX};
/// // full-x 0 is `make_parameter`-removed, so var-x k is full-x k+1
/// let map = VarToFull::build(3, |v| v.get() + 1);
/// assert_eq!(map.full_of(VarX::new(0)).unwrap().get(), 1);
/// ```
;
/// The var-x → full-x map, materialized once so a loop does not
/// re-borrow the NLP per row.
///
/// Built with [`VarToFull::build`] at the point of use, from the
/// NLP's own `var_x_to_full_x`. Lookups are bounds-checked and return
/// [`None`] rather than a neighbouring row's index — the whole point
/// of the exercise.
/// A per-full-x slice that can only be read with a [`FullX`].
///
/// Used for the `ActivityReport` arrays at the two sites where a var-x
/// row is live in the same scope. It borrows rather than owns, so it
/// costs nothing and does not duplicate the report.
/// The two spaces do not convert implicitly, in either direction.
///
/// These are the evidence that the newtype does the one job it exists
/// for. Each `compile_fail` is paired with the passing twin directly
/// below it, because a `compile_fail` that fails for the *wrong*
/// reason — a typo, an unresolved import — is a test that passes
/// vacuously, which is the failure mode this crate cares most about.
///
/// A var-x row cannot be used where a full-x index is expected:
///
/// ```compile_fail
/// use pounce_sensitivity::index::{FullX, VarX};
/// fn full_only(_: FullX) {}
/// full_only(VarX::new(0));
/// ```
///
/// and the same call typechecks with the right space, so the failure
/// above is about the types and not about the spelling:
///
/// ```
/// use pounce_sensitivity::index::{FullX, VarToFull, VarX};
/// fn full_only(_: FullX) {}
/// let map = VarToFull::build(1, |v| v.get());
/// full_only(map.full_of(VarX::new(0)).unwrap());
/// ```
///
/// And a `FullX` cannot be minted from a raw row at all, which is what
/// keeps the guarantee above from being one short line wide:
///
/// ```compile_fail
/// use pounce_sensitivity::index::{FullX, VarX};
/// let _ = FullX::new(VarX::new(0).get());
/// ```
///
/// A full-x index cannot be used where a var-x row is expected:
///
/// ```compile_fail
/// use pounce_sensitivity::index::{VarToFull, VarX};
/// fn var_only(_: VarX) {}
/// let map = VarToFull::build(1, |v| v.get());
/// var_only(map.full_of(VarX::new(0)).unwrap());
/// ```
///
/// ```
/// use pounce_sensitivity::index::{VarToFull, VarX};
/// fn var_only(_: VarX) {}
/// let map = VarToFull::build(1, |v| v.get());
/// var_only(VarX::new(0));
/// let _ = map.full_of(VarX::new(0)).unwrap();
/// ```
///
/// A full-x slice cannot be read with a var-x row — this is gh#672
/// finding 1's exact shape, `report.var_status.get(var_row)`, as a
/// type error:
///
/// ```compile_fail
/// use pounce_sensitivity::index::{FullXSlice, VarX};
/// let status = [0i8, 1, 2];
/// let full = FullXSlice::new(&status);
/// full.at(VarX::new(1));
/// ```
///
/// ```
/// use pounce_sensitivity::index::{FullXSlice, VarToFull, VarX};
/// let status = [0i8, 1, 2];
/// let full = FullXSlice::new(&status);
/// let map = VarToFull::build(2, |v| v.get() + 1);
/// assert_eq!(full.at(map.full_of(VarX::new(0)).unwrap()), Some(1));
/// assert_eq!(full.at(map.full_of(VarX::new(1)).unwrap()), Some(2));
/// ```
///
/// And a raw `usize` reaches neither, so an untyped index cannot drift
/// in from a caller:
///
/// ```compile_fail
/// use pounce_sensitivity::index::FullXSlice;
/// let status = [0i8, 1, 2];
/// FullXSlice::new(&status).at(1usize);
/// ```
;
/// A miss returns `None`, never a neighbour.
///
/// The defect this module exists for is not an out-of-bounds panic —
/// it is an in-range read of the wrong row. The bounds check below is
/// the cheap half; the type is the half that matters.