ocas-rewrite 0.13.1

Pattern matching, rewriting and rule-based simplification for oCAS
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
//! Pattern matching engine for oCAS.
//!
//! The matcher binds [`Pattern`] wildcards to [`Atom`]
//! sub-expressions. It supports associative/commutative matching for `Add`
//! and `Mul` using backtracking, and sequence wildcards for ordered argument
//! lists such as function arguments.

use std::collections::HashMap;

use ocas_atom::{Atom, AtomNode, Symbol};

use crate::pattern::{Pattern, WildcardLevel};

/// A collection of wildcard bindings produced by a successful match.
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_atom::Symbol;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::matcher::{match_pattern, Bindings, MatchValue};
/// use ocas_rewrite::pattern::{Pattern, WildcardLevel};
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let pat = Pattern::Wildcard(Symbol::new("w"), WildcardLevel::Single);
/// let bindings = match_pattern(pat, x).unwrap();
/// let value = bindings.get(Symbol::new("w")).unwrap();
/// assert!(matches!(value, MatchValue::Single(v) if v.to_string() == "x"));
/// ```
#[derive(Debug, Clone, Default)]
pub struct Bindings<'a> {
    map: HashMap<Symbol, MatchValue<'a>>,
}

impl<'a> Bindings<'a> {
    /// Create an empty binding set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Look up a binding by wildcard name.
    pub fn get(&self, name: Symbol) -> Option<&MatchValue<'a>> {
        self.map.get(&name)
    }

    /// Insert a single-atom binding. Returns `Err` if the name is already bound
    /// to a different value.
    fn insert_single(&mut self, name: Symbol, value: Atom<'a>) -> Result<(), MatchError> {
        match self.map.get(&name) {
            Some(MatchValue::Single(existing)) if *existing == value => Ok(()),
            Some(_) => Err(MatchError::InconsistentBinding),
            None => {
                self.map.insert(name, MatchValue::Single(value));
                Ok(())
            }
        }
    }

    /// Insert a multi-atom binding (used for sequence/blank null sequence).
    fn insert_sequence(&mut self, name: Symbol, value: &'a [Atom<'a>]) -> Result<(), MatchError> {
        match self.map.get(&name) {
            Some(MatchValue::Sequence(existing)) if *existing == value => Ok(()),
            Some(_) => Err(MatchError::InconsistentBinding),
            None => {
                self.map.insert(name, MatchValue::Sequence(value));
                Ok(())
            }
        }
    }
}

/// A value bound to a wildcard.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchValue<'a> {
    /// A single atom binding.
    Single(Atom<'a>),
    /// A slice of atoms bound to a sequence wildcard.
    Sequence(&'a [Atom<'a>]),
}

/// Errors that can occur during matching.
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_atom::Symbol;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::matcher::{match_pattern, MatchError};
/// use ocas_rewrite::pattern::{Pattern, WildcardLevel};
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let y = ctx.var("y");
/// let pat = Pattern::Literal(x);
/// let err = match_pattern(pat, y).unwrap_err();
/// assert!(matches!(err, MatchError::NoMatch));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchError {
    /// The pattern did not match the atom.
    NoMatch,
    /// A wildcard name was bound to two different values.
    InconsistentBinding,
}

impl std::fmt::Display for MatchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MatchError::NoMatch => write!(f, "pattern did not match"),
            MatchError::InconsistentBinding => write!(f, "inconsistent wildcard binding"),
        }
    }
}

impl std::error::Error for MatchError {}

