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

Template Haskell - How to lookup type operator name?

How do I lookup type operator name? This does not work: IssueTH.hs: {-# LANGUAGE TemplateHaskell #-} module IssueTH where import Language.Haskell.TH f :: Q [Dec] f = do Just n <- lookupTypeName "GHC.TypeLits.*" return [] Issue.hs: {-#…
Alexey Vagarenko
  • 1,206
  • 8
  • 15
2
votes
1 answer

Haskell-src-exts throws TemplateHaskell error

I'm trying to use the haskell-src-exts package to parse Haskell modules. Currently, I'm trying to parse the acme-io package's module, but I keep getting this error no matter what parse mode I try: *** Exception: fromParseResult: Parse failed at…
Athan Clark
  • 3,886
  • 2
  • 21
  • 39
2
votes
1 answer

GHC 7.10.2 warning when deriving Unbox instance using Data.Vector.Unboxed.Deriving

The following program: {-# LANGUAGE TemplateHaskell, RankNTypes, MultiParamTypeClasses, TypeFamilies #-} import Data.Vector.Unboxed import Data.Vector.Unboxed.Deriving import Data.Word data Pixel a = Pixel a deriving Show derivingUnbox "Pixel" …
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
2
votes
0 answers

Is there a way to find all typeclasses of type in Template Haskell

The question is in the title. Given a type, is there a way to find all the typeclasses it instantiates. I'm try to get the list of fields generated by makeField (lens).Example data Point = Point { _x :: Double, _y :: Double } deriving (Show). I…
mb14
  • 22,276
  • 7
  • 60
  • 102
2
votes
0 answers

How can I automate testing a Template Haskell function?

I defined a function using Template Haskell which generates a function definition given some type. The type is basically makeFunc :: Name -> Q [Dec] Right now, I use the -ddump-splices switch with GHC to see the generated splices. How can I…
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
2
votes
1 answer

Is it possible to create a function in Haskell which returns a list of the constructors for a data type?

Is it possible to create a function in Haskell which returns a list of the constructors for a data type? It should work like this: ghci> getConstructors Bool [True, False] ghci> getConstructors Maybe [Nothing, Just]
2
votes
2 answers

How can you have two records with the same field names?

I'm writing a JSON service for JIRA, and I've come across a requirement that conflicts with Haskell's namespace. I have this record data Assignee = Assignee {name :: Text} deriving Generic instance ToJSON Assignee This is dictated by what JIRA…
user1198582
2
votes
0 answers

Running Q Exp in a GhcMonad

I'm looking for something with a type similar to this: runQExpDynamically :: (GhcMonad m) => Q Exp -> m Dynamic Docs: GhcMonad, Q Exp, Dynamic, I'm looking for ways to combine two of my code examples; one for dynamically evaluating haskell code…
worldsayshi
  • 1,788
  • 15
  • 31
2
votes
2 answers

StateT with Q monad from template haskell

I would like to create a function that takes some declarations of type Dec (which I get from [d| ... |]) and modify them. Modifications will depend on previous declarations so I would like to be able to store them in map hidden in State monad -…
2
votes
1 answer

Use input from a custom quasiquoter as actual code

Lets say I have a custom quasiquote, named xpto: [xtpo|data Something = Abc | Def deriving (Eq,Ord,Show,Enum,Bounded)|] I want to actually declare this as it is, but additionally derive some more stuff based on the constructor names. That last part…
jcristovao
  • 554
  • 2
  • 12
2
votes
2 answers

How do you reify/walk a record definition in Haskell

Given an record (not an instance of the record, the record definition itself) such as: data Request = Expand { shortUrl :: [String] , hash :: [String] } | Shorten { longUrl :: String , domain ::…
haroldcarr
  • 1,523
  • 15
  • 17
2
votes
1 answer

Template Haskell - Static Assert

I've written the following function: staticAssert :: Bool -> Q [Dec] staticAssert cond = case cond of True -> return [] False -> fail "staticAssert failed" Basically this evaluates condition at compile time and if it is false causes a…
Clinton
  • 22,361
  • 15
  • 67
  • 163
2
votes
2 answers

How to design a library which can serve a Haskell module over network?

The task here is as follows : The client requests a function to be executed providing a name and it's arguments. The server executes the function with the provided arguments and returns the result. Something like this is fairly easy to implement…
Vamshi Surabhi
  • 437
  • 4
  • 15
2
votes
0 answers

When is the TemplateHaskell extension required in libraries providing TH deriving functions?

I was under the impression that using functions of type :: Name -> Q [Dec] had to be wrapped in a splice with the TemplateHaskell extension turned on, as e.g. documented in aeson: $(deriveJSON defaultOptions ''(,,,)) But I just noticed the lens…
jberryman
  • 16,334
  • 5
  • 42
  • 83
2
votes
3 answers

How to check whether instance exists for a polymorphic type with Template Haskell?

Suppose, I want to check whether an instance of Show exists for type [a] (which it does). If I do this: let t = ListT `AppT` VarT (mkName "a") $(stringE . show =<< isInstance ''Show [t]) I get the following error: Not in scope: type variable `a' In…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169