Opened 6 years ago

Last modified 3 years ago

#5416 new feature request

Local modules and Template Haskell declaration splices

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

Description

Sometimes you want a Template Haskell splice to expand to a bunch of declarations, some of which are not visible to the context. A trivial example might be

module M where
 $(f 4)

====> expands to 
module M where
 helper = ...blah...
 real_fun = ...helper....

But helper is only a helper function for real_fun and should not be exported by M, nor should it conflict with other definitions in M.

See #5398 for a more concrete example, and (at the end) a speculative suggestion for "local modules" that might be a good design.

Another thing you might want is for a splice to expand into some import declarations that bring some new stuff into scope. That too might be possible using this design.

Prioity uncertain, details very uncertain. I'm just making the ticket to keep track of the idea.

Change History (5)

comment:1 Changed 6 years ago by Saizan

Cc: sanzhiyan@… added

comment:2 Changed 6 years ago by igloo

You might want this in regular Haskell too, e.g. you could have with be a keyword and

with helper = ...
in instance Foo T where
       m1 x = helper True x
       m2 x = helper False x

Likewise you occasionally want local datatypes etc.

I don't know whether a "with...in" style syntax or a "submodule" style syntax would be better.

comment:3 Changed 6 years ago by igloo

Type: bugfeature request

comment:4 Changed 6 years ago by batterseapower

If you only want your helper to be visible in a number of value declarations, you can just have your TH generate this:

(real_fun1, real_fun2) = (real_fun1, real_fun2)
    where real_fun1 = ...; real_fun2 = ...; helper = ...

In the case where some of the declarations are classes/instances the situation is less clear.

comment:5 Changed 3 years ago by thomie

Component: CompilerTemplate Haskell
difficulty: Unknown
Note: See TracTickets for help on using tickets.