aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--samples/fun/rng-state.bruijn11
-rw-r--r--std/Generic/Monad.bruijn4
-rw-r--r--std/Monad/Parser.bruijn6
-rw-r--r--std/Monad/State.bruijn5
-rw-r--r--std/Result.bruijn9
5 files changed, 17 insertions, 18 deletions
diff --git a/samples/fun/rng-state.bruijn b/samples/fun/rng-state.bruijn
index 795bad1..49d77bc 100644
--- a/samples/fun/rng-state.bruijn
+++ b/samples/fun/rng-state.bruijn
@@ -14,14 +14,9 @@ rand [[[0 1 1]] rng]
rand-bool map even? rand
-# accumulating bind (reversed)
-triple1 rand >>= (rand >>= (rand >>= [[[[[0 3 [0 3 [0 3 [[0]]]]]]]]]))
-
-:test ((triple1 (+50) [[1]]) =? (+745)) ([[1]])
-
# normal bind
-triple2 rand >>= [rand >>= [rand >>= [[[0 4 [0 4 [0 4 [[0]]]]]]]]]
+triple rand >>= [rand >>= [rand >>= [pure [0 1 2 3]]]]
-:test ((triple2 (+50) [[1]]) =? (+595)) ([[1]])
+:test ((triple (+50) [[1]]) =? (+595)) ([[[2]]])
-main string→number → triple2
+main string→number → triple
diff --git a/std/Generic/Monad.bruijn b/std/Generic/Monad.bruijn
index 9e6c5d3..5373822 100644
--- a/std/Generic/Monad.bruijn
+++ b/std/Generic/Monad.bruijn
@@ -17,6 +17,10 @@ liftA3 [[[[(liftA2 3 2 1) <*> 0]]]] ⧗ (a → b → c → d) → (M a) → (M b
…<*… liftA2 k ⧗ (M a) → (M b) → (M a)
+…=<<… \bind
+
+…>>… [[1 >>= [1]]] ⧗ (M a) → (M b) → (M b)
+
# TODO: traverse, sequence
# in parser: string = traverse char!
diff --git a/std/Monad/Parser.bruijn b/std/Monad/Parser.bruijn
index 0364ea0..12c058f 100644
--- a/std/Monad/Parser.bruijn
+++ b/std/Monad/Parser.bruijn
@@ -38,7 +38,7 @@ fail [[R.err 1]] ⧗ a → (Parser a)
pure [[R.ok (1 : 0)]] ⧗ a → (Parser a)
-ap [[[R.apply (2 0) ok]]] ⧗ (Parser (a → b)) → (Parser a) → (Parser b)
+ap [[[R.bind (2 0) ok]]] ⧗ (Parser (a → b)) → (Parser a) → (Parser b)
ok &[[R.map ok (3 0)]]
ok &[[(3 1) : 0]]
@@ -52,14 +52,14 @@ string y [[0 [[[go]]] (pure [[0]])]] ⧗ String → (Parser a)
return pure ⧗ a → (Parser a)
-bind [[[R.apply (2 0) ok]]] ⧗ (Parser a) → (a → (Parser b)) → (Parser a)
+bind [[[R.bind (2 0) ok]]] ⧗ (Parser a) → (a → (Parser b)) → (Parser a)
ok &[[3 1 0]]
…>>=… bind
alt [[[2 0 R.ok err]]] ⧗ (Parser a) → (Parser a) → (Parser a)
err [2 1 R.ok err]
- err [R.err (error-compose 1 0)]
+ err R.err ∘ (error-compose 0)
…<|>… alt
diff --git a/std/Monad/State.bruijn b/std/Monad/State.bruijn
index 8a7ef25..6cf6019 100644
--- a/std/Monad/State.bruijn
+++ b/std/Monad/State.bruijn
@@ -11,3 +11,8 @@ map [[[1 0 [[[0 2 (5 1)]]]]]] ⧗ (a → b) → (State s a) → (State s b)
bind [[[2 0 [[3 0 1]]]]] ⧗ (State s a) → (a → (State s b)) → (State s b)
…>>=… bind
+
+pure [[[0 2 1]]] ⧗ a → (State s a)
+
+:test ((w' ∘ c) >>= [(w' ∘ c) >>= [pure 0]] [[0]]) (w' [[0]])
+:test ((w' ∘ c) >>= [(w' ∘ c) >>= [pure 0]] [[1]]) (w' [[1]])
diff --git a/std/Result.bruijn b/std/Result.bruijn
index 51eabdc..f7e93f0 100644
--- a/std/Result.bruijn
+++ b/std/Result.bruijn
@@ -57,15 +57,10 @@ map-err [[0 ok [err (2 0)]]] ⧗ (a → b) → (Result a) → (Result b)
:test (map-err [[1]] (ok [[0]])) ((ok [[0]]))
:test (map-err [[1]] (err [[0]])) ((err [[[0]]]))
-# applies encapsulated value to given function (if ok)
-apply [[1 0 err]] ⧗ (Result a) → (a → b) → (Result b)
-
-:test (apply (err [[0]]) [ok ([[1]] 0)]) (err [[0]])
-:test (apply (ok [[0]]) [ok ([[1]] 0)]) (ok [[[0]]])
-
pure ok ⧗ a → (Result a)
-bind [[1 0 err]] ⧗ (Result a) → (a → (Result b)) → (Result a)
+# applies encapsulated value to given function (if ok)
+bind [[1 0 err]] ⧗ (Result a) → (a → (Result b)) → (Result b)
…>>=… bind