PROPOSAL: Add 'Natural' type to base:Data.Word

Herbert Valerio Riedel hvr at gnu.org
Fri Nov 14 07:05:59 UTC 2014


On 2014-11-13 at 23:20:47 +0100, Mario Blažević wrote:
[...]
> 	Regarding the partial vs. saturated negation, I'm in favour of
> the former. However, there is another option nobody mentioned so far:
> NaN. I.e.,
>
> 1 - 2 = NaN
> 3 + 1 - 2 = 2
> 3 + (1 - 2) = NaN
>
> 	Could a GMP-based implementation provide such semantics
> without too much performance loss? If so, this would be my preference.

Purely from a technical point of view:

If you look at how it's implemented right now:

  https://phabricator.haskell.org/D473

being able to represent a 'not-a-natural' would be possible for the
GMP2-backed 'Natural' by adding a 'NatErr#' constructor, i.e.

  data Natural = NatS# Word# | NatJ# {-# UNPACK #-} !BigNat | NatErr#
               deriving (Eq,Ord)

that, however, would require to add one or two case-distinction to all
'Natural' operations, and we probably shouldn't auto-derive 'Eq'/'Ord'
anymore (as it's no longer to be considered a properly ordered
set/equivalence relation with that absorbing NatErr# element)

However, also the fallback implementation (for when GHC is configured with a
the old integer-gmp or the integer-simple backend) which is

  newtype Natural = Natural Integer

would need to become more complex, as the lightweight newtype would be
turned into something like

  data Natural = Natural !Integer | NaturalErr


So I'm afraid handling not-a-natural would indeed come at a cost.


Cheers,
  hvr


More information about the Libraries mailing list