omena-abstract-value 0.4.0

Abstract class value domain and selector projection contracts
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
use crate::{ABSTRACT_VALUE_CASCADE_FAMILY_CLAIM_LEVEL_V0, AbstractPropertyValueV0};
use serde::Serialize;
use std::collections::{BTreeMap, BTreeSet};

/// Research-staged cascade-family substrate for M6 positioning.
///
/// This is intentionally framing-neutral: it records context-indexed value
/// families and restriction morphisms without claiming a categorical gluing
/// theorem or committing paper-stage terminology.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CascadeValueFamilyV0 {
    pub schema_version: &'static str,
    pub product: &'static str,
    pub framing: &'static str,
    pub claim_level: &'static str,
    pub property_name: String,
    pub supported_readings: Vec<&'static str>,
    pub context_value_count: usize,
    pub restriction_map_count: usize,
    pub property_consistent: bool,
    pub dangling_restriction_count: usize,
    pub theorem_claimed: bool,
    pub members: Vec<CascadeValueFamilyMemberV0>,
    pub restriction_maps: Vec<CascadeRestrictionMapV0>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CascadeContextV0 {
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<String>,
    pub selectors: Vec<String>,
    pub conditions: Vec<String>,
    pub layers: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CascadeValueFamilyMemberV0 {
    pub context: CascadeContextV0,
    pub value: AbstractPropertyValueV0,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CascadeRestrictionMapV0 {
    pub parent_context_id: String,
    pub child_context_id: String,
    pub morphism: CascadeMorphismV0,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CascadeMorphismV0 {
    pub kind: &'static str,
    pub direction: &'static str,
    pub preserves_property_name: bool,
    pub evidence: Vec<&'static str>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CascadeStalkEvaluationV0 {
    pub schema_version: &'static str,
    pub product: &'static str,
    pub claim_level: &'static str,
    pub property_name: String,
    pub requested_context_id: String,
    pub requested_context_exists: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resolved_context_id: Option<String>,
    pub restriction_path: Vec<String>,
    pub used_restriction_map_count: usize,
    pub bounded_by_context_count: usize,
    pub bounded_resolution_ready: bool,
    pub theorem_claimed: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<AbstractPropertyValueV0>,
    pub resolved: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blocked_reason: Option<&'static str>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CascadeRestrictionCycleSummaryV0 {
    pub schema_version: &'static str,
    pub product: &'static str,
    pub claim_level: &'static str,
    pub cycle_detection_model: &'static str,
    pub context_count: usize,
    pub restriction_map_count: usize,
    pub cycle_count: usize,
    pub cycles: Vec<CascadeRestrictionCycleV0>,
    pub theorem_claimed: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CascadeRestrictionCycleV0 {
    pub path: Vec<String>,
}

pub fn summarize_context_indexed_cascade_value_family_v0(
    property_name: impl Into<String>,
    members: Vec<CascadeValueFamilyMemberV0>,
    restriction_maps: Vec<CascadeRestrictionMapV0>,
) -> CascadeValueFamilyV0 {
    let property_name = property_name.into();
    let mut members = members;
    members.sort_by(|left, right| left.context.id.cmp(&right.context.id));
    members.dedup_by(|left, right| left.context.id == right.context.id);

    let mut restriction_maps = restriction_maps;
    restriction_maps.sort();
    restriction_maps.dedup();

    let context_ids = members
        .iter()
        .map(|member| member.context.id.as_str())
        .collect::<BTreeSet<_>>();
    let property_consistent = members
        .iter()
        .all(|member| property_value_name(&member.value) == property_name);
    let dangling_restriction_count = restriction_maps
        .iter()
        .filter(|restriction| {
            !context_ids.contains(restriction.parent_context_id.as_str())
                || !context_ids.contains(restriction.child_context_id.as_str())
        })
        .count();
    let restriction_map_count = restriction_maps.len();

    CascadeValueFamilyV0 {
        schema_version: "0",
        product: "omena-abstract-value.cascade-value-family",
        framing: "framingNeutralCascadeFamily",
        claim_level: ABSTRACT_VALUE_CASCADE_FAMILY_CLAIM_LEVEL_V0,
        property_name,
        supported_readings: vec!["restrictionMapCompatible", "aggregationCompatible"],
        context_value_count: members.len(),
        restriction_map_count,
        property_consistent,
        dangling_restriction_count,
        theorem_claimed: false,
        members,
        restriction_maps,
    }
}

#[deprecated(
    since = "0.4.0",
    note = "use summarize_context_indexed_cascade_value_family_v0; removal is not before 1.0 and requires downstream migration plus zero audited non-compatibility uses"
)]
pub fn summarize_cascade_value_family_v0(
    property_name: impl Into<String>,
    members: Vec<CascadeValueFamilyMemberV0>,
    restriction_maps: Vec<CascadeRestrictionMapV0>,
) -> CascadeValueFamilyV0 {
    let mut family =
        summarize_context_indexed_cascade_value_family_v0(property_name, members, restriction_maps);
    family.supported_readings = vec!["presheafCompatible", "cosheafCompatible"];
    family
}

pub fn derive_context_indexed_cascade_restriction_maps_v0(
    members: &[CascadeValueFamilyMemberV0],
) -> Vec<CascadeRestrictionMapV0> {
    let context_ids = members
        .iter()
        .map(|member| member.context.id.as_str())
        .collect::<BTreeSet<_>>();
    let mut maps = members
        .iter()
        .filter_map(|member| {
            let parent_id = member.context.parent_id.as_deref()?;
            context_ids
                .contains(parent_id)
                .then(|| CascadeRestrictionMapV0 {
                    parent_context_id: parent_id.to_string(),
                    child_context_id: member.context.id.clone(),
                    morphism: context_indexed_cascade_refinement_morphism_v0(),
                })
        })
        .collect::<Vec<_>>();
    maps.sort();
    maps.dedup();
    maps
}

#[deprecated(
    since = "0.4.0",
    note = "use derive_context_indexed_cascade_restriction_maps_v0; removal is not before 1.0 and requires downstream migration plus zero audited non-compatibility uses"
)]
pub fn derive_cascade_restriction_maps_v0(
    members: &[CascadeValueFamilyMemberV0],
) -> Vec<CascadeRestrictionMapV0> {
    derive_context_indexed_cascade_restriction_maps_v0(members)
        .into_iter()
        .map(|mut restriction| {
            restriction.morphism.evidence = vec![
                "contextIndexedValueFamily",
                "parentChildCascadeContext",
                "noSheafTheoremClaim",
            ];
            restriction
        })
        .collect()
}

