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
4
votes
2 answers

Haskell variant of template metaprogramming

I'm new to Haskell. Given the whole premise of Haskell is that a function will always return the same value, I'd expect there to be some way of e.g. calculating fibonacci values of constants at compile time, like I can do in C++ with template…
lobsterism
  • 3,469
  • 2
  • 22
  • 36
4
votes
1 answer

Loop and Recursion unrolling

W_t = M_t[i] if 0 <= t <= 15 W_t = ROTL_1(W_(t-3) XOR W_(t-8) XOR W_(t-14) XOR W_(t-16)) if 16 <= t <= 79 This is from the SHA-1 standards. In haskell what you would trivially do is write a recursive function to do this, but to make it more…
Satvik
  • 11,238
  • 1
  • 38
  • 46
4
votes
3 answers

GHC stage restriction (Template Haskell)

I could not figure out why I am getting "GHC stage restriction" in the following code: import Language.Haskell.TH rules :: [ExpQ] rules = [ [| \a -> a |], [| \_ -> 1 |] ] findTransforms :: Int -> [ExpQ] -> Bool findTransforms _ [] =…
krokodil
  • 1,326
  • 10
  • 18
4
votes
5 answers

Function dealing with finite but arbitrary number of heterogenous elements

I am working on a library to study game theoretic learning. In this setting, N agents are brought together and interact with an environment. Each agent derives a model of the interaction. The model of one agent depends on its N-1 opponents. I wrote…
Nicolas Dudebout
  • 9,172
  • 2
  • 34
  • 43
4
votes
1 answer

Can I rely on Template Haskell expansion (using -ddump-splices) to always generate valid code?

I have written a small utility to expand all TH splices in a Haskell module, so that I can use the haskell module even where TH is unavailable. To accomplish this, I pass the -ddump-splices option to GHC while compiling the module and capture the…
Anupam Jain
  • 7,851
  • 2
  • 39
  • 74
4
votes
1 answer

Processing standard quasiquote from external file in Haskell

I wanted to read an external Haskell source file for compile-time AST manipulation. How can I do that? I tried something like the following, but it didn't compile with the error message "TH.hs:15:12: Declaration splices are not permitted inside…
Hiro
  • 475
  • 4
  • 11
4
votes
1 answer

Get all functions/values in scope with template haskell

With template haskell, is there a way to list all functions in scope? Something like allVarsInScope :: Q [Name] What I'm trying to do with this is get a list of all imported functions beginning with test_, and run the tests automatically.
Clark Gaebel
  • 17,280
  • 20
  • 66
  • 93
4
votes
1 answer

Expression quasiquoter for an AST where one constructor produces a monadic computation?

In a very simplified sense, I have something like the following: type Runtime a = {- More or less a StateT on top of an Either monad -} -- The list of strings in Fn is a bunch of parameter names, the values of -- which are pushed into the state of…
Kben
  • 43
  • 4
4
votes
1 answer

Export template haskell generated definitions

My module contains definitions, part of which are exported (in module clause). I want to export Template Haskell-generated declarations too. But since there is seemingly no way to modify module clause with TH, I cannot do this. Is it possible to…
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
4
votes
2 answers

Compile time code rewriting outside of template haskell scope?

Is it possible to create a function which rewrites haskell code at compile time from outside of template haskell quotes? For example: differentiate :: Floating a => (a -> a) -> a -> (a,a) differentiate = -- what goes here? f :: Num a => a -> a f =…
ghorn
  • 614
  • 3
  • 11
4
votes
1 answer

How to include code in different places during compilations in Haskell?

Quasi-quotes allow generating AST code during compilations, but it inserts generated code at the place where Quasi-quote was written. Is it possible in any way to insert the compile-time generated code elsewhere? For example in specific module files…
Kostas
  • 2,434
  • 3
  • 19
  • 18
3
votes
0 answers

How to refer to a module (and construct the names of its contents) in template haskell

I have a splice I'm writing in TH which needs to take a list of module names and create new names based on a convention I have for these modules. At the moment I am doing this by passing in Strings of the module names, but I'm not thrilled by this…
GTF
  • 8,031
  • 5
  • 36
  • 59
3
votes
1 answer

How to force Stack to re-compile when a text file read using template haskell changes?

I'm currently writing a configuration for xmonad. I wanted to make a variable color theme config, so I made each color theme a type, eg data Dracula = Dracula, and a Color type class class ColorTheme a where colorWhite::a->String colorWhite =…
Kareem Taha
  • 107
  • 1
  • 6
3
votes
1 answer

How to examine a quoted data constructor name in Template Haskell?

I'm trying to learn some Template Haskell. As an exercise, I wrote a function that can generate things like isLeft and isRight (inspired by this question). Here's my humble attempt: isA connam = do ConE nam <- connam nn <- newName "p" …
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
3
votes
2 answers

What is the difference between single double qoute/apostrophe in template-haskell?

When learning about Haskell lenses with the Optics package, i encountered the following example: data Person = Person { _name :: String , _age :: Int } makeLenses ''Person makePrisms 'Person What does a value of type Name represent and what…
manews
  • 340
  • 2
  • 12