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

Haskell -- how to use the new 4-argument quasi quoter

It looks like the quasi quoter syntax has changed to now accept 4 arguments [ link ]. Has anyone used it yet? Thanks. I just want to build something really really simple, and the examples on the web won't work now. Thanks in advance.
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
3
votes
3 answers

Is there an easy way to quote a type with constrained parameters?

> {-# LANGUAGE TemplateHaskell #-} > import Language.Haskell.TH > import Control.Monad Let's say I have a class like Default > class Default a where > def :: a There's a straightforward way to define instances for types that also have a Monoid…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
3
votes
1 answer

Traversing a Template Haskell AST

I have gethered that Haskell code in template-haskell is not represented as a single AST, but rather four cross-referencing types of Pat, Exp, Dec and Type. I have also found no traversal facilities within the library, or anywhere else for that…
shayan
  • 1,211
  • 9
  • 12
3
votes
2 answers

How to capture type variable in TemplateHaskell quote

I'm trying to write a Lift instance that lifts not only the constructor, but also its type variables. For example, take Proxy a. I need a Lift instance such that, when lift (Proxy @Int) is spliced, GHC will correctly infer than the generated…
dcastro
  • 66,540
  • 21
  • 145
  • 155
3
votes
0 answers

Proper way to roundtrip record field declarations

Suppose you're writing some Template Haskell code that transforms record declarations. The first transformation you would want to write is the identity one, right? So let's go over the fields and not change them: module TH where import…
Cactus
  • 27,075
  • 9
  • 69
  • 149
3
votes
1 answer

Accessing record fields as 'Maybe' values in Haskell

In Haskell, if I specify field names for a type with a single constructor, the compiler should generate appropriate functions MyType -> fieldType. This breaks down if MyType has multiple constructors with different arities or types however. I want…
myc3lium
  • 31
  • 5
3
votes
1 answer

How to generate imports and boilerplate lists using Template Haskell?

I'd like to replace this boilerplate with code generation: import qualified Y15.D01 import qualified Y15.D02 import qualified Y15.D03 import qualified Y15.D04 import qualified Y15.D05 import qualified Y15.D06HM import qualified Y15.D06IO import…
oshyshko
  • 2,008
  • 2
  • 21
  • 31
3
votes
1 answer

Haskell analog of lisp backquoting and splicing

In some lisps (e.g. elisp, common lisp) there is a feature called backquoting. It allows to construct a list while evaluating or splicing into it some elements. For example: `(1 2 (3 (+ 4 5))) ⇒ (1 2 (3 (+ 4 5))) ; just quoted unevaluated…
grepcake
  • 3,960
  • 3
  • 16
  • 26
3
votes
1 answer

How can I write a pattern quasi quoter in Haskell?

I use quasi quoters to create my smart-constructed data types at compile time. This looks something like: import qualified Data.Text as T import Language.Haskell.TH.Quote (QuasiQuoter(..)) import Language.Haskell.TH (Q, Exp, Pat(..), Lit(..)) import…
MaxGabriel
  • 7,617
  • 4
  • 35
  • 82
3
votes
1 answer

TemplateHaskell seems to not be reporting what function it needs to be imported

This import: import Data.Singletons.TH ( FalseSym0, FromEnum, MaxBound, MinBound, PEq, PShow, ShowsPrec, ShowStringSym0, SShow, ToEnum, TrueSym0, sShowsPrec, sShowString …
Vanson Samuel
  • 1,953
  • 2
  • 17
  • 30
3
votes
0 answers

Turn a function into a precomputed lookup table without going through Template Haskell

I have the following CLaSH function: toBCD :: Word8 -> Vec 3 Word8 toBCD x = x `div` 100 :> (x `div` 10) `mod` 10 :> x `mod` 10 :> Nil Of course, the resulting HDL module is unsynthesizable because of the division by a non-power of…
Cactus
  • 27,075
  • 9
  • 69
  • 149
3
votes
1 answer

Template Haskell: generating code for a function signature with the type class constraints

I need to generate code for the function signature with type class constraints, for example: fun :: (Ord a) => a -> a I'm using the following signature constructor: SigD Name Type So, I need to generate a type. My best guess is to use the…
O.Phillips
  • 351
  • 1
  • 12
3
votes
0 answers

Non linear patterns in quasi-quotes

I followed this tutorial to implement a quasi quoted DSL, and I now want to support non-linear patterns in a quoted pattern. That will allow a repeated binder in a pattern to assert the equality of the matched data. For example, one can then write…
rem
  • 893
  • 4
  • 18
3
votes
3 answers

Lenses and TypeFamilies

I've encountered a problem of using Control.Lens together with datatypes while using the -XTypeFamilies GHC pragma. {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} import Control.Lens (makeLenses) class SomeClass t where data…
3
votes
0 answers

Using Template Haskell’s reifyRoles with poly-kinded datatypes?

I would like to find out information about the roles of a type constructor’s type parameters at compile-time. Fortunately, Template Haskell provides such a function, reifyRoles. However, it gives me odd results when I use it on poly-kinded…
Alexis King
  • 43,109
  • 15
  • 131
  • 205