monads

mtl, transformers, monads-fd, monadLib, and the paradox of choice

Hackage has several packages for monad transformers: mtl: Monad transformer library transformers: Concrete functor and monad transformers monads-fd: Monad classes, using functional dependencies monads-tf: Monad classes, using type families monadLib: A collection of monad transformers. mtl-tf: Monad transformer library using type famili...

Can Haskell's monads be thought of as using and returning a hidden state parameter?

I don't understand the exact algebra and theory behind Haskell's monads. However, when I think about functional programming in general I get the impression that state would be modelled by taking an initial state and generating a copy of it to represent the next state. This is like when one list is appended to another; neither list gets...

Performance of looping over an Unboxed array in Haskell

First of all, it's great. However, I came across a situation where my benchmarks turned up weird results. I am new to Haskell, and this is first time I've gotten my hands dirty with mutable arrays and Monads. The code below is based on this example. I wrote a generic monadic for function that takes numbers and a step function rather...

Is do-notation specific to "base:GHC.Base.Monad"?

The idea that the standard Monad class is flawed and that it should actually extend Functor or Pointed is floating around. I'm not necessarily claiming that it is the right thing to do, but suppose that one was trying to do it: import Prelude hiding (Monad(..)) class Functor m => Monad m where return :: a -> m a join :: m (m a...

Avoiding explicit recursion in Haskell

The following simple function applies a given monadic function iteratively until it hits a Nothing, at which point it returns the last non-Nothing value. It does what I need, and I understand how it works. lastJustM :: (Monad m) => (a -> m (Maybe a)) -> a -> m a lastJustM g x = g x >>= maybe (return x) (lastJustM g) As part of my self...

Linking/Combining Type Classes in Haskell

Say I have two type classes defined as follows that are identical in function but different in names: class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a class PhantomMonad p where pbind :: p a -> (a -> p b) -> p b preturn :: a -> p b Is there a way to tie these two classes together so someth...

Null Safe dereferencing in Java like ?. in Groovy using Maybe monad

I'm working on a codebase ported from Objective C to Java. There are several usages of method chaining without nullchecks dog.collar().tag().name() I was looking for something similar to safe-dereferencing operator ?. in Groovy instead of having nullchecks dog.collar?.tag?.name This led to Maybe monad to have the notion of Nothing ...

An unusual type signature

In Monads for natural language semantics, Chung-Chieh Shan shows how monads can be used to give a nicely uniform restatement of the standard accounts of some different kinds of natural language phenomena (interrogatives, focus, intensionality, and quantification). He defines two composition operations, A_M and A'_M, that are useful for t...

LINQ query expressions that operate on types (monads?) other than IEnumerable<T> -- Possible uses?

I'm reading the book Real-world functional programming by Tomas Petricek and Jon Skeet and I'm having a hard time digesting the section on computation expressions1) (aka monads). Through this book, I learnt that contrary to my previous experiences LINQ query expressions aren't restricted to IEnumerable<T>, but can work on other custom...

Haskell maps returning a monad

The lookup function in Data.Map and Data.IntMap currently return values wrapped in Maybe with the type signature lookup :: Ord k => k -> Map k a -> Maybe a It used to have the more general type of lookup :: (Monad m, Ord k) => k -> Map k a -> m a I realize the former likely reduces the need of extra type specification, but ...

Haskell IO Passes to Another Function

This question here is related to http://stackoverflow.com/questions/3066956/haskell-input-return-tuple I wonder how we can passes the input from monad IO to another function in order to do some computation. Actually what i want is something like -- First Example test = savefile investinput -- Second Example maxinvest :: a maxinvest...

Large-scale design in Haskell?

Dear all, What is a good way to design/structure large functional programs, especially in Haskell? I've been through a bunch of the tutorials (Write Yourself a Scheme being my favorite, with Real World Haskell a close second) - but most of the programs are relatively small, and single-purpose. Additionally, I don't consider some of th...

map versus mapM behavior

I'm on the I/O chapter of Real World Haskell. Monads aren't discussed in the book for another 7 chapters. Which is to say, my understanding of I/O is, at best, incomplete. Right now I am trying to comprehend the mapM function. As I understand it, the function "executes" each element in the list which must be an "action" (IO monad). ...

Implementing the Haskell-MaybeMonad in F# - how can we get this lazy?

Hi, we are trying to build the Haskell-MaybeMonad sample from http://www.haskell.org/all_about_monads/html/maybemonad.html in F#. The idea is to search for a mailaddress in two dictionaries. If one of the both lookups returns a result we look into the third one. let bindM x k = match x with | Some value -> k value | None ...

Haskell: How to write interactive interpreter on top of a State monad?

We're working on a model filesystem that uses a state monad internally. We have a type class with operations like these: class Monad m => FS m where isDirectory :: Path -> m Bool children :: Path -> m [Path] ... We're working on a little interactive interpreter that will offer commands like cd, ls, cat, and so on. An oper...

Monad in non-programming terms

How would you describe a monad in non-programming terms? Is there some concept/thing outside of programming (outside of all programming, not just FP) which could be said to act or be monad-like in a significant way? ...

What is context in Haskell multiparameter type class

In chapter 15 of Real World Haskell, a type class is defined: class (Monad m) => MonadSupply s m | m -> s where A couple paragraphs later, it says that >>= and return don't need to be defined because of the context. But there's no further explanation of what it means by context. How does the compiler know MonadSupply is an instance ...

Reader Monad with Scalaz

I try to define the Reader monad with scalaz like this: import scalaz._ import Scalaz._ final class Reader[E,A](private[Reader] val runReader: E => A) object Reader { def apply[E,A](f: E => A) = new Reader[E,A](f) def env[E]: Reader[E,E] = Reader(identity _) implicit def ReaderMonad[E] = new Monad[PartialApply1Of2[Reader,E]#App...

Are there any nice-n-simple Computation Expression tutorials?

Hi all, I'm a C# guy looking to learn F# and functional programming. I'm hearing a lot about "monads" .. which I think (thanks to Google), are called "Computation Expressions" in F#? One aspect I'm trying to bend my head round is the State Monad/Computation Expression... But I need an idiot-proof, line-by-line, explanation about what'...

Tips for more elegant code with monads?

Hi. I finally got a hold on how to use monads (don't know if I understand them...), but my code is never very elegant. I guess is from a lack of grip on how all those functions on Control.Monad can really help. So I'd thought it would be nice to ask for tips on this in a particular piece of code using the state monad. The goal of the co...