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

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 "SomeModule"))) This information must always be there,…
sam boosalis
  • 1,997
  • 4
  • 20
  • 32
4
votes
2 answers

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 :: String cCode = unlines [ "int main(int…
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
4
votes
0 answers

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 -- ... ) where -- ... data Thing = {-…
nameless
  • 1,969
  • 11
  • 34
4
votes
1 answer

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 Control.Concurrent import…
rampion
  • 87,131
  • 49
  • 199
  • 315
4
votes
1 answer

Syntax error on 'mod' Haskell

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 `mod` 2 == 0 = n `div` 2 | otherwise = 3*n +…
leshow
  • 1,568
  • 2
  • 15
  • 30
4
votes
0 answers

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 Haskell programs, hopefully without building lots of…
Brad Larsen
  • 995
  • 1
  • 12
  • 20
4
votes
1 answer

Running template haskell in template haskell

insertST :: StateDecoder -> SomeState -> Update SomeState SomeThing insertST stDecoder st = ... the stuff in StateDecoder can't be used in $(makeAcidic ''SomeState ['insertST]) but if I declare a state and wrap it like this ... myDecoder ::…
TallerGhostWalt
  • 464
  • 3
  • 11
4
votes
0 answers

How to pass Parameters to ghci by use of TemplateHaskell?

I use ghc with TemplateHaskell. Somebody told me, that ghc itself uses ghci during compilation time to implement TemplateHaskell. Is there a way to pass parameters to ghci which may differ from the parameters used in ghc? Reason for my question: I…
Stephan Kulla
  • 4,739
  • 3
  • 26
  • 35
4
votes
2 answers

Specification of default ToJson format used by Aeson

Does anybody know where I can find documentation on how ADTs are translated to Json by Aeson's ToJSON? I'm using Haskell for a backend application, and I'm trying to write the JSON decoder for another functional language on the front end, so I'd…
jmite
  • 8,171
  • 6
  • 40
  • 81
4
votes
2 answers

Writing recursive template haskell functions

Is it possible to transform a recursive TH function into an equivalent form which will compile? The following definition doesn't work, because in order to compile fact you must first compile fact. fact :: ExpQ -> ExpQ fact n = [| case $n of …
user2407038
  • 14,400
  • 3
  • 29
  • 42
4
votes
1 answer

compiling a shared object written in haskell and template haskell and linking it with main in c

i am trying to compile several literate haskell (.lhs) files to a shared object (.so), and then to link it with a main written in c. The issue here, though, is that 2 of the files used to create the .so are template haskell. i followed the rules for…
4
votes
1 answer

Compiling a String into an Exp with TemplateHaskell

I'm looking for a way to compile a string of a valid Haskell expression code into a TH Exp. E.g., if there existed an appropriate function, I'd expect it to behave the following way: > $(theFunctionImLookingFor "\a -> a + 1") 2 3 I've already…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
4
votes
1 answer

How to allow embedding not only values but arbitrary haskell expressions in antiquotes

Recently, I have learned how to implement quasiquoters with antiquote capabilities, like printfQ in the following piece of code: main = do let itemName = "apple" price = 1.29 [printfQ| The price of #{itemName} is #{price}. |] The…
nushio
  • 753
  • 3
  • 12
4
votes
1 answer

Template Haskell names of declarations as strings

Say I have this: f x = x + 1 tt2 name o = sequence [valD (varP (mkName name)) (normalB [| f $(varE o) |]) []] I'd like to convert tt2 to tt: tt name o = [d| ??? = f $(varE o) |] I cannot figure out what ??? should be. This is probably because I…
me2
  • 3,933
  • 4
  • 17
  • 16
4
votes
1 answer

How do I use TemplateHaskell's addDependentFile on a file relative to the file being compiled?

I want that my TemplateHaskell expression, which uses IO and depends on the file MyDependency.txt, is recomputed when that file is being changed. Therefore I am using addDependentFile "MyDependency.txt" to tell ghc to check that file for…
nh2
  • 24,526
  • 11
  • 79
  • 128