Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.haskell > #564 > unrolled thread

Nested "Maybe"

Started byBen Bacarisse <ben.usenet@bsb.me.uk>
First post2021-04-09 22:13 +0100
Last post2021-04-09 17:33 -0400
Articles 4 — 3 participants

Back to article view | Back to comp.lang.haskell


Contents

  Nested "Maybe" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-09 22:13 +0100
    Re: Nested "Maybe" Paul Rubin <no.email@nospam.invalid> - 2021-04-09 14:24 -0700
      Re: Nested "Maybe" Paul Rubin <no.email@nospam.invalid> - 2021-04-09 14:27 -0700
    Re: Nested "Maybe" Mark Carroll <mtbc@bcs.org> - 2021-04-09 17:33 -0400

#564 — Nested "Maybe"

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2021-04-09 22:13 +0100
SubjectNested "Maybe"
Message-ID<878s5rtb4g.fsf@bsb.me.uk>
This group is looking quiet, but you never know...

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) but, equally obviously, I can't
write a function like this

  f a | a < 0     = Nothing
      | a > 10000 = Roughly (sqrt a)
      | otherwise = Just (a / 2)

without type errors.  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))

but I can't help wondering if I'm missing a neater way to do this.

-- 
Ben.

[toc] | [next] | [standalone]


#565

FromPaul Rubin <no.email@nospam.invalid>
Date2021-04-09 14:24 -0700
Message-ID<87blan883u.fsf@nightsong.com>
In reply to#564
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>   data Approximate a = Roughly a | Exactly (Maybe a)
>
>   f a | a < 0     = Exactly Nothing
>       | a > 10000 = Roughly (sqrt a)
>       | otherwise = Exactly (Just (a / 2))
>
> but I can't help wondering if I'm missing a neater way to do this.

That looks inside out?  I.e. the Maybe should be on the outside.

data Precision a = Roughly a | Exactly a
data Approximate a = Maybe (Precision a)

f a | a < 0 = Nothing
    | a > 10000 = Just (Roughly (sqrt a))
    | otherwise = Just (Exactly (a/2))

[toc] | [prev] | [next] | [standalone]


#566

FromPaul Rubin <no.email@nospam.invalid>
Date2021-04-09 14:27 -0700
Message-ID<877dlb87yn.fsf@nightsong.com>
In reply to#565
Paul Rubin <no.email@nospam.invalid> writes:
> data Precision a = Roughly a | Exactly a
> data Approximate a = Maybe (Precision a)

Alternatively maybe you want an actual 3-way Maybe, e.g.

data Approximate a = Invalid | Roughly a | Exactly a

and then you could write typeclass instances (monad, monoid, etc.)
analogously with Maybe.

[toc] | [prev] | [next] | [standalone]


#567

FromMark Carroll <mtbc@bcs.org>
Date2021-04-09 17:33 -0400
Message-ID<874kgfywg1.fsf@ixod.org>
In reply to#564
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.haskell


csiph-web