-
Notifications
You must be signed in to change notification settings - Fork 2
/
abstract.go
326 lines (283 loc) · 6.83 KB
/
abstract.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
package shapes
import (
"fmt"
"github.com/pkg/errors"
)
type rr struct{ start, end rune }
// generator holds the alphabet range allowed for variables
var generator = [...]rr{
{'a', 'z'},
{'α', 'ω'},
}
// Abstract is an abstract shape
type Abstract []Sizelike
// Gen creates an abstract with the provided dims.
// This is particularly useful for generating abstracts for higher order functions.
func Gen(d int) (retVal Abstract) {
if d <= 0 {
panic("Cannot generate an Abstract with d <= 0")
}
type state struct {
rangeNum int
letter rune
}
s := state{0, 'a'}
for i := 0; i < d; i++ {
retVal = append(retVal, Var(s.letter))
s.letter++
if s.letter > generator[s.rangeNum].end {
s.rangeNum++
s.letter = generator[s.rangeNum].start
}
}
return retVal
}
func (a Abstract) Cons(other Conser) (retVal Conser) {
switch ot := other.(type) {
case Shape:
l := len(a)
r := append(a, make(Abstract, len(ot))...)
r = r[:l]
for _, v := range ot {
r = append(r, Size(v))
}
retVal = r
case Abstract:
retVal = append(a, ot...)
}
return retVal
}
func (a Abstract) isConser() {}
func (a Abstract) ToShape() (s Shape, ok bool) {
s = make(Shape, len(a)) // TODO: perf - borrow
for i := range a {
sz, ok := a[i].(Size)
if !ok {
return nil, ok
}
s[i] = int(sz)
}
return s, true
}
func (a Abstract) Clone() Abstract {
retVal := make(Abstract, len(a))
copy(retVal, a)
return retVal
}
func (a Abstract) isExpr() {}
func (a Abstract) depth() int { return 1 }
// Dims returns the number of dimensions in the shape
func (a Abstract) Dims() int { return len(a) }
func (a Abstract) TotalSize() int { panic("Unable to get TotalSize for Abstract") }
func (a Abstract) DimSize(dim int) (Sizelike, error) {
if a.Dims() <= dim {
return nil, errors.Errorf("Cannot get Dim %d of %v", dim, a)
}
return a[dim], nil
}
func (a Abstract) T(axes ...Axis) (newShape Shapelike, err error) {
retVal := make(Abstract, len(a))
copy(retVal, a)
err = genericUnsafePermute(axesToInts(axes), retVal)
newShape = retVal
return
}
func (a Abstract) S(slices ...Slice) (newShape Shapelike, err error) {
if shp, ok := a.ToShape(); ok {
return shp.S(slices...)
}
opDims := len(a)
if len(slices) > opDims {
err = errors.Errorf(dimsMismatch, opDims, len(slices))
return
}
retVal := a.Clone()
for d, size := range a {
var sl Slice // default is a nil Slice
if d <= len(slices)-1 {
sl = slices[d]
}
if sl == nil {
retVal[d] = size
continue
}
switch s := size.(type) {
case Size:
var x int
if x, err = sliceSize(sl, int(s)); err != nil {
return nil, errors.Wrapf(err, "Unable to slice %v. Dim %d caused an error.", a, d)
}
retVal[d] = Size(x)
case Var:
retVal[d] = sizelikeSliceOf{SliceOf{toRange(sl), s}}
case BinOp:
retVal[d] = sizelikeSliceOf{SliceOf{toRange(sl), E2{s}}}
case UnaryOp:
retVal[d] = sizelikeSliceOf{SliceOf{toRange(sl), s}}
default:
return nil, errors.Errorf("%dth sizelike %v of %T is unsupported by S(). Perhaps make a pull request?", d, size, size)
}
// attempt to resolve if possible
if s, ok := retVal[d].(sizeOp); ok && s.isValid() {
if sz, err := s.resolveSize(); err == nil {
retVal[d] = sz
}
}
}
// drop any dimension with size 1, except the last dimension
offset := 0
dims := a.Dims()
for d := 0; d < dims; d++ {
if sz, ok := retVal[d].(Size); ok && sz == 1 && offset+d <= len(slices)-1 && slices[offset+d] != nil {
retVal = append(retVal[:d], retVal[d+1:]...)
d--
dims--
offset++
}
}
if shp, ok := retVal.ToShape(); ok {
if shp.IsScalar() {
return ScalarShape(), nil
}
return shp, nil
}
return retVal, nil
}
func (a Abstract) Repeat(axis Axis, repeats ...int) (retVal Shapelike, finalRepeats []int, size int, err error) {
var newShape Abstract
var sz Sizelike
switch {
case axis == AllAxes:
sz = UnaryOp{Prod, a}
newShape = Abstract{sz}
axis = 0
case a.Dims() == 1 && axis == 1: // "vector"
sz = Size(1)
newShape = a.Clone()
newShape = append(newShape, Size(1))
default:
if int(axis) >= a.Dims() {
// error
err = errors.Errorf(invalidAxis, axis, a.Dims())
return
}
sz = a[axis]
newShape = a.Clone()
}
size = -1
switch s := sz.(type) {
case Size:
size = int(s)
// special case to allow generic repeats
if size > 0 && len(repeats) == 1 {
rep := repeats[0]
repeats = make([]int, size)
for i := range repeats {
repeats[i] = rep
}
}
// optimistically check
reps := len(repeats)
if size > 0 && reps != size {
err = errors.Errorf(broadcastError, size, reps)
return
}
newSize := sumInts(repeats)
newShape[axis] = Size(newSize)
// set return values
finalRepeats = repeats
default:
// special case to allow generic repeats
if len(repeats) == 1 {
rep := Size(repeats[0])
newSize := BinOp{Mul, sz.(Expr), rep}
newShape[axis] = newSize
// set return values
finalRepeats = repeats
} else {
// cannot check if newShape[axis] == len(repeats)
// gotta take it on faith
newSize := sumInts(repeats)
newShape[axis] = Size(newSize)
// don't set finalRepeats. Should be nil
}
}
retVal = newShape
// try to resolve
if x, err := newShape.resolve(); err == nil {
retVal = x.(Shapelike)
}
return
}
func (a Abstract) Concat(axis Axis, others ...Shapelike) (newShape Shapelike, err error) {
panic("not implemented") // TODO: Implement
}
// Format implements fmt.Formatter, and formats a shape nicely
func (s Abstract) Format(st fmt.State, r rune) {
switch r {
case 'v', 's':
st.Write([]byte("("))
for i, v := range s {
switch vt := v.(type) {
case Size:
fmt.Fprintf(st, "%d", int(vt))
case Var:
fmt.Fprintf(st, "%c", rune(vt))
default:
fmt.Fprintf(st, "%v", v)
}
if i < len(s)-1 {
st.Write([]byte(", "))
}
}
st.Write([]byte(")"))
default:
fmt.Fprintf(st, "%v", []Sizelike(s))
}
}
func (s Abstract) apply(ss substitutions) substitutable {
retVal := make(Abstract, len(s))
copy(retVal, s)
for i, a := range s {
st := a.(substitutable)
retVal[i] = st.apply(ss).(Sizelike)
}
return retVal
}
func (s Abstract) freevars() (retVal varset) {
for _, a := range s {
if v, ok := a.(Var); ok {
retVal = append(retVal, v)
}
}
return unique(retVal)
}
func (s Abstract) subExprs() (retVal []substitutableExpr) {
for i := range s {
retVal = append(retVal, s[i].(substitutableExpr))
}
return retVal
}
func (s Abstract) resolve() (Expr, error) {
retVal := s.Clone()
for i, v := range s {
switch r := v.(type) {
case sizeOp:
if !r.isValid() {
retVal[i] = v
continue
}
sz, err := r.resolveSize()
if err != nil {
return nil, errors.Errorf("%dth sizelike of %v is not resolveable to a Size", i, s)
}
retVal[i] = sz
default:
return nil, errors.Errorf("Sizelike of %T is unhandled by Abstract", v)
}
}
if shp, ok := retVal.ToShape(); ok {
return shp, nil
}
return retVal, nil
}