/// Match a pattern against an atom, returning bindings on success.
///
/// `Add` and `Mul` are matched associatively and commutatively: arguments are
/// sorted and literal patterns are matched before single wildcards. Sequence
/// wildcards are not supported inside `Add`/`Mul` in this simplified matcher;
/// they are supported for ordered argument lists such as function arguments.
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_atom::Symbol;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::matcher::{match_pattern, MatchValue};
/// use ocas_rewrite::pattern::{Pattern, WildcardLevel};
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let y = ctx.var("y");
/// let sum = ctx.add(&[x, y]);
/// let pat = Pattern::Add(vec![
///     Pattern::Wildcard(Symbol::new("a"), WildcardLevel::Single),
///     Pattern::Wildcard(Symbol::new("b"), WildcardLevel::Single),
/// ]);
/// let bindings = match_pattern(pat, sum).unwrap();
/// let a = bindings.get(Symbol::new("a")).unwrap();
/// let b = bindings.get(Symbol::new("b")).unwrap();
/// assert!(matches!(a, MatchValue::Single(v) if v.to_string() == "x"));
/// assert!(matches!(b, MatchValue::Single(v) if v.to_string() == "y"));
/// ```
pub fn match_pattern<'a>(pattern: Pattern<'a>, atom: Atom<'a>) -> Result<Bindings<'a>, MatchError> {
    let mut bindings = Bindings::new();
    match_atom(&mut bindings, pattern, atom)?;
    Ok(bindings)
}

fn match_atom<'a>(
    bindings: &mut Bindings<'a>,
    pattern: Pattern<'a>,
    atom: Atom<'a>,
) -> Result<(), MatchError> {
    match pattern {
        Pattern::Literal(p) => {
            if p == atom {
                Ok(())
            } else {
                Err(MatchError::NoMatch)
            }
        }
        Pattern::Wildcard(name, WildcardLevel::Single) => bindings.insert_single(name, atom),
        Pattern::Wildcard(name, WildcardLevel::Sequence) => match atom.node() {
            AtomNode::Add(args) | AtomNode::Mul(args) | AtomNode::Fun(_, args) => {
                if args.is_empty() {
                    Err(MatchError::NoMatch)
                } else {
                    bindings.insert_sequence(name, args)
                }
            }
            _ => Err(MatchError::NoMatch),
        },
        Pattern::Wildcard(name, WildcardLevel::NullSequence) => match atom.node() {
            AtomNode::Add(args) | AtomNode::Mul(args) | AtomNode::Fun(_, args) => {
                bindings.insert_sequence(name, args)
            }
            _ => Err(MatchError::NoMatch),
        },
        Pattern::Add(pats) => match atom.node() {
            AtomNode::Add(args) => match_nary(bindings, &pats, args, true),
            _ => Err(MatchError::NoMatch),
        },
        Pattern::Mul(pats) => match atom.node() {
            AtomNode::Mul(args) => match_nary(bindings, &pats, args, true),
            _ => Err(MatchError::NoMatch),
        },
        Pattern::Pow(p_box) => match atom.node() {
            AtomNode::Pow(base, exp) => {
                let (p_base, p_exp) = *p_box;
                match_atom(bindings, p_base, *base)?;
                match_atom(bindings, p_exp, *exp)
            }
            _ => Err(MatchError::NoMatch),
        },
        Pattern::Fun(p_name, p_args) => match atom.node() {
            AtomNode::Fun(name, args) if *name == p_name => {
                match_nary(bindings, &p_args, args, false)
            }
            _ => Err(MatchError::NoMatch),
        },
    }
}

/// Match a pattern list against an argument list. For `Add` and `Mul` we sort
/// the target atoms and match literal/sequence patterns greedily; for `Fun`
/// arguments we keep the order and only allow a single trailing null-sequence
/// wildcard to absorb leftovers.
fn match_nary<'a>(
    bindings: &mut Bindings<'a>,
    patterns: &'_ [Pattern<'a>],
    atoms: &'a [Atom<'a>],
    associative_commutative: bool,
) -> Result<(), MatchError> {
    if associative_commutative {
        match_nary_ac(bindings, patterns, atoms)
    } else {
        match_nary_ordered(bindings, patterns, atoms)
    }
}

