free-monad

The Pause monad

僤鯓⒐⒋嵵緔 提交于 2019-11-28 13:27:35
问题 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 ()

Combining Free types

空扰寡人 提交于 2019-11-28 06:27:02
I've been recently teaching myself about the Free monad from the free package, but I've come across a problem with it. I would like to have different free monads for different libraries, essentially I would like to build DSLs for different contexts, but I would also like to be able to combine them together. As an example: {-# LANGUAGE DeriveFunctor #-} module TestingFree where import Control.Monad.Free data BellsF x = Ring x | Chime x deriving (Functor, Show) type Bells = Free BellsF data WhistlesF x = PeaWhistle x | SteamWhistle x deriving (Functor, Show) type Whistles = Free WhistlesF ring :

What monads can be expressed as Free over some functor?

半世苍凉 提交于 2019-11-28 04:42:56
The documentation for Free says: A number of common monads arise as free monads, Given data Empty a , Free Empty is isomorphic to the Identity monad. Free Maybe can be used to model a partiality monad where each layer represents running the computation for a while longer. What other monads are expressible using Free ? I could think only of one more: I believe Free (Const e) is isomorphic to Either e . Edit: What monads are not expressible using Free and why? Philip JF Almost all of them (up to issues involving looping and mfix ) but not Cont . Consider the State monad newtype State s a = State

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

假如想象 提交于 2019-11-27 09:40:49
问题 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,

What monads can be expressed as Free over some functor?

一曲冷凌霜 提交于 2019-11-27 05:25:10
问题 The documentation for Free says: A number of common monads arise as free monads, Given data Empty a , Free Empty is isomorphic to the Identity monad. Free Maybe can be used to model a partiality monad where each layer represents running the computation for a while longer. What other monads are expressible using Free ? I could think only of one more: I believe Free (Const e) is isomorphic to Either e . Edit: What monads are not expressible using Free and why? 回答1: Almost all of them (up to

Combining Free types

被刻印的时光 ゝ 提交于 2019-11-27 01:20:54
问题 I've been recently teaching myself about the Free monad from the free package, but I've come across a problem with it. I would like to have different free monads for different libraries, essentially I would like to build DSLs for different contexts, but I would also like to be able to combine them together. As an example: {-# LANGUAGE DeriveFunctor #-} module TestingFree where import Control.Monad.Free data BellsF x = Ring x | Chime x deriving (Functor, Show) type Bells = Free BellsF data

What are free monads?

一个人想着一个人 提交于 2019-11-26 08:38:56
问题 I\'ve seen the term Free Monad pop up every now and then for some time, but everyone just seems to use/discuss them without giving an explanation of what they are. So: what are free monads? (I\'d say I\'m familiar with monads and the Haskell basics, but have only a very rough knowledge of category theory.) 回答1: Edward Kmett's answer is obviously great. But, it is a bit technical. Here is a perhaps more accessible explanation. Free monads are just a general way of turning functors into monads.