aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--bruijn.cabal1
-rw-r--r--samples/interpreters/blc.bruijn4
-rw-r--r--samples/io/echo.bruijn5
-rw-r--r--samples/io/reverse.bruijn2
-rw-r--r--std/Monad.bruijn27
5 files changed, 35 insertions, 4 deletions
diff --git a/bruijn.cabal b/bruijn.cabal
index 3717789..b560caa 100644
--- a/bruijn.cabal
+++ b/bruijn.cabal
@@ -25,6 +25,7 @@ data-files:
std/List.bruijn
std/Logic.bruijn
std/Math.bruijn
+ std/Monad.bruijn
std/Number.bruijn
std/Option.bruijn
std/Pair.bruijn
diff --git a/samples/interpreters/blc.bruijn b/samples/interpreters/blc.bruijn
deleted file mode 100644
index f40bd4a..0000000
--- a/samples/interpreters/blc.bruijn
+++ /dev/null
@@ -1,4 +0,0 @@
-# TODO: understand and reverse engineer this mess
-# by Tromp/Jart
-
-main [(([(0 0)] [[[(((0 [[[[(2 [((4 (2 [((1 (2 [[(2 [((0 1) 2)])]])) (3 [(3 [((2 0) (1 0))])]))])) ((0 (1 [(0 1)])) [((3 [(3 [(1 (0 3))])]) 4)]))])]]]]) (2 2)) 1)]]]) [(0 ([(0 0)] [(0 0)]))])]
diff --git a/samples/io/echo.bruijn b/samples/io/echo.bruijn
index 192bab1..b17bda1 100644
--- a/samples/io/echo.bruijn
+++ b/samples/io/echo.bruijn
@@ -1,4 +1,9 @@
# "echo text | bruijn echo.bruijn"
+:import std/Monad .
+
# returning the binary encoded argument ⇒ echo
main [0]
+
+# alternative using monads
+main* read >>= return
diff --git a/samples/io/reverse.bruijn b/samples/io/reverse.bruijn
index 7a3553f..d5a0597 100644
--- a/samples/io/reverse.bruijn
+++ b/samples/io/reverse.bruijn
@@ -1,6 +1,8 @@
# "echo tacocat | bruijn reverse.bruijn"
:import std/List .
+:import std/Combinator .
+:import std/Monad .
# stdin is encoded as binary numbers in a list
# reversing the list reverses the input!
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")