-
Notifications
You must be signed in to change notification settings - Fork 153
/
polytype.go
284 lines (249 loc) · 6.85 KB
/
polytype.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
package semantic
import (
"sort"
"strings"
flatbuffers "github.com/google/flatbuffers/go"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/internal/fbsemantic"
)
// PolyType represents a polytype. This struct is a thin wrapper around
// Go code generated by the FlatBuffers compiler.
type PolyType struct {
fb *fbsemantic.PolyType
}
// For testing we need an exported method that checks whether PolyType.fb is nil.
func (pt PolyType) IsNil() bool {
return pt.fb == nil
}
// NewPolyType returns a new polytype given a flatbuffers polytype.
func NewPolyType(fb *fbsemantic.PolyType) (PolyType, error) {
if fb == nil {
return PolyType{}, errors.New(codes.Internal, "got nil fbsemantic.polytype")
}
return PolyType{fb: fb}, nil
}
// NumVars returns the number of type variables in this polytype.
func (pt PolyType) NumVars() int {
return pt.fb.VarsLength()
}
// Var returns the type variable at ordinal position i.
func (pt PolyType) Var(i int) (*fbsemantic.Var, error) {
if i < 0 || i >= pt.NumVars() {
return nil, errors.Newf(codes.Internal, "request for polytype var out of bounds: %v in %v", i, pt.NumVars())
}
v := new(fbsemantic.Var)
if !pt.fb.Vars(v, i) {
return nil, errors.Newf(codes.Internal, "missing var")
}
return v, nil
}
// NumConstraints returns the number of kind constraints in this polytype.
func (pt PolyType) NumConstraints() int {
return pt.fb.ConsLength()
}
// Constraint returns the constraint at ordinal position i.
func (pt PolyType) Constraint(i int) (*fbsemantic.Constraint, error) {
if i < 0 || i >= pt.NumConstraints() {
return nil, errors.Newf(codes.Internal, "request for constraint out of bounds: %v in %v", i, pt.NumConstraints())
}
c := new(fbsemantic.Constraint)
if !pt.fb.Cons(c, i) {
return nil, errors.Newf(codes.Internal, "missing constraint")
}
return c, nil
}
// SortedConstraints returns the constraints for this polytype sorted by type variable and constraint kind.
func (pt *PolyType) SortedConstraints() ([]*fbsemantic.Constraint, error) {
return pt.sortedConstraints(nil)
}
func (pt *PolyType) sortedConstraints(m map[uint64]uint64) ([]*fbsemantic.Constraint, error) {
ncs := pt.NumConstraints()
cs := make([]*fbsemantic.Constraint, ncs)
for i := 0; i < ncs; i++ {
c, err := pt.Constraint(i)
if err != nil {
return nil, err
}
cs[i] = c
}
sort.Slice(cs, func(i, j int) bool {
var tvi, tvj uint64
if m != nil {
var ok bool
tvi, ok = m[cs[i].Tvar(nil).I()]
if !ok {
panic("could not find var mapping")
}
tvj, ok = m[cs[j].Tvar(nil).I()]
if !ok {
panic("could not find var mapping")
}
} else {
tvi, tvj = cs[i].Tvar(nil).I(), cs[j].Tvar(nil).I()
}
if tvi == tvj {
return cs[i].Kind() < cs[j].Kind()
}
return tvi < tvj
})
return cs, nil
}
// Expr returns the monotype expression for this polytype.
func (pt PolyType) Expr() (MonoType, error) {
var tbl flatbuffers.Table
if !pt.fb.Expr(&tbl) {
return MonoType{}, errors.New(codes.Internal, "missing a polytype expr")
}
return NewMonoType(tbl, pt.fb.ExprType())
}
func (pt PolyType) SortedVars() ([]*fbsemantic.Var, error) {
return pt.sortedVars(nil)
}
func (pt PolyType) sortedVars(m map[uint64]uint64) ([]*fbsemantic.Var, error) {
nvars := pt.NumVars()
vars := make([]*fbsemantic.Var, nvars)
for i := 0; i < nvars; i++ {
arg, err := pt.Var(i)
if err != nil {
return nil, err
}
vars[i] = arg
}
sort.Slice(vars, func(i, j int) bool {
var ii, jj uint64
if m != nil {
var ok bool
if ii, ok = m[vars[i].I()]; !ok {
panic("could not find var mapping")
}
if jj, ok = m[vars[j].I()]; !ok {
panic("could not find var mapping")
}
} else {
ii = vars[i].I()
jj = vars[j].I()
}
return ii < jj
})
return vars, nil
}
// String returns a string representation for this polytype.
func (pt PolyType) String() string {
return pt.string(nil)
}
// CanonicalString returns a string representation for this polytype,
// where the tvar numbers are contiguous and indexed starting at zero.
// Tvar numbers are ordered by the order they appear in the monotype expression.
func (pt PolyType) CanonicalString() string {
m, err := pt.getCanonicalMapping()
if err != nil {
return "<" + err.Error() + ">"
}
return pt.string(m)
}
func (pt PolyType) string(m map[uint64]uint64) string {
if pt.fb == nil {
return "<polytype: nil>"
}
var sb strings.Builder
mt, err := pt.Expr()
if err != nil {
return "<" + err.Error() + ">"
}
sb.WriteString(mt.string(m))
cs, err := pt.sortedConstraints(m)
if err != nil {
return "<" + err.Error() + ">"
}
for i, cons := range cs {
tv := cons.Tvar(nil)
k := cons.Kind()
if i != 0 {
sb.WriteString(", ")
} else {
sb.WriteString(" where ")
}
mtv := monoTypeFromVar(tv)
sb.WriteString(mtv.string(m))
sb.WriteString(": ")
sb.WriteString(fbsemantic.EnumNamesKind[k])
}
return sb.String()
}
// GetCanonicalMapping returns a map of type variable numbers to
// canonicalized numbers that start from 0.
// Tests that do type inference will have type variables that are sensitive
// to changes in the standard library, this helps to solve that problem.
func (pt *PolyType) getCanonicalMapping() (map[uint64]uint64, error) {
tvm := make(map[uint64]uint64)
counter := uint64(0)
mt, err := pt.Expr()
if err != nil {
return nil, err
}
if err := mt.getCanonicalMapping(&counter, tvm); err != nil {
return nil, err
}
nvars := pt.NumVars()
for i := 0; i < nvars; i++ {
// Normally all the tvars should already be in the mapping because we
// have already visited the monotype expression.
// However, for the sake of debugging issues like this one
// https://github.com/influxdata/flux/issues/2355
// generate a mapping for the quantified vars just in case.
v, err := pt.Var(i)
if err != nil {
return nil, err
}
updateTVarMap(&counter, tvm, v.I())
}
return tvm, nil
}
type Instantiator struct {
subst Substitutor
inst map[uint64]MonoType
}
type Substitution struct {
TypeMap map[uint64]MonoType
variableCount uint64
}
type Substitutor interface {
Fresh() uint64
Apply(v uint64) (MonoType, bool)
}
func (s *Substitution) Fresh() uint64 {
var v = s.variableCount
s.variableCount += 1
return v
}
func (i *Substitution) Apply(v uint64) (MonoType, bool) {
t, ok := i.TypeMap[v]
return t, ok
}
func (s *Instantiator) Fresh() uint64 {
return s.subst.Fresh()
}
func (i *Instantiator) Apply(v uint64) (MonoType, bool) {
t, ok := i.inst[v]
if ok {
return t, ok
}
return i.subst.Apply(v)
}
func (pt PolyType) Instantiator(subst Substitutor) (Substitutor, error) {
nvars := pt.NumVars()
inst := &Instantiator{subst: subst, inst: make(map[uint64]MonoType)}
for i := 0; i < nvars; i++ {
arg, err := pt.Var(i)
if err != nil {
return inst, err
}
typ, err := NewVarType(subst.Fresh())
if err != nil {
return inst, err
}
inst.inst[arg.I()] = typ
}
return inst, nil
}