egglog 11.2.0__cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of egglog might be problematic. Click here for more details.
- egglog/__init__.py +13 -0
- egglog/bindings.cpython-314-x86_64-linux-gnu.so +0 -0
- egglog/bindings.pyi +734 -0
- egglog/builtins.py +1133 -0
- egglog/config.py +8 -0
- egglog/conversion.py +286 -0
- egglog/declarations.py +912 -0
- egglog/deconstruct.py +173 -0
- egglog/egraph.py +1875 -0
- egglog/egraph_state.py +680 -0
- egglog/examples/README.rst +5 -0
- egglog/examples/__init__.py +3 -0
- egglog/examples/bignum.py +32 -0
- egglog/examples/bool.py +38 -0
- egglog/examples/eqsat_basic.py +44 -0
- egglog/examples/fib.py +28 -0
- egglog/examples/higher_order_functions.py +42 -0
- egglog/examples/jointree.py +67 -0
- egglog/examples/lambda_.py +287 -0
- egglog/examples/matrix.py +175 -0
- egglog/examples/multiset.py +60 -0
- egglog/examples/ndarrays.py +144 -0
- egglog/examples/resolution.py +84 -0
- egglog/examples/schedule_demo.py +34 -0
- egglog/exp/__init__.py +3 -0
- egglog/exp/array_api.py +2019 -0
- egglog/exp/array_api_jit.py +51 -0
- egglog/exp/array_api_loopnest.py +74 -0
- egglog/exp/array_api_numba.py +69 -0
- egglog/exp/array_api_program_gen.py +510 -0
- egglog/exp/program_gen.py +425 -0
- egglog/exp/siu_examples.py +32 -0
- egglog/ipython_magic.py +41 -0
- egglog/pretty.py +509 -0
- egglog/py.typed +0 -0
- egglog/runtime.py +712 -0
- egglog/thunk.py +97 -0
- egglog/type_constraint_solver.py +113 -0
- egglog/version_compat.py +87 -0
- egglog/visualizer.css +1 -0
- egglog/visualizer.js +35777 -0
- egglog/visualizer_widget.py +39 -0
- egglog-11.2.0.dist-info/METADATA +74 -0
- egglog-11.2.0.dist-info/RECORD +46 -0
- egglog-11.2.0.dist-info/WHEEL +4 -0
- egglog-11.2.0.dist-info/licenses/LICENSE +21 -0
egglog/egraph_state.py
ADDED
|
@@ -0,0 +1,680 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Implement conversion to/from egglog.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import re
|
|
8
|
+
from collections import defaultdict
|
|
9
|
+
from dataclasses import dataclass, field, replace
|
|
10
|
+
from typing import TYPE_CHECKING, Literal, overload
|
|
11
|
+
|
|
12
|
+
from typing_extensions import assert_never
|
|
13
|
+
|
|
14
|
+
from . import bindings
|
|
15
|
+
from .declarations import *
|
|
16
|
+
from .declarations import ConstructorDecl
|
|
17
|
+
from .pretty import *
|
|
18
|
+
from .type_constraint_solver import *
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from collections.abc import Iterable
|
|
22
|
+
|
|
23
|
+
__all__ = ["GLOBAL_PY_OBJECT_SORT", "EGraphState", "span"]
|
|
24
|
+
|
|
25
|
+
# Create a global sort for python objects, so we can store them without an e-graph instance
|
|
26
|
+
# Needed when serializing commands to egg commands when creating modules
|
|
27
|
+
GLOBAL_PY_OBJECT_SORT = bindings.PyObjectSort()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def span(frame_index: int = 0) -> bindings.RustSpan:
|
|
31
|
+
"""
|
|
32
|
+
Returns a span for the current file and line.
|
|
33
|
+
|
|
34
|
+
If `frame_index` is passed, it will return the span for that frame in the stack, where 0 is the current frame
|
|
35
|
+
this is called in and 1 is the parent.
|
|
36
|
+
"""
|
|
37
|
+
# Currently disable this because it's too expensive.
|
|
38
|
+
# import inspect
|
|
39
|
+
|
|
40
|
+
# frame = inspect.stack()[frame_index + 1]
|
|
41
|
+
return bindings.RustSpan("", 0, 0)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass
|
|
45
|
+
class EGraphState:
|
|
46
|
+
"""
|
|
47
|
+
State of the EGraph declerations and rulesets, so when we pop/push the stack we know whats defined.
|
|
48
|
+
|
|
49
|
+
Used for converting to/from egg and for pretty printing.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
egraph: bindings.EGraph
|
|
53
|
+
# The decleratons we have added.
|
|
54
|
+
__egg_decls__: Declarations = field(default_factory=Declarations)
|
|
55
|
+
# Mapping of added rulesets to the added rules
|
|
56
|
+
rulesets: dict[str, set[RewriteOrRuleDecl]] = field(default_factory=dict)
|
|
57
|
+
|
|
58
|
+
# Bidirectional mapping between egg function names and python callable references.
|
|
59
|
+
# Note that there are possibly mutliple callable references for a single egg function name, like `+`
|
|
60
|
+
# for both int and rational classes.
|
|
61
|
+
egg_fn_to_callable_refs: dict[str, set[CallableRef]] = field(
|
|
62
|
+
default_factory=lambda: defaultdict(set, {"!=": {FunctionRef("!=")}})
|
|
63
|
+
)
|
|
64
|
+
callable_ref_to_egg_fn: dict[CallableRef, tuple[str, bool]] = field(
|
|
65
|
+
default_factory=lambda: {FunctionRef("!="): ("!=", False)}
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# Bidirectional mapping between egg sort names and python type references.
|
|
69
|
+
type_ref_to_egg_sort: dict[JustTypeRef, str] = field(default_factory=dict)
|
|
70
|
+
|
|
71
|
+
# Cache of egg expressions for converting to egg
|
|
72
|
+
expr_to_egg_cache: dict[ExprDecl, bindings._Expr] = field(default_factory=dict)
|
|
73
|
+
|
|
74
|
+
# Callables which have cost tables associated with them
|
|
75
|
+
cost_callables: set[CallableRef] = field(default_factory=set)
|
|
76
|
+
|
|
77
|
+
def copy(self) -> EGraphState:
|
|
78
|
+
"""
|
|
79
|
+
Returns a copy of the state. Th egraph reference is kept the same. Used for pushing/popping.
|
|
80
|
+
"""
|
|
81
|
+
return EGraphState(
|
|
82
|
+
egraph=self.egraph,
|
|
83
|
+
__egg_decls__=self.__egg_decls__.copy(),
|
|
84
|
+
rulesets={k: v.copy() for k, v in self.rulesets.items()},
|
|
85
|
+
egg_fn_to_callable_refs=defaultdict(set, {k: v.copy() for k, v in self.egg_fn_to_callable_refs.items()}),
|
|
86
|
+
callable_ref_to_egg_fn=self.callable_ref_to_egg_fn.copy(),
|
|
87
|
+
type_ref_to_egg_sort=self.type_ref_to_egg_sort.copy(),
|
|
88
|
+
expr_to_egg_cache=self.expr_to_egg_cache.copy(),
|
|
89
|
+
cost_callables=self.cost_callables.copy(),
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def schedule_to_egg(self, schedule: ScheduleDecl) -> bindings._Schedule:
|
|
93
|
+
match schedule:
|
|
94
|
+
case SaturateDecl(schedule):
|
|
95
|
+
return bindings.Saturate(span(), self.schedule_to_egg(schedule))
|
|
96
|
+
case RepeatDecl(schedule, times):
|
|
97
|
+
return bindings.Repeat(span(), times, self.schedule_to_egg(schedule))
|
|
98
|
+
case SequenceDecl(schedules):
|
|
99
|
+
return bindings.Sequence(span(), [self.schedule_to_egg(s) for s in schedules])
|
|
100
|
+
case RunDecl(ruleset_name, until):
|
|
101
|
+
self.ruleset_to_egg(ruleset_name)
|
|
102
|
+
config = bindings.RunConfig(ruleset_name, None if not until else list(map(self.fact_to_egg, until)))
|
|
103
|
+
return bindings.Run(span(), config)
|
|
104
|
+
case _:
|
|
105
|
+
assert_never(schedule)
|
|
106
|
+
|
|
107
|
+
def ruleset_to_egg(self, name: str) -> None:
|
|
108
|
+
"""
|
|
109
|
+
Registers a ruleset if it's not already registered.
|
|
110
|
+
"""
|
|
111
|
+
match self.__egg_decls__._rulesets[name]:
|
|
112
|
+
case RulesetDecl(rules):
|
|
113
|
+
if name not in self.rulesets:
|
|
114
|
+
if name:
|
|
115
|
+
self.egraph.run_program(bindings.AddRuleset(span(), name))
|
|
116
|
+
added_rules = self.rulesets[name] = set()
|
|
117
|
+
else:
|
|
118
|
+
added_rules = self.rulesets[name]
|
|
119
|
+
for rule in rules:
|
|
120
|
+
if rule in added_rules:
|
|
121
|
+
continue
|
|
122
|
+
cmd = self.command_to_egg(rule, name)
|
|
123
|
+
if cmd is not None:
|
|
124
|
+
self.egraph.run_program(cmd)
|
|
125
|
+
added_rules.add(rule)
|
|
126
|
+
case CombinedRulesetDecl(rulesets):
|
|
127
|
+
if name in self.rulesets:
|
|
128
|
+
return
|
|
129
|
+
self.rulesets[name] = set()
|
|
130
|
+
for ruleset in rulesets:
|
|
131
|
+
self.ruleset_to_egg(ruleset)
|
|
132
|
+
self.egraph.run_program(bindings.UnstableCombinedRuleset(span(), name, list(rulesets)))
|
|
133
|
+
|
|
134
|
+
def command_to_egg(self, cmd: CommandDecl, ruleset: str) -> bindings._Command | None:
|
|
135
|
+
match cmd:
|
|
136
|
+
case ActionCommandDecl(action):
|
|
137
|
+
action_egg = self.action_to_egg(action, expr_to_let=True)
|
|
138
|
+
if not action_egg:
|
|
139
|
+
return None
|
|
140
|
+
return bindings.ActionCommand(action_egg)
|
|
141
|
+
case RewriteDecl(tp, lhs, rhs, conditions) | BiRewriteDecl(tp, lhs, rhs, conditions):
|
|
142
|
+
self.type_ref_to_egg(tp)
|
|
143
|
+
rewrite = bindings.Rewrite(
|
|
144
|
+
span(),
|
|
145
|
+
self._expr_to_egg(lhs),
|
|
146
|
+
self._expr_to_egg(rhs),
|
|
147
|
+
[self.fact_to_egg(c) for c in conditions],
|
|
148
|
+
)
|
|
149
|
+
return (
|
|
150
|
+
bindings.RewriteCommand(ruleset, rewrite, cmd.subsume)
|
|
151
|
+
if isinstance(cmd, RewriteDecl)
|
|
152
|
+
else bindings.BiRewriteCommand(ruleset, rewrite)
|
|
153
|
+
)
|
|
154
|
+
case RuleDecl(head, body, name):
|
|
155
|
+
rule = bindings.Rule(
|
|
156
|
+
span(),
|
|
157
|
+
[self.action_to_egg(a) for a in head],
|
|
158
|
+
[self.fact_to_egg(f) for f in body],
|
|
159
|
+
)
|
|
160
|
+
return bindings.RuleCommand(name or "", ruleset, rule)
|
|
161
|
+
# TODO: Replace with just constants value and looking at REF of function
|
|
162
|
+
case DefaultRewriteDecl(ref, expr, subsume):
|
|
163
|
+
sig = self.__egg_decls__.get_callable_decl(ref).signature
|
|
164
|
+
assert isinstance(sig, FunctionSignature)
|
|
165
|
+
# Replace args with rule_var_name mapping
|
|
166
|
+
arg_mapping = tuple(
|
|
167
|
+
TypedExprDecl(tp.to_just(), UnboundVarDecl(name))
|
|
168
|
+
for name, tp in zip(sig.arg_names, sig.arg_types, strict=False)
|
|
169
|
+
)
|
|
170
|
+
rewrite_decl = RewriteDecl(
|
|
171
|
+
sig.semantic_return_type.to_just(), CallDecl(ref, arg_mapping), expr, (), subsume
|
|
172
|
+
)
|
|
173
|
+
return self.command_to_egg(rewrite_decl, ruleset)
|
|
174
|
+
case _:
|
|
175
|
+
assert_never(cmd)
|
|
176
|
+
|
|
177
|
+
@overload
|
|
178
|
+
def action_to_egg(self, action: ActionDecl) -> bindings._Action: ...
|
|
179
|
+
|
|
180
|
+
@overload
|
|
181
|
+
def action_to_egg(self, action: ActionDecl, expr_to_let: Literal[True] = ...) -> bindings._Action | None: ...
|
|
182
|
+
|
|
183
|
+
def action_to_egg(self, action: ActionDecl, expr_to_let: bool = False) -> bindings._Action | None: # noqa: C901, PLR0911, PLR0912
|
|
184
|
+
match action:
|
|
185
|
+
case LetDecl(name, typed_expr):
|
|
186
|
+
var_decl = LetRefDecl(name)
|
|
187
|
+
var_egg = self._expr_to_egg(var_decl)
|
|
188
|
+
self.expr_to_egg_cache[var_decl] = var_egg
|
|
189
|
+
return bindings.Let(span(), var_egg.name, self.typed_expr_to_egg(typed_expr))
|
|
190
|
+
case SetDecl(tp, call, rhs):
|
|
191
|
+
self.type_ref_to_egg(tp)
|
|
192
|
+
call_ = self._expr_to_egg(call)
|
|
193
|
+
return bindings.Set(span(), call_.name, call_.args, self._expr_to_egg(rhs))
|
|
194
|
+
case ExprActionDecl(typed_expr):
|
|
195
|
+
if expr_to_let:
|
|
196
|
+
maybe_typed_expr = self._transform_let(typed_expr)
|
|
197
|
+
if maybe_typed_expr:
|
|
198
|
+
typed_expr = maybe_typed_expr
|
|
199
|
+
else:
|
|
200
|
+
return None
|
|
201
|
+
return bindings.Expr_(span(), self.typed_expr_to_egg(typed_expr))
|
|
202
|
+
case ChangeDecl(tp, call, change):
|
|
203
|
+
self.type_ref_to_egg(tp)
|
|
204
|
+
call_ = self._expr_to_egg(call)
|
|
205
|
+
egg_change: bindings._Change
|
|
206
|
+
match change:
|
|
207
|
+
case "delete":
|
|
208
|
+
egg_change = bindings.Delete()
|
|
209
|
+
case "subsume":
|
|
210
|
+
egg_change = bindings.Subsume()
|
|
211
|
+
case _:
|
|
212
|
+
assert_never(change)
|
|
213
|
+
return bindings.Change(span(), egg_change, call_.name, call_.args)
|
|
214
|
+
case UnionDecl(tp, lhs, rhs):
|
|
215
|
+
self.type_ref_to_egg(tp)
|
|
216
|
+
return bindings.Union(span(), self._expr_to_egg(lhs), self._expr_to_egg(rhs))
|
|
217
|
+
case PanicDecl(name):
|
|
218
|
+
return bindings.Panic(span(), name)
|
|
219
|
+
case SetCostDecl(tp, expr, cost):
|
|
220
|
+
self.type_ref_to_egg(tp)
|
|
221
|
+
cost_table = self.create_cost_table(expr.callable)
|
|
222
|
+
args_egg = [self.typed_expr_to_egg(x, False) for x in expr.args]
|
|
223
|
+
return bindings.Set(span(), cost_table, args_egg, self._expr_to_egg(cost))
|
|
224
|
+
case _:
|
|
225
|
+
assert_never(action)
|
|
226
|
+
|
|
227
|
+
def create_cost_table(self, ref: CallableRef) -> str:
|
|
228
|
+
"""
|
|
229
|
+
Creates the egg cost table if needed and gets the name of the table.
|
|
230
|
+
"""
|
|
231
|
+
name = self.cost_table_name(ref)
|
|
232
|
+
if ref not in self.cost_callables:
|
|
233
|
+
self.cost_callables.add(ref)
|
|
234
|
+
signature = self.__egg_decls__.get_callable_decl(ref).signature
|
|
235
|
+
assert isinstance(signature, FunctionSignature), "Can only add cost tables for functions"
|
|
236
|
+
signature = replace(signature, return_type=TypeRefWithVars("i64"))
|
|
237
|
+
self.egraph.run_program(
|
|
238
|
+
bindings.FunctionCommand(span(), name, self._signature_to_egg_schema(signature), None)
|
|
239
|
+
)
|
|
240
|
+
return name
|
|
241
|
+
|
|
242
|
+
def cost_table_name(self, ref: CallableRef) -> str:
|
|
243
|
+
return f"cost_table_{self.callable_ref_to_egg(ref)[0]}"
|
|
244
|
+
|
|
245
|
+
def fact_to_egg(self, fact: FactDecl) -> bindings._Fact:
|
|
246
|
+
match fact:
|
|
247
|
+
case EqDecl(tp, left, right):
|
|
248
|
+
self.type_ref_to_egg(tp)
|
|
249
|
+
return bindings.Eq(span(), self._expr_to_egg(left), self._expr_to_egg(right))
|
|
250
|
+
case ExprFactDecl(typed_expr):
|
|
251
|
+
return bindings.Fact(self.typed_expr_to_egg(typed_expr, False))
|
|
252
|
+
case _:
|
|
253
|
+
assert_never(fact)
|
|
254
|
+
|
|
255
|
+
def callable_ref_to_egg(self, ref: CallableRef) -> tuple[str, bool]: # noqa: C901, PLR0912
|
|
256
|
+
"""
|
|
257
|
+
Returns the egg function name for a callable reference, registering it if it is not already registered.
|
|
258
|
+
|
|
259
|
+
Also returns whether the args should be reversed
|
|
260
|
+
"""
|
|
261
|
+
if ref in self.callable_ref_to_egg_fn:
|
|
262
|
+
return self.callable_ref_to_egg_fn[ref]
|
|
263
|
+
decl = self.__egg_decls__.get_callable_decl(ref)
|
|
264
|
+
egg_name = decl.egg_name or _sanitize_egg_ident(self._generate_callable_egg_name(ref))
|
|
265
|
+
self.egg_fn_to_callable_refs[egg_name].add(ref)
|
|
266
|
+
reverse_args = False
|
|
267
|
+
match decl:
|
|
268
|
+
case RelationDecl(arg_types, _, _):
|
|
269
|
+
self.egraph.run_program(
|
|
270
|
+
bindings.Relation(span(), egg_name, [self.type_ref_to_egg(a) for a in arg_types])
|
|
271
|
+
)
|
|
272
|
+
case ConstantDecl(tp, _):
|
|
273
|
+
# Use constructor decleration instead of constant b/c constants cannot be extracted
|
|
274
|
+
# https://github.com/egraphs-good/egglog/issues/334
|
|
275
|
+
is_function = self.__egg_decls__._classes[tp.name].builtin
|
|
276
|
+
schema = bindings.Schema([], self.type_ref_to_egg(tp))
|
|
277
|
+
if is_function:
|
|
278
|
+
self.egraph.run_program(bindings.FunctionCommand(span(), egg_name, schema, None))
|
|
279
|
+
else:
|
|
280
|
+
self.egraph.run_program(bindings.Constructor(span(), egg_name, schema, None, False))
|
|
281
|
+
case FunctionDecl(signature, builtin, _, merge):
|
|
282
|
+
if isinstance(signature, FunctionSignature):
|
|
283
|
+
reverse_args = signature.reverse_args
|
|
284
|
+
if not builtin:
|
|
285
|
+
assert isinstance(signature, FunctionSignature), "Cannot turn special function to egg"
|
|
286
|
+
# Compile functions that return unit to relations, because these show up in methods where you
|
|
287
|
+
# cant use the relation helper
|
|
288
|
+
schema = self._signature_to_egg_schema(signature)
|
|
289
|
+
if signature.return_type == TypeRefWithVars("Unit"):
|
|
290
|
+
if merge:
|
|
291
|
+
msg = "Cannot specify a merge function for a function that returns unit"
|
|
292
|
+
raise ValueError(msg)
|
|
293
|
+
self.egraph.run_program(bindings.Relation(span(), egg_name, schema.input))
|
|
294
|
+
else:
|
|
295
|
+
self.egraph.run_program(
|
|
296
|
+
bindings.FunctionCommand(
|
|
297
|
+
span(),
|
|
298
|
+
egg_name,
|
|
299
|
+
self._signature_to_egg_schema(signature),
|
|
300
|
+
self._expr_to_egg(merge) if merge else None,
|
|
301
|
+
)
|
|
302
|
+
)
|
|
303
|
+
case ConstructorDecl(signature, _, cost, unextractable):
|
|
304
|
+
self.egraph.run_program(
|
|
305
|
+
bindings.Constructor(
|
|
306
|
+
span(),
|
|
307
|
+
egg_name,
|
|
308
|
+
self._signature_to_egg_schema(signature),
|
|
309
|
+
cost,
|
|
310
|
+
unextractable,
|
|
311
|
+
)
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
case _:
|
|
315
|
+
assert_never(decl)
|
|
316
|
+
self.callable_ref_to_egg_fn[ref] = egg_name, reverse_args
|
|
317
|
+
return egg_name, reverse_args
|
|
318
|
+
|
|
319
|
+
def _signature_to_egg_schema(self, signature: FunctionSignature) -> bindings.Schema:
|
|
320
|
+
return bindings.Schema(
|
|
321
|
+
[self.type_ref_to_egg(a.to_just()) for a in signature.arg_types],
|
|
322
|
+
self.type_ref_to_egg(signature.semantic_return_type.to_just()),
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
def type_ref_to_egg(self, ref: JustTypeRef) -> str: # noqa: C901, PLR0912
|
|
326
|
+
"""
|
|
327
|
+
Returns the egg sort name for a type reference, registering it if it is not already registered.
|
|
328
|
+
"""
|
|
329
|
+
try:
|
|
330
|
+
return self.type_ref_to_egg_sort[ref]
|
|
331
|
+
except KeyError:
|
|
332
|
+
pass
|
|
333
|
+
decl = self.__egg_decls__._classes[ref.name]
|
|
334
|
+
self.type_ref_to_egg_sort[ref] = egg_name = decl.egg_name or _generate_type_egg_name(ref)
|
|
335
|
+
if not decl.builtin or ref.args:
|
|
336
|
+
if ref.args:
|
|
337
|
+
if ref.name == "UnstableFn":
|
|
338
|
+
# UnstableFn is a special case, where the rest of args are collected into a call
|
|
339
|
+
type_args: list[bindings._Expr] = [
|
|
340
|
+
bindings.Call(
|
|
341
|
+
span(),
|
|
342
|
+
self.type_ref_to_egg(ref.args[1]),
|
|
343
|
+
[bindings.Var(span(), self.type_ref_to_egg(a)) for a in ref.args[2:]],
|
|
344
|
+
),
|
|
345
|
+
bindings.Var(span(), self.type_ref_to_egg(ref.args[0])),
|
|
346
|
+
]
|
|
347
|
+
else:
|
|
348
|
+
# If any of methods have another type ref in them process all those first with substituted vars
|
|
349
|
+
# so that things like multiset - mapp will be added. Function type must be added first.
|
|
350
|
+
# Find all args of all methods and find any with type args themselves that are not this type and add them
|
|
351
|
+
tcs = TypeConstraintSolver(self.__egg_decls__)
|
|
352
|
+
tcs.bind_class(ref)
|
|
353
|
+
for method in decl.methods.values():
|
|
354
|
+
if not isinstance((signature := method.signature), FunctionSignature):
|
|
355
|
+
continue
|
|
356
|
+
for arg_tp in signature.arg_types:
|
|
357
|
+
if isinstance(arg_tp, TypeRefWithVars) and arg_tp.args and arg_tp.name != ref.name:
|
|
358
|
+
self.type_ref_to_egg(tcs.substitute_typevars(arg_tp, ref.name))
|
|
359
|
+
|
|
360
|
+
type_args = [bindings.Var(span(), self.type_ref_to_egg(a)) for a in ref.args]
|
|
361
|
+
args = (self.type_ref_to_egg(JustTypeRef(ref.name)), type_args)
|
|
362
|
+
else:
|
|
363
|
+
args = None
|
|
364
|
+
self.egraph.run_program(bindings.Sort(span(), egg_name, args))
|
|
365
|
+
# For builtin classes, let's also make sure we have the mapping of all egg fn names for class methods, because
|
|
366
|
+
# these can be created even without adding them to the e-graph, like `vec-empty` which can be extracted
|
|
367
|
+
# even if you never use that function.
|
|
368
|
+
if decl.builtin:
|
|
369
|
+
for method_name in decl.class_methods:
|
|
370
|
+
self.callable_ref_to_egg(ClassMethodRef(ref.name, method_name))
|
|
371
|
+
if decl.init:
|
|
372
|
+
self.callable_ref_to_egg(InitRef(ref.name))
|
|
373
|
+
|
|
374
|
+
return egg_name
|
|
375
|
+
|
|
376
|
+
def op_mapping(self) -> dict[str, str]:
|
|
377
|
+
"""
|
|
378
|
+
Create a mapping of egglog function name to Python function name, for use in the serialized format
|
|
379
|
+
for better visualization.
|
|
380
|
+
|
|
381
|
+
Includes cost tables
|
|
382
|
+
"""
|
|
383
|
+
return {
|
|
384
|
+
k: pretty_callable_ref(self.__egg_decls__, next(iter(v)))
|
|
385
|
+
for k, v in self.egg_fn_to_callable_refs.items()
|
|
386
|
+
if len(v) == 1
|
|
387
|
+
} | {
|
|
388
|
+
self.cost_table_name(ref): f"cost({pretty_callable_ref(self.__egg_decls__, ref, include_all_args=True)})"
|
|
389
|
+
for ref in self.cost_callables
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
def possible_egglog_functions(self, names: list[str]) -> Iterable[str]:
|
|
393
|
+
"""
|
|
394
|
+
Given a list of egglog functions, returns all the possible Python function strings
|
|
395
|
+
"""
|
|
396
|
+
for name in names:
|
|
397
|
+
for c in self.egg_fn_to_callable_refs[name]:
|
|
398
|
+
yield pretty_callable_ref(self.__egg_decls__, c)
|
|
399
|
+
|
|
400
|
+
def typed_expr_to_egg(self, typed_expr_decl: TypedExprDecl, transform_let: bool = True) -> bindings._Expr:
|
|
401
|
+
# transform all expressions with multiple parents into a let binding, so that less expressions
|
|
402
|
+
# are sent to egglog. Only for performance reasons.
|
|
403
|
+
if transform_let:
|
|
404
|
+
have_multiple_parents = _exprs_multiple_parents(typed_expr_decl)
|
|
405
|
+
for expr in reversed(have_multiple_parents):
|
|
406
|
+
self._transform_let(expr)
|
|
407
|
+
|
|
408
|
+
self.type_ref_to_egg(typed_expr_decl.tp)
|
|
409
|
+
return self._expr_to_egg(typed_expr_decl.expr)
|
|
410
|
+
|
|
411
|
+
def _transform_let(self, typed_expr: TypedExprDecl) -> TypedExprDecl | None:
|
|
412
|
+
"""
|
|
413
|
+
Rewrites this expression as a let binding if it's not already a let binding.
|
|
414
|
+
"""
|
|
415
|
+
# TODO: Replace with counter so that it works with hash collisions and is more stable
|
|
416
|
+
var_decl = LetRefDecl(f"__expr_{hash(typed_expr)}")
|
|
417
|
+
if var_decl in self.expr_to_egg_cache:
|
|
418
|
+
return None
|
|
419
|
+
var_egg = self._expr_to_egg(var_decl)
|
|
420
|
+
cmd = bindings.ActionCommand(bindings.Let(span(), var_egg.name, self.typed_expr_to_egg(typed_expr)))
|
|
421
|
+
try:
|
|
422
|
+
self.egraph.run_program(cmd)
|
|
423
|
+
# errors when creating let bindings for things like `(vec-empty)`
|
|
424
|
+
except bindings.EggSmolError:
|
|
425
|
+
return typed_expr
|
|
426
|
+
self.expr_to_egg_cache[typed_expr.expr] = var_egg
|
|
427
|
+
self.expr_to_egg_cache[var_decl] = var_egg
|
|
428
|
+
return None
|
|
429
|
+
|
|
430
|
+
@overload
|
|
431
|
+
def _expr_to_egg(self, expr_decl: CallDecl) -> bindings.Call: ...
|
|
432
|
+
|
|
433
|
+
@overload
|
|
434
|
+
def _expr_to_egg(self, expr_decl: UnboundVarDecl | LetRefDecl) -> bindings.Var: ...
|
|
435
|
+
|
|
436
|
+
@overload
|
|
437
|
+
def _expr_to_egg(self, expr_decl: ExprDecl) -> bindings._Expr: ...
|
|
438
|
+
|
|
439
|
+
def _expr_to_egg(self, expr_decl: ExprDecl) -> bindings._Expr: # noqa: PLR0912,C901
|
|
440
|
+
"""
|
|
441
|
+
Convert an ExprDecl to an egg expression.
|
|
442
|
+
"""
|
|
443
|
+
try:
|
|
444
|
+
return self.expr_to_egg_cache[expr_decl]
|
|
445
|
+
except KeyError:
|
|
446
|
+
pass
|
|
447
|
+
res: bindings._Expr
|
|
448
|
+
match expr_decl:
|
|
449
|
+
case LetRefDecl(name):
|
|
450
|
+
res = bindings.Var(span(), f"{name}")
|
|
451
|
+
case UnboundVarDecl(name, egg_name):
|
|
452
|
+
res = bindings.Var(span(), egg_name or f"_{name}")
|
|
453
|
+
case LitDecl(value):
|
|
454
|
+
l: bindings._Literal
|
|
455
|
+
match value:
|
|
456
|
+
case None:
|
|
457
|
+
l = bindings.Unit()
|
|
458
|
+
case bool(i):
|
|
459
|
+
l = bindings.Bool(i)
|
|
460
|
+
case int(i):
|
|
461
|
+
l = bindings.Int(i)
|
|
462
|
+
case float(f):
|
|
463
|
+
l = bindings.Float(f)
|
|
464
|
+
case str(s):
|
|
465
|
+
l = bindings.String(s)
|
|
466
|
+
case _:
|
|
467
|
+
assert_never(value)
|
|
468
|
+
res = bindings.Lit(span(), l)
|
|
469
|
+
case CallDecl(ref, args, _):
|
|
470
|
+
egg_fn, reverse_args = self.callable_ref_to_egg(ref)
|
|
471
|
+
egg_args = [self.typed_expr_to_egg(a, False) for a in args]
|
|
472
|
+
if reverse_args:
|
|
473
|
+
egg_args.reverse()
|
|
474
|
+
res = bindings.Call(span(), egg_fn, egg_args)
|
|
475
|
+
case PyObjectDecl(value):
|
|
476
|
+
res = GLOBAL_PY_OBJECT_SORT.store(value)
|
|
477
|
+
case PartialCallDecl(call_decl):
|
|
478
|
+
egg_fn_call = self._expr_to_egg(call_decl)
|
|
479
|
+
res = bindings.Call(
|
|
480
|
+
span(),
|
|
481
|
+
"unstable-fn",
|
|
482
|
+
[bindings.Lit(span(), bindings.String(egg_fn_call.name)), *egg_fn_call.args],
|
|
483
|
+
)
|
|
484
|
+
case _:
|
|
485
|
+
assert_never(expr_decl.expr)
|
|
486
|
+
self.expr_to_egg_cache[expr_decl] = res
|
|
487
|
+
return res
|
|
488
|
+
|
|
489
|
+
def exprs_from_egg(
|
|
490
|
+
self, termdag: bindings.TermDag, terms: list[bindings._Term], tp: JustTypeRef
|
|
491
|
+
) -> Iterable[TypedExprDecl]:
|
|
492
|
+
"""
|
|
493
|
+
Create a function that can convert from an egg term to a typed expr.
|
|
494
|
+
"""
|
|
495
|
+
state = FromEggState(self, termdag)
|
|
496
|
+
return [state.from_expr(tp, term) for term in terms]
|
|
497
|
+
|
|
498
|
+
def _get_possible_types(self, cls_name: str) -> frozenset[JustTypeRef]:
|
|
499
|
+
"""
|
|
500
|
+
Given a class name, returns all possible registered types that it can be.
|
|
501
|
+
"""
|
|
502
|
+
return frozenset(tp for tp in self.type_ref_to_egg_sort if tp.name == cls_name)
|
|
503
|
+
|
|
504
|
+
def _generate_callable_egg_name(self, ref: CallableRef) -> str:
|
|
505
|
+
"""
|
|
506
|
+
Generates a valid egg function name for a callable reference.
|
|
507
|
+
"""
|
|
508
|
+
match ref:
|
|
509
|
+
case FunctionRef(name):
|
|
510
|
+
return name
|
|
511
|
+
|
|
512
|
+
case ConstantRef(name):
|
|
513
|
+
# Prefix to avoid name collisions with local vars
|
|
514
|
+
return f"%{name}"
|
|
515
|
+
case (
|
|
516
|
+
MethodRef(cls_name, name)
|
|
517
|
+
| ClassMethodRef(cls_name, name)
|
|
518
|
+
| ClassVariableRef(cls_name, name)
|
|
519
|
+
| PropertyRef(cls_name, name)
|
|
520
|
+
):
|
|
521
|
+
return f"{cls_name}.{name}"
|
|
522
|
+
case InitRef(cls_name):
|
|
523
|
+
return f"{cls_name}.__init__"
|
|
524
|
+
case UnnamedFunctionRef(args, val):
|
|
525
|
+
parts = [str(self._expr_to_egg(a.expr)) + "-" + str(self.type_ref_to_egg(a.tp)) for a in args] + [
|
|
526
|
+
str(self.typed_expr_to_egg(val, False))
|
|
527
|
+
]
|
|
528
|
+
return "_".join(parts)
|
|
529
|
+
case _:
|
|
530
|
+
assert_never(ref)
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
# https://chatgpt.com/share/9ab899b4-4e17-4426-a3f2-79d67a5ec456
|
|
534
|
+
_EGGLOG_INVALID_IDENT = re.compile(r"[^\w\-+*/?!=<>&|^/%]")
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
def _sanitize_egg_ident(input_string: str) -> str:
|
|
538
|
+
"""
|
|
539
|
+
Replaces all invalid characters in an egg identifier with an underscore.
|
|
540
|
+
"""
|
|
541
|
+
return _EGGLOG_INVALID_IDENT.sub("_", input_string)
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
def _exprs_multiple_parents(typed_expr: TypedExprDecl) -> list[TypedExprDecl]:
|
|
545
|
+
"""
|
|
546
|
+
Returns all expressions that have multiple parents (a list but semantically just an ordered set).
|
|
547
|
+
"""
|
|
548
|
+
to_traverse = {typed_expr}
|
|
549
|
+
traversed = set[TypedExprDecl]()
|
|
550
|
+
traversed_twice = list[TypedExprDecl]()
|
|
551
|
+
while to_traverse:
|
|
552
|
+
typed_expr = to_traverse.pop()
|
|
553
|
+
if typed_expr in traversed:
|
|
554
|
+
traversed_twice.append(typed_expr)
|
|
555
|
+
continue
|
|
556
|
+
traversed.add(typed_expr)
|
|
557
|
+
expr = typed_expr.expr
|
|
558
|
+
if isinstance(expr, CallDecl):
|
|
559
|
+
to_traverse.update(expr.args)
|
|
560
|
+
elif isinstance(expr, PartialCallDecl):
|
|
561
|
+
to_traverse.update(expr.call.args)
|
|
562
|
+
return traversed_twice
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
def _generate_type_egg_name(ref: JustTypeRef) -> str:
|
|
566
|
+
"""
|
|
567
|
+
Generates an egg sort name for this type reference by linearizing the type.
|
|
568
|
+
"""
|
|
569
|
+
name = ref.name
|
|
570
|
+
if not ref.args:
|
|
571
|
+
return name
|
|
572
|
+
return f"{name}_{'_'.join(map(_generate_type_egg_name, ref.args))}"
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
@dataclass
|
|
576
|
+
class FromEggState:
|
|
577
|
+
"""
|
|
578
|
+
Dataclass containing state used when converting from an egg term to a typed expr.
|
|
579
|
+
"""
|
|
580
|
+
|
|
581
|
+
state: EGraphState
|
|
582
|
+
termdag: bindings.TermDag
|
|
583
|
+
# Cache of termdag ID to TypedExprDecl
|
|
584
|
+
cache: dict[int, TypedExprDecl] = field(default_factory=dict)
|
|
585
|
+
|
|
586
|
+
@property
|
|
587
|
+
def decls(self) -> Declarations:
|
|
588
|
+
return self.state.__egg_decls__
|
|
589
|
+
|
|
590
|
+
def from_expr(self, tp: JustTypeRef, term: bindings._Term) -> TypedExprDecl:
|
|
591
|
+
"""
|
|
592
|
+
Convert an egg term to a typed expr.
|
|
593
|
+
"""
|
|
594
|
+
expr_decl: ExprDecl
|
|
595
|
+
if isinstance(term, bindings.TermVar):
|
|
596
|
+
expr_decl = LetRefDecl(term.name)
|
|
597
|
+
elif isinstance(term, bindings.TermLit):
|
|
598
|
+
value = term.value
|
|
599
|
+
expr_decl = LitDecl(None if isinstance(value, bindings.Unit) else value.value)
|
|
600
|
+
elif isinstance(term, bindings.TermApp):
|
|
601
|
+
if term.name == "py-object":
|
|
602
|
+
call = self.termdag.term_to_expr(term, span())
|
|
603
|
+
expr_decl = PyObjectDecl(GLOBAL_PY_OBJECT_SORT.load(call))
|
|
604
|
+
elif term.name == "unstable-fn":
|
|
605
|
+
# Get function name
|
|
606
|
+
fn_term, *arg_terms = term.args
|
|
607
|
+
fn_value = self.resolve_term(fn_term, JustTypeRef("String"))
|
|
608
|
+
assert isinstance(fn_value.expr, LitDecl)
|
|
609
|
+
fn_name = fn_value.expr.value
|
|
610
|
+
assert isinstance(fn_name, str)
|
|
611
|
+
|
|
612
|
+
# Resolve what types the partially applied args are
|
|
613
|
+
assert tp.name == "UnstableFn"
|
|
614
|
+
call_decl = self.from_call(tp.args[0], bindings.TermApp(fn_name, arg_terms))
|
|
615
|
+
expr_decl = PartialCallDecl(call_decl)
|
|
616
|
+
else:
|
|
617
|
+
expr_decl = self.from_call(tp, term)
|
|
618
|
+
else:
|
|
619
|
+
assert_never(term)
|
|
620
|
+
return TypedExprDecl(tp, expr_decl)
|
|
621
|
+
|
|
622
|
+
def from_call(
|
|
623
|
+
self,
|
|
624
|
+
tp: JustTypeRef,
|
|
625
|
+
term: bindings.TermApp, # additional_arg_tps: tuple[JustTypeRef, ...]
|
|
626
|
+
) -> CallDecl:
|
|
627
|
+
"""
|
|
628
|
+
Convert a call to a CallDecl.
|
|
629
|
+
|
|
630
|
+
There could be Python call refs which match the call, so we need to find the correct one.
|
|
631
|
+
|
|
632
|
+
The additional_arg_tps are known types for arguments that come after the term args, used to infer types
|
|
633
|
+
for partially applied functions, where we know the types of the later args, but not of the earlier ones where
|
|
634
|
+
we have values for.
|
|
635
|
+
"""
|
|
636
|
+
# Find the first callable ref that matches the call
|
|
637
|
+
for callable_ref in self.state.egg_fn_to_callable_refs[term.name]:
|
|
638
|
+
# If this is a classmethod, we might need the type params that were bound for this type
|
|
639
|
+
# This could be multiple types if the classmethod is ambiguous, like map create.
|
|
640
|
+
possible_types: Iterable[JustTypeRef | None]
|
|
641
|
+
signature = self.decls.get_callable_decl(callable_ref).signature
|
|
642
|
+
assert isinstance(signature, FunctionSignature)
|
|
643
|
+
if isinstance(callable_ref, ClassMethodRef | InitRef | MethodRef):
|
|
644
|
+
# Need OR in case we have class method whose class whas never added as a sort, which would happen
|
|
645
|
+
# if the class method didn't return that type and no other function did. In this case, we don't need
|
|
646
|
+
# to care about the type vars and we we don't need to bind any possible type.
|
|
647
|
+
possible_types = self.state._get_possible_types(callable_ref.class_name) or [None]
|
|
648
|
+
cls_name = callable_ref.class_name
|
|
649
|
+
else:
|
|
650
|
+
possible_types = [None]
|
|
651
|
+
cls_name = None
|
|
652
|
+
for possible_type in possible_types:
|
|
653
|
+
tcs = TypeConstraintSolver(self.decls)
|
|
654
|
+
if possible_type and possible_type.args:
|
|
655
|
+
tcs.bind_class(possible_type)
|
|
656
|
+
try:
|
|
657
|
+
arg_types, bound_tp_params = tcs.infer_arg_types(
|
|
658
|
+
signature.arg_types, signature.semantic_return_type, signature.var_arg_type, tp, cls_name
|
|
659
|
+
)
|
|
660
|
+
except TypeConstraintError:
|
|
661
|
+
continue
|
|
662
|
+
args = tuple(self.resolve_term(a, tp) for a, tp in zip(term.args, arg_types, strict=False))
|
|
663
|
+
|
|
664
|
+
return CallDecl(
|
|
665
|
+
callable_ref,
|
|
666
|
+
args,
|
|
667
|
+
# Don't include bound type params if this is just a method, we only needed them for type resolution
|
|
668
|
+
# but dont need to store them
|
|
669
|
+
bound_tp_params if isinstance(callable_ref, ClassMethodRef | InitRef) else None,
|
|
670
|
+
)
|
|
671
|
+
raise ValueError(
|
|
672
|
+
f"Could not find callable ref for call {term}. None of these refs matched the types: {self.state.egg_fn_to_callable_refs[term.name]}"
|
|
673
|
+
)
|
|
674
|
+
|
|
675
|
+
def resolve_term(self, term_id: int, tp: JustTypeRef) -> TypedExprDecl:
|
|
676
|
+
try:
|
|
677
|
+
return self.cache[term_id]
|
|
678
|
+
except KeyError:
|
|
679
|
+
res = self.cache[term_id] = self.from_expr(tp, self.termdag.get(term_id))
|
|
680
|
+
return res
|