template-haskell

Replace record projection function with lenses

你说的曾经没有我的故事 提交于 2020-01-02 05:38:12
问题 Almost every time I make a record, I find myself adding makeLenses ''Record (from lens) immediately afterwards, and I never end up actually using the projection functions that the record gives me. In fact, looking at what makeLenses produces (with the GHC -ddump-splices flag), it looks like not even it uses those projection functions, except to choose a name for the lenses it produces. Is there some way, either through TemplateHaskell , or through a preprocessor, or frankly any other magic,

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

一笑奈何 提交于 2020-01-02 02:04:08
问题 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 ... instance Foldable Tuple2 where ... instance Functor Tuple2 where ... instance Applicative Tuple2 where ... This repeats from 2 to 15, so it looks like a job for Template Haskell. The generated code is always Haskell 98 compatible, so I'd like the final

Why doesn't Safe Haskell support Template Haskell?

╄→尐↘猪︶ㄣ 提交于 2020-01-02 00:17:10
问题 The documentation for Safe Haskell states: [...] Unfortunately Template Haskell can be used to subvert module boundaries and so could be used gain access to this constructor. [...] The use of the -XSafe flag to compile the Danger module restricts the features of Haskell that can be used to a safe subset. This includes disallowing unsafePerfromIO, Template Haskell,[...] Used as a macro system that translates an AST to another AST, should it not be possible to simply restrict TH to the safe

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

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 08:56:28
问题 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 (which uses language-c-quote) As I'm looking to build a quasi-quotation library of my own, I'm interested in design choices, API differences, performance etc. The only difference I'm aware of is that language-c-quote supports C and Objective-C,

Get a Haskell record's field names as a list of strings?

半城伤御伤魂 提交于 2020-01-01 04:11:08
问题 Say I have the following: data Rec = Rec { alpha :: Int, beta :: Double, phi :: Float } sample = Rec 1 2.3 4.5 I understand Template Haskell & the reify function can get me the record's field names. That is: print $(f sample) --> ["alpha", "beta", "phi"] There is also a claim that this can be done without Template Haskell. Can someone provide an example implementation for this can be accomplished? 回答1: It can be done with a Data (most GHC versions) or Generic (7.2.x and up) instance, which

Is it possible to use a bracketing syntactic sugar for an applicative functor?

被刻印的时光 ゝ 提交于 2020-01-01 01:59:32
问题 In McBride and Paterson's 'Applicative programming with effects' they introduce some lovely syntactic sugar for lifting a pure function: [| f x y z |] for f <$> x <*> y <*> z and I recall someone somewhere else using li f w x y z il or il f v w x y z li , and I thought/hoped that might be because it could be defined using some existing language feature and cunning definition of li and il . I can't find any reference to this beyond the paper, and assuming that [| and |] aren't likely to turn

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

拟墨画扇 提交于 2019-12-30 03:22:12
问题 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. Is a similar thing possible with Template Haskell? 回答1: Yes, it's possible. The GHC API will compile Template Haskell. A proof-of-concept is available at https://github.com/JohnLato/meta-th, which, although not very sophisticated, shows one general

Existential quantifier silently disrupts Template Haskell (makeLenses). Why?

限于喜欢 提交于 2019-12-24 03:09:10
问题 I have this file: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE ExistentialQuantification #-} module Toy where import Control.Lens data Bar = Bar { _barish :: String } data Foo = forall a. Show a => Foo { _fooish :: a } $(makeLenses ''Bar) $(makeLenses ''Foo) x = barish y = fooish and I get the following error message: Toy.hs:15:5: Not in scope: `fooish' Perhaps you meant `_fooish' (line 9) This is my first time attempting to use existential quantifiers; I have no idea why this combination of

Syntax error on 'mod' Haskell [closed]

荒凉一梦 提交于 2019-12-23 12:28:59
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . 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 + 1 I have that function in a .hs file, i fire up ghci in the same directory

Type synonyms “not in scope” when using Template Haskell

对着背影说爱祢 提交于 2019-12-23 10:49:31
问题 I am getting a strange error about a data type being "not in scope" when using Template Haskell. Here is my Main.hs file: {-# LANGUAGE TemplateHaskell #-} module Main where import Control.Lens import Data.Aeson import Data.Aeson.TH type Foo = Bar data Baz = Baz $(deriveJSON defaultOptions ''Baz) -- $(makeLenses ''Baz) data Bar = Bar main :: IO () main = print "hello" When trying to compile it, I get the following error: test-0.1.0.0: configure Configuring test-0.1.0.0... test-0.1.0.0: build