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
+84-1Lines changed: 84 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,4 +6,87 @@ file_format: mystnb
6
6
7
7
Alongside [the support for builtin `egglog` functionality](./egglog-translation.md), `egglog` also provides functionality to more easily integrate with the Python ecosystem.
8
8
9
-
## Methods, Classmethods, and P
9
+
## Custom Python Objects
10
+
11
+
We define a custom "primitive sort" (i.e. a builtin type) for `PyObject`s. This allows us to store any Python object in the e-graph.
12
+
13
+
### Saving Python Objects
14
+
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`.
16
+
17
+
```{code-cell} python
18
+
from egglog import *
19
+
egraph = EGraph()
20
+
one = egraph.save_object(1)
21
+
one
22
+
```
23
+
24
+
We see that this as saved internally as a pointer to the Python object. For hashable objects like `int` we store two integers, a hash of the type and a has of the value.
25
+
26
+
We can also store unhashable objects in the e-graph like lists.
27
+
28
+
```{code-cell} python
29
+
egraph.save_object([1, 2, 3])
30
+
```
31
+
32
+
We see that this is stored with one number, simply the `id` of the object.
33
+
34
+
```{admonition} Mutable Objects
35
+
:class: warning
36
+
37
+
While it is possible to store unhashable objects in the e-graph, you have to be careful defining any rules which create new unhashable objects. If each time a rule is run, it creates a new object, then the e-graph will never saturate.
38
+
39
+
Creating hashable objects is safer, since while the rule might create new Python objects each time it executes, they should have the same hash, i.e. be equal, so that the e-graph can saturate.
40
+
```
41
+
42
+
### Retrieving Python Objects
43
+
44
+
The inverse of `egraph.save_object` is `egraph.load_object`. This takes an expression of type `PyObject` and returns the Python object it represents.
45
+
46
+
```{code-cell} python
47
+
egraph.load_object(one)
48
+
```
49
+
50
+
### Builtin methods
51
+
52
+
Currently, we only support a few methods on `PyObject`s, but we plan to add more in the future.
Alongside this, we support a function `dict_update` method, which can allow you to combine some local local egglog expressions alongside, say, the locals and globals of the Python code you are evaling.
78
+
79
+
```{code-cell} python
80
+
# Need this from our globals()
81
+
def my_add(a, b):
82
+
return a + b
83
+
84
+
locals_expr = egraph.save_object(locals())
85
+
globals_expr = egraph.save_object(globals())
86
+
# Need `one` to map to the expression for `1` not the Python object of the expression
0 commit comments