forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldSimp.lean
More file actions
805 lines (727 loc) · 41 KB
/
FieldSimp.lean
File metadata and controls
805 lines (727 loc) · 41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel, David Renshaw, Heather Macbeth, Arend Mellendijk, Michael Rothgang
-/
module
public meta import Mathlib.Data.Ineq
public import Mathlib.Tactic.FieldSimp.Attr
public import Mathlib.Tactic.FieldSimp.Discharger
public import Mathlib.Tactic.FieldSimp.Lemmas
public import Mathlib.Util.AtomM.Recurse
public import Mathlib.Util.SynthesizeUsing
public import Mathlib.Data.Ineq
/-!
# `field_simp` tactic
Tactic to clear denominators in algebraic expressions.
-/
public meta section
open Lean Meta Qq
namespace Mathlib.Tactic.FieldSimp
initialize registerTraceClass `Tactic.field_simp
variable {v : Level} {M : Q(Type v)}
/-! ### Lists of expressions representing exponents and atoms, and operations on such lists -/
/-- Basic meta-code "normal form" object of the `field_simp` tactic: a type synonym
for a list of ordered triples comprising an expression representing a term of a type `M` (where
typically `M` is a field), together with an integer "power" and a natural number "index".
The natural number represents the index of the `M` term in the `AtomM` monad: this is not enforced,
but is sometimes assumed in operations. Thus when items `((a₁, x₁), k)` and `((a₂, x₂), k)`
appear in two different `FieldSimp.qNF` objects (i.e. with the same `ℕ`-index `k`), it is expected
that the expressions `x₁` and `x₂` are the same. It is also expected that the items in a
`FieldSimp.qNF` list are in strictly decreasing order by natural-number index.
By forgetting the natural number indices, an expression representing a `Mathlib.Tactic.FieldSimp.NF`
object can be built from a `FieldSimp.qNF` object; this construction is provided as
`Mathlib.Tactic.FieldSimp.qNF.toNF`. -/
abbrev qNF (M : Q(Type v)) := List ((ℤ × Q($M)) × ℕ)
namespace qNF
/-- Given `l` of type `qNF M`, i.e. a list of `(ℤ × Q($M)) × ℕ`s (two `Expr`s and a natural
number), build an `Expr` representing an object of type `NF M` (i.e. `List (ℤ × M)`) in the
in the obvious way: by forgetting the natural numbers and gluing together the integers and `Expr`s.
-/
def toNF (l : qNF q($M)) : Q(NF $M) := l.foldr (fun ((a, x), _) l ↦ q(($a, $x) ::ᵣ $l)) q([])
/-- Given `l` of type `qNF M`, i.e. a list of `(ℤ × Q($M)) × ℕ`s (two `Expr`s and a natural
number), apply an expression representing a function with domain `ℤ` to each of the `ℤ`
components. -/
def onExponent (l : qNF M) (f : ℤ → ℤ) : qNF M :=
l.map fun ((a, x), k) ↦ ((f a, x), k)
/-- Build a transparent expression for the product of powers represented by `l : qNF M`. -/
def evalPrettyMonomial (iM : Q(GroupWithZero $M)) (r : ℤ) (x : Q($M)) :
MetaM (Σ e : Q($M), Q(zpow' $x $r = $e)) := do
match r with
| 0 => /- If an exponent is zero then we must not have been able to prove that x is nonzero. -/
return ⟨q($x / $x), q(zpow'_zero_eq_div ..)⟩
| 1 => return ⟨x, q(zpow'_one $x)⟩
| .ofNat r => do
let pf ← mkDecideProofQ q($r ≠ 0)
return ⟨q($x ^ $r), q(zpow'_ofNat $x $pf)⟩
| r => do
let pf ← mkDecideProofQ q($r ≠ 0)
return ⟨q($x ^ $r), q(zpow'_of_ne_zero_right _ _ $pf)⟩
/-- Try to drop an expression `zpow' x r` from the beginning of a product. If `r ≠ 0` this of course
can't be done. If `r = 0`, then `zpow' x r` is equal to `x / x`, so it can be simplified to 1 (hence
dropped from the beginning of the product) if we can find a proof that `x ≠ 0`. -/
def tryClearZero
(disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type)) (iM : Q(CommGroupWithZero $M))
(r : ℤ) (x : Q($M)) (i : ℕ) (l : qNF M) :
MetaM <| Σ l' : qNF M, Q(NF.eval $(qNF.toNF (((r, x), i) :: l)) = NF.eval $(l'.toNF)) := do
if r != 0 then
return ⟨((r, x), i) :: l, q(rfl)⟩
try
let pf' : Q($x ≠ 0) ← disch q($x ≠ 0)
have pf_r : Q($r = 0) := ← mkDecideProofQ q($r = 0)
return ⟨l, (q(NF.eval_cons_of_pow_eq_zero $pf_r $pf' $(l.toNF)):)⟩
catch _=>
return ⟨((r, x), i) :: l, q(rfl)⟩
/-- Given `l : qNF M`, obtain `l' : qNF M` by removing all `l`'s exponent-zero entries where the
corresponding atom can be proved nonzero, and construct a proof that their associated expressions
are equal. -/
def removeZeros
(disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type)) (iM : Q(CommGroupWithZero $M))
(l : qNF M) :
MetaM <| Σ l' : qNF M, Q(NF.eval $(l.toNF) = NF.eval $(l'.toNF)) :=
match l with
| [] => return ⟨[], q(rfl)⟩
| ((r, x), i) :: t => do
let ⟨t', pf⟩ ← removeZeros disch iM t
let ⟨l', pf'⟩ ← tryClearZero disch iM r x i t'
let pf' : Q(NF.eval (($r, $x) ::ᵣ $(qNF.toNF t')) = NF.eval $(qNF.toNF l')) := pf'
let pf'' : Q(NF.eval (($r, $x) ::ᵣ $(qNF.toNF t)) = NF.eval $(qNF.toNF l')) :=
q(NF.eval_cons_eq_eval_of_eq_of_eq $r $x $pf $pf')
return ⟨l', pf''⟩
/-- Given a product of powers, split as a quotient: the positive powers divided by (the negations
of) the negative powers. -/
def split (iM : Q(CommGroupWithZero $M)) (l : qNF M) :
MetaM (Σ l_n l_d : qNF M, Q(NF.eval $(l.toNF)
= NF.eval $(l_n.toNF) / NF.eval $(l_d.toNF))) := do
match l with
| [] => return ⟨[], [], q(Eq.symm (div_one (1:$M)))⟩
| ((r, x), i) :: t =>
let ⟨t_n, t_d, pf⟩ ← split iM t
if r > 0 then
return ⟨((r, x), i) :: t_n, t_d, (q(NF.cons_eq_div_of_eq_div $r $x $pf):)⟩
else if r = 0 then
return ⟨((1, x), i) :: t_n, ((1, x), i) :: t_d, (q(NF.cons_zero_eq_div_of_eq_div $x $pf):)⟩
else
let r' : ℤ := -r
return ⟨t_n, ((r', x), i) :: t_d, (q(NF.cons_eq_div_of_eq_div' $r' $x $pf):)⟩
private def evalPrettyAux (iM : Q(CommGroupWithZero $M)) (l : qNF M) :
MetaM (Σ e : Q($M), Q(NF.eval $(l.toNF) = $e)) := do
match l with
| [] => return ⟨q(1), q(rfl)⟩
| [((r, x), _)] =>
let ⟨e, pf⟩ ← evalPrettyMonomial q(inferInstance) r x
return ⟨e, q(by rw [NF.eval_cons]; exact Eq.trans (one_mul _) $pf)⟩
| ((r, x), k) :: t =>
let ⟨e, pf_e⟩ ← evalPrettyMonomial q(inferInstance) r x
let ⟨t', pf⟩ ← evalPrettyAux iM t
have pf'' : Q(NF.eval $(qNF.toNF (((r, x), k) :: t)) = (NF.eval $(qNF.toNF t)) * zpow' $x $r) :=
(q(NF.eval_cons ($r, $x) $(qNF.toNF t)):)
return ⟨q($t' * $e), q(Eq.trans $pf'' (congr_arg₂ HMul.hMul $pf $pf_e))⟩
/-- Build a transparent expression for the product of powers represented by `l : qNF M`. -/
def evalPretty (iM : Q(CommGroupWithZero $M)) (l : qNF M) :
MetaM (Σ e : Q($M), Q(NF.eval $(l.toNF) = $e)) := do
let ⟨l_n, l_d, pf⟩ ← split iM l
let ⟨num, pf_n⟩ ← evalPrettyAux q(inferInstance) l_n
let ⟨den, pf_d⟩ ← evalPrettyAux q(inferInstance) l_d
match l_d with
| [] => return ⟨num, q(eq_div_of_eq_one_of_subst $pf $pf_n)⟩
| _ =>
let pf_n : Q(NF.eval $(l_n.toNF) = $num) := pf_n
let pf_d : Q(NF.eval $(l_d.toNF) = $den) := pf_d
let pf : Q(NF.eval $(l.toNF) = NF.eval $(l_n.toNF) / NF.eval $(l_d.toNF)) := pf
let pf_tot := q(eq_div_of_subst $pf $pf_n $pf_d)
return ⟨q($num / $den), pf_tot⟩
/-- Given two terms `l₁`, `l₂` of type `qNF M`, i.e. lists of `(ℤ × Q($M)) × ℕ`s (an integer, an
`Expr` and a natural number), construct another such term `l`, which will have the property that in
the field `$M`, the product of the "multiplicative linear combinations" represented by `l₁` and
`l₂` is the multiplicative linear combination represented by `l`.
The construction assumes, to be valid, that the lists `l₁` and `l₂` are in strictly decreasing order
by `ℕ`-component, and that if pairs `(a₁, x₁)` and `(a₂, x₂)` appear in `l₁`, `l₂` respectively with
the same `ℕ`-component `k`, then the expressions `x₁` and `x₂` are equal.
The construction is as follows: merge the two lists, except that if pairs `(a₁, x₁)` and `(a₂, x₂)`
appear in `l₁`, `l₂` respectively with the same `ℕ`-component `k`, then contribute a term
`(a₁ + a₂, x₁)` to the output list with `ℕ`-component `k`. -/
def mul : qNF q($M) → qNF q($M) → qNF q($M)
| [], l => l
| l, [] => l
| ((a₁, x₁), k₁) :: t₁, ((a₂, x₂), k₂) :: t₂ =>
if k₁ > k₂ then
((a₁, x₁), k₁) :: mul t₁ (((a₂, x₂), k₂) :: t₂)
else if k₁ = k₂ then
/- If we can prove that the atom is nonzero then we could remove it from this list,
but this will be done at a later stage. -/
((a₁ + a₂, x₁), k₁) :: mul t₁ t₂
else
((a₂, x₂), k₂) :: mul (((a₁, x₁), k₁) :: t₁) t₂
/-- Given two terms `l₁`, `l₂` of type `qNF M`, i.e. lists of `(ℤ × Q($M)) × ℕ`s (an integer, an
`Expr` and a natural number), recursively construct a proof that in the field `$M`, the product of
the "multiplicative linear combinations" represented by `l₁` and `l₂` is the multiplicative linear
combination represented by `FieldSimp.qNF.mul l₁ l₁`. -/
def mkMulProof (iM : Q(CommGroupWithZero $M)) (l₁ l₂ : qNF M) :
Q((NF.eval $(l₁.toNF)) * NF.eval $(l₂.toNF) = NF.eval $((qNF.mul l₁ l₂).toNF)) :=
match l₁, l₂ with
| [], l => (q(one_mul (NF.eval $(l.toNF))):)
| l, [] => (q(mul_one (NF.eval $(l.toNF))):)
| ((a₁, x₁), k₁) :: t₁, ((a₂, x₂), k₂) :: t₂ =>
if k₁ > k₂ then
let pf := mkMulProof iM t₁ (((a₂, x₂), k₂) :: t₂)
(q(NF.mul_eq_eval₁ ($a₁, $x₁) $pf):)
else if k₁ = k₂ then
let pf := mkMulProof iM t₁ t₂
(q(NF.mul_eq_eval₂ $a₁ $a₂ $x₁ $pf):)
else
let pf := mkMulProof iM (((a₁, x₁), k₁) :: t₁) t₂
(q(NF.mul_eq_eval₃ ($a₂, $x₂) $pf):)
/-- Given two terms `l₁`, `l₂` of type `qNF M`, i.e. lists of `(ℤ × Q($M)) × ℕ`s (an integer, an
`Expr` and a natural number), construct another such term `l`, which will have the property that in
the field `$M`, the quotient of the "multiplicative linear combinations" represented by `l₁` and
`l₂` is the multiplicative linear combination represented by `l`.
The construction assumes, to be valid, that the lists `l₁` and `l₂` are in strictly decreasing order
by `ℕ`-component, and that if pairs `(a₁, x₁)` and `(a₂, x₂)` appear in `l₁`, `l₂` respectively with
the same `ℕ`-component `k`, then the expressions `x₁` and `x₂` are equal.
The construction is as follows: merge the first list and the negation of the second list, except
that if pairs `(a₁, x₁)` and `(a₂, x₂)` appear in `l₁`, `l₂` respectively with the same
`ℕ`-component `k`, then contribute a term `(a₁ - a₂, x₁)` to the output list with `ℕ`-component `k`.
-/
def div : qNF M → qNF M → qNF M
| [], l => l.onExponent Neg.neg
| l, [] => l
| ((a₁, x₁), k₁) :: t₁, ((a₂, x₂), k₂) :: t₂ =>
if k₁ > k₂ then
((a₁, x₁), k₁) :: div t₁ (((a₂, x₂), k₂) :: t₂)
else if k₁ = k₂ then
((a₁ - a₂, x₁), k₁) :: div t₁ t₂
else
((-a₂, x₂), k₂) :: div (((a₁, x₁), k₁) :: t₁) t₂
/-- Given two terms `l₁`, `l₂` of type `qNF M`, i.e. lists of `(ℤ × Q($M)) × ℕ`s (an integer, an
`Expr` and a natural number), recursively construct a proof that in the field `$M`, the quotient
of the "multiplicative linear combinations" represented by `l₁` and `l₂` is the multiplicative
linear combination represented by `FieldSimp.qNF.div l₁ l₁`. -/
def mkDivProof (iM : Q(CommGroupWithZero $M)) (l₁ l₂ : qNF M) :
Q(NF.eval $(l₁.toNF) / NF.eval $(l₂.toNF) = NF.eval $((qNF.div l₁ l₂).toNF)) :=
match l₁, l₂ with
| [], l => (q(NF.one_div_eq_eval $(l.toNF)):)
| l, [] => (q(div_one (NF.eval $(l.toNF))):)
| ((a₁, x₁), k₁) :: t₁, ((a₂, x₂), k₂) :: t₂ =>
if k₁ > k₂ then
let pf := mkDivProof iM t₁ (((a₂, x₂), k₂) :: t₂)
(q(NF.div_eq_eval₁ ($a₁, $x₁) $pf):)
else if k₁ = k₂ then
let pf := mkDivProof iM t₁ t₂
(q(NF.div_eq_eval₂ $a₁ $a₂ $x₁ $pf):)
else
let pf := mkDivProof iM (((a₁, x₁), k₁) :: t₁) t₂
(q(NF.div_eq_eval₃ ($a₂, $x₂) $pf):)
end qNF
/-- Constraints on denominators which may need to be considered in `field_simp`: no condition,
nonzeroness, or strict positivity. -/
inductive DenomCondition (iM : Q(GroupWithZero $M))
| none
| nonzero
| positive (iM' : Q(PartialOrder $M)) (iM'' : Q(PosMulStrictMono $M))
(iM''' : Q(PosMulReflectLT $M)) (iM'''' : Q(ZeroLEOneClass $M))
namespace DenomCondition
/-- Given a field-simp-normal-form expression `L` (a product of powers of atoms), a proof (according
to the value of `DenomCondition`) of that expression's nonzeroness, strict positivity, etc. -/
@[expose] def proof {iM : Q(GroupWithZero $M)} (L : qNF M) : DenomCondition iM → Type
| .none => Unit
| .nonzero => Q(NF.eval $(qNF.toNF L) ≠ 0)
| .positive _ _ _ _ => Q(0 < NF.eval $(qNF.toNF L))
/-- The empty field-simp-normal-form expression `[]` (representing `1` as an empty product of powers
of atoms) can be proved to be nonzero, strict positivity, etc., as needed, as specified by the
value of `DenomCondition`. -/
def proofZero {iM : Q(CommGroupWithZero $M)} :
∀ cond : DenomCondition (M := M) q(inferInstance), cond.proof []
| .none => Unit.unit
| .nonzero => q(one_ne_zero (α := $M))
| .positive _ _ _ _ => q(zero_lt_one (α := $M))
end DenomCondition
/-- Given a proof of the nonzeroness, strict positivity, etc. (as specified by the value of
`DenomCondition`) of a field-simp-normal-form expression `L` (a product of powers of atoms),
construct a corresponding proof for `((r, e), i) :: L`.
In this version we also expose the proof of nonzeroness of `e`. -/
def mkDenomConditionProofSucc {iM : Q(CommGroupWithZero $M)}
(disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type))
{cond : DenomCondition (M := M) q(inferInstance)}
{L : qNF M} (hL : cond.proof L) (e : Q($M)) (r : ℤ) (i : ℕ) :
MetaM (Q($e ≠ 0) × cond.proof (((r, e), i) :: L)) := do
match cond with
| .none => return (← disch q($e ≠ 0), Unit.unit)
| .nonzero =>
let pf ← disch q($e ≠ 0)
let pf₀ : Q(NF.eval $(qNF.toNF L) ≠ 0) := hL
return (pf, q(NF.cons_ne_zero $r $pf $pf₀))
| .positive _ _ _ _ =>
let pf ← disch q(0 < $e)
let pf₀ : Q(0 < NF.eval $(qNF.toNF L)) := hL
let pf' := q(NF.cons_pos $r (x := $e) $pf $pf₀)
return (q(LT.lt.ne' $pf), pf')
/-- Given a proof of the nonzeroness, strict positivity, etc. (as specified by the value of
`DenomCondition`) of a field-simp-normal-form expression `L` (a product of powers of atoms),
construct a corresponding proof for `((r, e), i) :: L`. -/
def mkDenomConditionProofSucc' {iM : Q(CommGroupWithZero $M)}
(disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type))
{cond : DenomCondition (M := M) q(inferInstance)}
{L : qNF M} (hL : cond.proof L) (e : Q($M)) (r : ℤ) (i : ℕ) :
MetaM (cond.proof (((r, e), i) :: L)) := do
match cond with
| .none => return Unit.unit
| .nonzero =>
let pf ← disch q($e ≠ 0)
let pf₀ : Q(NF.eval $(qNF.toNF L) ≠ 0) := hL
return q(NF.cons_ne_zero $r $pf $pf₀)
| .positive _ _ _ _ =>
let pf ← disch q(0 < $e)
let pf₀ : Q(0 < NF.eval $(qNF.toNF L)) := hL
return q(NF.cons_pos $r (x := $e) $pf $pf₀)
namespace qNF
/-- Extract a common factor `L` of two products-of-powers `l₁` and `l₂` in `M`, in the sense that
both `l₁` and `l₂` are quotients by `L` of products of *positive* powers.
The variable `cond` specifies whether we extract a *certified nonzero[/positive]* (and therefore
potentially smaller) common factor. If so, the metaprogram returns a "proof" that this common factor
is nonzero/positive, i.e. an expression `Q(NF.eval $(L.toNF) ≠ 0)` / `Q(0 < NF.eval $(L.toNF))`. -/
partial def gcd (iM : Q(CommGroupWithZero $M)) (l₁ l₂ : qNF M)
(disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type))
(cond : DenomCondition (M := M) q(inferInstance)) :
MetaM <| Σ (L l₁' l₂' : qNF M),
Q((NF.eval $(L.toNF)) * NF.eval $(l₁'.toNF) = NF.eval $(l₁.toNF)) ×
Q((NF.eval $(L.toNF)) * NF.eval $(l₂'.toNF) = NF.eval $(l₂.toNF)) ×
cond.proof L :=
/- Handle the case where atom `i` is present in the first list but not the second. -/
let absent (l₁ l₂ : qNF M) (n : ℤ) (e : Q($M)) (i : ℕ) :
MetaM <| Σ (L l₁' l₂' : qNF M),
Q((NF.eval $(L.toNF)) * NF.eval $(l₁'.toNF) = NF.eval $(qNF.toNF (((n, e), i) :: l₁))) ×
Q((NF.eval $(L.toNF)) * NF.eval $(l₂'.toNF) = NF.eval $(l₂.toNF)) ×
cond.proof L := do
let ⟨L, l₁', l₂', pf₁, pf₂, pf₀⟩ ← gcd iM l₁ l₂ disch cond
if 0 < n then
-- Don't pull anything out
return ⟨L, ((n, e), i) :: l₁', l₂', (q(NF.eval_mul_eval_cons $n $e $pf₁):), q($pf₂), pf₀⟩
else if n = 0 then
-- Don't pull anything out, but eliminate the term if it is a cancellable zero
let ⟨l₁'', pf''⟩ ← tryClearZero disch iM 0 e i l₁'
let pf'' : Q(NF.eval ((0, $e) ::ᵣ $(l₁'.toNF)) = NF.eval $(l₁''.toNF)) := pf''
return ⟨L, l₁'', l₂', (q(NF.eval_mul_eval_cons_zero $pf₁ $pf''):), q($pf₂), pf₀⟩
try
let (pf, b) ← mkDenomConditionProofSucc disch pf₀ e n i
-- if nonzeroness proof succeeds
return ⟨((n, e), i) :: L, l₁', ((-n, e), i) :: l₂', (q(NF.eval_cons_mul_eval $n $e $pf₁):),
(q(NF.eval_cons_mul_eval_cons_neg $n $pf $pf₂):), b⟩
catch _ =>
-- if we can't prove nonzeroness, don't pull out e.
return ⟨L, ((n, e), i) :: l₁', l₂', (q(NF.eval_mul_eval_cons $n $e $pf₁):), q($pf₂), pf₀⟩
/- Handle the case where atom `i` is present in both lists. -/
let bothPresent (t₁ t₂ : qNF M) (n₁ n₂ : ℤ) (e : Q($M)) (i : ℕ) :
MetaM <| Σ (L l₁' l₂' : qNF M),
Q((NF.eval $(L.toNF)) * NF.eval $(l₁'.toNF) = NF.eval $(qNF.toNF (((n₁, e), i) :: t₁))) ×
Q((NF.eval $(L.toNF)) * NF.eval $(l₂'.toNF) = NF.eval $(qNF.toNF (((n₂, e), i) :: t₂))) ×
cond.proof L := do
let ⟨L, l₁', l₂', pf₁, pf₂, pf₀⟩ ← gcd iM t₁ t₂ disch cond
if n₁ < n₂ then
let N : ℤ := n₂ - n₁
return ⟨((n₁, e), i) :: L, l₁', ((n₂ - n₁, e), i) :: l₂',
(q(NF.eval_cons_mul_eval $n₁ $e $pf₁):), (q(NF.mul_eq_eval₂ $n₁ $N $e $pf₂):),
← mkDenomConditionProofSucc' disch pf₀ e n₁ i⟩
else if n₁ = n₂ then
return ⟨((n₁, e), i) :: L, l₁', l₂', (q(NF.eval_cons_mul_eval $n₁ $e $pf₁):),
(q(NF.eval_cons_mul_eval $n₂ $e $pf₂):), ← mkDenomConditionProofSucc' disch pf₀ e n₁ i⟩
else
let N : ℤ := n₁ - n₂
return ⟨((n₂, e), i) :: L, ((n₁ - n₂, e), i) :: l₁', l₂',
(q(NF.mul_eq_eval₂ $n₂ $N $e $pf₁):), (q(NF.eval_cons_mul_eval $n₂ $e $pf₂):),
← mkDenomConditionProofSucc' disch pf₀ e n₂ i⟩
match l₁, l₂ with
| [], [] => pure ⟨[], [], [],
(q(one_mul (NF.eval $(qNF.toNF (M := M) []))):),
(q(one_mul (NF.eval $(qNF.toNF (M := M) []))):), cond.proofZero⟩
| ((n, e), i) :: t, [] => do
let ⟨L, l₁', l₂', pf₁, pf₂, pf₀⟩ ← absent t [] n e i
return ⟨L, l₁', l₂', q($pf₁), q($pf₂), pf₀⟩
| [], ((n, e), i) :: t => do
let ⟨L, l₂', l₁', pf₂, pf₁, pf₀⟩ ← absent t [] n e i
return ⟨L, l₁', l₂', q($pf₁), q($pf₂), pf₀⟩
| ((n₁, e₁), i₁) :: t₁, ((n₂, e₂), i₂) :: t₂ => do
if i₁ > i₂ then
let ⟨L, l₁', l₂', pf₁, pf₂, pf₀⟩ ← absent t₁ (((n₂, e₂), i₂) :: t₂) n₁ e₁ i₁
return ⟨L, l₁', l₂', q($pf₁), q($pf₂), pf₀⟩
else if i₁ == i₂ then
try
bothPresent t₁ t₂ n₁ n₂ e₁ i₁
catch _ =>
-- if `bothPresent` fails, don't pull out `e`
-- the failure case of `bothPresent` should be:
-- * `.none` case: never
-- * `.nonzero` case: if `e` can't be proved nonzero
-- * `.positive _` case: if `e` can't be proved positive
let ⟨L, l₁', l₂', pf₁, pf₂, pf₀⟩ ← gcd iM t₁ t₂ disch cond
return ⟨L, ((n₁, e₁), i₁) :: l₁', ((n₂, e₂), i₂) :: l₂',
(q(NF.eval_mul_eval_cons $n₁ $e₁ $pf₁):), (q(NF.eval_mul_eval_cons $n₂ $e₂ $pf₂):), pf₀⟩
else
let ⟨L, l₂', l₁', pf₂, pf₁, pf₀⟩ ← absent t₂ (((n₁, e₁), i₁) :: t₁) n₂ e₂ i₂
return ⟨L, l₁', l₂', q($pf₁), q($pf₂), pf₀⟩
end qNF
/-! ### Core of the `field_simp` tactic -/
/-- The main algorithm behind the `field_simp` tactic: partially-normalizing an
expression in a field `M` into the form x1 ^ c1 * x2 ^ c2 * ... x_k ^ c_k,
where x1, x2, ... are distinct atoms in `M`, and c1, c2, ... are integers. -/
partial def normalize (disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type))
(iM : Q(CommGroupWithZero $M)) (x : Q($M)) :
AtomM (Σ y : Q($M), (Σ g : Sign M, Q($x = $(g.expr y))) ×
Σ l : qNF M, Q($y = NF.eval $(l.toNF))) := do
let baseCase (y : Q($M)) (normalize? : Bool) :
AtomM (Σ (l : qNF M), Q($y = NF.eval $(l.toNF))) := do
if normalize? then
let r ← (← read).evalAtom y
have y' : Q($M) := r.expr
have pf : Q($y = $y') := ← r.getProof
let (k, ⟨x', _⟩) ← AtomM.addAtomQ y'
pure ⟨[((1, x'), k)], q(Eq.trans $pf (NF.atom_eq_eval $x'))⟩
else
let (k, ⟨x', _⟩) ← AtomM.addAtomQ y
pure ⟨[((1, x'), k)], q(NF.atom_eq_eval $x')⟩
match x with
/- normalize a multiplication: `x₁ * x₂` -/
| ~q($x₁ * $x₂) =>
let ⟨y₁, ⟨g₁, pf₁_sgn⟩, l₁, pf₁⟩ ← normalize disch iM x₁
let ⟨y₂, ⟨g₂, pf₂_sgn⟩, l₂, pf₂⟩ ← normalize disch iM x₂
-- build the new list and proof
have pf := qNF.mkMulProof iM l₁ l₂
let ⟨G, pf_y⟩ := ← Sign.mul iM y₁ y₂ g₁ g₂
pure ⟨q($y₁ * $y₂), ⟨G, q(Eq.trans (congr_arg₂ HMul.hMul $pf₁_sgn $pf₂_sgn) $pf_y)⟩,
qNF.mul l₁ l₂, q(NF.mul_eq_eval $pf₁ $pf₂ $pf)⟩
/- normalize a division: `x₁ / x₂` -/
| ~q($x₁ / $x₂) =>
let ⟨y₁, ⟨g₁, pf₁_sgn⟩, l₁, pf₁⟩ ← normalize disch iM x₁
let ⟨y₂, ⟨g₂, pf₂_sgn⟩, l₂, pf₂⟩ ← normalize disch iM x₂
-- build the new list and proof
let pf := qNF.mkDivProof iM l₁ l₂
let ⟨G, pf_y⟩ := ← Sign.div iM y₁ y₂ g₁ g₂
pure ⟨q($y₁ / $y₂), ⟨G, q(Eq.trans (congr_arg₂ HDiv.hDiv $pf₁_sgn $pf₂_sgn) $pf_y)⟩,
qNF.div l₁ l₂, q(NF.div_eq_eval $pf₁ $pf₂ $pf)⟩
/- normalize an inversion: `y⁻¹` -/
| ~q($y⁻¹) =>
let ⟨y', ⟨g, pf_sgn⟩, l, pf⟩ ← normalize disch iM y
let pf_y ← Sign.inv iM y' g
-- build the new list and proof, casing according to the sign of `x`
pure ⟨q($y'⁻¹), ⟨g, q(Eq.trans (congr_arg Inv.inv $pf_sgn) $pf_y)⟩,
l.onExponent Neg.neg, (q(NF.inv_eq_eval $pf):)⟩
/- normalize an integer exponentiation: `y ^ (s : ℤ)` -/
| ~q($y ^ ($s : ℤ)) =>
let some s := Expr.int? s | pure ⟨x, ⟨.plus, q(rfl)⟩, ← baseCase x true⟩
if s = 0 then
pure ⟨q(1), ⟨Sign.plus, (q(zpow_zero $y):)⟩, [], q(NF.one_eq_eval $M)⟩
else
let ⟨y', ⟨g, pf_sgn⟩, l, pf⟩ ← normalize disch iM y
let pf_s ← mkDecideProofQ q($s ≠ 0)
let ⟨G, pf_y⟩ ← Sign.zpow iM y' g s
let pf_y' := q(Eq.trans (congr_arg (· ^ $s) $pf_sgn) $pf_y)
pure ⟨q($y' ^ $s), ⟨G, pf_y'⟩, l.onExponent (HMul.hMul s), (q(NF.zpow_eq_eval $pf_s $pf):)⟩
/- normalize a natural number exponentiation: `y ^ (s : ℕ)` -/
| ~q($y ^ ($s : ℕ)) =>
let some s := Expr.nat? s | pure ⟨x, ⟨.plus, q(rfl)⟩, ← baseCase x true⟩
if s = 0 then
pure ⟨q(1), ⟨Sign.plus, (q(pow_zero $y):)⟩, [], q(NF.one_eq_eval $M)⟩
else
let ⟨y', ⟨g, pf_sgn⟩, l, pf⟩ ← normalize disch iM y
let pf_s ← mkDecideProofQ q($s ≠ 0)
let ⟨G, pf_y⟩ ← Sign.pow iM y' g s
let pf_y' := q(Eq.trans (congr_arg (· ^ $s) $pf_sgn) $pf_y)
pure ⟨q($y' ^ $s), ⟨G, pf_y'⟩, l.onExponent (↑s * ·), (q(NF.pow_eq_eval $pf_s $pf):)⟩
/- normalize a `(1:M)` -/
| ~q(1) => pure ⟨q(1), ⟨Sign.plus, q(rfl)⟩, [], q(NF.one_eq_eval $M)⟩
/- normalize an addition: `a + b` -/
| ~q(HAdd.hAdd (self := @instHAdd _ $i) $a $b) =>
try
let _i ← synthInstanceQ q(Semifield $M)
assumeInstancesCommute
let ⟨_, ⟨g₁, pf_sgn₁⟩, l₁, pf₁⟩ ← normalize disch iM a
let ⟨_, ⟨g₂, pf_sgn₂⟩, l₂, pf₂⟩ ← normalize disch iM b
let ⟨L, l₁', l₂', pf₁', pf₂', _⟩ ← l₁.gcd iM l₂ disch .none
let ⟨e₁, pf₁''⟩ ← qNF.evalPretty iM l₁'
let ⟨e₂, pf₂''⟩ ← qNF.evalPretty iM l₂'
have pf_a := ← Sign.mkEqMul iM pf_sgn₁ q(Eq.trans $pf₁ (Eq.symm $pf₁')) pf₁''
have pf_b := ← Sign.mkEqMul iM pf_sgn₂ q(Eq.trans $pf₂ (Eq.symm $pf₂')) pf₂''
let e : Q($M) := q($(g₁.expr e₁) + $(g₂.expr e₂))
let ⟨sum, pf_atom⟩ ← baseCase e false
let L' := qNF.mul L sum
let pf_mul : Q((NF.eval $(L.toNF)) * NF.eval $(sum.toNF) = NF.eval $(L'.toNF)) :=
qNF.mkMulProof iM L sum
pure ⟨x, ⟨Sign.plus, q(rfl)⟩, L', q(subst_add $pf_a $pf_b $pf_atom $pf_mul)⟩
catch _ => pure ⟨x, ⟨.plus, q(rfl)⟩, ← baseCase x true⟩
/- normalize a subtraction: `a - b` -/
| ~q(HSub.hSub (self := @instHSub _ $i) $a $b) =>
try
let _i ← synthInstanceQ q(Field $M)
assumeInstancesCommute
let ⟨_, ⟨g₁, pf_sgn₁⟩, l₁, pf₁⟩ ← normalize disch iM a
let ⟨_, ⟨g₂, pf_sgn₂⟩, l₂, pf₂⟩ ← normalize disch iM b
let ⟨L, l₁', l₂', pf₁', pf₂', _⟩ ← l₁.gcd iM l₂ disch .none
let ⟨e₁, pf₁''⟩ ← qNF.evalPretty iM l₁'
let ⟨e₂, pf₂''⟩ ← qNF.evalPretty iM l₂'
have pf_a := ← Sign.mkEqMul iM pf_sgn₁ q(Eq.trans $pf₁ (Eq.symm $pf₁')) pf₁''
have pf_b := ← Sign.mkEqMul iM pf_sgn₂ q(Eq.trans $pf₂ (Eq.symm $pf₂')) pf₂''
let e : Q($M) := q($(g₁.expr e₁) - $(g₂.expr e₂))
let ⟨sum, pf_atom⟩ ← baseCase e false
let L' := qNF.mul L sum
let pf_mul : Q((NF.eval $(L.toNF)) * NF.eval $(sum.toNF) = NF.eval $(L'.toNF)) :=
qNF.mkMulProof iM L sum
pure ⟨x, ⟨Sign.plus, q(rfl)⟩, L', q(subst_sub $pf_a $pf_b $pf_atom $pf_mul)⟩
catch _ => pure ⟨x, ⟨.plus, q(rfl)⟩, ← baseCase x true⟩
/- normalize a negation: `-a` -/
| ~q(Neg.neg (self := $i) $a) =>
try
let iM' ← synthInstanceQ q(Field $M)
assumeInstancesCommute
let ⟨y, ⟨g, pf_sgn⟩, l, pf⟩ ← normalize disch iM a
let ⟨G, pf_y⟩ ← Sign.neg iM' y g
pure ⟨y, ⟨G, q(Eq.trans (congr_arg Neg.neg $pf_sgn) $pf_y)⟩, l, pf⟩
catch _ => pure ⟨x, ⟨.plus, q(rfl)⟩, ← baseCase x true⟩
-- TODO special-case handling of zero? maybe not necessary
/- anything else should be treated as an atom -/
| _ => pure ⟨x, ⟨.plus, q(rfl)⟩, ← baseCase x true⟩
/-- Given `x` in a commutative group-with-zero, construct a new expression in the standard form
*** / *** (all denominators at the end) which is equal to `x`. -/
def reduceExprQ (disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type))
(iM : Q(CommGroupWithZero $M)) (x : Q($M)) : AtomM (Σ x' : Q($M), Q($x = $x')) := do
let ⟨y, ⟨g, pf_sgn⟩, l, pf⟩ ← normalize disch iM x
let ⟨l', pf'⟩ ← qNF.removeZeros disch iM l
let ⟨x', pf''⟩ ← qNF.evalPretty iM l'
let pf_yx : Q($y = $x') := q(Eq.trans (Eq.trans $pf $pf') $pf'')
return ⟨g.expr x', q(Eq.trans $pf_sgn $(g.congr pf_yx))⟩
/-- Given `e₁` and `e₂`, cancel nonzero factors to construct a new equality which is logically
equivalent to `e₁ = e₂`. -/
def reduceEqQ (disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type))
(iM : Q(CommGroupWithZero $M)) (e₁ e₂ : Q($M)) :
AtomM (Σ f₁ f₂ : Q($M), Q(($e₁ = $e₂) = ($f₁ = $f₂))) := do
let ⟨_, ⟨g₁, pf_sgn₁⟩, l₁, pf_l₁⟩ ← normalize disch iM e₁
let ⟨_, ⟨g₂, pf_sgn₂⟩, l₂, pf_l₂⟩ ← normalize disch iM e₂
let ⟨L, l₁', l₂', pf_lhs, pf_rhs, pf₀⟩ ← l₁.gcd iM l₂ disch .nonzero
let pf₀ : Q(NF.eval $(qNF.toNF L) ≠ 0) := pf₀
let ⟨f₁', pf_l₁'⟩ ← l₁'.evalPretty iM
let ⟨f₂', pf_l₂'⟩ ← l₂'.evalPretty iM
have pf_ef₁ := ← Sign.mkEqMul iM pf_sgn₁ q(Eq.trans $pf_l₁ (Eq.symm $pf_lhs)) pf_l₁'
have pf_ef₂ := ← Sign.mkEqMul iM pf_sgn₂ q(Eq.trans $pf_l₂ (Eq.symm $pf_rhs)) pf_l₂'
return ⟨g₁.expr f₁', g₂.expr f₂', q(eq_eq_cancel_eq $pf_ef₁ $pf_ef₂ $pf₀)⟩
/-- Given `e₁` and `e₂`, cancel positive factors to construct a new inequality which is logically
equivalent to `e₁ ≤ e₂`. -/
def reduceLeQ (disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type))
(iM : Q(CommGroupWithZero $M)) (iM' : Q(PartialOrder $M))
(iM'' : Q(PosMulStrictMono $M)) (iM''' : Q(PosMulReflectLE $M)) (iM'''' : Q(ZeroLEOneClass $M))
(e₁ e₂ : Q($M)) :
AtomM (Σ f₁ f₂ : Q($M), Q(($e₁ ≤ $e₂) = ($f₁ ≤ $f₂))) := do
let ⟨_, ⟨g₁, pf_sgn₁⟩, l₁, pf_l₁⟩ ← normalize disch iM e₁
let ⟨_, ⟨g₂, pf_sgn₂⟩, l₂, pf_l₂⟩ ← normalize disch iM e₂
let ⟨L, l₁', l₂', pf_lhs, pf_rhs, pf₀⟩
← l₁.gcd iM l₂ disch (.positive iM' iM'' q(inferInstance) iM'''')
let pf₀ : Q(0 < NF.eval $(qNF.toNF L)) := pf₀
let ⟨f₁', pf_l₁'⟩ ← l₁'.evalPretty iM
let ⟨f₂', pf_l₂'⟩ ← l₂'.evalPretty iM
have pf_ef₁ := ← Sign.mkEqMul iM pf_sgn₁ q(Eq.trans $pf_l₁ (Eq.symm $pf_lhs)) pf_l₁'
have pf_ef₂ := ← Sign.mkEqMul iM pf_sgn₂ q(Eq.trans $pf_l₂ (Eq.symm $pf_rhs)) pf_l₂'
return ⟨g₁.expr f₁', g₂.expr f₂', q(le_eq_cancel_le $pf_ef₁ $pf_ef₂ $pf₀)⟩
/-- Given `e₁` and `e₂`, cancel positive factors to construct a new inequality which is logically
equivalent to `e₁ < e₂`. -/
def reduceLtQ (disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type))
(iM : Q(CommGroupWithZero $M)) (iM' : Q(PartialOrder $M))
(iM'' : Q(PosMulStrictMono $M)) (iM''' : Q(PosMulReflectLT $M)) (iM'''' : Q(ZeroLEOneClass $M))
(e₁ e₂ : Q($M)) :
AtomM (Σ f₁ f₂ : Q($M), Q(($e₁ < $e₂) = ($f₁ < $f₂))) := do
let ⟨_, ⟨g₁, pf_sgn₁⟩, l₁, pf_l₁⟩ ← normalize disch iM e₁
let ⟨_, ⟨g₂, pf_sgn₂⟩, l₂, pf_l₂⟩ ← normalize disch iM e₂
let ⟨L, l₁', l₂', pf_lhs, pf_rhs, pf₀⟩
← l₁.gcd iM l₂ disch (.positive iM' iM'' iM''' iM'''')
let pf₀ : Q(0 < NF.eval $(qNF.toNF L)) := pf₀
let ⟨f₁', pf_l₁'⟩ ← l₁'.evalPretty iM
let ⟨f₂', pf_l₂'⟩ ← l₂'.evalPretty iM
have pf_ef₁ := ← Sign.mkEqMul iM pf_sgn₁ q(Eq.trans $pf_l₁ (Eq.symm $pf_lhs)) pf_l₁'
have pf_ef₂ := ← Sign.mkEqMul iM pf_sgn₂ q(Eq.trans $pf_l₂ (Eq.symm $pf_rhs)) pf_l₂'
return ⟨g₁.expr f₁', g₂.expr f₂', q(lt_eq_cancel_lt $pf_ef₁ $pf_ef₂ $pf₀)⟩
/-- Given `x` in a commutative group-with-zero, construct a new expression in the standard form
*** / *** (all denominators at the end) which is equal to `x`. -/
def reduceExpr (disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type)) (x : Expr) :
AtomM Simp.Result := do
-- for `field_simp` to work with the recursive infrastructure in `AtomM.recurse`, we need to fail
-- on things `field_simp` would treat as atoms
guard x.isApp
let ⟨f, _⟩ := x.getAppFnArgs
guard <|
f ∈ [``HMul.hMul, ``HDiv.hDiv, ``Inv.inv, ``HPow.hPow, ``HAdd.hAdd, ``HSub.hSub, ``Neg.neg]
-- infer `u` and `K : Q(Type u)` such that `x : Q($K)`
let ⟨u, K, _⟩ ← inferTypeQ' x
-- find a `CommGroupWithZero` instance on `K`
let iK : Q(CommGroupWithZero $K) ← synthInstanceQ q(CommGroupWithZero $K)
-- run the core normalization function `normalizePretty` on `x`
trace[Tactic.field_simp] "putting {x} in \"field_simp\"-normal-form"
let ⟨e, pf⟩ ← reduceExprQ disch iK x
return { expr := e, proof? := some pf }
/-- Given an (in)equality `a = b` (respectively, `a ≤ b`, `a < b`), cancel nonzero (resp. positive)
factors to construct a new (in)equality which is logically equivalent to `a = b` (respectively,
`a ≤ b`, `a < b`). -/
def reduceProp (disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type)) (t : Expr) :
AtomM Simp.Result := do
let ⟨i, _, a, b⟩ ← t.ineq?
-- infer `u` and `K : Q(Type u)` such that `x : Q($K)`
let ⟨u, K, a⟩ ← inferTypeQ' a
-- find a `CommGroupWithZero` instance on `K`
let iK : Q(CommGroupWithZero $K) ← synthInstanceQ q(CommGroupWithZero $K)
trace[Tactic.field_simp] "clearing denominators in {a} ~ {b}"
-- run the core (in)equality-transforming mechanism on `a =/≤/< b`
match i with
| .eq =>
let ⟨a', b', pf⟩ ← reduceEqQ disch iK a b
let t' ← mkAppM `Eq #[a', b']
return { expr := t', proof? := pf }
| .le =>
let iK' : Q(PartialOrder $K) ← synthInstanceQ q(PartialOrder $K)
let iK'' : Q(PosMulStrictMono $K) ← synthInstanceQ q(PosMulStrictMono $K)
let iK''' : Q(PosMulReflectLE $K) ← synthInstanceQ q(PosMulReflectLE $K)
let iK'''' : Q(ZeroLEOneClass $K) ← synthInstanceQ q(ZeroLEOneClass $K)
let ⟨a', b', pf⟩ ← reduceLeQ disch iK iK' iK'' iK''' iK'''' a b
let t' ← mkAppM `LE.le #[a', b']
return { expr := t', proof? := pf }
| _ =>
let iK' : Q(PartialOrder $K) ← synthInstanceQ q(PartialOrder $K)
let iK'' : Q(PosMulStrictMono $K) ← synthInstanceQ q(PosMulStrictMono $K)
let iK''' : Q(PosMulReflectLT $K) ← synthInstanceQ q(PosMulReflectLT $K)
let iK'''' : Q(ZeroLEOneClass $K) ← synthInstanceQ q(ZeroLEOneClass $K)
let ⟨a', b', pf⟩ ← reduceLtQ disch iK iK' iK'' iK''' iK'''' a b
let t' ← mkAppM `LT.lt #[a', b']
return { expr := t', proof? := pf }
/-! ### Frontend -/
open Elab Tactic Lean.Parser.Tactic
/-- If the user provided a discharger, elaborate it. If not, we will use the `field_simp_discharge`
default discharger, which (among other things) includes a simp-run for the specified argument list,
so we elaborate those arguments. -/
def parseDischarger (d : Option (TSyntax ``discharger)) (args : Option (TSyntax ``simpArgs)) :
TacticM (∀ {u : Level} (type : Q(Sort u)), MetaM Q($type)) := do
match d with
| none =>
let ctx ← Simp.Context.ofArgs (args.getD ⟨.missing⟩) { contextual := true }
return fun e ↦ Prod.fst <$> (FieldSimp.discharge e).run ctx >>= Option.getM
| some d =>
if args.isSome then
logWarningAt args.get!
"Custom `field_simp` dischargers do not make use of the `field_simp` arguments list"
match d with
| `(discharger| (discharger := $tac)) =>
let tac := (evalTactic (← `(tactic| ($tac))) *> pruneSolvedGoals)
return (synthesizeUsing' · tac)
| _ => throwError "could not parse the provided discharger {d}"
/--
`field_simp` normalizes expressions in (semi-)fields by rewriting them to a common denominator,
i.e. to reduce them to expressions of the form `n / d` where neither `n` nor `d` contains any
division symbol. The `field_simp` tactic will also clear denominators in field *(in)equalities*, by
cross-multiplying.
A very common pattern is `field_simp; ring` (clear denominators, then the resulting goal is
solvable by the axioms of a commutative ring). The finishing tactic `field` is a shorthand for this
pattern.
The tactic will try discharge proofs of nonzeroness of denominators, and skip steps if discharging
fails. These denominators are made out of denominators appearing in the input expression,
by repeatedly taking products or divisors. The default discharger can be non-universal, i.e. can be
specific to the field at hand (order properties, explicit `≠ 0` hypotheses, `CharZero` if that is
known, etc). See `field_simp_discharge` for full details of the default discharger algorithm.
* `field_simp at l1 l2 ...` can be used to normalize at the given locations.
* `field_simp (disch := tac)` uses the tactic sequence `tac` to discharge nonzeroness/positivity
proofs.
* `field_simp [t₁, ..., tₙ]` provides terms `t₁`, ..., `tₙ` to the discharger for
nonzeroness/positivity proofs.
Examples:
```
-- `x / (1 - y) / (1 + y / (1 - y))` is reduced to `x / (1 - y + y)`
example (x y z : ℚ) (hy : 1 - y ≠ 0) :
⌊x / (1 - y) / (1 + y / (1 - y))⌋ < 3 := by
field_simp
-- new goal: `⊢ ⌊x / (1 - y + y)⌋ < 3`
sorry
-- `field_simp` will clear the `x` denominators in the following equation
example {K : Type*} [Field K] {x : K} (hx0 : x ≠ 0) :
(x + 1 / x) ^ 2 + (x + 1 / x) = 1 := by
field_simp
-- new goal: `⊢ (x ^ 2 + 1) * (x ^ 2 + 1 + x) = x ^ 2`
sorry
```
-/
elab (name := fieldSimp) "field_simp" d:(discharger)? args:(simpArgs)? loc:(location)? :
tactic => withMainContext do
let disch ← parseDischarger d args
let s ← IO.mkRef {}
let cleanup r := do r.mkEqTrans (← simpOnlyNames [] r.expr) -- convert e.g. `x = x` to `True`
let m := AtomM.recurse s { contextual := true } (wellBehavedDischarge := false)
(fun e ↦ reduceProp disch e <|> reduceExpr disch e) cleanup
let loc := (loc.map expandLocation).getD (.targets #[] true)
transformAtLocation (m ·) "field_simp" (ifUnchanged := .error) (mayCloseGoalFromHyp := true) loc
/--
`field_simp` normalizes an expression in a (semi-)field by rewriting it to a common denominator,
i.e. to reduce it to an expression of the form `n / d` where neither `n` nor `d` contains any
division symbol.
The `field_simp` conv tactic is a variant of the main (i.e., not conv) `field_simp` tactic. The
latter operates recursively on subexpressions, bringing *every* field-expression encountered to the
form `n / d`.
The tactic will try discharge proofs of nonzeroness of denominators, and skip steps if discharging
fails. These denominators are made out of denominators appearing in the input expression,
by repeatedly taking products or divisors. The default discharger can be non-universal, i.e. can be
specific to the field at hand (order properties, explicit `≠ 0` hypotheses, `CharZero` if that is
known, etc). See `field_simp_discharge` for full details of the default discharger algorithm.
* `field_simp (disch := tac)` uses the tactic sequence `tac` to discharge nonzeroness/positivity
proofs.
* `field_simp [t₁, ..., tₙ]` provides terms `t₁`, ..., `tₙ` to the discharger for
nonzeroness/positivity proofs.
Examples:
```
-- `x / (1 - y) / (1 + y / (1 - y))` is reduced to `x / (1 - y + y)`:
example (x y z : ℚ) (hy : 1 - y ≠ 0) :
⌊x / (1 - y) / (1 + y / (1 - y))⌋ < 3 := by
conv => enter [1, 1]; field_simp
-- new goal: `⊢ ⌊x / (1 - y + y)⌋ < 3`
```
-/
elab "field_simp" d:(discharger)? args:(simpArgs)? : conv => do
-- find the expression `x` to `conv` on
let x ← Conv.getLhs
let disch : ∀ {u : Level} (type : Q(Sort u)), MetaM Q($type) ← parseDischarger d args
-- bring into field_simp standard form
let r ← AtomM.run .reducible <| reduceExpr disch x
-- convert `x` to the output of the normalization
Conv.applySimpResult r
/--
`field` is a `simp` set that clears denominators in (semi-)field (in)equalities.
The `field` simp set is a variant of the `field_simp` tactic. The latter operates recursively on
subexpressions, bringing every field-expression encountered to the form `n / d`, and then attempts
to clear the denominator. (For confluence reasons, the `field` simprocs also have a slightly
different normal form from `field_simp`'s.)
The tactic will try discharge proofs of nonzeroness of denominators, and skip steps if discharging
fails. These denominators are made out of denominators appearing in the input expression,
by repeatedly taking products or divisors. The discharger can be non-universal, i.e. can be specific
to the field at hand (order properties, explicit `≠ 0` hypotheses, `CharZero` if that is known,
etc). See `field_simp_discharge` for full details of the discharger algorithm.
* `simp [field, t₁, ..., tₙ]` provides terms `t₁`, ..., `tₙ` to the discharger for
nonzeroness/positivity proofs.
Examples:
```
example {K : Type*} [Field K] {x : K} (hx0 : x ≠ 0) :
(x + 1 / x) ^ 2 + (x + 1 / x) = 1 := by
simp only [field]
-- new goal: `⊢ (x ^ 2 + 1) * (x ^ 2 + 1 + x) = x ^ 2`
```
-/
def proc : Simp.Simproc := fun (t : Expr) ↦ do
let ctx ← Simp.getContext
let disch e : MetaM Expr := Prod.fst <$> (FieldSimp.discharge e).run ctx >>= Option.getM
try
let r ← AtomM.run .reducible <| FieldSimp.reduceProp disch t
-- the `field_simp`-normal form is in opposition to the `simp`-lemmas `one_div` and `mul_inv`,
-- so we need to undo any such lemma applications, otherwise we can get infinite loops
return .visit <| ← r.mkEqTrans (← simpOnlyNames [``one_div, ``mul_inv] r.expr)
catch _ =>
return .continue
end Mathlib.Tactic.FieldSimp
open Mathlib.Tactic
simproc_decl fieldEq (Eq _ _) := FieldSimp.proc
simproc_decl fieldLe (LE.le _ _) := FieldSimp.proc
simproc_decl fieldLt (LT.lt _ _) := FieldSimp.proc
attribute [field, inherit_doc FieldSimp.proc] fieldEq fieldLe fieldLt
/-!
We register `field_simp` with the `hint` tactic.
-/
register_hint 1000 field_simp
register_try?_tactic (priority := 1000) field_simp