omena-cascade 0.5.0

Cascade-formal substrate for Omena CSS
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Custom-property substitution and dependency-graph computation summaries.
//!
//! Resolution decomposes the canonical custom-property dependency graph into
//! strongly connected components, invalidates every cyclic member, and then
//! evaluates the acyclic remainder in dependency order. Historical
//! proof-oriented API names remain as compatibility aliases.

use omena_syntax::ident::CanonicalCustomPropertyNameV0;
#[cfg(test)]
use std::cell::Cell;
use std::collections::{BTreeMap, HashMap, VecDeque};

use crate::{
    CascadeValue, CustomPropertyBoundedFixedPointComputationWitnessV0, CustomPropertyEnv,
    CustomPropertyGuaranteedInvalidReasonV0, CustomPropertyLeastFixedPointEntryV0,
    CustomPropertyLeastFixedPointIterationV0, CustomPropertyLeastFixedPointProofV0,
    CustomPropertyLeastFixedPointSummaryV0,
};

pub fn substitute_custom_properties(value: &CascadeValue, env: &CustomPropertyEnv) -> CascadeValue {
    let resolved_env = resolve_custom_property_env_least_fixed_point(env);
    substitute_custom_properties_against_resolved_env(value, &resolved_env)
}

pub fn resolve_custom_property_env_least_fixed_point(env: &CustomPropertyEnv) -> CustomPropertyEnv {
    compute_custom_property_env_least_fixed_point(env, TraceMode::Omit).resolved_env
}

