```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 ``` ```class Val a where val :: a -> Double data Var = Var Double instance Val Var where val (Var v) = v data Fun = Fun Double Double instance Show Fun where show (Fun v d) = show v ++ ", " ++ show d instance Eq Fun where (==) (Fun av ad) (Fun bv bd) = av == bv && ad == bd (/=) (Fun av ad) (Fun bv bd) = av /= bv || ad /= bd instance Num Fun where (+) (Fun av ad) (Fun bv bd) = Fun (av + bv) (ad + bd) (*) (Fun av ad) (Fun bv bd) = Fun (av * bv) (ad*bv + av*bd) (-) (Fun av ad) (Fun bv bd) = Fun (av - bv) (ad - bd) instance Val Fun where val (Fun v d) = v instance Fractional Fun where (/) (Fun av ad) (Fun bv bd) = Fun (av/bv) ((ad*bv - av*bd)/(bv*bv)) fromRational r = Fun (fromRational r) 0.0 instance Floating Fun where exp (Fun v d) = Fun (exp v) ((exp v) * d) log (Fun v d) = Fun (log v) (d / v) sin (Fun v d) = Fun (sin v) ((cos v) * d) cos (Fun v d) = Fun (cos v) (-(sin v) * d) asin (Fun v d) = Fun (asin v) (d / sqrt(1.0 - v**2.0)) acos (Fun v d) = Fun (acos v) (-d / sqrt(1.0 - v**2.0)) atan (Fun v d) = Fun (atan v) (d / (1.0 + v**2.0)) sinh (Fun v d) = Fun (sinh v) ((cosh v) * d) cosh (Fun v d) = Fun (sinh v) ((sinh v) * d) asinh (Fun v d) = Fun (asinh v) (d / sqrt(1.0 + v**2.0)) acosh (Fun v d) = Fun (acosh v) (d / (sqrt(1.0 - v**2.0)*sqrt(1.0 + v**2.0))) atanh (Fun v d) = Fun (atan v) (d / (1.0 - v**2.0)) toFun (Var v) = Fun v 1.0 der (Fun _ d) = d main = let x = toFun \$ Var 3.141592653589793 in do print \$ (x**2.0) + 2.0*x + 3.0 print \$ 2.0*(exp (sin x)) + x print \$ x/x print \$ tan x ```
 ```1 2 3 4 ``` ```19.1527897082689, 8.28318530717959 5.14159265358979, -1.0 1.0, 0.0 -1.22460635382238e-16, 1.0 ```