diff options
author | Kyle Serrecchia | 2025-02-01 11:54:54 -0800 |
---|---|---|
committer | GitHub | 2025-02-01 11:54:54 -0800 |
commit | e5c24f4d3df7059025698c71ac8a4b3883f75cb0 (patch) | |
tree | cc81f2c72da40461ce79dead81c196893edf2bf9 | |
parent | a4d244dc3eeb99528ab1847c261f3474234f0598 (diff) | |
parent | ac45c34c705dc2114d1155b66545d2b3a7ed5613 (diff) |
Merge pull request #8 from Gryfenfer97/main
Looks good! I didn't know what to expect for c++. Thank you!
-rw-r--r-- | languages/c/c++/README.md | 13 | ||||
-rw-r--r-- | languages/c/c++/lambda-core.cpp | 77 |
2 files changed, 90 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..c9e73b9 --- /dev/null +++ b/languages/c/c++/lambda-core.cpp @@ -0,0 +1,77 @@ +#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); + +constexpr auto pred = [](const auto &n) { + return [&n](const auto &f) { + return [&n, &f](auto x) { + return n([&f](const auto &g) { + return [&f, g](const auto &h) { return h(g(f)); }; + })([x](const auto &u) { return x; })([](const auto &a) { return a; }); + }; + }; +}; + +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); +} |