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

Haskell singletons : typelits package

I have a hard time convincing compiler that my types are correct. With regular Nats with Zero and Succ constructors it is pretty straightforward (the goal is to write replicate function for length-indexed lists (Vect)): replicate' :: SNat n -> a ->…
5
votes
1 answer

Dynamically build SQL-Queries wit Esqueleto and Template Haskell?

I'm writing a webapp wit Yesod & Persistent. I have a SQL-Database with several tables, containing characteristics of my "projects". I have a main table and for the Option with multiple values extra tables linked with the id. The user should be able…
Nudin
  • 351
  • 2
  • 11
5
votes
3 answers

Make Lenses (TH) with the Same Field Name using makeClassy

This question is regarding Edward A. Kmett's lens package (version 4.13) I have a number of different data types all of which have a field that denotes the maximum number of elements contained (a business rule subject to run time change, not a…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
5
votes
3 answers

HTF does not test props generated by TH

I want to do a number of similar tests on various types in my library. To simplify things, assume I have a number of vector types implementing Num class, and I want to generate the same QuickCheck property check prop_absNorm x y = abs x + abs y >=…
artem
  • 363
  • 1
  • 9
5
votes
2 answers

Type synonyms "not in scope" when using Template Haskell

I am getting a strange error about a data type being "not in scope" when using Template Haskell. Here is my Main.hs file: {-# LANGUAGE TemplateHaskell #-} module Main where import Control.Lens import Data.Aeson import Data.Aeson.TH type Foo =…
illabout
  • 3,517
  • 1
  • 18
  • 39
5
votes
1 answer

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| |] |] But this outputs (in my case): LamE [VarP…
Gareth Charnock
  • 1,166
  • 7
  • 17
5
votes
2 answers

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 a lot with different functions for f from within…
jek
  • 563
  • 2
  • 9
5
votes
2 answers

Generate final code from Template Haskell code

Problem Is it possible to generate "pure" Haskell code out of the one including Template Haskell functions? I want to get the code where all Haskell Template's qutations and splices are expanded? (to feed it into another Haskell compiler (Haste),…
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
5
votes
1 answer

Couldn't match kind `*' against `#'

What the heck is going on here: "Couldn't match kind `*' against `#'" I was trying the following in GHCi using TemplateHaskell (ghci -XTemplateHaskell) $(reify ''Show >>= dataToExpQ (const Nothing)) I was hoping to get an Exp out of this (which…
scravy
  • 11,904
  • 14
  • 72
  • 127
5
votes
1 answer

Working with a list in TemplateHaskell

Here's the tutorial I'm working from. He has an example, tupleReplicate, which returns a function that takes a value and replicates it: tupleReplicate :: Int -> Q Exp tupleReplicate n = do id <- newName "x" return $ LamE (VarP…
Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124
5
votes
0 answers

Creating a data type from a Name?

Possible Duplicate: Local variables in Template Haskell declarations I'm trying to construct a simple Template Haskell function that, given a string like "Foo", will construct the syntax tree for data Foo = Foo. Right now, I'm trying to do…
Venge
  • 2,417
  • 16
  • 21
4
votes
2 answers

Why does this Template Haskell work?

Consider this code: magic :: String -> Q Exp magic s = [e| putStrLn s |] Now, as best as I can tell, this shouldn't actually work. Inside the Oxford brackets, s is not in scope. And yet, the above apparently works perfectly. If we change this…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
4
votes
1 answer

Point-free style in Template Haskell

Consider the following Template Haskell function: composeQ :: ExpQ -> ExpQ -> ExpQ composeQ = \x y -> [| $(x) . $(y) |] Is it possible to eliminate the lambda expression from the right hand side of the equation and write composeQ using point-free…
4
votes
2 answers

Does Haskell have pointers/references to record members?

I can create and reference relative pointers to struct members in C++ using the ::*, .*, and ->* syntax like : char* fstab_t::*field = &fstab_t::fs_vfstype; my_fstab.*field = ... In Haskell, I can easily create temporary labels for record getters…
Jeff Burdges
  • 4,204
  • 23
  • 46
4
votes
1 answer

Haskell: How to tell if a type is an instance of class?

I'd like to create a Template Haskell function such that: $(isInstanceOf ''Read ''SomeType) will result in either True if SomeType is an instance of Read, and False otherwise. I tried to look at the result of reify and I think I'm looking for the…
user1002430