[GHC] #9699: TH function to list names in scope

#9699: TH function to list names in scope -------------------------------------+------------------------------------- Reporter: MikeIzbicki | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 7.8.3 Keywords: | Operating System: Architecture: Unknown/Multiple | Unknown/Multiple Difficulty: Unknown | Type of failure: Blocked By: | None/Unknown Related Tickets: | Test Case: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- I [asked about this on stackoverflow](http://stackoverflow.com/questions/26394199/using- templatehaskell-to-list-all-names-in-a-namespace), and apparently it doesn't exist yet. I want a TemplateHaskell function `variablesInScope :: Q [Name]` that returns a list of the `Name`'s of all the variables in scope. TemplateHaskell obviously has this information available in order to implement functions like `reify :: Name -> Q Info` and `lookupValueName :: String -> Q (Maybe Name)`. So it seems like this might be a pretty simple function to add. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9699 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9699: TH function to list names in scope -------------------------------------+------------------------------------- Reporter: | Owner: MikeIzbicki | Status: new Type: feature | Milestone: request | Version: 7.8.3 Priority: normal | Keywords: Component: Template | Architecture: Unknown/Multiple Haskell | Difficulty: Unknown Resolution: | Blocked By: Operating System: | Related Tickets: Unknown/Multiple | Type of failure: | None/Unknown | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Comment (by simonpj): Indeed it would not be hard. I'd be open to patches. There are open design questions. Do you mean "all names in scope" (including imported ones)? There may be a lot of those. Or just those bound in this module? Or just those bound by nested (non-top-level) definitions? Moreover, although, say `Data.List.partition` may not be ''lexically'' in scope, it's fine to generate a TH program that refers to it, thus: {{{ module Foo where import Bar( bar ) f x = $(bar 5) }}} where `bar` is a TH function in another module `Bar` where `Data.List.partition` is in scope, and `(bar 5)` expands to code mentioning `partition`. So, is `Data.List.partition` "in scope"? As ever, getting the specification right (and precise) is a big part of the work! Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9699#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9699: TH function to list names in scope -------------------------------------+------------------------------------- Reporter: MikeIzbicki | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 7.8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: None/Unknown | Unknown/Multiple Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by spinda): Attempt at a rough specification: (1) Extend [https://hackage.haskell.org/package/template- haskell-2.10.0.0/docs/Language-Haskell-TH-Syntax.html#t:ModuleInfo ModuleInfo] (obtained from [https://hackage.haskell.org/package/template- haskell-2.10.0.0/docs/Language-Haskell-TH-Syntax.html#v:reifyModule reifyModule]) to {{{ModuleInfo [Module] [Name]}}}, where {{{[Module]}}} is still the import list and {{{[Name]}}} contains the module's list of exported names. (2) Add {{{thisModule :: Q Module}}} producing the current [https://hackage.haskell.org/package/template-haskell-2.10.0.0/docs /Language-Haskell-TH-Syntax.html#t:Module Module]. (3) Add {{{topLevelNames :: Q [Name]}}} producing a list of top-level names (both exported and non-exported) bound in the current module that would be visible to {{{reify}}}. (4) Add {{{nestedNames :: Q [Name]}}} (in need of a better name) producing a list of the non-top-level (nested) names visible to {{{reify}}} in this context. (5) Add {{{parentNames :: Q [Name]}}} (also in need of a better name) producing a list of the names immediately associated with the current splicing context, if available. For example, {{{foo, bar :: $(typeSplice)}}} would see {{{foo}}} and {{{bar}}}, {{{foo = $(exprSplice)}}} would see {{{foo}}}, and {{{$(topLevelDecSplice)}}} would see {{{[]}}}. (6) ''Optional'' Add {{{isTopLevel :: Name -> Q Bool}}} to detect whether a name is bound at the top level (of the current module?). Something like this could alternately be accomplished by searching through {{{topLevelNames}}}. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9699#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9699: TH function to list names in scope -------------------------------------+------------------------------------- Reporter: MikeIzbicki | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 7.8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: None/Unknown | Unknown/Multiple Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Changes (by spinda): * cc: spinda (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9699#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9699: TH function to list names in scope -------------------------------------+------------------------------------- Reporter: MikeIzbicki | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 7.8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: None/Unknown | Unknown/Multiple Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by MikeIzbicki): This would work for my use case. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9699#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9699: TH function to list names in scope -------------------------------------+------------------------------------- Reporter: MikeIzbicki | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 7.8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: None/Unknown | Unknown/Multiple Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by goldfire): Replying to [comment:2 spinda]:
Attempt at a rough specification:
Thanks for putting this together. It's usually best to put this sort of specification on a (fresh) wiki page, so it can evolve more easily that is possible in a comment chain. A few reactions to the spec: - The current module's list of exported names might include names that are not in scope, due to top-level splices. Recall that in a top-level splice, definitions below the splice are not yet in scope, yet might be mentioned in an export list. And there might be exported names that there is no way to know about: for example, if the export list says `T(..)` and `T`'s definition is below a top-level splice, I doubt strongly GHC knows what constructors are in `T` when processing the top-level splice. - Rename `nestedNames` to `localNames`? - `parentNames` is underspecified. (What is returned within a type splice within an expression? What about a `let`-bound expression? What about in patterns?) I further believe that any tight specification will be woefully long and intricate. What is the expected use case of such a function? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9699#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9699: TH function to list names in scope -------------------------------------+------------------------------------- Reporter: MikeIzbicki | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 7.8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by mgsloan): * cc: mgsloan (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9699#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC