Created
April 8, 2013 17:58
-
-
Save syntacticsugar/5338961 to your computer and use it in GitHub Desktop.
In mathematics, Church encoding is a means of embedding data and operators into the lambda calculus, the most familiar form being the Church numerals, a representation of the natural numbers using lambda notation. The method is named for Alonzo Church, who first encoded data in the lambda calculus this way.
Terms that are usually considered prim…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# proccc | |
def one proccc, x | |
proccc.(x) | |
end | |
def two proccc, x | |
proccc.(proccc.(x)) | |
end | |
def three proccc,x | |
proccc[proccc[proccc[x]]] | |
end | |
def zero proccc, x | |
x | |
end | |
ZERO = ->(p) { ->(x) { x }} # zero = lambda{ | |
ONE = ->(p) { ->(x) { p[x] }} | |
TWO = ->(p) { ->(x) { p[p[x]] }} | |
THREE = ->(p) { ->(x) { p[p[p[x]]] }} | |
FOUR = ->(p) { ->(x) { p[p[p[p[x]]]] }} | |
=begin | |
Now, although we’re eschewing Ruby’s features inside our program, | |
it would be useful to be able to translate these foreign representations | |
of numbers into native Ruby values once they’re outside our program — | |
so that they can be usefully inspect‐ ed on the console or asserted | |
against in tests, or at least so that we can convince ourselves that | |
they really do represent numbers in the first place. | |
=end | |
def to_integer proccc | |
proccc[->(n) { n + 1 }][0] | |
end | |
TRUE = ->(x) { ->(y) { x } } | |
FALSE = ->(x) { ->(y) { y } } | |
def to_boolean proccc | |
proccc[true][false] | |
end | |
def ifff proccc,x,y | |
proccc[x][y] | |
end | |
# now, as a sexy proccc!!!!!1 :P :P | |
IF = | |
->(proccc) { | |
->(x) { | |
->(y) { | |
proccc[x][y] | |
} | |
} | |
} | |
# if we call an unknown number with TRUE as its second argument, | |
# it’ll return TRUE immediately if the number is ZERO. | |
# | |
# If it’s not ZERO then it’ll return whatever calling "p" returns, | |
# so if we make "p" a proc which always returns FALSE, we’ll get the behaviour we want: | |
def zzzero? proccc | |
proc[ ->(x) { FALSE }][TRUE] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment