Skip to content

Instantly share code, notes, and snippets.

@0x0dea

0x0dea/alonzo.rb Secret

Created August 15, 2015 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x0dea/7652f8f75f64b2c5da59 to your computer and use it in GitHub Desktop.
Save 0x0dea/7652f8f75f64b2c5da59 to your computer and use it in GitHub Desktop.
def λ
$. += 1
proc.curry
end
# Convert a Ruby integer to a Church numeral.
def church n
n.times.reduce(λ { |_, x| x }) { |n| λ { |n, f, x| f[n[f][x]] }[n] }
end
# Undo the above for printing/verifying that this madness wasn't all for naught.
def unchurch p
p[-> n { n + 1 }][0]
end
Common = λ{|f|λ{|x|f[λ{|_|x[x][_]}]}[λ{|x|f[λ{|_|x[x][_]}]}]}[λ{|f,a,b,c,i|λ{|n|
n[λ{|x|λ{|a,b|b}}][λ{|a,b|a}]}[i][c][λ{|_|λ{|m,n|n[λ{|n,f,x|f[n[f][x]]}][m]}[λ{|
f|λ{|x|f[λ{|_|x[x][_]}]}[λ{|x|f[λ{|_|x[x][_]}]}]}[λ{|f,n|λ{|n|n[λ{|x|λ{|a,b|b}}][
λ{|a,b|a}]}[n][λ{|a,b|a}][λ{|_|λ{|n|n[λ{|x|λ{|a,b|b}}][λ{|a,b|a}]}[λ{|n,f,x|n[λ{
|g,h|h[g[f]]}][λ{|_|x}][λ{|u|u}]}[n]][λ{|a,b|b}][f[λ{|n,f,x|n[λ{|g,h|h[g[f]]}][λ{
|_|x}][λ{|u|u}]}[λ{|n,f,x|n[λ{|g,h|h[g[f]]}][λ{|_|x}][λ{|u|u}]}[n]]]][_]}]}][λ{|
m,n|n[λ{|n,f,x|f[n[f][x]]}][m]}[a][b]][λ{|f,x|f[x]}][λ{|f,x|x}]][f[λ{|f|λ{|x|f[λ{
|_|x[x][_]}]}[λ{|x|f[λ{|_|x[x][_]}]}]}[λ{|f,n,m|λ{|n|n[λ{|x|λ{|a,b|b}}][λ{|a,b|a
}]}[λ{|m,n|n[λ{|n,f,x|n[λ{|g,h|h[g[f]]}][λ{|_|x}][λ{|u|u}]}][m]}[n][m]][n][λ{|_|
f[λ{|n,f,x|n[λ{|g,h|h[g[f]]}][λ{|_|x}][λ{|u|u}]}[n]][λ{|n,f,x|f[n[f][x]]}[m]][_]
}]}][a][λ{|f,x|x}]][λ{|f|λ{|x|f[λ{|_|x[x][_]}]}[λ{|x|f[λ{|_|x[x][_]}]}]}[λ{|f,n,
m|λ{|n|n[λ{|x|λ{|a,b|b}}][λ{|a,b|a}]}[λ{|m,n|n[λ{|n,f,x|n[λ{|g,h|h[g[f]]}][λ{|_|
x}][λ{|u|u}]}][m]}[n][m]][n][λ{|_|f[λ{|n,f,x|n[λ{|g,h|h[g[f]]}][λ{|_|x}][λ{|u|u}
]}[n]][λ{|n,f,x|f[n[f][x]]}[m]][_]}]}][b][λ{|f,x|x}]][c][λ{|n,f,x|n[λ{|g,h|h[g[f
]]}][λ{|_|x}][λ{|u|u}]}[i]]][_]}]}]
a = church 10
b = church 17
c = church 8 # bit width of calculations
puts "Common bits: #{unchurch Common[a, b, λ { |_, x| x }, c]}"
puts "Created #$. lambdas!" # ^ Church zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment