[GHC] #12410: Somehow detect splicing in ghci

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- I'm no TH expert but currently it seems you need a hack (adding `data X ;` or `pure []`) to do the following {{{ ghci> $(reify ''() >>= runIO.print >> return []) <interactive>:210:3: error: • Couldn't match type ‘[t0]’ with ‘Exp’ Expected type: ExpQ Actual type: Q [t0] • In the expression: reify ''() >>= runIO . print >> return [] In the untyped splice: $(reify ''() >>= runIO . print >> return []) }}} {{{#!hs
data X; $(reify ''() >>= runIO.print >> return []) TyConI (DataD [] GHC.Tuple.() [] Nothing [NormalC GHC.Tuple.() []] [])
pure []; $(reify ''() >>= runIO.print >> return []) TyConI (DataD [] GHC.Tuple.() [] Nothing [NormalC GHC.Tuple.() []] []) }}}
Same with [https://hackage.haskell.org/package/lens-4.14/docs/Control- Lens-TH.html#v:makeLenses makeLenses] (discussed [https://artyom.me/lens- over-tea-6 here], [https://github.com/ekmett/lens/issues/461 there]): {{{
data A = B { _int :: Int } makeLenses ''A
<interactive>:209:1: error: • No instance for (Show DecsQ) arising from a use of ‘print’ • In a stmt of an interactive GHCi command: print it }}} the following two work {{{#!hs
data A = B { _int :: Int } pure []; makeLenses ''A
view int (B 42) 42 }}}
{{{#!hs
data A = B { _int :: Int }; makeLenses ''A
view int (B 42) 42 }}}
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 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: | -------------------------------------+------------------------------------- Description changed by Iceland_jack: @@ -25,1 +25,3 @@ - over-tea-6 here], [https://github.com/ekmett/lens/issues/461 there]): + over-tea-6 here], [https://github.com/ekmett/lens/issues/461 there], + [https://www.reddit.com/r/haskelltil/comments/3ghacj/you_can_use_template_has... + hither]): New description: I'm no TH expert but currently it seems you need a hack (adding `data X ;` or `pure []`) to do the following {{{ ghci> $(reify ''() >>= runIO.print >> return []) <interactive>:210:3: error: • Couldn't match type ‘[t0]’ with ‘Exp’ Expected type: ExpQ Actual type: Q [t0] • In the expression: reify ''() >>= runIO . print >> return [] In the untyped splice: $(reify ''() >>= runIO . print >> return []) }}} {{{#!hs
data X; $(reify ''() >>= runIO.print >> return []) TyConI (DataD [] GHC.Tuple.() [] Nothing [NormalC GHC.Tuple.() []] [])
pure []; $(reify ''() >>= runIO.print >> return []) TyConI (DataD [] GHC.Tuple.() [] Nothing [NormalC GHC.Tuple.() []] []) }}}
Same with [https://hackage.haskell.org/package/lens-4.14/docs/Control- Lens-TH.html#v:makeLenses makeLenses] (discussed [https://artyom.me/lens- over-tea-6 here], [https://github.com/ekmett/lens/issues/461 there], [https://www.reddit.com/r/haskelltil/comments/3ghacj/you_can_use_template_has... hither]): {{{
data A = B { _int :: Int } makeLenses ''A
<interactive>:209:1: error: • No instance for (Show DecsQ) arising from a use of ‘print’ • In a stmt of an interactive GHCi command: print it }}} the following two work {{{#!hs
data A = B { _int :: Int } pure []; makeLenses ''A
view int (B 42) 42 }}}
{{{#!hs
data A = B { _int :: Int }; makeLenses ''A
view int (B 42) 42 }}}
-- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 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 Blaisorblade): * cc: Blaisorblade (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 8.0.1 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 goldfire): * component: Compiler => Template Haskell Comment: GHCi sees splices as ''expression'' splices. You want a ''declaration'' splice. The truth is that either one is reasonable. But perhaps GHCi can detect what you mean by the types and Do The Right Thing. Just like it already does to detect when you want an `IO` action. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 8.0.1 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: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): Replying to [comment:3 goldfire]:
But perhaps GHCi can detect what you mean by the types and Do The Right Thing. Just like it already does to detect when you want an `IO` action.
Yes I had something like that in mind, detect if the type is `DecsQ` ---- Side note: {{{#!hs -- WORKS
pure @Q @[Dec] []; $(reify ''() >>= runIO.print >> return []) TyConI (DataD [] GHC.Tuple.() [] Nothing [NormalC GHC.Tuple.() []] []) }}}
{{{#!hs -- WORKS
(pure [] :: DecsQ); $(reify ''() >>= runIO.print >> return []) TyConI (DataD [] GHC.Tuple.() [] Nothing [NormalC GHC.Tuple.() []] []) }}}
{{{#!hs -- DOESN'T
pure [] :: DecsQ; $(reify ''() >>= runIO.print >> return [])
<interactive>:374:1: error: Invalid type signature: pure [] :: ... Should be of form <variable> :: <type> }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 8.0.1 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: | -------------------------------------+------------------------------------- Comment (by mgsloan): Another alternative to get ghci to do declaration splices: `{ $(makeLenses ''A) }` -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 8.0.1 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: | -------------------------------------+------------------------------------- Comment (by RyanGlScott): I like the suggestion in comment:5. It's short, and fairly intuitive. Perhaps we should just enshrine this in the users' guide and call this bug fixed? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 8.0.1 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: | -------------------------------------+------------------------------------- Comment (by goldfire): What's wrong with comment:3? The GHCi prompt already is flexible over the type of the thing you write... -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 8.0.1 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: | -------------------------------------+------------------------------------- Comment (by RyanGlScott): The GHCi prompt is only flexible because it has syntactic cues that it uses to distinguish between expressions, declarations, imports, etc. With Template Haskell splices, you have no such syntactic cues. The only way I could envision comment:3 ever being feasible is if we hacked the typechecker to treat `Q Exp` and `Q [Dec]`-returning splices differently, which is a lot of work for questionable benefit. comment:6 has the advantage that it's simple, works today, and is consistent with how other expressions and declarations are treated in GHCi (for instance, `3;` won't parse in GHCi's prompt). In light of this, I'm inclined to favor the simpler solution. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 8.0.1 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: | -------------------------------------+------------------------------------- Comment (by goldfire): OK -- I won't fight. :) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12410: Somehow detect splicing in ghci -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Template Haskell | Version: 8.0.1 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: | -------------------------------------+------------------------------------- Comment (by monoidal): See also #7331. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12410#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC