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.

template-haskell
Filter by
Sorted by
Tagged with
2 votes
0 answers
64 views

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 { ...
mb14's user avatar
  • 22.5k
2 votes
0 answers
86 views

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 ...
Frerich Raabe's user avatar
0 votes
0 answers
89 views

error define a template haskell function

I am beginning to learn Template haskell, so I want create a function that receive a String param that define the name to the function to generate build_p5 :: String -> [Dec] build_p5 name = [ ...
oriaj's user avatar
  • 778
10 votes
2 answers
844 views

Generate a function using Template Haskell

Is it possible to define a function using Template Haskell? For example convertStringToValue :: String -> Int convertStringToValue "three" = 3 convertStringToValue "four" = 4 I also have a Map [...
user2512324's user avatar
10 votes
1 answer
847 views

What are the differences between inline-c and language-c-inline?

I've been briefly looking into quasi-quotation libraries for Haskell. These libraries allow Haskell to integrate with other languages. For integrating with C, there appears to be two packages with ...
Steven Shaw's user avatar
  • 6,163
1 vote
1 answer
100 views

How does one "run" a data declaration from a quasiquoter

I'm trying to write a quasiquoter for some type declarations. I've written something along the lines of {-# LANGUAGE TemplateHaskell #-} import Language.Haskell.TH as TH import Language.Haskell.TH....
Khanzor's user avatar
  • 4,870
2 votes
1 answer
163 views

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> ...
Netsu's user avatar
  • 365
0 votes
1 answer
208 views

Dynamically add routes at compile time in Scotty

Is it possible to add routes dynamically at compile time through a config file by using Template Haskell or any other way. Scotty has a function addRoute but I want to use it dynamically. Example ...
user2512324's user avatar
6 votes
1 answer
72 views

Is this expected behavior of Template Haskell?

Can anyone tell why this code doesn't compile data A = A { _b :: B } makeLenses ''A type B = String with message Not in scope: type constructor or class B and this does: type B = String data ...
Alexey Vagarenko's user avatar
1 vote
1 answer
52 views

Why does Template Haskell add unexpected parens?

I have the following: import Control.Applicative import Control.Monad import Language.Haskell.TH mkExp :: [Name] -> ExpQ mkExp (name:[]) = [| ZipList $(varE name) |] mkExp (name:names) = [| ...
Patrick Collins's user avatar
3 votes
1 answer
356 views

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 ...
ulbrec's user avatar
  • 65
1 vote
1 answer
42 views

Missing imports from Hamlet libraries

This is the code snippet from O reilly - Yesod - Widgets, getRootR = defaultLayout $ do setTitle "My Page Title" toWidget [lucius| h1 { color: green; } |] addScriptRemote "https://ajax....
user avatar
3 votes
1 answer
77 views

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],...
Beerend Lauwers's user avatar
-2 votes
1 answer
134 views

give (without duplicates) the names of actors who have co-starred in at least one film with a particular actor

I'm writing a movie database with Haskell. Like the title says, I am trying to give (without duplicates) the names of actors who have co-starred in at least one film with a particular actor. Here's ...
Tasos Barlas's user avatar
3 votes
1 answer
82 views

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 ...
Sebastian Mendez's user avatar
6 votes
3 answers
513 views

Calculate N-Ary (with different types !!) Cartesian Product in Haskell

I know that the function sequence can handle the [[1, 2], [3, 4]] -> [[1, 3], [1, 4], [2, 3], [2, 4]] problem. But I think the real cartesian product should handle the ([1, 2], ['a', 'b']) -> [(...
luochen1990's user avatar
  • 3,737
3 votes
1 answer
90 views

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 ...
user2141650's user avatar
  • 2,847
1 vote
1 answer
69 views

Obtaining TH.Name for '[] without -XTemplateHaskell

Is there a way to obtain (import from base modules or write expression) a value of type Language.Haskell.TH.Name that represents '[] without enabling -XTemplateHaskell? A good reason to do so is that ...
Helmut Grohne's user avatar
1 vote
1 answer
123 views

Automatically declaration yesod handlers using Template Haskell

For example, I have next entity type in Model User json username Text and following Haskell types: Entity User Delete handler for User: Routes file: /users/#UserId UserR DELETE Handler ...
Bet's user avatar
  • 389
4 votes
1 answer
173 views

does Template Haskell name quoting desugar 'x to NameG?

Can I always expect the single single-quote syntax to desugar to the NameG constructor? e.g. does 'x always desugar to (Name (OccName "x") (NameG VarName (PkgName "some-package") (ModName "...
sam boosalis's user avatar
  • 1,997
9 votes
1 answer
1k views

QuasiQuote with arguments

I want to write a quotation in Haskell. name argument needs to be passed into gen function to generate a declaration. quote :: String -> QuasiQuoter quote name = QuasiQuoter { quoteExp = ...
Song Zhang's user avatar
1 vote
1 answer
423 views

Use Template Haskell to generate instance recursively

In GenericPretty, there is an Out class with a default implementation by using GHC.Generic magic. As you can see that I defined Person data type, and if I want to implement Out class I have to write ...
Song Zhang's user avatar
28 votes
4 answers
2k views

Boilerplate-free annotation of ASTs in Haskell?

I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "...
Joey Eremondi's user avatar
3 votes
1 answer
262 views

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 ...
Joey Eremondi's user avatar
2 votes
2 answers
183 views

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 ...
user avatar
3 votes
1 answer
175 views

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 ...
Joey Eremondi's user avatar
2 votes
0 answers
80 views

Running Q Exp in a GhcMonad [duplicate]

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 ...
worldsayshi's user avatar
  • 1,881
9 votes
1 answer
453 views

A workaround for the “Template Haskell + C” bug?

I've got the following situation: Library X is a wrapper over some code in C. Library A depends on library X. Library B uses Template Haskell and depends on library A. GHC bug #9010 makes it ...
Emily's user avatar
  • 2,636
72 votes
1 answer
1k views

Using TemplateHaskell to list all names in a namespace

I want a TemplateHaskell function variablesInScope :: Q [Name] that returns a list of the Name's of all the variables in scope. TemplateHaskell obviously has this information available in order to ...
Mike Izbicki's user avatar
  • 6,336
3 votes
1 answer
622 views

Template Haskell error when trying out simple singletons example

With both Singletons 1.0 and github master (as of e8a7d6031c) against ghc 7.8.3 I get the following error testing out some simple singletons examples both from Richard Eisenberg's presentation and ...
faultywasps's user avatar
5 votes
1 answer
75 views

How do I write a quasi quoter in terms of another quasi quoter

If I'm working with a third party quasi-quoter, for example thirdParty :: QuasiQuoter, and I want to write my own in terms of this quasi-quoter, how do I do this? In ghci I tried runQ [| [thirdParty| ...
Gareth Charnock's user avatar
7 votes
0 answers
251 views

Normalize type family instance within Template Haskell splice

I'm using the genifunctors package to generate a functor instance for a type whose definition involves type families. The first module defines the data type itself: {-# LANGUAGE TypeFamilies #-} {-# ...
vlopez's user avatar
  • 594
51 votes
1 answer
551 views

Is it possible to generate comments to functions in Template Haskell?

In our project we have a lot of TH-generated functions. It'd make sense to add generic comments to them so that they are visible in Haddock/Hoogle. At the very least, something like "This has been ...
Petr's user avatar
  • 63.1k
5 votes
2 answers
906 views

Functions as arguments to be used in template haskell quote

This is partially a followup to Lift instance for a function?. However, the answer there is to either globally define the function or to rewrite it inside the quotation. However, we will be using foo ...
jek's user avatar
  • 563
4 votes
2 answers
275 views

Embedding long strings inline without using lists

For several projects I need to embed long strings into Haskell source code. The obvious way to do this is to unlines a list of lines. However, reading and maintaining this is cumbersome. cCode :: ...
Uli Köhler's user avatar
  • 13.4k
1 vote
1 answer
127 views

Can template-haskell be used to generate quasi-quotes?

A project that I'm currently working on makes extensive use of persistent. Instead of persistent's quasi-quoted syntax to specify models, I would like to use json. Right now, I use a script to ...
Vamshi Surabhi's user avatar
9 votes
1 answer
560 views

Map identity functor over record

I have a record type like this one: data VehicleState f = VehicleState { orientation :: f (Quaternion Double), orientationRate :: ...
Doug McClean's user avatar
  • 14.4k
4 votes
0 answers
130 views

Lifting abstract data type

Suppose there's a handy library for parsing things. It exports a function parseThing along with some types and helpers: module Text.Thing.Parser ( Thing () , parseThing , ParseError -- .....
nameless's user avatar
  • 1,979
3 votes
2 answers
195 views

Fieldwise distribution of Monoid, Applicative, Monad etc for Haskell records

Is there a package that does roughly the following: Given a record: data R = R { a :: TypeA, b :: TypeB, c :: TypeC } derives a lifted record: data R_L f = R_L { a_L :: f ...
Gracjan Polak's user avatar
16 votes
1 answer
366 views

Is it possible to compare two types, if one is assignable from the other?

Let's say I have two types: t1 <- [t| (Functor f) => (a -> b) -> f a -> f b |] t2 <- [t| (Int -> Char) -> [Int] -> [Char] |] Is it possible to determine in Template ...
Petr's user avatar
  • 63.1k
4 votes
1 answer
149 views

Is there an error in this quasi-quoter expression?

I'm trying to compile some example code from Network.JMacroRPC.Snap: module Main where import Network.JMacroRPC.Snap import Snap.Http.Server import Snap.Core import Language.Javascript.JMacro import ...
rampion's user avatar
  • 88.2k
4 votes
1 answer
7k views

Syntax error on 'mod' Haskell [closed]

I am following a haskell tutorial: http://www.seas.upenn.edu/~cis194/lectures/01-intro.html I am testing functions in ghci, i got to this part: hailstone :: Integer -> Integer hailstone n | n `...
leshow's user avatar
  • 1,598
11 votes
2 answers
257 views

Can I use template haskell to define missing functions?

I've got a situation where I need to compile some Haskell code on different machines. At least one of these machines has a rather old version of Control.Concurrent.STM, that doesn't know modifyTVar. ...
Jakob Runge's user avatar
  • 2,293
3 votes
0 answers
223 views

How to shorten `import qualified`?

I want to sort my imports by name, qualified and unqualified import A import qualified B import C import qualified D But there are big gaps in code, and lines are long, so I want ...
Yuri Syro's user avatar
  • 546
0 votes
1 answer
174 views

Template Haskell: Stage Error

I'm trying to understand how the Quasi Quoter generates TH structures. So I'm trying to convert the first example from Template Meta-programming for Haskell, from a quoted format to just the types. ...
Andrew Rademacher's user avatar
2 votes
2 answers
362 views

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 ...
kylo_el's user avatar
  • 23
2 votes
1 answer
65 views

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 ...
jcristovao's user avatar
4 votes
0 answers
383 views

Implementation tips for whole-program static analysis for Haskell

As part of a research project on property-based testing, I need to do static whole-program analysis of Haskell programs. I'm looking for suggestions on how to implement whole-program analysis of ...
Brad Larsen's user avatar
1 vote
0 answers
147 views

Why Template Haskell uses AST

I've been playing a bit with TemplateHaskell and I came across this problem, which is basically you can't do keyword name = [d| data $(name) = $(name) |] You have to do something like keyword ...
mb14's user avatar
  • 22.5k
7 votes
1 answer
166 views

Multiple function definitions with template haskell

Suppose I have a data type like so: data Color = Red | Blue | Green How would I generate a function like this using templatehaskell? myShow Red = ... myShow Blue = ... myShow Green = ... i.e. I'...
Vlad the Impala's user avatar

1
4
5
6 7 8