roseking's picture
Add lean source and binary files (part 79)
544609e verified
Raw History Blame Contribute Delete
46.2 kB
13
def equal (x y : Formula ι) : Formula ι := .branch (.le x y) (.le y x) (.constant 0)
14
def less (x y : Formula ι) : Formula ι := .branch (.le y x) (.constant 0) (.constant 1)
15
def neg (e : Formula ι) : Formula ι := .branch e (.constant 0) (.constant 1)
16
def and (e f : Formula ι) : Formula ι := .branch e f (.constant 0)
17
def or (e f : Formula ι) : Formula ι := .branch e (.constant 1) f
18
def all (es : List (Formula ι)) : Formula ι := es.foldr and (.constant 1)
19
def any (es : List (Formula ι)) : Formula ι := es.foldr or (.constant 0)
21
def satSub (x y : Formula ι) : Formula ι := .branch (.le y x) (.sub x y) (.constant 0)
22
def field (x offset width : Formula ι) : Formula ι :=
body · 1 line
  .mod (.div x (.powTwo offset)) (.powTwo width)
24
def bitAt (x index : Formula ι) : Formula ι := field x index (.constant 1)
26
@[simp] lemma equal_eval (x y : Formula ι) (vs : ι → ℕ) :
    (equal x y).eval vs=if x.eval vs=y.eval vs then 1 else 0 := by
proof · 5 lines
  by_cases h : x.eval vs=y.eval vs
  · simp [equal,eval,h]
  · by_cases hxy : x.eval vs≤y.eval vs <;>
      simp [equal,eval,h,hxy]
    omega
33
@[simp] lemma less_eval (x y : Formula ι) (vs : ι → ℕ) :
    (less x y).eval vs=if x.eval vs<y.eval vs then 1 else 0 := by
proof · 1 line
  by_cases h : y.eval vs≤x.eval vs <;> simp [less,eval,h]
36
@[simp] lemma neg_eval (x : Formula ι) (vs : ι → ℕ) :
    (neg x).eval vs=if x.eval vs=0 then 1 else 0 := rfl
38
@[simp] lemma and_eval (x y : Formula ι) (vs : ι → ℕ) :
    (and x y).eval vs=if x.eval vs=0 then 0 else y.eval vs := rfl
40
@[simp] lemma or_eval (x y : Formula ι) (vs : ι → ℕ) :
    (or x y).eval vs=if x.eval vs=0 then y.eval vs else 1 := rfl
42
@[simp] lemma all_eval [DecidableEq ι] (es : List (Formula ι)) (vs : ι → ℕ) :
    (all es).eval vs=if ∀e∈es,e.eval vs≠0 then 1 else 0 := by
proof · 6 lines
  induction es with
  | nil => simp [all,eval]
  | cons e es ih =>
    change (and e (all es)).eval vs=_
    rw [and_eval,ih]
    by_cases h : e.eval vs=0 <;> simp [h]
50
@[simp] lemma any_eval [DecidableEq ι] (es : List (Formula ι)) (vs : ι → ℕ) :
    (any es).eval vs=if ∃e∈es,e.eval vs≠0 then 1 else 0 := by
proof · 6 lines
  induction es with
  | nil => simp [any,eval]
  | cons e es ih =>
    change (or e (any es)).eval vs=_
    rw [or_eval,ih]
    by_cases h : e.eval vs=0 <;> simp [h]
58
@[simp] lemma satSub_eval (x y : Formula ι) (vs : ι → ℕ) :
    (satSub x y).eval vs=x.eval vs-y.eval vs := by
proof · 2 lines
  by_cases h : y.eval vs≤x.eval vs <;> simp [satSub,eval,h]
  omega
62
@[simp] lemma field_eval (x index width : Formula ι) (vs : ι → ℕ) :
    (field x index width).eval vs=(x.eval vs/2^(index.eval vs))%2^(width.eval vs) := rfl
65
lemma bool_all [DecidableEq ι] (es : List (Formula ι)) (vs : ι → ℕ) : (all es).eval vs≤1 := by rw [all_eval];split <;> omega
66
lemma bool_any [DecidableEq ι] (es : List (Formula ι)) (vs : ι → ℕ) : (any es).eval vs≤1 := by rw [any_eval];split <;> omega
67
lemma bool_equal (x y : Formula ι) (vs : ι → ℕ) : (equal x y).eval vs≤1 := by rw [equal_eval];split <;> omega
68
lemma bool_less (x y : Formula ι) (vs : ι → ℕ) : (less x y).eval vs≤1 := by rw [less_eval];split <;> omega
69
lemma bool_neg (x : Formula ι) (vs : ι → ℕ) : (neg x).eval vs≤1 := by rw [neg_eval];split <;> omega
71
lemma safe_equal {x y : Formula ι} {n : ℕ} {vs : ι → ℕ} (hx : x.Safe n vs) (hy : y.Safe n vs) :
    (equal x y).Safe n vs := by
proof · 4 lines
  simp only [equal,Safe,eval]
  refine ⟨⟨hx,hy⟩,?_,?_⟩
  · split <;> omega
  · split_ifs <;> simp_all
77
lemma safe_less {x y : Formula ι} {n : ℕ} {vs : ι → ℕ} (hn : 0<n)
    (hx : x.Safe n vs) (hy : y.Safe n vs) : (less x y).Safe n vs := by
proof · 5 lines
  simp only [less,Safe,eval]
  refine ⟨⟨hy,hx⟩,?_,?_⟩
  · split <;> omega
  · split_ifs <;> simp_all
    omega
84
lemma safe_neg {x : Formula ι} {n : ℕ} {vs : ι → ℕ} (hn : 0<n)
    (hx : x.Safe n vs) (hb : x.eval vs≤1) : (neg x).Safe n vs := by
proof · 4 lines
  simp only [neg,Safe]
  exact ⟨hx,hb,by
    split_ifs <;> simp_all
    omega⟩
90
lemma safe_and {x y : Formula ι} {n : ℕ} {vs : ι → ℕ}
    (hx : x.Safe n vs) (hy : y.Safe n vs) (hb : x.eval vs≤1) : (and x y).Safe n vs := by
proof · 2 lines
  simp only [and,Safe]
  exact ⟨hx,hb,by split_ifs <;> simp_all⟩
94
lemma safe_or {x y : Formula ι} {n : ℕ} {vs : ι → ℕ} (hn : 0<n)
    (hx : x.Safe n vs) (hy : y.Safe n vs) (hb : x.eval vs≤1) : (or x y).Safe n vs := by
proof · 4 lines
  simp only [or,Safe]
  exact ⟨hx,hb,by
    split_ifs <;> simp_all
    omega⟩
100
lemma safe_all {es : List (Formula ι)} {n : ℕ} {vs : ι → ℕ} (hn : 0<n)
    (h : ∀e∈es,e.Safe n vs ∧ e.eval vs≤1) : (all es).Safe n vs := by
proof · 4 lines
  induction es with
  | nil => simp [all,Safe];omega
  | cons e es ih =>
    exact safe_and (h e (by simp)).1 (ih (by intro a ha;exact h a (by simp [ha]))) (h e (by simp)).2
106
lemma safe_any {es : List (Formula ι)} {n : ℕ} {vs : ι → ℕ} (hn : 0<n)
    (h : ∀e∈es,e.Safe n vs ∧ e.eval vs≤1) : (any es).Safe n vs := by
proof · 4 lines
  induction es with
  | nil => simp [any,Safe]
  | cons e es ih =>
    exact safe_or hn (h e (by simp)).1 (ih (by intro a ha;exact h a (by simp [ha]))) (h e (by simp)).2
