Skip to content

Commit de2f0a8

Browse files
tmp
1 parent 75f69ab commit de2f0a8

11 files changed

Lines changed: 690 additions & 563 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ordered-float = "5"
2828
uuid = { version = "1.18", features = ["v4"] }
2929
rayon = "1.11"
3030
base64 = "0.22.1"
31+
tempfile = "3.24.0"
3132

3233
# Use patched version of egglog in experimental
3334
[patch.'https://github.com/egraphs-good/egglog']

python/egglog/builtins.py

Lines changed: 88 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __str__(self) -> str:
8181

8282
class Unit(BuiltinExpr, egg_sort="Unit"):
8383
"""
84-
The unit type. This is used to reprsent if a value exists in the e-graph or not.
84+
The unit type. This is used to represent if a value exists in the e-graph or not.
8585
"""
8686

8787
def __init__(self) -> None: ...
@@ -288,6 +288,9 @@ def bool_ge(self, other: i64Like) -> Bool: ...
288288
@method(egg_fn="abs")
289289
def __abs__(self) -> i64: ...
290290

291+
@method(egg_fn="vec-range")
292+
def range(self) -> Vec[i64]: ...
293+
291294

292295
# The types which can be converted into an i64
293296
i64Like: TypeAlias = i64 | int # noqa: N816, PYI042
@@ -1024,6 +1027,9 @@ def set(self, index: i64Like, value: T) -> Vec[T]: ...
10241027
@method(egg_fn="vec-union")
10251028
def __or__(self, other: Vec[T]) -> Vec[T]: ...
10261029

1030+
@method(egg_fn="unstable-vec-map", reverse_args=True)
1031+
def map(self, fn: Callable[[T], V]) -> Vec[V]: ...
1032+
10271033