pub fn summarize_custom_property_least_fixed_point(
    env: &CustomPropertyEnv,
) -> CustomPropertyLeastFixedPointSummaryV0 {
    let computation = compute_custom_property_env_least_fixed_point(env, TraceMode::Record);
    let entries = env
        .iter()
        .zip(computation.resolved_env.iter())
        .map(|((name, input), (resolved_name, resolved))| {
            assert_eq!(
                name, resolved_name,
                "the resolved environment must preserve the canonical input key set"
            );
            CustomPropertyLeastFixedPointEntryV0 {
                name: name.clone(),
                input: input.clone(),
                changed: resolved != input,
                guaranteed_invalid: *resolved == CascadeValue::GuaranteedInvalid,
                guaranteed_invalid_reason: computation.invalid_reasons.get(name).copied(),
                resolved: resolved.clone(),
            }
        })
        .collect::<Vec<_>>();
    let resolved_count = entries
        .iter()
        .filter(|entry| cascade_value_is_resolved(&entry.resolved))
        .count();
    let guaranteed_invalid_count = entries
        .iter()
        .filter(|entry| entry.guaranteed_invalid)
        .count();

    CustomPropertyLeastFixedPointSummaryV0 {
        schema_version: "0",
        product: "omena-cascade.custom-property-least-fixed-point",
        input_count: env.len(),
        resolved_count,
        guaranteed_invalid_count,
        iteration_count: computation.iteration_count,
        iteration_bound: computation.iteration_bound,
        reached_fixed_point: computation.reached_fixed_point,
        monotone_witness_valid: custom_property_iteration_trace_is_monotone(
            &computation.iteration_trace,
        ),
        proof: custom_property_least_fixed_point_proof(),
        iteration_trace: computation.iteration_trace,
        entries,
        ready_surfaces: vec![
            "customPropertySubstitution",
            "customPropertyLeastFixedPoint",
            "customPropertyLeastFixedPointProof",
            "customPropertyLeastFixedPointTrace",
            "cycleToGuaranteedInvalid",
        ],
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct CustomPropertyLeastFixedPointComputation {
    resolved_env: CustomPropertyEnv,
    invalid_reasons:
        BTreeMap<CanonicalCustomPropertyNameV0, CustomPropertyGuaranteedInvalidReasonV0>,
    iteration_count: usize,
    iteration_bound: usize,
    reached_fixed_point: bool,
    iteration_trace: Vec<CustomPropertyLeastFixedPointIterationV0>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TraceMode {
    Omit,
    Record,
}

fn compute_custom_property_env_least_fixed_point(
    env: &CustomPropertyEnv,
    trace_mode: TraceMode,
) -> CustomPropertyLeastFixedPointComputation {
    if env
        .values()
        .all(|value| !cascade_value_contains_var_reference(value))
    {
        let iteration_bound = env.len().max(1);
        let iteration_trace = match trace_mode {
            TraceMode::Omit => Vec::new(),
            TraceMode::Record if env.is_empty() => vec![CustomPropertyLeastFixedPointIterationV0 {
                iteration: 1,
                changed_count: 0,
                settled_count: 0,
                guaranteed_invalid_count: 0,
            }],
            TraceMode::Record => {
                let mut guaranteed_invalid_count = 0;
                env.iter()
                    .enumerate()
                    .map(|(index, (_, value))| {
                        guaranteed_invalid_count +=
                            usize::from(*value == CascadeValue::GuaranteedInvalid);
                        CustomPropertyLeastFixedPointIterationV0 {
                            iteration: index + 1,
                            changed_count: 0,
                            settled_count: index + 1,
                            guaranteed_invalid_count,
                        }
                    })
                    .collect()
            }
        };
        let invalid_reasons = env
            .iter()
            .filter(|(_, value)| **value == CascadeValue::GuaranteedInvalid)
            .map(|(name, _)| {
                (
                    name.clone(),
                    CustomPropertyGuaranteedInvalidReasonV0::InvalidDependencyWithoutFallback,
                )
            })
            .collect();
        return CustomPropertyLeastFixedPointComputation {
            resolved_env: env.clone(),
            invalid_reasons,
            iteration_count: iteration_bound,
            iteration_bound,
            reached_fixed_point: true,
            iteration_trace,
        };
    }
    let dependency_graph = custom_property_dependency_graph(env);
    let components = strongly_connected_components(&dependency_graph);
    let component_schedule = dependency_ordered_components(&dependency_graph, &components);
    let mut resolved_values = vec![None; dependency_graph.names.len()];
    let mut invalid_reasons_by_node = vec![None; dependency_graph.names.len()];
    let mut iteration_trace = match trace_mode {
        TraceMode::Omit => Vec::new(),
        TraceMode::Record => Vec::with_capacity(components.len().max(1)),
    };
    let mut changed_count = 0;
    let mut settled_count = 0;
    let mut guaranteed_invalid_count = 0;

    for component_index in component_schedule {
        let component = &components[component_index];
        if component_is_cyclic(component, &dependency_graph) {
            for node in component {
                let input = dependency_graph.values[*node];
                let resolved = CascadeValue::GuaranteedInvalid;
                record_custom_property_settlement(
                    trace_mode,
                    input,
                    &resolved,
                    &mut changed_count,
                    &mut settled_count,
                    &mut guaranteed_invalid_count,
                );
                resolved_values[*node] = Some(resolved);
                invalid_reasons_by_node[*node] =
                    Some(CustomPropertyGuaranteedInvalidReasonV0::CycleMember);
            }
        } else {
            for node in component {
                let value = dependency_graph.values[*node];
                let outcome = substitute_custom_properties_with_reason(
                    value,
                    &dependency_graph,
                    &resolved_values,
                    &invalid_reasons_by_node,
                );
                if let Some(reason) = outcome.invalid_reason {
                    invalid_reasons_by_node[*node] = Some(reason);
                }
                record_custom_property_settlement(
                    trace_mode,
                    value,
                    &outcome.value,
                    &mut changed_count,
                    &mut settled_count,
                    &mut guaranteed_invalid_count,
                );
                resolved_values[*node] = Some(outcome.value);
            }
        }

        if trace_mode == TraceMode::Record {
            iteration_trace.push(CustomPropertyLeastFixedPointIterationV0 {
                iteration: iteration_trace.len() + 1,
                changed_count,
                settled_count,
                guaranteed_invalid_count,
            });
        }
    }

    if trace_mode == TraceMode::Record && iteration_trace.is_empty() {
        iteration_trace.push(CustomPropertyLeastFixedPointIterationV0 {
            iteration: 1,
            changed_count: 0,
            settled_count: 0,
            guaranteed_invalid_count: 0,
        });
    }

    assert!(
        resolved_values.iter().all(Option::is_some),
        "the SCC schedule must evaluate every custom-property binding exactly once"
    );
    let mut resolved_env = env.clone();
    for (index, ((name, resolved), scheduled)) in
        resolved_env.iter_mut().zip(resolved_values).enumerate()
    {
        assert_eq!(
            name, dependency_graph.names[index],
            "the indexed result must preserve canonical input-key order"
        );
        if let Some(scheduled) = scheduled {
            *resolved = scheduled;
        }
    }
    let invalid_reasons = dependency_graph
        .names
        .iter()
        .zip(invalid_reasons_by_node)
        .filter_map(|(name, reason)| reason.map(|reason| ((*name).clone(), reason)))
        .collect::<BTreeMap<_, _>>();

    assert_eq!(
        resolved_env.len(),
        env.len(),
        "the SCC schedule must evaluate every custom-property binding exactly once"
    );
    assert!(
        resolved_env
            .values()
            .all(|value| !cascade_value_contains_var_reference(value)),
        "the acyclic component schedule must eliminate every var() reference"
    );

    CustomPropertyLeastFixedPointComputation {
        resolved_env,
        invalid_reasons,
        iteration_count: components.len().max(1),
        iteration_bound: components.len().max(1),
        reached_fixed_point: true,
        iteration_trace,
    }
}

struct CustomPropertyDependencyGraph<'a> {
    names: Vec<&'a CanonicalCustomPropertyNameV0>,
    values: Vec<&'a CascadeValue>,
    index_by_name: HashMap<&'a str, usize>,
    edges: Vec<Vec<usize>>,
}

#[cfg(test)]
thread_local! {
    static DEPENDENCY_GRAPH_BUILD_COUNT: Cell<usize> = const { Cell::new(0) };
}

#[cfg(test)]
pub(crate) fn reset_custom_property_dependency_graph_build_count() {
    DEPENDENCY_GRAPH_BUILD_COUNT.set(0);
}

#[cfg(test)]
pub(crate) fn custom_property_dependency_graph_build_count() -> usize {
    DEPENDENCY_GRAPH_BUILD_COUNT.get()
}

fn custom_property_dependency_graph(env: &CustomPropertyEnv) -> CustomPropertyDependencyGraph<'_> {
    #[cfg(test)]
    DEPENDENCY_GRAPH_BUILD_COUNT.set(DEPENDENCY_GRAPH_BUILD_COUNT.get() + 1);
    let entries = env.iter().collect::<Vec<_>>();
    let names = entries.iter().map(|(name, _)| *name).collect::<Vec<_>>();
    let values = entries.iter().map(|(_, value)| *value).collect::<Vec<_>>();
    let index_by_name = names
        .iter()
        .enumerate()
        .map(|(index, name)| (name.as_str(), index))
        .collect::<HashMap<_, _>>();
    let edges = values
        .iter()
        .map(|value| {
            let mut references = Vec::new();
            collect_custom_property_reference_indices(value, &index_by_name, &mut references);
            references.sort_unstable();
            references.dedup();
            references
        })
        .collect();
    CustomPropertyDependencyGraph {
        names,
        values,
        index_by_name,
        edges,
    }
}

#[inline]
fn record_custom_property_settlement(
    trace_mode: TraceMode,
    input: &CascadeValue,
    resolved: &CascadeValue,
    changed_count: &mut usize,
    settled_count: &mut usize,
    guaranteed_invalid_count: &mut usize,
) {
    if trace_mode == TraceMode::Omit {
        return;
    }
    *changed_count += usize::from(input != resolved);
    *settled_count += usize::from(!cascade_value_contains_var_reference(resolved));
    *guaranteed_invalid_count += usize::from(*resolved == CascadeValue::GuaranteedInvalid);
}

fn collect_custom_property_reference_indices(
    value: &CascadeValue,
    index_by_name: &HashMap<&str, usize>,
    references: &mut Vec<usize>,
) {
    match value {
        CascadeValue::Var { name, fallback } => {
            if let Some(index) = index_by_name.get(name.as_str()) {
                references.push(*index);
            }
            if let Some(fallback) = fallback {
                collect_custom_property_reference_indices(fallback, index_by_name, references);
            }
        }
        CascadeValue::Composite(parts) => {
            for part in parts {
                collect_custom_property_reference_indices(part, index_by_name, references);
            }
        }
        CascadeValue::Literal(_)
        | CascadeValue::Initial
        | CascadeValue::Inherit
        | CascadeValue::Indeterminate
        | CascadeValue::GuaranteedInvalid
        | CascadeValue::Unset => {}
    }
}

fn strongly_connected_components(graph: &CustomPropertyDependencyGraph<'_>) -> Vec<Vec<usize>> {
    let mut finish_order = Vec::with_capacity(graph.names.len());
    let mut visited = vec![false; graph.names.len()];
    for start in 0..graph.names.len() {
        if visited[start] {
            continue;
        }
        visited[start] = true;
        let mut stack = vec![(start, 0usize)];
        while let Some((node, neighbor_index)) = stack.last_mut() {
            if let Some(neighbor) = graph.edges[*node].get(*neighbor_index).copied() {
                *neighbor_index += 1;
                if !visited[neighbor] {
                    visited[neighbor] = true;
                    stack.push((neighbor, 0));
                }
            } else {
                let node = *node;
                stack.pop();
                finish_order.push(node);
            }
        }
    }

    let mut reverse_graph = vec![Vec::new(); graph.names.len()];
    for (source, targets) in graph.edges.iter().enumerate() {
        for target in targets {
            reverse_graph[*target].push(source);
        }
    }
    for neighbors in &mut reverse_graph {
        neighbors.sort_unstable();
    }

    let mut components = Vec::new();
    visited.fill(false);
    while let Some(node) = finish_order.pop() {
        if visited[node] {
            continue;
        }
        let mut component = Vec::new();
        visited[node] = true;
        let mut stack = vec![node];
        while let Some(current) = stack.pop() {
            component.push(current);
            for neighbor in reverse_graph[current].iter().rev() {
                if !visited[*neighbor] {
                    visited[*neighbor] = true;
                    stack.push(*neighbor);
                }
            }
        }
        component.sort_unstable_by_key(|index| graph.names[*index].as_str());
        components.push(component);
    }
    components
}

fn dependency_ordered_components(
    graph: &CustomPropertyDependencyGraph<'_>,
    components: &[Vec<usize>],
) -> Vec<usize> {
    let mut component_by_node = vec![0; graph.names.len()];
    for (component_index, component) in components.iter().enumerate() {
        for node in component {
            component_by_node[*node] = component_index;
        }
    }
    let mut component_dependencies = vec![Vec::new(); components.len()];
    for (source, targets) in graph.edges.iter().enumerate() {
        let source_component = component_by_node[source];
        for target in targets {
            let target_component = component_by_node[*target];
            if source_component != target_component {
                component_dependencies[source_component].push(target_component);
            }
        }
    }
    for dependencies in &mut component_dependencies {
        dependencies.sort_unstable();
        dependencies.dedup();
    }
    let mut dependents = vec![Vec::new(); components.len()];
    for (component, dependencies) in component_dependencies.iter().enumerate() {
        for dependency in dependencies {
            dependents[*dependency].push(component);
        }
    }
    for entries in &mut dependents {
        entries.sort_unstable();
    }
    let mut remaining_dependencies = component_dependencies
        .iter()
        .map(Vec::len)
        .collect::<Vec<_>>();
    let mut ready = (0..components.len())
        .filter(|index| remaining_dependencies[*index] == 0)
        .collect::<VecDeque<_>>();
    let mut schedule = Vec::with_capacity(components.len());
    while let Some(component) = ready.pop_front() {
        schedule.push(component);
        for dependent in &dependents[component] {
            remaining_dependencies[*dependent] -= 1;
            if remaining_dependencies[*dependent] == 0 {
                ready.push_back(*dependent);
            }
        }
    }
    assert_eq!(
        schedule.len(),
        components.len(),
        "the SCC condensation graph is acyclic"
    );
    schedule
}

fn component_is_cyclic(component: &[usize], graph: &CustomPropertyDependencyGraph<'_>) -> bool {
    component.len() > 1
        || component
            .first()
            .is_some_and(|node| graph.edges[*node].contains(node))
}

fn custom_property_iteration_trace_is_monotone(
    trace: &[CustomPropertyLeastFixedPointIterationV0],
) -> bool {
    trace
        .windows(2)
        .all(|pair| pair[0].settled_count <= pair[1].settled_count)
}

fn cascade_value_contains_var_reference(value: &CascadeValue) -> bool {
    match value {
        CascadeValue::Var { .. } => true,
        CascadeValue::Composite(values) => values.iter().any(cascade_value_contains_var_reference),
        CascadeValue::Literal(_)
        | CascadeValue::Initial
        | CascadeValue::Inherit
        | CascadeValue::Indeterminate
        | CascadeValue::GuaranteedInvalid
        | CascadeValue::Unset => false,
    }
}

/// Describes the finite graph computation used by custom-property substitution.
pub fn custom_property_bounded_fixed_point_computation_witness()
-> CustomPropertyBoundedFixedPointComputationWitnessV0 {
    CustomPropertyLeastFixedPointProofV0 {
        finite_domain: "canonical custom-property environment keys form a fixed finite dependency graph",
        transfer_function: "strongly connected components are scheduled dependency-first; cyclic components become guaranteed-invalid and acyclic components substitute against memoized dependencies",
        bounded_fixed_point_computation_witness: "every strongly connected component is processed exactly once; no non-converged approximation is returned",
        monotone_witness: "the compatibility trace records a nondecreasing count of bindings settled by the component schedule",
        monotonic_progress_witness: "each scheduled component only adds finalized bindings to the resolved environment",
        iteration_bound_formula: "max(1, strongly_connected_component_count)",
        cycle_policy: "fallback references are dependency edges and every member of a cyclic strongly connected component becomes guaranteed-invalid before outer fallbacks are evaluated",
        proof_obligations: vec![
            "canonical-key dependency graph",
            "fallback-inclusive dependency edges",
            "complete strongly connected component partition",
            "whole-cycle guaranteed-invalid assignment",
            "dependency-ordered acyclic substitution",
            "no non-converged approximation return",
        ],
    }
}

/// Compatibility wrapper for the earlier proof-oriented name.
fn custom_property_least_fixed_point_proof() -> CustomPropertyLeastFixedPointProofV0 {
    custom_property_bounded_fixed_point_computation_witness()
}

fn substitute_custom_properties_against_resolved_env(
    value: &CascadeValue,
    resolved_env: &CustomPropertyEnv,
) -> CascadeValue {
    match value {
        CascadeValue::Literal(_)
        | CascadeValue::Initial
        | CascadeValue::Inherit
        | CascadeValue::Indeterminate
        | CascadeValue::GuaranteedInvalid
        | CascadeValue::Unset => value.clone(),
        CascadeValue::Composite(parts) => {
            let resolved_parts = parts
                .iter()
                .map(|part| substitute_custom_properties_against_resolved_env(part, resolved_env))
                .collect::<Vec<_>>();
            if resolved_parts.contains(&CascadeValue::GuaranteedInvalid) {
                return CascadeValue::GuaranteedInvalid;
            }
            CascadeValue::Composite(resolved_parts)
        }
        CascadeValue::Var { name, fallback } => match resolved_env.get(name) {
            Some(CascadeValue::Unset | CascadeValue::GuaranteedInvalid) | None => fallback
                .as_deref()
                .map(|fallback| {
                    substitute_custom_properties_against_resolved_env(fallback, resolved_env)
                })
                .unwrap_or(CascadeValue::GuaranteedInvalid),
            Some(value) => value.clone(),
        },
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct CustomPropertySubstitutionOutcome {
    value: CascadeValue,
    invalid_reason: Option<CustomPropertyGuaranteedInvalidReasonV0>,
}

fn substitute_custom_properties_with_reason(
    value: &CascadeValue,
    graph: &CustomPropertyDependencyGraph<'_>,
    resolved_values: &[Option<CascadeValue>],
    invalid_reasons: &[Option<CustomPropertyGuaranteedInvalidReasonV0>],
) -> CustomPropertySubstitutionOutcome {
    match value {
        CascadeValue::Literal(_)
        | CascadeValue::Initial
        | CascadeValue::Inherit
        | CascadeValue::Indeterminate
        | CascadeValue::Unset => CustomPropertySubstitutionOutcome {
            value: value.clone(),
            invalid_reason: None,
        },
        CascadeValue::GuaranteedInvalid => CustomPropertySubstitutionOutcome {
            value: CascadeValue::GuaranteedInvalid,
            invalid_reason: Some(
                CustomPropertyGuaranteedInvalidReasonV0::InvalidDependencyWithoutFallback,
            ),
        },
        CascadeValue::Composite(parts) => {
            let mut resolved_parts = Vec::with_capacity(parts.len());
            let mut invalid_reason = None;
            for part in parts {
                let outcome = substitute_custom_properties_with_reason(
                    part,
                    graph,
                    resolved_values,
                    invalid_reasons,
                );
                invalid_reason = invalid_reason.or(outcome.invalid_reason);
                resolved_parts.push(outcome.value);
            }
            if let Some(invalid_reason) = invalid_reason {
                CustomPropertySubstitutionOutcome {
                    value: CascadeValue::GuaranteedInvalid,
                    invalid_reason: Some(invalid_reason),
                }
            } else {
                CustomPropertySubstitutionOutcome {
                    value: CascadeValue::Composite(resolved_parts),
                    invalid_reason: None,
                }
            }
        }
        CascadeValue::Var { name, fallback } => {
            let Some(index) = graph.index_by_name.get(name.as_str()).copied() else {
                return fallback
                    .as_deref()
                    .map(|fallback| {
                        substitute_custom_properties_with_reason(
                            fallback,
                            graph,
                            resolved_values,
                            invalid_reasons,
                        )
                    })
                    .unwrap_or(CustomPropertySubstitutionOutcome {
                        value: CascadeValue::GuaranteedInvalid,
                        invalid_reason: Some(
                            CustomPropertyGuaranteedInvalidReasonV0::MissingReference,
                        ),
                    });
            };
            assert!(
                resolved_values[index].is_some(),
                "the component schedule must settle dependencies before their consumers"
            );
            let Some(resolved) = resolved_values[index].as_ref() else {
                return CustomPropertySubstitutionOutcome {
                    value: CascadeValue::Indeterminate,
                    invalid_reason: None,
                };
            };
            match resolved {
                CascadeValue::Unset | CascadeValue::GuaranteedInvalid => fallback
                    .as_deref()
                    .map(|fallback| {
                        substitute_custom_properties_with_reason(
                            fallback,
                            graph,
                            resolved_values,
                            invalid_reasons,
                        )
                    })
                    .unwrap_or(CustomPropertySubstitutionOutcome {
                        value: CascadeValue::GuaranteedInvalid,
                        invalid_reason: Some(
                            CustomPropertyGuaranteedInvalidReasonV0::InvalidDependencyWithoutFallback,
                        ),
                    }),
                resolved => CustomPropertySubstitutionOutcome {
                    value: resolved.clone(),
                    invalid_reason: invalid_reasons[index],
                },
            }
        }
    }
}

fn cascade_value_is_resolved(value: &CascadeValue) -> bool {
    match value {
        CascadeValue::Literal(_) => true,
        CascadeValue::Composite(parts) => parts.iter().all(cascade_value_is_resolved),
        CascadeValue::Var { .. }
        | CascadeValue::Initial
        | CascadeValue::Inherit
        | CascadeValue::Indeterminate
        | CascadeValue::GuaranteedInvalid
        | CascadeValue::Unset => false,
    }
}