egglog 12.0.0__cp313-cp313t-manylinux_2_17_ppc64.manylinux2014_ppc64.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.
Files changed (48) hide show
  1. egglog/__init__.py +13 -0
  2. egglog/bindings.cpython-313t-powerpc64-linux-gnu.so +0 -0
  3. egglog/bindings.pyi +887 -0
  4. egglog/builtins.py +1144 -0
  5. egglog/config.py +8 -0
  6. egglog/conversion.py +290 -0
  7. egglog/declarations.py +964 -0
  8. egglog/deconstruct.py +176 -0
  9. egglog/egraph.py +2247 -0
  10. egglog/egraph_state.py +978 -0
  11. egglog/examples/README.rst +5 -0
  12. egglog/examples/__init__.py +3 -0
  13. egglog/examples/bignum.py +32 -0
  14. egglog/examples/bool.py +38 -0
  15. egglog/examples/eqsat_basic.py +44 -0
  16. egglog/examples/fib.py +28 -0
  17. egglog/examples/higher_order_functions.py +42 -0
  18. egglog/examples/jointree.py +64 -0
  19. egglog/examples/lambda_.py +287 -0
  20. egglog/examples/matrix.py +175 -0
  21. egglog/examples/multiset.py +60 -0
  22. egglog/examples/ndarrays.py +144 -0
  23. egglog/examples/resolution.py +84 -0
  24. egglog/examples/schedule_demo.py +34 -0
  25. egglog/exp/MoA.ipynb +617 -0
  26. egglog/exp/__init__.py +3 -0
  27. egglog/exp/any_expr.py +947 -0
  28. egglog/exp/any_expr_example.ipynb +408 -0
  29. egglog/exp/array_api.py +2019 -0
  30. egglog/exp/array_api_jit.py +51 -0
  31. egglog/exp/array_api_loopnest.py +74 -0
  32. egglog/exp/array_api_numba.py +69 -0
  33. egglog/exp/array_api_program_gen.py +510 -0
  34. egglog/exp/program_gen.py +427 -0
  35. egglog/exp/siu_examples.py +32 -0
  36. egglog/ipython_magic.py +41 -0
  37. egglog/pretty.py +566 -0
  38. egglog/py.typed +0 -0
  39. egglog/runtime.py +888 -0
  40. egglog/thunk.py +97 -0
  41. egglog/type_constraint_solver.py +111 -0
  42. egglog/visualizer.css +1 -0
  43. egglog/visualizer.js +35798 -0
  44. egglog/visualizer_widget.py +39 -0
  45. egglog-12.0.0.dist-info/METADATA +93 -0
  46. egglog-12.0.0.dist-info/RECORD +48 -0
  47. egglog-12.0.0.dist-info/WHEEL +5 -0
  48. egglog-12.0.0.dist-info/licenses/LICENSE +21 -0
