shifty-algebra 0.2.4

Core SHACL formalism IR: path algebra, shape grammar, selectors, schema, and reference semantics
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
//! Value types `T` — the `test(τ)` facets (doc 00 §1).
//!
//! A `ValueType` is a decidable predicate on literal values, `⟦τ⟧ ⊆ V`. The
//! paper folds datatype, numeric ranges, string length, and regex into this one
//! abstraction (Appendix B); we add language membership (gap-analysis **L1**).

use crate::term::{Literal, NamedNode};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;

/// A numeric/ordered bound for `sh:min/maxInclusive` / `sh:min/maxExclusive`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Bound {
    pub value: Literal,
    pub inclusive: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ValueType {
    /// `any` — `⟦any⟧ = V`.
    Any,
    /// `sh:datatype`.
    Datatype(NamedNode),
    /// `sh:minInclusive` / `sh:minExclusive` / `sh:maxInclusive` / `sh:maxExclusive`.
    NumericRange {
        lo: Option<Bound>,
        hi: Option<Bound>,
    },
    /// `sh:minLength` / `sh:maxLength`.
    Length { min: Option<u64>, max: Option<u64> },
    /// `sh:pattern` (+ `sh:flags`).
    Pattern { regex: String, flags: String },
    /// `sh:languageIn` (gap-analysis **L1**).
    LangIn(Vec<String>),
    /// Intersection of facets (e.g. a datatype stacked with a length bound).
    And(Vec<ValueType>),
}

impl ValueType {
    /// Intersect facets, flattening nested `And` and dropping `Any` (the unit of
    /// intersection). An empty intersection is `Any`.
    pub fn and(parts: Vec<ValueType>) -> ValueType {
        let mut flat = Vec::with_capacity(parts.len());
        for p in parts {
            match p {
                ValueType::Any => {}
                ValueType::And(inner) => flat.extend(inner),
                other => flat.push(other),
            }
        }
        match flat.len() {
            0 => ValueType::Any,
            1 => flat.pop().unwrap(),
            _ => ValueType::And(flat),
        }
    }

    /// Tighten an intersection of facets and detect same-family unsat
    /// (`docs/04-normalization.md` §4).
    ///
    /// Returns `None` when the facet set is unsatisfiable (⊥), `Some(Any)` when
    /// it imposes no constraint (⊤), else the tightened facet. Each rewrite is
    /// per-value truth-functional, hence sound under the gfp semantics.
    ///
    /// Scope: merges same-family bounds (numeric ranges, lengths), folds an
    /// empty range / `len.min > max` / two distinct datatypes to ⊥. Numeric
    /// ordering covers xsd numeric only; incomparable bounds (mixed/non-numeric
    /// datatypes) are *kept*, never merged or declared unsat. Cross-facet unsat
    /// (`datatype(xsd:string) ∧ NumericRange`) is deferred (§4 [todo]).
    pub fn normalize(&self) -> Option<ValueType> {
        let flat = match ValueType::and(vec![self.clone()]) {
            ValueType::And(parts) => parts,
            ValueType::Any => return Some(ValueType::Any),
            single => vec![single],
        };

        let mut lo: Option<Bound> = None;
        let mut hi: Option<Bound> = None;
        let mut len_min: Option<u64> = None;
        let mut len_max: Option<u64> = None;
        let mut has_range = false;
        let mut has_length = false;
        let mut datatype: Option<NamedNode> = None;
        // Facets we don't merge here (Pattern, LangIn) plus range bounds that
        // were incomparable with the running bound and so couldn't be folded.
        let mut rest: Vec<ValueType> = Vec::new();

        for f in flat {
            match f {
                ValueType::Any => {}
                ValueType::NumericRange { lo: ilo, hi: ihi } => {
                    has_range = true;
                    if let Some(b) = ilo {
                        lo = fold_bound(lo, b, Side::Lo, &mut rest);
                    }
                    if let Some(b) = ihi {
                        hi = fold_bound(hi, b, Side::Hi, &mut rest);
                    }
                }
                ValueType::Length { min, max } => {
                    has_length = true;
                    len_min = tighter_lower(len_min, min);
                    len_max = tighter_upper(len_max, max);
                }
                ValueType::Datatype(d) => match &datatype {
                    Some(prev) if *prev != d => return None, // two distinct datatypes ⇒ ⊥
                    _ => datatype = Some(d),
                },
                other => rest.push(other),
            }
        }

        // Same-family unsat.
        if range_is_empty(&lo, &hi) {
            return None;
        }
        if let (Some(mn), Some(mx)) = (len_min, len_max)
            && mn > mx
        {
            return None;
        }
        // Cross-facet unsat: a known non-numeric datatype with numeric range
        // bounds, or a language-tagged literal (LangIn) with numeric range bounds.
        if has_range {
            if datatype
                .as_ref()
                .is_some_and(|d| is_definitely_non_numeric_xsd(d.as_str()))
            {
                return None;
            }
            if rest.iter().any(|f| matches!(f, ValueType::LangIn(_))) {
                return None;
            }
        }

        // Reassemble in a canonical order; `and` re-flattens and drops `Any`.
        let mut parts = Vec::new();
        if let Some(d) = datatype {
            parts.push(ValueType::Datatype(d));
        }
        if has_range && !(lo.is_none() && hi.is_none()) {
            parts.push(ValueType::NumericRange { lo, hi });
        }
        if has_length && !(len_min.is_none() && len_max.is_none()) {
            parts.push(ValueType::Length {
                min: len_min,
                max: len_max,
            });
        }
        parts.extend(rest);
        Some(ValueType::and(parts))
    }
}

