Skip to content

Commit 6fd83b0

Browse files
committed
improve
1 parent b498fb0 commit 6fd83b0

1 file changed

Lines changed: 57 additions & 80 deletions

File tree

Mathlib/ModelTheory/Arithmetic/Presburger/Definability.lean

Lines changed: 57 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Authors: Dexin Zhang
66
import Mathlib.Algebra.GCDMonoid.Finset
77
import Mathlib.Algebra.GCDMonoid.Nat
88
import Mathlib.ModelTheory.Arithmetic.Presburger.Basic
9-
import Mathlib.ModelTheory.Arithmetic.Presburger.Semilinear.Nat
9+
import Mathlib.ModelTheory.Arithmetic.Presburger.Semilinear.FinitelyGenerated
1010
import Mathlib.ModelTheory.Definability
1111

1212
/-!
@@ -115,7 +115,7 @@ lemma boundedFormula_realize_semilinear {n} (φ : presburger[[A]].BoundedFormula
115115
| equal t₁ t₂ =>
116116
rcases term_realize_eq_add_dotProduct t₁ with ⟨k₁, u₁, ht₁⟩
117117
rcases term_realize_eq_add_dotProduct t₂ with ⟨k₂, u₂, ht₂⟩
118-
convert Semilinear.of_linear_equation ![k₁] ![k₂] (.of ![u₁]) (.of ![u₂])
118+
convert Semilinear.of_linear_equation_nat ![k₁] ![k₂] (.of ![u₁]) (.of ![u₂])
119119
simp [ht₁, ht₂]
120120
| rel f => nomatch f
121121
| falsum => exact Semilinear.empty
@@ -124,33 +124,24 @@ lemma boundedFormula_realize_semilinear {n} (φ : presburger[[A]].BoundedFormula
124124
simp [setOf_inter_eq_sep, imp_iff_not_or, compl_setOf]
125125
| @all n φ ih =>
126126
let e := (Equiv.sumAssoc α (Fin n) (Fin 1)).trans (Equiv.sumCongr (.refl α) finSumFinEquiv)
127-
convert (ih.compl.reindex e).proj.compl using 1
128-
simp_rw [compl_setOf, not_exists, Fin.forall_fin_succ_pi, Fin.forall_fin_zero_pi, mem_image,
129-
not_exists, mem_setOf, not_and, not_imp_not]
130-
congr! 3 with x k
131-
constructor
132-
· intro hφ y hy
133-
convert hφ using 1 <;> ext i
134-
· apply congr_fun (a := e.symm (Sum.inl i)) at hy
135-
simpa [e] using hy
136-
· apply congr_fun (a := e.symm (Sum.inr i)) at hy
137-
cases i using Fin.lastCases <;> simpa [e] using hy
138-
· intro h
139-
convert h (fun y => Sum.elim x ![k] (e.symm y)) ?_ using 1
140-
· ext i
141-
cases i using Fin.lastCases <;> simp [e]
142-
· ext i
143-
cases i with
144-
| inl => simp
145-
| inr i => fin_cases i; simp
127+
rw [← Semilinear.image_iff (LinearEquiv.funCongrLeft ℕ ℕ e)] at ih
128+
convert ih.compl.proj.compl using 1
129+
simp_rw [compl_setOf, not_exists, Fin.forall_fin_succ_pi, Fin.forall_fin_zero_pi,
130+
mem_compl_iff, mem_image, not_not, ← LinearEquiv.eq_symm_apply, LinearEquiv.funCongrLeft_symm,
131+
exists_eq_right, mem_setOf, LinearEquiv.funCongrLeft_apply, LinearMap.funLeft,
132+
LinearMap.coe_mk, AddHom.coe_mk]
133+
congr! 4
134+
ext i
135+
cases i using Fin.lastCases <;> simp [e]
146136

147137
lemma formula_realize_semilinear (φ : presburger[[A]].Formula α) :
148138
(setOf φ.Realize : Set (α → ℕ)).Semilinear := by
149-
convert (boundedFormula_realize_semilinear φ).reindex (Equiv.sumEmpty α (Fin 0)).symm
139+
let e := Equiv.sumEmpty α (Fin 0)
140+
convert (boundedFormula_realize_semilinear φ).image (LinearMap.funLeft ℕ ℕ e.symm)
150141
ext x
151142
simp only [mem_setOf_eq, mem_image]
152-
rw [((Equiv.sumEmpty α (Fin 0)).arrowCongr (.refl ℕ)).exists_congr_left]
153-
simp [Formula.Realize, Unique.eq_default, Function.comp_def]
143+
rw [(e.arrowCongr (.refl ℕ)).exists_congr_left]
144+
simp [Formula.Realize, Unique.eq_default, Function.comp_def, LinearMap.funLeft, e]
154145

155146
/-- A set is Presburger definable in `ℕ` if and only if it is semilinear. -/
156147
theorem definable_iff_semilinear {s : Set (α → ℕ)} :
@@ -161,18 +152,18 @@ theorem definable_iff_semilinear {s : Set (α → ℕ)} :
161152
is ultimately periodic, i.e. periodic after some number `k`. -/
162153
theorem definable₁_iff_ultimately_periodic {s : Set ℕ} :
163154
A.Definable₁ presburger s ↔ ∃ k, ∃ p > 0, ∀ x ≥ k, x ∈ s ↔ x + p ∈ s := by
164-
rw [Definable₁, definable_iff_semilinear]
155+
rw [Definable₁, definable_iff_semilinear,
156+
← Semilinear.image_iff (LinearEquiv.funUnique (Fin 1) ℕ ℕ), ← preimage_setOf_eq]
157+
simp only [LinearEquiv.funUnique_apply, Function.eval, Fin.default_eq_zero, setOf_mem_eq]
158+
rw [image_preimage_eq s fun x => ⟨![x], rfl⟩]
165159
constructor
166160
· intro hs
167161
apply Semilinear.proper_semilinear at hs
168-
rcases hs with ⟨S, hS, hs⟩
169-
simp only [Set.ext_iff, mem_setOf_eq, mem_sUnion, Finset.mem_coe] at hs
170-
replace hs := (hs ![·])
171-
simp only [Fin.isValue, cons_val_fin_one] at hs
172-
replace hS : ∀ t ∈ S, ∃ k, ∃ p > 0, ∀ x ≥ k, ![x] ∈ t ↔ ![x + p] ∈ t := by
162+
rcases hs with ⟨S, hS, rfl⟩
163+
replace hS : ∀ t ∈ S, ∃ k, ∃ p > 0, ∀ x ≥ k, x ∈ t ↔ x + p ∈ t := by
173164
intro t ht
174165
apply hS at ht
175-
rcases ht withv, t, ht, rfl⟩
166+
rcases ht witha, t, ht, rfl⟩
176167
have hcard : t.card ≤ 1 := by
177168
by_contra hcard
178169
simp only [not_le, Finset.one_lt_card_iff] at hcard
@@ -181,49 +172,41 @@ theorem definable₁_iff_ultimately_periodic {s : Set ℕ} :
181172
intro hb'
182173
apply ht.zero_notMem_image
183174
simp [← hb', hb]
184-
simp only [ne_eq, funext_iff, Fin.forall_fin_one, Pi.zero_apply] at hb'
185175
revert ht
186176
simp only [imp_false, not_linearIndepOn_finset_iffₛ, id_eq]
187-
refine ⟨Pi.single a (b 0), Pi.single b (a 0), ?_, a, ha, ?_⟩
188-
· simpa [Pi.single_apply, ha, hb, funext_iff (f := (b 0 : Fin 1 → ℕ) * a),
189-
Fin.forall_fin_one] using mul_comm (b 0) (a 0)
177+
refine ⟨Pi.single a b, Pi.single b a, ?_, a, ha, ?_⟩
178+
· simpa [Pi.single_apply, ha, hb] using mul_comm b a
190179
· simp [hab, hb']
191180
simp_rw [Finset.card_le_one_iff_subset_singleton, Finset.subset_singleton_iff] at hcard
192-
rcases hcard with ⟨u, (rfl | rfl)⟩
193-
· refine ⟨v 0 + 1, 1, zero_lt_one, fun x hx => ?_⟩
194-
have hx' : x ≠ v 0 := (Nat.lt_of_succ_le hx).ne'
195-
have hx'' : x + 1 ≠ v 0 := (Nat.lt_of_succ_le (Nat.le_succ_of_le hx)).ne'
196-
simp [funext_iff, Fin.forall_fin_one, hx', hx'']
197-
· have hu : u ≠ 0 := by
198-
intro hu
181+
rcases hcard with ⟨b, (rfl | rfl)⟩
182+
· refine ⟨a + 1, 1, zero_lt_one, fun x hx => ?_⟩
183+
simp [(Nat.lt_of_succ_le hx).ne', (Nat.lt_of_succ_le (Nat.le_succ_of_le hx)).ne']
184+
· have hb : b ≠ 0 := by
185+
intro hb
199186
apply ht.zero_notMem_image
200-
simp [hu]
201-
simp only [ne_eq, funext_iff, Fin.forall_fin_one, Pi.zero_apply, Nat.ne_zero_iff_zero_lt]
202-
at hu
203-
refine ⟨v 0, u 0, hu, fun x hx => ?_⟩
204-
simp only [Nat.succ_eq_add_one, Nat.reduceAdd, Finset.coe_singleton, mem_vadd_set,
205-
SetLike.mem_coe, Submodule.mem_span_singleton, nsmul_eq_mul, funext_iff, Pi.mul_apply,
206-
Pi.natCast_apply, Nat.cast_id, Fin.forall_fin_one, Fin.isValue, vadd_eq_add, Pi.add_apply,
207-
cons_val_fin_one]
187+
simp [hb]
188+
rw [Nat.ne_zero_iff_zero_lt] at hb
189+
refine ⟨a, b, hb, fun x hx => ?_⟩
190+
simp only [Finset.coe_singleton, mem_vadd_set, SetLike.mem_coe,
191+
Submodule.mem_span_singleton, smul_eq_mul, vadd_eq_add, exists_exists_eq_and]
208192
constructor
209-
· rintro ⟨y, ⟨a, ha⟩, rfl⟩
210-
refine ⟨y + u, ⟨a + 1, ?_⟩, ?_⟩ <;> simp [← ha, Nat.add_one_mul, add_assoc]
211-
· rintro ⟨y, ⟨a, ha⟩, heq⟩
212-
rw [← ha] at heq
213-
cases a with
193+
· rintro ⟨x, rfl⟩
194+
refine ⟨x + 1, ?_⟩
195+
simp [add_one_mul, add_assoc]
196+
· rintro ⟨y, heq
197+
cases y with
214198
| zero =>
215199
rw [zero_mul, add_zero] at heq
216-
simp only [heq, add_le_iff_nonpos_right, nonpos_iff_eq_zero] at hx
217-
simp [hx] at hu
218-
| succ a =>
200+
simp only [heq, ge_iff_le, add_le_iff_nonpos_right, nonpos_iff_eq_zero] at hx
201+
simp [hx] at hb
202+
| succ y =>
219203
rw [add_one_mul, ← add_assoc, add_right_cancel_iff] at heq
220-
refine ⟨a * u 0, ⟨a, rfl⟩, heq⟩
204+
exact ⟨y, heq⟩
221205
choose! k p hS hS' using hS
222206
refine ⟨S.sup k, S.lcm p, ?_, fun x hx => ?_⟩
223207
· rw [gt_iff_lt, Nat.pos_iff_ne_zero, ne_eq]
224208
simpa [Finset.lcm_eq_zero_iff, ← Nat.pos_iff_ne_zero]
225209
· simp only [ge_iff_le, Finset.sup_le_iff] at hx
226-
rw [hs, hs]
227210
refine exists_congr fun t => and_congr_right fun ht => ?_
228211
have hpt : p t ∣ S.lcm p := Finset.dvd_lcm ht
229212
rw [dvd_iff_exists_eq_mul_left] at hpt
@@ -239,33 +222,27 @@ theorem definable₁_iff_ultimately_periodic {s : Set ℕ} :
239222
have h₁ : {x ∈ s | x < k}.Finite := (Set.finite_lt_nat k).subset (sep_subset_setOf _ _)
240223
have h₂ : {x ∈ s | k ≤ x ∧ x < k + p}.Finite :=
241224
(Set.finite_Ico k (k + p)).subset (sep_subset_setOf _ _)
242-
convert (h₁.image (![·])).semilinear.union
243-
((h₂.image (![·])).semilinear.add (Semilinear.singleton ![p]).span) using 1
244-
ext v
245-
simp only [mem_setOf_eq, Nat.succ_eq_add_one, Nat.reduceAdd, sep_and, mem_union,
246-
mem_image, mem_add, mem_inter_iff, SetLike.mem_coe, Submodule.mem_span_singleton, smul_cons,
247-
smul_eq_mul, Matrix.smul_empty, exists_exists_eq_and, add_cons, empty_add_empty,
248-
exists_exists_and_eq_and, head_cons]
225+
convert h₁.semilinear.union (h₂.semilinear.add (Semilinear.span_finset {p}))
226+
ext x
227+
simp only [sep_and, Finset.coe_singleton, mem_union, mem_setOf_eq, mem_add, mem_inter_iff,
228+
SetLike.mem_coe, Submodule.mem_span_singleton, smul_eq_mul, exists_exists_eq_and]
249229
constructor
250-
· intro hv
251-
by_cases hv' : v 0 < k
252-
· refine Or.inl ⟨v 0, ⟨hv, hv'⟩, ?_⟩
253-
simp [funext_iff, Fin.forall_fin_one]
254-
· simp only [not_lt] at hv'
255-
refine Or.inr ⟨k + (v 0 - k) % p, ⟨⟨?_1, ?_2⟩, ?_1, ?_3⟩, (v 0 - k) / p, ?_4⟩
256-
· rw [← add_tsub_cancel_of_le hv', ← Nat.mod_add_div' (v 0 - k) p, ← add_assoc] at hv
257-
generalize (v 0 - k) / p = m at hv
230+
· intro hx
231+
by_cases hx' : x < k
232+
· exact Or.inl ⟨hx, hx'⟩
233+
· rw [not_lt] at hx'
234+
refine Or.inr ⟨k + (x - k) % p, ⟨⟨?_1, ?_2⟩, ?_1, ?_3⟩, (x - k) / p, ?_4⟩
235+
· rw [← add_tsub_cancel_of_le hx', ← Nat.mod_add_div' (x - k) p, ← add_assoc] at hx
236+
generalize (x - k) / p = m at hx
258237
induction m with
259-
| zero => simpa using hv
238+
| zero => simpa using hx
260239
| succ m ih =>
261240
refine ih ?_
262241
rwa [hs _ (Nat.le_add_right_of_le (Nat.le_add_right _ _)), add_assoc, ← Nat.add_one_mul]
263242
· apply Nat.le_add_right
264243
· apply Nat.add_lt_add_left (Nat.mod_lt _ hp)
265-
· rw [add_assoc, Nat.mod_add_div', add_tsub_cancel_of_le hv']
266-
simp [funext_iff, Fin.forall_fin_one]
267-
· rintro (⟨x, ⟨hx, _⟩, rfl⟩ | ⟨x, ⟨⟨hx, hx'⟩, _⟩, m, rfl⟩)
268-
<;> simp only [cons_val_fin_one]
244+
· rw [add_assoc, Nat.mod_add_div', add_tsub_cancel_of_le hx']
245+
· rintro (⟨hx, hx'⟩ | ⟨x, ⟨⟨hx, hx'⟩, _⟩, m, rfl⟩)
269246
· exact hx
270247
· induction m with
271248
| zero => simpa

0 commit comments

Comments
 (0)