/// Ordered matching for function arguments. Supports at most one trailing null
/// sequence wildcard. Sequence wildcards are not supported here.
fn match_nary_ordered<'a>(
    bindings: &mut Bindings<'a>,
    patterns: &'_ [Pattern<'a>],
    atoms: &'a [Atom<'a>],
) -> Result<(), MatchError> {
    let mut pat_idx = 0;
    let mut atom_idx = 0;
    while pat_idx < patterns.len() {
        let pat = &patterns[pat_idx];
        match pat {
            Pattern::Wildcard(name, WildcardLevel::NullSequence)
                if pat_idx == patterns.len() - 1 =>
            {
                let rest = &atoms[atom_idx..];
                bindings.insert_sequence(*name, rest)?;
                atom_idx = atoms.len();
                pat_idx += 1;
            }
            Pattern::Wildcard(name, WildcardLevel::Single) => {
                if atom_idx >= atoms.len() {
                    return Err(MatchError::NoMatch);
                }
                bindings.insert_single(*name, atoms[atom_idx])?;
                atom_idx += 1;
                pat_idx += 1;
            }
            _ => {
                if atom_idx >= atoms.len() {
                    return Err(MatchError::NoMatch);
                }
                match_atom(bindings, pat.clone(), atoms[atom_idx])?;
                atom_idx += 1;
                pat_idx += 1;
            }
        }
    }
    if atom_idx == atoms.len() {
        Ok(())
    } else {
        Err(MatchError::NoMatch)
    }
}

/// Associative/commutative matching for `Add` and `Mul`. We sort the target
/// atoms by their natural ordering and match literals first, then single
/// wildcards. Sequence wildcards are not supported in this simplified matcher
/// because the sorted permutation of `atoms` cannot be borrowed as a single
/// contiguous slice.
fn match_nary_ac<'a>(
    bindings: &mut Bindings<'a>,
    patterns: &'_ [Pattern<'a>],
    atoms: &'a [Atom<'a>],
) -> Result<(), MatchError> {
    if patterns.is_empty() {
        return if atoms.is_empty() {
            Ok(())
        } else {
            Err(MatchError::NoMatch)
        };
    }

    if patterns.iter().any(|p| {
        matches!(
            p,
            Pattern::Wildcard(_, WildcardLevel::Sequence | WildcardLevel::NullSequence)
        )
    }) {
        return Err(MatchError::NoMatch);
    }

    let mut sorted_atoms: Vec<Atom<'a>> = atoms.to_vec();
    sorted_atoms.sort();

    let mut matched = vec![false; sorted_atoms.len()];
    let mut single_wildcards: Vec<usize> = Vec::new();
    let mut literal_indices: Vec<usize> = Vec::new();

    for (i, pat) in patterns.iter().enumerate() {
        match pat {
            Pattern::Wildcard(_, WildcardLevel::Single) => single_wildcards.push(i),
            _ => literal_indices.push(i),
        }
    }

    // Match literals first.
    for pat_idx in literal_indices {
        let pat = &patterns[pat_idx];
        let mut found = false;
        for (i, atom) in sorted_atoms.iter().enumerate() {
            if matched[i] {
                continue;
            }
            let mut probe = bindings.clone();
            if match_atom(&mut probe, pat.clone(), *atom).is_ok() {
                *bindings = probe;
                matched[i] = true;
                found = true;
                break;
            }
        }
        if !found {
            return Err(MatchError::NoMatch);
        }
    }

    // Match single wildcards against the remaining atoms.
    let remaining: Vec<Atom<'a>> = sorted_atoms
        .iter()
        .enumerate()
        .filter(|(i, _)| !matched[*i])
        .map(|(_, a)| *a)
        .collect();
    if remaining.len() != single_wildcards.len() {
        return Err(MatchError::NoMatch);
    }
    for (pat_idx, atom) in single_wildcards.iter().zip(remaining.iter()) {
        if let Pattern::Wildcard(name, WildcardLevel::Single) = &patterns[*pat_idx] {
            bindings.insert_single(*name, *atom)?;
        }
    }

    Ok(())
}

