From 7b04d35a87f1ee2904579a916ef1a6b0aa6fc7b8 Mon Sep 17 00:00:00 2001
From: Herbert Valerio Riedel
Date: Sat, 19 Apr 2014 13:56:36 +0200
Subject: [PATCH] Weaken constraints on Data.Complex functions
The RealFloat data type context was dropped from Data.Complex some time
ago (see ea280135dd888ac41d8804a9e37e358180cf13ac). However, the rest of
the API in that module was left as-is even though many of the accessors
in that module would work with much more general types now.
This change simplifies the signatures of the functions above, which in
the current API all unnecessarily take a RealFloat constraint that they
don't use (and which may cause to pass around superflous type-class
dictionaries):
realPart :: Complex a -> a
imagPart :: Complex a -> a
conjugate :: Num a => Complex a -> Complex a
mkPolar :: Floating a => a -> a -> Complex a
cis :: Floating a => a -> Complex a
This was originally proposed by Edward Kmett in
http://www.haskell.org/pipermail/libraries/2014-March/022358.html
Signed-off-by: Herbert Valerio Riedel
---
libraries/base/Data/Complex.hs | 10 +++++-----
libraries/base/changelog.md | 2 ++
2 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/libraries/base/Data/Complex.hs b/libraries/base/Data/Complex.hs
index 190c5981155..b03848b636a 100644
--- a/libraries/base/Data/Complex.hs
+++ b/libraries/base/Data/Complex.hs
@@ -58,27 +58,27 @@ data Complex a
-- Functions over Complex
-- | Extracts the real part of a complex number.
-realPart :: (RealFloat a) => Complex a -> a
+realPart :: Complex a -> a
realPart (x :+ _) = x
-- | Extracts the imaginary part of a complex number.
-imagPart :: (RealFloat a) => Complex a -> a
+imagPart :: Complex a -> a
imagPart (_ :+ y) = y
-- | The conjugate of a complex number.
{-# SPECIALISE conjugate :: Complex Double -> Complex Double #-}
-conjugate :: (RealFloat a) => Complex a -> Complex a
+conjugate :: Num a => Complex a -> Complex a
conjugate (x:+y) = x :+ (-y)
-- | Form a complex number from polar components of magnitude and phase.
{-# SPECIALISE mkPolar :: Double -> Double -> Complex Double #-}
-mkPolar :: (RealFloat a) => a -> a -> Complex a
+mkPolar :: Floating a => a -> a -> Complex a
mkPolar r theta = r * cos theta :+ r * sin theta
-- | @'cis' t@ is a complex value with magnitude @1@
-- and phase @t@ (modulo @2*'pi'@).
{-# SPECIALISE cis :: Double -> Complex Double #-}
-cis :: (RealFloat a) => a -> Complex a
+cis :: Floating a => a -> Complex a
cis theta = cos theta :+ sin theta
-- | The function 'polar' takes a complex number and
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 3011fdfdc79..a72e4e68715 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -8,6 +8,8 @@
* Add `Data.List.sortOn` sorting function
+ * Weaken RealFloat constraints on some `Data.Complex` functions
+
## 4.7.0.0 *Apr 2014*
* Bundled with GHC 7.8.1
--
GitLab