Opened 22 months ago

Last modified 22 months ago

#14474 new feature request

reify RHS of "value" variable

Reported by: dailectic Owned by:
Priority: normal Milestone:
Component: Template Haskell Version: 8.2.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


According to the documentation, when reifying value variables "returning the RHS has not yet been implemented because of lack of interest". I'd like to formally request interest since I don't see a ticket here (may have missed it).

My motivating example is to make source available for documentation and better error messages. Something like:

printSource :: Name -> Q Exp
printSource n = do
    VarI _ _ (Just dec) <- reify n
    lift $ pprint dec

foo x = x * 2
fooSource = $(printSource 'foo) -- === "\x_0 -> x_0 GHC.Num.* 2"

How significant of a change is this? I could take a pass at it if pointed to the relevant bits, having not contributed to GHC before.

Change History (4)

comment:1 Changed 22 months ago by goldfire

I don't think this will be so easy.

GHC doesn't store the RHS of definitions in a convenient form to be reified into TH. Specifically, when GHC does store the RHS (only for inlinable definitions), it stores it in Core, not Haskell. So part of this work would be translating Core back to Haskell (not impossible).

It actually might make a nice project... but the TH AST you get out at the end might not look much like what the user typed in originally.

comment:2 Changed 22 months ago by dailectic

Actually I wouldn't mind just having access to core. Aren't definitions "inlinable" within their own module, or is intra-module inlining handled totally differently? It seems reasonable to only work where core is normally in scope.

comment:3 Changed 22 months ago by goldfire

The problem is that Core doesn't match up with TH syntax. I suppose it really just could return Core, without translating to the TH AST.

I'm not sure off the top of my head if all local definitions are inlinable. It wouldn't be hard to experiment and find out, though. GHC also supports cross-module inlining, so there is a chance to work with non-local definitions.

The code in question is all in the typecheck/TcSplice module. If you follow the types, this might not be so hard to do, after all. The definitions you're looking for, by the way, are in the Unfolding field of the IdInfo field of an Id.

comment:4 Changed 22 months ago by dailectic

Thanks for the pointer, I'll do some digging.

Note: See TracTickets for help on using tickets.