From ceecf5c55e65f45a2fd44c077848b65658de8c40 Mon Sep 17 00:00:00 2001 From: piaCOS Date: Sat, 1 Feb 2025 19:02:43 +0100 Subject: Add examples for the python language --- languages/p/python/README.md | 8 ++++++ languages/p/python/lambda-core.py | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 languages/p/python/README.md create mode 100644 languages/p/python/lambda-core.py (limited to 'languages/p') diff --git a/languages/p/python/README.md b/languages/p/python/README.md new file mode 100644 index 0000000..16e20bb --- /dev/null +++ b/languages/p/python/README.md @@ -0,0 +1,8 @@ +### Download Python 3 + +Run with: +``` +python3 lambda-core.py +``` + +*made with python 3.11.5* \ No newline at end of file diff --git a/languages/p/python/lambda-core.py b/languages/p/python/lambda-core.py new file mode 100644 index 0000000..c1a665c --- /dev/null +++ b/languages/p/python/lambda-core.py @@ -0,0 +1,53 @@ +# LOGIC +_true = lambda x: lambda y: x +_false = lambda x: lambda y: y +_not = lambda b: b(_false)(_true) +_and = lambda b1: lambda b2: b1(b2)(_false) +_or = lambda b1: lambda b2: b1(_true)(b2) + +# CHURCH NUMERALS +_zero = lambda f: lambda x: x +_succ = lambda n: lambda f: lambda x: f(n(f)(x)) +_pred = lambda n: lambda f: lambda x: n(lambda g: lambda h: h(g(f)))(lambda u: x)(lambda a: a) +_one = _succ(_zero) + +# HELPERS - not pure lambda calculus +read_bool = lambda b: print(b("t")("f")) +read_church = lambda n: print(n(lambda x: x+1)(0)) + +# EXAMPLES +print("LOGIC") +print("-------------") +print("TRUE/FALSE") +read_bool(_true) +read_bool(_false) + +print("NOT") +read_bool(_not(_true)) +read_bool(_not(_false)) + +print("AND") +read_bool(_and(_true)(_true)) +read_bool(_and(_true)(_false)) +read_bool(_and(_false)(_true)) +read_bool(_and(_false)(_false)) + +print("OR") +read_bool(_or(_true)(_true)) +read_bool(_or(_true)(_false)) +read_bool(_or(_false)(_true)) +read_bool(_or(_false)(_false)) + +print() +print("CHURCH NUMERALS") +print("-------------") +print("ZERO/SUCC") +read_church(_zero) +read_church(_one) +read_church(_succ(_one)) +read_church(_succ(_succ(_one))) + +print("PRED") +read_church(_pred(_one)) +read_church(_pred(_succ(_one))) +read_church(_pred(_succ(_succ(_one)))) -- cgit v1.2.3