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
9
votes
2 answers

Shortening code by exploiting symmetry among multiple type class instances

Context I'm writing a Haskell module that represents SI prefixes: module Unit.SI.Prefix where Each SI prefix has a corresponding data type: data Kilo = Kilo deriving Show data Mega = Mega deriving Show data Giga = Giga deriving Show data Tera =…
9
votes
1 answer

QuasiQuote with arguments

I want to write a quotation in Haskell. name argument needs to be passed into gen function to generate a declaration. quote :: String -> QuasiQuoter quote name = QuasiQuoter { quoteExp = undefined, quotePat = undefined, …
Song Zhang
  • 315
  • 1
  • 5
9
votes
1 answer

A workaround for the “Template Haskell + C” bug?

I've got the following situation: Library X is a wrapper over some code in C. Library A depends on library X. Library B uses Template Haskell and depends on library A. GHC bug #9010 makes it impossible to install library B using GHC 7.6. When TH…
Emily
  • 2,577
  • 18
  • 38
9
votes
1 answer

Map identity functor over record

I have a record type like this one: data VehicleState f = VehicleState { orientation :: f (Quaternion Double), orientationRate :: f (Quaternion Double), …
Doug McClean
  • 14,265
  • 6
  • 48
  • 70
9
votes
2 answers

Template Haskell: How to extract the number of arguments of a function?

I have a function which given a Name of a function it augments it, yielding another function applied to some other stuff (details not very relevant): mkSimple :: Name -> Int -> Q [Dec] mkSimple adapteeName argsNum = do adapterName <- newName…
Alfredo Di Napoli
  • 2,281
  • 3
  • 22
  • 28
8
votes
1 answer

Template Haskell data declarations that derive Show

The following doesn't compile: import Language.Haskell.TH makeAlpha n = [d| data Alpha = Alpha $(conT n) deriving (Show, Read) |] I can't make out what the error means at all: Can't derive instances where the instance context mentions type…
user1002430
8
votes
2 answers

Polynomial factorization in Haskell

With hammar's help I have made a template Haskell bit which compiles $(zModP 5) to newtype Z5 = Z5 Int instance Additive.C Z5 where (Z5 x) + (Z5 y) = Z5 $ (x + y) `mod` 5 ... I'm now facing a problem that I don't think I can solve this way. A…
Xodarap
  • 11,581
  • 11
  • 56
  • 94
8
votes
1 answer

Using constraints in Typed Template Haskell

I would like to use typeclass constraints in my Typed Template Haskell snippets, but just can't get them to work: the instances seem to be missing inside the splice. Here is a standalone, minimized version of my code to demonstrate the issue. The…
Cactus
  • 27,075
  • 9
  • 69
  • 149
8
votes
1 answer

TemplateHaskell and IO

Is there any proper way to make TH's functions safe if they use side effects? Say, I want to have a function that calls git in compile time and generates a version string: {-# LANGUAGE TemplateHaskell #-} module Qq where import…
voidlizard
  • 805
  • 5
  • 10
8
votes
1 answer

Omitting Nothing/null fields in Haskell's Aeson

I have a type {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE MultiWayIf #-} import GHC.Generics import Data.Aeson.TH import Data.Aeson.Types data MyJSONObject = MyJSONObject { name :: String , ptype ::…
user4601931
  • 4,982
  • 5
  • 30
  • 42
8
votes
5 answers

How to write a monad that prints "step i of N" when executing each statement in the monad?

I'm not even sure this is possible in any kind of monad; does it violate monad laws? But it seems like something that should be possible in some kind of construct or other. Specifically is there any way to have something that I can write something…
lobsterism
  • 3,469
  • 2
  • 22
  • 36
8
votes
2 answers

Generating lenses for a "lens" library with a custom name processor instead of the default "underscore"-based one

The standard makeLenses implementation generates lenses for all the fields of a record which begin with underscore. I very much dislike the idea of having to introduce such an awkward naming convention to my records for many reasons. What I want to…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
8
votes
1 answer

How can I remove boilerplate when writing overloaded strings?

Given the following code: {-# LANGUAGE OverloadedStrings #-} newtype Firstname = Firstname String deriving (Eq, Show) instance IsString Firstname where fromString = Firstname newtype Lastname = Lastname String deriving (Eq, Show) instance IsString…
Darren
  • 2,888
  • 1
  • 23
  • 34
8
votes
1 answer

Determining implementation of method based on available constraints

Suppose I have to the following memoisation functions. (Ignore the fact that they are pure please.) memoEq :: Eq a => (a -> b) -> a -> b memoOrd :: Ord a => (a -> b) -> a -> b memoHash :: Hashable a => (a -> b) -> a -> b Now I want to…
Alessandro Vermeulen
  • 1,321
  • 1
  • 9
  • 28
8
votes
2 answers

How to create a non-TH package from code generated using Template Haskell?

I'm making a small package that defines wrappers for tuples and adds instances form them, like newtype Tuple2 a = Tuple2 { untuple2 :: (a, a) } deriving (...) tuple2 :: a -> a -> Tuple2 a tuple2 = ... instance Traversable Tuple2 where…
Petr
  • 62,528
  • 13
  • 153
  • 317