And now for the motivation…

In Python:

def true(x, y): return x
def false(x, y): return y
def Not(x): return x(false, true)
def And(x, y): return x(y, false)
def Or(x, y): return x(true, y)
def Xor(x, y): return x(Not(y), y)

In C++:

#include <assert.h>
#include <stdio.h>

struct Boolean {
    typedef struct Boolean (*booleanSignature)(Boolean, Boolean);
    Boolean(booleanSignature ptr) : _ptr(ptr) { }
    operator booleanSignature() const { return _ptr; }
    booleanSignature _ptr;

Boolean True(Boolean x, Boolean y) {
    return x;

Boolean False(Boolean x, Boolean y) {
    return y;

Boolean Not(Boolean x) {
    return x(False, True);

Boolean And(Boolean x, Boolean y) {
    return x(y, False);

Boolean Or(Boolean x, Boolean y) {
    return x(True, y);

Boolean Xor(Boolean x, Boolean y) {
    return x(Not(y), y);

#define ASSERT(cond) \
    printf("%s     %s\n", (cond) ? "true " : "false", #cond)

int main() {
    ASSERT(And(True,  True)  == True);
    ASSERT(And(True,  False) == False);
    ASSERT(Or (True,  False) == True);
    ASSERT(Or (False, False) == True);
    ASSERT(Not(False)        == True);
    ASSERT(Xor(And(True, False), Or(False, True)) == True);

Leave a Reply

Your email address will not be published. Required fields are marked *