Qualified names in TH?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Qualified names in TH?

J. Garrett Morris-4
I'm trying to write some Template Haskell code that (among other
things) manipulates IntSets.  So, for example, I'd like a splice to
generate a call to Data.IntSet.fromList.  However, I'm not sure how
IntSet will be imported in the target module.  Is there a way to
resolve the fully qualified name (or similar) to a TH Name, without
having to know how it's imported in the splicing module?  (The obvious
approach---mkName "Data.IntSet.fromList"---seems not to work in GHC
7.8.)

Thanks!

 /g

--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Qualified names in TH?

Edward Kmett-2
Using {-# LANGUAGE TemplateHaskell #-} you can use 'foo and ''Foo to get access to the names in scope in the module that is building the splice, rather than worrying about what names are in scope in the module the code gets spliced into.

-Edward

On Mon, Mar 16, 2015 at 10:54 PM, J. Garrett Morris <[hidden email]> wrote:
I'm trying to write some Template Haskell code that (among other
things) manipulates IntSets.  So, for example, I'd like a splice to
generate a call to Data.IntSet.fromList.  However, I'm not sure how
IntSet will be imported in the target module.  Is there a way to
resolve the fully qualified name (or similar) to a TH Name, without
having to know how it's imported in the splicing module?  (The obvious
approach---mkName "Data.IntSet.fromList"---seems not to work in GHC
7.8.)

Thanks!

 /g

--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users


_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

RE: Qualified names in TH?

Simon Peyton Jones

What Edward says also applies to code quotations.  So, for example:

 

module M

import IntSet

 

f :: Q Exp -> Q Exp

f blah = [|  fromList $blah |]

 

module N where

     import M

     h x = $(f [| [x,x] |])

 

The splice expands to (fromList [x,x]), but the fromList guaranteed to be the fromList in scope where f is defined (in M), and NOT whatever other fromList might be in scope at the splice site in N.

 

Moreover, you do not need to import IntSet.

 

I hope that helps.  Would someone like to add a section to https://wiki.haskell.org/Template_Haskell, to explain?  There are several tutorials there as well.

 

Simon

 

From: Glasgow-haskell-users [mailto:[hidden email]] On Behalf Of Edward Kmett
Sent: 17 March 2015 05:01
To: J. Garrett Morris
Cc: GHC users
Subject: Re: Qualified names in TH?

 

Using {-# LANGUAGE TemplateHaskell #-} you can use 'foo and ''Foo to get access to the names in scope in the module that is building the splice, rather than worrying about what names are in scope in the module the code gets spliced into.

 

-Edward

 

On Mon, Mar 16, 2015 at 10:54 PM, J. Garrett Morris <[hidden email]> wrote:

I'm trying to write some Template Haskell code that (among other
things) manipulates IntSets.  So, for example, I'd like a splice to
generate a call to Data.IntSet.fromList.  However, I'm not sure how
IntSet will be imported in the target module.  Is there a way to
resolve the fully qualified name (or similar) to a TH Name, without
having to know how it's imported in the splicing module?  (The obvious
approach---mkName "Data.IntSet.fromList"---seems not to work in GHC
7.8.)

Thanks!

 /g

--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users

 


_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users