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 result to be a Haskell 98 compatible package too. Is it possible to generate a piece of code using Template Haskell and make a package from it that doesn't use TH itself? (I'd prefer an automated way, if possible.)


回答1:


There are tools for it:

  • EvilSplicer (removed from sources on 13 Oct 2018), written by Joey Hess before he discovered
  • zeroth, which does not seem to be in active development any more.

Disclaimer: I have not tried any of these myself.




回答2:


It's always possible to just write a Haskell program that outputs Haskell source code as an ordinary text file. You can then compile that like any other file.

What this doesn't give you, of course:

  • Syntax checking. (I.e., the code you generate might contain syntax errors.)
  • The ability to inspect hand-written code already compiled.
  • The ability to use other GHC features like type inference.


来源:https://stackoverflow.com/questions/16621582/how-to-create-a-non-th-package-from-code-generated-using-template-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!