8

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.)

Community
  • 1
  • 1
Petr
  • 62,528
  • 13
  • 153
  • 317

2 Answers2

4

There are tools for it:

Disclaimer: I have not tried any of these myself.

imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139
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.
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
  • A long time before TH and syb, I wrote so many program writing programs that I wrote a program writing program writing program. It read source code from a .rtf file and put the code at each different level based on the colour of the text! I'm not sure if I ever ported it from Gofer to Haskell, but it was fun anyway. – AndrewC May 19 '13 at 07:52
  • That is an option, yes, but my question asked specifically for a TH solution. – Petr May 21 '13 at 05:48