logicaffeine_compile/optimize/egraph/enode.rs
1//! The Architect's term language — flat, multi-arity, `Copy`.
2//!
3//! Children are `NodeId`s (e-class references after canonicalization).
4//! `Var` carries `(symbol index, version)` — versions partition reads of a
5//! mutable name so equality never leaks across a write. `Opaque` is the
6//! escape hatch: any expression the e-graph does not model (calls, indexing,
7//! collection literals, text) becomes an opaque leaf whose ORIGINAL `&Expr`
8//! pointer is held by the converter, so extraction reproduces it verbatim —
9//! effects and error behavior preserved by construction.
10
11use super::NodeId;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub enum CompilerENode {
15 Int(i64),
16 Bool(bool),
17 /// f64 bit pattern. Floats are NEVER arithmetic-rewritten (no identity,
18 /// no reassociation — bit-exactness is the contract); the variant exists
19 /// so float-valued subtrees still participate in congruence.
20 Float(u64),
21 /// (symbol index, version)
22 Var(u32, u32),
23 /// Index into the converter's originals table.
24 Opaque(u32),
25
26 Add(NodeId, NodeId),
27 Sub(NodeId, NodeId),
28 Mul(NodeId, NodeId),
29 Div(NodeId, NodeId),
30 Mod(NodeId, NodeId),
31 Shl(NodeId, NodeId),
32 Shr(NodeId, NodeId),
33 BitXor(NodeId, NodeId),
34 /// Bitwise `&`/`|` on Int (the surface `&`/`|` symbols; also what the
35 /// mod-pow2 mask rule synthesizes).
36 BitAnd(NodeId, NodeId),
37 BitOr(NodeId, NodeId),
38 /// Logical `and`/`or`: truthiness in, Bool out, short-circuit. Rules
39 /// consult class facts (`is_bool`) before rewriting.
40 And(NodeId, NodeId),
41 Or(NodeId, NodeId),
42 Not(NodeId),
43
44 Eq(NodeId, NodeId),
45 Ne(NodeId, NodeId),
46 Lt(NodeId, NodeId),
47 Le(NodeId, NodeId),
48 Gt(NodeId, NodeId),
49 Ge(NodeId, NodeId),
50
51 Concat(NodeId, NodeId),
52 Len(NodeId),
53 Index(NodeId, NodeId),
54 /// `copy of xs` — a fresh, unaliased deep copy. Total.
55 Copy(NodeId),
56 /// `xs sliced from a to b` (1-based, inclusive). NON-total: raises on
57 /// out-of-bounds, so rewrites may never delete one without proofs.
58 Slice(NodeId, NodeId, NodeId),
59 /// `xs contains x` — membership. Total; modeled for congruence/CSE
60 /// only (no rewrite touches its semantics).
61 Contains(NodeId, NodeId),
62}
63
64impl CompilerENode {
65 /// Children in evaluation order.
66 pub fn children(&self) -> Vec<NodeId> {
67 use CompilerENode::*;
68 match *self {
69 Int(_) | Bool(_) | Float(_) | Var(..) | Opaque(_) => vec![],
70 Not(a) | Len(a) | Copy(a) => vec![a],
71 Add(a, b)
72 | Sub(a, b)
73 | Mul(a, b)
74 | Div(a, b)
75 | Mod(a, b)
76 | Shl(a, b)
77 | Shr(a, b)
78 | BitXor(a, b)
79 | BitAnd(a, b)
80 | BitOr(a, b)
81 | And(a, b)
82 | Or(a, b)
83 | Eq(a, b)
84 | Ne(a, b)
85 | Lt(a, b)
86 | Le(a, b)
87 | Gt(a, b)
88 | Ge(a, b)
89 | Concat(a, b)
90 | Index(a, b)
91 | Contains(a, b) => {
92 vec![a, b]
93 }
94 Slice(a, b, c) => vec![a, b, c],
95 }
96 }
97
98 /// Rebuild the node with each child mapped (canonicalization).
99 pub fn map_children(self, mut f: impl FnMut(NodeId) -> NodeId) -> Self {
100 use CompilerENode::*;
101 match self {
102 Int(_) | Bool(_) | Float(_) | Var(..) | Opaque(_) => self,
103 Not(a) => Not(f(a)),
104 Len(a) => Len(f(a)),
105 Add(a, b) => Add(f(a), f(b)),
106 Sub(a, b) => Sub(f(a), f(b)),
107 Mul(a, b) => Mul(f(a), f(b)),
108 Div(a, b) => Div(f(a), f(b)),
109 Mod(a, b) => Mod(f(a), f(b)),
110 Shl(a, b) => Shl(f(a), f(b)),
111 Shr(a, b) => Shr(f(a), f(b)),
112 BitXor(a, b) => BitXor(f(a), f(b)),
113 BitAnd(a, b) => BitAnd(f(a), f(b)),
114 BitOr(a, b) => BitOr(f(a), f(b)),
115 And(a, b) => And(f(a), f(b)),
116 Or(a, b) => Or(f(a), f(b)),
117 Eq(a, b) => Eq(f(a), f(b)),
118 Ne(a, b) => Ne(f(a), f(b)),
119 Lt(a, b) => Lt(f(a), f(b)),
120 Le(a, b) => Le(f(a), f(b)),
121 Gt(a, b) => Gt(f(a), f(b)),
122 Ge(a, b) => Ge(f(a), f(b)),
123 Concat(a, b) => Concat(f(a), f(b)),
124 Index(a, b) => Index(f(a), f(b)),
125 Copy(a) => Copy(f(a)),
126 Slice(a, b, c) => Slice(f(a), f(b), f(c)),
127 Contains(a, b) => Contains(f(a), f(b)),
128 }
129 }
130
131 /// Ops whose evaluation can never raise a runtime error, given total
132 /// children. Div/Mod (zero divisor), Index/Slice (bounds), and Opaque
133 /// (arbitrary effects) are the non-total ones — a rewrite may only
134 /// DELETE a subterm whose whole tree is total, or it would erase the
135 /// program's error/effect behavior. Len/Copy/Contains raise KIND
136 /// errors over non-collections, so they are non-total HERE and earn
137 /// totality through class facts in
138 /// [`super::CompilerEGraph::provably_total`].
139 pub fn op_is_total(&self) -> bool {
140 !matches!(
141 self,
142 CompilerENode::Div(..)
143 | CompilerENode::Mod(..)
144 | CompilerENode::Index(..)
145 | CompilerENode::Slice(..)
146 | CompilerENode::Len(..)
147 | CompilerENode::Copy(..)
148 | CompilerENode::Contains(..)
149 | CompilerENode::Opaque(_)
150 )
151 }
152}