template-haskell

How do I make lenses from a record in GHCi

拥有回忆 提交于 2019-12-05 11:13:23
问题 I want to play around with the Lens library a bit. I've loaded it into GHCi and created a record data type with the appropriate underscores: > data Foo a = Foo {_arg1 :: Int, _arg2 :: [a]} I would like to make the lenses for Foo using the makeLenses template. I would like to do this without needing to read through the entire set of Template-Haskell docs. What incantation can I type in at the GHCi prompt to get this to work? 回答1: Tested in GHCi 7.8.3: :set -XTemplateHaskell :m +Control.Lens :{

Automatic derivation of Data.Vector.Unbox with associated type synonyms

蓝咒 提交于 2019-12-05 06:44:29
I have a datatype newtype Zq q = Zq (IntType q) where 'q' will be an instance of the class class Foo a where type IntType a and 'IntType' is just the underlying representation (i.e. Int, Integral, etc) associated with 'q'. I want to make Zq an instance of Data.Vector.Unbox . We are currently manually deriving Unbox using about 50 lines of trivial code as suggested in the link above. We will be making several different types 'Unbox' in our code, so writing 50 lines for each type is not appealing. I found two alternatives here . One alternative is to use this package which uses Template Haskell

Performing type equality in template haskell

荒凉一梦 提交于 2019-12-05 05:15:29
问题 I have a function in Template Haskell that extracts the type information for sum of record constructors as below: listFields :: Name -> Q ([[(String,Name,Type)]]) listFields name = do TyConI (DataD _ _ _ cons _) <- reify name let showClause (RecC conName fields) = (map (\(x,_,t) -> (nameBase $ x,x,t)) fields) return $ map showClause cons Given the type in there for a field, how do you compare equality of that type with a particular type like GHC.Base.String or Data.Text.Internal.Text ? I see

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

若如初见. 提交于 2019-12-05 03:11:38
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 result to be a Haskell 98 compatible package too. Is it possible to generate a piece of code using Template

Evaluating a function at compile time with Template Haskell

南笙酒味 提交于 2019-12-05 02:12:34
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 like: $(hString "hello, world") :: HashString I want the hash, and the text packing to happen at compile time. How do I do this? Here's what I've tried so far, but I'm not sure if its right, nor am I sure it does everything at compile time: hString :: String -> Q Exp hString s = [| HashString (hash $ T.pack s) (T.pack s) |] The way you've written your code, no evaluation will happen at compile

How to get rid of $(…) and [| … |] syntax when using a Template Haskell function?

 ̄綄美尐妖づ 提交于 2019-12-05 00:13:01
I'm trying to learn some Template Haskell. As an exercise, I wrote a function that can generate things like isLeft and isRight (inspired by this question ). Here's my humble attempt: isA connam = do ConE nam <- connam nn <- newName "p" lamE [varP nn] $ caseE (varE nn) [ match (conP nam [wildP]) ( normalB [| True |] ) [], match wildP ( normalB [| False |] ) [] ] The problem is that I have to write $(isA [| Left |]) instead of the more intuitive isA Left . Is it possible to get rid of the ugly syntax? I can't seem to find the answer in the documentation. The function only works with one-argument

Why doesn't Safe Haskell support Template Haskell?

天涯浪子 提交于 2019-12-04 22:47:11
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 subset of Haskell, and also restrict the resulting AST to this subset? A bit further down on the page you

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

你说的曾经没有我的故事 提交于 2019-12-04 20:33:37
问题 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 like do someOp () someOtherOp () thirdOp () and it would print step 1 of 3 step 2 of 3 step 3 of 3 Would this require Template Haskell or would a monad work? (And if Template Haskell is required, how to do it that way?) 回答1: I assume that you want

Code generation with Scala

大憨熊 提交于 2019-12-04 17:00:16
问题 When using the SBT toolchain in Scala, is it possible to write a task that will read a special part of the project's source to generate scala-code at compile time. Any ideas or even articles/tutorials on this? I am looking for something rather similar to Template Haskell. 回答1: treehugger.scala is a library designed for code generation. import treehugger.forest._ import definitions._ import treehuggerDSL._ val tree: Tree = Predef_println APPLY LIT("Hello, world!") println(tree) println

Data constructor in template haskell

橙三吉。 提交于 2019-12-04 02:32:05
I'm trying to create the ring Z/n (like normal arithmetic, but modulo some integer). An example instance is Z4: instance Additive.C Z4 where zero = Z4 0 (Z4 x) + (Z4 y) = Z4 $ (x + y) `mod` 4 And so on for the ring. I'd like to be able to quickly generate these things, and I think the way to do it is with template haskell. Ideally I'd like to just go $(makeZ 4) and have it spit out the code for Z4 like I defined above. I'm having a lot of trouble with this though. When I do genData n = [d| data $n = $n Integer] I get "parse error in data/newtype declaration". It does work if I don't use