diff --git a/labubu.c b/labubu.c index 5138241..2c34391 100644 --- a/labubu.c +++ b/labubu.c @@ -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)))"); } diff --git a/utils.hs b/utils.hs new file mode 100644 index 0000000..cca9192 --- /dev/null +++ b/utils.hs @@ -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) <> ")"