GHC: insert compilation date - haskell

I thought there would already be a question about this, but I can't find one.
I want my program to print out the date it was compiled on. What's the easiest way to set that up?
I can think of several possibilities, but none of them are what you'd call "easy". Ideally I'd like to be able to just do ghc --make Foo and have Foo print out the compilation date each time I run it.
Various non-easy possibilities that spring to mind:
Learn Template Haskell. Figure out how to use Data.Time to fetch today's date. Find a way how to transform that into a string. (Now my program requires TH in order to work. I also need to convince it to recompile that module every time, otherwise I get the compilation date for that module [which never changes] rather than the whole program.)
Write a shell script that generates a tiny Haskell module containing the system date. (Now I have to use that shell script rather than compile my program directly. Also, shell scripting on Windows leaves much to be desired!)
Sit down and write some Haskell code which generates a tiny Haskell module containing the date. (More portable than previous idea - but still requires extra build steps or the date printed will be incorrect.)
There might be some way to do this through Cabal - but do I really want to package up this little program just to get a date facility?
Does anybody have any simpler suggestions?

Using Template Haskell for this is relatively simple.
You just need to:
Run IO action within Template Haskell monad:
runIO :: IO a -> Exp a
Then create a string literal with:
stringE :: String -> ExpQ
Put a whole expression within a quasiquote.
$( ... )
This program will print time of its compilation:
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH
import Data.Time
main = print $(stringE =<< runIO (show `fmap` Data.Time.getCurrentTime))
You may put the relevant fragment into a module that imports all other modules to make sure it is recompiled.
Or take current revision information from your versioning system. See: TemplateHaskell and IO

