From c902382e3645777bf92f98fd8b873c0591921e25 Mon Sep 17 00:00:00 2001
From: Daniel Fischer <[email protected]>
Date: Tue, 17 May 2011 22:53:40 +0200
Subject: [PATCH] Adjust behaviour of gcd
Change behaviour of 'gcd 0 0' from raising an exception to
returning 0 to make it total and conform to mathematical practice.

GHC/Real.lhs  12 +++
1 files changed, 3 insertions(+), 9 deletions()
diff git a/GHC/Real.lhs b/GHC/Real.lhs
index 17d0452..0b4d4d5 100644
a

b

x ^^ n = if n >= 0 then x^n else recip (x^(negate n)) 
520  520  in if even e then (nn :% dd) else (negate nn :% dd) 
521  521  
522  522   
523     @'gcd' x y@ is the greatest (positive) integer that divides both @x@ 
 523    @'gcd' x y@ is the greatest (nonnegative) integer that divides both @x@ 
524  524   and @y@; for example @'gcd' (3) 6@ = @3@, @'gcd' (3) (6)@ = @3@, 
525    @'gcd' 0 4@ = @4@. @'gcd' 0 0@ raises a runtime error. 
 525   @'gcd' 0 4@ = @4@. @'gcd' 0 0@ = @0@. 
526  526  gcd :: (Integral a) => a > a > a 
527   gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined" 
528  527  gcd x y = gcd' (abs x) (abs y) 
529  528  where gcd' a 0 = a 
530  529  gcd' a b = gcd' b (a `rem` b) 
… 
… 
lcm x y = abs ((x `quot` (gcd x y)) * y) 
539  538  #ifdef OPTIMISE_INTEGER_GCD_LCM 
540  539  {# RULES 
541  540  "gcd/Int>Int>Int" gcd = gcdInt 
542   "gcd/Integer>Integer>Integer" gcd = gcdInteger' 
 541  "gcd/Integer>Integer>Integer" gcd = gcdInteger 
543  542  "lcm/Integer>Integer>Integer" lcm = lcmInteger 
544  543  #} 
545  544  
546   gcdInteger' :: Integer > Integer > Integer 
547   gcdInteger' 0 0 = error "GHC.Real.gcdInteger': gcd 0 0 is undefined" 
548   gcdInteger' a b = gcdInteger a b 
549   
550  545  gcdInt :: Int > Int > Int 
551   gcdInt 0 0 = error "GHC.Real.gcdInt: gcd 0 0 is undefined" 
552  546  gcdInt a b = fromIntegral (gcdInteger (fromIntegral a) (fromIntegral b)) 
553  547  #endif 
554  548  