While writing the post about performance, I got interested in Template Haskell, specially in the $(lift) construction. Investigating a little bit about it, I got to the following code:
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH.Syntax
import X
main :: IO ()
main = print $(lift $ x)
With X.hs:
module X where
import System.IO.Unsafe
x :: Int
x = read $ unsafePerformIO $ putStr "Number: " >> getLine
The result was a surprise to me. It asked for the number in compilation time.
$ ghc --make main
[2 of 2] Compiling Main ( main.hs, main.o )
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package array-0.3.0.0 ... linking ... done.
Loading package containers-0.3.0.0 ... linking ... done.
Loading package pretty-1.0.1.1 ... linking ... done.
Loading package template-haskell ... linking ... done.
Number: 5
Linking main ...
marcot@zezinho:~/codigo/haskell$ ./main
5
18/3/2010 (quinta-feira) às 12:13 |
That’s very funny! :-P