Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate final code from Template Haskell code

Problem

Is it possible to generate "pure" Haskell code out of the one including Template Haskell functions?

I want to get the code where all Haskell Template's qutations and splices are expanded? (to feed it into another Haskell compiler (Haste), which does not support Template Haskell yet.)

Example

module TupleReplicate:

tupleReplicate n = do 
    id <- newName "x"
    return $ LamE ([VarP id]) (TupE $ replicate n $ VarE id)

main:

main :: IO ()
main = do
    print $(tupleReplicate 3) "x"
    return ()

can be expanded to:

main :: IO ()
main = do
    print (\x->(x,x,x)) "x"
    return ()
like image 355
Wojciech Danilo Avatar asked Nov 03 '22 18:11

Wojciech Danilo


1 Answers

The solution using Template Haskell pretty printer can be found here: Preferred method for viewing code generated by Template Haskell

There are also other tools, as answered to a question here: How to create a non-TH package from code generated using Template Haskell?

The result of -ddump-splices is not always valid haskell code, it is only for the programmer.

like image 148
Boldizsár Németh Avatar answered Nov 10 '22 13:11

Boldizsár Németh