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

Building Singleton types involving Text/Symbols using TemplateHaskell

I find that I can't use the functions from Data.Singletons.TH to create singletons for any types involving Text/Symbols in them. Demote Symbol = Text so this is clearly meant to be an intended use-case for singletons, but regardless of which one I…
dspyz
  • 5,280
  • 2
  • 25
  • 63
2
votes
1 answer

How to replicate the behaviour of 'name in a TH splice

Consider this Haskell file: {-# LANGUAGE TemplateHaskell #-} {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} module Text (main) where import Test.Inspection import Data.Text as T import Data.Text.Encoding as E import Data.ByteString…
Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139
2
votes
1 answer

Haskell macro to create a Map from their names to some expressions?

I have some variables a and b. I want to create the map Data.Map.fromList [("a",a),("b",b)] rapidly, by typing something like magic [a,b]. I want to do this live in GHCI, not from within a module. I spent some time learning Template Haskell for…
Jeffrey Benjamin Brown
  • 3,427
  • 2
  • 28
  • 40
2
votes
1 answer

Template Haskell: Generate Records

With Template Haskell I would like to generate records, eg: data MyRecordA = MyRecordA {fooA :: String, barA :: Bool} The uppercase A in MyRecordA, fooA, barA and the type Bool of the second field should be variable and specified by the caller…
Jogger
  • 1,617
  • 2
  • 12
  • 22
2
votes
1 answer

hamletFile, luciusFile, juliusFile variables not in scope

I'm building a simple application using Yesod and I'm having a hard time bringing in external files. Here is my code: {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE…
Pedro Hoehl Carvalho
  • 2,183
  • 2
  • 19
  • 25
2
votes
1 answer

Define a Recursive Function in Template Haskell

I want to implement a generic recursion operator for (at first simple) ADTs. (Simple means that only with constructors whose argument types are the defined one.) The general idea is to be able to use something as simple as $(recop ''Alg). It is easy…
Quirin F. Schroll
  • 1,302
  • 1
  • 11
  • 25
2
votes
2 answers

Initialize function by file contents at compile time

Is there a way to initialize a function someText :: Text which value will be stored in a file available at compile time? I thought I could use TH for that, but for now I just found embedFile :: FilePath -> Q Exp runQ :: Quasi m => Q a -> m a I can…
user2556165
2
votes
1 answer

TemplateHaskell class name with conflict newName

I have a TemplateHaskell function creating a class name: test :: Q [Dec] test = do clsname <- newName "A" a <- newName "a" return [ ClassD [] clsname [PlainTV a] [][] ] The classname is generated with newName, so should be conflict…
ondra
  • 9,122
  • 1
  • 25
  • 34
2
votes
1 answer

Generate a data declaration with TemplateHaskell

I wonder how to generate a bunch of constants based on a list of names. I started with this working example: ConstantCreation.hs module ConstantCreation where import Language.Haskell.TH createConstant :: String -> Q [Dec] createConstant…
Echologie
  • 83
  • 5
2
votes
1 answer

Is it possible GHCJS to reuse code generated by Template Haskell

At this moment GHCJS fails to compile postgresql-simple package (see [1]). I want to use persistent package to generate DB models. I wonder is it possible to compile models with GHC itself and re-use code generated by Template Haskell in GHCJS…
Geradlus_RU
  • 1,466
  • 2
  • 20
  • 37
2
votes
1 answer

How to replace `zs` in `[p| zs@(z:_) |]` with a name I generate myself?

If I run: > runQ [p| zs@(z:_) |] AsP zs_46 (InfixP (VarP z_47) GHC.Types.: WildP) I'd like to replace zs and z with names I generate myself. I can replace z: > let z = mkName "z" > runQ [p| zs@($(varP z):_) |] AsP zs_48 (InfixP (VarP z) GHC.Types.:…
rityzmon
  • 1,945
  • 16
  • 26
2
votes
2 answers

Type declarations in Template Haskell

Consider the following type (in Haskell): data CType = CInt | CDouble | CProduct String [(String, CType)] I would like to have a function that would generate corresponding Haskell type declarations. Constructors CInt and CDouble should correspond…
Katty J.
  • 686
  • 4
  • 11
2
votes
2 answers

Performing type equality in template haskell

I have a function in Template Haskell that extracts the type information for sum of record constructors as below: listFields :: Name -> Q ([[(String,Name,Type)]]) listFields name = do TyConI (DataD _ _ _ cons _) <- reify name let showClause…
Sal
  • 4,312
  • 1
  • 17
  • 26
2
votes
1 answer

How to auto derive FromJSON using Template Haskell, Aeson, and type families

I would like to use template haskell to auto generate the ToJSON (Bar Baz), or FromJSON (Bar Baz) instances. the deriveJSON is of type Options -> Name -> Q [Dec] how to I construct the Name type when the type takes parameters? {-# LANGUAGE…
whitehead1415
  • 435
  • 4
  • 14
2
votes
1 answer

Haskell, Gen instance of B when class A provides enough info for class B

While writing a class for a Collection/Container type (btw point me towards existing types if i'm reinventing the wheel) to provide a general interface for adding and removing elements from any 'Collection' type. class (Eq (c a), Monoid (c a)) =>…
jpostma1
  • 33
  • 6