Documentation

CslibFrontier.Tactic.LinarithCast

Casting natural-number and integer facts for linarith #

A real-valued bound often rests on counting facts proved in ℕ: hk : k < n, hcut : c ≤ a + b. linarith casts such facts to ℤ and no further, so a goal in ℝ does not see them, and each one is restated in ℝ by hand first:

have hk' : (k : ℝ) + 1 ≤ n := by exact_mod_cast hk
have hcut' : (c : ℝ) ≤ a + b := by exact_mod_cast hcut
linarith

cast_linarith is linarith with one more preprocessing step: every comparison between natural numbers or integers among the hypotheses and the supplied terms is also cast into each ordered field in which the problem has a comparison, and the casts are pushed to the leaves with push_cast. A strict comparison a < b of natural numbers or integers is first strengthened to a + 1 ≤ b, which the field no longer knows. linarith itself then adds 0 ≤ (x : K) for each cast natural number x. The comparisons already in the field, the negated goal among them, have their casts pushed to the leaves as well, so ↑(a + b) and ↑a + ↑b are one sum and a push_cast before the call is not needed. The example above becomes cast_linarith [hk, hcut], or with a scoped hypothesis list, cast_linarith only [hk, hcut].

cast_nlinarith is the same step in front of nlinarith.

Main definitions #

Implementation notes #

The fields are the types, other than ℕ and ℤ, of the comparisons that reach the preprocessor (the negated goal among them) and that are linearly ordered fields. Original facts are kept, so linarith's own cast to ℤ still applies. Each cast fact, and each comparison in a field that contains a cast, costs one push_cast call; the cast to ℤ already costs one per natural-number fact. cast_linarith only [...] limits both to the listed facts.

A cast term is compared with the other atoms up to reducible defeq, as in linarith. A local definition x := (n : ℝ) made with set unfolds at that transparency, so x and the cast (n : ℝ) are the same atom.

castToField casts every comparison between natural numbers or integers into each linearly ordered field in which the input has a comparison, pushing the casts to the leaves. A strict comparison a < b is cast as (a : K) + 1 ≤ b. The original comparisons are kept. The casts in the comparisons of those fields are pushed to the leaves too, so that ↑(a + b) and ↑a + ↑b are the same sum.

Run it after filterComparisons, so that negated comparisons are already rewritten, and before natToInt, which adds 0 ≤ (x : K) for the cast natural numbers x.

cast_linarith is linarith that also uses the comparisons between natural numbers and between integers among its hypotheses, cast into the ordered field of the goal. A strict comparison a < b of naturals or integers is used as (a : K) + 1 ≤ b. The casts in the goal and in the hypotheses in the field are pushed to the leaves.

example (k n : ℕ) (x : ℝ) (hk : k < n) (hx : x ≤ k) : x + 1 ≤ n := by
  cast_linarith [hk]

It takes the arguments of linarith: cast_linarith!, a configuration, only and a list of terms. Each cast fact costs a push_cast call; cast_linarith only [...] casts only the listed facts.

cast_nlinarith is nlinarith with the preprocessing of cast_linarith: comparisons between natural numbers and between integers are also cast into the ordered field of the goal.

cast_linarith is linarith that also uses the comparisons between natural numbers and between integers among its hypotheses, cast into the ordered field of the goal. A strict comparison a < b of naturals or integers is used as (a : K) + 1 ≤ b. The casts in the goal and in the hypotheses in the field are pushed to the leaves.

example (k n : ℕ) (x : ℝ) (hk : k < n) (hx : x ≤ k) : x + 1 ≤ n := by
  cast_linarith [hk]

It takes the arguments of linarith: cast_linarith!, a configuration, only and a list of terms. Each cast fact costs a push_cast call; cast_linarith only [...] casts only the listed facts.

cast_nlinarith is nlinarith with the preprocessing of cast_linarith: comparisons between natural numbers and between integers are also cast into the ordered field of the goal.