[Haskell-cafe] haskell programming guidelines

John Meacham john at repetae.net
Mon Feb 27 23:12:16 EST 2006


On Mon, Feb 27, 2006 at 10:57:17PM -0500, Cale Gibbard wrote:
> Well, this is an issue. Perhaps a version of error which makes the
> line/column number available to its parameter would help... something
> along the lines of
> 
> type SourcePos = (Integer, Integer)
>     -- possibly a data/newtype with a nicer Show instance
> errorPos :: (SourcePos -> String) -> a


Yes, this is what jhc's SRCLOC_ANNOTATE addreses, more or less.

> This would give all the benefits normally acquired from the expansion
> of the syntax sugar while allowing you to additionally add any extra
> messages you'd like. Further, you'd not be required to work in, say
> the identity monad, in order to get line number messages for failures
> (though in GHC at least, irrefutable pattern match failures in lambdas
> and let also get line numbered).

Well, the benefit of the Identity monad is so that the user of a routine
can choose to recover gracefully by using a different monad, you only
use the Identity monad when you are making a choice to bottom out on
errors. using 'error' directly is not an option in said cases because it
would take away the ability of the user of a routine to catch errors
properly. error should only be used for reporting bugs that should never
happen, not user visible failure.

The writer of a library shouldn't decide how (non-buggy) failure should
be handled, the user of it should.

> I'm actually really against the inclusion of fail in the Monad class,
> so finding a reasonable replacement for any constructive uses it might
> have had is important to me.

I know you keep saying this, We start with the exact same premises and
goals, yet somehow come to the exact opposite conclusion. I have not
quite figured out why.

However, a quick survey shows that _every single_ monad defined in the
standard and fptools libraries has an interesting non-error 'fail'
method other than Identity, whose sole purpose is to turn 'fail's into
errors.  Separating out a MonadError with 'fail' seems rather odd as
every monad will be an instance of it! (including Identity, since
turning fails into errors is its main purpose)

(the monads like 'Reader' and 'Writer' are actually just shorthand for
ReaderT a Identity, the inner monad determines the failure mode)

        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list