egglog/egraph.py ADDED
@@ -0,0 +1,2247 @@
1
+ from __future__ import annotations
2
+
3
+ import contextlib
4
+ import inspect
5
+ import pathlib
6
+ import tempfile
7
+ from collections.abc import Callable, Generator, Iterable
8
+ from contextvars import ContextVar, Token
9
+ from dataclasses import InitVar, dataclass, field
10
+ from functools import partial
11
+ from inspect import Parameter, currentframe, getmodule, signature
12
+ from types import FrameType, FunctionType
13
+ from typing import (
14
+ TYPE_CHECKING,
15
+ Any,
16
+ ClassVar,
17
+ Generic,
18
+ Literal,
19
+ Never,
20
+ Protocol,
21
+ Self,
22
+ TypeAlias,
23
+ TypedDict,
24
+ TypeVar,
25
+ assert_never,
26
+ cast,
27
+ get_type_hints,
28
+ overload,
29
+ )
30
+ from uuid import uuid4
31
+ from warnings import warn
32
+
33
+ import graphviz
34
+ from typing_extensions import ParamSpec, Unpack
35
+
36
+ from . import bindings
37
+ from .conversion import *
38
+ from .conversion import convert_to_same_type, resolve_literal
39
+ from .declarations import *
40
+ from .egraph_state import *
41
+ from .ipython_magic import IN_IPYTHON
42
+ from .pretty import pretty_decl
43
+ from .runtime import *
44
+ from .thunk import *
45
+
46
+ if TYPE_CHECKING:
47
+ from .builtins import String, Unit, i64, i64Like
48
+
49
+
50
+ __all__ = [
51
+ "Action",
52
+ "BackOff",
53
+ "BaseExpr",
54
+ "BuiltinExpr",
55
+ "Command",
56
+ "Command",
57
+ "CostModel",
58
+ "EGraph",
59
+ "Expr",
60
+ "ExprCallable",
61
+ "Fact",
62
+ "Fact",
63
+ "GraphvizKwargs",
64
+ "GreedyDagCost",
65
+ "RewriteOrRule",
66
+ "Ruleset",
67
+ "Schedule",
68
+ "_BirewriteBuilder",
69
+ "_EqBuilder",
70
+ "_NeBuilder",
71
+ "_RewriteBuilder",
72
+ "_SetBuilder",
73
+ "_UnionBuilder",
74
+ "back_off",
75
+ "birewrite",
76
+ "check",
77
+ "check_eq",
78
+ "constant",
79
+ "default_cost_model",
80
+ "delete",
81
+ "eq",
82
+ "expr_action",
83
+ "expr_fact",
84
+ "expr_parts",
85
+ "function",
86
+ "get_cost",
87
+ "greedy_dag_cost_model",
88
+ "let",
89
+ "method",
90
+ "ne",
91
+ "panic",
92
+ "relation",
93
+ "rewrite",
94
+ "rule",
95
+ "ruleset",
96
+ "run",
97
+ "seq",
98
+ "set_",
99
+ "set_cost",
100
+ "set_current_ruleset",
101
+ "subsume",
102
+ "union",
103
+ "unstable_combine_rulesets",
104
+ "var",
105
+ "vars_",
106
+ ]
107
+
108
+
109
+ T = TypeVar("T")
110
+ P = ParamSpec("P")
111
+ EXPR_TYPE = TypeVar("EXPR_TYPE", bound="type[Expr]")
112
+ BASE_EXPR_TYPE = TypeVar("BASE_EXPR_TYPE", bound="type[BaseExpr]")
113
+ EXPR = TypeVar("EXPR", bound="Expr")
114
+ BASE_EXPR = TypeVar("BASE_EXPR", bound="BaseExpr")
115
+ BE1 = TypeVar("BE1", bound="BaseExpr")
116
+ BE2 = TypeVar("BE2", bound="BaseExpr")
117
+ BE3 = TypeVar("BE3", bound="BaseExpr")
118
+ BE4 = TypeVar("BE4", bound="BaseExpr")
119
+ # Attributes which are sometimes added to classes by the interpreter or the dataclass decorator, or by ipython.
120
+ # We ignore these when inspecting the class.
121
+
122
+ IGNORED_ATTRIBUTES = {
123
+ "__module__",
124
+ "__doc__",
125
+ "__dict__",
126
+ "__weakref__",
127
+ "__orig_bases__",
128
+ "__annotations__",
129
+ "__qualname__",
130
+ "__firstlineno__",
131
+ "__static_attributes__",
132
+ "__match_args__",
133
+ # Ignore all reflected binary method
134
+ *(f"__r{m[2:]}" for m in NUMERIC_BINARY_METHODS),
135
+ }
136
+
137
+
138
+ def check_eq(x: BASE_EXPR, y: BASE_EXPR, schedule: Schedule | None = None, *, add_second=True, display=False) -> EGraph:
139
+ """
140
+ Verifies that two expressions are equal after running the schedule.
141
+
142
+ If add_second is true, then the second expression is added to the egraph before running the schedule.
143
+ """
144
+ egraph = EGraph()
145
+ x_var = egraph.let("__check_eq_x", x)
146
+ y_var: BASE_EXPR = egraph.let("__check_eq_y", y) if add_second else y
147
+ if schedule:
148
+ try:
149
+ egraph.run(schedule)
150
+ finally:
151
+ if display:
152
+ egraph.display()
153
+ fact = eq(x_var).to(y_var)
154
+ try:
155
+ egraph.check(fact)
156
+ except bindings.EggSmolError as err:
157
+ if display:
158
+ egraph.display()
159
+ err.add_note(f"Failed:\n{eq(x).to(y)}\n\nExtracted:\n {eq(egraph.extract(x)).to(egraph.extract(y))})")
160
+ raise
161
+ return egraph
162
+
163
+
164
+ def check(x: FactLike, schedule: Schedule | None = None, *given: ActionLike) -> None:
165
+ """
166
+ Verifies that the fact is true given some assumptions and after running the schedule.
167
+ """
168
+ egraph = EGraph()
169
+ if given:
170
+ egraph.register(*given)
171
+ if schedule:
172
+ egraph.run(schedule)
173
+ egraph.check(x)
174
+
175
+
176
+ # We seperate the function and method overloads to make it simpler to know if we are modifying a function or method,
177
+ # So that we can add the functions eagerly to the registry and wait on the methods till we process the class.
178
+
179
+
180
+ CALLABLE = TypeVar("CALLABLE", bound=Callable)
181
+ CONSTRUCTOR_CALLABLE = TypeVar("CONSTRUCTOR_CALLABLE", bound=Callable[..., "Expr | None"])
182
+
183
+ EXPR_NONE = TypeVar("EXPR_NONE", bound="Expr | None")
184
+ BASE_EXPR_NONE = TypeVar("BASE_EXPR_NONE", bound="BaseExpr | None")
185
+
186
+
187
+ @overload
188
+ def method(
189
+ *,
190
+ preserve: Literal[True],
191
+ ) -> Callable[[CALLABLE], CALLABLE]: ...
192
+
193
+
194
+ # function wihout merge
195
+ @overload
196
+ def method(
197
+ *,
198
+ egg_fn: str | None = ...,
199
+ reverse_args: bool = ...,
200
+ mutates_self: bool = ...,
201
+ ) -> Callable[[CALLABLE], CALLABLE]: ...
202
+
203
+
204
+ # function
205
+ @overload
206
+ def method(
207
+ *,
208
+ egg_fn: str | None = ...,
209
+ merge: Callable[[BASE_EXPR, BASE_EXPR], BASE_EXPR] | None = ...,
210
+ mutates_self: bool = ...,
211
+ ) -> Callable[[Callable[P, BASE_EXPR]], Callable[P, BASE_EXPR]]: ...
212
+
213
+
214
+ # constructor
215
+ @overload
216
+ def method(
217
+ *,
218
+ egg_fn: str | None = ...,
219
+ cost: int | None = ...,
220
+ mutates_self: bool = ...,
221
+ unextractable: bool = ...,
222
+ subsume: bool = ...,
223
+ ) -> Callable[[Callable[P, EXPR_NONE]], Callable[P, EXPR_NONE]]: ...
224
+
225
+
226
+ def method(
227
+ *,
228
+ egg_fn: str | None = None,
229
+ cost: int | None = None,
230
+ merge: Callable[[BASE_EXPR, BASE_EXPR], BASE_EXPR] | None = None,
231
+ preserve: bool = False,
232
+ mutates_self: bool = False,
233
+ unextractable: bool = False,
234
+ subsume: bool = False,
235
+ reverse_args: bool = False,
236
+ ) -> Callable[[Callable[P, BASE_EXPR_NONE]], Callable[P, BASE_EXPR_NONE]]:
237
+ """
238
+ Any method can be decorated with this to customize it's behavior. This is only supported in classes which subclass :class:`Expr`.
239
+ """
240
+ merge = cast("Callable[[object, object], object]", merge)
241
+ return lambda fn: _WrappedMethod(
242
+ egg_fn, cost, merge, fn, preserve, mutates_self, unextractable, subsume, reverse_args
243
+ )
244
+
245
+
246
+ @overload
247
+ def function(fn: CALLABLE, /) -> CALLABLE: ...
248
+
249
+
250
+ # function without merge
251
+ @overload
252
+ def function(
253
+ *,
254
+ egg_fn: str | None = ...,
255
+ builtin: bool = ...,
256
+ mutates_first_arg: bool = ...,
257
+ ) -> Callable[[CALLABLE], CALLABLE]: ...
258
+
259
+
260
+ # function
261
+ @overload
262
+ def function(
263
+ *,
264
+ egg_fn: str | None = ...,
265
+ merge: Callable[[BASE_EXPR, BASE_EXPR], BASE_EXPR] | None = ...,
266
+ builtin: bool = ...,
267
+ mutates_first_arg: bool = ...,
268
+ ) -> Callable[[Callable[P, BASE_EXPR]], Callable[P, BASE_EXPR]]: ...
269
+
270
+
271
+ # constructor
272
+ @overload
273
+ def function(
274
+ *,
275
+ egg_fn: str | None = ...,
276
+ cost: int | None = ...,
277
+ mutates_first_arg: bool = ...,
278
+ unextractable: bool = ...,
279
+ ruleset: Ruleset | None = ...,
280
+ subsume: bool = ...,
281
+ ) -> Callable[[CONSTRUCTOR_CALLABLE], CONSTRUCTOR_CALLABLE]: ...
282
+
283
+
284
+ def function(*args, **kwargs) -> Any:
285
+ """
286
+ Decorate a function typing stub to create an egglog function for it.
287
+
288
+ If a body is included, it will be added to the `ruleset` passed in as a default rewrite.
289
+
290
+ This will default to creating a "constructor" in egglog, unless a merge function is passed in or the return
291
+ type is a primtive, then it will be a "function".
292
+ """
293
+ fn_locals = currentframe().f_back.f_locals # type: ignore[union-attr]
294
+
295
+ # If we have any positional args, then we are calling it directly on a function
296
+ if args:
297
+ assert len(args) == 1
298
+ return _FunctionConstructor(fn_locals)(args[0])
299
+ # otherwise, we are passing some keyword args, so save those, and then return a partial
300
+ return _FunctionConstructor(fn_locals, **kwargs)
301
+
302
+
303
+ class _ExprMetaclass(type):
304
+ """
305
+ Metaclass of Expr.
306
+
307
+ Used to override isistance checks, so that runtime expressions are instances of Expr at runtime.
308
+ """
309
+
310
+ def __new__( # type: ignore[misc]
311
+ cls: type[_ExprMetaclass],
312
+ name: str,
313
+ bases: tuple[type, ...],
314
+ namespace: dict[str, Any],
315
+ egg_sort: str | None = None,
316
+ ruleset: Ruleset | None = None,
317
+ ) -> RuntimeClass | type:
318
+ # If this is the Expr subclass, just return the class
319
+ if not bases or bases == (BaseExpr,):
320
+ return super().__new__(cls, name, bases, namespace)
321
+ builtin = BuiltinExpr in bases
322
+ # TODO: Raise error on subclassing or multiple inheritence
323
+
324
+ frame = currentframe()
325
+ assert frame
326
+ prev_frame = frame.f_back
327
+ assert prev_frame
328
+ cls_ident = Ident(name, _get_module(prev_frame))
329
+ # Pass in an instance of the class so that when we are generating the decls
330
+ # we can update them eagerly so that we can access the methods in the class body
331
+ runtime_cls = RuntimeClass(None, TypeRefWithVars(cls_ident)) # type: ignore[arg-type]
332
+
333
+ # Store frame so that we can get live access to updated locals/globals
334
+ # Otherwise, f_locals returns a copy
335
+ # https://peps.python.org/pep-0667/
336
+ runtime_cls.__egg_decls_thunk__ = Thunk.fn(
337
+ _generate_class_decls,
338
+ namespace,
339
+ prev_frame,
340
+ builtin,
341
+ egg_sort,
342
+ cls_ident,
343
+ ruleset,
344
+ runtime_cls,
345
+ )
346
+ return runtime_cls
347
+
348
+ def __instancecheck__(cls, instance: object) -> bool:
349
+ return isinstance(instance, RuntimeExpr)
350
+
351
+
352
+ class BaseExpr(metaclass=_ExprMetaclass):
353
+ """
354
+ Either a builtin or a user defined expression type.
355
+ """
356
+
357
+ # these methods are only provided for type checking
358
+ # The real implementations are in the runtime system
359
+
360
+ def __ne__(self, other: Self) -> Unit: ... # type: ignore[override, empty-body]
361
+
362
+ # not currently dissalowing other types of equality https://github.com/python/typeshed/issues/8217#issuecomment-3140873292
363
+ def __eq__(self, other: Self) -> Fact: ... # type: ignore[override, empty-body]
364
+
365
+ def __replace_expr__(self, new_expr: Self) -> None:
366
+ """
367
+ Replace the current expression with the new expression in place.
368
+ """
369
+
370
+
371
+ class BuiltinExpr(BaseExpr, metaclass=_ExprMetaclass):
372
+ """
373
+ A builtin expr type, not an eqsort.
374
+ """
375
+
376
+
377
+ class Expr(BaseExpr, metaclass=_ExprMetaclass):
378
+ """
379
+ Subclass this to define a custom expression type.
380
+ """
381
+
382
+
383
+ def _generate_class_decls( # noqa: C901,PLR0912
384
+ namespace: dict[str, Any],
385
+ frame: FrameType,
386
+ builtin: bool,
387
+ egg_sort: str | None,
388
+ cls_ident: Ident,
389
+ ruleset: Ruleset | None,
390
+ runtime_cls: RuntimeClass,
391
+ ) -> Declarations:
392
+ """
393
+ Lazy constructor for class declerations to support classes with methods whose types are not yet defined.
394
+ """
395
+ parameters: list[TypeVar] = (
396
+ # Get the generic params from the orig bases generic class
397
+ namespace["__orig_bases__"][1].__parameters__ if "__orig_bases__" in namespace else []
398
+ )
399
+ type_vars = tuple(ClassTypeVarRef.from_type_var(p) for p in parameters)
400
+ del parameters
401
+ cls_decl = ClassDecl(
402
+ egg_sort, type_vars, builtin, match_args=namespace.pop("__match_args__", ()), doc=namespace.pop("__doc__", None)
403
+ )
404
+ decls = Declarations(_classes={cls_ident: cls_decl})
405
+ # Update class think eagerly when resolving so that lookups work in methods
406
+ runtime_cls.__egg_decls_thunk__ = Thunk.value(decls)
407
+
408
+ ##
409
+ # Register class variables
410
+ ##
411
+ # Create a dummy type to pass to get_type_hints to resolve the annotations we have
412
+ _Dummytype = type("_DummyType", (), {"__annotations__": namespace.get("__annotations__", {})})
413
+ for k, v in get_type_hints(_Dummytype, globalns=frame.f_globals, localns=frame.f_locals).items():
414
+ if getattr(v, "__origin__", None) == ClassVar:
415
+ (inner_tp,) = v.__args__
416
+ type_ref = resolve_type_annotation_mutate(decls, inner_tp)
417
+ cls_decl.class_variables[k] = ConstantDecl(type_ref.to_just())
418
+ _add_default_rewrite(
419
+ decls, ClassVariableRef(cls_ident, k), type_ref, namespace.pop(k, None), ruleset, subsume=False
420
+ )
421
+ else:
422
+ msg = f"On class {cls_ident}, for attribute '{k}', expected a ClassVar, but got {v}"
423
+ raise NotImplementedError(msg)
424
+
425
+ ##
426
+ # Register methods, classmethods, preserved methods, and properties
427
+ ##
428
+ # Get all the methods from the class
429
+ filtered_namespace: list[tuple[str, Any]] = [
430
+ (k, v) for k, v in namespace.items() if k not in IGNORED_ATTRIBUTES or isinstance(v, _WrappedMethod)
431
+ ]
432
+
433
+ # all methods we should try adding default functions for
434
+ add_default_funcs: list[Callable[[], None]] = []
435
+ # Then register each of its methods
436
+ for method_name, method in filtered_namespace:
437
+ is_init = method_name == "__init__"
438
+ # Don't register the init methods for literals, since those don't use the type checking mechanisms
439
+ if is_init and cls_ident in LIT_IDENTS:
440
+ continue
441
+ match method:
442
+ case _WrappedMethod(egg_fn, cost, merge, fn, preserve, mutates, unextractable, subsume, reverse_args):
443
+ pass
444
+ case _:
445
+ egg_fn, cost, merge = None, None, None
446
+ fn = method
447
+ unextractable, preserve, subsume = False, False, False
448
+ mutates = method_name in ALWAYS_MUTATES_SELF
449
+ reverse_args = False
450
+ if preserve or method_name in ALWAYS_PRESERVED:
451
+ cls_decl.preserved_methods[method_name] = fn
452
+ continue
453
+ locals = frame.f_locals
454
+ ref: ClassMethodRef | MethodRef | PropertyRef | InitRef
455
+ # TODO: Store deprecated message so we can get at runtime
456
+ if (getattr(fn, "__deprecated__", None)) is not None:
457
+ fn = fn.__wrapped__ # type: ignore[attr-defined]
458
+ match fn:
459
+ case classmethod():
460
+ ref = ClassMethodRef(cls_ident, method_name)
461
+ fn = fn.__func__
462
+ case property():
463
+ ref = PropertyRef(cls_ident, method_name)
464
+ fn = fn.fget
465
+ case _:
466
+ ref = InitRef(cls_ident) if is_init else MethodRef(cls_ident, method_name)
467
+ if isinstance(fn, _WrappedMethod):
468
+ msg = f"{cls_ident}.{method_name} Add the @method(...) decorator above @classmethod or @property"
469
+
470
+ raise ValueError(msg) # noqa: TRY004
471
+ special_function_name: SpecialFunctions | None = (
472
+ "fn-partial" if egg_fn == "unstable-fn" else "fn-app" if egg_fn == "unstable-app" else None
473
+ )
474
+ if special_function_name:
475
+ decl = FunctionDecl(special_function_name, builtin=True, egg_name=egg_fn)
476
+ decls.set_function_decl(ref, decl)
477
+ continue
478
+ try:
479
+ add_rewrite = _fn_decl(
480
+ decls,
481
+ egg_fn,
482
+ ref,
483
+ fn,
484
+ locals,
485
+ cost,
486
+ merge,
487
+ mutates,
488
+ builtin,
489
+ ruleset=ruleset,
490
+ unextractable=unextractable,
491
+ subsume=subsume,
492
+ reverse_args=reverse_args,
493
+ )
494
+ except Exception as e:
495
+ e.add_note(f"Error processing {cls_ident}.{method_name}")
496
+ raise
497
+
498
+ if not builtin and not isinstance(ref, InitRef):
499
+ add_default_funcs.append(add_rewrite)
500
+
501
+ # Add all rewrite methods at the end so that all methods are registered first and can be accessed
502
+ # in the bodies
503
+ for add_rewrite in add_default_funcs:
504
+ add_rewrite()
505
+ return decls
506
+
507
+
508
+ @dataclass
509
+ class _FunctionConstructor:
510
+ hint_locals: dict[str, Any]
511
+ builtin: bool = False
512
+ mutates_first_arg: bool = False
513
+ egg_fn: str | None = None
514
+ cost: int | None = None
515
+ merge: Callable[[object, object], object] | None = None
516
+ unextractable: bool = False
517
+ ruleset: Ruleset | None = None
518
+ subsume: bool = False
519
+
520
+ def __call__(self, fn: Callable) -> RuntimeFunction:
521
+ return RuntimeFunction(*split_thunk(Thunk.fn(self.create_decls, fn)))
522
+
523
+ def create_decls(self, fn: Callable) -> tuple[Declarations, CallableRef]:
524
+ decls = Declarations()
525
+ add_rewrite = _fn_decl(
526
+ decls,
527
+ self.egg_fn,
528
+ ref := FunctionRef(Ident(fn.__name__, fn.__module__)),
529
+ fn,
530
+ self.hint_locals,
531
+ self.cost,
532
+ self.merge,
533
+ self.mutates_first_arg,
534
+ self.builtin,
535
+ ruleset=self.ruleset,
536
+ subsume=self.subsume,
537
+ unextractable=self.unextractable,
538
+ )
539
+ add_rewrite()
540
+ return decls, ref
541
+
542
+
543
+ def _fn_decl(
544
+ decls: Declarations,
545
+ egg_name: str | None,
546
+ ref: FunctionRef | MethodRef | PropertyRef | ClassMethodRef | InitRef,
547
+ fn: object,
548
+ # Pass in the locals, retrieved from the frame when wrapping,
549
+ # so that we support classes and function defined inside of other functions (which won't show up in the globals)
550
+ hint_locals: dict[str, Any],
551
+ cost: int | None,
552
+ merge: Callable[[object, object], object] | None,
553
+ mutates_first_arg: bool,
554
+ is_builtin: bool,
555
+ subsume: bool,
556
+ ruleset: Ruleset | None = None,
557
+ unextractable: bool = False,
558
+ reverse_args: bool = False,
559
+ ) -> Callable[[], None]:
560
+ """
561
+ Sets the function decl for the function object and returns the ref as well as a thunk that sets the default callable.
562
+ """
563
+ if isinstance(fn, RuntimeFunction):
564
+ msg = "Inside of classes, wrap methods with the `method` decorator, not `function`"
565
+ raise ValueError(msg) # noqa: TRY004
566
+ if not isinstance(fn, FunctionType):
567
+ raise NotImplementedError(f"Can only generate function decls for functions not {fn} {type(fn)}")
568
+
569
+ # Instead of passing both globals and locals, just pass the globals. Otherwise, for some reason forward references
570
+ # won't be resolved correctly
571
+ # We need this to be false so it returns "__forward_value__" https://github.com/python/cpython/blob/440ed18e08887b958ad50db1b823e692a747b671/Lib/typing.py#L919
572
+ # https://github.com/egraphs-good/egglog-python/issues/210
573
+ hint_globals = {**fn.__globals__, **hint_locals}
574
+ hints = get_type_hints(fn, hint_globals)
575
+
576
+ params = list(signature(fn).parameters.values())
577
+
578
+ # If this is an init function, or a classmethod, the first arg is not used
579
+ if isinstance(ref, ClassMethodRef | InitRef):
580
+ params = params[1:]
581
+
582
+ if _last_param_variable(params):
583
+ *params, var_arg_param = params
584
+ # For now, we don't use the variable arg name
585
+ var_arg_type = resolve_type_annotation_mutate(decls, hints[var_arg_param.name])
586
+ else:
587
+ var_arg_type = None
588
+ arg_types = tuple(
589
+ decls.get_paramaterized_class(ref.ident)
590
+ if i == 0 and isinstance(ref, MethodRef | PropertyRef)
591
+ else resolve_type_annotation_mutate(decls, hints[t.name])
592
+ for i, t in enumerate(params)
593
+ )
594
+
595
+ # Resolve all default values as arg types
596
+ arg_defaults = [
597
+ resolve_literal(t, p.default, Thunk.value(decls)) if p.default is not Parameter.empty else None
598
+ for (t, p) in zip(arg_types, params, strict=True)
599
+ ]
600
+
601
+ decls.update(*arg_defaults)
602
+
603
+ return_type = (
604
+ decls.get_paramaterized_class(ref.ident)
605
+ if isinstance(ref, InitRef)
606
+ else arg_types[0]
607
+ if mutates_first_arg
608
+ else resolve_type_annotation_mutate(decls, hints["return"])
609
+ )
610
+
611
+ arg_names = tuple(t.name for t in params)
612
+
613
+ merged = (
614
+ None
615
+ if merge is None
616
+ else resolve_literal(
617
+ return_type,
618
+ merge(
619
+ RuntimeExpr.__from_values__(decls, TypedExprDecl(return_type.to_just(), UnboundVarDecl("old", "old"))),
620
+ RuntimeExpr.__from_values__(decls, TypedExprDecl(return_type.to_just(), UnboundVarDecl("new", "new"))),
621
+ ),
622
+ lambda: decls,
623
+ )
624
+ )
625
+ decls |= merged
626
+
627
+ # defer this in generator so it doesn't resolve for builtins eagerly
628
+ args = (TypedExprDecl(tp.to_just(), UnboundVarDecl(name)) for name, tp in zip(arg_names, arg_types, strict=True))
629
+
630
+ return_type_is_eqsort = (
631
+ not decls._classes[return_type.ident].builtin if isinstance(return_type, TypeRefWithVars) else False
632
+ )
633
+ is_constructor = not is_builtin and return_type_is_eqsort and merged is None
634
+ signature_ = FunctionSignature(
635
+ return_type=None if mutates_first_arg else return_type,
636
+ var_arg_type=var_arg_type,
637
+ arg_types=arg_types,
638
+ arg_names=arg_names,
639
+ arg_defaults=tuple(a.__egg_typed_expr__.expr if a is not None else None for a in arg_defaults),
640
+ reverse_args=reverse_args,
641
+ )
642
+ doc = fn.__doc__
643
+ decl: ConstructorDecl | FunctionDecl
644
+ if is_constructor:
645
+ decl = ConstructorDecl(signature_, egg_name, cost, unextractable, doc)
646
+ else:
647
+ if cost is not None:
648
+ msg = "Cost can only be set for constructors"
649
+ raise ValueError(msg)
650
+ if unextractable:
651
+ msg = "Unextractable can only be set for constructors"
652
+ raise ValueError(msg)
653
+ decl = FunctionDecl(
654
+ signature=signature_,
655
+ egg_name=egg_name,
656
+ merge=merged.__egg_typed_expr__.expr if merged is not None else None,
657
+ builtin=is_builtin,
658
+ doc=doc,
659
+ )
660
+ decls.set_function_decl(ref, decl)
661
+ return Thunk.fn(
662
+ _add_default_rewrite_function,
663
+ decls,
664
+ ref,
665
+ fn,
666
+ args,
667
+ ruleset,
668
+ subsume,
669
+ return_type,
670
+ mutates_first_arg,
671
+ context=f"creating {ref}",
672
+ )
673
+
674
+
675
+ # Overload to support aritys 0-4 until variadic generic support map, so we can map from type to value
676
+ @overload
677
+ def relation(
678
+ name: str, tp1: type[BE1], tp2: type[BE2], tp3: type[BE3], tp4: type[BE4], /
679
+ ) -> Callable[[BE1, BE2, BE3, BE4], Unit]: ...
680
+
681
+
682
+ @overload
683
+ def relation(name: str, tp1: type[BE1], tp2: type[BE2], tp3: type[BE3], /) -> Callable[[BE1, BE2, BE3], Unit]: ...
684
+
685
+
686
+ @overload
687
+ def relation(name: str, tp1: type[BE1], tp2: type[BE2], /) -> Callable[[BE1, BE2], Unit]: ...
688
+
689
+
690
+ @overload
691
+ def relation(name: str, tp1: type[BE1], /, *, egg_fn: str | None = None) -> Callable[[BE1], Unit]: ...
692
+
693
+
694
+ @overload
695
+ def relation(name: str, /, *, egg_fn: str | None = None) -> Callable[[], Unit]: ...
696
+
697
+
698
+ def relation(name: str, /, *tps: type, egg_fn: str | None = None) -> Callable[..., Unit]:
699
+ """
700
+ Creates a function whose return type is `Unit` and has a default value.
701
+ """
702
+ ident = Ident(name, _get_module())
703
+
704
+ decls_thunk = Thunk.fn(_relation_decls, ident, tps, egg_fn)
705
+ return cast("Callable[..., Unit]", RuntimeFunction(decls_thunk, Thunk.value(FunctionRef(ident))))
706
+
707
+
708
+ def _get_module(frame: FrameType | None = None) -> str | None:
709
+ if frame is None:
710
+ frame = currentframe()
711
+ assert frame is not None
712
+ frame = frame.f_back
713
+ assert frame is not None
714
+ frame = frame.f_back
715
+ assert frame is not None
716
+
717
+ module = getmodule(frame)
718
+ return module.__name__ if module is not None else None
719
+
720
+
721
+ def _relation_decls(ident: Ident, tps: tuple[type, ...], egg_fn: str | None) -> Declarations:
722
+ from .builtins import Unit # noqa: PLC0415
723
+
724
+ decls = Declarations()
725
+ decls |= cast("RuntimeClass", Unit)
726
+ arg_types = tuple(resolve_type_annotation_mutate(decls, tp).to_just() for tp in tps)
727
+ decls._functions[ident] = RelationDecl(arg_types, tuple(None for _ in tps), egg_fn)
728
+ return decls
729
+
730
+
731
+ def constant(
732
+ name: str,
733
+ tp: type[BASE_EXPR],
734
+ default_replacement: BASE_EXPR | None = None,
735
+ /,
736
+ *,
737
+ egg_name: str | None = None,
738
+ ruleset: Ruleset | None = None,
739
+ ) -> BASE_EXPR:
740
+ """
741
+ A "constant" is implemented as the instantiation of a value that takes no args.
742
+ This creates a function with `name` and return type `tp` and returns a value of it being called.
743
+ """
744
+ return cast(
745
+ "BASE_EXPR",
746
+ RuntimeExpr(*split_thunk(Thunk.fn(_constant_thunk, name, tp, egg_name, default_replacement, ruleset))),
747
+ )
748
+
749
+
750
+ def _constant_thunk(
751
+ name: str, tp: type, egg_name: str | None, default_replacement: object, ruleset: Ruleset | None
752
+ ) -> tuple[Declarations, TypedExprDecl]:
753
+ decls = Declarations()
754
+ type_ref = resolve_type_annotation_mutate(decls, tp)
755
+ ident = Ident(name, _get_module())
756
+ callable_ref = ConstantRef(ident)
757
+ decls._constants[ident] = ConstantDecl(type_ref.to_just(), egg_name)
758
+ _add_default_rewrite(decls, callable_ref, type_ref, default_replacement, ruleset, subsume=False)
759
+ return decls, TypedExprDecl(type_ref.to_just(), CallDecl(callable_ref))
760
+
761
+
762
+ def _add_default_rewrite_function(
763
+ decls: Declarations,
764
+ ref: FunctionRef | MethodRef | PropertyRef | ClassMethodRef | InitRef,
765
+ fn: Callable,
766
+ args: Iterable[TypedExprDecl],
767
+ ruleset: Ruleset | None,
768
+ subsume: bool,
769
+ res_type: TypeOrVarRef,
770
+ mutates_first_arg: bool,
771
+ ) -> None:
772
+ args = list(args)
773
+ arg_exprs: list[RuntimeExpr | RuntimeClass] = [RuntimeExpr.__from_values__(decls, a) for a in args]
774
+ # If this is a classmethod, add the class as the first arg
775
+ if isinstance(ref, ClassMethodRef):
776
+ tp = decls.get_paramaterized_class(ref.ident)
777
+ arg_exprs.insert(0, RuntimeClass(Thunk.value(decls), tp))
778
+ with set_current_ruleset(ruleset):
779
+ res = fn(*arg_exprs)
780
+ # If the function mutates the first arg and we have overwritten it, then use that as the result
781
+ if mutates_first_arg and arg_exprs[0].__egg_typed_expr__ != args[0]:
782
+ res = arg_exprs[0]
783
+ _add_default_rewrite(decls, ref, res_type, res, ruleset, subsume)
784
+
785
+
786
+ def _add_default_rewrite(
787
+ decls: Declarations,
788
+ ref: CallableRef,
789
+ type_ref: TypeOrVarRef,
790
+ default_rewrite: object,
791
+ ruleset: Ruleset | None,
792
+ subsume: bool,
793
+ ) -> None:
794
+ """
795
+ Adds a default rewrite for the callable, if the default rewrite is not None
796
+
797
+ Will add it to the ruleset if it is passed in, or add it to the default ruleset on the passed in decls if not.
798
+ """
799
+ if default_rewrite is None:
800
+ return
801
+ resolved_value = resolve_literal(type_ref, default_rewrite, Thunk.value(decls))
802
+ rewrite_decl = DefaultRewriteDecl(ref, resolved_value.__egg_typed_expr__.expr, subsume)
803
+ ruleset_decls = _add_default_rewrite_inner(decls, rewrite_decl, ruleset)
804
+ ruleset_decls |= resolved_value
805
+
806
+
807
+ def _add_default_rewrite_inner(
808
+ decls: Declarations, rewrite_decl: DefaultRewriteDecl, ruleset: Ruleset | None
809
+ ) -> Declarations:
810
+ if ruleset:
811
+ ruleset_decls = ruleset._current_egg_decls
812
+ ruleset_decl = ruleset.__egg_ruleset__
813
+ else:
814
+ ruleset_decls = decls
815
+ ruleset_decl = decls.default_ruleset
816
+ ruleset_decl.rules.append(rewrite_decl)
817
+ return ruleset_decls
818
+
819
+
820
+ def _last_param_variable(params: list[Parameter]) -> bool:
821
+ """
822
+ Checks if the last paramater is a variable arg.
823
+
824
+ Raises an error if any of the other params are not positional or keyword.
825
+ """
826
+ found_var_arg = False
827
+ for param in params:
828
+ if found_var_arg:
829
+ msg = "Can only have a single var arg at the end"
830
+ raise ValueError(msg)
831
+ kind = param.kind
832
+ if kind == Parameter.VAR_POSITIONAL:
833
+ found_var_arg = True
834
+ elif kind != Parameter.POSITIONAL_OR_KEYWORD:
835
+ raise ValueError(f"Can only register functions with positional or keyword args, not {param.kind}")
836
+ return found_var_arg
837
+
838
+
839
+ class GraphvizKwargs(TypedDict, total=False):
840
+ max_functions: int | None
841
+ max_calls_per_function: int | None
842
+ n_inline_leaves: int
843
+ split_primitive_outputs: bool
844
+ split_functions: list[ExprCallable]
845
+ include_temporary_functions: bool
846
+
847
+
848
+ @dataclass
849
+ class EGraph:
850
+ """
851
+ A collection of expressions where each expression is part of a distinct equivalence class.
852
+
853
+ Can run actions, check facts, run schedules, or extract minimal cost expressions.
854
+ """
855
+
856
+ seminaive: InitVar[bool] = True
857
+ save_egglog_string: InitVar[bool] = False
858
+
859
+ _state: EGraphState = field(init=False, repr=False)
860
+ # For pushing/popping with egglog
861
+ _state_stack: list[EGraphState] = field(default_factory=list, repr=False)
862
+ # For storing the global "current" egraph
863
+ _token_stack: list[EGraph] = field(default_factory=list, repr=False)
864
+
865
+ def __post_init__(self, seminaive: bool, save_egglog_string: bool) -> None:
866
+ egraph = bindings.EGraph(seminaive=seminaive, record=save_egglog_string)
867
+ self._state = EGraphState(egraph)
868
+
869
+ def _add_decls(self, *decls: DeclerationsLike) -> None:
870
+ for d in decls:
871
+ self._state.__egg_decls__ |= d
872
+
873
+ def set_report_level(self, level: bindings._ReportLevel) -> None:
874
+ """
875
+ Set the level of detail recorded in subsequent run reports.
876
+ """
877
+ self._egraph.set_report_level(level)
878
+
879
+ @property
880
+ def as_egglog_string(self) -> str:
881
+ """
882
+ Returns the egglog string for this module.
883
+ """
884
+ cmds = self._egraph.commands()
885
+ if cmds is None:
886
+ msg = "Can't get egglog string unless EGraph created with save_egglog_string=True"
887
+ raise ValueError(msg)
888
+ return cmds
889
+
890
+ def _ipython_display_(self) -> None:
891
+ self.display()
892
+
893
+ def input(self, fn: Callable[..., String], path: str) -> None:
894
+ """
895
+ Loads a CSV file and sets it as *input, output of the function.
896
+ """
897
+ self._egraph.run_program(bindings.Input(span(1), self._callable_to_egg(fn)[1], path))
898
+
899
+ def _callable_to_egg(self, fn: ExprCallable) -> tuple[CallableRef, str]:
900
+ ref, decls = resolve_callable(fn)
901
+ self._add_decls(decls)
902
+ return ref, self._state.callable_ref_to_egg(ref)[0]
903
+
904
+ # TODO: Change let to be action...
905
+ def let(self, name: str, expr: BASE_EXPR) -> BASE_EXPR:
906
+ """
907
+ Define a new expression in the egraph and return a reference to it.
908
+ """
909
+ action = let(name, expr)
910
+ self.register(action)
911
+ runtime_expr = to_runtime_expr(expr)
912
+ self._add_decls(runtime_expr)
913
+ return cast(
914
+ "BASE_EXPR",
915
+ RuntimeExpr.__from_values__(
916
+ self.__egg_decls__, TypedExprDecl(runtime_expr.__egg_typed_expr__.tp, LetRefDecl(name))
917
+ ),
918
+ )
919
+
920
+ def include(self, path: str) -> None:
921
+ """
922
+ Include a file of rules.
923
+ """
924
+ msg = "Not implemented yet, because we don't have a way of registering the types with Python"
925
+ raise NotImplementedError(msg)
926
+
927
+ def output(self) -> None:
928
+ msg = "Not imeplemented yet, because there are no examples in the egglog repo"
929
+ raise NotImplementedError(msg)
930
+
931
+ @overload
932
+ def run(self, limit: int, /, *until: Fact, ruleset: Ruleset | None = None) -> bindings.RunReport: ...
933
+
934
+ @overload
935
+ def run(self, schedule: Schedule, /) -> bindings.RunReport: ...
936
+
937
+ def run(
938
+ self, limit_or_schedule: int | Schedule, /, *until: Fact, ruleset: Ruleset | None = None
939
+ ) -> bindings.RunReport:
940
+ """
941
+ Run the egraph until the given limit or until the given facts are true.
942
+ """
943
+ if isinstance(limit_or_schedule, int):
944
+ limit_or_schedule = run(ruleset, *until) * limit_or_schedule
945
+ return self._run_schedule(limit_or_schedule)
946
+
947
+ def _run_schedule(self, schedule: Schedule) -> bindings.RunReport:
948
+ self._add_decls(schedule)
949
+ cmd = self._state.run_schedule_to_egg(schedule.schedule)
950
+ (command_output,) = self._egraph.run_program(cmd)
951
+ assert isinstance(command_output, bindings.RunScheduleOutput)
952
+ return command_output.report
953
+
954
+ def stats(self) -> bindings.RunReport:
955
+ """
956
+ Returns the overall run report for the egraph.
957
+ """
958
+ (output,) = self._egraph.run_program(bindings.PrintOverallStatistics(span(1), None))
959
+ assert isinstance(output, bindings.OverallStatistics)
960
+ return output.report
961
+
962
+ def check_bool(self, *facts: FactLike) -> bool:
963
+ """
964
+ Returns true if the facts are true in the egraph.
965
+ """
966
+ try:
967
+ self.check(*facts)
968
+ # TODO: Make a separate exception class for this
969
+ except Exception as e:
970
+ if "Check failed" in str(e):
971
+ return False
972
+ raise
973
+ return True
974
+
975
+ def check(self, *facts: FactLike) -> None:
976
+ """
977
+ Check if a fact is true in the egraph.
978
+ """
979
+ self._egraph.run_program(self._facts_to_check(facts))
980
+
981
+ def check_fail(self, *facts: FactLike) -> None:
982
+ """
983
+ Checks that one of the facts is not true
984
+ """
985
+ self._egraph.run_program(bindings.Fail(span(1), self._facts_to_check(facts)))
986
+
987
+ def _facts_to_check(self, fact_likes: Iterable[FactLike]) -> bindings.Check:
988
+ facts = _fact_likes(fact_likes)
989
+ self._add_decls(*facts)
990
+ egg_facts = [self._state.fact_to_egg(f.fact) for f in _fact_likes(facts)]
991
+ return bindings.Check(span(2), egg_facts)
992
+
993
+ @overload
994
+ def extract(
995
+ self, expr: BASE_EXPR, /, include_cost: Literal[False] = False, cost_model: CostModel | None = None
996
+ ) -> BASE_EXPR: ...
997
+
998
+ @overload
999
+ def extract(
1000
+ self, expr: BASE_EXPR, /, include_cost: Literal[True], cost_model: None = None
1001
+ ) -> tuple[BASE_EXPR, int]: ...
1002
+
1003
+ @overload
1004
+ def extract(
1005
+ self, expr: BASE_EXPR, /, include_cost: Literal[True], cost_model: CostModel[COST]
1006
+ ) -> tuple[BASE_EXPR, COST]: ...
1007
+
1008
+ def extract(
1009
+ self, expr: BASE_EXPR, /, include_cost: bool = False, cost_model: CostModel[COST] | None = None
1010
+ ) -> BASE_EXPR | tuple[BASE_EXPR, COST]:
1011
+ """
1012
+ Extract the lowest cost expression from the egraph.
1013
+ """
1014
+ runtime_expr = to_runtime_expr(expr)
1015
+ self._add_decls(runtime_expr)
1016
+ tp = runtime_expr.__egg_typed_expr__.tp
1017
+ if cost_model is None:
1018
+ extract_report = self._run_extract(runtime_expr, 0)
1019
+ assert isinstance(extract_report, bindings.ExtractBest)
1020
+ res = self._from_termdag(extract_report.termdag, extract_report.term, tp)
1021
+ cost = cast("COST", extract_report.cost)
1022
+ else:
1023
+ # TODO: For some reason we need this or else it wont be registered. Not sure why
1024
+ self.register(expr)
1025
+ egg_cost_model = _CostModel(cost_model, self).to_bindings_cost_model()
1026
+ egg_sort = self._state.type_ref_to_egg(tp)
1027
+ extractor = bindings.Extractor([egg_sort], self._state.egraph, egg_cost_model)
1028
+ termdag = bindings.TermDag()
1029
+ value = self._state.typed_expr_to_value(runtime_expr.__egg_typed_expr__)
1030
+ cost, term = extractor.extract_best(self._state.egraph, termdag, value, egg_sort)
1031
+ res = self._from_termdag(termdag, term, tp)
1032
+ return (res, cost) if include_cost else res
1033
+
1034
+ def _from_termdag(self, termdag: bindings.TermDag, term: bindings._Term, tp: JustTypeRef) -> Any:
1035
+ (new_typed_expr,) = self._state.exprs_from_egg(termdag, [term], tp)
1036
+ return RuntimeExpr.__from_values__(self.__egg_decls__, new_typed_expr)
1037
+
1038
+ def extract_multiple(self, expr: BASE_EXPR, n: int) -> list[BASE_EXPR]:
1039
+ """
1040
+ Extract multiple expressions from the egraph.
1041
+ """
1042
+ runtime_expr = to_runtime_expr(expr)
1043
+ self._add_decls(runtime_expr)
1044
+ extract_report = self._run_extract(runtime_expr, n)
1045
+ assert isinstance(extract_report, bindings.ExtractVariants)
1046
+ new_exprs = self._state.exprs_from_egg(
1047
+ extract_report.termdag, extract_report.terms, runtime_expr.__egg_typed_expr__.tp
1048
+ )
1049
+ return [cast("BASE_EXPR", RuntimeExpr.__from_values__(self.__egg_decls__, expr)) for expr in new_exprs]
1050
+
1051
+ def _run_extract(self, expr: RuntimeExpr, n: int) -> bindings._CommandOutput:
1052
+ egg_expr = self._state.typed_expr_to_egg(expr.__egg_typed_expr__)
1053
+ # If we have defined any cost tables use the custom extraction
1054
+ args = (egg_expr, bindings.Lit(span(2), bindings.Int(n)))
1055
+ if self._state.cost_callables:
1056
+ cmd: bindings._Command = bindings.UserDefined(span(2), "extract", list(args))
1057
+ else:
1058
+ cmd = bindings.Extract(span(2), *args)
1059
+ try:
1060
+ return self._egraph.run_program(cmd)[0]
1061
+ except BaseException as e:
1062
+ e.add_note("while extracting expr:\n" + str(expr))
1063
+ raise
1064
+
1065
+ def push(self) -> None:
1066
+ """
1067
+ Push the current state of the egraph, so that it can be popped later and reverted back.
1068
+ """
1069
+ self._egraph.run_program(bindings.Push(1))
1070
+ self._state_stack.append(self._state)
1071
+ self._state = self._state.copy()
1072
+
1073
+ def pop(self) -> None:
1074
+ """
1075
+ Pop the current state of the egraph, reverting back to the previous state.
1076
+ """
1077
+ self._egraph.run_program(bindings.Pop(span(1), 1))
1078
+ self._state = self._state_stack.pop()
1079
+
1080
+ def __enter__(self) -> Self:
1081
+ """
1082
+ Copy the egraph state, so that it can be reverted back to the original state at the end.
1083
+
1084
+ Also sets the current egraph to this one.
1085
+ """
1086
+ self.push()
1087
+ return self
1088
+
1089
+ def __exit__(self, exc_type, exc, exc_tb) -> None:
1090
+ self.pop()
1091
+
1092
+ def _serialize(
1093
+ self,
1094
+ **kwargs: Unpack[GraphvizKwargs],
1095
+ ) -> bindings.SerializedEGraph:
1096
+ max_functions = kwargs.pop("max_functions", None)
1097
+ max_calls_per_function = kwargs.pop("max_calls_per_function", None)
1098
+ split_primitive_outputs = kwargs.pop("split_primitive_outputs", True)
1099
+ split_functions = kwargs.pop("split_functions", [])
1100
+ include_temporary_functions = kwargs.pop("include_temporary_functions", False)
1101
+ n_inline_leaves = kwargs.pop("n_inline_leaves", 0)
1102
+ serialized = self._egraph.serialize(
1103
+ [],
1104
+ max_functions=max_functions,
1105
+ max_calls_per_function=max_calls_per_function,
1106
+ include_temporary_functions=include_temporary_functions,
1107
+ )
1108
+ if serialized.discarded_functions:
1109
+ msg = ", ".join(set(self._state.possible_egglog_functions(serialized.discarded_functions)))
1110
+ warn(f"Omitted: {msg}", stacklevel=3)
1111
+ if serialized.truncated_functions:
1112
+ msg = ", ".join(set(self._state.possible_egglog_functions(serialized.truncated_functions)))
1113
+ warn(f"Truncated: {msg}", stacklevel=3)
1114
+ if split_primitive_outputs or split_functions:
1115
+ additional_ops = {self._callable_to_egg(f)[1] for f in split_functions}
1116
+ serialized.split_classes(self._egraph, additional_ops)
1117
+ serialized.map_ops(self._state.op_mapping())
1118
+
1119
+ for _ in range(n_inline_leaves):
1120
+ serialized.inline_leaves()
1121
+
1122
+ return serialized
1123
+
1124
+ def _graphviz(self, **kwargs: Unpack[GraphvizKwargs]) -> graphviz.Source:
1125
+ serialized = self._serialize(**kwargs)
1126
+
1127
+ original = serialized.to_dot()
1128
+ # Add link to stylesheet to the graph, so that edges light up on hover
1129
+ # https://gist.github.com/sverweij/93e324f67310f66a8f5da5c2abe94682
1130
+ styles = """/* the lines within the edges */
1131
+ .edge:active path,
1132
+ .edge:hover path {
1133
+ stroke: fuchsia;
1134
+ stroke-width: 3;
1135
+ stroke-opacity: 1;
1136
+ }
1137
+ /* arrows are typically drawn with a polygon */
1138
+ .edge:active polygon,
1139
+ .edge:hover polygon {
1140
+ stroke: fuchsia;
1141
+ stroke-width: 3;
1142
+ fill: fuchsia;
1143
+ stroke-opacity: 1;
1144
+ fill-opacity: 1;
1145
+ }
1146
+ /* If you happen to have text and want to color that as well... */
1147
+ .edge:active text,
1148
+ .edge:hover text {
1149
+ fill: fuchsia;
1150
+ }"""
1151
+ p = pathlib.Path(tempfile.gettempdir()) / "graphviz-styles.css"
1152
+ p.write_text(styles)
1153
+ with_stylesheet = original.replace("{", f'{{stylesheet="{p!s}"', 1)
1154
+ return graphviz.Source(with_stylesheet)
1155
+
1156
+ def display(self, graphviz: bool = False, **kwargs: Unpack[GraphvizKwargs]) -> None:
1157
+ """
1158
+ Displays the e-graph.
1159
+
1160
+ If in IPython it will display it inline, otherwise it will write it to a file and open it.
1161
+ """
1162
+ from IPython.display import SVG, display # noqa: PLC0415
1163
+
1164
+ from .visualizer_widget import VisualizerWidget # noqa: PLC0415
1165
+
1166
+ if graphviz:
1167
+ if IN_IPYTHON:
1168
+ svg = self._graphviz(**kwargs).pipe(format="svg", quiet=True, encoding="utf-8")
1169
+ display(SVG(svg))
1170
+ else:
1171
+ self._graphviz(**kwargs).render(view=True, format="svg", quiet=True)
1172
+ else:
1173
+ serialized = self._serialize(**kwargs)
1174
+ VisualizerWidget(egraphs=[serialized.to_json()]).display_or_open()
1175
+
1176
+ def saturate(
1177
+ self,
1178
+ schedule: Schedule | None = None,
1179
+ *,
1180
+ expr: Expr | None = None,
1181
+ max: int = 1000,
1182
+ visualize: bool = True,
1183
+ **kwargs: Unpack[GraphvizKwargs],
1184
+ ) -> None:
1185
+ """
1186
+ Saturate the egraph, running the given schedule until the egraph is saturated.
1187
+ It serializes the egraph at each step and returns a widget to visualize the egraph.
1188
+
1189
+ If an `expr` is passed, it's also extracted after each run and printed
1190
+ """
1191
+ from .visualizer_widget import VisualizerWidget # noqa: PLC0415
1192
+
1193
+ def to_json() -> str:
1194
+ if expr is not None:
1195
+ print(self.extract(expr), "\n")
1196
+ return self._serialize(**kwargs).to_json()
1197
+
1198
+ if visualize:
1199
+ egraphs = [to_json()]
1200
+ i = 0
1201
+ # Always visualize, even if we encounter an error
1202
+ try:
1203
+ while (self.run(schedule or 1).updated) and i < max:
1204
+ i += 1
1205
+ if visualize:
1206
+ egraphs.append(to_json())
1207
+ except:
1208
+ if visualize:
1209
+ egraphs.append(to_json())
1210
+ raise
1211
+ finally:
1212
+ if visualize:
1213
+ VisualizerWidget(egraphs=egraphs).display_or_open()
1214
+
1215
+ @property
1216
+ def _egraph(self) -> bindings.EGraph:
1217
+ return self._state.egraph
1218
+
1219
+ @property
1220
+ def __egg_decls__(self) -> Declarations:
1221
+ return self._state.__egg_decls__
1222
+
1223
+ def register(
1224
+ self,
1225
+ /,
1226
+ command_or_generator: ActionLike | RewriteOrRule | RewriteOrRuleGenerator,
1227
+ *command_likes: ActionLike | RewriteOrRule,
1228
+ ) -> None:
1229
+ """
1230
+ Registers any number of rewrites or rules.
1231
+ """
1232
+ if isinstance(command_or_generator, FunctionType):
1233
+ assert not command_likes
1234
+ current_frame = inspect.currentframe()
1235
+ assert current_frame
1236
+ original_frame = current_frame.f_back
1237
+ assert original_frame
1238
+ command_likes = tuple(_rewrite_or_rule_generator(command_or_generator, original_frame))
1239
+ else:
1240
+ command_likes = (cast("CommandLike", command_or_generator), *command_likes)
1241
+ commands = [_command_like(c) for c in command_likes]
1242
+ self._register_commands(commands)
1243
+
1244
+ def _register_commands(self, cmds: list[Command]) -> None:
1245
+ self._add_decls(*cmds)
1246
+ egg_cmds = [egg_cmd for cmd in cmds if (egg_cmd := self._command_to_egg(cmd)) is not None]
1247
+ self._egraph.run_program(*egg_cmds)
1248
+
1249
+ def _command_to_egg(self, cmd: Command) -> bindings._Command | None:
1250
+ ruleset_ident = Ident("")
1251
+ cmd_decl: CommandDecl
1252
+ match cmd:
1253
+ case RewriteOrRule(_, cmd_decl, ruleset):
1254
+ if ruleset:
1255
+ ruleset_ident = ruleset.__egg_ident__
1256
+ case Action(_, action):
1257
+ cmd_decl = ActionCommandDecl(action)
1258
+ case _:
1259
+ assert_never(cmd)
1260
+ return self._state.command_to_egg(cmd_decl, ruleset_ident)
1261
+
1262
+ def function_size(self, fn: ExprCallable) -> int:
1263
+ """
1264
+ Returns the number of rows in a certain function
1265
+ """
1266
+ egg_name = self._callable_to_egg(fn)[1]
1267
+ (output,) = self._egraph.run_program(bindings.PrintSize(span(1), egg_name))
1268
+ assert isinstance(output, bindings.PrintFunctionSize)
1269
+ return output.size
1270
+
1271
+ def all_function_sizes(self) -> list[tuple[ExprCallable, int]]:
1272
+ """
1273
+ Returns a list of all functions and their sizes.
1274
+ """
1275
+ (output,) = self._egraph.run_program(bindings.PrintSize(span(1), None))
1276
+ assert isinstance(output, bindings.PrintAllFunctionsSize)
1277
+ return [(callables[0], size) for (name, size) in output.sizes if (callables := self._egg_fn_to_callables(name))]
1278
+
1279
+ def _egg_fn_to_callables(self, egg_fn: str) -> list[ExprCallable]:
1280
+ return [
1281
+ cast("ExprCallable", create_callable(self._state.__egg_decls__, ref))
1282
+ for ref in self._state.egg_fn_to_callable_refs[egg_fn]
1283
+ ]
1284
+
1285
+ def function_values(
1286
+ self, fn: Callable[..., BASE_EXPR] | BASE_EXPR, length: int | None = None
1287
+ ) -> dict[BASE_EXPR, BASE_EXPR]:
1288
+ """
1289
+ Given a callable that is a "function", meaning it returns a primitive or has a merge set,
1290
+ returns a mapping of the function applied with its arguments to its values
1291
+
1292
+ If length is specified, only the first `length` values will be returned.
1293
+ """
1294
+ ref, egg_name = self._callable_to_egg(fn)
1295
+ cmd = bindings.PrintFunction(span(1), egg_name, length, None, bindings.DefaultPrintFunctionMode())
1296
+ (output,) = self._egraph.run_program(cmd)
1297
+ assert isinstance(output, bindings.PrintFunctionOutput)
1298
+ signature = self.__egg_decls__.get_callable_decl(ref).signature
1299
+ assert isinstance(signature, FunctionSignature)
1300
+ tp = signature.semantic_return_type.to_just()
1301
+ return {
1302
+ self._from_termdag(output.termdag, call, tp): self._from_termdag(output.termdag, res, tp)
1303
+ for (call, res) in output.terms
1304
+ }
1305
+
1306
+ def lookup_function_value(self, expr: BASE_EXPR) -> BASE_EXPR | None:
1307
+ """
1308
+ Given an expression that is a function call, looks up the value of the function call if it exists.
1309
+ """
1310
+ runtime_expr = to_runtime_expr(expr)
1311
+ typed_expr = runtime_expr.__egg_typed_expr__
1312
+ assert isinstance(typed_expr.expr, CallDecl | GetCostDecl)
1313
+ egg_fn, typed_args = self._state.translate_call(typed_expr.expr)
1314
+ values_args = [self._state.typed_expr_to_value(a) for a in typed_args]
1315
+ possible_value = self._egraph.lookup_function(egg_fn, values_args)
1316
+ if possible_value is None:
1317
+ return None
1318
+ return cast(
1319
+ "BASE_EXPR",
1320
+ RuntimeExpr.__from_values__(
1321
+ self.__egg_decls__,
1322
+ TypedExprDecl(typed_expr.tp, self._state.value_to_expr(typed_expr.tp, possible_value)),
1323
+ ),
1324
+ )
1325
+
1326
+ def has_custom_cost(self, fn: ExprCallable) -> bool:
1327
+ """
1328
+ Checks if the any custom costs have been set for this expression callable.
1329
+ """
1330
+ resolved, _ = resolve_callable(fn)
1331
+ return resolved in self._state.cost_callables
1332
+
1333
+
1334
+ # Either a constant or a function.
1335
+ ExprCallable: TypeAlias = Callable[..., BaseExpr] | BaseExpr
1336
+
1337
+
1338
+ @dataclass(frozen=True)
1339
+ class _WrappedMethod:
1340
+ """
1341
+ Used to wrap a method and store some extra options on it before processing it when processing the class.
1342
+ """
1343
+
1344
+ egg_fn: str | None
1345
+ cost: int | None
1346
+ merge: Callable[[object, object], object] | None
1347
+ fn: Callable
1348
+ preserve: bool
1349
+ mutates_self: bool
1350
+ unextractable: bool
1351
+ subsume: bool
1352
+ reverse_args: bool
1353
+
1354
+ def __call__(self, *args, **kwargs) -> Never:
1355
+ msg = "We should never call a wrapped method. Did you forget to wrap the class?"
1356
+ raise NotImplementedError(msg)
1357
+
1358
+
1359
+ def ruleset(
1360
+ rule_or_generator: RewriteOrRule | RewriteOrRuleGenerator | None = None,
1361
+ *rules: RewriteOrRule,
1362
+ name: None | str = None,
1363
+ ) -> Ruleset:
1364
+ """
1365
+ Creates a ruleset with the following rules.
1366
+
1367
+ If no name is provided, try using the name of the funciton.
1368
+ """
1369
+ module: str | None
1370
+ if isinstance(rule_or_generator, FunctionType):
1371
+ name = name or rule_or_generator.__name__
1372
+ module = rule_or_generator.__module__
1373
+ else:
1374
+ module = _get_module()
1375
+ r = Ruleset(Ident(name, module if name else None) if name is not None else None)
1376
+ if rule_or_generator is not None:
1377
+ r.register(rule_or_generator, *rules, _increase_frame=True)
1378
+ return r
1379
+
1380
+
1381
+ @dataclass
1382
+ class Schedule(DelayedDeclerations):
1383
+ """
1384
+ A composition of some rulesets, either composing them sequentially, running them repeatedly, running them till saturation, or running until some facts are met
1385
+ """
1386
+
1387
+ # Defer declerations so that we can have rule generators that used not yet defined yet
1388
+ schedule: ScheduleDecl
1389
+
1390
+ def __str__(self) -> str:
1391
+ return pretty_decl(self.__egg_decls__, self.schedule)
1392
+
1393
+ def __repr__(self) -> str:
1394
+ return str(self)
1395
+
1396
+ def __mul__(self, length: int) -> Schedule:
1397
+ """
1398
+ Repeat the schedule a number of times.
1399
+ """
1400
+ return Schedule(self.__egg_decls_thunk__, RepeatDecl(self.schedule, length))
1401
+
1402
+ def saturate(self) -> Schedule:
1403
+ """
1404
+ Run the schedule until the e-graph is saturated.
1405
+ """
1406
+ return Schedule(self.__egg_decls_thunk__, SaturateDecl(self.schedule))
1407
+
1408
+ def __add__(self, other: Schedule) -> Schedule:
1409
+ """
1410
+ Run two schedules in sequence.
1411
+ """
1412
+ return Schedule(Thunk.fn(Declarations.create, self, other), SequenceDecl((self.schedule, other.schedule)))
1413
+
1414
+
1415
+ @dataclass
1416
+ class Ruleset(Schedule):
1417
+ """
1418
+ A collection of rules, which can be run as a schedule.
1419
+ """
1420
+
1421
+ __egg_decls_thunk__: Callable[[], Declarations] = field(init=False)
1422
+ schedule: RunDecl = field(init=False)
1423
+ ident: Ident | None
1424
+
1425
+ # Current declerations we have accumulated
1426
+ _current_egg_decls: Declarations = field(default_factory=Declarations)
1427
+ # Current rulesets we have accumulated
1428
+ __egg_ruleset__: RulesetDecl = field(init=False)
1429
+ # Rule generator functions that have been deferred, to allow for late type binding
1430
+ deferred_rule_gens: list[Callable[[], Iterable[RewriteOrRule]]] = field(default_factory=list)
1431
+
1432
+ def __post_init__(self) -> None:
1433
+ self.schedule = RunDecl(self.__egg_ident__, ())
1434
+ self.__egg_ruleset__ = self._current_egg_decls._rulesets[self.__egg_ident__] = RulesetDecl([])
1435
+ self.__egg_decls_thunk__ = self._update_egg_decls
1436
+
1437
+ def _update_egg_decls(self) -> Declarations:
1438
+ """
1439
+ To return the egg decls, we go through our deferred rules and add any we haven't yet
1440
+ """
1441
+ while self.deferred_rule_gens:
1442
+ with set_current_ruleset(self):
1443
+ rules = self.deferred_rule_gens.pop()()
1444
+ self._current_egg_decls.update(*rules)
1445
+ self.__egg_ruleset__.rules.extend(r.decl for r in rules)
1446
+ return self._current_egg_decls
1447
+
1448
+ def append(self, rule: RewriteOrRule) -> None:
1449
+ """
1450
+ Register a rule with the ruleset.
1451
+ """
1452
+ self._current_egg_decls |= rule
1453
+ self.__egg_ruleset__.rules.append(rule.decl)
1454
+
1455
+ def register(
1456
+ self,
1457
+ /,
1458
+ rule_or_generator: RewriteOrRule | RewriteOrRuleGenerator,
1459
+ *rules: RewriteOrRule,
1460
+ _increase_frame: bool = False,
1461
+ ) -> None:
1462
+ """
1463
+ Register rewrites or rules, either as a function or as values.
1464
+ """
1465
+ if isinstance(rule_or_generator, RewriteOrRule):
1466
+ self.append(rule_or_generator)
1467
+ for r in rules:
1468
+ self.append(r)
1469
+ else:
1470
+ assert not rules
1471
+ current_frame = inspect.currentframe()
1472
+ assert current_frame
1473
+ original_frame = current_frame.f_back
1474
+ assert original_frame
1475
+ if _increase_frame:
1476
+ original_frame = original_frame.f_back
1477
+ assert original_frame
1478
+ self.deferred_rule_gens.append(Thunk.fn(_rewrite_or_rule_generator, rule_or_generator, original_frame))
1479
+
1480
+ def __str__(self) -> str:
1481
+ return pretty_decl(self._current_egg_decls, self.__egg_ruleset__, ruleset_ident=self.ident)
1482
+
1483
+ def __repr__(self) -> str:
1484
+ return str(self)
1485
+
1486
+ def __or__(self, other: Ruleset | UnstableCombinedRuleset) -> UnstableCombinedRuleset:
1487
+ return unstable_combine_rulesets(self, other)
1488
+
1489
+ # Create a unique name if we didn't pass one from the user
1490
+ @property
1491
+ def __egg_ident__(self) -> Ident:
1492
+ return self.ident or Ident(f"ruleset_{id(self)}")
1493
+
1494
+
1495
+ @dataclass
1496
+ class UnstableCombinedRuleset(Schedule):
1497
+ __egg_decls_thunk__: Callable[[], Declarations] = field(init=False)
1498
+ schedule: RunDecl = field(init=False)
1499
+ ident: Ident | None
1500
+ rulesets: InitVar[list[Ruleset | UnstableCombinedRuleset]]
1501
+
1502
+ def __post_init__(self, rulesets: list[Ruleset | UnstableCombinedRuleset]) -> None:
1503
+ self.schedule = RunDecl(self.__egg_ident__, ())
1504
+ # Don't use thunk so that this is re-evaluated each time its requsted, so that additions inside will
1505
+ # be added after its been evaluated once.
1506
+ self.__egg_decls_thunk__ = partial(self._create_egg_decls, *rulesets)
1507
+
1508
+ @property
1509
+ def __egg_ident__(self) -> Ident:
1510
+ return self.ident or Ident(f"combined_ruleset_{id(self)}")
1511
+
1512
+ def _create_egg_decls(self, *rulesets: Ruleset | UnstableCombinedRuleset) -> Declarations:
1513
+ decls = Declarations.create(*rulesets)
1514
+ decls._rulesets[self.__egg_ident__] = CombinedRulesetDecl(tuple(r.__egg_ident__ for r in rulesets))
1515
+ return decls
1516
+
1517
+ def __or__(self, other: Ruleset | UnstableCombinedRuleset) -> UnstableCombinedRuleset:
1518
+ return unstable_combine_rulesets(self, other)
1519
+
1520
+
1521
+ def unstable_combine_rulesets(
1522
+ *rulesets: Ruleset | UnstableCombinedRuleset, name: str | None = None
1523
+ ) -> UnstableCombinedRuleset:
1524
+ """
1525
+ Combine multiple rulesets into a single ruleset.
1526
+ """
1527
+ return UnstableCombinedRuleset(Ident(name, _get_module()) if name else None, list(rulesets))
1528
+
1529
+
1530
+ @dataclass
1531
+ class RewriteOrRule:
1532
+ __egg_decls__: Declarations
1533
+ decl: RewriteOrRuleDecl
1534
+ ruleset: Ruleset | None = None
1535
+
1536
+ def __str__(self) -> str:
1537
+ return pretty_decl(self.__egg_decls__, self.decl)
1538
+
1539
+ def __repr__(self) -> str:
1540
+ return str(self)
1541
+
1542
+
1543
+ @dataclass
1544
+ class Fact:
1545
+ """
1546
+ A query on an EGraph, either by an expression or an equivalence between multiple expressions.
1547
+ """
1548
+
1549
+ __egg_decls__: Declarations
1550
+ fact: FactDecl
1551
+
1552
+ def __str__(self) -> str:
1553
+ return pretty_decl(self.__egg_decls__, self.fact)
1554
+
1555
+ def __repr__(self) -> str:
1556
+ return str(self)
1557
+
1558
+ def __bool__(self) -> bool:
1559
+ """
1560
+ Returns True if the two sides of an equality are structurally equal.
1561
+ """
1562
+ match self.fact:
1563
+ case EqDecl(_, left, right):
1564
+ return left == right
1565
+ case ExprFactDecl(TypedExprDecl(_, CallDecl(FunctionRef(name), (left_tp, right_tp)))) if (
1566
+ name == Ident.builtin("!=")
1567
+ ):
1568
+ return left_tp != right_tp
1569
+ msg = f"Can only check equality for == or != not {self}"
1570
+ raise ValueError(msg)
1571
+
1572
+
1573
+ @dataclass
1574
+ class Action:
1575
+ """
1576
+ A change to an EGraph, either unioning multiple expressions, setting the value of a function call, deleting an expression, or panicing.
1577
+ """
1578
+
1579
+ __egg_decls__: Declarations
1580
+ action: ActionDecl
1581
+
1582
+ def __str__(self) -> str:
1583
+ return pretty_decl(self.__egg_decls__, self.action)
1584
+
1585
+ def __repr__(self) -> str:
1586
+ return str(self)
1587
+
1588
+
1589
+ # We use these builders so that when creating these structures we can type check
1590
+ # if the arguments are the same type of expression
1591
+
1592
+
1593
+ def rewrite(lhs: EXPR, ruleset: None = None, *, subsume: bool = False) -> _RewriteBuilder[EXPR]:
1594
+ """Rewrite the given expression to a new expression."""
1595
+ return _RewriteBuilder(lhs, ruleset, subsume)
1596
+
1597
+
1598
+ def birewrite(lhs: EXPR, ruleset: None = None) -> _BirewriteBuilder[EXPR]:
1599
+ """Rewrite the given expression to a new expression and vice versa."""
1600
+ return _BirewriteBuilder(lhs, ruleset)
1601
+
1602
+
1603
+ def eq(expr: BASE_EXPR) -> _EqBuilder[BASE_EXPR]:
1604
+ """Check if the given expression is equal to the given value."""
1605
+ return _EqBuilder(expr)
1606
+
1607
+
1608
+ def ne(expr: BASE_EXPR) -> _NeBuilder[BASE_EXPR]:
1609
+ """Check if the given expression is not equal to the given value."""
1610
+ return _NeBuilder(expr)
1611
+
1612
+
1613
+ def panic(message: str) -> Action:
1614
+ """Raise an error with the given message."""
1615
+ return Action(Declarations(), PanicDecl(message))
1616
+
1617
+
1618
+ def set_cost(expr: BaseExpr, cost: i64Like) -> Action:
1619
+ """Set the cost of the given expression."""
1620
+ from .builtins import i64 # noqa: PLC0415
1621
+
1622
+ expr_runtime = to_runtime_expr(expr)
1623
+ typed_expr_decl = expr_runtime.__egg_typed_expr__
1624
+ expr_decl = typed_expr_decl.expr
1625
+ assert isinstance(expr_decl, CallDecl), "Can only set cost of calls, not literals or vars"
1626
+ cost_decl = to_runtime_expr(convert(cost, i64)).__egg_typed_expr__.expr
1627
+ return Action(expr_runtime.__egg_decls__, SetCostDecl(typed_expr_decl.tp, expr_decl, cost_decl))
1628
+
1629
+
1630
+ def let(name: str, expr: BaseExpr) -> Action:
1631
+ """Create a let binding."""
1632
+ runtime_expr = to_runtime_expr(expr)
1633
+ return Action(runtime_expr.__egg_decls__, LetDecl(name, runtime_expr.__egg_typed_expr__))
1634
+
1635
+
1636
+ def expr_action(expr: BaseExpr) -> Action:
1637
+ runtime_expr = to_runtime_expr(expr)
1638
+ return Action(runtime_expr.__egg_decls__, ExprActionDecl(runtime_expr.__egg_typed_expr__))
1639
+
1640
+
1641
+ def delete(expr: BaseExpr) -> Action:
1642
+ """Create a delete expression."""
1643
+ runtime_expr = to_runtime_expr(expr)
1644
+ typed_expr = runtime_expr.__egg_typed_expr__
1645
+ call_decl = typed_expr.expr
1646
+ assert isinstance(call_decl, CallDecl), "Can only delete calls, not literals or vars"
1647
+ return Action(runtime_expr.__egg_decls__, ChangeDecl(typed_expr.tp, call_decl, "delete"))
1648
+
1649
+
1650
+ def subsume(expr: Expr) -> Action:
1651
+ """Subsume an expression so it cannot be matched against or extracted"""
1652
+ runtime_expr = to_runtime_expr(expr)
1653
+ typed_expr = runtime_expr.__egg_typed_expr__
1654
+ call_decl = typed_expr.expr
1655
+ assert isinstance(call_decl, CallDecl), "Can only subsume calls, not literals or vars"
1656
+ return Action(runtime_expr.__egg_decls__, ChangeDecl(typed_expr.tp, call_decl, "subsume"))
1657
+
1658
+
1659
+ def expr_fact(expr: BaseExpr) -> Fact:
1660
+ runtime_expr = to_runtime_expr(expr)
1661
+ return Fact(runtime_expr.__egg_decls__, ExprFactDecl(runtime_expr.__egg_typed_expr__))
1662
+
1663
+
1664
+ def union(lhs: EXPR) -> _UnionBuilder[EXPR]:
1665
+ """Create a union of the given expression."""
1666
+ return _UnionBuilder(lhs=lhs)
1667
+
1668
+
1669
+ def set_(lhs: BASE_EXPR) -> _SetBuilder[BASE_EXPR]:
1670
+ """Create a set of the given expression."""
1671
+ return _SetBuilder(lhs=lhs)
1672
+
1673
+
1674
+ def rule(*facts: FactLike, ruleset: None = None, name: str | None = None) -> _RuleBuilder:
1675
+ """Create a rule with the given facts."""
1676
+ return _RuleBuilder(facts=_fact_likes(facts), name=name, ruleset=ruleset)
1677
+
1678
+
1679
+ def var(name: str, bound: type[T], egg_name: str | None = None) -> T:
1680
+ """Create a new variable with the given name and type."""
1681
+ return cast("T", _var(name, bound, egg_name=egg_name))
1682
+
1683
+
1684
+ def _var(name: str, bound: object, egg_name: str | None) -> RuntimeExpr:
1685
+ """Create a new variable with the given name and type."""
1686
+ decls_like, type_ref = resolve_type_annotation(bound)
1687
+ return RuntimeExpr(
1688
+ Thunk.fn(Declarations.create, decls_like),
1689
+ Thunk.value(TypedExprDecl(type_ref.to_just(), UnboundVarDecl(name, egg_name))),
1690
+ )
1691
+
1692
+
1693
+ def vars_(names: str, bound: type[BASE_EXPR]) -> Iterable[BASE_EXPR]:
1694
+ """Create variables with the given names and type."""
1695
+ for name in names.split(" "):
1696
+ yield var(name, bound)
1697
+
1698
+
1699
+ @dataclass
1700
+ class _RewriteBuilder(Generic[EXPR]):
1701
+ lhs: EXPR
1702
+ ruleset: Ruleset | None
1703
+ subsume: bool
1704
+
1705
+ def to(self, rhs: EXPR, *conditions: FactLike) -> RewriteOrRule:
1706
+ lhs = to_runtime_expr(self.lhs)
1707
+ facts = _fact_likes(conditions)
1708
+ rhs = convert_to_same_type(rhs, lhs)
1709
+ rule = RewriteOrRule(
1710
+ Declarations.create(lhs, rhs, *facts, self.ruleset),
1711
+ RewriteDecl(
1712
+ lhs.__egg_typed_expr__.tp,
1713
+ lhs.__egg_typed_expr__.expr,
1714
+ rhs.__egg_typed_expr__.expr,
1715
+ tuple(f.fact for f in facts),
1716
+ self.subsume,
1717
+ ),
1718
+ )
1719
+ if self.ruleset:
1720
+ self.ruleset.append(rule)
1721
+ return rule
1722
+
1723
+ def __str__(self) -> str:
1724
+ lhs = to_runtime_expr(self.lhs)
1725
+ return lhs.__egg_pretty__("rewrite")
1726
+
1727
+
1728
+ @dataclass
1729
+ class _BirewriteBuilder(Generic[EXPR]):
1730
+ lhs: EXPR
1731
+ ruleset: Ruleset | None
1732
+
1733
+ def to(self, rhs: EXPR, *conditions: FactLike) -> RewriteOrRule:
1734
+ lhs = to_runtime_expr(self.lhs)
1735
+ facts = _fact_likes(conditions)
1736
+ rhs = convert_to_same_type(rhs, lhs)
1737
+ rule = RewriteOrRule(
1738
+ Declarations.create(lhs, rhs, *facts, self.ruleset),
1739
+ BiRewriteDecl(
1740
+ lhs.__egg_typed_expr__.tp,
1741
+ lhs.__egg_typed_expr__.expr,
1742
+ rhs.__egg_typed_expr__.expr,
1743
+ tuple(f.fact for f in facts),
1744
+ ),
1745
+ )
1746
+ if self.ruleset:
1747
+ self.ruleset.append(rule)
1748
+ return rule
1749
+
1750
+ def __str__(self) -> str:
1751
+ lhs = to_runtime_expr(self.lhs)
1752
+ return lhs.__egg_pretty__("birewrite")
1753
+
1754
+
1755
+ @dataclass
1756
+ class _EqBuilder(Generic[BASE_EXPR]):
1757
+ expr: BASE_EXPR
1758
+
1759
+ def to(self, other: BASE_EXPR) -> Fact:
1760
+ expr = to_runtime_expr(self.expr)
1761
+ other = convert_to_same_type(other, expr)
1762
+ return Fact(
1763
+ Declarations.create(expr, other),
1764
+ EqDecl(expr.__egg_typed_expr__.tp, expr.__egg_typed_expr__.expr, other.__egg_typed_expr__.expr),
1765
+ )
1766
+
1767
+ def __repr__(self) -> str:
1768
+ return str(self)
1769
+
1770
+ def __str__(self) -> str:
1771
+ expr = to_runtime_expr(self.expr)
1772
+ return expr.__egg_pretty__("eq")
1773
+
1774
+
1775
+ @dataclass
1776
+ class _NeBuilder(Generic[BASE_EXPR]):
1777
+ lhs: BASE_EXPR
1778
+
1779
+ def to(self, rhs: BASE_EXPR) -> Unit:
1780
+ from .builtins import Unit # noqa: PLC0415
1781
+
1782
+ lhs = to_runtime_expr(self.lhs)
1783
+ rhs = convert_to_same_type(rhs, lhs)
1784
+ res = RuntimeExpr.__from_values__(
1785
+ Declarations.create(cast("RuntimeClass", Unit), lhs, rhs),
1786
+ TypedExprDecl(
1787
+ JustTypeRef(Ident.builtin("Unit")),
1788
+ CallDecl(FunctionRef(Ident.builtin("!=")), (lhs.__egg_typed_expr__, rhs.__egg_typed_expr__)),
1789
+ ),
1790
+ )
1791
+ return cast("Unit", res)
1792
+
1793
+ def __repr__(self) -> str:
1794
+ return str(self)
1795
+
1796
+ def __str__(self) -> str:
1797
+ expr = to_runtime_expr(self.lhs)
1798
+ return expr.__egg_pretty__("ne")
1799
+
1800
+
1801
+ @dataclass
1802
+ class _SetBuilder(Generic[BASE_EXPR]):
1803
+ lhs: BASE_EXPR
1804
+
1805
+ def to(self, rhs: BASE_EXPR) -> Action:
1806
+ lhs = to_runtime_expr(self.lhs)
1807
+ rhs = convert_to_same_type(rhs, lhs)
1808
+ lhs_expr = lhs.__egg_typed_expr__.expr
1809
+ assert isinstance(lhs_expr, CallDecl), "Can only set function calls"
1810
+ return Action(
1811
+ Declarations.create(lhs, rhs),
1812
+ SetDecl(lhs.__egg_typed_expr__.tp, lhs_expr, rhs.__egg_typed_expr__.expr),
1813
+ )
1814
+
1815
+ def __repr__(self) -> str:
1816
+ return str(self)
1817
+
1818
+ def __str__(self) -> str:
1819
+ lhs = to_runtime_expr(self.lhs)
1820
+ return lhs.__egg_pretty__("set_")
1821
+
1822
+
1823
+ @dataclass
1824
+ class _UnionBuilder(Generic[EXPR]):
1825
+ lhs: EXPR
1826
+
1827
+ def with_(self, rhs: EXPR) -> Action:
1828
+ lhs = to_runtime_expr(self.lhs)
1829
+ rhs = convert_to_same_type(rhs, lhs)
1830
+ return Action(
1831
+ Declarations.create(lhs, rhs),
1832
+ UnionDecl(lhs.__egg_typed_expr__.tp, lhs.__egg_typed_expr__.expr, rhs.__egg_typed_expr__.expr),
1833
+ )
1834
+
1835
+ def __repr__(self) -> str:
1836
+ return str(self)
1837
+
1838
+ def __str__(self) -> str:
1839
+ lhs = to_runtime_expr(self.lhs)
1840
+ return lhs.__egg_pretty__("union")
1841
+
1842
+
1843
+ @dataclass
1844
+ class _RuleBuilder:
1845
+ facts: tuple[Fact, ...]
1846
+ name: str | None
1847
+ ruleset: Ruleset | None
1848
+
1849
+ def then(self, *actions: ActionLike) -> RewriteOrRule:
1850
+ actions = _action_likes(actions)
1851
+ rule = RewriteOrRule(
1852
+ Declarations.create(self.ruleset, *actions, *self.facts),
1853
+ RuleDecl(tuple(a.action for a in actions), tuple(f.fact for f in self.facts), self.name),
1854
+ )
1855
+ if self.ruleset:
1856
+ self.ruleset.append(rule)
1857
+ return rule
1858
+
1859
+ def __str__(self) -> str:
1860
+ # TODO: Figure out how to stringify rulebuilder that preserves statements
1861
+ args = list(map(str, self.facts))
1862
+ if self.name is not None:
1863
+ args.append(f"name={self.name}")
1864
+ if self.ruleset is not None:
1865
+ args.append(f"ruleset={self.ruleset}")
1866
+ return f"rule({', '.join(args)})"
1867
+
1868
+
1869
+ def expr_parts(expr: BaseExpr) -> TypedExprDecl:
1870
+ """
1871
+ Returns the underlying type and decleration of the expression. Useful for testing structural equality or debugging.
1872
+ """
1873
+ if not isinstance(expr, RuntimeExpr):
1874
+ raise TypeError(f"Expected a RuntimeExpr not {expr}")
1875
+ return expr.__egg_typed_expr__
1876
+
1877
+
1878
+ def to_runtime_expr(expr: BaseExpr) -> RuntimeExpr:
1879
+ if not isinstance(expr, RuntimeExpr):
1880
+ raise TypeError(f"Expected a RuntimeExpr not {expr}")
1881
+ return expr
1882
+
1883
+
1884
+ def run(ruleset: Ruleset | None = None, *until: FactLike, scheduler: BackOff | None = None) -> Schedule:
1885
+ """
1886
+ Create a run configuration.
1887
+ """
1888
+ facts = _fact_likes(until)
1889
+ return Schedule(
1890
+ Thunk.fn(Declarations.create, ruleset, *facts),
1891
+ RunDecl(
1892
+ ruleset.__egg_ident__ if ruleset else Ident(""),
1893
+ tuple(f.fact for f in facts),
1894
+ scheduler.scheduler if scheduler else None,
1895
+ ),
1896
+ )
1897
+
1898
+
1899
+ def back_off(match_limit: None | int = None, ban_length: None | int = None) -> BackOff:
1900
+ """
1901
+ Create a backoff scheduler configuration.
1902
+
1903
+ ```python
1904
+ schedule = run(analysis_ruleset).saturate() + run(ruleset, scheduler=back_off(match_limit=1000, ban_length=5)) * 10
1905
+ ```
1906
+ This will run the `analysis_ruleset` until saturation, then run `ruleset` 10 times, using a backoff scheduler.
1907
+ """
1908
+ return BackOff(BackOffDecl(id=uuid4(), match_limit=match_limit, ban_length=ban_length))
1909
+
1910
+
1911
+ @dataclass(frozen=True)
1912
+ class BackOff:
1913
+ scheduler: BackOffDecl
1914
+
1915
+ def scope(self, schedule: Schedule) -> Schedule:
1916
+ """
1917
+ Defines the scheduler to be created directly before the inner schedule, instead of the default which is at the
1918
+ most outer scope.
1919
+ """
1920
+ return Schedule(schedule.__egg_decls_thunk__, LetSchedulerDecl(self.scheduler, schedule.schedule))
1921
+
1922
+ def __str__(self) -> str:
1923
+ return pretty_decl(Declarations(), self.scheduler)
1924
+
1925
+ def __repr__(self) -> str:
1926
+ return str(self)
1927
+
1928
+
1929
+ def seq(*schedules: Schedule) -> Schedule:
1930
+ """
1931
+ Run a sequence of schedules.
1932
+ """
1933
+ return Schedule(Thunk.fn(Declarations.create, *schedules), SequenceDecl(tuple(s.schedule for s in schedules)))
1934
+
1935
+
1936
+ ActionLike: TypeAlias = Action | BaseExpr
1937
+
1938
+
1939
+ def _action_likes(action_likes: Iterable[ActionLike]) -> tuple[Action, ...]:
1940
+ return tuple(map(_action_like, action_likes))
1941
+
1942
+
1943
+ def _action_like(action_like: ActionLike) -> Action:
1944
+ if isinstance(action_like, Action):
1945
+ return action_like
1946
+ return expr_action(action_like)
1947
+
1948
+
1949
+ Command: TypeAlias = Action | RewriteOrRule
1950
+
1951
+ CommandLike: TypeAlias = ActionLike | RewriteOrRule
1952
+
1953
+
1954
+ def _command_like(command_like: CommandLike) -> Command:
1955
+ if isinstance(command_like, RewriteOrRule):
1956
+ return command_like
1957
+ return _action_like(command_like)
1958
+
1959
+
1960
+ RewriteOrRuleGenerator = Callable[..., Iterable[RewriteOrRule]]
1961
+
1962
+
1963
+ def _rewrite_or_rule_generator(gen: RewriteOrRuleGenerator, frame: FrameType) -> Iterable[RewriteOrRule]:
1964
+ """
1965
+ Returns a thunk which will call the function with variables of the type and name of the arguments.
1966
+ """
1967
+ # Need to manually pass in the frame locals from the generator, because otherwise classes defined within function
1968
+ # will not be available in the annotations
1969
+ # combine locals and globals so that they are the same dict. Otherwise get_type_hints will go through the wrong
1970
+ # path and give an error for the test
1971
+ # python/tests/test_no_import_star.py::test_no_import_star_rulesset
1972
+ combined = {**gen.__globals__, **frame.f_locals}
1973
+ hints = get_type_hints(gen, combined, combined)
1974
+ args = [_var(p.name, hints[p.name], egg_name=None) for p in signature(gen).parameters.values()]
1975
+ return list(gen(*args))
1976
+
1977
+
1978
+ FactLike = Fact | BaseExpr
1979
+
1980
+
1981
+ def _fact_likes(fact_likes: Iterable[FactLike]) -> tuple[Fact, ...]:
1982
+ return tuple(map(_fact_like, fact_likes))
1983
+
1984
+
1985
+ def _fact_like(fact_like: FactLike) -> Fact:
1986
+ if isinstance(fact_like, Fact):
1987
+ return fact_like
1988
+ return expr_fact(fact_like)
1989
+
1990
+
1991
+ _CURRENT_RULESET = ContextVar[Ruleset | None]("CURRENT_RULESET", default=None)
1992
+
1993
+
1994
+ def get_current_ruleset() -> Ruleset | None:
1995
+ return _CURRENT_RULESET.get()
1996
+
1997
+
1998
+ @contextlib.contextmanager
1999
+ def set_current_ruleset(r: Ruleset | None) -> Generator[None, None, None]:
2000
+ token: Token[Ruleset | None] = _CURRENT_RULESET.set(r)
2001
+ try:
2002
+ yield
2003
+ finally:
2004
+ _CURRENT_RULESET.reset(token)
2005
+
2006
+
2007
+ def get_cost(expr: BaseExpr) -> i64:
2008
+ """
2009
+ Return a lookup of the cost of an expression. If not set, won't match.
2010
+ """
2011
+ assert isinstance(expr, RuntimeExpr)
2012
+ expr_decl = expr.__egg_typed_expr__.expr
2013
+ if not isinstance(expr_decl, CallDecl):
2014
+ msg = "Can only get cost of function calls, not literals or variables"
2015
+ raise TypeError(msg)
2016
+ return RuntimeExpr.__from_values__(
2017
+ expr.__egg_decls__,
2018
+ TypedExprDecl(JustTypeRef("i64"), GetCostDecl(expr_decl.callable, expr_decl.args)),
2019
+ )
2020
+
2021
+
2022
+ class Comparable(Protocol):
2023
+ def __lt__(self, other: Self) -> bool: ...
2024
+ def __le__(self, other: Self) -> bool: ...
2025
+ def __gt__(self, other: Self) -> bool: ...
2026
+ def __ge__(self, other: Self) -> bool: ...
2027
+
2028
+
2029
+ COST = TypeVar("COST", bound=Comparable)
2030
+
2031
+
2032
+ class CostModel(Protocol, Generic[COST]):
2033
+ """
2034
+ A cost model for an e-graph. Used to determine the cost of an expression based on its structure and the costs of its sub-expressions.
2035
+
2036
+ Called with an expression and the costs of its children, returns the total cost of the expression.
2037
+
2038
+ Additionally, the cost model should guarantee that a term has a no-smaller cost
2039
+ than its subterms to avoid cycles in the extracted terms for common case usages.
2040
+ For more niche usages, a term can have a cost less than its subterms.
2041
+ As long as there is no negative cost cycle, the default extractor is guaranteed to terminate in computing the costs.
2042
+ However, the user needs to be careful to guarantee acyclicity in the extracted terms.
2043
+ """
2044
+
2045
+ def __call__(self, egraph: EGraph, expr: BaseExpr, children_costs: list[COST]) -> COST:
2046
+ """
2047
+ The total cost of a term given the cost of the root e-node and its immediate children's total costs.
2048
+ """
2049
+ raise NotImplementedError
2050
+
2051
+
2052
+ def default_cost_model(egraph: EGraph, expr: BaseExpr, children_costs: list[int]) -> int:
2053
+ """
2054
+ A default cost model for an e-graph, which looks up costs set on function calls, or uses 1 as the default cost.
2055
+ """
2056
+ from .builtins import Container # noqa: PLC0415
2057
+ from .deconstruct import get_callable_fn # noqa: PLC0415
2058
+
2059
+ # 1. First prefer if the expr has a custom cost set on it
2060
+ if (
2061
+ (callable_fn := get_callable_fn(expr)) is not None
2062
+ and egraph.has_custom_cost(callable_fn)
2063
+ and (i := egraph.lookup_function_value(get_cost(expr))) is not None
2064
+ ):
2065
+ self_cost = int(i)
2066
+ # 2. Else, check if this is a callable and it has a cost set on its declaration
2067
+ elif callable_fn is not None and (callable_cost := get_callable_cost(callable_fn)) is not None:
2068
+ self_cost = callable_cost
2069
+ # 3. Else, if this is a container, it has no cost, otherwise it has a cost of 1
2070
+ else:
2071
+ # By default, all nodes have a cost of 1 except for containers which have a cost of 0
2072
+ self_cost = 0 if isinstance(expr, Container) else 1
2073
+ # Sum up the costs of the children and our own cost
2074
+ return sum(children_costs, start=self_cost)
2075
+
2076
+
2077
+ class ComparableAddSub(Comparable, Protocol):
2078
+ def __add__(self, other: Self) -> Self: ...
2079
+ def __sub__(self, other: Self) -> Self: ...
2080
+
2081
+
2082
+ DAG_COST = TypeVar("DAG_COST", bound=ComparableAddSub)
2083
+
2084
+
2085
+ @dataclass
2086
+ class GreedyDagCost(Generic[DAG_COST]):
2087
+ """
2088
+ Cost of a DAG, which stores children costs. Use `.total` to get the underlying cost.
2089
+ """
2090
+
2091
+ total: DAG_COST
2092
+ _costs: dict[TypedExprDecl, DAG_COST] = field(repr=False)
2093
+
2094
+ def __eq__(self, other: object) -> bool:
2095
+ if not isinstance(other, GreedyDagCost):
2096
+ return NotImplemented
2097
+ return self.total == other.total
2098
+
2099
+ def __lt__(self, other: Self) -> bool:
2100
+ return self.total < other.total
2101
+
2102
+ def __le__(self, other: Self) -> bool:
2103
+ return self.total <= other.total
2104
+
2105
+ def __gt__(self, other: Self) -> bool:
2106
+ return self.total > other.total
2107
+
2108
+ def __ge__(self, other: Self) -> bool:
2109
+ return self.total >= other.total
2110
+
2111
+ def __hash__(self) -> int:
2112
+ return hash(self.total)
2113
+
2114
+
2115
+ @dataclass
2116
+ class GreedyDagCostModel(CostModel[GreedyDagCost[DAG_COST]]):
2117
+ """
2118
+ A cost model which will count duplicate nodes only once.
2119
+
2120
+ Should have similar behavior as https://github.com/egraphs-good/extraction-gym/blob/main/src/extract/greedy_dag.rs
2121
+ but implemented as a cost model that will be used with the default extractor.
2122
+ """
2123
+
2124
+ base: CostModel[DAG_COST]
2125
+
2126
+ def __call__(
2127
+ self, egraph: EGraph, expr: BaseExpr, children_costs: list[GreedyDagCost[DAG_COST]]
2128
+ ) -> GreedyDagCost[DAG_COST]:
2129
+ cost = self.base(egraph, expr, [c.total for c in children_costs])
2130
+ for c in children_costs:
2131
+ cost -= c.total
2132
+ costs = {}
2133
+ for c in children_costs:
2134
+ costs.update(c._costs)
2135
+ total = sum(costs.values(), start=cost)
2136
+ costs[to_runtime_expr(expr).__egg_typed_expr__] = cost
2137
+ return GreedyDagCost(total, costs)
2138
+
2139
+
2140
+ @overload
2141
+ def greedy_dag_cost_model() -> CostModel[GreedyDagCost[int]]: ...
2142
+
2143
+
2144
+ @overload
2145
+ def greedy_dag_cost_model(base: CostModel[DAG_COST]) -> CostModel[GreedyDagCost[DAG_COST]]: ...
2146
+
2147
+
2148
+ def greedy_dag_cost_model(base: CostModel[Any] = default_cost_model) -> CostModel[GreedyDagCost[Any]]:
2149
+ """
2150
+ Creates a greedy dag cost model from a base cost model.
2151
+ """
2152
+ return GreedyDagCostModel(base or default_cost_model)
2153
+
2154
+
2155
+ def get_callable_cost(fn: ExprCallable) -> int | None:
2156
+ """
2157
+ Returns the cost of a callable, if it has one set. Otherwise returns None.
2158
+ """
2159
+ callable_ref, decls = resolve_callable(fn)
2160
+ callable_decl = decls.get_callable_decl(callable_ref)
2161
+ return callable_decl.cost if isinstance(callable_decl, ConstructorDecl) else 1
2162
+
2163
+
2164
+ @dataclass
2165
+ class _CostModel(Generic[COST]):
2166
+ """
2167
+ Implements the methods compatible with the bindings for the cost model.
2168
+ """
2169
+
2170
+ model: CostModel[COST]
2171
+ egraph: EGraph
2172
+ enode_cost_results: dict[tuple[str, tuple[bindings.Value, ...]], int] = field(default_factory=dict)
2173
+ enode_cost_expressions: list[RuntimeExpr] = field(default_factory=list)
2174
+ fold_results: dict[tuple[int, tuple[COST, ...]], COST] = field(default_factory=dict)
2175
+ base_value_cost_results: dict[tuple[str, bindings.Value], COST] = field(default_factory=dict)
2176
+ container_cost_results: dict[tuple[str, bindings.Value, tuple[COST, ...]], COST] = field(default_factory=dict)
2177
+
2178
+ def call_model(self, expr: RuntimeExpr, children_costs: list[COST]) -> COST:
2179
+ return self.model(self.egraph, cast("BaseExpr", expr), children_costs)
2180
+ # if __debug__:
2181
+ # for c in children_costs:
2182
+ # if res <= c:
2183
+ # msg = f"Cost model {self.model} produced a cost {res} less than or equal to a child cost {c} for {expr}"
2184
+ # raise ValueError(msg)
2185
+
2186
+ def fold(self, _fn: str, index: int, children_costs: list[COST]) -> COST:
2187
+ try:
2188
+ return self.fold_results[(index, tuple(children_costs))]
2189
+ except KeyError:
2190
+ pass
2191
+
2192
+ expr = self.enode_cost_expressions[index]
2193
+ return self.call_model(expr, children_costs)
2194
+
2195
+ # enode cost is only ever called right before fold, for the head_cost
2196
+ def enode_cost(self, name: str, args: list[bindings.Value]) -> int:
2197
+ try:
2198
+ return self.enode_cost_results[(name, tuple(args))]
2199
+ except KeyError:
2200
+ pass
2201
+ (callable_ref,) = self.egraph._state.egg_fn_to_callable_refs[name]
2202
+ signature = self.egraph.__egg_decls__.get_callable_decl(callable_ref).signature
2203
+ assert isinstance(signature, FunctionSignature)
2204
+ arg_exprs = [
2205
+ TypedExprDecl(tp.to_just(), self.egraph._state.value_to_expr(tp.to_just(), arg))
2206
+ for (arg, tp) in zip(args, signature.arg_types, strict=True)
2207
+ ]
2208
+ res_type = signature.semantic_return_type.to_just()
2209
+ res = RuntimeExpr.__from_values__(
2210
+ self.egraph.__egg_decls__,
2211
+ TypedExprDecl(res_type, CallDecl(callable_ref, tuple(arg_exprs))),
2212
+ )
2213
+ index = len(self.enode_cost_expressions)
2214
+ self.enode_cost_expressions.append(res)
2215
+ self.enode_cost_results[(name, tuple(args))] = index
2216
+ return index
2217
+
2218
+ def base_value_cost(self, tp: str, value: bindings.Value) -> COST:
2219
+ try:
2220
+ return self.base_value_cost_results[(tp, value)]
2221
+ except KeyError:
2222
+ pass
2223
+ type_ref = self.egraph._state.egg_sort_to_type_ref[tp]
2224
+ expr = RuntimeExpr.__from_values__(
2225
+ self.egraph.__egg_decls__,
2226
+ TypedExprDecl(type_ref, self.egraph._state.value_to_expr(type_ref, value)),
2227
+ )
2228
+ res = self.call_model(expr, [])
2229
+ self.base_value_cost_results[(tp, value)] = res
2230
+ return res
2231
+
2232
+ def container_cost(self, tp: str, value: bindings.Value, element_costs: list[COST]) -> COST:
2233
+ try:
2234
+ return self.container_cost_results[(tp, value, tuple(element_costs))]
2235
+ except KeyError:
2236
+ pass
2237
+ type_ref = self.egraph._state.egg_sort_to_type_ref[tp]
2238
+ expr = RuntimeExpr.__from_values__(
2239
+ self.egraph.__egg_decls__,
2240
+ TypedExprDecl(type_ref, self.egraph._state.value_to_expr(type_ref, value)),
2241
+ )
2242
+ res = self.call_model(expr, element_costs)
2243
+ self.container_cost_results[(tp, value, tuple(element_costs))] = res
2244
+ return res
2245
+
2246
+ def to_bindings_cost_model(self) -> bindings.CostModel[COST, int]:
2247
+ return bindings.CostModel(self.fold, self.enode_cost, self.container_cost, self.base_value_cost)