Giter VIP home page Giter VIP logo

python-church-encodings's Introduction

Church Encodings in Python

This 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 encodings were developed by the late and famous Alonzo Church. Church is probably most well known for inventing lambda calculus, a formal branch of mathematics that introduces the notion of lambdas, or anonymous functions. You may have used them before when programming.

Church encodings are a very interesting development arising from lambda calculus. Church found out that every concept in programming languages can be represented using functions! 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

Z Combinator

The 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 wiki for more details.

Conditionals

The translation for some of the lambda functions involved using regular if/then statements instead of the ifelse lamdba 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.

Installation

  • Install using pip.
    pip install --user church_encoding

  • Import the church module.
    python
    >>> import church_encoding.church as church

  • Play around with the functions.
    >>> dir(church)
    >>> (church.unchurch_bool) ((church.AND) (church.true) (church.false))
    False

Here is a full list of the commands:

Boolean true/false: true, false, unchurch_bool

Boolean operators: AND, NOT, OR, XOR

Z-combinator: Z

Conditional: ifelse

Natural numbers: zero, one, two, three, num, unchurch_num

Integers: convertNZ, unchurch_int

Comparison operators (natural numbers): eq, geq, gt, leq, lt

Arithmetic operators (natural numbers): pred, succ, add, sub, mult, div, divnZ, exp, fac, is_zero

Arithmetic operators (integers): addZ, subZ, multZ, divZ, neg, onezero

Lists: car, cdr, cons

For usage examples, see the test suite

Credits

I'd like to give credit where it's due:

And finally, Church himself, for being such a badass!

Alonzo Church
Alonzo Church

python-church-encodings's People

Contributors

risto-stevcev avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

saintchimera

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.