File size: 4,275 Bytes
e5a97c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Combinatorics.MatchingCount.Sampling.TreeDPCount

namespace OAI

namespace MatchingFPRAS.ExecutableDP

lemma privateFlag_perm {b c : Bag} (h : b.Perm c) (l : ℕ) :
    privateFlag (l,b)=privateFlag (l,c) := by simp only [privateFlag,h.length_eq,h.mem_iff]
lemma sharedFlag_perm {b c : Bag} (h : b.Perm c) (l : ℕ) :
    sharedFlag (l,b)=sharedFlag (l,c) := by simp only [sharedFlag,privateFlag_perm h,h.mem_iff]
lemma totalFlag_perm {b c : Bag} (h : b.Perm c) (l : ℕ) :
    totalFlag (l,b)=totalFlag (l,c) := by simp only [totalFlag,h.mem_iff]

lemma forall₂_filter {A B : Type} {R : A → B → Prop} {as : List A} {bs : List B}
    (h : List.Forall₂ R as bs) (p : A → Bool) (q : B → Bool)
    (he : ∀ a b,R a b → p a=q b) : List.Forall₂ R (as.filter p) (bs.filter q) := by
  induction h with
  | nil => exact .nil
  | @cons a b as bs hab hs ih =>
    simp only [List.filter_cons,he a b hab]
    cases q b <;> simp only [Bool.false_eq_true,↓reduceIte]
    · exact ih
    · exact .cons hab ih

lemma privateNumber_rows {a b : Bags} (h : List.Forall₂ List.Perm a b) (l : ℕ) :
    privateNumber a l=privateNumber b l :=
  (forall₂_filter h _ _ (fun _ _ hh=>privateFlag_perm hh l)).length_eq
lemma sharedNumber_rows {a b : Bags} (h : List.Forall₂ List.Perm a b) (l : ℕ) :
    sharedNumber a l=sharedNumber b l :=
  (forall₂_filter h _ _ (fun _ _ hh=>sharedFlag_perm hh l)).length_eq
lemma totalNumber_rows {a b : Bags} (h : List.Forall₂ List.Perm a b) (l : ℕ) :
    totalNumber a l=totalNumber b l :=
  (forall₂_filter h _ _ (fun _ _ hh=>totalFlag_perm hh l)).length_eq
lemma trim_rows {a b : Bags} (h : List.Forall₂ List.Perm a b) (l : ℕ) :
    List.Forall₂ List.Perm (trim a l) (trim b l) :=
  forall₂_filter h _ _ (fun _ _ hh=>congrArg Bool.not (totalFlag_perm hh l))
lemma update_rows {a b : Bags} (h : List.Forall₂ List.Perm a b)
    (ps : Parents) (l m B : ℕ) (t : Table) :
    update (((a,ps),(l,t)),(m,B))=update (((b,ps),(l,t)),(m,B)) := by
  simp only [update,context,privateNumber_rows h,sharedNumber_rows h,totalNumber_rows h]
lemma run_rows {a b : Bags} (h : List.Forall₂ List.Perm a b)
    (p : Params) (ls : List ℕ) (t : Table) :
    (runState p ls a t).2=(runState p ls b t).2 := by
  induction ls generalizing a b t with
  | nil => rfl
  | cons l ls ih =>
    simp only [runState,update_rows h]
    exact ih (trim_rows h l) _
lemma count_rows {a b : Bags} (h : List.Forall₂ List.Perm a b) (ps : Parents) (ls : List ℕ) :
    count ((a,ps),ls)=count ((b,ps),ls) := by
  simp only [count,run,h.length_eq,run_rows h]
lemma run_perm {a b : Bags} (h : a.Perm b) (p : Params) (ls : List ℕ) (t : Table) :
    (runState p ls a t).2=(runState p ls b t).2 := by
  induction ls generalizing a b t with
  | nil => rfl
  | cons l ls ih =>
    simp only [runState,update_perm h]
    exact ih (h.filter _) _
lemma count_perm {a b : Bags} (h : a.Perm b) (ps : Parents) (ls : List ℕ) :
    count ((a,ps),ls)=count ((b,ps),ls) := by
  simp only [count,run,h.length_eq,run_perm h]

noncomputable def orderedRep {n m : ℕ} (bags : Fin n → Finset (Fin m)) : Bags :=
  List.ofFn (fun i=>bagRep (bags i))

lemma orderedRep_perm {n m : ℕ} (bags : Fin n → Finset (Fin m)) :
    (orderedRep bags).Perm (activeRep bags Finset.univ) := by
  unfold orderedRep activeRep
  change (List.ofFn ((bagRep ∘ bags) ∘ id)).Perm _
  rw [← List.map_ofFn]
  apply List.Perm.map
  apply List.perm_of_nodup_nodup_toFinset_eq
  · exact List.nodup_ofFn.mpr Function.injective_id
  · exact Finset.nodup_toList _
  · ext i; simp

lemma count_ordered {n m : ℕ} (bags : Fin n → Finset (Fin m))
    (parent : Fin m → Option (Fin m)) (ls : List (Fin m)) :
    count ((orderedRep bags,parentRep parent),ls.map Fin.val)=UnitRefresh.Materialized.count bags parent ls := by
  rw [count_perm (orderedRep_perm bags),count_eq]

lemma count_represented {n m : ℕ} (bags : Fin n → Finset (Fin m))
    (parent : Fin m → Option (Fin m)) (ls : List (Fin m)) (bs : Bags)
    (hbs : List.Forall₂ List.Perm bs (orderedRep bags)) :
    count ((bs,parentRep parent),ls.map Fin.val)=UnitRefresh.Materialized.count bags parent ls := by
  rw [count_rows hbs,count_ordered]

end MatchingFPRAS.ExecutableDP

end OAI