aboutsummaryrefslogtreecommitdiffstats
path: root/notes/term_rewriting.md
blob: 9d0255a10839d9c71dcbfd8a198df20df248c71b (plain)
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

Syntax for term re-writing:

    (rule <pattern>
          <substitute>)

Pattern examples:

    (? a)
    (? a number?)
    (? a not-number?)
    (? a number? odd?)
    (? a (free? x))
    (? a (neq? 0))

    ; match zero or more into a list; expand list in-line, same order
    (?* l)

Full examples:

    (rule (/ 1 (/ (? a) (? b)))
          (/ (? b) (? a)))

Simplifications:

- variable names must be single Latin letter
- predicates are fixed to a set of type checks

## Implementation

Separate rule lists, one for each "head" type (Sum, Product, Power, etc).

    struct RuleSet {
        sum_rules: Vec<Rule>,
        product_rules: Vec<Rule>,
        [...]
    }

    struct Rule {
        variables: HashMap<type, char, Vec<predicates>>,
        pattern: MatchExpr,
        substitute: MatchExpr,
    }

    enum MatchExpr {
        variable: char,
        atom: CExpr,
        list: { head: expr_type, tail: Vec<MatchExpr> },
    }

----------------

Backburner:
- are additional pattern-wide predicates necessary or helpful?