ocas-rewrite 0.27.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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//! Rewrite rules for oCAS.
//!
//! A [`Rule`] pairs a pattern with a replacement builder. When the pattern
//! matches a sub-expression, the builder receives the wildcard bindings and
//! produces a replacement atom. Rules are applied by the [`simplify()`](crate::simplify::simplify)
//! function in a bottom-up traversal until no more changes occur.

use ocas_atom::{Atom, AtomArena, AtomNode, Symbol};
use ocas_core::FastHashMap;

use crate::matcher::{Bindings, MatchError, MatchValue, match_pattern};
use crate::pattern::{Pattern, PatternAlloc, WildcardLevel};

/// A rewrite rule.
///
/// Rules are typically created from parsed patterns via [`Rule::new`], or by
/// using the convenience constructors in the [`rules`](crate::rules) module.
///
/// The replacement and condition use boxed closures so that users can capture
/// their environment when building custom rules. Built-in rules still use
/// plain closures, so they incur only a single allocation per rule.
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_atom::Symbol;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::matcher::{Bindings, MatchValue};
/// use ocas_rewrite::pattern::{Pattern, WildcardLevel};
/// use ocas_rewrite::rules::Rule;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let pat = Pattern::Wildcard(Symbol::new("x"), WildcardLevel::Single);
/// let rule = Rule::new(pat, |bindings: &Bindings, _ctx: &AtomArena| {
///     let x = bindings.get(Symbol::new("x")).unwrap();
///     let MatchValue::Single(v) = x else { panic!("expected single"); };
///     _ctx.mul(&[_ctx.num(2), *v])
/// });
///
/// let y = ctx.var("y");
/// let result = rule.apply(&ctx, y).unwrap();
/// assert_eq!(result.to_string(), "2*y");
/// ```
pub struct Rule<'a> {
    pattern: Pattern<'a>,
    replacement: Replacement<'a>,
    condition: Option<Condition<'a>>,
    /// Optional template pattern; when present, `apply` instantiates it with
    /// the match bindings instead of calling `replacement`.
    template: Option<Pattern<'a>>,
}

type Replacement<'a> = Box<dyn Fn(&Bindings<'a>, &AtomArena<'a>) -> Atom<'a> + 'a>;
type Condition<'a> = Box<dyn Fn(&Bindings<'a>) -> bool + 'a>;