pub fn context_indexed_cascade_refinement_morphism_v0() -> CascadeMorphismV0 {
    CascadeMorphismV0 {
        kind: "contextRefinement",
        direction: "parentToChildRestriction",
        preserves_property_name: true,
        evidence: vec![
            "contextIndexedValueFamily",
            "parentChildCascadeContext",
            "noCategoricalGluingTheoremClaim",
        ],
    }
}

#[deprecated(
    since = "0.4.0",
    note = "use context_indexed_cascade_refinement_morphism_v0; removal is not before 1.0 and requires downstream migration plus zero audited non-compatibility uses"
)]
pub fn cascade_context_refinement_morphism_v0() -> CascadeMorphismV0 {
    let mut morphism = context_indexed_cascade_refinement_morphism_v0();
    morphism.evidence = vec![
        "contextIndexedValueFamily",
        "parentChildCascadeContext",
        "noSheafTheoremClaim",
    ];
    morphism
}

pub fn cascade_value_for_context<'a>(
    family: &'a CascadeValueFamilyV0,
    context_id: &str,
) -> Option<&'a AbstractPropertyValueV0> {
    family
        .members
        .iter()
        .find(|member| member.context.id == context_id)
        .map(|member| &member.value)
}

pub fn cascade_family_context_values(
    family: &CascadeValueFamilyV0,
) -> BTreeMap<String, AbstractPropertyValueV0> {
    family
        .members
        .iter()
        .map(|member| (member.context.id.clone(), member.value.clone()))
        .collect()
}

pub fn evaluate_cascade_stalk_v0(
    family: &CascadeValueFamilyV0,
    context_id: &str,
) -> CascadeStalkEvaluationV0 {
    let values = cascade_family_context_values(family);
    let requested_context_exists = values.contains_key(context_id);
    let parent_by_child = family
        .restriction_maps
        .iter()
        .map(|restriction| {
            (
                restriction.child_context_id.clone(),
                restriction.parent_context_id.clone(),
            )
        })
        .collect::<BTreeMap<_, _>>();
    let mut restriction_path = Vec::new();
    let mut current = context_id.to_string();
    let bound = family.context_value_count.max(1);

    for _ in 0..=bound {
        restriction_path.push(current.clone());
        if let Some(value) = values.get(&current)
            && !property_value_is_bottom(value)
        {
            return CascadeStalkEvaluationV0 {
                schema_version: "0",
                product: "omena-abstract-value.cascade-stalk-evaluation",
                claim_level: "fixtureWitnessBoundedCascadeStalkEvaluation",
                property_name: family.property_name.clone(),
                requested_context_id: context_id.to_string(),
                requested_context_exists,
                resolved_context_id: Some(current),
                used_restriction_map_count: restriction_path.len().saturating_sub(1),
                bounded_by_context_count: bound,
                bounded_resolution_ready: true,
                theorem_claimed: false,
                value: Some(value.clone()),
                resolved: true,
                blocked_reason: None,
                restriction_path,
            };
        }
        let Some(parent) = parent_by_child.get(&current) else {
            break;
        };
        current = parent.clone();
    }

    CascadeStalkEvaluationV0 {
        schema_version: "0",
        product: "omena-abstract-value.cascade-stalk-evaluation",
        claim_level: "fixtureWitnessBoundedCascadeStalkEvaluation",
        property_name: family.property_name.clone(),
        requested_context_id: context_id.to_string(),
        requested_context_exists,
        resolved_context_id: None,
        restriction_path,
        used_restriction_map_count: 0,
        bounded_by_context_count: bound,
        bounded_resolution_ready: true,
        theorem_claimed: false,
        value: None,
        resolved: false,
        blocked_reason: Some("no non-bottom value found along bounded restriction path"),
    }
}

