free-monad

Structurally enforced Free Alternative, without left distributivity

只谈情不闲聊 提交于 2019-12-03 10:44:44
There is a nice Free Alternative in the great free package, which lifts a Functor to a left-distributive Alternative. That is, the claim is that: runAlt :: Alternative g => (forall x. f x -> g x) -> Alt f a -> g a is an Alternative homomorphism , with liftAlt . And, indeed, it is one, but only for left-distributive Alternative instances. Of course, in reality, very few Alternative instances are actually left-distributive. Most of the alternative instances that actually matter (parsers, MaybeT f for most Monad f, etc.) are not left-distributive. This fact can be shown by an example where runAlt

Navigating and modifying ASTs built on the Free monad in Haskell

半城伤御伤魂 提交于 2019-12-03 09:28:40
问题 I'm attempting to structure an AST using the Free monad based on some helpful literature that I've read online. I have some questions about working with these kinds of ASTs in practice, which I've boiled down to the following example. Suppose my language allows for the following commands: {-# LANGUAGE DeriveFunctor #-} data Command next = DisplayChar Char next | DisplayString String next | Repeat Int (Free Command ()) next | Done deriving (Eq, Show, Functor) and I define the Free monad

MonadFix instance for interpreter monad transformer generated by FreeT?

梦想的初衷 提交于 2019-12-03 07:18:20
I have a simplified version of the standard interpreter monad transformer generated by FreeT : data InteractiveF p r a = Interact p (r -> a) type Interactive p r = FreeT (InteractiveF p r) p is the "prompt", and r is the "environment"...one would run this using something like: runInteractive :: Monad m => (p -> m r) -> Interactive p r m a -> m a runInteractive prompt iact = do ran <- runFreeT iact case ran of Pure x -> return x Free (Interact p f) -> do response <- prompt p runInteractive prompt (f resp) instance MonadFix m => MonadFix (FreeT (InteractiveF p r)) m a) mfix = -- ??? I feel like

How to asign a value from the IO monad to a RankNType qualified constructor

ε祈祈猫儿з 提交于 2019-12-02 13:30:17
问题 (UPDATED) I have made an interface using a Free Monad to a generic data store. I want to place the specific interpreter (:: DataStore a -> IO a) chosen by the user at run time into a state monad along with some other information. I cannot seem to put anything into this field in the data structure. How do I put a value into a field defined as a higher rank type? Below is a minimum example: {-# LANGUAGE RankNTypes, DeriveFunctor #-} data ProgramState = PS { -- line 3 [...] , storageInterface ::

What was wrong with Control.MonadPlus.Free?

亡梦爱人 提交于 2019-12-01 15:46:20
The free MonadPlus defined as data Free f a = Pure a | Free (f (Free f a)) | Plus [Free f a] was removed in free 4.6 with the following remark ( changelog ): Removed Control.MonadPlus.Free . Use FreeT f [] instead and the result will be law-abiding. What was the problem, in particular, what laws didn't hold? dfeuer According to this issue in the bug tracker the old definition does not obey the associative law. Although I know little about such things, I suspect an other problem is redundancy: Pure a Plus [Pure a] Plus [Plus [Pure a]] ... all seem to represent the same thing. Free structures

What was wrong with Control.MonadPlus.Free?

人盡茶涼 提交于 2019-12-01 14:38:33
问题 The free MonadPlus defined as data Free f a = Pure a | Free (f (Free f a)) | Plus [Free f a] was removed in free 4.6 with the following remark (changelog): Removed Control.MonadPlus.Free . Use FreeT f [] instead and the result will be law-abiding. What was the problem, in particular, what laws didn't hold? 回答1: According to this issue in the bug tracker the old definition does not obey the associative law. Although I know little about such things, I suspect an other problem is redundancy:

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

The Pause monad

蓝咒 提交于 2019-11-29 18:43:01
Monads can do many amazing, crazy things. They can create variables which hold a superposition of values. They can allow you to access data from the future before you compute it. They can allow you to write destructive updates, but not really. And then the continuation monad allows you to break people's minds! Ususally your own. ;-) But here's a challenge: Can you make a monad which can be paused ? data Pause s x instance Monad (Pause s) mutate :: (s -> s) -> Pause s () yield :: Pause s () step :: s -> Pause s () -> (s, Maybe (Pause s ())) The Pause monad is a kind of state monad (hence mutate

Applicative vs. monadic combinators and the free monad in Scalaz

南笙酒味 提交于 2019-11-29 12:42:15
问题 A couple of weeks ago Dragisa Krsmanovic asked a question here about how to use the free monad in Scalaz 7 to avoid stack overflows in this situation (I've adapted his code a bit): import scalaz._, Scalaz._ def setS(i: Int): State[List[Int], Unit] = modify(i :: _) val s = (1 to 100000).foldLeft(state[List[Int], Unit](())) { case (st, i) => st.flatMap(_ => setS(i)) } s(Nil) I thought that just lifting a trampoline into StateT should work: import Free.Trampoline val s = (1 to 100000).foldLeft

When would I want to use a Free Monad + Interpreter pattern?

我怕爱的太早我们不能终老 提交于 2019-11-28 16:21:34
I'm working on a project that, amongst other things, involves a database access layer. Pretty normal, really. In a previous project, a collaborator encouraged me to use the Free Monads concept for a database layer and so I did. Now I'm trying to decide in my new project what I gain. In the previous project, I had an API that looked rather like this. saveDocument :: RawDocument -> DBAction () getDocuments :: DocumentFilter -> DBAction [RawDocument] getDocumentStats :: DBAction [(DocId, DocumentStats)] etc. About twenty such public functions. To support them, I had the DBAction data structure: