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

Generating a type annotation using DataKinds within a TH QuasiQuote

In a haskell project using template haskell, I am trying to generate an expression that has a type annotation as a phantom type. A simple example would be a situation with DataKinds and KindSignatures like: {-# LANGUAGE DataKinds, KindSignatures…
Matt Ahrens
  • 129
  • 7
3
votes
1 answer

Variables in Template Haskell custom quasiquoter

An example is worth a thousand words. Here is a pretty simple quasi quoter I just made up. import Language.Haskell.TH.Quote import Language.Haskell.TH.Syntax quoter :: QuasiQuoter quoter = QuasiQuoter { quotePat = parse } where parse ::…
Alec
  • 31,829
  • 7
  • 67
  • 114
3
votes
1 answer

Extract value environment from Q monad

In Template Haskell, the Q monad is where all the magic happens. However, Q has a pretty limited API. I would like to have a value of type valueNameStore :: Q (String -> Maybe Name) which basically captures the functionality lookupValueName ::…
Alec
  • 31,829
  • 7
  • 67
  • 114
3
votes
1 answer

How can I programatically produce this datatype from the other?

I'd like to use DSum for something. To work with DSum, you need to have a 'tag' type which takes one type argument, e.g. data Tag a where AFirst :: Tag Int ASecond :: Tag String However, I'd like to use this internally in a library. I want the…
ajp
  • 1,723
  • 14
  • 22
3
votes
2 answers

Adding an Ord instance to 'singleton' package generated naturals

I am using very simple type-level naturals generated with the singletons package. I am now trying to add an Ord instance to them. {-# LANGUAGE MultiParamTypeClasses, TemplateHaskell, KindSignatures, DataKinds, ScopedTypeVariables, GADTs,…
3
votes
1 answer

Is it possible to use template haskell to get the name of the current file?

The closest I can see is using reifyModule and thisModule, but that doesn't work. {-# LANGUAGE TemplateHaskell #-} import Language.Haskell.TH import Language.Haskell.TH.Syntax main = putStrLn $(LitE . StringL . show <$>…
Alex R
  • 2,201
  • 1
  • 19
  • 32
3
votes
0 answers

In TemplateHaskell, is there a way to get more accurate information about instances than what reifyInstance provides?

I'm currently writing a QuickCheck style library and relying on Template Haskell for generating a large number of test cases. I want to generate code like: quickCheck (prop_Num_plus_is_associative :: Integer -> Integer -> Integer -> Property) The…
simon1505475
  • 675
  • 3
  • 9
3
votes
1 answer

TemplateHaskell memory usage when compiling

I have a memory consumption problem when using TemplateHaskell in RuzzSolver, one of my Haskell project. Sources of RuzzSolver are available on GitHub . To achieve good performance, I load a ~380000 words dictionary into a Tree structure (from the…
zigazou
  • 1,725
  • 9
  • 13
3
votes
1 answer

Generate function definition with template haskell

How to write a template Haskell function such that: mkFunc "func" generates func = "func" I've tried this mkFunc x = ValD (VarP x) (NormalB (LitE (StringL x))) [] But it doesn't typecheck: Couldn't match type ‘Name’ with ‘[Char]’ Expected type:…
doofin
  • 508
  • 2
  • 13
3
votes
1 answer

Composite Primary Key in Yesod

I am fairly new to Haskell and have been experimenting with yesod for about a week now. I have been trying to connect to an existing database that has a composite primary key in sqlite. I managed to get the code to work with Database.Persist.Sqlite…
ulbrec
  • 65
  • 4
3
votes
1 answer

Yesod's TH generates incorrect code?

I'm making a Yesod subsite and am getting a type error in some Template Haskell-generated code: Yesod\DataSource\Data.hs:19:1: Couldn't match type `[Char]' with `Text' Expected type: () -> ([Text], [(Text, Text)]) -> Maybe (Route DataSource) …
Beerend Lauwers
  • 872
  • 7
  • 14
3
votes
1 answer

How can I splice in type context in a class instance declaration?

For example: let context = sequence [classP (mkName "Eq") [varT (mkName "a")]] in [d| instance $(context) => Bar (Foo a) where quux _ = undefined |] The result that I want is instance Eq a => Bar (Foo a) where quux _ = undefined,…
Sebastian Mendez
  • 661
  • 9
  • 18
3
votes
1 answer

Testing Template Haskell with mkName

I have a function that returns a dynamically-bound Type - in essence, ConT $ mkName "MyType". Of course, the actual function is significantly more complicated, enough so that I'd like to write tests for it, and preferably legible ones. But the…
user2141650
  • 2,827
  • 1
  • 15
  • 23
3
votes
1 answer

Including text files to embed with Cabal

I've got a library I'm building that includes a few very large strings using Template Haskell and file-embed. I'm wondering, how do I ensure these files are included when I run Cabal dist? The are all in a folder called core, but some are within…
jmite
  • 8,171
  • 6
  • 40
  • 81
3
votes
1 answer

Reassociating trees in Template Haskell AST's

I'm upgrading a library where I translate Haskell to another language. Right now I'm using Meta.Parse to read in a Haskell module, and get back its TemplateHaskell AST, as described here. The problem I'm running into is that, when I run the parse, I…
jmite
  • 8,171
  • 6
  • 40
  • 81