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