impl<'a> Rule<'a> {
    /// Create a rule from a pattern and a replacement builder.
    ///
    /// The `replacement` closure receives the bindings produced by a successful
    /// match and the arena context so it can construct new atoms.
    ///
    /// # Example
    ///
    /// ```
    /// use ocas_atom::AtomArena;
    /// use ocas_atom::Symbol;
    /// use ocas_core::arena::Arena;
    /// use ocas_rewrite::matcher::{Bindings, MatchValue};
    /// use ocas_rewrite::pattern::{Pattern, WildcardLevel};
    /// use ocas_rewrite::rules::Rule;
    ///
    /// let arena = Arena::new();
    /// let ctx = AtomArena::new(&arena);
    /// let pat = Pattern::Wildcard(Symbol::new("x"), WildcardLevel::Single);
    /// let rule = Rule::new(pat, |bindings: &Bindings, _ctx: &AtomArena| {
    ///     let x = bindings.get(Symbol::new("x")).unwrap();
    ///     let MatchValue::Single(v) = x else { panic!("expected single"); };
    ///     _ctx.pow(*v, _ctx.num(2))
    /// });
    ///
    /// let z = ctx.var("z");
    /// let result = rule.apply(&ctx, z).unwrap();
    /// assert_eq!(result.to_string(), "z^2");
    /// ```
    pub fn new<F>(pattern: Pattern<'a>, replacement: F) -> Self
    where
        F: Fn(&Bindings<'a>, &AtomArena<'a>) -> Atom<'a> + 'a,
    {
        Self {
            pattern,
            replacement: Box::new(replacement),
            condition: None,
            template: None,
        }
    }

    /// Add a condition that must hold for the rule to fire.
    ///
    /// The condition is evaluated after a successful match but before the
    /// replacement is built.
    ///
    /// # Example
    ///
    /// ```
    /// use ocas_atom::AtomArena;
    /// use ocas_atom::Symbol;
    /// use ocas_core::arena::Arena;
    /// use ocas_rewrite::matcher::{Bindings, MatchValue};
    /// use ocas_rewrite::pattern::{Pattern, WildcardLevel};
    /// use ocas_rewrite::rules::Rule;
    ///
    /// let arena = Arena::new();
    /// let ctx = AtomArena::new(&arena);
    /// let pat = Pattern::Wildcard(Symbol::new("x"), WildcardLevel::Single);
    /// let rule = Rule::new(pat, |_bindings: &Bindings, ctx: &AtomArena| {
    ///     ctx.num(99)
    /// }).with_condition(|bindings: &Bindings| {
    ///     let x = bindings.get(Symbol::new("x")).unwrap();
    ///     let MatchValue::Single(v) = x else { return false; };
    ///     v.to_string() == "y"
    /// });
    ///
    /// let y = ctx.var("y");
    /// let z = ctx.var("z");
    /// assert_eq!(rule.apply(&ctx, y).unwrap().to_string(), "99");
    /// assert!(rule.apply(&ctx, z).is_none());
    /// ```
    pub fn with_condition<F>(mut self, condition: F) -> Self
    where
        F: Fn(&Bindings<'a>) -> bool + 'a,
    {
        self.condition = Some(Box::new(condition));
        self
    }

    /// Try to apply this rule to `atom`. Returns `Some` if the rule matched and
    /// the condition (if any) was satisfied.
    pub fn apply(&self, ctx: &'a AtomArena<'a>, atom: Atom<'a>) -> Option<Atom<'a>> {
        match match_pattern(self.pattern.clone(), atom) {
            Ok(bindings) => {
                if let Some(cond) = &self.condition
                    && !cond(&bindings)
                {
                    return None;
                }
                if let Some(tmpl) = &self.template {
                    let next = instantiate(ctx, tmpl, &bindings)?;
                    Some(ocas_atom::normalize::normalize(ctx, next))
                } else {
                    Some((self.replacement)(&bindings, ctx))
                }
            }
            Err(MatchError::NoMatch)
            | Err(MatchError::InconsistentBinding)
            | Err(MatchError::BudgetExhausted) => None,
        }
    }

    /// Build a rule from pattern/template strings (template instantiation).
    ///
    /// The template may contain the same wildcard names as the pattern;
    /// applying the rule instantiates the template with the match bindings and
    /// normalizes the result. This is the mechanism used by the integration
    /// rule library (ocas-calc) to express rules like
    /// `sin(x_)^n_ -> -sin(x_)^(n_-1)*cos(x_)/n_ + ...` without hand-written
    /// closures.
    pub fn from_template(
        ctx: &'a AtomArena<'a>,
        alloc: &'a impl PatternAlloc<'a>,
        pattern: &str,
        template: &str,
    ) -> Rule<'a> {
        let pat = pattern_from_str(ctx, alloc, pattern);
        let tmpl = pattern_from_str(ctx, alloc, template);
        Rule {
            pattern: pat,
            replacement: Box::new(|_, ctx| ctx.num(0)),
            condition: None,
            template: Some(tmpl),
        }
    }
}

/// Instantiate a template pattern with wildcard bindings.
///
/// Semantics:
/// - `Literal(a)` → `a`.
/// - `Wildcard(name, Single)` → the bound atom (unbound → `None`).
/// - `Wildcard(name, Sequence | NullSequence)` → only valid spliced into an
///   n-ary (`Add`/`Mul`/`Fun`) argument list, where the bound atoms are
///   inserted in order; at the top level → `None`.
/// - `Add`/`Mul`/`Fun` → rebuilt recursively (with sequence splice);
///   `Pow` → both sides rebuilt.
///
/// Returns `None` when a wildcard is unbound, bound at the wrong level, or
/// spliced outside an argument list.
pub fn instantiate<'a>(
    ctx: &'a AtomArena<'a>,
    template: &Pattern<'a>,
    bindings: &Bindings<'a>,
) -> Option<Atom<'a>> {
    match template {
        Pattern::Literal(a) => Some(*a),
        Pattern::Wildcard(name, WildcardLevel::Single) => match bindings.get(*name)? {
            MatchValue::Single(v) => Some(*v),
            MatchValue::Sequence(_) => None,
        },
        Pattern::Wildcard(name, level) => {
            // Sequence wildcards must be spliced inside an argument list;
            // a sequence wildcard at the root of the template is ambiguous.
            let _ = (name, level);
            None
        }
        Pattern::Add(pats) => {
            let args = instantiate_args(ctx, pats, bindings)?;
            Some(ctx.add(&args))
        }
        Pattern::Mul(pats) => {
            let args = instantiate_args(ctx, pats, bindings)?;
            Some(ctx.mul(&args))
        }
        Pattern::Fun(name, pats) => {
            let args = instantiate_args(ctx, pats, bindings)?;
            Some(ctx.fun(name.as_str(), &args))
        }
        Pattern::Pow(p_box) => {
            let (p_base, p_exp) = &**p_box;
            let base = instantiate(ctx, p_base, bindings)?;
            let exp = instantiate(ctx, p_exp, bindings)?;
            Some(ctx.pow(base, exp))
        }
    }
}

