{-# LANGUAGE TypeSynonymInstances #-} module RealFunction where type RealFunction a = (a -> Double) instance Show (RealFunction a) where show _ = "Real Function" instance Eq (RealFunction a) where _ == _ = False instance Num (RealFunction a) where (f1 + f2) x = (f1 x) + (f2 x) (f1 - f2) x = (f1 x) - (f2 x) (f1 * f2) x = (f1 x) * (f2 x) (negate f) x = -1 * (f x) (abs f) x = abs (f x) (signum f) x = signum (f x) fromInteger i _ = fromInteger i -- Takes a constant, and a function as arguments. Returns a new -- function representing the original function times the constant. cmult :: Double -> (RealFunction a) -> (RealFunction a) cmult coeff f = (*coeff) . f -- Takes a function f and an exponent n. Returns a new function, f^n. fexp :: (RealFunction a) -> Int -> (RealFunction a) fexp f n | n == 0 = (\_ -> 1) | otherwise = \x -> (f x)^n