monads

Understanding of Parallel typeclass from Cats

跟風遠走 提交于 2019-12-06 11:32:57
There is a typeclass called Parallel in Cats . The purpose of this class is to provide parallel computations for some monads that don't support parallel computations out of the box like Either for example. I know that Monad is used for dependent computations and thus requires sequential execution. Applicative is used for independent computations, so such computations can be parallelized. It's also known that each Monad is Applicative (monad is applicative functor). So now I can't put together all concepts from the theory. If all monads are also applicatives, why I can't use applicative nature

Type-level Shapeless : aggregating a HList type elements

好久不见. 提交于 2019-12-06 08:03:01
问题 I want to fold a HList with this Monad but at the type-level trait TypeMonad{ type Append[A,B] = A with B type Identity = Any } Hence, the HList : "A :: B:: C :: HNil " would give the type " A with B with C with Any" Quite easy to do if i had implemented HList : sealed trait HList { type Fuse } final trait HCons[H,L<:HList] extends HList { type Fuse = H with L#Fuse } final trait HNil extends Hlist { type Fuse = Any } However I don't know how to have this functionality in the shapeless

python: mutating `globals` to dynamically put things in scope

泪湿孤枕 提交于 2019-12-06 07:17:04
问题 how terrible an idea is this? class monad implements the with interface to put things in and out of scope, so i can write a library of generic functions like m_chain who refer to functions unit and bind who can have an implementation put in at runtime. (It doesn't matter what all this code does or if it's a good idea.) other ideas i tried all revolved around passing around a structure containing unit/bind as an argument or a kwarg, or putting m_chain in a class, implement it in terms of self

How to understand the state type in Haskell's ST monad

帅比萌擦擦* 提交于 2019-12-06 06:04:50
问题 Jones and Launchbury described in their paper "Lazy Functional State Threads" the ST monad. To ensure that mutable variables can't be used outside the context (or "thread") they have been created in, they use special types, including one of higher rank. Here four important examples: newVar :: ∀ s a. a -> ST s (MutVar s a) readVar :: ∀ s a. MutVar s a -> ST s a writeVar :: ∀ s a. MutVar s a -> a -> ST s () runST :: ∀ a. (∀ s. ST s a) -> a In order to get the idea behind that construction I

Why is it bind's argument's responsibility to unit its value?

空扰寡人 提交于 2019-12-06 05:53:21
The typical monad bind function has the following signature: m a -> (a -> m b) -> m b As I understand it (and I might well be wrong,) the function (a -> m b) is just a mapping function from one structure a to another b . Assuming that is correct begs the question why bind 's signature is not simply: m a -> (a -> b) -> m b Given that unit is part of a monad's definition; why give the function (a -> m b) the responsibility to call unit on whatever value b it produced – wouldn't it be more sensible to make it part of bind ? A function like m a -> (a -> b) -> m b would be equivalent to fmap :: (a

How do you save a tree data structure to binary file in Haskell

给你一囗甜甜゛ 提交于 2019-12-06 05:31:37
问题 I'm trying to save a simple (but quite big) Tree structure into a binary file using Haskell. The structure looks something like this: -- For simplicity assume each Node has only 4 childs data Tree = Node [Tree] | Leaf [Int] And here is how I need the data look on disk: Each node starts with four 32-bit offsets to it's children, then follow the childs. I don't care much about the leafs, let's say it's just n consecutive 32-bit numbers. For practival purposes I would need some node labels or

Minimal Purely Applicative Parser

纵然是瞬间 提交于 2019-12-06 04:30:49
问题 I'm trying to figure out how to build a "purely applicative parser" based on a simple parser implementation. The parser would not use monads in its implementation. I asked this question previously but mis-framed it so I'm trying again. Here is the basic type and its Functor , Applicative and Alternative implementations: newtype Parser a = Parser { parse :: String -> [(a,String)] } instance Functor Parser where fmap f (Parser cs) = Parser (\s -> [(f a, b) | (a, b) <- cs s]) instance

Is there a lazy mapM?

牧云@^-^@ 提交于 2019-12-06 04:04:25
问题 At first glance I thought these two functions would work the same: firstM _ [] = return Nothing firstM p (x:xs) = p x >>= \r -> if r then return (Just x) else firstM p xs firstM' p xs = fmap listToMaybe (mapM p xs) But they don't. In particular, firstM stops as soon as the first p x is true. But firstM' , because of mapM , needs the evaluate the whole list. Is there a "lazy mapM " that enables the second definition, or at least one that doesn't require explicit recursion? 回答1: There isn't

Help me understand this Scala code: scalaz IO Monad and implicits

痞子三分冷 提交于 2019-12-06 03:37:30
问题 This is a followup to this question. 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

Implementing this monad/type in Haskell?

狂风中的少年 提交于 2019-12-06 02:30:40
I really cannot figure out the syntax necessary for this, and it probably comes from my lack of understanding of how types work. I want a type DataPoint , which stores either a tuple (x, dataval) or two fields x and dataval (where x is a Double and dataval is a Complex Double . I want a Monad instance where it goes something like: instance Monad (DataPoint x dataval) where return dataval = DataPoint 0.0 dataval DataPoint x dataval >>= f = DataPoint x (f dataval) Basically, the "value" of the monad is dataval, and x is just a part of the container. I don't think my syntax is correct though. If