template-haskell

Export template haskell generated definitions

安稳与你 提交于 2019-12-10 07:00:11
问题 My module contains definitions, part of which are exported (in module clause). I want to export Template Haskell-generated declarations too. But since there is seemingly no way to modify module clause with TH, I cannot do this. Is it possible to specify that TH-generated declarations should be exported at all? Or maybe there are other ways to do this? 回答1: You need to export the names of the generated TH declarations. For example, if you have a TH function that generates a data B = C | D

Generate a function using Template Haskell

谁都会走 提交于 2019-12-09 15:19:41
问题 Is it possible to define a function using Template Haskell? For example convertStringToValue :: String -> Int convertStringToValue "three" = 3 convertStringToValue "four" = 4 I also have a Map [Char] Int . fromList [("five",5),("six",6)] How can I add functions convertStringToValue "six" = 6 convertStringToValue "five" = 5 at compile time using Template Haskell and the Map ? It appears quite silly to use Template Haskell for this purpose but I would like to know nevertheless. 回答1: You can do

TemplateHaskell and IO

我的未来我决定 提交于 2019-12-09 12:24:22
问题 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 System.Process import Language.Haskell.TH version = $( [| (readProcess "git" ["rev-parse", "HEAD"] "") |] ) the type of version is IO String. But version is completely free of side effects in runtime, it has side effects only in compile time. Is there any way to make

what's the correct way to have template haskell wrap a function with source information (e.g. line number)

只愿长相守 提交于 2019-12-07 06:45:44
问题 Suppose I start with a function fromJust Nothing = error "fromJust got Nothing!" fromJust (Just x) = x Then, I want to add source information via Template Haskell for better error messages. Let's imagine that I could add an extra parameter to the function fromJust' loc Nothing = error $ "fromJust got Nothing at " ++ (loc_filename loc) fromJust' loc (Just x) = x and then have some fromJust macro that I could use in source code like, x = $fromJust $ Map.lookup k m hack I did manage to hack it,

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

怎甘沉沦 提交于 2019-12-07 03:46:16
问题 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

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

試著忘記壹切 提交于 2019-12-06 19:04:53
问题 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

How can I programatically produce this datatype from the other?

痞子三分冷 提交于 2019-12-05 20:50:50
I'd like to use DSum for something. To work with DSum , you need to have a 'tag' type which takes one type argument, e.g. data Tag a where AFirst :: Tag Int ASecond :: Tag String However, I'd like to use this internally in a library. I want the interface that I expose to users to take any old datatype, e.g. data SomeUserType1 = Foo Int | Bar String it's clearly quite mechanical to go from this to the Tag a type given above. So, is it possible to do this in code, with some sort of generic programming techniques? Here's another example to be clear about the type of mapping I want to produce.

Data constructor in template haskell

家住魔仙堡 提交于 2019-12-05 15:41:23
问题 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|

Replace record projection function with lenses

早过忘川 提交于 2019-12-05 14:04:39
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, that I could get record projection functions to be straight up Van Laarhoven lenses instead? To be

what's the correct way to have template haskell wrap a function with source information (e.g. line number)

不想你离开。 提交于 2019-12-05 11:17:27
Suppose I start with a function fromJust Nothing = error "fromJust got Nothing!" fromJust (Just x) = x Then, I want to add source information via Template Haskell for better error messages. Let's imagine that I could add an extra parameter to the function fromJust' loc Nothing = error $ "fromJust got Nothing at " ++ (loc_filename loc) fromJust' loc (Just x) = x and then have some fromJust macro that I could use in source code like, x = $fromJust $ Map.lookup k m hack I did manage to hack it, by using quasiquotes and lifting the string of the source filename. It seems that Loc doesn't have a