/
evalstate.go
587 lines (536 loc) · 14.8 KB
/
evalstate.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package expreduce
import (
"bytes"
"fmt"
"log"
"sort"
"strings"
"time"
)
type DefMap map[string]Def
type EvalState struct {
// Embedded type for logging
CASLogger
defined DefMap
trace *Expression
NoInit bool
timeCounter TimeCounterGroup
freeze bool
thrown *Expression
}
func (this *EvalState) Load(def Definition) {
// TODO: deprecate most of this. We should be using .m files now.
def.Name = this.GetStringDef("System`$Context", "") + def.Name
this.MarkSeen(def.Name)
EvalInterp("$Context = \"Private`\"", this)
// TODO: do we really need SetDelayed here, or should we just write to
// downvalues directly? If we did this, we could potentially remove the
// "bootstrap" attribute that SetDelayed has.
for _, rule := range def.Rules {
(NewExpression([]Ex{
&Symbol{"System`SetDelayed"},
Interp(rule.Lhs, this),
Interp(rule.Rhs, this),
})).Eval(this)
}
if len(def.Usage) > 0 {
(NewExpression([]Ex{
&Symbol{"System`SetDelayed"},
NewExpression([]Ex{
&Symbol{"System`MessageName"},
&Symbol{def.Name},
&String{"usage"},
}),
&String{def.Usage},
})).Eval(this)
}
newDef, foundDef := this.defined[def.Name]
if !foundDef {
newDef = Def{}
}
if def.legacyEvalFn != nil {
newDef.legacyEvalFn = def.legacyEvalFn
}
protectedAttrs := append(def.Attributes, "Protected")
newDef.attributes = stringsToAttributes(protectedAttrs)
if def.Default != "" {
newDef.defaultExpr = Interp(def.Default, this)
}
if def.toString != nil {
// Global so that standard String() interface can access these
toStringFns[def.Name] = def.toString
}
this.defined[def.Name] = newDef
EvalInterp("$Context = \"System`\"", this)
}
func (es *EvalState) Init(loadAllDefs bool) {
es.defined = make(map[string]Def)
// These are fundamental symbols that affect even the parsing of
// expressions. We must define them before even the bootstrap definitions.
es.Define(&Symbol{"System`$Context"}, &String{"System`"})
es.Define(&Symbol{"System`$ContextPath"}, NewExpression([]Ex{
&Symbol{"System`List"},
&String{"System`"},
}))
es.timeCounter.Init()
es.NoInit = !loadAllDefs
if !es.NoInit {
// Init modules
// Mark all core builtins as seen in the System context:
es.MarkSeen("System`Symbol")
es.MarkSeen("System`Integer")
es.MarkSeen("System`Rational")
es.MarkSeen("System`String")
es.MarkSeen("System`True")
es.MarkSeen("System`False")
es.MarkSeen("System`InputForm")
es.MarkSeen("System`OutputForm")
es.MarkSeen("System`FullForm")
es.MarkSeen("System`ESimpleExamples")
es.MarkSeen("System`EFurtherExamples")
es.MarkSeen("System`ETests")
es.MarkSeen("System`EKnownFailures")
es.MarkSeen("System`EKnownDangerous")
es.MarkSeen("System`ESameTest")
es.MarkSeen("System`EStringTest")
es.MarkSeen("System`EExampleOnlyInstruction")
es.MarkSeen("System`EComment")
es.MarkSeen("System`EResetState")
es.MarkSeen("System`ExpreduceNonConditional")
es.MarkSeen("System`Debug")
es.MarkSeen("System`Info")
es.MarkSeen("System`Notice")
es.MarkSeen("System`Echo")
es.MarkSeen("System`Row")
es.MarkSeen("System`Attributes")
es.MarkSeen("System`Orderless")
es.MarkSeen("System`Flat")
es.MarkSeen("System`OneIdentity")
es.MarkSeen("System`Listable")
es.MarkSeen("System`Constant")
es.MarkSeen("System`NumericFunction")
es.MarkSeen("System`Protected")
es.MarkSeen("System`Locked")
es.MarkSeen("System`ReadProtected")
es.MarkSeen("System`HoldFirst")
es.MarkSeen("System`HoldRest")
es.MarkSeen("System`HoldAll")
es.MarkSeen("System`HoldAllComplete")
es.MarkSeen("System`NHoldFirst")
es.MarkSeen("System`NHoldRest")
es.MarkSeen("System`NHoldAll")
es.MarkSeen("System`SequenceHold")
es.MarkSeen("System`Temporary")
es.MarkSeen("System`Stub")
es.MarkSeen("System`$Failed")
es.MarkSeen("System`AppellF1")
es.MarkSeen("System`Cosh")
es.MarkSeen("System`Sinh")
es.MarkSeen("System`Tanh")
es.MarkSeen("System`Sec")
es.MarkSeen("System`Sech")
es.MarkSeen("System`Csc")
es.MarkSeen("System`Csch")
es.MarkSeen("System`Cot")
es.MarkSeen("System`Coth")
es.MarkSeen("System`ArcSin")
es.MarkSeen("System`ArcSinh")
es.MarkSeen("System`ArcCos")
es.MarkSeen("System`ArcCosh")
es.MarkSeen("System`ArcTan")
es.MarkSeen("System`ArcTanh")
for _, defSet := range GetAllDefinitions() {
for _, def := range defSet.Defs {
es.MarkSeen(es.GetStringDef("System`$Context", "") + def.Name)
}
}
// Load bootstrap definitions first.
for _, defSet := range GetAllDefinitions() {
for _, def := range defSet.Defs {
if def.Bootstrap {
es.Load(def)
}
}
}
// Load rest of definitions.
for _, defSet := range GetAllDefinitions() {
for _, def := range defSet.Defs {
if !def.Bootstrap {
es.Load(def)
}
}
fn := fmt.Sprintf("resources/%v.m", defSet.Name)
data, err := Asset(fn)
if err == nil {
EvalInterp("$Context = \"Private`\"", es)
EvalInterpMany(string(data), fn, es)
EvalInterp("$Context = \"System`\"", es)
}
}
// System initialization
fn := "resources/init.m"
data := MustAsset(fn)
EvalInterpMany(string(data), fn, es)
}
EvalInterp("$Context = \"Global`\"", es)
EvalInterp("$ContextPath = Append[$ContextPath, \"Global`\"]", es)
EvalInterp("$ExpreduceContextStack = {\"Global`\"}", es)
}
func NewEvalState() *EvalState {
var es EvalState
es.Init(true)
es.SetUpLogging()
es.DebugOff()
return &es
}
func NewEvalStateNoLog(loadAllDefs bool) *EvalState {
var es EvalState
es.Init(loadAllDefs)
es.CASLogger.debugState = false
return &es
}
func (this *EvalState) IsDef(name string) bool {
_, isd := this.defined[name]
return isd
}
func (this *EvalState) GetDef(name string, lhs Ex) (Ex, bool, *Expression) {
if !this.IsDef(name) {
return nil, false, nil
}
// Special case for checking simple variable definitions like "a = 5".
// TODO: Perhaps split out single var values into the Definition to avoid
// iterating over every one.
if _, lhsIsSym := lhs.(*Symbol); lhsIsSym {
for _, def := range this.defined[name].downvalues {
if hp, hpDef := HeadAssertion(def.rule.Parts[1], "System`HoldPattern"); hpDef {
if len(hp.Parts) == 2 {
if _, symDef := hp.Parts[1].(*Symbol); symDef {
return def.rule.Parts[2], true, def.rule
}
}
}
}
return nil, false, nil
}
this.Debugf("Inside GetDef(\"%s\",%s)", name, lhs)
for i := range this.defined[name].downvalues {
def := this.defined[name].downvalues[i].rule
defStr, lhsDefStr := "", ""
started := int64(0)
if this.isProfiling {
defStr = def.String()
lhsDefStr = lhs.String() + defStr
started = time.Now().UnixNano()
}
res, replaced := Replace(lhs, def, this)
if this.isProfiling {
elapsed := float64(time.Now().UnixNano()-started) / 1000000000
this.timeCounter.AddTime(CounterGroupDefTime, defStr, elapsed)
this.timeCounter.AddTime(CounterGroupLhsDefTime, lhsDefStr, elapsed)
}
if replaced {
return res, true, def
}
}
return nil, false, nil
}
func (this *EvalState) GetSymDef(name string) (Ex, bool) {
sym := &Symbol{name}
symDef, isDef, _ := this.GetDef(name, sym)
return symDef, isDef
}
func (this *EvalState) DefineAttrs(sym *Symbol, rhs Ex) {
attrsList, attrsIsList := HeadAssertion(rhs, "System`List")
if !attrsIsList {
return
}
var stringAttrs []string
for _, attrEx := range attrsList.Parts[1:] {
attrSym, attrIsSym := attrEx.(*Symbol)
if !attrIsSym {
return
}
if !strings.HasPrefix(attrSym.Name, "System`") {
continue
}
stringAttrs = append(stringAttrs, attrSym.Name[7:])
}
attrs := stringsToAttributes(stringAttrs)
if !this.IsDef(sym.Name) {
this.defined[sym.Name] = Def{}
}
tmp := this.defined[sym.Name]
tmp.attributes = attrs
this.defined[sym.Name] = tmp
}
func (this *EvalState) DefineDownValues(sym *Symbol, rhs Ex) {
dvList, isList := HeadAssertion(rhs, "System`List")
if !isList {
fmt.Println("Assignment to DownValues must be List of Rules.")
return
}
dvs := []DownValue{}
for _, dvEx := range dvList.Parts[1:] {
rule, isRule := HeadAssertion(dvEx, "System`Rule")
if !isRule {
rule, isRule = HeadAssertion(dvEx, "System`RuleDelayed")
}
if !isRule || len(rule.Parts) != 3 {
fmt.Println("Assignment to DownValues must be List of Rules.")
}
dvs = append(dvs, DownValue{rule: rule})
}
if !this.IsDef(sym.Name) {
this.defined[sym.Name] = Def{}
}
tmp := this.defined[sym.Name]
tmp.downvalues = dvs
this.defined[sym.Name] = tmp
}
func (this *EvalState) MarkSeen(name string) {
if !this.IsDef(name) {
newDef := Def{
downvalues: []DownValue{},
}
this.defined[name] = newDef
}
}
// Attempts to compute a specificity metric for a rule. Higher specificity rules
// should be tried first.
func ruleSpecificity(lhs Ex, rhs Ex, name string) int {
if name == "Rubi`Int" {
return 100
}
// I define complexity as the length of the Lhs.String()
// because it is simple, and it works for most of the common cases. We wish
// to attempt f[x_Integer] before we attempt f[x_]. If LHSs map to the same
// "complexity" score, order then matters. TODO: Create better measure of
// complexity (or specificity)
context, contextPath := DefinitionComplexityStringFormArgs()
specificity := len(lhs.StringForm("InputForm", context, contextPath))
if _, rhsIsCond := HeadAssertion(rhs, "System`Condition"); rhsIsCond {
// Condition rules will be ranked in order of definition, not
// specificity. I'm not entirely sure if this is correct, but it seems
// to be the case for all the Rubi rules.
specificity = 100
}
return specificity
}
func (this *EvalState) Define(lhs Ex, rhs Ex) {
if this.IsFrozen() {
return
}
// This function used to require a name as a parameter. Centralize the logic
// here.
name := ""
LhsSym, ok := lhs.(*Symbol)
if ok {
name = LhsSym.Name
}
LhsF, ok := lhs.(*Expression)
if ok {
headAsSym, headIsSym := LhsF.Parts[0].(*Symbol)
if headIsSym {
name = headAsSym.Name
if name == "System`Attributes" {
if len(LhsF.Parts) != 2 {
return
}
modifiedSym, modifiedIsSym := LhsF.Parts[1].(*Symbol)
if !modifiedIsSym {
return
}
this.DefineAttrs(modifiedSym, rhs)
return
} else if name == "System`DownValues" {
if len(LhsF.Parts) != 2 {
return
}
modifiedSym, modifiedIsSym := LhsF.Parts[1].(*Symbol)
if !modifiedIsSym {
return
}
this.DefineDownValues(modifiedSym, rhs)
return
}
}
_, opExpr, isVerbatimOp := OperatorAssertion(lhs, "System`Verbatim")
if isVerbatimOp {
opSym, opIsSym := opExpr.Parts[1].(*Symbol)
if opIsSym {
name = opSym.Name
}
}
}
if name == "" {
log.Fatalf("Trying to define an invalid lhs: %v", lhs)
}
this.Debugf("Inside es.Define(\"%s\",%s,%s)", name, lhs, rhs)
heldLhs := E(S("HoldPattern"), lhs)
if !this.IsDef(name) {
newDef := Def{
downvalues: []DownValue{
DownValue{
rule: NewExpression([]Ex{
&Symbol{"System`Rule"}, heldLhs, rhs,
}),
},
},
}
this.defined[name] = newDef
return
}
// Overwrite identical rules.
for _, dv := range this.defined[name].downvalues {
existingRule := dv.rule
existingLhs := existingRule.Parts[1]
if IsSameQ(existingLhs, heldLhs, &this.CASLogger) {
existingRhsCond := maskNonConditional(existingRule.Parts[2])
newRhsCond := maskNonConditional(rhs)
if IsSameQ(existingRhsCond, newRhsCond, &this.CASLogger) {
dv.rule.Parts[2] = rhs
return
}
}
}
// Insert into definitions for name. Maintain order of decreasing
// complexity.
var tmp = this.defined[name]
newSpecificity := ruleSpecificity(heldLhs, rhs, name)
for i, dv := range this.defined[name].downvalues {
if dv.specificity == 0 {
dv.specificity = ruleSpecificity(
dv.rule.Parts[1],
dv.rule.Parts[2],
name,
)
}
if dv.specificity < newSpecificity {
newRule := NewExpression([]Ex{&Symbol{"System`Rule"}, heldLhs, rhs})
tmp.downvalues = append(
tmp.downvalues[:i],
append(
[]DownValue{DownValue{
rule: newRule,
specificity: newSpecificity,
}},
this.defined[name].downvalues[i:]...,
)...,
)
this.defined[name] = tmp
return
}
}
tmp.downvalues = append(tmp.downvalues, DownValue{rule: NewExpression([]Ex{&Symbol{"System`Rule"}, heldLhs, rhs})})
this.defined[name] = tmp
}
func (this *EvalState) ClearAll() {
this.Init(!this.NoInit)
}
func (this *EvalState) Clear(name string) {
_, ok := this.defined[name]
if ok {
this.defined[name] = Def{}
//delete(this.defined, name)
}
}
func (this *EvalState) GetDefinedSnapshot() map[string]Def {
return CopyDefs(this.defined)
}
func (this *EvalState) String() string {
var buffer bytes.Buffer
buffer.WriteString("{")
// We sort the keys here such that converting identical EvalStates always
// produces the same string.
keys := []string{}
for k := range this.defined {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := this.defined[k]
buffer.WriteString(k)
buffer.WriteString(": ")
buffer.WriteString(v.String())
buffer.WriteString(", ")
}
if strings.HasSuffix(buffer.String(), ", ") {
buffer.Truncate(buffer.Len() - 2)
}
buffer.WriteString("}")
return buffer.String()
}
func (this *EvalState) IsFrozen() bool {
return this.freeze
}
func (this *EvalState) SetFrozen(frozen bool) {
this.freeze = frozen
}
func (this *EvalState) GetStringDef(name string, defaultVal string) string {
nameSym := &Symbol{name}
def, isDef, _ := this.GetDef(name, nameSym)
if !isDef {
return defaultVal
}
defString, defIsString := def.(*String)
if !defIsString {
return defaultVal
}
return defString.Val
}
func (this *EvalState) GetListDef(name string) *Expression {
nameSym := &Symbol{name}
def, isDef, _ := this.GetDef(name, nameSym)
if !isDef {
return NewExpression([]Ex{&Symbol{"System`List"}})
}
defList, defIsList := HeadAssertion(def, "System`List")
if !defIsList {
return NewExpression([]Ex{&Symbol{"System`List"}})
}
return defList
}
func (es *EvalState) Throw(e *Expression) {
es.thrown = e
}
func (es *EvalState) HasThrown() bool {
return es.thrown != nil
}
func (es *EvalState) ProcessTopLevelResult(e Ex) Ex {
if es.HasThrown() {
fmt.Printf("Throw::nocatch: %v returned to top level but uncaught.\n\n", es.thrown)
toReturn := NewExpression([]Ex{
&Symbol{"System`Hold"},
es.thrown,
})
// Clear exception
es.Throw(nil)
return toReturn
}
return e
}
func maskNonConditional(e Ex) Ex {
var res Ex = &Symbol{"System`ExpreduceNonConditional"}
if asHead, isHead := HeadAssertion(e, "System`Condition"); isHead {
res = NewExpression([]Ex{
&Symbol{"System`Condition"},
maskNonConditional(asHead.Parts[1]),
asHead.Parts[2],
})
}
heads := []string{"System`With", "System`Module"}
for _, head := range heads {
if asHead, isHead := HeadAssertion(e, head); isHead {
if len(asHead.Parts) == 3 {
if _, hasCond := HeadAssertion(asHead.Parts[2], "System`Condition"); hasCond {
res = NewExpression([]Ex{
&Symbol{head},
asHead.Parts[1],
maskNonConditional(asHead.Parts[2]),
})
}
}
}
}
return res
}