-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.nim
276 lines (234 loc) · 6.61 KB
/
term.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
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
import std/strformat
import std/hashes
type TermTypeEnum* = enum
name, inh, predImpl, `tuple`, prod, sequence, uvar, qvar, impl, sim, img #, extSet
type
TermObj* = ref Term
Term* = object
case type0*: TermTypeEnum
of name:
name*: string
of inh, predImpl, impl, sim:
subject*: TermObj
predicate*: TermObj
of `tuple`, prod, sequence:
items0*: seq[TermObj]
of uvar, qvar: # universal variable + question variable
id*: int64
of img:
idx*: int
content*: seq[TermObj]
base*: TermObj
proc termMkName*(s:string): TermObj =
return TermObj(type0:name, name:s)
proc termMkInh*(subject: TermObj, predicate: TermObj): TermObj =
return TermObj(type0:inh, subject:subject, predicate:predicate)
proc termMkSim*(subject: TermObj, predicate: TermObj): TermObj =
return TermObj(type0:sim, subject:subject, predicate:predicate)
proc termMkPredImpl*(subject: TermObj, predicate: TermObj): TermObj =
return TermObj(type0:predImpl, subject:subject, predicate:predicate)
proc termMkImpl*(subject: TermObj, predicate: TermObj): TermObj =
return TermObj(type0:impl, subject:subject, predicate:predicate)
# TODO LOW< remove this function>
proc termMkProd*(items0:varargs[TermObj]): TermObj =
var items1: seq[TermObj] = @[]
for iv in items0:
items1.add(iv)
return TermObj(type0:prod, items0:items1)
proc termMkProd2*(items:seq[TermObj]): TermObj =
return TermObj(type0:prod, items0:items)
proc termMkTuple*(items0:varargs[TermObj]): TermObj =
var items1: seq[TermObj] = @[]
for iv in items0:
items1.add(iv)
return TermObj(type0:`tuple`, items0:items1)
# TODO LOW< remove this function>
proc termMkSeq*(items0:varargs[TermObj]): TermObj =
var items1: seq[TermObj] = @[]
for iv in items0:
items1.add(iv)
return TermObj(type0:sequence, items0:items1)
proc termMkSeq2*(items:seq[TermObj]): TermObj =
return TermObj(type0:sequence, items0:items)
proc termMkImg*(idx: int, base:TermObj, content:seq[TermObj]): TermObj =
return TermObj(type0:img, idx:idx, base:base, content:content)
proc termMkUvar*(id: int64): TermObj =
return TermObj(type0:uvar, id: id)
proc termMkQvar*(id: int64): TermObj =
return TermObj(type0:qvar, id: id)
proc termEq*(a: TermObj, b: TermObj): bool =
case a.type0
of name:
case b.type0
of name:
return a.name == b.name
else:
return false
of inh:
case b.type0
of inh:
return termEq(a.subject, b.subject) and termEq(b.predicate, b.predicate)
else:
return false
of predImpl:
case b.type0
of predImpl:
return termEq(a.subject, b.subject) and termEq(b.predicate, b.predicate)
else:
return false
of impl:
case b.type0
of impl:
return termEq(a.subject, b.subject) and termEq(b.predicate, b.predicate)
else:
return false
of `tuple`:
case b.type0
of `tuple`:
if a.items0.len == b.items0.len:
for iidx in 0..a.items0.len-1:
if not termEq(a.items0[iidx], b.items0[iidx]):
return false
return true
return false
else:
return false
of prod:
case b.type0
of prod:
if a.items0.len == b.items0.len:
for iidx in 0..a.items0.len-1:
if not termEq(a.items0[iidx], b.items0[iidx]):
return false
return true
return false
else:
return false
of sequence:
case b.type0
of sequence:
if a.items0.len == b.items0.len:
for iidx in 0..a.items0.len-1:
if not termEq(a.items0[iidx], b.items0[iidx]):
return false
return true
return false
else:
return false
of uvar, qvar:
return false # variables are never equal!
of sim:
case b.type0
of sim:
return termEq(a.subject, b.subject) and termEq(b.predicate, b.predicate)
else:
return false
of img:
case b.type0
of img:
if a.content.len != b.content.len:
return false
for idx in 0..<a.content.len:
if not termEq(a.content[idx], b.content[idx]):
return false
if not termEq(a.base, b.base):
return false
if a.idx != b.idx:
return false
return true
else:
return false
proc hash*(t: TermObj): Hash =
case t.type0
of name:
return hash(t.name)
of inh, predImpl, impl, sim:
return hash(t.subject) + hash(t.predicate)
of uvar, qvar:
return hash(t.id)
of `tuple`, prod, sequence:
# TODO
return hash(0)
of img:
# TODO
return hash(0)
proc convTermToStr*(t: TermObj): string =
case t.type0
of name:
return t.name
of inh:
return fmt"<{convTermToStr(t.subject)} --> {convTermToStr(t.predicate)}>"
of sim:
return fmt"<{convTermToStr(t.subject)} <-> {convTermToStr(t.predicate)}>"
of predImpl:
return fmt"<{convTermToStr(t.subject)} =/> {convTermToStr(t.predicate)}>"
of impl:
return fmt"<{convTermToStr(t.subject)} ==> {convTermToStr(t.predicate)}>"
of `tuple`, prod, sequence:
var s0 = ""
var idx = 0
for iv in t.items0:
s0 = s0&convTermToStr(iv)
if idx < t.items0.len-1:
case t.type0
of `tuple`:
s0 = s0&"##"
of sequence:
s0 = s0&","
of prod:
s0 = s0&"*"
of inh, name, predImpl, uvar, qvar, impl, sim, img:
discard
idx=idx+1
return fmt"({s0})"
of uvar:
return &"$${t.id}"
of qvar:
return &"?{t.id}"
of img:
if t.idx == 0:
return &"(/ {convTermToStr(t.base)} _ {convTermToStr(t.content[0])})"
return &"(/ {convTermToStr(t.base)} {convTermToStr(t.content[0])} _)"
# returns nil if some error occurred
proc seqRemoveLast*(t: TermObj): TermObj =
case t.type0
of sequence:
return termMkSeq2(t.items0[0..t.items0.len-1-1])
else:
return nil
# casts sequence to term if length is one
# else it returns a sequence
# returns nil if some error occurred
proc seqRemoveLastAndCastToTermIfNecessary*(t: TermObj): TermObj =
let seq0: TermObj = seqRemoveLast(t)
if seq0 == nil:
return nil
# is seq, check if it has only one item, return that if so
case seq0.type0
of sequence:
if seq0.items0.len == 1:
return seq0.items0[0]
else:
return seq0
else:
return nil
# TERMUTILS
# fold seq term
func termFoldSeq*(term: TermObj): TermObj =
var items: seq[TermObj] = @[]
# unwind
func unwindRec(term: TermObj) =
case term.type0
of sequence:
for iItem in term.items0:
unwindRec(iItem)
else:
items.add(term) # store
unwindRec(term)
return termMkSeq2(items)
func checkIsSeq*(t: TermObj): bool =
case t.type0
of sequence:
return true
else:
return false