diff options
author | Gryfenfer97 | 2025-02-01 19:43:32 +0100 |
---|---|---|
committer | Gryfenfer97 | 2025-02-01 19:43:32 +0100 |
commit | 982fed7f0e1e52479151c3e43d78d6cf35684250 (patch) | |
tree | 84562eaf45f66977efe85ac3f79a172383c11a10 | |
parent | d49c3de9fb2b51c702c4d0f958c3a7c685201a37 (diff) |
Add C++ language
-rw-r--r-- | languages/c/c++/README.md | 13 | ||||
-rw-r--r-- | languages/c/c++/lambda-core.cpp | 67 |
2 files changed, 80 insertions, 0 deletions
diff --git a/languages/c/c++/README.md b/languages/c/c++/README.md new file mode 100644 index 0000000..41b7129 --- /dev/null +++ b/languages/c/c++/README.md @@ -0,0 +1,13 @@ +# C++ Lambda Core + +## Requirements + +- GCC 14 or above + +## Compile and run + +```sh +g++ lambda-core.cpp -std=c++23 -o app +./app +``` + diff --git a/languages/c/c++/lambda-core.cpp b/languages/c/c++/lambda-core.cpp new file mode 100644 index 0000000..3c1ae20 --- /dev/null +++ b/languages/c/c++/lambda-core.cpp @@ -0,0 +1,67 @@ +#include <print> + +namespace logic { +constexpr auto _true = [](const auto &x) { + return [&x](const auto &) { return x; }; +}; + +constexpr auto _false = [](const auto &) { + return [](const auto &y) { return y; }; +}; + +constexpr auto _not = [](const auto &x) { return x(_false)(_true); }; + +constexpr auto _and = [](const auto &x) { + return [&x](const auto &y) { return x(y)(_false); }; +}; + +constexpr auto _or = [](const auto &x) { + return [&x](const auto &y) { return x(_true)(y); }; +}; + +namespace test { +constexpr bool convert(const auto &x) { return x(true)(false); } + +void print(const auto &x) { std::println("{}", convert(x)); } +} // namespace test +} // namespace logic + +namespace church { +constexpr auto zero = [](const auto &) { + return [](const auto &x) { return x; }; +}; + +constexpr auto succ = [](const auto &n) { + return [&n](const auto &f) { + return [&n, &f](const auto &x) { return f(n(f)(x)); }; + }; +}; + +constexpr auto one = succ(zero); + +namespace test { +constexpr unsigned int convert(const auto &n) { + return n([](const auto &x) { return x + 1; })(0); +} + +void print(const auto &n) { std::println("{}", convert(n)); } +} // namespace test +} // namespace church + +int main() { + using namespace logic; + using namespace church; + + static_assert(logic::test::convert(_true) == true); + static_assert(logic::test::convert(_false) == false); + static_assert(logic::test::convert(_and(_true)(_true)) == true); + static_assert(logic::test::convert(_and(_true)(_false)) == false); + static_assert(logic::test::convert(_or(_true)(_true)) == true); + static_assert(logic::test::convert(_or(_true)(_false)) == true); + logic::test::print(_true); + + static_assert(church::test::convert(zero) == 0); + static_assert(church::test::convert(succ(zero)) == 1); + // static_assert(church::test::convert(pred(succ(one))) == 1); + church::test::print(one); +} |