/// Instantiate a template argument list, splicing sequence wildcards.
fn instantiate_args<'a>(
    ctx: &'a AtomArena<'a>,
    pats: &[Pattern<'a>],
    bindings: &Bindings<'a>,
) -> Option<Vec<Atom<'a>>> {
    let mut out = Vec::with_capacity(pats.len());
    for pat in pats {
        match pat {
            Pattern::Wildcard(name, WildcardLevel::Sequence | WildcardLevel::NullSequence) => {
                match bindings.get(*name)? {
                    MatchValue::Sequence(slice) => out.extend_from_slice(slice),
                    MatchValue::Single(_) => return None,
                }
            }
            _ => out.push(instantiate(ctx, pat, bindings)?),
        }
    }
    Some(out)
}

/// Head key used to index rules for quick dispatch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HeadKey {
    /// A function application with the given head symbol.
    Fun(Symbol),
    /// An addition (`Add`).
    Add,
    /// A product (`Mul`).
    Mul,
    /// A power (`Pow`).
    Pow,
    /// Anything else (numbers, variables, ...).
    Any,
}

/// The head key of an atom, used to look up candidate rules.
pub fn head_of(atom: Atom<'_>) -> HeadKey {
    match atom.node() {
        AtomNode::Fun(name, _) => HeadKey::Fun(*name),
        AtomNode::Add(_) => HeadKey::Add,
        AtomNode::Mul(_) => HeadKey::Mul,
        AtomNode::Pow(_, _) => HeadKey::Pow,
        AtomNode::Num(_) | AtomNode::Var(_) => HeadKey::Any,
    }
}

/// The bucket a rule's pattern root dispatches to.
fn pattern_head_key(pattern: &Pattern<'_>) -> HeadKey {
    match pattern {
        Pattern::Fun(name, _) => HeadKey::Fun(*name),
        Pattern::Add(_) => HeadKey::Add,
        Pattern::Mul(_) => HeadKey::Mul,
        Pattern::Pow(_) => HeadKey::Pow,
        Pattern::Literal(_) | Pattern::Wildcard(_, _) => HeadKey::Any,
    }
}

/// A head-indexed rule table.
///
/// [`RuleTable::from_rules`] buckets rules by their pattern's root node;
/// [`RuleTable::apply`] scans only the bucket matching the atom's head plus
/// the `Any` bucket. Within a bucket rules keep insertion order and the first
/// match wins — exactly the semantics of a linear scan over the same rules.
pub struct RuleTable<'a> {
    by_head: FastHashMap<HeadKey, Vec<Rule<'a>>>,
}

impl<'a> RuleTable<'a> {
    /// Build a table from rules, bucketing by pattern root.
    pub fn from_rules(rules: Vec<Rule<'a>>) -> Self {
        let mut by_head: FastHashMap<HeadKey, Vec<Rule<'a>>> = FastHashMap::default();
        for rule in rules {
            let key = pattern_head_key(&rule.pattern);
            by_head.entry(key).or_default().push(rule);
        }
        Self { by_head }
    }

    /// Apply the first matching rule to `atom` (head bucket, then `Any`).
    pub fn apply(&self, ctx: &'a AtomArena<'a>, atom: Atom<'a>) -> Option<Atom<'a>> {
        let key = head_of(atom);
        if let Some(rules) = self.by_head.get(&key) {
            for rule in rules {
                if let Some(next) = rule.apply(ctx, atom) {
                    return Some(next);
                }
            }
        }
        if key != HeadKey::Any
            && let Some(rules) = self.by_head.get(&HeadKey::Any)
        {
            for rule in rules {
                if let Some(next) = rule.apply(ctx, atom) {
                    return Some(next);
                }
            }
        }
        None
    }
}

// ---------------------------------------------------------------------------
// Built-in algebraic rules
// ---------------------------------------------------------------------------

fn pattern_from_str<'a>(
    ctx: &'a AtomArena<'a>,
    alloc: &'a impl PatternAlloc<'a>,
    s: &str,
) -> Pattern<'a> {
    let atom = ocas_parse::parse(ctx, s).expect("built-in rule pattern is valid");
    Pattern::from_atom(alloc, atom)
}

