File size: 3,497 Bytes
d03762b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/- Copyright (c) Heather Macbeth, 2023.  All rights reserved. -/
import Mathlib.Algebra.GroupPower.Order
import Mathlib.Tactic.Positivity

open Lean

syntax (name := cancelDischarger) "cancel_discharger " : tactic
syntax (name := cancelAux) "cancel_aux " term " at " term : tactic
syntax (name := cancel) "cancel " term " at " term : tactic

macro_rules | `(tactic| cancel_discharger) => `(tactic | positivity)
macro_rules
| `(tactic| cancel_discharger) =>`(tactic | fail "cancel failed, could not verify the following side condition:")

/-! ### powers -/
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := lt_of_pow_lt_pow (n := $a) (by cancel_discharger) $(mkIdent h))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := le_of_pow_le_pow (n := $a) (by cancel_discharger) (by cancel_discharger) $(mkIdent h))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := pow_eq_zero (n := $a) $(mkIdent h))


/-! ### multiplication, LHS and RHS -/
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := mul_left_cancel₀ (a := $a) (by cancel_discharger) $(mkIdent h))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := mul_right_cancel₀ (b := $a) (by cancel_discharger) $(mkIdent h))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := le_of_mul_le_mul_left (a := $a) $(mkIdent h) (by cancel_discharger))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := le_of_mul_le_mul_right (a := $a) $(mkIdent h) (by cancel_discharger))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := lt_of_mul_lt_mul_left (a := $a) $(mkIdent h) (by cancel_discharger))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := lt_of_mul_lt_mul_right (a := $a) $(mkIdent h) (by cancel_discharger))

/-! ### multiplication, just LHS -/
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := pos_of_mul_pos_right (a := $a) $(mkIdent h) (by cancel_discharger))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := pos_of_mul_pos_left (b := $a) $(mkIdent h) (by cancel_discharger))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := nonneg_of_mul_nonneg_right (a := $a) $(mkIdent h) (by cancel_discharger))
macro_rules
| `(tactic| cancel_aux $a at $h) =>
  let h := h.raw.getId
  `(tactic | replace $(mkIdent h):ident := nonneg_of_mul_nonneg_left (b := $a) $(mkIdent h) (by cancel_discharger))

-- TODO to trigger this needs some `guard_hyp` in the `cancel_aux` implementations
elab_rules : tactic
  | `(tactic| cancel $a at $h) => do
  let goals ← Elab.Tactic.getGoals
  let goalsMsg := MessageData.joinSep (goals.map MessageData.ofGoal) m!"\n\n"
  throwError "cancel failed: no '{a}' to cancel\n{goalsMsg}"

-- TODO build in a `try change 1_ at h` to upgrade the `0 < _` result in the case of Nat
macro_rules | `(tactic| cancel $a at $h) => `(tactic| cancel_aux $a at $h; try apply $h)