[Haskell-beginners] comment on this debugging trick?
Dennis Raddle
dennis.raddle at gmail.com
Fri Nov 27 23:55:07 UTC 2015
On Fri, Nov 27, 2015 at 2:51 PM, Jeffrey Brown <jeffbrown.the at gmail.com>
wrote:
> In parsing libraries for Haskell the "parse" function (or its equivalent)
> typically returns an Either. For instance there's
>
> parse :: Stream
> <http://hackage.haskell.org/package/parsec-3.1.6/docs/Text-Parsec-Prim.html#t:Stream>
> s Identity
> <http://hackage.haskell.org/package/transformers-0.4.1.0/docs/Data-Functor-Identity.html#t:Identity> t
> => Parsec
> <http://hackage.haskell.org/package/parsec-3.1.6/docs/Text-Parsec-Prim.html#t:Parsec> s
> () a -> SourceName
> <http://hackage.haskell.org/package/parsec-3.1.6/docs/Text-Parsec-Pos.html#t:SourceName>->
> s -> Either
> <http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Either.html#t:Either>
> ParseError
> <http://hackage.haskell.org/package/parsec-3.1.6/docs/Text-Parsec-Error.html#t:ParseError>
> a
>
> in Text.Parsec.Prim. This is an example of making invalid state
> impossible. parse could return a list of all possible parses, with the
> empty list signifying that parsing failed. But by using an Either, the case
> of failure can be distinguished as a Left. Haskell will know to treat the
> Left differently, and if you need an error report, that left can bubble
> (through multiple Either-returning functions, with some handy do-notation)
> up to the user without ever invoking an exception to the ordinary control
> flow.
>
I've used the Either monad for exception handling. Yes, I was using do
notation. This current project is just for myself. Exceptions represent
either bugs or malformed input. I can catch some of them in the IO monad so
my program prints an error but keeps running and lets me try again. If the
program crashes out, no big problem.
I'm parsing MusicXML with Text.XML.Light. The XML is always well-formed.
However *MusicXML* is not a well-defined language. Like, does every <note>
element have a child element called <voice>? No guarantee I can find, yet
it has been true in the examples I've tried with the only typesetter I'm
using to generate MusicXML. I can get my program running quickly without
bothering with the case that <voice> is absent. But if that case someday
occurs, I want to know precisely and immediately. Yet I don't want to write
a detailed error message for every violated assumption, of which there are
dozens necessary. So my solution is to find these things with case
exhaustions. The program crashes and I have to inspect the code, but no
problem.
D
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151127/588aab42/attachment-0001.html>
More information about the Beginners
mailing list