aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Monad.bruijn
diff options
context:
space:
mode:
Diffstat (limited to 'std/Monad.bruijn')
-rw-r--r--std/Monad.bruijn27
1 files changed, 27 insertions, 0 deletions
diff --git a/std/Monad.bruijn b/std/Monad.bruijn
new file mode 100644
index 0000000..e015efa
--- /dev/null
+++ b/std/Monad.bruijn
@@ -0,0 +1,27 @@
+# MIT License, Copyright (c) 2023 Marvin Borner
+# monadic interface for anything based on lists (e.g. IO, strings)
+# afaik originally proposed by John Tromp and inspired by Haskell
+
+:import std/Pair .
+:import std/Combinator .
+
+read [0] ⧗ a → (M a)
+
+return [[1 : 0]] ⧗ a → (M a)
+
+pure return ⧗ a → (M a)
+
+# monadic bind operator
+…>>=… [[[2 0 1]]] ⧗ (M a) → (a → (M b)) → (M a)
+
+:test ((read >>= return) "woa") ("woa")
+
+# monadic reverse bind operator
+…=<<… \…>>=… ⧗ (a → (M b)) → (M a) → (M b)
+
+:test ((return =<< read) "woa") ("woa")
+
+# monadic compose operator
+…>>… [[1 >>= [1]]] ⧗ (M a) → (M b) → (M b)
+
+:test ((read >> (return 'a')) "hah") ("aah")