On 405 Mar 2020, Ben Bacarisse wrote:
> Recently I wanted a type that adds another alternative to Maybe,
> specifically an approximate result, so I wrote (somewhat without
> thinking)
>
> data Approximate a = Roughly a | Maybe a
>
> Haskell does not complain about the type (after all, it just looks like
> I'm defining a constructor called Maybe)
Yes, I think that's exactly what you're doing, unconnected with the type
Maybe.
(snip)
> I can nest the Maybe in a new type:
>
> data Approximate a = Roughly a | Exactly (Maybe a)
>
> f a | a < 0 = Exactly Nothing
> | a > 10000 = Roughly (sqrt a)
> | otherwise = Exactly (Just (a / 2))
That's what I would have done, depending on why I wanted to use Maybe at
all. E.g., if you don't need much of what Maybe does, you could ignore
it and define your own three-option type and reimplement the Maybe-like
stuff you do need. Or you may find you get much of what you need if you
wrap the other way, e.g., leave it as a,
data Approximate a = Roughly a | Exactly a
and use Maybe (Approximate a).
> but I can't help wondering if I'm missing a neater way to do this.
I'm not aware of one but I know only "pedestrian" Haskell so somebody,
perhaps more familiar with esoteric extensions, may be able to show us
some magic.
-- Mark