Langlib

Langlib.Automata.LinearBounded.RowNumeral

Synchronous finite-row numerals #

This file supplies small, reusable finite-state verifiers for counters stored across a row. Digits are written least significant first, so carry propagation is checked in one left-to-right sweep, exactly the direction used by CertifiedRowSystem.

The digit order is made explicit by a DigitCodec. This keeps the scanners independent of a particular order instance; orderedCodec supplies the canonical increasing codec for every finite linear order.

structure RowNumeral.DigitCodec (D : Type u_1) [Fintype D] :
Type u_1

A numbering of a finite digit type by 0, ..., card D - 1.

Instances For

    The canonical increasing numbering of a finite linearly ordered digit type.

    Equations
    Instances For

      The radix of a row numeral.

      Equations
      Instances For
        def RowNumeral.DigitCodec.digitValue {D : Type u_1} [Fintype D] (E : DigitCodec D) (d : D) :

        Numeric value of one digit.

        Equations
        Instances For
          def RowNumeral.DigitCodec.zero {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) :
          D

          The zero digit.

          Equations
          Instances For
            @[simp]
            theorem RowNumeral.DigitCodec.toFin_zero {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) :
            E.toFin E.zero = 0,
            def RowNumeral.DigitCodec.last {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) :
            D

            The greatest digit.

            Equations
            Instances For
              def RowNumeral.DigitCodec.next {D : Type u_1} [Fintype D] (E : DigitCodec D) (d : D) :

              The next digit, or none at the greatest digit.

              Equations
              Instances For
                theorem RowNumeral.DigitCodec.next_eq_some_iff {D : Type u_1} [Fintype D] (E : DigitCodec D) (d d' : D) :
                E.next d = some d' E.digitValue d + 1 < E.radix E.digitValue d' = E.digitValue d + 1
                def RowNumeral.DigitCodec.one {D : Type u_1} [Fintype D] (E : DigitCodec D) (h : 1 < E.radix) :
                D

                The digit one, available when the radix is at least two.

                Equations
                Instances For
                  @[simp]
                  theorem RowNumeral.DigitCodec.digitValue_one {D : Type u_1} [Fintype D] (E : DigitCodec D) (h : 1 < E.radix) :
                  E.digitValue (E.one h) = 1
                  def RowNumeral.DigitCodec.value {D : Type u_1} [Fintype D] (E : DigitCodec D) :
                  List D

                  Value of an LSD-first digit row.

                  Equations
                  Instances For
                    def RowNumeral.DigitCodec.zeroRow {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) (width : ) :

                    The all-zero row of a given width.

                    Equations
                    Instances For
                      def RowNumeral.DigitCodec.oneRow {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) (h : 1 < E.radix) :
                      List D

                      The numeral one at a positive width. At width zero it is the empty row.

                      Equations
                      Instances For
                        @[simp]
                        theorem RowNumeral.DigitCodec.length_zeroRow {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) (width : ) :
                        (E.zeroRow width).length = width
                        @[simp]
                        theorem RowNumeral.DigitCodec.length_oneRow {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) (h : 1 < E.radix) (width : ) :
                        (E.oneRow h width).length = width
                        @[simp]
                        theorem RowNumeral.DigitCodec.value_zeroRow {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) (width : ) :
                        E.value (E.zeroRow width) = 0
                        @[simp]
                        theorem RowNumeral.DigitCodec.value_oneRow {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) (h : 1 < E.radix) {width : } (hw : 0 < width) :
                        E.value (E.oneRow h width) = 1

                        Cellwise zero and copy checks #

                        def RowNumeral.DigitCodec.zeroStep {D : Type u_1} [Fintype D] [Nonempty D] [DecidableEq D] (E : DigitCodec D) (ok : Bool) (d : D) :

                        One cell of an all-zero check. false is an absorbing failure state.

                        Equations
                        Instances For
                          def RowNumeral.DigitCodec.copyStep {D : Type u_1} [Fintype D] [DecidableEq D] (_E : DigitCodec D) (ok : Bool) (old new : D) :

                          One cell of a synchronous equality/copy check.

                          Equations
                          Instances For

                            State of the one-row checker.

                            Instances For

                              Run a zero checker over a whole row.

                              Equations
                              Instances For
                                def RowNumeral.DigitCodec.checkCopy {D : Type u_1} [Fintype D] [DecidableEq D] (E : DigitCodec D) :
                                List DList DBool

                                Run a copy/equality checker over two rows; a length mismatch rejects.

                                Equations
                                Instances For
                                  def RowNumeral.DigitCodec.checkOne {D : Type u_1} [Fintype D] [Nonempty D] [DecidableEq D] (E : DigitCodec D) (h : 1 < E.radix) :
                                  List DBool

                                  Run the canonical-one checker. The empty row rejects.

                                  Equations
                                  Instances For
                                    @[simp]
                                    @[simp]
                                    theorem RowNumeral.DigitCodec.checkCopy_eq_true_iff {D : Type u_1} [Fintype D] [DecidableEq D] (E : DigitCodec D) (old new : List D) :
                                    E.checkCopy old new = true new = old
                                    @[simp]
                                    theorem RowNumeral.DigitCodec.checkOne_eq_true_iff {D : Type u_1} [Fintype D] [Nonempty D] [DecidableEq D] (E : DigitCodec D) (h : 1 < E.radix) (row : List D) :
                                    E.checkOne h row = true 0 < row.length row = E.oneRow h row.length

                                    Synchronous successor #

                                    State of the one-pass successor verifier.

                                    Instances For
                                      Equations
                                      Instances For

                                        Check one aligned pair of digits while propagating a carry from left to right.

                                        Equations
                                        Instances For

                                          Run the synchronous successor verifier; a length mismatch rejects.

                                          Equations
                                          Instances For

                                            Increment an LSD-first row, returning an overflow bit.

                                            Equations
                                            Instances For
                                              @[simp]
                                              theorem RowNumeral.DigitCodec.increment_length {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) (row : List D) :
                                              (E.increment row).1.length = row.length

                                              Overflow occurs exactly at the all-greatest digit row.

                                              theorem RowNumeral.DigitCodec.increment_fst_of_overflow {D : Type u_1} [Fintype D] [Nonempty D] (E : DigitCodec D) (row : List D) (h : (E.increment row).2 = true) :
                                              (E.increment row).1 = E.zeroRow row.length

                                              On overflow, canonical increment wraps to the all-zero row.

                                              A successful non-overflowing scan is exactly canonical row successor.

                                              A carry remaining after the last cell is exactly canonical overflow.

                                              Synchronous comparison #

                                              The scan still runs from least to most significant digit. A newly read unequal digit is more significant than everything seen before, so it replaces the accumulated ordering.

                                              def RowNumeral.DigitCodec.compareStep {D : Type u_1} [Fintype D] (E : DigitCodec D) (q : Ordering) (x y : D) :

                                              Compare one aligned pair during an LSD-first scan.

                                              Equations
                                              Instances For

                                                Run the synchronous comparison scan; a length mismatch rejects.

                                                Equations
                                                Instances For

                                                  Canonical LSD-first comparison of two rows.

                                                  Equations
                                                  Instances For
                                                    def RowNumeral.DigitCodec.rowsEqual {D : Type u_1} [Fintype D] (E : DigitCodec D) (xs ys : List D) :

                                                    Equality result of the synchronous comparison.

                                                    Equations
                                                    Instances For
                                                      def RowNumeral.DigitCodec.rowsLess {D : Type u_1} [Fintype D] (E : DigitCodec D) (xs ys : List D) :

                                                      Strict less-than result of the synchronous comparison.

                                                      Equations
                                                      Instances For