Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does TemplateHaskell cause GHC to load packages?

I have a trivial Template Haskell program that prints the name of the current module (Main, here):

{-# LANGUAGE TemplateHaskell #-}
module Main
( main
) where

import Language.Haskell.TH
import Language.Haskell.TH.Syntax

modName ∷ String
modName = $(fmap loc_module qLocation »= λmod → return (LitE (StringL mod) ))

main ∷ IO ()
main = putStrLn modName

When I compile this, I get the following Loading messages from ghc:

tsuraan@localhost ~/test/modname $ ghc --make Main
[1 of 1] 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.4.0.0 ... linking ... done.
Loading package deepseq-1.3.0.0 ... linking ... done.
Loading package containers-0.4.2.1 ... linking ... done.
Loading package pretty-1.1.1.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
Linking Main ...

Why does ghc load all these packages when Template Haskell is enabled? Whenever I build a program that uses Template Haskell, especially one that is built against a lot of packages, my compile warnings are overwhelmed with these superfluous "Loading" messages. It would be nice if I could stop the messages from being printed, or stop the (unnecessary?) module loading from happening at all.

like image 436
tsuraan Avatar asked Jul 10 '12 21:07

tsuraan


Video Answer


1 Answers

Template Haskell runs at compile time, via a bytecode interpreter (GHCi). Any package dependencies that you have -- at compile time -- will be loaded dynamically into GHC -- at compile time, so that you can execute your splices.

One of your dependencies is the Template Haskell library itself, which in turn depends on most of the core things.

like image 126
Don Stewart Avatar answered Sep 24 '22 11:09

Don Stewart