monads

How are mutable arrays implemented in Haskell?

◇◆丶佛笑我妖孽 提交于 2019-11-27 19:21:22
I've read many research papers on this topic, and they usually argue that arrays are implemented using Monads. But none of these papers gave a clear definition of how the "type" Array itself should be defined, they only gave definitions for the functions using monads to access or modify this type. How are arrays, having O(1) time to access or modify an indexed element, implemented in Haskell ?! (such as STUArray and MArray) How are arrays, having O(1) time to access or modify an indexed element, implemented in Haskell They are implemented via primitive operations in the runtime system for

To what extent are Applicative/Monad instances uniquely determined?

旧巷老猫 提交于 2019-11-27 19:17:12
问题 As described this question/answers, Functor instances are uniquely determined, if they exists. For lists, there are two well know Applicative instances: [] and ZipList. So Applicative isn't unique (see also Can GHC derive Functor and Applicative instances for a monad transformer? and Why is there no -XDeriveApplicative extension?). However, ZipList needs infinite lists, as its pure repeats a given element indefinitely. Are there other, perhaps better examples of data structures that have at

Defining a new monad in haskell raises no instance for Applicative

核能气质少年 提交于 2019-11-27 19:14:12
I am trying to define a new monad and I am getting a strange error newmonad.hs newtype Wrapped a = Wrap {unwrap :: a} instance Monad Wrapped where (>>=) (Wrap x) f = f x return x = Wrap x main = do putStrLn "yay" $ ghc --version The Glorious Glasgow Haskell Compilation System, version 7.10.1 $ ghc newmonad.hs [1 of 1] Compiling Main ( newmonad.hs, newmonad.o ) newmonad.hs:2:10: No instance for (Applicative Wrapped) arising from the superclasses of an instance declaration In the instance declaration for ‘Monad Wrapped’ Why do I need to define an instance of Applicative ? This is the

why isn't Validation a Monad? (scalaz7)

南笙酒味 提交于 2019-11-27 19:04:02
an example use case: def div2(i: Int): Validation[String, Int] = if (i%2 == 0) Validation.success(i/2) else Validation.failure("odd") def div4(i: Int) = for { a <- div2(i) b <- div2(a) } yield b error : Unable to unapply type scalaz.Validation[String,Int] into a type constructor of kind M[_] that is classified by the type class scalaz.Bind I guess the error is caused by the compiler can't find a Monad instance for Validation[String, Int] I can make one for myself, like: object Instances { implicit def validationMonad[E] = new Monad[({type L[A] = Validation[E, A]})#L] { override def point[A](a:

Haskell pre-monadic I/O

百般思念 提交于 2019-11-27 18:28:13
I wonder how I/O were done in Haskell in the days when IO monad was still not invented. Anyone knows an example. Edit: Can I/O be done without the IO Monad in modern Haskell? I'd prefer an example that works with modern GHC. Before the IO monad was introduced, main was a function of type [Response] -> [Request] . A Request would represent an I/O action like writing to a channel or a file, or reading input, or reading environment variables etc.. A Response would be the result of such an action. For example if you performed a ReadChan or ReadFile request, the corresponding Response would be Str

What exactly does “effectful” mean

人盡茶涼 提交于 2019-11-27 18:22:43
Time and again I read the term effectful , but I am still unable to give a clear definition of what it means. I assume the correct context is effectful computations , but I've also seen the term effectful values ) I used to think that effectful means having side effects . But in Haskell there are no side-effects (except to some extent IO). Still there are effectful computations all over the place. Then I read that monads are used to create effectful computations. I can somewhat understand this in the context of the State Monad. But I fail to see any side-effect in the Maybe monad. In general

Why should Applicative be a superclass of Monad?

岁酱吖の 提交于 2019-11-27 18:07:30
Given: Applicative m, Monad m => mf :: m (a -> b), ma :: m a it seems to be considered a law that: mf <*> ma === do { f <- mf; a <- ma; return (f a) } or more concisely: (<*>) === ap The documentation for Control.Applicative says that <*> is "sequential application," and that suggests that (<*>) = ap . This means that <*> must evaluate effects sequentially from left to right, for consistency with >>= ... But that feels wrong. McBride and Paterson's original paper seems to imply that the left-to-right sequencing is arbitrary: The IO monad, and indeed any Monad, can be made Applicative by taking

In what sense is the IO Monad pure?

半城伤御伤魂 提交于 2019-11-27 17:14:57
I've had the IO monad described to me as a State monad where the state is "the real world". The proponents of this approach to IO argue that this makes IO operations pure, as in referentially transparent. Why is that? From my perspective it appears that code inside the IO monad have plenty of observable side effects. Also, isn't it possible to describe pretty much any non-pure function like a function of the real world? For example, can't we think of, say, C's malloc as being a function that takes a RealWorld and an Int and returns a pointer and a RealWorld , only just like in the IO monad the

Simplest non-trivial monad transformer example for “dummies”, IO+Maybe

别等时光非礼了梦想. 提交于 2019-11-27 17:13:08
Could someone give a super simple (few lines) monad transformer example, which is non-trivial (i.e. not using the Identity monad - that I understand). For example, how would someone create a monad that does IO and can handle failure (Maybe)? What would be the simplest example that would demonstrate this? I have skimmed through a few monad transformer tutorials and they all seem to use State Monad or Parsers or something complicated (for a newbee). I would like to see something simpler than that. I think IO+Maybe would be simple, but I don't really know how to do that myself. How could I use an

Monads as adjunctions

这一生的挚爱 提交于 2019-11-27 17:01:07
I've been reading about monads in category theory. One definition of monads uses a pair of adjoint functors. A monad is defined by a round-trip using those functors. Apparently adjunctions are very important in category theory, but I haven't seen any explanation of Haskell monads in terms of adjoint functors. Has anyone given it a thought? Edit : Just for fun, I'm going to do this right. Original answer preserved below The current adjunction code for category-extras now is in the adjunctions package: http://hackage.haskell.org/package/adjunctions I'm just going to work through the state monad