Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 658cd38

Browse files
committed
feat(tactic/derive_fintype): derive fintype instances (#3772)
This adds a `@[derive fintype]` implementation, like so: ``` @[derive fintype] inductive foo (α : Type) | A : α → foo | B : α → α → foo | C : α × α → foo | D : foo ```
1 parent 18746ef commit 658cd38

File tree

8 files changed

+423
-7
lines changed

8 files changed

+423
-7
lines changed

src/data/finset/basic.lean

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,31 @@ by rw [coe_singleton, set.singleton_subset_iff]
251251
{a} ⊆ s ↔ a ∈ s :=
252252
singleton_subset_set_iff
253253

254+
/-! ### cons -/
255+
256+
/-- `cons a s h` is the set `{a} ∪ s` containing `a` and the elements of `s`. It is the same as
257+
`insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq α`,
258+
and the union is guaranteed to be disjoint. -/
259+
def cons {α} (a : α) (s : finset α) (h : a ∉ s) : finset α :=
260+
⟨a :: s.1, multiset.nodup_cons.2 ⟨h, s.2⟩⟩
261+
262+
@[simp] theorem mem_cons {α a s h b} : b ∈ @cons α a s h ↔ b = a ∨ b ∈ s :=
263+
by rcases s with ⟨⟨s⟩⟩; apply list.mem_cons_iff
264+
265+
@[simp] theorem cons_val {a : α} {s : finset α} (h : a ∉ s) : (cons a s h).1 = a :: s.1 := rfl
266+
267+
/-! ### disjoint union -/
268+
269+
/-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`.
270+
It is the same as `s ∪ t`, but it does not require decidable equality on the type. The hypothesis
271+
ensures that the sets are disjoint. -/
272+
def disj_union {α} (s t : finset α) (h : ∀ a ∈ s, a ∉ t) : finset α :=
273+
⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, h⟩⟩
274+
275+
@[simp] theorem mem_disj_union {α s t h a} :
276+
a ∈ @disj_union α s t h ↔ a ∈ s ∨ a ∈ t :=
277+
by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append
278+
254279
/-! ### insert -/
255280
section decidable_eq
256281
variables [decidable_eq α]
@@ -276,6 +301,9 @@ mem_ndinsert_of_mem h
276301
theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s :=
277302
(mem_insert.1 h).resolve_left
278303

304+
@[simp] theorem cons_eq_insert {α} [decidable_eq α] (a s h) : @cons α a s h = insert a s :=
305+
ext $ λ a, by simp
306+
279307
@[simp, norm_cast] lemma coe_insert (a : α) (s : finset α) :
280308
↑(insert a s) = (insert a ↑s : set α) :=
281309
set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff]
@@ -289,10 +317,10 @@ eq_of_veq $ ndinsert_of_mem h
289317
insert_eq_of_mem $ mem_singleton_self _
290318

291319
theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) :=
292-
ext $ λ x, by simp only [finset.mem_insert, or.left_comm]
320+
ext $ λ x, by simp only [mem_insert, or.left_comm]
293321

294322
@[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s :=
295-
ext $ λ x, by simp only [finset.mem_insert, or.assoc.symm, or_self]
323+
ext $ λ x, by simp only [mem_insert, or.assoc.symm, or_self]
296324

297325
@[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ :=
298326
ne_empty_of_mem (mem_insert_self a s)
@@ -342,8 +370,8 @@ def subtype_insert_equiv_option {t : finset α} {x : α} (h : x ∉ t) :
342370
{i // i ∈ insert x t} ≃ option {i // i ∈ t} :=
343371
begin
344372
refine
345-
{ to_fun := λ y, if h : ↑y = x then none else some ⟨y, (finset.mem_insert.mp y.2).resolve_left h⟩,
346-
inv_fun := λ y, y.elim ⟨x, finset.mem_insert_self _ _⟩ $ λ z, ⟨z, finset.mem_insert_of_mem z.2⟩,
373+
{ to_fun := λ y, if h : ↑y = x then none else some ⟨y, (mem_insert.mp y.2).resolve_left h⟩,
374+
inv_fun := λ y, y.elim ⟨x, mem_insert_self _ _⟩ $ λ z, ⟨z, mem_insert_of_mem z.2⟩,
347375
.. },
348376
{ intro y, by_cases h : ↑y = x,
349377
simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk],
@@ -366,6 +394,9 @@ ndunion_eq_union s₁.2
366394

367395
@[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion
368396

397+
@[simp] theorem disj_union_eq_union {α} [decidable_eq α] (s t h) : @disj_union α s t h = s ∪ t :=
398+
ext $ λ a, by simp
399+
369400
theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ :=
370401
mem_union.2 $ or.inl h
371402

src/data/finset/pi.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ lemma pi_cons_injective {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) :
5858
assume e₁ e₂ eq,
5959
@multiset.pi_cons_injective α _ δ a b s.1 hs _ _ $
6060
funext $ assume e, funext $ assume h,
61-
have pi.cons s a b e₁ e (by simpa only [mem_cons, mem_insert] using h) =
62-
pi.cons s a b e₂ e (by simpa only [mem_cons, mem_insert] using h),
61+
have pi.cons s a b e₁ e (by simpa only [multiset.mem_cons, mem_insert] using h) =
62+
pi.cons s a b e₂ e (by simpa only [multiset.mem_cons, mem_insert] using h),
6363
{ rw [eq] },
6464
this
6565

src/data/multiset/nodup.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ quot.induction_on s $ λ l, nodup_iff_count_le_one
5656
(d : nodup s) (h : a ∈ s) : count a s = 1 :=
5757
le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h)
5858

59+
lemma nodup_iff_pairwise {α} {s : multiset α} : nodup s ↔ pairwise (≠) s :=
60+
quotient.induction_on s $ λ l, (pairwise_coe_iff_pairwise (by exact λ a b, ne.symm)).symm
61+
5962
lemma pairwise_of_nodup {r : α → α → Prop} {s : multiset α} :
6063
(∀a∈s, ∀b∈s, a ≠ b → r a b) → nodup s → pairwise r s :=
6164
quotient.induction_on s $ assume l h hl, ⟨l, rfl, hl.imp_of_mem $ assume a b ha hb, h a ha b hb⟩

src/data/sigma/basic.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ end sigma
8484
section psigma
8585
variables {α : Sort*} {β : α → Sort*}
8686

87+
/-- Nondependent eliminator for `psigma`. -/
88+
def psigma.elim {γ} (f : ∀ a, β a → γ) (a : psigma β) : γ :=
89+
psigma.cases_on a f
90+
91+
@[simp] theorem psigma.elim_val {γ} (f : ∀ a, β a → γ) (a b) : psigma.elim f ⟨a, b⟩ = f a b := rfl
92+
8793
instance [inhabited α] [inhabited (β (default α))] : inhabited (psigma β) :=
8894
⟨⟨default α, default (β (default α))⟩⟩
8995

src/tactic/default.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import tactic.interval_cases
2929
import tactic.reassoc_axiom -- most likely useful only for category_theory
3030
import tactic.slice
3131
import tactic.subtype_instance
32+
import tactic.derive_fintype
3233
import tactic.group
3334
import tactic.cancel_denoms
3435
import tactic.zify

0 commit comments

Comments
 (0)