Personal tools

Indent

From HaskellWiki

Revision as of 11:05, 4 April 2006 by DonStewart (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

1 Indenting Haskell Mechanically

indent(1) is a tool to format C program source. At the time of writing we have no such tool for Haskell in common use -- although this would be fairly easy to write, given the existing parsing and pretty printing libraries.

Here are some other solutions

1.1 Emacs

Emacs has an indent mode. Though it is often considered poor at laying out Haskell code.

1.2 Use GHC

GHC provides a --ddump-parsed flag. It can be used as follows. Note that it will strip comments -- so take care! It will also normalise values -- so 0xdeadbeef becomes a large decimal.

    main = do  {
        ;print (f 10)
                ;      print (f 20)
       ;print (f 0xdeadbeef)       
    }                                                                                                 
    f x = sqrt  ( fromIntegral 
            (x * 10133123 `mod` 
                        1231111121 :: Int ))

Running it through:

$ ghc -fno-code -ddump-parsed B.hs
                                     
==================== Parser ====================
main = do                                       
         print (f 10)
         print (f 20)
         print (f 3735928559)
f x = sqrt (fromIntegral ((x * 10133123) `mod` 1231111121 :: Int))


Postprocess:

$ ghc -fno-code -ddump-parsed B.hs | sed '/^===/d'
main = do
         print (f 10)
         print (f 20)
         print (f 3735928559)
f x = sqrt (fromIntegral ((x * 10133123) `mod` 1231111121 :: Int))

Which is a reasonable result.

1.3 Use ghc-api

?

1.4 Use Language.Haskell

Downside is the lack of parsing the extensions we use. And it will also strip comments.