112
lemma safe_satSub {x y : Formula ι} {n : ℕ} {vs : ι → ℕ}
    (hx : x.Safe n vs) (hy : y.Safe n vs) : (satSub x y).Safe n vs := by
proof · 4 lines
  simp only [satSub,Safe,eval]
  refine ⟨⟨hy,hx⟩,?_,?_⟩
  · split <;> omega
  · split_ifs <;> simp_all
122
def implies (x y : Formula ι) : Formula ι := .branch x y (.constant 1)
123
lemma implies_eval (x y : Formula ι) (v : ι → ℕ) :
    (implies x y).eval v=if x.eval v=0 then 1 else y.eval v := rfl
125
@[simp] lemma implies_ne_zero (x y : Formula ι) (v : ι → ℕ) :
    (implies x y).eval v≠0 ↔ (x.eval v≠0 → y.eval v≠0) := by
proof · 1 line
  by_cases h : x.eval v=0 <;> simp [implies_eval,h]
128
lemma bool_implies (x y : Formula ι) (v : ι → ℕ) (hy : y.eval v≤1) :
    (implies x y).eval v≤1 := by rw [implies_eval];split <;> omega
130
lemma safe_implies {x y : Formula ι} {n : ℕ} {v : ι → ℕ} (hn : 0<n)
    (hx : x.Safe n v) (hy : y.Safe n v) (hb : x.eval v≤1) : (implies x y).Safe n v := by
proof · 4 lines
  simp only [implies,Safe]
  exact ⟨hx,hb,by
    split_ifs <;> simp_all
    omega⟩
137
lemma safe_field {x i w : Formula ι} {n : ℕ} {v : ι → ℕ}
    (hx : x.Safe n v) (hi : i.Safe n v) (hw : w.Safe n v)
    (hin : i.eval v<n) (hwn : w.eval v<n) : (field x i w).Safe n v := by