macro_rules! binding_single {
    ($bindings:expr, $name:expr) => {
        match $bindings.get(ocas_atom::Symbol::new($name)) {
            Some(crate::matcher::MatchValue::Single(atom)) => *atom,
            _ => panic!(concat!("expected single binding for '", $name, "'")),
        }
    };
}

/// `x + 0 -> x`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::add_zero;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.add(&[x, ctx.num(0)]);
/// let rule = add_zero(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "x");
/// ```
pub fn add_zero<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "x_ + 0"), |bindings, _ctx| {
        binding_single!(bindings, "x")
    })
}

/// `0 + x -> x`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::add_zero_left;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.add(&[ctx.num(0), x]);
/// let rule = add_zero_left(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "x");
/// ```
pub fn add_zero_left<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "0 + x_"), |bindings, _ctx| {
        binding_single!(bindings, "x")
    })
}

/// `x * 0 -> 0`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::mul_zero;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.mul(&[x, ctx.num(0)]);
/// let rule = mul_zero(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "0");
/// ```
pub fn mul_zero<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "x_ * 0"), |_bindings, ctx| {
        ctx.num(0)
    })
}

/// `0 * x -> 0`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::mul_zero_left;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.mul(&[ctx.num(0), x]);
/// let rule = mul_zero_left(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "0");
/// ```
pub fn mul_zero_left<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "0 * x_"), |_bindings, ctx| {
        ctx.num(0)
    })
}

/// `x * 1 -> x`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::mul_one;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.mul(&[x, ctx.num(1)]);
/// let rule = mul_one(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "x");
/// ```
pub fn mul_one<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "x_ * 1"), |bindings, _ctx| {
        binding_single!(bindings, "x")
    })
}

/// `1 * x -> x`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::mul_one_left;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.mul(&[ctx.num(1), x]);
/// let rule = mul_one_left(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "x");
/// ```
pub fn mul_one_left<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "1 * x_"), |bindings, _ctx| {
        binding_single!(bindings, "x")
    })
}

/// `x + x -> 2*x`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::add_same;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.add(&[x, x]);
/// let rule = add_same(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "2*x");
/// ```
pub fn add_same<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "x_ + x_"), |bindings, ctx| {
        let x = binding_single!(bindings, "x");
        ctx.mul(&[ctx.num(2), x])
    })
}

/// `x ^ 0 -> 1`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::pow_zero;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.pow(x, ctx.num(0));
/// let rule = pow_zero(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "1");
/// ```
pub fn pow_zero<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "x_ ^ 0"), |_bindings, ctx| {
        ctx.num(1)
    })
}

/// `x ^ 1 -> x`
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::pow_one;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let expr = ctx.pow(x, ctx.num(1));
/// let rule = pow_one(&ctx, &());
/// let result = rule.apply(&ctx, expr).unwrap();
/// assert_eq!(result.to_string(), "x");
/// ```
pub fn pow_one<'a>(ctx: &'a AtomArena<'a>, alloc: &'a impl PatternAlloc<'a>) -> Rule<'a> {
    Rule::new(pattern_from_str(ctx, alloc, "x_ ^ 1"), |bindings, _ctx| {
        binding_single!(bindings, "x")
    })
}

