Primitive types and Prelude shenanigans
William Lee Irwin III
wli@holomorphy.com
Fri, 16 Feb 2001 11:47:10 -0800
William Lee Irwin III <wli@holomorphy.com> pisze:
>> literal "5" gets mapped to (fromPositiveInteger 5)
>> literal "-9" gets mapped to (fromNonZeroInteger -9)
On Fri, Feb 16, 2001 at 05:42:17PM +0000, Marcin 'Qrczak' Kowalczyk wrote:
> Note that when a discussed generic Prelude replacement
> framework is done, and ghc's rules are changed to expand -9 to
> negate (fromInteger 9) instead of fromInteger (-9), then you don't
> need uglification of the fromInteger function to be able to define
> types with only nonnegative numeric values. Just define your negate
> in an appropriate class, different from the fromInteger's class.
Good point, the canonical injection from the positive integers into
the various supersets (with structure) thereof handles it nicely.
I foresee:
fromPositiveInteger :: ContainsPositiveIntegers t => PositiveInteger -> t
instance ContainsPositiveIntegers Integer where ...
instance AdditiveGroup Integer where ...
negate :: AdditiveGroup t => t -> t {- this seems natural, but see below -}
fromPositiveInteger 5 :: ContainsPositiveIntegers t => t
negate $ fromPositiveInteger 5
:: (AdditiveGroup t, ContainsPositiveIntegers t) => t
which is not exactly what I want (and could probably use some aesthetic
tweaking); I had in mind that negative integers would somehow imply a
ContainsNonZeroIntegers or ContainsAllIntegers instance or the like.
The solution actually imposes a rather natural instance (though one
which could cause overlaps):
instance (AdditiveGroup t, ContainsPositiveIntegers t)
=> ContainsAllIntegers t where ...
I suppose one big wrinkle comes in when I try to discuss negation in
the multiplicative monoid of nonzero integers. That question already
exists without the Prelude's altered handling of negative literals.
negate . fromInteger $ n just brings it immediately to the surface.
0 and 1 will still take some work, but I don't expect help with them.
Thanks for the simplification!
Cheers,
Bill