-
Notifications
You must be signed in to change notification settings - Fork 2
/
ut.go
274 lines (231 loc) · 5.63 KB
/
ut.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
package ut
import (
"log"
"strings"
)
var (
// VAR stands for variable type
VAR = func(t rune) bool {
return t == Variable
}
// STR stands for constants or composite terms
STR = func(t rune) bool {
return t == Atom ||
t == Float ||
t == Int ||
t == String ||
t == Void ||
t == Functor
}
)
type (
// Entry stands for Unification Table Entry
Entry struct {
Term string
Functor string
Components []int
Type rune
}
// UT stands for Unification Table
UT struct {
// Lookup table (term) -> (index)
Lookup map[string]int
Entries []*Entry
Bindings map[int]int
}
)
// Arity is the arity of the term; for variables and constants, it is 0.
func (e *Entry) Arity() int {
if e.Type != Functor {
return 0
}
return len(e.Components)
}
// Unify returns a unification maps with VAR bindings.
// Also see ut.MGU for particular terms.
func Unify(x, y string) map[string]string {
tokens := Tokenize(x, y)
ut := New(tokens)
ix, iy := ut.Lookup[x], ut.Lookup[y]
if !ut.Unify(ix, iy) {
return nil
}
mgu := make(map[string]string)
for i, j := range ut.Bindings {
j = ut.dereference(j)
mgu[ut.Entries[i].Term] = ut.termString(j)
}
return mgu
}
// New creates a new Unification Table.
func New(tokens []*Token) (ut *UT) {
ut = &UT{Lookup: make(map[string]int), Bindings: make(map[int]int)}
for i, n := 0, len(tokens)-1; n >= 0; n-- {
t := tokens[n]
if _, exists := ut.Lookup[t.Term]; exists {
continue
}
e := &Entry{
Term: t.Term,
Functor: t.Functor,
Type: t.Type,
}
for _, c := range t.Components {
idx, exists := ut.Lookup[c]
if !exists {
log.Fatalf("Component: %s not found", c)
}
e.Components = append(e.Components, idx)
}
ut.Lookup[t.Term] = i
ut.Entries = append(ut.Entries, e)
i++
}
return ut
}
// Unify tries to calculate MGU (Most General Unifier)
func (ut *UT) Unify(ix, iy int) bool {
var (
// stacks
sx, sy = []int{ix}, []int{iy}
empty = func(s []int) bool {
return len(s) == 0
}
pop = func(s []int) (int, []int) {
n := len(s)
if n > 0 {
i := s[n-1]
s = s[:n-1]
return i, s
}
return 0, nil
}
push = func(s []int, i ...int) []int {
return append(s, i...)
}
)
for !empty(sx) && !empty(sy) {
// pop entries from stacks
ix, sx = pop(sx)
iy, sy = pop(sy)
ex, ey := ut.Entries[ix], ut.Entries[iy]
switch true {
// case 1: ex is bound to a term and ey is bound to a term}
case STR(ex.Type) && STR(ey.Type):
ax, ay := ex.Arity(), ey.Arity()
if ex.Functor != ey.Functor || ax != ay {
return false
}
if ax > 0 {
sx = push(sx, ex.Components...)
sy = push(sy, ey.Components...)
}
// case 2: ex is bound to a term and ey is bound to a variable
case STR(ex.Type) && VAR(ey.Type):
if idx, _, b := ut.bindSTR(ix, iy); !b {
sx = push(sx, ix)
sy = push(sy, idx)
}
// case 3: ex is bound to a variable and ey is bound to a term
case VAR(ex.Type) && STR(ey.Type):
if idx, _, b := ut.bindSTR(iy, ix); !b {
sx = push(sx, idx)
sy = push(sy, iy)
}
// case 4: ex is bound to a variable and ey is bound to a variable
case VAR(ex.Type) && VAR(ey.Type):
if idx1, idx2, b := ut.bindVAR(ix, iy); !b {
sx = push(sx, idx1)
sy = push(sy, idx2)
}
}
}
return true
}
// MGU returns The Most General Unifier as a string for a given term.
// It dereferences bindings and term componenets.
func (ut *UT) MGU(term string) string {
i, ok := ut.Lookup[term]
if !ok {
return ""
}
i = ut.dereference(ut.Bindings[i])
return ut.termString(i)
}
// termString constructs a new term string by dereferencing all components.
func (ut *UT) termString(idx int) string {
if idx >= len(ut.Entries) {
return ""
}
e := ut.Entries[idx]
if e.Type != Functor {
return e.Functor
}
components := []string{}
for _, c := range e.Components {
i := ut.dereference(c)
if i != idx && ut.Entries[i].Type == Functor {
components = append(components, ut.termString(i))
} else {
components = append(components, ut.Entries[i].Functor)
}
}
return ut.Entries[idx].Functor + "(" + strings.Join(components, ",") + ")"
}
// dereference follows bindings and returns index for dereferenced variable.
func (ut *UT) dereference(idx int) int {
i, ok := idx, true
for ok {
i, ok = ut.Bindings[i]
if ok {
idx = i
}
}
return idx
}
// bindSTR tries to bind a VAR(varIdx) to STR(strIdx).
// If VAR(varIdx) is already bound then dereference it and try again
// or returns indexes to push them on stacks
func (ut *UT) bindSTR(strIdx, varIdx int) (int, int, bool) {
idx, ok := ut.Bindings[varIdx]
if !ok {
// var is a free variable
// bind var to str
ut.Bindings[varIdx] = strIdx
return strIdx, varIdx, true
}
// var is already bound - dereference
idx = ut.dereference(idx)
// var is bound to a STR
e := ut.Entries[idx]
if STR(e.Type) {
return idx, varIdx, false
}
// free variable
ut.Bindings[varIdx] = idx
return idx, varIdx, true
}
// bindVAR tries to bind two VARs.
// If both are already bound the function returns indexes to push them on stacks
// and false as boolean information that binding failed.
func (ut *UT) bindVAR(varIdx1, varIdx2 int) (int, int, bool) {
i1, ok1 := ut.Bindings[varIdx1]
i2, ok2 := ut.Bindings[varIdx2]
// var1 is free and var2 is free
if !ok1 && !ok2 {
ut.Bindings[varIdx1] = varIdx2
return varIdx1, varIdx2, true
}
// var1 is free and var2 is bound
if !ok1 && ok2 {
ut.Bindings[varIdx1] = varIdx2
return varIdx1, varIdx2, true
}
// var1 is bound and var2 us free
if ok1 && !ok2 {
ut.Bindings[varIdx2] = varIdx1
return varIdx1, varIdx2, true
}
// var1 is bound and var2 is bound
return i1, i2, false
}