/// Return the default set of algebraic rewrite rules.
///
/// # Example
///
/// ```
/// use ocas_atom::AtomArena;
/// use ocas_core::arena::Arena;
/// use ocas_rewrite::rules::default_rules;
///
/// let arena = Arena::new();
/// let ctx = AtomArena::new(&arena);
/// let x = ctx.var("x");
/// let rules = default_rules(&ctx, &());
/// let expr = ctx.add(&[x, ctx.num(0)]);
/// let mut current = expr;
/// for rule in &rules {
///     if let Some(next) = rule.apply(&ctx, current) {
///         current = next;
///     }
/// }
/// assert_eq!(current.to_string(), "x");
/// ```
pub fn default_rules<'a>(
    ctx: &'a AtomArena<'a>,
    alloc: &'a impl PatternAlloc<'a>,
) -> Vec<Rule<'a>> {
    vec![
        add_zero(ctx, alloc),
        add_zero_left(ctx, alloc),
        mul_zero(ctx, alloc),
        mul_zero_left(ctx, alloc),
        mul_one(ctx, alloc),
        mul_one_left(ctx, alloc),
        add_same(ctx, alloc),
        pow_zero(ctx, alloc),
        pow_one(ctx, alloc),
    ]
}

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

    struct VecAlloc;

    impl<'a> PatternAlloc<'a> for VecAlloc {
        fn alloc_slice(&self, _items: &[Pattern<'a>]) -> &'a [Pattern<'a>] {
            // Not used by the matcher with the current Vec-based Pattern design.
            unreachable!()
        }
    }

    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 add_zero_applies() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let rule = add_zero(&ctx, &alloc);
        let x = ctx.var("x");
        let atom = ctx.add(&[x, ctx.num(0)]);
        let result = rule.apply(&ctx, atom).unwrap();
        assert_eq!(result, x);
    }

    #[test]
    fn mul_zero_applies() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let rule = mul_zero(&ctx, &alloc);
        let x = ctx.var("x");
        let atom = ctx.mul(&[x, ctx.num(0)]);
        let result = rule.apply(&ctx, atom).unwrap();
        assert_eq!(result, ctx.num(0));
    }

    #[test]
    fn add_same_applies() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let rule = add_same(&ctx, &alloc);
        let x = ctx.var("x");
        let atom = ctx.add(&[x, x]);
        let result = rule.apply(&ctx, atom).unwrap();
        assert_eq!(result.to_string(), "2*x");
    }

    #[test]
    fn rule_with_condition_can_block() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let pat = pat_atom(&ctx, &alloc, "x_");
        let rule = Rule::new(pat, |bindings, _ctx| binding_single!(bindings, "x"))
        .with_condition(|bindings| {
            matches!(
                bindings.get(ocas_atom::Symbol::new("x")),
                Some(crate::matcher::MatchValue::Single(a)) if matches!(a.node(), ocas_atom::AtomNode::Num(_))
            )
        });

        let x = ctx.var("x");
        assert!(rule.apply(&ctx, x).is_none());
        assert!(rule.apply(&ctx, ctx.num(5)).is_some());
    }

    #[test]
    fn from_template_instantiates_single_wildcards() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let rule = Rule::from_template(&ctx, &alloc, "x_^n_", "x_^(n_+1)*(n_+1)^-1");
        let atom = ocas_parse::parse(&ctx, "x^3").unwrap();
        let result = rule.apply(&ctx, atom).unwrap();
        // n_ binds to 3, template is x^(n+1)*(n+1)^-1 = x^4/4.
        assert_eq!(result.to_string(), "(4^-1)*(x^4)");
        // Non-matching shape: no rewrite.
        assert!(rule.apply(&ctx, ctx.var("y")).is_none());
    }

    #[test]
    fn from_template_splices_sequence_wildcards() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        // `f(a__, b_)` -> `g(a__, b_)` with a non-empty sequence splice.
        let rule = Rule::from_template(&ctx, &alloc, "f(a__, b_)", "g(a__, b_)");
        let atom = ocas_parse::parse(&ctx, "f(x, y, z)").unwrap();
        let result = rule.apply(&ctx, atom).unwrap();
        assert_eq!(result.to_string(), "g(x, y, z)");
    }

    #[test]
    fn instantiate_unbound_wildcard_is_none() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let tmpl = pat_atom(&ctx, &alloc, "x_^2");
        let bindings = Bindings::new();
        assert!(instantiate(&ctx, &tmpl, &bindings).is_none());
    }

    #[test]
    fn rule_table_equals_linear_scan_on_default_rules() {
        let arena = Arena::new();
        let ctx = AtomArena::new(&arena);
        let alloc = VecAlloc;
        let rules = default_rules(&ctx, &alloc);
        let table = RuleTable::from_rules(rules);
        let samples = [
            "x + 0", "0 + x", "x*0", "0*x", "x*1", "1*x", "x + x", "y^0", "y^1", "2*x + 0", "x*y",
            "0",
        ];
        for s in samples {
            let atom = ocas_parse::parse(&ctx, s).unwrap();
            let mut linear = atom;
            for rule in default_rules(&ctx, &alloc) {
                if let Some(next) = rule.apply(&ctx, linear) {
                    linear = next;
                }
            }
            let indexed = table.apply(&ctx, atom).unwrap_or(atom);
            assert_eq!(
                indexed, linear,
                "table vs linear disagree on {s}: {indexed} vs {linear}"
            );
        }
    }
}