From 51e6dd2c75d18087948b5c2378d343aa8914903f Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 1 Feb 2025 17:39:11 +0530 Subject: [PATCH] Add Lambda Calculus in Java --- languages/j/java/LambdaCore.java | 65 ++++++++++++++++++++++++++++++++ languages/j/java/README.md | 23 +++++++++++ 2 files changed, 88 insertions(+) create mode 100644 languages/j/java/LambdaCore.java create mode 100644 languages/j/java/README.md diff --git a/languages/j/java/LambdaCore.java b/languages/j/java/LambdaCore.java new file mode 100644 index 0000000..653736c --- /dev/null +++ b/languages/j/java/LambdaCore.java @@ -0,0 +1,65 @@ +import java.util.function.Function; + +public interface LambdaCore { + + interface Bool extends Function> { + } + + interface UnaryBoolOp extends Function { + } + + interface BinaryBoolOp extends Function> { + } + + interface ChurchNumeral extends Function, Function> { + } + + Bool TRUE = x -> y -> x; + Bool FALSE = x -> y -> y; + + UnaryBoolOp NOT = b -> b.apply(FALSE).apply(TRUE); + BinaryBoolOp AND = b1 -> b2 -> b1.apply(b2).apply(FALSE); + BinaryBoolOp OR = b1 -> b2 -> b1.apply(TRUE).apply(b2); + + ChurchNumeral ZERO = x -> y -> y; + Function SUCC = w -> y -> x -> y.apply(w.apply(y).apply(x)); + +// Function PRED = n -> f -> x -> +// n.apply(g -> h -> h.apply(g.apply(f))) +// .apply(u -> x) +// .apply(u -> u); + + static void main(String[] args) { + printBool(TRUE); // TRUE + printBool(FALSE); // FALSE + + printBool(NOT.apply(TRUE)); // FALSE + printBool(NOT.apply(FALSE)); // TRUE + + printBool(AND.apply(FALSE).apply(FALSE)); // FALSE + printBool(AND.apply(TRUE).apply(FALSE)); // FALSE + printBool(AND.apply(FALSE).apply(TRUE)); // FALSE + printBool(AND.apply(TRUE).apply(TRUE)); // TRUE + + printBool(OR.apply(FALSE).apply(FALSE)); // FALSE + printBool(OR.apply(TRUE).apply(FALSE)); // TRUE + printBool(OR.apply(FALSE).apply(TRUE)); // TRUE + printBool(OR.apply(TRUE).apply(TRUE)); // TRUE + + printChurchNumeral(ZERO); // 0 + printChurchNumeral(SUCC.apply(ZERO)); // 1 + } + + static void printBool(Bool b) { + if (b == TRUE) + System.out.println("BOOLEAN TRUE"); + else if (b == FALSE) + System.out.println("BOOLEAN FALSE"); + else + throw new IllegalStateException(); + } + + static void printChurchNumeral(ChurchNumeral n) { + System.out.println(n.apply(x -> x + 1).apply(0)); + } +} diff --git a/languages/j/java/README.md b/languages/j/java/README.md new file mode 100644 index 0000000..b6dd14b --- /dev/null +++ b/languages/j/java/README.md @@ -0,0 +1,23 @@ +# Java 21 Installation Guide + +## Download Java 21 +Download Java 21 from the official Oracle website: +[Java 21 Downloads](https://www.oracle.com/in/java/technologies/downloads/#java21) + +## Installation Steps +1. Select the appropriate JDK version for your OS (Windows, macOS, or Linux). +2. Download the installer or compressed archive. +3. Follow the installation instructions provided by Oracle. + +## Verify Installation +Run the following command to check the installed Java version: + +```sh +java -version +``` + +# Run Code + +```sh +java LambdaCore.java +```