diff options
author | Marvin Borner | 2024-06-07 13:31:29 +0200 |
---|---|---|
committer | Marvin Borner | 2024-06-07 14:37:52 +0200 |
commit | dba4ea41ce332f6945e61f580aee675a50a9b237 (patch) | |
tree | d715dfd711103adec2bb5bfed7e3520c9d7285a4 /std/Monad.bruijn | |
parent | eb21453b1491b602db9ae2076c6caea4f866d7d8 (diff) |
Some more work on monads
Diffstat (limited to 'std/Monad.bruijn')
-rw-r--r-- | std/Monad.bruijn | 49 |
1 files changed, 3 insertions, 46 deletions
diff --git a/std/Monad.bruijn b/std/Monad.bruijn index 0dae29e..0feec73 100644 --- a/std/Monad.bruijn +++ b/std/Monad.bruijn @@ -1,47 +1,4 @@ -# 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 +# MIT License, Copyright (c) 2024 Marvin Borner +# just a reference to the list monad (default interface) -:import std/List . -: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") - -# monadifies a list -lift-m map ⧗ (a → b) → (M a) → (M b) - -# monadifies a list with two monadic arguments -lift-m2 [[[concat ([[4 1 0] <$> 1] <$> 1)]]] ⧗ (a → b → c) → (M a) → (M b) → (M c) - -# evaluates monadic actions -sequence foldr (lift-m2 cons) {}empty ⧗ (List (M a)) → (M (List a)) - ->‣ [sequence ∘∘ 0] - -# traverses list based on modifier -traverse sequence ∘∘ map ⧗ (a → (M b)) → (N a) → (M (N b)) - -# performs action n times -replicate-m >replicate ⧗ Number → (M a) → (M (List a)) - -# maps elements to a monadic action -map-m >map ⧗ (a → (M b)) → (List a) → (M (List b)) +:input std/Monad/List |