-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyperop.rb
More file actions
365 lines (302 loc) · 6.07 KB
/
hyperop.rb
File metadata and controls
365 lines (302 loc) · 6.07 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
require_relative 'expr'
# Combine a and b using op{k}, the kth "hyperization" of op i.e.
# a if b = 1
# a op b if k = 0
# a op{k-1} (a op{k} (b-1)) if k > 0
#
# If k is omitted, then k = b, giving the "limit" of op i.e.
# a op{b} b
def successor(a, b, k=b, n=1, &op)
if b == 1
a
elsif k == 1
op[a, b]
elsif n == 1
successor(a, successor(a, b-1, k, 1, &op), k-1, 1, &op)
else
successor(a, b, b, n-1) do |x, y|
successor(x, y, k-1, n, &op)
end
end
end
# Combine a and b using the kth hyperoperation e.g.
# a + b if k = 0
# a * b if k = 1
# a ^{k-1} b if k > 1 (see https://en.wikipedia.org/wiki/Knuth%27s_up-arrow_notation)
#
# If k is omitted, then k = b, giving
# a ^{b-1} b
#
# a *{k} b
def hyper_k(a, b, k=b)
successor(a, b, k) do |x, y|
x + y
end
end
# a *{n} b
def hyper_n(a, b)
hyper_k(a, b, b)
end
# a *{n+k} b
def hyper_n_plus_k(a, b, k=b)
successor(a, b, k) do |x, y|
hyper_n(x, y)
end
end
# a *{kn} b
def hyper_kn(a, b, k=b)
successor(a, b, k, 2) do |x, y|
hyper_n(x, y)
end
end
# a *{n^k} b
def hyper_n_up_k(a, b, k)
successor(a, b, k, 3) do |x, y|
hyper_n(x, y)
end
end
def hyper_n_up_n(a, b)
hyper_n_up_k(a, b, b)
end
def hyper_n_up2_k(a, b, k)
successor(a, b, k, 4) do |x, y|
hyper_n(x, y)
end
end
# a *{2n} b
def hyper_2n(a, b)
hyper_n_plus_k(a, b, b)
end
# a *{2n+k} b
def hyper_2n_plus_k(a, b, k=b)
successor(a, b, k) do |x, y|
hyper_2n(x, y)
end
end
# a *{3n} b
def hyper_3n(a, b)
hyper_2n_plus_k(a, b, b)
end
def hyper_3n_plus_k(a, b, k)
successor(a, b, k) do |x, y|
hyper_2n_plus_k(x, y, y)
end
end
# a *{4n+k} b
def hyper_4n_plus_k(a, b, k)
successor(a, b, k) do |x, y|
hyper_3n_plus_k(x, y, y)
end
end
# a *{5n+k} b
def hyper_5n_plus_k(a, b, k)
successor(a, b, k) do |x, y|
hyper_4n_plus_k(x, y, y)
end
end
def hyper_6n(a, b)
hyper_5n_plus_k(a, b, b)
end
# a *{6n+k} b
def hyper_6n_plus_k(a, b, k)
successor(a, b, k) do |x, y|
hyper_5n_plus_k(x, y, y)
end
end
def hyper_n2(a, b)
hyper_kn(a, b, b)
end
def hyper_n2_k(a, b, k)
successor(a, b, k) do |x, y|
hyper_n2(x, y)
end
end
def hyper_2n2(a, b)
hyper_n2_k(a, b, b)
end
def hyper_2n2_k(a, b, k)
successor(a, b, k) do |x, y|
hyper_2n2(x, y)
end
end
def hyper_3n2(a, b)
hyper_2n2_k(a, b, b)
end
def hyper_3n2_k(a, b, k)
successor(a, b, k) do |x, y|
hyper_3n2(x, y)
end
end
def hyper_kn2(a, b, k)
successor(a, b, k, 2) do |x, y|
hyper_n2(x, y)
end
end
def hyper_n3(a, b)
hyper_kn2(a, b, b)
end
def hyper_kn3(a, b, k)
successor(a, b, k, 2) do |x, y|
hyper_n3(x, y)
end
end
def hyper_n4(a, b)
hyper_kn3(a, b, b)
end
def limit_kn(a, b)
limit(a, b) do |x, y|
limit_k(x, y)
end
end
def succop(a, k, b, &op)
if b.one?
a
elsif k.zero?
op[a, b]
else
binary_pow(a, b) do |x, y|
succop(x, k-1, y, &op)
end
end
end
def limitop(a, b, &op)
if b.one?
a
else
succop(a, b, b, &op)
end
end
def succop_k(a, k, b)
succop(a, k, b) do |x, y|
x + y
end
end
def limitop_k(a, b)
limitop(a, b) do |x, y|
x + y
end
end
def succop_kn(a, k, b)
succop(a, k, b) do |x, y|
limitop_k(x, y)
end
end
def limitop_kn(a, b)
limitop(a, b) do |x, y|
limitop_k(x, y)
end
end
def succop_n_k(a, k, b)
succop(a, k, b) do |x, y|
limitop_kn(x, y)
end
end
def limitop_n_k(a, b)
limitop(a, b) do |x, y|
limitop_kn(x, y)
end
end
class Integer
def hyperop(x, y)
case self
when 0
x + y
when 1
x * y
else
negative? and raise ArgumentError, "Hyperoperation is undefined for negative index #{self}"
if x.one?
y
elsif y.one?
x
else
binary_pow(x, y, 1) do |a, b|
(self-1).hyperop(a, b)
end
end
end
end
end
module Hyperop
class Base
include Latex::Inspectable
def inspect_latex
"\\circ_{#{subscript}}"
end
def render(a, b)
"#{Latex.render(a)} + #{Latex.render(b)}"
end
def successor
Successor.new(self)
end
def limit
Limit.new(self)
end
end
class Primitive < Base
def subscript
'0'
end
def apply(a, b)
a + b
end
def limit
self
end
end
class Limit < Base
attr :op
def initialize(op)
@op = op
end
def subscript
op.successor('n').subscript
end
def apply(a, b)
op.successor(b).apply(a, b)
end
def limit
self
end
def successor(n=1)
Successor.new(self, n)
end
end
class Successor < Base
attr :op, :index
def initialize(op, index)
@op = op
@index = index
end
def subscript
"#{op.subscript} + #{index}"
end
def _apply(n, a, b)
if a.zero? || b.zero?
0
elsif b.one?
a
elsif n.zero?
op.apply(a, b)
else
_apply(n-1, a, _apply(n, a, b-1))
end
end
def apply(a, b)
_apply(index, a, b)
end
def successor(n=1)
if n.zero?
self
else
n = index + n
if n.zero?
op
else
Successor.new(op, index + n)
end
end
end
end
P = Primitive.new
end