monadfix

Tying the Knot with a State monad

一个人想着一个人 提交于 2019-12-03 02:16:48
问题 I'm working on a Haskell project that involves tying a big knot: I'm parsing a serialized representation of a graph, where each node is at some offset into the file, and may reference another node by its offset. So I need to build up a map from offsets to nodes while parsing, which I can feed back to myself in a do rec block. I have this working, and kinda-sorta-reasonably abstracted into a StateT -esque monad transformer: {-# LANGUAGE DoRec, GeneralizedNewtypeDeriving #-} import qualified

Using Cont to acquire values from the future and the past

旧时模样 提交于 2019-12-02 21:36:00
I'm writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program: data Program m = Instruction (m ()) (Program m) | Control (m (Program m)) | Halt However, it's tricky to parse a textual representation of a brainfuck program into this data type. The problem arises with trying to correctly parse square brackets, because there is some knot-tying to do so that the final Instruction inside a loop links to the loop's Control again. A bit more preliminary information. See this version on the github repo for all the details. type

Tying the Knot with a State monad

徘徊边缘 提交于 2019-12-02 17:16:26
I'm working on a Haskell project that involves tying a big knot: I'm parsing a serialized representation of a graph, where each node is at some offset into the file, and may reference another node by its offset. So I need to build up a map from offsets to nodes while parsing, which I can feed back to myself in a do rec block. I have this working, and kinda-sorta-reasonably abstracted into a StateT -esque monad transformer: {-# LANGUAGE DoRec, GeneralizedNewtypeDeriving #-} import qualified Control.Monad.State as S data Knot s = Knot { past :: s, future :: s } newtype RecStateT s m a =

Is it possible to implement MonadFix for `Free`?

寵の児 提交于 2019-11-30 06:08:24
http://hackage.haskell.org/package/free in Control.Monad.Free.Free allows one to get access to the "free monad" for any given Functor . It does not, however, have a MonadFix instance. Is this because such an instance cannot be written, or was it just left out? If such an instance cannot be written, why not? Consider the description of what mfix does: The fixed point of a monadic computation. mfix f executes the action f only once, with the eventual output fed back as the input. The word "executes", in the context of Free , means creating layers of the Functor . Thus, "only once" means that in

Is a lazy, breadth-first monadic rose tree unfold possible?

天涯浪子 提交于 2019-11-30 02:14:24
Data.Tree includes unfoldTreeM_BF and unfoldForestM_BF functions to construct trees breadth-first using the results of monadic actions. The tree unfolder can be written easily using the forest unfolder, so I'll focus on the latter: unfoldForestM_BF :: Monad m => (b -> m (a, [b])) -> [b] -> m [Tree a] Starting with a list of seeds, it applies a function to each, generating actions that will produce the tree roots and the seeds for the next level of unfolding. The algorithm used is somewhat strict, so using unfoldForestM_BF with the Identity monad is not exactly the same as using the pure

Is a lazy, breadth-first monadic rose tree unfold possible?

孤街醉人 提交于 2019-11-28 23:10:28
问题 Data.Tree includes unfoldTreeM_BF and unfoldForestM_BF functions to construct trees breadth-first using the results of monadic actions. The tree unfolder can be written easily using the forest unfolder, so I'll focus on the latter: unfoldForestM_BF :: Monad m => (b -> m (a, [b])) -> [b] -> m [Tree a] Starting with a list of seeds, it applies a function to each, generating actions that will produce the tree roots and the seeds for the next level of unfolding. The algorithm used is somewhat