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

Function to output function name

Is it possible in Haskell to implement a function which returns its own function name? A possible type could be (a -> b) -> String.
Stuart Paton
  • 291
  • 4
  • 13
11
votes
2 answers

Is it possible to generate and run TemplateHaskell generated code at runtime?

Is it possible to generate and run TemplateHaskell generated code at runtime? Using C, at runtime, I can: create the source code of a function, call out to gcc to compile it to a .so (linux) (or use llvm, etc.), load the .so and call the function.…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
10
votes
1 answer

Haskell introspecting a record's field names and types

Based on a recent exchange, I've been convinced to use Template Haskell to generate some code to ensure compile-time type safety. I need to introspect record field names and types. I understand I can get field names by using constrFields . toConstr…
user1002430
10
votes
1 answer

Using Template Haskell, how can I splice the same type into multiple locations?

I'm defining instances of classes from vector-space for the OpenGL types, and to spare my typing muscles, I want to use Template Haskell to write a bunch of the instances for me. I started out small by defining function to derive instances for…
acfoltzer
  • 5,588
  • 31
  • 48
10
votes
3 answers

Splicing type signature in template haskell

I'm trying to create a type signature for a function in template haskell. Is there an easy way of doing this? I've done some workarounds to solve it in the meantime, but it should be easier, right? -- TH.hs module Lib.TH (mkFunction) where import…
Hashmush
  • 1,973
  • 2
  • 11
  • 12
10
votes
1 answer

What is the $() construct?

I have been trying to find in the Haskell reference the use of this: getHomeR = defaultLayout $ do setTitle "My Awesome Site" $(widgetFile "home") Specifically: $(widgetFile "home") I know that the $ operator gives precedence to whatever…
Luis Dos Reis
  • 393
  • 3
  • 15
10
votes
2 answers

Generate a function using Template Haskell

Is it possible to define a function using Template Haskell? For example convertStringToValue :: String -> Int convertStringToValue "three" = 3 convertStringToValue "four" = 4 I also have a Map [Char] Int. fromList [("five",5),("six",6)] How can I…
user2512324
  • 791
  • 1
  • 6
  • 21
10
votes
1 answer

What are the differences between inline-c and language-c-inline?

I've been briefly looking into quasi-quotation libraries for Haskell. These libraries allow Haskell to integrate with other languages. For integrating with C, there appears to be two packages with similar functionality: inline-c language-c-inline…
Steven Shaw
  • 6,063
  • 3
  • 33
  • 44
10
votes
1 answer

How to get the declaration of a function using `reify`?

Function reify allows me to look up information about a given name. For a function the returned value is VarI: data Info = ... | VarI Name Type (Maybe Dec) Fixity | ... Here I can examine the function's type, and I'd also like to examine its…
Petr
  • 62,528
  • 13
  • 153
  • 317
10
votes
6 answers

Generate a random string at compile time or run time and use it in the rest of the program

What would be the best way to do this? unsafePerformIO? Template Haskell? Something else? I have never used either of those so I don't know many of the details of using them. Note that the program will be compiled every time it is run, so it doesn't…
Drew
  • 12,578
  • 11
  • 58
  • 98
10
votes
2 answers

Is there a template haskell function for quoting?

I am playing with Template Haskell. I want to create a quasi quoter which allows me to create default initializers for records, i.e. something like [record| data Config = { shouldDoX = True; featureY :: Integer, optionZ = Nothing } |] should create…
scravy
  • 11,904
  • 14
  • 72
  • 127
10
votes
1 answer

Reify a module into a record

Suppose I have an arbitrary module module Foo where foo :: Moo -> Goo bar :: Car -> Far baz :: Can -> Haz where foo, bar, and baz are correctly implemented, etc. I'd like to reify this module into an automatically-generated data type and…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
9
votes
2 answers

How to circumvent GHC Stage Restriction?

I am writing a code generator whose output depends on datatype fields description which is stored in their class instances. However, I cannot find how to run a function with a TH-generated argument. {-# LANGUAGE TemplateHaskell, ScopedTypeVariables…
Boris
  • 614
  • 3
  • 10
9
votes
1 answer

Evaluating a function at compile time with Template Haskell

I am writing a simple HashString class, which is just a string and its hash: data HashString = HashString Int -- ^ hash T.Text -- ^ string! Now I'm trying to generate these at compile time with something…
Clark Gaebel
  • 17,280
  • 20
  • 66
  • 93
9
votes
3 answers

Template Haskell with record field name as variable?

I've got the following piece of code that implements a monad. I'm trying to use it to simplify the setting of fields with more complex logic later on. data Rec = Rec { alpha :: Int, beta :: Double, } deriving (Show) defaultRec = Rec 0 0…
user1002430
1 2
3
24 25