monads

Scala and cats: Implicit conversion to identity monad

ぐ巨炮叔叔 提交于 2019-12-07 08:10:22
问题 I have a function that computes sum of squares for numeric types as shown below. import cats.syntax.functor._ import cats.syntax.applicative._ import cats.{Id, Monad} import scala.language.higherKinds object PowerOfMonads { /** * Ultimate sum of squares method * * @param x First value in context * @param y Second value in context * @tparam F Monadic context * @tparam T Type parameter in the Monad * @return Sum of squares of first and second values in the Monadic context */ def sumOfSquares[F[

ApplicativeDo in Haskell

两盒软妹~` 提交于 2019-12-07 07:23:45
问题 AFAIK one of the new additions to GHC8 is the ApplicativeDo language extension, which desugars the do-notation to the corresponding Applicative methods ( <$> , <*> ) if possible. I have the following questions. How does it decide whether the desugaring to Applicative methods is possible? From what I know, it does a dependency check (if the later depends on the result of former) to decide the eligibility. Are there any other criteria? Although this addition makes applicative code easier to

Help me understand this Scala code: scalaz IO Monad

送分小仙女□ 提交于 2019-12-07 07:13:17
问题 Here's the code I'm trying to understand (it's from http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/): object io { sealed trait IO[A] { def unsafePerformIO: A } object IO { def apply[A](a: => A): IO[A] = new IO[A] { def unsafePerformIO = a } } implicit val IOMonad = new Monad[IO] { def pure[A](a: => A): IO[A] = IO(a) def bind[A,B](a: IO[A], f: A => IO[B]): IO[B] = IO { implicitly[Monad[Function0]].bind(() => a.unsafePerformIO, (x:A) => () => f(x)

Relationship between fmap and bind

梦想与她 提交于 2019-12-07 06:10:45
问题 After looking up the Control.Monad documentation, I'm confused about this passage: The above laws imply: fmap f xs = xs >>= return . f How do they imply that? 回答1: Control.Applicative says As a consequence of these laws, the Functor instance for f will satisfy fmap f x = pure f <*> x The relationship between Applicative and Monad says pure = return (<*>) = ap ap says return f `ap` x1 `ap` ... `ap` xn is equivalent to liftMn f x1 x2 ... xn Therefore fmap f x = pure f <*> x = return f `ap` x =

`mfix` not working as expected

送分小仙女□ 提交于 2019-12-07 05:34:41
问题 I expected the following code to first prompt start: , then wait for a user response, before echoing the previous user response back, and awaiting a new one: import System.IO (hFlush, stdout) import Control.Monad.Fix (mfix) f :: [String] -> IO [String] f = mapM $ \x -> putStr x >> putStr ": " >> hFlush stdout >> getLine g x = f ("start":x) main = mfix g But it gives the error thread blocked indefinitely in an MVar operation after the first line is entered. Why is this and how can I fix it

Multiple flatMap methods for a single monad?

谁都会走 提交于 2019-12-07 03:29:10
问题 Does it make sense to define multiple flatMap (or >>= / bind in Haskell) methods in a Monad? The very few monads I actually use ( Option , Try , Either projections) only define one flatMap method. For exemple, could it make sense to define a flatMap method on Option which would take a function producing a Try ? So that Option[Try[User]] would be flattened as Option[User] for exemple? (Considering loosing the exception is not a problem ...) Or a monad should just define one flatMap method,

What is this thing similar to KleisliFunctor?

一个人想着一个人 提交于 2019-12-07 03:22:33
问题 Here is how we can define KleisliFunctor: class (Monad m, Functor f) => KleisliFunctor m f where kmap :: (a -> m b) -> f a -> f b kmap f = kjoin . fmap f kjoin :: f (m a) -> f a kjoin = kmap id Does this type class class (Functor f, Monad m) => Absorb f m where (>>~) :: f a -> (a -> m b) -> m b a >>~ f = ajoin $ fmap f a ajoin :: f (m a) -> m a ajoin a = a >>~ id fit somewhere into category theory? What are the laws? Are they a >>~ g . f === fmap f a >>~ g a >>~ (f >=> g) === a >>~ f >>= g ?

Adjoint functors determine monad transformers, but where's lift?

只愿长相守 提交于 2019-12-07 02:46:54
问题 I'm intrigued by the construction described here for determining a monad transformer from adjoint functors. Here's some code that summarizes the basic idea: {-# LANGUAGE MultiParamTypeClasses #-} import Control.Monad newtype Three g f m a = Three { getThree :: g (m (f a)) } class (Functor f, Functor g) => Adjoint f g where counit :: f (g a) -> a unit :: a -> g (f a) instance (Adjoint f g, Monad m) => Monad (Three g f m) where return = Three . fmap return . unit m >>= f = Three $ fmap (>>=

Collecting IO outputs into list

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 01:34:08
问题 How can I issue multiple calls to SDL.pollEvent :: IO Event until the output is SDL.NoEvent and collect all the results into a list? In imperative terms something like this: events = [] event = SDL.pollEvent while ( event != SDL.NoEvent ) { events.add( event ) event = SDL.pollEvent } 回答1: James Cook was so kind to extend monad-loops with this function: unfoldWhileM :: Monad m => (a -> Bool) -> m a -> m [a] used with SDL: events <- unfoldWhileM (/= SDL.NoEvent) SDL.pollEvent 回答2: You could use

Design of interface abstraction

亡梦爱人 提交于 2019-12-07 01:27:24
问题 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