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. Lifting (Either SqlError a) in a monad stack (Bryan Vicknair) 2. Re: Lifting (Either SqlError a) in a monad stack (Karl Voelker) 3. Re: print [] (AntC) ---------------------------------------------------------------------- Message: 1 Date: Mon, 26 Aug 2013 14:05:00 -0700 From: Bryan Vicknair <bryanv...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Lifting (Either SqlError a) in a monad stack Message-ID: <20130826210500.GA4710@bry-m6300> Content-Type: text/plain; charset=us-ascii I'm at the beginning of the monad transformers journey, and this is stumping me. I've read "Monad Transformers Step by Step" which made me confident enough to play with simple stacks, but I'm a bit lost in the following example from work. Please excuse any incorrect transformer terminology. In a WAI web app, I want to chain these two actions inside a monad stack: > safeConnect :: ConnectInfo -> IO (Either SqlError Connection) > safeInsert :: Thing -> Connection -> IO (Either SqlError Id) Here is an example where I would like 'result' to be (Right Id) only if the DB connection *and* the insert were successful, (Left SqlError) otherwise. > add :: Request -> ResourceT IO Response > add _ = do > result <- liftIO $ safeConnect devConnInfo >>= safeInsert thing > case result of > (Left e) -> return $ dbErr e > (Right _) -> return $ postRedirect > where thing = exampleThing The compiler tells me (edited for brevity): Expected type: Either SqlError Connection -> GHC.Types.IO a0 Actual type: Connection -> GHC.Types.IO (Either SqlError Id) In the return type of a call of `safeInsert' In the second argument of `(>>=)', namely `safeInsert thing' How can I chain safeConnect and safeInsert using the (Either SqlError a) monad inside WAI's (ResourceT IO Response) monad stack? ------------------------------ Message: 2 Date: Mon, 26 Aug 2013 16:18:16 -0700 From: Karl Voelker <ktvoel...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Lifting (Either SqlError a) in a monad stack Message-ID: <CAFfow0x=qtktos2wtqkjnoozhstnfjrz7fgdrotrhjfndl0...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" The problem is that the Either SqlError monad is not actually a part of your "monad stack". It's just something that appears in the result type of safeConnect and safeInsert. In order to put Either SqlError into your stack, you'll need a monad transformer which exhibits Either-like semantics, such as EitherT or ErrorT. Then your safeConnect would have type "ConnectInfo -> EitherT SqlError IO Connection". http://hackage.haskell.org/packages/archive/either/3.4.1/doc/html/Control-Monad-Trans-Either.html http://hackage.haskell.org/packages/archive/transformers/0.3.0.0/doc/html/Control-Monad-Trans-Error.html -Karl On Mon, Aug 26, 2013 at 2:05 PM, Bryan Vicknair <bryanv...@gmail.com> wrote: > I'm at the beginning of the monad transformers journey, and this is > stumping > me. I've read "Monad Transformers Step by Step" which made me confident > enough > to play with simple stacks, but I'm a bit lost in the following example > from > work. Please excuse any incorrect transformer terminology. > > In a WAI web app, I want to chain these two actions inside a monad stack: > > > safeConnect :: ConnectInfo -> IO (Either SqlError Connection) > > safeInsert :: Thing -> Connection -> IO (Either SqlError Id) > > Here is an example where I would like 'result' to be (Right Id) only if > the DB > connection *and* the insert were successful, (Left SqlError) otherwise. > > > add :: Request -> ResourceT IO Response > > add _ = do > > result <- liftIO $ safeConnect devConnInfo >>= safeInsert thing > > case result of > > (Left e) -> return $ dbErr e > > (Right _) -> return $ postRedirect > > where thing = exampleThing > > The compiler tells me (edited for brevity): > > Expected type: Either SqlError Connection -> GHC.Types.IO a0 > Actual type: Connection -> GHC.Types.IO (Either SqlError Id) > In the return type of a call of `safeInsert' > In the second argument of `(>>=)', namely > `safeInsert thing' > > How can I chain safeConnect and safeInsert using the (Either SqlError a) > monad > inside WAI's (ResourceT IO Response) monad stack? > > _______________________________________________ > 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/20130826/98d85c5e/attachment-0001.html> ------------------------------ Message: 3 Date: Tue, 27 Aug 2013 09:46:59 +0000 (UTC) From: AntC <anthony_clay...@clear.net.nz> To: beginners@haskell.org Subject: Re: [Haskell-beginners] print [] Message-ID: <loom.20130827t114022-...@post.gmane.org> Content-Type: text/plain; charset=utf-8 > Lukas Lehner <lehner.lukas <at> gmail.com> writes: > > ... My question is that I have to enforce the type that specific way (don't want to use?ExtendedDefaultRules) or is there some more generic way? > print flatten (List ([] :: Show a => [a]) Even for an empty list, we have to provide evidence that the list is showable. That's what the type signature is doing: I am a list of something/anything showable. (If you want all of your NestedLists's to be showable, it's better to put a Show constraint on the data decl. But that would take us into existential types or GADT's.) AntC ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 62, Issue 23 *****************************************