-
Notifications
You must be signed in to change notification settings - Fork 259
/
Defs.lean
1386 lines (1115 loc) · 54.4 KB
/
Defs.lean
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Scott Morrison
-/
import Mathlib.Algebra.Group.Indicator
import Mathlib.Algebra.Group.Submonoid.Basic
import Mathlib.Data.Set.Finite
#align_import data.finsupp.defs from "leanprover-community/mathlib"@"842328d9df7e96fd90fc424e115679c15fb23a71"
/-!
# Type of functions with finite support
For any type `α` and any type `M` with zero, we define the type `Finsupp α M` (notation: `α →₀ M`)
of finitely supported functions from `α` to `M`, i.e. the functions which are zero everywhere
on `α` except on a finite set.
Functions with finite support are used (at least) in the following parts of the library:
* `MonoidAlgebra R M` and `AddMonoidAlgebra R M` are defined as `M →₀ R`;
* polynomials and multivariate polynomials are defined as `AddMonoidAlgebra`s, hence they use
`Finsupp` under the hood;
* the linear combination of a family of vectors `v i` with coefficients `f i` (as used, e.g., to
define linearly independent family `LinearIndependent`) is defined as a map
`Finsupp.total : (ι → M) → (ι →₀ R) →ₗ[R] M`.
Some other constructions are naturally equivalent to `α →₀ M` with some `α` and `M` but are defined
in a different way in the library:
* `Multiset α ≃+ α →₀ ℕ`;
* `FreeAbelianGroup α ≃+ α →₀ ℤ`.
Most of the theory assumes that the range is a commutative additive monoid. This gives us the big
sum operator as a powerful way to construct `Finsupp` elements, which is defined in
`Algebra/BigOperators/Finsupp`.
-- Porting note: the semireducibility remark no longer applies in Lean 4, afaict.
Many constructions based on `α →₀ M` use `semireducible` type tags to avoid reusing unwanted type
instances. E.g., `MonoidAlgebra`, `AddMonoidAlgebra`, and types based on these two have
non-pointwise multiplication.
## Main declarations
* `Finsupp`: The type of finitely supported functions from `α` to `β`.
* `Finsupp.single`: The `Finsupp` which is nonzero in exactly one point.
* `Finsupp.update`: Changes one value of a `Finsupp`.
* `Finsupp.erase`: Replaces one value of a `Finsupp` by `0`.
* `Finsupp.onFinset`: The restriction of a function to a `Finset` as a `Finsupp`.
* `Finsupp.mapRange`: Composition of a `ZeroHom` with a `Finsupp`.
* `Finsupp.embDomain`: Maps the domain of a `Finsupp` by an embedding.
* `Finsupp.zipWith`: Postcomposition of two `Finsupp`s with a function `f` such that `f 0 0 = 0`.
## Notations
This file adds `α →₀ M` as a global notation for `Finsupp α M`.
We also use the following convention for `Type*` variables in this file
* `α`, `β`, `γ`: types with no additional structure that appear as the first argument to `Finsupp`
somewhere in the statement;
* `ι` : an auxiliary index type;
* `M`, `M'`, `N`, `P`: types with `Zero` or `(Add)(Comm)Monoid` structure; `M` is also used
for a (semi)module over a (semi)ring.
* `G`, `H`: groups (commutative or not, multiplicative or additive);
* `R`, `S`: (semi)rings.
## Implementation notes
This file is a `noncomputable theory` and uses classical logic throughout.
## TODO
* Expand the list of definitions and important lemmas to the module docstring.
-/
noncomputable section
open Finset Function
variable {α β γ ι M M' N P G H R S : Type*}
/-- `Finsupp α M`, denoted `α →₀ M`, is the type of functions `f : α → M` such that
`f x = 0` for all but finitely many `x`. -/
structure Finsupp (α : Type*) (M : Type*) [Zero M] where
/-- The support of a finitely supported function (aka `Finsupp`). -/
support : Finset α
/-- The underlying function of a bundled finitely supported function (aka `Finsupp`). -/
toFun : α → M
/-- The witness that the support of a `Finsupp` is indeed the exact locus where its
underlying function is nonzero. -/
mem_support_toFun : ∀ a, a ∈ support ↔ toFun a ≠ 0
#align finsupp Finsupp
#align finsupp.support Finsupp.support
#align finsupp.to_fun Finsupp.toFun
#align finsupp.mem_support_to_fun Finsupp.mem_support_toFun
@[inherit_doc]
infixr:25 " →₀ " => Finsupp
namespace Finsupp
/-! ### Basic declarations about `Finsupp` -/
section Basic
variable [Zero M]
instance instFunLike : FunLike (α →₀ M) α M :=
⟨toFun, by
rintro ⟨s, f, hf⟩ ⟨t, g, hg⟩ (rfl : f = g)
congr
ext a
exact (hf _).trans (hg _).symm⟩
#align finsupp.fun_like Finsupp.instFunLike
/-- Helper instance for when there are too many metavariables to apply the `DFunLike` instance
directly. -/
instance instCoeFun : CoeFun (α →₀ M) fun _ => α → M :=
inferInstance
#align finsupp.has_coe_to_fun Finsupp.instCoeFun
@[ext]
theorem ext {f g : α →₀ M} (h : ∀ a, f a = g a) : f = g :=
DFunLike.ext _ _ h
#align finsupp.ext Finsupp.ext
#align finsupp.ext_iff DFunLike.ext_iff
lemma ne_iff {f g : α →₀ M} : f ≠ g ↔ ∃ a, f a ≠ g a := DFunLike.ne_iff
#align finsupp.coe_fn_inj DFunLike.coe_fn_eq
#align finsupp.coe_fn_injective DFunLike.coe_injective
#align finsupp.congr_fun DFunLike.congr_fun
@[simp, norm_cast]
theorem coe_mk (f : α → M) (s : Finset α) (h : ∀ a, a ∈ s ↔ f a ≠ 0) : ⇑(⟨s, f, h⟩ : α →₀ M) = f :=
rfl
#align finsupp.coe_mk Finsupp.coe_mk
instance instZero : Zero (α →₀ M) :=
⟨⟨∅, 0, fun _ => ⟨fun h ↦ (not_mem_empty _ h).elim, fun H => (H rfl).elim⟩⟩⟩
#align finsupp.has_zero Finsupp.instZero
@[simp, norm_cast] lemma coe_zero : ⇑(0 : α →₀ M) = 0 := rfl
#align finsupp.coe_zero Finsupp.coe_zero
theorem zero_apply {a : α} : (0 : α →₀ M) a = 0 :=
rfl
#align finsupp.zero_apply Finsupp.zero_apply
@[simp]
theorem support_zero : (0 : α →₀ M).support = ∅ :=
rfl
#align finsupp.support_zero Finsupp.support_zero
instance instInhabited : Inhabited (α →₀ M) :=
⟨0⟩
#align finsupp.inhabited Finsupp.instInhabited
@[simp]
theorem mem_support_iff {f : α →₀ M} : ∀ {a : α}, a ∈ f.support ↔ f a ≠ 0 :=
@(f.mem_support_toFun)
#align finsupp.mem_support_iff Finsupp.mem_support_iff
@[simp, norm_cast]
theorem fun_support_eq (f : α →₀ M) : Function.support f = f.support :=
Set.ext fun _x => mem_support_iff.symm
#align finsupp.fun_support_eq Finsupp.fun_support_eq
theorem not_mem_support_iff {f : α →₀ M} {a} : a ∉ f.support ↔ f a = 0 :=
not_iff_comm.1 mem_support_iff.symm
#align finsupp.not_mem_support_iff Finsupp.not_mem_support_iff
@[simp, norm_cast]
theorem coe_eq_zero {f : α →₀ M} : (f : α → M) = 0 ↔ f = 0 := by rw [← coe_zero, DFunLike.coe_fn_eq]
#align finsupp.coe_eq_zero Finsupp.coe_eq_zero
theorem ext_iff' {f g : α →₀ M} : f = g ↔ f.support = g.support ∧ ∀ x ∈ f.support, f x = g x :=
⟨fun h => h ▸ ⟨rfl, fun _ _ => rfl⟩, fun ⟨h₁, h₂⟩ =>
ext fun a => by
classical
exact if h : a ∈ f.support then h₂ a h else by
have hf : f a = 0 := not_mem_support_iff.1 h
have hg : g a = 0 := by rwa [h₁, not_mem_support_iff] at h
rw [hf, hg]⟩
#align finsupp.ext_iff' Finsupp.ext_iff'
@[simp]
theorem support_eq_empty {f : α →₀ M} : f.support = ∅ ↔ f = 0 :=
mod_cast @Function.support_eq_empty_iff _ _ _ f
#align finsupp.support_eq_empty Finsupp.support_eq_empty
theorem support_nonempty_iff {f : α →₀ M} : f.support.Nonempty ↔ f ≠ 0 := by
simp only [Finsupp.support_eq_empty, Finset.nonempty_iff_ne_empty, Ne]
#align finsupp.support_nonempty_iff Finsupp.support_nonempty_iff
#align finsupp.nonzero_iff_exists Finsupp.ne_iff
theorem card_support_eq_zero {f : α →₀ M} : card f.support = 0 ↔ f = 0 := by simp
#align finsupp.card_support_eq_zero Finsupp.card_support_eq_zero
instance instDecidableEq [DecidableEq α] [DecidableEq M] : DecidableEq (α →₀ M) := fun f g =>
decidable_of_iff (f.support = g.support ∧ ∀ a ∈ f.support, f a = g a) ext_iff'.symm
#align finsupp.decidable_eq Finsupp.instDecidableEq
theorem finite_support (f : α →₀ M) : Set.Finite (Function.support f) :=
f.fun_support_eq.symm ▸ f.support.finite_toSet
#align finsupp.finite_support Finsupp.finite_support
theorem support_subset_iff {s : Set α} {f : α →₀ M} :
↑f.support ⊆ s ↔ ∀ a ∉ s, f a = 0 := by
simp only [Set.subset_def, mem_coe, mem_support_iff]; exact forall_congr' fun a => not_imp_comm
#align finsupp.support_subset_iff Finsupp.support_subset_iff
/-- Given `Finite α`, `equivFunOnFinite` is the `Equiv` between `α →₀ β` and `α → β`.
(All functions on a finite type are finitely supported.) -/
@[simps]
def equivFunOnFinite [Finite α] : (α →₀ M) ≃ (α → M) where
toFun := (⇑)
invFun f := mk (Function.support f).toFinite.toFinset f fun _a => Set.Finite.mem_toFinset _
left_inv _f := ext fun _x => rfl
right_inv _f := rfl
#align finsupp.equiv_fun_on_finite Finsupp.equivFunOnFinite
@[simp]
theorem equivFunOnFinite_symm_coe {α} [Finite α] (f : α →₀ M) : equivFunOnFinite.symm f = f :=
equivFunOnFinite.symm_apply_apply f
#align finsupp.equiv_fun_on_finite_symm_coe Finsupp.equivFunOnFinite_symm_coe
/--
If `α` has a unique term, the type of finitely supported functions `α →₀ β` is equivalent to `β`.
-/
@[simps!]
noncomputable def _root_.Equiv.finsuppUnique {ι : Type*} [Unique ι] : (ι →₀ M) ≃ M :=
Finsupp.equivFunOnFinite.trans (Equiv.funUnique ι M)
#align equiv.finsupp_unique Equiv.finsuppUnique
#align equiv.finsupp_unique_symm_apply_support_val Equiv.finsuppUnique_symm_apply_support_val
#align equiv.finsupp_unique_symm_apply_to_fun Equiv.finsuppUnique_symm_apply_toFun
#align equiv.finsupp_unique_apply Equiv.finsuppUnique_apply
@[ext]
theorem unique_ext [Unique α] {f g : α →₀ M} (h : f default = g default) : f = g :=
ext fun a => by rwa [Unique.eq_default a]
#align finsupp.unique_ext Finsupp.unique_ext
theorem unique_ext_iff [Unique α] {f g : α →₀ M} : f = g ↔ f default = g default :=
⟨fun h => h ▸ rfl, unique_ext⟩
#align finsupp.unique_ext_iff Finsupp.unique_ext_iff
end Basic
/-! ### Declarations about `single` -/
section Single
variable [Zero M] {a a' : α} {b : M}
/-- `single a b` is the finitely supported function with value `b` at `a` and zero otherwise. -/
def single (a : α) (b : M) : α →₀ M where
support :=
haveI := Classical.decEq M
if b = 0 then ∅ else {a}
toFun :=
haveI := Classical.decEq α
Pi.single a b
mem_support_toFun a' := by
classical
obtain rfl | hb := eq_or_ne b 0
· simp [Pi.single, update]
rw [if_neg hb, mem_singleton]
obtain rfl | ha := eq_or_ne a' a
· simp [hb, Pi.single, update]
simp [Pi.single_eq_of_ne' ha.symm, ha]
#align finsupp.single Finsupp.single
theorem single_apply [Decidable (a = a')] : single a b a' = if a = a' then b else 0 := by
classical
simp_rw [@eq_comm _ a a']
convert Pi.single_apply a b a'
#align finsupp.single_apply Finsupp.single_apply
theorem single_apply_left {f : α → β} (hf : Function.Injective f) (x z : α) (y : M) :
single (f x) y (f z) = single x y z := by classical simp only [single_apply, hf.eq_iff]
#align finsupp.single_apply_left Finsupp.single_apply_left
theorem single_eq_set_indicator : ⇑(single a b) = Set.indicator {a} fun _ => b := by
classical
ext
simp [single_apply, Set.indicator, @eq_comm _ a]
#align finsupp.single_eq_set_indicator Finsupp.single_eq_set_indicator
@[simp]
theorem single_eq_same : (single a b : α →₀ M) a = b := by
classical exact Pi.single_eq_same (f := fun _ ↦ M) a b
#align finsupp.single_eq_same Finsupp.single_eq_same
@[simp]
theorem single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ M) a' = 0 := by
classical exact Pi.single_eq_of_ne' h _
#align finsupp.single_eq_of_ne Finsupp.single_eq_of_ne
theorem single_eq_update [DecidableEq α] (a : α) (b : M) :
⇑(single a b) = Function.update (0 : _) a b := by
classical rw [single_eq_set_indicator, ← Set.piecewise_eq_indicator, Set.piecewise_singleton]
#align finsupp.single_eq_update Finsupp.single_eq_update
theorem single_eq_pi_single [DecidableEq α] (a : α) (b : M) : ⇑(single a b) = Pi.single a b :=
single_eq_update a b
#align finsupp.single_eq_pi_single Finsupp.single_eq_pi_single
@[simp]
theorem single_zero (a : α) : (single a 0 : α →₀ M) = 0 :=
DFunLike.coe_injective <| by
classical simpa only [single_eq_update, coe_zero] using Function.update_eq_self a (0 : α → M)
#align finsupp.single_zero Finsupp.single_zero
theorem single_of_single_apply (a a' : α) (b : M) :
single a ((single a' b) a) = single a' (single a' b) a := by
classical
rw [single_apply, single_apply]
ext
split_ifs with h
· rw [h]
· rw [zero_apply, single_apply, ite_self]
#align finsupp.single_of_single_apply Finsupp.single_of_single_apply
theorem support_single_ne_zero (a : α) (hb : b ≠ 0) : (single a b).support = {a} :=
if_neg hb
#align finsupp.support_single_ne_zero Finsupp.support_single_ne_zero
theorem support_single_subset : (single a b).support ⊆ {a} := by
classical show ite _ _ _ ⊆ _; split_ifs <;> [exact empty_subset _; exact Subset.refl _]
#align finsupp.support_single_subset Finsupp.support_single_subset
theorem single_apply_mem (x) : single a b x ∈ ({0, b} : Set M) := by
rcases em (a = x) with (rfl | hx) <;> [simp; simp [single_eq_of_ne hx]]
#align finsupp.single_apply_mem Finsupp.single_apply_mem
theorem range_single_subset : Set.range (single a b) ⊆ {0, b} :=
Set.range_subset_iff.2 single_apply_mem
#align finsupp.range_single_subset Finsupp.range_single_subset
/-- `Finsupp.single a b` is injective in `b`. For the statement that it is injective in `a`, see
`Finsupp.single_left_injective` -/
theorem single_injective (a : α) : Function.Injective (single a : M → α →₀ M) := fun b₁ b₂ eq => by
have : (single a b₁ : α →₀ M) a = (single a b₂ : α →₀ M) a := by rw [eq]
rwa [single_eq_same, single_eq_same] at this
#align finsupp.single_injective Finsupp.single_injective
theorem single_apply_eq_zero {a x : α} {b : M} : single a b x = 0 ↔ x = a → b = 0 := by
simp [single_eq_set_indicator]
#align finsupp.single_apply_eq_zero Finsupp.single_apply_eq_zero
theorem single_apply_ne_zero {a x : α} {b : M} : single a b x ≠ 0 ↔ x = a ∧ b ≠ 0 := by
simp [single_apply_eq_zero]
#align finsupp.single_apply_ne_zero Finsupp.single_apply_ne_zero
theorem mem_support_single (a a' : α) (b : M) : a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 := by
simp [single_apply_eq_zero, not_or]
#align finsupp.mem_support_single Finsupp.mem_support_single
theorem eq_single_iff {f : α →₀ M} {a b} : f = single a b ↔ f.support ⊆ {a} ∧ f a = b := by
refine ⟨fun h => h.symm ▸ ⟨support_single_subset, single_eq_same⟩, ?_⟩
rintro ⟨h, rfl⟩
ext x
by_cases hx : a = x <;> simp only [hx, single_eq_same, single_eq_of_ne, Ne, not_false_iff]
exact not_mem_support_iff.1 (mt (fun hx => (mem_singleton.1 (h hx)).symm) hx)
#align finsupp.eq_single_iff Finsupp.eq_single_iff
theorem single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : M) :
single a₁ b₁ = single a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ ∨ b₁ = 0 ∧ b₂ = 0 := by
constructor
· intro eq
by_cases h : a₁ = a₂
· refine Or.inl ⟨h, ?_⟩
rwa [h, (single_injective a₂).eq_iff] at eq
· rw [DFunLike.ext_iff] at eq
have h₁ := eq a₁
have h₂ := eq a₂
simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (Ne.symm h)] at h₁ h₂
exact Or.inr ⟨h₁, h₂.symm⟩
· rintro (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩)
· rfl
· rw [single_zero, single_zero]
#align finsupp.single_eq_single_iff Finsupp.single_eq_single_iff
/-- `Finsupp.single a b` is injective in `a`. For the statement that it is injective in `b`, see
`Finsupp.single_injective` -/
theorem single_left_injective (h : b ≠ 0) : Function.Injective fun a : α => single a b :=
fun _a _a' H => (((single_eq_single_iff _ _ _ _).mp H).resolve_right fun hb => h hb.1).left
#align finsupp.single_left_injective Finsupp.single_left_injective
theorem single_left_inj (h : b ≠ 0) : single a b = single a' b ↔ a = a' :=
(single_left_injective h).eq_iff
#align finsupp.single_left_inj Finsupp.single_left_inj
theorem support_single_ne_bot (i : α) (h : b ≠ 0) : (single i b).support ≠ ⊥ := by
simpa only [support_single_ne_zero _ h] using singleton_ne_empty _
#align finsupp.support_single_ne_bot Finsupp.support_single_ne_bot
theorem support_single_disjoint {b' : M} (hb : b ≠ 0) (hb' : b' ≠ 0) {i j : α} :
Disjoint (single i b).support (single j b').support ↔ i ≠ j := by
rw [support_single_ne_zero _ hb, support_single_ne_zero _ hb', disjoint_singleton]
#align finsupp.support_single_disjoint Finsupp.support_single_disjoint
@[simp]
theorem single_eq_zero : single a b = 0 ↔ b = 0 := by
simp [DFunLike.ext_iff, single_eq_set_indicator]
#align finsupp.single_eq_zero Finsupp.single_eq_zero
theorem single_swap (a₁ a₂ : α) (b : M) : single a₁ b a₂ = single a₂ b a₁ := by
classical simp only [single_apply, eq_comm]
#align finsupp.single_swap Finsupp.single_swap
instance instNontrivial [Nonempty α] [Nontrivial M] : Nontrivial (α →₀ M) := by
inhabit α
rcases exists_ne (0 : M) with ⟨x, hx⟩
exact nontrivial_of_ne (single default x) 0 (mt single_eq_zero.1 hx)
#align finsupp.nontrivial Finsupp.instNontrivial
theorem unique_single [Unique α] (x : α →₀ M) : x = single default (x default) :=
ext <| Unique.forall_iff.2 single_eq_same.symm
#align finsupp.unique_single Finsupp.unique_single
@[simp]
theorem unique_single_eq_iff [Unique α] {b' : M} : single a b = single a' b' ↔ b = b' := by
rw [unique_ext_iff, Unique.eq_default a, Unique.eq_default a', single_eq_same, single_eq_same]
#align finsupp.unique_single_eq_iff Finsupp.unique_single_eq_iff
lemma apply_single [AddCommMonoid N] [AddCommMonoid P]
{F : Type*} [FunLike F N P] [AddMonoidHomClass F N P] (e : F)
(a : α) (n : N) (b : α) :
e ((single a n) b) = single a (e n) b := by
classical
simp only [single_apply]
split_ifs
· rfl
· exact map_zero e
theorem support_eq_singleton {f : α →₀ M} {a : α} :
f.support = {a} ↔ f a ≠ 0 ∧ f = single a (f a) :=
⟨fun h =>
⟨mem_support_iff.1 <| h.symm ▸ Finset.mem_singleton_self a,
eq_single_iff.2 ⟨subset_of_eq h, rfl⟩⟩,
fun h => h.2.symm ▸ support_single_ne_zero _ h.1⟩
#align finsupp.support_eq_singleton Finsupp.support_eq_singleton
theorem support_eq_singleton' {f : α →₀ M} {a : α} :
f.support = {a} ↔ ∃ b ≠ 0, f = single a b :=
⟨fun h =>
let h := support_eq_singleton.1 h
⟨_, h.1, h.2⟩,
fun ⟨_b, hb, hf⟩ => hf.symm ▸ support_single_ne_zero _ hb⟩
#align finsupp.support_eq_singleton' Finsupp.support_eq_singleton'
theorem card_support_eq_one {f : α →₀ M} :
card f.support = 1 ↔ ∃ a, f a ≠ 0 ∧ f = single a (f a) := by
simp only [card_eq_one, support_eq_singleton]
#align finsupp.card_support_eq_one Finsupp.card_support_eq_one
theorem card_support_eq_one' {f : α →₀ M} :
card f.support = 1 ↔ ∃ a, ∃ b ≠ 0, f = single a b := by
simp only [card_eq_one, support_eq_singleton']
#align finsupp.card_support_eq_one' Finsupp.card_support_eq_one'
theorem support_subset_singleton {f : α →₀ M} {a : α} : f.support ⊆ {a} ↔ f = single a (f a) :=
⟨fun h => eq_single_iff.mpr ⟨h, rfl⟩, fun h => (eq_single_iff.mp h).left⟩
#align finsupp.support_subset_singleton Finsupp.support_subset_singleton
theorem support_subset_singleton' {f : α →₀ M} {a : α} : f.support ⊆ {a} ↔ ∃ b, f = single a b :=
⟨fun h => ⟨f a, support_subset_singleton.mp h⟩, fun ⟨b, hb⟩ => by
rw [hb, support_subset_singleton, single_eq_same]⟩
#align finsupp.support_subset_singleton' Finsupp.support_subset_singleton'
theorem card_support_le_one [Nonempty α] {f : α →₀ M} :
card f.support ≤ 1 ↔ ∃ a, f = single a (f a) := by
simp only [card_le_one_iff_subset_singleton, support_subset_singleton]
#align finsupp.card_support_le_one Finsupp.card_support_le_one
theorem card_support_le_one' [Nonempty α] {f : α →₀ M} :
card f.support ≤ 1 ↔ ∃ a b, f = single a b := by
simp only [card_le_one_iff_subset_singleton, support_subset_singleton']
#align finsupp.card_support_le_one' Finsupp.card_support_le_one'
@[simp]
theorem equivFunOnFinite_single [DecidableEq α] [Finite α] (x : α) (m : M) :
Finsupp.equivFunOnFinite (Finsupp.single x m) = Pi.single x m := by
ext
simp [Finsupp.single_eq_pi_single, equivFunOnFinite]
#align finsupp.equiv_fun_on_finite_single Finsupp.equivFunOnFinite_single
@[simp]
theorem equivFunOnFinite_symm_single [DecidableEq α] [Finite α] (x : α) (m : M) :
Finsupp.equivFunOnFinite.symm (Pi.single x m) = Finsupp.single x m := by
rw [← equivFunOnFinite_single, Equiv.symm_apply_apply]
#align finsupp.equiv_fun_on_finite_symm_single Finsupp.equivFunOnFinite_symm_single
end Single
/-! ### Declarations about `update` -/
section Update
variable [Zero M] (f : α →₀ M) (a : α) (b : M) (i : α)
/-- Replace the value of a `α →₀ M` at a given point `a : α` by a given value `b : M`.
If `b = 0`, this amounts to removing `a` from the `Finsupp.support`.
Otherwise, if `a` was not in the `Finsupp.support`, it is added to it.
This is the finitely-supported version of `Function.update`. -/
def update (f : α →₀ M) (a : α) (b : M) : α →₀ M where
support := by
haveI := Classical.decEq α; haveI := Classical.decEq M
exact if b = 0 then f.support.erase a else insert a f.support
toFun :=
haveI := Classical.decEq α
Function.update f a b
mem_support_toFun i := by
classical
rw [Function.update]
simp only [eq_rec_constant, dite_eq_ite, ne_eq]
split_ifs with hb ha ha <;>
try simp only [*, not_false_iff, iff_true, not_true, iff_false]
· rw [Finset.mem_erase]
simp
· rw [Finset.mem_erase]
simp [ha]
· rw [Finset.mem_insert]
simp [ha]
· rw [Finset.mem_insert]
simp [ha]
#align finsupp.update Finsupp.update
@[simp, norm_cast]
theorem coe_update [DecidableEq α] : (f.update a b : α → M) = Function.update f a b := by
delta update Function.update
ext
dsimp
split_ifs <;> simp
#align finsupp.coe_update Finsupp.coe_update
@[simp]
theorem update_self : f.update a (f a) = f := by
classical
ext
simp
#align finsupp.update_self Finsupp.update_self
@[simp]
theorem zero_update : update 0 a b = single a b := by
classical
ext
rw [single_eq_update]
rfl
#align finsupp.zero_update Finsupp.zero_update
theorem support_update [DecidableEq α] [DecidableEq M] :
support (f.update a b) = if b = 0 then f.support.erase a else insert a f.support := by
classical
dsimp only [update]
congr!
#align finsupp.support_update Finsupp.support_update
@[simp]
theorem support_update_zero [DecidableEq α] : support (f.update a 0) = f.support.erase a := by
classical
simp only [update, ite_true, mem_support_iff, ne_eq, not_not]
congr!
#align finsupp.support_update_zero Finsupp.support_update_zero
variable {b}
theorem support_update_ne_zero [DecidableEq α] (h : b ≠ 0) :
support (f.update a b) = insert a f.support := by
classical
simp only [update, h, ite_false, mem_support_iff, ne_eq]
congr!
#align finsupp.support_update_ne_zero Finsupp.support_update_ne_zero
theorem support_update_subset [DecidableEq α] [DecidableEq M] :
support (f.update a b) ⊆ insert a f.support := by
rw [support_update]
split_ifs
· exact (erase_subset _ _).trans (subset_insert _ _)
· rfl
theorem update_comm (f : α →₀ M) {a₁ a₂ : α} (h : a₁ ≠ a₂) (m₁ m₂ : M) :
update (update f a₁ m₁) a₂ m₂ = update (update f a₂ m₂) a₁ m₁ :=
letI := Classical.decEq α
DFunLike.coe_injective <| Function.update_comm h _ _ _
@[simp] theorem update_idem (f : α →₀ M) (a : α) (b c : M) :
update (update f a b) a c = update f a c :=
letI := Classical.decEq α
DFunLike.coe_injective <| Function.update_idem _ _ _
end Update
/-! ### Declarations about `erase` -/
section Erase
variable [Zero M]
/--
`erase a f` is the finitely supported function equal to `f` except at `a` where it is equal to `0`.
If `a` is not in the support of `f` then `erase a f = f`.
-/
def erase (a : α) (f : α →₀ M) : α →₀ M where
support :=
haveI := Classical.decEq α
f.support.erase a
toFun a' :=
haveI := Classical.decEq α
if a' = a then 0 else f a'
mem_support_toFun a' := by
classical
rw [mem_erase, mem_support_iff]; dsimp
split_ifs with h
· exact ⟨fun H _ => H.1 h, fun H => (H rfl).elim⟩
· exact and_iff_right h
#align finsupp.erase Finsupp.erase
@[simp]
theorem support_erase [DecidableEq α] {a : α} {f : α →₀ M} :
(f.erase a).support = f.support.erase a := by
classical
dsimp only [erase]
congr!
#align finsupp.support_erase Finsupp.support_erase
@[simp]
theorem erase_same {a : α} {f : α →₀ M} : (f.erase a) a = 0 := by
classical simp only [erase, coe_mk, ite_true]
#align finsupp.erase_same Finsupp.erase_same
@[simp]
theorem erase_ne {a a' : α} {f : α →₀ M} (h : a' ≠ a) : (f.erase a) a' = f a' := by
classical simp only [erase, coe_mk, h, ite_false]
#align finsupp.erase_ne Finsupp.erase_ne
theorem erase_apply [DecidableEq α] {a a' : α} {f : α →₀ M} :
f.erase a a' = if a' = a then 0 else f a' := by
rw [erase, coe_mk]
convert rfl
@[simp]
theorem erase_single {a : α} {b : M} : erase a (single a b) = 0 := by
ext s; by_cases hs : s = a
· rw [hs, erase_same]
rfl
· rw [erase_ne hs]
exact single_eq_of_ne (Ne.symm hs)
#align finsupp.erase_single Finsupp.erase_single
theorem erase_single_ne {a a' : α} {b : M} (h : a ≠ a') : erase a (single a' b) = single a' b := by
ext s; by_cases hs : s = a
· rw [hs, erase_same, single_eq_of_ne h.symm]
· rw [erase_ne hs]
#align finsupp.erase_single_ne Finsupp.erase_single_ne
@[simp]
theorem erase_of_not_mem_support {f : α →₀ M} {a} (haf : a ∉ f.support) : erase a f = f := by
ext b; by_cases hab : b = a
· rwa [hab, erase_same, eq_comm, ← not_mem_support_iff]
· rw [erase_ne hab]
#align finsupp.erase_of_not_mem_support Finsupp.erase_of_not_mem_support
@[simp, nolint simpNF] -- Porting note: simpNF linter claims simp can prove this, it can not
theorem erase_zero (a : α) : erase a (0 : α →₀ M) = 0 := by
classical rw [← support_eq_empty, support_erase, support_zero, erase_empty]
#align finsupp.erase_zero Finsupp.erase_zero
theorem erase_eq_update_zero (f : α →₀ M) (a : α) : f.erase a = update f a 0 :=
letI := Classical.decEq α
ext fun _ => (Function.update_apply _ _ _ _).symm
-- The name matches `Finset.erase_insert_of_ne`
theorem erase_update_of_ne (f : α →₀ M) {a a' : α} (ha : a ≠ a') (b : M) :
erase a (update f a' b) = update (erase a f) a' b := by
rw [erase_eq_update_zero, erase_eq_update_zero, update_comm _ ha]
-- not `simp` as `erase_of_not_mem_support` can prove this
theorem erase_idem (f : α →₀ M) (a : α) :
erase a (erase a f) = erase a f := by
rw [erase_eq_update_zero, erase_eq_update_zero, update_idem]
@[simp] theorem update_erase_eq_update (f : α →₀ M) (a : α) (b : M) :
update (erase a f) a b = update f a b := by
rw [erase_eq_update_zero, update_idem]
@[simp] theorem erase_update_eq_erase (f : α →₀ M) (a : α) (b : M) :
erase a (update f a b) = erase a f := by
rw [erase_eq_update_zero, erase_eq_update_zero, update_idem]
end Erase
/-! ### Declarations about `onFinset` -/
section OnFinset
variable [Zero M]
/-- `Finsupp.onFinset s f hf` is the finsupp function representing `f` restricted to the finset `s`.
The function must be `0` outside of `s`. Use this when the set needs to be filtered anyways,
otherwise a better set representation is often available. -/
def onFinset (s : Finset α) (f : α → M) (hf : ∀ a, f a ≠ 0 → a ∈ s) : α →₀ M where
support :=
haveI := Classical.decEq M
s.filter (f · ≠ 0)
toFun := f
mem_support_toFun := by classical simpa
#align finsupp.on_finset Finsupp.onFinset
@[simp]
theorem onFinset_apply {s : Finset α} {f : α → M} {hf a} : (onFinset s f hf : α →₀ M) a = f a :=
rfl
#align finsupp.on_finset_apply Finsupp.onFinset_apply
@[simp]
theorem support_onFinset_subset {s : Finset α} {f : α → M} {hf} :
(onFinset s f hf).support ⊆ s := by
classical convert filter_subset (f · ≠ 0) s
#align finsupp.support_on_finset_subset Finsupp.support_onFinset_subset
-- @[simp] -- Porting note (#10618): simp can prove this
theorem mem_support_onFinset {s : Finset α} {f : α → M} (hf : ∀ a : α, f a ≠ 0 → a ∈ s) {a : α} :
a ∈ (Finsupp.onFinset s f hf).support ↔ f a ≠ 0 := by
rw [Finsupp.mem_support_iff, Finsupp.onFinset_apply]
#align finsupp.mem_support_on_finset Finsupp.mem_support_onFinset
theorem support_onFinset [DecidableEq M] {s : Finset α} {f : α → M}
(hf : ∀ a : α, f a ≠ 0 → a ∈ s) :
(Finsupp.onFinset s f hf).support = s.filter fun a => f a ≠ 0 := by
dsimp [onFinset]; congr
#align finsupp.support_on_finset Finsupp.support_onFinset
end OnFinset
section OfSupportFinite
variable [Zero M]
/-- The natural `Finsupp` induced by the function `f` given that it has finite support. -/
noncomputable def ofSupportFinite (f : α → M) (hf : (Function.support f).Finite) : α →₀ M where
support := hf.toFinset
toFun := f
mem_support_toFun _ := hf.mem_toFinset
#align finsupp.of_support_finite Finsupp.ofSupportFinite
theorem ofSupportFinite_coe {f : α → M} {hf : (Function.support f).Finite} :
(ofSupportFinite f hf : α → M) = f :=
rfl
#align finsupp.of_support_finite_coe Finsupp.ofSupportFinite_coe
instance instCanLift : CanLift (α → M) (α →₀ M) (⇑) fun f => (Function.support f).Finite where
prf f hf := ⟨ofSupportFinite f hf, rfl⟩
#align finsupp.can_lift Finsupp.instCanLift
end OfSupportFinite
/-! ### Declarations about `mapRange` -/
section MapRange
variable [Zero M] [Zero N] [Zero P]
/-- The composition of `f : M → N` and `g : α →₀ M` is `mapRange f hf g : α →₀ N`,
which is well-defined when `f 0 = 0`.
This preserves the structure on `f`, and exists in various bundled forms for when `f` is itself
bundled (defined in `Data/Finsupp/Basic`):
* `Finsupp.mapRange.equiv`
* `Finsupp.mapRange.zeroHom`
* `Finsupp.mapRange.addMonoidHom`
* `Finsupp.mapRange.addEquiv`
* `Finsupp.mapRange.linearMap`
* `Finsupp.mapRange.linearEquiv`
-/
def mapRange (f : M → N) (hf : f 0 = 0) (g : α →₀ M) : α →₀ N :=
onFinset g.support (f ∘ g) fun a => by
rw [mem_support_iff, not_imp_not]; exact fun H => (congr_arg f H).trans hf
#align finsupp.map_range Finsupp.mapRange
@[simp]
theorem mapRange_apply {f : M → N} {hf : f 0 = 0} {g : α →₀ M} {a : α} :
mapRange f hf g a = f (g a) :=
rfl
#align finsupp.map_range_apply Finsupp.mapRange_apply
@[simp]
theorem mapRange_zero {f : M → N} {hf : f 0 = 0} : mapRange f hf (0 : α →₀ M) = 0 :=
ext fun _ => by simp only [hf, zero_apply, mapRange_apply]
#align finsupp.map_range_zero Finsupp.mapRange_zero
@[simp]
theorem mapRange_id (g : α →₀ M) : mapRange id rfl g = g :=
ext fun _ => rfl
#align finsupp.map_range_id Finsupp.mapRange_id
theorem mapRange_comp (f : N → P) (hf : f 0 = 0) (f₂ : M → N) (hf₂ : f₂ 0 = 0) (h : (f ∘ f₂) 0 = 0)
(g : α →₀ M) : mapRange (f ∘ f₂) h g = mapRange f hf (mapRange f₂ hf₂ g) :=
ext fun _ => rfl
#align finsupp.map_range_comp Finsupp.mapRange_comp
theorem support_mapRange {f : M → N} {hf : f 0 = 0} {g : α →₀ M} :
(mapRange f hf g).support ⊆ g.support :=
support_onFinset_subset
#align finsupp.support_map_range Finsupp.support_mapRange
@[simp]
theorem mapRange_single {f : M → N} {hf : f 0 = 0} {a : α} {b : M} :
mapRange f hf (single a b) = single a (f b) :=
ext fun a' => by
classical simpa only [single_eq_pi_single] using Pi.apply_single _ (fun _ => hf) a _ a'
#align finsupp.map_range_single Finsupp.mapRange_single
theorem support_mapRange_of_injective {e : M → N} (he0 : e 0 = 0) (f : ι →₀ M)
(he : Function.Injective e) : (Finsupp.mapRange e he0 f).support = f.support := by
ext
simp only [Finsupp.mem_support_iff, Ne, Finsupp.mapRange_apply]
exact he.ne_iff' he0
#align finsupp.support_map_range_of_injective Finsupp.support_mapRange_of_injective
end MapRange
/-! ### Declarations about `embDomain` -/
section EmbDomain
variable [Zero M] [Zero N]
/-- Given `f : α ↪ β` and `v : α →₀ M`, `Finsupp.embDomain f v : β →₀ M`
is the finitely supported function whose value at `f a : β` is `v a`.
For a `b : β` outside the range of `f`, it is zero. -/
def embDomain (f : α ↪ β) (v : α →₀ M) : β →₀ M where
support := v.support.map f
toFun a₂ :=
haveI := Classical.decEq β
if h : a₂ ∈ v.support.map f then
v
(v.support.choose (fun a₁ => f a₁ = a₂)
(by
rcases Finset.mem_map.1 h with ⟨a, ha, rfl⟩
exact ExistsUnique.intro a ⟨ha, rfl⟩ fun b ⟨_, hb⟩ => f.injective hb))
else 0
mem_support_toFun a₂ := by
dsimp
split_ifs with h
· simp only [h, true_iff_iff, Ne]
rw [← not_mem_support_iff, not_not]
classical apply Finset.choose_mem
· simp only [h, Ne, ne_self_iff_false, not_true_eq_false]
#align finsupp.emb_domain Finsupp.embDomain
@[simp]
theorem support_embDomain (f : α ↪ β) (v : α →₀ M) : (embDomain f v).support = v.support.map f :=
rfl
#align finsupp.support_emb_domain Finsupp.support_embDomain
@[simp]
theorem embDomain_zero (f : α ↪ β) : (embDomain f 0 : β →₀ M) = 0 :=
rfl
#align finsupp.emb_domain_zero Finsupp.embDomain_zero
@[simp]
theorem embDomain_apply (f : α ↪ β) (v : α →₀ M) (a : α) : embDomain f v (f a) = v a := by
classical
change dite _ _ _ = _
split_ifs with h <;> rw [Finset.mem_map' f] at h
· refine congr_arg (v : α → M) (f.inj' ?_)
exact Finset.choose_property (fun a₁ => f a₁ = f a) _ _
· exact (not_mem_support_iff.1 h).symm
#align finsupp.emb_domain_apply Finsupp.embDomain_apply
theorem embDomain_notin_range (f : α ↪ β) (v : α →₀ M) (a : β) (h : a ∉ Set.range f) :
embDomain f v a = 0 := by
classical
refine dif_neg (mt (fun h => ?_) h)
rcases Finset.mem_map.1 h with ⟨a, _h, rfl⟩
exact Set.mem_range_self a
#align finsupp.emb_domain_notin_range Finsupp.embDomain_notin_range
theorem embDomain_injective (f : α ↪ β) : Function.Injective (embDomain f : (α →₀ M) → β →₀ M) :=
fun l₁ l₂ h => ext fun a => by simpa only [embDomain_apply] using DFunLike.ext_iff.1 h (f a)
#align finsupp.emb_domain_injective Finsupp.embDomain_injective
@[simp]
theorem embDomain_inj {f : α ↪ β} {l₁ l₂ : α →₀ M} : embDomain f l₁ = embDomain f l₂ ↔ l₁ = l₂ :=
(embDomain_injective f).eq_iff
#align finsupp.emb_domain_inj Finsupp.embDomain_inj
@[simp]
theorem embDomain_eq_zero {f : α ↪ β} {l : α →₀ M} : embDomain f l = 0 ↔ l = 0 :=
(embDomain_injective f).eq_iff' <| embDomain_zero f
#align finsupp.emb_domain_eq_zero Finsupp.embDomain_eq_zero
theorem embDomain_mapRange (f : α ↪ β) (g : M → N) (p : α →₀ M) (hg : g 0 = 0) :
embDomain f (mapRange g hg p) = mapRange g hg (embDomain f p) := by
ext a
by_cases h : a ∈ Set.range f
· rcases h with ⟨a', rfl⟩
rw [mapRange_apply, embDomain_apply, embDomain_apply, mapRange_apply]
· rw [mapRange_apply, embDomain_notin_range, embDomain_notin_range, ← hg] <;> assumption
#align finsupp.emb_domain_map_range Finsupp.embDomain_mapRange
theorem single_of_embDomain_single (l : α →₀ M) (f : α ↪ β) (a : β) (b : M) (hb : b ≠ 0)
(h : l.embDomain f = single a b) : ∃ x, l = single x b ∧ f x = a := by
classical
have h_map_support : Finset.map f l.support = {a} := by
rw [← support_embDomain, h, support_single_ne_zero _ hb]
have ha : a ∈ Finset.map f l.support := by simp only [h_map_support, Finset.mem_singleton]
rcases Finset.mem_map.1 ha with ⟨c, _hc₁, hc₂⟩
use c
constructor
· ext d
rw [← embDomain_apply f l, h]
by_cases h_cases : c = d
· simp only [Eq.symm h_cases, hc₂, single_eq_same]
· rw [single_apply, single_apply, if_neg, if_neg h_cases]
by_contra hfd
exact h_cases (f.injective (hc₂.trans hfd))
· exact hc₂
#align finsupp.single_of_emb_domain_single Finsupp.single_of_embDomain_single
@[simp]
theorem embDomain_single (f : α ↪ β) (a : α) (m : M) :
embDomain f (single a m) = single (f a) m := by
classical
ext b
by_cases h : b ∈ Set.range f
· rcases h with ⟨a', rfl⟩
simp [single_apply]
· simp only [embDomain_notin_range, h, single_apply, not_false_iff]
rw [if_neg]
rintro rfl
simp at h
#align finsupp.emb_domain_single Finsupp.embDomain_single
end EmbDomain
/-! ### Declarations about `zipWith` -/
section ZipWith
variable [Zero M] [Zero N] [Zero P]
/-- Given finitely supported functions `g₁ : α →₀ M` and `g₂ : α →₀ N` and function `f : M → N → P`,
`Finsupp.zipWith f hf g₁ g₂` is the finitely supported function `α →₀ P` satisfying
`zipWith f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, which is well-defined when `f 0 0 = 0`. -/
def zipWith (f : M → N → P) (hf : f 0 0 = 0) (g₁ : α →₀ M) (g₂ : α →₀ N) : α →₀ P :=
onFinset
(haveI := Classical.decEq α; g₁.support ∪ g₂.support)
(fun a => f (g₁ a) (g₂ a))
fun a (H : f _ _ ≠ 0) => by
classical
rw [mem_union, mem_support_iff, mem_support_iff, ← not_and_or]
rintro ⟨h₁, h₂⟩; rw [h₁, h₂] at H; exact H hf
#align finsupp.zip_with Finsupp.zipWith
@[simp]
theorem zipWith_apply {f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M} {g₂ : α →₀ N} {a : α} :
zipWith f hf g₁ g₂ a = f (g₁ a) (g₂ a) :=
rfl
#align finsupp.zip_with_apply Finsupp.zipWith_apply
theorem support_zipWith [D : DecidableEq α] {f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M}
{g₂ : α →₀ N} : (zipWith f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := by
convert support_onFinset_subset
#align finsupp.support_zip_with Finsupp.support_zipWith
@[simp]
theorem zipWith_single_single (f : M → N → P) (hf : f 0 0 = 0) (a : α) (m : M) (n : N) :
zipWith f hf (single a m) (single a n) = single a (f m n) := by
ext a'
rw [zipWith_apply]
obtain rfl | ha' := eq_or_ne a a'
· rw [single_eq_same, single_eq_same, single_eq_same]
· rw [single_eq_of_ne ha', single_eq_of_ne ha', single_eq_of_ne ha', hf]
end ZipWith