pub fn summarize_cascade_restriction_cycles_v0(
    family: &CascadeValueFamilyV0,
) -> CascadeRestrictionCycleSummaryV0 {
    let context_ids = family
        .members
        .iter()
        .map(|member| member.context.id.clone())
        .collect::<BTreeSet<_>>();
    let mut adjacency = BTreeMap::<String, Vec<String>>::new();
    for restriction in &family.restriction_maps {
        adjacency
            .entry(restriction.parent_context_id.clone())
            .or_default()
            .push(restriction.child_context_id.clone());
    }
    for targets in adjacency.values_mut() {
        targets.sort();
        targets.dedup();
    }

    let mut cycles = BTreeSet::<CascadeRestrictionCycleV0>::new();
    for start in &context_ids {
        collect_restriction_cycles_from(
            start,
            start,
            &adjacency,
            &mut Vec::new(),
            &mut cycles,
            family.context_value_count.max(1),
        );
    }
    let cycles = cycles.into_iter().collect::<Vec<_>>();
    CascadeRestrictionCycleSummaryV0 {
        schema_version: "0",
        product: "omena-abstract-value.cascade-restriction-cycle-summary",
        claim_level: "fixtureWitnessBoundedRestrictionCycleDetection",
        cycle_detection_model: "boundedRestrictionCycleWitnessNotCohomologyTheorem",
        context_count: family.context_value_count,
        restriction_map_count: family.restriction_map_count,
        cycle_count: cycles.len(),
        cycles,
        theorem_claimed: false,
    }
}

fn collect_restriction_cycles_from(
    start: &str,
    current: &str,
    adjacency: &BTreeMap<String, Vec<String>>,
    path: &mut Vec<String>,
    cycles: &mut BTreeSet<CascadeRestrictionCycleV0>,
    bound: usize,
) {
    if path.len() > bound {
        return;
    }
    path.push(current.to_string());
    if let Some(children) = adjacency.get(current) {
        for child in children {
            if child == start && path.len() > 1 {
                let mut cycle = path.clone();
                cycle.push(start.to_string());
                cycles.insert(CascadeRestrictionCycleV0 {
                    path: canonical_restriction_cycle(cycle),
                });
            } else if !path.contains(child) {
                collect_restriction_cycles_from(start, child, adjacency, path, cycles, bound);
            }
        }
    }
    path.pop();
}

fn canonical_restriction_cycle(mut cycle: Vec<String>) -> Vec<String> {
    if cycle.len() <= 2 {
        return cycle;
    }
    cycle.pop();
    let Some((start_index, _)) = cycle
        .iter()
        .enumerate()
        .min_by(|left, right| left.1.cmp(right.1))
    else {
        return cycle;
    };
    let mut canonical = (0..cycle.len())
        .map(|offset| cycle[(start_index + offset) % cycle.len()].clone())
        .collect::<Vec<_>>();
    canonical.push(canonical[0].clone());
    canonical
}

fn property_value_is_bottom(value: &AbstractPropertyValueV0) -> bool {
    matches!(value, AbstractPropertyValueV0::Bottom { .. })
}

fn property_value_name(value: &AbstractPropertyValueV0) -> &str {
    match value {
        AbstractPropertyValueV0::Bottom { property_name }
        | AbstractPropertyValueV0::Exact { property_name, .. }
        | AbstractPropertyValueV0::FiniteSet { property_name, .. }
        | AbstractPropertyValueV0::CustomPropertyReference { property_name, .. }
        | AbstractPropertyValueV0::Top { property_name } => property_name,
    }
}