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

What is the Unit type?

Normally in Haskell, tuples of length one aren't allowed (AFAIK). However, when messing with Template Haskell, I got this: oneElementTuple = $(do{ x <- newName "x"; return $ LamE [VarP x] (TupE [Just (VarE x)]) -- one element…
fluffyyboii
  • 635
  • 6
  • 15
4
votes
0 answers

Lifting Existential datatypes or Data.Dynamic into Template Haskell?

The following introduction is provided to ensure you understand how I reached the problem (to not fall prey to the XY problem): I am working on a program which turns a parser in Parsec-like DSL into an actual LL(1) parser (and in the future…
Qqwy
  • 5,214
  • 5
  • 42
  • 83
4
votes
1 answer

`lift` a type into a Template Haskell `TypeQ`

If I have a value (of a type that is an instance of the Lift typeclass), I can use lift to create a Template Haskell representation of a term that evaluates to that value. Is there anything similar for types? To give a small example, suppose I…
Cactus
  • 27,075
  • 9
  • 69
  • 149
4
votes
1 answer

Haskell: Making Quasi-Quoted values strict / evaluated at compile-time

I have a 'Month' type, which is roughly newtype Month = Month Word8 where the Month constructor isn't exported; instead, a function mon :: Word8 -> Maybe Month mon i = if i > 0 && i < 13 then Just $ Month i else Nothing is…
user3416536
  • 1,429
  • 9
  • 20
4
votes
1 answer

Would it be possible to derive Data.Vector.Unbox via GHC's generic deriving?

It's possible to derive Storable via GHC's generic deriving mechanism: http://hackage.haskell.org/package/derive-storable (and https://hackage.haskell.org/package/derive-storable-plugin for performance). The only library I can find for deriving…
LogicChains
  • 4,332
  • 2
  • 18
  • 27
4
votes
0 answers

Why is there no representation for TH in TH?

Note that: Exp in template-haskell corresponds to HsExpr in the GHC AST Type in template-haskell corresponds to HsType in the GHC AST Dec in template-haskell corresponds to HsDecl in the GHC AST However, there is one set of constructors missing in…
Alec
  • 31,829
  • 7
  • 67
  • 114
4
votes
0 answers

Whole file Template Haskell error

I'm writing a TH library and I have a situation where I'm doing some module-wide stuff. In a nutshell, I'm collecting information from all quasiquotes and putQ-ing it into Q. Then, once the Haskell module has been typechecked, I look up all this…
Alec
  • 31,829
  • 7
  • 67
  • 114
4
votes
1 answer

Modify Haskell nested record by value

Say I have a nested structure as follows: data Bar = Bar { _id :: Integer, _bars :: [Bar] } data Foo = Foo { _bars :: [Bar] } And I have a Foo with a bunch of Bars with various ids: foo = Foo [Bar 1 [Bar 2], Bar 3 [Bar 4, Bar 5]] How do, perhaps…
clmn
  • 43
  • 2
4
votes
1 answer

How to call the quasiquoter for haskell syntax explicitly?

I'm building an eDSL on top of HaTeX. The problem I'm facing is that I want to display Haskell expressions in my LaTeX document and I want to use the same Haskell expression to help generate the document. The obvious answer is to copy and paste the…
simon1505475
  • 675
  • 3
  • 9
4
votes
1 answer

How can QuickCheck test all properties for each sample

...instead of generating 100 new random samples for each property? My testsuite contains the TemplateHaskell hack explained here [1] to test all functions named prop_*. Running the test program prints === prop_foo from tests/lala.lhs:20 === +++ OK,…
stefan
  • 674
  • 1
  • 5
  • 13
4
votes
1 answer

Replace record projection function with lenses

Almost every time I make a record, I find myself adding makeLenses ''Record (from lens) immediately afterwards, and I never end up actually using the projection functions that the record gives me. In fact, looking at what makeLenses produces (with…
Alec
  • 31,829
  • 7
  • 67
  • 114
4
votes
1 answer

Why are arbitrary sized tuples useful? (Template Haskell)

In the introductory text for Template Haskell one of the examples for why Template Haskell is useful is working with arbitrary sized tuples. What is the purpose of arbitrary sized tuples? If the data type is the same why not use a list? And if the…
user668074
  • 1,111
  • 10
  • 16
4
votes
1 answer

In TemplateHaskell, how do I figure out that an imported module has been renamed?

I am writing a bit of TemplateHaskell for stringing together QuickCheck style specifications. I require every module containing properties to export a symbol called ''axiom_set''. Then, my checkAxioms function finds all the ''axiom_set'' symbols…
simon1505475
  • 675
  • 3
  • 9
4
votes
1 answer

How to create a pattern match for Int in a function taking a Type?

I have a function that takes a Type: data MyType = IntT | BoolT | OtherT typeToMyType :: Type -> MyType How can I write a pattern that would state I want to match on the type of Int? What have I tried? 1 [t| GHC.Types.Int |] will create a value of…
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
4
votes
3 answers

Custom deriving(Read,Show) for enum type

Let's say I have this enumeration type: data TVShow = BobsBurgers | MrRobot | BatmanTAS and I want to define instances for Read and Show with the following behavior: show BobsBurgers = "Bob's Burgers" show MrRobot = "Mr. Robot" show BatmanTAS =…
Pubby
  • 51,882
  • 13
  • 139
  • 180