/// Which end of a `NumericRange` a bound constrains.
#[derive(Clone, Copy)]
enum Side {
    Lo,
    Hi,
}

/// The tighter lower length bound (larger min); `None` is no bound.
fn tighter_lower(a: Option<u64>, b: Option<u64>) -> Option<u64> {
    match (a, b) {
        (Some(x), Some(y)) => Some(x.max(y)),
        (s, None) | (None, s) => s,
    }
}

/// The tighter upper length bound (smaller max); `None` is no bound.
fn tighter_upper(a: Option<u64>, b: Option<u64>) -> Option<u64> {
    match (a, b) {
        (Some(x), Some(y)) => Some(x.min(y)),
        (s, None) | (None, s) => s,
    }
}

/// Fold an incoming bound into the running bound on one side, keeping the
/// tighter of the two. If the two are not numerically comparable we cannot
/// merge them, so the incoming bound is preserved as its own range facet in
/// `rest` and the running bound is left unchanged (conjunction is order- and
/// grouping-independent, so this stays sound — we just tighten less).
fn fold_bound(
    cur: Option<Bound>,
    new: Bound,
    side: Side,
    rest: &mut Vec<ValueType>,
) -> Option<Bound> {
    let Some(c) = cur else { return Some(new) };
    match compare_literals(&c.value, &new.value) {
        Some(Ordering::Equal) => {
            // same value: the exclusive bound is tighter
            Some(Bound {
                value: c.value,
                inclusive: c.inclusive && new.inclusive,
            })
        }
        Some(ord) => {
            let cur_tighter = match side {
                Side::Lo => ord == Ordering::Greater, // larger lower bound is tighter
                Side::Hi => ord == Ordering::Less,    // smaller upper bound is tighter
            };
            if cur_tighter { Some(c) } else { Some(new) }
        }
        None => {
            rest.push(match side {
                Side::Lo => ValueType::NumericRange {
                    lo: Some(new),
                    hi: None,
                },
                Side::Hi => ValueType::NumericRange {
                    lo: None,
                    hi: Some(new),
                },
            });
            Some(c)
        }
    }
}