proof · 4 lines
  have H (a : ℕ) (ha : a<n) : 2*(2^a)≤2^n := by
    rw [←Nat.pow_succ']
    exact Nat.pow_le_pow_right (by omega) (by omega)
  exact ⟨⟨hx,⟨hi,hin⟩,by change 0<2^(i.eval v);positivity,H _ hin⟩,⟨hw,hwn⟩,by change 0<2^(w.eval v);positivity,H _ hwn⟩
148
instance (q : ℕ) (v : Fin 32 → ℕ) : Decidable (InRange q v) := by unfold InRange;infer_instance
149
abbrev F := Formula (Fin 2)
150
def height : F := .read 0
151
def word : F := .read 1
152
def scale (a : ℕ) : F := .mul (.constant a) height
153
def slot (s : Fin 32) : F := field word (scale (2*s.val)) (scale 2)
154
def flag (s : Fin 512) : F := bitAt word (.add (scale 64) (.constant s.val))
155
def bound : F := .powTwo height
157
def ranges : F := all
body · 6 lines
  [less (slot 0) (.constant 3),less (slot 1) bound,
   implies (equal (slot 0) (.constant 2)) (equal (slot 1) (.constant 0)),
   less (slot 2) (.constant 5),less (slot 3) (.constant 10),.le (slot 4) (slot 3),
   less (slot 5) (.constant 3),less (slot 6) bound,
   implies (equal (slot 5) (.constant 0)) (equal (slot 6) (.constant 0)),
   less (slot 7) bound,less (slot 8) (.constant 9)]
164
def reservation : F := and
body · 2 lines
  (equal (field word (scale 18) (scale 46)) (.constant 0))
  (equal (.div word (.powTwo (.add (scale 64) (.constant 8)))) (.constant 0))
167
def valid : F := and ranges reservation
169
def inputs (h w : ℕ) : Fin 2 → ℕ := ![h,w]
170
@[simp] lemma height_eval (h w : ℕ) : height.eval (inputs h w)=h := rfl
171
@[simp] lemma word_eval (h w : ℕ) : word.eval (inputs h w)=w := rfl
172
@[simp] lemma scale_eval (a h w : ℕ) : (scale a).eval (inputs h w)=a*h := rfl
173
@[simp] lemma slot_eval (s : Fin 32) (h w : ℕ) :
    (slot s).eval (inputs h w)=(w/2^(s.val*(2*h)))%2^(2*h) := by
proof · 1 line
  simp [slot,Nat.mul_comm,Nat.mul_left_comm]
176
@[simp] lemma bound_eval (h w : ℕ) : bound.eval (inputs h w)=2^h := rfl
178
lemma ranges_eval (h w : ℕ) : ranges.eval (inputs h w)=
    if InRange (2^h) (fun s : Fin 32 => (w/2^(s.val*(2*h)))%2^(2*h)) then 1 else 0 := by
proof · 1 line
  simp [ranges,InRange,eval,and_assoc]
182
lemma reservation_eval (h w : ℕ) : reservation.eval (inputs h w)=
    if (w/2^(18*h))%2^(46*h)=0 ∧ w/2^(64*h+8)=0 then 1 else 0 := by
proof · 3 lines
  simp only [reservation,and_eval,equal_eval,field_eval,scale_eval,word_eval,eval]
  by_cases H : (w/2^(18*h))%2^(46*h)=0 <;>
    by_cases J : w/2^(64*h+8)=0 <;> simp [H,J]
188
lemma valid_eval (h w : ℕ) : valid.eval (inputs h w)=
    if InRange (2^h) (fun s : Fin 32 => (w/2^(s.val*(2*h)))%2^(2*h)) ∧
      ((w/2^(18*h))%2^(46*h)=0 ∧ w/2^(64*h+8)=0) then 1 else 0 := by
proof · 2 lines
  rw [valid,and_eval,ranges_eval,reservation_eval]
  split_ifs <;> simp_all
194
theorem accepts_iff_decode {h : ℕ} (hh : 0<h) (w : Raw h) :
    valid.eval (inputs h (natCode w))=1 ↔ (decode hh w).isSome=true := by
proof · 2 lines
  rw [decode_isSome_arithmetic hh w,valid_eval]
  split_ifs <;> simp_all
199
lemma valid_boolean (h w : ℕ) : valid.eval (inputs h w)≤1 := by rw [valid_eval];split_ifs <;> omega
201
lemma safe_constant {a n : ℕ} (ha : a ≤ 64) (hn : 7≤n) (v : Fin 2 → ℕ) :
    (Formula.constant a : F).Safe n v := by
proof · 4 lines
  change a.bits.length≤n
  have hb : a<2^7 := by norm_num;omega
  have h := (Nat.size_le).mpr hb
  simpa only [Nat.size_eq_bits_len] using h.trans hn
208
lemma scale_safe {a h w n : ℕ} (ha : a≤64) (hn : 64*h+10≤n) :
    (scale a).Safe n (inputs h w) := by
proof · 3 lines
  refine ⟨safe_constant ha (by omega) _,trivial,?_⟩
  have H : a*h<n := by nlinarith
  exact H.trans (Nat.lt_two_pow_self)
214
lemma slots_safe {h w n : ℕ} (_hh : 0<h) (hn : 64*h+10≤n) (s : Fin 32) :
    (slot s).Safe n (inputs h w) := by
proof · 6 lines
  apply safe_field (x:=word) (i:=scale (2*s.val)) (w:=scale 2) (n:=n) (v:=inputs h w) trivial
    (scale_safe (by have := s.isLt;omega) hn) (scale_safe (by omega) hn)
  · rw [scale_eval]
    have := s.isLt
    nlinarith
  · rw [scale_eval];omega
223
lemma bound_safe {h w n : ℕ} (hn : 64*h+10≤n) : bound.Safe n (inputs h w) := by
proof · 1 line
  exact ⟨trivial,by change h<n;omega⟩
226
lemma ranges_safe {h w n : ℕ} (hh : 0<h) (hn : 64*h+10≤n) : ranges.Safe n (inputs h w) := by
proof · 18 lines
  have Hn : 0<n := by omega
  have HC (a : ℕ) (ha : a≤64) : (Formula.constant a : F).Safe n (inputs h w) := safe_constant ha (by omega) _
  have HS := slots_safe (w:=w) hh hn
  have HB := bound_safe (w:=w) hn
  apply safe_all Hn
  intro e he
  simp only [List.mem_cons,List.mem_nil_iff,or_false] at he
  rcases he with rfl|rfl|rfl|rfl|rfl|rfl|rfl|rfl|rfl|rfl|rfl
  all_goals constructor
  all_goals first
    | exact safe_less Hn (HS _) HB
    | exact safe_less Hn (HS _) (HC _ (by omega))
    | exact safe_implies Hn (safe_equal (HS _) (HC _ (by omega)))
        (safe_equal (HS _) (HC _ (by omega))) (bool_equal _ _ _)
    | exact ⟨HS _,HS _⟩
    | exact bool_less _ _ _
    | exact bool_implies _ _ _ (bool_equal _ _ _)
    | (simp only [eval];split_ifs <;> omega)
246
lemma reservation_safe {h w n : ℕ} (_hh : 0<h) (hn : 64*h+10≤n) :
    reservation.Safe n (inputs h w) := by
proof · 15 lines
  have H0 := safe_constant (a:=0) (by omega) (show 7≤n by omega) (inputs h w)
  have Hf := safe_field (x:=word) (i:=scale 18) (w:=scale 46) (n:=n) (v:=inputs h w) trivial
    (scale_safe (by omega) hn) (scale_safe (by omega) hn)
    (by rw [scale_eval];omega) (by rw [scale_eval];omega)
  have Ha : (Formula.add (scale 64) (.constant 8)).Safe n (inputs h w) := by
    refine ⟨scale_safe (by omega) hn,safe_constant (by omega) (by omega) _,?_⟩
    change 64*h+8<2^n
    exact (show 64*h+8<n by omega).trans Nat.lt_two_pow_self
  have Hp : (Formula.powTwo (.add (scale 64) (.constant 8))).Safe n (inputs h w) := ⟨Ha,by change 64*h+8<n;omega⟩
  have Hd : (Formula.div word (.powTwo (.add (scale 64) (.constant 8)))).Safe n (inputs h w) := by
    refine ⟨trivial,Hp,by change 0<2^(64*h+8);positivity,?_⟩
    change 2*2^(64*h+8)≤2^n
    rw [←Nat.pow_succ']
    exact Nat.pow_le_pow_right (by omega) (by omega)
  exact safe_and (safe_equal Hf H0) (safe_equal Hd H0) (bool_equal _ _ _)
264
theorem valid_safe {h w n : ℕ} (hh : 0<h) (hn : 64*h+10≤n) : valid.Safe n (inputs h w) :=
proof · 1 line
  safe_and (ranges_safe hh hn) (reservation_safe hh hn) (bool_all _ _)
272
def boolean (b : Bool) : ℕ := if b then 1 else 0
273
@[simp] lemma boolean_false : boolean false=0 := rfl
274
@[simp] lemma boolean_true : boolean true=1 := rfl
275
lemma boolean_injective : Function.Injective boolean := by intro a b;cases a <;> cases b <;> simp [boolean]
276
@[simp] lemma boolean_eq_zero (b : Bool) : boolean b=0 ↔ b=false := by cases b <;> decide
277
@[simp] lemma boolean_eq_one (b : Bool) : boolean b=1 ↔ b=true := by cases b <;> decide
278
@[simp] lemma boolean_bit_zero : boolean (bit 0)=0 := rfl
279
lemma boolean_lt_two (b : Bool) : boolean b<2 := by cases b <;> decide
281
abbrev Record (A : Type) := (Fin 9 → A) × (Fin 8 → A)
282
def cellRecord {q : ℕ} (c : Cell q) : Record ℕ :=
body · 2 lines
  ⟨fun s => integers c ⟨s.val,by have := s.isLt;omega⟩,
   fun s => boolean (flags c ⟨s.val,by have := s.isLt;omega⟩)⟩
286
lemma repr_injective {h : ℕ} (hh : 4≤h) : Function.Injective (cellRecord (q:=2^h)) := by
proof · 14 lines
  intro c d H
  apply encode_injective hh
  unfold encode
  congr 1
  · funext s
    by_cases hs : s.val<9
    · have Hs := congrArg (fun r : Record ℕ => r.1 ⟨s.val,hs⟩) H
      exact Hs
    · rw [integers_unused c s (by omega),integers_unused d s (by omega)]
  · funext s
    by_cases hs : s.val<8
    · have Hs := congrArg (fun r : Record ℕ => r.2 ⟨s.val,hs⟩) H
      exact boolean_injective Hs
    · rw [flags_unused c s (by omega),flags_unused d s (by omega)]
302
lemma repr_integer {q : ℕ} (c : Cell q) (i : Fin 9) :
    (cellRecord c).1 i=integers c ⟨i.val,by have := i.isLt;omega⟩ := rfl
304
lemma repr_flag {q : ℕ} (c : Cell q) (i : Fin 8) :
    (cellRecord c).2 i=boolean (flags c ⟨i.val,by have := i.isLt;omega⟩) := rfl
312
abbrev Expr := Record (Formula ι)
314
def evaluate (a : Expr (ι:=ι)) (v : ι → ℕ) : Record ℕ :=
body · 1 line
  ⟨fun i => (a.1 i).eval v,fun i => (a.2 i).eval v⟩
317
def ofRaw (height word : Formula ι) : Expr (ι:=ι) :=
body · 2 lines
  ⟨fun i => field word (.mul (.constant (2*i.val)) height) (.mul (.constant 2) height),
   fun i => bitAt word (.add (.mul (.constant 64) height) (.constant i.val))⟩
322
lemma ofRaw_represents {h : ℕ} (hh : 4≤h) (c : Cell (2^h)) (H W : Formula ι) (v : ι → ℕ)
    (hH : H.eval v=h) (hW : W.eval v=natCode (encode c)) :
    evaluate (ofRaw H W) v=cellRecord c := by
proof · 9 lines
  apply Prod.ext
  · funext i
    have HR := readInt_arithmetic (show 0<h by omega) (encode c) ⟨i.val,by have := i.isLt;omega⟩
    rw [readInt_encode hh] at HR
    simpa [evaluate,ofRaw,Formula.eval,hH,hW,cellRecord,Nat.mul_comm,Nat.mul_left_comm,Nat.mul_assoc] using HR.symm
  · funext i
    have HR := flag_as_nat (show 0<h by omega) (encode c) ⟨i.val,by have := i.isLt;omega⟩
    rw [readFlag_encode] at HR
    simpa [evaluate,ofRaw,bitAt,Formula.eval,hH,hW,cellRecord,boolean] using HR.symm
335
def equalCells (a b : Expr (ι:=ι)) : Formula ι :=
body · 2 lines
  and (all (List.ofFn (fun i : Fin 9 => equal (a.1 i) (b.1 i))))
    (all (List.ofFn (fun i : Fin 8 => equal (a.2 i) (b.2 i))))
339
lemma equalCells_eval (a b : Expr (ι:=ι)) (v : ι → ℕ) :
    (equalCells a b).eval v=if evaluate a v=evaluate b v then 1 else 0 := by
proof · 8 lines
  simp only [equalCells,and_eval,all_eval]
  have H (n : ℕ) (a b : Fin n → Formula ι) :
      (∀e∈List.ofFn (fun i => equal (a i) (b i)),e.eval v≠0) ↔
        (fun i => (a i).eval v)=(fun i => (b i).eval v) := by
    simp [funext_iff]
  simp only [H]
  simp only [evaluate,Prod.mk.injEq]
  split_ifs <;> simp_all
350
lemma equality_exact {h : ℕ} (hh : 4≤h) (a b : Expr (ι:=ι)) (v : ι → ℕ)
    (c d : Cell (2^h)) (ha : evaluate a v=cellRecord c) (hb : evaluate b v=cellRecord d) :
    (equalCells a b).eval v=1 ↔ c=d := by
proof · 2 lines
  simp only [equalCells_eval,ha,hb,(repr_injective hh).eq_iff]
  split_ifs <;> simp_all
356
lemma equalCells_bool (a b : Expr (ι:=ι)) (v : ι → ℕ) : (equalCells a b).eval v≤1 := by
proof · 1 line
  rw [equalCells_eval];split_ifs <;> omega
359
def wing (a : Expr (ι:=ι)) (m : Marker) : Expr (ι:=ι) :=
body · 2 lines
  ⟨fun i => match i.val with | 2 => .constant (markerTag m) | 7 | 8 => .constant 0 | _ => a.1 i,
   fun i => match i.val with | 1 | 5 | 6 | 7 => .constant 0 | _ => a.2 i⟩
362
def boundary (a : Expr (ι:=ι)) (side : Formula ι) : Expr (ι:=ι) :=
body · 2 lines
  ⟨(wing a .boundary).1,
   fun i => match i.val with | 1 => side | 5 | 6 | 7 => .constant 0 | _ => a.2 i⟩
365
def full (a : Expr (ι:=ι)) (side clause : Formula ι) (phase : Phase)
    (reg : Fin 3 → Formula ι) : Expr (ι:=ι) :=
body · 2 lines
  ⟨fun i => match i.val with | 2 => .constant 3 | 7 => clause | 8 => .constant (phaseTag phase) | _ => a.1 i,
   fun i => match i.val with | 1 => side | 5 => reg 0 | 6 => reg 1 | 7 => reg 2 | _ => a.2 i⟩
369
def idle (a : Expr (ι:=ι)) (side : Formula ι) : Expr (ι:=ι) :=
body · 2 lines
  ⟨fun i => match i.val with | 2 | 3 | 4 | 7 | 8 => .constant 0 | _ => a.1 i,
   fun i => if i.val=2 then .branch side (a.2 3) (a.2 2) else .constant 0⟩
372
def setChecker (a : Expr (ι:=ι)) (tag index : Formula ι) : Expr (ι:=ι) :=
body · 1 line
  ⟨fun i => match i.val with | 5 => tag | 6 => index | _ => a.1 i,a.2⟩
374
def setColor (a : Expr (ι:=ι)) (color : Formula ι) : Expr (ι:=ι) :=
body · 1 line
  ⟨fun i => match i.val with | 0 => .constant 1 | 1 => color | _ => a.1 i,a.2⟩
379
lemma represent_int {q : ℕ} {a : Expr (ι:=ι)} {v : ι → ℕ} {c : Cell q}
    (h : evaluate a v=cellRecord c) (i : Fin 9) : (a.1 i).eval v=(cellRecord c).1 i := congrArg (fun r => r.1 i) h
381
lemma represent_flag {q : ℕ} {a : Expr (ι:=ι)} {v : ι → ℕ} {c : Cell q}
    (h : evaluate a v=cellRecord c) (i : Fin 8) : (a.2 i).eval v=(cellRecord c).2 i := congrArg (fun r => r.2 i) h
388
lemma wing_represents (m : Marker) : evaluate (wing a m) v=cellRecord (c.wing m) := by
proof · 3 lines
  apply Prod.ext <;> funext i <;> fin_cases i <;>
    simp [evaluate,wing,Formula.eval,represent_int ha,represent_flag ha,cellRecord,
      integers,flags,Cell.wing,Skeleton.mark,defaultHead,Cell.pos,Cell.marker,Cell.metadata,phaseTag]
393
lemma boundary_represents (side : Formula ι) (s : Bool) (hs : side.eval v=boolean s) :
    evaluate (boundary a side) v=cellRecord (c.boundary s) := by
proof · 3 lines
  apply Prod.ext <;> funext i <;> fin_cases i <;>
    simp [evaluate,boundary,wing,Formula.eval,represent_int ha,represent_flag ha,hs,cellRecord,
      integers,flags,Cell.boundary,Skeleton.mark,defaultHead,Cell.pos,Cell.marker,Cell.metadata,phaseTag]
400
lemma full_represents (side clause : Formula ι) (phase : Phase) (reg : Fin 3 → Formula ι)
    (s : Bool) (j : Fin q) (r : Reg) (hs : side.eval v=boolean s) (hj : clause.eval v=j.val)
    (hr : ∀i,(reg i).eval v=boolean (bit (r i))) :
    evaluate (full a side clause phase reg) v=cellRecord (c.full s j phase r) := by
proof · 3 lines
  apply Prod.ext <;> funext i <;> fin_cases i <;>
    simp [evaluate,full,Formula.eval,represent_int ha,represent_flag ha,hs,hj,hr,cellRecord,
      integers,flags,Cell.full,Skeleton.mark,Cell.pos,Cell.marker,Cell.metadata,markerTag]
408
lemma idle_represents (side : Formula ι) (s : Bool) (hs : side.eval v=boolean s) :
    evaluate (idle a side) v=cellRecord (c.idle s) := by
proof · 5 lines
  apply Prod.ext <;> funext i <;> fin_cases i <;>
    simp [evaluate,idle,Formula.eval,represent_int ha,represent_flag ha,hs,cellRecord,
      integers,flags,Cell.idle,defaultHead,defaultMeta,Cell.pos,Cell.marker,Cell.metadata,
      markerTag,phaseTag,bit,boolean]
  cases s <;> simp
417
lemma setChecker_represents (tag index : Formula ι) (ch : Palette.Check (Fin q))
    (ht : tag.eval v=checkerTag ch) (hi : index.eval v=checkerIndex ch) :
    evaluate (setChecker a tag index) v=cellRecord (c.setChecker ch) := by
proof · 3 lines
  apply Prod.ext <;> funext i <;> fin_cases i <;>
    simp [evaluate,setChecker,represent_int ha,represent_flag ha,ht,hi,cellRecord,
      integers,flags,Cell.setChecker,Cell.pos,Cell.marker,Cell.metadata]
425
lemma setColor_represents (color : Formula ι) (col : Fin q) (hc : color.eval v=col.val) :
    evaluate (setColor a color) v=cellRecord (c.setColor col) := by
proof · 3 lines
  apply Prod.ext <;> funext i <;> fin_cases i <;>
    simp [evaluate,setColor,Formula.eval,represent_int ha,represent_flag ha,hc,cellRecord,
      integers,flags,Cell.setColor,Cell.pos,Cell.marker,Cell.metadata,positionTag,positionIndex]
435
@[simp] lemma constant_eval (k : ℕ) (v : ι → ℕ) : (.constant k : Formula ι).eval v=k := rfl
436
@[simp] lemma add_eval (a b : Formula ι) (v : ι → ℕ) : (Formula.add a b).eval v=a.eval v+b.eval v := rfl
437
@[simp↓] lemma equal_ne_zero (x y : Formula ι) (v : ι → ℕ) :
    (equal x y).eval v≠0 ↔ x.eval v=y.eval v := by simp [equal_eval]
439
@[simp↓] lemma all_ne_zero [DecidableEq ι] (es : List (Formula ι)) (v : ι → ℕ) :
    (all es).eval v≠0 ↔ ∀e∈es,e.eval v≠0 := by simp [all_eval]
441
@[simp↓] lemma any_ne_zero [DecidableEq ι] (es : List (Formula ι)) (v : ι → ℕ) :
    (any es).eval v≠0 ↔ ∃e∈es,e.eval v≠0 := by simp [any_eval]
443
@[simp↓] lemma and_ne_zero (x y : Formula ι) (v : ι → ℕ) :
    (and x y).eval v≠0 ↔ x.eval v≠0 ∧ y.eval v≠0 := by
proof · 1 line
  rw [and_eval];split_ifs <;> simp_all
446
@[simp↓] lemma or_ne_zero (x y : Formula ι) (v : ι → ℕ) :
    (or x y).eval v≠0 ↔ x.eval v≠0 ∨ y.eval v≠0 := by
proof · 1 line
  rw [or_eval];split_ifs <;> simp_all
449
@[simp↓] lemma neg_ne_zero (x : Formula ι) (v : ι → ℕ) :
    (neg x).eval v≠0 ↔ ¬x.eval v≠0 := by simp [neg_eval]
457
def first (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  all [equal (a.1 4) (.constant 0),equal (a.1 0) (.constant 0),equal (a.1 1) (.constant 0)]
459
def last (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  all [equal (a.1 4) (a.1 3),equal (a.1 0) (.constant 2)]
461
def hasMarker (a : Expr (ι:=ι)) (m : Marker) := equal (a.1 2) (.constant (markerTag m))
462
def hasPhase (a : Expr (ι:=ι)) (p : Phase) := equal (a.1 8) (.constant (phaseTag p))
463
def isCore (a : Expr (ι:=ι)) := equal (a.1 0) (.constant 0)
464
def isPalette (a : Expr (ι:=ι)) := equal (a.1 0) (.constant 1)
465
def isStop (a : Expr (ι:=ι)) := equal (a.1 0) (.constant 2)
466
def hasHead (a : Expr (ι:=ι)) := or (hasMarker a .boundary) (hasMarker a .full)
467
def endpoint (a : Expr (ι:=ι)) := any ([Phase.acquisition,.beforeGate,.afterGate,.finish,.success].map (hasPhase a))
469
def admissible (a : Expr (ι:=ι)) : Formula ι :=
body · 2 lines
  and (implies (hasMarker a .right) (neg (first a)))
    (implies (hasMarker a .left) (neg (last a)))
472
def leftExternal (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  or (hasMarker a .idle) (and (neg (hasMarker a .right)) (first a))
474
def rightExternal (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  or (hasMarker a .idle) (or (hasMarker a .boundary) (and (neg (hasMarker a .left)) (last a)))
476
def acceptsLeft (a b : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  .branch (hasMarker a .right) (or (hasMarker b .full) (hasMarker b .right)) (hasMarker b .left)
478
def acceptsRight (a b : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  .branch (hasMarker a .left) (any [hasMarker b .left,hasMarker b .boundary,hasMarker b .full]) (hasMarker b .right)
480
def structural (a b : Expr (ι:=ι)) (Q : Formula ι) : Formula ι :=
body · 4 lines
  any [all [isCore a,isCore b,equal (.add (a.1 1) (.constant 1)) (b.1 1)],
    all [isCore a,isPalette b,equal (.add (a.1 1) (.constant 1)) Q],
    and (isPalette a) (isPalette b),and (isPalette a) (isStop b),
    all [isStop a,isCore b,equal (b.1 1) (.constant 0)]]
485
def internal (a b : Expr (ι:=ι)) : Formula ι :=
body · 3 lines
  all [equal (a.1 3) (b.1 3),equal (a.2 0) (b.2 0),
    .branch (isStop a) (equal (.add (a.1 4) (.constant 1)) (b.1 4)) (equal (a.1 4) (b.1 4)),
    neg (first b),neg (last a)]
489
def skeletonPair (a b : Expr (ι:=ι)) (Q : Formula ι) : Formula ι :=
body · 2 lines
  and (structural a b Q) (or (and (rightExternal a) (leftExternal b))
    (all [neg (rightExternal a),neg (leftExternal b),acceptsRight a b,acceptsLeft b a,internal a b]))
493
lemma markerTag_injective : Function.Injective markerTag := by intro a b;cases a <;> cases b <;> simp [markerTag]
494
lemma phaseTag_injective : Function.Injective phaseTag := by intro a b;cases a <;> cases b <;> simp [phaseTag]
499
lemma first_exact : (first a).eval v≠0 ↔ c.skeleton.first := by
proof · 3 lines
  cases hp : c.skeleton.pos <;>
    simp [first,represent_int ha,cellRecord,integers,Skeleton.first,Cell.pos,hp,
      positionTag,positionIndex,Cell.metadata]
504
lemma last_exact : (last a).eval v≠0 ↔ c.skeleton.last := by
proof · 3 lines
  cases hp : c.skeleton.pos <;>
    simp [last,represent_int ha,cellRecord,integers,Skeleton.last,Cell.pos,hp,
      positionTag,Cell.metadata]
509
lemma marker_exact (m : Marker) : (hasMarker a m).eval v≠0 ↔ c.marker=m := by
proof · 1 line
  simp [hasMarker,represent_int ha,cellRecord,integers,markerTag_injective.eq_iff]
512
lemma phase_exact (p : Phase) : (hasPhase a p).eval v≠0 ↔ c.head.phase=p := by
proof · 1 line
  simp [hasPhase,represent_int ha,cellRecord,integers,phaseTag_injective.eq_iff]
515
lemma core_exact : (isCore a).eval v≠0 ↔ c.isCore := by
proof · 1 line
  cases hp : c.skeleton.pos <;> simp [isCore,represent_int ha,cellRecord,integers,Cell.isCore,Cell.pos,hp,positionTag]
518
lemma palette_exact : (isPalette a).eval v≠0 ↔ c.isPalette := by
proof · 1 line
  cases hp : c.skeleton.pos <;> simp [isPalette,represent_int ha,cellRecord,integers,Cell.isPalette,Cell.pos,hp,positionTag]
521
lemma stop_exact : (isStop a).eval v≠0 ↔ c.pos=.stop := by
proof · 1 line
  cases hp : c.skeleton.pos <;> simp [isStop,represent_int ha,cellRecord,integers,Cell.pos,hp,positionTag]
524
lemma head_exact : (hasHead a).eval v≠0 ↔ Skeleton.IsHead c.skeleton := by
proof · 1 line
  simp only [hasHead,Formula.or_ne_zero,marker_exact ha,Skeleton.IsHead,Cell.marker]
527
lemma endpoint_exact : (endpoint a).eval v≠0 ↔ Cell.endpoint c.head.phase := by
proof · 1 line
  simp only [endpoint,Formula.any_ne_zero,List.map_cons,List.map_nil,List.mem_cons,List.not_mem_nil,or_false,exists_eq_or_imp,exists_eq_left,phase_exact ha,Cell.endpoint]
529
lemma admissible_exact : (admissible a).eval v≠0 ↔ c.skeleton.Admissible := by
proof · 1 line
  simp [admissible,first_exact ha,last_exact ha,marker_exact ha,Skeleton.Admissible,Cell.marker]
531
lemma leftExternal_exact : (leftExternal a).eval v≠0 ↔ c.skeleton.leftExternal := by
proof · 1 line
  simp only [leftExternal,Formula.or_ne_zero,Formula.and_ne_zero,Formula.neg_ne_zero,first_exact ha,marker_exact ha,Skeleton.leftExternal,Cell.marker]
534
lemma rightExternal_exact : (rightExternal a).eval v≠0 ↔ c.skeleton.rightExternal := by
proof · 1 line
  simp only [rightExternal,Formula.or_ne_zero,Formula.and_ne_zero,Formula.neg_ne_zero,last_exact ha,marker_exact ha,Skeleton.rightExternal,Cell.marker]
538
lemma acceptsLeft_exact : (acceptsLeft a b).eval v≠0 ↔ c.skeleton.acceptsLeft d.marker := by
proof · 7 lines
  change (if (hasMarker a .right).eval v=0 then (hasMarker b .left).eval v else
    (or (hasMarker b .full) (hasMarker b .right)).eval v)≠0 ↔ _
  by_cases h : c.skeleton.marker=.right
  · have H := (marker_exact ha .right).mpr h
    simp only [H,ite_false,ite_true,Formula.or_ne_zero,marker_exact hb,Skeleton.acceptsLeft,Cell.marker,h]
  · have H : (hasMarker a .right).eval v=0 := not_not.mp (mt (marker_exact ha .right).mp h)
    simp [H,marker_exact hb,Skeleton.acceptsLeft,Cell.marker,h]
547
lemma acceptsRight_exact : (acceptsRight a b).eval v≠0 ↔ c.skeleton.acceptsRight d.marker := by
proof · 7 lines
  change (if (hasMarker a .left).eval v=0 then (hasMarker b .right).eval v else
    (any [hasMarker b .left,hasMarker b .boundary,hasMarker b .full]).eval v)≠0 ↔ _
  by_cases h : c.skeleton.marker=.left
  · have H := (marker_exact ha .left).mpr h
    simp only [H,ite_false,ite_true,marker_exact hb,Skeleton.acceptsRight,Cell.marker,h,Formula.any_ne_zero,List.mem_cons,List.not_mem_nil,or_false,exists_eq_or_imp,exists_eq_left]
  · have H : (hasMarker a .left).eval v=0 := not_not.mp (mt (marker_exact ha .left).mp h)
    simp only [H,ite_false,ite_true,marker_exact hb,Skeleton.acceptsRight,Cell.marker,h]
555
lemma structural_exact (Q : Formula ι) (hQ : Q.eval v=q) :
    (structural a b Q).eval v≠0 ↔ Skeleton.Structural c.skeleton d.skeleton := by
proof · 6 lines
  simp only [structural,any_ne_zero,all_ne_zero,List.mem_cons,List.not_mem_nil,or_false,
    exists_eq_or_imp,exists_eq_left,forall_eq_or_imp,forall_eq,and_ne_zero,core_exact ha,core_exact hb,palette_exact ha,
    palette_exact hb,stop_exact ha,stop_exact hb,equal_ne_zero,Formula.constant_eval,Formula.add_eval,represent_int ha,
    represent_int hb,hQ,cellRecord,integers]
  cases hp : c.skeleton.pos <;> cases hr : d.skeleton.pos <;>
    simp [Cell.isCore,Cell.isPalette,hp,hr,positionIndex,Skeleton.Structural,Cell.pos]
563
lemma internal_exact : (internal a b).eval v≠0 ↔ Skeleton.Internal c.skeleton d.skeleton := by
proof · 17 lines
  have B : (Formula.branch (isStop a)
      (equal (.add (a.1 4) (.constant 1)) (b.1 4)) (equal (a.1 4) (b.1 4))).eval v≠0 ↔
      (if c.pos=.stop then c.metadata.block.val+1=d.metadata.block.val
        else c.metadata.block.val=d.metadata.block.val) := by
    change (if (isStop a).eval v=0 then (equal (a.1 4) (b.1 4)).eval v
      else (equal (.add (a.1 4) (.constant 1)) (b.1 4)).eval v)≠0 ↔ _
    by_cases hp : c.pos=.stop
    · have HS := (stop_exact ha).mpr hp
      simp [HS,hp,represent_int ha,represent_int hb,cellRecord,integers]
    · have HS : (isStop a).eval v=0 := not_not.mp (mt (stop_exact ha).mp hp)
      simp [HS,hp,represent_int ha,represent_int hb,cellRecord,integers]
  simp only [internal,Formula.all_ne_zero,List.mem_cons,List.not_mem_nil,or_false,
    forall_eq_or_imp,forall_eq,Formula.equal_ne_zero,B,Formula.neg_ne_zero]
  simp [represent_int ha,represent_int hb,represent_flag ha,represent_flag hb,cellRecord,integers,flags,
    boolean_injective.eq_iff,first_exact hb,last_exact ha,Skeleton.Internal,Cell.metadata,Cell.pos]
  simp only [Fin.ext_iff]
  rfl
581
lemma skeletonPair_exact (Q : Formula ι) (hQ : Q.eval v=q) :
    (skeletonPair a b Q).eval v≠0 ↔ Skeleton.Pair c.skeleton d.skeleton := by
proof · 2 lines
  simp only [skeletonPair,Formula.and_ne_zero,Formula.or_ne_zero,Formula.all_ne_zero,Formula.neg_ne_zero,List.mem_cons,List.not_mem_nil,or_false,forall_eq_or_imp,forall_eq,structural_exact ha hb Q hQ,rightExternal_exact ha,leftExternal_exact hb,
    acceptsRight_exact ha hb,acceptsLeft_exact hb ha,internal_exact ha hb,Skeleton.Pair,Cell.marker]
589
@[simp] lemma bit_false_iff (x : ZMod 2) : bit x=false ↔ x=0 := by
proof · 6 lines
  constructor
  · intro H
    have HH := congrArg unbit H
    rw [unbit_bit] at HH
    exact HH
  · rintro rfl;rfl
596
@[simp] lemma bit_true_iff (x : ZMod 2) : bit x=true ↔ x=1 := by
proof · 6 lines
  constructor
  · intro H
    have HH := congrArg unbit H
    rw [unbit_bit] at HH
    exact HH
  · rintro rfl;rfl
603
@[simp] lemma boolean_bit_zero_iff (x : ZMod 2) : boolean (bit x)=0 ↔ x=0 := by simp
604
@[simp] lemma boolean_bit_one_iff (x : ZMod 2) : boolean (bit x)=1 ↔ x=1 := by simp
605
lemma boolean_bit_add (x y : ZMod 2) :
    boolean (bit (x+y))=(boolean (bit x)+boolean (bit y))%2 := by
proof · 1 line
  fin_cases x <;> fin_cases y <;> decide
612
@[simp↓] lemma le_ne_zero (a b : Formula ι) (v : ι → ℕ) :
    (Formula.le a b).eval v≠0 ↔ a.eval v≤b.eval v := by simp [eval]
614
@[simp↓] lemma less_ne_zero (a b : Formula ι) (v : ι → ℕ) :
    (less a b).eval v≠0 ↔ a.eval v<b.eval v := by simp [less_eval]
623
def regZero (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  all [equal (a.2 5) (.constant 0),equal (a.2 6) (.constant 0),equal (a.2 7) (.constant 0)]
625
def headZero (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  all [equal (a.2 1) (.constant 0),equal (a.1 7) (.constant 0),hasPhase a .acquisition,regZero a]
627
def metaZero (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  and (equal (a.1 3) (.constant 0)) (equal (a.2 0) (.constant 0))
629
def dataZero (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  and (equal (a.2 2) (.constant 0)) (equal (a.2 3) (.constant 0))
631
def checkerIdle (a : Expr (ι:=ι)) : Formula ι := equal (a.1 5) (.constant 0)
632
def checkerPassed (a : Expr (ι:=ι)) : Formula ι := equal (a.1 5) (.constant 1)
633
def checkerHead (a : Expr (ι:=ι)) : Formula ι := equal (a.1 5) (.constant 2)
634
def headFree (a : Expr (ι:=ι)) : Formula ι := and (neg (hasHead a)) (neg (checkerHead a))
635
def coreLimit (a : Expr (ι:=ι)) (size : Formula ι) : Formula ι :=
body · 1 line
  implies (and (isCore a) (.le size (a.1 1))) (dataZero a)
637
def formats (a : Expr (ι:=ι)) (size count : Formula ι) : Formula ι := all
body · 10 lines
  [admissible a,
   implies (neg (isPalette a)) (checkerIdle a),
   coreLimit a size,
   implies (neg (isCore a)) (and (dataZero a) (equal (a.2 4) (.constant 0))),
   implies (hasMarker a .idle) (all [metaZero a,headZero a,equal (a.2 3) (.constant 0),equal (a.2 4) (.constant 0)]),
   implies (or (hasMarker a .left) (hasMarker a .right)) (headZero a),
   implies (hasMarker a .boundary) (all [equal (a.1 7) (.constant 0),hasPhase a .acquisition,regZero a]),
   implies (hasHead a) (implies (equal (a.2 0) (.constant 1)) (equal (a.2 1) (.constant 0))),
   implies (hasMarker a .full) (all [implies (hasPhase a .success) (equal (a.1 7) (.constant 0)),
     implies (neg (hasPhase a .success)) (less (a.1 7) count),implies (endpoint a) (last a)])]
649
def palettePair (a b : Expr (ι:=ι)) : Formula ι := any
body · 4 lines
  [and (checkerIdle a) (checkerIdle b),
   all [checkerPassed a,checkerPassed b,equal (a.1 6) (b.1 6)],
   all [checkerPassed a,checkerHead b,equal (a.1 6) (b.1 6)],
   and (checkerHead a) (checkerIdle b)]
654
def checkerPair (a b : Expr (ι:=ι)) : Formula ι :=
body · 2 lines
  and (implies (and (isPalette a) (isPalette b)) (palettePair a b))
    (implies (and (isPalette a) (isStop b)) (neg (checkerPassed a)))
657
def pair (a b : Expr (ι:=ι)) (Q : Formula ι) : Formula ι := and (skeletonPair a b Q) (checkerPair a b)
659
lemma reg_zero_iff (r : Reg) : r=0 ↔ r 0=0 ∧ r 1=0 ∧ r 2=0 := by
proof · 3 lines
  constructor
  · rintro rfl;simp
  · intro h;funext i;fin_cases i <;> simp_all
663
lemma data_zero_iff (f : Bool → ZMod 2) : f=0 ↔ f false=0 ∧ f true=0 := by
proof · 3 lines
  constructor
  · rintro rfl;simp
  · intro h;funext i;cases i <;> simp_all
667
lemma meta_zero_iff (m : Meta) : m=defaultMeta ↔ m.arity=0 ∧ m.zeroTask=false := by
proof · 9 lines
  constructor
  · rintro rfl;simp [defaultMeta]
  · rcases m with ⟨arity,task,block⟩;dsimp only
    rintro ⟨rfl,rfl⟩
    have H : block=0 := by
      apply Fin.ext
      have HB := block.isLt
      norm_num at HB ⊢
    subst block;rfl
677
lemma head_zero_iff {q : ℕ} [NeZero q] (h : Head q) : h=defaultHead ↔
    h.side=false ∧ h.clause=0 ∧ h.phase=.acquisition ∧ h.reg=0 := by
proof · 1 line
  cases h;simp [defaultHead,Head.mk.injEq]
685
lemma regZero_exact : (regZero a).eval v≠0 ↔ c.head.reg=0 := by
proof · 3 lines
  simp only [regZero,Formula.all_ne_zero,List.mem_cons,List.not_mem_nil,or_false,forall_eq_or_imp,forall_eq,
    Formula.equal_ne_zero,Formula.constant_eval]
  simp [represent_flag ha,cellRecord,flags,reg_zero_iff]
690
lemma dataZero_exact : (dataZero a).eval v≠0 ↔ c.data=0 := by
proof · 2 lines
  simp only [dataZero,Formula.and_ne_zero,Formula.equal_ne_zero,Formula.constant_eval]
  simp [represent_flag ha,cellRecord,flags,data_zero_iff]
694
lemma metaZero_exact : (metaZero a).eval v≠0 ↔ c.metadata=defaultMeta := by
proof · 2 lines
  simp only [metaZero,Formula.and_ne_zero,Formula.equal_ne_zero,Formula.constant_eval]
  simp [represent_int ha,represent_flag ha,cellRecord,integers,flags,meta_zero_iff]
697
lemma headZero_exact : (headZero a).eval v≠0 ↔ c.head=defaultHead := by
proof · 3 lines
  simp only [headZero,Formula.all_ne_zero,List.mem_cons,List.not_mem_nil,or_false,forall_eq_or_imp,forall_eq,
    Formula.equal_ne_zero,Formula.constant_eval,phase_exact ha,regZero_exact ha]
  simp [represent_int ha,represent_flag ha,cellRecord,integers,flags,head_zero_iff]
702
lemma checkerIdle_exact : (checkerIdle a).eval v≠0 ↔ c.checker=.idle := by
proof · 1 line
  cases hc : c.checker <;> simp [checkerIdle,represent_int ha,cellRecord,integers,hc,checkerTag]
705
lemma checkerPassed_exact : (checkerPassed a).eval v≠0 ↔ ∃ x,c.checker=.passed x := by
proof · 1 line
  cases hc : c.checker <;> simp [checkerPassed,represent_int ha,cellRecord,integers,hc,checkerTag]
708
lemma checkerHead_exact : (checkerHead a).eval v≠0 ↔ ∃ x,c.checker=.head x := by
proof · 1 line
  cases hc : c.checker <;> simp [checkerHead,represent_int ha,cellRecord,integers,hc,checkerTag]
711
lemma headFree_exact : (headFree a).eval v≠0 ↔ c.HeadFree := by
proof · 2 lines
  simp only [headFree,Formula.and_ne_zero,Formula.neg_ne_zero,head_exact ha,checkerHead_exact ha]
  simp [Cell.HeadFree]
715
lemma coreLimit_exact (size : Formula ι) (n : ℕ) (hn : size.eval v=n) :
    (coreLimit a size).eval v≠0 ↔ ∀p,c.pos=.core p → n≤p.val → c.data=0 := by
proof · 3 lines
  simp only [coreLimit,implies_ne_zero,Formula.and_ne_zero,core_exact ha,le_ne_zero,hn,dataZero_exact ha]
  cases hp : c.pos <;>
    simp [represent_int ha,cellRecord,integers,Cell.isCore,hp,positionIndex]
720
lemma formats_exact (S : Schedule q) (size count : Formula ι) (n : ℕ)
    (hn : size.eval v=n) (hc : count.eval v=(S.count c.metadata.task).val) :
    (formats a size count).eval v≠0 ↔ c.Format S n := by
proof · 7 lines
  simp only [formats,Formula.all_ne_zero,List.mem_cons,List.not_mem_nil,or_false,forall_eq_or_imp,forall_eq,
    implies_ne_zero,Formula.and_ne_zero,Formula.or_ne_zero,Formula.neg_ne_zero,
    admissible_exact ha,checkerIdle_exact ha,core_exact ha,palette_exact ha,coreLimit_exact ha size n hn,
    dataZero_exact ha,marker_exact ha,metaZero_exact ha,headZero_exact ha,head_exact ha,
    phase_exact ha,regZero_exact ha,endpoint_exact ha,last_exact ha,Formula.equal_ne_zero,
    Formula.constant_eval,less_ne_zero,hc]
  simp [represent_int ha,represent_flag ha,cellRecord,integers,flags,Cell.Format]
735
lemma check_pair_iff {q : ℕ} (x y : Palette.Check (Fin q)) : Palette.Pair x y ↔
    (x=.idle ∧ y=.idle) ∨ (∃z,x=.passed z ∧ y=.passed z) ∨
      (∃z,x=.passed z ∧ y=.head z) ∨ (∃z,x=.head z ∧ y=.idle) := by
proof · 7 lines
  constructor
  · intro h;cases h <;> simp
  · rintro (⟨rfl,rfl⟩ | ⟨z,rfl,rfl⟩ | ⟨z,rfl,rfl⟩ | ⟨z,rfl,rfl⟩)
    · exact .idle
    · exact .passed z
    · exact .next z
    · exact .rest z
746
def passedOwn (a : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  all [isPalette a,checkerPassed a,equal (a.1 1) (a.1 6)]
748
def duplicate (a b : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  all [isPalette a,isPalette b,equal (a.1 1) (b.1 1)]
750
def missing (a b : Expr (ι:=ι)) : Formula ι :=
body · 1 line
  all [isPalette a,checkerHead a,isStop b,neg (equal (a.1 1) (a.1 6))]
752
def semanticZero (a : Expr (ι:=ι)) (truth : Formula ι) : Formula ι :=
body · 4 lines
  and (hasMarker a .full) (any
    [and (any [hasPhase a .acquisition,hasPhase a .finish,hasPhase a .success]) (neg (regZero a)),
     and (hasPhase a .afterGate) (equal truth (.constant 0)),
     and (hasPhase a .success) (equal (a.2 0) (.constant 1))])
757
def unaryZero (a : Expr (ι:=ι)) (size count truth : Formula ι) : Formula ι :=
body · 1 line
  any [neg (formats a size count),semanticZero a truth,passedOwn a]
759
def binaryZero (a b : Expr (ι:=ι)) (Q : Formula ι) : Formula ι :=
body · 1 line
  any [neg (pair a b Q),duplicate a b,missing a b]
765
lemma palettePair_exact : (palettePair a b).eval v≠0 ↔ Palette.Pair c.checker d.checker := by
proof · 9 lines
  rw [check_pair_iff]
  simp only [palettePair,Formula.any_ne_zero,Formula.all_ne_zero,Formula.and_ne_zero,
    List.mem_cons,List.not_mem_nil,or_false,exists_eq_or_imp,exists_eq_left,forall_eq_or_imp,forall_eq,
    checkerIdle_exact ha,checkerIdle_exact hb,checkerPassed_exact ha,checkerPassed_exact hb,
    checkerHead_exact ha,checkerHead_exact hb,Formula.equal_ne_zero]
  cases hc : c.checker <;> cases hd : d.checker <;>
    simp [represent_int ha,represent_int hb,cellRecord,integers,hc,hd,checkerIndex]
  all_goals simp only [Fin.ext_iff]
  all_goals exact eq_comm
775
lemma checkerPair_exact : (checkerPair a b).eval v≠0 ↔ CheckerPair c d := by
proof · 4 lines
  have HL : (¬∃ x,c.checker=.passed x) ↔ Palette.LastOK c.checker := by
    cases c.checker <;> simp [Palette.LastOK]
  simp only [checkerPair,Formula.and_ne_zero,implies_ne_zero,Formula.neg_ne_zero,
    palette_exact ha,palette_exact hb,stop_exact hb,palettePair_exact ha hb,checkerPassed_exact ha,HL,CheckerPair]
780
lemma pair_exact (Q : Formula ι) (hQ : Q.eval v=q) :
    (pair a b Q).eval v≠0 ↔ Pair c d := by
proof · 1 line
  simp only [pair,Formula.and_ne_zero,skeletonPair_exact ha hb Q hQ,checkerPair_exact ha hb,Pair]
784
lemma passedOwn_exact : (passedOwn a).eval v≠0 ↔ ∃x,c.pos=.palette x ∧ c.checker=.passed x := by
proof · 6 lines
  simp only [passedOwn,Formula.all_ne_zero,List.mem_cons,List.not_mem_nil,or_false,forall_eq_or_imp,forall_eq,
    palette_exact ha,checkerPassed_exact ha,Formula.equal_ne_zero]
  cases hp : c.pos <;> cases hc : c.checker <;>
    simp [represent_int ha,cellRecord,integers,hp,hc,Cell.isPalette,checkerIndex,positionIndex]
  simp only [Fin.ext_iff]
  exact eq_comm
791
lemma duplicate_exact : (duplicate a b).eval v≠0 ↔ ∃x,c.pos=.palette x ∧ d.pos=.palette x := by
proof · 6 lines
  simp only [duplicate,Formula.all_ne_zero,List.mem_cons,List.not_mem_nil,or_false,forall_eq_or_imp,forall_eq,
    palette_exact ha,palette_exact hb,Formula.equal_ne_zero]
  cases hp : c.pos <;> cases hr : d.pos <;>
    simp [represent_int ha,represent_int hb,cellRecord,integers,hp,hr,Cell.isPalette,positionIndex]
  simp only [Fin.ext_iff]
  exact eq_comm
798
lemma missing_exact : (missing a b).eval v≠0 ↔
    ∃ x y,c.pos=.palette x ∧ c.checker=.head y ∧ d.pos=.stop ∧ x≠y := by
proof · 6 lines
  simp only [missing,Formula.all_ne_zero,List.mem_cons,List.not_mem_nil,or_false,forall_eq_or_imp,forall_eq,
    palette_exact ha,checkerHead_exact ha,stop_exact hb,Formula.neg_ne_zero,Formula.equal_ne_zero]
  cases hp : c.pos <;> cases hc : c.checker <;>
    simp [represent_int ha,cellRecord,integers,hp,hc,Cell.isPalette,checkerIndex,positionIndex]
  simp only [Fin.ext_iff]
  simp
807
lemma semanticZero_exact (S : Schedule q) (truth : Formula ι)
    (ht : truth.eval v=boolean ((S.clause c.metadata.task c.head.clause).truth c.head.reg)) :
    (semanticZero a truth).eval v≠0 ↔ SemanticZero S c := by
proof · 4 lines
  simp only [semanticZero,Formula.and_ne_zero,Formula.any_ne_zero,Formula.neg_ne_zero,
    List.mem_cons,List.not_mem_nil,or_false,exists_eq_or_imp,exists_eq_left,
    marker_exact ha,phase_exact ha,regZero_exact ha,Formula.equal_ne_zero,Formula.constant_eval,ht]
  simp [represent_flag ha,cellRecord,flags,SemanticZero]
815
lemma unaryZero_exact (S : Schedule q) (size count truth : Formula ι) (n : ℕ)
    (hn : size.eval v=n) (hc : count.eval v=(S.count c.metadata.task).val)
    (ht : truth.eval v=boolean ((S.clause c.metadata.task c.head.clause).truth c.head.reg)) :
    (unaryZero a size count truth).eval v≠0 ↔ UnaryZero S n c := by
proof · 3 lines
  simp only [unaryZero,Formula.any_ne_zero,Formula.neg_ne_zero,List.mem_cons,List.not_mem_nil,
    or_false,exists_eq_or_imp,exists_eq_left,formats_exact ha S size count n hn hc,
    semanticZero_exact ha S truth ht,passedOwn_exact ha,UnaryZero]
822
lemma binaryZero_exact (Q : Formula ι) (hQ : Q.eval v=q) :
    (binaryZero a b Q).eval v≠0 ↔ BinaryZero c d := by
proof · 3 lines
  simp only [binaryZero,Formula.any_ne_zero,Formula.neg_ne_zero,List.mem_cons,List.not_mem_nil,
    or_false,exists_eq_or_imp,exists_eq_left,pair_exact ha hb Q hQ,duplicate_exact ha hb,
    missing_exact ha hb,BinaryZero]