Package: church_encoding
# Church Encodings in PythonThis exercise was to demonstrate and play around with church encodings in a more involved way, and as a way for me to gain experience with python and it’s type system.Church encodingswere developed by the late and famous Alonzo Church. Church is probably most well known for inventinglambda calculus, a formal branch of mathematics that introduces the notion oflambas, oranonymous functions. You may have used them before when programming.Church encodingsare a very interesting development arising fromlambda calculus. Church found out that every concept in programming languages can be represented usingfunctions! everything from boolean logic, conditional statements, numbers (natural, integer, real, complex, imaginary), and even loops (infinite loops also)!The most interesting thing about this is that numbers aren’t anything special in math, they’re just convenient placeholders. Math is really just logic in it’s purest form.So far I’ve translated church booleans, church boolean operators, church conditionals, church comparison operators, church numerals (natural numbers), church arithmetic operators (for natural numbers), church lists, church integers, loops with the z-combinator, and church arithmetic operators (for integers) less the exponential and factorial operators.View the package on [PyPI](http://pypi.python.org/pypi/church_encoding/)## Z CombinatorThe Z combinator is used in place of the Y combinator in normal form progrmaming languages that don’t implement lazy evaluation or call by name. See the [fixed-point combinator](https://en.wikipedia.org/wiki/Fixed-point_combinator) wiki for more details.## ConditionalsThe translation for some of the lambda functions involved using regular if/then statements instead of theifelselamdba function. I suspect that this might again be a limitation like the Z combinator, where the evaluation order plays a role in being able to use lambdas in the truest sense. There may be a workaround I’ll find to get it to work some day.## InstallationInstall using pip.pip install--userchurch_encodingImport the church module.python>>> import church_encoding.church as churchPlay around with the functions.>>> dir(church)>>> (church.unchurch_bool) ((church.AND) (church.true) (church.false))FalseHere is a full list of the commands:Boolean true/false:true,false,unchurch_boolBoolean operators:AND,NOT,OR,XORZ-combinator:ZConditional:ifelseNatural numbers:zero,one,two,three,num,unchurch_numIntegers:convertNZ,unchurch_intComparison operators (natural numbers):eq,geq,gt,leq,ltArithmetic operators (natural numbers):pred,succ,add,sub,mult,div,divnZ,exp,fac,is_zeroArithmetic operators (integers):addZ,subZ,multZ,divZ,neg,onezeroLists:car,cdr,consFor usage examples, see the [test suite](https://github.com/Risto-Stevcev/python-church-encodings/blob/master/church_encoding/test/church_test.py)## CreditsI’d like to give credit where it’s due:[James Tauber’s Blog](http://jtauber.com/blog/2008/11/26/church_encoding_in_python/) for starting me off in the right direction.The [chuch encoding](https://en.wikipedia.org/wiki/Church_encoding) wikipedia page for being so incredibly detailed and informative.And finally, Church himself, for being such a badass!![Alonzo Church](https://upload.wikimedia.org/wikipedia/en/a/a6/Alonzo_Church.jpg)
Alonzo Church