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

Generate InstanceD declaration that contains type families

I am writing a function deriveMyTypeClass ::Q [Dec] where, given the name of a type, I am walking over it's constructors and instancing a typeclass I wrote based on the structure. My type class looks like: class MyTypeclass a where type Foo a f…
0
votes
1 answer

Template Haskell on Aeson

I have a data type like this: module My.Module data A = A { aFoo :: Integer } deriving (Generic, Show) And I have generic option for Aeson import Data.Char ( toUpper, toLower ) genericOptions :: String -> Options genericOptions prefix =…
autumn322
  • 447
  • 3
  • 10
0
votes
1 answer

Quasiquoter concrete syntax for visible type application

In the following snippet, the first argument to foo, SNat @n, is assembled manually: [e|foo $(appTypeE (conE 'SNat) n')|] where n' = litT . numTyLit . fromIntegral $ n Is there concrete TH quasiquoter syntax for this? I.e. I'd like to write…
Cactus
  • 27,075
  • 9
  • 69
  • 149
0
votes
1 answer

Discovering more typeclass instances

When I was new to Haskell I had very hard time finding instances for various types. Motivated by this, much later I noticed reifyInstances. While I know little about Template Haskell, it seems that that we can discover more instances than usual…
sevo
  • 4,559
  • 1
  • 15
  • 31
0
votes
1 answer

Module Frames - TableTypes does not work

I am a data scientist familiar with languages such as R and python. I have been trying to learn haskell for two months. There is a module people employ to deal with frames in haskell similarly to common packages in those other languages (tidyverse…
0
votes
0 answers

How to use a custom PersistField in TemplateHaskell model file?

I have the following model in config/models: Post json title Text intro Text posted UTCTime default=now() published Bool default=true content Markdown Maybe cvId CvItemId Maybe projectId ProjectId Maybe I have…
Ben
  • 1,561
  • 4
  • 21
  • 33
0
votes
1 answer

Frames library can only find 1 column of my CSV file

Using the following .csv file: Desc,Status "CT3","undone" I have written the following program: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE QuasiQuotes #-} module Main…
user1023733
  • 805
  • 5
  • 14
0
votes
1 answer

Declaring an Opaleye table without using TemplateHaskell

The opaleye basic tutorial gives an example on how to use user defined types in record types and queries: data Birthday' a b = Birthday { bdName :: a, bdDay :: b } type Birthday = Birthday' String Day type BirthdayColumn = Birthday' (Column PGText)…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
0
votes
1 answer

Is it possible to generate record fields in haskell?

Suppose I have a prefix p and list of strings ss. I would like to generate a record field name, p ++ s, for each of the strings s in ss. data Example = Example { field1 :: String , field2 :: String -- the rest of the fields are generated ,…
Wilfred
  • 799
  • 2
  • 16
  • 26
0
votes
1 answer

Haskell: makeLenses - data constructor not in scope

I have this simple code here to define a type and use makeLenses to generate lenses. module Api.Jira.Types.Search ( SearchRequest(..) ) where import GHC.Generics import qualified Data.Text as T import Data.Aeson (FromJSON, ToJSON) import…
theduke
  • 3,027
  • 4
  • 29
  • 28
0
votes
0 answers

Template Haskell: how to rewrite this?

I have this pSizeOf function which takes a type param and returns the size of that type as type-level literal: import Foreign.Storable import Language.Haskell.TH import qualified Data.Kind as K pSizeOf :: forall (t :: K.Type). (Storable t) =>…
Alexey Vagarenko
  • 1,206
  • 8
  • 15
0
votes
0 answers

Is there a way to lift Template Haskell Names?

Since Name has a Show instance and strings can be used to generate a corresponding Name, I thought about using the following method: \ name -> [e| mkName $(lift (show name)) |] This works fine in ghci. But in compiled code, the actual compiled…
Theelepel
  • 193
  • 6
0
votes
1 answer

Generate FFI call using Template Haskell

In the Frames library, one has a readTable function, that generates a record type based on a CSV file. Is it possible to generate a call like foreign import ccall unsafe "// c code" c_foo :: Int -> IO ()? I've tried to accomplish so using a [d| ...…
Nick Tchayka
  • 563
  • 3
  • 14
0
votes
0 answers

How to use a Name list in a Template Haskell function?

I'd like to parameterize the template so that the user could give a list Name's defining methods. The following example tries to be minimal. There is a data D1 that contains fields, unknown in advance (to the template). Users wants to derive an…
Gspia
  • 809
  • 6
  • 15
0
votes
1 answer

Having difficulty understanding type inference in Haskell

In Haskell, we know that if we have some function f with the type signature f :: a -> a, then Haskell can infer the following types: f "alpha" will have type [Char]; f 1234 will have type Num a => a f Just will have type a -> Maybe a and so on. With…
1 2 3
24
25