-
Notifications
You must be signed in to change notification settings - Fork 0
/
unify.nim
233 lines (188 loc) · 6.69 KB
/
unify.nim
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
import std/tables
import std/options
import std/strformat
# for debugging
import term
type
UnifiedVarsRef* = ref UnifiedVars
UnifiedVars* = object
uVarAssignments*: Table[int64, TermObj] # variable assignment
qVarAssignments*: Table[int64, TermObj] # variable assignments for question variables
type UnifyModeEnum* = enum
unifyModeUvar
unifyModeQvar
# term unifier
proc termTryUnify*(a: TermObj, b: TermObj, vars: UnifiedVarsRef, unifyMode: UnifyModeEnum): bool =
proc tryAssign(id: int64, t: TermObj): bool =
#echo(&"try to assign {convTermToStr(t)} to varId={id}") # DBG
if unifyMode == unifyModeUvar:
if id in vars.uVarAssignments:
# assignment must be equal!
return termEq(vars.uVarAssignments[id], b)
else:
# assign
vars.uVarAssignments[id] = b
return true
else:
if id in vars.qVarAssignments:
# assignment must be equal!
return termEq(vars.qVarAssignments[id], b)
else:
# assign
vars.qVarAssignments[id] = b
return true
#echo(&"unify00: try to unify {convTermToStr(a)} + {convTermToStr(b)}") # DBG
case a.type0
of inh:
case b.type0
of inh:
return termTryUnify(a.subject, b.subject, vars, unifyMode) and termTryUnify(a.predicate, b.predicate, vars, unifyMode)
of uvar, qvar:
return tryAssign(b.id, a)
of `tuple`, sequence, name, prod, predImpl, impl, sim, img:
return false
of sim:
case b.type0
of sim:
return termTryUnify(a.subject, b.subject, vars, unifyMode) and termTryUnify(a.predicate, b.predicate, vars, unifyMode)
of uvar, qvar:
return tryAssign(b.id, a)
of `tuple`, sequence, name, prod, predImpl, impl, inh, img:
return false
of predImpl:
case b.type0
of predImpl:
return termTryUnify(a.subject, b.subject, vars, unifyMode) and termTryUnify(a.predicate, b.predicate, vars, unifyMode)
of uvar, qvar:
return tryAssign(b.id, a)
of `tuple`, sequence, name, prod, inh, impl, sim, img:
return false
of impl:
case b.type0
of impl:
return termTryUnify(a.subject, b.subject, vars, unifyMode) and termTryUnify(a.predicate, b.predicate, vars, unifyMode)
of uvar, qvar:
return tryAssign(b.id, a)
of `tuple`, sequence, name, prod, inh, predImpl, sim, img:
return false
of name:
case b.type0
of name:
return a.name == b.name
of uvar, qvar:
return tryAssign(b.id, a)
of inh, `tuple`, sequence, prod, predImpl, impl, sim, img:
return false
of `tuple`:
case b.type0
of `tuple`:
if a.items0.len != b.items0.len:
return false
for iidx in 0..a.items0.len-1:
if not termTryUnify(a.items0[iidx], b.items0[iidx], vars, unifyMode):
return false
return true
of uvar, qvar:
return tryAssign(b.id, a)
of inh, name, prod, predImpl, sequence, impl, sim, img:
return false
of sequence:
case b.type0
of sequence:
if a.items0.len != b.items0.len:
return false
for iidx in 0..<a.items0.len:
if not termTryUnify(a.items0[iidx], b.items0[iidx], vars, unifyMode):
return false
return true
of uvar, qvar:
return tryAssign(b.id, a)
of inh, `tuple`, name, prod, predImpl, impl, sim, img:
return false
of prod:
case b.type0
of prod:
if a.items0.len != b.items0.len:
return false
for iidx in 0..<a.items0.len:
if not termTryUnify(a.items0[iidx], b.items0[iidx], vars, unifyMode):
return false
return true
of uvar, qvar:
return tryAssign(b.id, a)
of inh, `tuple`, name, predImpl, sequence, impl, sim, img:
return false
of uvar, qvar:
case b.type0
of prod, inh, `tuple`, name, predImpl, sequence, impl, sim, img:
return tryAssign(a.id, b)
of uvar, qvar:
return false # var and var don't unify!
of img:
case b.type0
of img:
if a.idx != b.idx:
return false
if not termTryUnify(a.base, b.base, vars, unifyMode):
return false
if a.content.len != b.content.len:
return false
for iidx in 0..<a.content.len:
if not termTryUnify(a.content[iidx], b.content[iidx], vars, unifyMode):
return false
return true
of uvar, qvar:
return tryAssign(b.id, a)
of inh, `tuple`, name, predImpl, sequence, impl, sim, prod:
return false
proc unifySubstitute*(t: TermObj, vars: UnifiedVarsRef): TermObj =
case t.type0
of inh:
return termMkInh(unifySubstitute(t.subject, vars), unifySubstitute(t.predicate, vars))
of predImpl:
return termMkPredImpl(unifySubstitute(t.subject, vars), unifySubstitute(t.predicate, vars))
of impl:
return termMkImpl(unifySubstitute(t.subject, vars), unifySubstitute(t.predicate, vars))
of name:
return t
of `tuple`, sequence, prod:
var s: seq[TermObj] = @[]
for iv in t.items0:
s.add(unifySubstitute(iv, vars))
case t.type0
of `tuple`:
return TermObj(type0:`tuple`, items0:s)
of sequence:
return TermObj(type0:sequence, items0:s)
of prod:
return TermObj(type0:prod, items0:s)
of inh, predImpl, name, uvar, qvar, impl, sim, img:
# internal error, should never occur!
return t # HACK< just return >
of uvar:
if t.id in vars.uVarAssignments:
return vars.uVarAssignments[t.id]
else:
return t # leave it unassigned
of qvar:
if t.id in vars.qVarAssignments:
return vars.qVarAssignments[t.id]
else:
return t # leave it unassigned
of sim:
return termMkSim(unifySubstitute(t.subject, vars), unifySubstitute(t.predicate, vars))
of img:
var s: seq[TermObj] = @[]
for iv in t.content:
s.add(unifySubstitute(iv, vars))
return termMkImg(t.idx, unifySubstitute(t.base, vars), s)
# try to unify and assign variables to terms
proc termTryUnifyAndAssign3*(a: TermObj, b: TermObj, unifyMode: UnifyModeEnum): Option[tuple[a: TermObj, b: TermObj, vars:UnifiedVarsRef]] =
var vars: UnifiedVarsRef = UnifiedVarsRef(uVarAssignments:initTable[int64, TermObj](), qVarAssignments:initTable[int64, TermObj]())
if not termTryUnify(a, b, vars, unifyMode):
return none(tuple[a: TermObj, b: TermObj, vars: UnifiedVarsRef])
let aUnified: TermObj = unifySubstitute(a, vars)
let bUnified: TermObj = unifySubstitute(b, vars)
return some((aUnified, bUnified, vars))
proc termCheckUnify(a: TermObj, b: TermObj): bool =
return termTryUnifyAndAssign3(a, b, unifyModeUvar).isSome()