-
Notifications
You must be signed in to change notification settings - Fork 2
/
expr.go
339 lines (278 loc) · 7.02 KB
/
expr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package shapes
import (
"fmt"
)
//go-sumtype:decl Expr
// Expr represents an expression. The following types are Expr:
//
// Shape | Abstract | Arrow | Compound
// Var | Size | UnaryOp
// IndexOf | TransposeOf | SliceOf | RepeatOf | ConcatOf
// Sli | Axis | Axes
//
// A compact BNF is as follows:
//
// E := S | A | E → E | (E s.t. X)
// a | Sz | Π E | Σ E | D E
// I n E | T []Ax E | L : E | R Ax n E | C Ax E E
// : | Ax | []Ax
type Expr interface {
isExpr()
depth() int // depth of the expression at at that point
substitutable
}
// Var represents a variable. A variable can represent:
// - a Shape (e.g. a in the expression a → b)
// - a Size (e.g. (a, b))
// - a Slice (in Arrow expressions)
type Var rune
func (v Var) isSizelike() {}
func (v Var) isSlicelike() {}
func (v Var) isExpr() {}
func (v Var) Format(s fmt.State, r rune) { fmt.Fprintf(s, "%c", rune(v)) }
func (v Var) apply(ss substitutions) substitutable {
if len(ss) == 0 {
return v
}
for _, s := range ss {
if s.For == v {
return s.Sub
}
}
return v
}
func (v Var) freevars() varset { return varset{v} }
func (v Var) subExprs() []substitutableExpr { return nil }
func (v Var) depth() int { return 0 }
// Axis represents an axis in doing shape stuff.
type Axis int
const (
AllAxes Axis = -65535
)
func (a Axis) isExpr() {}
func (a Axis) apply(ss substitutions) substitutable { return a }
func (a Axis) freevars() varset { return nil }
func (a Axis) subExprs() []substitutableExpr { return nil }
func (a Axis) depth() int { return 0 }
func (a Axis) Format(s fmt.State, c rune) {
if c == 'x' {
if a == AllAxes {
fmt.Fprintf(s, "⁼")
return
}
fmt.Fprintf(s, supInt(int(a)))
return
}
if a == AllAxes {
fmt.Fprintf(s, ":")
return
}
fmt.Fprintf(s, "%d", int(a))
}
func ResolveAxis(a Axis, s Shapelike) Axis {
if a == AllAxes {
return AllAxes
}
if a < 0 {
return Axis(s.Dims() + int(a))
}
return a
}
// Axes represents a list of axes.
// Despite being a container type (i.e. an Axis is an Expr),
// it returns nil for Exprs(). This is because we want to treat Axes as a monolithic entity.
type Axes []Axis
func (a Axes) isExpr() {}
func (a Axes) Format(s fmt.State, r rune) {
if r == 'x' {
fmt.Fprintf(s, supInts(axesToInts(a)))
return
}
fmt.Fprintf(s, "X%v", axesToInts(a))
}
func (a Axes) apply(ss substitutions) substitutable { return a }
func (a Axes) freevars() varset { return nil }
func (a Axes) subExprs() []substitutableExpr { return nil }
func (a Axes) depth() int { return 1 } // Axes is a single term
func (a Axes) Dims() int { return len(a) }
func (a Axes) AsInts() []int { return axesToInts(a) }
func (a Axes) Eq(other Axes) bool {
return len(a) == len(other) && intsEq(axesToInts(a), axesToInts(other))
}
// Size represents a size of a dimension/axis
type Size int
func (s Size) isExpr() {}
func (s Size) isSizelike() {}
func (s Size) apply(ss substitutions) substitutable { return s }
func (s Size) freevars() varset { return nil }
func (s Size) subExprs() []substitutableExpr { return nil }
func (s Size) depth() int { return 1 }
func (s Size) Format(f fmt.State, r rune) { fmt.Fprintf(f, "%d", int(s)) }
// Size also implements Operation (i.e. it's a Const)
func (s Size) isValid() bool { return true }
func (s Size) resolveSize() (Size, error) { return s, nil }
// Sizes are a list of sizes.
type Sizes []Size
func (s Sizes) isExpr() {}
func (s Sizes) Format(f fmt.State, r rune) { fmt.Fprintf(f, "Sz%v", sizesToInts(s)) }
func (s Sizes) apply(ss substitutions) substitutable { return s }
func (s Sizes) freevars() varset { return nil }
func (s Sizes) subExprs() []substitutableExpr { return nil }
func (s Sizes) depth() int { return 1 }
func (s Sizes) AsInts() []int { return sizesToInts(s) }
// complex expressions
// Arrow represents a function of shapes, from A → B. Arrows are right associative.
type Arrow struct {
A, B Expr
}
// MakeArrow is a utility function for writing correct Arrow expressions.
//
// Consider for example, matrix multiplication. It is written plainly as follows:
//
// MatMul: (a, b) → (b, c) → (a, c)
//
// However, note that because arrows are right associative and they're a binary operator, it
// actually is more correctly written like this:
//
// MatMul: (a, b) → ((b, c) → (a, c))
//
// This makes writing plain Arrow{} expressions a bit fraught with errors (see example).
// Thus, the MakeArrow function is created to help write more correct expressions.
func MakeArrow(exprs ...Expr) Arrow {
if len(exprs) < 2 {
panic("Expect at least two expressions to make an Arrow")
}
a := Arrow{A: exprs[0]}
if len(exprs) > 2 {
a.B = MakeArrow(exprs[1:]...)
} else {
a.B = exprs[1]
}
return a
}
func (a Arrow) isExpr() {}
func (a Arrow) depth() int { return max(a.A.depth(), a.B.depth()) + 1 }
func (a Arrow) Format(s fmt.State, r rune) {
if _, ok := a.A.(Arrow); ok {
fmt.Fprintf(s, "(%v) → %v", a.A, a.B)
return
}
fmt.Fprintf(s, "%v → %v", a.A, a.B)
}
func (a Arrow) apply(ss substitutions) substitutable {
return Arrow{
A: a.A.apply(ss).(Expr),
B: a.B.apply(ss).(Expr),
}
}
func (a Arrow) freevars() varset { return arrowToTup(&a).freevars() }
func (a Arrow) subExprs() []substitutableExpr {
return []substitutableExpr{a.A.(substitutableExpr), a.B.(substitutableExpr)}
}
/* Example
MatMul:
(a, b) → (b, c) → (a, c)
is represented as:
Arrow {
Arrow {
Abstract{Var('a'), Var('b')},
Abstract{Var('b'), Var('c')},
},
Abstract{Var('a'), Var('c')},
}
Add:
a → a → a
is represented as:
Arrow {
Arrow {
Var('a'),
Var('a'),
},
Var('a'),
}
Flatten/Ravel:
a → Πa
is represented as:
Arrow {
Var('a'),
UnaryOp {Prod, Var('a')},
}
Sum:
a → ()
is represented as:
Arrow {
Var('a'),
Shape{},
}
Transpose:
a → Axes → Tr Axes a
is represented as:
Arrow {
Arrow{
Var('a'),
axes,
},
TransposeOf {
axes,
Var('a'),
},
}
Slice:
a → Sli → SliceOf Sli a
if represented as:
Arrow {
Arrow {
Var('a'),
sli,
},
SliceOf {
sli,
Var('a'),
}
}
More complicated examples:
Reshape:
a → b → b s.t. (Πa = Πb)
is represented as
Compound {
Arrow {
Arrow {
Var('a'),
Var('b'),
},
Var('b'),
},
SubjectTo {
Eq,
UnaryOp{Prod, Var('a')},
UnaryOp{Prod, Var('b')},
}
}
Sum a matrix columnwise (along axis 1):
(a → Axes→ Tr Axes a) → b s.t. (D b = D a - 1)
is represented as:
Compound {
Arrow {
Arrow {
Arrow {
Var('a'),
Axes{1,0},
},
TransposeOf {
Axes{1, 0},
Var('a')
},
},
Var('b'),
},
SubjectTo {
Eq,
UnaryOp{Dim, Var('b')},
BinaryOp {
Sub,
UnaryOp {Dim, Var('a')},
Size(1),
}
},
}
*/