weirflow 0.1.0

GPU-first dataflow analysis primitives for Vyre and Santh compiler pipelines.
Documentation
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
//! Soundness regime markers  -  re-exported from `vyre_foundation::soundness`.
//!
//! The canonical definition lives in vyre per the LEGO discipline (weir
//! always calls vyre, vyre never calls anything else). This module exists
//! to keep `weir::soundness::*` paths stable for existing consumers while
//! the implementation lives upstream.

pub use vyre_foundation::soundness::{
    validate_dynamic_pipeline, validate_dynamic_primitive, validate_pipeline, validate_primitive,
    DynamicPrimitiveSoundness, DynamicSoundnessViolation, PrecisionContract, PrimitiveSoundness,
    Soundness, SoundnessTagged, SoundnessViolation,
};

/// Dataflow result paired with the soundness regime under which it was derived.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DataflowEvidence<T> {
    /// Decoded dataflow payload.
    pub value: T,
    /// Soundness regime for `value`.
    pub soundness: Soundness,
}

impl<T> DataflowEvidence<T> {
    /// Attach a soundness regime to decoded dataflow evidence.
    #[inline]
    #[must_use]
    pub const fn new(value: T, soundness: Soundness) -> Self {
        Self { value, soundness }
    }

    /// Transform the payload while preserving the soundness regime.
    #[inline]
    pub fn map<U>(self, transform: impl FnOnce(T) -> U) -> DataflowEvidence<U> {
        DataflowEvidence {
            value: transform(self.value),
            soundness: self.soundness,
        }
    }

    /// Borrow the payload without dropping the soundness tag.
    #[inline]
    #[must_use]
    pub fn as_ref(&self) -> DataflowEvidence<&T> {
        DataflowEvidence {
            value: &self.value,
            soundness: self.soundness,
        }
    }
}

impl<T: PartialOrd> PartialOrd for DataflowEvidence<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        match soundness_rank(self.soundness).cmp(&soundness_rank(other.soundness)) {
            std::cmp::Ordering::Equal => self.value.partial_cmp(&other.value),
            ord => Some(ord),
        }
    }
}

impl<T: Ord> Ord for DataflowEvidence<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match soundness_rank(self.soundness).cmp(&soundness_rank(other.soundness)) {
            std::cmp::Ordering::Equal => self.value.cmp(&other.value),
            ord => ord,
        }
    }
}

