Template Haskell question

I'm interested if it's possible to use functions from some module without explicitly importing it. In ghci it's done on the fly, like this: Prelude> Data.Map.empty Loading package array-0.3.0.2 ... linking ... done. Loading package containers-0.4.0.0 ... linking ... done. fromList [] But without gchi it seems impossible. I have the file Test.hs:
{-# LANGUAGE TemplateHaskell #-} module Test where import Language.Haskell.TH
x :: ExpQ x = global $ mkName "Data.Map.empty"
When I load it in ghci, all works: $ ghci -XTemplateHaskell Test.hs *Test> $x Loading package pretty-1.0.1.2 ... linking ... done. Loading package array-0.3.0.2 ... linking ... done. Loading package containers-0.4.0.0 ... linking ... done. Loading package template-haskell ... linking ... done. fromList [] But when I try to use it from other module, it fails. File Main.hs:
{-# LANGUAGE TemplateHaskell #-} module Main where import Test
main = do print $x
$ runhaskell Main.hs Main.hs:5:9: Not in scope: `Data.Map.empty' In the result of the splice: $x To see what the splice expanded to, use -ddump-splices In the first argument of `print', namely `$x' In the expression: print ($x) -- All the best, Alexey

Hello, assuming you mean avoiding the import of Data.Map in the module *using* x, you can use name quotations: A.hs:
{-# LANGUAGE TemplateHaskell #-}
module A where
import Data.Map import Language.Haskell.TH
x = varE 'empty
B.hs:
{-# LANGUAGE TemplateHaskell #-} module B where import A
a = $x
empty = "" -- No collision; the 'empty in A.hs becomes a fully qualified -- name according to what's in scope in A.hs, so 'a' has type -- Map k a
To avoid the import in A.hs too:
module A where
import Language.Haskell.TH import Language.Haskell.TH.Syntax(mkNameG_v)
x = varE (mkNameG_v "containers-0.4.0.0" "Data.Map" "empty")
By the way, 'global' currently is a synonym for 'varE'; I guess it used to be something different and remains for backwards compatibility. Cheers, Daniel On 2011-April-17 Sunday 18:42:15 Alexey Karakulov wrote:
I'm interested if it's possible to use functions from some module without explicitly importing it. In ghci it's done on the fly, like this:
Prelude> Data.Map.empty Loading package array-0.3.0.2 ... linking ... done. Loading package containers-0.4.0.0 ... linking ... done. fromList []
But without gchi it seems impossible.
I have the file Test.hs:
{-# LANGUAGE TemplateHaskell #-} module Test where import Language.Haskell.TH
x :: ExpQ x = global $ mkName "Data.Map.empty"
When I load it in ghci, all works:
$ ghci -XTemplateHaskell Test.hs *Test> $x Loading package pretty-1.0.1.2 ... linking ... done. Loading package array-0.3.0.2 ... linking ... done. Loading package containers-0.4.0.0 ... linking ... done. Loading package template-haskell ... linking ... done. fromList []
But when I try to use it from other module, it fails. File Main.hs:
{-# LANGUAGE TemplateHaskell #-} module Main where import Test
main = do
print $x
$ runhaskell Main.hs
Main.hs:5:9: Not in scope: `Data.Map.empty' In the result of the splice: $x To see what the splice expanded to, use -ddump-splices In the first argument of `print', namely `$x' In the expression: print ($x)
-- All the best, Alexey
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (2)
-
Alexey Karakulov
-
Daniel Schüssler