This commit is contained in:
2025-10-04 23:34:16 +01:00
parent a86ccba427
commit 5a554f2c8e
2 changed files with 10 additions and 6 deletions

View File

@@ -279,7 +279,7 @@ labubu_ast* reduce (labubu_ast* ast, labubu_variables* env) {
case AST_FUNCTION:
f = malloc(sizeof (labubu_ast));
f->tag = AST_FUNCTION;
f->data.AST_FUNCTION.body = reduce(ast->data.AST_FUNCTION.body, env);
f->data.AST_FUNCTION.body = ast->data.AST_FUNCTION.body;
f->data.AST_FUNCTION.lambda = ast->data.AST_FUNCTION.lambda;
return f;
case AST_VARIABLE:
@@ -299,17 +299,15 @@ labubu_ast* reduce (labubu_ast* ast, labubu_variables* env) {
*ast1 = *ast;
return ast1;
case AST_APPLICATION:
v = reduce(ast->data.AST_APPLICATION.v, env);
f = reduce(ast->data.AST_APPLICATION.f, env);
if (f->tag == AST_FUNCTION) {
push(env, f->data.AST_FUNCTION.lambda, v);
push(env, f->data.AST_FUNCTION.lambda, ast->data.AST_APPLICATION.v);
labubu_ast* ast1 = reduce(f->data.AST_FUNCTION.body, env);
pop(env);
return ast1;
} else {
v = reduce(ast->data.AST_APPLICATION.v, env);
labubu_ast* ast1 = malloc(sizeof (labubu_ast));
*ast1 = (labubu_ast) {
.tag = AST_APPLICATION,
@@ -348,5 +346,6 @@ int main (void) {
read("(\\x.x x)");
read("(\\x.x \\y.y)");
read("((\\a.\\b.((a b) \\x.\\y.y) \\x.\\y.y) \\x.\\y.x)");
read("(\\n.\\f.\\x.(f ((n f) x)) \\f.\\x.(f (f (f x))))");
read("\\f.(\\x.(f (x x)) \\x.(f (x x)))");
read("\\f.(\\x.(f(x x))\\x.(f(x x)))");
}

5
utils.hs Normal file
View File

@@ -0,0 +1,5 @@
church :: Integer -> String
church n = "\\f.\\x."<>church' n
where church' :: Integer -> String
church' 0 = "x"
church' x = "(f " <> church' (x - 1) <> ")"