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

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 around the web from reasonably recent blog posts and…
3
votes
2 answers

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 TypeA, b_L :: f TypeB, …
Gracjan Polak
  • 596
  • 3
  • 16
3
votes
0 answers

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 to shorten it {-# LANGUAGE CPP #-} #define…
Yuri Syro
  • 549
  • 2
  • 16
3
votes
2 answers

Read module from file in TemplateHaskell

So, most quasi-quoters for TemplateHaskell have an option where you can read in a quasi-quoted string from a file, instead of typing it in the brackets like [quoter|... some code ...|] . I'm wondering, is there an equivalent of this for the normal…
jmite
  • 8,171
  • 6
  • 40
  • 81
3
votes
1 answer

Record syntax for QuasiQuoter constructor

The documentation for template Haskell says that QuasiQuoter is defined as data QuasiQuoter = QuasiQuoter { quoteExp :: String -> Q Exp, quotePat :: String -> Q Pat, quoteType ::…
user782220
  • 10,677
  • 21
  • 72
  • 135
3
votes
1 answer

Compiling large data structures in Haskell

I have a CSV file with stock trading history, its size is 70 megabytes. I want to run my program on it, but do not want to wait for 30 seconds every start. 1. Just translate CSV file into Haskell source file like this: From |…
3
votes
1 answer

Trouble with Template Haskell stage restriction

I just start learning Template Haskell, and stuck on simple problem with splicing. In one module I've implemented function tupleN which replies N-th element of the tuple: tupleN :: Lift a => a -> Int -> Q Exp tupleN a n = do (TupE as) <- lift a …
Dmitry Bespalov
  • 5,179
  • 3
  • 26
  • 33
3
votes
1 answer

Haskell: I think I could really use Lisp-like macros here

The AI code for my little soccer game basically works like this: There is a function that derives facts that describe the current situation on the pitch: deriveFacts :: GameState -> [Fact] ... where the facts look kind of like this: data Fact = …
martingw
  • 4,153
  • 3
  • 21
  • 26
3
votes
1 answer

Use Shakespeare-text and external file

How can I convert the below example to use an external file instead of the embedded lazy text quasi quotes? {-# LANGUAGE QuasiQuotes, OverloadedStrings #-} import Text.Shakespeare.Text import qualified Data.Text.Lazy.IO as TLIO import Data.Text…
Adam Gordon Bell
  • 3,083
  • 2
  • 26
  • 53
2
votes
2 answers

Romans, rubies and the Haskell

Motivated by Romans, rubies and the D, I wanted to see if the same could be done in Haskell. module Romans where import Language.Haskell.TH import Language.Haskell.TH.Syntax import Data.Text num :: String -> String num s = rep $ pack s where …
Arlen
  • 6,641
  • 4
  • 29
  • 61
2
votes
2 answers

Dynamic dispatch, smart constructors, Template Haskell perhaps?

I'm looking at HaskellWiki > Existential type # Dynamic dispatch mechanism. And I'm thinking, there should be a way in Template Haskell to take this part: class Shape_ a where ... type Radius = Double data Circle = Circle Radius instance…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
2
votes
2 answers

How do I capture the named value created by this TH code

Here's my Template Haskell code objectGIDDeclaration :: String -> Integer -> Q [Dec] objectGIDDeclaration dnameSTR gid = pure fDec where dname = dnameSTR <> "GID" fDec = [fSig] Sig = ValD fname objectType [] fname = VarP (mkName dname) …
2
votes
1 answer

How can I write COMPLETE pragmas for types with many constructors?

Suppose I have a type with many constructors and a few pattern synonyms. I'd like to use pattern synonyms to replace a few of the constructors. How can I write the necessary COMPLETE pragma(s) without having to write out all the constructors by hand…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
2
votes
1 answer

Evaluation of template haskell in Yesod

While going through the examples of the Yesod Book, I'm running into an issue with the following snippet: {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} {-#…
wout
  • 2,477
  • 2
  • 21
  • 32
2
votes
0 answers

Best method for reading compile-time flags from within TemplateHaskell?

I want to conditionally compile a function based on the target platform. I have several versions: linux :: Q Dec linux = [d|f = f in f|] windows :: Q Dec windows = [d|f = f in f|] -- and so on I want to splice only the correct version into the…
lachrimae
  • 79
  • 7