[GHC] #10708: Rejection of constant functions defined using conditional pattern matching

#10708: Rejection of constant functions defined using conditional pattern matching -------------------------------------+------------------------------------- Reporter: | Owner: HubertGaravel | Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.1 Keywords: | Operating System: Linux Architecture: x86 | Type of failure: GHC rejects | valid program Test Case: | Blocked By: Blocking: | Related Tickets: Differential Revisions: | -------------------------------------+------------------------------------- Hi, The Haskell program below is rejected, although it is correct. GHC gives the following error message: {{{ HASKELL/tricky.hs:12:1: Multiple declarations of `Main.d3' Declared at: HASKELL/tricky.hs:10:1 HASKELL/tricky.hs:11:1 HASKELL/tricky.hs:12:1 }}} I agree that the definition of d3 is tricky and useless, but it seems to be valid. This example was exctracted from Haskell code automatically produced by an in-house code generator. {{{#!hs data Nat = D0 | Succ Nat deriving (Show, Eq, Ord) d1 :: Nat d2 :: Nat d3 :: Nat d1 = (Succ D0) d2 | D0 == D0 = D0 d3 | D0 /= D0 = D0 d3 | (Succ D0) == D0 = D0 d3 | (Succ D0) /= D0 = (Succ D0) main = do print d1 print d2 print d3 }}} Best regards Hubert -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10708 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10708: Rejection of constant functions defined using conditional pattern matching -------------------------------------+------------------------------------- Reporter: HubertGaravel | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.1 Resolution: invalid | Keywords: Operating System: Linux | Architecture: x86 Type of failure: GHC rejects | Test Case: valid program | Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Changes (by goldfire): * status: new => closed * resolution: => invalid Comment: I believe you mean the code to say this: {{{ data Nat = D0 | Succ Nat deriving (Show, Eq, Ord) d1 :: Nat d2 :: Nat d3 :: Nat d1 = (Succ D0) d2 | D0 == D0 = D0 d3 | D0 /= D0 = D0 | (Succ D0) == D0 = D0 | (Succ D0) /= D0 = (Succ D0) main = do print d1 print d2 print d3 }}} The name of a defined symbol is not repeated in the different branches. Thanks for reporting, however! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10708#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10708: Rejection of constant functions defined using conditional pattern matching -------------------------------------+------------------------------------- Reporter: HubertGaravel | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.1 Resolution: invalid | Keywords: Operating System: Linux | Architecture: x86 Type of failure: GHC rejects | Test Case: valid program | Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by simonpj): well of course you ''can'' define `d3` like that. But the question posed by the ticket is: is it valid Haskell to write `d3` as in the description. Looking at the [https://www.haskell.org/onlinereport/haskell2010/haskellch4.html language spec] section 4.4.3, it seems that a 'funlhs' must have at least one argument. So this must be a 'pat' lhs, which can only have one set of guarded rhss. So yes, I think it's invalid. Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10708#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10708: Rejection of constant functions defined using conditional pattern matching -------------------------------------+------------------------------------- Reporter: HubertGaravel | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.1 Resolution: invalid | Keywords: Operating System: Linux | Architecture: x86 Type of failure: GHC rejects | Test Case: valid program | Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by HubertGaravel): Thanks for your prompt answers. Yet, assuming that the definition of d3 is invalid because a funlhs must have at least one argument, why is the definition of d2 accepted in the example given? Like d3, d2 is defined by a conditional rule and is not followed by an argument. I assume that GHC processes lines fromp top to bottom and that if it complains about the definition of d3, it means that it finds the definition of d1 and d2 valid. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10708#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10708: Rejection of constant functions defined using conditional pattern matching -------------------------------------+------------------------------------- Reporter: HubertGaravel | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.1 Resolution: invalid | Keywords: Operating System: Linux | Architecture: x86 Type of failure: GHC rejects | Test Case: valid program | Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by goldfire): Something defined without an argument ''can'' have a pattern guard on it. It's just that, if there are multiple patterns, the thing being defined must not be repeated before each guard. So any of the lines defining `d3` are valid -- they're just not valid in concert. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10708#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10708: Rejection of constant functions defined using conditional pattern matching -------------------------------------+------------------------------------- Reporter: HubertGaravel | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.1 Resolution: invalid | Keywords: Operating System: Linux | Architecture: x86 Type of failure: GHC rejects | Test Case: valid program | Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by HubertGaravel): Thanks for the insight. If I try to reformulate, the definition of d3 is allowed by the syntax but forbidden by the static semantics. Perhaps the static semantics rule is too rigid and could be relaxed to make Haskell more regular. The example below shows that regularity is broken for arity 0 (empty pattern list). But I can live with this bizarre exception and will modify my Haskell generator to handle this corner case. Thanks again! Hubert {{{#!hs -- valid for arity 2 f2 x y | x == 0 = x f2 x y | x /= 0 = x + 1 -- valid for arity 1 f1 x | x == 0 = x f1 x | x /= 0 = x + 1 -- invalid for arity 0 f0 | 0 == 0 = 0 f0 | 0 /= 0 = 1 }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10708#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10708: Rejection of constant functions defined using conditional pattern matching -------------------------------------+------------------------------------- Reporter: HubertGaravel | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Changes (by goldfire): * status: closed => new * type: bug => feature request * resolution: invalid => * os: Linux => Unknown/Multiple * architecture: x86 => Unknown/Multiple Comment: I find comment:5 compelling. But is it worth implementing, and invariably adding a new language flag? I'm not sure, but I'm turning this back around into a feature request. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10708#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC