[Haskell-beginners] mayBe stuck

Brent Yorgey byorgey at seas.upenn.edu
Fri Aug 6 05:24:19 EDT 2010

On Fri, Aug 06, 2010 at 09:40:05AM +0100, Colin Paul Adams wrote:
> >>>>> "Brent" == Brent Yorgey <byorgey at seas.upenn.edu> writes:
>     Brent> On Thu, Aug 05, 2010 at 06:20:06PM -0500, aditya siram wrote:
>     >> Normally yes, but here we are guaranteed to get a 'Just ...'
>     >> value because of the 'isNothing' guard.  -deech
>     Brent> You are correct, but that's not the point.  Every time you
>     Brent> use fromJust (or head, or unsafePerformIO...) you shift the
>     Brent> burden of proving that it is safe from the compiler onto
>     Brent> yourself.
> But the compiler could indeed prove that it's safe, if the typing system
> reflected the precondition. 

Sure. But it doesn't.  

My concern is a pragmatic one rather than theoretical.  Maybe for the
benefit of other beginners reading this, I should spell it out a bit
more clearly rather than trying to be too cute: if you ever find
yourself using fromJust, (or head, or any other functions that can
sometimes cause your program to crash), you have to convince yourself
that such a use is safe -- but this probably means you are doing too
much work yourself, rather than letting the compiler do the work for
you.  You should see if there is a way to refactor your code to use
pattern matching, or 'maybe', or 'fromMaybe'.  In deech's particular
case there was a way to pattern-match on the Maybe value (and also
make the code a lot shorter at the same time) rather than using
isNothing and fromJust.

> Since Haskell allows programming with
> partial functions, you always have this burden at present.

Yes.  But that doesn't mean there isn't value in the discipline of
avoiding them.  The burden of making sure you never use fromJust (or
head, or ...) is much lighter than the burden of proving that every
such use is safe.


More information about the Beginners mailing list