10281034
for sequence_type in (list, tuple):
10291035
converter(
@@ -1036,87 +1042,6 @@ def __or__(self, other: Vec[T]) -> Vec[T]: ...
10361042

10371043
VecLike: TypeAlias = Vec[T] | tuple[TO, ...] | list[TO]
10381044

1039-
v = Vec(i64(10))
1040-
v.append([i64(10)])
1041-
1042-
1043-
class PyObject(BuiltinExpr, egg_sort="PyObject"):
1044-
@method(preserve=True)
1045-
@deprecated("use .value")
1046-
def eval(self) -> object:
1047-
return self.value
1048-
1049-
@method(preserve=True) # type: ignore[prop-decorator]
1050-
@property
1051-
def value(self) -> object:
1052-
expr = cast("RuntimeExpr", self).__egg_typed_expr__.expr
1053-
if not isinstance(expr, PyObjectDecl):
1054-
raise ExprValueError(self, "PyObject(x)")
1055-
return cloudpickle.loads(expr.pickled)
1056-
1057-
__match_args__ = ("value",)
1058-
1059-
def __init__(self, value: object) -> None: ...
1060-
1061-
@method(egg_fn="py-call")
1062-
def __call__(self, *args: object) -> PyObject: ...
1063-
1064-
@method(egg_fn="py-call-extended")
1065-
def call_extended(self, args: PyObject, kwargs: PyObject) -> PyObject:
1066-
"""
1067-
Call the PyObject with the given args and kwargs PyObjects.
1068-
"""
1069-
1070-
@method(egg_fn="py-from-string")
1071-
@classmethod
1072-
def from_string(cls, s: StringLike) -> PyObject: ...
1073-
1074-
@method(egg_fn="py-to-string")
1075-
def to_string(self) -> String: ...
1076-
1077-
@method(egg_fn="py-to-bool")
1078-
def to_bool(self) -> Bool: ...
1079-
1080-
@method(egg_fn="py-dict-update")
1081-
def dict_update(self, *keys_and_values: object) -> PyObject: ...
1082-
1083-
@method(egg_fn="py-from-int")
1084-
@classmethod
1085-
def from_int(cls, i: i64Like) -> PyObject: ...
1086-
1087-
@method(egg_fn="py-dict")
1088-
@classmethod
1089-
def dict(cls, *keys_and_values: object) -> PyObject: ...
1090-
1091-
1092-
converter(object, PyObject, PyObject)
1093-
1094-
1095-
@function(builtin=True, egg_fn="py-eval")
1096-
def py_eval(code: StringLike, globals: object = PyObject.dict(), locals: object = PyObject.dict()) -> PyObject: ...
1097-
1098-
1099-
class PyObjectFunction(Protocol):
1100-
def __call__(self, *__args: PyObject) -> PyObject: ...
1101-
1102-
1103-
@deprecated("use PyObject(fn) directly")
1104-
def py_eval_fn(fn: Callable) -> PyObjectFunction:
1105-
"""
1106-
Takes a python callable and maps it to a callable which takes and returns PyObjects.
1107-
1108-
It translates it to a call which uses `py_eval` to call the function, passing in the
1109-
args as locals, and using the globals from function.
1110-
"""
1111-
return PyObject(fn)
1112-
1113-
1114-
@function(builtin=True, egg_fn="py-exec")
1115-
def py_exec(code: StringLike, globals: object = PyObject.dict(), locals: object = PyObject.dict()) -> PyObject:
1116-
"""
1117-
Copies the locals, execs the Python code, and returns the locals with any updates.
1118-
"""
1119-
11201045

11211046
TS = TypeVarTuple("TS")
11221047

@@ -1167,9 +1092,9 @@ def __call__(self, *args: *TS) -> T: ...
11671092

11681093

11691094
# Method Type is for builtins like __getitem__
1170-
converter(MethodType, UnstableFn, lambda m: UnstableFn[*get_type_args()](m.__func__, m.__self__))
1171-
converter(RuntimeFunction, UnstableFn, lambda rf: UnstableFn[*get_type_args()](rf))
1172-
converter(partial, UnstableFn, lambda p: UnstableFn[*get_type_args()](p.func, *p.args))
1095+
converter(MethodType, UnstableFn, lambda m: UnstableFn[*get_type_args()](m.__func__, m.__self__)) # type: ignore[operator, misc]
1096+
converter(RuntimeFunction, UnstableFn, lambda rf: UnstableFn[*get_type_args()](rf)) # type: ignore[operator, misc]
1097+
converter(partial, UnstableFn, lambda p: UnstableFn[*get_type_args()](p.func, *p.args)) # type: ignore[operator, misc]
11731098

11741099

11751100
def _convert_function(fn: FunctionType) -> UnstableFn:
@@ -1207,5 +1132,83 @@ def _convert_function(fn: FunctionType) -> UnstableFn:
12071132
converter(FunctionType, UnstableFn, _convert_function)
12081133

12091134

1135+
class PyObject(BuiltinExpr, egg_sort="PyObject"):
1136+
@method(preserve=True)
1137+
@deprecated("use .value")
1138+
def eval(self) -> object:
1139+
return self.value
1140+
1141+
@method(preserve=True) # type: ignore[prop-decorator]
1142+
@property
1143+
def value(self) -> object:
1144+
expr = cast("RuntimeExpr", self).__egg_typed_expr__.expr
1145+
if not isinstance(expr, PyObjectDecl):
1146+
raise ExprValueError(self, "PyObject(x)")
1147+
return cloudpickle.loads(expr.pickled)
1148+
1149+
__match_args__ = ("value",)
1150+
1151+
def __init__(self, value: object) -> None: ...
1152+
1153+
@method(egg_fn="py-call")
1154+
def __call__(self, *args: object) -> PyObject: ...
1155+
1156+
@method(egg_fn="py-call-extended")
1157+
def call_extended(self, args: PyObject, kwargs: PyObject) -> PyObject:
1158+
"""
1159+
Call the PyObject with the given args and kwargs PyObjects.
1160+
"""
1161+
1162+
@method(egg_fn="py-from-string")
1163+
@classmethod
1164+
def from_string(cls, s: StringLike) -> PyObject: ...
1165+
1166+
@method(egg_fn="py-to-string")
1167+
def to_string(self) -> String: ...
1168+
1169+
@method(egg_fn="py-to-bool")
1170+
def to_bool(self) -> Bool: ...
1171+
1172+
@method(egg_fn="py-dict-update")
1173+
def dict_update(self, *keys_and_values: object) -> PyObject: ...
1174+
1175+
@method(egg_fn="py-from-int")
1176+
@classmethod
1177+
def from_int(cls, i: i64Like) -> PyObject: ...
1178+
1179+
@method(egg_fn="py-dict")
1180+
@classmethod
1181+
def dict(cls, *keys_and_values: object) -> PyObject: ...
1182+
1183+
1184+
converter(object, PyObject, PyObject)
1185+
1186+
1187+
@function(builtin=True, egg_fn="py-eval")
1188+
def py_eval(code: StringLike, globals: object = PyObject.dict(), locals: object = PyObject.dict()) -> PyObject: ...
1189+
1190+
1191+
class PyObjectFunction(Protocol):
1192+
def __call__(self, *__args: PyObject) -> PyObject: ...
1193+
1194+
1195+
@deprecated("use PyObject(fn) directly")
1196+
def py_eval_fn(fn: Callable) -> PyObjectFunction:
1197+
"""
1198+
Takes a python callable and maps it to a callable which takes and returns PyObjects.
1199+
1200+
It translates it to a call which uses `py_eval` to call the function, passing in the
1201+
args as locals, and using the globals from function.
1202+
"""
1203+
return PyObject(fn)
1204+
1205+
1206+
@function(builtin=True, egg_fn="py-exec")
1207+
def py_exec(code: StringLike, globals: object = PyObject.dict(), locals: object = PyObject.dict()) -> PyObject:
1208+
"""
1209+
Copies the locals, execs the Python code, and returns the locals with any updates.
1210+
"""
1211+
1212+
12101213
Container: TypeAlias = Map | Set | MultiSet | Vec | UnstableFn
12111214
Primitive: TypeAlias = String | Bool | i64 | f64 | Rational | BigInt | BigRat | PyObject | Unit

python/egglog/conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
_CONVERSION_DECLS = Declarations.create()
2525
# Defer a list of declerations to be added to the global declerations, so that we can not trigger them procesing
2626
# until we need them
27-
_TO_PROCESS_DECLS: list[DeclerationsLike] = []
27+
_TO_PROCESS_DECLS: list[DeclarationsLike] = []
2828

2929

3030
def retrieve_conversion_decls() -> Declarations:

python/egglog/declarations.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
"ConstructorDecl",
5151
"Declarations",
5252
"Declarations",
53-
"DeclerationsLike",
53+
"DeclarationsLike",
5454
"DefaultRewriteDecl",
55-
"DelayedDeclerations",
55+
"DelayedDeclarations",
5656
"DummyDecl",
5757
"EqDecl",
5858
"ExprActionDecl",
@@ -63,7 +63,7 @@
6363
"FunctionRef",
6464
"FunctionSignature",
6565
"GetCostDecl",
66-
"HasDeclerations",
66+
"HasDeclarations",
6767
"Ident",
6868
"InitRef",
6969
"JustTypeRef",
@@ -101,12 +101,12 @@
101101
"ValueDecl",
102102
"collect_unbound_vars",
103103
"replace_typed_expr",
104-
"upcast_declerations",
104+
"upcast_declarations",
105105
]
106106

