diff options
author | Marvin Borner | 2024-11-27 15:16:11 +0100 |
---|---|---|
committer | Marvin Borner | 2024-11-27 15:16:11 +0100 |
commit | 4b38d1af08288e80a29c84fe02a40ac925331f21 (patch) | |
tree | ba2e959966c1fb4ccb521cf41aaeb7fe2005d775 /std | |
parent | 96c3527d87766f3fb653c31306eb78317dcbc1cf (diff) |
Monad improvements
Diffstat (limited to 'std')
-rw-r--r-- | std/Generic/Monad.bruijn | 4 | ||||
-rw-r--r-- | std/Monad/Parser.bruijn | 6 | ||||
-rw-r--r-- | std/Monad/State.bruijn | 5 | ||||
-rw-r--r-- | std/Result.bruijn | 9 |
4 files changed, 14 insertions, 10 deletions
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 |