File size: 7,961 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Computability.PerfectCompleteness.Foundations.ReducedDomains

namespace OAI

section

namespace PerfectCompleteness.DomainEncoding

open ClauseSupport ReducedDomains

variable {I C Y Z : Type*}

theorem allowed_dropped_iff (value : ReducedValue) :
    Allowed .dropped value ↔ value = .dropped := by
  cases value <;> simp [Allowed]

theorem legalAssignments_all_dropped :
    legalAssignments (fun _ : I => .dropped) = {fun _ : I => .dropped} := by
  ext values
  simp only [mem_legalAssignments, allowed_dropped_iff, Set.mem_singleton_iff]
  exact ⟨fun h => funext h, fun h i => congrFun h i⟩

theorem localKey_eq_dropped_of_unused (s : MixedSupport.Slot)
    (f : C → s.Domain → Y) (h : Unused f) :
    MixedSupport.localKey s f = .dropped := by
  classical
  cases s with
  | clause occurrence variableIDs signs =>
      change clauseKey occurrence variableIDs f = .dropped
      simp only [clauseKey, clauseMode_unused f h]
  | bit variableID =>
      change C → Bool → Y at f
      change Unused (D := Bool) f at h
      change bitKey variableID f = .dropped
      simp only [bitKey, ite_eq_left h]

theorem keyFields_eq_dropped_of_unused [DecidableEq I]
    (slots : I → MixedSupport.Slot) (f : MixedSupport.Assignment slots → Y)
    (h : ∀ i, Unused (MixedSupport.sectionFunction slots f i)) :
    MixedSupport.keyFields slots f = fun _ => .dropped := by
  funext i
  exact localKey_eq_dropped_of_unused (slots i)
    (MixedSupport.sectionFunction slots f i) (h i)

theorem keyFields_eq_dropped_of_constant [DecidableEq I]
    (slots : I → MixedSupport.Slot) (f : MixedSupport.Assignment slots → Y)
    (h : ∀ x x', f x = f x') :
    MixedSupport.keyFields slots f = fun _ => .dropped := by
  apply keyFields_eq_dropped_of_unused slots f
  intro i context u v
  exact h _ _

def retainedFields {n : Nat} (keys : Fin n → SlotKey) : List (Fin n × SlotKey) :=
  ((List.finRange n).map (fun i => (i, keys i))).filter
    (fun entry => decide (entry.2 ≠ .dropped))

theorem retainedKeys_eq_retainedFields {n : Nat} (slots : Fin n → MixedSupport.Slot)
    (f : MixedSupport.Assignment slots → Y) :
    MixedSupport.retainedKeys slots f = retainedFields (MixedSupport.keyFields slots f) := rfl

theorem mem_retainedFields {n : Nat} (keys : Fin n → SlotKey) (i : Fin n)
    (key : SlotKey) :
    (i, key) ∈ retainedFields keys ↔ key = keys i ∧ key ≠ .dropped := by
  constructor
  · intro h
    obtain ⟨hm, hretained⟩ := List.mem_filter.mp h
    obtain ⟨j, _, hpair⟩ := List.mem_map.mp hm
    have hji : j = i := congrArg Prod.fst hpair
    subst j
    have hkey : keys i = key := congrArg Prod.snd hpair
    exact ⟨hkey.symm, of_decide_eq_true hretained⟩
  · rintro ⟨rfl, hretained⟩
    apply List.mem_filter.mpr
    refine ⟨List.mem_map.mpr ⟨i, ?_, rfl⟩, ?_⟩
    · simp
    · exact decide_eq_true hretained

def lookupField [DecidableEq I] (fields : List (I × SlotKey)) (i : I) : SlotKey :=
  match fields with
  | [] => .dropped
  | (j, key) :: rest => if i = j then key else lookupField rest i

theorem lookupField_eq_of_mem [DecidableEq I] (fields : List (I × SlotKey))
    (i : I) (key : SlotKey) :
    (i, key) ∈ fields → (∀ key', (i, key') ∈ fields → key' = key) →
      lookupField fields i = key := by
  induction fields with
  | nil => simp
  | cons head rest ih =>
      rcases head with ⟨j, k⟩
      intro hmem hunique
      by_cases hij : i = j
      · subst i
        simp only [lookupField]
        exact hunique k (by simp)
      · simp only [lookupField, ite_eq_right hij]
        apply ih
        · apply (List.mem_cons.mp hmem).resolve_left
          intro hpair
          exact hij (congrArg Prod.fst hpair)
        · intro key' hkey'
          exact hunique key' (List.mem_cons_of_mem _ hkey')

