notnl

Java 实现 λ-calculus

  •  
  •   notnl · Nov 21, 2016 · 2529 views
    This topic created in 3459 days ago, the information mentioned may be changed or developed.

    最近教授上课讲了讲 Lambda 的 reduction ,比较感兴趣,就下来自己用 Java 实现了一个。

    github: noti0na1/LambdaCalculus-java

    例子

    生成一个 lambda 表达式并输出

    Expression fun = λ("x", apply(λ("x", apply("f", "x")), "x"));
    System.out.println("fun = " + fun.toString());
    // fun = λx.(λx.f x) x
    

    对于一个 lambda 表达式,你也可以对它进行归约

    System.out.println("fun  (reduced) = " + fun.reduce());
    //  fun  (reduced) =  λx.f x
    

    还可以归约到底并显示每一步

    // apply this function
    Expression app = apply(fun, "z");
    // (λx.(λx.f x) x) z
    System.out.println("app = " + app.toString());
    // f z
    System.out.println("app (fully reduced) = " + app.deepReduce().toString());
    // print reduce steps
    // or app.printReduceSteps(2) to print certain steps
    app.printReduceSteps();
    // (λx.(λx.f x) x) ((λx.x) z)
    // (λx.f x) ((λx.x) z)
    // (λx.f x) z
    // f z
    

    更多例子可以查看 com.notnl.lambda.examples, 我在例子里还实现了 lambda 演算中的算术、逻辑与谓词和有序对 (cons)。

    欢迎各位指出问题或给建议,谢谢 😁

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4651 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 10:09 · PVG 18:09 · LAX 03:09 · JFK 06:09
    ♥ Do have faith in what you're doing.