title: Haskell: pattern matching on undefined values
body: <p>As I understand it, Haskell's <code>undefined</code> value - of a particular type - is a value that can't be determined, for whatever reason (maybe there is no sensible definition, or the calculation diverges).  For instance: <code>undefined :: [Int]</code> is a list, so it must be constructed with either [] or (:), but it doesn't know which one!  Hence it makes sense that case splitting on undefined makes the whole expression undefined: I don't know whether <code>null (undefined :: [a])</code> is True or False, because I don't know whether <code>undefined :: [a]</code> is empty or not.</p>
  
  <p>(By the way - if you disagree with my suggestion that <code>undefined</code> <em>is</em> built with a constructor, then surely <code>null (undefined :: [a])</code> should evaluate to False?  After all <code>undefined</code> isn't equivalent to []!)</p>
  
  <p>However, Haskell pattern matching doesn't follow this way of thinking.</p>
  
  <pre><code>data Foo = Foo Int String  -- Only one data constructor
  
  silly :: Foo -&gt; Bool
  silly (Foo _ _) = True
  
  ghci&gt; silly (undefined :: Foo)
  *** Exception: Prelude.undefined    -- But whatever the value is, it must 
                                      -- be constructed with Foo.
                                      -- So why fail to match?
  </code></pre>
  
  <p>(I know that newtype behaves differently here.)</p>
  
  <p>Also:</p>
  
  <pre><code>foo :: Int -&gt; String -&gt; Bool
  foo 8 "Hello" = True
  foo _ _       = False
  
  ghci&gt; foo undefined undefined
  *** Exception: Prelude.undefined     -- GOOD - can't tell which case to choose.
  
  ghci&gt; foo undefined "Hello"
  *** Exception: Prelude.undefined     -- GOOD - still can't tell.
  
  ghci&gt; foo undefined "Goodbye"
  *** Exception: Prelude.undefined     -- BAD  - should return false!
                                       -- Pattern match on first line should fail,
                                       -- because whatever the int is, the
                                       -- string can't match the given pattern.
  </code></pre>
  
  <p>I think the rules on pattern matching currently say that if a sub-pattern fails then the pattern should immediately fail; if it diverges, then the whole pattern should also diverge; and if it succeeds, then the next sub-pattern should be tried.  If all sub-patterns succeed, then the whole pattern succeeds.</p>
  
  <p>What I am suggesting is that if any of the sub-patterns fail then the whole pattern should fail; if none fail but some diverge then the whole pattern should diverge; and if all succeed then the whole pattern should succeed.</p>
  
  <p>Why is this not how Haskell does things?</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>So to summarise my interpretation of the responses I've read:</p>
  
  <p><code>undefined</code> should be read as "This would cause your program to run forever" not "This is not well defined", and <code>Exception: Prelude.undefined</code> should be read as "Watch out! Your program would have never terminated if undefined was an infinite loop" rather than "I don't know what to do because I don't know what value undefined is".</p>
  
  <p>Could somebody please verify that this is correct?  If so then I will probably accept mb14's answer.</p>
  
  <p>Thanks everyone. Sorry for being so slow on the uptake!</p>