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
8
votes
1 answer

Template Haskell: GHC stage restriction and how to overcome

I have the following code in a module: {-# LANGUAGE TemplateHaskell #-} module Alpha where import Language.Haskell.TH import Data.List data Alpha = Alpha { name :: String, value :: Int } deriving (Show) findName n = find ((== n) . name) findx…
me2
  • 3,933
  • 4
  • 17
  • 16
8
votes
2 answers

get function name inside it

I have a bunch of functions like: method1, method2, method3. For all of them there are HUnit test functions like: testMethod1, testMethod2, testMethod3. testMethod1 = TestCase $ assertEqual "testmethod1" ... testMethod2 = TestCase $ assertEqual…
7
votes
5 answers

Print and execute a string

I find myself writing a lot of code like putStr "foo (bar 1) (bar 2) =" print $ foo (bar 1) (bar 2) The trouble is, the printed message can get out of sync with the actual executed code. The obvious solution is to auto-generate this code. One way…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
7
votes
4 answers

Convert a String to a Type Constructor in Haskell

Does anyone know if there's a function in Haskell which does something like this: "Int" -> Int "String" -> String "Bool" -> Bool ie. it takes a string representation of a type constructor name, and converts it to the actual type constructor, both…
Jack
  • 2,153
  • 5
  • 28
  • 43
7
votes
1 answer

How can I use Template Haskell to build structures polymorphically?

I can write an instance -- In Data.Sequence.Internal instance Lift a => Lift (Seq a) where ... letting users lift fully realized sequences into splices. But suppose I want something a bit different, to build functions for creating…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
7
votes
1 answer

Serialization in Haskell

From the bird's view, my question is: Is there a universal mechanism for as-is data serialization in Haskell? Introduction The origin of the problem does not root in Haskell indeed. Once, I tried to serialize a python dictionary where a hash…
Sergey Sosnin
  • 1,313
  • 13
  • 30
7
votes
3 answers

Emitting warnings from Template Haskell splices

I know that I can cause a compile-time error by calling fail from a splice, but is it possible to only generate a warning? In particular I would like it to be possible to turn this warning into an error when compiling with -Werror. Essentially what…
hammar
  • 138,522
  • 17
  • 304
  • 385
7
votes
1 answer

Local variables in Template Haskell declarations

I'm reading through pozorvlak's baby steps post on Template Haskell in an attempt to understand it myself, and I came across this section: Recall that we were trying to programmatically produce declarations of the form data Fred = Fred. Let's try…
rampion
  • 87,131
  • 49
  • 199
  • 315
7
votes
1 answer

How to bring type information into value level in Haskell?

I'm looking for ways of way to bring type information into value level in Haskell. One way I know to represent any type information as a value is the Language.Haskell.TH.Type. Is there any way to implement a function that takes Proxy a and returns…
7
votes
1 answer

Can't find inerface-file declaration for variable

I'm trying to shift execution of some code to compile time with GHC 8 and template-haskel-2.11. The code looks like this: myThHelper :: FilePath -> Q Exp myThHelper path = runIO (compileThatFile path) >>= liftData This is a simplified version of…
Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
7
votes
1 answer

How to use getQ and putQ in template haskell?

I would like to handle states in Q monad with Template Haskell. According to some stack overflow's answers, there is a solution that uses unsafePerformIO, but I want to avoid using it as long as I can. I found getQ and putQ in the module…
kiripon
  • 71
  • 5
7
votes
0 answers

Normalize type family instance within Template Haskell splice

I'm using the genifunctors package to generate a functor instance for a type whose definition involves type families. The first module defines the data type itself: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE DataKinds…
vlopez
  • 594
  • 4
  • 9
7
votes
1 answer

Multiple function definitions with template haskell

Suppose I have a data type like so: data Color = Red | Blue | Green How would I generate a function like this using templatehaskell? myShow Red = ... myShow Blue = ... myShow Green = ... i.e. I'm looking for multiple definitions for a function…
Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124
7
votes
1 answer

Is there a way how to enumerate all functions in a module using Template Haskell?

While I can use reify to get information about most other syntactic constructs, I couldn't find anything that would give some information about a module.
Petr
  • 62,528
  • 13
  • 153
  • 317
7
votes
1 answer

Template Haskell error when using "deriving"

A little confused with the results I'm getting. Using the following: GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base…
me2
  • 3,933
  • 4
  • 17
  • 16