retrie icon indicating copy to clipboard operation
retrie copied to clipboard

Rewrite TH splices

Open dmjio opened this issue 4 years ago • 3 comments

@xich, very cool package.

I'd like to rewrite TemplateHaskell splices (e.g. makeLenses ''Person), into function calls.

{- 
  data Person
  = Person
  { _age :: Int
  } deriving (Show, Eq)
-}

age :: Lens' Person Int
age = lens _age $ \record field -> record { _age = field }

Would retrie be a good candidate to support this operation? Right now I'm doing manual AST traversal / rewriting in a GHC plugin, seems a little heavy-handed.

dmjio avatar Mar 31 '20 16:03 dmjio

In principle, we can rewrite anything that GHC parses. In practice, I've never tried to rewrite TH splices before, so I'll have to build out the syntax.

I'm not sure I understand your example though. Do you want to replace the splice with the resulting code? Retrie isn't equipped to actually run the TH.

xich avatar Mar 31 '20 16:03 xich

Yes, just a mechanical replacement. No actual TH will be executed (TH would be omitted altogether). Use case here would be for cross-compilation. GHC has a requirement that TH splices are executed on the target rather than host (due to IO that could make assumptions about the environment). A real solution would be to GHC itself, to separate TH into pure and impure variants, where the former is always executed on the host and latter the target. But since we don't have that I'm forced to do AST rewriting.

dmjio avatar Mar 31 '20 17:03 dmjio

Can you create a test case that demonstrates the rewrite you want?

Example: https://github.com/facebookincubator/retrie/blob/master/tests/inputs/Adhoc.test

(Essentially this is a diff showing before and after, with command-line arguments above the ===.)

I think that would help me understand what needs adding, concretely.

xich avatar Apr 09 '20 04:04 xich