[Haskell-cafe] ghc 7.0.3 view patterns and exhaustiveness

Twan van Laarhoven twanvl at gmail.com
Wed Sep 21 23:57:16 CEST 2011


On 2011-09-21 22:06, Brent Yorgey wrote:
> On Tue, Sep 20, 2011 at 10:31:58PM -0400, Richard Cobbe wrote:
>>
>>      numVarRefs :: Term ->  Integer
>>      numVarRefs (view ->  Var _) = 1
>>      numVarRefs (view ->  App rator rand) = numVarRefs rator + numVarRefs rand
>>      numVarRefs (view ->  Lam _ body) = numVarRefs body
>>      -- numVarRefs (view ->  _) = error "bogus"
>>
> This is a known limitation.  Your particular example is perhaps not so
> hard to figure out, but what if we had
>
>    view :: Bool ->  Bool
>    view x = search for a counterexample to the Goldbach conjecture; if
>             one is found, return x, otherwise return False
>
>    foo (view ->  False) = ...

But in Richard's example, all possible values of the result of view are handled. 
For your foo example, the equivalent would be.

     foo (view -> False) = ...
     foo (view -> True)  = ...

Ghc should be able to detect this case, and not issue a warning. All that needs 
to be done is check if the function can be rewritten to

     numVarRefs t = case view t of ...

Where ... is exhaustive.


Twan



More information about the Haskell-Cafe mailing list