state-monad

How do I use a persistent State monad with Spock?

做~自己de王妃 提交于 2019-12-11 09:15:04
问题 I'm just starting out with haskell and I'm having issues with a basic "echo" REST server. Spock looked like a nice starting place for a REST server, and I though I got the basics of the State monad, but I'm having issues understanding how to put a runState around the spock code. Here's the code I've got so far. {-# LANGUAGE OverloadedStrings #-} module Main where import Data.Monoid import Web.Spock.Safe import qualified Control.Monad.State as S storeData :: String -> S.State String String

Monad transformers: Implementation of a stack machine with MaybeT (State Stack)

。_饼干妹妹 提交于 2019-12-11 06:01:43
问题 I'm trying to implement a Maybe-State monad transformer and use it to implement a simple stack machine. The definitions of state monad and maybe should be correct. Now I'm trying to implement pop: pop :: MaybeT (State Stack) Int So that if the stack is empty it returns nothing, otherwise it returns Just <popped stack> . This is what I have so far: pop :: MaybeT (State Stack) Int pop = guard True (do (r:rs) <- get put rs return r) (Obviously True is just a dummy placeholder - I'll implement

Lift through nested state transformers (mtl)

£可爱£侵袭症+ 提交于 2019-12-11 02:31:33
问题 So I'm working on an extensible application framework, and a key part of the framework is to be able to run state monads over many different state types; I've got it set up, and can run the nested state monads; however a key feature I need is for monads over nested states to be able to run actions over the global state as well; I managed to rig this up using some complicated Free Monads in an earlier project, but now I'm using mtl and I'm a bit stuck. Here's some context: newtype App a = App

How to derive a state monad from first principles?

十年热恋 提交于 2019-12-11 02:23:07
问题 I am trying to come up with an implementation of State Monad derived from examples of function composition. Here I what I came up with: First deriving the concept of Monad: data Maybe' a = Nothing' | Just' a deriving Show sqrt' :: (Floating a, Ord a) => a -> Maybe' a sqrt' x = if x < 0 then Nothing' else Just' (sqrt x) inv' :: (Floating a, Ord a) => a -> Maybe' a inv' x = if x == 0 then Nothing' else Just' (1/x) log' :: (Floating a, Ord a) => a -> Maybe' a log' x = if x == 0 then Nothing'

“instance Show State where” doesn't compile

三世轮回 提交于 2019-12-11 00:53:57
问题 This is the State Monad code I am trying to figure out data State a = State (Int -> (a, Int)) instance Monad State where return x = State (\c -> (x, c)) State m >>= f = State (\c -> case m c of { (a, acount) -> case f a of State b -> b acount}) getState = State (\c -> (c, c)) putState count = State (\_ -> ((), count)) instance Show State where -- doesn't work show (State a) = show a -- doesn't work I am trying to make State as instance of Show so that I can see the action of getState and

Separating State for a Model and GUI IO ( Wx) : Stack or FRP?

折月煮酒 提交于 2019-12-11 00:35:04
问题 For my diagramming tool, I'd like to keep the code of the core model isolated from the GUI. In the following example, the "state " is passed around with vDiag , which is a Tvar . This is a design decision in wx. Now, For my diagramming tool, I 'd like the core model to be "stored" in a fgl Graph, (with complex types in it), and wx will be given only a view on it; say in this example, a list of points for read access when painting, and some functions to write when clicking, dragging, etc.. . I

Lifting a value in the State monad in Haskell

大城市里の小女人 提交于 2019-12-10 20:33:41
问题 I am writing a Sudoku generator/solver in Haskell as a learning exercise. My solve function takes in a UArray but returns a State Int (UArray ...) so that it can also return the maximum difficulty level that it found while solving. This is my function so far (still in the very experimental early stage): import Control.Monad.State (State, put) import Control.Monad.Trans.Class (lift) import Data.Array.MArray (thaw) import Data.Array.ST (runSTUArray) import Data.Array.Unboxed (UArray) -- ...

Haskell Monad State Example

北慕城南 提交于 2019-12-10 19:54:20
问题 I'm experimenting with Haskell's Control.Monad.State by trying to iterate through a list of either strings or integers, counting them, and replacing string entries with the integer 0 . I have managed to do the counting part, but failed in creating the replaced list. Here is my code which correctly prints [3,6] to the screen. How can I make it create the desired list [6,0,3,8,0,2,9,1,0] ? module Main( main ) where import Control.Monad.State l = [ Right 6, Left "AAA", Right 3, Right 8, Left

Stateful loop with different types of breaks

你说的曾经没有我的故事 提交于 2019-12-10 15:08:56
问题 I am trying to convert the following stateful imperative code into Haskell. while (true) { while (get()) { if (put1()) { failImmediately(); } } if (put2()) { succeedImmediately(); } } Both the put1 and put2 read a state of the system and modify it. get can for simplicity just read the state. failImmediately should break out of the endless-loop and present one type of result, succeedImmediately should also break out but present a different result. What I tried to use was State Env Result where

How can I combine the CheckingFuelMonad with a State monad in Hoopl?

故事扮演 提交于 2019-12-10 13:00:00
问题 I am using the Hoopl library and would like to carry some state around while rewriting. The rewrite functions are polymorphic regarding the monad used, but I cannot figure out how to combine a State monad with one of the library's Fuel monads. Below is a minimal example. MyMonad is a synonym combining Hoopl's CheckingFuelMonad and a State monad carrying a flag. Stmt is just a placeholder for my intermediate language and isn't really important. {-# LANGUAGE GADTs, RankNTypes #-} import