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
1
vote
1 answer

How should I structure constrained parameters in Haskell?

I want to build a large schema in Haskell. The constituents take parameters and the parameters are constrained. As an example, I might decide that a circle takes one parameter called Radius, which is constrained to be non-negative. I will define…
AndyJost
  • 1,085
  • 1
  • 10
  • 18
1
vote
1 answer

Compile time constant in Haskell

I am trying to have precomputed data embedded in Haskell. That is catToMap li = Map.fromList $ zip [0..] li cat1 = catToMap ["aa", "bb", "cc"] dim = Map.size cat1 I would like to use dim statically in a type definition: type Network =…
Henricus V.
  • 898
  • 1
  • 8
  • 29
1
vote
1 answer

Stack is unable to resolve the correct package version

I am trying to use grenade in Haskell. Using stack as a configuration tool, I have # stack.yaml extra-deps: - diagrams-solve-0.1.1 - dual-tree-0.2.2 - SVGFonts-1.6.0.3 - diagrams-core-1.4.1 - diagrams-lib-1.4.2.2 - diagrams-postscript-1.4.1 -…
Henricus V.
  • 898
  • 1
  • 8
  • 29
1
vote
1 answer

Lens package: TH Deriving instances for datatype holding derived datatype

I have the following: data Dog = Dog { _x :: Int } makeFieldsNoPrefix ''Dog data Cat = Cat { _dog :: Dog } makeFieldsNoPrefix ''Cat This gives me a HasX and a HasDog class along with instances instance HasDog Cat Dog and instance HasX…
Charles Durham
  • 1,707
  • 11
  • 17
1
vote
1 answer

Making Cabal Aware of External TemplateHaskell Dependencies

To embed some static files in a build, I'm using: https://hackage.haskell.org/package/file-embed-0.0.10.1/docs/Data-FileEmbed.html It allows you to do: MyFile.hs myFile :: Data.ByteString.ByteString myFile = $(embedFile "something/external.txt") To…
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
1
vote
1 answer

How does one transform a data type into BSON in Haskell?

I've been trying to understand how to transform a Data type into a Document with Haskell's Data.Bson.Mapping package, however, I couldn't figure it out and the example in the docs didn't help much either. How should I resolve this problem? No…
Lukas Süsslin
  • 553
  • 1
  • 3
  • 12
1
vote
1 answer

How to call constructor from Template Haskell

I have function (let's call it mkSome) which constructs some data type with Template Haskell. It has typical signature Name -> Q [Dec]. Somewhere in its body I'm extracting constructors of another type with pattern-matching: case tyCons of DataD…
RandomB
  • 3,367
  • 19
  • 30
1
vote
0 answers

Using Template Haskell to derive classes

So, I found myself doing something like this: newtype T x = T x deriving (Eq, Ord, ...) And I thought this was annoying, so I'm thinking about making a package, using Template Haskell, that adds all the instances in base. Perhaps it could even be…
Clinton
  • 22,361
  • 15
  • 67
  • 163
1
vote
1 answer

Template Haskell Show instance not working

Does anyone know why this code: module Language.P4.UtilTest where import Language.P4.Util (mkShow) data Dummy = Bogus Char | Nonsense Int $(mkShow ''Dummy) is producing this error: Davids-Air-2:P4 dbanas$ stack ghc -- UtilTest.hs…
dbanas
  • 1,707
  • 14
  • 24
1
vote
0 answers

What is $sel in a Template Haskell Name?

I'm calling my TH function, called createRecordSplice in the following manner: data User = User {userFoo :: String, userBar :: Text} deriving (Eq, Show, Generic) createRecordSplice "user" ''User ['userBar] "NewUser" "nuser" Notice that the record…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
1
vote
1 answer

Structure a Haskell project for user-friendly handling of "plugins"

The question is how to structure a Haskell project in order to allow the user of a library to provide own plugins without having to mix her own code into the library's source code. Let me elaborate... Suppose I have a library called foolib…
mcmayer
  • 1,931
  • 12
  • 22
1
vote
1 answer

How to define a type in a TemplateHaskell function and use it in the same function?

Is there any way to have as single TH function, define a type, and use the type, as well? Relevant code below. PersonPoly2 is being defined by makeRecordSplice and then being passed to makeAdaptorAndInstance (from Opalaye), which is also a TH…
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
1
vote
1 answer

Write an = inside QuasiQuotes

How do I write a = inside a QuasiQuote expression? [uri|https://graph.facebook.com/me?fields=id,name,email|] Currently I get example/Facebook/test.hs:56:83: error: parse error on input ‘=’ Perhaps you need a 'let' in a 'do' block? e.g.…
Reactormonk
  • 21,472
  • 14
  • 74
  • 123
1
vote
1 answer

Is it possible to emit raw source code with Template Haskell?

Say I have a String (or Text or whatever) containing valid Haskell code. Is there a way to convert it into a [Dec] with Template Haskell? I'm pretty sure the AST doesn't directly go to GHC so there's going to be a printing and then a parsing stage…
Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
1
vote
1 answer

Apply type constructor to components of a record type

Given a fixed type constructor T, and a record type R, is there a mechanism to create a declaration for the record type whose components are those of R, but with T applied to the type. For example, with T being Maybe, and R, data Foo { bar ::…
bartfrenk
  • 103
  • 3
  • 4