The preprocessor helpfully defines __DATE__ and __TIME__ macros (just like in C), so this works:
{-# LANGUAGE CPP #-}
main = putStrLn (__DATE__ ++ " " ++ __TIME__)
This is probably simpler than Michal's suggestion of Template Haskell, but doesn't let you choose the format of the date.

Related

How can I set OverloadedStrings in an ihaskell notebook?

I understand from the sample notebook that I should be able to enable and disable extensions as follows:
-- We can disable extensions.
:ext NoEmptyDataDecls
data Thing
<interactive>:1:1: error:
• ‘Thing’ has no constructors (EmptyDataDecls permits this)
• In the data declaration for ‘Thing’
-- And enable extensions.
:ext EmptyDataDecls
data Thing
However, when I try this with OverloadedStrings, I do not see any success. You can see from the below that T.lines is looking for String rather than Text. Why?
What am I misunderstanding or doing wrong?
Good news: the notebook above does have OverloadedStrings loaded correctly. The problem is you need to read the file with:
T.readFile
So
main :: IO ()
main = do
text <- T.readFile "./data.txt"
print $ T.lines text
This was confusing because the error highlighted T.lines rather than readFile. It turns out readFile does not produce a form of textual data that will automatically cast to the format required by T.lines (it produces String, not Text). You had to know that there is an entirely other function to call that does do that. The type system will not convert between these string-like formats for you. You must do this yourself by calling a file-reading function that explicitly returns a Text: here, T.readFile.

How can I get GHC to emit a warning for a given function?

Suppose I've decided that everywhere in a given code base (package) I want to use a custom getCurrentTimeMicroseconds rather than getCurrentTime. Is there a way for me to get GHC to emit warnings me about uses of getCurrentTime, only in that code base? (Not for anything upstream or downstream.)
Bonus question, suppose I want to selectively allow usages with an explicit annotation at the use site (preferably not module-wide). Is this also possible?
This is what types are for. The type you use for times should reflect the constraints you want to place on the values it represents.
For example, you could wrap UTCTime like so:
newtype UTCTimeMicroseconds = UTCTimeMicroseconds { picos :: UTCTime }
microsecondsFromPicos :: UTCTime -> UTCTimeMicroseconds
microsecondsFromPicos = ...
getCurrentTimeMilliseconds :: IO UTCTimeMicroseconds
getCurrentTimeMilliseconds = microsecondsFromPicos <$> getCurrentTime
And use the new type everywhere in your package that you need times to have this property.
If you want to be strict about it, don't export the UTCTimeMicroseconds constructor, so the only way to get one of these values is to use microsecondsFromPicos, which enforces your requirement.
That makes any misuse an error, not a warning, but in most cases that's what you want anyway.
When you do want to use UTCTime with the full resolution, or just don't care, you can just use that type as usual. It'll be easy to find the places in your code base where that happens, because they'll be the only places where UTCTime is used.
I can't think of a way to do this right now, but I think the closest you can get is something like:
create a new package my-time that depends on time
re-export your shim functions annotated with warnings, like
import qualified Data.Time as Time
{-# WARNING getCurrentTime "you should prefer getCurrentTimeMicroseconds" #-}
getCurrentTime = Time.getCurrentTime
depend on my-time in your packages
obviously this doesn't give you a way to enforce not importing Data.Time.getCurrentTime, and is even less satisfying when the code you want to shim is in Prelude or base.

In TemplateHaskell, how do I figure out that an imported module has been renamed?

I am writing a bit of TemplateHaskell for stringing together QuickCheck style specifications. I require every module containing properties to export a symbol called ''axiom_set''. Then, my checkAxioms function finds all the ''axiom_set'' symbols from modules imported where I call checkAxioms.
checkAxioms :: DecsQ
checkAxioms = do
ModuleInfo ms <- reifyModule =<< thisModule
forM_ ms $ \mi#(Module _ m) -> do
runIO . print =<< lookupValueName (modString m ++ ".axiom_set")
The above code should find all the imported "axiom_set" symbols. However, if Module.Axioms defines axiom_set but that I imported as follows
import Module.Axioms as MA
my code can't find MA.axiom_set. Any advice?
I don't think there's a way to do that. This seems to be a limitation of TemplateHaskell.
It kinda makes sense to have only fully qualified names in ModuleInfos list of imported modules, but the fact that we can't use those fully qualified names in lookupValueName is bad. I think we need a variant of lookupValueName or lookupName that takes a Module as argument.
I suggest openning an issue at GHC issue tracker: https://ghc.haskell.org/trac/ghc/newticket We have ongoing work to improve TH in the next major release. Part of the work is about improving the package documentation, exported functions etc. This can be one of the improvements.

Alpha conversion on a Haskell expression

Given a Haskell expression, I'd like to perform alpha conversion, ie. rename some of the non free variables.
I've started implementing my own function for this, which works on a haskell-src-exts Exp tree, however it turns out to be surprisingly nontrivial, so I can't help wondering - is there an established easy-to-use library solution for this kind of source conversion? Ideally, it should integrate with haskell-src-exts.
This is one of the problems where the "Scrap Your Boilerplate" style generic libraries shine!
The one I'm most familiar with is the uniplate package, but I don't actually have it installed at the moment, so I'll use the (very similar) functionality found in the lens package. The idea here is that it uses Data.Data.Data (which is the best qualified name ever) and related classes to perform generic operations in a polymorphic way.
Here's the simplest possible example:
alphaConvert :: Module -> Module
alphaConvert = template %~ changeName
changeName :: Name -> Name
changeName (Ident n) = Ident $ n ++ "_conv"
changeName n = n
The (%~) operator is from lens and just means to to apply the function changeName to everything selected by the generic traversal template. So what this does is find every alphanumeric identifier and append _conv to it. Running this program on its own source produces this:
module AlphaConv where
import Language.Haskell.Exts
import Control.Lens
import Control.Lens.Plated
import Data.Data.Lens
instance Plated_conv Module_conv
main_conv
= do ParseOk_conv md_conv <- parseFile_conv "AlphaConv.hs"
putStrLn_conv $ prettyPrint_conv md_conv
let md'_conv = alphaConvert_conv md_conv
putStrLn_conv $ prettyPrint_conv md'_conv
alphaConvert_conv :: Module_conv -> Module_conv
alphaConvert_conv = template_conv %~ changeName_conv
changeName_conv :: Name_conv -> Name_conv
changeName_conv (Ident_conv n_conv)
= Ident_conv $ n_conv ++ "_conv"
changeName_conv n_conv = n_conv
Not terribly useful since it doesn't distinguish between identifiers bound locally and those defined in an outside scope (such as being imported), but it demonstrates the basic idea.
lens may seem a bit intimidating (it has a lot more functionality than just this); you may find uniplate or another library more approachable.
The way you'd approach your actual problem would be a multi-part transformation that first selects the subexpressions you want to alpha-convert inside of, then uses a transformation on those to modify the names you want changed.

How to get the literal value of a TemplateHaskell named variable

If I have a Name in TemplateHaskell and want to find out the value of the variable that it names, provided that the variable is declared as a literal, can this be done?
var = "foo"
-- Can `contentsOf` be defined?
$((contentsOf . mkName $ "var") >>= guard . (== "foo"))
In theory, yes. In practice, no.
Finding out stuff about existing names is done using reify :: Name -> Q Info, and for a definition like that you would get back a VarI value, which includes a Maybe Dec field. This would seem to suggest that you might in some cases be able to get the syntax tree for the declaration of the variable, which would allow you to extract the literal, however current versions of GHC always returns Nothing in this field, so you're out of luck for a pure TH solution.
However, TH does allow arbitrary IO actions to be run, so you could potentially work around this by loading and parsing the module yourself using something like haskell-src-exts, however I suspect that would be more trouble than it's worth.

Resources