theorem lookupField_eq_dropped [DecidableEq I] (fields : List (I × SlotKey)) (i : I) :
    (∀ key, (i, key) ∉ fields) → lookupField fields i = .dropped := by
  induction fields with
  | nil => intro _; rfl
  | cons head rest ih =>
      rcases head with ⟨j, k⟩
      intro hnone
      have hij : i ≠ j := by
        intro hij
        subst i
        exact hnone k (by simp)
      simp only [lookupField, ite_eq_right hij]
      apply ih
      intro key hkey
      exact hnone key (List.mem_cons_of_mem _ hkey)

theorem lookupField_retainedFields {n : Nat} (keys : Fin n → SlotKey) (i : Fin n) :
    lookupField (retainedFields keys) i = keys i := by
  by_cases hdropped : keys i = .dropped
  · calc
      lookupField (retainedFields keys) i = .dropped := by
        apply lookupField_eq_dropped
        intro key hkey
        obtain ⟨hkey, hretained⟩ := (mem_retainedFields keys i key).mp hkey
        exact hretained (hkey.trans hdropped)
      _ = keys i := hdropped.symm
  · apply lookupField_eq_of_mem
    · exact (mem_retainedFields keys i (keys i)).mpr ⟨rfl, hdropped⟩
    · intro key hkey
      exact ((mem_retainedFields keys i key).mp hkey).1

theorem retainedFields_injective {n : Nat} :
    Function.Injective (retainedFields (n := n)) := by
  intro keys keys' h
  funext i
  have hlookup := congrArg (fun fields => lookupField fields i) h
  simpa only [lookupField_retainedFields] using hlookup

theorem keyFields_eq_of_retainedKeys_eq {n : Nat}
    (slots projected : Fin n → MixedSupport.Slot)
    (f : MixedSupport.Assignment slots → Y)
    (g : MixedSupport.Assignment projected → Z)
    (h : MixedSupport.retainedKeys slots f = MixedSupport.retainedKeys projected g) :
    MixedSupport.keyFields slots f = MixedSupport.keyFields projected g :=
  retainedFields_injective h

theorem legalAssignments_eq_of_retainedKeys_eq {n : Nat}
    (slots projected : Fin n → MixedSupport.Slot)
    (f : MixedSupport.Assignment slots → Y)
    (g : MixedSupport.Assignment projected → Z)
    (h : MixedSupport.retainedKeys slots f = MixedSupport.retainedKeys projected g) :
    legalAssignments (MixedSupport.keyFields slots f) =
      legalAssignments (MixedSupport.keyFields projected g) :=
  congrArg legalAssignments (keyFields_eq_of_retainedKeys_eq slots projected f g h)

abbrev RetainedLocation (keys : I → SlotKey) := {i : I // keys i ≠ .dropped}

abbrev RetainedAssignment (keys : I → SlotKey) :=
  (i : RetainedLocation keys) → {value : ReducedValue // Allowed (keys i.val) value}

def restrictAssignment (keys : I → SlotKey) (values : ↥(legalAssignments keys)) :
    RetainedAssignment keys :=
  fun i => ⟨values.val i.val, values.property i.val⟩

noncomputable def extendAssignment (keys : I → SlotKey) (values : RetainedAssignment keys) :
    ↥(legalAssignments keys) := by
  classical
  refine ⟨fun i => if h : keys i = .dropped then .dropped else (values ⟨i, h⟩).val, ?_⟩
  change ∀ i, Allowed (keys i)
    (if h : keys i = .dropped then .dropped else (values ⟨i, h⟩).val)
  intro i
  by_cases h : keys i = .dropped
  · rw [dite_eq_left h, h]
    trivial
  · simpa only [dite_eq_right h] using (values ⟨i, h⟩).property

noncomputable def legalRetainedEquiv (keys : I → SlotKey) :
    ↥(legalAssignments keys) ≃ RetainedAssignment keys := by
  classical
  refine
    { toFun := restrictAssignment keys
      invFun := extendAssignment keys
      left_inv := ?_
      right_inv := ?_ }
  · intro values
    apply Subtype.ext
    funext i
    change (if h : keys i = .dropped then .dropped else values.val i) = values.val i
    by_cases h : keys i = .dropped
    · simp only [dite_eq_left h]
      have hv : Allowed .dropped (values.val i) := by
        simpa only [h] using values.property i
      exact ((allowed_dropped_iff _).mp hv).symm
    · simp only [dite_eq_right h]
  · intro values
    funext i
    apply Subtype.ext
    change (if h : keys i.val = .dropped then .dropped
      else (values ⟨i.val, h⟩).val) = (values i).val
    simp only [dite_eq_right i.property]

end PerfectCompleteness.DomainEncoding

end

end OAI