summaryrefslogtreecommitdiff
path: root/Nominal/Instances/Finsupp.lean
blob: 360c88a59877bc937aa44479d8c4207e7de1b0ca (plain)
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
import Mathlib
import Nominal.Support
import Nominal.Equivariance
import Nominal.Category
import Nominal.Permutations
import Nominal.Instances.Basic

/-!
# Constructing Finitely Supported Sets

We can restrict arbitrary `(FPerm 𝔸)`-sets to finitely supported subsets, which are nominal sets.

Examples of this include finitely supported sets (`FinsuppSet`) and finitely supported functions (`FinsuppFunction`).
-/

variable (𝔸 : Type) [Infinite 𝔸] [Countable 𝔸] [DecidableEq 𝔸]

namespace PermSet

/-!
## Finitely Supported Subsets

We first define how every `PermSet` can be restricted to its finitely supported elements (`Finsupp`), which form a
nominal set.
-/
section FSSubsets

/-- Restrict a type to its finitely supported elements -/
abbrev Finsupp (X : Type) [PermSet 𝔸 X] : Type :=
  { x : X // ∃ S, PermSet.HasSupport 𝔸 x S ∧ S.Finite }

namespace Finsupp

variable {X : Type} [PermSet 𝔸 X]

instance : PermSet 𝔸 (Finsupp 𝔸 X) where
  smul π x := ⟨π • x.val, by
    obtain ⟨S, ⟨hS₁, hS₂⟩⟩ := x.prop
    use π • S; constructor
    · rw [← Rel.equivariant 𝔸 (HasSupport 𝔸)]
      exact hS₁
    · exact Set.Finite.smul_set hS₂⟩
  one_smul x := by
    apply Subtype.ext
    exact MulAction.one_smul x.val
  mul_smul π τ x := by
    apply Subtype.ext
    exact mul_smul π τ x.val

@[simp] lemma smul_val (π : FPerm 𝔸) (x : Finsupp 𝔸 X) : (π • x).val = π • x.val := rfl

instance instValEquivariant : Function.Equivariant 𝔸 (Subtype.val : Finsupp 𝔸 X → X) := by
  constructor; simp

instance : NominalSet 𝔸 (Finsupp 𝔸 X) where
  fin_supp x := by
    obtain ⟨S, ⟨hS₁, hS₂⟩⟩ := x.prop
    use S; constructor
    · intro π hπ
      apply Subtype.ext
      exact hS₁ hπ
    · exact hS₂

end Finsupp

end FSSubsets

/-!
## Finitely Supported Sets

Since the powerset of an (FPerm 𝔸)-set is an (FPerm 𝔸)-set, we can restrict ourselves to finitely supported sets to
construct a nominal set `FinsuppSet`.

Furthermore, common binary operations such as union, intersection, and difference are equivariant functions.
-/
section FSSets

abbrev FinsuppSet (X : Type) [PermSet 𝔸 X] : Type :=
  Finsupp 𝔸 (Set X)

namespace FinsuppSet

variable (X Y : Type) [NominalSet 𝔸 X] [NominalSet 𝔸 Y]

private def union' : (FinsuppSet 𝔸 X × FinsuppSet 𝔸 X) → Set X :=
  fun (S₁, S₂) ↦ S₁.val ∪ S₂.val

instance : Function.Equivariant 𝔸 (union' 𝔸 X) := by
  constructor; intro (S₁, S₂) π
  simp only [union', PermSet.Finsupp.smul_val]
  exact Eq.symm Set.smul_set_union

def union : (FinsuppSet 𝔸 X × FinsuppSet 𝔸 X) → FinsuppSet 𝔸 X :=
  fun p ↦ ⟨union' 𝔸 X p, by
    use NominalSet.supp 𝔸 p; constructor
    · apply Function.Equivariant.preserve_support
      apply NominalSet.supp_is_support
    · apply NominalSet.supp_finite⟩

instance : Function.Equivariant 𝔸 (union 𝔸 X) := by
  constructor; intro p π
  apply Subtype.ext
  simp only [union, Finsupp.smul_val]
  apply Function.equivariant

instance : Union (FinsuppSet 𝔸 X) := ⟨Function.curry (union 𝔸 X)⟩

@[simp] lemma union_val {S₁ S₂ : FinsuppSet 𝔸 X} :
    (S₁ ∪ S₂).val = S₁.val ∪ S₂.val := rfl

@[simp] lemma union_equivariant {S₁ S₂ : FinsuppSet 𝔸 X} {π : FPerm 𝔸} :
    π • (S₁ ∪ S₂) = π • S₁ ∪ π • S₂ := by
  show π • (union _ _ (S₁, S₂)) = union _ _ (π • (S₁, S₂))
  rw [← Function.equivariant]

private def inter' : (FinsuppSet 𝔸 X × FinsuppSet 𝔸 X) → Set X :=
  fun (S₁, S₂) ↦ S₁.val ∩ S₂.val

instance : Function.Equivariant 𝔸 (inter' 𝔸 X) := by
  constructor; intro (S₁, S₂) π
  simp only [inter', PermSet.Finsupp.smul_val]
  exact Eq.symm Set.smul_set_inter

def inter : (FinsuppSet 𝔸 X × FinsuppSet 𝔸 X) → FinsuppSet 𝔸 X :=
  fun p ↦ ⟨inter' 𝔸 X p, by
    use NominalSet.supp 𝔸 p; constructor
    · apply Function.Equivariant.preserve_support
      apply NominalSet.supp_is_support
    · apply NominalSet.supp_finite⟩

instance : Function.Equivariant 𝔸 (inter 𝔸 X) := by
  constructor; intro p π
  apply Subtype.ext
  simp only [inter, Finsupp.smul_val]
  apply Function.equivariant

instance : Inter (FinsuppSet 𝔸 X) := ⟨Function.curry (inter 𝔸 X)⟩

@[simp] lemma inter_val {S₁ S₂ : FinsuppSet 𝔸 X} :
    (S₁ ∩ S₂).val = S₁.val ∩ S₂.val := rfl

@[simp] lemma inter_equivariant {S₁ S₂ : FinsuppSet 𝔸 X} {π : FPerm 𝔸} :
    π • (S₁ ∩ S₂) = π • S₁ ∩ π • S₂ := by
  show π • (inter _ _ (S₁, S₂)) = inter _ _ (π • (S₁, S₂))
  rw [← Function.equivariant]

private def sdiff' : (FinsuppSet 𝔸 X × FinsuppSet 𝔸 X) → Set X :=
  fun (S₁, S₂) ↦ S₁.val \ S₂.val

instance : Function.Equivariant 𝔸 (sdiff' 𝔸 X) := by
  constructor; intro (S₁, S₂) π
  simp only [sdiff', PermSet.Finsupp.smul_val]
  exact Eq.symm Set.smul_set_sdiff

def sdiff : (FinsuppSet 𝔸 X × FinsuppSet 𝔸 X) → FinsuppSet 𝔸 X :=
  fun p ↦ ⟨sdiff' 𝔸 X p, by
    use NominalSet.supp 𝔸 p; constructor
    · apply Function.Equivariant.preserve_support
      apply NominalSet.supp_is_support
    · apply NominalSet.supp_finite⟩

instance : Function.Equivariant 𝔸 (sdiff 𝔸 X) := by
  constructor; intro p π
  apply Subtype.ext
  simp only [sdiff, Finsupp.smul_val]
  apply Function.equivariant

instance : SDiff (FinsuppSet 𝔸 X) := ⟨Function.curry (sdiff 𝔸 X)⟩

@[simp] lemma sdiff_val {S₁ S₂ : FinsuppSet 𝔸 X} :
    (S₁ \ S₂).val = S₁.val \ S₂.val := rfl

@[simp] lemma sdiff_equivariant {S₁ S₂ : FinsuppSet 𝔸 X} {π : FPerm 𝔸} :
    π • (S₁ \ S₂) = π • S₁ \ π • S₂ := by
  show π • (sdiff _ _ (S₁, S₂)) = sdiff _ _ (π • (S₁, S₂))
  rw [← Function.equivariant]

end FinsuppSet

end FSSets

/-!
## Finitely Supported Functions

Since functions between (FPerm 𝔸)-sets are an (FPerm 𝔸)-set, we can restrict ourselves to finitely supported functions
to construct a nominal set `FinsuppFunction`.

Furthermore, there is an isomorphism between finietly supported functions (X →fs Y) and sets 𝒫fs (X × Y).
-/
section FSFunctions

abbrev FinsuppFunction (X Y : Type) [PermSet 𝔸 X] [PermSet 𝔸 Y] : Type :=
  Finsupp 𝔸 (X → Y)

lemma FinsuppFunction.supp_image {X Y : Type} [NominalSet 𝔸 X] [NominalSet 𝔸 Y] (f : PermSet.FinsuppFunction 𝔸 X Y) :
    ∀ x, PermSet.HasSupport 𝔸 (f.val x) (NominalSet.supp 𝔸 f ∪ NominalSet.supp 𝔸 x) := by
  intro x π hπ
  have hπ₁ : π ∈ PermSet.fix 𝔸 (NominalSet.supp 𝔸 f) := by
    apply PermSet.fix_antitone_mem 𝔸 hπ; exact Set.subset_union_left
  have hπ₂ : π ∈ PermSet.fix 𝔸 (NominalSet.supp 𝔸 x) := by
    apply PermSet.fix_antitone_mem 𝔸 hπ; exact Set.subset_union_right
  suffices (π • f).val (π • x) = f.val x by
    simp at this
    exact this
  rw [show π • f = f from NominalSet.supp_is_support 𝔸 hπ₁]
  rw [show π • x = x from NominalSet.supp_is_support 𝔸 hπ₂]

variable (X Y : Type) [NominalSet 𝔸 X] [NominalSet 𝔸 Y]

namespace Finsupp

private def flatten' (f : X → PermSet.FinsuppSet 𝔸 Y) : Set (X × Y) :=
  {p : X × Y | p.snd ∈ (f p.fst).val}

instance : Function.Equivariant 𝔸 (flatten' 𝔸 X Y) := by
  constructor
  intro f π; ext ⟨x, y⟩; constructor
  · simp only [flatten', PermSet.smul_function, PermSet.Finsupp.smul_val, Set.mem_setOf_eq]
    intro hy
    have hy' : π⁻¹ • y ∈ π⁻¹ • π • (f (π⁻¹ • x)).val := by
      rw [Set.smul_mem_smul_set_iff]; exact hy
    rw [inv_smul_smul] at hy'
    rw [show (x, y) = π • (π⁻¹ • x, π⁻¹ • y) by simp]
    rw [Set.smul_mem_smul_set_iff]
    exact hy'
  · intro hxy
    have hxy' : π⁻¹ • (x, y) ∈ π⁻¹ • π • flatten' 𝔸 X Y f := by
      rw [Set.smul_mem_smul_set_iff]; exact hxy
    simp only [inv_smul_smul, Prod.smul_mk, flatten', Set.mem_setOf_eq] at hxy'
    simp only [flatten', PermSet.smul_function, PermSet.Finsupp.smul_val, Set.mem_setOf_eq]
    have hy : π • π⁻¹ • y ∈ π • (f (π⁻¹ • x)).val := by
      rw [Set.smul_mem_smul_set_iff]; exact hxy'
    simp only [smul_inv_smul] at hy
    exact hy

def flatten (f : PermSet.FinsuppFunction 𝔸 X (PermSet.FinsuppSet 𝔸 Y)) :
    PermSet.FinsuppSet 𝔸 (X × Y) :=
  ⟨flatten' 𝔸 X Y f.val, by
    obtain ⟨S, ⟨hS₁, hS₂⟩⟩ := f.prop
    use S; constructor
    · apply Function.Equivariant.preserve_support
      exact hS₁
    · exact hS₂⟩

instance : Function.Equivariant 𝔸 (flatten 𝔸 X Y) := by
  constructor; intro p π
  apply Subtype.ext
  simp only [flatten, Finsupp.smul_val]
  apply Function.equivariant

private def unflatten' (S : PermSet.FinsuppSet 𝔸 (X × Y)) : X → (PermSet.FinsuppSet 𝔸 Y) :=
  fun x ↦ ⟨{y : Y | (x, y) ∈ S.val}, by
    use NominalSet.supp 𝔸 S ∪ NominalSet.supp 𝔸 x; constructor
    · intro π hπ
      have hfixx : π⁻¹ • x = x := by
        apply NominalSet.supp_is_support
        rw [← PermSet.fix_inv]
        exact PermSet.fix_antitone_mem 𝔸 hπ Set.subset_union_right
      have hfixS : π⁻¹ • S = S := by
        apply NominalSet.supp_is_support
        rw [← PermSet.fix_inv]
        exact PermSet.fix_antitone_mem 𝔸 hπ Set.subset_union_left
      ext y; constructor
      · intro hy
        simp only [Set.mem_smul_set_iff_inv_smul_mem, Set.mem_setOf_eq] at hy
        rw [← hfixx, ← hfixS, smul_val, Set.mem_inv_smul_set_iff, Prod.smul_mk, smul_inv_smul, smul_inv_smul] at hy
        exact hy
      · intro hy
        simp only [Set.mem_smul_set_iff_inv_smul_mem, Set.mem_setOf_eq]
        simp only [Set.mem_setOf_eq] at hy
        have hy' : (π⁻¹ • x, π⁻¹ • y) ∈ π⁻¹ • S.val := Set.smul_mem_smul_set hy
        rw [hfixx, ← smul_val, hfixS] at hy'
        exact hy'
    · simp only [Set.finite_union]; constructor
      · exact NominalSet.supp_finite 𝔸
      · exact NominalSet.supp_finite 𝔸⟩

instance : Function.Equivariant 𝔸 (unflatten' 𝔸 X Y) := by
  constructor
  intro S π
  ext x y
  simp only [unflatten', smul_val, Set.mem_setOf_eq, smul_function]
  simp only [Set.mem_smul_set_iff_inv_smul_mem, Set.mem_setOf_eq, Prod.smul_mk]

def unflatten (S : PermSet.FinsuppSet 𝔸 (X × Y)) : PermSet.FinsuppFunction 𝔸 X (PermSet.FinsuppSet 𝔸 Y) :=
  ⟨unflatten' 𝔸 X Y S, by
    obtain ⟨A, ⟨hS₁, hS₂⟩⟩ := S.prop
    use A; constructor
    · apply Function.Equivariant.preserve_support
      rw [← Function.Equivariant.preserve_support_inj 𝔸 Subtype.val_injective] at hS₁
      exact hS₁
    · exact hS₂⟩

instance : Function.Equivariant 𝔸 (unflatten 𝔸 X Y) := by
  constructor; intro p π
  apply Subtype.ext
  simp only [unflatten, Finsupp.smul_val]
  apply Function.equivariant

def flattenEquiv  :
    EquivariantEquiv 𝔸
      (PermSet.FinsuppFunction 𝔸 X (PermSet.FinsuppSet 𝔸 Y))
      (PermSet.FinsuppSet 𝔸 (X × Y)) where
  toFun := flatten 𝔸 X Y
  invFun := unflatten 𝔸 X Y
  toFun_equivariant := inferInstance

end Finsupp

end FSFunctions

end PermSet