You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/python-integration.md
+51Lines changed: 51 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,9 @@ We define a custom "primitive sort" (i.e. a builtin type) for `PyObject`s. This
15
15
To create an expression of type `PyObject`, we have to use the `egraph.save_object` method. This method takes a Python object and returns an expression of type `PyObject`.
This is a bit subtle at the moment, and we plan on adding an easier wrapper to eval arbitrary Python code in the future.
95
+
96
+
## "Preserved" methods
97
+
98
+
You can use the the `@egraph.method(preserve=True)` decorator to mark a method as "preserved", meaning that calling it will actually execute the body of the function and a coresponding egglog function will not be created,
99
+
100
+
Normally, all methods defined on a egglog `Expr` will ignore their bodies and simply build an expression object based on the arguments.
101
+
102
+
However, there are times in Python when you need the return type of a method to be an instance of a particular Python type, and some similar acting expression won't cut it.
103
+
104
+
For example, let's say you are implementing a `Bool` expression, but you want to be able to use it in `if` statements in Python. That means it needs to define a `__bool__` methods which returns a Python `bool`, based on evaluating the expression.
105
+
106
+
```{code-cell} python
107
+
@egraph.class_
108
+
class Bool(Expr):
109
+
@egraph.method(preserve=True)
110
+
def __bool__(self) -> bool:
111
+
# Add this expression converted to a Python object to the e-graph
0 commit comments