これは、技術的にはさらに別のGHC拡張機能であるTemplate Haskellを使用して行うことができますが、おそらくCプリプロセッサよりも「純粋」です。
ここからコードが盗まれ、わずかに変更されました。
{-# 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]
(別のモジュールから)次のように使用します。
{-# LANGUAGE TemplateHaskell #-}
module Main where
import WithLocation
main = do
$withLocation $ putStrLn "===oo0=Ü=0oo=== Kilroy was here"