/
matchq.go
453 lines (418 loc) · 13.3 KB
/
matchq.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package expreduce
const MaxUint = ^uint(0)
const MaxInt = int(MaxUint >> 1)
const MaxUint64 = ^uint64(0)
const MaxInt64 = int64(MaxUint64 >> 1)
type matchIter interface {
// returns ismatch, pd, isdone
next() (bool, *PDManager, bool)
}
type dummyMatchIter struct {
isMatchQ bool
pm *PDManager
isDone bool
}
func (this *dummyMatchIter) next() (bool, *PDManager, bool) {
return this.isMatchQ, this.pm, this.isDone
}
func NewMatchIter(a Ex, b Ex, pm *PDManager, es *EvalState) (matchIter, bool) {
patternHead := ""
patExpr, patIsExpr := b.(*Expression)
if patIsExpr {
sym, isSym := patExpr.Parts[0].(*Symbol)
if isSym {
patternHead = sym.Name
}
}
if patternHead == "System`Except" {
except := patExpr
if len(except.Parts) == 2 {
matchq, _ := IsMatchQ(a, except.Parts[1], EmptyPD(), es)
return &dummyMatchIter{!matchq, pm, true}, true
} else if len(except.Parts) == 3 {
matchq, _ := IsMatchQ(a, except.Parts[1], EmptyPD(), es)
if !matchq {
matchqb, newPm := IsMatchQ(a, except.Parts[2], pm, es)
return &dummyMatchIter{matchqb, newPm, true}, true
}
return &dummyMatchIter{false, pm, true}, true
}
} else if patternHead == "System`Alternatives" {
alts := patExpr
for _, alt := range alts.Parts[1:] {
// I recently changed the third argument from EmptyPD() to pm
// because MatchQ[{a, b}, {a_, k | a_}] was returning True, causing
// problems in some of the boolean patterns. Might need to make
// similar changes to the other pattern clauses.
matchq, newPD := IsMatchQ(a, alt, pm, es)
if matchq {
return &dummyMatchIter{matchq, newPD, true}, true
}
}
return &dummyMatchIter{false, pm, true}, true
} else if patternHead == "System`PatternTest" {
patternTest := patExpr
if len(patternTest.Parts) == 3 {
matchq, newPD := IsMatchQ(a, patternTest.Parts[1], EmptyPD(), es)
if matchq {
// Some Q functions are very simple and occur very often. For
// some of these, skip the Eval() call and return the boolean
// directly.
testSym, testIsSym := patternTest.Parts[2].(*Symbol)
if testIsSym {
var qFunction singleParamQType
if testSym.Name == "System`NumberQ" {
qFunction = numberQ
}
if qFunction != nil {
if qFunction(a) {
return &dummyMatchIter{true, newPD, true}, true
} else {
return &dummyMatchIter{false, pm, true}, true
}
}
}
// I used to create a NewEvalState here, but I have evidence
// that the same evalstate is used:
// MatchQ[1, a_?((mytestval = 999; NumberQ[#]) &)] // Timing
//tmpEs := NewEvalStateNoLog(true)
res := (NewExpression([]Ex{
patternTest.Parts[2],
a,
})).Eval(es)
resSymbol, resIsSymbol := res.(*Symbol)
if resIsSymbol {
if resSymbol.Name == "System`True" {
return &dummyMatchIter{true, newPD, true}, true
}
}
}
return &dummyMatchIter{false, pm, true}, true
}
} else if patternHead == "System`Condition" {
condition := patExpr
if len(condition.Parts) == 3 {
mi, cont := NewMatchIter(a, condition.Parts[1], EmptyPD(), es)
for cont {
matchq, newPD, done := mi.next()
cont = !done
if matchq {
//tmpEs := NewEvalStateNoLog(true)
res := condition.Parts[2].DeepCopy()
res = ReplacePD(res, es, newPD).Eval(es)
resSymbol, resIsSymbol := res.(*Symbol)
if resIsSymbol {
if resSymbol.Name == "System`True" {
return &dummyMatchIter{true, newPD, true}, true
}
}
}
}
}
} else if patternHead == "System`Optional" {
optional := patExpr
if len(optional.Parts) == 2 {
matchq, newPD := IsMatchQ(a, optional.Parts[1], pm, es)
if matchq {
return &dummyMatchIter{matchq, newPD, true}, true
}
}
} else if patternHead == "System`HoldPattern" {
holdPattern := patExpr
if len(holdPattern.Parts) == 2 {
return NewMatchIter(a, holdPattern.Parts[1], pm, es)
}
}
// Continue normally
_, aIsFlt := a.(*Flt)
_, aIsInteger := a.(*Integer)
_, aIsString := a.(*String)
_, aIsSymbol := a.(*Symbol)
aRational, aIsRational := a.(*Rational)
bRational, bIsRational := b.(*Rational)
aExpression, aIsExpression := a.(*Expression)
bExpression, bIsExpression := b.(*Expression)
// Special case for the operator form of Verbatim
forceOrdered := false
verbatimOp, opExpr, isVerbatimOp := OperatorAssertion(b, "System`Verbatim")
if aIsExpression && isVerbatimOp {
if len(opExpr.Parts) == 2 {
if IsSameQ(aExpression.Parts[0], opExpr.Parts[1], &es.CASLogger) {
b = NewExpression(append([]Ex{opExpr.Parts[1]}, verbatimOp.Parts[1:]...))
bExpression, bIsExpression = b.(*Expression)
forceOrdered = true
}
}
}
// This initial value is just a randomly chosen placeholder
var headEx Ex
if aIsFlt {
headEx = &Symbol{"System`Real"}
} else if aIsInteger {
headEx = &Symbol{"System`Integer"}
} else if aIsString {
headEx = &Symbol{"System`String"}
} else if aIsExpression {
headEx = aExpression.Parts[0]
} else if aIsSymbol {
headEx = &Symbol{"System`Symbol"}
} else if aIsRational {
headEx = &Symbol{"System`Rational"}
}
if IsBlankTypeOnly(b) {
ibtc, ibtcNewPDs := IsBlankTypeCapturing(b, a, headEx, pm, &es.CASLogger)
if ibtc {
return &dummyMatchIter{true, ibtcNewPDs, true}, true
}
return &dummyMatchIter{false, EmptyPD(), true}, true
}
// Handle special case for matching Rational[a_Integer, b_Integer]
if aIsRational && bIsExpression {
matchq, newPm := isMatchQRational(aRational, bExpression, pm, es)
return &dummyMatchIter{matchq, newPm, true}, true
} else if aIsExpression && bIsRational {
matchq, newPm := isMatchQRational(bRational, aExpression, pm, es)
return &dummyMatchIter{matchq, newPm, true}, true
}
canAssumeHead := false
assumingHead := false
if bIsExpression {
bExpressionSym, bExpressionSymOk := bExpression.Parts[0].(*Symbol)
if bExpressionSymOk {
oneIdentity := bExpressionSym.Attrs(&es.defined).OneIdentity
hasDefaultExpr := bExpressionSym.Default(&es.defined) != nil
containsOptional := false
for _, part := range bExpression.Parts[1:] {
if _, isOpt := HeadAssertion(part, "System`Optional"); isOpt {
containsOptional = true
break
}
}
if oneIdentity && hasDefaultExpr && containsOptional {
canAssumeHead = true
}
}
// Handle special case where MatchQ[a,a+c_.] is True
if canAssumeHead && !aIsExpression {
// Normally this would always fail, but if the conditions are right,
// let's configure the variables such that we at least try for a
// sequence match.
assumingHead = true
aIsExpression = true
aExpression = NewExpression([]Ex{bExpressionSym, a})
}
if aIsExpression {
aExpressionSym, aExpressionSymOk := aExpression.Parts[0].(*Symbol)
if canAssumeHead && aExpressionSymOk {
if aExpressionSym.Name != bExpressionSym.Name {
assumingHead = true
aIsExpression = true
aExpression = NewExpression([]Ex{bExpressionSym, a})
}
}
}
}
if !assumingHead {
if aIsFlt || aIsInteger || aIsString || aIsSymbol || aIsRational {
return &dummyMatchIter{IsSameQ(a, b, &es.CASLogger), EmptyPD(), true}, true
} else if !(aIsExpression && bIsExpression) {
return &dummyMatchIter{false, EmptyPD(), true}, true
}
}
attrs := Attributes{}
sequenceHead := "Sequence"
startI := 0
aExpressionSym, aExpressionSymOk := aExpression.Parts[0].(*Symbol)
bExpressionSym, bExpressionSymOk := bExpression.Parts[0].(*Symbol)
if aExpressionSymOk && bExpressionSymOk {
if aExpressionSym.Name == bExpressionSym.Name {
attrs = aExpressionSym.Attrs(&es.defined)
sequenceHead = aExpressionSym.Name
startI = 1
}
}
isOrderless := attrs.Orderless && !forceOrdered
isFlat := attrs.Flat && !forceOrdered
nomi, ok := NewSequenceMatchIter(aExpression.Parts[startI:], bExpression.Parts[startI:], isOrderless, isFlat, sequenceHead, pm, es)
if !ok {
return &dummyMatchIter{false, pm, true}, true
}
return nomi, true
}
func isMatchQRational(a *Rational, b *Expression, pm *PDManager, es *EvalState) (bool, *PDManager) {
return IsMatchQ(
NewExpression([]Ex{
&Symbol{"System`Rational"},
&Integer{a.Num},
&Integer{a.Den},
}),
b, pm, es)
}
type assignedIterState struct {
formI int
assnI int
pm *PDManager
}
type assignedMatchIter struct {
assn [][]int
// Inherited from sequenceMatchIter
components []Ex
lhs_components []parsedForm
pm *PDManager
sequenceHead string
es *EvalState
stack []assignedIterState
}
func NewAssignedMatchIter(assn [][]int, smi *sequenceMatchIter) assignedMatchIter {
ami := assignedMatchIter{}
ami.assn = assn
ami.components = smi.components
ami.lhs_components = smi.lhs_components
ami.pm = smi.pm
ami.sequenceHead = smi.sequenceHead
ami.es = smi.es
ami.stack = []assignedIterState{
{0, 0, CopyPD(ami.pm)},
}
return ami
}
func (ami *assignedMatchIter) next() bool {
for len(ami.stack) > 0 {
var p assignedIterState
l := len(ami.stack)
ami.stack, p = ami.stack[:l-1], ami.stack[l-1]
if p.formI >= len(ami.assn) {
// We found a sequence match!
ami.pm = p.pm
return true
}
lhs := ami.lhs_components[p.formI]
if p.assnI >= len(ami.assn[p.formI]) {
// Reached end of form. Attempt to define the sequence and continue
// on success.
seq := make([]Ex, len(ami.assn[p.formI]))
for i, assignedComp := range ami.assn[p.formI] {
seq[i] = ami.components[assignedComp]
}
patOk := DefineSequence(lhs, seq, p.pm, ami.sequenceHead, ami.es)
if patOk {
ami.stack = append(ami.stack, assignedIterState{
p.formI + 1, 0, p.pm,
})
}
continue
}
//matches, newPm := IsMatchQ(comp, lhs.form, p.pm, ami.es)
//if matches {
comp := ami.components[ami.assn[p.formI][p.assnI]]
toAddReversed := []*PDManager{}
mi, cont := NewMatchIter(comp, lhs.form, p.pm, ami.es)
for cont {
matchq, submatches, done := mi.next()
cont = !done
if matchq {
// TODO: Perhaps check if submatches are different before
// adding?
toAddReversed = append(toAddReversed, submatches)
}
}
for i := len(toAddReversed) - 1; i >= 0; i-- {
updatedPm := p.pm
if toAddReversed[i].Len() > 0 {
if len(toAddReversed) > 1 {
updatedPm = CopyPD(p.pm)
}
updatedPm.Update(toAddReversed[i])
}
ami.stack = append(ami.stack, assignedIterState{
p.formI, p.assnI + 1, updatedPm,
})
}
}
return false
}
type sequenceMatchIter struct {
components []Ex
lhs_components []parsedForm
pm *PDManager
sequenceHead string
es *EvalState
ai assnIter
iteratingAmi bool
ami assignedMatchIter
}
func NewSequenceMatchIter(components []Ex, lhs_components []Ex, isOrderless bool, isFlat bool, sequenceHead string, pm *PDManager, es *EvalState) (matchIter, bool) {
headDefault := (&Symbol{sequenceHead}).Default(&es.defined)
fp_components := make([]parsedForm, len(lhs_components))
for i, comp := range lhs_components {
fp_components[i] = ParseForm(comp, isFlat, sequenceHead, headDefault, &es.CASLogger)
}
return NewSequenceMatchIterPreparsed(components, fp_components, isOrderless, sequenceHead, pm, es)
}
func NewSequenceMatchIterPreparsed(components []Ex, lhs_components []parsedForm, isOrderless bool, sequenceHead string, pm *PDManager, es *EvalState) (matchIter, bool) {
nomi := &sequenceMatchIter{}
nomi.components = components
nomi.lhs_components = lhs_components
nomi.pm = pm
nomi.sequenceHead = sequenceHead
nomi.es = es
origFrozen := es.IsFrozen()
es.SetFrozen(true)
formMatches := make([][]bool, len(lhs_components))
for i, mustContain := range lhs_components {
// Right now I have this strange definition of "form". It's basically where I convert blank sequences to blanks at the bottom level. What if I did this at all levels and perhaps did something with patterns?
// TODO: prevent the checks here from modifying state so I can use the "rm" function.
formMatches[i] = make([]bool, len(components))
num_matches := 0
for j, part := range components {
matchq, _ := IsMatchQ(part, mustContain.form, EmptyPD(), es)
if matchq {
num_matches++
}
formMatches[i][j] = matchq
}
if num_matches < mustContain.startI {
es.SetFrozen(origFrozen)
return nomi, false
}
}
es.SetFrozen(origFrozen)
nomi.ai = NewAssnIter(len(components), lhs_components, formMatches, isOrderless)
return nomi, true
}
func (this *sequenceMatchIter) next() (bool, *PDManager, bool) {
for {
if this.iteratingAmi && this.ami.next() {
return true, this.ami.pm, false
}
this.iteratingAmi = false
if !this.ai.next() {
break
}
this.ami = NewAssignedMatchIter(this.ai.assns, this)
this.iteratingAmi = true
}
return false, this.pm, true
}
// HELPER FUNCTIONS
func ComponentsIsMatchQ(components []Ex, lhs_components []Ex, isOrderless bool, isFlat bool, sequenceHead string, pm *PDManager, es *EvalState) (bool, *PDManager) {
omi, cont := NewSequenceMatchIter(components, lhs_components, isOrderless, isFlat, sequenceHead, pm, es)
return GetMatchQ(omi, cont, pm)
}
func GetMatchQ(mi matchIter, cont bool, pm *PDManager) (bool, *PDManager) {
for cont {
matchq, newPd, done := mi.next()
cont = !done
// TODO: I could probably update my matchiters to only return if they
// have a match or are done.
if matchq {
return true, newPd
}
}
return false, pm
}
// TODO: do not export this
func IsMatchQ(a Ex, b Ex, pm *PDManager, es *EvalState) (bool, *PDManager) {
mi, cont := NewMatchIter(a, b, pm, es)
return GetMatchQ(mi, cont, pm)
}