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

What manner of Haskell syntax is used in [$parseRoutes|/ Home GET|]?

I found this code on the front page of the Yesod project: import Yesod data HelloWorld = HelloWorld mkYesod "HelloWorld" [$parseRoutes|/ Home GET|] instance Yesod HelloWorld where approot _ = "" getHome = applyLayout [$hamlet|%h1 Hello World|] main…
qomp
  • 61
  • 1
6
votes
1 answer

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 A = A { _b :: B } makeLenses ''A Without…
Alexey Vagarenko
  • 1,206
  • 8
  • 15
6
votes
3 answers

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']) -> [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')] problem, and…
luochen1990
  • 3,689
  • 1
  • 22
  • 37
6
votes
2 answers

Is there a nice(r) way of writing this Template Haskell code involving singleton data types?

I've just started to use Template Haskell (I've finally got a use case, yay!) and now I'm cognitively stuck. What I'm trying to do is generating a singleton datatype declaration of the form data $V = $V deriving (Eq,Ord) starting from a name V…
yatima2975
  • 6,580
  • 21
  • 42
6
votes
1 answer

Splicing arbitrary expressions in a Haskell quasiquoter

Reading through Why It’s Nice to be Quoted, in section 3 there's an example of splicing a variable identifier in a quasiquote. subst [:lam | $exp:e1 $exp:e2 |] x y = let e1' = subst e1 x y e2' = subst e2 x y in [:lam |…
6
votes
1 answer

Template Haskell and Implicit Parameters

Is there a way to create functions with implicit parameters or let bindings with implicit parameters using template haskell? I.e. is it possible to generate a signature like this using template haskell: doSomething :: (?context :: Context) => m…
scravy
  • 11,904
  • 14
  • 72
  • 127
6
votes
2 answers

Can Template Haskell generate multi-param typeclass instances?

The latest (2.8.0.0) definition for the Dec has the following instance constructor: InstanceD Cxt Type [Dec] Seems that only one type can be instantiated. Is there a way to work around this?
bfops
  • 5,348
  • 5
  • 36
  • 48
5
votes
2 answers

How to avoid extra indentation in Template Haskell declaration quotations?

I have a toy program: $ cat a.hs main = putStrLn "Toy example" $ runghc a.hs Toy example Let's add some Template Haskell to it: $ cat b.hs {-# LANGUAGE TemplateHaskell #-} id [d| main = putStrLn "Toy example" |] $ runghc b.hs b.hs:3:0: parse error…
dave4420
  • 46,404
  • 6
  • 118
  • 152
5
votes
1 answer

How to collect values spread throughout a Haskell codebase

I have a web application written in Haskell (using ghcjs on the client side and ghc on the server side) and I need a way to collect the CSS values which are spread throughout the modules. Currently I use a technique involving a CssStyle class and…
David Fox
  • 654
  • 4
  • 10
5
votes
2 answers

How to dynamically call a function which defined in multiple modules in the same signature

I've defined a lot of functions (say, 100+), each of which do a specific work but with the same signature. That is something like: module R001 (run) where run = module R002 (run) where run = What I wanna do is to provide the…
claude
  • 237
  • 1
  • 8
5
votes
2 answers

what's the correct way to have template haskell wrap a function with source information (e.g. line number)

Suppose I start with a function fromJust Nothing = error "fromJust got Nothing!" fromJust (Just x) = x Then, I want to add source information via Template Haskell for better error messages. Let's imagine that I could add an extra parameter to the…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
5
votes
0 answers

Is there a function/library/technique to help generate correct/non-redundant contexts for TH generated instances?

Is there a a function that can take a given context/[Pred] and simplify it? Some examples for what I mean by that: If the context contains Eq [a] replace it with Eq a (because it implies the more complicated constraint) If the context contains Eq…
yairchu
  • 23,680
  • 7
  • 69
  • 109
5
votes
2 answers

Extracting context for tracing/logging via haskell meta programming

In our haskell code base, business logic is interlaved with tracing and logging code. This can obscure the business logic and make it harder to understand and debug. I am looking for ideas how to reduce the code footprint of logging and tracing to…
cvogt
  • 11,260
  • 30
  • 46
5
votes
1 answer

Wai template functions cannot find Libz.so

: can't load .so/.DLL for: libz.so (libz.so: cannot open shared object file: no such file or directory) This is the error I'm getting while trying to install some of the WAI…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
5
votes
1 answer

What is code introspection in haskell?

In the yesod book there is a paragraph: Template Haskell is essentially Haskell which generates a Haskell Abstract Syntax Tree (AST). There’s actually more power in TH than that, as it can actually introspect code. We don’t use these facilities in…
Qwertie
  • 5,784
  • 12
  • 45
  • 89