#[inline]
const fn soundness_rank(s: Soundness) -> u8 {
    match s {
        Soundness::MustUnder => 0,
        Soundness::Exact => 1,
        Soundness::MayOver => 2,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ------------------------------------------------------------------
    // Functor laws for DataflowEvidence::map
    // ------------------------------------------------------------------

    #[test]
    fn map_identity_exact() {
        let ev = DataflowEvidence::new(42, Soundness::Exact);
        assert_eq!(ev.clone().map(|x| x), ev);
    }

    #[test]
    fn map_identity_may_over() {
        let ev = DataflowEvidence::new(String::from("hello"), Soundness::MayOver);
        assert_eq!(ev.clone().map(|x| x), ev);
    }

    #[test]
    fn map_identity_must_under() {
        let ev = DataflowEvidence::new(vec![1, 2, 3], Soundness::MustUnder);
        assert_eq!(ev.clone().map(|x| x), ev);
    }

    #[test]
    fn map_composition_exact() {
        let ev = DataflowEvidence::new(5, Soundness::Exact);
        let f = |x: i32| x * 2;
        let g = |x: i32| x + 3;
        let left = ev.clone().map(f).map(g);
        let right = ev.map(|x| g(f(x)));
        assert_eq!(left, right);
    }

    #[test]
    fn map_composition_may_over() {
        let ev = DataflowEvidence::new(10, Soundness::MayOver);
        let f = |x: i32| x - 1;
        let g = |x: i32| x * x;
        let left = ev.clone().map(f).map(g);
        let right = ev.map(|x| g(f(x)));
        assert_eq!(left, right);
    }

    #[test]
    fn map_composition_must_under() {
        let ev = DataflowEvidence::new(7u64, Soundness::MustUnder);
        let f = |x: u64| x.wrapping_add(1);
        let g = |x: u64| x >> 2;
        let left = ev.clone().map(f).map(g);
        let right = ev.map(|x| g(f(x)));
        assert_eq!(left, right);
    }

    #[test]
    fn map_preserves_soundness_exact() {
        let ev = DataflowEvidence::new(1, Soundness::Exact);
        let mapped = ev.map(|x| x.to_string());
        assert_eq!(mapped.soundness, Soundness::Exact);
    }

    #[test]
    fn map_preserves_soundness_may_over() {
        let ev = DataflowEvidence::new(1, Soundness::MayOver);
        let mapped = ev.map(|x| x.to_string());
        assert_eq!(mapped.soundness, Soundness::MayOver);
    }

    #[test]
    fn map_preserves_soundness_must_under() {
        let ev = DataflowEvidence::new(1, Soundness::MustUnder);
        let mapped = ev.map(|x| x.to_string());
        assert_eq!(mapped.soundness, Soundness::MustUnder);
    }

    #[test]
    fn map_changes_value_type() {
        let ev = DataflowEvidence::new(123, Soundness::Exact);
        let mapped: DataflowEvidence<String> = ev.map(|x| x.to_string());
        assert_eq!(mapped.value, "123");
    }

    #[test]
    fn map_on_reference_type() {
        let ev = DataflowEvidence::new(vec![1, 2, 3], Soundness::Exact);
        let mapped = ev.map(|mut v| {
            v.push(4);
            v
        });
        assert_eq!(mapped.value, vec![1, 2, 3, 4]);
    }

    #[test]
    fn map_chain_three_functions() {
        let ev = DataflowEvidence::new(2, Soundness::Exact);
        let result = ev.map(|x| x + 1).map(|x| x * 2).map(|x| x - 1);
        assert_eq!(result.value, 5); // (2+1)*2-1 = 5
        assert_eq!(result.soundness, Soundness::Exact);
    }

    // ------------------------------------------------------------------
    // DataflowEvidence::as_ref correctness
    // ------------------------------------------------------------------

    #[test]
    fn as_ref_exact_preserves_soundness() {
        let ev = DataflowEvidence::new(42, Soundness::Exact);
        let borrowed = ev.as_ref();
        assert_eq!(borrowed.soundness, Soundness::Exact);
    }

    #[test]
    fn as_ref_may_over_preserves_soundness() {
        let ev = DataflowEvidence::new(42, Soundness::MayOver);
        let borrowed = ev.as_ref();
        assert_eq!(borrowed.soundness, Soundness::MayOver);
    }

    #[test]
    fn as_ref_must_under_preserves_soundness() {
        let ev = DataflowEvidence::new(42, Soundness::MustUnder);
        let borrowed = ev.as_ref();
        assert_eq!(borrowed.soundness, Soundness::MustUnder);
    }

    #[test]
    fn as_ref_exact_value_matches() {
        let ev = DataflowEvidence::new(String::from("test"), Soundness::Exact);
        let borrowed = ev.as_ref();
        assert_eq!(*borrowed.value, "test");
    }

    #[test]
    fn as_ref_may_over_value_matches() {
        let ev = DataflowEvidence::new(vec![7, 8, 9], Soundness::MayOver);
        let borrowed = ev.as_ref();
        assert_eq!(borrowed.value, &vec![7, 8, 9]);
    }

    #[test]
    fn as_ref_must_under_value_matches() {
        let ev = DataflowEvidence::new(std::f64::consts::PI, Soundness::MustUnder);
        let borrowed = ev.as_ref();
        assert_eq!(*borrowed.value, std::f64::consts::PI);
    }

    #[test]
    fn as_ref_does_not_consume_original() {
        let ev = DataflowEvidence::new(100, Soundness::Exact);
        let _borrowed = ev.as_ref();
        assert_eq!(ev.value, 100); // ev still usable
    }

    #[test]
    fn as_ref_twice_same_result() {
        let ev = DataflowEvidence::new('a', Soundness::MayOver);
        let b1 = ev.as_ref();
        let b2 = ev.as_ref();
        assert_eq!(b1.value, b2.value);
        assert_eq!(b1.soundness, b2.soundness);
    }

    #[test]
    fn as_ref_then_map() {
        let ev = DataflowEvidence::new(5, Soundness::Exact);
        let borrowed = ev.as_ref();
        let mapped = borrowed.map(|&x| x * 3);
        assert_eq!(mapped.value, 15);
        assert_eq!(mapped.soundness, Soundness::Exact);
    }

    // ------------------------------------------------------------------
    // Equality and ordering
    // ------------------------------------------------------------------

    #[test]
    fn eq_same_value_same_soundness_exact() {
        let a = DataflowEvidence::new(42, Soundness::Exact);
        let b = DataflowEvidence::new(42, Soundness::Exact);
        assert_eq!(a, b);
    }

    #[test]
    fn eq_same_value_same_soundness_may_over() {
        let a = DataflowEvidence::new(42, Soundness::MayOver);
        let b = DataflowEvidence::new(42, Soundness::MayOver);
        assert_eq!(a, b);
    }

    #[test]
    fn eq_same_value_same_soundness_must_under() {
        let a = DataflowEvidence::new(42, Soundness::MustUnder);
        let b = DataflowEvidence::new(42, Soundness::MustUnder);
        assert_eq!(a, b);
    }

    #[test]
    fn ne_same_value_different_soundness() {
        let a = DataflowEvidence::new(42, Soundness::Exact);
        let b = DataflowEvidence::new(42, Soundness::MayOver);
        assert_ne!(a, b);
    }

    #[test]
    fn ne_different_value_same_soundness() {
        let a = DataflowEvidence::new(42, Soundness::Exact);
        let b = DataflowEvidence::new(43, Soundness::Exact);
        assert_ne!(a, b);
    }

    #[test]
    fn ne_different_value_different_soundness() {
        let a = DataflowEvidence::new(42, Soundness::Exact);
        let b = DataflowEvidence::new(43, Soundness::MayOver);
        assert_ne!(a, b);
    }

    #[test]
    fn eq_reflected() {
        let a = DataflowEvidence::new("x", Soundness::Exact);
        let b = DataflowEvidence::new("x", Soundness::Exact);
        assert!(a == b && b == a);
    }

    #[test]
    fn eq_transitive() {
        let a = DataflowEvidence::new(1, Soundness::MayOver);
        let b = DataflowEvidence::new(1, Soundness::MayOver);
        let c = DataflowEvidence::new(1, Soundness::MayOver);
        assert!(a == b && b == c && a == c);
    }

    #[test]
    fn ordering_must_under_less_than_exact() {
        let a = DataflowEvidence::new(100, Soundness::MustUnder);
        let b = DataflowEvidence::new(1, Soundness::Exact);
        assert!(a < b);
    }

    #[test]
    fn ordering_exact_less_than_may_over() {
        let a = DataflowEvidence::new(100, Soundness::Exact);
        let b = DataflowEvidence::new(1, Soundness::MayOver);
        assert!(a < b);
    }

    #[test]
    fn ordering_must_under_less_than_may_over() {
        let a = DataflowEvidence::new(1000, Soundness::MustUnder);
        let b = DataflowEvidence::new(0, Soundness::MayOver);
        assert!(a < b);
    }

    #[test]
    fn ordering_same_soundness_by_value() {
        let a = DataflowEvidence::new(10, Soundness::Exact);
        let b = DataflowEvidence::new(20, Soundness::Exact);
        assert!(a < b);
        assert!(b > a);
    }

    #[test]
    fn ordering_equal_elements() {
        let a = DataflowEvidence::new(5, Soundness::Exact);
        let b = DataflowEvidence::new(5, Soundness::Exact);
        assert_eq!(a.cmp(&b), std::cmp::Ordering::Equal);
    }

    #[test]
    fn ordering_total_order_all_variants() {
        let vals = vec![
            DataflowEvidence::new(1, Soundness::MustUnder),
            DataflowEvidence::new(2, Soundness::MustUnder),
            DataflowEvidence::new(1, Soundness::Exact),
            DataflowEvidence::new(2, Soundness::Exact),
            DataflowEvidence::new(1, Soundness::MayOver),
            DataflowEvidence::new(2, Soundness::MayOver),
        ];
        for i in 0..vals.len() {
            for j in 0..vals.len() {
                let ord = vals[i].cmp(&vals[j]);
                let reverse = vals[j].cmp(&vals[i]);
                assert_eq!(ord.reverse(), reverse);
            }
        }
    }

    #[test]
    fn ordering_sorts_correctly() {
        let mut vals = vec![
            DataflowEvidence::new(2, Soundness::MayOver),
            DataflowEvidence::new(1, Soundness::Exact),
            DataflowEvidence::new(3, Soundness::MustUnder),
        ];
        vals.sort();
        assert_eq!(vals[0].soundness, Soundness::MustUnder);
        assert_eq!(vals[0].value, 3);
        assert_eq!(vals[1].soundness, Soundness::Exact);
        assert_eq!(vals[1].value, 1);
        assert_eq!(vals[2].soundness, Soundness::MayOver);
        assert_eq!(vals[2].value, 2);
    }

    #[test]
    fn clone_produces_equal_value() {
        let a = DataflowEvidence::new(42i32, Soundness::Exact);
        let b = a.clone();
        assert_eq!(a, b);
    }

    #[test]
    fn debug_format_includes_value_and_soundness() {
        let ev = DataflowEvidence::new(99, Soundness::Exact);
        let s = format!("{:?}", ev);
        assert!(s.contains("99"));
        assert!(s.contains("Exact"));
    }

    #[test]
    fn new_constructor_sets_fields() {
        let ev = DataflowEvidence::new("payload", Soundness::MayOver);
        assert_eq!(ev.value, "payload");
        assert_eq!(ev.soundness, Soundness::MayOver);
    }
}