# Break `abs` into two aspects

27 messages
12
Open this post in threaded view
|

## Break `abs` into two aspects

 This two-step proposal suggests to alter the official Haskell 2010 standard.1. Introduction:The `Num` typeclass effectively represents a ring. Yet it also has `abs` and `signum`. `abs` represents a norm, but its type is wrong:    abs :: a -> aMathematically, the return value must be a nonnegative real number. Yet there is no guarantee that the type `a` can have that value. This led us to have too strong restriction to `instance Num Complex`:    instance RealFloat a => Num (Complex a) where ...I found a way for `abs` (and `signum`) to have much more mathematical place.2. Step 1:First, I suggest to add the following variants of `abs` and `signum`:    realAbs :: Real a => a -> a    realAbs x = if x >= 0 then x else negate x    realSignum :: Real a => a -> a    realSignum x = case compare 0 x of         LT -> -1         EQ -> 0         _  -> 1These can replace `abs` and `signum` instances for integral and rational types.3. Step 2:Second, I suggest to move `abs` and `signum` from `Num` to `Floating`:    class Floating a where         abs :: a -> a         signum :: a -> a         ...This exploits the fact that `Floating` represents a field with characteristic 0, regarding exponential and trigonometric functions are defined as Taylor series. The definition of convergence of series is defined using norms.4. Conclusion:This enables us to implement rings (Num) and fields (Fractional) without concerning about norms. For example, Gaussian integers. _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 On Tue, 28 Jan 2020, Dannyu NDos wrote: > 3. Step 2: > > Second, I suggest to move `abs` and `signum` from `Num` to `Floating`: > >     class Floating a where >          abs :: a -> a >          signum :: a -> a >          ... Rational is not Floating, but has reasonable abs and signum._______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 Rational is applicable the suggested new functions.2020년 1월 28일 (화) 오후 7:49, Henning Thielemann <[hidden email]>님이 작성: On Tue, 28 Jan 2020, Dannyu NDos wrote: > 3. Step 2: > > Second, I suggest to move `abs` and `signum` from `Num` to `Floating`: > >     class Floating a where >          abs :: a -> a >          signum :: a -> a >          ... Rational is not Floating, but has reasonable abs and signum. _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 On Tue, 28 Jan 2020, Dannyu NDos wrote: > Rational is applicable the suggested new functions. I see, realAbs and realSignum are not methods, but top-level functions. _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 In reply to this post by Dannyu NDos On Tue, 28 Jan 2020, Dannyu NDos wrote: > `abs` represents a norm, but its type is wrong There are two useful meanings of `abs`, which coincide for integers. One is a norm. Another one is to define `abs` as a mapping from a ring R to a factor ring R / U(R), where U(R) is a ring of units, and `signum` as a mapping from R to U(R) such that `abs a * signum a = a`. > This enables us to implement rings (Num) and fields (Fractional) without > concerning about norms. For example, Gaussian integers. For Gaussian integers I find convenient to define `signum z` with a codomain {1, i, -1, -I} (basically, to which quadrant does z belong?) and `abs z` with the first quadrant as a codomain. Best regards, Andrew _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 Well said Andrew!There’s a second twist : last I checked our abs for complex numbers isn’t the Euclidean norm or any Lp norm ..We could define the Pth power of the lpNorm for any complex a I think.  Though that’s a weaker operation.  On Tue, Jan 28, 2020 at 7:11 AM Andrew Lelechenko <[hidden email]> wrote:On Tue, 28 Jan 2020, Dannyu NDos wrote: > `abs` represents a norm, but its type is wrong There are two useful meanings of `abs`, which coincide for integers. One is a norm. Another one is to define `abs` as a mapping from a ring R to a factor ring R / U(R), where U(R) is a ring of units, and `signum` as a mapping from R to U(R) such that `abs a * signum a = a`. > This enables us to implement rings (Num) and fields (Fractional) without > concerning about norms. For example, Gaussian integers. For Gaussian integers I find convenient to define `signum z` with a codomain {1, i, -1, -I} (basically, to which quadrant does z belong?) and `abs z` with the first quadrant as a codomain. Best regards, Andrew _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 One long running pain point is that the abs definition we have for complex numbers is terrible.  Does anyone use it ?On Tue, Jan 28, 2020 at 9:59 AM Carter Schonwald <[hidden email]> wrote:Well said Andrew!There’s a second twist : last I checked our abs for complex numbers isn’t the Euclidean norm or any Lp norm ..We could define the Pth power of the lpNorm for any complex a I think.  Though that’s a weaker operation.  On Tue, Jan 28, 2020 at 7:11 AM Andrew Lelechenko <[hidden email]> wrote:On Tue, 28 Jan 2020, Dannyu NDos wrote: > `abs` represents a norm, but its type is wrong There are two useful meanings of `abs`, which coincide for integers. One is a norm. Another one is to define `abs` as a mapping from a ring R to a factor ring R / U(R), where U(R) is a ring of units, and `signum` as a mapping from R to U(R) such that `abs a * signum a = a`. > This enables us to implement rings (Num) and fields (Fractional) without > concerning about norms. For example, Gaussian integers. For Gaussian integers I find convenient to define `signum z` with a codomain {1, i, -1, -I} (basically, to which quadrant does z belong?) and `abs z` with the first quadrant as a codomain. Best regards, Andrew _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 In reply to this post by Dannyu NDos @Andrew, it's not clear how you are posing U(R) as "ring of units." It's a multiplicative group, and is not closed under addition.2020년 1월 28일 (화) 19:42, Dannyu NDos <[hidden email]>님이 작성:This two-step proposal suggests to alter the official Haskell 2010 standard.1. Introduction:The `Num` typeclass effectively represents a ring. Yet it also has `abs` and `signum`. `abs` represents a norm, but its type is wrong:    abs :: a -> aMathematically, the return value must be a nonnegative real number. Yet there is no guarantee that the type `a` can have that value. This led us to have too strong restriction to `instance Num Complex`:    instance RealFloat a => Num (Complex a) where ...I found a way for `abs` (and `signum`) to have much more mathematical place.2. Step 1:First, I suggest to add the following variants of `abs` and `signum`:    realAbs :: Real a => a -> a    realAbs x = if x >= 0 then x else negate x    realSignum :: Real a => a -> a    realSignum x = case compare 0 x of         LT -> -1         EQ -> 0         _  -> 1These can replace `abs` and `signum` instances for integral and rational types.3. Step 2:Second, I suggest to move `abs` and `signum` from `Num` to `Floating`:    class Floating a where         abs :: a -> a         signum :: a -> a         ...This exploits the fact that `Floating` represents a field with characteristic 0, regarding exponential and trigonometric functions are defined as Taylor series. The definition of convergence of series is defined using norms.4. Conclusion:This enables us to implement rings (Num) and fields (Fractional) without concerning about norms. For example, Gaussian integers. _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 I think it's more mathematically clear to define `abs` by U(R) acting on R. U(R) acts on R via multiplication, which defines an equivalence relation on R called 'associatedness.' _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 In reply to this post by Andrew Lelechenko On Tue, 28 Jan 2020, Dannyu NDos wrote: > Second, I suggest to move `abs` and `signum` from `Num` to `Floating` I can fully relate your frustration with `abs` and `signum` (and numeric type classes in Haskell altogether). But IMO breaking both in `Num` and in `Floating` at once is not a promising way to make things proper. I would rather follow the beaten track of Applicative Monad and Semigroup Monoid proposals and - as a first step - introduce a superclass (probably, borrowing the design from `semirings` package): class Semiring a where   zero  :: a   plus  :: a -> a -> a   one   :: a   times :: a -> a -> a   fromNatural :: Natural -> a class Semiring a => Num a where ... Tangible benefits in `base` include: a) instance Semiring Bool, b) a total instance Semiring Natural (in contrast to a partial instance Num Natural), c) instance Num a => Semiring (Complex a) (in contrast to instance RealFloat a => Num (Complex a)), d) newtypes Sum and Product would require only Semiring constraint instead of Num. Best regards, Andrew _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 that actually sounds pretty sane. I think! On Fri, Jan 31, 2020 at 3:38 PM Andrew Lelechenko <[hidden email]> wrote:On Tue, 28 Jan 2020, Dannyu NDos wrote: > Second, I suggest to move `abs` and `signum` from `Num` to `Floating` I can fully relate your frustration with `abs` and `signum` (and numeric type classes in Haskell altogether). But IMO breaking both in `Num` and in `Floating` at once is not a promising way to make things proper. I would rather follow the beaten track of Applicative Monad and Semigroup Monoid proposals and - as a first step - introduce a superclass (probably, borrowing the design from `semirings` package): class Semiring a where   zero  :: a   plus  :: a -> a -> a   one   :: a   times :: a -> a -> a   fromNatural :: Natural -> a class Semiring a => Num a where ... Tangible benefits in `base` include: a) instance Semiring Bool, b) a total instance Semiring Natural (in contrast to a partial instance Num Natural), c) instance Num a => Semiring (Complex a) (in contrast to instance RealFloat a => Num (Complex a)), d) newtypes Sum and Product would require only Semiring constraint instead of Num. Best regards, Andrew _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

 Andrew: could you explain the algebra notation you were using for short hand?  I think I followed, but for people the libraries list might be their first exposure to advanced / graduate abstract algebra (which winds up being simpler than most folks expect ;) )On Fri, Jan 31, 2020 at 4:36 PM Carter Schonwald <[hidden email]> wrote:that actually sounds pretty sane. I think! On Fri, Jan 31, 2020 at 3:38 PM Andrew Lelechenko <[hidden email]> wrote:On Tue, 28 Jan 2020, Dannyu NDos wrote: > Second, I suggest to move `abs` and `signum` from `Num` to `Floating` I can fully relate your frustration with `abs` and `signum` (and numeric type classes in Haskell altogether). But IMO breaking both in `Num` and in `Floating` at once is not a promising way to make things proper. I would rather follow the beaten track of Applicative Monad and Semigroup Monoid proposals and - as a first step - introduce a superclass (probably, borrowing the design from `semirings` package): class Semiring a where   zero  :: a   plus  :: a -> a -> a   one   :: a   times :: a -> a -> a   fromNatural :: Natural -> a class Semiring a => Num a where ... Tangible benefits in `base` include: a) instance Semiring Bool, b) a total instance Semiring Natural (in contrast to a partial instance Num Natural), c) instance Num a => Semiring (Complex a) (in contrast to instance RealFloat a => Num (Complex a)), d) newtypes Sum and Product would require only Semiring constraint instead of Num. Best regards, Andrew _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________ Libraries mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Open this post in threaded view
|

## Re: Break `abs` into two aspects

Open this post in threaded view
|

## Re: Break `abs` into two aspects

Open this post in threaded view
|

## Re: Break `abs` into two aspects

Open this post in threaded view
|

## Re: Break `abs` into two aspects

Open this post in threaded view
|

## Re: Break `abs` into two aspects

Open this post in threaded view
|

## Re: Break `abs` into two aspects

Open this post in threaded view
|