Lambda calculus in C: logical operator and NOT operator

I wanted to give implementations in different programming languages ​​for constructing the lambda calculus of Boolean elements and the NOT operator.

It:

TRUE = lx.ly. x
FALSE = lx.ly. y
NOT = lx. x FALSE TRUE

This is trivial to do in Javascript and Python, for example,

var TRUE = function(x,y){ return x;} 
var FALSE = function(x,y){ return y;}
var NOT = function(b){ return b(FALSE,TRUE) ; } 

but I can't figure out how to do this in C.

A naive idea to implement something like this

lambda true(lambda x, lambda y){ return x ; }
lambda false(lambda x, lambda y){ return x ; }

lambda not(lambda (b)(lambda, lambda) ){ return b(false,true) ;}

not possible in C because it typedefdoes not allow a recursive definition

typedef void (*lambda)(lambda,lambda) ;not valid in C

Is there any way to do this in C? and is there a way to do this that makes sense to use as a case study? That is, if the syntax starts to become cumbersome, it ends up defeating its purpose ...

Finally, if C is ultimately too limited, the answer in C ++ will also work for me, albeit with the same “complexity” of limitation

C.

EDIT: ,

typedef void *(*bol)() ;

bol true(bol x, bol y){ return x ; }
bol false(bol x, bol y){ return x ; }

bol not(bol b ){ return b(false,true) ;}



int main(){
 bol p = not((bol)true);

 return 0;
}

EDIT2: , , , .

, @Antti Haapala @n.m , C.

, ++ .

+4
1

, C , - struct, :

#include <stdio.h>
#include <stdarg.h>

typedef struct LAMBDA {
    struct LAMBDA * (*function)(struct LAMBDA *, ...);
} *lambda;

lambda trueFunction(lambda x, ...) {return x;}
lambda true = &(struct LAMBDA) {.function = trueFunction};

lambda falseFunction(lambda x, ...) {va_list argp; va_start(argp, x); lambda y = va_arg(argp, lambda); va_end(argp); return y;}
lambda false = &(struct LAMBDA) {.function = falseFunction};

lambda notFunction(lambda b, ...) {return b->function(false, true);}
lambda not = &(struct LAMBDA) {.function = notFunction};

int main() {
    lambda p1 = not->function(true);
    lambda p2 = not->function(false);
    printf("%p %p %p %p", true, p1, false, p2);
    return 0;
}

, , , .

+1

Source: https://habr.com/ru/post/1689548/


All Articles