egglog 6.1.0__cp312-none-win_amd64.whl → 7.1.0__cp312-none-win_amd64.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 +1 -1
- egglog/bindings.cp312-win_amd64.pyd +0 -0
- egglog/bindings.pyi +9 -0
- egglog/builtins.py +42 -2
- egglog/conversion.py +177 -0
- egglog/declarations.py +354 -734
- egglog/egraph.py +602 -800
- egglog/egraph_state.py +456 -0
- egglog/exp/array_api.py +100 -88
- egglog/exp/array_api_numba.py +6 -1
- egglog/exp/siu_examples.py +35 -0
- egglog/pretty.py +464 -0
- egglog/runtime.py +279 -431
- egglog/thunk.py +71 -0
- egglog/type_constraint_solver.py +5 -2
- {egglog-6.1.0.dist-info → egglog-7.1.0.dist-info}/METADATA +7 -7
- {egglog-6.1.0.dist-info → egglog-7.1.0.dist-info}/RECORD +19 -14
- {egglog-6.1.0.dist-info → egglog-7.1.0.dist-info}/WHEEL +0 -0
- {egglog-6.1.0.dist-info → egglog-7.1.0.dist-info}/license_files/LICENSE +0 -0
egglog/egraph_state.py
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Implement conversion to/from egglog.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from collections import defaultdict
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from typing import TYPE_CHECKING, overload
|
|
10
|
+
from weakref import WeakKeyDictionary
|
|
11
|
+
|
|
12
|
+
from typing_extensions import assert_never
|
|
13
|
+
|
|
14
|
+
from . import bindings
|
|
15
|
+
from .declarations import *
|
|
16
|
+
from .pretty import *
|
|
17
|
+
from .type_constraint_solver import TypeConstraintError, TypeConstraintSolver
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from collections.abc import Iterable
|
|
21
|
+
|
|
22
|
+
__all__ = ["EGraphState", "GLOBAL_PY_OBJECT_SORT"]
|
|
23
|
+
|
|
24
|
+
# Create a global sort for python objects, so we can store them without an e-graph instance
|
|
25
|
+
# Needed when serializing commands to egg commands when creating modules
|
|
26
|
+
GLOBAL_PY_OBJECT_SORT = bindings.PyObjectSort()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class EGraphState:
|
|
31
|
+
"""
|
|
32
|
+
State of the EGraph declerations and rulesets, so when we pop/push the stack we know whats defined.
|
|
33
|
+
|
|
34
|
+
Used for converting to/from egg and for pretty printing.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
egraph: bindings.EGraph
|
|
38
|
+
# The decleratons we have added.
|
|
39
|
+
__egg_decls__: Declarations = field(default_factory=Declarations)
|
|
40
|
+
# Mapping of added rulesets to the added rules
|
|
41
|
+
rulesets: dict[str, set[RewriteOrRuleDecl]] = field(default_factory=dict)
|
|
42
|
+
|
|
43
|
+
# Bidirectional mapping between egg function names and python callable references.
|
|
44
|
+
# Note that there are possibly mutliple callable references for a single egg function name, like `+`
|
|
45
|
+
# for both int and rational classes.
|
|
46
|
+
egg_fn_to_callable_refs: dict[str, set[CallableRef]] = field(
|
|
47
|
+
default_factory=lambda: defaultdict(set, {"!=": {FunctionRef("!=")}})
|
|
48
|
+
)
|
|
49
|
+
callable_ref_to_egg_fn: dict[CallableRef, str] = field(default_factory=lambda: {FunctionRef("!="): "!="})
|
|
50
|
+
|
|
51
|
+
# Bidirectional mapping between egg sort names and python type references.
|
|
52
|
+
type_ref_to_egg_sort: dict[JustTypeRef, str] = field(default_factory=dict)
|
|
53
|
+
|
|
54
|
+
# Cache of egg expressions for converting to egg
|
|
55
|
+
expr_to_egg_cache: WeakKeyDictionary[ExprDecl, bindings._Expr] = field(default_factory=WeakKeyDictionary)
|
|
56
|
+
|
|
57
|
+
def copy(self) -> EGraphState:
|
|
58
|
+
"""
|
|
59
|
+
Returns a copy of the state. Th egraph reference is kept the same. Used for pushing/popping.
|
|
60
|
+
"""
|
|
61
|
+
return EGraphState(
|
|
62
|
+
egraph=self.egraph,
|
|
63
|
+
__egg_decls__=self.__egg_decls__.copy(),
|
|
64
|
+
rulesets={k: v.copy() for k, v in self.rulesets.items()},
|
|
65
|
+
egg_fn_to_callable_refs=defaultdict(set, {k: v.copy() for k, v in self.egg_fn_to_callable_refs.items()}),
|
|
66
|
+
callable_ref_to_egg_fn=self.callable_ref_to_egg_fn.copy(),
|
|
67
|
+
type_ref_to_egg_sort=self.type_ref_to_egg_sort.copy(),
|
|
68
|
+
expr_to_egg_cache=self.expr_to_egg_cache.copy(),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
def schedule_to_egg(self, schedule: ScheduleDecl) -> bindings._Schedule:
|
|
72
|
+
match schedule:
|
|
73
|
+
case SaturateDecl(schedule):
|
|
74
|
+
return bindings.Saturate(self.schedule_to_egg(schedule))
|
|
75
|
+
case RepeatDecl(schedule, times):
|
|
76
|
+
return bindings.Repeat(times, self.schedule_to_egg(schedule))
|
|
77
|
+
case SequenceDecl(schedules):
|
|
78
|
+
return bindings.Sequence([self.schedule_to_egg(s) for s in schedules])
|
|
79
|
+
case RunDecl(ruleset_name, until):
|
|
80
|
+
self.ruleset_to_egg(ruleset_name)
|
|
81
|
+
config = bindings.RunConfig(ruleset_name, None if not until else list(map(self.fact_to_egg, until)))
|
|
82
|
+
return bindings.Run(config)
|
|
83
|
+
case _:
|
|
84
|
+
assert_never(schedule)
|
|
85
|
+
|
|
86
|
+
def ruleset_to_egg(self, name: str) -> None:
|
|
87
|
+
"""
|
|
88
|
+
Registers a ruleset if it's not already registered.
|
|
89
|
+
"""
|
|
90
|
+
match self.__egg_decls__._rulesets[name]:
|
|
91
|
+
case RulesetDecl(rules):
|
|
92
|
+
if name not in self.rulesets:
|
|
93
|
+
if name:
|
|
94
|
+
self.egraph.run_program(bindings.AddRuleset(name))
|
|
95
|
+
added_rules = self.rulesets[name] = set()
|
|
96
|
+
else:
|
|
97
|
+
added_rules = self.rulesets[name]
|
|
98
|
+
for rule in rules:
|
|
99
|
+
if rule in added_rules:
|
|
100
|
+
continue
|
|
101
|
+
self.egraph.run_program(self.command_to_egg(rule, name))
|
|
102
|
+
added_rules.add(rule)
|
|
103
|
+
case CombinedRulesetDecl(rulesets):
|
|
104
|
+
if name in self.rulesets:
|
|
105
|
+
return
|
|
106
|
+
self.rulesets[name] = set()
|
|
107
|
+
for ruleset in rulesets:
|
|
108
|
+
self.ruleset_to_egg(ruleset)
|
|
109
|
+
self.egraph.run_program(bindings.UnstableCombinedRuleset(name, list(rulesets)))
|
|
110
|
+
|
|
111
|
+
def command_to_egg(self, cmd: CommandDecl, ruleset: str) -> bindings._Command:
|
|
112
|
+
match cmd:
|
|
113
|
+
case ActionCommandDecl(action):
|
|
114
|
+
return bindings.ActionCommand(self.action_to_egg(action))
|
|
115
|
+
case RewriteDecl(tp, lhs, rhs, conditions) | BiRewriteDecl(tp, lhs, rhs, conditions):
|
|
116
|
+
self.type_ref_to_egg(tp)
|
|
117
|
+
rewrite = bindings.Rewrite(
|
|
118
|
+
self.expr_to_egg(lhs),
|
|
119
|
+
self.expr_to_egg(rhs),
|
|
120
|
+
[self.fact_to_egg(c) for c in conditions],
|
|
121
|
+
)
|
|
122
|
+
return (
|
|
123
|
+
bindings.RewriteCommand(ruleset, rewrite, cmd.subsume)
|
|
124
|
+
if isinstance(cmd, RewriteDecl)
|
|
125
|
+
else bindings.BiRewriteCommand(ruleset, rewrite)
|
|
126
|
+
)
|
|
127
|
+
case RuleDecl(head, body, name):
|
|
128
|
+
rule = bindings.Rule(
|
|
129
|
+
[self.action_to_egg(a) for a in head],
|
|
130
|
+
[self.fact_to_egg(f) for f in body],
|
|
131
|
+
)
|
|
132
|
+
return bindings.RuleCommand(name or "", ruleset, rule)
|
|
133
|
+
case _:
|
|
134
|
+
assert_never(cmd)
|
|
135
|
+
|
|
136
|
+
def action_to_egg(self, action: ActionDecl) -> bindings._Action:
|
|
137
|
+
match action:
|
|
138
|
+
case LetDecl(name, typed_expr):
|
|
139
|
+
return bindings.Let(name, self.typed_expr_to_egg(typed_expr))
|
|
140
|
+
case SetDecl(tp, call, rhs):
|
|
141
|
+
self.type_ref_to_egg(tp)
|
|
142
|
+
call_ = self.expr_to_egg(call)
|
|
143
|
+
return bindings.Set(call_.name, call_.args, self.expr_to_egg(rhs))
|
|
144
|
+
case ExprActionDecl(typed_expr):
|
|
145
|
+
return bindings.Expr_(self.typed_expr_to_egg(typed_expr))
|
|
146
|
+
case ChangeDecl(tp, call, change):
|
|
147
|
+
self.type_ref_to_egg(tp)
|
|
148
|
+
call_ = self.expr_to_egg(call)
|
|
149
|
+
egg_change: bindings._Change
|
|
150
|
+
match change:
|
|
151
|
+
case "delete":
|
|
152
|
+
egg_change = bindings.Delete()
|
|
153
|
+
case "subsume":
|
|
154
|
+
egg_change = bindings.Subsume()
|
|
155
|
+
case _:
|
|
156
|
+
assert_never(change)
|
|
157
|
+
return bindings.Change(egg_change, call_.name, call_.args)
|
|
158
|
+
case UnionDecl(tp, lhs, rhs):
|
|
159
|
+
self.type_ref_to_egg(tp)
|
|
160
|
+
return bindings.Union(self.expr_to_egg(lhs), self.expr_to_egg(rhs))
|
|
161
|
+
case PanicDecl(name):
|
|
162
|
+
return bindings.Panic(name)
|
|
163
|
+
case _:
|
|
164
|
+
assert_never(action)
|
|
165
|
+
|
|
166
|
+
def fact_to_egg(self, fact: FactDecl) -> bindings._Fact:
|
|
167
|
+
match fact:
|
|
168
|
+
case EqDecl(tp, exprs):
|
|
169
|
+
self.type_ref_to_egg(tp)
|
|
170
|
+
return bindings.Eq([self.expr_to_egg(e) for e in exprs])
|
|
171
|
+
case ExprFactDecl(typed_expr):
|
|
172
|
+
return bindings.Fact(self.typed_expr_to_egg(typed_expr))
|
|
173
|
+
case _:
|
|
174
|
+
assert_never(fact)
|
|
175
|
+
|
|
176
|
+
def callable_ref_to_egg(self, ref: CallableRef) -> str:
|
|
177
|
+
"""
|
|
178
|
+
Returns the egg function name for a callable reference, registering it if it is not already registered.
|
|
179
|
+
"""
|
|
180
|
+
if ref in self.callable_ref_to_egg_fn:
|
|
181
|
+
return self.callable_ref_to_egg_fn[ref]
|
|
182
|
+
decl = self.__egg_decls__.get_callable_decl(ref)
|
|
183
|
+
self.callable_ref_to_egg_fn[ref] = egg_name = decl.egg_name or _generate_callable_egg_name(ref)
|
|
184
|
+
self.egg_fn_to_callable_refs[egg_name].add(ref)
|
|
185
|
+
match decl:
|
|
186
|
+
case RelationDecl(arg_types, _, _):
|
|
187
|
+
self.egraph.run_program(bindings.Relation(egg_name, [self.type_ref_to_egg(a) for a in arg_types]))
|
|
188
|
+
case ConstantDecl(tp, _):
|
|
189
|
+
# Use function decleration instead of constant b/c constants cannot be extracted
|
|
190
|
+
# https://github.com/egraphs-good/egglog/issues/334
|
|
191
|
+
self.egraph.run_program(
|
|
192
|
+
bindings.Function(bindings.FunctionDecl(egg_name, bindings.Schema([], self.type_ref_to_egg(tp))))
|
|
193
|
+
)
|
|
194
|
+
case FunctionDecl():
|
|
195
|
+
if not decl.builtin:
|
|
196
|
+
signature = decl.signature
|
|
197
|
+
assert isinstance(signature, FunctionSignature), "Cannot turn special function to egg"
|
|
198
|
+
egg_fn_decl = bindings.FunctionDecl(
|
|
199
|
+
egg_name,
|
|
200
|
+
bindings.Schema(
|
|
201
|
+
[self.type_ref_to_egg(a.to_just()) for a in signature.arg_types],
|
|
202
|
+
self.type_ref_to_egg(signature.semantic_return_type.to_just()),
|
|
203
|
+
),
|
|
204
|
+
self.expr_to_egg(decl.default) if decl.default else None,
|
|
205
|
+
self.expr_to_egg(decl.merge) if decl.merge else None,
|
|
206
|
+
[self.action_to_egg(a) for a in decl.on_merge],
|
|
207
|
+
decl.cost,
|
|
208
|
+
decl.unextractable,
|
|
209
|
+
)
|
|
210
|
+
self.egraph.run_program(bindings.Function(egg_fn_decl))
|
|
211
|
+
case _:
|
|
212
|
+
assert_never(decl)
|
|
213
|
+
return egg_name
|
|
214
|
+
|
|
215
|
+
def type_ref_to_egg(self, ref: JustTypeRef) -> str:
|
|
216
|
+
"""
|
|
217
|
+
Returns the egg sort name for a type reference, registering it if it is not already registered.
|
|
218
|
+
"""
|
|
219
|
+
try:
|
|
220
|
+
return self.type_ref_to_egg_sort[ref]
|
|
221
|
+
except KeyError:
|
|
222
|
+
pass
|
|
223
|
+
decl = self.__egg_decls__._classes[ref.name]
|
|
224
|
+
self.type_ref_to_egg_sort[ref] = egg_name = decl.egg_name or _generate_type_egg_name(ref)
|
|
225
|
+
if not decl.builtin or ref.args:
|
|
226
|
+
if ref.args:
|
|
227
|
+
if ref.name == "UnstableFn":
|
|
228
|
+
# UnstableFn is a special case, where the rest of args are collected into a call
|
|
229
|
+
type_args: list[bindings._Expr] = [
|
|
230
|
+
bindings.Call(
|
|
231
|
+
self.type_ref_to_egg(ref.args[1]),
|
|
232
|
+
[bindings.Var(self.type_ref_to_egg(a)) for a in ref.args[2:]],
|
|
233
|
+
),
|
|
234
|
+
bindings.Var(self.type_ref_to_egg(ref.args[0])),
|
|
235
|
+
]
|
|
236
|
+
else:
|
|
237
|
+
type_args = [bindings.Var(self.type_ref_to_egg(a)) for a in ref.args]
|
|
238
|
+
args = (self.type_ref_to_egg(JustTypeRef(ref.name)), type_args)
|
|
239
|
+
else:
|
|
240
|
+
args = None
|
|
241
|
+
self.egraph.run_program(bindings.Sort(egg_name, args))
|
|
242
|
+
# For builtin classes, let's also make sure we have the mapping of all egg fn names for class methods, because
|
|
243
|
+
# these can be created even without adding them to the e-graph, like `vec-empty` which can be extracted
|
|
244
|
+
# even if you never use that function.
|
|
245
|
+
if decl.builtin:
|
|
246
|
+
for method in decl.class_methods:
|
|
247
|
+
self.callable_ref_to_egg(ClassMethodRef(ref.name, method))
|
|
248
|
+
|
|
249
|
+
return egg_name
|
|
250
|
+
|
|
251
|
+
def op_mapping(self) -> dict[str, str]:
|
|
252
|
+
"""
|
|
253
|
+
Create a mapping of egglog function name to Python function name, for use in the serialized format
|
|
254
|
+
for better visualization.
|
|
255
|
+
"""
|
|
256
|
+
return {
|
|
257
|
+
k: pretty_callable_ref(self.__egg_decls__, next(iter(v)))
|
|
258
|
+
for k, v in self.egg_fn_to_callable_refs.items()
|
|
259
|
+
if len(v) == 1
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
def typed_expr_to_egg(self, typed_expr_decl: TypedExprDecl) -> bindings._Expr:
|
|
263
|
+
self.type_ref_to_egg(typed_expr_decl.tp)
|
|
264
|
+
return self.expr_to_egg(typed_expr_decl.expr)
|
|
265
|
+
|
|
266
|
+
@overload
|
|
267
|
+
def expr_to_egg(self, expr_decl: CallDecl) -> bindings.Call: ...
|
|
268
|
+
|
|
269
|
+
@overload
|
|
270
|
+
def expr_to_egg(self, expr_decl: ExprDecl) -> bindings._Expr: ...
|
|
271
|
+
|
|
272
|
+
def expr_to_egg(self, expr_decl: ExprDecl) -> bindings._Expr:
|
|
273
|
+
"""
|
|
274
|
+
Convert an ExprDecl to an egg expression.
|
|
275
|
+
|
|
276
|
+
Cached using weakrefs to avoid memory leaks.
|
|
277
|
+
"""
|
|
278
|
+
try:
|
|
279
|
+
return self.expr_to_egg_cache[expr_decl]
|
|
280
|
+
except KeyError:
|
|
281
|
+
pass
|
|
282
|
+
|
|
283
|
+
res: bindings._Expr
|
|
284
|
+
match expr_decl:
|
|
285
|
+
case VarDecl(name):
|
|
286
|
+
res = bindings.Var(name)
|
|
287
|
+
case LitDecl(value):
|
|
288
|
+
l: bindings._Literal
|
|
289
|
+
match value:
|
|
290
|
+
case None:
|
|
291
|
+
l = bindings.Unit()
|
|
292
|
+
case bool(i):
|
|
293
|
+
l = bindings.Bool(i)
|
|
294
|
+
case int(i):
|
|
295
|
+
l = bindings.Int(i)
|
|
296
|
+
case float(f):
|
|
297
|
+
l = bindings.F64(f)
|
|
298
|
+
case str(s):
|
|
299
|
+
l = bindings.String(s)
|
|
300
|
+
case _:
|
|
301
|
+
assert_never(value)
|
|
302
|
+
res = bindings.Lit(l)
|
|
303
|
+
case CallDecl(ref, args, _):
|
|
304
|
+
egg_fn = self.callable_ref_to_egg(ref)
|
|
305
|
+
egg_args = [self.typed_expr_to_egg(a) for a in args]
|
|
306
|
+
res = bindings.Call(egg_fn, egg_args)
|
|
307
|
+
case PyObjectDecl(value):
|
|
308
|
+
res = GLOBAL_PY_OBJECT_SORT.store(value)
|
|
309
|
+
case PartialCallDecl(call_decl):
|
|
310
|
+
egg_fn_call = self.expr_to_egg(call_decl)
|
|
311
|
+
res = bindings.Call("unstable-fn", [bindings.Lit(bindings.String(egg_fn_call.name)), *egg_fn_call.args])
|
|
312
|
+
case _:
|
|
313
|
+
assert_never(expr_decl.expr)
|
|
314
|
+
|
|
315
|
+
self.expr_to_egg_cache[expr_decl] = res
|
|
316
|
+
return res
|
|
317
|
+
|
|
318
|
+
def exprs_from_egg(
|
|
319
|
+
self, termdag: bindings.TermDag, terms: list[bindings._Term], tp: JustTypeRef
|
|
320
|
+
) -> Iterable[TypedExprDecl]:
|
|
321
|
+
"""
|
|
322
|
+
Create a function that can convert from an egg term to a typed expr.
|
|
323
|
+
"""
|
|
324
|
+
state = FromEggState(self, termdag)
|
|
325
|
+
return [state.from_expr(tp, term) for term in terms]
|
|
326
|
+
|
|
327
|
+
def _get_possible_types(self, cls_name: str) -> frozenset[JustTypeRef]:
|
|
328
|
+
"""
|
|
329
|
+
Given a class name, returns all possible registered types that it can be.
|
|
330
|
+
"""
|
|
331
|
+
return frozenset(tp for tp in self.type_ref_to_egg_sort if tp.name == cls_name)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def _generate_type_egg_name(ref: JustTypeRef) -> str:
|
|
335
|
+
"""
|
|
336
|
+
Generates an egg sort name for this type reference by linearizing the type.
|
|
337
|
+
"""
|
|
338
|
+
name = ref.name
|
|
339
|
+
if not ref.args:
|
|
340
|
+
return name
|
|
341
|
+
return f"{name}_{'_'.join(map(_generate_type_egg_name, ref.args))}"
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _generate_callable_egg_name(ref: CallableRef) -> str:
|
|
345
|
+
"""
|
|
346
|
+
Generates a valid egg function name for a callable reference.
|
|
347
|
+
"""
|
|
348
|
+
match ref:
|
|
349
|
+
case FunctionRef(name) | ConstantRef(name):
|
|
350
|
+
return name
|
|
351
|
+
case (
|
|
352
|
+
MethodRef(cls_name, name)
|
|
353
|
+
| ClassMethodRef(cls_name, name)
|
|
354
|
+
| ClassVariableRef(cls_name, name)
|
|
355
|
+
| PropertyRef(cls_name, name)
|
|
356
|
+
):
|
|
357
|
+
return f"{cls_name}_{name}"
|
|
358
|
+
case _:
|
|
359
|
+
assert_never(ref)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
@dataclass
|
|
363
|
+
class FromEggState:
|
|
364
|
+
"""
|
|
365
|
+
Dataclass containing state used when converting from an egg term to a typed expr.
|
|
366
|
+
"""
|
|
367
|
+
|
|
368
|
+
state: EGraphState
|
|
369
|
+
termdag: bindings.TermDag
|
|
370
|
+
# Cache of termdag ID to TypedExprDecl
|
|
371
|
+
cache: dict[int, TypedExprDecl] = field(default_factory=dict)
|
|
372
|
+
|
|
373
|
+
@property
|
|
374
|
+
def decls(self) -> Declarations:
|
|
375
|
+
return self.state.__egg_decls__
|
|
376
|
+
|
|
377
|
+
def from_expr(self, tp: JustTypeRef, term: bindings._Term) -> TypedExprDecl:
|
|
378
|
+
"""
|
|
379
|
+
Convert an egg term to a typed expr.
|
|
380
|
+
"""
|
|
381
|
+
expr_decl: ExprDecl
|
|
382
|
+
if isinstance(term, bindings.TermVar):
|
|
383
|
+
expr_decl = VarDecl(term.name)
|
|
384
|
+
elif isinstance(term, bindings.TermLit):
|
|
385
|
+
value = term.value
|
|
386
|
+
expr_decl = LitDecl(None if isinstance(value, bindings.Unit) else value.value)
|
|
387
|
+
elif isinstance(term, bindings.TermApp):
|
|
388
|
+
if term.name == "py-object":
|
|
389
|
+
call = bindings.termdag_term_to_expr(self.termdag, term)
|
|
390
|
+
expr_decl = PyObjectDecl(self.state.egraph.eval_py_object(call))
|
|
391
|
+
if term.name == "unstable-fn":
|
|
392
|
+
# Get function name
|
|
393
|
+
fn_term, *arg_terms = term.args
|
|
394
|
+
fn_value = self.resolve_term(fn_term, JustTypeRef("String"))
|
|
395
|
+
assert isinstance(fn_value.expr, LitDecl)
|
|
396
|
+
fn_name = fn_value.expr.value
|
|
397
|
+
assert isinstance(fn_name, str)
|
|
398
|
+
|
|
399
|
+
# Resolve what types the partiallied applied args are
|
|
400
|
+
assert tp.name == "UnstableFn"
|
|
401
|
+
call_decl = self.from_call(tp.args[0], bindings.TermApp(fn_name, arg_terms))
|
|
402
|
+
expr_decl = PartialCallDecl(call_decl)
|
|
403
|
+
else:
|
|
404
|
+
expr_decl = self.from_call(tp, term)
|
|
405
|
+
else:
|
|
406
|
+
assert_never(term)
|
|
407
|
+
return TypedExprDecl(tp, expr_decl)
|
|
408
|
+
|
|
409
|
+
def from_call(
|
|
410
|
+
self,
|
|
411
|
+
tp: JustTypeRef,
|
|
412
|
+
term: bindings.TermApp, # additional_arg_tps: tuple[JustTypeRef, ...]
|
|
413
|
+
) -> CallDecl:
|
|
414
|
+
"""
|
|
415
|
+
Convert a call to a CallDecl.
|
|
416
|
+
|
|
417
|
+
There could be Python call refs which match the call, so we need to find the correct one.
|
|
418
|
+
|
|
419
|
+
The additional_arg_tps are known types for arguments that come after the term args, used to infer types
|
|
420
|
+
for partially applied functions, where we know the types of the later args, but not of the earlier ones where
|
|
421
|
+
we have values for.
|
|
422
|
+
"""
|
|
423
|
+
# Find the first callable ref that matches the call
|
|
424
|
+
for callable_ref in self.state.egg_fn_to_callable_refs[term.name]:
|
|
425
|
+
# If this is a classmethod, we might need the type params that were bound for this type
|
|
426
|
+
# This could be multiple types if the classmethod is ambiguous, like map create.
|
|
427
|
+
possible_types: Iterable[JustTypeRef | None]
|
|
428
|
+
signature = self.decls.get_callable_decl(callable_ref).to_function_decl().signature
|
|
429
|
+
assert isinstance(signature, FunctionSignature)
|
|
430
|
+
if isinstance(callable_ref, ClassMethodRef):
|
|
431
|
+
possible_types = self.state._get_possible_types(callable_ref.class_name)
|
|
432
|
+
cls_name = callable_ref.class_name
|
|
433
|
+
else:
|
|
434
|
+
possible_types = [None]
|
|
435
|
+
cls_name = None
|
|
436
|
+
for possible_type in possible_types:
|
|
437
|
+
tcs = TypeConstraintSolver(self.decls)
|
|
438
|
+
if possible_type and possible_type.args:
|
|
439
|
+
tcs.bind_class(possible_type)
|
|
440
|
+
|
|
441
|
+
try:
|
|
442
|
+
arg_types, bound_tp_params = tcs.infer_arg_types(
|
|
443
|
+
signature.arg_types, signature.semantic_return_type, signature.var_arg_type, tp, cls_name
|
|
444
|
+
)
|
|
445
|
+
except TypeConstraintError:
|
|
446
|
+
continue
|
|
447
|
+
args = tuple(self.resolve_term(a, tp) for a, tp in zip(term.args, arg_types, strict=False))
|
|
448
|
+
return CallDecl(callable_ref, args, bound_tp_params)
|
|
449
|
+
raise ValueError(f"Could not find callable ref for call {term}")
|
|
450
|
+
|
|
451
|
+
def resolve_term(self, term_id: int, tp: JustTypeRef) -> TypedExprDecl:
|
|
452
|
+
try:
|
|
453
|
+
return self.cache[term_id]
|
|
454
|
+
except KeyError:
|
|
455
|
+
res = self.cache[term_id] = self.from_expr(tp, self.termdag.nodes[term_id])
|
|
456
|
+
return res
|