impl<'a> Pattern<'a> {
    fn _wildcard_level(&self) -> Option<WildcardLevel> {
        match self {
            Pattern::Wildcard(_, level) => Some(*level),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ocas_atom::AtomArena;
    use ocas_core::arena::Arena;

    struct VecAlloc;

    impl<'a> crate::pattern::PatternAlloc<'a> for VecAlloc {
        fn alloc_slice(&self, items: &[Pattern<'a>]) -> &'a [Pattern<'a>] {
            let leaked: Box<[Pattern<'a>]> = items.to_vec().into_boxed_slice();
            Box::leak(leaked)
        }
    }

    fn pat_atom<'a>(ctx: &'a AtomArena<'a>, alloc: &'a VecAlloc, s: &'a str) -> Pattern<'a> {
        let atom = ocas_parse::parse(ctx, s).unwrap();
        Pattern::from_atom(alloc, atom)
    }

    #[test]
    fn single_wildcard_binds_any_atom() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let pat = pat_atom(&ctx, &alloc, "x_");
        let y = ctx.var("y");
        let bindings = match_pattern(pat, y).unwrap();
        assert!(matches!(bindings.get(Symbol::new("x")), Some(MatchValue::Single(a)) if *a == y));
    }

    #[test]
    fn add_pattern_matches_two_atoms() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let pat = pat_atom(&ctx, &alloc, "x_ + y_");
        let x = ctx.var("x");
        let y = ctx.var("y");
        let sum = ctx.add(&[x, y]);
        let bindings = match_pattern(pat, sum).unwrap();
        assert!(matches!(
            bindings.get(Symbol::new("x")),
            Some(MatchValue::Single(a)) if *a == x
        ));
        assert!(matches!(
            bindings.get(Symbol::new("y")),
            Some(MatchValue::Single(a)) if *a == y
        ));
    }

    #[test]
    fn add_pattern_ignores_order() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let pat = pat_atom(&ctx, &alloc, "x_ + y_");
        let a = ctx.var("a");
        let b = ctx.var("b");
        let sum = ctx.add(&[b, a]);
        let bindings = match_pattern(pat, sum).unwrap();
        assert!(matches!(
            bindings.get(Symbol::new("x")),
            Some(MatchValue::Single(atom)) if *atom == a
        ));
        assert!(matches!(
            bindings.get(Symbol::new("y")),
            Some(MatchValue::Single(atom)) if *atom == b
        ));
    }

    #[test]
    fn add_pattern_requires_exact_match_count() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let pat = pat_atom(&ctx, &alloc, "x_ + y_");
        let a = ctx.var("a");
        let b = ctx.var("b");
        let c = ctx.var("c");
        let sum = ctx.add(&[a, b, c]);
        assert!(match_pattern(pat, sum).is_err());
    }

    #[test]
    fn inconsistent_binding_fails() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let pat = pat_atom(&ctx, &alloc, "x_ + x_");
        let a = ctx.var("a");
        let b = ctx.var("b");
        let sum = ctx.add(&[a, b]);
        assert!(match_pattern(pat, sum).is_err());
    }

    #[test]
    fn sequence_wildcard_in_fun_absorbs_remainder() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let pat = pat_atom(&ctx, &alloc, "f(x_, rest___)");
        let a = ctx.var("a");
        let b = ctx.var("b");
        let c = ctx.var("c");
        let f = ctx.fun("f", &[a, b, c]);
        let bindings = match_pattern(pat, f).unwrap();
        assert!(matches!(
            bindings.get(Symbol::new("x")),
            Some(MatchValue::Single(atom)) if *atom == a
        ));
        let rest = bindings.get(Symbol::new("rest")).unwrap();
        assert!(matches!(rest, MatchValue::Sequence(s) if s.len() == 2));
    }
}