pr4xis 0.6.0

Prove your domain is correct — ontology-driven rule enforcement with category theory, logical composition, and runtime state machines
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use super::category::Category;
use super::monad::Writer;
use super::monoid::Monoid;

// Traced Category — the writer monad applied to categories.
//
// Lifts any Category C into a traced version where compose()
// automatically accumulates provenance records through the
// Writer monad. The trace emerges from composition itself.
//
// TracedMorphism<M> = Writer<Vec<TraceRecord>, M>
//
// The monoid is Vec<TraceRecord> (concatenation).
// The monad is Writer: bind sequences computations and accumulates logs.
//
// Categorically: this is Joyal-Street-Verity (1996) trace operator.
// Algebraically: this is Moggi (1991) writer monad.
//
// References:
// - Joyal, Street & Verity, "Traced Monoidal Categories" (1996)
// - Moggi, "Notions of Computation and Monads" (1991)
// - W3C PROV-O (2013) — trace records are PROV Activities

/// A trace record produced by a single computation step.
/// Aligned with W3C PROV-O: this is a prov:Activity.
#[derive(Debug, Clone, PartialEq)]
pub struct TraceRecord {
    /// Which ontology/category produced this record.
    pub ontology: String,
    /// What operation was performed.
    pub operation: String,
    /// Detail of what happened.
    pub detail: String,
    /// Whether this step succeeded or had issues.
    pub status: TraceRecordStatus,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TraceRecordStatus {
    Ok,
    Warning,
    Error,
}

/// A traced morphism: the writer monad Writer<Vec<TraceRecord>, M>.
///
/// The morphism carries its provenance. Composition concatenates traces
/// via the Vec monoid — no manual instrumentation needed.
pub type TracedMorphism<M> = Writer<Vec<TraceRecord>, M>;

/// Convenience constructors for traced morphisms.
pub trait TracedMorphismExt<M: Clone + std::fmt::Debug> {
    /// Create a traced morphism with an initial trace record.
    fn traced(morphism: M, ontology: &str, operation: &str, detail: &str) -> TracedMorphism<M>;

    /// Create from a bare morphism with no trace.
    fn bare(morphism: M) -> TracedMorphism<M>;

    /// Add a trace record.
    fn record(&mut self, ontology: &str, operation: &str, detail: &str);

    /// Add a warning trace record.
    fn warn(&mut self, ontology: &str, operation: &str, detail: &str);
}

impl<M: Clone + std::fmt::Debug> TracedMorphismExt<M> for TracedMorphism<M> {
    fn traced(morphism: M, ontology: &str, operation: &str, detail: &str) -> Self {
        Writer::new(
            morphism,
            vec![TraceRecord {
                ontology: ontology.into(),
                operation: operation.into(),
                detail: detail.into(),
                status: TraceRecordStatus::Ok,
            }],
        )
    }

    fn bare(morphism: M) -> Self {
        Writer::pure(morphism)
    }

    fn record(&mut self, ontology: &str, operation: &str, detail: &str) {
        self.log.push(TraceRecord {
            ontology: ontology.into(),
            operation: operation.into(),
            detail: detail.into(),
            status: TraceRecordStatus::Ok,
        });
    }

