Saturday, June 02, 2012

[hvmwglnx] MonadPlus and case statements

In Haskell, I wish there were a guarantee that fail _ = mzero for pattern match failure within "do" notation in MonadPlus.  It's true for Maybe and list.

I wish there were a syntactic construct, CASEMZERO, that implicitly did case foo { ... ; _ -> mzero }, that is, implictly adding a final "match everything" pattern  _ -> mzero.  (Granted, it is only a few keystrokes saved in typing.)  It's kind of a more powerful version of the "guard" function.

Similarly, I wish there were a syntactic construct, CASERETURNUNIT, that implicitly added _ -> return (); as the final default alternative to case statements.  It's kind of a more powerful version of the "when" function.

I wish there were a more compact way to write this: msum [case x of { Pat1 -> foo ; _ -> mzero }, case x of { Pat2 -> bar ; _ -> mzero }, ... ], that is, repeatedly matching x against many patterns and combining the results.  MSUMCASE x of { Pat1 -> foo ; Pat2 -> bar }

I wish there were a syntactic construct that allowed putting the "match anything" default alternative as the first alternative in a case statement.  The "default" is the main flow, the following patterns are a few exceptional cases.  This is vaguely like the unless function which allows putting the "else" block first.  UNLESSCASE.

Previous thoughts on case statements.  I suspect many of these are a consequence of patterns not being first class objects in Haskell.

Possibly relevant: MonadPlus reform proposal

No comments :