107107

108108
@dataclass(match_args=False)
109-
class DelayedDeclerations:
109+
class DelayedDeclarations:
110110
__egg_decls_thunk__: Callable[[], Declarations] = field(repr=False)
111111

112112
@property
@@ -122,20 +122,20 @@ def __egg_decls__(self) -> Declarations:
122122

123123

124124
@runtime_checkable
125-
class HasDeclerations(Protocol):
125+
class HasDeclarations(Protocol):
126126
@property
127127
def __egg_decls__(self) -> Declarations: ...
128128

129129

130-
DeclerationsLike: TypeAlias = Union[HasDeclerations, None, "Declarations"]
130+
DeclarationsLike: TypeAlias = Union[HasDeclarations, None, "Declarations"]
131131

132132

133-
def upcast_declerations(declerations_like: Iterable[DeclerationsLike]) -> list[Declarations]:
133+
def upcast_declarations(declarations_like: Iterable[DeclarationsLike]) -> list[Declarations]:
134134
d = []
135-
for l in declerations_like:
135+
for l in declarations_like:
136136
if l is None:
137137
continue
138-
if isinstance(l, HasDeclerations):
138+
if isinstance(l, HasDeclarations):
139139
d.append(l.__egg_decls__)
140140
elif isinstance(l, Declarations):
141141
d.append(l)
@@ -179,8 +179,8 @@ def default_ruleset(self) -> RulesetDecl:
179179
return ruleset
180180

181181
@classmethod
182-
def create(cls, *others: DeclerationsLike) -> Declarations:
183-
others = upcast_declerations(others)
182+
def create(cls, *others: DeclarationsLike) -> Declarations:
183+
others = upcast_declarations(others)
184184
if not others:
185185
return Declarations()
186186
first, *rest = others
@@ -195,26 +195,26 @@ def copy(self) -> Declarations:
195195
self.update_other(new)
196196
return new
197197

198-
def update(self, *others: DeclerationsLike) -> None:
198+
def update(self, *others: DeclarationsLike) -> None:
199199
for other in others:
200200
self |= other
201201

202-
def __or__(self, other: DeclerationsLike) -> Declarations:
202+
def __or__(self, other: DeclarationsLike) -> Declarations:
203203
result = self.copy()
204204
result |= other
205205
return result
206206

207-
def __ior__(self, other: DeclerationsLike) -> Self:
207+
def __ior__(self, other: DeclarationsLike) -> Self:
208208
if other is None:
209209
return self
210-
if isinstance(other, HasDeclerations):
210+
if isinstance(other, HasDeclarations):
211211
other = other.__egg_decls__
212212
other.update_other(self)
213213
return self
214214

215215
def update_other(self, other: Declarations) -> None:
216216
"""
217-
Updates the other decl with these values in palce.
217+
Updates the other decl with these values in place.
218218
"""
219219
other._functions |= self._functions
220220
other._classes |= self._classes
@@ -325,7 +325,7 @@ class ClassDecl:
325325
builtin: bool = False
326326
init: ConstructorDecl | FunctionDecl | None = None
327327
class_methods: dict[str, FunctionDecl | ConstructorDecl] = field(default_factory=dict)
328-
# These have to be seperate from class_methods so that printing them can be done easily
328+
# These have to be separate from class_methods so that printing them can be done easily
329329
class_variables: dict[str, ConstantDecl] = field(default_factory=dict)
330330
methods: dict[str, FunctionDecl | ConstructorDecl] = field(default_factory=dict)
331331
properties: dict[str, FunctionDecl | ConstructorDecl] = field(default_factory=dict)

0 commit comments

Comments
 (0)