haskellpragmalivecoding

Finding the line number of a function in Haskell


I am trying to create a Haskell program which draws some simple 2d shapes to screen, but when you hover over each shape, it prints the line of source code where the shape was created.

In order to do this I would like to be able to create shapes with parameters for their dimensions and a final parameter which indicates the line number. Something like this:

rect1 = Shape(Rectangle 2 2 lineNumber)

This would create a rectangle of width 2 pixels, height 2 pixels, and use a function lineNumber to store the line this piece of code was written on. Does such a function exist in Haskell? Is it simple to create one?

I have searched stack overflow and found this question where the answerer suggests that the __LINE__ pragma from C++ can be used to achieve a similar effect. Is this the best way to go about it or is there a way to do it in pure Haskell?


Solution

  • You can do this with Template Haskell, which is technically yet another GHC extension, but is probably somehow more "pure" than C preprocessor.

    Code stolen from here and modified slightly.

    {-# LANGUAGE TemplateHaskell #-}
    
    module WithLocation (withLocation) where
    import Language.Haskell.TH
    
    withLocation' :: String -> IO a -> IO a
    withLocation' s f = do { putStrLn s ; f }
    
    withLocation :: Q Exp
    withLocation = withFileLine [| withLocation' |]
    
    withFileLine :: Q Exp -> Q Exp
    withFileLine f = do
        let loc = fileLine =<< location
        appE f loc
    
    fileLine :: Loc -> Q Exp
    fileLine loc = do
        let floc = formatLoc loc
        [| $(litE $ stringL floc) |]
    
    formatLoc :: Loc -> String
    formatLoc loc = let file = loc_filename loc
                        (line, col) = loc_start loc
                    in concat [file, ":", show line, ":", show col]
    

    Use it like this (from another module):

    {-# LANGUAGE TemplateHaskell #-}
    
    module Main where
    import WithLocation
    
    main = do
      $withLocation $ putStrLn "===oo0=Ü=0oo=== Kilroy was here"