-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfinite.rb
More file actions
208 lines (173 loc) · 3.71 KB
/
transfinite.rb
File metadata and controls
208 lines (173 loc) · 3.71 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
require 'active_support/core_ext/object/try'
require_relative 'math'
require_relative 'coercible'
require_relative 'set'
class Numeric
def ordinal?
false
end
def cardinal?
false
end
end
class Integer
def ordinal?
!negative?
end
def limit_ordinal?
false
end
def successor_ordinal?
self > 0
end
def cantor_normal_form
ordinal? or raise TypeError, "#{inspect} is not an ordinal"
NormalizedHash.new(0).merge!(0 => self)
end
def cardinal?
!negative?
end
def countable?
true
end
def andup
if block_given?
n = self
loop do
yield n
n += 1
end
else
self .. ALEPH0 # TODO should be ordinal
end
end
end
class Cardinal < Numeric
include Coercible
include Comparable
include Latex::Inspectable
attr :cardinal_index
def initialize(index)
index.ordinal? or raise TypeError, "#{index.inspect} is not an ordinal"
@cardinal_index = index
end
class << self
def new(index)
if index.zero?
@aleph_zero ||= super(0)
elsif index.one?
@aleph_one ||= super(1)
else
super(index)
end
end
alias_method :[], :new
def _verify(*s)
s.each do |x|
x.try(:cardinal?) or raise ArgumentError, "#{x} is not a cardinal number"
end
end
def cmp(a, b)
_verify(a, b)
if a.finite?
if b.finite?
a.to_i <=> b.to_i
else
-1
end
else
if b.finite?
1
else
a.cardinal_index <=> b.cardinal_index
end
end
end
def add(a, b)
_verify(a, b)
a.max(b)
end
def sub(a, b)
_verify(a, b)
if a > b
a
else
raise Math::DomainError
end
end
def mul(a, b)
_verify(a, b)
if a.zero? || b.zero?
0
else
a.max(b)
end
end
def div(a, b)
_verify(a, b)
raise ZeroDivisionError if b.zero?
if a > b
a
else
raise Math::DomainError
end
end
end
forward :to_s, :inspect
def inspect
"ℵ#{cardinal_index.to_subscript}"
end
def inspect_latex
"\\aleph_#{cardinal_index}"
end
def cardinal?
true
end
def finite?
false
end
def infinite?
true
end
def countable?
cardinal_index.zero?
end
def ==(x)
x.is_a?(Cardinal) && cardinal_index == x.cardinal_index
end
def <=>(x)
if x.is_a?(Numeric) && x.cardinal?
if x.finite?
1
else
cardinal_index <=> x.cardinal_index
end
end
end
def times
countable? or raise "#{self} is not enumerable"
if block_given?
i = 0
loop do
yield i
i += 1
end
else
enum_for :times
end
end
module RangePrepend
def size
if self.end.infinite?
ALEPH0
else
super
end
end
end
end
class Range
prepend Cardinal::RangePrepend
end
ALEPH0 = Cardinal[0]
ALEPH1 = Cardinal[1]