[GHC] #12188: Pattern variables bound by PatternGuards are not accessible in where clause

#12188: Pattern variables bound by PatternGuards are not accessible in where clause -------------------------------------+------------------------------------- Reporter: heisenbug | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 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: -------------------------------------+------------------------------------- Consider: {{{#!hs foo :: Int -> Bool foo a | Just b <- return a = c where c = a == b }}} I get: {{{ test.hs:3:18: Not in scope: b }}} Is there any reason why pattern variables originating from pattern guards should not work in `where` clauses? Problem is present in GHC HEAD too. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12188 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12188: Pattern variables bound by PatternGuards are not accessible in where clause -------------------------------------+------------------------------------- Reporter: heisenbug | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by heisenbug): * failure: None/Unknown => GHC rejects valid program -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12188#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12188: Pattern variables bound by PatternGuards are not accessible in where clause -------------------------------------+------------------------------------- Reporter: heisenbug | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by goldfire): * status: new => closed * resolution: => invalid Comment: Right now, GHC works the other way, accepting this: {{{#!hs foo :: Int -> Int foo x | should_add = x + 1 | otherwise = x - 1 where should_add = x > 0 }}} I've not checked to see whether this is specified in the Report, but my guess is that it is. I'm closing as invalid, but do reopen if I've misunderstood. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12188#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12188: Pattern variables bound by PatternGuards are not accessible in where clause -------------------------------------+------------------------------------- Reporter: heisenbug | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by heisenbug): Replying to [comment:2 goldfire]:
Right now, GHC works the other way, accepting this:
{{{#!hs foo :: Int -> Int foo x | should_add = x + 1 | otherwise = x - 1 where should_add = x > 0 }}}
I've not checked to see whether this is specified in the Report, but my guess is that it is.
I'm closing as invalid, but do reopen if I've misunderstood.
I am not sure whether we are on the same page. In your example `should_add` is a boolean guard, it does not establish a new binding. In my example `b` is a pattern variable bound by the pattern guard. Your example does not contain such a thing. Your `should_add` use is morally equivalent to my `return` (and `a`) use. Do you still think this ticket is invalid? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12188#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12188: Pattern variables bound by PatternGuards are not accessible in where clause -------------------------------------+------------------------------------- Reporter: heisenbug | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by simonpj): Richard is right. In Haskell * The patterns of the function definition scope over guards, where clause, and body * The where clause scopes over the guards, and body. * The guards scope over the body only Once could define the language differently, but that's the way it is in Haskell. You can include let-binding in the guards, if you want: {{{ foo :: Int -> Bool foo a | Just b <- return a , let c = a == b = c }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12188#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12188: Pattern variables bound by PatternGuards are not accessible in where clause -------------------------------------+------------------------------------- Reporter: heisenbug | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by heisenbug): Replying to [comment:4 simonpj]:
Richard is right. In Haskell
* The patterns of the function definition scope over guards, where clause, and body * The where clause scopes over the guards, and body. * The guards scope over the body only
Once could define the language differently, but that's the way it is in Haskell.
Oh, pity. Surely those rules were set in stone when pattern guards weren't a thing yet. Thanks for the `let` tip, I'll look into it and see how it helps in my case.
You can include let-binding in the guards, if you want: {{{ foo :: Int -> Bool foo a | Just b <- return a , let c = a == b = c }}}
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12188#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12188: Pattern variables bound by PatternGuards are not accessible in where clause -------------------------------------+------------------------------------- Reporter: heisenbug | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by rwbarton): Since a single where block can scope over multiple guarded right-hand sides, it doesn't make sense to have variables bound in a guard be in scope in the body of the where clause anyways. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12188#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC