Is the Maybe Monad Instance ever good?

I wouldn’t call that an abuse. To my eyes, MonadFail is for pattern-match failures and doing general error reporting with it is the real abuse.

2 Likes

Well it looks like little cheating to write X a ← pure xOrY since I need to wrap my thing in Just just to immediately unwrap and pattern match on it.

1 Like

I was surprised to discover that let in a do-block doesn’t desugar into MonadFail calls, and that this function is partial:

data XOrY = X Int | Y Bool deriving Show
maybeDoubleX :: XOrY -> Maybe Int
maybeDoubleX xOrY = do
  let X x = xOrY
  Just $ x * 2

The “trick” you describe also works well when writing list comprehensions, FWIW. This can sometimes be really handy when traversing lists containing values of a sum type.

1 Like

I was thinking: Have the best/worst of both worlds—go MonadComprehension! >:D

2 Likes

If you have a text comparison tool that shows results like this:

one way to model that result is to have it be a [[Maybe String]], where both layers of [] could actually be something better in producation code, e.g. the outer could be a some vector type, and the inner one some fixed-length vector type (fixed in the case that your application only supports 2-way diff, for instance).

In this case, I think Maybe fits be bill: Nothing is the way you model those grey placeholder lines between 6 and 7 on the left and between 4 and 5 on the right. There’s no additional information to be tied to those placeholders, as far as text alignment goes.

1 Like

Well, actually @sellout just made me realize that this usecase I described is a usecase for Maybe as a type, not for its Monad instance. I suppose I haven’t answered the OP’s question :sweat_smile:

1 Like