-
Notifications
You must be signed in to change notification settings - Fork 32
/
env.go
339 lines (313 loc) · 6.37 KB
/
env.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
package engine
var varContext = NewVariable()
var rootContext = NewAtom("root")
type envKey int64
func newEnvKey(v Variable) envKey {
// A new Variable is always bigger than the previous ones.
// So, if we used the Variable itself as the key, insertions to the Env tree would be skewed to the right.
k := envKey(v)
if k/2 != 0 {
k *= -1
}
return k
}
type color uint8
const (
red color = iota
black
)
// Env is a mapping from variables to terms.
type Env struct {
// basically, this is Red-Black tree from Purely Functional Data Structures by Okazaki.
color color
left, right *Env
binding
}
type binding struct {
key envKey
value Term
// attributes?
}
var rootEnv = &Env{
binding: binding{
key: newEnvKey(varContext),
value: rootContext,
},
}
// NewEnv creates an empty environment.
func NewEnv() *Env {
return nil
}
// lookup returns a term that the given variable is bound to.
func (e *Env) lookup(v Variable) (Term, bool) {
k := newEnvKey(v)
node := e
if node == nil {
node = rootEnv
}
for {
if node == nil {
return nil, false
}
switch {
case k < node.key:
node = node.left
case k > node.key:
node = node.right
default:
return node.value, true
}
}
}
// bind adds a new entry to the environment.
func (e *Env) bind(v Variable, t Term) *Env {
k := newEnvKey(v)
node := e
if node == nil {
node = rootEnv
}
ret := *node.insert(k, t)
ret.color = black
return &ret
}
func (e *Env) insert(k envKey, v Term) *Env {
if e == nil {
return &Env{color: red, binding: binding{key: k, value: v}}
}
switch {
case k < e.key:
ret := *e
ret.left = e.left.insert(k, v)
ret.balance()
return &ret
case k > e.key:
ret := *e
ret.right = e.right.insert(k, v)
ret.balance()
return &ret
default:
ret := *e
ret.value = v
return &ret
}
}
func (e *Env) balance() {
var (
a, b, c, d *Env
x, y, z binding
)
switch {
case e.left != nil && e.left.color == red:
switch {
case e.left.left != nil && e.left.left.color == red:
a = e.left.left.left
b = e.left.left.right
c = e.left.right
d = e.right
x = e.left.left.binding
y = e.left.binding
z = e.binding
case e.left.right != nil && e.left.right.color == red:
a = e.left.left
b = e.left.right.left
c = e.left.right.right
d = e.right
x = e.left.binding
y = e.left.right.binding
z = e.binding
default:
return
}
case e.right != nil && e.right.color == red:
switch {
case e.right.left != nil && e.right.left.color == red:
a = e.left
b = e.right.left.left
c = e.right.left.right
d = e.right.right
x = e.binding
y = e.right.left.binding
z = e.right.binding
case e.right.right != nil && e.right.right.color == red:
a = e.left
b = e.right.left
c = e.right.right.left
d = e.right.right.right
x = e.binding
y = e.right.binding
z = e.right.right.binding
default:
return
}
default:
return
}
*e = Env{
color: red,
left: &Env{color: black, left: a, right: b, binding: x},
right: &Env{color: black, left: c, right: d, binding: z},
binding: y,
}
}
// Resolve follows the variable chain and returns the first non-variable term or the last free variable.
func (e *Env) Resolve(t Term) Term {
var stop []Variable
for t != nil {
switch v := t.(type) {
case Variable:
for _, s := range stop {
if v == s {
return v
}
}
ref, ok := e.lookup(v)
if !ok {
return v
}
stop = append(stop, v)
t = ref
default:
return v
}
}
return nil
}
// simplify trys to remove as many variables as possible from term t.
func (e *Env) simplify(t Term) Term {
return simplify(t, nil, e)
}
func simplify(t Term, simplified map[termID]Compound, env *Env) Term {
if simplified == nil {
simplified = map[termID]Compound{}
}
t = env.Resolve(t)
if c, ok := simplified[id(t)]; ok {
return c
}
switch t := t.(type) {
case charList, codeList:
return t
case list:
l := make(list, len(t))
simplified[id(t)] = l
for i, e := range t {
l[i] = simplify(e, simplified, env)
}
return l
case *partial:
var p partial
simplified[id(t)] = &p
p.Compound = simplify(t.Compound, simplified, env).(Compound)
tail := simplify(*t.tail, simplified, env)
p.tail = &tail
return &p
case Compound:
c := compound{
functor: t.Functor(),
args: make([]Term, t.Arity()),
}
simplified[id(t)] = &c
for i := 0; i < t.Arity(); i++ {
c.args[i] = simplify(t.Arg(i), simplified, env)
}
return &c
default:
return t
}
}
type variables []Variable
// freeVariables extracts variables in the given Term.
func (e *Env) freeVariables(t Term) []Variable {
return e.appendFreeVariables(nil, t)
}
func (e *Env) appendFreeVariables(fvs variables, t Term) variables {
switch t := e.Resolve(t).(type) {
case Variable:
for _, v := range fvs {
if v == t {
return fvs
}
}
return append(fvs, t)
case Compound:
for i := 0; i < t.Arity(); i++ {
fvs = e.appendFreeVariables(fvs, t.Arg(i))
}
}
return fvs
}
// Unify unifies 2 terms.
func (e *Env) Unify(x, y Term) (*Env, bool) {
return e.unify(x, y, false)
}
func (e *Env) unifyWithOccursCheck(x, y Term) (*Env, bool) {
return e.unify(x, y, true)
}
func (e *Env) unify(x, y Term, occursCheck bool) (*Env, bool) {
x, y = e.Resolve(x), e.Resolve(y)
switch x := x.(type) {
case Variable:
switch {
case x == y:
return e, true
case occursCheck && contains(y, x, e):
return e, false
default:
return e.bind(x, y), true
}
case Compound:
switch y := y.(type) {
case Variable:
return e.unify(y, x, occursCheck)
case Compound:
if x.Functor() != y.Functor() {
return e, false
}
if x.Arity() != y.Arity() {
return e, false
}
var ok bool
for i := 0; i < x.Arity(); i++ {
e, ok = e.unify(x.Arg(i), y.Arg(i), occursCheck)
if !ok {
return e, false
}
}
return e, true
default:
return e, false
}
default: // atomic
switch y := y.(type) {
case Variable:
return e.unify(y, x, occursCheck)
default:
return e, x == y
}
}
}
func contains(t, s Term, env *Env) bool {
switch t := t.(type) {
case Variable:
if t == s {
return true
}
ref, ok := env.lookup(t)
if !ok {
return false
}
return contains(ref, s, env)
case Compound:
if s, ok := s.(Atom); ok && t.Functor() == s {
return true
}
for i := 0; i < t.Arity(); i++ {
if contains(t.Arg(i), s, env) {
return true
}
}
return false
default:
return t == s
}
}