egglog 9.0.0__cp313-cp313-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/pretty.py ADDED
@@ -0,0 +1,510 @@
1
+ """
2
+ Pretty printing for declerations.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from collections import Counter, defaultdict
8
+ from dataclasses import dataclass, field
9
+ from typing import TYPE_CHECKING, TypeAlias
10
+
11
+ import black
12
+ from typing_extensions import assert_never
13
+
14
+ from .declarations import *
15
+
16
+ if TYPE_CHECKING:
17
+ from collections.abc import Mapping
18
+
19
+
20
+ __all__ = [
21
+ "BINARY_METHODS",
22
+ "UNARY_METHODS",
23
+ "pretty_callable_ref",
24
+ "pretty_decl",
25
+ ]
26
+ MAX_LINE_LENGTH = 110
27
+ LINE_DIFFERENCE = 10
28
+ BLACK_MODE = black.Mode(line_length=180)
29
+
30
+ # Use this special character in place of the args, so that if the args are inlined
31
+ # in the viz, they will replace it
32
+ ARG_STR = "·"
33
+
34
+ # Special methods which we might want to use as functions
35
+ # Mapping to the operator they represent for pretty printing them
36
+ # https://docs.python.org/3/reference/datamodel.html
37
+ BINARY_METHODS = {
38
+ "__lt__": "<",
39
+ "__le__": "<=",
40
+ "__eq__": "==",
41
+ "__ne__": "!=",
42
+ "__gt__": ">",
43
+ "__ge__": ">=",
44
+ # Numeric
45
+ "__add__": "+",
46
+ "__sub__": "-",
47
+ "__mul__": "*",
48
+ "__matmul__": "@",
49
+ "__truediv__": "/",
50
+ "__floordiv__": "//",
51
+ "__mod__": "%",
52
+ # TODO: Support divmod, with tuple return value
53
+ # "__divmod__": "divmod",
54
+ # TODO: Three arg power
55
+ "__pow__": "**",
56
+ "__lshift__": "<<",
57
+ "__rshift__": ">>",
58
+ "__and__": "&",
59
+ "__xor__": "^",
60
+ "__or__": "|",
61
+ }
62
+
63
+
64
+ UNARY_METHODS = {
65
+ "__pos__": "+",
66
+ "__neg__": "-",
67
+ "__invert__": "~",
68
+ }
69
+
70
+ AllDecls: TypeAlias = RulesetDecl | CombinedRulesetDecl | CommandDecl | ActionDecl | FactDecl | ExprDecl | ScheduleDecl
71
+
72
+
73
+ def pretty_decl(
74
+ decls: Declarations, decl: AllDecls, *, wrapping_fn: str | None = None, ruleset_name: str | None = None
75
+ ) -> str:
76
+ """
77
+ Pretty print a decleration.
78
+
79
+ This will use re-format the result and put the expression on the last line, preceeded by the statements.
80
+ """
81
+ traverse = TraverseContext(decls)
82
+ traverse(decl, toplevel=True)
83
+ pretty = traverse.pretty()
84
+ expr = pretty(decl, ruleset_name=ruleset_name)
85
+ if wrapping_fn:
86
+ expr = f"{wrapping_fn}({expr})"
87
+ program = "\n".join([*pretty.statements, expr])
88
+ try:
89
+ # TODO: Try replacing with ruff for speed
90
+ # https://github.com/amyreese/ruff-api
91
+ return black.format_str(program, mode=BLACK_MODE).strip()
92
+ except black.parsing.InvalidInput:
93
+ return program
94
+
95
+
96
+ def pretty_callable_ref(
97
+ decls: Declarations,
98
+ ref: CallableRef,
99
+ first_arg: ExprDecl | None = None,
100
+ bound_tp_params: tuple[JustTypeRef, ...] | None = None,
101
+ ) -> str:
102
+ """
103
+ Pretty print a callable reference, using a dummy value for
104
+ the args if the function is not in the form `f(x, ...)`.
105
+
106
+ To be used in the visualization.
107
+ """
108
+ # Pass in three dummy args, which are the max used for any operation that
109
+ # is not a generic function call
110
+ args: list[ExprDecl] = [VarDecl(ARG_STR, False)] * 3
111
+ if first_arg:
112
+ args.insert(0, first_arg)
113
+ context = PrettyContext(decls, defaultdict(lambda: 0))
114
+ res = context._call_inner(ref, args, bound_tp_params=bound_tp_params, parens=False)
115
+ # Either returns a function or a function with args. If args are provided, they would just be called,
116
+ # on the function, so return them, because they are dummies
117
+ if isinstance(res, tuple):
118
+ return res[0]
119
+ return res
120
+
121
+
122
+ # TODO: Add a different pretty callable ref that doesnt fill in wholes but instead returns the function
123
+ # so that things like Math.__add__ will be represented properly
124
+
125
+
126
+ @dataclass
127
+ class TraverseContext:
128
+ """
129
+ State for traversing expressions (or declerations that contain expressions), so we can know how many parents each
130
+ expression has.
131
+ """
132
+
133
+ decls: Declarations
134
+
135
+ # All expressions we have seen (incremented the parent counts of all children)
136
+ _seen: set[AllDecls] = field(default_factory=set)
137
+ # The number of parents for each expressions
138
+ parents: Counter[AllDecls] = field(default_factory=Counter)
139
+
140
+ def pretty(self) -> PrettyContext:
141
+ """
142
+ Create a pretty context from the state of this traverse context.
143
+ """
144
+ return PrettyContext(self.decls, self.parents)
145
+
146
+ def __call__(self, decl: AllDecls, toplevel: bool = False) -> None: # noqa: C901, PLR0912
147
+ if not toplevel:
148
+ self.parents[decl] += 1
149
+ if decl in self._seen:
150
+ return
151
+ match decl:
152
+ case RewriteDecl(_, lhs, rhs, conditions) | BiRewriteDecl(_, lhs, rhs, conditions):
153
+ self(lhs)
154
+ self(rhs)
155
+ for cond in conditions:
156
+ self(cond)
157
+ case RuleDecl(head, body, _):
158
+ for action in head:
159
+ self(action)
160
+ for fact in body:
161
+ self(fact)
162
+ case SetDecl(_, lhs, rhs) | UnionDecl(_, lhs, rhs) | EqDecl(_, lhs, rhs):
163
+ self(lhs)
164
+ self(rhs)
165
+ case LetDecl(_, d) | ExprActionDecl(d) | ExprFactDecl(d):
166
+ self(d.expr)
167
+ case ChangeDecl(_, d, _) | SaturateDecl(d) | RepeatDecl(d, _) | ActionCommandDecl(d):
168
+ self(d)
169
+ case PanicDecl(_) | VarDecl(_) | LitDecl(_) | PyObjectDecl(_):
170
+ pass
171
+ case SequenceDecl(decls) | RulesetDecl(decls):
172
+ for de in decls:
173
+ if isinstance(de, DefaultRewriteDecl):
174
+ continue
175
+ self(de)
176
+ case CallDecl(ref, exprs, _):
177
+ match ref:
178
+ case FunctionRef(UnnamedFunctionRef(_, res)):
179
+ self(res.expr)
180
+ case _:
181
+ for e in exprs:
182
+ self(e.expr)
183
+ case RunDecl(_, until):
184
+ if until:
185
+ for f in until:
186
+ self(f)
187
+ case PartialCallDecl(c):
188
+ self(c)
189
+ case CombinedRulesetDecl(_):
190
+ pass
191
+ case DefaultRewriteDecl():
192
+ pass
193
+ case _:
194
+ assert_never(decl)
195
+
196
+ self._seen.add(decl)
197
+
198
+
199
+ @dataclass
200
+ class PrettyContext:
201
+ """
202
+
203
+ We need to build up a list of all the expressions we are pretty printing, so that we can see who has parents and who is mutated
204
+ and create temp variables for them.
205
+
206
+ """
207
+
208
+ decls: Declarations
209
+ parents: Mapping[AllDecls, int]
210
+
211
+ # All the expressions we have saved as names
212
+ names: dict[AllDecls, str] = field(default_factory=dict)
213
+ # A list of statements assigning variables or calling destructive ops
214
+ statements: list[str] = field(default_factory=list)
215
+ # Mapping of type to the number of times we have generated a name for that type, used to generate unique names
216
+ _gen_name_types: dict[str, int] = field(default_factory=lambda: defaultdict(lambda: 0))
217
+
218
+ def __call__(
219
+ self, decl: AllDecls, *, unwrap_lit: bool = False, parens: bool = False, ruleset_name: str | None = None
220
+ ) -> str:
221
+ if decl in self.names:
222
+ return self.names[decl]
223
+ expr, tp_name = self.uncached(decl, unwrap_lit=unwrap_lit, parens=parens, ruleset_name=ruleset_name)
224
+ # We use a heuristic to decide whether to name this sub-expression as a variable
225
+ # The rough goal is to reduce the number of newlines, given our line length of ~180
226
+ # We determine it's worth making a new line for this expression if the total characters
227
+ # it would take up is > than some constant (~ line length).
228
+ line_diff: int = len(expr) - LINE_DIFFERENCE
229
+ n_parents = self.parents[decl]
230
+ if n_parents > 1 and n_parents * line_diff > MAX_LINE_LENGTH:
231
+ self.names[decl] = expr_name = self._name_expr(tp_name, expr, copy_identifier=False)
232
+ return expr_name
233
+ return expr
234
+
235
+ def uncached(self, decl: AllDecls, *, unwrap_lit: bool, parens: bool, ruleset_name: str | None) -> tuple[str, str]: # noqa: C901, PLR0911, PLR0912
236
+ match decl:
237
+ case LitDecl(value):
238
+ match value:
239
+ case None:
240
+ return "Unit()", "Unit"
241
+ case bool(b):
242
+ return str(b) if unwrap_lit else f"Bool({b})", "Bool"
243
+ case int(i):
244
+ return str(i) if unwrap_lit else f"i64({i})", "i64"
245
+ case float(f):
246
+ return str(f) if unwrap_lit else f"f64({f})", "f64"
247
+ case str(s):
248
+ return repr(s) if unwrap_lit else f"String({s!r})", "String"
249
+ assert_never(value)
250
+ case VarDecl(name):
251
+ return name, name
252
+ case CallDecl(_, _, _):
253
+ return self._call(decl, parens)
254
+ case PartialCallDecl(CallDecl(ref, typed_args, _)):
255
+ return self._pretty_partial(ref, [a.expr for a in typed_args], parens), "fn"
256
+ case PyObjectDecl(value):
257
+ return repr(value) if unwrap_lit else f"PyObject({value!r})", "PyObject"
258
+ case ActionCommandDecl(action):
259
+ return self(action), "action"
260
+ case RewriteDecl(_, lhs, rhs, conditions) | BiRewriteDecl(_, lhs, rhs, conditions):
261
+ args = ", ".join(map(self, (rhs, *conditions)))
262
+ fn = "rewrite" if isinstance(decl, RewriteDecl) else "birewrite"
263
+ return f"{fn}({self(lhs)}).to({args})", "rewrite"
264
+ case RuleDecl(head, body, name):
265
+ l = ", ".join(map(self, body))
266
+ if name:
267
+ l += f", name={name}"
268
+ r = ", ".join(map(self, head))
269
+ return f"rule({l}).then({r})", "rule"
270
+ case SetDecl(_, lhs, rhs):
271
+ return f"set_({self(lhs)}).to({self(rhs)})", "action"
272
+ case UnionDecl(_, lhs, rhs):
273
+ return f"union({self(lhs)}).with_({self(rhs)})", "action"
274
+ case LetDecl(name, expr):
275
+ return f"let({name!r}, {self(expr.expr)})", "action"
276
+ case ExprActionDecl(expr):
277
+ return self(expr.expr), "action"
278
+ case ExprFactDecl(expr):
279
+ return self(expr.expr), "fact"
280
+ case ChangeDecl(_, expr, change):
281
+ return f"{change}({self(expr)})", "action"
282
+ case PanicDecl(s):
283
+ return f"panic({s!r})", "action"
284
+ case EqDecl(_, left, right):
285
+ return f"eq({self(left)}).to({self(right)})", "fact"
286
+ case RulesetDecl(rules):
287
+ if ruleset_name:
288
+ return f"ruleset(name={ruleset_name!r})", f"ruleset_{ruleset_name}"
289
+ args = ", ".join(self(r) for r in rules if not isinstance(r, DefaultRewriteDecl))
290
+ return f"ruleset({args})", "ruleset"
291
+ case CombinedRulesetDecl(rulesets):
292
+ if ruleset_name:
293
+ rulesets = (*rulesets, f"name={ruleset_name!r})")
294
+ return f"unstable_combine_rulesets({', '.join(rulesets)})", "combined_ruleset"
295
+ case SaturateDecl(schedule):
296
+ return f"{self(schedule, parens=True)}.saturate()", "schedule"
297
+ case RepeatDecl(schedule, times):
298
+ return f"{self(schedule, parens=True)} * {times}", "schedule"
299
+ case SequenceDecl(schedules):
300
+ if len(schedules) == 2:
301
+ return f"{self(schedules[0], parens=True)} + {self(schedules[1], parens=True)}", "schedule"
302
+ args = ", ".join(map(self, schedules))
303
+ return f"seq({args})", "schedule"
304
+ case RunDecl(ruleset_name, until):
305
+ ruleset = self.decls._rulesets[ruleset_name]
306
+ ruleset_str = self(ruleset, ruleset_name=ruleset_name)
307
+ if not until:
308
+ return ruleset_str, "schedule"
309
+ args = ", ".join(map(self, until))
310
+ return f"run({ruleset_str}, {args})", "schedule"
311
+ case DefaultRewriteDecl():
312
+ msg = "default rewrites should not be pretty printed"
313
+ raise TypeError(msg)
314
+ assert_never(decl)
315
+
316
+ def _call(
317
+ self,
318
+ decl: CallDecl,
319
+ parens: bool,
320
+ ) -> tuple[str, str]:
321
+ """
322
+ Pretty print the call. Also returns if it was saved as a name.
323
+
324
+ :param parens: If true, wrap the call in parens if it is a binary method call.
325
+ """
326
+ args = [a.expr for a in decl.args]
327
+ ref = decl.callable
328
+ # Special case !=
329
+ if decl.callable == FunctionRef("!="):
330
+ l, r = self(args[0]), self(args[1])
331
+ return f"ne({l}).to({r})", "Unit"
332
+ signature = self.decls.get_callable_decl(ref).signature
333
+
334
+ # Determine how many of the last arguments are defaults, by iterating from the end and comparing the arg with the default
335
+ n_defaults = 0
336
+ # Dont try counting defaults for function application
337
+ if isinstance(signature, FunctionSignature):
338
+ for arg, default in zip(
339
+ reversed(args), reversed(signature.arg_defaults), strict=not signature.var_arg_type
340
+ ):
341
+ if arg != default:
342
+ break
343
+ n_defaults += 1
344
+ if n_defaults:
345
+ args = args[:-n_defaults]
346
+
347
+ # If this is a function application, the type is the first type arg of the function object
348
+ if signature == "fn-app":
349
+ tp_name = decl.args[0].tp.args[0].name
350
+ else:
351
+ assert isinstance(signature, FunctionSignature)
352
+ tp_name = signature.semantic_return_type.name
353
+ if isinstance(signature, FunctionSignature) and signature.mutates:
354
+ first_arg = args[0]
355
+ expr_str = self(first_arg)
356
+ # copy an identifier expression iff it has multiple parents (b/c then we can't mutate it directly)
357
+ has_multiple_parents = self.parents[first_arg] > 1
358
+ self.names[decl] = expr_name = self._name_expr(tp_name, expr_str, copy_identifier=has_multiple_parents)
359
+ # Set the first arg to be the name of the mutated arg and return the name
360
+ args[0] = VarDecl(expr_name, True)
361
+ else:
362
+ expr_name = None
363
+ res = self._call_inner(ref, args, decl.bound_tp_params, parens)
364
+ expr = (
365
+ (f"{res[0]}({', '.join(self(a, parens=False, unwrap_lit=True) for a in res[1])})")
366
+ if isinstance(res, tuple)
367
+ else res
368
+ )
369
+ # If we have a name, then we mutated
370
+ if expr_name:
371
+ self.statements.append(expr)
372
+ return expr_name, tp_name
373
+ return expr, tp_name
374
+
375
+ def _call_inner( # noqa: C901, PLR0911, PLR0912
376
+ self,
377
+ ref: CallableRef,
378
+ args: list[ExprDecl],
379
+ bound_tp_params: tuple[JustTypeRef, ...] | None,
380
+ parens: bool,
381
+ ) -> tuple[str, list[ExprDecl]] | str:
382
+ """
383
+ Pretty print the call, returning either the full function call or a tuple of the function and the args.
384
+ """
385
+ match ref:
386
+ case FunctionRef(name):
387
+ return name, args
388
+ case ClassMethodRef(class_name, method_name):
389
+ tp_ref = JustTypeRef(class_name, bound_tp_params or ())
390
+ return f"{tp_ref}.{method_name}", args
391
+ case MethodRef(_class_name, method_name):
392
+ slf, *args = args
393
+ slf = self(slf, parens=True)
394
+ match method_name:
395
+ case _ if method_name in UNARY_METHODS:
396
+ expr = f"{UNARY_METHODS[method_name]}{slf}"
397
+ return f"({expr})" if parens else expr
398
+ case _ if method_name in BINARY_METHODS:
399
+ expr = f"{slf} {BINARY_METHODS[method_name]} {self(args[0], parens=True, unwrap_lit=True)}"
400
+ return f"({expr})" if parens else expr
401
+ case "__getitem__":
402
+ return f"{slf}[{self(args[0], unwrap_lit=True)}]"
403
+ case "__call__":
404
+ return slf, args
405
+ case "__delitem__":
406
+ return f"del {slf}[{self(args[0], unwrap_lit=True)}]"
407
+ case "__setitem__":
408
+ return f"{slf}[{self(args[0], unwrap_lit=True)}] = {self(args[1], unwrap_lit=True)}"
409
+ case _:
410
+ return f"{slf}.{method_name}", args
411
+ case ConstantRef(name):
412
+ return name
413
+ case ClassVariableRef(class_name, variable_name):
414
+ return f"{class_name}.{variable_name}"
415
+ case PropertyRef(_class_name, property_name):
416
+ return f"{self(args[0], parens=True)}.{property_name}"
417
+ case InitRef(class_name):
418
+ tp_ref = JustTypeRef(class_name, bound_tp_params or ())
419
+ return str(tp_ref), args
420
+ case UnnamedFunctionRef():
421
+ expr = self._pretty_function_body(ref, [])
422
+ return f"({expr})", args
423
+ assert_never(ref)
424
+
425
+ def _generate_name(self, typ: str) -> str:
426
+ self._gen_name_types[typ] += 1
427
+ return f"_{typ}_{self._gen_name_types[typ]}"
428
+
429
+ def _name_expr(self, tp_name: str, expr_str: str, copy_identifier: bool) -> str:
430
+ # tp_name =
431
+ # If the thing we are naming is already a variable, we don't need to name it
432
+ if expr_str.isidentifier():
433
+ if copy_identifier:
434
+ name = self._generate_name(tp_name)
435
+ self.statements.append(f"{name} = copy({expr_str})")
436
+ else:
437
+ name = expr_str
438
+ else:
439
+ name = self._generate_name(tp_name)
440
+ self.statements.append(f"{name} = {expr_str}")
441
+ return name
442
+
443
+ def _pretty_partial(self, ref: CallableRef, args: list[ExprDecl], parens: bool) -> str:
444
+ """
445
+ Returns a partial function call as a string.
446
+ """
447
+ match ref:
448
+ case FunctionRef(name):
449
+ fn = name
450
+ case UnnamedFunctionRef():
451
+ res = self._pretty_function_body(ref, args)
452
+ return f"({res})" if parens else res
453
+ case (
454
+ ClassMethodRef(class_name, method_name)
455
+ | MethodRef(class_name, method_name)
456
+ | PropertyRef(class_name, method_name)
457
+ ):
458
+ fn = f"{class_name}.{method_name}"
459
+ case InitRef(class_name):
460
+ fn = class_name
461
+ case ConstantRef(_):
462
+ msg = "Constants should not be callable"
463
+ raise NotImplementedError(msg)
464
+ case ClassVariableRef(_, _):
465
+ msg = "Class variables should not be callable"
466
+ raise NotADirectoryError(msg)
467
+ case _:
468
+ assert_never(ref)
469
+ if not args:
470
+ return fn
471
+ arg_strs = (
472
+ fn,
473
+ *(self(a, parens=False, unwrap_lit=True) for a in args),
474
+ )
475
+ return f"partial({', '.join(arg_strs)})"
476
+
477
+ def _pretty_function_body(self, fn: UnnamedFunctionRef, args: list[ExprDecl]) -> str:
478
+ """
479
+ Pretty print the body of a function, partially applying some arguments.
480
+ """
481
+ var_args = fn.args
482
+ replacements = {var_arg: TypedExprDecl(var_arg.tp, arg) for var_arg, arg in zip(var_args, args, strict=False)}
483
+ var_args = var_args[len(args) :]
484
+ res = replace_typed_expr(fn.res, replacements)
485
+ arg_names = fn.args[len(args) :]
486
+ prefix = "lambda"
487
+ if arg_names:
488
+ prefix += f" {', '.join(self(a.expr) for a in arg_names)}"
489
+ return f"{prefix}: {self(res.expr)}"
490
+
491
+
492
+ def _plot_line_length(expr: object): # pragma: no cover
493
+ """
494
+ Plots the number of line lengths based on different max lengths
495
+ """
496
+ global MAX_LINE_LENGTH, LINE_DIFFERENCE
497
+ import altair as alt
498
+ import pandas as pd
499
+
500
+ sizes = []
501
+ for line_length in range(40, 180, 10):
502
+ MAX_LINE_LENGTH = line_length
503
+ for diff in range(0, 40, 5):
504
+ LINE_DIFFERENCE = diff
505
+ new_l = len(str(expr).split())
506
+ sizes.append((line_length, diff, new_l))
507
+
508
+ df = pd.DataFrame(sizes, columns=["MAX_LINE_LENGTH", "LENGTH_DIFFERENCE", "n"])
509
+
510
+ return alt.Chart(df).mark_rect().encode(x="MAX_LINE_LENGTH:O", y="LENGTH_DIFFERENCE:O", color="n:Q")
egglog/py.typed ADDED
File without changes