-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions_formula.py
294 lines (250 loc) · 10.4 KB
/
functions_formula.py
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
from functions import *
from functions_string import *
config.globals=globals()
# isnamelc:
# isalphalc
# isnamelc isalnumlc
isnamelc=recursive_relation(config,
name='isnamelc',
desc='lower case name [0-9,a-z,_] starting with a-z',
zero='lambda: n0',
relation='lambda x,y: or_f(isalphalc(x),and_f(isalnumlc(item(length(x),x)),recursive_isnamelc(y,slice(n1,ac(length(x)),x))))'
)
# isnameuc:
# isalphauc
# isnameuc isalnumuc
isnameuc=recursive_relation(config,
name='isnameuc',
desc='upper case name [0-9,A-Z,_] starting with A-Z',
zero='lambda: n0',
relation='lambda x,y: or_f(isalphauc(x),and_f(isalnumuc(item(length(x),x)),recursive_isnameuc(y,slice(n1,ac(length(x)),x))))'
)
# istermlist:
# isterm
# istermlist , isterm
# needs termcode,termlistcode
istermlist1=argmin_function(config,
name='istermlist1',
desc='returns position of , if x has the form istermlist , isterm',
max='lambda x,termcode,termlistcode: length(x)',
relation='lambda pos,x,termcode,termlistcode: and_f3(equal(item(pos,x),n44),recursive_istermlist_help(termlistcode,slice(n1,acfull(pos),x),termcode),recursive_isterm(termcode,slice(sc(pos),length(x),x)))'
)
# needs termcode
istermlist_help=recursive_relation(config,
name='istermlist_help',
desc='x is a comma separated list of terms',
zero='lambda termcode: n0',
relation='lambda x,termcode,y: or_f(recursive_isterm(termcode,x),notzero(istermlist1(x,termcode,y)))'
)
# isterm:
# isnamelc
# isnamelc ( istermlist )
# needs termcode
isterm1=argmin_function(config,
name='isterm1',
desc='returns position of ( if x has the form isnamelc ( istermlist )',
max='lambda x,termcode: length(x)',
relation='lambda pos,x,termcode: and_f4(equal(item(length(x),x),n41),equal(item(pos,x),n40),isnamelc(slice(n1,acfull(pos),x)),istermlist_help(slice(sc(pos),acfull(length(x)),x),termcode))'
)
isterm=recursive_relation(config,
name='isterm',
desc='x is a term',
zero='lambda: n0',
relation='lambda x,y: or_f(isnamelc(x),notzero(isterm1(x,y)))'
)
istermlist2=argmin_function(config,
name='istermlist2',
desc='returns position of , if x has the form istermlist , isterm (using the now defined isterm)',
max='lambda x,termlistcode: length(x)',
relation='lambda pos,x,termlistcode: and_f3(equal(item(pos,x),n44),recursive_istermlist(termlistcode,slice(n1,acfull(pos),x)),isterm(slice(sc(pos),length(x),x)))'
)
istermlist=recursive_relation(config,
name='istermlist',
desc='x is a comma separated list of terms (using the now defined isterm)',
zero='lambda: n0',
relation='lambda x,y: or_f(isterm(x),notzero(istermlist2(x,y)))'
)
termlistpos=argmin_function(config,
name='termlistpos',
desc='returns position of ( if x has the form isnamelc ( istermlist )',
max='lambda x: length(x)',
relation='lambda pos,x: and_f4(equal(item(length(x),x),n41),equal(item(pos,x),n40),isnamelc(slice(n1,acfull(pos),x)),istermlist(slice(sc(pos),acfull(length(x)),x)))'
)
term_namelc=basic_function(config,
name='term_namelc',
desc='returns namelc if x has the form namelc ( termlist )',
define='lambda x: cond(notzero(termlistpos(x)),slice(n1,acfull(termlistpos(x)),x),n0)'
)
term_termlist=basic_function(config,
name='term_termlist',
desc='returns termlist if x has the form namelc ( termlist )',
define='lambda x: cond(notzero(termlistpos(x)),slice(sc(termlistpos(x)),acfull(length(x)),x),n0)'
)
lasttermpos=argmin_function(config,
name='lasttermpos',
desc='returns position of , if x has the form termlist , term',
max='lambda x: length(x)',
relation='lambda pos,x: and_f3(equal(item(pos,x),n44),istermlist(slice(n1,acfull(pos),x)),isterm(slice(sc(pos),length(x),x)))'
)
termlist_start=basic_function(config,
name='termlist_start',
desc='returns termlist if x has the form termlist, term',
define='lambda x: cond(notzero(lasttermpos(x)),slice(n1,acfull(lasttermpos(x)),x),n0)'
)
termlist_end=basic_function(config,
name='termlist_end',
desc='returns term if x has the form termlist, term',
define='lambda x: cond(notzero(lasttermpos(x)),slice(ad(n2,acfull(lasttermpos(x))),length(x),x),n0)'
)
# isproposition:
# isnameuc ( termlist )
isproposition1=argmin_function(config,
name='isproposition1',
desc='returns position of ( if x has the form isnameuc ( termlist )',
max='lambda x: length(x)',
relation='lambda pos,x: and_f4(equal(item(length(x),x),n41),equal(item(pos,x),n40),isnameuc(slice(n1,acfull(pos),x)),istermlist(slice(sc(pos),acfull(length(x)),x)))'
)
isproposition=basic_function(config,
name='isproposition',
desc='x is a proposition',
define='lambda x: notzero(isproposition1(x))'
)
proposition_nameuc=basic_function(config,
name='proposition_nameuc',
desc='returns nameuc if x has the form nameuc ( termlist )',
define='lambda x: cond(isproposition(x),slice(n1,acfull(isproposition1(x)),x),n0)'
)
proposition_termlist=basic_function(config,
name='proposition_termlist',
desc='returns termlist if x has the form nameuc ( termlist )',
define='lambda x: cond(isproposition(x),slice(sc(isproposition1(x)),acfull(length(x)),x),n0)'
)
# isequation:
# term = term
isequation1=argmin_function(config,
name='isequation1',
desc='returns position of = if x has the form term = term',
max='lambda x: length(x)',
relation='lambda pos,x: and_f3(equal(item(pos,x),n61),isterm(slice(n1,acfull(pos),x)),isterm(slice(sc(pos),length(x),x)))'
)
isequation=basic_function(config,
name='isequation',
desc='x is an equation',
define='lambda x: notzero(isequation1(x))'
)
equation_term1=basic_function(config,
name='equation_term1',
desc='returns term1 if x has the form term1 = term2',
define='lambda x: cond(isequation(x),slice(n1,acfull(isequation1(x)),x),n0)'
)
equation_term2=basic_function(config,
name='equation_term2',
desc='returns term2 if x has the form term1 = term2',
define='lambda x: cond(isequation(x),slice(sc(isequation1(x)),length(x),x),n0)'
)
# needs formulacode
isnegation_help=basic_function(config,
name='isnegation_help',
desc='x has the form ~ formula (is a negation)',
define='lambda x,formulacode: and_f(equal(item(n1,x),n126),recursive_isformula(formulacode,slice(n2,length(x),x)))'
)
# needs formulacode
# we need slice(2+x to avoid endless recursion in actual computation
isconjunction1_help=argmin_function(config,
name='isconjunction1_help',
desc='returns position of & if x has the form ( formula & formula )',
max='lambda x,formulacode: length(x)',
relation='lambda pos,x,formulacode: and_f5(equal(item(n1,x),n40),equal(item(length(x),x),n41),equal(item(pos,x),n38),recursive_isformula(formulacode,slice(n2,acfull(pos),x)),recursive_isformula(formulacode,slice(ad(n2,acfull(pos)),acfull(length(x)),x)))'
)
# needs formulacode
isconjunction_help=basic_function(config,
name='isconjunction_help',
desc='x is a conjunction',
define='lambda x,formulacode: notzero(isconjunction1_help(x,formulacode))'
)
# needs formulacode
# we need slice(2+x to avoid endless recursion in actual computation
isgeneralization1_help=argmin_function(config,
name='isgeneralization1_help',
desc='returns position of : if x has the form ! isnamelc : formula',
max='lambda x,formulacode: length(x)',
relation='lambda pos,x,formulacode: and_f4(equal(item(n1,x),n33),equal(item(pos,x),n58),isnamelc(slice(n2,acfull(pos),x)),recursive_isformula(formulacode,slice(ad(n2,acfull(pos)),length(x),x)))'
)
# needs formulacode
isgeneralization_help=basic_function(config,
name='isgeneralization_help',
desc='x is a generalization',
define='lambda x,formulacode: notzero(isgeneralization1_help(x,formulacode))'
)
isformula=recursive_relation(config,
name='isformula',
desc='x is a formula (proposition|equation|negation|conjunction|generalization)',
zero='lambda: n0',
relation='lambda x,y: or_f5(isproposition(x),isequation(x),isnegation_help(x,y),isconjunction_help(x,y),isgeneralization_help(x,y))'
)
# redefinition of formula functions using isformula
isnegation=basic_function(config,
name='isnegation',
desc='x has the form ~ formula (is a negation)',
define='lambda x: and_f(equal(item(n1,x),n126),isformula(slice(n2,length(x),x)))'
)
# we need slice(2+x to avoid endless recursion in actual computation
isconjunction1=argmin_function(config,
name='isconjunction1',
desc='returns position of & if x has the form ( formula & formula )',
max='lambda x: length(x)',
relation='lambda pos,x: and_f5(equal(item(n1,x),n40),equal(item(length(x),x),n41),equal(item(pos,x),n38),isformula(slice(n2,acfull(pos),x)),isformula(slice(ad(n2,acfull(pos)),acfull(length(x)),x)))'
)
isconjunction=basic_function(config,
name='isconjunction',
desc='x is a conjunction',
define='lambda x: notzero(isconjunction1(x))'
)
# we need slice(2+x to avoid endless recursion in actual computation
isgeneralization1=argmin_function(config,
name='isgeneralization1',
desc='returns position of : if x has the form ! isnamelc : formula',
max='lambda x: length(x)',
relation='lambda pos,x: and_f4(equal(item(n1,x),n33),equal(item(pos,x),n58),isnamelc(slice(n2,acfull(pos),x)),isformula(slice(ad(n2,acfull(pos)),length(x),x)))'
)
isgeneralization=basic_function(config,
name='isgeneralization',
desc='x is a generalization',
define='lambda x: notzero(isgeneralization1(x))'
)
negation_formula=basic_function(config,
name='negation_formula',
desc='returns formula if x has the form ~ formula',
define='lambda x: cond(isnegation(x),slice(n2,length(x),x),n0)'
)
conjunction_formula1=basic_function(config,
name='conjunction_formula1',
desc='returns formula1 if x has the form ( formula1 & formula2 )',
define='lambda x: cond(isconjunction(x),slice(n2,acfull(isconjunction1(x)),x),n0)'
)
conjunction_formula2=basic_function(config,
name='conjunction_formula2',
desc='returns formula2 if x has the form ( formula1 & formula2 )',
define='lambda x: cond(isconjunction(x),slice(ad(n2,acfull(isconjunction1(x))),acfull(length(x)),x),n0)'
)
generalization_namelc=basic_function(config,
name='generalization_namelc',
desc='returns namelc if x has the form ! isnamelc : formula',
define='lambda x: cond(isgeneralization(x),slice(n2,acfull(isgeneralization1(x)),x),n0)'
)
generalization_formula=basic_function(config,
name='generalization_formula',
desc='returns formula if x has the form ! isnamelc : formula',
define='lambda x: cond(isgeneralization(x),slice(ad(n2,acfull(isgeneralization1(x))),length(x),x),n0)'
)
getsuccx=basic_function(config,
name='getsuccx',
desc='returns sc(x) as term (x is supposed to be a term)',
define='lambda x: concat5(n115,n99,n40,x,n41)'
)
number=primitive_recursive_function(config,
name='number',
desc='returns x as term expressed with n0 and sc()',
zero='lambda: concat(n110,n48)',
next='lambda x: getsuccx(number(x))'
)