Grok Monad Transformers - some help needed

I'm having another go at figuring out Monad Transformers, starting at the same point I've started and stopped the last couple of times. That's this tutorial... http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html Onion layers, lift etc - I get that. But I've never actually got anything to work - and therefore never built the confidence to move on. Problem 1 - this is literate Haskell. I've tried feeding it to haddock - all I get is an error about it lacking a main. I don't know what to actually do with this after putting it in a *.lhs file. No big deal, but... Problem 2 - even cutting the code out, shoving it in a *.hs file, and :loading it into GHCi, I still get a lot of errors. For the functions test1 and test2, the fixes were explicit type signatures. Easy enough to figure out... test1 :: State Int (Int, Int) test2 :: State String (String, String) I guess the basic issue is the same with test3 (and though I haven't tried the others today, probably test5 and test7 too). The trouble there is that I don't know what those type signatures should be because I don't know that much about monad transformers and how they can work. I can see what's going on in the body of test3, but that is a very small amount of understanding - the type signatures are important. And I can't use :type because GHCi is rejecting the code. In short... HELP!!!

I don't know what to actually do with this after putting it in a *.lhs file.
You can :load *.lhs into ghci the same way you load .hs-files.
I'm not sure why you were getting the ambiguous type errors. I didn't:
test1 :: StateT Integer Identity (Integer, Integer)
test2 :: StateT [Char] Identity ([Char], [Char])
test3 :: StateT Integer (StateT [Char] Identity) (Integer, [Char])
test5 :: StateT Integer IO ()
test7 :: StateT Integer (StateT [Char] Identity) (Integer, [Char])
Hope that helps.
Arseniy.
On 2 January 2012 06:03, Steve Horne
I'm having another go at figuring out Monad Transformers, starting at the same point I've started and stopped the last couple of times. That's this tutorial...
http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html
Onion layers, lift etc - I get that. But I've never actually got anything to work - and therefore never built the confidence to move on.
Problem 1 - this is literate Haskell. I've tried feeding it to haddock - all I get is an error about it lacking a main. I don't know what to actually do with this after putting it in a *.lhs file.
No big deal, but...
Problem 2 - even cutting the code out, shoving it in a *.hs file, and :loading it into GHCi, I still get a lot of errors.
For the functions test1 and test2, the fixes were explicit type signatures. Easy enough to figure out...
test1 :: State Int (Int, Int) test2 :: State String (String, String)
I guess the basic issue is the same with test3 (and though I haven't tried the others today, probably test5 and test7 too). The trouble there is that I don't know what those type signatures should be because I don't know that much about monad transformers and how they can work. I can see what's going on in the body of test3, but that is a very small amount of understanding - the type signatures are important. And I can't use :type because GHCi is rejecting the code.
In short... HELP!!!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 02/01/2012 06:12, Arseniy Alekseyev wrote:
I don't know what to actually do with this after putting it in a *.lhs file. You can :load *.lhs into ghci the same way you load .hs-files. I swear I tried this before, but now it suddenly works.
Must be the chaos of stupid random assumptions making me do stupid things - I go through this phase with anything new. I have no idea what I was doing to screw things up, but I'm pretty there's no conspiracy to drive me nuts. Though now I think of it... hmmm... I wonder... The errors I was getting from GHC mostly referred to "the monomorphism restriction" - at least for test1 and test2. The errors for test3 were much longer and scarier. I'll try to figure out what's different going from GHC to GHCi later - for the moment, I'm getting back to those transformers. BTW - interesting how the signatures of test1 and test2 are reported - I hadn't realised monad transformers were relevant there. Of course it does seem a bit silly to implement both StateT and State when StateT can implement State for you.

On Mon, Jan 2, 2012 at 03:32, Steve Horne
BTW - interesting how the signatures of test1 and test2 are reported - I hadn't realised monad transformers were relevant there. Of course it does seem a bit silly to implement both StateT and State when StateT can implement State for you.
If you're using the mtl1 library, State is separate from StateT. With the transformers library (or mtl2, which is a wrapper for transformers) State is defined as StateT Identity. Part of the reason for this is that older ghc didn't optimize very well with something like StateT Identity; part is simply because monad transformers came later and nobody bothered to go back and rework the standalone monads as transformers over an Identity monad. The original Monad library included non-transformer-based multi-monads such as RWS (Reader+Writer+State) as well, since nobody has worked out transformers as a concept yet. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (3)
-
Arseniy Alekseyev
-
Brandon Allbery
-
Steve Horne