Skip to content

Instantly share code, notes, and snippets.

@YuukiARIA
Created April 13, 2015 10:50
Show Gist options
  • Save YuukiARIA/a4bd8a5df3cfaf3db0ba to your computer and use it in GitHub Desktop.
Save YuukiARIA/a4bd8a5df3cfaf3db0ba to your computer and use it in GitHub Desktop.
module Peano
O = [].freeze
def succ(n)
n | [n]
end
def pred_rec(n, p, q)
if n == p
q
else
pred_rec(n, succ(p), succ(q))
end
end
def pred(n)
if n == O
O
else
pred_rec(n, succ(O), O)
end
end
def add(x, y)
if y == O
x
else
succ(add(x, pred(y)))
end
end
end
class Array
def to_int
if self == Peano::O
0
else
1 + Peano.pred(self).to_int
end
end
end
class Integer
def to_peano
if self == 0
Peano::O
else
Peano.succ((self - 1).to_peano)
end
end
end
include Peano
p O.to_int
# ==> 0
p succ(O).to_int
# ==> 1
p succ(succ(O)).to_int
# ==> 2
p pred(succ(succ(O))).to_int
# ==> 1
p pred(add(10.to_peano, 3.to_peano)).to_int
# ==> 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment