Advertisement
Guest User

taranovsky.rb

a guest
Mar 29th, 2020
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.12 KB | None | 0 0
  1. =begin
  2. Usage: $ irb -r ./taranovsky.rb
  3. irb(main):001:0> include Taranovsky
  4. => Object
  5. irb(main):002:0> ZERO
  6. => 0 : ON
  7. irb(main):003:0> ONE
  8. => C(0, 0) : ON
  9. irb(main):004:0> ω
  10. => C(C(0, 0), 0) : ON
  11. irb(main):005:0> Ω
  12. => Ω_1 : ON
  13. irb(main):006:0> Omega.new(2)
  14. => Ω_2 : ON
  15. irb(main):007:0> C.nat(3)
  16. => C(0, C(0, C(0, 0))) : ON
  17. irb(main):008:0> ε_0 = C(Ω, 0)
  18. => C(Ω_1, 0) : ON
  19. irb(main):009:0> ω < ε_0
  20. => true
  21. irb(main):010:0> ε_0[C.nat 3]
  22. => C(C(C(0, 0), 0), 0) : ON
  23. irb(main):011:0> he0 = Hardy(C(Ω, 0), 2)
  24. => H[C(Ω_1, 0)](2)
  25. irb(main):012:0> he0 = he0.next
  26. => H[C(C(0, 0), 0)](2)
  27. irb(main):013:0> he0 = he0.next
  28. => H[C(0, C(0, 0))](2)
  29. irb(main):014:0> he0 = he0.next
  30. => H[C(0, 0)](3)
  31. irb(main):015:0> he0 = he0.next
  32. => H[0](4)
  33. irb(main):016:0> he0 = he0.value
  34. => 4
  35. =end
  36.  
  37. module Taranovsky
  38.   class Hardy
  39.     attr_reader :a, :n
  40.  
  41.     def initialize(a, n)
  42.       a = C.nat a if a.kind_of? Integer
  43.       @a, @n = a, n
  44.     end
  45.    
  46.     def inspect
  47.       "H[#{a.to_s}](#{n.to_s})"
  48.     end
  49.    
  50.     def value
  51.       a, n = @a, @n
  52.       loop do
  53.         case a.cof
  54.         when ZERO
  55.           return n
  56.         when ONE
  57.           a = a[ZERO]
  58.           n += 1
  59.         else
  60.           a = a[C.nat n]
  61.         end
  62.       end
  63.     end
  64.    
  65.     def next
  66.       case @a.cof
  67.       when ZERO
  68.         self
  69.       when ONE
  70.         Hardy.new(@a[ZERO], @n+1)
  71.       else
  72.         Hardy.new(@a[C.nat @n], @n)
  73.       end
  74.     end
  75.   end
  76.  
  77.   def Hardy(a, n)
  78.     Hardy.new(a, n)
  79.   end
  80.  
  81.   class Ord
  82.     def cof;end
  83.     def base;end
  84.     def [] n;end
  85.     def to_a; [];end
  86.    
  87.     def < other
  88.       x, y = to_a, other.to_a
  89.       x.zip(y) do |(a, b)|
  90.         return true if a < b
  91.         return false if  a > b
  92.       end
  93.       return x.size < y.size
  94.     end
  95.    
  96.     def === other; end
  97.    
  98.     def <= other
  99.       self < other or self === other
  100.     end
  101.    
  102.     def >= other
  103.       not self < other
  104.     end
  105.    
  106.     def > other
  107.       self >= other and not self === other
  108.     end
  109.    
  110.     def inspect
  111.       "#{to_s} : ON"
  112.     end
  113.   end
  114.  
  115.   class Zero < Ord
  116.     def cof
  117.       self
  118.     end
  119.     def base
  120.       self
  121.     end
  122.     def []
  123.       raise "0 has no sequence"
  124.     end
  125.     def to_a
  126.       [1]
  127.     end
  128.    
  129.     def === other
  130.       other.kind_of? Zero
  131.     end
  132.    
  133.     def to_s
  134.       '0'
  135.     end
  136.   end
  137.  
  138.   class Omega < Ord
  139.     attr_reader :n
  140.     def initialize(n = 1)
  141.       @n = n
  142.     end
  143.     def cof
  144.       self
  145.     end
  146.     def base
  147.       self
  148.     end
  149.     def [](a)
  150.       a
  151.     end
  152.     def to_a
  153.       [@n+1]
  154.     end
  155.     def === other
  156.       other.kind_of? Omega and other.n === @n
  157.     end
  158.     def to_s
  159.       "Ω_#{@n}"
  160.     end
  161.   end
  162.  
  163.   class C < Ord
  164.     attr_reader :a, :b  
  165.    
  166.     def initialize(a, b)
  167.       @a, @b = a, b
  168.     end
  169.    
  170.     def self.nat(n)
  171.       if n > 0
  172.         C[ZERO, nat(n-1)]
  173.       else
  174.         ZERO
  175.       end
  176.     end
  177.    
  178.     def self.[](a, b)
  179.       a = nat a if a.kind_of? Integer
  180.       b = nat b if b.kind_of? Integer
  181.       new(a, b)
  182.     end
  183.    
  184.     def base
  185.       @b.base
  186.     end
  187.    
  188.     def cof
  189.       case @a.cof
  190.       when ZERO
  191.         ONE
  192.       when ONE
  193.         OMEGA
  194.       else
  195.         if @a.base <= @b.base
  196.           @a.cof
  197.         else
  198.           OMEGA
  199.         end
  200.       end
  201.     end
  202.    
  203.     def [](n)
  204.       case @a.cof
  205.       when ZERO
  206.         @b
  207.       when ONE
  208.         if n === ZERO
  209.           @b
  210.         else
  211.           C[@a[0], self[n[0]]]
  212.         end
  213.       else
  214.         if @a.base <= @b.base
  215.           C[@a[n], @b]
  216.         else
  217.           if n === ZERO
  218.             @b
  219.           else
  220.             C[@a[self[n[0]]], @b.base]
  221.           end
  222.         end
  223.       end
  224.     end
  225.    
  226.     def to_a
  227.       @b.to_a + @a.to_a + [0]
  228.     end
  229.    
  230.     def === other
  231.       other.kind_of? C and @a === other.a and @b === other.b
  232.     end
  233.    
  234.     def to_s
  235.       "C(#{@a.to_s}, #{@b.to_s})"
  236.     end
  237.   end
  238.  
  239.   ZERO = Zero.new
  240.   ONE = C[ZERO, ZERO]
  241.   OMEGA_1 = Omega.new(1)
  242.   OMEGA = C[ONE, ZERO]
  243.  
  244.   def C(a, b)
  245.     C[a, b]
  246.   end
  247.  
  248.   def Ω
  249.     OMEGA_1
  250.   end
  251.  
  252.   def ω
  253.     OMEGA
  254.   end
  255. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement