Skip to content

Commit

Permalink
Monad improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinborner committed Nov 27, 2024
1 parent 96c3527 commit 4b38d1a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
11 changes: 3 additions & 8 deletions samples/fun/rng-state.bruijn
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions std/Generic/Monad.bruijn
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
6 changes: 3 additions & 3 deletions std/Monad/Parser.bruijn
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions std/Monad/State.bruijn
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
9 changes: 2 additions & 7 deletions std/Result.bruijn
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4b38d1a

Please sign in to comment.