Repository navigation
Expand file tree
/
Copy pathMathlibIntegerUnitClassification.lean
More file actions
80 lines (68 loc) · 4.01 KB
/
Copy pathMathlibIntegerUnitClassification.lean
File metadata and controls
80 lines (68 loc) · 4.01 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
module
public import Mathlib.Algebra.Ring.Int.Units
import all Mathlib.Algebra.Ring.Int.Units
import Lean
public section
namespace OmegaProofs.IntegerUnitClassification
theorem twoValues : ∀ u : ℤˣ, u = 1 ∨ u = -1 := @Int.units_eq_one_or
theorem different : ∀ {u v : ℤˣ}, u ≠ v ↔ u = -v := @Int.units_ne_iff_eq_neg
theorem valueEquality : ∀ {α : Type u} [Monoid α] {a b : αˣ}, (a = b) = ((a : α) = (b : α)) := @Int.units_eq_one_or._simp_1_1
theorem negativeValueEquality : ∀ {α : Type u} [Monoid α] {a b : αˣ}, (a = b) = ((a : α) = (b : α)) := @Int.units_ne_iff_eq_neg._simp_1_1
open Lean Elab Command in
run_elab
let prefixName := Name.num `_private.Mathlib.Algebra.Ring.Int.Units 0
for (wrapper, suffix) in [
(`OmegaProofs.IntegerUnitClassification.valueEquality, `Int.units_eq_one_or._simp_1_1),
(`OmegaProofs.IntegerUnitClassification.negativeValueEquality, `Int.units_ne_iff_eq_neg._simp_1_1)] do
let .thmInfo info ← getConstInfo wrapper | throwError "expected theorem {wrapper}"
let .const actual _ := info.value | throwError "expected direct reference in {wrapper}"
unless actual == prefixName ++ suffix do throwError "wrong private constant in {wrapper}"
private def signed (s n : ℕ) : ℤ := if s = 0 then n else -(n : ℤ)
private def positive (s n : ℕ) := if s = 0 then n else 0
private def negative (s n : ℕ) := if s = 0 then 0 else n
private def sign (s : ℕ) : ℕ := if s = 0 then 0 else 1
private def flip (s : ℕ) : ℕ := if s = 0 then 1 else 0
private def act (s t : ℕ) := if s = 0 then sign t else flip t
-- Start with the value and inverse of an arbitrary unit, before classifying either.
private theorem allIntegers (z : ℤ) : ∃ s n : ℕ, signed s n = z := by
cases z with
| ofNat n => exact ⟨0,n,rfl⟩
| negSucc n => exact ⟨1,n+1,rfl⟩
example (u : ℤˣ) : ∃ s n t m : ℕ, signed s n = u ∧ signed t m = ↑u⁻¹ ∧
signed s n * signed t m = 1 := by
obtain ⟨s,n,hs⟩ := allIntegers u
obtain ⟨t,m,ht⟩ := allIntegers ↑u⁻¹
exact ⟨s,n,t,m,hs,ht,by rw [hs,ht]; exact u.mul_inv⟩
example (s n t m : ℕ) : signed (act s t) (n*m) = signed s n * signed t m := by
cases s <;> cases t <;> simp [act, sign, flip, signed]
example (s n t m : ℕ) : signed s n = signed t m ↔
positive s n = positive t m ∧ negative s n = negative t m := by
cases s <;> cases t <;> simp [signed,positive,negative] <;> omega
example (s : ℕ) : signed (sign s) 1 = 1 ∨ signed (sign s) 1 = -1 := by
cases s <;> simp [sign,signed]
example (s t : ℕ) : signed s 1 ≠ signed t 1 ↔ act s t = 1 := by
cases s <;> cases t <;> simp [signed,act,sign,flip]
example (s t : ℕ) : signed s 1 = -(signed t 1) ↔ flip (act s (flip t)) = 1 := by
cases s <;> cases t <;> simp [signed,act,sign,flip]
-- The controls allow products 2 and -2, each with a factor of magnitude 2.
-- Keeping only the zero negative part also admits 2, outside both unit cases.
example : positive (act 0 0) (2*1) = 2 ∧ negative (act 0 0) (2*1) = 0 ∧ (2 : ℕ) ≠ 1 := by decide
example : positive (act 1 0) (2*1) = 0 ∧ negative (act 1 0) (2*1) = 2 ∧ (2 : ℕ) ≠ 1 := by decide
example : negative (act 0 0) (2*1) = 0 ∧ positive 0 2 ≠ positive (sign 0) 1 := by decide
-- A unit stores a value, an inverse and two proofs. Once both components
-- agree, proof irrelevance identifies the two records at every universe.
example {α : Type u} [Monoid α] (a b : αˣ) : a = b ↔ a.val = b.val ∧ a.inv = b.inv := by
constructor
· rintro rfl; exact ⟨rfl,rfl⟩
· cases a with | mk a x ha hx =>
cases b with | mk b y hb hy =>
rintro ⟨h₁,h₂⟩; change a = b at h₁; change x = y at h₂
subst b; subst y; rfl
example (P Q : Prop) : (P = Q) ↔ (P ↔ Q) := ⟨Iff.of_eq,propext⟩
example {α : Type u} [Monoid α] (a b : αˣ) : a.inv * a.val = 1 ∧ b.val * b.inv = 1 :=
⟨a.inv_mul, b.mul_inv⟩
#print axioms twoValues
#print axioms different
#print axioms valueEquality
#print axioms negativeValueEquality
end OmegaProofs.IntegerUnitClassification