aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlost2025-01-31 23:44:46 +0800
committerlost2025-01-31 23:44:46 +0800
commit8d29de102164f2d2808b9efda65e5956146ed766 (patch)
tree405e208342da91c910e8742e817c12b82cdf228d
parentbf0fa34b9e41e579ad524792af1d305177ba830d (diff)
add elixir impl
-rw-r--r--languages/e/elixir/README.md13
-rwxr-xr-xlanguages/e/elixir/lambda-core.exs94
2 files changed, 107 insertions, 0 deletions
diff --git a/languages/e/elixir/README.md b/languages/e/elixir/README.md
new file mode 100644
index 0000000..04f786e
--- /dev/null
+++ b/languages/e/elixir/README.md
@@ -0,0 +1,13 @@
+# Elixir installation
+
+https://elixir-lang.org/install.html
+
+# Run code
+
+```sh
+chmod +x ./lambda-core.exs
+
+./lambda-core.exs
+```
+
+
diff --git a/languages/e/elixir/lambda-core.exs b/languages/e/elixir/lambda-core.exs
new file mode 100755
index 0000000..2db8051
--- /dev/null
+++ b/languages/e/elixir/lambda-core.exs
@@ -0,0 +1,94 @@
+#!/usr/bin/env elixir
+
+###########
+# LOGIC #
+###########
+
+_true = fn x -> fn y -> x end end
+_false = fn x -> fn y -> y end end
+
+_not = fn b -> b.(_false).(_true) end
+_and = fn b1 -> fn b2 -> b1.(b2).(_false) end end
+_or = fn b1 -> fn b2 -> b1.(_true).(b2) end end
+
+#####################
+# CHURCH NUMERALS #
+#####################
+
+_zero = fn f -> fn x -> x end end
+_succ = fn n -> fn f -> fn x -> f.(n.(f).(x)) end end end
+
+_pred = fn n ->
+ fn f ->
+ fn x ->
+ n.(fn g -> fn h -> h.(g.(f)) end end).(fn u -> x end).(fn a -> a end)
+ end
+ end
+end
+
+_one = _succ.(_zero)
+
+########################################
+# HELPERS - not pure lambda calculus #
+########################################
+
+read_bool = fn b -> IO.puts(b.("t").("f")) end
+read_church = fn n -> IO.puts(n.(fn x -> x + 1 end).(0)) end
+
+##############
+# EXAMPLES #
+##############
+
+IO.puts("LOGIC")
+IO.puts("--------------")
+IO.puts("TRUE/FALSE")
+# t
+read_bool.(_true)
+# f
+read_bool.(_false)
+
+IO.puts("NOT")
+# f
+read_bool.(_not.(_true))
+# t
+read_bool.(_not.(_false))
+
+IO.puts("AND")
+# t
+read_bool.(_and.(_true).(_true))
+# f
+read_bool.(_and.(_true).(_false))
+# f
+read_bool.(_and.(_false).(_true))
+# f
+read_bool.(_and.(_false).(_false))
+
+IO.puts("OR")
+# t
+read_bool.(_or.(_true).(_true))
+# t
+read_bool.(_or.(_true).(_false))
+# t
+read_bool.(_or.(_false).(_true))
+# f
+read_bool.(_or.(_false).(_false))
+
+IO.puts("\nCHURCH NUMERALS")
+IO.puts("--------------")
+IO.puts("ZERO/SUCC")
+# 0
+read_church.(_zero)
+# 1
+read_church.(_succ.(_zero))
+# 2
+read_church.(_succ.(_succ.(_zero)))
+# 3
+read_church.(_succ.(_succ.(_succ.(_zero))))
+
+IO.puts("PRED")
+# 0
+read_church.(_pred.(_one))
+# 1
+read_church.(_pred.(_succ.(_one)))
+# 2
+read_church.(_pred.(_succ.(_succ.(_one))))