diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/abstract/monad.lux | 183 |
1 files changed, 0 insertions, 183 deletions
diff --git a/stdlib/source/lux/abstract/monad.lux b/stdlib/source/lux/abstract/monad.lux deleted file mode 100644 index d32bdacbb..000000000 --- a/stdlib/source/lux/abstract/monad.lux +++ /dev/null @@ -1,183 +0,0 @@ -(.module: - [lux #* - [meta - ["." location]]] - [// - [functor (#+ Functor)]]) - -(def: (list\fold f init xs) - (All [a b] - (-> (-> b a a) a (List b) a)) - (case xs - #.Nil - init - - (#.Cons x xs') - (list\fold f (f x init) xs'))) - -(def: (list\size xs) - (All [a] (-> (List a) Nat)) - (loop [counter 0 - xs xs] - (case xs - #.Nil - counter - - (#.Cons _ xs') - (recur (inc counter) xs')))) - -(def: (reverse xs) - (All [a] - (-> (List a) (List a))) - (list\fold (function (_ head tail) (#.Cons head tail)) - #.Nil - xs)) - -(def: (as_pairs xs) - (All [a] (-> (List a) (List [a a]))) - (case xs - (#.Cons x1 (#.Cons x2 xs')) - (#.Cons [x1 x2] (as_pairs xs')) - - _ - #.Nil)) - -(interface: #export (Monad m) - (: (Functor m) - &functor) - (: (All [a] - (-> a (m a))) - wrap) - (: (All [a] - (-> (m (m a)) (m a))) - join)) - -(macro: #export (do tokens state) - {#.doc (doc "Macro for easy concatenation of monadic operations." - (do monad - [y (f1 x) - z (f2 z)] - (wrap (f3 z))))} - (case (: (Maybe [(Maybe Text) Code (List Code) Code]) - (case tokens - (^ (list [_ (#.Record (list [[_ (#.Identifier ["" name])] monad]))] [_ (#.Tuple bindings)] body)) - (#.Some [(#.Some name) monad bindings body]) - - (^ (list monad [_ (#.Tuple bindings)] body)) - (#.Some [#.None monad bindings body]) - - _ - #.None)) - (#.Some [?name monad bindings body]) - (if (|> bindings list\size .int ("lux i64 %" +2) ("lux i64 =" +0)) - (let [[module short] (name_of ..do) - gensym (: (-> Text Code) - (|>> ($_ "lux text concat" module " " short " ") [""] #.Identifier [location.dummy])) - g!_ (gensym "_") - g!map (gensym "map") - g!join (gensym "join") - body' (list\fold (: (-> [Code Code] Code Code) - (function (_ binding body') - (let [[var value] binding] - (case var - [_ (#.Tag ["" "let"])] - (` (let (~ value) (~ body'))) - - _ - (` (|> (~ value) ((~ g!map) (function ((~ g!_) (~ var)) (~ body'))) (~ g!join))) - )))) - body - (reverse (as_pairs bindings)))] - (#.Right [state (list (case ?name - (#.Some name) - (let [name [location.dummy (#.Identifier ["" name])]] - (` ({(~ name) - ({[(~ g!map) (~' wrap) (~ g!join)] - (~ body')} - (~ name))} - (~ monad)))) - - #.None - (` ({[(~ g!map) (~' wrap) (~ g!join)] - (~ body')} - (~ monad)))))])) - (#.Left "'do' bindings must have an even number of parts.")) - - #.None - (#.Left "Wrong syntax for 'do'"))) - -(def: #export (bind monad f) - (All [! a b] - (-> (Monad !) (-> a (! b)) - (-> (! a) (! b)))) - (|>> (\ monad map f) - (\ monad join))) - -(def: #export (seq monad) - {#.doc "Run all the monadic values in the list and produce a list of the base values."} - (All [M a] - (-> (Monad M) (List (M a)) - (M (List a)))) - (let [(^open "!\.") monad] - (function (recur xs) - (case xs - #.Nil - (!\wrap #.Nil) - - (#.Cons x xs') - (|> x - (!\map (function (_ _x) - (!\map (|>> (#.Cons _x)) (recur xs')))) - !\join))))) - -(def: #export (map monad f) - {#.doc "Apply a monadic function to all values in a list."} - (All [M a b] - (-> (Monad M) (-> a (M b)) (List a) - (M (List b)))) - (let [(^open "!\.") monad] - (function (recur xs) - (case xs - #.Nil - (!\wrap #.Nil) - - (#.Cons x xs') - (|> (f x) - (!\map (function (_ _x) - (!\map (|>> (#.Cons _x)) (recur xs')))) - !\join))))) - -(def: #export (filter monad f) - {#.doc "Filter the values in a list with a monadic function."} - (All [! a b] - (-> (Monad !) (-> a (! Bit)) (List a) - (! (List a)))) - (let [(^open "!\.") monad] - (function (recur xs) - (case xs - #.Nil - (!\wrap #.Nil) - - (#.Cons head xs') - (|> (f head) - (!\map (function (_ verdict) - (!\map (function (_ tail) - (if verdict - (#.Cons head tail) - tail)) - (recur xs')))) - !\join))))) - -(def: #export (fold monad f init xs) - {#.doc "Fold a list with a monadic function."} - (All [M a b] - (-> (Monad M) (-> b a (M a)) a (List b) - (M a))) - (case xs - #.Nil - (\ monad wrap init) - - (#.Cons x xs') - (do monad - [init' (f x init)] - (fold monad f init' xs')))) |