28

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.

Stephen Diehl
  • 8,271
  • 5
  • 38
  • 56

1 Answers1

23

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:

module Foo where

a = do x <- [3..4]
       [1..2]
       return (x, 42)

Next, we'll ask GHC to compile it and output the result after the desugaring phase:

$ ghc -c Foo.hs -ddump-ds

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:

Foo.a :: [(GHC.Integer.Type.Integer, GHC.Integer.Type.Integer)]
LclIdX
[]
Foo.a =
  >>=_agg
    @ GHC.Integer.Type.Integer
    @ (GHC.Integer.Type.Integer, GHC.Integer.Type.Integer)
    (enumFromTo_ag7
       (GHC.Integer.smallInteger 3) (GHC.Integer.smallInteger 4))
    (\ (x_adf :: GHC.Integer.Type.Integer) ->
       >>_agn
         @ GHC.Integer.Type.Integer
         @ (GHC.Integer.Type.Integer, GHC.Integer.Type.Integer)
         (enumFromTo_ags
            (GHC.Integer.smallInteger 1) (GHC.Integer.smallInteger 2))
         (return_aki
            @ (GHC.Integer.Type.Integer, GHC.Integer.Type.Integer)
            (x_adf, GHC.Integer.smallInteger 42)))

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 to GHC.Integer.smallInteger, and make the operators infix again, you're left with something like this:

Foo.a :: [(GHC.Integer.Type.Integer, GHC.Integer.Type.Integer)]
Foo.a = enumFromTo 3 4 >>= \x -> enumFromTo 1 2 >> return (x, 42)
hammar
  • 138,522
  • 17
  • 304
  • 385
  • 4
    Which begs the question... is there something out there that will automatically remove _xyz suffixes, type applications, calls to GHC.Integer.smallInteger and make operators infix again? – hugomg Nov 05 '11 at 11:58
  • 1
    @missingno: None that I'm aware of. However, there are libraries available for working with either Haskell or Core on Hackage, so it shouldn't be too hard to hack something together if you want it badly enough. – hammar Nov 05 '11 at 12:26
  • 4
    The trouble is, by the time you've written the code to do that, you don't need it any more. – Paul Johnson Nov 05 '11 at 12:42
  • 5
    Just for fun, [a quick and dirty do-notation desugarer for Haskell 98](https://gist.github.com/1341505). – hammar Nov 05 '11 at 13:39
  • 9
    @missingno there is a flag to suppress the suffices, `ghc -ddump-ds -dsuppress-uniques Foo.hs` will removethem from the output. From ghc-7.2 on, there's also a `-ddump-to-file` flag which will make ghc dump the output into in this case Foo.dump-ds. Very useful if you want more than one dump, e.g. `ghc -ddump-ds -ddump-simpl -ddump-to-file Foo` will produce Foo.dump-ds and Foo.dump.simpl (btw., `-ddump-simpl` also provides a desugaring if you compile without optimisations). – Daniel Fischer Nov 05 '11 at 13:53
  • @missingno: Looks like there is a tool which does most of this [commented-out in the source of the extcore package](http://hackage.haskell.org/packages/archive/extcore/1.0.1/doc/html/src/Language-Core-DebugPrinter.html). It shouldn't be too hard to fix the compile errors and make it build. – hammar Nov 05 '11 at 14:46
  • @hammar Thanks for the gist, works beautifully for the examples I was trying out. – Stephen Diehl Nov 05 '11 at 22:41