Search code examples
coq

exact value of the derivative on Coq


I want to represent the exact value of the derivative. I can calculate an approximation like this.

Require Import Coq.Reals.Reals.
Open Scope R_scope.

Definition QuadraticFunction (x:R) := x^2.

Definition differentiation (x:R)(I:R -> R):=
 let h := 0.000000001 in
 ((I (x+h)) - (I x)) / h.

But, we can not calculate the exact value of the derivative on the computer. For that reason, I want to represent the exact value with the inductive type or others somehow.

I know D_in of Reals.Rderiv, which returns Prop.

I need your help. Thank you.


Solution

  • I need to make four remarks

    • You should look at coquelicot, this is an extension library on top of Reals that has a nicer treatment of derivatives.

    • There is no inductive type involved in the presentation of real numbers. In fact, it is part of theoretical folklore that real numbers cannot be represented as an inductive type, in the sense that we usually mean. In an inductive type, you can usually compare two elements by a finite computation. In real numbers, such a comparison faces the difficulty that some numbers are defined by a process of infinite refinement. One of the foundations of the real numbers is that the set is complete, meaning that every Cauchy sequence has a limit. This is often used as a way to define new real numbers.

    • what does it mean to calculate a number? How do you calculate PI (the circle ratio). You cannot return 3.14, because it is not the exact value. So you need to keep PI as the result. But why would PI be better than (4 * atan(1)), or lim(4 - 4/3 + 4/5 - 4/7 ...)? So, you do not calculate real numbers as you would do with pocket calculators, because you need to keep the precision. The best you can do, is to return an exact representation as rational value when the real number is rational, an "understandable symbolic expression", or an interval approximation. But interval approximations are not exact, and understandable symbolic expression is an ambiguous specification. How do you choose which expression is most understandable?

    • There is no function that takes an arbitrary function and returns its derivative in a point as a real number, because we have to take into account that some functions are not derivable everywhere. The Reals library does have a function that makes it possible to talk about the value of a derivative for a derivable function. This is called derive.

    Here is a script that does the whole process.

    Require Import Coq.Reals.Reals.
    
    Require Import Coq.Reals.Reals.
     Open Scope R_scope.
    
    Definition QuadraticFunction (x:R) := x^2.
    
    Lemma derivable_qf : derivable QuadraticFunction.
    Proof.
    now repeat apply derivable_mult;
        (apply derivable_id || apply derivable_const).
    Qed.
    
    Definition QuadraticFunctionDerivative :=
      derive QuadraticFunction derivable_qf.
    

    Now you have a name for the derivative function, and you can even show that it is equal to another simple function. But whether this other simple function is the result of calculating the derivative is subjective. Here is an example using just the Reals library, but using Coquelicot would give a much more concise script (because derivative computation can be automated, interested readers should also look at the answer by @larsr to the same question).

    Lemma QuadraticFunctionDerivativeSimple (x : R) :
      QuadraticFunctionDerivative x = 2 * x.
    Proof.
    unfold QuadraticFunctionDerivative, derive, QuadraticFunction; simpl.
    rewrite derive_pt_eq.
    replace (2 * x) with (1 * (x * 1) + x * (1 * 1 + x * 0)) by ring.
    apply (derivable_pt_lim_mult (fun x => x) (fun x => x * 1)).
      apply derivable_pt_lim_id.
    apply (derivable_pt_lim_mult (fun x => x) (fun x => 1)).
      apply derivable_pt_lim_id.
    apply derivable_pt_lim_const.
    Qed.
    

    This is probably not the best way to solve the problem, but it is the one I came up with after thinking about the problem for a few minutes.