Euclidean algorithm for ℕ #

This file sets up a version of the Euclidean algorithm that only works with natural numbers. Given 0 < a, b, it computes the unique (w, x, y, z, d) such that the following identities hold:

This story is closely related to the structure of SL₂(ℕ) (as a free monoid on two generators) and the theory of continued fractions.

Main declarations #

Notes #

See Nat.Xgcd for a very similar algorithm allowing values in .

structure PNat.XgcdType :
  • wp :

    wp is a variable which changes through the algorithm.

  • x :

    x satisfies a / d = w + x at the final step.

  • y :

    y satisfies b / d = z + y at the final step.

  • zp :

    zp is a variable which changes through the algorithm.

  • ap :

    ap is a variable which changes through the algorithm.

  • bp :

    bp is a variable which changes through the algorithm.

A term of XgcdType is a system of six naturals. They should be thought of as representing the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] together with the vector [a, b] = [ap + 1, bp + 1].

Instances For

    The Repr instance converts terms to strings in a way that reflects the matrix/vector interpretation as above.

    def' (w : ℕ+) (x : ) (y : ) (z : ℕ+) (a : ℕ+) (b : ℕ+) :

    Another mk using ℕ and ℕ+

    Instances For

      w = wp + 1

      Instances For

        z = zp + 1

        Instances For

          a = ap + 1

          Instances For

            b = bp + 1

            Instances For

              r = a % b: remainder

              Instances For

                q = ap / bp: quotient

                Instances For

                  qp = q - 1

                  Instances For

                    The map v gives the product of the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] and the vector [a, b] = [ap + 1, bp + 1]. The map vp gives [sp, tp] such that v = [sp + 1, tp + 1].

                    Instances For

                      v = [sp + 1, tp + 1], check vp

                      Instances For

                        succ₂ [t.1, t.2] = [t.1.succ, t.2.succ]

                        Instances For

                          IsSpecial holds if the matrix has determinant one.

                          Instances For

                            IsSpecial' is an alternative of IsSpecial.

                            Instances For

                              IsReduced holds if the two entries in the vector are the same. The reduction algorithm will produce a system with this property, whose product vector is the same as for the original system.

                              Instances For

                                IsReduced' is an alternative of IsReduced.

                                Instances For

                                  flip flips the placement of variables during the algorithm.

                                  Instances For

                                    Properties of division with remainder for a / b.

                                    The following function provides the starting point for our algorithm. We will apply an iterative reduction process to it, which will produce a system satisfying IsReduced. The gcd can be read off from this final system.

                                    Instances For

                                      finish happens when the reducing process ends.

                                      Instances For

                                        This is the main reduction step, which is used when u.r ≠ 0, or equivalently b does not divide a.

                                        Instances For

                                          We will apply the above step recursively. The following result is used to ensure that the process terminates.

                                          The reduction step does not change the product vector.

                                          We can now define the full reduction function, which applies step as long as possible, and then applies finish. Note that the "have" statement puts a fact in the local context, and the equation compiler uses this fact to help construct the full definition in terms of well-founded recursion. The same fact needs to be introduced in all the inductive proofs of properties given below.

                                          Instances For
                                            def PNat.xgcd (a : ℕ+) (b : ℕ+) :

                                            Extended Euclidean algorithm

                                            Instances For
                                              def PNat.gcdD (a : ℕ+) (b : ℕ+) :

                                              gcdD a b = gcd a b

                                              Instances For
                                                def PNat.gcdW (a : ℕ+) (b : ℕ+) :

                                                Final value of w

                                                Instances For
                                                  def PNat.gcdX (a : ℕ+) (b : ℕ+) :

                                                  Final value of x

                                                  Instances For
                                                    def PNat.gcdY (a : ℕ+) (b : ℕ+) :

                                                    Final value of y

                                                    Instances For
                                                      def PNat.gcdZ (a : ℕ+) (b : ℕ+) :

                                                      Final value of z

                                                      Instances For
                                                        def PNat.gcdA' (a : ℕ+) (b : ℕ+) :

                                                        Final value of a / d

                                                        Instances For
                                                          def PNat.gcdB' (a : ℕ+) (b : ℕ+) :

                                                          Final value of b / d

                                                          Instances For
                                                            theorem PNat.gcdA'_coe (a : ℕ+) (b : ℕ+) :
                                                            ↑(PNat.gcdA' a b) = ↑(PNat.gcdW a b) + PNat.gcdX a b
                                                            theorem PNat.gcdB'_coe (a : ℕ+) (b : ℕ+) :
                                                            ↑(PNat.gcdB' a b) = PNat.gcdY a b + ↑(PNat.gcdZ a b)
                                                            theorem PNat.gcd_props (a : ℕ+) (b : ℕ+) :
                                                            let d := PNat.gcdD a b; let w := PNat.gcdW a b; let x := PNat.gcdX a b; let y := PNat.gcdY a b; let z := PNat.gcdZ a b; let a' := PNat.gcdA' a b; let b' := PNat.gcdB' a b; w * z = Nat.succPNat (x * y) a = a' * d b = b' * d z * a' = Nat.succPNat (x * b') w * b' = Nat.succPNat (y * a') z * a = x * b + d w * b = y * a + d
                                                            theorem PNat.gcd_eq (a : ℕ+) (b : ℕ+) :
                                                            theorem PNat.gcd_a_eq (a : ℕ+) (b : ℕ+) :
                                                            theorem PNat.gcd_b_eq (a : ℕ+) (b : ℕ+) :
                                                            theorem PNat.gcd_rel_left (a : ℕ+) (b : ℕ+) :
                                                            ↑(PNat.gcdZ a b) * a = PNat.gcdX a b * b + ↑(PNat.gcd a b)
                                                            theorem PNat.gcd_rel_right (a : ℕ+) (b : ℕ+) :
                                                            ↑(PNat.gcdW a b) * b = PNat.gcdY a b * a + ↑(PNat.gcd a b)