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

Defining TH Lift instances for algebraic data types

Suppose I have an algebraic data type with multiple constructors, like data Animal a = Mouse a | Beaver a | Rabbit a How would I create a Lift instance effectively? The easiest way of doing so would be instance (Lift a) => Lift (Animal a) where …
David
  • 8,275
  • 5
  • 26
  • 36
7
votes
1 answer

Standalone deriving declaration in Template Haskell quotation

Why Template Haskell ignores standalone deriving declaration in quotation? {-# LANGUAGE TemplateHaskell, StandaloneDeriving #-} data Test a = Test a $([d| deriving instance Show a => Show (Test a); f x = x |]) ghci> :l Test.hs [1 of 1] Compiling…
leventov
  • 14,760
  • 11
  • 69
  • 98
7
votes
1 answer

Is there a way of deriving Binary instances for Vinyl record types using Derive and Template Haskell or otherwise

I have been trying out the Vinyl package, which uses type level kinds to create record structures with field level polymorphism and automatically provided lenses. Both of these features would be very handy to my project, as the former allows for…
Vic Smith
  • 3,477
  • 1
  • 18
  • 29
7
votes
1 answer

How to use Template Haskell to get the body of function?

Currently I'm trying to do a translation from a Haskell subset without having to deal with all the parsing, typechecking etc. issues. Documentation didn't help me to figure out a function to get the function's body (all the definitions) by its…
polkovnikov.ph
  • 6,256
  • 6
  • 44
  • 79
7
votes
1 answer

Why does Template Haskell allow arbitrary IO operations during compilation?

As I'm trying to learn about TH I found out that it allows arbitrary IO actions during compilation (see What's so bad about Template Haskell?). This seems quite dangerous to me. Why is that? Is it really necessary for some tasks? Or is it just a…
Petr
  • 62,528
  • 13
  • 153
  • 317
7
votes
2 answers

Why does TemplateHaskell cause GHC to load packages?

I have a trivial Template Haskell program that prints the name of the current module (Main, here): {-# LANGUAGE TemplateHaskell #-} module Main ( main ) where import Language.Haskell.TH import Language.Haskell.TH.Syntax modName ∷ String modName =…
tsuraan
  • 240
  • 1
  • 3
7
votes
2 answers

Automatic derivation of Data.Vector.Unbox with associated type synonyms

I have a datatype newtype Zq q = Zq (IntType q) where 'q' will be an instance of the class class Foo a where type IntType a and 'IntType' is just the underlying representation (i.e. Int, Integral, etc) associated with 'q'. I want to make Zq an…
crockeea
  • 21,651
  • 10
  • 48
  • 101
6
votes
2 answers

How does Langage.Haskell.TH.report work?

Unfortunately, many Template Haskell functions have absolutely no documentation at all. One such function is report. It takes a Bool and a String, and produces a compilation error with the specified string as the error message. Does anybody have any…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
6
votes
1 answer

How to get the literal value of a TemplateHaskell named variable

If I have a Name in TemplateHaskell and want to find out the value of the variable that it names, provided that the variable is declared as a literal, can this be done? var = "foo" -- Can `contentsOf` be defined? $((contentsOf . mkName $ "var") >>=…
dflemstr
  • 25,947
  • 5
  • 70
  • 105
6
votes
2 answers

Template Haskell type quoting problems

The TemplateHaskell quoting documents two quotes ('') as the way to get the Name of a type: > ''String GHC.Base.String This works fine for this type (name). However, I can't find a way to make it work nice for e.g. Maybe String: > ''Maybe String --…
iustin
  • 343
  • 1
  • 11
6
votes
1 answer

Generating TExp using Template Haskell

I'm just getting started with template haskell. I've written a function that takes a function a -> [b] and generates an expression for a function b -> a: {-# OPTIONS_GHC -Wall -Wextra -Werror #-} module Surjection where import…
rampion
  • 87,131
  • 49
  • 199
  • 315
6
votes
1 answer

When does cabal recompile a module which contains Template Haskell?

I understand that cabal will recompile a module if the interface of any of its dependencies has changed. It seems that this simple rule does not hold if the module contains Template Haskell. In that case, even just adding a trailing newline…
tarleb
  • 19,863
  • 4
  • 51
  • 80
6
votes
1 answer

Compile-time checked URIs

I want to make an expression such that I have a compiletime error or a URI. [uri|http://stackoverflow.com|] should compile, but [uri|foo:/bar:\|] should not. I've come across QuasiQuotes, which are apparently for this kind of problem. However, I…
Reactormonk
  • 21,472
  • 14
  • 74
  • 123
6
votes
1 answer

Deriving Read Instances for GADTs

How can I automatically derive a Read instance for this GADTs: {-# LANGUAGE GADTs, StandaloneDeriving #-} data TypeDec a where TypeDecInt :: TypeDec Int TypeDecString :: TypeDec String deriving instance Show (TypeDec a) data Bar where …
homam
  • 1,945
  • 1
  • 19
  • 26
6
votes
1 answer

Generate dynamic Name in template haskell using the current scope

I'm writing a template haskell splice, and am struggling to generate the right kind of Names. If I want to generate a known name (say, a function f), I can use 'f. This requires f to be in scope where I'm defining the splice, not where it's used,…
Erik Hesselink
  • 2,420
  • 21
  • 25