@@ -368,7 +368,17 @@ Again, trivial definition in Haskell
368368> :* (g :* derive f :+ f :* (Log :. f) :* derive g)
369369> derive Id = Const 1
370370> derive (Const _) = Const 0
371- > derive (f :. g) = derive g :* (derive f :. g)
371+
372+ For a function composition $ f \circ g$ we have to handle the case
373+ where $ f$ is a constant function, as we may get division by zero if we
374+ naively apply the chain rule. We'll make use of the `simplify` function,
375+ which we'll define later, to reduce compositions of constant functions to
376+ just constant functions.
377+
378+ > derive (f :. g) =
379+ > case simplify f of
380+ > Const a -> Const 0
381+ > sf -> (derive sf :. g) :* derive g
372382> derive (Delta h f) = Delta h (derive f)
373383> derive (D f) = derive (derive f)
374384
@@ -441,7 +451,7 @@ For addition and subtraction, it's $0$
441451> -> simplify (Const (a + b) :* f')
442452> (f', g') -> f' :+ g'
443453> simplify (f :- g) = case (simplify f, simplify g) of
444- > (f', Const 0 :- g') -> f' :+ g'
454+ > (f', Const 0 :- g') -> simplify ( f' :+ g')
445455> (f', Const 0 ) -> f'
446456> (Const a, Const b) -> if a > b
447457> then Const (a - b)
@@ -498,9 +508,10 @@ $$b^{-n} = \frac{1}{b^n}$$
498508Intuitively, the identity function is the identity element for function composition
499509
500510> simplify (f :. g) = case (simplify f, simplify g) of
501- > (Id , g') -> g'
502- > (f', Id ) -> f'
503- > (f', g') -> f' :. g'
511+ > (Const a, _) -> Const a
512+ > (Id , g') -> g'
513+ > (f', Id ) -> f'
514+ > (f', g') -> f' :. g'
504515
505516> simplify (Delta h f) = Delta h (simplify f)
506517> simplify (D (I f')) = simplify f'
0 commit comments