0

I have got the following code:

F (S core ps) = FAll core [] ps
  where
    FAll core acc ((name, (pc : pcs)) : ps) 
       = case F' (pc : pcs) (readC pc core) core of
            Nothing -> 
                        if (length pcs) /= 0 then FAll core ((name, pcs) : acc) ps

                        else FAll core acc ps


            Just (core', [pc']) -> let
                                     pc'' = pc' `mod` coresize
                                     pcs' = pcs ++ [pc'']
                                   in  FAll core' ((name, pcs') : acc) ps
stepAll core acc [] = S core (reverse acc)

It compiles fine but when I run the program it gives the following error:

Melon.hs:(172,10)-(182,74): Non-exhaustive patterns in case

where the number indicating the rows are the ones from the "= case F' (pc : pcs) (readC pc core) core of" to the "in FAll core' ((name, pcs') : acc) ps"

I think the problem is in exhausting the pattern for (pc : pcs) but I just cannot understand how I can solve it.

Any help would be appreciated.

The code has been updated with this one:

I wrote the following:

Just (core', (pc' : pcs')) -> let
                                  pc'' = pc' `mod` coresize
                                  pcs' = pcs' ++ [pc'']
                              in  stepAll core' ((name, pcs') : acc) ps
Just (core', []) -> stepAll core acc ps

but the program just falls into an infinite loop :S

  • I suspect the revised code has an algorithm bug unrelated to the case checking, but without having any idea what your code is doing (and being a little confused about what some of it means, since there seem to be capitalized identifiers where I wouldn't expect) I'm not sure what to advise. – C. A. McCann Dec 09 '11 at 21:23
  • The infinite loop is due to `pcs' = pcs' ++ [pc'']`. Both `pcs'` in this equation are the same, a new variable shadowing the identically named one from the `Just` pattern, thus you try do define `pcs'` in terms of itself, which gives an infinite loop if `pcs'` is ever demanded. You probably wanted `let ... pcs'' = pcs' ++ [pc''] in stepAll core' ((name,pcs'') : acc) ps`. – Daniel Fischer Dec 09 '11 at 21:26
  • Oh, I misread it as `pcs ++ [pc'']`, same as the other case. Yeah, I expect that's why you're getting an infinite loop. – C. A. McCann Dec 09 '11 at 21:29
  • 2
    Related: http://stackoverflow.com/questions/8435575/non-exhaustive-patterns-in-function, http://stackoverflow.com/questions/2737650/better-exception-for-non-exhaustive-patterns-in-case – Matvey Aksenov Dec 09 '11 at 21:30

1 Answers1

8

"Non-exhaustive patterns" means that you have a set of pattern matches that don't cover all possible combinations. In your code, you have cases like the following:

case {- stuff -} of
    Nothing ->               -- etc.
    Just (core', [pc']) ->   -- etc.

You handle both patterns for the Maybe portion, and the pair only has one pattern, but you match only on a single-element list, so this will fail for patterns that would look like Just (core', []) or Just (core', (pc' : pcs')).

It's usually best to handle all possible cases (i.e., have exhaustive pattern matches) even when you expect that some cases will never happen. If you're really, really certain that a case is impossible, then use something like error "this will never happen because blah blah blah". If you can't explain why it'll never happen, then you should consider handling it properly. :]

C. A. McCann
  • 76,893
  • 19
  • 209
  • 302