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

Is it possible to compare two types, if one is assignable from the other?

Let's say I have two types: t1 <- [t| (Functor f) => (a -> b) -> f a -> f b |] t2 <- [t| (Int -> Char) -> [Int] -> [Char] |] Is it possible to determine in Template Haskell that an expression of t1 can also be of t2? (Without implementing type…
Petr
  • 62,528
  • 13
  • 153
  • 317
15
votes
1 answer

Is it possible to get a type of any expression using Template Haskell?

Given an expression foo, I could declare a top-level function bar = foo and get the type of foo as Type by reifying bar: case reify 'bar of VarI _ t _ _ -> t Is there a direct way of getting the type of foo, without creating the redundant…
Petr
  • 62,528
  • 13
  • 153
  • 317
15
votes
2 answers

Profiling Template Haskell

I have a TH-heavy file which takes around 30 seconds to compile. What are some techniques I can use to help debug the performance of my Template Haskell?
James Koppel
  • 1,587
  • 1
  • 10
  • 16
15
votes
3 answers

Code generation with Scala

When using the SBT toolchain in Scala, is it possible to write a task that will read a special part of the project's source to generate scala-code at compile time. Any ideas or even articles/tutorials on this? I am looking for something rather…
Lanbo
  • 15,118
  • 16
  • 70
  • 147
14
votes
0 answers

How to emit rewrite rules from Template Haskell

I wrote some TemplateHaskell that emits rewrite rules, but GHC (8.6.5) rejects my rules with the following error: Rule "mapKWith/Pure": Illegal expression: ((mapKWith @Pure) constraintProxy) in left-hand side: ((mapKWith @Pure)…
yairchu
  • 23,680
  • 7
  • 69
  • 109
14
votes
3 answers

Is it possible to iterate the application of a non-endomorphism?

In Haskell if I want to repeatedly apply an endomorphism a -> a to a value of type a I can just use iterate. What about a function that is not an endomorphisms, but generic enough to work correctly on its return type? Consider for example Just :: a…
marcosh
  • 8,780
  • 5
  • 44
  • 74
14
votes
1 answer

Haskell: Template Haskell and the scope

This code is compiled fine: data None = None { _f :: Int } type Simpl = Env type Env = Int However, I got an error with this code: {-# LANGUAGE TemplateHaskell #-} import Control.Lens data None = None { _f :: Int } type Simpl = Env makeLenses…
IruT
  • 323
  • 1
  • 8
13
votes
1 answer

Force pre-computation of a constant

I have a constant declaration in Haskell -- can I force this to be evaluated ahead of time? I'm seeing some code that looks roughly like, myList = [(a, b), (c, d)] ... map (f . fst) myList take time in the fst call when I profile it (it does have…
12
votes
1 answer

How to get rid of $(...) and [| ... |] syntax when using a Template Haskell function?

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
12
votes
0 answers

How can I write property tests for Template Haskell splices?

I always like to be able to write property tests for my code. When that code is Template Haskell, I'm mostly interested in the behavior of the generated code. Intuitively, I'd like to be able to write things like import Test.QuickCheck checkLift ::…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
11
votes
2 answers

How can I easily see the output from a Template Haskell statement?

I have the following Template Haskell code in my module, which is part of a larger application. $(derive makeFoldable ''JStatement) I suspect that the generated instance of Foldable is not exactly what I originally had in mind, but I can't find a…
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
11
votes
1 answer

Data constructor in template haskell

I'm trying to create the ring Z/n (like normal arithmetic, but modulo some integer). An example instance is Z4: instance Additive.C Z4 where zero = Z4 0 (Z4 x) + (Z4 y) = Z4 $ (x + y) `mod` 4 And so on for the ring. I'd like to be able to…
Xodarap
  • 11,581
  • 11
  • 56
  • 94
11
votes
0 answers

Using Template Haskell to add libraries with which to link

I'm currently hacking my way through trying to make quasiquotes for writing Rust code inline in Haskell. I think I have the code generation work done (including things like marshaling Haskell types to and from generated Rust ones). I now have the…
Alec
  • 31,829
  • 7
  • 67
  • 114
11
votes
2 answers

Is there a (Template) Haskell library that would allow me to print/dump a few local bindings with their respective names?

For instance: let x = 1 in putStrLn [dump|x, x+1|] would print something like x=1, (x+1)=2 And even if there isn't anything like this currently, would it be possible to write something similar?
Wizek
  • 4,854
  • 2
  • 25
  • 52
11
votes
2 answers

Can I use template haskell to define missing functions?

I've got a situation where I need to compile some Haskell code on different machines. At least one of these machines has a rather old version of Control.Concurrent.STM, that doesn't know modifyTVar. My current workaround is to copy the code for…
Jakob Runge
  • 2,287
  • 7
  • 36
  • 47
1
2
3
24 25