/// Is `iri` a XSD datatype that is definitely not numeric?
///
/// Conservative: unknown datatypes return `false` so we never mis-declare
/// a custom numeric datatype as unsat.
fn is_definitely_non_numeric_xsd(iri: &str) -> bool {
    const XSD: &str = "http://www.w3.org/2001/XMLSchema#";
    let Some(local) = iri.strip_prefix(XSD) else {
        return false;
    };
    matches!(
        local,
        "string"
            | "normalizedString"
            | "token"
            | "language"
            | "NMTOKEN"
            | "Name"
            | "NCName"
            | "ID"
            | "IDREF"
            | "ENTITY"
            | "boolean"
            | "date"
            | "dateTime"
            | "time"
            | "duration"
            | "gYear"
            | "gMonth"
            | "gDay"
            | "gYearMonth"
            | "gMonthDay"
            | "hexBinary"
            | "base64Binary"
            | "anyURI"
            | "QName"
            | "NOTATION"
    )
}

/// Is `[lo, hi]` empty? `lo > hi`, or `lo == hi` with either end exclusive.
/// Only decides when the two bounds are numerically comparable.
fn range_is_empty(lo: &Option<Bound>, hi: &Option<Bound>) -> bool {
    let (Some(l), Some(h)) = (lo, hi) else {
        return false;
    };
    match compare_literals(&l.value, &h.value) {
        Some(Ordering::Greater) => true,
        Some(Ordering::Equal) => !(l.inclusive && h.inclusive),
        _ => false,
    }
}

