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
//! A confidence in the closed unit interval: [`Confidence`], the widths it measures in, and the
//! `f32` one this crate has always called [`Score`](crate::Score).
use ;
/// A confidence or ratio in the closed unit interval `[0.0, 1.0]`, measured in `S`.
///
/// The constructor is the only way in, and it refuses everything outside the interval and every
/// value the carrier admits that is not a number. That refusal is what buys the total order below:
/// with `NaN` excluded, `partial_cmp` never returns `None`, so `Ord` is a fact about the type rather
/// than a convention its users have to keep.
///
/// # The WIDTH is a parameter and the INVARIANT is not
///
/// Every carrier promises the same interval and the same order. What a carrier chooses is how many
/// bits the promise is kept in — `f32` for a detector's own output, `f64` where a fusion has already
/// spent the narrower width, [`half::f16`] where a quantised model emits one, and
/// [`rust_decimal::Decimal`] where the value is a base-ten quantity that no binary float IS. The
/// vocabulary is closed and the reason is [`ConfidenceCarrier`]'s: each member's refusal is a
/// MEASURED argument about that width, not a shape a bound could check.
///
/// [`Score`](crate::Score) is `Confidence<f32>` under the name this crate spent on it before the
/// width was a parameter, and it is an alias rather than a wrapper — the two are one type, so a call
/// site that spells either spells both.
///
/// # Examples
///
/// ```
/// use ingraph::{Confidence, Score};
///
/// assert_eq!(Score::new(0.82).unwrap().get(), 0.82);
/// assert!(Score::new(1.5).is_none());
///
/// // The same interval, one width up.
/// assert_eq!(Confidence::<f64>::ONE.get(), 1.0);
/// ```
///
/// A width outside the vocabulary is refused where it is named, and the note says what the
/// vocabulary is:
///
/// ```compile_fail,E0277
/// fn main() {
/// let _ = ingraph::Confidence::<i32>::ZERO;
/// }
/// ```
;
/// A confidence measured in `f32`, under the name this crate spent on it before the width was a
/// parameter.
///
/// An ALIAS and not a wrapper: `Score` and `Confidence<f32>` are one type, so every row this crate
/// wrote for the first is a row the second has, every call site that spells either spells both, and
/// the `Score` a schema publishes is the `Score` it published before.
///
/// It survives the generalisation rather than being carried by it. A confidence is what the word
/// means in every declaration this framework has seen — a sound-event detection, a face detection, a
/// speaker cluster, a zero-shot tag — and `f32` is the width all four of them measure in.
pub type Score = ;
// `PartialEq` is derived and `Eq` is not, because the derive would be the wrong statement in one of
// the two cases: a carrier's equality is already the right one here, and its REFLEXIVITY is a
// property of this type rather than of the carrier — `x == x` fails for `NaN` alone, and a
// `Confidence` is never one.
/// The widths a [`Confidence`] measures in.
///
/// # It is SEALED, and the seal is not caution
///
/// A member is admitted by a MEASUREMENT of that width: which of its values are not numbers, which
/// two of its values compare equal and carry different bits, and whether its own `Hash` already
/// answers for the second. Those are facts about a carrier that this crate checks and pins with a
/// test apiece; a bound cannot check them, so an implementation written outside this crate would be
/// an unchecked promise standing where a checked one is. Extend the set by asking the framework.
///
/// # What each member owes
///
/// [`ZERO`](Self::ZERO) and [`ONE`](Self::ONE) are the interval's ends AT THIS WIDTH, and they are
/// consts rather than a conversion because the endpoints are exactly representable in every carrier
/// here and a conversion would put a rounding question where there is none.
///
/// [`admit`](Self::admit) is the whole of the refusal, and everything below it is stated relative to
/// what it let through: [`admitted_cmp`](Self::admitted_cmp) may be total only because `admit`
/// refused the values that make an order partial, and [`hash_admitted`](Self::hash_admitted) may
/// hash bits only because `admit` left no two equal values with different ones.
/// The seal. Public inside a private module, so the bound is nameable from this crate's own rows and
/// unnameable — hence unimplementable — from outside it.
/// The two IEEE binary widths Rust ships, whose refusal is one argument written at two widths.
///
/// `NaN` and the infinities are refused, and `-0.0` is admitted and NORMALISED to `+0.0`: it
/// satisfies the range check, compares equal to `+0.0`, and carries different bits — so admitting it
/// unchanged would make two equal values hash differently.
///
/// `+ 0.0` is what does the normalising, and it maps `-0.0` to `+0.0` and is the identity everywhere
/// else in the interval.
ieee_carriers!;
/// IEEE binary16, and the ONE width here whose refusal is written against the carrier's own
/// comparisons rather than against Rust's literals.
///
/// The clauses are `f32`'s unchanged — `NaN` and the infinities out, `-0.0` normalised — and what
/// changes is how each is spelled. The range is checked against [`ZERO`](ConfidenceCarrier::ZERO)
/// and [`ONE`](ConfidenceCarrier::ONE) because `0.0` and `1.0` are `f64` literals and this width is
/// not one; the normalisation is an EQUALITY rather than an addition because `half`'s arithmetic
/// round-trips through `f32`, and a round trip is a second rounding standing where the whole point
/// is that nothing moves.
///
/// The equality IS the normalisation's argument, and it is the same one `f32` spends: `-0.0 == 0.0`
/// is true, so the branch catches exactly the value whose bits differ from the one it compares equal
/// to, and is the identity everywhere else.
/// The BASE-TEN width, and the one carrier that spends NEITHER of the float clauses.
///
/// **There is no `NaN` arm and no infinity arm**, and the absence is the type's rather than an
/// omission: [`rust_decimal::Decimal`] has no such values to refuse, which is why this row's `admit`
/// is the interval check alone and why its order is a delegation — `Decimal` is [`Ord`] already, so
/// the totality this trait buys the floats is a fact it arrived with.
///
/// **There is no `-0.0` normalisation either, and here the reason is a MEASUREMENT.** A decimal has
/// a negative zero AND a scale, so `-0` and `0` and `0.00` and `0` are two pairs that compare equal
/// while carrying different bits — a strictly worse position than the floats', where there is only
/// the one pair. `Decimal`'s own `Hash` closes both: it hashes `normalize()`, which strips trailing
/// zeros and converts `-0` to `0`. So the delegation below is the whole of the obligation, and the
/// two pairs are pinned by tests rather than left to the dependency's word.