Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Difference between Monad composition and transformation (Dennis Raddle) 2. Re: Difference between Monad composition and transformation (Brent Yorgey) 3. Re: MonadError (Nick Vanderweit) 4. Re: associative arrays (Nick Vanderweit) ---------------------------------------------------------------------- Message: 1 Date: Sat, 25 Aug 2012 03:14:02 -0700 From: Dennis Raddle <dennis.rad...@gmail.com> Subject: Re: [Haskell-beginners] Difference between Monad composition and transformation To: Haskell Beginners <beginners@haskell.org> Message-ID: <CAKxLvoobf=nxutmq005ykp5iti9egat4emtmv1v_bj+ydqr...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" I'm not exactly a Haskell beginner but haven't grasped much of it yet. I have a feeling the answer to your question has something to do with the fact that the monad transformer library provides instances that allow the use of, for example, "get" and "put" in any transformed monad that has a StateT somewhere in it. Or throwError in anything that has an ErrorT in it. In other words, you don't need to lift everything into the right level. Okay, that's a crude way of putting it, but someone will probably come along and clarify. Dennis On Sat, Aug 25, 2012 at 1:15 AM, Song Zhang <vxan...@gmail.com> wrote: > > When I use a State Monad transformer to combine with a Writer Monad > StateT s (Writer w) a. is it different from composition of State Monad and > Writer Monad. It is State s (Writer w a) ? > StateT is defined as (s -> m (a, s)), so StateT s (Writer w) a can be > regarded as (s -> Writer w a) , which is (s -> ((a,w),s) > and on the other hand State s (Writer w a) is (s -> ((a,w),s). I suppose > the are similar and if so, what is the point we still get Monad > transformers? Thanks > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120825/5d31f39b/attachment-0001.htm> ------------------------------ Message: 2 Date: Sat, 25 Aug 2012 08:34:10 -0400 From: Brent Yorgey <byor...@seas.upenn.edu> Subject: Re: [Haskell-beginners] Difference between Monad composition and transformation To: beginners@haskell.org Message-ID: <20120825123410.ga15...@seas.upenn.edu> Content-Type: text/plain; charset=us-ascii On Sat, Aug 25, 2012 at 03:14:02AM -0700, Dennis Raddle wrote: > I'm not exactly a Haskell beginner but haven't grasped much of it yet. I > have a feeling the answer to your question has something to do with the > fact that the monad transformer library provides instances that allow the > use of, for example, "get" and "put" in any transformed monad that has a > StateT somewhere in it. Or throwError in anything that has an ErrorT in it. > In other words, you don't need to lift everything into the right level. Well, not really, that's a separate thing to do with type classes like MonadState and MonadError. To answer the OP's question, literally composing monadic types does often give you a type similar to the transformer version. However, the point of composing monads is to give you a new, combined monad. If you just use the type (State s (Writer w a)) this is just a State computation which happens to return something of type Writer w a. Combining such things with >>= does not take the Writer into account at all. On the other hand, StateT s (Writer w) a is a separate type with a Monad instance that combines the effects of State and Writer. -Brent > > On Sat, Aug 25, 2012 at 1:15 AM, Song Zhang <vxan...@gmail.com> wrote: > > > > > When I use a State Monad transformer to combine with a Writer Monad > > StateT s (Writer w) a. is it different from composition of State Monad and > > Writer Monad. It is State s (Writer w a) ? > > StateT is defined as (s -> m (a, s)), so StateT s (Writer w) a can be > > regarded as (s -> Writer w a) , which is (s -> ((a,w),s) > > and on the other hand State s (Writer w a) is (s -> ((a,w),s). I suppose > > the are similar and if so, what is the point we still get Monad > > transformers? Thanks > > > > > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners ------------------------------ Message: 3 Date: Sat, 25 Aug 2012 09:05:19 -0600 From: Nick Vanderweit <nick.vanderw...@gmail.com> Subject: Re: [Haskell-beginners] MonadError To: beginners@haskell.org Message-ID: <1506317.nA6l2h4R76@euler> Content-Type: text/plain; charset="us-ascii" The use of throwError :: e -> m a on a String type implies that e is String, but you have declared that e is more general. Here is a version that type checks: x :: (Error e, MonadError e m) => Float -> m Float x y = if y == 0 then throwError . strMsg $ "zero" else return y strMsg is part of the Error class, and will allow you to represent these errors more generally. Nick On Saturday, August 25, 2012 12:30:23 AM Dennis Raddle wrote: > I'm enjoying writing code that uses the Error monad to provide errors, > which I can catch and rethrow to add context. Kind of like getting a stack > trace with any context I want every time an error happens. > > In most cases I use monad transformers, so the actual monad type would be > something like "ErrorT String (State Int) a" > > But much of my code that handles errors doesn't need to know the entire > context. It uses throwError, but otherwise doesn't care anything else about > the type of the monadic computation. I.e., it needs to "know" that it's > inside a monadic computation of a typeclass MonadError. > > So I discovered that I could declare a function like this: > > x :: MonadError String m => Float -> m Float > x y = if y == 0 > then throwError "zero" > else return y > > My awareness of class constraints is vague, so as I worked on this problem > I knew in a fuzzy way it was going to look something like that, but I had > to experiment. > > Also, because I put "String" in the class constraint, I need to turn on > FlexibleContexts. > > So my question is, how would I do it without making "String" explicit? > > If I put > > x:: MonadError e m => Float -> m Float > x y = if y == 0 > then throwError "zero" > else return y > > I get errors saying "couldn't match expected type e against inferred type > [Char]" and something about functional dependencies > > I fiddled with it a bit but couldn't make it work. ------------------------------ Message: 4 Date: Sat, 25 Aug 2012 09:11:30 -0600 From: Nick Vanderweit <nick.vanderw...@gmail.com> Subject: Re: [Haskell-beginners] associative arrays To: beginners@haskell.org Message-ID: <2393110.4LR4UsJOVH@euler> Content-Type: text/plain; charset="us-ascii" I'd still recommend Data.Map, since it's a much more efficient data structure for the task. Nick On Friday, August 24, 2012 11:44:16 PM Karl Voelker wrote: > On Fri, Aug 24, 2012 at 10:05 PM, Christopher Howard > > <christopher.how...@frigidcode.com> wrote: > > What is typically used in Haskell circles to provide associative array > > functionality? (I.e., key-value type arrays.) I'm not really looking for > > efficiency so much as interface convenience. Obviously I could code it > > myself based on lists or something, but I don't want to reinvent the > > wheel. > > In simple situations, "coding it yourself" doesn't require much of any > code, since the Prelude comes with a lookup function for lists of > pairs: > > lookup :: Eq a => a -> [(a, b)] -> Maybe b > > -Karl V. > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 50, Issue 29 *****************************************