Permalink
Browse files

Adding Eq and Show constraints as appropriate

  • Loading branch information...
1 parent c40a53a commit f2d81f868466e7028bbd8744788b1cf9e9efd02e @DanBurton DanBurton committed Feb 21, 2012
Showing with 15 additions and 14 deletions.
  1. +1 −0 .gitignore
  2. +8 −8 Data/Number/Dif.hs
  3. +6 −6 Data/Number/Symbolic.hs
View
@@ -0,0 +1 @@
+/dist
View
@@ -36,13 +36,13 @@ dCon x = C x
-- |The 'dVar' function turns a number into a variable
-- number. This is the number with with respect to which
-- the derivaticve is computed.
-dVar :: (Num a) => a -> Dif a
+dVar :: (Num a, Eq a) => a -> Dif a
dVar x = D x 1
-- |The 'df' takes a 'Dif' number and returns its first
-- derivative. The function can be iterated to to get
-- higher derivaties.
-df :: (Num a) => Dif a -> Dif a
+df :: (Num a, Eq a) => Dif a -> Dif a
df (D _ x') = x'
df (C _ ) = 0
@@ -64,11 +64,11 @@ mkDif = D
--
-- > deriv f = val . df . f . dVar
--
-deriv :: (Num a, Num b) => (Dif a -> Dif b) -> (a -> b)
+deriv :: (Num a, Num b, Eq a, Eq b) => (Dif a -> Dif b) -> (a -> b)
deriv f = val . df . f . dVar
-- |Convert a 'Dif' function to an ordinary function.
-unDif :: (Num a) => (Dif a -> Dif b) -> (a -> b)
+unDif :: (Num a, Eq a) => (Dif a -> Dif b) -> (a -> b)
unDif f = val . f . dVar
instance (Show a) => Show (Dif a) where
@@ -83,7 +83,7 @@ instance (Eq a) => Eq (Dif a) where
instance (Ord a) => Ord (Dif a) where
x `compare` y = val x `compare` val y
-instance (Num a) => Num (Dif a) where
+instance (Num a, Eq a) => Num (Dif a) where
(C x) + (C y) = C (x + y)
(C x) + (D y y') = D (x + y) y'
(D x x') + (C y) = D (x + y) x'
@@ -115,18 +115,18 @@ instance (Num a) => Num (Dif a) where
signum (C x) = C (signum x)
signum (D x _) = C (signum x)
-instance (Fractional a) => Fractional (Dif a) where
+instance (Fractional a, Eq a) => Fractional (Dif a) where
recip (C x) = C (recip x)
recip (D x x') = ip
where ip = D (recip x) (-x' * ip * ip)
fromRational r = C (fromRational r)
-lift :: (Num a) => [a -> a] -> Dif a -> Dif a
+lift :: (Num a, Eq a) => [a -> a] -> Dif a -> Dif a
lift (f : _) (C x) = C (f x)
lift (f : f') p@(D x x') = D (f x) (x' * lift f' p)
lift _ _ = error "lift"
-instance (Floating a) => Floating (Dif a) where
+instance (Floating a, Eq a) => Floating (Dif a) where
pi = C pi
exp (C x) = C (exp x)
View
@@ -33,7 +33,7 @@ con = Con
-- | The expression @subst x v e@ substitutes the expression @v@ for each
-- occurence of the variable @x@ in @e@.
-subst :: (Num a) => String -> Sym a -> Sym a -> Sym a
+subst :: (Num a, Eq a) => String -> Sym a -> Sym a -> Sym a
subst _ _ e@(Con _) = e
subst x v e@(App x' _ []) | x == x' = v
| otherwise = e
@@ -65,7 +65,7 @@ instance (Show a) => Show (Sym a) where
showsPrec p (App f _ xs) =
showParen (p>10) (foldl (.) (showString f) (map (\ x -> showChar ' ' . showsPrec 11 x) xs))
-instance (Num a) => Num (Sym a) where
+instance (Num a, Eq a) => Num (Sym a) where
x + y = binOp (+) x "+" y
x - y = binOp (-) x "-" y
x * y = binOp (*) x "*" y
@@ -74,12 +74,12 @@ instance (Num a) => Num (Sym a) where
signum x = unOp signum "signum" x
fromInteger x = Con (fromInteger x)
-instance (Fractional a) => Fractional (Sym a) where
+instance (Fractional a, Eq a) => Fractional (Sym a) where
x / y = binOp (/) x "/" y
fromRational x = Con (fromRational x)
-- Assume the numbers are a field and simplify a little
-binOp :: (Num a) => (a->a->a) -> Sym a -> String -> Sym a -> Sym a
+binOp :: (Num a, Eq a) => (a->a->a) -> Sym a -> String -> Sym a -> Sym a
binOp f (Con x) _ (Con y) = Con (f x y)
binOp _ x "+" 0 = x
binOp _ 0 "+" x = x
@@ -141,7 +141,7 @@ instance (Real a) => Real (Sym a) where
instance (RealFrac a) => RealFrac (Sym a) where
properFraction (Con c) = (i, Con c') where (i, c') = properFraction c
-instance (Floating a) => Floating (Sym a) where
+instance (Floating a, Eq a) => Floating (Sym a) where
pi = var "pi"
exp = unOp exp "exp"
sqrt = unOp sqrt "sqrt"
@@ -161,7 +161,7 @@ instance (Floating a) => Floating (Sym a) where
atanh = unOp atanh "atanh"
acosh = unOp acosh "acosh"
-instance (RealFloat a) => RealFloat (Sym a) where
+instance (RealFloat a, Show a) => RealFloat (Sym a) where
floatRadix = floatRadix . unSym
floatDigits = floatDigits . unSym
floatRange = floatRange . unSym

0 comments on commit f2d81f8

Please sign in to comment.