egglog 9.0.0__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of egglog might be problematic. Click here for more details.

@@ -0,0 +1,144 @@
1
+ # mypy: disable-error-code="empty-body"
2
+
3
+ """
4
+ N-Dimensional Arrays
5
+ ====================
6
+
7
+ Example of building NDarray in the vein of Mathemetics of Arrays.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from egglog import *
13
+
14
+ egraph = EGraph()
15
+
16
+
17
+ class Value(Expr):
18
+ def __init__(self, v: i64Like) -> None: ...
19
+
20
+ def __mul__(self, other: Value) -> Value: ...
21
+
22
+ def __add__(self, other: Value) -> Value: ...
23
+
24
+
25
+ i, j = vars_("i j", i64)
26
+ egraph.register(
27
+ rewrite(Value(i) * Value(j)).to(Value(i * j)),
28
+ rewrite(Value(i) + Value(j)).to(Value(i + j)),
29
+ )
30
+
31
+
32
+ class Values(Expr):
33
+ def __init__(self, v: Vec[Value]) -> None: ...
34
+
35
+ def __getitem__(self, idx: Value) -> Value: ...
36
+
37
+ def length(self) -> Value: ...
38
+
39
+ def concat(self, other: Values) -> Values: ...
40
+
41
+
42
+ @egraph.register
43
+ def _values(vs: Vec[Value], other: Vec[Value]):
44
+ yield rewrite(Values(vs)[Value(i)]).to(vs[i])
45
+ yield rewrite(Values(vs).length()).to(Value(vs.length()))
46
+ yield rewrite(Values(vs).concat(Values(other))).to(Values(vs.append(other)))
47
+ # yield rewrite(l.concat(r).length()).to(l.length() + r.length())
48
+ # yield rewrite(l.concat(r)[idx])
49
+
50
+
51
+ class NDArray(Expr):
52
+ """
53
+ An n-dimensional array.
54
+ """
55
+
56
+ def __getitem__(self, idx: Values) -> Value: ...
57
+
58
+ def shape(self) -> Values: ...
59
+
60
+
61
+ @function
62
+ def arange(n: Value) -> NDArray: ...
63
+
64
+
65
+ @egraph.register
66
+ def _ndarray_arange(n: Value, idx: Values):
67
+ yield rewrite(arange(n).shape()).to(Values(Vec(n)))
68
+ yield rewrite(arange(n)[idx]).to(idx[Value(0)])
69
+
70
+
71
+ def assert_simplifies(left: Expr, right: Expr) -> None:
72
+ """
73
+ Simplify and print
74
+ """
75
+ egraph.register(left)
76
+ egraph.run(30)
77
+ res = egraph.extract(left)
78
+ print(f"{left} == {right} ➡ {res}")
79
+ egraph.check(eq(left).to(right))
80
+
81
+
82
+ assert_simplifies(arange(Value(10)).shape(), Values(Vec(Value(10))))
83
+ assert_simplifies(arange(Value(10))[Values(Vec(Value(0)))], Value(0))
84
+ assert_simplifies(arange(Value(10))[Values(Vec(Value(1)))], Value(1))
85
+
86
+
87
+ @function
88
+ def py_value(s: StringLike) -> Value: ...
89
+
90
+
91
+ @egraph.register
92
+ def _py_value(l: String, r: String):
93
+ yield rewrite(py_value(l) + py_value(r)).to(py_value(join(l, " + ", r)))
94
+ yield rewrite(py_value(l) * py_value(r)).to(py_value(join(l, " * ", r)))
95
+
96
+
97
+ @function
98
+ def py_values(s: StringLike) -> Values: ...
99
+
100
+
101
+ @egraph.register
102
+ def _py_values(l: String, r: String):
103
+ yield rewrite(py_values(l)[py_value(r)]).to(py_value(join(l, "[", r, "]")))
104
+ yield rewrite(py_values(l).length()).to(py_value(join("len(", l, ")")))
105
+ yield rewrite(py_values(l).concat(py_values(r))).to(py_values(join(l, " + ", r)))
106
+
107
+
108
+ @function
109
+ def py_ndarray(s: StringLike) -> NDArray: ...
110
+
111
+
112
+ @egraph.register
113
+ def _py_ndarray(l: String, r: String):
114
+ yield rewrite(py_ndarray(l)[py_values(r)]).to(py_value(join(l, "[", r, "]")))
115
+ yield rewrite(py_ndarray(l).shape()).to(py_values(join(l, ".shape")))
116
+ yield rewrite(arange(py_value(l))).to(py_ndarray(join("np.arange(", l, ")")))
117
+
118
+
119
+ assert_simplifies(py_ndarray("x").shape(), py_values("x.shape"))
120
+ assert_simplifies(arange(py_value("x"))[py_values("y")], py_value("np.arange(x)[y]"))
121
+ # assert_simplifies(arange(py_value("x"))[py_values("y")], py_value("y[0]"))
122
+
123
+
124
+ @function
125
+ def cross(l: NDArray, r: NDArray) -> NDArray: ...
126
+
127
+
128
+ @egraph.register
129
+ def _cross(l: NDArray, r: NDArray, idx: Values):
130
+ yield rewrite(cross(l, r).shape()).to(l.shape().concat(r.shape()))
131
+ yield rewrite(cross(l, r)[idx]).to(l[idx] * r[idx])
132
+
133
+
134
+ assert_simplifies(cross(arange(Value(10)), arange(Value(11))).shape(), Values(Vec(Value(10), Value(11))))
135
+ assert_simplifies(cross(py_ndarray("x"), py_ndarray("y")).shape(), py_values("x.shape + y.shape"))
136
+ assert_simplifies(cross(py_ndarray("x"), py_ndarray("y"))[py_values("idx")], py_value("x[idx] * y[idx]"))
137
+
138
+
139
+ @egraph.register
140
+ def _cross_py(l: String, r: String):
141
+ yield rewrite(cross(py_ndarray(l), py_ndarray(r))).to(py_ndarray(join("np.multiply.outer(", l, ", ", r, ")")))
142
+
143
+
144
+ assert_simplifies(cross(py_ndarray("x"), py_ndarray("y"))[py_values("idx")], py_value("np.multiply.outer(x, y)[idx]"))
@@ -0,0 +1,84 @@
1
+ """
2
+ Resolution theorem proving.
3
+ ===========================
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from typing import ClassVar
9
+
10
+ from egglog import *
11
+
12
+ egraph = EGraph()
13
+
14
+
15
+ class Boolean(Expr):
16
+ FALSE: ClassVar[Boolean]
17
+
18
+ def __or__(self, other: Boolean) -> Boolean: # type: ignore[empty-body]
19
+ ...
20
+
21
+ def __invert__(self) -> Boolean: # type: ignore[empty-body]
22
+ ...
23
+
24
+
25
+ # Show off two ways of creating constants, either as top level values or as classvars
26
+ T = constant("T", Boolean)
27
+ F = Boolean.FALSE
28
+
29
+ p, a, b, c, as_, bs = vars_("p a b c as bs", Boolean)
30
+ egraph.register(
31
+ # clauses are assumed in the normal form (or a (or b (or c False)))
32
+ union(~F).with_(T),
33
+ union(~T).with_(F),
34
+ # "Solving" negation equations
35
+ rule(eq(~p).to(T)).then(union(p).with_(F)),
36
+ rule(eq(~p).to(F)).then(union(p).with_(T)),
37
+ # canonicalize associtivity. "append" for clauses terminate with false
38
+ rewrite((a | b) | c).to(a | (b | c)),
39
+ # commutativity
40
+ rewrite(a | (b | c)).to(b | (a | c)),
41
+ # absoprtion
42
+ rewrite(a | (a | b)).to(a | b),
43
+ rewrite(a | (~a | b)).to(T),
44
+ # Simplification
45
+ rewrite(F | a).to(a),
46
+ rewrite(a | F).to(a),
47
+ rewrite(T | a).to(T),
48
+ rewrite(a | T).to(T),
49
+ # unit propagation
50
+ # This is kind of interesting actually.
51
+ # Looks a bit like equation solving
52
+ rule(eq(T).to(p | F)).then(union(p).with_(T)),
53
+ # resolution
54
+ # This counts on commutativity to bubble everything possible up to the front of the clause.
55
+ rule(
56
+ eq(T).to(a | as_),
57
+ eq(T).to(~a | bs),
58
+ ).then(
59
+ union(as_ | bs).with_(T),
60
+ ),
61
+ )
62
+
63
+
64
+ # Example predicate
65
+ @function
66
+ def pred(x: i64Like) -> Boolean: # type: ignore[empty-body]
67
+ ...
68
+
69
+
70
+ p0 = egraph.let("p0", pred(0))
71
+ p1 = egraph.let("p1", pred(1))
72
+ p2 = egraph.let("p2", pred(2))
73
+ egraph.register(
74
+ union(p1 | (~p2 | F)).with_(T),
75
+ union(p2 | (~p0 | F)).with_(T),
76
+ union(p0 | (~p1 | F)).with_(T),
77
+ union(p1).with_(F),
78
+ union(~p0 | (~p1 | (p2 | F))).with_(T),
79
+ )
80
+ egraph.run(10)
81
+ egraph.check(ne(T).to(F))
82
+ egraph.check(eq(p0).to(F))
83
+ egraph.check(eq(p2).to(F))
84
+ egraph
@@ -0,0 +1,34 @@
1
+ """
2
+ Schedule demo
3
+ =============
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from egglog import *
9
+
10
+ left = relation("left", i64)
11
+ right = relation("right", i64)
12
+
13
+
14
+ x, y = vars_("x y", i64)
15
+ step_left = ruleset(
16
+ rule(
17
+ left(x),
18
+ right(x),
19
+ ).then(left(x + 1)),
20
+ )
21
+ step_right = ruleset(
22
+ rule(
23
+ left(x),
24
+ right(y),
25
+ eq(x).to(y + 1),
26
+ ).then(right(x)),
27
+ )
28
+
29
+ egraph = EGraph()
30
+ egraph.register(left(i64(0)), right(i64(0)))
31
+ egraph.run((step_right.saturate() + step_left.saturate()) * 10)
32
+ egraph.check(left(i64(10)), right(i64(9)))
33
+ egraph.check_fail(left(i64(11)), right(i64(10)))
34
+ egraph
egglog/exp/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """
2
+ Experimental interfaces built on egglog.
3
+ """