-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.go
98 lines (89 loc) · 1.98 KB
/
bind.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
package main
import (
"fmt"
)
type Bind struct {
terms *Subst
kinds *Subst
memo map[string]Mark
}
// RewriteMark takes a Mark from some fact's Deps array and returns a new
// bonemeat after mapping its bound terms and kinds.
func (this Bind) Rewrite(mark Mark) Mark {
if this.memo == nil {
this.memo = make(map[string]Mark)
}
return mark.Rewrite(this)
}
// Given the original bonemeat need and the new bonemeat, write the mapping.
func (this Bind) Bind(mark Mark, entry *Entry) (out Bind, ok bool) {
newMark := entry.Mark().Rewrite(this)
var that Bind
workDone := false
mapStuff := func(w int, s *Subst) (out *Subst, ok bool) {
out, ok = s, true
// TODO: should move this code into mark
for i, x := range mark.list[w] {
if newMark.list[w][i] != x {
out, ok = out.Put(x, newMark.list[w][i])
if !ok {
return out, false
}
workDone = true
}
}
return out, true
}
that.terms, ok = mapStuff(1, this.terms)
if !ok {
return this, false
}
that.kinds, ok = mapStuff(2, this.kinds)
if !ok {
return this, false
}
if workDone {
return that, true
} else {
return this, true
}
}
func (this Bind) Term(term string) string {
t, _ := this.terms.Get(term)
return t
}
func (this Bind) Kind(kind string) string {
k, _ := this.kinds.Get(kind)
return k
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
// Returns the amount by which this bind is less than that one.
func (this Bind) LessThan(that Bind) int {
t := abs(that.terms.Len() - this.terms.Len())
k := abs(that.kinds.Len() - this.kinds.Len())
//TODO: this is not right
return t + k
}
func (this Bind) String() string {
return fmt.Sprintf("{%v;%v}", this.terms, this.kinds)
}
// TODO: need something like this
// shortcuts the transitive closure the map. panics if any cycle is detected.
func closeTransMap(m map[string]string) {
for cont := 0; cont < 2; cont++ {
for k, v := range m {
if j, ok := m[v]; ok {
if j == k {
panic(-1)
}
m[k] = j
cont = 0
}
}
}
}