    fn warn(&mut self, ontology: &str, operation: &str, detail: &str) {
        self.log.push(TraceRecord {
            ontology: ontology.into(),
            operation: operation.into(),
            detail: detail.into(),
            status: TraceRecordStatus::Warning,
        });
    }
}

/// A traced category: wraps any Category C with the writer monad.
///
/// Every morphism becomes Writer<Vec<TraceRecord>, M>.
/// Composition accumulates traces via the Vec monoid.
pub struct TracedCategory<C: Category>(std::marker::PhantomData<C>);

impl<C: Category> TracedCategory<C>
where
    C::Morphism: Clone,
    C::Object: Clone,
{
    /// Compose two traced morphisms.
    ///
    /// Writer bind: (m₁, w₁) >>= (m₂, w₂) → (m₁∘m₂, w₁ ++ w₂)
    pub fn compose(
        f: &TracedMorphism<C::Morphism>,
        g: &TracedMorphism<C::Morphism>,
    ) -> Option<TracedMorphism<C::Morphism>> {
        let composed = C::compose(&f.value, &g.value)?;
        Some(Writer::new(composed, f.log.combine(&g.log)))
    }

    /// Identity with empty trace.
    pub fn identity(obj: &C::Object) -> TracedMorphism<C::Morphism> {
        Writer::pure(C::identity(obj))
    }
}

// ---- Algebraic structure integrations ----

/// Convert a flat trace (Vec<TraceRecord>) into a Cofree tree.
///
/// The Cofree comonad gives each trace record its full context.
/// The root is the first record, children are subsequent records.
///
/// Reference: Uustalu & Vene, "Comonadic Notions of Computation" (2008)
pub fn trace_to_cofree(records: &[TraceRecord]) -> super::comonad::Cofree<TraceRecord> {
    if records.is_empty() {
        return super::comonad::Cofree::leaf(TraceRecord {
            ontology: "empty".into(),
            operation: "none".into(),
            detail: "no trace".into(),
            status: TraceRecordStatus::Ok,
        });
    }
    if records.len() == 1 {
        return super::comonad::Cofree::leaf(records[0].clone());
    }
    super::comonad::Cofree::node(
        records[0].clone(),
        records[1..]
            .iter()
            .map(|r| super::comonad::Cofree::leaf(r.clone()))
            .collect(),
    )
}

/// Fold a trace tree using a catamorphism to produce a summary.
///
/// Reference: Meijer, Fokkinga & Paterson (1991)
pub fn fold_trace<F: 'static>(
    tree: &super::comonad::Cofree<TraceRecord>,
    alg: &super::algebra::Algebra<TraceRecord, F>,
) -> F {
    super::algebra::cata(alg, tree)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::category::entity::Entity;
    use crate::category::relationship::Relationship;

    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    enum TestObj {
        A,
        B,
        C,
    }

    impl Entity for TestObj {
        fn variants() -> Vec<Self> {
            vec![Self::A, Self::B, Self::C]
        }
    }

    #[derive(Debug, Clone, PartialEq, Eq, Hash)]
    struct TestMorph {
        from: TestObj,
        to: TestObj,
    }

    impl Relationship for TestMorph {
        type Object = TestObj;
        fn source(&self) -> TestObj {
            self.from
        }
        fn target(&self) -> TestObj {
            self.to
        }
    }

    struct TestCat;
    impl Category for TestCat {
        type Object = TestObj;
        type Morphism = TestMorph;

        fn identity(obj: &TestObj) -> TestMorph {
            TestMorph {
                from: *obj,
                to: *obj,
            }
        }

        fn compose(f: &TestMorph, g: &TestMorph) -> Option<TestMorph> {
            if f.to == g.from {
                Some(TestMorph {
                    from: f.from,
                    to: g.to,
                })
            } else {
                None
            }
        }

        fn morphisms() -> Vec<TestMorph> {
            vec![
                TestMorph {
                    from: TestObj::A,
                    to: TestObj::B,
                },
                TestMorph {
                    from: TestObj::B,
                    to: TestObj::C,
                },
            ]
        }
    }

    #[test]
    fn traced_compose_accumulates_records() {
        let f = TracedMorphism::traced(
            TestMorph {
                from: TestObj::A,
                to: TestObj::B,
            },
            "TestOntology",
            "step1",
            "A → B",
        );
        let g = TracedMorphism::traced(
            TestMorph {
                from: TestObj::B,
                to: TestObj::C,
            },
            "TestOntology",
            "step2",
            "B → C",
        );

        let h = TracedCategory::<TestCat>::compose(&f, &g).unwrap();
        assert_eq!(h.value.from, TestObj::A);
        assert_eq!(h.value.to, TestObj::C);
        assert_eq!(h.log.len(), 2);
        assert_eq!(h.log[0].operation, "step1");
        assert_eq!(h.log[1].operation, "step2");
    }

    #[test]
    fn traced_identity_has_no_trace() {
        let id = TracedCategory::<TestCat>::identity(&TestObj::A);
        assert_eq!(id.value.from, TestObj::A);
        assert_eq!(id.value.to, TestObj::A);
        assert!(id.log.is_empty());
    }

    #[test]
    fn traced_compose_with_identity_preserves_trace() {
        let f = TracedMorphism::traced(
            TestMorph {
                from: TestObj::A,
                to: TestObj::B,
            },
            "Test",
            "lookup",
            "found",
        );
        let id = TracedCategory::<TestCat>::identity(&TestObj::B);

        let h = TracedCategory::<TestCat>::compose(&f, &id).unwrap();
        assert_eq!(h.log.len(), 1);
        assert_eq!(h.log[0].detail, "found");
    }

    #[test]
    fn trace_records_have_status() {
        let mut f = TracedMorphism::traced(
            TestMorph {
                from: TestObj::A,
                to: TestObj::B,
            },
            "Test",
            "parse",
            "success",
        );
        f.warn("Test", "parse", "ambiguous — multiple parses");

        assert_eq!(f.log.len(), 2);
        assert_eq!(f.log[0].status, TraceRecordStatus::Ok);
        assert_eq!(f.log[1].status, TraceRecordStatus::Warning);
    }

    #[test]
    fn compose_incompatible_returns_none() {
        let f = TracedMorphism::traced(
            TestMorph {
                from: TestObj::A,
                to: TestObj::B,
            },
            "Test",
            "step1",
            "A → B",
        );
        let g = TracedMorphism::traced(
            TestMorph {
                from: TestObj::A,
                to: TestObj::C,
            },
            "Test",
            "step2",
            "A → C",
        );
        assert!(TracedCategory::<TestCat>::compose(&f, &g).is_none());
    }

    // --- Writer monad law verification for TracedCategory ---

    #[test]
    fn writer_monad_left_identity() {
        // pure(m) >>= f = f(m)
        let m = TestMorph {
            from: TestObj::A,
            to: TestObj::B,
        };
        let traced = TracedMorphism::<TestMorph>::bare(m.clone());
        let result = traced.bind(|morph| TracedMorphism::traced(morph, "Test", "step", "applied"));
        assert_eq!(result.value, m);
        assert_eq!(result.log.len(), 1);
    }

    #[test]
    fn writer_monad_right_identity() {
        // m >>= pure = m
        let traced = TracedMorphism::traced(
            TestMorph {
                from: TestObj::A,
                to: TestObj::B,
            },
            "Test",
            "step",
            "done",
        );
        let original_log = traced.log.clone();
        let result = traced.bind(TracedMorphism::<TestMorph>::pure);
        assert_eq!(result.log, original_log);
    }

    // --- Algebraic integration tests ---

    #[test]
    fn trace_to_cofree_tree() {
        let records = vec![
            TraceRecord {
                ontology: "Language".into(),
                operation: "tokenize".into(),
                detail: "5 tokens".into(),
                status: TraceRecordStatus::Ok,
            },
            TraceRecord {
                ontology: "Grammar".into(),
                operation: "parse".into(),
                detail: "S[dcl]".into(),
                status: TraceRecordStatus::Ok,
            },
        ];

        let tree = trace_to_cofree(&records);
        assert_eq!(tree.extract().ontology, "Language");
        assert_eq!(tree.tail.len(), 1);
        assert_eq!(tree.tail[0].extract().ontology, "Grammar");
    }

    #[test]
    fn fold_trace_counts_steps() {
        let records = vec![
            TraceRecord {
                ontology: "A".into(),
                operation: "op".into(),
                detail: "d".into(),
                status: TraceRecordStatus::Ok,
            },
            TraceRecord {
                ontology: "B".into(),
                operation: "op".into(),
                detail: "d".into(),
                status: TraceRecordStatus::Ok,
            },
            TraceRecord {
                ontology: "C".into(),
                operation: "op".into(),
                detail: "d".into(),
                status: TraceRecordStatus::Warning,
            },
        ];

        let tree = trace_to_cofree(&records);
        let count_alg =
            crate::category::algebra::Algebra::new(|_record: &TraceRecord, children: &[usize]| {
                1 + children.iter().sum::<usize>()
            });
        let total = fold_trace(&tree, &count_alg);
        assert_eq!(total, 3);
    }

    #[test]
    fn fold_trace_collects_ontologies() {
        let records = vec![
            TraceRecord {
                ontology: "Language".into(),
                operation: "tok".into(),
                detail: "d".into(),
                status: TraceRecordStatus::Ok,
            },
            TraceRecord {
                ontology: "Grammar".into(),
                operation: "parse".into(),
                detail: "d".into(),
                status: TraceRecordStatus::Ok,
            },
        ];

        let tree = trace_to_cofree(&records);
        let collect_alg = crate::category::algebra::Algebra::new(
            |record: &TraceRecord, children: &[Vec<String>]| {
                let mut names = vec![record.ontology.clone()];
                for c in children {
                    names.extend(c.iter().cloned());
                }
                names
            },
        );
        let names = fold_trace(&tree, &collect_alg);
        assert_eq!(names, vec!["Language", "Grammar"]);
    }
}