Documentation

Mathlib.Tactic.NormNum.Result

The Result type for norm_num #

We set up predicates IsNat, IsInt, and IsRat, stating that an element of a ring is equal to the "normal form" of a natural number, integer, or rational number coerced into that ring.

We then define Result e, which contains a proof that a typed expression e : Q($α) is equal to the coercion of an explicit natural number, integer, or rational number, or is either true or false.

@[implicit_reducible]

A shortcut (non)instance for AddMonoidWithOne α from Semiring α to shrink generated proofs.

Instances For
    @[implicit_reducible]

    A shortcut (non)instance for AddMonoidWithOne α from Ring α to shrink generated proofs.

    Instances For

      A shortcut (non)instance for Nat.AtLeastTwo (n + 2) to shrink generated proofs.

      def Mathlib.Meta.NormNum.inferAddMonoidWithOne {u : Lean.Level} (α : Q(Type u)) :
      Lean.MetaM Q(AddMonoidWithOne «$α»)

      Helper function to synthesize a typed AddMonoidWithOne α expression.

      Instances For
        def Mathlib.Meta.NormNum.inferSemiring {u : Lean.Level} (α : Q(Type u)) :
        Lean.MetaM Q(Semiring «$α»)

        Helper function to synthesize a typed Semiring α expression.

        Instances For
          def Mathlib.Meta.NormNum.inferRing {u : Lean.Level} (α : Q(Type u)) :
          Lean.MetaM Q(Ring «$α»)

          Helper function to synthesize a typed Ring α expression.

          Instances For
            def Mathlib.Meta.NormNum.mkRawIntLit (n : ) :
            Q()

            Represent an integer as a "raw" typed expression.

            This uses .lit (.natVal n) internally to represent a natural number, rather than the preferred OfNat.ofNat form. We use this internally to avoid unnecessary typeclass searches.

            This function is the inverse of Expr.intLit!.

            Instances For
              def Mathlib.Meta.NormNum.mkRawRatLit (q : ) :
              Q()

              Represent an integer as a "raw" typed expression.

              This .lit (.natVal n) internally to represent a natural number, rather than the preferred OfNat.ofNat form. We use this internally to avoid unnecessary typeclass searches.

              Instances For
                def Mathlib.Meta.NormNum.rawIntLitNatAbs (n : Q()) :
                (m : Q()) × Q(«$n».natAbs = «$m»)

                Extract the raw natlit representing the absolute value of a raw integer literal (of the type produced by Mathlib.Meta.NormNum.mkRawIntLit) along with an equality proof.

                Instances For
                  def Mathlib.Meta.NormNum.mkOfNat {u : Lean.Level} (α : Q(Type u)) (_sα : Q(AddMonoidWithOne «$α»)) (lit : Q()) :
                  Lean.MetaM ((a' : Q(«$α»)) × Q(«$lit» = «$a'»))

                  Constructs an ofNat application a' with the canonical instance, together with a proof that the instance is equal to the result of Nat.cast on the given AddMonoidWithOne instance.

                  This function is performance-critical, as many higher level tactics have to construct numerals. So rather than using typeclass search we hardcode the (relatively small) set of solutions to the typeclass problem.

                  Instances For
                    structure Mathlib.Meta.NormNum.IsNat {α : Type u} [AddMonoidWithOne α] (a : α) (n : ) :

                    Assert that an element of a semiring is equal to the coercion of some natural number.

                    • out : a = n

                      The element is equal to the coercion of the natural number.

                    Instances For
                      def Nat.rawCast {α : Type u} [AddMonoidWithOne α] (n : ) :
                      α

                      A "raw nat cast" is an expression of the form (Nat.rawCast lit : α) where lit is a raw natural number literal. These expressions are used by tactics like ring to decrease the number of typeclass arguments required in each use of a number literal at type α.

                      Instances For
                        theorem Mathlib.Meta.NormNum.IsNat.to_eq {α : Type u} [AddMonoidWithOne α] {n : } {a a' : α} :
                        IsNat a nn = a'a = a'
                        theorem Mathlib.Meta.NormNum.IsNat.to_raw_eq {α : Type u} {a : α} {n : } [AddMonoidWithOne α] :
                        IsNat a na = n.rawCast
                        theorem Mathlib.Meta.NormNum.isNat.natElim {p : Prop} {n n' : } :
                        IsNat n n'p n'p n
                        structure Mathlib.Meta.NormNum.IsInt {α : Type u} [Ring α] (a : α) (n : ) :

                        Assert that an element of a ring is equal to the coercion of some integer.

                        • out : a = n

                          The element is equal to the coercion of the integer.

                        Instances For
                          def Int.rawCast {α : Type u} [Ring α] (n : ) :
                          α

                          A "raw int cast" is an expression of the form:

                          • (Nat.rawCast lit : α) where lit is a raw natural number literal
                          • (Int.rawCast (Int.negOfNat lit) : α) where lit is a nonzero raw natural number literal

                          (That is, we only actually use this function for negative integers.) This representation is used by tactics like ring to decrease the number of typeclass arguments required in each use of a number literal at type α.

                          Instances For
                            theorem Mathlib.Meta.NormNum.IsInt.to_isNat {α : Type u_1} [Ring α] {a : α} {n : } :
                            IsInt a (Int.ofNat n)IsNat a n
                            theorem Mathlib.Meta.NormNum.IsNat.to_isInt {α : Type u_1} [Ring α] {a : α} {n : } :
                            IsNat a nIsInt a (Int.ofNat n)
                            theorem Mathlib.Meta.NormNum.IsInt.to_raw_eq {α : Type u} {a : α} {n : } [Ring α] :
                            IsInt a na = n.rawCast
                            theorem Mathlib.Meta.NormNum.IsInt.neg_to_eq {α : Type u_1} [Ring α] {n : } {a a' : α} :
                            IsInt a (Int.negOfNat n)n = a'a = -a'
                            theorem Mathlib.Meta.NormNum.IsInt.nonneg_to_eq {α : Type u_1} [Ring α] {n : } {a a' : α} (h : IsInt a (Int.ofNat n)) (e : n = a') :
                            a = a'
                            inductive Mathlib.Meta.NormNum.IsRat {α : Type u} [Ring α] (a : α) (num : ) (denom : ) :

                            Assert that an element of a ring is equal to num / denom (and denom is invertible so that this makes sense). We will usually also have num and denom coprime, although this is not part of the definition.

                            • mk {α : Type u} [Ring α] {a : α} {num : } {denom : } (inv : Invertible denom) (eq : a = num * denom) : IsRat a num denom
                            Instances For
                              inductive Mathlib.Meta.NormNum.IsNNRat {α : Type u} [Semiring α] (a : α) (num denom : ) :

                              Assert that an element of a semiring is equal to num / denom (and denom is invertible so that this makes sense). We will usually also have num and denom coprime, although this is not part of the definition.

                              Instances For
                                def NNRat.rawCast {α : Type u} [DivisionSemiring α] (n d : ) :
                                α

                                A "raw nnrat cast" is an expression of the form:

                                • (Nat.rawCast lit : α) where lit is a raw natural number literal
                                • (NNRat.rawCast n d : α) where n is a raw nat literal, d is a raw nat literal, and d is not 1 or 0.

                                This representation is used by tactics like ring to decrease the number of typeclass arguments required in each use of a number literal at type α.

                                Instances For
                                  def Rat.rawCast {α : Type u} [DivisionRing α] (n : ) (d : ) :
                                  α

                                  A "raw rat cast" is an expression of the form:

                                  • (Nat.rawCast lit : α) where lit is a raw natural number literal
                                  • (Int.rawCast (Int.negOfNat lit) : α) where lit is a nonzero raw natural number literal
                                  • (NNRat.rawCast n d : α) where n is a raw nat literal, d is a raw nat literal, and d is not 1 or 0.
                                  • (Rat.rawCast (Int.negOfNat n) d : α) where n is a raw nat literal, d is a raw nat literal, n is not 0, and d is not 1 or 0.

                                  This representation is used by tactics like ring to decrease the number of typeclass arguments required in each use of a number literal at type α.

                                  Instances For
                                    theorem Mathlib.Meta.NormNum.IsNNRat.to_isNat {α : Type u_1} [Semiring α] {a : α} {n : } :
                                    IsNNRat a n 1IsNat a n
                                    theorem Mathlib.Meta.NormNum.IsRat.to_isNNRat {α : Type u_1} [Ring α] {a : α} {n d : } :
                                    IsRat a (Int.ofNat n) dIsNNRat a n d
                                    theorem Mathlib.Meta.NormNum.IsNat.to_isNNRat {α : Type u_1} [Semiring α] {a : α} {n : } :
                                    IsNat a nIsNNRat a n 1
                                    theorem Mathlib.Meta.NormNum.IsNNRat.to_isRat {α : Type u_1} [Ring α] {a : α} {n d : } :
                                    IsNNRat a n dIsRat a (Int.ofNat n) d
                                    theorem Mathlib.Meta.NormNum.IsRat.to_isInt {α : Type u_1} [Ring α] {a : α} {n : } :
                                    IsRat a n 1IsInt a n
                                    theorem Mathlib.Meta.NormNum.IsInt.to_isRat {α : Type u_1} [Ring α] {a : α} {n : } :
                                    IsInt a nIsRat a n 1
                                    theorem Mathlib.Meta.NormNum.IsNNRat.to_raw_eq {α : Type u} {n d : } [DivisionSemiring α] {a : α} :
                                    IsNNRat a n da = NNRat.rawCast n d
                                    theorem Mathlib.Meta.NormNum.IsRat.to_raw_eq {α : Type u} {n : } {d : } [DivisionRing α] {a : α} :
                                    IsRat a n da = Rat.rawCast n d
                                    theorem Mathlib.Meta.NormNum.IsRat.neg_to_eq {α : Type u_1} [DivisionRing α] {n d : } {a n' d' : α} :
                                    IsRat a (Int.negOfNat n) dn = n'd = d'a = -(n' / d')
                                    theorem Mathlib.Meta.NormNum.IsNNRat.to_eq {α : Type u_1} [DivisionSemiring α] {n d : } {a n' d' : α} :
                                    IsNNRat a n dn = n'd = d'a = n' / d'
                                    theorem Mathlib.Meta.NormNum.IsNNRat.of_raw (α : Type u_1) [DivisionSemiring α] (n d : ) (h : d 0) :
                                    theorem Mathlib.Meta.NormNum.IsRat.of_raw (α : Type u_1) [DivisionRing α] (n : ) (d : ) (h : d 0) :
                                    IsRat (Rat.rawCast n d) n d
                                    theorem Mathlib.Meta.NormNum.IsNNRat.den_nz {α : Type u_1} [DivisionSemiring α] {a : α} {n d : } :
                                    IsNNRat a n dd 0
                                    theorem Mathlib.Meta.NormNum.IsRat.den_nz {α : Type u_1} [DivisionRing α] {a : α} {n : } {d : } :
                                    IsRat a n dd 0

                                    The result of norm_num running on an expression x of type α. Untyped version of Result.

                                    Instances For
                                      @[implicit_reducible]
                                      def Mathlib.Meta.NormNum.Result {u : Lean.Level} {α : Q(Type u)} (x : Q(«$α»)) :

                                      The result of norm_num running on an expression x of type α.

                                      Instances For
                                        @[implicit_reducible]
                                        instance Mathlib.Meta.NormNum.instInhabitedResult {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} :
                                        Inhabited (Result x)
                                        @[match_pattern, inline]
                                        def Mathlib.Meta.NormNum.Result.isTrue {x : Q(Prop)} (proof : Q(«$x»)) :
                                        Result q(«$x»)

                                        The result is proof : x, where x is a (true) proposition.

                                        Instances For
                                          @[match_pattern, inline]
                                          def Mathlib.Meta.NormNum.Result.isFalse {x : Q(Prop)} (proof : Q(¬«$x»)) :
                                          Result q(«$x»)

                                          The result is proof : ¬x, where x is a (false) proposition.

                                          Instances For
                                            @[match_pattern, inline]
                                            def Mathlib.Meta.NormNum.Result.isNat {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} (inst : Q(AddMonoidWithOne «$α») := by assumption) (lit : Q()) (proof : Q(IsNat «$x» «$lit»)) :

                                            The result is lit : ℕ (a raw nat literal) and proof : isNat x lit.

                                            Instances For
                                              @[match_pattern, inline]
                                              def Mathlib.Meta.NormNum.Result.isNegNat {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} (inst : Q(Ring «$α») := by assumption) (lit : Q()) (proof : Q(IsInt «$x» (Int.negOfNat «$lit»))) :

                                              The result is -lit where lit is a raw nat literal and proof : isInt x (.negOfNat lit).

                                              Instances For
                                                @[match_pattern, inline]
                                                def Mathlib.Meta.NormNum.Result.isNNRat {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} (inst : Q(DivisionSemiring «$α») := by assumption) (q : ) (n d : Q()) (proof : Q(IsNNRat «$x» «$n» «$d»)) :

                                                The result is proof : IsNNRat x n d, where n a raw nat literal, d is a raw nat literal (not 0 or 1), n and d are coprime, and q is the value of n / d.

                                                Instances For
                                                  @[match_pattern, inline]
                                                  def Mathlib.Meta.NormNum.Result.isNegNNRat {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} (inst : Q(DivisionRing «$α») := by assumption) (q : ) (n d : Q()) (proof : Q(IsRat «$x» (Int.negOfNat «$n») «$d»)) :

                                                  The result is proof : IsRat x n d, where n is .negOfNat lit with lit a raw nat literal, d is a raw nat literal (not 0 or 1), n and d are coprime, and q is the value of n / d.

                                                  Instances For
                                                    def Mathlib.Meta.NormNum.Result.isInt {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} (inst : Q(Ring «$α») := by assumption) (z : Q()) (n : ) (proof : Q(IsInt «$x» «$z»)) :

                                                    The result is z : ℤ and proof : isNat x z.

                                                    Instances For
                                                      def Mathlib.Meta.NormNum.Result.isNNRat' {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} (inst : Q(DivisionSemiring «$α») := by assumption) (q : ) (n d : Q()) (proof : Q(IsNNRat «$x» «$n» «$d»)) :

                                                      The result is q : NNRat and proof : isNNRat x q.

                                                      Instances For
                                                        def Mathlib.Meta.NormNum.Result.isRat {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} (inst : Q(DivisionRing «$α») := by assumption) (q : ) (n : Q()) (d : Q()) (proof : Q(IsRat «$x» «$n» «$d»)) :

                                                        The result is q : ℚ and proof : isRat x q.

                                                        Instances For
                                                          @[implicit_reducible]
                                                          instance Mathlib.Meta.NormNum.instToMessageDataResult {u : Lean.Level} {α : Q(Type u)} {x : Q(«$α»)} :
                                                          Lean.ToMessageData (Result x)
                                                          def Mathlib.Meta.NormNum.Result.toRat {u : Lean.Level} {α : Q(Type u)} {e : Q(«$α»)} :
                                                          Result eOption

                                                          Returns the rational number that is the result of norm_num evaluation.

                                                          Instances For
                                                            def Mathlib.Meta.NormNum.Result.toRatNZ {u : Lean.Level} {α : Q(Type u)} {e : Q(«$α»)} :
                                                            Result eOption ( × Option Lean.Expr)

                                                            Returns the rational number that is the result of norm_num evaluation, along with a proof that the denominator is nonzero in the isRat case.

                                                            Instances For
                                                              def Mathlib.Meta.NormNum.Result.toInt {u : Lean.Level} {α : Q(Type u)} {e : Q(«$α»)} (_i : Q(Ring «$α») := by with_reducible assumption) :
                                                              Result eOption ( × (lit : Q()) × Q(IsInt «$e» «$lit»))

                                                              Extract from a Result the integer value (as both a term and an expression), and the proof that the original expression is equal to this integer.

                                                              Instances For
                                                                def Mathlib.Meta.NormNum.Result.toNNRat' {u : Lean.Level} {α : Q(Type u)} {e : Q(«$α»)} (_i : Q(DivisionSemiring «$α») := by with_reducible assumption) :
                                                                Result eOption ( × (n : Q()) × (d : Q()) × Q(IsNNRat «$e» «$n» «$d»))

                                                                Extract from a Result the rational value (as both a term and an expression), and the proof that the original expression is equal to this rational number.

                                                                Instances For
                                                                  def Mathlib.Meta.NormNum.Result.toRat' {u : Lean.Level} {α : Q(Type u)} {e : Q(«$α»)} (_i : Q(DivisionRing «$α») := by with_reducible assumption) :
                                                                  Result eOption ( × (n : Q()) × (d : Q()) × Q(IsRat «$e» «$n» «$d»))

                                                                  Extract from a Result the rational value (as both a term and an expression), and the proof that the original expression is equal to this rational number.

                                                                  Instances For
                                                                    def Mathlib.Meta.NormNum.Result.toRawEq {u : Lean.Level} {α : Q(Type u)} {e : Q(«$α»)} :
                                                                    Result e(e' : Q(«$α»)) × Q(«$e» = «$e'»)

                                                                    Given a NormNum.Result e (which uses IsNat, IsInt, IsRat to express equality to a rational numeral), converts it to an equality e = Nat.rawCast n, e = Int.rawCast n, or e = Rat.rawCast n d to a raw cast expression, so it can be used for rewriting.

                                                                    Instances For
                                                                      def Mathlib.Meta.NormNum.Result.toRawIntEq {u : Lean.Level} {α : Q(Type u)} {e : Q(«$α»)} :
                                                                      Result eOption ( × (e' : Q(«$α»)) × Q(«$e» = «$e'»))

                                                                      Result.toRawEq but providing an integer. Given a NormNum.Result e for something known to be an integer (which uses IsNat or IsInt to express equality to an integer numeral), converts it to an equality e = Nat.rawCast n or e = Int.rawCast n to a raw cast expression, so it can be used for rewriting. Gives none if not an integer.

                                                                      Instances For
                                                                        def Mathlib.Meta.NormNum.Result.ofRawNat {u : Lean.Level} {α : Q(Type u)} (e : Q(«$α»)) :

                                                                        Constructs a Result out of a raw nat cast. Assumes e is a raw nat cast expression.

                                                                        Instances For
                                                                          def Mathlib.Meta.NormNum.Result.ofRawInt {u : Lean.Level} {α : Q(Type u)} (n : ) (e : Q(«$α»)) :

                                                                          Constructs a Result out of a raw int cast. Assumes e is a raw int cast expression denoting n.

                                                                          Instances For
                                                                            def Mathlib.Meta.NormNum.Result.ofRawNNRat {u : Lean.Level} {α : Q(Type u)} (q : ) (e : Q(«$α»)) (hyp : Option Lean.Expr := none) :

                                                                            Constructs a Result out of a raw rat cast. Assumes e is a raw rat cast expression denoting n.

                                                                            Instances For
                                                                              def Mathlib.Meta.NormNum.Result.ofRawRat {u : Lean.Level} {α : Q(Type u)} (q : ) (e : Q(«$α»)) (hyp : Option Lean.Expr := none) :

                                                                              Constructs a Result out of a raw rat cast. Assumes e is a raw rat cast expression denoting n.

                                                                              Instances For
                                                                                def Mathlib.Meta.NormNum.Result.toSimpResult {u : Lean.Level} {α : Q(Type u)} {e : Q(«$α»)} :
                                                                                Result eLean.MetaM Lean.Meta.Simp.Result

                                                                                Convert a Result to a Simp.Result.

                                                                                Instances For
                                                                                  @[reducible, inline]
                                                                                  abbrev Mathlib.Meta.NormNum.BoolResult (p : Q(Prop)) (b : Bool) :

                                                                                  Given Mathlib.Meta.NormNum.Result.isBool p b, this is the type of p. Note that BoolResult p b is definitionally equal to Expr, and if you write match b with ..., then in the true branch BoolResult p true is reducibly equal to Q($p) and in the false branch it is reducibly equal to Q(¬ $p).

                                                                                  Instances For
                                                                                    def Mathlib.Meta.NormNum.Result.ofBoolResult {p : Q(Prop)} {b : Bool} (prf : BoolResult p b) :

                                                                                    Obtain a Result from a BoolResult.

                                                                                    Instances For
                                                                                      def Mathlib.Meta.NormNum.Result.eqTrans {u : Lean.Level} {α : Q(Type u)} {a b : Q(«$α»)} (eq : Q(«$a» = «$b»)) :
                                                                                      Result bResult a

                                                                                      If a = b and we can evaluate b, then we can evaluate a.

                                                                                      Instances For