monads

Design of interface abstraction

╄→гoц情女王★ 提交于 2019-12-05 05:03:01
Currently, I try to write a small game program (Skat) as a hobby project. Skat is a trick-taking game were two players play against a single player. As there are different kinds of players (lokal player, network player, computer, etc.), I wanted to abstract the interface to the player. My basic idea is to use a typeclass Player , that defines all kind of things, a player have to do and to know (playing a card, get notified about who won the trick, etc). Then, the whole game is just done by a function playSkat :: (Player a, Player b, Player c) => a -> b -> c -> IO () where a , b and c might be

Is print in Haskell a pure function?

ぐ巨炮叔叔 提交于 2019-12-05 04:16:16
Is print in Haskell a pure function; why or why not? I'm thinking it's not, because it does not always return the same value as pure functions should. danidiaz A value of type IO Int is not really an Int . It's more like a piece of paper which reads "hey Haskell runtime, please produce an Int value in such and such way". The piece of paper is inert and remains the same, even if the Int s eventually produced by the runtime are different. You send the piece of paper to the runtime by assigning it to main . If the IO action never comes in the way of main and instead languishes inside some

Does a Powerset-over-Reader monad exist?

▼魔方 西西 提交于 2019-12-05 03:39:55
The canonical 'Monad instance' for environment sharing plus nondeterminism is as follows (using pseudo-Haskell, since Haskell's Data.Set isn't, of course, monadic): eta :: a -> r -> {a} -- '{a}' means the type of a set of a's eta x = \r -> {x} bind :: (r -> {a}) -> (a -> r -> {b}) -> r -> {b} m `bind` f = \r -> {v | x ∈ m r, v ∈ f x r} Generally, when trying to combine a 'container' monad like Powerset (List, Writer, etc) with a second monad m (here, roughly, Reader), one 'wraps' m around the container monad, as done above. I wonder, then, about the following potential Powerset-over-Reader

What effects are modeled by the stream (infinite list) monad?

帅比萌擦擦* 提交于 2019-12-05 03:25:15
Various instances of monads model different type of effects: for example, Maybe models partiality, List non-determinism, Reader read-only state. I would like to know if there is such an intuitive explanation for the monad instance of the stream data type (or infinite list or co-list), data Stream a = Cons a (Stream a) (see below its monad instance definition). I've stumbled upon the stream monad on a few different occasions and I would like to understand better its uses. data Stream a = Cons a (Stream a) instance Functor Stream where fmap f (Cons x xs) = Cons (f x) (fmap f xs) instance

What can we do with Alternative but cannot do with Monoid?

99封情书 提交于 2019-12-05 02:12:11
I read Why MonadPlus and not Monad + Monoid? and I understand a theoretical difference, but I cannot figure out a practical difference, because for List it looks the same. mappend [1] [2] == [1] <|> [2] Yes. Maybe has different implementations mappend (Just "a") (Just "b") /= (Just "a") <|> (Just "b") But we can implement Maybe Monoid in the same way as Alternative instance Monoid (Maybe a) where Nothing `mappend` m = m m `mappend` _ = m So, can someone show the code example which explains a practical difference between Alternative and Monoid? The question is not a duplicate of Why MonadPlus

Propagation of State Monad

会有一股神秘感。 提交于 2019-12-05 02:09:15
问题 I have the following function for walking around "edges" of the "graph" of my game world. It alters the state of the world--specifically, the player's location. I need to report a message alerting the player of their change in location as well. So I could either return a tuple of (message, newWorld), or I could use a State monad. (Right? I'm new to this stuff.) Here's my attempt at the monad approach: walk dir = do world <- get let attempt = filter (\e -> edgeDirection e == dir) $ edges edges

How to write without Do notation

只愿长相守 提交于 2019-12-05 02:06:35
I was playing around with composable failures and managed to write a function with the signature getPerson :: IO (Maybe Person) where a Person is: data Person = Person String Int deriving Show It works and I've written it in the do-notation as follows: import Control.Applicative getPerson = do name <- getLine -- step 1 age <- getInt -- step 2 return $ Just Person <*> Just name <*> age where getInt :: IO (Maybe Int) getInt = do n <- fmap reads getLine :: IO [(Int,String)] case n of ((x,""):[]) -> return (Just x) _ -> return Nothing I wrote this function with the intent of creating composable

Is there any difference between “MonadIO m” and “MonadBaseControl IO m”?

给你一囗甜甜゛ 提交于 2019-12-05 01:58:44
Function runTCPClient from network-conduit has the following signature: runTCPClient :: (MonadIO m, MonadBaseControl IO m) => ClientSettings m -> Application m -> m () MonadIO m provides liftIO :: IO a -> m a and MonadBaseControl IO m provides liftBase :: IO a -> m a There is no visible difference. Do they provide the same functionality? If yes, why the duplication in the type signature? If not, what's the difference? liftBase is part of MonadBase which is a generalization of MonadIO for any base monad and, as you said, MonadBase IO provides the same functionality as MonadIO . However,

Monad instance of a number-parameterised vector?

假如想象 提交于 2019-12-05 01:44:18
问题 Statically sized vectors in Haskell are shown in Oleg Kiselyov's Number-parameterized types and can also be found in the Data.Param.FSVec type from the parameterized-data module on Hackage: data Nat s => FSVec s a FSVec is not an instance of the Monad type class. The monad instance for lists, can be used to remove or duplicate elements: Prelude> [1,2,3] >>= \i -> case i of 1 -> [1,1]; 2 -> []; _ -> [i] [1,1,3] Whether similar to the list version or not, is it possible to construct a monad

Does Writer Monad guarantee right associative concatenation?

落花浮王杯 提交于 2019-12-05 01:40:26
It was claimed in Validations in Haskell that use of a Writer guarantees right-associative concatenation. However, this example seems to show otherwise. What's the correct answer? {-# LANGUAGE OverloadedStrings #-} import Control.Monad.Writer import Data.String data TM = TMempty | TMappend TM TM | TMfromString String instance IsString TM where fromString = TMfromString instance Monoid TM where mempty = TMempty mappend = TMappend instance Show TM where showsPrec d TMempty = showString "\"\"" showsPrec d (TMfromString s) = showString $ show s showsPrec d (TMappend a b) = showParen (d > 0) $