As I'm learning Haskell I'm realizing that do
notation is just syntatic sugar:
a = do x <- [3..4]
[1..2]
return (x, 42)
Translates into
a = [3..4] >>= (\x -> [1..2] >>= (\_ -> return (x, 42)))
I realize that I'll probably use do-notation but I'd like to understand whats going on in translation. So purely for pedagogical reasons, is there a way for ghc/ghci to give me the corresponding bind statements for a fairly complex monad written in do-notation?
Edit. It turns out lambdabot on #haskell can do this:
<Guest61347> @undo do x <- [3..4] ; [1..2] ; return (x, 42)
<lambdabot> [3 .. 4] >>= \ x -> [1 .. 2] >> return (x, 42)
Here's the source code the Undo plugin.
You can ask for the output of GHC's desugarer, however this will also desugar a lot of other syntax.
First, we'll put your code in a module
Foo.hs
:Next, we'll ask GHC to compile it and output the result after the desugaring phase:
The output may look rather messy, because it is a variant of Haskell called Core that is used as GHC's intermediate language. However, it's not too hard to read once you get used to it. In the middle of some other definitions we find yours:
Core isn't too pretty, but being able to read it is very useful when working with GHC, as you can read the dump after later stages to see how GHC is optimizing your code.
If we remove the
_xyz
suffixes added by the renamer, as well as the type applications@ Xyz
and the calls toGHC.Integer.smallInteger
, and make the operators infix again, you're left with something like this: