monads

“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

Testing monadic laws using QuickCheck

醉酒当歌 提交于 2019-12-10 21:56:25
问题 Is there a library or tools for testing the laws of a custom monad? My current hacked attempt goes something like this: Define Arbitrary1 , similar to Eq1 , Show1 etc. Define a helper type that wraps Arbitrary1 as Arbitrary . Define a test (for example) for monadic laws. Is any of this already implemented somewhere? {-# LANGUAGE RankNTypes, ScopedTypeVariables #-} import Data.Functor.Classes import Data.Proxy import Test.QuickCheck import Test.QuickCheck.Function import Test.QuickCheck.Poly

Lift a function and its argument to a different monadic context

旧巷老猫 提交于 2019-12-10 20:26:37
问题 I am not sure how to formulate this question scientifically exact, so I am just going to show you an example. I am using state in a StateT transformer. Underlying is IO . Inside the StateT IO operation I need to use alloca . However, I can't lift alloca to StateT IO because it expects an argument of type (Ptr a -> IO a) while I require it to work with an argument of (Ptr a -> StateT IO MyState a) . (However, this is a generic question about monad transformers rather than specific to IO ,

Using Data.Map in monadic context

让人想犯罪 __ 提交于 2019-12-10 19:42:26
问题 A map that I am operating on has monadic keys (of type IO Double ). I need to use findMax on this map. Can I use liftM for this? Map.findMax $ Map.fromList [(f x, "X"), (f y, "Y"), (f z, "Z")] Here f x has type IO Double . 回答1: It doesn't really make sense to have IO -typed values as keys in a map. A value of type IO t for some type t can be thought of as a "program" that produces a value of type t each time it is run: you can run it multiple times and every time it may produce a different

Question about applicative and nested Maybe

孤者浪人 提交于 2019-12-10 18:57:43
问题 I wrote this function: appFunc :: Integer -> Integer -> Bool -> Maybe (Integer,Integer) appFunc i1 i2 b = if b then Just (i1,i2) else Nothing And then I use it as such in GHCi: > appFunc <$> Just 3 <*> Nothing <*> Just True Nothing Which is great because if at least one of the parameters is Nothing then the whole expression evaluates to Nothing . However, when all parameters are Just then I get a nested Maybe : > appFunc <$> Just 3 <*> Just 1 <*> Just False Just Nothing Ideally, I would like

Operator on do indentation

耗尽温柔 提交于 2019-12-10 18:24:28
问题 hindent changed my code to: do download i inputFile onException (callProcess (List.head args) (List.tail args)) (removeFileIfExists name) `finally` removeFileIfExists inputFile I can't determine if the finally applies to the rest of the do block, or just the state beginning onException . According to this, If you see something unexpected in a list, like where, insert a closing brace before instead of a semicolon. I'm unsure if that rule is applying here. Does the `finally` apply to the rest

Is there a way to unwrap a type from an IO monad?

蹲街弑〆低调 提交于 2019-12-10 18:09:02
问题 I have this very simple function import qualified Data.ByteString.Lazy as B getJson :: IO B.ByteString getJson = B.readFile jsonFile readJFile :: IO (Maybe Response) readJFile = parsing >>= (\d -> case d of Left err -> return Nothing Right ps -> return (Just ps)) where parsing = fmap eitherDecode getJson :: IO (Either String Response) where jsonFile is a path to a file on my harddrive (pardon the lack of do-notation, but I found this more clear to work with) my question is; is there a way for

Stack space overflow with the ST monad

假装没事ソ 提交于 2019-12-10 18:05:00
问题 The following short Haskell program is intended to count a list of items from a file. The version using foldl' works fine, but the version using the ST Monad gives a stack space overflow message. Clearly there's a space leak of some sort here, but I haven't been able to solve it. The really interesting part is that the ST monad is supposed to be doing in-place updates and shouldn't be letting resources grow like this, although that may only pertain to main memory and not stack space. Could

Could this do-monad be replaced by a let block?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 17:56:33
问题 The author here provides the following example usage of a do-monad to combine test generators: (require '[clojure.test.check.generators :as gen]) (require '[clojure.algo.monads :as m]) (m/defmonad gen-m [m-bind gen/bind m-result gen/return]) (def vector-and-elem (m/domonad gen-m [n (gen/choose 1 10) v (gen/vector gen/int n) e (gen/element v)] [v, e])) (gen/sample vector-and-elem) ([[0 -1 1 0 -1 0 -1 1] 0] [[1 1 3 3 3 -1 0 -2 2] 3] [[8 4] 8]... There commentator here asserts that this is a