Skip to content

Commit 784b9ab

Browse files
Updates for last talk
1 parent 37e3a3f commit 784b9ab

12 files changed

Lines changed: 4119 additions & 2012 deletions

File tree

Cargo.lock

Lines changed: 22 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ ignore = [
220220
"INP001",
221221
# don't replace lambdas with functions because we need them to defer resolution
222222
"PLW0108",
223+
# unicode vars
224+
"PLC2401"
223225
]
224226
select = ["ALL"]
225227

@@ -252,16 +254,19 @@ preview = true
252254

253255
[tool.mypy]
254256
ignore_missing_imports = true
255-
allow_redefinition = true
257+
allow_redefinition_new = true
256258
exclude = ["__snapshots__", "_build", "^conftest.py$"]
257259
warn_unused_configs = true
258260
disallow_subclassing_any = true
259261
check_untyped_defs = true
260262
warn_redundant_casts = true
261263
warn_unused_ignores = true
264+
warn_unreachable = true
262265
strict_equality = true
263266
extra_checks = true
264267
strict_equality_for_none = true
268+
enable_incomplete_feature = ["TypeForm"]
269+
local_partial_types = true
265270

266271
[tool.maturin]
267272
python-source = "python"

python/egglog/builtins.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"i64",
6767
"i64Like",
6868
"join",
69+
"map_best_common_float_scale",
6970
"map_bigrat_intersect_min",
7071
"map_bigrat_subtract",
7172
"map_contains_key_swapped",
@@ -713,6 +714,10 @@ def map_nonconst_nonunit_f64_values(xs: Map[Map[T, V], f64]) -> MultiSet[f64]: .
713714
def map_divide_all_values_by_f64(factor: f64, xs: Map[T, f64]) -> Map[T, f64]: ...
714715

715716

717+
@function(egg_fn="map-best-common-float-scale", builtin=True)
718+
def map_best_common_float_scale(xs: Map[T, f64]) -> f64: ...
719+
720+
716721
@function(egg_fn="map-integer-residual-split-candidate", builtin=True)
717722
def map_integer_residual_split_candidate(xs: Map[Map[T, BigRat], f64]) -> Pair[Map[T, BigRat], Map[Map[T, BigRat], f64]]: ...
718723

python/egglog/egraph.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import graphviz
3333
from opentelemetry import trace
34-
from typing_extensions import ParamSpec, Unpack
34+
from typing_extensions import ParamSpec, TypeForm, Unpack
3535

3636
from . import bindings
3737
from ._tracing import call_with_current_trace
@@ -1318,7 +1318,7 @@ def extract(
13181318
cost = cast("COST", extract_report.cost)
13191319
else:
13201320
if not isinstance(runtime_expr.__egg_typed_expr__.expr, (LetRefDecl, UnboundVarDecl)):
1321-
self.register(expr)
1321+
self._register_extract_root(runtime_expr)
13221322
egg_cost_model = _CostModel(cost_model, self).to_bindings_cost_model()
13231323
egg_sort = self._state.type_ref_to_egg(tp)
13241324
extractor = call_with_current_trace(bindings.Extractor, [egg_sort], self._state.egraph, egg_cost_model)
@@ -1554,6 +1554,20 @@ def _register_commands(self, cmds: list[Command]) -> None:
15541554
egg_cmds = [egg_cmd for cmd in cmds if (egg_cmd := self._command_to_egg(cmd)) is not None]
15551555
self._state.run_program(*egg_cmds)
15561556

1557+
def _register_extract_root(self, runtime_expr: RuntimeExpr) -> None:
1558+
"""
1559+
Register the exact extraction root without synthetic let factoring.
1560+
1561+
Synthetic lets are a command-size optimization for public registration,
1562+
but custom-cost extraction immediately evaluates the original root value.
1563+
Registering a let-factored presentation can leave the custom extractor
1564+
without a costed parent for that exact value in the direct command API.
1565+
"""
1566+
self._add_decls(runtime_expr)
1567+
action_egg = self._state.action_to_egg(ExprActionDecl(runtime_expr.__egg_typed_expr__), expr_to_let=False)
1568+
if action_egg is not None:
1569+
self._state.run_program(bindings.ActionCommand(action_egg))
1570+
15571571
def _command_to_egg(self, cmd: Command) -> bindings._Command | None:
15581572
ruleset_ident = Ident("")
15591573
cmd_decl: CommandDecl
@@ -1835,7 +1849,7 @@ class Schedule(DelayedDeclarations):
18351849
A composition of some rulesets, either composing them sequentially, running them repeatedly, running them till saturation, or running until some facts are met
18361850
"""
18371851

1838-
# Defer declerations so that we can have rule generators that used not yet defined yet
1852+
# Defer declarations so that we can have rule generators that used not yet defined yet
18391853
schedule: ScheduleDecl
18401854

18411855
def __str__(self) -> str:
@@ -2138,7 +2152,7 @@ def rule(*facts: FactLike, ruleset: None = None, name: str | None = None) -> _Ru
21382152
return _RuleBuilder(facts=_fact_likes(facts), name=name, ruleset=ruleset)
21392153

21402154

2141-
def var(name: str, bound: type[T], egg_name: str | None = None) -> T:
2155+
def var(name: str, bound: TypeForm[T], egg_name: str | None = None) -> T:
21422156
"""Create a new variable with the given name and type."""
21432157
return cast("T", _var(name, bound, egg_name=egg_name))
21442158

python/egglog/egraph_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def ruleset_to_egg(self, ident: Ident) -> None:
410410
def command_to_egg(self, cmd: CommandDecl, ruleset: Ident) -> bindings._Command | None:
411411
match cmd:
412412
case ActionCommandDecl(action):
413-
action_egg = self.action_to_egg(action, expr_to_let=True)
413+
action_egg = self.action_to_egg(action)# , expr_to_let=False)
414414
if not action_egg:
415415
return None
416416
return bindings.ActionCommand(action_egg)

0 commit comments

Comments
 (0)