[Haskell-beginners] comment on this debugging trick?
Jeffrey Brown
jeffbrown.the at gmail.com
Fri Nov 27 22:51:45 UTC 2015
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.
On Fri, Nov 27, 2015 at 12:51 PM, Dennis Raddle <dennis.raddle at gmail.com>
wrote:
>
>
> On Fri, Nov 27, 2015 at 10:31 AM, Jeffrey Brown <jeffbrown.the at gmail.com>
> wrote:
>
>> Elliot Cameron, very smart guy, once advised me to make illegal state
>> invalid. Jake Brownson, another, recommended trying to use Maybes and
>> Eithers instead of throwing exceptions. I'm having trouble coming up with
>> examples but I think they're both helpful.
>>
>>
> I think I know roughly what you are talking about, but there are places
> where unexpected input might result in something like an empty list.
> Suppose I need the head of that list under normal conditions. Maybe the
> list is produced by library code so I can't alter the data structures.
> That's the kind of situation I want to identify precisely and immediately.
>
> D
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
--
Jeffrey Benjamin Brown
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151127/e9d28423/attachment.html>
More information about the Beginners
mailing list