Questions tagged [template-haskell]

Template Haskell is a GHC extension to Haskell that adds compile-time meta-programming facilities. This allows users to write programs that generate or modify their program at compile time: a form of compile-time macros.

You can learn more about it here:

375 questions
270
votes
6 answers

What's so bad about Template Haskell?

It seems that Template Haskell is often viewed by the Haskell community as an unfortunate convenience. It's hard to put into words exactly what I have observed in this regard, but consider these few examples Template Haskell listed under "The Ugly…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
260
votes
1 answer

Getting associated type synonyms with template Haskell

Can Template Haskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the necessary information. It works for getting…
72
votes
1 answer

Using TemplateHaskell to list all names in a namespace

I want a TemplateHaskell function variablesInScope :: Q [Name] that returns a list of the Name's of all the variables in scope. TemplateHaskell obviously has this information available in order to implement functions like reify :: Name -> Q Info…
Mike Izbicki
  • 6,286
  • 1
  • 23
  • 53
71
votes
2 answers

How to properly communicate compile-time information to Template Haskell functions?

I need to communicate some information from compile scripts into Template Haskell. Currently the compile scripts keep the information in the system environment, so I just read it using System.Environment.getEnvironment wrapped in runIO. Is there a…
Petr
  • 62,528
  • 13
  • 153
  • 317
51
votes
1 answer

Is it possible to generate comments to functions in Template Haskell?

In our project we have a lot of TH-generated functions. It'd make sense to add generic comments to them so that they are visible in Haddock/Hoogle. At the very least, something like "This has been generated by TH." Is something like that possible?
Petr
  • 62,528
  • 13
  • 153
  • 317
45
votes
3 answers

Preferred method for viewing code generated by Template Haskell

As you know, Template Haskell is used to generate various kinds of AST splices programmatically at compile-time. However, a splice can often be very opaque, and it is often difficult to discern what a splice actually generates. If you run the Q…
dflemstr
  • 25,947
  • 5
  • 70
  • 105
28
votes
4 answers

Boilerplate-free annotation of ASTs in Haskell?

I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "annotation" to certain nodes, such as tail-calls, etc. I…
27
votes
1 answer

Template Haskell: reify in GHCi

Is it somehow possible to do reify in GHCi? When I try it using 'runQ' it complains "can not do reify in the IO monad". >>> runQ (reify ''Bool) Template Haskell error: Can't do `reify' in the IO monad *** Exception: user error (Template Haskell…
scravy
  • 11,904
  • 14
  • 72
  • 127
20
votes
3 answers

Is there any Template Haskell tutorial for someone who doesn't know Lisp?

I wanted to learn Template Haskell but all tutorials I find either assume that you learned lisp and know what lisp macros are, or that you know some cs theory jargon - things as splices, quasiquotations, etc... - or some theoretical results about…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
20
votes
1 answer

Template Haskell: Is there a function (or special syntax) that parses a String and returns Q Exp?

I am trying to learn a bit of Template Haskell and Quasi Quotation, and I am looking for a function that takes a String and parses it to Q Exp, so the type is: String -> Q Exp Tried searching hoogle, but the results I saw had to do with lifting…
Wizek
  • 4,854
  • 2
  • 25
  • 52
20
votes
3 answers

Is it possible to use a bracketing syntactic sugar for an applicative functor?

In McBride and Paterson's 'Applicative programming with effects' they introduce some lovely syntactic sugar for lifting a pure function: [| f x y z |] for f <$> x <*> y <*> z and I recall someone somewhere else using li f w x y z il or il f v w…
AndrewC
  • 32,300
  • 7
  • 79
  • 115
19
votes
2 answers

GHC -ddump-splices option — Template Haskell

I'm following the Yesod book, which states: But by using the -ddump-splices GHC option, we can get an immediate look at the generated code. A much cleaned up version of it is: How would I do this? I've tried compiling my file with ghc…
Matthew H
  • 5,831
  • 8
  • 47
  • 82
18
votes
1 answer

Get a Haskell record's field names as a list of strings?

Say I have the following: data Rec = Rec { alpha :: Int, beta :: Double, phi :: Float } sample = Rec 1 2.3 4.5 I understand Template Haskell & the reify function can get me the record's field names. That is: print $(f sample) -->…
user1002430
18
votes
1 answer

How do I make lenses from a record in GHCi

I want to play around with the Lens library a bit. I've loaded it into GHCi and created a record data type with the appropriate underscores: > data Foo a = Foo {_arg1 :: Int, _arg2 :: [a]} I would like to make the lenses for Foo using the…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
17
votes
1 answer

Why doesn't Safe Haskell support Template Haskell?

The documentation for Safe Haskell states: [...] Unfortunately Template Haskell can be used to subvert module boundaries and so could be used gain access to this constructor. [...] The use of the -XSafe flag to compile the Danger module restricts…
user239558
  • 6,964
  • 1
  • 28
  • 35
1
2 3
24 25