-(^) :: (Num a, Integral b) => a -> b -> a
-_ ^ 0 = 1
-x ^ n | n > 0 = f x (n-1) x
- where f _ 0 y = y
- f a d y = g a d where
- g b i | even i = g (b*b) (i `quot` 2)
- | otherwise = f b (i-1) (b*y)
-_ ^ _ = error "Prelude.^: negative exponent"
+(^) :: (Num a, Integral b) => a -> b -> a
+x0 ^ y0 | y0 < 0 = error "Negative exponent"
+ | y0 == 0 = 1
+ | otherwise = f x0 y0
+ where -- f : x0 ^ y0 = x ^ y
+ f x y | even y = f (x * x) (y `quot` 2)
+ | y == 1 = x
+ | otherwise = g (x * x) ((y - 1) `quot` 2) x
+ -- g : x0 ^ y0 = (x ^ y) * z
+ g x y z | even y = g (x * x) (y `quot` 2) z
+ | y == 1 = x * z
+ | otherwise = g (x * x) ((y - 1) `quot` 2) (x * z)