/// Numeric comparison of two literals; `None` when not comparable in this slice
/// (non-numeric datatypes or unparseable lexical forms). Covers xsd numeric
/// only — dateTime/duration ordering is deferred (see `docs/BACKLOG.md`).
fn compare_literals(a: &Literal, b: &Literal) -> Option<Ordering> {
    let x: f64 = a.value().parse().ok()?;
    let y: f64 = b.value().parse().ok()?;
    x.partial_cmp(&y)
}

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

    #[test]
    fn and_flattens_and_drops_any() {
        let xsd_string = NamedNode::new("http://www.w3.org/2001/XMLSchema#string").unwrap();
        let dt = ValueType::Datatype(xsd_string);
        let len = ValueType::Length {
            min: Some(1),
            max: None,
        };
        let combined = ValueType::and(vec![
            ValueType::Any,
            dt.clone(),
            ValueType::and(vec![len.clone(), ValueType::Any]),
        ]);
        assert_eq!(combined, ValueType::And(vec![dt, len]));
    }

    #[test]
    fn and_units() {
        assert_eq!(ValueType::and(vec![]), ValueType::Any);
        assert_eq!(
            ValueType::and(vec![ValueType::Any, ValueType::Any]),
            ValueType::Any
        );
    }

    fn int(n: i64) -> Literal {
        Literal::new_typed_literal(
            n.to_string(),
            NamedNode::new("http://www.w3.org/2001/XMLSchema#integer").unwrap(),
        )
    }

    fn incl(n: i64) -> Bound {
        Bound {
            value: int(n),
            inclusive: true,
        }
    }

    fn excl(n: i64) -> Bound {
        Bound {
            value: int(n),
            inclusive: false,
        }
    }

    #[test]
    fn empty_range_is_unsat() {
        // [5, 3] is empty
        let vt = ValueType::NumericRange {
            lo: Some(incl(5)),
            hi: Some(incl(3)),
        };
        assert_eq!(vt.normalize(), None);
    }

    #[test]
    fn point_range_exclusive_end_is_unsat() {
        // [5, 5) is empty
        let vt = ValueType::NumericRange {
            lo: Some(incl(5)),
            hi: Some(excl(5)),
        };
        assert_eq!(vt.normalize(), None);
        // but [5, 5] is a single point, satisfiable
        let pt = ValueType::NumericRange {
            lo: Some(incl(5)),
            hi: Some(incl(5)),
        };
        assert!(pt.normalize().is_some());
    }

    #[test]
    fn merges_numeric_ranges() {
        // (≥1) ∧ (≤10) → [1, 10]
        let vt = ValueType::and(vec![
            ValueType::NumericRange {
                lo: Some(incl(1)),
                hi: None,
            },
            ValueType::NumericRange {
                lo: None,
                hi: Some(incl(10)),
            },
        ]);
        assert_eq!(
            vt.normalize(),
            Some(ValueType::NumericRange {
                lo: Some(incl(1)),
                hi: Some(incl(10))
            })
        );
    }

    #[test]
    fn merges_to_tighter_bounds() {
        // (≥1) ∧ (≥5) → ≥5 ; (≤10) ∧ (≤3) → ≤3 ⇒ [5, 3] ⇒ ⊥
        let vt = ValueType::and(vec![
            ValueType::NumericRange {
                lo: Some(incl(1)),
                hi: Some(incl(10)),
            },
            ValueType::NumericRange {
                lo: Some(incl(5)),
                hi: Some(incl(3)),
            },
        ]);
        assert_eq!(vt.normalize(), None);
    }

    #[test]
    fn exclusive_wins_on_tie() {
        // (≥5 incl) ∧ (≥5 excl) → ≥5 excl
        let vt = ValueType::and(vec![
            ValueType::NumericRange {
                lo: Some(incl(5)),
                hi: None,
            },
            ValueType::NumericRange {
                lo: Some(excl(5)),
                hi: None,
            },
        ]);
        assert_eq!(
            vt.normalize(),
            Some(ValueType::NumericRange {
                lo: Some(excl(5)),
                hi: None
            })
        );
    }

    #[test]
    fn distinct_datatypes_are_unsat() {
        let xsd_int = NamedNode::new("http://www.w3.org/2001/XMLSchema#integer").unwrap();
        let xsd_string = NamedNode::new("http://www.w3.org/2001/XMLSchema#string").unwrap();
        let vt = ValueType::and(vec![
            ValueType::Datatype(xsd_int),
            ValueType::Datatype(xsd_string),
        ]);
        assert_eq!(vt.normalize(), None);
    }

    #[test]
    fn merges_length_bounds_and_unsat() {
        // minLength 2 ∧ maxLength 5 → {2..5}
        let ok = ValueType::and(vec![
            ValueType::Length {
                min: Some(2),
                max: None,
            },
            ValueType::Length {
                min: None,
                max: Some(5),
            },
        ]);
        assert_eq!(
            ok.normalize(),
            Some(ValueType::Length {
                min: Some(2),
                max: Some(5)
            })
        );
        // minLength 5 ∧ maxLength 2 → ⊥
        let bad = ValueType::and(vec![
            ValueType::Length {
                min: Some(5),
                max: None,
            },
            ValueType::Length {
                min: None,
                max: Some(2),
            },
        ]);
        assert_eq!(bad.normalize(), None);
    }

    #[test]
    fn incomparable_bounds_are_kept_not_dropped() {
        // An integer lower bound and a non-numeric "date" upper bound can't be
        // compared in this slice: keep both, declare neither unsat.
        let date = Literal::new_typed_literal(
            "2020-01-01",
            NamedNode::new("http://www.w3.org/2001/XMLSchema#date").unwrap(),
        );
        let vt = ValueType::and(vec![
            ValueType::NumericRange {
                lo: Some(incl(1)),
                hi: None,
            },
            ValueType::NumericRange {
                lo: None,
                hi: Some(Bound {
                    value: date,
                    inclusive: true,
                }),
            },
        ]);
        let n = vt.normalize().expect("not unsat");
        // both bounds still present somewhere in the result
        let s = format!("{n:?}");
        assert!(s.contains("date"), "date bound dropped: {s}");
    }

    #[test]
    fn any_normalizes_to_any() {
        assert_eq!(ValueType::Any.normalize(), Some(ValueType::Any));
    }
}