on master help sign in
Merge pull request #3565 from Synthetica9/Complex_Num_fix

Generalized the Num instance for Complex

commit4c914792914f37c1a14835684e7878c595ba6f50 authorNiklas Larsson <metaniklas@gmail.com> dateThu 5 Jan 2017, 16:08 (10 years ago) parent119d9643b22e26
1 file changed+11−3
libs/base/Data/Complex.idr+11−3
libs/base/Data/Complex.idr+11−3
@@ -63,15 +63,23 @@ conjugate (r:+i) = (r :+ (0-i))
6363implementation Functor Complex where
6464 map f (r :+ i) = f r :+ f i
6565
66−-- We can't do "implementation Num a => Num (Complex a)" because
67−-- we need "abs" which needs "magnitude" which needs "sqrt" which needs Double
68−implementation Num (Complex Double) where
66+-- We can do "implementation Neg a => Num (Complex a)" because
67+-- we don't need "abs", only (-)
68+implementation Neg a => Num (Complex a) where
6969 (+) (a:+b) (c:+d) = ((a+c):+(b+d))
7070 (*) (a:+b) (c:+d) = ((a*c-b*d):+(b*c+a*d))
7171 fromInteger x = (fromInteger x:+0)
7272
73+-- We can't do "implementation Neg a => Neg (Complex a)" because
74+-- we need "abs" which needs "magnitude" which needs "sqrt" which needs Double
7375implementation Neg (Complex Double) where
7476 negate = map negate
7577 (-) (a:+b) (c:+d) = ((a-c):+(b-d))
7678 abs (a:+b) = (magnitude (a:+b):+0)
7779
80+-- As soon as abs moves out of Neg (issue #3500), the following would become a
81+-- valid instance:
82+
83+-- implementation Neg a => Neg (Complex a) where
84+-- negate = map negate
85+-- (-) (a:+b) (c:+d) = ((a-c):+(b-d))