File size: 3,856 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
import Mathlib
import OAI.Combinatorics.MatchingCount.Machines.TreeFiniteOps

namespace OAI

namespace MatchingFPRAS.TreeTyped
variable {A B : Type} [Codec A] [Codec B] [LinearOrder A] [LinearOrder B]

def boolOr : Realizer (fun x : Bool×Bool=>x.1 || x.2) :=
  (choose first (constant true) second).congr (by intro x; cases x.1 <;> rfl)
lemma polynomial_boolOr : PolynomialTime boolOr := by
  unfold boolOr
  exact polynomial_congr _ (polynomial_choose polynomial_first (polynomial_constant true) polynomial_second)

def colexLe (E : Realizer (fun x : A×A=>decide (x.1=x.2)))
    (R : Realizer (fun x : A×A=>decide (x.1≤x.2))) :
    Realizer (fun x : Finset A×Finset A=>decide (toColex x.1≤toColex x.2)) :=
  (composition (pair (composition (pair second first) (finsetSDiff E)) (finsetSDiff E))
    (finsetAll (composition (pair second first) (finsetAny R)))).congr (by
      intro x
      apply Bool.eq_iff_iff.mpr
      simp only [Function.comp_apply,decide_eq_true_eq,Finset.mem_sdiff]
      rw [Finset.Colex.toColex_le_toColex]
      constructor
      · intro h a ha hn
        obtain ⟨b,⟨hb,hbn⟩,hab⟩:=h a ⟨ha,hn⟩
        exact ⟨b,hb,hbn,hab⟩
      · intro h a ⟨ha,hn⟩
        obtain ⟨b,hb,hbn,hab⟩:=h ha hn
        exact ⟨b,⟨hb,hbn⟩,hab⟩)
lemma polynomial_colexLe {E : Realizer (fun x : A×A=>decide (x.1=x.2))}
    {R : Realizer (fun x : A×A=>decide (x.1≤x.2))}
    (hE : PolynomialTime E) (hR : PolynomialTime R) : PolynomialTime (colexLe E R) := by
  unfold colexLe
  apply polynomial_congr
  exact polynomial_composition (polynomial_pair
    (polynomial_composition (polynomial_pair polynomial_second polynomial_first) (polynomial_finsetSDiff hE))
    (polynomial_finsetSDiff hE))
    (polynomial_finsetAll (polynomial_composition (polynomial_pair polynomial_second polynomial_first)
      (polynomial_finsetAny hR)))

def lexLe (E : Realizer (fun x : A×A=>decide (x.1=x.2)))
    (R : Realizer (fun x : A×A=>decide (x.1≤x.2)))
    (S : Realizer (fun x : B×B=>decide (x.1≤x.2))) :
    Realizer (fun x : (A×B)×(A×B)=>decide (toLex x.1≤toLex x.2)) :=
  (composition (pair (composition (pair first.fst second.fst) R)
    (composition (pair (composition (composition (pair first.fst second.fst) E) boolNot)
      (composition (pair first.snd second.snd) S)) boolOr)) boolAnd).congr (by
        intro x
        apply Bool.eq_iff_iff.mpr
        simp only [Function.comp_apply,Bool.and_eq_true,Bool.or_eq_true,Bool.not_eq_true',
          decide_eq_true_eq,decide_eq_false_iff_not,Prod.Lex.le_iff]
        rcases x with ⟨⟨a,b⟩,⟨c,d⟩⟩
        dsimp only
        constructor
        · rintro ⟨h,h'⟩
          rcases lt_or_eq_of_le h with h | h
          · exact Or.inl h
          · exact Or.inr ⟨h,h'.resolve_left (not_not.mpr h)⟩
        · rintro (h|⟨h,h'⟩)
          · exact ⟨h.le,Or.inl (ne_of_lt h)⟩
          · exact ⟨h.le,Or.inr h'⟩)
lemma polynomial_lexLe {E : Realizer (fun x : A×A=>decide (x.1=x.2))}
    {R : Realizer (fun x : A×A=>decide (x.1≤x.2))}
    {S : Realizer (fun x : B×B=>decide (x.1≤x.2))}
    (hE : PolynomialTime E) (hR : PolynomialTime R) (hS : PolynomialTime S) :
    PolynomialTime (lexLe E R S) := by
  unfold lexLe
  exact polynomial_congr _ (polynomial_composition (polynomial_pair
    (polynomial_composition (polynomial_pair (polynomial_fst polynomial_first) (polynomial_fst polynomial_second)) hR)
    (polynomial_composition (polynomial_pair
      (polynomial_composition (polynomial_composition
        (polynomial_pair (polynomial_fst polynomial_first) (polynomial_fst polynomial_second)) hE) polynomial_boolNot)
      (polynomial_composition (polynomial_pair (polynomial_snd polynomial_first) (polynomial_snd polynomial_second)) hS)) polynomial_boolOr))
    polynomial_boolAnd)
end MatchingFPRAS.TreeTyped

end OAI