Skip to content

Commit f0be5d6

Browse files
Document properties and conversions
1 parent 2550590 commit f0be5d6

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

docs/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
changelog
55
reference/high-level
66
reference/egglog-translation
7+
reference/python-integration
78
reference/bindings
89
```

docs/reference/egglog-translation.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ The currently unsupported features are:
1212

1313
- Proof mode: Not currently tested, but could add support if needed.
1414
- Naive mode: Not currently exposed, but could add support
15-
- `(include ...)`: This command includes another `.egg` file, so not sure how this would translate to Python.
1615
- `(output ...)`: No examples in the tests, so not sure how this works.
1716
- `(calc ...)`: Could be implemented, but haven't yet.
1817

@@ -145,14 +144,14 @@ Note that by default, the egg name for any method is the Python class name combi
145144
# (Num i64)
146145
# (Var String)
147146
# (Add Math Math)
148-
# (Mul Math Math))
147+
# (Mul Math Math)
148+
# (Neg Math))
149149
150150
@egraph.class_
151151
class Math(Expr):
152152
@egraph.method(egg_fn="Num")
153153
def __init__(self, v: i64Like):
154154
...
155-
156155
@egraph.method(egg_fn="Var")
157156
@classmethod
158157
def var(cls, v: StringLike) -> Math:
@@ -166,8 +165,28 @@ class Math(Expr):
166165
def __mul__(self, other: Math) -> Math:
167166
...
168167
169-
# egg: (Mul (Num 2) (Add (Var "x") (Num 3))))
170-
Math(2) * (Math.var("x") + Math(3))
168+
@egraph.method(egg_fn="Neg")
169+
@property
170+
def neg(self) -> Math:
171+
...
172+
173+
# egg: (Neg (Mul (Num 2) (Add (Var "x") (Num 3)))))
174+
(Math(2) * (Math.var("x") + Math(3))).neg
175+
```
176+
177+
As shown above, we can also use the `@classmethod` and `@property` decorators to define class methods and properties.
178+
179+
#### Custom Type Promotion
180+
181+
Similar to how an `int` can be automatically upcasted to an `i64`, we also support registering conversion to your custom types. For example:
182+
183+
```{code-cell} python
184+
converter(int, Math, Math)
185+
converter(str, Math, Math.var)
186+
187+
Math(2) + 30 + "x"
188+
# equal to
189+
Math(2) + Math(i64(30)) + Math.var(String("x"))
171190
```
172191

173192
### Declarations
@@ -328,7 +347,7 @@ Since it uses a fluent API, static type checkers can verify that the type of the
328347

329348
The `(birewrite ...)` command in egglog is syntactic sugar for creating two rewrites, one in each direction. In Python, we can use the `birewrite(expr).to(expr, *when)` function to create two rules that rewrite in each direction.
330349

331-
### Using funcitons to define vars
350+
### Using functions to define vars
332351

333352
Instead of defining variables with `vars_`, we can also use functions to define variables. This can be more succinct
334353
and also will make sure the variables won't be used outside of the scope of the function.

docs/reference/python-integration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
file_format: mystnb
3+
---
4+
5+
# Python Integration
6+
7+
Alongside [the support for builtin `egglog` functionality](./egglog-translation.md), `egglog` also provides functionality to more easily integrate with the Python ecosystem.
8+
9+
## Methods, Classmethods, and P

0 commit comments

Comments
 (0)