Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  nodelist  faq  login

If the code and the comments disagree, then both are probably wrong. -- Norm Schryer


programming / comp.lang.haskell / Re: Nested "Maybe"

SubjectAuthor
* Nested "Maybe"Ben Bacarisse
+* Re: Nested "Maybe"Paul Rubin
|`- Re: Nested "Maybe"Paul Rubin
`- Re: Nested "Maybe"Mark Carroll

1
Subject: Nested "Maybe"
From: Ben Bacarisse
Newsgroups: comp.lang.haskell
Organization: A noiseless patient Spider
Date: Fri, 9 Apr 2021 21:13 UTC
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.haskell
Subject: Nested "Maybe"
Date: Fri, 09 Apr 2021 22:13:19 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <878s5rtb4g.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="1b1766d764a146b8f669bf59b8d75174";
logging-data="17832"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+oeBcBPSD8M8M0366qqjbgmS8LiKTkYVE="
Cancel-Lock: sha1:BFQd5YHspNwCXmMM49ZU6PL77jQ=
sha1:FBss4sXc7Q2wFXBI+s3//rkE8wQ=
X-BSB-Auth: 1.7905c2e322cf85bafc16.20210409221319BST.878s5rtb4g.fsf@bsb.me.uk
View all headers
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.


Subject: Re: Nested "Maybe"
From: Paul Rubin
Newsgroups: comp.lang.haskell
Organization: A noiseless patient Spider
Date: Fri, 9 Apr 2021 21:24 UTC
References: 1
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: no.em...@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.haskell
Subject: Re: Nested "Maybe"
Date: Fri, 09 Apr 2021 14:24:05 -0700
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <87blan883u.fsf@nightsong.com>
References: <878s5rtb4g.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="1b119873b4e364aebe8352f3ae5ece3f";
logging-data="13611"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+YsiOKGx9zwcvKPv8H1zv/"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)
Cancel-Lock: sha1:/CnZ/NckKtScDxbHslqZHqeqM/o=
sha1:rF2jHewcqFtQnhTNnqrk2u8KX0Y=
View all headers
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))


Subject: Re: Nested "Maybe"
From: Paul Rubin
Newsgroups: comp.lang.haskell
Organization: A noiseless patient Spider
Date: Fri, 9 Apr 2021 21:27 UTC
References: 1 2
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: no.em...@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.haskell
Subject: Re: Nested "Maybe"
Date: Fri, 09 Apr 2021 14:27:12 -0700
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <877dlb87yn.fsf@nightsong.com>
References: <878s5rtb4g.fsf@bsb.me.uk> <87blan883u.fsf@nightsong.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="1b119873b4e364aebe8352f3ae5ece3f";
logging-data="13611"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+7qXXjtw2DISEtvlx528C6"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)
Cancel-Lock: sha1:49/wS1mdP1HrDcf/GS7LFJxwfHk=
sha1:fQ6d2otKb0kTNKpcCcqTeFy5SFE=
View all headers
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.


Subject: Re: Nested "Maybe"
From: Mark Carroll
Newsgroups: comp.lang.haskell
Organization: none
Date: Fri, 9 Apr 2021 21:33 UTC
References: 1
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mtb...@bcs.org (Mark Carroll)
Newsgroups: comp.lang.haskell
Subject: Re: Nested "Maybe"
Date: Fri, 09 Apr 2021 17:33:50 -0400
Organization: none
Lines: 40
Message-ID: <874kgfywg1.fsf@ixod.org>
References: <878s5rtb4g.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="1d5bfb687e86c8f4ea4e258e4708a337";
logging-data="20443"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180+6WgZxfK/VpBkPqg+ygN"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:HEtNEtJBrKXACiRXFR+1SK18LKQ=
sha1:vuLqguzTQ50kZHu6NDhDpwRVHIA=
View all headers
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


1
rocksolid light 0.7.2
clearneti2ptor