Datasets:
AI4M
/

text
stringlengths
0
3.34M
The measure of any set is zero under the null measure.
[STATEMENT] lemma bounded_abs_range[intro]: "bounded (range f :: real set) \<Longrightarrow> bounded (range (\<lambda>x. abs (f x)))" [PROOF STATE] proof (prove) goal (1 subgoal): 1. bounded (range f) \<Longrightarrow> bounded (range (\<lambda>x. \<bar>f x\<bar>)) [PROOF STEP] by (auto simp: bounded_iff)
[GOAL] ⊒ factors 0 = [] [PROOFSTEP] rw [factors] [GOAL] ⊒ factors 1 = [] [PROOFSTEP] rw [factors] [GOAL] n : β„• ⊒ βˆ€ {p : β„•}, p ∈ factors n β†’ Prime p [PROOFSTEP] match n with | 0 => simp | 1 => simp | k + 2 => intro p h let m := minFac (k + 2) have : (k + 2) / m < (k + 2) := factors_lemma have h₁ : p = m ∨ p ∈ factors ((k + 2) / m) := List.mem_cons.1 (by rwa [factors] at h ) exact Or.casesOn h₁ (fun hβ‚‚ => hβ‚‚.symm β–Έ minFac_prime (by simp)) prime_of_mem_factors [GOAL] n : β„• ⊒ βˆ€ {p : β„•}, p ∈ factors 0 β†’ Prime p [PROOFSTEP] simp [GOAL] n : β„• ⊒ βˆ€ {p : β„•}, p ∈ factors 1 β†’ Prime p [PROOFSTEP] simp [GOAL] n k : β„• ⊒ βˆ€ {p : β„•}, p ∈ factors (k + 2) β†’ Prime p [PROOFSTEP] intro p h [GOAL] n k p : β„• h : p ∈ factors (k + 2) ⊒ Prime p [PROOFSTEP] let m := minFac (k + 2) [GOAL] n k p : β„• h : p ∈ factors (k + 2) m : β„• := minFac (k + 2) ⊒ Prime p [PROOFSTEP] have : (k + 2) / m < (k + 2) := factors_lemma [GOAL] n k p : β„• h : p ∈ factors (k + 2) m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 ⊒ Prime p [PROOFSTEP] have h₁ : p = m ∨ p ∈ factors ((k + 2) / m) := List.mem_cons.1 (by rwa [factors] at h ) [GOAL] n k p : β„• h : p ∈ factors (k + 2) m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 ⊒ p ∈ m :: factors ((k + 2) / m) [PROOFSTEP] rwa [factors] at h [GOAL] n k p : β„• h : p ∈ factors (k + 2) m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 h₁ : p = m ∨ p ∈ factors ((k + 2) / m) ⊒ Prime p [PROOFSTEP] exact Or.casesOn h₁ (fun hβ‚‚ => hβ‚‚.symm β–Έ minFac_prime (by simp)) prime_of_mem_factors [GOAL] n k p : β„• h : p ∈ factors (k + 2) m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 h₁ : p = m ∨ p ∈ factors ((k + 2) / m) hβ‚‚ : p = m ⊒ k + 2 β‰  1 [PROOFSTEP] simp [GOAL] ⊒ 0 β‰  0 β†’ List.prod (factors 0) = 0 [PROOFSTEP] simp [GOAL] ⊒ 1 β‰  0 β†’ List.prod (factors 1) = 1 [PROOFSTEP] simp [GOAL] k : β„• x✝ : k + 2 β‰  0 m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 ⊒ List.prod (factors (k + 2)) = k + 2 [PROOFSTEP] have h₁ : (k + 2) / m β‰  0 := fun h => by have : (k + 2) = 0 * m := (Nat.div_eq_iff_eq_mul_left (minFac_pos _) (minFac_dvd _)).1 h rw [zero_mul] at this ; exact (show k + 2 β‰  0 by simp) this [GOAL] k : β„• x✝ : k + 2 β‰  0 m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 h : (k + 2) / m = 0 ⊒ False [PROOFSTEP] have : (k + 2) = 0 * m := (Nat.div_eq_iff_eq_mul_left (minFac_pos _) (minFac_dvd _)).1 h [GOAL] k : β„• x✝ : k + 2 β‰  0 m : β„• := minFac (k + 2) this✝ : (k + 2) / m < k + 2 h : (k + 2) / m = 0 this : k + 2 = 0 * m ⊒ False [PROOFSTEP] rw [zero_mul] at this [GOAL] k : β„• x✝ : k + 2 β‰  0 m : β„• := minFac (k + 2) this✝ : (k + 2) / m < k + 2 h : (k + 2) / m = 0 this : k + 2 = 0 ⊒ False [PROOFSTEP] exact (show k + 2 β‰  0 by simp) this [GOAL] k : β„• x✝ : k + 2 β‰  0 m : β„• := minFac (k + 2) this✝ : (k + 2) / m < k + 2 h : (k + 2) / m = 0 this : k + 2 = 0 ⊒ k + 2 β‰  0 [PROOFSTEP] simp [GOAL] k : β„• x✝ : k + 2 β‰  0 m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 h₁ : (k + 2) / m β‰  0 ⊒ List.prod (factors (k + 2)) = k + 2 [PROOFSTEP] rw [factors, List.prod_cons, prod_factors h₁, Nat.mul_div_cancel' (minFac_dvd _)] [GOAL] p : β„• hp : Prime p ⊒ factors p = [p] [PROOFSTEP] have : p = p - 2 + 2 := (tsub_eq_iff_eq_add_of_le hp.two_le).mp rfl [GOAL] p : β„• hp : Prime p this : p = p - 2 + 2 ⊒ factors p = [p] [PROOFSTEP] rw [this, Nat.factors] [GOAL] p : β„• hp : Prime p this : p = p - 2 + 2 ⊒ (let m := minFac (p - 2 + 2); let_fun this := (_ : (p - 2 + 2) / minFac (p - 2 + 2) < p - 2 + 2); m :: factors ((p - 2 + 2) / m)) = [p - 2 + 2] [PROOFSTEP] simp only [Eq.symm this] [GOAL] p : β„• hp : Prime p this : p = p - 2 + 2 ⊒ minFac p :: factors (p / minFac p) = [p] [PROOFSTEP] have : Nat.minFac p = p := (Nat.prime_def_minFac.mp hp).2 [GOAL] p : β„• hp : Prime p this✝ : p = p - 2 + 2 this : minFac p = p ⊒ minFac p :: factors (p / minFac p) = [p] [PROOFSTEP] simp only [this, Nat.factors, Nat.div_self (Nat.Prime.pos hp)] [GOAL] n : β„• ⊒ βˆ€ {a : β„•}, (βˆ€ (p : β„•), Prime p β†’ p ∣ n β†’ a ≀ p) β†’ List.Chain (fun x x_1 => x ≀ x_1) a (factors n) [PROOFSTEP] match n with | 0 => simp | 1 => simp | k + 2 => intro a h let m := minFac (k + 2) have : (k + 2) / m < (k + 2) := factors_lemma rw [factors] refine' List.Chain.cons ((le_minFac.2 h).resolve_left (by simp)) (factors_chain _) exact fun p pp d => minFac_le_of_dvd pp.two_le (d.trans <| div_dvd_of_dvd <| minFac_dvd _) [GOAL] n : β„• ⊒ βˆ€ {a : β„•}, (βˆ€ (p : β„•), Prime p β†’ p ∣ 0 β†’ a ≀ p) β†’ List.Chain (fun x x_1 => x ≀ x_1) a (factors 0) [PROOFSTEP] simp [GOAL] n : β„• ⊒ βˆ€ {a : β„•}, (βˆ€ (p : β„•), Prime p β†’ p ∣ 1 β†’ a ≀ p) β†’ List.Chain (fun x x_1 => x ≀ x_1) a (factors 1) [PROOFSTEP] simp [GOAL] n k : β„• ⊒ βˆ€ {a : β„•}, (βˆ€ (p : β„•), Prime p β†’ p ∣ k + 2 β†’ a ≀ p) β†’ List.Chain (fun x x_1 => x ≀ x_1) a (factors (k + 2)) [PROOFSTEP] intro a h [GOAL] n k a : β„• h : βˆ€ (p : β„•), Prime p β†’ p ∣ k + 2 β†’ a ≀ p ⊒ List.Chain (fun x x_1 => x ≀ x_1) a (factors (k + 2)) [PROOFSTEP] let m := minFac (k + 2) [GOAL] n k a : β„• h : βˆ€ (p : β„•), Prime p β†’ p ∣ k + 2 β†’ a ≀ p m : β„• := minFac (k + 2) ⊒ List.Chain (fun x x_1 => x ≀ x_1) a (factors (k + 2)) [PROOFSTEP] have : (k + 2) / m < (k + 2) := factors_lemma [GOAL] n k a : β„• h : βˆ€ (p : β„•), Prime p β†’ p ∣ k + 2 β†’ a ≀ p m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 ⊒ List.Chain (fun x x_1 => x ≀ x_1) a (factors (k + 2)) [PROOFSTEP] rw [factors] [GOAL] n k a : β„• h : βˆ€ (p : β„•), Prime p β†’ p ∣ k + 2 β†’ a ≀ p m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 ⊒ List.Chain (fun x x_1 => x ≀ x_1) a (let m := minFac (k + 2); let_fun this := (_ : (k + 2) / minFac (k + 2) < k + 2); m :: factors ((k + 2) / m)) [PROOFSTEP] refine' List.Chain.cons ((le_minFac.2 h).resolve_left (by simp)) (factors_chain _) [GOAL] n k a : β„• h : βˆ€ (p : β„•), Prime p β†’ p ∣ k + 2 β†’ a ≀ p m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 ⊒ Β¬k + 2 = 1 [PROOFSTEP] simp [GOAL] n k a : β„• h : βˆ€ (p : β„•), Prime p β†’ p ∣ k + 2 β†’ a ≀ p m : β„• := minFac (k + 2) this : (k + 2) / m < k + 2 ⊒ βˆ€ (p : β„•), Prime p β†’ p ∣ (k + 2) / minFac (k + 2) β†’ minFac (k + 2) ≀ p [PROOFSTEP] exact fun p pp d => minFac_le_of_dvd pp.two_le (d.trans <| div_dvd_of_dvd <| minFac_dvd _) [GOAL] n : β„• ⊒ factors (n + 2) = minFac (n + 2) :: factors ((n + 2) / minFac (n + 2)) [PROOFSTEP] rw [factors] [GOAL] n : β„• ⊒ factors n = [] ↔ n = 0 ∨ n = 1 [PROOFSTEP] constructor [GOAL] case mp n : β„• ⊒ factors n = [] β†’ n = 0 ∨ n = 1 [PROOFSTEP] intro h [GOAL] case mpr n : β„• ⊒ n = 0 ∨ n = 1 β†’ factors n = [] [PROOFSTEP] intro h [GOAL] case mp n : β„• h : factors n = [] ⊒ n = 0 ∨ n = 1 [PROOFSTEP] rcases n with (_ | _ | n) [GOAL] case mp.zero h : factors zero = [] ⊒ zero = 0 ∨ zero = 1 [PROOFSTEP] exact Or.inl rfl [GOAL] case mp.succ.zero h : factors (succ zero) = [] ⊒ succ zero = 0 ∨ succ zero = 1 [PROOFSTEP] exact Or.inr rfl [GOAL] case mp.succ.succ n : β„• h : factors (succ (succ n)) = [] ⊒ succ (succ n) = 0 ∨ succ (succ n) = 1 [PROOFSTEP] rw [factors] at h [GOAL] case mp.succ.succ n : β„• h : (let m := minFac (n + 2); let_fun this := (_ : (n + 2) / minFac (n + 2) < n + 2); m :: factors ((n + 2) / m)) = [] ⊒ succ (succ n) = 0 ∨ succ (succ n) = 1 [PROOFSTEP] injection h [GOAL] case mpr n : β„• h : n = 0 ∨ n = 1 ⊒ factors n = [] [PROOFSTEP] rcases h with (rfl | rfl) [GOAL] case mpr.inl ⊒ factors 0 = [] [PROOFSTEP] exact factors_zero [GOAL] case mpr.inr ⊒ factors 1 = [] [PROOFSTEP] exact factors_one [GOAL] a b : β„• ha : a β‰  0 hb : b β‰  0 h : factors a ~ factors b ⊒ a = b [PROOFSTEP] simpa [prod_factors ha, prod_factors hb] using List.Perm.prod_eq h [GOAL] n p : β„• h : p ∈ factors n ⊒ p ∣ n [PROOFSTEP] rcases n.eq_zero_or_pos with (rfl | hn) [GOAL] case inl p : β„• h : p ∈ factors 0 ⊒ p ∣ 0 [PROOFSTEP] exact dvd_zero p [GOAL] case inr n p : β„• h : p ∈ factors n hn : n > 0 ⊒ p ∣ n [PROOFSTEP] rwa [← mem_factors_iff_dvd hn.ne' (prime_of_mem_factors h)] [GOAL] n p : β„• h : p ∈ factors n ⊒ p ≀ n [PROOFSTEP] rcases n.eq_zero_or_pos with (rfl | hn) [GOAL] case inl p : β„• h : p ∈ factors 0 ⊒ p ≀ 0 [PROOFSTEP] rw [factors_zero] at h [GOAL] case inl p : β„• h : p ∈ [] ⊒ p ≀ 0 [PROOFSTEP] cases h [GOAL] case inr n p : β„• h : p ∈ factors n hn : n > 0 ⊒ p ≀ n [PROOFSTEP] exact le_of_dvd hn (dvd_of_mem_factors h) [GOAL] n : β„• l : List β„• h₁ : prod l = n hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p ⊒ l ~ factors n [PROOFSTEP] refine' perm_of_prod_eq_prod _ _ _ [GOAL] case refine'_1 n : β„• l : List β„• h₁ : prod l = n hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p ⊒ prod l = prod (factors n) [PROOFSTEP] rw [h₁] [GOAL] case refine'_1 n : β„• l : List β„• h₁ : prod l = n hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p ⊒ n = prod (factors n) [PROOFSTEP] refine' (prod_factors _).symm [GOAL] case refine'_1 n : β„• l : List β„• h₁ : prod l = n hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p ⊒ n β‰  0 [PROOFSTEP] rintro rfl [GOAL] case refine'_1 l : List β„• hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p h₁ : prod l = 0 ⊒ False [PROOFSTEP] rw [prod_eq_zero_iff] at h₁ [GOAL] case refine'_1 l : List β„• hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p h₁ : 0 ∈ l ⊒ False [PROOFSTEP] exact Prime.ne_zero (hβ‚‚ 0 h₁) rfl [GOAL] case refine'_2 n : β„• l : List β„• h₁ : prod l = n hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p ⊒ βˆ€ (p : β„•), p ∈ l β†’ _root_.Prime p [PROOFSTEP] simp_rw [← prime_iff] [GOAL] case refine'_2 n : β„• l : List β„• h₁ : prod l = n hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p ⊒ βˆ€ (p : β„•), p ∈ l β†’ Prime p [PROOFSTEP] exact hβ‚‚ [GOAL] case refine'_3 n : β„• l : List β„• h₁ : prod l = n hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p ⊒ βˆ€ (p : β„•), p ∈ factors n β†’ _root_.Prime p [PROOFSTEP] simp_rw [← prime_iff] [GOAL] case refine'_3 n : β„• l : List β„• h₁ : prod l = n hβ‚‚ : βˆ€ (p : β„•), p ∈ l β†’ Prime p ⊒ βˆ€ (p : β„•), p ∈ factors n β†’ Prime p [PROOFSTEP] exact fun p => prime_of_mem_factors [GOAL] p : β„• hp : Prime p n : β„• ⊒ factors (p ^ n) = replicate n p [PROOFSTEP] symm [GOAL] p : β„• hp : Prime p n : β„• ⊒ replicate n p = factors (p ^ n) [PROOFSTEP] rw [← List.replicate_perm] [GOAL] p : β„• hp : Prime p n : β„• ⊒ replicate n p ~ factors (p ^ n) [PROOFSTEP] apply Nat.factors_unique (List.prod_replicate n p) [GOAL] p : β„• hp : Prime p n : β„• ⊒ βˆ€ (p_1 : β„•), p_1 ∈ replicate n p β†’ Prime p_1 [PROOFSTEP] intro q hq [GOAL] p : β„• hp : Prime p n q : β„• hq : q ∈ replicate n p ⊒ Prime q [PROOFSTEP] rwa [eq_of_mem_replicate hq] [GOAL] n p : β„• hpos : n β‰  0 h : βˆ€ {d : β„•}, Prime d β†’ d ∣ n β†’ d = p ⊒ n = p ^ length (factors n) [PROOFSTEP] set k := n.factors.length [GOAL] n p : β„• hpos : n β‰  0 h : βˆ€ {d : β„•}, Prime d β†’ d ∣ n β†’ d = p k : β„• := length (factors n) ⊒ n = p ^ k [PROOFSTEP] rw [← prod_factors hpos, ← prod_replicate k p, eq_replicate_of_mem fun d hd => h (prime_of_mem_factors hd) (dvd_of_mem_factors hd)] [GOAL] a b : β„• ha : a β‰  0 hb : b β‰  0 ⊒ factors (a * b) ~ factors a ++ factors b [PROOFSTEP] refine' (factors_unique _ _).symm [GOAL] case refine'_1 a b : β„• ha : a β‰  0 hb : b β‰  0 ⊒ prod (factors a ++ factors b) = a * b [PROOFSTEP] rw [List.prod_append, prod_factors ha, prod_factors hb] [GOAL] case refine'_2 a b : β„• ha : a β‰  0 hb : b β‰  0 ⊒ βˆ€ (p : β„•), p ∈ factors a ++ factors b β†’ Prime p [PROOFSTEP] intro p hp [GOAL] case refine'_2 a b : β„• ha : a β‰  0 hb : b β‰  0 p : β„• hp : p ∈ factors a ++ factors b ⊒ Prime p [PROOFSTEP] rw [List.mem_append] at hp [GOAL] case refine'_2 a b : β„• ha : a β‰  0 hb : b β‰  0 p : β„• hp : p ∈ factors a ∨ p ∈ factors b ⊒ Prime p [PROOFSTEP] cases' hp with hp' hp' [GOAL] case refine'_2.inl a b : β„• ha : a β‰  0 hb : b β‰  0 p : β„• hp' : p ∈ factors a ⊒ Prime p [PROOFSTEP] exact prime_of_mem_factors hp' [GOAL] case refine'_2.inr a b : β„• ha : a β‰  0 hb : b β‰  0 p : β„• hp' : p ∈ factors b ⊒ Prime p [PROOFSTEP] exact prime_of_mem_factors hp' [GOAL] a b : β„• hab : coprime a b ⊒ factors (a * b) ~ factors a ++ factors b [PROOFSTEP] rcases a.eq_zero_or_pos with (rfl | ha) [GOAL] case inl b : β„• hab : coprime 0 b ⊒ factors (0 * b) ~ factors 0 ++ factors b [PROOFSTEP] simp [(coprime_zero_left _).mp hab] [GOAL] case inr a b : β„• hab : coprime a b ha : a > 0 ⊒ factors (a * b) ~ factors a ++ factors b [PROOFSTEP] rcases b.eq_zero_or_pos with (rfl | hb) [GOAL] case inr.inl a : β„• ha : a > 0 hab : coprime a 0 ⊒ factors (a * 0) ~ factors a ++ factors 0 [PROOFSTEP] simp [(coprime_zero_right _).mp hab] [GOAL] case inr.inr a b : β„• hab : coprime a b ha : a > 0 hb : b > 0 ⊒ factors (a * b) ~ factors a ++ factors b [PROOFSTEP] exact perm_factors_mul ha.ne' hb.ne' [GOAL] n k : β„• h : k β‰  0 ⊒ factors n <+ factors (n * k) [PROOFSTEP] cases' n with hn [GOAL] case zero k : β„• h : k β‰  0 ⊒ factors zero <+ factors (zero * k) [PROOFSTEP] simp [zero_mul] [GOAL] case succ k : β„• h : k β‰  0 hn : β„• ⊒ factors (succ hn) <+ factors (succ hn * k) [PROOFSTEP] apply sublist_of_subperm_of_sorted _ (factors_sorted _) (factors_sorted _) [GOAL] k : β„• h : k β‰  0 hn : β„• ⊒ factors (succ hn) <+~ factors (succ hn * k) [PROOFSTEP] simp [(perm_factors_mul (Nat.succ_ne_zero _) h).subperm_left] [GOAL] k : β„• h : k β‰  0 hn : β„• ⊒ factors (succ hn) <+~ factors (succ hn) ++ factors k [PROOFSTEP] exact (sublist_append_left _ _).subperm [GOAL] n k : β„• h : n ∣ k h' : k β‰  0 ⊒ factors n <+ factors k [PROOFSTEP] obtain ⟨a, rfl⟩ := h [GOAL] case intro n a : β„• h' : n * a β‰  0 ⊒ factors n <+ factors (n * a) [PROOFSTEP] exact factors_sublist_right (right_ne_zero_of_mul h') [GOAL] a b : β„• ha : a β‰  0 h : factors a <+~ factors b ⊒ a ∣ b [PROOFSTEP] rcases b.eq_zero_or_pos with (rfl | hb) [GOAL] case inl a : β„• ha : a β‰  0 h : factors a <+~ factors 0 ⊒ a ∣ 0 [PROOFSTEP] exact dvd_zero _ [GOAL] case inr a b : β„• ha : a β‰  0 h : factors a <+~ factors b hb : b > 0 ⊒ a ∣ b [PROOFSTEP] rcases a with (_ | _ | a) [GOAL] case inr.zero b : β„• hb : b > 0 ha : zero β‰  0 h : factors zero <+~ factors b ⊒ zero ∣ b [PROOFSTEP] exact (ha rfl).elim [GOAL] case inr.succ.zero b : β„• hb : b > 0 ha : succ zero β‰  0 h : factors (succ zero) <+~ factors b ⊒ succ zero ∣ b [PROOFSTEP] exact one_dvd _ --Porting note: previous proof --use (b.factors.diff a.succ.succ.factors).prod [GOAL] case inr.succ.succ b : β„• hb : b > 0 a : β„• ha : succ (succ a) β‰  0 h : factors (succ (succ a)) <+~ factors b ⊒ succ (succ a) ∣ b [PROOFSTEP] use(@List.diff _ instBEq b.factors a.succ.succ.factors).prod [GOAL] case h b : β„• hb : b > 0 a : β„• ha : succ (succ a) β‰  0 h : factors (succ (succ a)) <+~ factors b ⊒ b = succ (succ a) * prod (List.diff (factors b) (factors (succ (succ a)))) [PROOFSTEP] nth_rw 1 [← Nat.prod_factors ha] [GOAL] case h b : β„• hb : b > 0 a : β„• ha : succ (succ a) β‰  0 h : factors (succ (succ a)) <+~ factors b ⊒ b = prod (factors (succ (succ a))) * prod (List.diff (factors b) (factors (succ (succ a)))) [PROOFSTEP] rw [← List.prod_append, List.Perm.prod_eq <| List.subperm_append_diff_self_of_count_le <| List.subperm_ext_iff.mp h, Nat.prod_factors hb.ne'] [GOAL] a b : β„• ha : a β‰  0 hb : b β‰  0 p : β„• ⊒ p ∈ factors (a * b) ↔ p ∈ factors a ∨ p ∈ factors b [PROOFSTEP] rw [mem_factors (mul_ne_zero ha hb), mem_factors ha, mem_factors hb, ← and_or_left] [GOAL] a b : β„• ha : a β‰  0 hb : b β‰  0 p : β„• ⊒ Prime p ∧ p ∣ a * b ↔ Prime p ∧ (p ∣ a ∨ p ∣ b) [PROOFSTEP] simpa only [and_congr_right_iff] using Prime.dvd_mul [GOAL] a b : β„• hab : coprime a b ⊒ List.Disjoint (factors a) (factors b) [PROOFSTEP] intro q hqa hqb [GOAL] a b : β„• hab : coprime a b q : β„• hqa : q ∈ factors a hqb : q ∈ factors b ⊒ False [PROOFSTEP] apply not_prime_one [GOAL] a b : β„• hab : coprime a b q : β„• hqa : q ∈ factors a hqb : q ∈ factors b ⊒ Prime 1 [PROOFSTEP] rw [← eq_one_of_dvd_coprimes hab (dvd_of_mem_factors hqa) (dvd_of_mem_factors hqb)] [GOAL] a b : β„• hab : coprime a b q : β„• hqa : q ∈ factors a hqb : q ∈ factors b ⊒ Prime q [PROOFSTEP] exact prime_of_mem_factors hqa [GOAL] a b : β„• hab : coprime a b p : β„• ⊒ p ∈ factors (a * b) ↔ p ∈ factors a βˆͺ factors b [PROOFSTEP] rcases a.eq_zero_or_pos with (rfl | ha) [GOAL] case inl b p : β„• hab : coprime 0 b ⊒ p ∈ factors (0 * b) ↔ p ∈ factors 0 βˆͺ factors b [PROOFSTEP] simp [(coprime_zero_left _).mp hab] [GOAL] case inr a b : β„• hab : coprime a b p : β„• ha : a > 0 ⊒ p ∈ factors (a * b) ↔ p ∈ factors a βˆͺ factors b [PROOFSTEP] rcases b.eq_zero_or_pos with (rfl | hb) [GOAL] case inr.inl a p : β„• ha : a > 0 hab : coprime a 0 ⊒ p ∈ factors (a * 0) ↔ p ∈ factors a βˆͺ factors 0 [PROOFSTEP] simp [(coprime_zero_right _).mp hab] [GOAL] case inr.inr a b : β„• hab : coprime a b p : β„• ha : a > 0 hb : b > 0 ⊒ p ∈ factors (a * b) ↔ p ∈ factors a βˆͺ factors b [PROOFSTEP] rw [mem_factors_mul ha.ne' hb.ne', List.mem_union_iff] [GOAL] p a b : β„• hpa : p ∈ factors a hb : b β‰  0 ⊒ p ∈ factors (a * b) [PROOFSTEP] rcases eq_or_ne a 0 with (rfl | ha) [GOAL] case inl p b : β„• hb : b β‰  0 hpa : p ∈ factors 0 ⊒ p ∈ factors (0 * b) [PROOFSTEP] simp at hpa [GOAL] case inr p a b : β„• hpa : p ∈ factors a hb : b β‰  0 ha : a β‰  0 ⊒ p ∈ factors (a * b) [PROOFSTEP] apply (mem_factors_mul ha hb).2 (Or.inl hpa) [GOAL] p a b : β„• hpb : p ∈ factors b ha : a β‰  0 ⊒ p ∈ factors (a * b) [PROOFSTEP] rw [mul_comm] [GOAL] p a b : β„• hpb : p ∈ factors b ha : a β‰  0 ⊒ p ∈ factors (b * a) [PROOFSTEP] exact mem_factors_mul_left hpb ha
[GOAL] ⊒ ConcreteCategory CompleteLatCat [PROOFSTEP] dsimp [CompleteLatCat] [GOAL] ⊒ ConcreteCategory (Bundled CompleteLattice) [PROOFSTEP] infer_instance [GOAL] Ξ± Ξ² : CompleteLatCat e : ↑α ≃o ↑β ⊒ { tosInfHom := { toFun := ↑e, map_sInf' := (_ : βˆ€ (s : Set ↑α), ↑e (sInf s) = sInf (↑e '' s)) }, map_sSup' := (_ : βˆ€ (s : Set ↑α), ↑e (sSup s) = sSup (↑e '' s)) } ≫ { tosInfHom := { toFun := ↑(OrderIso.symm e), map_sInf' := (_ : βˆ€ (s : Set ↑β), ↑(OrderIso.symm e) (sInf s) = sInf (↑(OrderIso.symm e) '' s)) }, map_sSup' := (_ : βˆ€ (s : Set ↑β), ↑(OrderIso.symm e) (sSup s) = sSup (↑(OrderIso.symm e) '' s)) } = πŸ™ Ξ± [PROOFSTEP] ext [GOAL] case w Ξ± Ξ² : CompleteLatCat e : ↑α ≃o ↑β x✝ : (forget CompleteLatCat).obj Ξ± ⊒ ↑({ tosInfHom := { toFun := ↑e, map_sInf' := (_ : βˆ€ (s : Set ↑α), ↑e (sInf s) = sInf (↑e '' s)) }, map_sSup' := (_ : βˆ€ (s : Set ↑α), ↑e (sSup s) = sSup (↑e '' s)) } ≫ { tosInfHom := { toFun := ↑(OrderIso.symm e), map_sInf' := (_ : βˆ€ (s : Set ↑β), ↑(OrderIso.symm e) (sInf s) = sInf (↑(OrderIso.symm e) '' s)) }, map_sSup' := (_ : βˆ€ (s : Set ↑β), ↑(OrderIso.symm e) (sSup s) = sSup (↑(OrderIso.symm e) '' s)) }) x✝ = ↑(πŸ™ Ξ±) x✝ [PROOFSTEP] exact e.symm_apply_apply _ [GOAL] Ξ± Ξ² : CompleteLatCat e : ↑α ≃o ↑β ⊒ { tosInfHom := { toFun := ↑(OrderIso.symm e), map_sInf' := (_ : βˆ€ (s : Set ↑β), ↑(OrderIso.symm e) (sInf s) = sInf (↑(OrderIso.symm e) '' s)) }, map_sSup' := (_ : βˆ€ (s : Set ↑β), ↑(OrderIso.symm e) (sSup s) = sSup (↑(OrderIso.symm e) '' s)) } ≫ { tosInfHom := { toFun := ↑e, map_sInf' := (_ : βˆ€ (s : Set ↑α), ↑e (sInf s) = sInf (↑e '' s)) }, map_sSup' := (_ : βˆ€ (s : Set ↑α), ↑e (sSup s) = sSup (↑e '' s)) } = πŸ™ Ξ² [PROOFSTEP] ext [GOAL] case w Ξ± Ξ² : CompleteLatCat e : ↑α ≃o ↑β x✝ : (forget CompleteLatCat).obj Ξ² ⊒ ↑({ tosInfHom := { toFun := ↑(OrderIso.symm e), map_sInf' := (_ : βˆ€ (s : Set ↑β), ↑(OrderIso.symm e) (sInf s) = sInf (↑(OrderIso.symm e) '' s)) }, map_sSup' := (_ : βˆ€ (s : Set ↑β), ↑(OrderIso.symm e) (sSup s) = sSup (↑(OrderIso.symm e) '' s)) } ≫ { tosInfHom := { toFun := ↑e, map_sInf' := (_ : βˆ€ (s : Set ↑α), ↑e (sInf s) = sInf (↑e '' s)) }, map_sSup' := (_ : βˆ€ (s : Set ↑α), ↑e (sSup s) = sSup (↑e '' s)) }) x✝ = ↑(πŸ™ Ξ²) x✝ [PROOFSTEP] exact e.apply_symm_apply _
------------------------------------------------------------------------------ -- The alternating bit protocol (ABP) is correct ------------------------------------------------------------------------------ {-# OPTIONS --exact-split #-} {-# OPTIONS --no-sized-types #-} {-# OPTIONS --no-universe-polymorphism #-} {-# OPTIONS --without-K #-} -- This module proves the correctness of the ABP following the -- formalization in Dybjer and Sander (1989). -- N.B This module does not contain combined proofs, but it imports -- modules which contain combined proofs. module FOTC.Program.ABP.CorrectnessProofATP where open import FOTC.Base open import FOTC.Base.List open import FOTC.Data.Bool open import FOTC.Data.Bool.PropertiesATP using ( not-Bool ) open import FOTC.Data.Stream.Type open import FOTC.Data.Stream.Equality.PropertiesATP open import FOTC.Program.ABP.ABP open import FOTC.Program.ABP.Lemma1ATP open import FOTC.Program.ABP.Lemma2ATP open import FOTC.Program.ABP.Fair.Type open import FOTC.Program.ABP.Terms open import FOTC.Relation.Binary.Bisimilarity.Type ------------------------------------------------------------------------------ -- Main theorem. abpCorrect : βˆ€ {b os₁ osβ‚‚ is} β†’ Bit b β†’ Fair os₁ β†’ Fair osβ‚‚ β†’ Stream is β†’ is β‰ˆ abpTransfer b os₁ osβ‚‚ is abpCorrect {b} {os₁} {osβ‚‚} {is} Bb Fos₁ Fosβ‚‚ Sis = β‰ˆ-coind B h₁ hβ‚‚ where postulate h₁ : βˆ€ {ks ls} β†’ B ks ls β†’ βˆƒ[ k' ] βˆƒ[ ks' ] βˆƒ[ ls' ] ks ≑ k' ∷ ks' ∧ ls ≑ k' ∷ ls' ∧ B ks' ls' {-# ATP prove h₁ lemma₁ lemmaβ‚‚ not-Bool #-} postulate hβ‚‚ : B is (abpTransfer b os₁ osβ‚‚ is) {-# ATP prove hβ‚‚ #-} ------------------------------------------------------------------------------ -- abpTransfer produces a Stream. postulate abpTransfer-Stream : βˆ€ {b os₁ osβ‚‚ is} β†’ Bit b β†’ Fair os₁ β†’ Fair osβ‚‚ β†’ Stream is β†’ Stream (abpTransfer b os₁ osβ‚‚ is) {-# ATP prove abpTransfer-Stream β‰ˆβ†’Streamβ‚‚ abpCorrect #-} ------------------------------------------------------------------------------ -- References -- -- Dybjer, Peter and Sander, Herbert P. (1989). A Functional -- Programming Approach to the Specification and Verification of -- Concurrent Systems. Formal Aspects of Computing 1, pp. 303–319.
import M4R.Algebra.Ring.Ideal namespace M4R namespace Ideal def subtype_subset [Ring Ξ±] {S : Set (Ideal Ξ±)} : S β†’ S β†’ Prop := fun (x y : S) => x.val βŠ† y.val local infix:50 " ≀ " => subtype_subset def of_subtype_set {s : Set Ξ±} (ss : Set ↑s) : Set Ξ± := {x | βˆƒ y ∈ ss, y.val = x} theorem of_subtype_set.subset {s : Set Ξ±} (ss : Set ↑s) : (of_subtype_set ss) βŠ† s := by intro _ ⟨⟨_, ys⟩, _, hyx⟩ rw [←hyx]; exact ys theorem ideal_zorn [Ring Ξ±] (S : Set (Ideal Ξ±)) (h : βˆ€ c : Set (Ideal Ξ±), c βŠ† S β†’ Zorn.Chain Subset.subset c β†’ βˆƒ ub ∈ S, βˆ€ a ∈ c, a βŠ† ub) : βˆƒ m ∈ S, βˆ€ a ∈ S, m βŠ† a β†’ a = m := by have : βˆ€ c : Set S, Zorn.Chain subtype_subset c β†’ βˆƒ ub : S, βˆ€ a ∈ c, a ≀ ub := fun c hc => have : Zorn.Chain Subset.subset (of_subtype_set c) := by intro x xc y yc hxy let ⟨a, ac, hax⟩ := xc let ⟨b, bc, hbx⟩ := yc have := hc a ac b bc (fun hab => hxy (by rw [←hax, ←hbx]; exact congrArg Subtype.val hab)) rw [←hax, ←hbx]; exact this have ⟨ub, ubS, hub⟩ := h (of_subtype_set c) (of_subtype_set.subset c) this ⟨⟨ub, ubS⟩, fun ⟨a, aS⟩ ac => hub a ⟨⟨a, aS⟩, ac, rfl⟩⟩ let ⟨⟨m, mS⟩, hm⟩ := Zorn.lemma_eq (@subtype_subset _ _ S) this Subset.trans (fun hab hbc => Set.elementExt (Ideal.antisymm hab hbc)) exact ⟨m, mS, fun a as hma => have := hm ⟨a, as⟩ hma congrArg Subtype.val this⟩ def ideal_chain [Ring Ξ±] (S : Set (Ideal Ξ±)) (hS : Nonempty S) (hc : Zorn.Chain Subset.subset S) : Ideal Ξ± where subset := ⋃₀ (Set.toSetSet S Ideal.subset) has_zero := let ⟨x, xS⟩ := Classical.choice hS ⟨x.subset, ⟨x, xS, rfl⟩, x.has_zero⟩ add_closed := by intro a b ⟨i, ⟨I, IS, Ii⟩, ai⟩ ⟨j, ⟨J, JS, Jj⟩, bj⟩ cases Classical.em (I = J) with | inl h => rw [←Ii] at ai; rw [←h] at Jj; rw [←Jj] at bj exact ⟨I.subset, ⟨I, IS, rfl⟩, I.add_closed ai bj⟩ | inr h => cases hc I IS J JS h with | inl h => rw [←Ii] at ai; rw [←Jj] at bj exact ⟨J.subset, ⟨J, JS, rfl⟩, J.add_closed (h ai) bj⟩ | inr h => rw [←Ii] at ai; rw [←Jj] at bj exact ⟨I.subset, ⟨I, IS, rfl⟩, I.add_closed ai (h bj)⟩ mul_closed := by intro a b ⟨i, ⟨I, IS, Ii⟩, bi⟩ rw [←Ii] at bi exact ⟨I.subset, ⟨I, IS, rfl⟩, I.mul_closed a bi⟩ theorem ideal_chain_subset [Ring Ξ±] (S : Set (Ideal Ξ±)) {I : Ideal Ξ±} (IS : I ∈ S) (hc : Zorn.Chain Subset.subset S) : I βŠ† ideal_chain S ⟨I, IS⟩ hc := fun x xI => ⟨I.subset, ⟨I, IS, rfl⟩, xI⟩ theorem ideal_chain_proper [Ring Ξ±] (S : Set (Ideal Ξ±)) (hS : Nonempty S) (hc : Zorn.Chain Subset.subset S) : (βˆ€ I ∈ S, I.proper_ideal) β†’ (ideal_chain S hS hc).proper_ideal := by intro h hU let ⟨_, ⟨I, IS, hI⟩, hIu⟩ := is_unit_ideal.mp hU rw [←hI] at hIu exact absurd (is_unit_ideal.mpr hIu) (h I IS) theorem ideal_chain_disjoint [Ring Ξ±] (S : Set (Ideal Ξ±)) (hS : Nonempty S) (hc : Zorn.Chain Subset.subset S) (S' : Set Ξ±) : (βˆ€ I ∈ S, Set.disjoint I.subset S') β†’ (Set.disjoint (ideal_chain S hS hc).subset S') := by intro h apply Set.disjoint.elementwise.mpr intro x ⟨y, ⟨J, JS, hJy⟩, xy⟩; rw [←hJy] at xy exact Set.disjoint.elementwise.mp (h J JS) x xy end Ideal end M4R
CALL SplitMatrix(matrix_to_split, & & this%process_grid%number_of_blocks_rows, & & this%process_grid%number_of_blocks_columns, LOCALMATRIX)
section \<open> Weakest Precondition Calculus \<close> theory utp_wp imports utp_wlp begin text \<open> This calculus is like the liberal version, but also accounts for termination. It is equivalent to the relational preimage. \<close> consts uwp :: "'a \<Rightarrow> 'b \<Rightarrow> 'c" utp_const uwp(0) utp_lift_notation uwp (0) syntax "_uwp" :: "logic \<Rightarrow> logic \<Rightarrow> logic" (infix "wp" 60) translations "_uwp P b" == "CONST uwp P b" definition wp_upred :: "('\<alpha>, '\<beta>) urel \<Rightarrow> '\<beta> cond \<Rightarrow> '\<alpha> cond" where [upred_defs]: "wp_upred P b = Pre(P ;; ?[b])" adhoc_overloading uwp wp_upred term "P wp true" theorem refine_iff_wp: fixes P Q :: "('\<alpha>, '\<beta>) urel" shows "P \<sqsubseteq> Q \<longleftrightarrow> (\<forall> b. `P wp b \<Rightarrow> Q wp b`)" apply (rel_auto) oops theorem wp_refine_iff: "(\<forall> r. `Q wp r \<Rightarrow> P wp r`) \<longleftrightarrow> P \<sqsubseteq> Q" by (rel_auto robust; fastforce) theorem wp_refine_intro: "(\<And> r. `Q wp r \<Rightarrow> P wp r`) \<Longrightarrow> P \<sqsubseteq> Q" using wp_refine_iff by blast theorem wp_eq_iff: "(\<forall> r. P wp r = Q wp r) \<longrightarrow> P = Q" by (rel_auto robust; fastforce) theorem wp_eq_intro: "(\<And> r. P wp r = Q wp r) \<Longrightarrow> P = Q" by (simp add: wp_eq_iff) lemma wp_true: "P wp true = Pre(P)" by (rel_auto) lemma wp_false [wp]: "P wp false = false" by (rel_auto) lemma wp_abort [wp]: "false wp b = false" by (rel_auto) lemma wp_seq [wp]: "(P ;; Q) wp b = P wp (Q wp b)" by (simp add: wp_upred_def, metis Pre_seq RA1) lemma wp_disj [wp]: "(P \<or> Q) wp b = (P wp b \<or> Q wp b)" by (rel_auto) lemma wp_ndet [wp]: "(P \<sqinter> Q) wp b = (P wp b \<or> Q wp b)" by (rel_auto) lemma wp_cond [wp]: "(P \<triangleleft> b \<triangleright>\<^sub>r Q) wp r = ((b \<Rightarrow> P wp r) \<and> ((\<not> b) \<Rightarrow> Q wp r))" by rel_auto lemma wp_UINF_mem [wp]: "(\<Sqinter> i\<in>I \<bullet> P(i)) wp b = (\<Sqinter> i\<in>I \<bullet> P(i) wp b)" by (rel_auto) lemma wp_UINF_ind [wp]: "(\<Sqinter> i \<bullet> P(i)) wp b = (\<Sqinter> i \<bullet> P(i) wp b)" by (rel_auto) lemma wp_UINF_ind_2 [wp]: "(\<Sqinter> (i, j) \<bullet> P i j) wp b = (\<Or> (i, j) \<bullet> (P i j) wp b)" by (rel_auto) lemma wp_UINF_ind_3 [wp]: "(\<Sqinter> (i, j, k) \<bullet> P i j k) wp b = (\<Or> (i, j, k) \<bullet> (P i j k) wp b)" by (rel_blast) lemma wp_test [wp]: "?[b] wp c = (b \<and> c)" by (rel_auto) lemma wp_gcmd [wp]: "(b \<longrightarrow>\<^sub>r P) wp c = (b \<and> P wp c)" by (rel_auto) theorem wp_skip [wp]: "II wp r = r" by rel_auto lemma wp_assigns [wp]: "\<langle>\<sigma>\<rangle>\<^sub>a wp b = \<sigma> \<dagger> b" by (rel_auto) lemma wp_nd_assign [wp]: "(x := *) wp b = (\<^bold>\<exists> v \<bullet> b\<lbrakk>\<guillemotleft>v\<guillemotright>/&x\<rbrakk>)" by (simp add: nd_assign_def wp, rel_auto) lemma wp_rel_frext [wp]: assumes "vwb_lens a" "a \<sharp> q" shows "a:[P]\<^sup>+ wp (p \<oplus>\<^sub>p a \<and> q) = ((P wp p) \<oplus>\<^sub>p a \<and> q)" using assms by (rel_auto, metis (full_types), metis mwb_lens_def vwb_lens_mwb weak_lens.put_get) lemma wp_rel_aext_unrest [wp]: "\<lbrakk> vwb_lens a; a \<sharp> b \<rbrakk> \<Longrightarrow> a:[P]\<^sup>+ wp b = (b \<and> (P wp true) \<oplus>\<^sub>p a)" by (rel_auto, metis, metis mwb_lens_def vwb_lens_mwb weak_lens.put_get) lemma wp_rel_aext_usedby [wp]: "\<lbrakk> vwb_lens a; a \<natural> b \<rbrakk> \<Longrightarrow> a:[P]\<^sup>+ wp b = (P wp (b \<restriction>\<^sub>e a)) \<oplus>\<^sub>p a" by (rel_auto, metis mwb_lens_def vwb_lens_mwb weak_lens.put_get) lemma wp_wlp_conjugate: "P wp b = (\<not> P wlp (\<not> b))" by (rel_auto) text \<open> Weakest Precondition and Weakest Liberal Precondition are equivalent for terminating deterministic programs. \<close> lemma wlp_wp_equiv_lem: "\<lbrakk>(mk\<^sub>e (Pair a)) \<dagger> II\<rbrakk>\<^sub>e a" by (rel_auto) lemma wlp_wp_equiv_total_det: "(\<forall> b . P wp b = P wlp b) \<longleftrightarrow> (Pre(P) = true \<and> ufunctional P)" apply (rel_auto) apply blast apply (rename_tac a b y) apply (subgoal_tac "\<lbrakk>(mk\<^sub>e (Pair a)) \<dagger> II\<rbrakk>\<^sub>e b") apply (simp add: assigns_r.rep_eq skip_r_def subst.rep_eq subst_id.rep_eq Abs_uexpr_inverse) using wlp_wp_equiv_lem apply fastforce apply blast done lemma total_det_then_wlp_wp_equiv: "\<lbrakk> Pre(P) = true; ufunctional P \<rbrakk> \<Longrightarrow> P wp b = P wlp b" using wlp_wp_equiv_total_det by blast lemma Pre_as_wp: "Pre(P) = P wp true" by (simp add: wp_true) lemma nmods_via_wp: "\<lbrakk> vwb_lens x; \<And> v. P wp (&x = \<guillemotleft>v\<guillemotright>) = U(&x = \<guillemotleft>v\<guillemotright>) \<rbrakk> \<Longrightarrow> P nmods x" by (rel_auto, metis vwb_lens.put_eq) method wp_calc = (rule wp_refine_intro wp_eq_intro, wp_tac) method wp_auto = (wp_calc, rel_auto) end
[STATEMENT] lemma Mem_refl_E [intro!]: "insert (x IN x) H \<turnstile> A" [PROOF STATE] proof (prove) goal (1 subgoal): 1. insert (x IN x) H \<turnstile> A [PROOF STEP] by (metis Disj_I1 Mem_Neg_refl anti_deduction thin0)
[STATEMENT] lemma tanh_real_neg_iff [simp]: "tanh (x :: real) < 0 \<longleftrightarrow> x < 0" [PROOF STATE] proof (prove) goal (1 subgoal): 1. (tanh x < 0) = (x < 0) [PROOF STEP] by (simp add: tanh_def field_simps)
(t_test<- t.test(x = df$rt[df$Condition == "Descriptive"], y = df$rt[df$Condition == "Experiential"], paired = TRUE, alternative = "two.sided")) report(t_test) rm(ttest, t_test)
def f (x : Nat) : Nat := x + (set_option trace.Meta.synthInstance true in 1) def g (x : Nat) : Nat := 0 + x.succ theorem ex : f = g := by simp only [f] set_option trace.Meta.Tactic.simp true in simp only [Nat.add_succ, g] simp only [Nat.zero_add]
theory HLint imports "../HOLCF_Prelude" "../List_Comprehension" begin section \<open>HLint\<close> text \<open> The tool \texttt{hlint} analyses Haskell code and, based on a data base of rewrite rules, suggests stylistic improvements to it. We verify a number of these rules using our implementation of the Haskell standard library. \<close> (* -- I/O *) (* putStrLn (show x) ==> print x *) (* mapM_ putChar ==> putStr *) (* hGetChar stdin ==> getChar *) (* hGetLine stdin ==> getLine *) (* hGetContents stdin ==> getContents *) (* hPutChar stdout ==> putChar *) (* hPutStr stdout ==> putStr *) (* hPutStrLn stdout ==> putStrLn *) (* hPrint stdout ==> print *) (* hWaitForInput a 0 ==> hReady a *) (* hPutStrLn a (show b) ==> hPrint a b *) (* hIsEOF stdin ==> isEOF *) (* -- EXIT *) (* exitWith ExitSuccess ==> exitSuccess *) subsection \<open>Ord\<close> (* not (a == b) ==> a /= b where note = "incorrect if either value is NaN" *) (* not (a /= b) ==> a == b where note = "incorrect if either value is NaN" *) (* not (a > b) ==> a <= b where note = "incorrect if either value is NaN" *) (* not (a >= b) ==> a < b where note = "incorrect if either value is NaN" *) (* not (a < b) ==> a >= b where note = "incorrect if either value is NaN" *) (* not (a <= b) ==> a > b where note = "incorrect if either value is NaN" *) (* compare x y /= GT ==> x <= y *) (* compare x y == LT ==> x < y *) (* compare x y /= LT ==> x >= y *) (* compare x y == GT ==> x > y *) text \<open>@{verbatim \<open>x == a || x == b || x == c ==> x `elem` [a,b,c]\<close> } \<close> lemma "(eq\<cdot>(x::'a::Eq_sym)\<cdot>a orelse eq\<cdot>x\<cdot>b orelse eq\<cdot>x\<cdot>c) = elem\<cdot>x\<cdot>[a, b, c]" by (auto simp add: eq_sym) text \<open>@{verbatim \<open> x /= a && x /= b && x /= c ==> x `notElem` [a,b,c]\<close> } \<close> lemma "(neq\<cdot>(x::'a::Eq_sym)\<cdot>a andalso neq\<cdot>x\<cdot>b andalso neq\<cdot>x\<cdot>c) = notElem\<cdot>x\<cdot>[a, b, c]" by (auto simp add: eq_sym) (* compare (f x) (f y) ==> Data.Ord.comparing f x y -- not that great *) (* on compare f ==> Data.Ord.comparing f -- not that great *) (* -- READ/SHOW *) (* showsPrec 0 x "" ==> show x *) (* readsPrec 0 ==> reads *) (* showsPrec 0 ==> shows *) (* showIntAtBase 16 intToDigit ==> showHex *) (* showIntAtBase 8 intToDigit ==> showOct *) subsection \<open>List\<close> text \<open>@{verbatim \<open> concat (map f x) ==> concatMap f x\<close> } \<close> lemma "concat\<cdot>(map\<cdot>f\<cdot>x) = concatMap\<cdot>f\<cdot>x" by (auto simp add: concatMap_def) text \<open>@{verbatim \<open> concat [a, b] ==> a ++ b\<close> } \<close> lemma "concat\<cdot>[a, b] = a ++ b" by auto text \<open>@{verbatim \<open> map f (map g x) ==> map (f . g) x\<close> } \<close> lemma "map\<cdot>f\<cdot>(map\<cdot>g\<cdot>x) = map\<cdot>(f oo g)\<cdot>x" by auto text \<open>@{verbatim \<open> x !! 0 ==> head x\<close> } \<close> lemma "x !! 0 = head\<cdot>x" by (cases x) auto text \<open>@{verbatim \<open> take n (repeat x) ==> replicate n x\<close> } \<close> lemma "take\<cdot>n\<cdot>(repeat\<cdot>x) = replicate\<cdot>n\<cdot>x" by (simp add: replicate_def) text \<open>@{verbatim \<open>lemma "head\<cdot>(reverse\<cdot>x) = last\<cdot>x" \<close> } \<close> lemma "head\<cdot>(reverse\<cdot>x) = last\<cdot>x" proof (cases "finite_list x") case True then show ?thesis by (induct x rule: reverse_induct) (auto simp add: last_append_singleton) next case False then show ?thesis by (simp add: last_spine_strict reverse_spine_strict) qed text \<open>@{verbatim \<open> head (drop n x) ==> x !! n where note = "if the index is non-negative"\<close> } \<close> lemma assumes "le\<cdot>0\<cdot>n \<noteq> FF" shows "head\<cdot>(drop\<cdot>n\<cdot>x) = x !! n" proof (cases "le\<cdot>0\<cdot>n") assume "le\<cdot>0\<cdot>n = FF" with assms show ?thesis.. next assume "le\<cdot>0\<cdot>n = TT" then show ?thesis proof (induction arbitrary: x rule: nonneg_Integer_induct) case 0 show ?case by (cases x) auto next case (step i x) from step.hyps have [simp]:"le\<cdot>i\<cdot>0 = FF" by (cases i, auto simp add: one_Integer_def zero_Integer_def) from step.hyps have [simp]:"eq\<cdot>i\<cdot>0 = FF" by (cases i, auto simp add: one_Integer_def zero_Integer_def) show ?case using step.IH by (cases x)auto qed qed simp text \<open>@{verbatim \<open> reverse (tail (reverse x)) ==> init x\<close> } \<close> lemma "reverse\<cdot>(tail\<cdot>(reverse\<cdot>x)) \<sqsubseteq> init\<cdot>x" proof (cases "finite_list x") case True then show ?thesis by (induct x rule: reverse_induct) (auto simp add: init_append_singleton) next case False then show ?thesis by (auto simp add: reverse_spine_strict) qed text \<open>@{verbatim \<open> take (length x - 1) x ==> init x\<close> } \<close> lemma assumes "x \<noteq> []" shows "take\<cdot>(length\<cdot>x - 1)\<cdot>x \<sqsubseteq> init\<cdot>x" using assms proof (induct x) case (Cons y ys) show ?case proof (cases ys) note IH = Cons case (Cons z zs) show ?thesis using IH by (cases "length\<cdot>zs") (auto simp: Cons one_Integer_def dest: length_ge_0) qed (auto simp: one_Integer_def) qed auto text \<open>@{verbatim \<open> foldr (++) [] ==> concat\<close> } \<close> lemma foldr_append_concat:"foldr\<cdot>append\<cdot>[] = concat" proof (rule cfun_eqI) fix xs :: "[['a]]" show "foldr\<cdot>append\<cdot>[]\<cdot>xs = concat\<cdot>xs" by (induct xs) auto qed text \<open>@{verbatim \<open> foldl (++) [] ==> concat\<close> } \<close> lemma "foldl\<cdot>append\<cdot>[] \<sqsubseteq> concat" proof (rule cfun_belowI) fix xs :: "[['a]]" show "foldl\<cdot>append\<cdot>[]\<cdot>xs \<sqsubseteq> concat\<cdot>xs" by (cases "finite_list xs") (auto simp add: foldr_append_concat foldl_assoc_foldr foldl_spine_strict) qed text \<open>@{verbatim \<open> span (not . p) ==> break p\<close> } \<close> lemma "span\<cdot>(neg oo p) = break\<cdot>p" by simp text \<open>@{verbatim \<open> break (not . p) ==> span p\<close> } \<close> lemma "break\<cdot>(neg oo p) = span\<cdot>p" by (unfold break.simps) (subst assoc_oo, simp) (* concatMap (++ "\n") ==> unlines *) text \<open>@{verbatim \<open> or (map p x) ==> any p x\<close> } \<close> lemma "the_or\<cdot>(map\<cdot>p\<cdot>x) = any\<cdot>p\<cdot>x" by simp text \<open>@{verbatim \<open> and (map p x) ==> all p x\<close> } \<close> lemma "the_and\<cdot>(map\<cdot>p\<cdot>x) = all\<cdot>p\<cdot>x" by simp text \<open>@{verbatim \<open> zipWith (,) ==> zip\<close> } \<close> lemma "zipWith\<cdot>\<langle>,\<rangle> = zip" by (simp add: zip_def) text \<open>@{verbatim \<open> zipWith3 (,,) ==> zip3\<close> } \<close> lemma "zipWith3\<cdot>\<langle>,,\<rangle> = zip3" by (simp add: zip3_def) text \<open>@{verbatim \<open> length x == 0 ==> null x where note = "increases laziness"\<close> } \<close> lemma "eq\<cdot>(length\<cdot>x)\<cdot>0 \<sqsubseteq> null\<cdot>x" proof (cases x) case (Cons y ys) then show ?thesis by (cases "length\<cdot>ys") (auto dest: length_ge_0 simp: zero_Integer_def one_Integer_def) qed simp+ text \<open>@{verbatim \<open> length x /= 0 ==> not (null x)\<close> } \<close> lemma "neq\<cdot>(length\<cdot>x)\<cdot>0 \<sqsubseteq> neg\<cdot>(null\<cdot>x)" proof (cases x) case (Cons y ys) then show ?thesis by (cases "length\<cdot>ys") (auto dest: length_ge_0 simp: zero_Integer_def one_Integer_def) qed simp+ (* (\x -> [x]) ==> (:[]) *) text \<open>@{verbatim \<open> map (uncurry f) (zip x y) ==> zipWith f x y\<close> } \<close> lemma "map\<cdot>(uncurry\<cdot>f)\<cdot>(zip\<cdot>x\<cdot>y) = zipWith\<cdot>f\<cdot>x\<cdot>y" proof (induct x arbitrary: y) case (Cons x xs y) then show ?case by (cases y) auto qed auto text \<open>@{verbatim \<open> map f (zip x y) ==> zipWith (curry f) x y where _ = isVar f\<close> } \<close> lemma "map\<cdot>f\<cdot>(zip\<cdot>x\<cdot>y) = zipWith\<cdot>(curry\<cdot>f)\<cdot>x\<cdot>y" proof(induct x arbitrary: y) case (Cons x xs y) then show ?case by (cases y) auto qed auto text \<open>@{verbatim \<open> not (elem x y) ==> notElem x y\<close> } \<close> lemma "neg\<cdot>(elem\<cdot>x\<cdot>y) = notElem\<cdot>x\<cdot>y" by (induct y) auto text \<open>@{verbatim \<open> foldr f z (map g x) ==> foldr (f . g) z x\<close> } \<close> lemma "foldr\<cdot>f\<cdot>z\<cdot>(map\<cdot>g\<cdot>x) = foldr\<cdot>(f oo g)\<cdot>z\<cdot>x" by (induct x) auto (* x ++ concatMap (' ':) y ==> unwords (x:y) *) (* intercalate " " ==> unwords *) (* concat (intersperse x y) ==> intercalate x y where _ = notEq x " " *) (* concat (intersperse " " x) ==> unwords x *) text \<open>@{verbatim \<open> null (filter f x) ==> not (any f x)\<close> } \<close> lemma "null\<cdot>(filter\<cdot>f\<cdot>x) = neg\<cdot>(any\<cdot>f\<cdot>x)" proof (induct x) case (Cons x xs) then show ?case by (cases "f\<cdot>x") auto qed auto text \<open>@{verbatim \<open> filter f x == [] ==> not (any f x)\<close> } \<close> lemma "eq\<cdot>(filter\<cdot>f\<cdot>x)\<cdot>[] = neg\<cdot>(any\<cdot>f\<cdot>x)" proof (induct x) case (Cons x xs) then show ?case by (cases "f\<cdot>x") auto qed auto text \<open>@{verbatim \<open> filter f x /= [] ==> any f x\<close> } \<close> lemma "neq\<cdot>(filter\<cdot>f\<cdot>x)\<cdot>[] = any\<cdot>f\<cdot>x" proof (induct x) case (Cons x xs) then show ?case by (cases "f\<cdot>x") auto qed auto text \<open>@{verbatim \<open> any (== a) ==> elem a\<close> } \<close> lemma "any\<cdot>(\<Lambda> z. eq\<cdot>z\<cdot>a) = elem\<cdot>a" proof (rule cfun_eqI) fix xs show "any\<cdot>(\<Lambda> z. eq\<cdot>z\<cdot>a)\<cdot>xs = elem\<cdot>a\<cdot>xs" by (induct xs) auto qed text \<open>@{verbatim \<open> any ((==) a) ==> elem a\<close> } \<close> lemma "any\<cdot>(eq\<cdot>(a::'a::Eq_sym)) = elem\<cdot>a" proof (rule cfun_eqI) fix xs show "any\<cdot>(eq\<cdot>a)\<cdot>xs = elem\<cdot>a\<cdot>xs" by (induct xs) (auto simp: eq_sym) qed text \<open>@{verbatim \<open>any (a ==) ==> elem a\<close> } \<close> lemma "any\<cdot>(\<Lambda> z. eq\<cdot>(a::'a::Eq_sym)\<cdot>z) = elem\<cdot>a" proof (rule cfun_eqI) fix xs show "any\<cdot>(\<Lambda> z. eq\<cdot>a\<cdot>z)\<cdot>xs = elem\<cdot>a\<cdot>xs" by (induct xs) (auto simp: eq_sym) qed text \<open>@{verbatim \<open> all (/= a) ==> notElem a\<close> } \<close> lemma "all\<cdot>(\<Lambda> z. neq\<cdot>z\<cdot>(a::'a::Eq_sym)) = notElem\<cdot>a" proof (rule cfun_eqI) fix xs show "all\<cdot>(\<Lambda> z. neq\<cdot>z\<cdot>a)\<cdot>xs = notElem\<cdot>a\<cdot>xs" by (induct xs) auto qed text \<open>@{verbatim \<open> all (a /=) ==> notElem a\<close> } \<close> lemma "all\<cdot>(\<Lambda> z. neq\<cdot>(a::'a::Eq_sym)\<cdot>z) = notElem\<cdot>a" proof (rule cfun_eqI) fix xs show "all\<cdot>(\<Lambda> z. neq\<cdot>a\<cdot>z)\<cdot>xs = notElem\<cdot>a\<cdot>xs" by (induct xs) (auto simp: eq_sym) qed (* findIndex ((==) a) ==> elemIndex a *) (* findIndex (a ==) ==> elemIndex a *) (* findIndex (== a) ==> elemIndex a *) (* findIndices ((==) a) ==> elemIndices a *) (* findIndices (a ==) ==> elemIndices a *) (* findIndices (== a) ==> elemIndices a *) (* lookup b (zip l [0..]) ==> elemIndex b l *) subsection \<open>Folds\<close> (* foldr (>>) (return ()) ==> sequence_ *) text \<open>@{verbatim \<open> foldr (&&) True ==> and\<close> } \<close> lemma "foldr\<cdot>trand\<cdot>TT = the_and" by (subst the_and.simps, rule) text \<open>@{verbatim \<open> foldl (&&) True ==> and\<close> } \<close> lemma foldl_to_and:"foldl\<cdot>trand\<cdot>TT \<sqsubseteq> the_and" proof (rule cfun_belowI) fix xs show "foldl\<cdot>trand\<cdot>TT\<cdot>xs \<sqsubseteq> the_and\<cdot>xs" by (cases "finite_list xs") (auto simp: foldl_assoc_foldr foldl_spine_strict) qed text \<open>@{verbatim \<open> foldr1 (&&) ==> and\<close> } \<close> lemma "foldr1\<cdot>trand \<sqsubseteq> the_and" proof (rule cfun_belowI) fix xs show "foldr1\<cdot>trand\<cdot>xs \<sqsubseteq> the_and\<cdot>xs" proof (induct xs) case (Cons y ys) then show ?case by (cases ys) (auto elim: monofun_cfun_arg) qed simp+ qed text \<open>@{verbatim \<open> foldl1 (&&) ==> and\<close> } \<close> lemma "foldl1\<cdot>trand \<sqsubseteq> the_and" proof (rule cfun_belowI) fix x have "foldl1\<cdot>trand\<cdot>x \<sqsubseteq> foldl\<cdot>trand\<cdot>TT\<cdot>x" by (cases x, auto) also have "... \<sqsubseteq> the_and\<cdot>x" by (rule monofun_cfun_fun[OF foldl_to_and]) finally show "foldl1\<cdot>trand\<cdot>x \<sqsubseteq> the_and\<cdot>x" . qed text \<open>@{verbatim \<open> foldr (||) False ==> or\<close> } \<close> lemma "foldr\<cdot>tror\<cdot>FF = the_or" by (subst the_or.simps, rule) text \<open>@{verbatim \<open> foldl (||) False ==> or\<close> } \<close> lemma foldl_to_or: "foldl\<cdot>tror\<cdot>FF \<sqsubseteq> the_or" proof (rule cfun_belowI) fix xs show "foldl\<cdot>tror\<cdot>FF\<cdot>xs \<sqsubseteq> the_or\<cdot>xs" by (cases "finite_list xs") (auto simp: foldl_assoc_foldr foldl_spine_strict) qed text \<open>@{verbatim \<open> foldr1 (||) ==> or\<close> } \<close> lemma "foldr1\<cdot>tror \<sqsubseteq> the_or" proof (rule cfun_belowI) fix xs show "foldr1\<cdot>tror\<cdot>xs \<sqsubseteq> the_or\<cdot>xs" proof (induct xs) case (Cons y ys) then show ?case by (cases ys) (auto elim: monofun_cfun_arg) qed simp+ qed text \<open>@{verbatim \<open> foldl1 (||) ==> or\<close> } \<close> lemma "foldl1\<cdot>tror \<sqsubseteq> the_or" proof(rule cfun_belowI) fix x have "foldl1\<cdot>tror\<cdot>x \<sqsubseteq> foldl\<cdot>tror\<cdot>FF\<cdot>x" by (cases x, auto) also have "... \<sqsubseteq> the_or\<cdot>x" by (rule monofun_cfun_fun[OF foldl_to_or]) finally show "foldl1\<cdot>tror\<cdot>x \<sqsubseteq> the_or\<cdot>x" . qed (* foldl (+) 0 ==> sum *) (* foldr (+) 0 ==> sum *) (* foldl1 (+) ==> sum *) (* foldr1 (+) ==> sum *) (* foldl ( * ) 1 ==> product *) (* foldr ( * ) 1 ==> product *) (* foldl1 ( * ) ==> product *) (* foldr1 ( * ) ==> product *) (* foldl1 max ==> maximum *) (* foldr1 max ==> maximum *) (* foldl1 min ==> minimum *) (* foldr1 min ==> minimum *) (* foldr mplus mzero ==> msum *) subsection \<open>Function\<close> text \<open>@{verbatim \<open> (\x -> x) ==> id\<close> } \<close> lemma "(\<Lambda> x. x) = ID" by (metis ID_def) text \<open>@{verbatim \<open> (\x y -> x) ==> const\<close> } \<close> lemma "(\<Lambda> x y. x) = const" by (intro cfun_eqI) simp text \<open>@{verbatim \<open>(\(x,y) -> y) ==> fst where _ = notIn x y\<close> } \<close> lemma "(\<Lambda> \<langle>x, y\<rangle>. x) = fst" proof (rule cfun_eqI) fix p show "(case p of \<langle>x, y\<rangle> \<Rightarrow> x) = fst \<cdot> p" proof (cases p) case bottom then show ?thesis by simp next case Tuple2 then show ?thesis by simp qed qed text \<open>@{verbatim \<open>(\(x,y) -> y) ==> snd where _ = notIn x y\<close> } \<close> lemma "(\<Lambda> \<langle>x, y\<rangle>. y) = snd" proof (rule cfun_eqI) fix p show "(case p of \<langle>x, y\<rangle> \<Rightarrow> y) = snd \<cdot> p" proof (cases p) case bottom then show ?thesis by simp next case Tuple2 then show ?thesis by simp qed qed text \<open>@{verbatim \<open> (\x y-> f (x,y)) ==> curry f where _ = notIn [x,y] f\<close> } \<close> lemma "(\<Lambda> x y. f\<cdot>\<langle>x, y\<rangle>) = curry\<cdot>f" by (auto intro!: cfun_eqI) text \<open>@{verbatim \<open> (\(x,y) -> f x y) ==> uncurry f where _ = notIn [x,y] f\<close> } \<close> lemma "(\<Lambda> \<langle>x, y\<rangle>. f\<cdot>x\<cdot>y) \<sqsubseteq> uncurry\<cdot>f" by (rule cfun_belowI, rename_tac x, case_tac x, auto) (* (($) . f) ==> f *) (* (f $) ==> f *) text \<open>@{verbatim \<open> (\x -> y) ==> const y where _ = isAtom y && notIn x y\<close> } \<close> lemma "(\<Lambda> x. y) = const\<cdot>y" by (intro cfun_eqI) simp (* flip f x y ==> f y x where _ = isApp original *) lemma "flip\<cdot>f\<cdot>x\<cdot>y = f\<cdot>y\<cdot>x" by simp (* (\a b -> o (f a) (f b)) ==> o `Data.Function.on` f *) (* -- CHAR *) (* a >= 'a' && a <= 'z' ==> isAsciiLower a *) (* a >= 'A' && a <= 'Z' ==> isAsciiUpper a *) (* a >= '0' && a <= '9' ==> isDigit a *) (* a >= '0' && a <= '7' ==> isOctDigit a *) (* not (isControl a) ==> isPrint a *) (* isLower a || isUpper a ==> isAlpha a *) (* isAlpha a || isDigit a ==> isAlphaNum a *) subsection \<open>Bool\<close> text \<open>@{verbatim \<open> a == True ==> a\<close> } \<close> lemma eq_true:"eq\<cdot>x\<cdot>TT = x" by (cases x, auto) text \<open>@{verbatim \<open> a == False ==> not a\<close> } \<close> lemma eq_false:"eq\<cdot>x\<cdot>FF = neg\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open> (if a then x else x) ==> x where note = "reduces strictness"\<close> } \<close> lemma if_equal:"(If a then x else x) \<sqsubseteq> x" by (cases a, auto) text \<open>@{verbatim \<open> (if a then True else False) ==> a\<close> } \<close> lemma "(If a then TT else FF) = a" by (cases a, auto) text \<open>@{verbatim \<open> (if a then False else True) ==> not a\<close> } \<close> lemma "(If a then FF else TT) = neg\<cdot>a" by (cases a, auto) text \<open>@{verbatim \<open> (if a then t else (if b then t else f)) ==> if a || b then t else f\<close> } \<close> lemma "(If a then t else (If b then t else f)) = (If a orelse b then t else f)" by (cases a, auto) text \<open>@{verbatim \<open> (if a then (if b then t else f) else f) ==> if a && b then t else f\<close> } \<close> lemma "(If a then (If b then t else f) else f) = (If a andalso b then t else f)" by (cases a, auto) text \<open>@{verbatim \<open> (if x then True else y) ==> x || y where _ = notEq y False\<close> } \<close> lemma "(If x then TT else y) = (x orelse y)" by (cases x, auto) text \<open>@{verbatim \<open> (if x then y else False) ==> x && y where _ = notEq y True\<close> } \<close> lemma "(If x then y else FF) = (x andalso y)" by (cases x, auto) (* case a of {True -> t; False -> f} ==> if a then t else f *) (* case a of {False -> f; True -> t} ==> if a then t else f *) (* case a of {True -> t; _ -> f} ==> if a then t else f *) (* case a of {False -> f; _ -> t} ==> if a then t else f *) text \<open>@{verbatim \<open> (if c then (True, x) else (False, x)) ==> (c, x) where note = "reduces strictness"\<close> } \<close> lemma "(If c then \<langle>TT, x\<rangle> else \<langle>FF, x\<rangle>) \<sqsubseteq> \<langle>c, x\<rangle>" by (cases c, auto) text \<open>@{verbatim \<open> (if c then (False, x) else (True, x)) ==> (not c, x) where note = "reduces strictness"\<close> } \<close> lemma "(If c then \<langle>FF, x\<rangle> else \<langle>TT, x\<rangle>) \<sqsubseteq> \<langle>neg\<cdot>c, x\<rangle>" by (cases c, auto) text \<open>@{verbatim \<open> or [x,y] ==> x || y\<close> } \<close> lemma "the_or\<cdot>[x, y] = (x orelse y)" by (fixrec_simp) text \<open>@{verbatim \<open> or [x,y,z] ==> x || y || z\<close> } \<close> lemma "the_or\<cdot>[x, y, z] = (x orelse y orelse z)" by (fixrec_simp) text \<open>@{verbatim \<open> and [x,y] ==> x && y\<close> } \<close> lemma "the_and\<cdot>[x, y] = (x andalso y)" by (fixrec_simp) text \<open>@{verbatim \<open> and [x,y,z] ==> x && y && z\<close> } \<close> lemma "the_and\<cdot>[x, y, z] = (x andalso y andalso z)" by (fixrec_simp) subsection \<open>Arrow\<close> (* id *** g ==> second g *) (* f *** id ==> first f *) (* zip (map f x) (map g x) ==> map (f Control.Arrow.&&& g) x *) (* (\(x,y) -> (f x, g y)) ==> f Control.Arrow.*** g where _ = notIn [x,y] [f,g] *) (* (\x -> (f x, g x)) ==> f Control.Arrow.&&& g where _ = notIn x [f,g] *) (* (\(x,y) -> (f x,y)) ==> Control.Arrow.first f where _ = notIn [x,y] f *) (* (\(x,y) -> (x,f y)) ==> Control.Arrow.second f where _ = notIn [x,y] f *) (* (f (fst x), g (snd x)) ==> (f Control.Arrow.*** g) x *) text \<open>@{verbatim \<open> (fst x, snd x) ==> x\<close> } \<close> lemma "x \<sqsubseteq> \<langle>fst\<cdot>x, snd\<cdot>x\<rangle>" by (cases x, auto) (* -- FUNCTOR *) (* fmap f (fmap g x) ==> fmap (f . g) x *) (* fmap id ==> id *) (* -- MONAD *) (* return a >>= f ==> f a *) (* m >>= return ==> m *) (* m >>= return . f ==> Control.Monad.liftM f m -- cannot be fmap, because is in Functor not Monad *) (* (if x then y else return ()) ==> Control.Monad.when x $ _noParen_ y where _ = not (isAtom y) *) (* (if x then y else return ()) ==> Control.Monad.when x y where _ = isAtom y *) (* (if x then return () else y) ==> Control.Monad.unless x $ _noParen_ y where _ = not (isAtom y) *) (* (if x then return () else y) ==> Control.Monad.unless x y where _ = isAtom y *) (* sequence (map f x) ==> mapM f x *) (* sequence_ (map f x) ==> mapM_ f x *) (* flip mapM ==> Control.Monad.forM *) (* flip mapM_ ==> Control.Monad.forM_ *) (* flip forM ==> mapM *) (* flip forM_ ==> mapM_ *) (* when (not x) ==> unless x *) (* x >>= id ==> Control.Monad.join x *) (* liftM f (liftM g x) ==> liftM (f . g) x *) (* a >> return () ==> void a *) (* fmap (const ()) ==> void *) (* flip (>=>) ==> (<=<) *) (* flip (<=<) ==> (>=>) *) (* (\x -> f x >>= g) ==> f Control.Monad.>=> g where _ = notIn x [f,g] *) (* (\x -> f =<< g x) ==> f Control.Monad.<=< g where _ = notIn x [f,g] *) (* a >> forever a ==> forever a *) (* liftM2 id ==> ap *) (* -- MONAD LIST *) (* liftM unzip (mapM f x) ==> Control.Monad.mapAndUnzipM f x *) (* sequence (zipWith f x y) ==> Control.Monad.zipWithM f x y *) (* sequence_ (zipWith f x y) ==> Control.Monad.zipWithM_ f x y *) (* sequence (replicate n x) ==> Control.Monad.replicateM n x *) (* sequence_ (replicate n x) ==> Control.Monad.replicateM_ n x *) (* mapM f (map g x) ==> mapM (f . g) x *) (* mapM_ f (map g x) ==> mapM_ (f . g) x *) (* -- APPLICATIVE / TRAVERSABLE *) (* flip traverse ==> for *) (* flip for ==> traverse *) (* flip traverse_ ==> for_ *) (* flip for_ ==> traverse_ *) (* foldr ( *>) (pure ()) ==> sequenceA_ *) (* foldr (<|>) empty ==> asum *) (* liftA2 (flip ($)) ==> (<**>) *) (* Just <$> a <|> pure Nothing ==> optional a *) (* -- LIST COMP *) (* (if b then [x] else []) ==> [x | b] *) (* [x | x <- y] ==> y where _ = isVar x *) (* -- SEQ *) subsection \<open>Seq\<close> text \<open>@{verbatim \<open> x `seq` x ==> x\<close> } \<close> lemma "seq\<cdot>x\<cdot>x = x" by (simp add: seq_def) (* id $! x ==> x *) (* x `seq` y ==> y where _ = isWHNF x *) (* f $! x ==> f x where _ = isWHNF x *) (* evaluate x ==> return x where _ = isWHNF x *) (* -- MAYBE *) (* maybe x id ==> Data.Maybe.fromMaybe x *) (* maybe False (const True) ==> Data.Maybe.isJust *) (* maybe True (const False) ==> Data.Maybe.isNothing *) (* not (isNothing x) ==> isJust x *) (* not (isJust x) ==> isNothing x *) (* maybe [] (:[]) ==> maybeToList *) (* catMaybes (map f x) ==> mapMaybe f x *) (* (case x of Nothing -> y; Just a -> a) ==> fromMaybe y x *) (* (if isNothing x then y else f (fromJust x)) ==> maybe y f x *) (* (if isJust x then f (fromJust x) else y) ==> maybe y f x *) (* maybe Nothing (Just . f) ==> fmap f *) (* map fromJust . filter isJust ==> Data.Maybe.catMaybes *) (* x == Nothing ==> isNothing x *) (* Nothing == x ==> isNothing x *) (* x /= Nothing ==> Data.Maybe.isJust x *) (* Nothing /= x ==> Data.Maybe.isJust x *) (* concatMap (maybeToList . f) ==> Data.Maybe.mapMaybe f *) (* concatMap maybeToList ==> catMaybes *) (* maybe n Just x ==> Control.Monad.mplus x n *) (* (case x of Just a -> a; Nothing -> y) ==> fromMaybe y x *) (* (if isNothing x then y else fromJust x) ==> fromMaybe y x *) (* (if isJust x then fromJust x else y) ==> fromMaybe y x *) (* isJust x && (fromJust x == y) ==> x == Just y *) (* mapMaybe f (map g x) ==> mapMaybe (f . g) x *) (* fromMaybe a (fmap f x) ==> maybe a f x *) (* [x | Just x <- a] ==> Data.Maybe.catMaybes a *) (* -- EITHER *) (* [a | Left a <- a] ==> lefts a *) (* [a | Right a <- a] ==> rights a *) (* -- INFIX *) (* X.elem x y ==> x `X.elem` y where _ = not (isInfixApp original) && not (isParen result) *) (* X.notElem x y ==> x `X.notElem` y where _ = not (isInfixApp original) && not (isParen result) *) (* X.isInfixOf x y ==> x `X.isInfixOf` y where _ = not (isInfixApp original) && not (isParen result) *) (* X.isSuffixOf x y ==> x `X.isSuffixOf` y where _ = not (isInfixApp original) && not (isParen result) *) (* X.isPrefixOf x y ==> x `X.isPrefixOf` y where _ = not (isInfixApp original) && not (isParen result) *) (* X.union x y ==> x `X.union` y where _ = not (isInfixApp original) && not (isParen result) *) (* X.intersect x y ==> x `X.intersect` y where _ = not (isInfixApp original) && not (isParen result) *) (* -- MATHS *) (* fromIntegral x ==> x where _ = isLitInt x *) (* fromInteger x ==> x where _ = isLitInt x *) (* x + negate y ==> x - y *) (* 0 - x ==> negate x *) (* log y / log x ==> logBase x y *) (* x ** 0.5 ==> sqrt x *) (* sin x / cos x ==> tan x *) (* sinh x / cosh x ==> tanh x *) (* n `rem` 2 == 0 ==> even n *) (* n `rem` 2 /= 0 ==> odd n *) (* not (even x) ==> odd x *) (* not (odd x) ==> even x *) (* x ** 0.5 ==> sqrt x *) (* x ^^ y ==> x ** y where _ = isLitInt y *) (* x ^ 0 ==> 1 *) (* round (x - 0.5) ==> floor x *) (* -- CONCURRENT *) (* mapM_ (writeChan a) ==> writeList2Chan a *) (* -- EXCEPTION *) (* Prelude.catch ==> Control.Exception.catch where note = "Prelude.catch does not catch most exceptions" *) (* flip Control.Exception.catch ==> handle *) (* flip handle ==> Control.Exception.catch *) (* flip (catchJust p) ==> handleJust p *) (* flip (handleJust p) ==> catchJust p *) (* Control.Exception.bracket b (const a) (const t) ==> Control.Exception.bracket_ b a t *) (* Control.Exception.bracket (openFile x y) hClose ==> withFile x y *) (* Control.Exception.bracket (openBinaryFile x y) hClose ==> withBinaryFile x y *) (* throw (ErrorCall a) ==> error a *) (* a `seq` return a ==> Control.Exception.evaluate a *) (* toException NonTermination ==> nonTermination *) (* toException NestedAtomically ==> nestedAtomically *) (* -- WEAK POINTERS *) (* mkWeak a a b ==> mkWeakPtr a b *) (* mkWeak a (a, b) c ==> mkWeakPair a b c *) subsection \<open>Evaluate\<close> text \<open>@{verbatim \<open> True && x ==> x\<close> } \<close> lemma "(TT andalso x) = x" by auto text \<open>@{verbatim \<open> False && x ==> False\<close> } \<close> lemma "(FF andalso x) = FF" by auto text \<open>@{verbatim \<open> True || x ==> True\<close> } \<close> lemma "(TT orelse x) = TT" by auto text \<open>@{verbatim \<open> False || x ==> x\<close> } \<close> lemma "(FF orelse x) = x" by auto text \<open>@{verbatim \<open> not True ==> False\<close> } \<close> lemma "neg\<cdot>TT = FF" by auto text \<open>@{verbatim \<open> not False ==> True\<close> } \<close> lemma "neg\<cdot>FF = TT" by auto (* Nothing >>= k ==> Nothing *) (* either f g (Left x) ==> f x *) (* either f g (Right y) ==> g y *) text \<open>@{verbatim \<open> fst (x,y) ==> x\<close> } \<close> lemma "fst\<cdot>\<langle>x, y\<rangle> = x" by auto text \<open>@{verbatim \<open> snd (x,y) ==> y\<close> } \<close> lemma "snd\<cdot>\<langle>x, y\<rangle> = y" by auto text \<open>@{verbatim \<open> f (fst p) (snd p) ==> uncurry f p\<close> } \<close> lemma "f\<cdot>(fst\<cdot>p)\<cdot>(snd\<cdot>p) = uncurry\<cdot>f\<cdot>p" by (cases p, auto) text \<open>@{verbatim \<open> init [x] ==> []\<close> } \<close> lemma "init\<cdot>[x] = []" by auto text \<open>@{verbatim \<open> null [] ==> True\<close> } \<close> lemma "null\<cdot>[] = TT" by auto text \<open>@{verbatim \<open> length [] ==> 0\<close> } \<close> lemma "length\<cdot>[] = 0" by auto text \<open>@{verbatim \<open> foldl f z [] ==> z\<close> } \<close> lemma "foldl\<cdot>f\<cdot>z\<cdot>[] = z" by simp text \<open>@{verbatim \<open> foldr f z [] ==> z\<close> } \<close> lemma "foldr\<cdot>f\<cdot>z\<cdot>[] = z" by auto text \<open>@{verbatim \<open> foldr1 f [x] ==> x\<close> } \<close> lemma "foldr1\<cdot>f\<cdot>[x] = x" by simp text \<open>@{verbatim \<open> scanr f z [] ==> [z]\<close> } \<close> lemma "scanr\<cdot>f\<cdot>z\<cdot>[] = [z]" by simp text \<open>@{verbatim \<open> scanr1 f [] ==> []\<close> } \<close> lemma "scanr1\<cdot>f\<cdot>[] = []" by simp text \<open>@{verbatim \<open> scanr1 f [x] ==> [x]\<close> } \<close> lemma "scanr1\<cdot>f\<cdot>[x] = [x]" by simp text \<open>@{verbatim \<open> take n [] ==> []\<close> } \<close> lemma "take\<cdot>n\<cdot>[] \<sqsubseteq> []" by (cases n, auto) text \<open>@{verbatim \<open> drop n [] ==> []\<close> } \<close> lemma "drop\<cdot>n\<cdot>[] \<sqsubseteq> []" by (subst drop.simps) (auto simp: if_equal) text \<open>@{verbatim \<open> takeWhile p [] ==> []\<close> } \<close> lemma "takeWhile\<cdot>p\<cdot>[] = []" by (fixrec_simp) text \<open>@{verbatim \<open> dropWhile p [] ==> []\<close> } \<close> lemma "dropWhile\<cdot>p\<cdot>[] = []" by (fixrec_simp) text \<open>@{verbatim \<open> span p [] ==> ([],[])\<close> } \<close> lemma "span\<cdot>p\<cdot>[] = \<langle>[], []\<rangle>" by (fixrec_simp) (* lines "" ==> [] *) (* unwords [] ==> "" *) (* x - 0 ==> x *) (* x * 1 ==> x *) (* x / 1 ==> x *) text \<open>@{verbatim \<open> concat [a] ==> a\<close> } \<close> lemma "concat\<cdot>[a] = a" by auto text \<open>@{verbatim \<open> concat [] ==> []\<close> } \<close> lemma "concat\<cdot>[] = []" by auto text \<open>@{verbatim \<open> zip [] [] ==> []\<close> } \<close> lemma "zip\<cdot>[]\<cdot>[] = []" by auto text \<open>@{verbatim \<open> id x ==> x\<close> } \<close> lemma "ID\<cdot>x = x" by auto text \<open>@{verbatim \<open> const x y ==> x\<close> } \<close> lemma "const\<cdot>x\<cdot>y = x" by simp subsection \<open>Complex hints\<close> text \<open>@{verbatim \<open> take (length t) s == t ==> t `Data.List.isPrefixOf` s\<close> } \<close> lemma fixes t :: "['a::Eq_sym]" shows "eq\<cdot>(take\<cdot>(length\<cdot>t)\<cdot>s)\<cdot>t \<sqsubseteq> isPrefixOf\<cdot>t\<cdot>s" by (subst eq_sym) (rule eq_take_length_isPrefixOf) text \<open>@{verbatim \<open> (take i s == t) ==> _eval_ ((i >= length t) && (t `Data.List.isPrefixOf` s))\<close> } \<close> text \<open>The hint is not true in general, as the following two lemmas show:\<close> lemma assumes "t = []" and "s = x : xs" and "i = 1" shows "\<not> (eq\<cdot>(take\<cdot>i\<cdot>s)\<cdot>t \<sqsubseteq> (le\<cdot>(length\<cdot>t)\<cdot>i andalso isPrefixOf\<cdot>t\<cdot>s))" using assms by simp lemma assumes "le\<cdot>0\<cdot>i = TT" and "le\<cdot>i\<cdot>0 = FF" and "s = \<bottom>" and "t = []" shows "\<not> ((le\<cdot>(length\<cdot>t)\<cdot>i andalso isPrefixOf\<cdot>t\<cdot>s) \<sqsubseteq> eq\<cdot>(take\<cdot>i\<cdot>s)\<cdot>t)" using assms by (subst take.simps) simp (* -- clever hint, but not actually a good idea *) (* (do a <- f; g a) ==> f >>= g *) (* a $$$$ b $$$$ c ==> a . b $$$$$ c *) (* not (a == b) ==> a /= b *) lemma "neg\<cdot>(eq\<cdot>a\<cdot>b) = neq\<cdot>a\<cdot>b" by auto text \<open>@{verbatim \<open>not (a /= b) ==> a == b\<close> } \<close> lemma "neg\<cdot>(neq\<cdot>a\<cdot>b) = eq\<cdot>a\<cdot>b" by auto text \<open>@{verbatim \<open>map id ==> id\<close> } \<close> lemma map_id:"map\<cdot>ID = ID" by (auto simp add: cfun_eq_iff) text \<open>@{verbatim \<open>x == [] ==> null x\<close> } \<close> lemma "eq\<cdot>x\<cdot>[] = null\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>any id ==> or\<close> } \<close> lemma "any\<cdot>ID = the_or" by (auto simp add:map_id) text \<open>@{verbatim \<open>all id ==> and\<close> } \<close> lemma "all\<cdot>ID = the_and" by (auto simp add:map_id) text \<open>@{verbatim \<open>(if x then False else y) ==> (not x && y)\<close> } \<close> lemma "(If x then FF else y) = (neg\<cdot>x andalso y)" by (cases x, auto) text \<open>@{verbatim \<open>(if x then y else True) ==> (not x || y)\<close> } \<close> lemma "(If x then y else TT) = (neg\<cdot>x orelse y)" by (cases x, auto) text \<open>@{verbatim \<open>not (not x) ==> x\<close> } \<close> lemma "neg\<cdot>(neg\<cdot>x) = x" by auto text \<open>@{verbatim \<open>(if c then f x else f y) ==> f (if c then x else y)\<close> } \<close> lemma "(If c then f\<cdot>x else f\<cdot>y) \<sqsubseteq> f\<cdot>(If c then x else y)" by (cases c, auto) text \<open>@{verbatim \<open>(\ x -> [x]) ==> (: [])\<close> } \<close> lemma "(\<Lambda> x. [x]) = (\<Lambda> z. z : [])" by auto text \<open>@{verbatim \<open>True == a ==> a\<close> } \<close> lemma "eq\<cdot>TT\<cdot>a = a" by (cases a, auto) text \<open>@{verbatim \<open>False == a ==> not a\<close> } \<close> lemma "eq\<cdot>FF\<cdot>a = neg\<cdot>a" by (cases a, auto) text \<open>@{verbatim \<open>a /= True ==> not a\<close> } \<close> lemma "neq\<cdot>a\<cdot>TT = neg\<cdot>a" by (cases a, auto) text \<open>@{verbatim \<open>a /= False ==> a\<close> } \<close> lemma "neq\<cdot>a\<cdot>FF = a" by (cases a, auto) text \<open>@{verbatim \<open>True /= a ==> not a\<close> } \<close> lemma "neq\<cdot>TT\<cdot>a = neg\<cdot>a" by (cases a, auto) text \<open>@{verbatim \<open>False /= a ==> a\<close> } \<close> lemma "neq\<cdot>FF\<cdot>a = a" by (cases a, auto) text \<open>@{verbatim \<open>not (isNothing x) ==> isJust x\<close> } \<close> lemma "neg\<cdot>(isNothing\<cdot>x) = isJust\<cdot>x" by auto text \<open>@{verbatim \<open>not (isJust x) ==> isNothing x\<close> } \<close> lemma "neg\<cdot>(isJust\<cdot>x) = isNothing\<cdot>x" by auto text \<open>@{verbatim \<open>x == Nothing ==> isNothing x\<close> } \<close> lemma "eq\<cdot>x\<cdot>Nothing = isNothing\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>Nothing == x ==> isNothing x\<close> } \<close> lemma "eq\<cdot>Nothing\<cdot>x = isNothing\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>x /= Nothing ==> Data.Maybe.isJust x\<close> } \<close> lemma "neq\<cdot>x\<cdot>Nothing = isJust\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>Nothing /= x ==> Data.Maybe.isJust x\<close> } \<close> lemma "neq\<cdot>Nothing\<cdot>x = isJust\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>(if isNothing x then y else fromJust x) ==> fromMaybe y x\<close> } \<close> lemma "(If isNothing\<cdot>x then y else fromJust\<cdot>x) = fromMaybe\<cdot>y\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>(if isJust x then fromJust x else y) ==> fromMaybe y x\<close> } \<close> lemma "(If isJust\<cdot>x then fromJust\<cdot>x else y) = fromMaybe\<cdot>y\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>(isJust x && (fromJust x == y)) ==> x == Just y\<close> } \<close> lemma "(isJust\<cdot>x andalso (eq\<cdot>(fromJust\<cdot>x)\<cdot>y)) = eq\<cdot>x\<cdot>(Just\<cdot>y)" by (cases x, auto) text \<open>@{verbatim \<open>elem True ==> or\<close> } \<close> lemma "elem\<cdot>TT = the_or" proof (rule cfun_eqI) fix xs show "elem\<cdot>TT\<cdot>xs = the_or\<cdot>xs" by (induct xs) (auto simp: eq_true) qed text \<open>@{verbatim \<open>notElem False ==> and\<close> } \<close> lemma "notElem\<cdot>FF = the_and" proof (rule cfun_eqI) fix xs show "notElem\<cdot>FF\<cdot>xs = the_and\<cdot>xs" by (induct xs) (auto simp: eq_false) qed text \<open>@{verbatim \<open>all ((/=) a) ==> notElem a\<close> } \<close> lemma "all\<cdot>(neq\<cdot>(a::'a::Eq_sym)) = notElem\<cdot>a" proof (rule cfun_eqI) fix xs show "all\<cdot>(neq\<cdot>a)\<cdot>xs = notElem\<cdot>a\<cdot>xs" by (induct xs) (auto simp: eq_sym) qed text \<open>@{verbatim \<open>maybe x id ==> Data.Maybe.fromMaybe x\<close> } \<close> lemma "maybe\<cdot>x\<cdot>ID = fromMaybe\<cdot>x" proof (rule cfun_eqI) fix xs show "maybe\<cdot>x\<cdot>ID\<cdot>xs = fromMaybe\<cdot>x\<cdot>xs" by (cases xs) auto qed text \<open>@{verbatim \<open>maybe False (const True) ==> Data.Maybe.isJust\<close> } \<close> lemma "maybe\<cdot>FF\<cdot>(const\<cdot>TT) = isJust" proof (rule cfun_eqI) fix x :: "'a Maybe" show "maybe\<cdot>FF\<cdot>(const\<cdot>TT)\<cdot>x = isJust\<cdot>x" by (cases x) simp+ qed text \<open>@{verbatim \<open>maybe True (const False) ==> Data.Maybe.isNothing\<close> } \<close> lemma "maybe\<cdot>TT\<cdot>(const\<cdot>FF) = isNothing" proof (rule cfun_eqI) fix x :: "'a Maybe" show "maybe\<cdot>TT\<cdot>(const\<cdot>FF)\<cdot>x = isNothing\<cdot>x" by (cases x) simp+ qed text \<open>@{verbatim \<open>maybe [] (: []) ==> maybeToList\<close> } \<close> lemma "maybe\<cdot>[]\<cdot>(\<Lambda> z. z : []) = maybeToList" proof (rule cfun_eqI) fix x :: "'a Maybe" show "maybe\<cdot>[]\<cdot>(\<Lambda> z. z : [])\<cdot>x = maybeToList\<cdot>x" by (cases x) simp+ qed text \<open>@{verbatim \<open>catMaybes (map f x) ==> mapMaybe f x\<close> } \<close> lemma "catMaybes\<cdot>(map\<cdot>f\<cdot>x) = mapMaybe\<cdot>f\<cdot>x" by auto text \<open>@{verbatim \<open>(if isNothing x then y else f (fromJust x)) ==> maybe y f x\<close> } \<close> lemma "(If isNothing\<cdot>x then y else f\<cdot>(fromJust\<cdot>x)) = maybe\<cdot>y\<cdot>f\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>(if isJust x then f (fromJust x) else y) ==> maybe y f x\<close> } \<close> lemma "(If isJust\<cdot>x then f\<cdot>(fromJust\<cdot>x) else y) = maybe\<cdot>y\<cdot>f\<cdot>x" by (cases x, auto) text \<open>@{verbatim \<open>(map fromJust . filter isJust) ==> Data.Maybe.catMaybes\<close> } \<close> lemma "(map\<cdot>fromJust oo filter\<cdot>isJust) = catMaybes" proof (rule cfun_eqI) fix xs :: "['a Maybe]" show "(map\<cdot>fromJust oo filter\<cdot>isJust)\<cdot>xs = catMaybes\<cdot>xs" proof (induct xs) case (Cons y ys) then show ?case by (cases y) simp+ qed simp+ qed text \<open>@{verbatim \<open>concatMap (maybeToList . f) ==> Data.Maybe.mapMaybe f\<close> } \<close> lemma "concatMap\<cdot>(maybeToList oo f) = mapMaybe\<cdot>f" proof (rule cfun_eqI) fix xs show "concatMap\<cdot>(maybeToList oo f)\<cdot>xs = mapMaybe\<cdot>f\<cdot>xs" by (induct xs) auto qed text \<open>@{verbatim \<open>concatMap maybeToList ==> catMaybes\<close> } \<close> lemma "concatMap\<cdot>maybeToList = catMaybes" by auto text \<open>@{verbatim \<open>mapMaybe f (map g x) ==> mapMaybe (f . g) x\<close> } \<close> lemma "mapMaybe\<cdot>f\<cdot>(map\<cdot>g\<cdot>x) = mapMaybe\<cdot>(f oo g)\<cdot>x" by auto text \<open>@{verbatim \<open>(($) . f) ==> f\<close> } \<close> lemma "(dollar oo f) = f" by (auto simp add:cfun_eq_iff) text \<open>@{verbatim \<open>(f $) ==> f\<close> } \<close> lemma "(\<Lambda> z. dollar\<cdot>f\<cdot>z) = f" by (auto simp add:cfun_eq_iff) text \<open>@{verbatim \<open>(\ a b -> g (f a) (f b)) ==> g `Data.Function.on` f\<close> } \<close> lemma "(\<Lambda> a b. g\<cdot>(f\<cdot>a)\<cdot>(f\<cdot>b)) = on\<cdot>g\<cdot>f" by (auto simp add:cfun_eq_iff) text \<open>@{verbatim \<open>id $! x ==> x\<close> } \<close> lemma "dollarBang\<cdot>ID\<cdot>x = x" by (auto simp add:seq_def) text \<open>@{verbatim \<open>[x | x <- y] ==> y\<close> } \<close> lemma "[x | x <- y] = y" by (induct y, auto) text \<open>@{verbatim \<open>isPrefixOf (reverse x) (reverse y) ==> isSuffixOf x y\<close> } \<close> lemma "isPrefixOf\<cdot>(reverse\<cdot>x)\<cdot>(reverse\<cdot>y) = isSuffixOf\<cdot>x\<cdot>y" by auto text \<open>@{verbatim \<open>concat (intersperse x y) ==> intercalate x y\<close> } \<close> lemma "concat\<cdot>(intersperse\<cdot>x\<cdot>y) = intercalate\<cdot>x\<cdot>y" by auto text \<open>@{verbatim \<open>x `seq` y ==> y\<close> } \<close> lemma assumes "x \<noteq> \<bottom>" shows "seq\<cdot>x\<cdot>y = y" using assms by (simp add: seq_def) text \<open>@{verbatim \<open>f $! x ==> f x\<close> } \<close> lemma assumes "x \<noteq> \<bottom>" shows "dollarBang\<cdot>f\<cdot>x = f\<cdot>x" using assms by (simp add: seq_def) text \<open>@{verbatim \<open>maybe (f x) (f . g) ==> (f . maybe x g)\<close> } \<close> lemma "maybe\<cdot>(f\<cdot>x)\<cdot>(f oo g) \<sqsubseteq> (f oo maybe\<cdot>x\<cdot>g)" proof (rule cfun_belowI) fix y show "maybe\<cdot>(f\<cdot>x)\<cdot>(f oo g)\<cdot>y \<sqsubseteq> (f oo maybe\<cdot>x\<cdot>g)\<cdot>y" by (cases y) auto qed end
\section{Radial dam break on a dry bed} A radial dam break test problem involving a dry bed. Should show a rarefaction fan. Note that the reference solution is found from the 1D FVM for SWE involving varying width and topography. See a paper of Roberts and Wilson~\cite{RW2011}. \subsection{Results} We should see excellent agreement between the reference and numerical solutions. \begin{figure} \begin{center} \includegraphics[width=0.9\textwidth]{stage_plot.png} \end{center} \caption{Stage results} \end{figure} \begin{figure} \begin{center} \includegraphics[width=0.9\textwidth]{rmom_plot.png} \end{center} \caption{Radial momentum results} \end{figure} \begin{figure} \begin{center} \includegraphics[width=0.9\textwidth]{rvel_plot.png} \end{center} \caption{Radial velocity results} \end{figure} \endinput
[STATEMENT] lemma pCons_0_as_mult:(*TODO: Move *) shows "pCons (0 :: 'a :: comm_semiring_1) = (\<lambda>p. pCons 0 1 * p)" [PROOF STATE] proof (prove) goal (1 subgoal): 1. pCons (0::'a) = (*) (pCons (0::'a) 1) [PROOF STEP] by auto
import game.world_08_advanced_addition namespace mynat theorem mul_pos (a b : mynat) : a β‰  0 β†’ b β‰  0 β†’ a * b β‰  0 := begin[nat_num_game] intros anz bnz f, cases b, { apply bnz, refl }, { rw mul_succ at f, apply anz, induction a, refl, { apply add_left_eq_zero, rwa f, } } end theorem eq_zero_or_eq_zero_of_mul_eq_zero (a b : mynat) (h : a * b = 0) : a = 0 ∨ b = 0 := begin[nat_num_game] induction b, { right, refl }, { left, rw mul_succ at h, apply add_left_eq_zero, apply h } end theorem mul_eq_zero_iff (a b : mynat): a * b = 0 ↔ a = 0 ∨ b = 0 := begin[nat_num_game] split, apply eq_zero_or_eq_zero_of_mul_eq_zero, { intro or_eq_zero, cases or_eq_zero, rwa [or_eq_zero, zero_mul], rwa [or_eq_zero, mul_zero], } end theorem mul_left_cancel (a b c : mynat) (ha : a β‰  0) : a * b = a * c β†’ b = c := begin[nat_num_game] induction c generalizing b, { rw mul_zero, intro h, cases eq_zero_or_eq_zero_of_mul_eq_zero _ _ h, { exfalso, exact ha h_1 }, exact h_1 }, { intro eq, cases b, { rw mul_zero at eq, exfalso, apply ha, symmetry at eq, cases eq_zero_or_eq_zero_of_mul_eq_zero _ _ eq, exact h, { exfalso, exact succ_ne_zero _ h, } },{ have hyp : b = c_n, { apply c_ih, rw mul_succ at eq, rw mul_succ at eq, apply add_right_cancel _ _ _ eq, }, rwa hyp, } } end end mynat
Require Import Coq.Reals.Rdefinitions. Require Import Coq.Reals.RIneq. Require Import Logic.Logic. Require Import Logic.ArithFacts. Open Scope HP_scope. Open Scope string_scope. (* The distance traveled starting with velocity v, acceleration a, traveling for time t. *) Definition tdist (v:Term) (a:Term) (t:Term) : Term := v*t + (/2)%R*a*t^^2. (* Some useful lemmas about tdist. *) Lemma tdist_incr : forall v1 v2 a1 a2 d1 d2, |-- v1 <= v2 -->> a1 <= a2 -->> d1 <= d2 -->> 0 <= a2 -->> 0 <= d1 -->> 0 <= tdist v2 a2 d2 -->> tdist v1 a1 d1 <= tdist v2 a2 d2. Proof. breakAbstraction; simpl; unfold eval_comp; simpl; intros. repeat match goal with | [ _ : context [eval_term ?t ?s1 ?s2] |- _ ] => generalize dependent (eval_term t s1 s2) end; intros; repeat match goal with | [ _ : context [eval_term ?t ?s1 ?s2] |- _ ] => generalize dependent (eval_term t s1 s2) end; intros. match goal with |- (?e <= _)%R => destruct (Rle_dec 0 e) end; solve_linear. destruct H4; repeat match goal with | [ H : @eq R _ _ |- _ ] => rewrite <- H end; solve_linear. apply Rle_trans with (r2:=((r3 + /2*r2*r0)*r0)%R); solve_linear. apply Rle_trans with (r2:=((r1 + /2*r*r4)*r4)%R); solve_linear. apply Rmult_le_compat; solve_linear. - eapply Rmult_le_lt_0; eauto; solve_linear. - solve_nonlinear. Qed. Lemma tdist_vel_neg : forall v a t, |-- 0 <= t -->> v <= 0 -->> v + a*t <= 0 -->> tdist v a t <= 0. Proof. solve_nonlinear. Qed. Lemma tdist_neg : forall v1 v2 a1 a2 d1 d2, |-- v1 <= v2 -->> a1 <= a2 -->> d1 <= d2 -->> 0 <= a2 -->> 0 <= d1 -->> tdist v2 a2 d2 <= 0 -->> tdist v1 a1 d1 <= 0. Proof. breakAbstraction; simpl; unfold eval_comp; simpl; intros. repeat match goal with | [ _ : context [eval_term ?t ?s1 ?s2] |- _ ] => generalize dependent (eval_term t s1 s2) end; intros; repeat match goal with | [ _ : context [eval_term ?t ?s1 ?s2] |- _ ] => generalize dependent (eval_term t s1 s2) end; intros. match goal with |- (?e <= _)%R => destruct (Rle_dec 0 e) end; solve_linear. destruct H4; repeat match goal with | [ H : @eq R _ _ |- _ ] => rewrite <- H end; solve_linear. apply Rle_trans with (r2:=((r3 + /2*r2*r0)*r0)%R); solve_linear. apply Rle_trans with (r2:=((r1 + /2*r*r4)*r4)%R); solve_linear. apply Rmult_le_compat; solve_linear. - eapply Rmult_le_lt_0; eauto; solve_linear. - solve_nonlinear. Qed. Lemma tdist_pos : forall v a t, |-- 0 <= v -->> 0 <= a -->> 0 <= t -->> 0 <= tdist v a t. Proof. solve_nonlinear. Qed. Lemma tdist_incr_acc : forall a1 a2 v t, |-- a1 <= a2 -->> tdist v a1 t <= tdist v a2 t. Proof. solve_nonlinear. Qed. (* Generic parameters of the height shims. *) Module Type SdistParams. (* Our breaking acceleration. *) Variable amin : R. Hypothesis amin_lt_0 : (amin < 0)%R. End SdistParams. (* Definitions for implementing the height shims in the source language. *) Module SdistUtil (Import Params : SdistParams). (* The distance traveled before stopping when applying acceleration amin, starting with velocity v. *) Definition sdist (v:Term) : Term := (v^^2)*(--(/2)%R)*(/Params.amin)%R. (* Some useful lemmas about sdist. *) Lemma tdist_sdist_incr : forall v1 v2 a1 a2 d1 d2, |-- v1 <= v2 -->> a1 <= a2 -->> d1 <= d2 -->> 0 <= a2 -->> 0 <= d1 -->> 0 <= v1 + a1*d1 -->> tdist v1 a1 d1 + sdist (v1 + a1*d1) <= tdist v2 a2 d2 + sdist (v2 + a2*d2). Proof. pose proof Params.amin_lt_0. breakAbstraction; unfold eval_comp; simpl; intros. repeat match goal with | [ _ : context [eval_term ?t ?s1 ?s2] |- _ ] => generalize dependent (eval_term t s1 s2) end; intros; repeat match goal with | [ _ : context [eval_term ?t ?s1 ?s2] |- _ ] => generalize dependent (eval_term t s1 s2) end; intros. apply Rplus_le_algebra. apply Rmult_neg_le_algebra with (r2:=Params.amin); auto. rewrite Rminus_0_l. apply Rmult_pos_ge_algebra with (r2:=(2)%R); solve_linear. R_simplify; simpl; solve_linear. solve_nonlinear. Qed. Lemma sdist_tdist : forall v t, |-- tdist v Params.amin t <= sdist v. Proof. pose proof Params.amin_lt_0. breakAbstraction; simpl; unfold eval_comp; simpl; intros. apply Rplus_le_algebra. apply Rmult_neg_le_algebra with (r2:=Params.amin); auto. apply Rmult_neg_ge_algebra with (r2:=(-4)%R); solve_linear. R_simplify; solve_linear. solve_nonlinear. Qed. Lemma sdist_tdist_tdist : forall v t, |-- tdist v Params.amin t + sdist (v + Params.amin*t) <= sdist v. Proof. pose proof Params.amin_lt_0. breakAbstraction; simpl; unfold eval_comp; simpl; intros. apply Rplus_le_algebra. apply Rmult_neg_le_algebra with (r2:=Params.amin); auto. apply Rmult_neg_ge_algebra with (r2:=(-4)%R); solve_linear. R_simplify; solve_linear. Qed. Lemma sdist_incr : forall v1 v2, |-- 0 <= v1 <= v2 -->> sdist v1 <= sdist v2. Proof. pose proof Params.amin_lt_0. breakAbstraction; simpl; unfold eval_comp; simpl; intros. do 2 rewrite (Rmult_assoc _ (0 - / 2) (/ Params.amin))%R. apply Rmult_le_compat; solve_linear. - apply Rmult_0_le; solve_linear. - assert (/ Params.amin < 0)%R by solve_linear. solve_linear. - apply Rmult_le_compat; solve_linear. Qed. Lemma sdist_gt_0 : forall v, |-- 0 <= sdist v. Proof. breakAbstraction. intros. pose proof Params.amin_lt_0. assert (/Params.amin < 0)%R by solve_linear. assert (0 - / 2 < 0)%R by solve_linear. generalize dependent (/Params.amin)%R. generalize dependent (0 - / 2)%R. clear H0. solve_nonlinear. Qed. End SdistUtil.
(* Title: HOL/Examples/Peirce.thy Author: Makarius *) section \<open>Peirce's Law\<close> theory Peirce imports MainRLT begin text \<open> We consider Peirce's Law: \<open>((A \<longrightarrow> B) \<longrightarrow> A) \<longrightarrow> A\<close>. This is an inherently non-intuitionistic statement, so its proof will certainly involve some form of classical contradiction. The first proof is again a well-balanced combination of plain backward and forward reasoning. The actual classical step is where the negated goal may be introduced as additional assumption. This eventually leads to a contradiction.\<^footnote>\<open>The rule involved there is negation elimination; it holds in intuitionistic logic as well.\<close>\<close> theorem "((A \<longrightarrow> B) \<longrightarrow> A) \<longrightarrow> A" proof assume "(A \<longrightarrow> B) \<longrightarrow> A" show A proof (rule classical) assume "\<not> A" have "A \<longrightarrow> B" proof assume A with \<open>\<not> A\<close> show B by contradiction qed with \<open>(A \<longrightarrow> B) \<longrightarrow> A\<close> show A .. qed qed text \<open> In the subsequent version the reasoning is rearranged by means of ``weak assumptions'' (as introduced by \<^theory_text>\<open>presume\<close>). Before assuming the negated goal \<open>\<not> A\<close>, its intended consequence \<open>A \<longrightarrow> B\<close> is put into place in order to solve the main problem. Nevertheless, we do not get anything for free, but have to establish \<open>A \<longrightarrow> B\<close> later on. The overall effect is that of a logical \<^emph>\<open>cut\<close>. Technically speaking, whenever some goal is solved by \<^theory_text>\<open>show\<close> in the context of weak assumptions then the latter give rise to new subgoals, which may be established separately. In contrast, strong assumptions (as introduced by \<^theory_text>\<open>assume\<close>) are solved immediately. \<close> theorem "((A \<longrightarrow> B) \<longrightarrow> A) \<longrightarrow> A" proof assume "(A \<longrightarrow> B) \<longrightarrow> A" show A proof (rule classical) presume "A \<longrightarrow> B" with \<open>(A \<longrightarrow> B) \<longrightarrow> A\<close> show A .. next assume "\<not> A" show "A \<longrightarrow> B" proof assume A with \<open>\<not> A\<close> show B by contradiction qed qed qed text \<open> Note that the goals stemming from weak assumptions may be even left until qed time, where they get eventually solved ``by assumption'' as well. In that case there is really no fundamental difference between the two kinds of assumptions, apart from the order of reducing the individual parts of the proof configuration. Nevertheless, the ``strong'' mode of plain assumptions is quite important in practice to achieve robustness of proof text interpretation. By forcing both the conclusion \<^emph>\<open>and\<close> the assumptions to unify with the pending goal to be solved, goal selection becomes quite deterministic. For example, decomposition with rules of the ``case-analysis'' type usually gives rise to several goals that only differ in there local contexts. With strong assumptions these may be still solved in any order in a predictable way, while weak ones would quickly lead to great confusion, eventually demanding even some backtracking. \<close> end
import ReactorModel.Objects.Reaction open Reactor (Component) class ReactorType (Ξ± : Type) where ports : Ξ± β†’ Kind β†’ ID ⇀ Value acts : Ξ± β†’ ID ⇀ Action state : Ξ± β†’ ID ⇀ Value rcns : Ξ± β†’ ID ⇀ Reaction nest : Ξ± β†’ ID ⇀ Ξ± namespace ReactorType abbrev cptType [ReactorType Ξ±] : Component β†’ Type | .rtr => Ξ± | .rcn => Reaction | .val cpt => cpt.type abbrev cpt? [inst : ReactorType Ξ±] : (cpt : Component) β†’ Ξ± β†’ ID ⇀ inst.cptType cpt | .rtr => nest | .rcn => rcns | .prt k => (ports Β· k) | .act => acts | .stv => state inductive Member [ReactorType Ξ±] (cpt : Component) (i : ID) : Ξ± β†’ Type _ | final : (i ∈ cpt? cpt rtr) β†’ Member cpt i rtr | nest : (nest rtr₁ j = some rtrβ‚‚) β†’ (m : Member cpt i rtrβ‚‚) β†’ Member cpt i rtr₁ class Extensional (Ξ±) extends ReactorType Ξ± where ext_iff : rtr₁ = rtrβ‚‚ ↔ (ports rtr₁ = ports rtrβ‚‚) ∧ (acts rtr₁ = acts rtrβ‚‚) ∧ (state rtr₁ = state rtrβ‚‚) ∧ (rcns rtr₁ = rcns rtrβ‚‚) ∧ (nest rtr₁ = nest rtrβ‚‚) @[ext] theorem Extensional.ext [inst : Extensional Ξ±] {rtr₁ rtrβ‚‚ : Ξ±} : (ports rtr₁ = ports rtrβ‚‚) ∧ (acts rtr₁ = acts rtrβ‚‚) ∧ (state rtr₁ = state rtrβ‚‚) ∧ (rcns rtr₁ = rcns rtrβ‚‚) ∧ (nest rtr₁ = nest rtrβ‚‚) β†’ rtr₁ = rtrβ‚‚ := inst.ext_iff.mpr end ReactorType
open Function theorem constApply {y : Ξ²} {x : Ξ±} : const Ξ± y x = y := rfl example (p : Nat β†’ Prop) (h : p 1) : p (const Nat 1 2) := by simp only [constApply]; trace_state; exact h -- simplifies example (p : Ξ± β†’ Prop) (h : p a) : p (const Nat a 2) := by simp only [constApply]; trace_state; exact h -- simplifies example (f : Ξ± β†’ Ξ±) (p : Ξ± β†’ Prop) (h : p (f a)) : p (const Nat (f a) 2) := by simp only [constApply]; trace_state; exact h -- simplifies
Formal statement is: lemma degree_0 [simp]: "degree 0 = 0" Informal statement is: The degree of the zero polynomial is zero.
open import Coinduction using ( ∞ ) open import Data.ByteString using ( ByteString ; strict ; lazy ) open import Data.String using ( String ) module System.IO.Primitive where infixl 1 _>>=_ -- The Unit type and its sole inhabitant postulate Unit : Set unit : Unit {-# COMPILED_TYPE Unit () #-} {-# COMPILED unit () #-} -- The IO type and its primitives -- TODO: Make these universe-polymorphic once the compiler supports it postulate IO : Set β†’ Set return : {A : Set} β†’ A β†’ (IO A) _>>=_ : {A B : Set} β†’ (IO A) β†’ (A β†’ (IO B)) β†’ (IO B) inf : {A : Set} β†’ ∞(IO A) β†’ (IO A) {-# BUILTIN IO IO #-} {-# COMPILED_TYPE IO IO #-} {-# COMPILED return (\ _ a -> return a) #-} {-# COMPILED _>>=_ (\ _ _ a f -> a >>= f) #-} -- {-# COMPILED inf (\ _ a -> a) #-} -- The commitment primitives postulate commit : IO Unit onCommit : (IO Unit) β†’ (IO Unit) {-# IMPORT System.IO.AgdaFFI #-} {-# COMPILED commit System.IO.AgdaFFI.commit #-} {-# COMPILED onCommit System.IO.AgdaFFI.onCommit #-} -- The low-level binary handle primitives. -- TODO: Should the string etc. functions be built on top of -- the binary functions, or should we link directly to the Haskell -- string functions? -- TODO: Think about append and read-write modes. postulate HandleR : Set stdin : HandleR hOpenR : String β†’ (IO HandleR) hGetLazy : HandleR β†’ (IO (ByteString lazy)) hGetStrict : HandleR β†’ (IO (ByteString strict)) hCloseR : HandleR β†’ (IO Unit) {-# IMPORT System.IO #-} {-# COMPILED_TYPE HandleR System.IO.Handle #-} {-# COMPILED stdin System.IO.stdin #-} {-# COMPILED hOpenR System.IO.AgdaFFI.hOpen System.IO.ReadMode #-} {-# COMPILED hGetStrict System.IO.AgdaFFI.hGetStrict #-} {-# COMPILED hGetLazy System.IO.AgdaFFI.hGetLazy #-} {-# COMPILED hCloseR System.IO.AgdaFFI.hClose #-} postulate HandleW : Set stdout : HandleW stderr : HandleW hOpenW : String β†’ (IO HandleW) hPutLazy : HandleW β†’ (ByteString lazy) β†’ (IO Unit) hPutStrict : HandleW β†’ (ByteString strict) β†’ (IO Unit) hCloseW : HandleW β†’ (IO Unit) {-# COMPILED_TYPE HandleW System.IO.Handle #-} {-# COMPILED stdout System.IO.stdout #-} {-# COMPILED stderr System.IO.stderr #-} {-# COMPILED hOpenW System.IO.AgdaFFI.hOpen System.IO.WriteMode #-} {-# COMPILED hPutStrict System.IO.AgdaFFI.hPutStrict #-} {-# COMPILED hPutLazy System.IO.AgdaFFI.hPutLazy #-} {-# COMPILED hCloseW System.IO.AgdaFFI.hClose #-}
lemma homeomorphic_to_disc: assumes S: "S \<noteq> {}" and prev: "S = UNIV \<or> (\<exists>f g. f holomorphic_on S \<and> g holomorphic_on ball 0 1 \<and> (\<forall>z \<in> S. f z \<in> ball 0 1 \<and> g(f z) = z) \<and> (\<forall>z \<in> ball 0 1. g z \<in> S \<and> f(g z) = z))" (is "_ \<or> ?P") shows "S homeomorphic ball (0::complex) 1"
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Compile the Simulink model through Matlab command line. %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% disp('Compiling the Simulink model...') pause(1.5) try positionControlBalancing([], [], [], 'compile') positionControlBalancing([], [], [], 'term') catch ME errorMessages = ME; end clc disp('Compilation done.') % warning about Simulink timing warning('The model will anyways start with FEW SECONDS OF DELAY after pressing the ''Start Model'' button.')
{-# OPTIONS --safe --erased-cubical #-} module Erased-cubical where -- Modules that use --cubical can be imported. open import Erased-cubical.Cubical-again -- Code from such modules that was originally defined in modules using -- --without-K or --erased-cubical can be used without restrictions. _ : {A : Set} β†’ A β†’ βˆ₯ A βˆ₯ _ = ∣_∣ _ : D _ = c -- Matching on an erased constructor that was defined in a module that -- uses --cubical is fine, and makes it possible to use erased -- definitions in the right-hand side. f : Dβ€² β†’ Dβ€² f cβ€² = cβ€²
import data.bool import data.int.gcd import tactic def mod' (a: β„€) (b: β„•): β„€ := a - b * ((a + b/2) / b) theorem mod'_eq (a: β„€) (b: β„•): mod' a b = ((a + b / 2) % b - b / 2) := begin simp [mod', int.mod_def], linarith, end theorem mod_lower_bound (a: β„€) (b: β„•) (h: b β‰  0): mod' a b β‰₯ -((b: β„€) / 2) := by simp [mod'_eq, int.mod_nonneg, int.coe_nat_ne_zero, h] theorem div_mul_gt (a: β„€) { b: β„€ } (h: b > 0): a / b * b > a - b := begin have := int.div_add_mod a b, have : (a / b) * b = a - a % b := by linarith, rw this, have := int.mod_lt a (ne_of_gt h), rw int.abs_eq_nat_abs at this, rw int.nat_abs_of_nonneg (le_of_lt h) at this, linarith, end theorem mod_upper_bound (a: β„€) (b: β„•) (h: b β‰  0): (mod' a b) ≀ (b: β„€) / 2 := begin set m := (a + (b: β„€) / 2) % b with m_def, suffices : m ≀ (b: β„€) / 2 + (b: β„€) / 2, { rw [mod'_eq], linarith, }, have : m ≀ (b: β„€) - 1 := begin have := int.mod_lt (a + (b: β„€) / 2) (int.coe_nat_ne_zero.mpr h), simp at this, exact int.le_sub_one_iff.mpr this, end, have c : (b: β„€) - 1 ≀ (b: β„€) / 2 * 2 := begin have : (2: β„€) > 0 := by linarith, have := div_mul_gt b this, linarith, end, linarith, end theorem mod_abs_bound (a: β„€) (b: β„•) (h: b β‰  0): (mod' a b).nat_abs ≀ b / 2 := begin rw ←int.coe_nat_le, rw ←int.abs_eq_nat_abs, rw abs_le, exact and.intro (mod_lower_bound a b h) (mod_upper_bound a b h), end
module TelescopingLet3 where module Star where β˜… : Set₁ β˜… = Set β˜…β‚ : Setβ‚‚ β˜…β‚ = Set₁ module MEndo (open Star) (A : β˜…) where Endo = A β†’ A -- at this point, β˜… should no longer be in scope data D3 (open Star using (β˜…β‚)) : β˜…β‚ where c : (A : β˜…) β†’ D3 -- β˜…β‚ is in scope -- β˜… is not in scope since it was not brought in scope -- (not included in the using ())
/- Copyright (c) 2014 Floris van Doorn (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import data.nat.pow import data.nat.bits /-! > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Lemmas about `size`. -/ namespace nat /-! ### `shiftl` and `shiftr` -/ lemma shiftl_eq_mul_pow (m) : βˆ€ n, shiftl m n = m * 2 ^ n | 0 := (nat.mul_one _).symm | (k+1) := show bit0 (shiftl m k) = m * (2 * 2 ^ k), by rw [bit0_val, shiftl_eq_mul_pow, mul_left_comm, mul_comm 2] lemma shiftl'_tt_eq_mul_pow (m) : βˆ€ n, shiftl' tt m n + 1 = (m + 1) * 2 ^ n | 0 := by simp [shiftl, shiftl', pow_zero, nat.one_mul] | (k+1) := begin change bit1 (shiftl' tt m k) + 1 = (m + 1) * (2 * 2 ^ k), rw bit1_val, change 2 * (shiftl' tt m k + 1) = _, rw [shiftl'_tt_eq_mul_pow, mul_left_comm, mul_comm 2], end lemma one_shiftl (n) : shiftl 1 n = 2 ^ n := (shiftl_eq_mul_pow _ _).trans (nat.one_mul _) @[simp] lemma zero_shiftl (n) : shiftl 0 n = 0 := (shiftl_eq_mul_pow _ _).trans (nat.zero_mul _) lemma shiftr_eq_div_pow (m) : βˆ€ n, shiftr m n = m / 2 ^ n | 0 := (nat.div_one _).symm | (k+1) := (congr_arg div2 (shiftr_eq_div_pow k)).trans $ by rw [div2_val, nat.div_div_eq_div_mul, mul_comm]; refl @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := (shiftr_eq_div_pow _ _).trans (nat.zero_div _) theorem shiftl'_ne_zero_left (b) {m} (h : m β‰  0) (n) : shiftl' b m n β‰  0 := by induction n; simp [bit_ne_zero, shiftl', *] theorem shiftl'_tt_ne_zero (m) : βˆ€ {n} (h : n β‰  0), shiftl' tt m n β‰  0 | 0 h := absurd rfl h | (succ n) _ := nat.bit1_ne_zero _ /-! ### `size` -/ @[simp] theorem size_zero : size 0 = 0 := by simp [size] @[simp] theorem size_bit {b n} (h : bit b n β‰  0) : size (bit b n) = succ (size n) := begin rw size, conv { to_lhs, rw [binary_rec], simp [h] }, rw div2_bit, end @[simp] theorem size_bit0 {n} (h : n β‰  0) : size (bit0 n) = succ (size n) := @size_bit ff n (nat.bit0_ne_zero h) @[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) := @size_bit tt n (nat.bit1_ne_zero n) @[simp] theorem size_one : size 1 = 1 := show size (bit1 0) = 1, by rw [size_bit1, size_zero] @[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n β‰  0) : size (shiftl' b m n) = size m + n := begin induction n with n IH; simp [shiftl'] at h ⊒, rw [size_bit h, nat.add_succ], by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]], rw s0 at h ⊒, cases b, {exact absurd rfl h}, have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0, rw [shiftl'_tt_eq_mul_pow] at this, obtain rfl := succ.inj (eq_one_of_dvd_one ⟨_, this.symm⟩), rw one_mul at this, obtain rfl : n = 0 := nat.eq_zero_of_le_zero (le_of_not_gt $ Ξ» hn, ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this), refl end @[simp] theorem size_shiftl {m} (h : m β‰  0) (n) : size (shiftl m n) = size m + n := size_shiftl' (shiftl'_ne_zero_left _ h _) theorem lt_size_self (n : β„•) : n < 2^size n := begin rw [← one_shiftl], have : βˆ€ {n}, n = 0 β†’ n < shiftl 1 (size n), { simp }, apply binary_rec _ _ n, {apply this rfl}, intros b n IH, by_cases bit b n = 0, {apply this h}, rw [size_bit h, shiftl_succ], exact bit_lt_bit0 _ IH end theorem size_le {m n : β„•} : size m ≀ n ↔ m < 2^n := ⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h), begin rw [← one_shiftl], revert n, apply binary_rec _ _ m, { intros n h, simp }, { intros b m IH n h, by_cases e : bit b m = 0, { simp [e] }, rw [size_bit e], cases n with n, { exact e.elim (nat.eq_zero_of_le_zero (le_of_lt_succ h)) }, { apply succ_le_succ (IH _), apply lt_imp_lt_of_le_imp_le (Ξ» h', bit0_le_bit _ h') h } } end⟩ theorem lt_size {m n : β„•} : m < size n ↔ 2^m ≀ n := by rw [← not_lt, decidable.iff_not_comm, not_lt, size_le] theorem size_pos {n : β„•} : 0 < size n ↔ 0 < n := by rw lt_size; refl theorem size_eq_zero {n : β„•} : size n = 0 ↔ n = 0 := by have := @size_pos n; simp [pos_iff_ne_zero] at this; exact decidable.not_iff_not.1 this theorem size_pow {n : β„•} : size (2^n) = n+1 := le_antisymm (size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _)) (lt_size.2 $ le_rfl) theorem size_le_size {m n : β„•} (h : m ≀ n) : size m ≀ size n := size_le.2 $ lt_of_le_of_lt h (lt_size_self _) lemma size_eq_bits_len (n : β„•) : n.bits.length = n.size := begin induction n using nat.binary_rec' with b n h ih, { simp, }, rw [size_bit, bits_append_bit _ _ h], { simp [ih], }, { simpa [bit_eq_zero_iff], } end end nat
# algorithm/z-test.jl using Test @test ncg(:test) @test ogm_ls(:test) @test pogm_restart(:test)
Require Import ssreflect ssrbool eqtype ssrnat seq. Set Implicit Arguments. Require Import bigop Omega fintype. Definition f (n:nat) := (\sum_(i<n.+1) i). Lemma S10 : f(10)=55. Theorem sommation : forall (n : nat), 2 * sumn (iota 1 n) = n*(n.+1). induction n; first trivial. replace (n.+1) with (n+1) by ring. rewrite (iota_add ). rewrite sumn_cat. simpl. ring_simplify. rewrite IHn. ring. Qed. Theorem sommation_ssr n : 2 * sumn (iota 1 n) = n * (n + 1). Proof. elim: n => [//= | n IHn]. (* rewrite -{1 3}[n.+1]addn1. *) rewrite -(addn1 n) iota_add sumn_cat. ring_simplify. rewrite IHn. simpl. ring. Qed. Theorem sommation3 n : 2 * sumn (iota 1 n) = n * (n + 1). Proof. elim: n => [//= | n IHn]. (* rewrite -{1 3}[n.+1]addn1. *) rewrite -(addn1 n). rewrite iota_add /= sumn_cat !mulnDr IHn /= mulnC -!addnA. congr (_ + _). by rewrite muln0 !muln1 /= !add0n addn0 [RHS]addnA [RHS]addnC [n + 1]addnC. Qed. Theorem sommation4 n : 2 * (\sum_(i<n.+1) i) = n * (n + 1). elim : n => [|n IHn]. rewrite big_ord_recr. rewrite big_ord0. reflexivity. replace (\sum_(i < n.+2) i) with ((\sum_(i < n.+1) i) + n.+1). rewrite mulnDr. rewrite IHn. ring. symmetry. apply big_ord_recr. Qed.
(* Title: Schutz_Spacetime/TemporalOrderExtra.thy Authors: Richard Schmoetten, Jake Palmer and Jacques D. Fleuriot University of Edinburgh, 2021 *) theory TemporalOrderExtra imports Main Minkowski TernaryOrdering TemporalOrderOnPath "HOL-Algebra.Order" begin section \<open>Ternary-to-binary\<close> context MinkowskiSpacetime begin \<comment> \<open>The cases we need to cover where x <= y if we take a < b as our ordering basis: [abxy], [axby], [axyb], [xaby], [xayb], [xyab]. \<close> abbreviation betw_gorder :: "'a \<Rightarrow> 'a \<Rightarrow> 'a set \<Rightarrow> 'a gorder" where "betw_gorder a b Q \<equiv> \<lparr>carrier = Q, eq = (=), le = (\<lambda>x y. x = y \<comment> \<open>Base case. Everything from here is < really.\<close> \<or> x = a \<and> y = b \<comment> \<open>a < y\<noteq>b case (\<noteq> fact is covered by [___].\<close> \<or> x = a \<and> ([a;b;y] \<or> [a;y;b]) \<comment> \<open>b < y case. Can't have [b a y].\<close> \<or> x = b \<and> [a;b;y] \<comment> \<open>x < a\<close> \<or> y = a \<and> [x;a;b] \<comment> \<open>x < b\<close> \<or> y = b \<and> ([x;a;b] \<or> [a;x;b]) \<comment> \<open>4-event cases.\<close> \<or> [a;b;x;y] \<or> [a;x;b;y] \<or> [a;x;y;b] \<or> [x;a;b;y] \<or> [x;a;y;b] \<or> [x;y;a;b] ) \<rparr>" lemma betw_total_order_on_path: fixes a b :: 'a and Q :: "'a set" assumes "Q \<in> \<P>" and "a \<in> Q" and "b \<in> Q" and "a \<noteq> b" shows "total_order (betw_gorder a b Q)" (is "total_order ?o") proof show "\<And>x. x \<in> carrier ?o \<Longrightarrow> x .=\<^bsub>?o\<^esub> x" by simp next show "\<And>x y. \<lbrakk>x .=\<^bsub>?o\<^esub> y; x \<in> carrier ?o; y \<in> carrier ?o\<rbrakk> \<Longrightarrow> y .=\<^bsub>?o\<^esub> x" by simp next show "\<And>x y z. \<lbrakk>x .=\<^bsub>?o\<^esub> y; y .=\<^bsub>?o\<^esub> z; x \<in> carrier ?o; y \<in> carrier ?o; z \<in> carrier ?o\<rbrakk> \<Longrightarrow> x .=\<^bsub>?o\<^esub> z" by simp next show "\<And>x. x \<in> carrier ?o \<Longrightarrow> x \<sqsubseteq>\<^bsub>?o\<^esub> x" by simp next show "\<And>x y. \<lbrakk>x \<sqsubseteq>\<^bsub>?o\<^esub> y; y \<sqsubseteq>\<^bsub>?o\<^esub> x; x \<in> carrier ?o; y \<in> carrier ?o\<rbrakk> \<Longrightarrow> x .=\<^bsub>?o\<^esub> y" by (smt abc_abc_neq abcd_dcba_only(18) betw4_sym betw4_weak eq_object.select_convs(1) gorder.select_convs(1)) next show "\<And>x y z. \<lbrakk>x \<sqsubseteq>\<^bsub>?o\<^esub> y; y \<sqsubseteq>\<^bsub>?o\<^esub> z; x \<in> carrier ?o; y \<in> carrier ?o; z \<in> carrier ?o\<rbrakk> \<Longrightarrow> x \<sqsubseteq>\<^bsub>?o\<^esub> z" by (smt (z3) abc_acd_abd abc_acd_bcd abc_bcd_acd abc_only_cba(1,4) assms(1) gorder.select_convs(1) partial_object.select_convs(1) some_betw2) next show "\<And>x y z w. \<lbrakk>x .=\<^bsub>?o\<^esub> y; z .=\<^bsub>?o\<^esub> w; x \<in> carrier ?o; y \<in> carrier ?o; z \<in> carrier ?o; w \<in> carrier ?o\<rbrakk> \<Longrightarrow> (x \<sqsubseteq>\<^bsub>?o\<^esub> z) = (y \<sqsubseteq>\<^bsub>?o\<^esub> w)" by simp next show "(.=\<^bsub>?o\<^esub>) = (=)" by simp next show "\<And>x y. \<lbrakk>x \<in> carrier (betw_gorder a b Q); y \<in> carrier (betw_gorder a b Q)\<rbrakk> \<Longrightarrow> x \<sqsubseteq>\<^bsub>betw_gorder a b Q\<^esub> y \<or> y \<sqsubseteq>\<^bsub>betw_gorder a b Q\<^esub> x" by (smt (z3) abc_sym assms gorder.select_convs(1) partial_object.select_convs(1) some_betw2 some_betw4b) qed end end
import data.set.basic import data.list import logic.relator #check list.forallβ‚‚ namespace MCL_untyped inductive type | int | float open type instance : decidable_eq type := sorry @[reducible] def type_map : type β†’ Type | int := β„• | float := β„• prefix β–Έ := type_map @[reducible] def untyped_value := β„• @[reducible] def var_map : Type := Ξ  t : type, string β†’ list β„• β†’ β–Έt def var_map.update (t : type) (n : string) (n_idx : list β„•) (v : β–Έt) (s : var_map) := Ξ» t' m m_idx, if c : (n = m ∧ n_idx = m_idx ∧ t = t') then (begin rw [and.right (and.right c)] at v, exact v, end) else s t' m m_idx @[simp] lemma var_map_update_get {t n idx v} {a : var_map} : a.update t n idx v t n idx = v := begin unfold var_map.update, simp, refl, end structure state := (global : var_map) def state.updateGloabl (g : var_map) (s : state) : state := {global := g, ..s} structure declaration := (scope : β„•)(type : type)(nridx : β„•) def signature := string β†’ option declaration inductive expression : Type | var (n : string) (idx : list expression) : expression | add : expression β†’ expression β†’ expression | literal_int {} (n : β„•) : expression instance : has_add expression := ⟨expression.add⟩ instance : has_zero expression := ⟨expression.literal_int 0⟩ instance : has_one expression := ⟨expression.literal_int 1⟩ open expression inductive compute_typed_expression (sig : signature) (s : state) : Ξ  t : type, expression β†’ β–Έt β†’ Prop | global_var (n : string) (d) {t : type} {idx_expr idx_evaled} (hs : sig n = some d) (ht : t = d.type) (hi : list.forallβ‚‚ (compute_typed_expression int) idx_expr idx_evaled) : compute_typed_expression t (var n idx_expr) (s.global t n idx_evaled) -- equality as hypthoses allows to to call cases on hβ‚‚ in compute_typed_expression_unique | add {e₁ eβ‚‚ n₁ nβ‚‚} (h₁ : compute_typed_expression int e₁ n₁) (hβ‚‚ : compute_typed_expression int eβ‚‚ nβ‚‚) : compute_typed_expression int (add e₁ eβ‚‚) (n₁ + nβ‚‚) | literal {n} : compute_typed_expression int (literal_int n) n #check compute_typed_expression.literal @[simp] -- causes the empty list to be simplified immediately (no unfold required) def compute_expr_list (t sig s) (idx_expr : list expression) (idx_evaled : list (β–Έt)) := list.forallβ‚‚ (Ξ» expr eval, compute_typed_expression sig s t expr eval) idx_expr idx_evaled @[simp] def compute_idx_expr := compute_expr_list int inductive program | assign (n : string) : list (expression) β†’ expression β†’ program | seq : program β†’ program β†’ program | loop (n : string) : expression β†’ program β†’ program | skip : program infixr ` ;; `:90 := program.seq open program inductive big_step : (program Γ— signature Γ— state) β†’ state β†’ Prop | assign_global {t : type} {n expr d} {sig : signature} {val} {s : state} {idx_expr : list expression} {idx_evaled : list β„•} (ht : sig n = some d) (h_eval : compute_typed_expression sig s d.type expr val) (h_idx : compute_idx_expr sig s idx_expr idx_evaled) : big_step ((assign n idx_expr expr), sig, s) { global := s.global.update d.type n idx_evaled val , ..s } | seq {s u v sig p₁ pβ‚‚} (hp₁ : big_step (p₁, sig, s) u) (hpβ‚‚ : big_step (pβ‚‚, sig, u) v) : big_step (seq p₁ pβ‚‚, sig, s) v infix ` ⟹ `:110 := big_step lemma compute_typed_expression_unique {sig s t expr r₁ rβ‚‚} (hβ‚‚ : compute_typed_expression sig s t expr rβ‚‚) (h₁ : compute_typed_expression sig s t expr r₁) : r₁ = rβ‚‚ := begin induction h₁, { cases hβ‚‚, have : h₁_idx_evaled = h₁_idx_evaled := begin have : relator.right_unique (compute_typed_expression sig s int) := begin unfold relator.right_unique, intros expr val₁ valβ‚‚ h₁ hβ‚‚, -- PROBLEM: i don't get IH for (list.forallβ‚‚ (compute_typed_expression)) because the recursor does not support it sorry end, sorry, --apply list.right_unique_forallβ‚‚, end, sorry, -- refl, }, { cases hβ‚‚, have : h₁_n₁ = hβ‚‚_n₁ := by apply h₁_x hβ‚‚_h₁, rw this, have : h₁_nβ‚‚ = hβ‚‚_nβ‚‚ := by apply h₁_x_1 hβ‚‚_hβ‚‚, rw this, refl, }, { cases hβ‚‚, refl, } end lemma compute_typed_expression_right_unique {sig s t} : relator.right_unique (compute_typed_expression sig s t) := begin unfold relator.right_unique, intros expr val₁ valβ‚‚ h₁ hβ‚‚, apply compute_typed_expression_unique, repeat { assumption }, end lemma compute_expr_list_unique {sig s} {t : type} {idx_expr : list expression} {eval₁ evalβ‚‚} (h₁ : compute_expr_list t sig s idx_expr eval₁) (hβ‚‚ : compute_expr_list t sig s idx_expr evalβ‚‚) : eval₁ = evalβ‚‚ := begin -- rw ← list.forallβ‚‚_eq_eq_eq, -- does rewrite perform funext here? apply list.right_unique_forallβ‚‚ (@compute_typed_expression_right_unique sig s t), repeat { assumption }, end @[simp] lemma big_step_assign {sig s u expr n idx_expr idx_evaled} {d : declaration} {val : β–Έ(d.type)} (hp : ((assign n idx_expr expr), sig, s) ⟹ u) (hi : compute_idx_expr sig s idx_expr idx_evaled) (hd : sig n = some d) (he : compute_typed_expression sig s (d.type) expr val) : s.global.update (d.type) n idx_evaled val = u.global := begin cases hp, simp, have : hp_idx_evaled = idx_evaled := by apply compute_expr_list_unique hp_h_idx hi, subst this, rw hd at hp_ht, simp at hp_ht, subst hp_ht, have : val = hp_val := by apply compute_typed_expression_unique he hp_h_eval, subst this, end lemma big_step_assign' {sig s u expr n idx_expr} (hp : ((assign n idx_expr expr), sig, s) ⟹ u) : βˆƒ (d : declaration) idx_evaled val, s.global.update (d.type) n idx_evaled val = u.global := begin cases hp, apply Exists.intro hp_d, apply Exists.intro hp_idx_evaled, apply Exists.intro hp_val, simp, end -- lemma big_step_seq {s} () : := begin def s₁ : signature | "n" := some {scope := 1, type := int, nridx := 0} | _ := none def p : program := assign "n" [] (literal_int 1) set_option trace.simplify.rewrite true example {s u} (hp : (p, s₁, s) ⟹ u) : u.global int "n" [] = 1 := begin cases hp, simp at hp_ht, cases hp_ht, simp, cases hp_h_eval, sorry, -- apply var_map_update_get, -- simp, end end MCL_untyped
[STATEMENT] lemma binomial_pmf_Suc: assumes "p \<in> {0..1}" shows "binomial_pmf (Suc n) p = do {b \<leftarrow> bernoulli_pmf p; k \<leftarrow> binomial_pmf n p; return_pmf ((if b then 1 else 0) + k)}" (is "_ = ?rhs") [PROOF STATE] proof (prove) goal (1 subgoal): 1. binomial_pmf (Suc n) p = bernoulli_pmf p \<bind> (\<lambda>b. binomial_pmf n p \<bind> (\<lambda>k. return_pmf ((if b then 1 else 0) + k))) [PROOF STEP] proof (intro pmf_eqI) [PROOF STATE] proof (state) goal (1 subgoal): 1. \<And>i. pmf (binomial_pmf (Suc n) p) i = pmf (bernoulli_pmf p \<bind> (\<lambda>b. binomial_pmf n p \<bind> (\<lambda>k. return_pmf ((if b then 1 else 0) + k)))) i [PROOF STEP] fix k [PROOF STATE] proof (state) goal (1 subgoal): 1. \<And>i. pmf (binomial_pmf (Suc n) p) i = pmf (bernoulli_pmf p \<bind> (\<lambda>b. binomial_pmf n p \<bind> (\<lambda>k. return_pmf ((if b then 1 else 0) + k)))) i [PROOF STEP] have A: "indicator {Suc a} (Suc b) = indicator {a} b" for a b [PROOF STATE] proof (prove) goal (1 subgoal): 1. indicator {Suc a} (Suc b) = indicator {a} b [PROOF STEP] by (simp add: indicator_def) [PROOF STATE] proof (state) this: indicator {Suc ?a1} (Suc ?b1) = indicator {?a1} ?b1 goal (1 subgoal): 1. \<And>i. pmf (binomial_pmf (Suc n) p) i = pmf (bernoulli_pmf p \<bind> (\<lambda>b. binomial_pmf n p \<bind> (\<lambda>k. return_pmf ((if b then 1 else 0) + k)))) i [PROOF STEP] show "pmf (binomial_pmf (Suc n) p) k = pmf ?rhs k" [PROOF STATE] proof (prove) goal (1 subgoal): 1. pmf (binomial_pmf (Suc n) p) k = pmf (bernoulli_pmf p \<bind> (\<lambda>b. binomial_pmf n p \<bind> (\<lambda>k. return_pmf ((if b then 1 else 0) + k)))) k [PROOF STEP] by (cases k; cases "k > n") (insert assms, auto simp: pmf_bind measure_pmf_single A field_split_simps algebra_simps not_less less_eq_Suc_le [symmetric] power_diff') [PROOF STATE] proof (state) this: pmf (binomial_pmf (Suc n) p) k = pmf (bernoulli_pmf p \<bind> (\<lambda>b. binomial_pmf n p \<bind> (\<lambda>k. return_pmf ((if b then 1 else 0) + k)))) k goal: No subgoals! [PROOF STEP] qed
#include <stdio.h> #include <stdlib.h> #include <math.h> #define COMPEARTH_PRIVATE_GEM3 1 #include "compearth.h" #ifdef DEBUG #ifdef COMPEARTH_USE_MKL #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wreserved-id-macro" #pragma clang diagnostic ignored "-Wstrict-prototypes" #endif #include <mkl_cblas.h> #ifdef __clang__ #pragma clang diagnostic pop #endif #else #include <cblas.h> #endif #endif /*! * @brief Compute a rotation matrix given an axis and angle * * @param[in] n Number of matrices to generate. * @param[in] v Rotation axis. This is an array of dimension [3 x n] * with leading dimension 3. * @param[in] xi Rotation angles (degrees). This is an array of * dimension [n]. * * @param[in] U Rotation matrices. This is an array of dimension * [3 x 3 x n] where each leading [3 x 3] matrix is * in column major format. * * @result 0 indicates success. * * @date 2016 - Ben Baker translated Carl Tape's rotmat_gen.m to C * * @copyright MIT * */ int compearth_eulerUtil_rotmatGen(const int n, const double *__restrict__ v, const double *__restrict__ xi, double *__restrict__ U) { double R1[9], R2[9], R3[9], R4[9], R5[9], R54[9], R543[9], R5432[9]; double ele, nvph_deg, nvth_deg, rho, vph, vth, vph_deg, vth_deg; int i, ierr; const double pi180i = 180.0/M_PI; ierr = 0; for (i=0; i<n; i++) { compearth_matlab_cart2sph(1, &v[3*i+0], &v[3*i+1], &v[3*i+2], &vph, &ele, &rho); vth = M_PI_2 - ele; vth_deg = vth*pi180i; vph_deg = vph*pi180i; nvph_deg =-vph_deg; nvth_deg =-vth_deg; ierr = ierr + compearth_eulerUtil_rotmat(1, &nvph_deg, 3, R1); ierr = ierr + compearth_eulerUtil_rotmat(1, &nvth_deg, 2, R2); ierr = ierr + compearth_eulerUtil_rotmat(1, &xi[i], 3, R3); ierr = ierr + compearth_eulerUtil_rotmat(1, &vth_deg, 2, R4); ierr = ierr + compearth_eulerUtil_rotmat(1, &vph_deg, 3, R5); if (ierr != 0) { fprintf(stderr, "%s: Error computing rotation matrices %d\n", __func__, i); } gemm3_colMajorNoTransNoTrans(R5, R4, R54); gemm3_colMajorNoTransNoTrans(R54, R3, R543); gemm3_colMajorNoTransNoTrans(R543, R2, R5432); gemm3_colMajorNoTransNoTrans(R5432, R1, &U[9*i]); #ifdef DEBUG double U9[9]; cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, 3, 3, 3, 1.0, R5, 3, R4, 3, 0.0, R54, 3); cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, 3, 3, 3, 1.0, R54, 3, R3, 3, 0.0, R543, 3); cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, 3, 3, 3, 1.0, R543, 3, R2, 3, 0.0, R5432, 3); cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, 3, 3, 3, 1.0, R5432, 3, R1, 3, 0.0, U9, 3); for (int k=0; k<9; k++) { if (fabs(U9[k] - U[9*i+k]) > 1.e-14) { printf("Computation failed: %f %f\n", U9[k], U[9*i+k]); ierr = ierr + 1; } } #endif } return ierr; }
PROGRAM question5 IMPLICIT NONE real :: x = 2 print *, (1./3)*log(1 + sqrt(x)) + (1./15.)*(2. + sqrt(x)) STOP END PROGRAM question5
#cut window from final display data frame cutWindow<-function(data,proj,window=NULL){ if(is.null(window)){ return(data) } else{ corners<-data.frame(lon=c(window[1],window[2]),lat=c(window[3],window[4])) if(proj!="NA"){ corners<-SpatialPoints(corners,CRS(proj_latlon)) corners<-as.data.frame(spTransform(corners,CRS(proj))) } data<-data[data$lon>corners$lon[1]&data$lon<corners$lon[2], ] data<-data[data$lat>corners$lat[1]&data$lat<corners$lat[2], ] return(data) } }
= Hibiscus ( restaurant ) =
/- Copyright (c) 2022 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel ! This file was ported from Lean 3 source module category_theory.balanced ! leanprover-community/mathlib commit 32253a1a1071173b33dc7d6a218cf722c6feb514 ! Please do not edit these lines, except to modify the commit id ! if you have ported upstream changes. -/ import Mathlib.CategoryTheory.EpiMono /-! # Balanced categories A category is called balanced if any morphism that is both monic and epic is an isomorphism. Balanced categories arise frequently. For example, categories in which every monomorphism (or epimorphism) is strong are balanced. Examples of this are abelian categories and toposes, such as the category of types. -/ universe v u namespace CategoryTheory variable {C : Type u} [Category.{v} C] section variable (C) /-- A category is called balanced if any morphism that is both monic and epic is an isomorphism. -/ class Balanced : Prop where isIso_of_mono_of_epi : βˆ€ {X Y : C} (f : X ⟢ Y) [Mono f] [Epi f], IsIso f #align category_theory.balanced CategoryTheory.Balanced end theorem isIso_of_mono_of_epi [Balanced C] {X Y : C} (f : X ⟢ Y) [Mono f] [Epi f] : IsIso f := Balanced.isIso_of_mono_of_epi _ #align category_theory.is_iso_of_mono_of_epi CategoryTheory.isIso_of_mono_of_epi theorem isIso_iff_mono_and_epi [Balanced C] {X Y : C} (f : X ⟢ Y) : IsIso f ↔ Mono f ∧ Epi f := ⟨fun _ => ⟨inferInstance, inferInstance⟩, fun ⟨_, _⟩ => isIso_of_mono_of_epi _⟩ #align category_theory.is_iso_iff_mono_and_epi CategoryTheory.isIso_iff_mono_and_epi section attribute [local instance] isIso_of_mono_of_epi theorem balanced_opposite [Balanced C] : Balanced Cα΅’α΅– := { isIso_of_mono_of_epi := fun f fmono fepi => by rw [← Quiver.Hom.op_unop f] exact isIso_of_op _ } #align category_theory.balanced_opposite CategoryTheory.balanced_opposite end end CategoryTheory
State Before: ⊒ Pairwise fun m n => 1 ≀ dist m n State After: m n : β„€ hne : m β‰  n ⊒ 1 ≀ dist m n Tactic: intro m n hne State Before: m n : β„€ hne : m β‰  n ⊒ 1 ≀ dist m n State After: m n : β„€ hne : m β‰  n ⊒ 1 ≀ abs (↑m - ↑n) Tactic: rw [dist_eq] State Before: m n : β„€ hne : m β‰  n ⊒ 1 ≀ abs (↑m - ↑n) State After: m n : β„€ hne : m β‰  n ⊒ 1 ≀ abs (m - n) Tactic: norm_cast State Before: m n : β„€ hne : m β‰  n ⊒ 1 ≀ abs (m - n) State After: no goals Tactic: rwa [← zero_add (1 : β„€), Int.add_one_le_iff, abs_pos, sub_ne_zero]
import MyNat.Definition import MyNat.Inequality -- le_iff_exists_add import Mathlib.Tactic.Use -- use tactic import AdditionWorld.Level2 -- add_assoc import InequalityWorld.Level2 -- le_refl_mynat import Mathlib.Init.Algebra.Order namespace MyNat open MyNat /-! # Inequality world. ## Level 5: `le_trans` Another straightforward one. ## Lemma : le_trans ≀ is transitive. In other words, if `a ≀ b` and `b ≀ c` then `a ≀ c`. -/ theorem le_trans (a b c : MyNat) (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by cases hab with | _ d hd => cases hbc with | _ e he => use (d + e) rw [←add_assoc] rw [←hd] assumption /-! This proved that the natural numbers are a preorder. -/ instance : Preorder MyNat := ⟨le_refl_mynat, le_trans, lt⟩ /-! Next up [Level 6](./Level6.lean.md) -/
[STATEMENT] theorem tortoise_hare: "\<lbrace>\<langle>True\<rangle>\<rbrace> tortoise_hare \<lbrace>nu \<^bold>\<in> \<langle>{0<..lambda + mu}\<rangle> \<^bold>\<and> l \<^bold>= \<langle>lambda\<rangle> \<^bold>\<and> m \<^bold>= \<langle>mu\<rangle>\<rbrace>" [PROOF STATE] proof (prove) goal (1 subgoal): 1. \<lbrace>\<lambda>s. True\<rbrace> tortoise_hare \<lbrace>\<lambda>s. nu s \<in> {0<..lambda + mu} \<and> l s = lambda \<and> m s = mu\<rbrace> [PROOF STEP] unfolding tortoise_hare_def [PROOF STATE] proof (prove) goal (1 subgoal): 1. \<lbrace>\<lambda>s. True\<rbrace> find_nu ;; find_mu ;; find_lambda \<lbrace>\<lambda>s. nu s \<in> {0<..lambda + mu} \<and> l s = lambda \<and> m s = mu\<rbrace> [PROOF STEP] by (rule find_nu find_mu find_lambda wp_intro)+
[STATEMENT] lemma \<gamma>_ivl_nice: "\<gamma>_ivl[l,h] = {i. l \<le> Fin i \<and> Fin i \<le> h}" [PROOF STATE] proof (prove) goal (1 subgoal): 1. \<gamma>_ivl [l, h] = {i. l \<le> Fin i \<and> Fin i \<le> h} [PROOF STEP] by transfer (simp add: \<gamma>_rep_def)
module Main import Graphics.Util.Glfw import Graphics.Util.GlfwConfig initDisplay : String -> Int -> Int -> IO GlfwWindow initDisplay title width height = do glfw <- glfwInit glfwWindowHint GLFW_CONTEXT_VERSION_MAJOR 4 glfwWindowHint GLFW_CONTEXT_VERSION_MINOR 1 glfwWindowHint GLFW_OPENGL_FORWARD_COMPAT 1 glfwWindowHint GLFW_OPENGL_PROFILE (toInt GLFW_OPENGL_CORE_PROFILE) win <- glfwCreateWindow title width height defaultMonitor -- now we pretend every thing is going to be ok glfwMakeContextCurrent win pure win isKeyPressed : GlfwWindow -> Char -> IO Bool isKeyPressed win key = do ev <- glfwGetKey win key if ev == GLFW_PRESS then pure True else pure False main : IO () main = do win <- initDisplay "Hello Idris" 640 480 glfwSetInputMode win GLFW_STICKY_KEYS 1 eventLoop win 0 glfwDestroyWindow win glfwTerminate pure () where eventLoop : GlfwWindow -> Int -> IO () eventLoop win ticks = do glfwPollEvents key <- glfwGetFunctionKey win GLFW_KEY_ESCAPE shouldClose <- glfwWindowShouldClose win if shouldClose || key == GLFW_PRESS then pure () else do eventLoop win (ticks+1)
lemma setdist_empty2 [simp]: "setdist t {} = 0"
/- Copyright (c) 2018 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton ! This file was ported from Lean 3 source module topology.stone_cech ! leanprover-community/mathlib commit 3e32bc908f617039c74c06ea9a897e30c30803c2 ! Please do not edit these lines, except to modify the commit id ! if you have ported upstream changes. -/ import Mathbin.Topology.Bases import Mathbin.Topology.DenseEmbedding /-! # Stone-Čech compactification > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Construction of the Stone-Čech compactification using ultrafilters. Parts of the formalization are based on "Ultrafilters and Topology" by Marius Stekelenburg, particularly section 5. -/ noncomputable section open Filter Set open Topology universe u v section Ultrafilter #print ultrafilterBasis /- /- The set of ultrafilters on Ξ± carries a natural topology which makes it the Stone-Čech compactification of Ξ± (viewed as a discrete space). -/ /-- Basis for the topology on `ultrafilter Ξ±`. -/ def ultrafilterBasis (Ξ± : Type u) : Set (Set (Ultrafilter Ξ±)) := range fun s : Set Ξ± => { u | s ∈ u } #align ultrafilter_basis ultrafilterBasis -/ variable {Ξ± : Type u} instance : TopologicalSpace (Ultrafilter Ξ±) := TopologicalSpace.generateFrom (ultrafilterBasis Ξ±) #print ultrafilterBasis_is_basis /- theorem ultrafilterBasis_is_basis : TopologicalSpace.IsTopologicalBasis (ultrafilterBasis Ξ±) := ⟨by rintro _ ⟨a, rfl⟩ _ ⟨b, rfl⟩ u ⟨ua, ub⟩ refine' ⟨_, ⟨a ∩ b, rfl⟩, inter_mem ua ub, fun v hv => ⟨_, _⟩⟩ <;> apply mem_of_superset hv <;> simp [inter_subset_right a b], eq_univ_of_univ_subset <| subset_unionβ‚›_of_mem <| ⟨univ, eq_univ_of_forall fun u => univ_mem⟩, rfl⟩ #align ultrafilter_basis_is_basis ultrafilterBasis_is_basis -/ #print ultrafilter_isOpen_basic /- /-- The basic open sets for the topology on ultrafilters are open. -/ theorem ultrafilter_isOpen_basic (s : Set Ξ±) : IsOpen { u : Ultrafilter Ξ± | s ∈ u } := ultrafilterBasis_is_basis.IsOpen ⟨s, rfl⟩ #align ultrafilter_is_open_basic ultrafilter_isOpen_basic -/ #print ultrafilter_isClosed_basic /- /-- The basic open sets for the topology on ultrafilters are also closed. -/ theorem ultrafilter_isClosed_basic (s : Set Ξ±) : IsClosed { u : Ultrafilter Ξ± | s ∈ u } := by rw [← isOpen_compl_iff] convert ultrafilter_isOpen_basic (sᢜ) ext u exact ultrafilter.compl_mem_iff_not_mem.symm #align ultrafilter_is_closed_basic ultrafilter_isClosed_basic -/ /- warning: ultrafilter_converges_iff -> ultrafilter_converges_iff is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}} {u : Ultrafilter.{u1} (Ultrafilter.{u1} Ξ±)} {x : Ultrafilter.{u1} Ξ±}, Iff (LE.le.{u1} (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (Preorder.toLE.{u1} (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (PartialOrder.toPreorder.{u1} (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (Filter.partialOrder.{u1} (Ultrafilter.{u1} Ξ±)))) ((fun (a : Type.{u1}) (b : Type.{u1}) [self : HasLiftT.{succ u1, succ u1} a b] => self.0) (Ultrafilter.{u1} (Ultrafilter.{u1} Ξ±)) (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (HasLiftT.mk.{succ u1, succ u1} (Ultrafilter.{u1} (Ultrafilter.{u1} Ξ±)) (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (CoeTCβ‚“.coe.{succ u1, succ u1} (Ultrafilter.{u1} (Ultrafilter.{u1} Ξ±)) (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (Ultrafilter.Filter.hasCoeT.{u1} (Ultrafilter.{u1} Ξ±)))) u) (nhds.{u1} (Ultrafilter.{u1} Ξ±) (Ultrafilter.topologicalSpace.{u1} Ξ±) x)) (Eq.{succ u1} (Ultrafilter.{u1} Ξ±) x (joinM.{u1} Ultrafilter.{u1} Ultrafilter.monad.{u1} Ξ± u)) but is expected to have type forall {Ξ± : Type.{u1}} {u : Ultrafilter.{u1} (Ultrafilter.{u1} Ξ±)} {x : Ultrafilter.{u1} Ξ±}, Iff (LE.le.{u1} (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (Preorder.toLE.{u1} (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (PartialOrder.toPreorder.{u1} (Filter.{u1} (Ultrafilter.{u1} Ξ±)) (Filter.instPartialOrderFilter.{u1} (Ultrafilter.{u1} Ξ±)))) (Ultrafilter.toFilter.{u1} (Ultrafilter.{u1} Ξ±) u) (nhds.{u1} (Ultrafilter.{u1} Ξ±) (Ultrafilter.topologicalSpace.{u1} Ξ±) x)) (Eq.{succ u1} (Ultrafilter.{u1} Ξ±) x (joinM.{u1} Ultrafilter.{u1} Ultrafilter.monad.{u1} Ξ± u)) Case conversion may be inaccurate. Consider using '#align ultrafilter_converges_iff ultrafilter_converges_iffβ‚“'. -/ /-- Every ultrafilter `u` on `ultrafilter Ξ±` converges to a unique point of `ultrafilter Ξ±`, namely `mjoin u`. -/ theorem ultrafilter_converges_iff {u : Ultrafilter (Ultrafilter Ξ±)} {x : Ultrafilter Ξ±} : ↑u ≀ 𝓝 x ↔ x = joinM u := by rw [eq_comm, ← Ultrafilter.coe_le_coe] change ↑u ≀ 𝓝 x ↔ βˆ€ s ∈ x, { v : Ultrafilter Ξ± | s ∈ v } ∈ u simp only [TopologicalSpace.nhds_generateFrom, le_infα΅’_iff, ultrafilterBasis, le_principal_iff, mem_set_of_eq] constructor Β· intro h a ha exact h _ ⟨ha, a, rfl⟩ Β· rintro h a ⟨xi, a, rfl⟩ exact h _ xi #align ultrafilter_converges_iff ultrafilter_converges_iff #print ultrafilter_compact /- instance ultrafilter_compact : CompactSpace (Ultrafilter Ξ±) := ⟨isCompact_iff_ultrafilter_le_nhds.mpr fun f _ => ⟨joinM f, trivial, ultrafilter_converges_iff.mpr rfl⟩⟩ #align ultrafilter_compact ultrafilter_compact -/ #print Ultrafilter.t2Space /- instance Ultrafilter.t2Space : T2Space (Ultrafilter Ξ±) := t2_iff_ultrafilter.mpr fun x y f fx fy => have hx : x = joinM f := ultrafilter_converges_iff.mp fx have hy : y = joinM f := ultrafilter_converges_iff.mp fy hx.trans hy.symm #align ultrafilter.t2_space Ultrafilter.t2Space -/ instance : TotallyDisconnectedSpace (Ultrafilter Ξ±) := by rw [totallyDisconnectedSpace_iff_connectedComponent_singleton] intro A simp only [Set.eq_singleton_iff_unique_mem, mem_connectedComponent, true_and_iff] intro B hB rw [← Ultrafilter.coe_le_coe] intro s hs rw [connectedComponent_eq_interα΅’_clopen, Set.mem_interα΅’] at hB let Z := { F : Ultrafilter Ξ± | s ∈ F } have hZ : IsClopen Z := ⟨ultrafilter_isOpen_basic s, ultrafilter_isClosed_basic s⟩ exact hB ⟨Z, hZ, hs⟩ /- warning: ultrafilter_comap_pure_nhds -> ultrafilter_comap_pure_nhds is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}} (b : Ultrafilter.{u1} Ξ±), LE.le.{u1} (Filter.{u1} Ξ±) (Preorder.toLE.{u1} (Filter.{u1} Ξ±) (PartialOrder.toPreorder.{u1} (Filter.{u1} Ξ±) (Filter.partialOrder.{u1} Ξ±))) (Filter.comap.{u1, u1} Ξ± (Ultrafilter.{u1} Ξ±) (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.hasPure.{u1} Ξ±) (nhds.{u1} (Ultrafilter.{u1} Ξ±) (Ultrafilter.topologicalSpace.{u1} Ξ±) b)) ((fun (a : Type.{u1}) (b : Type.{u1}) [self : HasLiftT.{succ u1, succ u1} a b] => self.0) (Ultrafilter.{u1} Ξ±) (Filter.{u1} Ξ±) (HasLiftT.mk.{succ u1, succ u1} (Ultrafilter.{u1} Ξ±) (Filter.{u1} Ξ±) (CoeTCβ‚“.coe.{succ u1, succ u1} (Ultrafilter.{u1} Ξ±) (Filter.{u1} Ξ±) (Ultrafilter.Filter.hasCoeT.{u1} Ξ±))) b) but is expected to have type forall {Ξ± : Type.{u1}} (b : Ultrafilter.{u1} Ξ±), LE.le.{u1} (Filter.{u1} Ξ±) (Preorder.toLE.{u1} (Filter.{u1} Ξ±) (PartialOrder.toPreorder.{u1} (Filter.{u1} Ξ±) (Filter.instPartialOrderFilter.{u1} Ξ±))) (Filter.comap.{u1, u1} Ξ± (Ultrafilter.{u1} Ξ±) (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.instPureUltrafilter.{u1} Ξ±) (nhds.{u1} (Ultrafilter.{u1} Ξ±) (Ultrafilter.topologicalSpace.{u1} Ξ±) b)) (Ultrafilter.toFilter.{u1} Ξ± b) Case conversion may be inaccurate. Consider using '#align ultrafilter_comap_pure_nhds ultrafilter_comap_pure_nhdsβ‚“'. -/ theorem ultrafilter_comap_pure_nhds (b : Ultrafilter Ξ±) : comap pure (𝓝 b) ≀ b := by rw [TopologicalSpace.nhds_generateFrom] simp only [comap_infi, comap_principal] intro s hs rw [← le_principal_iff] refine' infα΅’_le_of_le { u | s ∈ u } _ refine' infα΅’_le_of_le ⟨hs, ⟨s, rfl⟩⟩ _ exact principal_mono.2 fun a => id #align ultrafilter_comap_pure_nhds ultrafilter_comap_pure_nhds section Embedding /- warning: ultrafilter_pure_injective -> ultrafilter_pure_injective is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}}, Function.Injective.{succ u1, succ u1} Ξ± (Ultrafilter.{u1} Ξ±) (Pure.pure.{u1, u1} (fun {Ξ± : Type.{u1}} => Ultrafilter.{u1} Ξ±) Ultrafilter.hasPure.{u1} Ξ±) but is expected to have type forall {Ξ± : Type.{u1}}, Function.Injective.{succ u1, succ u1} Ξ± (Ultrafilter.{u1} Ξ±) (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.instPureUltrafilter.{u1} Ξ±) Case conversion may be inaccurate. Consider using '#align ultrafilter_pure_injective ultrafilter_pure_injectiveβ‚“'. -/ theorem ultrafilter_pure_injective : Function.Injective (pure : Ξ± β†’ Ultrafilter Ξ±) := by intro x y h have : {x} ∈ (pure x : Ultrafilter Ξ±) := singleton_mem_pure rw [h] at this exact (mem_singleton_iff.mp (mem_pure.mp this)).symm #align ultrafilter_pure_injective ultrafilter_pure_injective open TopologicalSpace /- warning: dense_range_pure -> denseRange_pure is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}}, DenseRange.{u1, u1} (Ultrafilter.{u1} Ξ±) (Ultrafilter.topologicalSpace.{u1} Ξ±) Ξ± (Pure.pure.{u1, u1} (fun {Ξ± : Type.{u1}} => Ultrafilter.{u1} Ξ±) Ultrafilter.hasPure.{u1} Ξ±) but is expected to have type forall {Ξ± : Type.{u1}}, DenseRange.{u1, u1} (Ultrafilter.{u1} Ξ±) (Ultrafilter.topologicalSpace.{u1} Ξ±) Ξ± (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.instPureUltrafilter.{u1} Ξ±) Case conversion may be inaccurate. Consider using '#align dense_range_pure denseRange_pureβ‚“'. -/ /-- The range of `pure : Ξ± β†’ ultrafilter Ξ±` is dense in `ultrafilter Ξ±`. -/ theorem denseRange_pure : DenseRange (pure : Ξ± β†’ Ultrafilter Ξ±) := fun x => mem_closure_iff_ultrafilter.mpr ⟨x.map pure, range_mem_map, ultrafilter_converges_iff.mpr (bind_pure x).symm⟩ #align dense_range_pure denseRange_pure /- warning: induced_topology_pure -> induced_topology_pure is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}}, Eq.{succ u1} (TopologicalSpace.{u1} Ξ±) (TopologicalSpace.induced.{u1, u1} Ξ± (Ultrafilter.{u1} Ξ±) (Pure.pure.{u1, u1} (fun {Ξ± : Type.{u1}} => Ultrafilter.{u1} Ξ±) Ultrafilter.hasPure.{u1} Ξ±) (Ultrafilter.topologicalSpace.{u1} Ξ±)) (Bot.bot.{u1} (TopologicalSpace.{u1} Ξ±) (CompleteLattice.toHasBot.{u1} (TopologicalSpace.{u1} Ξ±) (TopologicalSpace.completeLattice.{u1} Ξ±))) but is expected to have type forall {Ξ± : Type.{u1}}, Eq.{succ u1} (TopologicalSpace.{u1} Ξ±) (TopologicalSpace.induced.{u1, u1} Ξ± (Ultrafilter.{u1} Ξ±) (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.instPureUltrafilter.{u1} Ξ±) (Ultrafilter.topologicalSpace.{u1} Ξ±)) (Bot.bot.{u1} (TopologicalSpace.{u1} Ξ±) (CompleteLattice.toBot.{u1} (TopologicalSpace.{u1} Ξ±) (TopologicalSpace.instCompleteLatticeTopologicalSpace.{u1} Ξ±))) Case conversion may be inaccurate. Consider using '#align induced_topology_pure induced_topology_pureβ‚“'. -/ /-- The map `pure : Ξ± β†’ ultra_filter Ξ±` induces on `Ξ±` the discrete topology. -/ theorem induced_topology_pure : TopologicalSpace.induced (pure : Ξ± β†’ Ultrafilter Ξ±) Ultrafilter.topologicalSpace = βŠ₯ := by apply eq_bot_of_singletons_open intro x use { u : Ultrafilter Ξ± | {x} ∈ u }, ultrafilter_isOpen_basic _ simp #align induced_topology_pure induced_topology_pure /- warning: dense_inducing_pure -> denseInducing_pure is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}}, DenseInducing.{u1, u1} Ξ± (Ultrafilter.{u1} Ξ±) (Bot.bot.{u1} (TopologicalSpace.{u1} Ξ±) (CompleteLattice.toHasBot.{u1} (TopologicalSpace.{u1} Ξ±) (TopologicalSpace.completeLattice.{u1} Ξ±))) (Ultrafilter.topologicalSpace.{u1} Ξ±) (Pure.pure.{u1, u1} (fun {Ξ± : Type.{u1}} => Ultrafilter.{u1} Ξ±) Ultrafilter.hasPure.{u1} Ξ±) but is expected to have type forall {Ξ± : Type.{u1}}, DenseInducing.{u1, u1} Ξ± (Ultrafilter.{u1} Ξ±) (Bot.bot.{u1} (TopologicalSpace.{u1} Ξ±) (CompleteLattice.toBot.{u1} (TopologicalSpace.{u1} Ξ±) (TopologicalSpace.instCompleteLatticeTopologicalSpace.{u1} Ξ±))) (Ultrafilter.topologicalSpace.{u1} Ξ±) (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.instPureUltrafilter.{u1} Ξ±) Case conversion may be inaccurate. Consider using '#align dense_inducing_pure denseInducing_pureβ‚“'. -/ /-- `pure : Ξ± β†’ ultrafilter Ξ±` defines a dense inducing of `Ξ±` in `ultrafilter Ξ±`. -/ theorem denseInducing_pure : @DenseInducing _ _ βŠ₯ _ (pure : Ξ± β†’ Ultrafilter Ξ±) := letI : TopologicalSpace Ξ± := βŠ₯ ⟨⟨induced_topology_pure.symm⟩, denseRange_pure⟩ #align dense_inducing_pure denseInducing_pure /- warning: dense_embedding_pure -> denseEmbedding_pure is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}}, DenseEmbedding.{u1, u1} Ξ± (Ultrafilter.{u1} Ξ±) (Bot.bot.{u1} (TopologicalSpace.{u1} Ξ±) (CompleteLattice.toHasBot.{u1} (TopologicalSpace.{u1} Ξ±) (TopologicalSpace.completeLattice.{u1} Ξ±))) (Ultrafilter.topologicalSpace.{u1} Ξ±) (Pure.pure.{u1, u1} (fun {Ξ± : Type.{u1}} => Ultrafilter.{u1} Ξ±) Ultrafilter.hasPure.{u1} Ξ±) but is expected to have type forall {Ξ± : Type.{u1}}, DenseEmbedding.{u1, u1} Ξ± (Ultrafilter.{u1} Ξ±) (Bot.bot.{u1} (TopologicalSpace.{u1} Ξ±) (CompleteLattice.toBot.{u1} (TopologicalSpace.{u1} Ξ±) (TopologicalSpace.instCompleteLatticeTopologicalSpace.{u1} Ξ±))) (Ultrafilter.topologicalSpace.{u1} Ξ±) (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.instPureUltrafilter.{u1} Ξ±) Case conversion may be inaccurate. Consider using '#align dense_embedding_pure denseEmbedding_pureβ‚“'. -/ -- The following refined version will never be used /-- `pure : Ξ± β†’ ultrafilter Ξ±` defines a dense embedding of `Ξ±` in `ultrafilter Ξ±`. -/ theorem denseEmbedding_pure : @DenseEmbedding _ _ βŠ₯ _ (pure : Ξ± β†’ Ultrafilter Ξ±) := letI : TopologicalSpace Ξ± := βŠ₯ { denseInducing_pure with inj := ultrafilter_pure_injective } #align dense_embedding_pure denseEmbedding_pure end Embedding section Extension /- Goal: Any function `Ξ± β†’ Ξ³` to a compact Hausdorff space `Ξ³` has a unique extension to a continuous function `ultrafilter Ξ± β†’ Ξ³`. We already know it must be unique because `Ξ± β†’ ultrafilter Ξ±` is a dense embedding and `Ξ³` is Hausdorff. For existence, we will invoke `dense_embedding.continuous_extend`. -/ variable {Ξ³ : Type _} [TopologicalSpace Ξ³] #print Ultrafilter.extend /- /-- The extension of a function `Ξ± β†’ Ξ³` to a function `ultrafilter Ξ± β†’ Ξ³`. When `Ξ³` is a compact Hausdorff space it will be continuous. -/ def Ultrafilter.extend (f : Ξ± β†’ Ξ³) : Ultrafilter Ξ± β†’ Ξ³ := letI : TopologicalSpace Ξ± := βŠ₯ dense_inducing_pure.extend f #align ultrafilter.extend Ultrafilter.extend -/ variable [T2Space Ξ³] /- warning: ultrafilter_extend_extends -> ultrafilter_extend_extends is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}} {Ξ³ : Type.{u2}} [_inst_1 : TopologicalSpace.{u2} Ξ³] [_inst_2 : T2Space.{u2} Ξ³ _inst_1] (f : Ξ± -> Ξ³), Eq.{max (succ u1) (succ u2)} (Ξ± -> Ξ³) (Function.comp.{succ u1, succ u1, succ u2} Ξ± (Ultrafilter.{u1} Ξ±) Ξ³ (Ultrafilter.extend.{u1, u2} Ξ± Ξ³ _inst_1 f) (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.hasPure.{u1} Ξ±)) f but is expected to have type forall {Ξ± : Type.{u2}} {Ξ³ : Type.{u1}} [_inst_1 : TopologicalSpace.{u1} Ξ³] [_inst_2 : T2Space.{u1} Ξ³ _inst_1] (f : Ξ± -> Ξ³), Eq.{max (succ u2) (succ u1)} (Ξ± -> Ξ³) (Function.comp.{succ u2, succ u2, succ u1} Ξ± (Ultrafilter.{u2} Ξ±) Ξ³ (Ultrafilter.extend.{u2, u1} Ξ± Ξ³ _inst_1 f) (Pure.pure.{u2, u2} Ultrafilter.{u2} Ultrafilter.instPureUltrafilter.{u2} Ξ±)) f Case conversion may be inaccurate. Consider using '#align ultrafilter_extend_extends ultrafilter_extend_extendsβ‚“'. -/ theorem ultrafilter_extend_extends (f : Ξ± β†’ Ξ³) : Ultrafilter.extend f ∘ pure = f := by letI : TopologicalSpace Ξ± := βŠ₯ haveI : DiscreteTopology Ξ± := ⟨rfl⟩ exact funext (dense_inducing_pure.extend_eq continuous_of_discreteTopology) #align ultrafilter_extend_extends ultrafilter_extend_extends variable [CompactSpace Ξ³] /- warning: continuous_ultrafilter_extend -> continuous_ultrafilter_extend is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}} {Ξ³ : Type.{u2}} [_inst_1 : TopologicalSpace.{u2} Ξ³] [_inst_2 : T2Space.{u2} Ξ³ _inst_1] [_inst_3 : CompactSpace.{u2} Ξ³ _inst_1] (f : Ξ± -> Ξ³), Continuous.{u1, u2} (Ultrafilter.{u1} Ξ±) Ξ³ (Ultrafilter.topologicalSpace.{u1} Ξ±) _inst_1 (Ultrafilter.extend.{u1, u2} Ξ± Ξ³ _inst_1 f) but is expected to have type forall {Ξ± : Type.{u2}} {Ξ³ : Type.{u1}} [_inst_1 : TopologicalSpace.{u1} Ξ³] [_inst_2 : T2Space.{u1} Ξ³ _inst_1] [_inst_3 : CompactSpace.{u1} Ξ³ _inst_1] (f : Ξ± -> Ξ³), Continuous.{u2, u1} (Ultrafilter.{u2} Ξ±) Ξ³ (Ultrafilter.topologicalSpace.{u2} Ξ±) _inst_1 (Ultrafilter.extend.{u2, u1} Ξ± Ξ³ _inst_1 f) Case conversion may be inaccurate. Consider using '#align continuous_ultrafilter_extend continuous_ultrafilter_extendβ‚“'. -/ theorem continuous_ultrafilter_extend (f : Ξ± β†’ Ξ³) : Continuous (Ultrafilter.extend f) := by have : βˆ€ b : Ultrafilter Ξ±, βˆƒ c, Tendsto f (comap pure (𝓝 b)) (𝓝 c) := fun b => -- b.map f is an ultrafilter on Ξ³, which is compact, so it converges to some c in Ξ³. let ⟨c, _, h⟩ := isCompact_univ.ultrafilter_le_nhds (b.map f) (by rw [le_principal_iff] <;> exact univ_mem) ⟨c, le_trans (map_mono (ultrafilter_comap_pure_nhds _)) h⟩ letI : TopologicalSpace Ξ± := βŠ₯ haveI : NormalSpace Ξ³ := normalOfCompactT2 exact dense_inducing_pure.continuous_extend this #align continuous_ultrafilter_extend continuous_ultrafilter_extend /- warning: ultrafilter_extend_eq_iff -> ultrafilter_extend_eq_iff is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}} {Ξ³ : Type.{u2}} [_inst_1 : TopologicalSpace.{u2} Ξ³] [_inst_2 : T2Space.{u2} Ξ³ _inst_1] [_inst_3 : CompactSpace.{u2} Ξ³ _inst_1] {f : Ξ± -> Ξ³} {b : Ultrafilter.{u1} Ξ±} {c : Ξ³}, Iff (Eq.{succ u2} Ξ³ (Ultrafilter.extend.{u1, u2} Ξ± Ξ³ _inst_1 f b) c) (LE.le.{u2} (Filter.{u2} Ξ³) (Preorder.toLE.{u2} (Filter.{u2} Ξ³) (PartialOrder.toPreorder.{u2} (Filter.{u2} Ξ³) (Filter.partialOrder.{u2} Ξ³))) ((fun (a : Type.{u2}) (b : Type.{u2}) [self : HasLiftT.{succ u2, succ u2} a b] => self.0) (Ultrafilter.{u2} Ξ³) (Filter.{u2} Ξ³) (HasLiftT.mk.{succ u2, succ u2} (Ultrafilter.{u2} Ξ³) (Filter.{u2} Ξ³) (CoeTCβ‚“.coe.{succ u2, succ u2} (Ultrafilter.{u2} Ξ³) (Filter.{u2} Ξ³) (Ultrafilter.Filter.hasCoeT.{u2} Ξ³))) (Ultrafilter.map.{u1, u2} Ξ± Ξ³ f b)) (nhds.{u2} Ξ³ _inst_1 c)) but is expected to have type forall {Ξ± : Type.{u2}} {Ξ³ : Type.{u1}} [_inst_1 : TopologicalSpace.{u1} Ξ³] [_inst_2 : T2Space.{u1} Ξ³ _inst_1] [_inst_3 : CompactSpace.{u1} Ξ³ _inst_1] {f : Ξ± -> Ξ³} {b : Ultrafilter.{u2} Ξ±} {c : Ξ³}, Iff (Eq.{succ u1} Ξ³ (Ultrafilter.extend.{u2, u1} Ξ± Ξ³ _inst_1 f b) c) (LE.le.{u1} (Filter.{u1} Ξ³) (Preorder.toLE.{u1} (Filter.{u1} Ξ³) (PartialOrder.toPreorder.{u1} (Filter.{u1} Ξ³) (Filter.instPartialOrderFilter.{u1} Ξ³))) (Ultrafilter.toFilter.{u1} Ξ³ (Ultrafilter.map.{u2, u1} Ξ± Ξ³ f b)) (nhds.{u1} Ξ³ _inst_1 c)) Case conversion may be inaccurate. Consider using '#align ultrafilter_extend_eq_iff ultrafilter_extend_eq_iffβ‚“'. -/ /-- The value of `ultrafilter.extend f` on an ultrafilter `b` is the unique limit of the ultrafilter `b.map f` in `Ξ³`. -/ theorem ultrafilter_extend_eq_iff {f : Ξ± β†’ Ξ³} {b : Ultrafilter Ξ±} {c : Ξ³} : Ultrafilter.extend f b = c ↔ ↑(b.map f) ≀ 𝓝 c := ⟨fun h => by -- Write b as an ultrafilter limit of pure ultrafilters, and use -- the facts that ultrafilter.extend is a continuous extension of f. let b' : Ultrafilter (Ultrafilter Ξ±) := b.map pure have t : ↑b' ≀ 𝓝 b := ultrafilter_converges_iff.mpr (bind_pure _).symm rw [← h] have := (continuous_ultrafilter_extend f).Tendsto b refine' le_trans _ (le_trans (map_mono t) this) change _ ≀ map (Ultrafilter.extend f ∘ pure) ↑b rw [ultrafilter_extend_extends] exact le_rfl, fun h => letI : TopologicalSpace Ξ± := βŠ₯ dense_inducing_pure.extend_eq_of_tendsto (le_trans (map_mono (ultrafilter_comap_pure_nhds _)) h)⟩ #align ultrafilter_extend_eq_iff ultrafilter_extend_eq_iff end Extension end Ultrafilter section StoneCech /- Now, we start with a (not necessarily discrete) topological space Ξ± and we want to construct its Stone-Čech compactification. We can build it as a quotient of `ultrafilter Ξ±` by the relation which identifies two points if the extension of every continuous function Ξ± β†’ Ξ³ to a compact Hausdorff space sends the two points to the same point of Ξ³. -/ variable (Ξ± : Type u) [TopologicalSpace Ξ±] #print stoneCechSetoid /- instance stoneCechSetoid : Setoid (Ultrafilter Ξ±) where R x y := βˆ€ (Ξ³ : Type u) [TopologicalSpace Ξ³], βˆ€ [T2Space Ξ³] [CompactSpace Ξ³] (f : Ξ± β†’ Ξ³) (hf : Continuous f), Ultrafilter.extend f x = Ultrafilter.extend f y iseqv := ⟨fun x Ξ³ tΞ³ h₁ hβ‚‚ f hf => rfl, fun x y xy Ξ³ tΞ³ h₁ hβ‚‚ f hf => (xy Ξ³ f hf).symm, fun x y z xy yz Ξ³ tΞ³ h₁ hβ‚‚ f hf => (xy Ξ³ f hf).trans (yz Ξ³ f hf)⟩ #align stone_cech_setoid stoneCechSetoid -/ #print StoneCech /- /-- The Stone-Čech compactification of a topological space. -/ def StoneCech : Type u := Quotient (stoneCechSetoid Ξ±) #align stone_cech StoneCech -/ variable {Ξ±} instance : TopologicalSpace (StoneCech Ξ±) := by unfold StoneCech <;> infer_instance instance [Inhabited Ξ±] : Inhabited (StoneCech Ξ±) := by unfold StoneCech <;> infer_instance #print stoneCechUnit /- /-- The natural map from Ξ± to its Stone-Čech compactification. -/ def stoneCechUnit (x : Ξ±) : StoneCech Ξ± := ⟦pure x⟧ #align stone_cech_unit stoneCechUnit -/ #print denseRange_stoneCechUnit /- /-- The image of stone_cech_unit is dense. (But stone_cech_unit need not be an embedding, for example if Ξ± is not Hausdorff.) -/ theorem denseRange_stoneCechUnit : DenseRange (stoneCechUnit : Ξ± β†’ StoneCech Ξ±) := denseRange_pure.Quotient #align dense_range_stone_cech_unit denseRange_stoneCechUnit -/ section Extension variable {Ξ³ : Type u} [TopologicalSpace Ξ³] [T2Space Ξ³] [CompactSpace Ξ³] variable {Ξ³' : Type u} [TopologicalSpace Ξ³'] [T2Space Ξ³'] variable {f : Ξ± β†’ Ξ³} (hf : Continuous f) attribute [local elab_with_expected_type] Quotient.lift #print stoneCechExtend /- /-- The extension of a continuous function from Ξ± to a compact Hausdorff space Ξ³ to the Stone-Čech compactification of Ξ±. -/ def stoneCechExtend : StoneCech Ξ± β†’ Ξ³ := Quotient.lift (Ultrafilter.extend f) fun x y xy => xy Ξ³ f hf #align stone_cech_extend stoneCechExtend -/ #print stoneCechExtend_extends /- theorem stoneCechExtend_extends : stoneCechExtend hf ∘ stoneCechUnit = f := ultrafilter_extend_extends f #align stone_cech_extend_extends stoneCechExtend_extends -/ #print continuous_stoneCechExtend /- theorem continuous_stoneCechExtend : Continuous (stoneCechExtend hf) := continuous_quot_lift _ (continuous_ultrafilter_extend f) #align continuous_stone_cech_extend continuous_stoneCechExtend -/ #print stoneCech_hom_ext /- theorem stoneCech_hom_ext {g₁ gβ‚‚ : StoneCech Ξ± β†’ Ξ³'} (h₁ : Continuous g₁) (hβ‚‚ : Continuous gβ‚‚) (h : g₁ ∘ stoneCechUnit = gβ‚‚ ∘ stoneCechUnit) : g₁ = gβ‚‚ := by apply Continuous.ext_on denseRange_stoneCechUnit h₁ hβ‚‚ rintro x ⟨x, rfl⟩ apply congr_fun h x #align stone_cech_hom_ext stoneCech_hom_ext -/ end Extension /- warning: convergent_eqv_pure -> convergent_eqv_pure is a dubious translation: lean 3 declaration is forall {Ξ± : Type.{u1}} [_inst_1 : TopologicalSpace.{u1} Ξ±] {u : Ultrafilter.{u1} Ξ±} {x : Ξ±}, (LE.le.{u1} (Filter.{u1} Ξ±) (Preorder.toLE.{u1} (Filter.{u1} Ξ±) (PartialOrder.toPreorder.{u1} (Filter.{u1} Ξ±) (Filter.partialOrder.{u1} Ξ±))) ((fun (a : Type.{u1}) (b : Type.{u1}) [self : HasLiftT.{succ u1, succ u1} a b] => self.0) (Ultrafilter.{u1} Ξ±) (Filter.{u1} Ξ±) (HasLiftT.mk.{succ u1, succ u1} (Ultrafilter.{u1} Ξ±) (Filter.{u1} Ξ±) (CoeTCβ‚“.coe.{succ u1, succ u1} (Ultrafilter.{u1} Ξ±) (Filter.{u1} Ξ±) (Ultrafilter.Filter.hasCoeT.{u1} Ξ±))) u) (nhds.{u1} Ξ± _inst_1 x)) -> (HasEquivβ‚“.Equiv.{succ u1} (Ultrafilter.{u1} Ξ±) (setoidHasEquiv.{succ u1} (Ultrafilter.{u1} Ξ±) (stoneCechSetoid.{u1} Ξ± _inst_1)) u (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.hasPure.{u1} Ξ± x)) but is expected to have type forall {Ξ± : Type.{u1}} [_inst_1 : TopologicalSpace.{u1} Ξ±] {u : Ultrafilter.{u1} Ξ±} {x : Ξ±}, (LE.le.{u1} (Filter.{u1} Ξ±) (Preorder.toLE.{u1} (Filter.{u1} Ξ±) (PartialOrder.toPreorder.{u1} (Filter.{u1} Ξ±) (Filter.instPartialOrderFilter.{u1} Ξ±))) (Ultrafilter.toFilter.{u1} Ξ± u) (nhds.{u1} Ξ± _inst_1 x)) -> (HasEquiv.Equiv.{succ u1, 0} (Ultrafilter.{u1} Ξ±) (instHasEquiv.{succ u1} (Ultrafilter.{u1} Ξ±) (stoneCechSetoid.{u1} Ξ± _inst_1)) u (Pure.pure.{u1, u1} Ultrafilter.{u1} Ultrafilter.instPureUltrafilter.{u1} Ξ± x)) Case conversion may be inaccurate. Consider using '#align convergent_eqv_pure convergent_eqv_pureβ‚“'. -/ theorem convergent_eqv_pure {u : Ultrafilter Ξ±} {x : Ξ±} (ux : ↑u ≀ 𝓝 x) : u β‰ˆ pure x := fun Ξ³ tΞ³ h₁ hβ‚‚ f hf => by skip trans f x; swap; symm all_goals refine' ultrafilter_extend_eq_iff.mpr (le_trans (map_mono _) (hf.tendsto _)) Β· apply pure_le_nhds; Β· exact ux #align convergent_eqv_pure convergent_eqv_pure #print continuous_stoneCechUnit /- theorem continuous_stoneCechUnit : Continuous (stoneCechUnit : Ξ± β†’ StoneCech Ξ±) := continuous_iff_ultrafilter.mpr fun x g gx => by have : ↑(g.map pure) ≀ 𝓝 g := by rw [ultrafilter_converges_iff] <;> exact (bind_pure _).symm have : (g.map stoneCechUnit : Filter (StoneCech Ξ±)) ≀ 𝓝 ⟦g⟧ := continuousAt_iff_ultrafilter.mp (continuous_quotient_mk'.Tendsto g) _ this rwa [show ⟦g⟧ = ⟦pure x⟧ from Quotient.sound <| convergent_eqv_pure gx] at this #align continuous_stone_cech_unit continuous_stoneCechUnit -/ #print StoneCech.t2Space /- instance StoneCech.t2Space : T2Space (StoneCech Ξ±) := by rw [t2_iff_ultrafilter] rintro ⟨x⟩ ⟨y⟩ g gx gy apply Quotient.sound intro Ξ³ tΞ³ h₁ hβ‚‚ f hf skip let ff := stoneCechExtend hf change ff ⟦x⟧ = ff ⟦y⟧ have lim := fun (z : Ultrafilter Ξ±) (gz : (g : Filter (StoneCech Ξ±)) ≀ 𝓝 ⟦z⟧) => ((continuous_stoneCechExtend hf).Tendsto _).mono_left gz exact tendsto_nhds_unique (limUnder x gx) (limUnder y gy) #align stone_cech.t2_space StoneCech.t2Space -/ #print StoneCech.compactSpace /- instance StoneCech.compactSpace : CompactSpace (StoneCech Ξ±) := Quotient.compactSpace #align stone_cech.compact_space StoneCech.compactSpace -/ end StoneCech
{-# OPTIONS --rewriting #-} -- 2015-02-17 Jesper and Andreas postulate A : Set R : A β†’ A β†’ Set f : A β†’ A g : A β†’ A r : R (f _) (g _) {-# BUILTIN REWRITE R #-} {-# REWRITE r #-}
import data.real.irrational import topology.basic import algebra.order.floor --OUTPUT 1 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 := begin -- Let $\alpha$ be an irrational number. Then for distinct $i, j \in \mathbb{Z}$, we must have $\{i \alpha\} \neq\{j \alpha\}$. have h1 : βˆ€ i j : β„€, i β‰  j β†’ int.fract (Ξ± * ↑i) β‰  int.fract (Ξ± * ↑j), from by { assume (i j : β„€) (hneq : i β‰  j), -- If this were not true, then assume hfalse : int.fract (Ξ± * ↑i) = int.fract (Ξ± * ↑j), -- $i \alpha-\lfloor i \alpha\rfloor=\{i \alpha\}=\{j \alpha\}=j \alpha-\lfloor j \alpha\rfloor$, have h2 : (Ξ± * ↑i) - (int.floor (Ξ± * ↑i)) = int.fract (Ξ± * ↑i), from by {rw ← int.fract_eq_of_nonneg (Ξ± * ↑i), ring}, have h3 : (Ξ± * ↑j) - (int.floor (Ξ± * ↑j)) = int.fract (Ξ± * ↑j), from by {rw ← int.fract_eq_of_nonneg (Ξ± * ↑j), ring}, have h4 : (Ξ± * ↑i) - (int.floor (Ξ± * ↑i)) = (Ξ± * ↑j) - (int.floor (Ξ± * ↑j)), from by {rw [h2,h3,hfalse]}, -- which yields the false statement $\alpha=\frac{\lfloor i \alpha\rfloor-\lfloor j \alpha\rfloor}{i-j} \in \mathbb{Q}$. have h5 : Ξ± = (int.floor (Ξ± * ↑i) - int.floor (Ξ± * ↑j)) / (i - j), from by {rw [h4,int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int.fract_eq_of_nonneg (Ξ± * ↑i),int.fract_eq_of_nonneg (Ξ± * ↑j),int. end --Needs more than 2000 tokens! --OUTPUT 2 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 := begin -- Let $\alpha$ be an irrational number. Then for distinct $i, j \in \mathbb{Z}$, we must have $\{i \alpha\} \neq\{j \alpha\}$. If this were not true, then have h1 : βˆ€ i j : β„€, i β‰  j β†’ int.fract (Ξ± * ↑i) β‰  int.fract (Ξ± * ↑j), from by { assume i j : β„€, assume hneq : i β‰  j, assume hfracteq : int.fract (Ξ± * ↑i) = int.fract (Ξ± * ↑j), -- then -- $$ -- i \alpha-\lfloor i \alpha\rfloor=\{i \alpha\}=\{j \alpha\}=j \alpha-\lfloor j \alpha\rfloor, -- $$ have h2 : (Ξ± * ↑i) - (int.floor (Ξ± * ↑i)) = int.fract (Ξ± * ↑i), from by {rw ← hfracteq, rw int.fract_eq_of_nonneg (le_of_lt (int.floor_lt_iff.mpr (int.lt_of_mul_lt_mul_left (int.coe_nat_lt.mpr (nat.succ_pos _)) (int.coe_nat_pos.mpr (nat.succ_pos _)))))}, have h3 : (Ξ± * ↑j) - (int.floor (Ξ± * ↑j)) = int.fract (Ξ± * ↑j), from by {rw ← hfracteq, rw int.fract_eq_of_nonneg (le_of_lt (int.floor_lt_iff.mpr (int.lt_of_mul_lt_mul_left (int.coe_nat_lt.mpr (nat.succ_pos _)) (int.coe_nat_pos.mpr (nat.succ_pos _)))))}, -- which yields the false statement $\alpha=\frac{\lfloor i \alpha\rfloor-\lfloor j \alpha\rfloor}{i-j} \in \mathbb{Q}$. have h4 : Ξ± = ((int.floor (Ξ± * ↑i)) - (int.floor (Ξ± * ↑j))) / (i - j), from by {rw [h2,h3,int.sub_sub_cancel], ring}, have h5 : Ξ± ∈ β„š, from by {apply quotient.exact, rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat, ← int.cast_coe_nat], rw [← int.cast_coe_nat, ← end --Needs more than 2000 tokens! --OUTPUT 3 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 := begin -- Let $\alpha$ be an irrational number. Then for distinct $i, j \in \mathbb{Z}$, we must have $\{i \alpha\} \neq\{j \alpha\}$. have h1 : βˆ€ i j : β„€, i β‰  j β†’ int.fract (Ξ± * ↑i) β‰  int.fract (Ξ± * ↑j), from assume (i j : β„€) (hij : i β‰  j), have h2 : Ξ± β‰  (int.fract (Ξ± * ↑i) - int.fract (Ξ± * ↑j)) / (i - j), from assume h3 : Ξ± = (int.fract (Ξ± * ↑i) - int.fract (Ξ± * ↑j)) / (i - j), have h4 : (int.fract (Ξ± * ↑i) - int.fract (Ξ± * ↑j)) / (i - j) ∈ β„š, from by { apply quotient.exact, rw [h3, ← int.coe_nat_eq_coe_nat_iff], apply int.coe_nat_dvd, rw [int.coe_nat_sub, int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_sub, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, rw [int.coe_nat_mul, int.coe_nat_mul], apply int.coe_nat_dvd, end --Needs more than 2000 tokens! /- FEW SHOT PROMPTS TO CODEX(START) /--`theorem` Power Set is Closed under Intersection Let $S$ be a set. Let $\powerset S$ be the power set of $S$. Then: :$\forall A, B \in \powerset S: A \cap B \in \powerset S$ `proof` Let $A, B \in \powerset S$. Then by the definition of power set, $A \subseteq S$ and $B \subseteq S$. From Intersection is Subset we have that $A \cap B \subseteq A$. It follows from Subset Relation is Transitive that $A \cap B \subseteq S$. Thus $A \cap B \in \powerset S$ and closure is proved. {{qed}} -/ theorem power_set_intersection_closed {Ξ± : Type*} (S : set Ξ±) : βˆ€ A B ∈ 𝒫 S, (A ∩ B) ∈ 𝒫 S := begin -- $A$ and $B$ are sets. $A$ and $B$ belong to power set of $S$ assume (A : set Ξ±) (hA : A ∈ 𝒫 S) (B : set Ξ±) (hB : B ∈ 𝒫 S), -- Then $A βŠ† S$ and $B βŠ† S$, by power set definition have h1 : (A βŠ† S) ∧ (B βŠ† S), from by {split,apply set.subset_of_mem_powerset,exact hA,apply set.subset_of_mem_powerset,exact hB}, -- Then $(A ∩ B) βŠ† A$, by intersection of set is a subset have h2 : (A ∩ B) βŠ† A, from by apply set.inter_subset_left, -- Then $(A ∩ B) βŠ† S$, by subset relation is transitive have h3 : (A ∩ B) βŠ† S, from by {apply set.subset.trans h2 h1.left}, -- Hence $(A ∩ B) ∈ 𝒫 S$, by power set definition show (A ∩ B) ∈ 𝒫 S, from by {apply set.mem_powerset h3}, end /--`theorem` Square of Sum :$\forall x, y \in \R: \paren {x + y}^2 = x^2 + 2 x y + y^2$ `proof` Follows from the distribution of multiplication over addition: {{begin-eqn}} {{eqn | l = \left({x + y}\right)^2 | r = \left({x + y}\right) \cdot \left({x + y}\right) }} {{eqn | r = x \cdot \left({x + y}\right) + y \cdot \left({x + y}\right) | c = Real Multiplication Distributes over Addition }} {{eqn | r = x \cdot x + x \cdot y + y \cdot x + y \cdot y | c = Real Multiplication Distributes over Addition }} {{eqn | r = x^2 + 2xy + y^2 | c = }} {{end-eqn}} {{qed}} -/ theorem square_of_sum (x y : ℝ) : (x + y)^2 = (x^2 + 2*x*y + y^2) := begin -- expand the power calc (x + y)^2 = (x+y)*(x+y) : by rw sq -- distributive property of multiplication over addition gives: ... = x*(x+y) + y*(x+y) : by rw add_mul -- applying the above property further gives: ... = x*x + x*y + y*x + y*y : by {rw [mul_comm x (x+y),mul_comm y (x+y)], rw [add_mul,add_mul], ring} -- rearranging the terms using commutativity and adding gives: ... = x^2 + 2*x*y + y^2 : by {repeat {rw ← sq}, rw mul_comm y x, ring} end /--`theorem` Identity of Group is Unique Let $\struct {G, \circ}$ be a group. Then there is a unique identity element $e \in G$. `proof` From Group has Latin Square Property, there exists a unique $x \in G$ such that: :$a x = b$ and there exists a unique $y \in G$ such that: :$y a = b$ Setting $b = a$, this becomes: There exists a unique $x \in G$ such that: :$a x = a$ and there exists a unique $y \in G$ such that: :$y a = a$ These $x$ and $y$ are both $e$, by definition of identity element. {{qed}} -/ theorem group_identity_unique {G : Type*} [group G] : βˆƒ! e : G, βˆ€ a : G, e * a = a ∧ a * e = a := begin -- Group has Latin Square Property have h1 : βˆ€ a b : G, βˆƒ! x : G, a * x = b, from by { assume a b : G, use a⁻¹ * b, obviously, }, have h2 : βˆ€ a b : G, βˆƒ! y : G, y * a = b, from by { assume a b : G, use b * a⁻¹, obviously, }, -- Setting $b = a$, this becomes: have h3 : βˆ€ a : G, βˆƒ! x : G, a * x = a, from assume a : G, h1 a a, have h4 : βˆ€ a : G, βˆƒ! y : G, y * a = a, from assume a : G, h2 a a, -- These $x$ and $y$ are both $(1 : G)$, by definition of identity element have h5 : βˆ€ a : G, classical.some (h3 a).exists = (1 : G), from assume a :G, exists_unique.unique (h3 a) (classical.some_spec (exists_unique.exists (h3 a))) (mul_one a), have h6 : βˆ€ a : G, classical.some (h4 a).exists = (1 : G), from assume a : G, exists_unique.unique (h4 a) (classical.some_spec (exists_unique.exists (h4 a))) (one_mul a), show βˆƒ! e : G, βˆ€ a : G, e * a = a ∧ a * e = a, from by { use (1 : G), have h7 : βˆ€ e : G, (βˆ€ a : G, e * a = a ∧ a * e = a) β†’ e = 1, from by { assume (e : G) (hident : βˆ€ a : G, e * a = a ∧ a * e = a), have h8 : βˆ€ a : G, e = classical.some (h3 a).exists, from assume (a : G), exists_unique.unique (h3 a) (hident a).right (classical.some_spec (exists_unique.exists (h3 a))), have h9 : βˆ€ a : G, e = classical.some (h4 a).exists, from assume (a : G), exists_unique.unique (h4 a) (hident a).left (classical.some_spec (exists_unique.exists (h4 a))), show e = (1 : G), from eq.trans (h9 e) (h6 _), }, exact ⟨by obviously, h7⟩, } end /--`theorem` Density of irrational orbit The fractional parts of the integer multiples of an irrational number form a dense subset of the unit interval `proof` Let $\alpha$ be an irrational number. Then for distinct $i, j \in \mathbb{Z}$, we must have $\{i \alpha\} \neq\{j \alpha\}$. If this were not true, then $$ i \alpha-\lfloor i \alpha\rfloor=\{i \alpha\}=\{j \alpha\}=j \alpha-\lfloor j \alpha\rfloor, $$ which yields the false statement $\alpha=\frac{\lfloor i \alpha\rfloor-\lfloor j \alpha\rfloor}{i-j} \in \mathbb{Q}$. Hence, $$ S:=\{\{i \alpha\} \mid i \in \mathbb{Z}\} $$ is an infinite subset of $\left[0,1\right]$. By the Bolzano-Weierstrass theorem, $S$ has a limit point in $[0, 1]$. One can thus find pairs of elements of $S$ that are arbitrarily close. Since (the absolute value of) the difference of any two elements of $S$ is also an element of $S$, it follows that $0$ is a limit point of $S$. To show that $S$ is dense in $[0, 1]$, consider $y \in[0,1]$, and $\epsilon>0$. Then by selecting $x \in S$ such that $\{x\}<\epsilon$ (which exists as $0$ is a limit point), and $N$ such that $N \cdot\{x\} \leq y<(N+1) \cdot\{x\}$, we get: $|y-\{N x\}|<\epsilon$. QED -/ theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 := FEW SHOT PROMPTS TO CODEX(END)-/
# breadth first search function bfs(goal, start) reset_timer!(to::TimerOutput) node = createtree(start) if node.state == goal return node end frontier = [node] reached = [node.state] while !isempty(frontier) @timeit to "Moves" acts = moves(node.state) @timeit to "Pop" node = popfirst!(frontier) @timeit to "Expand" states = expansion(node.state) for (currentstate, currentaction) in zip(states, acts) if currentstate == goal sovle = addnode(currentaction, node, currentstate) displaysolution(sovle) return sovle end if !in(currentstate, reached) @timeit to "Node Creation" currentnode = addnode(currentaction, node, currentstate) @timeit to "Push" push!(frontier, currentnode) end end end println("No solutions found!"^10) return nothing end
import MyNat.Definition import MyNat.Addition -- add_succ import MyNat.Multiplication -- mul_succ import AdvancedAdditionWorld.Level9 -- succ_ne_zero import Mathlib.Tactic.LeftRight namespace MyNat open MyNat /-! # Advanced Multiplication World ## Level 2: `eq_zero_or_eq_zero_of_mul_eq_zero` A variant on the previous level. ## Theorem If `ab = 0`, then at least one of `a` or `b` is equal to zero. -/ theorem eq_zero_or_eq_zero_of_mul_eq_zero (a b : MyNat) (h : a * b = 0) : a = 0 ∨ b = 0 := by cases a with | zero => left rfl | succ a' => cases b with | zero => right rfl | succ b' => exfalso rw [mul_succ] at h rw [add_succ] at h exact succ_ne_zero _ h /-! Next up [Level 3](./Level3.lean.md) -/
(* Title: HOL/Isar_Examples/Peirce.thy Author: Makarius *) section \<open>Peirce's Law\<close> theory Peirce imports Main begin text \<open> We consider Peirce's Law: \<open>((A \<longrightarrow> B) \<longrightarrow> A) \<longrightarrow> A\<close>. This is an inherently non-intuitionistic statement, so its proof will certainly involve some form of classical contradiction. The first proof is again a well-balanced combination of plain backward and forward reasoning. The actual classical step is where the negated goal may be introduced as additional assumption. This eventually leads to a contradiction.\<^footnote>\<open>The rule involved there is negation elimination; it holds in intuitionistic logic as well.\<close>\<close> theorem "((A \<longrightarrow> B) \<longrightarrow> A) \<longrightarrow> A" proof assume "(A \<longrightarrow> B) \<longrightarrow> A" show A proof (rule classical) assume "\<not> A" have "A \<longrightarrow> B" proof assume A with \<open>\<not> A\<close> show B by contradiction qed with \<open>(A \<longrightarrow> B) \<longrightarrow> A\<close> show A .. qed qed text \<open> In the subsequent version the reasoning is rearranged by means of ``weak assumptions'' (as introduced by \<^theory_text>\<open>presume\<close>). Before assuming the negated goal \<open>\<not> A\<close>, its intended consequence \<open>A \<longrightarrow> B\<close> is put into place in order to solve the main problem. Nevertheless, we do not get anything for free, but have to establish \<open>A \<longrightarrow> B\<close> later on. The overall effect is that of a logical \<^emph>\<open>cut\<close>. Technically speaking, whenever some goal is solved by \<^theory_text>\<open>show\<close> in the context of weak assumptions then the latter give rise to new subgoals, which may be established separately. In contrast, strong assumptions (as introduced by \<^theory_text>\<open>assume\<close>) are solved immediately. \<close> theorem "((A \<longrightarrow> B) \<longrightarrow> A) \<longrightarrow> A" proof assume "(A \<longrightarrow> B) \<longrightarrow> A" show A proof (rule classical) presume "A \<longrightarrow> B" with \<open>(A \<longrightarrow> B) \<longrightarrow> A\<close> show A .. next assume "\<not> A" show "A \<longrightarrow> B" proof assume A with \<open>\<not> A\<close> show B by contradiction qed qed qed text \<open> Note that the goals stemming from weak assumptions may be even left until qed time, where they get eventually solved ``by assumption'' as well. In that case there is really no fundamental difference between the two kinds of assumptions, apart from the order of reducing the individual parts of the proof configuration. Nevertheless, the ``strong'' mode of plain assumptions is quite important in practice to achieve robustness of proof text interpretation. By forcing both the conclusion \<^emph>\<open>and\<close> the assumptions to unify with the pending goal to be solved, goal selection becomes quite deterministic. For example, decomposition with rules of the ``case-analysis'' type usually gives rise to several goals that only differ in there local contexts. With strong assumptions these may be still solved in any order in a predictable way, while weak ones would quickly lead to great confusion, eventually demanding even some backtracking. \<close> end
section \<open>Sets and Extended Natural Numbers\<close> theory ESet_Extensions imports "../Basics/Functions" Basic_Extensions CCPO_Extensions begin lemma card_lessThan_enat[simp]: "card {..< enat k} = card {..< k}" proof - have 1: "{..< enat k} = enat ` {..< k}" unfolding lessThan_def image_Collect using enat_iless by force have "card {..< enat k} = card (enat ` {..< k})" unfolding 1 by rule also have "\<dots> = card {..< k}" using card_image inj_enat by metis finally show ?thesis by this qed lemma card_atMost_enat[simp]: "card {.. enat k} = card {.. k}" proof - have 1: "{.. enat k} = enat ` {.. k}" unfolding atMost_def image_Collect using enat_ile by force have "card {.. enat k} = card (enat ` {.. k})" unfolding 1 by rule also have "\<dots> = card {.. k}" using card_image inj_enat by metis finally show ?thesis by this qed lemma enat_Collect: assumes "\<infinity> \<notin> A" shows "{i. enat i \<in> A} = the_enat ` A" using assms by (safe, force) (metis enat_the_enat) lemma Collect_lessThan: "{i. enat i < n} = the_enat ` {..< n}" proof - have 1: "\<infinity> \<notin> {..< n}" by simp have "{i. enat i < n} = {i. enat i \<in> {..< n}}" by simp also have "\<dots> = the_enat ` {..< n}" using enat_Collect 1 by this finally show ?thesis by this qed instantiation set :: (type) esize_ccpo begin function esize_set where "finite A \<Longrightarrow> esize A = enat (card A)" | "infinite A \<Longrightarrow> esize A = \<infinity>" by auto termination by lexicographic_order lemma esize_iff_empty[iff]: "esize A = 0 \<longleftrightarrow> A = {}" by (cases "finite A", auto) lemma esize_iff_infinite[iff]: "esize A = \<infinity> \<longleftrightarrow> infinite A" by force lemma esize_singleton[simp]: "esize {a} = eSuc 0" by simp instance proof fix A :: "'a set" assume 1: "esize A \<noteq> \<infinity>" show "finite {B. B \<subseteq> A}" using 1 by simp next fix A B :: "'a set" assume 1: "A \<subseteq> B" show "esize A \<le> esize B" proof (cases "finite B") case False show ?thesis using False by auto next case True have 2: "finite A" using True 1 by auto show ?thesis using card_mono True 1 2 by auto qed next fix A B :: "'a set" assume 1: "esize A \<noteq> \<infinity>" "A \<subset> B" show "esize A < esize B" using psubset_card_mono 1 by (cases "finite B", auto) qed end lemma esize_image[simp, intro]: assumes "inj_on f A" shows "esize (f ` A) = esize A" using card_image finite_imageD assms by (cases "finite A", auto) lemma esize_insert1[simp]: "a \<notin> A \<Longrightarrow> esize (insert a A) = eSuc (esize A)" by (cases "finite A", force+) lemma esize_insert2[simp]: "a \<in> A \<Longrightarrow> esize (insert a A) = esize A" using insert_absorb by metis lemma esize_remove1[simp]: "a \<notin> A \<Longrightarrow> esize (A - {a}) = esize A" by (cases "finite A", force+) lemma esize_remove2[simp]: "a \<in> A \<Longrightarrow> esize (A - {a}) = epred (esize A)" by (cases "finite A", force+) lemma esize_union_disjoint[simp]: assumes "A \<inter> B = {}" shows "esize (A \<union> B) = esize A + esize B" proof (cases "finite (A \<union> B)") case True show ?thesis using card_Un_disjoint assms True by auto next case False show ?thesis using False by (cases "finite A", auto) qed lemma esize_lessThan[simp]: "esize {..< n} = n" proof (cases n) case (enat k) have 1: "finite {..< n}" unfolding enat by (metis finite_lessThan_enat_iff not_enat_eq) show ?thesis using 1 unfolding enat by simp next case (infinity) have 1: "infinite {..< n}" unfolding infinity using infinite_lessThan_infty by simp show ?thesis using 1 unfolding infinity by simp qed lemma esize_atMost[simp]: "esize {.. n} = eSuc n" proof (cases n) case (enat k) have 1: "finite {.. n}" unfolding enat by (metis atMost_iff finite_enat_bounded) show ?thesis using 1 unfolding enat by simp next case (infinity) have 1: "infinite {.. n}" unfolding infinity by (metis atMost_iff enat_ord_code(3) infinite_lessThan_infty infinite_super subsetI) show ?thesis using 1 unfolding infinity by simp qed lemma least_eSuc[simp]: assumes "A \<noteq> {}" shows "least (eSuc ` A) = eSuc (least A)" proof (rule antisym) obtain k where 10: "k \<in> A" using assms by blast have 11: "eSuc k \<in> eSuc ` A" using 10 by auto have 20: "least A \<in> A" using 10 LeastI by metis have 21: "least (eSuc ` A) \<in> eSuc ` A" using 11 LeastI by metis have 30: "\<And> l. l \<in> A \<Longrightarrow> least A \<le> l" using 10 Least_le by metis have 31: "\<And> l. l \<in> eSuc ` A \<Longrightarrow> least (eSuc ` A) \<le> l" using 11 Least_le by metis show "least (eSuc ` A) \<le> eSuc (least A)" using 20 31 by auto show "eSuc (least A) \<le> least (eSuc ` A)" using 21 30 by auto qed lemma Inf_enat_eSuc[simp]: "\<Sqinter> (eSuc ` A) = eSuc (\<Sqinter> A)" unfolding Inf_enat_def by simp definition lift :: "nat set \<Rightarrow> nat set" where "lift A \<equiv> insert 0 (Suc ` A)" lemma liftI_0[intro, simp]: "0 \<in> lift A" unfolding lift_def by auto lemma liftI_Suc[intro]: "a \<in> A \<Longrightarrow> Suc a \<in> lift A" unfolding lift_def by auto lemma liftE[elim]: assumes "b \<in> lift A" obtains (0) "b = 0" | (Suc) a where "b = Suc a" "a \<in> A" using assms unfolding lift_def by auto lemma lift_esize[simp]: "esize (lift A) = eSuc (esize A)" unfolding lift_def by auto lemma lift_least[simp]: "least (lift A) = 0" unfolding lift_def by auto primrec nth_least :: "'a set \<Rightarrow> nat \<Rightarrow> 'a :: wellorder" where "nth_least A 0 = least A" | "nth_least A (Suc n) = nth_least (A - {least A}) n" lemma nth_least_wellformed[intro?, simp]: assumes "enat n < esize A" shows "nth_least A n \<in> A" using assms proof (induct n arbitrary: A) case 0 show ?case using 0 by simp next case (Suc n) have 1: "A \<noteq> {}" using Suc(2) by auto have 2: "enat n < esize (A - {least A})" using Suc(2) 1 by simp have 3: "nth_least (A - {least A}) n \<in> A - {least A}" using Suc(1) 2 by this show ?case using 3 by simp qed lemma card_wellformed[intro?, simp]: fixes k :: "'a :: wellorder" assumes "k \<in> A" shows "enat (card {i \<in> A. i < k}) < esize A" proof (cases "finite A") case False show ?thesis using False by simp next case True have 1: "esize {i \<in> A. i < k} < esize A" using True assms by fastforce show ?thesis using True 1 by simp qed lemma card_nth_least[simp]: assumes "enat n < esize A" shows "card {k \<in> A. k < nth_least A n} = n" using assms proof (induct n arbitrary: A) case 0 have 1: "{k \<in> A. k < least A} = {}" using least_not_less by auto show ?case using nth_least.simps(1) card_empty 1 by metis next case (Suc n) have 1: "A \<noteq> {}" using Suc(2) by auto have 2: "enat n < esize (A - {least A})" using Suc(2) 1 by simp have 3: "nth_least A 0 < nth_least A (Suc n)" using nth_least_strict_mono Suc(2) by blast have 4: "{k \<in> A. k < nth_least A (Suc n)} = {least A} \<union> {k \<in> A - {least A}. k < nth_least (A - {least A}) n}" using 1 3 by auto have 5: "card {k \<in> A - {least A}. k < nth_least (A - {least A}) n} = n" using Suc(1) 2 by this have 6: "finite {k \<in> A - {least A}. k < nth_least (A - {least A}) n}" using 5 Collect_empty_eq card_infinite infinite_imp_nonempty least_not_less nth_least.simps(1) by (metis (no_types, lifting)) have "card {k \<in> A. k < nth_least A (Suc n)} = card ({least A} \<union> {k \<in> A - {least A}. k < nth_least (A - {least A}) n})" using 4 by simp also have "\<dots> = card {least A} + card {k \<in> A - {least A}. k < nth_least (A - {least A}) n}" using 6 by simp also have "\<dots> = Suc n" using 5 by simp finally show ?case by this qed lemma card_nth_least_le[simp]: assumes "enat n < esize A" shows "card {k \<in> A. k \<le> nth_least A n} = Suc n" proof - have 1: "{k \<in> A. k \<le> nth_least A n} = {nth_least A n} \<union> {k \<in> A. k < nth_least A n}" using assms by auto have 2: "card {k \<in> A. k < nth_least A n} = n" using assms by simp have 3: "finite {k \<in> A. k < nth_least A n}" using 2 Collect_empty_eq card_infinite infinite_imp_nonempty least_not_less nth_least.simps(1) by (metis (no_types, lifting)) have "card {k \<in> A. k \<le> nth_least A n} = card ({nth_least A n} \<union> {k \<in> A. k < nth_least A n})" unfolding 1 by rule also have "\<dots> = card {nth_least A n} + card {k \<in> A. k < nth_least A n}" using 3 by simp also have "\<dots> = Suc n" using assms by simp finally show ?thesis by this qed lemma nth_least_card: fixes k :: nat assumes "k \<in> A" shows "nth_least A (card {i \<in> A. i < k}) = k" proof (rule nat_set_card_equality_less) have 1: "enat (card {l \<in> A. l < k}) < esize A" proof (cases "finite A") case False show ?thesis using False by simp next case True have 1: "{l \<in> A. l < k} \<subset> A" using assms by blast have 2: "card {l \<in> A. l < k} < card A" using psubset_card_mono True 1 by this show ?thesis using True 2 by simp qed show "nth_least A (card {l \<in> A. l < k}) \<in> A" using 1 by rule show "k \<in> A" using assms by this show "card {z \<in> A. z < nth_least A (card {i \<in> A. i < k})} = card {z \<in> A. z < k}" using 1 by simp qed interpretation nth_least: bounded_function_pair "{i. enat i < esize A}" "A" "nth_least A" "\<lambda> k. card {i \<in> A. i < k}" using nth_least_wellformed card_wellformed by (unfold_locales, blast+) interpretation nth_least: injection "{i. enat i < esize A}" "A" "nth_least A" "\<lambda> k. card {i \<in> A. i < k}" using card_nth_least by (unfold_locales, blast) interpretation nth_least: surjection "{i. enat i < esize A}" "A" "nth_least A" "\<lambda> k. card {i \<in> A. i < k}" for A :: "nat set" using nth_least_card by (unfold_locales, blast) interpretation nth_least: bijection "{i. enat i < esize A}" "A" "nth_least A" "\<lambda> k. card {i \<in> A. i < k}" for A :: "nat set" by unfold_locales lemma nth_least_strict_mono_inverse: fixes A :: "nat set" assumes "enat k < esize A" "enat l < esize A" "nth_least A k < nth_least A l" shows "k < l" using assms by (metis not_less_iff_gr_or_eq nth_least_strict_mono) lemma nth_least_less_card_less: fixes k :: nat shows "enat n < esize A \<and> nth_least A n < k \<longleftrightarrow> n < card {i \<in> A. i < k}" proof safe assume 1: "enat n < esize A" "nth_least A n < k" have 2: "nth_least A n \<in> A" using 1(1) by rule have "n = card {i \<in> A. i < nth_least A n}" using 1 by simp also have "\<dots> < card {i \<in> A. i < k}" using 1(2) 2 by simp finally show "n < card {i \<in> A. i < k}" by this next assume 1: "n < card {i \<in> A. i < k}" have "enat n < enat (card {i \<in> A. i < k})" using 1 by simp also have "\<dots> = esize {i \<in> A. i < k}" by simp also have "\<dots> \<le> esize A" by blast finally show 2: "enat n < esize A" by this have 3: "n = card {i \<in> A. i < nth_least A n}" using 2 by simp have 4: "card {i \<in> A. i < nth_least A n} < card {i \<in> A. i < k}" using 1 2 by simp have 5: "nth_least A n \<in> A" using 2 by rule show "nth_least A n < k" using 4 5 by simp qed lemma nth_least_less_esize_less: "enat n < esize A \<and> enat (nth_least A n) < k \<longleftrightarrow> enat n < esize {i \<in> A. enat i < k}" using nth_least_less_card_less by (cases k, simp+) lemma nth_least_le: assumes "enat n < esize A" shows "n \<le> nth_least A n" using assms proof (induct n) case 0 show ?case using 0 by simp next case (Suc n) have "n \<le> nth_least A n" using Suc by (metis Suc_ile_eq less_imp_le) also have "\<dots> < nth_least A (Suc n)" using nth_least_strict_mono Suc(2) by blast finally show ?case by simp qed lemma nth_least_eq: assumes "enat n < esize A" "enat n < esize B" assumes "\<And> i. i \<le> nth_least A n \<Longrightarrow> i \<le> nth_least B n \<Longrightarrow> i \<in> A \<longleftrightarrow> i \<in> B" shows "nth_least A n = nth_least B n" using assms proof (induct n arbitrary: A B) case 0 have 1: "least A = least B" proof (rule least_eq) show "A \<noteq> {}" using 0(1) by simp show "B \<noteq> {}" using 0(2) by simp next fix i assume 2: "i \<le> least A" "i \<le> least B" show "i \<in> A \<longleftrightarrow> i \<in> B" using 0(3) 2 unfolding nth_least.simps by this qed show ?case using 1 by simp next case (Suc n) have 1: "A \<noteq> {}" "B \<noteq> {}" using Suc(2, 3) by auto have 2: "least A = least B" proof (rule least_eq) show "A \<noteq> {}" using 1(1) by this show "B \<noteq> {}" using 1(2) by this next fix i assume 3: "i \<le> least A" "i \<le> least B" have 4: "nth_least A 0 \<le> nth_least A (Suc n)" using Suc(2) by blast have 5: "nth_least B 0 \<le> nth_least B (Suc n)" using Suc(3) by blast have 6: "i \<le> nth_least A (Suc n)" "i \<le> nth_least B (Suc n)" using 3 4 5 by auto show "i \<in> A \<longleftrightarrow> i \<in> B" using Suc(4) 6 by this qed have 3: "nth_least (A - {least A}) n = nth_least (B - {least B}) n" proof (rule Suc(1)) show "enat n < esize (A - {least A})" using Suc(2) 1(1) by simp show "enat n < esize (B - {least B})" using Suc(3) 1(2) by simp next fix i assume 3: "i \<le> nth_least (A - {least A}) n" "i \<le> nth_least (B - {least B}) n" have 4: "i \<le> nth_least A (Suc n)" "i \<le> nth_least B (Suc n)" using 3 by simp+ have 5: "i \<in> A \<longleftrightarrow> i \<in> B" using Suc(4) 4 by this show "i \<in> A - {least A} \<longleftrightarrow> i \<in> B - {least B}" using 2 5 by auto qed show ?case using 3 by simp qed lemma nth_least_restrict[simp]: assumes "enat i < esize {i \<in> s. enat i < k}" shows "nth_least {i \<in> s. enat i < k} i = nth_least s i" proof (rule nth_least_eq) show "enat i < esize {i \<in> s. enat i < k}" using assms by this show "enat i < esize s" using nth_least_less_esize_less assms by auto next fix l assume 1: "l \<le> nth_least {i \<in> s. enat i < k} i" have 2: "nth_least {i \<in> s. enat i < k} i \<in> {i \<in> s. enat i < k}" using assms by rule have "enat l \<le> enat (nth_least {i \<in> s. enat i < k} i)" using 1 by simp also have "\<dots> < k" using 2 by simp finally show "l \<in> {i \<in> s. enat i < k} \<longleftrightarrow> l \<in> s" by auto qed lemma least_nth_least[simp]: assumes "A \<noteq> {}" "\<And> i. i \<in> A \<Longrightarrow> enat i < esize B" shows "least (nth_least B ` A) = nth_least B (least A)" using assms by simp lemma nth_least_nth_least[simp]: assumes "enat n < esize A" "\<And> i. i \<in> A \<Longrightarrow> enat i < esize B" shows "nth_least B (nth_least A n) = nth_least (nth_least B ` A) n" using assms proof (induct n arbitrary: A) case 0 show ?case using 0 by simp next case (Suc n) have 1: "A \<noteq> {}" using Suc(2) by auto have 2: "nth_least B ` (A - {least A}) = nth_least B ` A - nth_least B ` {least A}" proof (rule inj_on_image_set_diff) show "inj_on (nth_least B) {i. enat i < esize B}" using nth_least.inj_on by this show "A - {least A} \<subseteq> {i. enat i < esize B}" using Suc(3) by blast show "{least A} \<subseteq> {i. enat i < esize B}" using Suc(3) 1 by force qed have "nth_least B (nth_least A (Suc n)) = nth_least B (nth_least (A - {least A}) n)" by simp also have "\<dots> = nth_least (nth_least B ` (A - {least A})) n" using Suc 1 by force also have "\<dots> = nth_least (nth_least B ` A - nth_least B ` {least A}) n" unfolding 2 by rule also have "\<dots> = nth_least (nth_least B ` A - {nth_least B (least A)}) n" by simp also have "\<dots> = nth_least (nth_least B ` A - {least (nth_least B ` A)}) n" using Suc(3) 1 by auto also have "\<dots> = nth_least (nth_least B ` A) (Suc n)" by simp finally show ?case by this qed lemma nth_least_Max[simp]: assumes "finite A" "A \<noteq> {}" shows "nth_least A (card A - 1) = Max A" using assms proof (induct "card A - 1" arbitrary: A) case 0 have 1: "card A = 1" using 0 by (metis One_nat_def Suc_diff_1 card_gt_0_iff) obtain a where 2: "A = {a}" using 1 by rule show ?case unfolding 2 by (simp del: insert_iff) next case (Suc n) have 1: "least A \<in> A" using Suc(4) by rule have 2: "card (A - {least A}) = Suc n" using Suc(2, 3) 1 by simp have 3: "A - {least A} \<noteq> {}" using 2 Suc(3) by fastforce have "nth_least A (card A - 1) = nth_least A (Suc n)" unfolding Suc(2) by rule also have "\<dots> = nth_least (A - {least A}) n" by simp also have "\<dots> = nth_least (A - {least A}) (card (A - {least A}) - 1)" unfolding 2 by simp also have "\<dots> = Max (A - {least A})" proof (rule Suc(1)) show "n = card (A - {least A}) - 1" unfolding 2 by simp show "finite (A - {least A})" using Suc(3) by simp show "A - {least A} \<noteq> {}" using 3 by this qed also have "\<dots> = Max A" using Suc(3) 3 by simp finally show ?case by this qed lemma nth_least_le_Max: assumes "finite A" "A \<noteq> {}" "enat n < esize A" shows "nth_least A n \<le> Max A" proof - have "nth_least A n \<le> nth_least A (card A - 1)" proof (rule nth_least_mono) show "enat (card A - 1) < esize A" by (metis Suc_diff_1 Suc_ile_eq assms(1) assms(2) card_eq_0_iff esize_set.simps(1) not_gr0 order_refl) show "n \<le> card A - 1" by (metis Suc_diff_1 Suc_leI antisym_conv assms(1) assms(3) enat_ord_simps(2) esize_set.simps(1) le_less neq_iff not_gr0) qed also have "\<dots> = Max A" using nth_least_Max assms(1, 2) by this finally show ?thesis by this qed lemma nth_least_not_contains: fixes k :: nat assumes "enat (Suc n) < esize A" "nth_least A n < k" "k < nth_least A (Suc n)" shows "k \<notin> A" proof assume 1: "k \<in> A" have 2: "nth_least A (card {i \<in> A. i < k}) = k" using nth_least.right_inverse 1 by this have 3: "n < card {i \<in> A. i < k}" proof (rule nth_least_strict_mono_inverse) show "enat n < esize A" using assms(1) by auto show "enat (card {i \<in> A. i < k}) < esize A" using nth_least.g.wellformed 1 by simp show "nth_least A n < nth_least A (card {i \<in> A. i < k})" using assms(2) 2 by simp qed have 4: "card {i \<in> A. i < k} < Suc n" proof (rule nth_least_strict_mono_inverse) show "enat (card {i \<in> A. i < k}) < esize A" using nth_least.g.wellformed 1 by simp show "enat (Suc n) < esize A" using assms(1) by this show "nth_least A (card {i \<in> A. i < k}) < nth_least A (Suc n)" using assms(3) 2 by simp qed show "False" using 3 4 by auto qed lemma nth_least_Suc[simp]: assumes "enat n < esize A" shows "nth_least (Suc ` A) n = Suc (nth_least A n)" using assms proof (induct n arbitrary: A) case (0) have 1: "A \<noteq> {}" using 0 by auto show ?case using 1 by simp next case (Suc n) have 1: "enat n < esize (A - {least A})" proof - have 2: "A \<noteq> {}" using Suc(2) by auto have 3: "least A \<in> A" using LeastI 2 by fast have 4: "A = insert (least A) A" using 3 by auto have "eSuc (enat n) = enat (Suc n)" by simp also have "\<dots> < esize A" using Suc(2) by this also have "\<dots> = esize (insert (least A) A)" using 4 by simp also have "\<dots> = eSuc (esize (A - {least A}))" using 3 2 by simp finally show ?thesis using Extended_Nat.eSuc_mono by metis qed have "nth_least (Suc ` A) (Suc n) = nth_least (Suc ` A - {least (Suc ` A)}) n" by simp also have "\<dots> = nth_least (Suc ` (A - {least A})) n" by simp also have "\<dots> = Suc (nth_least (A - {least A}) n)" using Suc(1) 1 by this also have "\<dots> = Suc (nth_least A (Suc n))" by simp finally show ?case by this qed lemma nth_least_list_card[simp]: assumes "enat n \<le> esize A" shows "card {k \<in> A. k < nth_least (lift A) n} = n" using less_Suc_eq_le assms by (cases n, auto simp del: nth_least.simps) end
Formal statement is: lemma open_lessThan [continuous_intros, simp]: "open {..< a}" Informal statement is: The set $\{x \in \mathbb{R} \mid x < a\}$ is open.
function [ obj ] = bold_grad_cd( obj, n ) % Calculate jacobian matrix of predicted fMRI BOLD signal with respect to % DCM parameters using central difference method. % % This is a protected method of the tapas_Huge class. It cannot be called % from outside the class. % % Author: Yu Yao ([email protected]) % Copyright (C) 2019 Translational Neuromodeling Unit % Institute for Biomedical Engineering, % University of Zurich and ETH Zurich. % % This file is part of TAPAS, which is released under the terms of the GNU % General Public Licence (GPL), version 3. For further details, see % <https://www.gnu.org/licenses/>. % % This software is provided "as is", without warranty of any kind, express % or implied, including, but not limited to the warranties of % merchantability, fitness for a particular purpose and non-infringement. % % This software is intended for research only. Do not use for clinical % purpose. Please note that this toolbox is under active development. % Considerable changes may occur in future releases. For support please % refer to: % https://github.com/translationalneuromodeling/tapas/issues % % prediction error for current mean DCM parameter mu_n = obj.posterior.mu_n(n,:); data = obj.data(n); obj.aux.epsilon{n} = obj.bold_gen(mu_n, data, obj.inputs(n), ... obj.options.hemo, obj.R, obj.L, obj.idx ); % gradient obj.aux.G{n} = zeros(numel(obj.data(n).bold), obj.idx.P_c + obj.idx.P_h); data.bold(:) = 0; % central difference for p = 1:obj.idx.P_c + obj.idx.P_h for s = -1:2:1 % perturb parameters mu_n = obj.posterior.mu_n(n,:); mu_n(p) = mu_n(p) + s*obj.options.delta; % generate bold signal pred = obj.bold_gen(mu_n, data, obj.inputs(n), obj.options.hemo,... obj.R, obj.L, obj.idx ); % calculate difference obj.aux.G{n}(:,p) = obj.aux.G{n}(:,p) - s*pred(:); end end % normalize by step size obj.aux.G{n} = obj.aux.G{n}/obj.options.delta/2; end
MODULE mo_column_extra CONTAINS SUBROUTINE compute ( nz , q , t , nproma ) USE mo_column INTEGER , INTENT(IN) :: nproma INTEGER , INTENT(IN) :: nz REAL , INTENT(INOUT) :: t ( : , : ) REAL , INTENT(INOUT) :: q ( : , : ) CALL compute_column ( nz , q , t , nproma = nproma ) END SUBROUTINE compute END MODULE mo_column_extra
section\<open>Cohen forcing notions\<close> theory Cohen_Posets_Relative imports Forcing_Notions Transitive_Models.Delta_System_Relative Transitive_Models.Partial_Functions_Relative begin locale cohen_data = fixes \<kappa> I J::i assumes zero_lt_kappa: "0<\<kappa>" begin lemmas zero_lesspoll_kappa = zero_lesspoll[OF zero_lt_kappa] end \<comment> \<open>\<^locale>\<open>cohen_data\<close>\<close> abbreviation inj_dense :: "[i,i,i,i]\<Rightarrow>i" where "inj_dense(I,J,w,x) \<equiv> { p\<in>Fn(\<omega>,I\<times>\<omega>,J) . (\<exists>n\<in>\<omega>. \<langle>\<langle>w,n\<rangle>,1\<rangle> \<in> p \<and> \<langle>\<langle>x,n\<rangle>,0\<rangle> \<in> p) }" lemma dense_inj_dense: assumes "w \<in> I" "x \<in> I" "w \<noteq> x" "p\<in>Fn(\<omega>,I\<times>\<omega>,J)" "0\<in>J" "1\<in>J" shows "\<exists>d\<in>inj_dense(I,J,w,x). \<langle>d ,p\<rangle> \<in> Fnle(\<omega>,I\<times>\<omega>,J)" proof - obtain n where "\<langle>w,n\<rangle> \<notin> domain(p)" "\<langle>x,n\<rangle> \<notin> domain(p)" "n \<in> \<omega>" proof - { assume "\<langle>w,n\<rangle> \<in> domain(p) \<or> \<langle>x,n\<rangle> \<in> domain(p)" if "n \<in> \<omega>" for n then have "\<omega> \<subseteq> range(domain(p))" by blast then have "\<not> Finite(p)" using Finite_range Finite_domain subset_Finite nat_not_Finite by auto with \<open>p \<in> _\<close> have False using Fn_nat_eq_FiniteFun FiniteFun.dom_subset[of "I\<times>\<omega>" J] Fin_into_Finite by auto } with that\<comment> \<open>the shape of the goal puts assumptions in this variable\<close> show ?thesis by auto qed moreover note \<open>p \<in> _\<close> assms moreover from calculation have "cons(\<langle>\<langle>x,n\<rangle>,0\<rangle>, p) \<in> Fn(\<omega>,I\<times>\<omega>,J)" using FiniteFun.consI[of "\<langle>x,n\<rangle>" "I\<times>\<omega>" 0 J p] Fn_nat_eq_FiniteFun by auto ultimately have "cons(\<langle>\<langle>w,n\<rangle>,1\<rangle>, cons(\<langle>\<langle>x,n\<rangle>,0\<rangle>, p) ) \<in> Fn(\<omega>,I\<times>\<omega>,J)" using FiniteFun.consI[of "\<langle>w,n\<rangle>" "I \<times> \<omega>" 1 J "cons(\<langle>\<langle>x,n\<rangle>,0\<rangle>, p)"] Fn_nat_eq_FiniteFun by auto with \<open>n \<in> \<omega>\<close> show ?thesis using \<open>p \<in> _\<close> by (intro bexI) auto qed locale add_reals = cohen_data nat _ 2 subsection\<open>Combinatorial results on Cohen posets\<close> sublocale cohen_data \<subseteq> forcing_notion "Fn(\<kappa>,I,J)" "Fnle(\<kappa>,I,J)" 0 proof show "0 \<in> Fn(\<kappa>, I, J)" using zero_lt_kappa zero_in_Fn by simp then show "\<forall>p\<in>Fn(\<kappa>, I, J). \<langle>p, 0\<rangle> \<in> Fnle(\<kappa>, I, J)" unfolding preorder_on_def refl_def trans_on_def by auto next show "preorder_on(Fn(\<kappa>, I, J), Fnle(\<kappa>, I, J))" unfolding preorder_on_def refl_def trans_on_def by blast qed context cohen_data begin lemma compat_imp_Un_is_function: assumes "G \<subseteq> Fn(\<kappa>, I, J)" "\<And>p q. p \<in> G \<Longrightarrow> q \<in> G \<Longrightarrow> compat(p,q)" shows "function(\<Union>G)" unfolding function_def proof (intro allI ballI impI) fix x y y' assume "\<langle>x, y\<rangle> \<in> \<Union>G" "\<langle>x, y'\<rangle> \<in> \<Union>G" then obtain p q where "\<langle>x, y\<rangle> \<in> p" "\<langle>x, y'\<rangle> \<in> q" "p \<in> G" "q \<in> G" by auto moreover from this and assms obtain r where "r \<in> Fn(\<kappa>, I, J)" "r \<supseteq> p" "r \<supseteq> q" using compatD[of p q] by force moreover from this have "function(r)" using Fn_is_function by simp ultimately show "y = y'" unfolding function_def by fastforce qed lemma Un_filter_is_function: "filter(G) \<Longrightarrow> function(\<Union>G)" using compat_imp_Un_is_function filter_imp_compat[of G] filter_subset_notion by simp end \<comment> \<open>\<^locale>\<open>cohen_data\<close>\<close> locale M_cohen = M_delta + assumes countable_lepoll_assms2: "M(A') \<Longrightarrow> M(A) \<Longrightarrow> M(b) \<Longrightarrow> M(f) \<Longrightarrow> separation(M, \<lambda>y. \<exists>x\<in>A'. y = \<langle>x, \<mu> i. x \<in> if_range_F_else_F(\<lambda>a. {p \<in> A . domain(p) = a}, b, f, i)\<rangle>)" and countable_lepoll_assms3: "M(A) \<Longrightarrow> M(f) \<Longrightarrow> M(b) \<Longrightarrow> M(D) \<Longrightarrow> M(r') \<Longrightarrow> M(A')\<Longrightarrow> separation(M, \<lambda>y. \<exists>x\<in>A'. y = \<langle>x, \<mu> i. x \<in> if_range_F_else_F(drSR_Y(r', D, A), b, f, i)\<rangle>)" lemma (in M_library) Fnle_rel_Aleph_rel1_closed[intro,simp]: "M(Fnle\<^bsup>M\<^esup>(\<aleph>\<^bsub>1\<^esub>\<^bsup>M\<^esup>, \<aleph>\<^bsub>1\<^esub>\<^bsup>M\<^esup>, \<omega> \<rightarrow>\<^bsup>M\<^esup> 2))" by simp locale M_add_reals = M_cohen + add_reals begin lemmas zero_lesspoll_rel_kappa = zero_lesspoll_rel[OF zero_lt_kappa] end \<comment> \<open>\<^locale>\<open>M_add_reals\<close>\<close> (* MOVE THIS to some appropriate place. Notice that in Forcing_Notions we don't import anything relative. *) relativize relational "compat_in" "is_compat_in" external synthesize "compat_in" from_definition "is_compat_in" assuming "nonempty" context notes Un_assoc[simp] Un_trasposition_aux2[simp] begin arity_theorem for "compat_in_fm" end lemma (in M_trivial) compat_in_abs[absolut]: assumes "M(A)" "M(r)" "M(p)" "M(q)" shows "is_compat_in(M,A,r,p,q) \<longleftrightarrow> compat_in(A,r,p,q)" using assms unfolding is_compat_in_def compat_in_def by simp definition antichain :: "i\<Rightarrow>i\<Rightarrow>i\<Rightarrow>o" where "antichain(P,leq,A) \<equiv> A\<subseteq>P \<and> (\<forall>p\<in>A. \<forall>q\<in>A. p\<noteq>q \<longrightarrow> \<not>compat_in(P,leq,p,q))" relativize relational "antichain" "antichain_rel" definition ccc :: "i \<Rightarrow> i \<Rightarrow> o" where "ccc(P,leq) \<equiv> \<forall>A. antichain(P,leq,A) \<longrightarrow> |A| \<le> nat" abbreviation antichain_rel_abbr :: "[i\<Rightarrow>o,i,i,i] \<Rightarrow> o" (\<open>antichain\<^bsup>_\<^esup>'(_,_,_')\<close>) where "antichain\<^bsup>M\<^esup>(P,leq,A) \<equiv> antichain_rel(M,P,leq,A)" abbreviation antichain_r_set :: "[i,i,i,i] \<Rightarrow> o" (\<open>antichain\<^bsup>_\<^esup>'(_,_,_')\<close>) where "antichain\<^bsup>M\<^esup>(P,leq,A) \<equiv> antichain_rel(##M,P,leq,A)" context M_trivial begin lemma antichain_abs [absolut]: "\<lbrakk> M(A); M(P); M(leq) \<rbrakk> \<Longrightarrow> antichain\<^bsup>M\<^esup>(P,leq,A) \<longleftrightarrow> antichain(P,leq,A)" unfolding antichain_rel_def antichain_def by (simp add:absolut) end \<comment> \<open>\<^locale>\<open>M_trivial\<close>\<close> relativize relational "ccc" "ccc_rel" abbreviation ccc_rel_abbr :: "[i\<Rightarrow>o,i,i]\<Rightarrow>o" (\<open>ccc\<^bsup>_\<^esup>'(_,_')\<close>) where "ccc_rel_abbr(M) \<equiv> ccc_rel(M)" abbreviation ccc_r_set :: "[i,i,i]\<Rightarrow>o" (\<open>ccc\<^bsup>_\<^esup>'(_,_')\<close>) where "ccc_r_set(M) \<equiv> ccc_rel(##M)" context M_cardinals begin lemma def_ccc_rel: shows "ccc\<^bsup>M\<^esup>(P,leq) \<longleftrightarrow> (\<forall>A[M]. antichain\<^bsup>M\<^esup>(P,leq,A) \<longrightarrow> |A|\<^bsup>M\<^esup> \<le> \<omega>)" using is_cardinal_iff unfolding ccc_rel_def by (simp add:absolut) end \<comment> \<open>\<^locale>\<open>M_cardinals\<close>\<close> context M_FiniteFun begin lemma Fnle_nat_closed[intro,simp]: assumes "M(I)" "M(J)" shows "M(Fnle(\<omega>,I,J))" unfolding Fnle_def Fnlerel_def Rrel_def using supset_separation FiniteFun_closed Fn_nat_eq_FiniteFun assms by simp lemma Fn_nat_closed: assumes "M(A)" "M(B)" shows "M(Fn(\<omega>,A,B))" using assms Fn_nat_eq_FiniteFun by simp end \<comment> \<open>\<^locale>\<open>M_FiniteFun\<close>\<close> context M_add_reals begin lemma lam_replacement_drSR_Y: "M(A) \<Longrightarrow> M(D) \<Longrightarrow> M(r') \<Longrightarrow> lam_replacement(M, drSR_Y(r',D,A))" using lam_replacement_drSR_Y by simp lemma (in M_trans) mem_F_bound3: fixes F A defines "F \<equiv> dC_F" shows "x\<in>F(A,c) \<Longrightarrow> c \<in> (range(f) \<union> {domain(x). x\<in>A})" using apply_0 unfolding F_def by (cases "M(c)", auto simp:F_def drSR_Y_def dC_F_def) lemma ccc_rel_Fn_nat: assumes "M(I)" shows "ccc\<^bsup>M\<^esup>(Fn(nat,I,2), Fnle(nat,I,2))" proof - have repFun_dom_closed:"M({domain(p) . p \<in> A})" if "M(A)" for A using RepFun_closed domain_replacement_simp transM[OF _ \<open>M(A)\<close>] that by auto from assms have "M(Fn(nat,I,2))" using Fn_nat_eq_FiniteFun by simp { fix A assume "\<not> |A|\<^bsup>M\<^esup> \<le> nat" "M(A)" "A \<subseteq> Fn(nat, I, 2)" moreover from this have "countable_rel(M,{p\<in>A. domain(p) = d})" if "M(d)" for d proof (cases "d\<prec>\<^bsup>M\<^esup>nat \<and> d \<subseteq> I") case True with \<open>A \<subseteq> Fn(nat, I, 2)\<close> \<open>M(A)\<close> have "{p \<in> A . domain(p) = d} \<subseteq> d \<rightarrow>\<^bsup>M\<^esup> 2" using domain_of_fun function_space_rel_char[of _ 2] by (auto,subgoal_tac "M(domain(x))",simp_all add:transM[of _ A],force) moreover from True \<open>M(d)\<close> have "Finite(d \<rightarrow>\<^bsup>M\<^esup> 2)" using Finite_Pi[THEN [2] subset_Finite, of _ d "\<lambda>_. 2"] lesspoll_rel_nat_is_Finite_rel function_space_rel_char[of _ 2] by auto moreover from \<open>M(d)\<close> have "M(d \<rightarrow>\<^bsup>M\<^esup> 2)" by simp moreover from \<open>M(A)\<close> have "M({p \<in> A . domain(p) = d})" using separation_closed domain_eq_separation[OF \<open>M(d)\<close>] by simp ultimately show ?thesis using subset_Finite[of _ "d\<rightarrow>\<^bsup>M\<^esup>2" ] by (auto intro!:Finite_imp_countable_rel) next case False with \<open>A \<subseteq> Fn(nat, I, 2)\<close> \<open>M(A)\<close> have "domain(p) \<noteq> d" if "p\<in>A" for p proof - note False that \<open>M(A)\<close> moreover from this obtain d' where "d' \<subseteq> I" "p\<in>d' \<rightarrow> 2" "d' \<prec> \<omega>" using FnD[OF subsetD[OF \<open>A\<subseteq>_\<close> \<open>p\<in>A\<close>]] by auto moreover from this have "p \<approx> d'" "domain(p) = d'" using function_eqpoll Pi_iff by auto ultimately show ?thesis using lesspoll_nat_imp_lesspoll_rel transM[of p] by auto qed then show ?thesis using empty_lepoll_relI by auto qed have 2:"M(x) \<Longrightarrow> x \<in> dC_F(X, i) \<Longrightarrow> M(i)" for x X i unfolding dC_F_def by auto moreover have "uncountable_rel(M,{domain(p) . p \<in> A})" proof interpret M_replacement_lepoll M dC_F using lam_replacement_dC_F domain_eq_separation lam_replacement_inj_rel lam_replacement_minimum unfolding dC_F_def proof(unfold_locales,simp_all) fix X b f assume "M(X)" "M(b)" "M(f)" with 2[of X] show "lam_replacement(M, \<lambda>x. \<mu> i. x \<in> if_range_F_else_F(\<lambda>d. {p \<in> X . domain(p) = d}, b, f, i))" using lam_replacement_dC_F domain_eq_separation mem_F_bound3 countable_lepoll_assms2 repFun_dom_closed by (rule_tac lam_Least_assumption_general[where U="\<lambda>_. {domain(x). x\<in>X}"],auto) qed (auto) have "\<exists>a\<in>A. x = domain(a) \<Longrightarrow> M(dC_F(A,x))" for x using \<open>M(A)\<close> transM[OF _ \<open>M(A)\<close>] by (auto) moreover have "w \<in> A \<and> domain(w) = x \<Longrightarrow> M(x)" for w x using transM[OF _ \<open>M(A)\<close>] by auto ultimately interpret M_cardinal_UN_lepoll _ "dC_F(A)" "{domain(p). p\<in>A}" using lam_replacement_dC_F lam_replacement_inj_rel \<open>M(A)\<close> lepoll_assumptions domain_eq_separation lam_replacement_minimum countable_lepoll_assms2 repFun_dom_closed lepoll_assumptions1[OF \<open>M(A)\<close> repFun_dom_closed[OF \<open>M(A)\<close>]] apply(unfold_locales) by(simp_all del:if_range_F_else_F_def, rule_tac lam_Least_assumption_general[where U="\<lambda>_. {domain(x). x\<in>A}"]) (auto simp del:if_range_F_else_F_def simp add:dC_F_def) from \<open>A \<subseteq> Fn(nat, I, 2)\<close> have x:"(\<Union>d\<in>{domain(p) . p \<in> A}. {p\<in>A. domain(p) = d}) = A" by auto moreover assume "countable_rel(M,{domain(p) . p \<in> A})" moreover note \<open>\<And>d. M(d) \<Longrightarrow> countable_rel(M,{p\<in>A. domain(p) = d})\<close> moreover from \<open>M(A)\<close> have "p\<in>A \<Longrightarrow> M(domain(p))" for p by (auto dest: transM) ultimately have "countable_rel(M,A)" using countable_rel_imp_countable_rel_UN unfolding dC_F_def by auto with \<open>\<not> |A|\<^bsup>M\<^esup> \<le> nat\<close> \<open>M(A)\<close> show False using countable_rel_iff_cardinal_rel_le_nat by simp qed moreover from \<open>A \<subseteq> Fn(nat, I, 2)\<close> \<open>M(A)\<close> have "p \<in> A \<Longrightarrow> Finite(domain(p))" for p using lesspoll_rel_nat_is_Finite_rel[of "domain(p)"] lesspoll_nat_imp_lesspoll_rel[of "domain(p)"] domain_of_fun[of p _ "\<lambda>_. 2"] by (auto dest:transM) moreover note repFun_dom_closed[OF \<open>M(A)\<close>] ultimately obtain D where "delta_system(D)" "D \<subseteq> {domain(p) . p \<in> A}" "D \<approx>\<^bsup>M\<^esup> \<aleph>\<^bsub>1\<^esub>\<^bsup>M\<^esup>" "M(D)" using delta_system_uncountable_rel[of "{domain(p) . p \<in> A}"] by auto then have delta:"\<forall>d1\<in>D. \<forall>d2\<in>D. d1 \<noteq> d2 \<longrightarrow> d1 \<inter> d2 = \<Inter>D" using delta_system_root_eq_Inter by simp moreover from \<open>D \<approx>\<^bsup>M\<^esup> \<aleph>\<^bsub>1\<^esub>\<^bsup>M\<^esup>\<close> \<open>M(D)\<close> have "uncountable_rel(M,D)" using uncountable_rel_iff_subset_eqpoll_rel_Aleph_rel1 by auto moreover from this and \<open>D \<subseteq> {domain(p) . p \<in> A}\<close> obtain p1 where "p1 \<in> A" "domain(p1) \<in> D" using uncountable_rel_not_empty[of D] by blast moreover from this and \<open>p1 \<in> A \<Longrightarrow> Finite(domain(p1))\<close> have "Finite(domain(p1))" using Finite_domain by simp moreover define r where "r \<equiv> \<Inter>D" moreover from \<open>M(D)\<close> have "M(r)" "M(r\<times>2)" unfolding r_def by simp_all ultimately have "Finite(r)" using subset_Finite[of "r" "domain(p1)"] by auto have "countable_rel(M,{restrict(p,r) . p\<in>A})" proof - note \<open>M(Fn(nat, I, 2))\<close> \<open>M(r)\<close> moreover from this have "f\<in>Fn(nat, I, 2) \<Longrightarrow> M(restrict(f,r))" for f by (blast dest: transM) ultimately have "f\<in>Fn(nat, I, 2) \<Longrightarrow> restrict(f,r) \<in> Pow_rel(M,r \<times> 2)" for f using restrict_subset_Sigma[of f _ "\<lambda>_. 2" r] Pow_rel_char by (auto del:FnD dest!:FnD simp: Pi_def) (auto dest:transM) with \<open>A \<subseteq> Fn(nat, I, 2)\<close> have "{restrict(f,r) . f \<in> A } \<subseteq> Pow_rel(M,r \<times> 2)" by fast moreover from \<open>M(A)\<close> \<open>M(r)\<close> have "M({restrict(f,r) . f \<in> A })" using RepFun_closed restrict_strong_replacement transM[OF _ \<open>M(A)\<close>] by auto moreover note \<open>Finite(r)\<close> \<open>M(r)\<close> ultimately show ?thesis using Finite_Sigma[THEN Finite_Pow_rel, of r "\<lambda>_. 2"] by (intro Finite_imp_countable_rel) (auto intro:subset_Finite) qed moreover from \<open>M(A)\<close> \<open>M(D)\<close> have "M({p\<in>A. domain(p) \<in> D})" using domain_mem_separation by simp have "uncountable_rel(M,{p\<in>A. domain(p) \<in> D})" (is "uncountable_rel(M,?X)") proof from \<open>D \<subseteq> {domain(p) . p \<in> A}\<close> have "(\<lambda>p\<in>?X. domain(p)) \<in> surj(?X, D)" using lam_type unfolding surj_def by auto moreover from \<open>M(A)\<close> \<open>M(?X)\<close> have "M(\<lambda>p\<in>?X. domain(p))" using lam_closed[OF domain_replacement \<open>M(?X)\<close>] transM[OF _ \<open>M(?X)\<close>] by simp moreover note \<open>M(?X)\<close> \<open>M(D)\<close> moreover from calculation have surjection:"(\<lambda>p\<in>?X. domain(p)) \<in> surj\<^bsup>M\<^esup>(?X, D)" using surj_rel_char by simp moreover assume "countable_rel(M,?X)" moreover note \<open>uncountable_rel(M,D)\<close> ultimately show False using surj_rel_countable_rel[OF _ surjection] by auto qed moreover have "D = (\<Union>f\<in>Pow_rel(M,r\<times>2) . {y . p\<in>A, restrict(p,r) = f \<and> y=domain(p) \<and> domain(p) \<in> D})" proof - { fix z assume "z \<in> D" with \<open>M(D)\<close> have \<open>M(z)\<close> by (auto dest:transM) from \<open>z\<in>D\<close> \<open>D \<subseteq> _\<close> \<open>M(A)\<close> obtain p where "domain(p) = z" "p \<in> A" "M(p)" by (auto dest:transM) moreover from \<open>A \<subseteq> Fn(nat, I, 2)\<close> \<open>M(z)\<close> and this have "p \<in> z \<rightarrow>\<^bsup>M\<^esup> 2" using domain_of_fun function_space_rel_char by (auto del:FnD dest!:FnD) moreover from this \<open>M(z)\<close> have "p \<in> z \<rightarrow> 2" using domain_of_fun function_space_rel_char by (auto) moreover from this \<open>M(r)\<close> have "restrict(p,r) \<subseteq> r \<times> 2" using function_restrictI[of p r] fun_is_function[of p z "\<lambda>_. 2"] restrict_subset_Sigma[of p z "\<lambda>_. 2" r] function_space_rel_char by (auto simp:Pi_def) moreover from \<open>M(p)\<close> \<open>M(r)\<close> have "M(restrict(p,r))" by simp moreover note \<open>M(r)\<close> ultimately have "\<exists>p\<in>A. restrict(p,r) \<in> Pow_rel(M,r\<times>2) \<and> domain(p) = z" using Pow_rel_char by auto } then show ?thesis by (intro equalityI) (force)+ qed from \<open>M(D)\<close>\<open>M(r)\<close> have "M({y . p\<in>A, restrict(p,r) = f \<and> y=domain(p) \<and> domain(p) \<in> D})" (is "M(?Y(A,f))") if "M(f)" "M(A)" for f A using drSR_Y_closed[unfolded drSR_Y_def] that by simp then obtain f where "uncountable_rel(M,?Y(A,f))" "M(f)" proof - have 1:"M(i)" if "M(B)" "M(x)" "x \<in> {y . x \<in> B, restrict(x, r) = i \<and> y = domain(x) \<and> domain(x) \<in> D}" for B x i using that \<open>M(r)\<close> by (auto dest:transM) note \<open>M(r)\<close> moreover from this have "M(Pow\<^bsup>M\<^esup>(r \<times> 2))" by simp moreover note \<open>M(A)\<close> \<open>\<And>f A. M(f) \<Longrightarrow> M(A) \<Longrightarrow> M(?Y(A,f))\<close> \<open>M(D)\<close> moreover from calculation interpret M_replacement_lepoll M "drSR_Y(r,D)" using countable_lepoll_assms3 lam_replacement_inj_rel lam_replacement_drSR_Y drSR_Y_closed lam_Least_assumption_drSR_Y lam_replacement_minimum by (unfold_locales,simp_all add:drSR_Y_def,rule_tac 1,simp_all) from calculation have "x \<in> Pow\<^bsup>M\<^esup>(r \<times> 2) \<Longrightarrow> M(drSR_Y(r, D, A, x))" for x unfolding drSR_Y_def by (auto dest:transM) ultimately interpret M_cardinal_UN_lepoll _ "?Y(A)" "Pow_rel(M,r\<times>2)" using countable_lepoll_assms3 lam_replacement_drSR_Y lepoll_assumptions[where S="Pow_rel(M,r\<times>2)", unfolded lepoll_assumptions_defs] lam_Least_assumption_drSR_Y[unfolded drSR_Y_def] lam_replacement_minimum unfolding drSR_Y_def apply unfold_locales apply (simp_all add:lam_replacement_inj_rel del: if_range_F_else_F_def,rule_tac 1,simp_all) by ((fastforce dest:transM[OF _ \<open>M(A)\<close>])+) { from \<open>Finite(r)\<close> \<open>M(r)\<close> have "countable_rel(M,Pow_rel(M,r\<times>2))" using Finite_Sigma[THEN Finite_Pow_rel] Finite_imp_countable_rel by simp moreover assume "M(f) \<Longrightarrow> countable_rel(M,?Y(A,f))" for f moreover note \<open>D = (\<Union>f\<in>Pow_rel(M,r\<times>2) .?Y(A,f))\<close> \<open>M(r)\<close> moreover note \<open>uncountable_rel(M,D)\<close> ultimately have "False" using countable_rel_imp_countable_rel_UN by (auto dest: transM) } with that show ?thesis by auto qed moreover from this \<open>M(A)\<close> and \<open>M(f) \<Longrightarrow> M(A) \<Longrightarrow> M(?Y(A,f))\<close> have "M(?Y(A,f))" by blast ultimately obtain j where "j \<in> inj_rel(M,nat, ?Y(A,f))" "M(j)" using uncountable_rel_iff_nat_lt_cardinal_rel[THEN iffD1, THEN leI, THEN cardinal_rel_le_imp_lepoll_rel, THEN lepoll_relD] by auto with \<open>M(?Y(A,f))\<close> have "j`0 \<noteq> j`1" "j`0 \<in> ?Y(A,f)" "j`1 \<in> ?Y(A,f)" using inj_is_fun[THEN apply_type, of j nat "?Y(A,f)"] inj_rel_char unfolding inj_def by auto then obtain p q where "domain(p) \<noteq> domain(q)" "p \<in> A" "q \<in> A" "domain(p) \<in> D" "domain(q) \<in> D" "restrict(p,r) = restrict(q,r)" by auto moreover from this and delta have "domain(p) \<inter> domain(q) = r" unfolding r_def by simp moreover note \<open>A \<subseteq> Fn(nat, I, 2)\<close> Fn_nat_abs[OF \<open>M(I)\<close> nat_into_M[of 2],simplified] moreover from calculation have "p \<in> Fn\<^bsup>M\<^esup>(nat, I, 2)" "q \<in> Fn\<^bsup>M\<^esup>(nat, I, 2)" by auto moreover from calculation have "p \<union> q \<in> Fn(nat, I, 2)" using restrict_eq_imp_compat_rel InfCard_rel_nat by simp ultimately have "\<exists>p\<in>A. \<exists>q\<in>A. p \<noteq> q \<and> compat_in(Fn(nat, I, 2), Fnle(nat, I, 2), p, q)" unfolding compat_in_def by (rule_tac bexI[of _ p], rule_tac bexI[of _ q]) blast } moreover from assms have "M(Fnle(\<omega>,I,2))" by simp moreover note \<open>M(Fn(\<omega>,I,2))\<close> ultimately show ?thesis using def_ccc_rel by (auto simp:absolut antichain_def) fastforce qed end \<comment> \<open>\<^locale>\<open>M_add_reals\<close>\<close> end
As the British force under Captain Sir Richard Strachan approached the convoy , the escorting frigate RΓ©solue opened fire . A general action followed , with Strachan succeeding in forcing the French ship to surrender within twenty minutes and both sides suffering damage and casualties . All of the French vessels were searched and subsequently returned to MahΓ© , the local French authorities reacting furiously at what they perceived as a violation of their neutral position . Messages were sent back to France reporting the action from Commodore Saint @-@ FΓ©lix but they evoked little response . Although under normal circumstances the battle might have provoked a diplomatic incident , the upheavals of the ongoing French Revolution meant that the despatches had little effect .
! example.f90 -- ! Example of using "exceptions" in combination with the ! preproc preprocessor ! module example_routines use exceptions use exception_handling implicit none contains subroutine mysub( x, y ) real(dp) :: x, y if ( & x > 0.0 .and. & y > 0.0 ) then write(*,*) "ERROR: condition failed at line 14 in file example.f90" write(*,*) " x > 0.0 .and. &" write(*,*) " y > 0.0" stop endif write(*,*) 'In mysub (example_routines)' if ( x <= 0.0 ) then write(*,*) 'At 16' call exception_throw( 'example.f90', 17,& 'Parameter x must be positive' ) return 'Parameter x must be positive' ) write(*,*) 'At 17' endif write(*,*) 'At 18' if ( x > 10.0 ) then write(*,*) 'Leaving mysub' return endif if ( x > 10.0 ) then write(*,*) 'At 19' write(*,*) 'At 20' y = log10(x) write(*,*) 'At 21' write(*,*) 'Leaving mysub' end subroutine mysub end module ! ! The main program ! program example use exceptions use example_routines implicit none real(dp) :: & x, y integer :: i ! TRY exception_catch = exception_catch + 1 if ( exception_thrown ) then call exception_setpos( 'example.f90', 36) goto 1111 endif try if ( exception_thrown ) then call exception_setpos( 'example.f90', 37) goto 1111 endif write(*,*) 'In example (example)' do i = 1,5 write(*,*) 'At 37' if ( exception_thrown ) then call exception_setpos( 'example.f90', 38) goto 1111 endif x = 1.5 - 0.5*i write(*,*) 'At 38' if ( exception_thrown ) then call exception_setpos( 'example.f90', 39) goto 1111 endif call mysub( x, y ) write(*,*) 'At 39' if ( exception_thrown ) then call exception_setpos( 'example.f90', 40) goto 1111 endif write(*,*) i, x, y write(*,*) 'At 40' if ( exception_thrown ) then call exception_setpos( 'example.f90', 41) goto 1111 endif enddo write(*,*) 'At 41' ! CATCH goto 1112 1111 continue exception_thrown = .false. catch write(*,*) 'At 42' ! An exception is caught, report it! write(*,*) 'At 43' call & exception_report( 0 ) write(*,*) 'At 45' ! ENDTRY 1112 continue exception_catch = exception_catch - 1 endtry write(*,*) 'At 46' write(*,*) 'At 47' x = -1.0 write(*,*) 'At 48' call mysub( x, y ) write(*,*) 'At 49' write(*,*) 'Finally: ', x, y write(*,*) 'At 50' write(*,*) 'At 51' write(*,*) 'Leaving example' stop write(*,*) 'At 52' end program
import Smt theorem neg (x : Int) : - -x = x := by smt <;> sorry
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import algebra.group_power.basic import algebra.group_with_zero.divisibility import data.nat.order.lemmas /-! # Definitions and properties of `nat.gcd`, `nat.lcm`, and `nat.coprime` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Generalizations of these are provided in a later file as `gcd_monoid.gcd` and `gcd_monoid.lcm`. Note that the global `is_coprime` is not a straightforward generalization of `nat.coprime`, see `nat.is_coprime_iff_coprime` for the connection between the two. -/ namespace nat /-! ### `gcd` -/ theorem gcd_dvd (m n : β„•) : (gcd m n ∣ m) ∧ (gcd m n ∣ n) := gcd.induction m n (Ξ»n, by rw gcd_zero_left; exact ⟨dvd_zero n, dvd_refl n⟩) (Ξ»m n npos, by rw ←gcd_rec; exact Ξ» ⟨IH₁, IHβ‚‚βŸ©, ⟨IHβ‚‚, (dvd_mod_iff IHβ‚‚).1 IHβ‚βŸ©) theorem gcd_dvd_left (m n : β„•) : gcd m n ∣ m := (gcd_dvd m n).left theorem gcd_dvd_right (m n : β„•) : gcd m n ∣ n := (gcd_dvd m n).right theorem gcd_le_left {m} (n) (h : 0 < m) : gcd m n ≀ m := le_of_dvd h $ gcd_dvd_left m n theorem gcd_le_right (m) {n} (h : 0 < n) : gcd m n ≀ n := le_of_dvd h $ gcd_dvd_right m n theorem dvd_gcd {m n k : β„•} : k ∣ m β†’ k ∣ n β†’ k ∣ gcd m n := gcd.induction m n (Ξ»n _ kn, by rw gcd_zero_left; exact kn) (Ξ»n m mpos IH H1 H2, by rw gcd_rec; exact IH ((dvd_mod_iff H1).2 H2) H1) theorem dvd_gcd_iff {m n k : β„•} : k ∣ gcd m n ↔ k ∣ m ∧ k ∣ n := iff.intro (Ξ» h, ⟨h.trans (gcd_dvd m n).left, h.trans (gcd_dvd m n).right⟩) (Ξ» h, dvd_gcd h.left h.right) theorem gcd_comm (m n : β„•) : gcd m n = gcd n m := dvd_antisymm (dvd_gcd (gcd_dvd_right m n) (gcd_dvd_left m n)) (dvd_gcd (gcd_dvd_right n m) (gcd_dvd_left n m)) theorem gcd_eq_left_iff_dvd {m n : β„•} : m ∣ n ↔ gcd m n = m := ⟨λ h, by rw [gcd_rec, mod_eq_zero_of_dvd h, gcd_zero_left], Ξ» h, h β–Έ gcd_dvd_right m n⟩ theorem gcd_eq_right_iff_dvd {m n : β„•} : m ∣ n ↔ gcd n m = m := by rw gcd_comm; apply gcd_eq_left_iff_dvd theorem gcd_assoc (m n k : β„•) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) @[simp] theorem gcd_one_right (n : β„•) : gcd n 1 = 1 := eq.trans (gcd_comm n 1) $ gcd_one_left n theorem gcd_mul_left (m n k : β„•) : gcd (m * n) (m * k) = m * gcd n k := gcd.induction n k (Ξ»k, by repeat {rw mul_zero <|> rw gcd_zero_left}) (Ξ»k n H IH, by rwa [←mul_mod_mul_left, ←gcd_rec, ←gcd_rec] at IH) theorem gcd_mul_right (m n k : β„•) : gcd (m * n) (k * n) = gcd m k * n := by rw [mul_comm m n, mul_comm k n, mul_comm (gcd m k) n, gcd_mul_left] theorem gcd_pos_of_pos_left {m : β„•} (n : β„•) (mpos : 0 < m) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_left m n) mpos theorem gcd_pos_of_pos_right (m : β„•) {n : β„•} (npos : 0 < n) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_right m n) npos theorem eq_zero_of_gcd_eq_zero_left {m n : β„•} (H : gcd m n = 0) : m = 0 := or.elim (nat.eq_zero_or_pos m) id (assume H1 : 0 < m, absurd (eq.symm H) (ne_of_lt (gcd_pos_of_pos_left _ H1))) theorem eq_zero_of_gcd_eq_zero_right {m n : β„•} (H : gcd m n = 0) : n = 0 := by rw gcd_comm at H; exact eq_zero_of_gcd_eq_zero_left H @[simp] theorem gcd_eq_zero_iff {i j : β„•} : gcd i j = 0 ↔ i = 0 ∧ j = 0 := begin split, { intro h, exact ⟨eq_zero_of_gcd_eq_zero_left h, eq_zero_of_gcd_eq_zero_right h⟩, }, { rintro ⟨rfl, rfl⟩, exact nat.gcd_zero_right 0 } end theorem gcd_div {m n k : β„•} (H1 : k ∣ m) (H2 : k ∣ n) : gcd (m / k) (n / k) = gcd m n / k := (decidable.eq_or_ne k 0).elim (Ξ»k0, by rw [k0, nat.div_zero, nat.div_zero, nat.div_zero, gcd_zero_right]) (Ξ»H3, mul_right_cancelβ‚€ H3 $ by rw [ nat.div_mul_cancel (dvd_gcd H1 H2), ←gcd_mul_right, nat.div_mul_cancel H1, nat.div_mul_cancel H2]) theorem gcd_greatest {a b d : β„•} (hda : d ∣ a) (hdb : d ∣ b) (hd : βˆ€ e : β„•, e ∣ a β†’ e ∣ b β†’ e ∣ d) : d = a.gcd b := (dvd_antisymm (hd _ (gcd_dvd_left a b) (gcd_dvd_right a b)) (dvd_gcd hda hdb)).symm theorem gcd_dvd_gcd_of_dvd_left {m k : β„•} (n : β„•) (H : m ∣ k) : gcd m n ∣ gcd k n := dvd_gcd ((gcd_dvd_left m n).trans H) (gcd_dvd_right m n) theorem gcd_dvd_gcd_of_dvd_right {m k : β„•} (n : β„•) (H : m ∣ k) : gcd n m ∣ gcd n k := dvd_gcd (gcd_dvd_left n m) ((gcd_dvd_right n m).trans H) theorem gcd_dvd_gcd_mul_left (m n k : β„•) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right (m n k : β„•) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _) theorem gcd_dvd_gcd_mul_left_right (m n k : β„•) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (m n k : β„•) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _) theorem gcd_eq_left {m n : β„•} (H : m ∣ n) : gcd m n = m := dvd_antisymm (gcd_dvd_left _ _) (dvd_gcd dvd_rfl H) theorem gcd_eq_right {m n : β„•} (H : n ∣ m) : gcd m n = n := by rw [gcd_comm, gcd_eq_left H] -- Lemmas where one argument is a multiple of the other @[simp] lemma gcd_mul_left_left (m n : β„•) : gcd (m * n) n = n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (dvd_mul_left _ _) dvd_rfl) @[simp] lemma gcd_mul_left_right (m n : β„•) : gcd n (m * n) = n := by rw [gcd_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_left (m n : β„•) : gcd (n * m) n = n := by rw [mul_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_right (m n : β„•) : gcd n (n * m) = n := by rw [gcd_comm, gcd_mul_right_left] -- Lemmas for repeated application of `gcd` @[simp] lemma gcd_gcd_self_right_left (m n : β„•) : gcd m (gcd m n) = gcd m n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (gcd_dvd_left _ _) dvd_rfl) @[simp] lemma gcd_gcd_self_right_right (m n : β„•) : gcd m (gcd n m) = gcd n m := by rw [gcd_comm n m, gcd_gcd_self_right_left] @[simp] lemma gcd_gcd_self_left_right (m n : β„•) : gcd (gcd n m) m = gcd n m := by rw [gcd_comm, gcd_gcd_self_right_right] @[simp] lemma gcd_gcd_self_left_left (m n : β„•) : gcd (gcd m n) m = gcd m n := by rw [gcd_comm m n, gcd_gcd_self_left_right] -- Lemmas where one argument consists of addition of a multiple of the other @[simp] lemma gcd_add_mul_right_right (m n k : β„•) : gcd m (n + k * m) = gcd m n := by simp [gcd_rec m (n + k * m), gcd_rec m n] @[simp] lemma gcd_add_mul_left_right (m n k : β„•) : gcd m (n + m * k) = gcd m n := by simp [gcd_rec m (n + m * k), gcd_rec m n] @[simp] lemma gcd_mul_right_add_right (m n k : β„•) : gcd m (k * m + n) = gcd m n := by simp [add_comm _ n] @[simp] lemma gcd_mul_left_add_right (m n k : β„•) : gcd m (m * k + n) = gcd m n := by simp [add_comm _ n] @[simp] lemma gcd_add_mul_right_left (m n k : β„•) : gcd (m + k * n) n = gcd m n := by rw [gcd_comm, gcd_add_mul_right_right, gcd_comm] @[simp] lemma gcd_add_mul_left_left (m n k : β„•) : gcd (m + n * k) n = gcd m n := by rw [gcd_comm, gcd_add_mul_left_right, gcd_comm] @[simp] lemma gcd_mul_right_add_left (m n k : β„•) : gcd (k * n + m) n = gcd m n := by rw [gcd_comm, gcd_mul_right_add_right, gcd_comm] @[simp] lemma gcd_mul_left_add_left (m n k : β„•) : gcd (n * k + m) n = gcd m n := by rw [gcd_comm, gcd_mul_left_add_right, gcd_comm] -- Lemmas where one argument consists of an addition of the other @[simp] lemma gcd_add_self_right (m n : β„•) : gcd m (n + m) = gcd m n := eq.trans (by rw one_mul) (gcd_add_mul_right_right m n 1) @[simp] lemma gcd_add_self_left (m n : β„•) : gcd (m + n) n = gcd m n := by rw [gcd_comm, gcd_add_self_right, gcd_comm] @[simp] lemma gcd_self_add_left (m n : β„•) : gcd (m + n) m = gcd n m := by rw [add_comm, gcd_add_self_left] @[simp] lemma gcd_self_add_right (m n : β„•) : gcd m (m + n) = gcd m n := by rw [add_comm, gcd_add_self_right] /-! ### `lcm` -/ theorem lcm_comm (m n : β„•) : lcm m n = lcm n m := by delta lcm; rw [mul_comm, gcd_comm] @[simp] theorem lcm_zero_left (m : β„•) : lcm 0 m = 0 := by delta lcm; rw [zero_mul, nat.zero_div] @[simp] theorem lcm_zero_right (m : β„•) : lcm m 0 = 0 := lcm_comm 0 m β–Έ lcm_zero_left m @[simp] theorem lcm_one_left (m : β„•) : lcm 1 m = m := by delta lcm; rw [one_mul, gcd_one_left, nat.div_one] @[simp] theorem lcm_one_right (m : β„•) : lcm m 1 = m := lcm_comm 1 m β–Έ lcm_one_left m @[simp] theorem lcm_self (m : β„•) : lcm m m = m := or.elim (nat.eq_zero_or_pos m) (Ξ»h, by rw [h, lcm_zero_left]) (Ξ»h, by delta lcm; rw [gcd_self, nat.mul_div_cancel _ h]) theorem dvd_lcm_left (m n : β„•) : m ∣ lcm m n := dvd.intro (n / gcd m n) (nat.mul_div_assoc _ $ gcd_dvd_right m n).symm theorem dvd_lcm_right (m n : β„•) : n ∣ lcm m n := lcm_comm n m β–Έ dvd_lcm_left n m theorem gcd_mul_lcm (m n : β„•) : gcd m n * lcm m n = m * n := by delta lcm; rw [nat.mul_div_cancel' ((gcd_dvd_left m n).trans (dvd_mul_right m n))] theorem lcm_dvd {m n k : β„•} (H1 : m ∣ k) (H2 : n ∣ k) : lcm m n ∣ k := or.elim (nat.eq_zero_or_pos k) (Ξ»h, by rw h; exact dvd_zero _) (Ξ»kpos, dvd_of_mul_dvd_mul_left (gcd_pos_of_pos_left n (pos_of_dvd_of_pos H1 kpos)) $ by rw [gcd_mul_lcm, ←gcd_mul_right, mul_comm n k]; exact dvd_gcd (mul_dvd_mul_left _ H2) (mul_dvd_mul_right H1 _)) theorem lcm_dvd_mul (m n : β„•) : lcm m n ∣ m * n := lcm_dvd (dvd_mul_right _ _) (dvd_mul_left _ _) lemma lcm_dvd_iff {m n k : β„•} : lcm m n ∣ k ↔ m ∣ k ∧ n ∣ k := ⟨λ h, ⟨(dvd_lcm_left _ _).trans h, (dvd_lcm_right _ _).trans h⟩, and_imp.2 lcm_dvd⟩ theorem lcm_assoc (m n k : β„•) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm (lcm_dvd (lcm_dvd (dvd_lcm_left m (lcm n k)) ((dvd_lcm_left n k).trans (dvd_lcm_right m (lcm n k)))) ((dvd_lcm_right n k).trans (dvd_lcm_right m (lcm n k)))) (lcm_dvd ((dvd_lcm_left m n).trans (dvd_lcm_left (lcm m n) k)) (lcm_dvd ((dvd_lcm_right m n).trans (dvd_lcm_left (lcm m n) k)) (dvd_lcm_right (lcm m n) k))) theorem lcm_ne_zero {m n : β„•} (hm : m β‰  0) (hn : n β‰  0) : lcm m n β‰  0 := by { intro h, simpa [h, hm, hn] using gcd_mul_lcm m n, } /-! ### `coprime` See also `nat.coprime_of_dvd` and `nat.coprime_of_dvd'` to prove `nat.coprime m n`. -/ instance (m n : β„•) : decidable (coprime m n) := by unfold coprime; apply_instance theorem coprime_iff_gcd_eq_one {m n : β„•} : coprime m n ↔ gcd m n = 1 := iff.rfl theorem coprime.gcd_eq_one {m n : β„•} (h : coprime m n) : gcd m n = 1 := h theorem coprime.lcm_eq_mul {m n : β„•} (h : coprime m n) : lcm m n = m * n := by rw [←one_mul (lcm m n), ←h.gcd_eq_one, gcd_mul_lcm] theorem coprime.symm {m n : β„•} : coprime n m β†’ coprime m n := (gcd_comm m n).trans theorem coprime_comm {m n : β„•} : coprime n m ↔ coprime m n := ⟨coprime.symm, coprime.symm⟩ theorem coprime.symmetric : symmetric coprime := Ξ» m n, coprime.symm theorem coprime.dvd_of_dvd_mul_right {m n k : β„•} (H1 : coprime k n) (H2 : k ∣ m * n) : k ∣ m := let t := dvd_gcd (dvd_mul_left k m) H2 in by rwa [gcd_mul_left, H1.gcd_eq_one, mul_one] at t theorem coprime.dvd_mul_right {m n k : β„•} (H : coprime k n) : k ∣ m * n ↔ k ∣ m := ⟨H.dvd_of_dvd_mul_right, Ξ» h, dvd_mul_of_dvd_left h n⟩ theorem coprime.dvd_mul_left {m n k : β„•} (H : coprime k m) : k ∣ m * n ↔ k ∣ n := ⟨H.dvd_of_dvd_mul_left, Ξ» h, dvd_mul_of_dvd_right h m⟩ theorem coprime.gcd_mul_left_cancel {k : β„•} (m : β„•) {n : β„•} (H : coprime k n) : gcd (k * m) n = gcd m n := have H1 : coprime (gcd (k * m) n) k, by rw [coprime, gcd_assoc, H.symm.gcd_eq_one, gcd_one_right], dvd_antisymm (dvd_gcd (H1.dvd_of_dvd_mul_left (gcd_dvd_left _ _)) (gcd_dvd_right _ _)) (gcd_dvd_gcd_mul_left _ _ _) theorem coprime.gcd_mul_right_cancel (m : β„•) {k n : β„•} (H : coprime k n) : gcd (m * k) n = gcd m n := by rw [mul_comm m k, H.gcd_mul_left_cancel m] theorem coprime.gcd_mul_left_cancel_right {k m : β„•} (n : β„•) (H : coprime k m) : gcd m (k * n) = gcd m n := by rw [gcd_comm m n, gcd_comm m (k * n), H.gcd_mul_left_cancel n] theorem coprime.gcd_mul_right_cancel_right {k m : β„•} (n : β„•) (H : coprime k m) : gcd m (n * k) = gcd m n := by rw [mul_comm n k, H.gcd_mul_left_cancel_right n] theorem coprime_div_gcd_div_gcd {m n : β„•} (H : 0 < gcd m n) : coprime (m / gcd m n) (n / gcd m n) := by rw [coprime_iff_gcd_eq_one, gcd_div (gcd_dvd_left m n) (gcd_dvd_right m n), nat.div_self H] theorem not_coprime_of_dvd_of_dvd {m n d : β„•} (dgt1 : 1 < d) (Hm : d ∣ m) (Hn : d ∣ n) : Β¬ coprime m n := Ξ» co, not_lt_of_ge (le_of_dvd zero_lt_one $ by rw [←co.gcd_eq_one]; exact dvd_gcd Hm Hn) dgt1 theorem exists_coprime {m n : β„•} (H : 0 < gcd m n) : βˆƒ m' n', coprime m' n' ∧ m = m' * gcd m n ∧ n = n' * gcd m n := ⟨_, _, coprime_div_gcd_div_gcd H, (nat.div_mul_cancel (gcd_dvd_left m n)).symm, (nat.div_mul_cancel (gcd_dvd_right m n)).symm⟩ theorem exists_coprime' {m n : β„•} (H : 0 < gcd m n) : βˆƒ g m' n', 0 < g ∧ coprime m' n' ∧ m = m' * g ∧ n = n' * g := let ⟨m', n', h⟩ := exists_coprime H in ⟨_, m', n', H, h⟩ @[simp] theorem coprime_add_self_right {m n : β„•} : coprime m (n + m) ↔ coprime m n := by rw [coprime, coprime, gcd_add_self_right] @[simp] theorem coprime_self_add_right {m n : β„•} : coprime m (m + n) ↔ coprime m n := by rw [add_comm, coprime_add_self_right] @[simp] theorem coprime_add_self_left {m n : β„•} : coprime (m + n) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_self_left] @[simp] theorem coprime_self_add_left {m n : β„•} : coprime (m + n) m ↔ coprime n m := by rw [coprime, coprime, gcd_self_add_left] @[simp] lemma coprime_add_mul_right_right (m n k : β„•) : coprime m (n + k * m) ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_right_right] @[simp] lemma coprime_add_mul_left_right (m n k : β„•) : coprime m (n + m * k) ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_left_right] @[simp] lemma coprime_mul_right_add_right (m n k : β„•) : coprime m (k * m + n) ↔ coprime m n := by rw [coprime, coprime, gcd_mul_right_add_right] @[simp] lemma coprime_mul_left_add_right (m n k : β„•) : coprime m (m * k + n) ↔ coprime m n := by rw [coprime, coprime, gcd_mul_left_add_right] @[simp] lemma coprime_add_mul_right_left (m n k : β„•) : coprime (m + k * n) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_right_left] @[simp] lemma coprime_add_mul_left_left (m n k : β„•) : coprime (m + n * k) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_left_left] @[simp] lemma coprime_mul_right_add_left (m n k : β„•) : coprime (k * n + m) n ↔ coprime m n := by rw [coprime, coprime, gcd_mul_right_add_left] @[simp] lemma coprime_mul_left_add_left (m n k : β„•) : coprime (n * k + m) n ↔ coprime m n := by rw [coprime, coprime, gcd_mul_left_add_left] theorem coprime.mul {m n k : β„•} (H1 : coprime m k) (H2 : coprime n k) : coprime (m * n) k := (H1.gcd_mul_left_cancel n).trans H2 theorem coprime.mul_right {k m n : β„•} (H1 : coprime k m) (H2 : coprime k n) : coprime k (m * n) := (H1.symm.mul H2.symm).symm theorem coprime.coprime_dvd_left {m k n : β„•} (H1 : m ∣ k) (H2 : coprime k n) : coprime m n := eq_one_of_dvd_one (by delta coprime at H2; rw ← H2; exact gcd_dvd_gcd_of_dvd_left _ H1) theorem coprime.coprime_dvd_right {m k n : β„•} (H1 : n ∣ m) (H2 : coprime k m) : coprime k n := (H2.symm.coprime_dvd_left H1).symm theorem coprime.coprime_mul_left {k m n : β„•} (H : coprime (k * m) n) : coprime m n := H.coprime_dvd_left (dvd_mul_left _ _) theorem coprime.coprime_mul_right {k m n : β„•} (H : coprime (m * k) n) : coprime m n := H.coprime_dvd_left (dvd_mul_right _ _) theorem coprime.coprime_mul_left_right {k m n : β„•} (H : coprime m (k * n)) : coprime m n := H.coprime_dvd_right (dvd_mul_left _ _) theorem coprime.coprime_mul_right_right {k m n : β„•} (H : coprime m (n * k)) : coprime m n := H.coprime_dvd_right (dvd_mul_right _ _) theorem coprime.coprime_div_left {m n a : β„•} (cmn : coprime m n) (dvd : a ∣ m) : coprime (m / a) n := begin by_cases a_split : (a = 0), { subst a_split, rw zero_dvd_iff at dvd, simpa [dvd] using cmn, }, { rcases dvd with ⟨k, rfl⟩, rw nat.mul_div_cancel_left _ (nat.pos_of_ne_zero a_split), exact coprime.coprime_mul_left cmn, }, end theorem coprime.coprime_div_right {m n a : β„•} (cmn : coprime m n) (dvd : a ∣ n) : coprime m (n / a) := (coprime.coprime_div_left cmn.symm dvd).symm lemma coprime_mul_iff_left {k m n : β„•} : coprime (m * n) k ↔ coprime m k ∧ coprime n k := ⟨λ h, ⟨coprime.coprime_mul_right h, coprime.coprime_mul_left h⟩, Ξ» ⟨h, _⟩, by rwa [coprime_iff_gcd_eq_one, coprime.gcd_mul_left_cancel n h]⟩ lemma coprime_mul_iff_right {k m n : β„•} : coprime k (m * n) ↔ coprime k m ∧ coprime k n := by simpa only [coprime_comm] using coprime_mul_iff_left lemma coprime.gcd_left (k : β„•) {m n : β„•} (hmn : coprime m n) : coprime (gcd k m) n := hmn.coprime_dvd_left $ gcd_dvd_right k m lemma coprime.gcd_right (k : β„•) {m n : β„•} (hmn : coprime m n) : coprime m (gcd k n) := hmn.coprime_dvd_right $ gcd_dvd_right k n lemma coprime.gcd_both (k l : β„•) {m n : β„•} (hmn : coprime m n) : coprime (gcd k m) (gcd l n) := (hmn.gcd_left k).gcd_right l lemma coprime.mul_dvd_of_dvd_of_dvd {a n m : β„•} (hmn : coprime m n) (hm : m ∣ a) (hn : n ∣ a) : m * n ∣ a := let ⟨k, hk⟩ := hm in hk.symm β–Έ mul_dvd_mul_left _ (hmn.symm.dvd_of_dvd_mul_left (hk β–Έ hn)) theorem coprime_one_left : βˆ€ n, coprime 1 n := gcd_one_left theorem coprime_one_right : βˆ€ n, coprime n 1 := gcd_one_right theorem coprime.pow_left {m k : β„•} (n : β„•) (H1 : coprime m k) : coprime (m ^ n) k := nat.rec_on n (coprime_one_left _) (Ξ»n IH, H1.mul IH) theorem coprime.pow_right {m k : β„•} (n : β„•) (H1 : coprime k m) : coprime k (m ^ n) := (H1.symm.pow_left n).symm theorem coprime.pow {k l : β„•} (m n : β„•) (H1 : coprime k l) : coprime (k ^ m) (l ^ n) := (H1.pow_left _).pow_right _ @[simp] lemma coprime_pow_left_iff {n : β„•} (hn : 0 < n) (a b : β„•) : nat.coprime (a ^ n) b ↔ nat.coprime a b := begin obtain ⟨n, rfl⟩ := exists_eq_succ_of_ne_zero hn.ne', rw [pow_succ, nat.coprime_mul_iff_left], exact ⟨and.left, Ξ» hab, ⟨hab, hab.pow_left _⟩⟩ end @[simp] lemma coprime_pow_right_iff {n : β„•} (hn : 0 < n) (a b : β„•) : nat.coprime a (b ^ n) ↔ nat.coprime a b := by rw [nat.coprime_comm, coprime_pow_left_iff hn, nat.coprime_comm] theorem coprime.eq_one_of_dvd {k m : β„•} (H : coprime k m) (d : k ∣ m) : k = 1 := by rw [← H.gcd_eq_one, gcd_eq_left d] @[simp] theorem coprime_zero_left (n : β„•) : coprime 0 n ↔ n = 1 := by simp [coprime] @[simp] theorem coprime_zero_right (n : β„•) : coprime n 0 ↔ n = 1 := by simp [coprime] theorem not_coprime_zero_zero : Β¬ coprime 0 0 := by simp @[simp] theorem coprime_one_left_iff (n : β„•) : coprime 1 n ↔ true := by simp [coprime] @[simp] theorem coprime_one_right_iff (n : β„•) : coprime n 1 ↔ true := by simp [coprime] @[simp] theorem coprime_self (n : β„•) : coprime n n ↔ n = 1 := by simp [coprime] lemma gcd_mul_of_coprime_of_dvd {a b c : β„•} (hac : coprime a c) (b_dvd_c : b ∣ c) : gcd (a * b) c = b := begin rcases exists_eq_mul_left_of_dvd b_dvd_c with ⟨d, rfl⟩, rw [gcd_mul_right], convert one_mul b, exact coprime.coprime_mul_right_right hac, end lemma coprime.eq_of_mul_eq_zero {m n : β„•} (h : m.coprime n) (hmn : m * n = 0) : m = 0 ∧ n = 1 ∨ m = 1 ∧ n = 0 := (nat.eq_zero_of_mul_eq_zero hmn).imp (Ξ» hm, ⟨hm, n.coprime_zero_left.mp $ hm β–Έ h⟩) (Ξ» hn, ⟨m.coprime_zero_left.mp $ hn β–Έ h.symm, hn⟩) /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. See `exists_dvd_and_dvd_of_dvd_mul` for the more general but less constructive version for other `gcd_monoid`s. -/ def prod_dvd_and_dvd_of_dvd_prod {m n k : β„•} (H : k ∣ m * n) : { d : {m' // m' ∣ m} Γ— {n' // n' ∣ n} // k = d.1 * d.2 } := begin cases h0 : (gcd k m), case nat.zero { obtain rfl : k = 0 := eq_zero_of_gcd_eq_zero_left h0, obtain rfl : m = 0 := eq_zero_of_gcd_eq_zero_right h0, exact ⟨⟨⟨0, dvd_refl 0⟩, ⟨n, dvd_refl n⟩⟩, (zero_mul n).symm⟩ }, case nat.succ : tmp { have hpos : 0 < gcd k m := h0.symm β–Έ nat.zero_lt_succ _; clear h0 tmp, have hd : gcd k m * (k / gcd k m) = k := (nat.mul_div_cancel' (gcd_dvd_left k m)), refine ⟨⟨⟨gcd k m, gcd_dvd_right k m⟩, ⟨k / gcd k m, _⟩⟩, hd.symm⟩, apply dvd_of_mul_dvd_mul_left hpos, rw [hd, ← gcd_mul_right], exact dvd_gcd (dvd_mul_right _ _) H } end lemma dvd_mul {x m n : β„•} : x ∣ (m * n) ↔ βˆƒ y z, y ∣ m ∧ z ∣ n ∧ y * z = x := begin split, { intro h, obtain ⟨⟨⟨y, hy⟩, ⟨z, hz⟩⟩, rfl⟩ := prod_dvd_and_dvd_of_dvd_prod h, exact ⟨y, z, hy, hz, rfl⟩, }, { rintro ⟨y, z, hy, hz, rfl⟩, exact mul_dvd_mul hy hz }, end theorem gcd_mul_dvd_mul_gcd (k m n : β„•) : gcd k (m * n) ∣ gcd k m * gcd k n := begin rcases (prod_dvd_and_dvd_of_dvd_prod $ gcd_dvd_right k (m * n)) with ⟨⟨⟨m', hm'⟩, ⟨n', hn'⟩⟩, h⟩, replace h : gcd k (m * n) = m' * n' := h, rw h, have hm'n' : m' * n' ∣ k := h β–Έ gcd_dvd_left _ _, apply mul_dvd_mul, { have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n', exact dvd_gcd hm'k hm' }, { have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n', exact dvd_gcd hn'k hn' } end theorem coprime.gcd_mul (k : β„•) {m n : β„•} (h : coprime m n) : gcd k (m * n) = gcd k m * gcd k n := dvd_antisymm (gcd_mul_dvd_mul_gcd k m n) ((h.gcd_both k k).mul_dvd_of_dvd_of_dvd (gcd_dvd_gcd_mul_right_right _ _ _) (gcd_dvd_gcd_mul_left_right _ _ _)) theorem pow_dvd_pow_iff {a b n : β„•} (n0 : 0 < n) : a ^ n ∣ b ^ n ↔ a ∣ b := begin refine ⟨λ h, _, Ξ» h, pow_dvd_pow_of_dvd h _⟩, cases nat.eq_zero_or_pos (gcd a b) with g0 g0, { simp [eq_zero_of_gcd_eq_zero_right g0] }, rcases exists_coprime' g0 with ⟨g, a', b', g0', co, rfl, rfl⟩, rw [mul_pow, mul_pow] at h, replace h := dvd_of_mul_dvd_mul_right (pow_pos g0' _) h, have := pow_dvd_pow a' n0, rw [pow_one, (co.pow n n).eq_one_of_dvd h] at this, simp [eq_one_of_dvd_one this] end lemma gcd_mul_gcd_of_coprime_of_mul_eq_mul {a b c d : β„•} (cop : c.coprime d) (h : a * b = c * d) : a.gcd c * b.gcd c = c := begin apply dvd_antisymm, { apply nat.coprime.dvd_of_dvd_mul_right (nat.coprime.mul (cop.gcd_left _) (cop.gcd_left _)), rw ← h, apply mul_dvd_mul (gcd_dvd _ _).1 (gcd_dvd _ _).1 }, { rw [gcd_comm a _, gcd_comm b _], transitivity c.gcd (a * b), rw [h, gcd_mul_right_right d c], apply gcd_mul_dvd_mul_gcd } end /-- If `k:β„•` divides coprime `a` and `b` then `k = 1` -/ lemma eq_one_of_dvd_coprimes {a b k : β„•} (h_ab_coprime : coprime a b) (hka : k ∣ a) (hkb : k ∣ b) : k = 1 := begin rw coprime_iff_gcd_eq_one at h_ab_coprime, have h1 := dvd_gcd hka hkb, rw h_ab_coprime at h1, exact nat.dvd_one.mp h1, end lemma coprime.mul_add_mul_ne_mul {m n a b : β„•} (cop : coprime m n) (ha : a β‰  0) (hb : b β‰  0) : a * m + b * n β‰  m * n := begin intro h, obtain ⟨x, rfl⟩ : n ∣ a := cop.symm.dvd_of_dvd_mul_right ((nat.dvd_add_iff_left (dvd_mul_left n b)).mpr ((congr_arg _ h).mpr (dvd_mul_left n m))), obtain ⟨y, rfl⟩ : m ∣ b := cop.dvd_of_dvd_mul_right ((nat.dvd_add_iff_right (dvd_mul_left m (n*x))).mpr ((congr_arg _ h).mpr (dvd_mul_right m n))), rw [mul_comm, mul_ne_zero_iff, ←one_le_iff_ne_zero] at ha hb, refine mul_ne_zero hb.2 ha.2 (eq_zero_of_mul_eq_self_left (ne_of_gt (add_le_add ha.1 hb.1)) _), rw [← mul_assoc, ← h, add_mul, add_mul, mul_comm _ n, ←mul_assoc, mul_comm y] end end nat
(* Not considering singleton template-polymorphic inductive types as propositions for injection/inversion *) (* This is also #4560 and #6273 *) Inductive foo := foo_1. Goal forall (a b : foo), Some a = Some b -> a = b. Proof. intros a b H. inversion H. reflexivity. Qed. (* Check that Prop is not concerned *) Inductive bar : Prop := bar_1. Goal forall (a b : bar), Some a = Some b -> a = b. Proof. intros a b H. inversion H. Fail reflexivity. Abort.
lemma measurable_restrict_countable: assumes X[intro]: "countable X" assumes sets[simp]: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M" assumes space[simp]: "\<And>x. x \<in> X \<Longrightarrow> f x \<in> space N" assumes f: "f \<in> measurable (restrict_space M (- X)) N" shows "f \<in> measurable M N"
data _≑_ {A : Set} (x : A) : A β†’ Set where refl : x ≑ x data Maybe (A : Set) : Set where nothing : Maybe A just : A β†’ Maybe A postulate String : Set {-# BUILTIN STRING String #-} infix 1000 β™―_ postulate ∞ : βˆ€ {a} (A : Set a) β†’ Set a β™―_ : βˆ€ {a} {A : Set a} β†’ A β†’ ∞ A β™­ : βˆ€ {a} {A : Set a} β†’ ∞ A β†’ A {-# BUILTIN INFINITY ∞ #-} {-# BUILTIN SHARP β™―_ #-} {-# BUILTIN FLAT β™­ #-} infixr 5 _∷_ data Colist (A : Set) : Set where [] : Colist A _∷_ : (x : A) (xs : ∞ (Colist A)) β†’ Colist A postulate Char : Set {-# BUILTIN CHAR Char #-} Costring : Set Costring = Colist Char data β„• : Set where zero : β„• suc : β„• β†’ β„• {-# BUILTIN NATURAL β„• #-} data List {a} (A : Set a) : Set a where [] : List A _∷_ : (x : A) (xs : List A) β†’ List A {-# BUILTIN LIST List #-} {-# BUILTIN NIL [] #-} {-# BUILTIN CONS _∷_ #-} private primitive primStringAppend : String β†’ String β†’ String primStringToList : String β†’ List Char primStringFromList : List Char β†’ String infixr 5 _++_ _++_ : String β†’ String β†’ String _++_ = primStringAppend toList : String β†’ List Char toList = primStringToList fromList : List Char β†’ String fromList = primStringFromList fromListToColist : {A : Set} β†’ List A β†’ Colist A fromListToColist [] = [] fromListToColist (x ∷ xs) = x ∷ β™― fromListToColist xs toCostring : String β†’ Costring toCostring s = fromListToColist (toList s) ------------------------------------------------------------------------ costringToString : Costring β†’ β„• β†’ Maybe String costringToString [] _ = just "" costringToString (x ∷ xs) 0 with β™­ xs ... | [] = just (fromList (x ∷ [])) ... | _ ∷ _ = nothing costringToString (x ∷ xs) (suc n) with costringToString (β™­ xs) n ... | nothing = nothing ... | just xsβ€² = just (fromList (x ∷ []) ++ xsβ€²) test0 : costringToString (toCostring "") 0 ≑ just "" test0 = refl test1 : costringToString (toCostring "apa") 3 ≑ just "apa" test1 = refl
(** BingShrinkingCriterion.v by Ken'ichi Kuga **************) (** Simplified using SSReflect by Mitsuharu Yamamoto ******) (***********************************************************) (* Bing Shrinking Criterion *) (* and *) (* Bing Shrinking Theorem for compact spaces *) (* *********************************************************) (* Definition approximable_by_homeos (f:X->Y): Prop:= forall eps:R, eps>0 -> exists h:point_set Xt -> point_set Yt, homeomorphism h /\ (forall x:X, d' (f x) (h x) < eps). Definition Bing_shrinkable (f:X->Y): Prop:= forall eps:R, eps>0 -> exists h : point_set Xt -> point_set Xt, homeomorphism h /\ (forall x:X, d' (f x) (f (h x)) < eps) /\ (forall x1 x2:X, (f x1) = (f x2) -> d (h x1) (h x2) < eps). Theorem Bing_Shrinking_Theorem: forall f: point_set Xt -> point_set Yt, continuous f -> surjective f -> (Bing_shrinkable f -> approximable_by_homeos f). ************************************************************) Require Import ProofIrrelevance. Require Import ClassicalChoice. Require Import Classical. Require Import Fourier. Require Import FunctionalExtensionality. From mathcomp Require Import ssreflect ssrbool. From ZornsLemma Require Import Proj1SigInjective. From Topology Require Import MetricSpaces RTopology ContinuousFactorization. From Topology Require Import Completeness Compactness WeakTopology Homeomorphisms. From Topology Require Import RationalsInReals Continuity. (*******************************) Require Import BaireSpaces. Require Import LemmasForBSC. (*******************************) Open Scope R_scope. Section Topological_Lemmas. (*** Some frequently used inequqlities ***) (* (* This can be proved with "auto with *". *) Lemma pos_INR_Sn: forall n:nat, 0 < INR (S n). Proof. by move=> n; apply: lt_0_INR; apply: lt_0_Sn. Qed. (* This can be proved with "auto with *". *) Lemma pos_inv_INR_Sn: forall n:nat, 0 < /INR (S n). Proof. by move=> n0; apply: Rinv_0_lt_compat; apply: pos_INR_Sn. Qed. *) Lemma Rlt_inv_INR_S_contravar: forall n m:nat, (n < m)%nat -> /INR (S m) < /INR (S n). Proof. move=> n m nltm. apply: Rinv_lt_contravar; first by apply: Rmult_lt_0_compat; auto with *. by apply: lt_INR; apply: lt_n_S. Qed. Lemma Rle_inv_INR_S_contravar: forall n m:nat, (n <= m)%nat -> /INR (S m) <= /INR (S n). Proof. move=> n m nlem. apply: Rinv_le_contravar; first by auto with *. by apply: le_INR; apply: le_n_S. Qed. (******) Definition id_map (XT:TopologicalSpace): point_set XT -> point_set XT:= fun x:point_set XT => x. Lemma id_map_continuous : forall XT:TopologicalSpace, continuous (id_map XT). Proof. move=> XT V V_open. suff ->: inverse_image (id_map XT) V = V by []. apply: Extensionality_Ensembles; split => x //. by case. Qed. Lemma id_map_homeomorphism : forall XT:TopologicalSpace, homeomorphism (id_map XT). Proof. move=>XT. by exists (id_map XT) => //; apply: id_map_continuous. Qed. Variable T:Type. Variable dt:T->T->R. Hypothesis dt_metric: metric dt. Let Tt := MetricTopology dt dt_metric. Lemma open_ball_open: forall (x: T) (r: R), r > 0 -> open (open_ball T dt x r : Ensemble (point_set Tt)). Proof. move=> x r H_r_pos. apply: open_ball_is_open => //. by apply: MetricTopology_metrizable. Qed. Lemma MetricTopology_Hausdorff: Hausdorff (MetricTopology dt dt_metric). Proof. apply: T3_sep_impl_Hausdorff. apply: normal_sep_impl_T3_sep. apply: metrizable_impl_normal_sep. exists dt => //. exact: MetricTopology_metrizable. Qed. Lemma lim_range: forall (x: T) (xn:Net nat_DS Tt) (r:R) (n0:nat), (forall n:nat, (n0 <= n)%nat -> dt (xn n0) (xn n) <= r) -> net_limit xn x -> dt (xn n0) x <= r. Proof. move=> x xn r n0 dtxn0n lim_x. apply: Rnot_gt_le => H. pose eps := dt (xn n0) x - r. have eps_pos: eps > 0 by apply: Rgt_minus. case: (lim_x (open_ball T dt x eps)). - exact: open_ball_open. - constructor; by rewrite metric_zero. - rewrite /= => x0 H0. suff: dt (xn n0) x < r + eps by apply: Rge_not_lt; rewrite /eps; fourier. pose m0 := max x0 n0. apply: Rle_lt_trans (_ : (dt (xn n0) (xn m0) + dt (xn m0) x) < _); first by apply: triangle_inequality. apply: Rplus_le_lt_compat (dtxn0n _ _) _; first by apply: Max.le_max_r. case: (H0 m0); first by apply: Max.le_max_l. by rewrite metric_sym. Qed. End Topological_Lemmas. Section BingShrinkingTheorem. Variables X Y:Type. Variables (d:X->X->R) (d':Y->Y->R). Variable (y0:X->Y) (Y0:Y). Hypotheses (d_metric: metric d) (d'_metric: metric d'). Hypothesis X_inhabited: inhabited X. Let Xt := MetricTopology d d_metric. Let Yt := MetricTopology d' d'_metric. Let CMap := { f:X->Y | bound (Im Full_set (fun x:X=> d' (y0 x) (f x)))/\ @continuous Xt Yt f }. Let um (f g:CMap):R. refine (match f, g with exist f0 Hf, exist g0 Hg => proj1_sig (sup (Im Full_set (fun x:X => d' (f0 x) (g0 x))) _ _) end). destruct Hf as [hf _]. destruct hf as [mf]. destruct Hg as [hg _]. destruct hg as [mg]. exists (mf + mg). red; intros. destruct H1. rewrite H2. apply Rle_trans with (d' (y0 x) (f0 x) + d' (y0 x) (g0 x)). rewrite (metric_sym _ d' d'_metric (y0 x) (f0 x)); apply triangle_inequality; trivial. assert (d' (y0 x) (f0 x) <= mf) by (apply H; exists x; trivial). assert (d' (y0 x) (g0 x) <= mg) by (apply H0; exists x; trivial). auto with real. destruct X_inhabited as [x0]. exists (d' (f0 x0) (g0 x0)); exists x0. constructor. reflexivity. (* Ssreflect-style proof. But this makes the term bigger. - case: Hf => [[mf Hf] _]; case: Hg => [[mg Hg] _]. exists (mf + mg) => _ [x _ _ ->]. apply: Rle_trans (_ : d' (f0 x) (y0 x) + d' (y0 x) (g0 x) <= _); first by apply: triangle_inequality. rewrite (metric_sym _ _ d'_metric). apply: Rplus_le_compat. + by apply: Hf; exists x. + by apply: Hg; exists x. - case: X_inhabited => [x0]. by exists (d' (f0 x0) (g0 x0)); exists x0. *) Defined. Lemma um_metric: metric um. Proof. constructor. - move=> [f0 Hf] [g0 Hg] /=. case: X_inhabited => [x0] /=. case: sup => /= x [Hxub Hxleast]. apply: Rge_trans (_ : d' (f0 x0) (g0 x0) >= _); last by case: d'_metric. apply: Rle_ge. apply: Hxub. by exists x0. - move=> [f0 Hf] [g0 Hg] /=. case: sup => /= x [Hxub Hxleast]. case: sup => /= y [Hyub Hyleast]. have j: Im Full_set (fun x:X => d'(f0 x) (g0 x)) = Im Full_set (fun x:X => d'(g0 x) (f0 x)) by apply: Extensionality_Ensembles; split => /=; move => _ [x1 _ _ ->]; exists x1 => //; rewrite metric_sym. apply: Rle_antisym. + by apply: Hxleast; rewrite j; apply: Hyub. + by apply: Hyleast; rewrite -j; apply: Hxub. - move=> [f0 Hf] [g0 Hg] [h0 Hh] /=. case: sup => /= x [Hxub Hxleast]. case: sup => /= y [Hyub Hyleast]. case: sup => /= z [Hzub Hzleast]. apply: Hxleast => _ [x2 _ _ ->]. apply: Rle_trans (_ : d' (f0 x2) (g0 x2) + d' (g0 x2) (h0 x2) <= _); first by case: d'_metric. apply: Rplus_le_compat. + by apply: Hyub; exists x2. + by apply: Hzub; exists x2. - move=> [f0 [Bf Cf]] /=. case: sup => /= x [Hxub Hxleast]. apply: Rle_antisym. + apply: Hxleast => _ [x0 _ _ ->]. rewrite metric_zero //. by auto with real. + apply: Hxub. case: X_inhabited => [x0]. exists x0 => //. by rewrite metric_zero. - move=> [f0 [Bf Cf]] [g0 [Bg Cg]] /=. case: sup => /= x0 [Hx0ub Hx0least] Hx0. (* Require Import Proj1SigInjective.*) apply: subset_eq_compatT. (* Require Import FunctionalExtensionality.*) extensionality x. apply: (metric_strict _ _ d'_metric). rewrite Hx0 in Hx0ub. rewrite Hx0 in Hx0least. apply: Rle_antisym. + apply: Hx0ub. by exists x. + apply: Rge_le. by case: d'_metric. Qed. Lemma Rle_d'_um: forall (f g:CMap) (x:X), d' (proj1_sig f x) (proj1_sig g x) <= um f g. Proof. move=> [f0 [Bf Cf]] [g0 [Bg Cg]] /= x. case sup => /= x0 [Hx0ub Hx0least]. apply: Hx0ub. by exists x. Qed. Lemma Rle_um_all_d': forall (f g:CMap) (r:R), r > 0 -> (forall x:X, d' (proj1_sig f x) (proj1_sig g x) <=r) -> um f g <= r. Proof. move=> [f0 [Bf Cf]] [g0 [Bg Cg]] /= r r_pos Hd'r. case sup => /= x0 [Hx0ub Hx0least]. apply: Hx0least. move=> _ [x _ _ ->]. exact: Hd'r. Qed. Let CMapt := MetricTopology um um_metric. Lemma um_complete: complete d' d'_metric -> complete um um_metric. Proof. move=> cpl_d' fn cauchy_fn. pose yn (x:X): Net nat_DS Yt:= fun n:nat => (proj1_sig (fn n%nat)) x. have cauchy_yn: forall x:X, cauchy d' (yn x). { move=> x eps pos_eps. case: (cauchy_fn _ pos_eps) => [N cau_fn]. exists N. move=> m n hm hn. apply: Rle_lt_trans (_ : um (fn m) (fn n) < _); first by apply: Rle_d'_um. by apply: cau_fn. } pose choice_R (x:X) (y:Y): Prop := net_limit (yn x) y. have choice_f0: forall x:X, exists y:Y, (choice_R x y) by move=> x; apply: cpl_d'; apply: cauchy_yn. have [f0 Hf0]: exists f0: X->Y, (forall x:X, choice_R x (f0 x)) by apply: choice. have Bf0: bound (Im Full_set (fun x:X=> d' (y0 x) (f0 x))). { case: (cauchy_fn 1); first by apply: Rlt_0_1. move=> n0 Bd1. case: (proj2_sig (fn n0)) => [[ub Bfn0] _]. exists (ub+1) => _ [x _ _ ->]. apply: Rle_trans (_ : d' (y0 x) (proj1_sig (fn n0) x) + d' (proj1_sig (fn n0) x) (f0 x) <= _); first by apply: triangle_inequality. apply: Rplus_le_compat. - apply: (Bfn0 (d' (y0 x) (proj1_sig (fn n0) x))). by exists x. - have d'um1: forall n:nat, (n >= n0)%nat -> d' (proj1_sig (fn n0) x) (proj1_sig (fn n) x) < 1. { move=> n hn. apply: Rle_lt_trans (_ : um (fn n0) (fn n) < _); first by apply: Rle_d'_um. by apply: Bd1. } apply: Rnot_lt_le => Fh. pose ep := d' (proj1_sig (fn n0) x) (f0 x) - 1. have hpos_ep: ep > 0 by apply: Rgt_minus. case: (Hf0 x (open_ball Y d' (f0 x) ep)). + exact: open_ball_open. + constructor. by rewrite metric_zero. + rewrite /= => x0 H1. pose m0 := max n0 x0. case: (H1 m0); first by apply: Max.le_max_r. have H3: d' (proj1_sig (fn n0) x) (yn x m0) < 1 by apply: d'um1; apply: Max.le_max_l. apply: Rle_not_gt. rewrite metric_sym //. apply: Rle_move_pr2ml. apply: Rle_trans (_ : d' (proj1_sig (fn n0) x) (yn x m0) + d' (yn x m0) (f0 x) <= _); first by apply: triangle_inequality. apply: Rlt_le. rewrite Rplus_comm. exact: Rplus_lt_compat_l. } have Cf0: @continuous Xt Yt f0. { apply: pointwise_continuity => /= x. apply: (metric_space_fun_continuity Xt Yt _ _ d d'). - exact: MetricTopology_metrizable. - exact: MetricTopology_metrizable. - move=> eps eps_pos /=. case: (cauchy_fn (/4 * eps)); first by fourier. move=> N H. have f0fN: forall x:X, d' (f0 x) (proj1_sig (fn N) x) <= /4 * eps. { move=> x0. apply: Rnot_gt_le => H0. pose de := d' (f0 x0) (proj1_sig (fn N) x0) - /4 * eps. have de_pos: de > 0 by apply: Rgt_minus. case (Hf0 x0 (open_ball Y d' (f0 x0) de)). - exact: open_ball_open. - constructor. by rewrite metric_zero. - rewrite /= => x1 H1. pose N1 := max N x1. have f0ynx1 : d' (f0 x0) (yn x0 N1) < de by case: (H1 N1) => //; by apply: Max.le_max_r. have ynNynN1 : d' (yn x0 N1) (yn x0 N) < /4 * eps by apply: Rle_lt_trans (_ : um (fn N1) (fn N) < _); [apply: Rle_d'_um | apply: H => //; apply: Max.le_max_l]. have: d' (f0 x0) (yn x0 N) < de + /4 * eps. + apply: Rle_lt_trans (_ : d' (f0 x0) (yn x0 N1) + d' (yn x0 N1) (yn x0 N) < _); first by apply: triangle_inequality. exact: Rplus_lt_compat. apply: Rge_not_lt. rewrite /de /yn. by fourier. } case: (proj2_sig (fn N)) => _. move/continuous_func_continuous_everywhere/(_ x). move/metric_space_fun_continuity_converse. case/(_ d d')/(_ _ _ (/2 * eps)); [exact: MetricTopology_metrizable | exact: MetricTopology_metrizable | by fourier |]. move=> delta [delta_pos HC]. exists delta; split => // x' dxx'_le_delta. rewrite [x in _ < x](_ : eps = /4*eps + /2*eps + /4*eps); last by field. apply: Rle_lt_trans (_ : d' (f0 x) (proj1_sig (fn N) x') + d'(proj1_sig (fn N) x') (f0 x') < _); first by apply: triangle_inequality. rewrite [d' _ (f0 x')]metric_sym //. apply: Rplus_lt_le_compat => //. apply: Rle_lt_trans (_ : d' (f0 x) (proj1_sig (fn N) x) + d' (proj1_sig (fn N) x) (proj1_sig (fn N) x') < _); first by apply: triangle_inequality. apply: Rplus_le_lt_compat => //. by apply: HC. } exists (exist (fun g:X->Y => bound (Im Full_set (fun x:X=> d' (y0 x) (g x))) /\ @continuous Xt Yt g) f0 (conj Bf0 Cf0)). apply: (metric_space_net_limit CMapt um). apply: MetricTopology_metrizable. move=> eps eps_pos. case: (cauchy_fn (/2*eps)); first by fourier. move=> i0 H. exists i0 => j i0j. apply: Rle_lt_trans (_ : /2*eps < _). - apply: Rle_um_all_d'; first by fourier. move=> x /=. rewrite -[proj1_sig (fn j) x]/((yn x) j). rewrite metric_sym //. rewrite /= in i0j. apply: (lim_range Y d' d'_metric (f0 x) (yn x) (/2*eps) j) => n le_j_n. + apply: Rle_trans (_ : um (fn j) (fn n) <= _); first by apply: Rle_d'_um. apply: Rlt_le. apply: H. by auto. + exact: le_trans le_j_n. - exact: Hf0. - by fourier. Qed. Hypothesis X_compact: compact Xt. Hypothesis Y_compact: compact Yt. Hypothesis y0Y0: forall x:X, y0 x = Y0. Lemma continuous_bounded: forall f : point_set Xt -> point_set Yt, continuous f -> bound (Im Full_set (fun x:X=> d' (y0 x) (f x))). Proof. move=> f f_conti. pose g (y : point_set Yt) : point_set RTop := d' Y0 y. pose ft (x : point_set Xt) : point_set RTop := g (f x). have ft_conti: continuous ft. { apply: continuous_composition => //. apply: pointwise_continuity => y. apply: (metric_space_fun_continuity Yt RTop _ _ d' R_metric). - exact: MetricTopology_metrizable. - exact: RTop_metrization. - move=> eps eps_pos. exists eps; split => //. move=> y' d'yy'_eps. rewrite /R_metric /g. apply: Rabs_def1. + apply: Rlt_move_pr2ml. apply: Rle_lt_trans (_ : d' Y0 y + d' y y' < _); first by apply: triangle_inequality. rewrite Rplus_comm. exact: Rplus_lt_compat_r. + apply: Rlt_move_pl2mr. rewrite Rplus_comm. apply: Rlt_move_mr2pl. apply: Rle_lt_trans (_ : d' Y0 y' + d' y' y < _); first by apply: triangle_inequality. apply: Rplus_lt_compat_l. by rewrite metric_sym // Ropp_involutive. } apply: R_compact_subset_bounded. have ->: Im Full_set (fun x : X => d' (y0 x) (f x)) = Im Full_set ft by apply: Extensionality_Ensembles; split; move=> _ [x _ _ ->]; exists x => //; rewrite y0Y0. (* Require Import ContinuousFactorization. *) (*have ft_img: forall x:point_set Xt, In (Im Full_set ft) (ft x). move=>x. by apply Im_intro with x.*) pose ftr := continuous_surj_factorization ft. apply: (compact_image ftr) => //. - exact: continuous_surj_factorization_is_continuous. - exact: continuous_surj_factorization_is_surjective. Qed. (* continuous_bounded *) Let W (eps:R): Ensemble (point_set CMapt) := fun g:CMap => forall (x1 x2:X), (proj1_sig g x1) = (proj1_sig g x2) -> d x1 x2 < eps. Lemma W_is_open: forall (eps:R), eps > 0 -> open (W eps). Proof. move=> r rpos. suff ->: W r = interior (W r) by apply: interior_open. apply: Extensionality_Ensembles; split; last by apply: interior_deflationary. move=> fr fr_in_W. rewrite -[W r]Complement_Complement interior_complement => fr_in_clcoW. (********* fr found ***************) pose RR (n:nat) (g:CMap):Prop := In (Complement (W r)) g /\ um fr g < (/ INR (S n)). have [gn Hgn]: exists gn : nat -> CMap, forall n:nat, RR n (gn n). { apply: choice => n. have [gn Hgn]: Inhabited (Intersection (Complement (W r)) (open_ball CMap um fr (/ INR (S n)))). { apply: (closure_impl_meets_every_open_neighborhood _ _ fr) => //. - apply: open_ball_open. auto with *. - constructor. rewrite metric_zero; last by apply: um_metric. auto with *. } case: Hgn => {gn} gn CWgn [frgn]. exists gn; split => //. } pose RA (k:nat) (Ak: X * X * Y * CMap): Prop := (proj1_sig (snd Ak)) (fst (fst (fst Ak))) = (snd (fst Ak)) /\ (proj1_sig (snd Ak)) (snd (fst (fst Ak))) = (snd (fst Ak)) /\ d (fst (fst (fst Ak))) (snd (fst (fst Ak))) >= r /\ um fr (snd Ak) < / INR (S k). have [abcgn Habcgn]: exists Ak: nat -> X * X * Y * CMap, forall k:nat, (RA k (Ak k)). { apply: choice => k. (********) pose nr := S O. (********) have [ak [bk [Ck dakbk_r]]]: exists (ak bk:X), (proj1_sig (gn (max nr k)) ak) = (proj1_sig (gn (max nr k)) bk) /\ d ak bk >= r. { apply: NNPP => Hnex. case: (Hgn (max nr k)). case=> ak bk Ck. apply: Rnot_ge_lt => dakbk_r. apply: Hnex. by exists ak, bk. } exists (ak, bk, (proj1_sig (gn (max nr k)) ak), (gn (max nr k))). repeat split=> //. case: (Hgn (max nr k)) => _ H0. apply: Rlt_le_trans (_ : / INR (S (Init.Nat.max nr k)) <= _) => //. apply: Rle_inv_INR_S_contravar. exact: Max.le_max_r. } pose a_net:Net nat_DS Xt:= (fun (n:nat) => fst (fst (fst (abcgn n)))). have [lim_a H_lim_a]: exists a: point_set Xt, net_cluster_point a_net a by apply: compact_impl_net_cluster_point => //; apply: (inhabits O). pose a_cond (n N:nat):= (n <= N)%nat /\ d lim_a (a_net N) < / INR (S n). have [Na H_Na]: exists Na : nat -> nat, forall n, a_cond n (Na n). { apply: choice => n. case: (H_lim_a (open_ball X d lim_a (/INR (S n))) _ _ n). - apply: open_ball_open. - auto with *. - constructor. rewrite metric_zero => //. auto with *. - move=> x [H [H0]]. by exists x. } pose b_net:Net nat_DS Xt := (fun (n:nat) => snd (fst (fst (abcgn (Na n))))). have [lim_b H_lim_b]: exists b: point_set Xt, net_cluster_point b_net b by apply: compact_impl_net_cluster_point => //; apply: (inhabits O). pose ab_cond (n N:nat) := (n <= N)%nat /\ (d lim_a (a_net (Na N)) < / INR (S n)) /\ (d lim_b (b_net N) < / INR (S n)). have [Nab H_Nab]: exists Nab : nat -> nat, forall n, ab_cond n (Nab n). { apply: choice => n. case: (H_lim_b (open_ball X d lim_b (/INR (S n))) _ _ n). - apply: open_ball_open. - auto with *. - constructor. rewrite metric_zero => //. auto with *. - move=> x [H [H0]]. exists x; repeat split => //. apply: Rlt_le_trans (_ : /INR (S x) <= _); first by case: (H_Na x). exact: Rle_inv_INR_S_contravar. } (*******************) pose aN (n:nat) : X := a_net (Na (Nab n)). pose bN (n:nat) : X := b_net (Nab n). pose cN (n:nat) : Y := snd (fst (abcgn (Na (Nab n)))). pose gN (n:nat) : CMap := snd (abcgn (Na (Nab n))). (********************) have gNaN_cN : forall n:nat, proj1_sig (gN n) (aN n) = (cN n) by move=> n; case: (Habcgn (Na (Nab n))). have gNbN_cN : forall n:nat, proj1_sig (gN n) (bN n) = (cN n) by move=> n; case: (Habcgn (Na (Nab n))) => _ []. have daNbN_r : forall n:nat, d (aN n) (bN n) >= r by move=> n; case: (Habcgn (Na (Nab n))) => _ [_ []]. have umfrgN : forall n:nat, um fr (gN n) < / INR (S n). { move=> n. apply: Rlt_le_trans (_ : / INR (S (Na (Nab n))) <= _); first by case: (Habcgn (Na (Nab n))) => _ [_ []]. apply: Rle_inv_INR_S_contravar. apply: le_trans (_ : (Nab n <= _)%nat); first by case: (H_Nab n). by case: (H_Na (Nab n)). } have dlimaaN: forall n:nat, d lim_a (aN n) < / INR (S n). { move=> n. apply: Rlt_le_trans (_ : / INR (S (Nab n)) <= _); first by case: (H_Na (Nab n)). apply: Rle_inv_INR_S_contravar. by case: (H_Nab n). } have dlimbbN: forall n:nat, d lim_b (bN n) < / INR (S n) by move=> n; case: (H_Nab n) => _ []. have d_metrizes: metrizes Xt d by apply: MetricTopology_metrizable. have d'_metrizes: metrizes Yt d' by apply: MetricTopology_metrizable. have frab: (proj1_sig fr lim_a) = (proj1_sig fr lim_b). { apply: (metric_strict _ d') => //. apply: Rle_antisym; last by apply: Rge_le; apply: metric_nonneg. apply: Rnot_gt_le. set eps := d' (proj1_sig fr lim_a) (proj1_sig fr lim_b). move=> eps_pos. case: (proj2_sig fr) => _ fr_conti. have fr_conti_at_a: forall epsa : R, epsa > 0 -> exists deltaa:R, deltaa > 0 /\ forall a': X, d lim_a a' < deltaa -> d' (proj1_sig fr lim_a) (proj1_sig fr a') < epsa. by apply: (metric_space_fun_continuity_converse Xt Yt _ _ d d') => //; apply: continuous_func_continuous_everywhere. have fr_conti_at_b: forall epsb : R, epsb > 0 -> exists deltab:R, deltab > 0 /\ forall b': X, d lim_b b' < deltab -> d' (proj1_sig fr lim_b) (proj1_sig fr b') < epsb. by apply: (metric_space_fun_continuity_converse Xt Yt _ _ d d') => //; apply: continuous_func_continuous_everywhere. case: (fr_conti_at_a (/4*eps)); first by fourier. move=> dela [dela_pos fr_conti_a] {fr_conti_at_a}. case: (fr_conti_at_b (/4*eps)); first by fourier. move=> delb [delb_pos fr_conti_b] {fr_conti_at_b}. pose del := Rmin dela delb. have [N [N_pos N_large]]: exists N:nat, (N > 0)%nat /\ /INR N < Rmin (/4*eps) del by apply: RationalsInReals.inverses_of_nats_approach_0; do !apply: Rmin_pos => //; fourier. have HinvN: / INR (S N) < / INR N. { apply: Rinv_lt_contravar. apply: Rmult_lt_0_compat; auto with *. exact: lt_INR. } suff: d' (proj1_sig fr lim_a) (proj1_sig fr lim_b) < eps by apply: Rlt_irrefl. rewrite (_ : eps = /4*eps + /4*eps + /4*eps + /4*eps); last by field. apply: Rle_lt_trans (_ : d' (proj1_sig fr lim_a) (proj1_sig fr (bN N)) + d' (proj1_sig fr (bN N)) (proj1_sig fr lim_b) < _); first by apply: triangle_inequality. apply: Rplus_lt_compat; last first. - rewrite metric_sym //. apply: fr_conti_b. apply: Rlt_le_trans (_ : del <= _); last by apply: Rmin_r. apply: Rlt_le_trans (_ : Rmin (/4*eps) del <= _ ); last by apply: Rmin_r. apply: Rlt_trans (_ : / INR N < _) => //. exact: Rlt_trans (_ : / INR (S N) < _). apply: Rle_lt_trans (_ : d' (proj1_sig fr lim_a) (proj1_sig (gN N) (bN N)) + d' (proj1_sig (gN N) (bN N)) (proj1_sig fr (bN N)) < _); first by apply: triangle_inequality. apply: Rplus_lt_compat; last first. - rewrite metric_sym //. apply: Rle_lt_trans (_ : um fr (gN N) < _); first by apply: Rle_d'_um. apply: Rlt_trans (_ : / INR (S N) < _) => //. apply: Rlt_trans (_ : / INR N < _) => //. apply: Rlt_le_trans (_ : Rmin (/ 4 * eps) del <= _) => //. exact: Rmin_l. apply: Rle_lt_trans (_ : d' (proj1_sig fr lim_a) (proj1_sig (gN N) (aN N)) + d' (proj1_sig (gN N) (aN N)) (proj1_sig (gN N) (bN N)) < _); first by apply: triangle_inequality. rewrite gNaN_cN gNbN_cN metric_zero // Rplus_0_r. apply: Rle_lt_trans (_ : d' (proj1_sig fr lim_a) (proj1_sig fr (aN N)) + d' (proj1_sig fr (aN N)) (cN N) < _); first by apply: triangle_inequality. apply: Rplus_lt_compat. - apply: fr_conti_a. apply: Rlt_le_trans (_ : del <= _); last by apply: Rmin_l. apply: Rlt_le_trans (_ : Rmin (/4*eps) del <= _ ); last by apply: Rmin_r. apply: Rlt_trans (_ : / INR N < _) => //. exact: Rlt_trans (_ : / INR (S N) < _). - rewrite -gNaN_cN. apply: Rle_lt_trans (_ : um fr (gN N) < _); first by apply: Rle_d'_um. apply: Rlt_trans (_ : / INR (S N) < _) => //. apply: Rlt_trans (_ : / INR N < _) => //. apply: Rlt_le_trans (_ : Rmin (/ 4 * eps) del <= _) => //. exact: Rmin_l. } have dlimalimb_r: d lim_a lim_b < r by apply: fr_in_W. pose eps2 := r - d lim_a lim_b. have eps2_pos: eps2 > 0. by apply: Rgt_minus. have [N [N_pos INR_e2]]: exists N:nat, (N > 0)%nat /\ / INR N < /2 * eps2. by apply: RationalsInReals.inverses_of_nats_approach_0; fourier. apply: Rlt_not_ge (daNbN_r N). have HinvSN: / INR (S N) < /2 * eps2. { apply: Rlt_trans (_ : / INR N < _) => //. apply: Rinv_lt_contravar. apply: Rmult_lt_0_compat; auto with *. exact: lt_INR. } rewrite (_ : r = /2 * eps2 + ((r - eps2) + /2 * eps2)); last by field. apply: Rle_lt_trans (_ : d (aN N) lim_a + d lim_a (bN N) < _); first by apply: triangle_inequality. apply: Rplus_lt_compat; first by rewrite (metric_sym _ d) //; apply: Rlt_trans (_ : / INR (S N) < _). apply: Rle_lt_trans (_ : d lim_a lim_b + d lim_b (bN N) < _); first by apply: triangle_inequality. apply: Rplus_le_lt_compat; last by apply: Rlt_trans (_ : / INR (S N) < _). by rewrite /eps2; fourier. Qed. (*** W_is_open is defined ***) Lemma bijection_complement: forall (Xt Yt:TopologicalSpace) (f: (point_set Xt) -> (point_set Yt)) (U: Ensemble (point_set Xt)), bijective f -> Complement (Im U f) = Im (Complement U) f. Proof. move=> XT YT f U [inj_f surj_f]. apply: Extensionality_Ensembles; split => y H_y. - case: (surj_f y) => x H. exists x => // In_U_x. apply: H_y. by exists x. - case Ey0: _ / H_y => [x Hx y1 Hy1]. case Ey1: _ / => [x0 Hx0 y2 Hy2]. apply: Hx. move: Ey1. rewrite Hy1 Hy2. by move/inj_f => ->. Qed. Lemma bij_conti_is_homeo_for_compact_Hausdorff_spaces: compact Xt -> compact Yt -> Hausdorff Xt -> Hausdorff Yt -> forall f: point_set Xt -> point_set Yt, bijective f -> continuous f -> homeomorphism f. Proof. move=> Xt_compact Yt_compact Xt_Hdf Yt_Hdf f f_bijective f_continuous. apply: invertible_open_map_is_homeomorphism => //; first by apply: bijective_impl_invertible. move=> U U_open. apply: closed_complement_open => //. apply: compact_closed => //. have CImUf: forall xP : {x: point_set Xt | In (Complement U) x}, Complement (Im U f) (f (proj1_sig xP)). { move=> [x Hx]. rewrite bijection_complement //. by exists x. } pose fP (xP: {x:point_set Xt | In (Complement U) x}) := exist (Complement (Im U f)) (f (proj1_sig xP)) (CImUf xP). apply: (@compact_image (SubspaceTopology (Complement U)) (SubspaceTopology (Complement (Im U f))) fP). - apply: closed_compact; first by apply: Xt_compact. by rewrite /= Complement_Complement. - move=> V V_open. have [W' [W'_open V_inv_W']]: exists W:Ensemble (point_set Yt), open W /\ V = inverse_image (@subspace_inc Yt (Complement (Im U f))) W by apply: subspace_topology_topology. have ->: inverse_image fP V = inverse_image (@subspace_inc Xt (Complement U)) (inverse_image f W') by apply: Extensionality_Ensembles; split; rewrite V_inv_W' => [? [[]]]. apply: subspace_inc_continuous. exact: f_continuous. - case: f_bijective => f_inj f_surj. move=> [y0' Hy0']. case: (f_surj y0') => x0 H. rewrite/fP. have InCUx0: In (Complement U) x0 by move=> InUx0; apply: Hy0'; exists x0. exists (exist _ _ InCUx0). exact: subset_eq_compatT. Qed. (*** bij_cont_is_homeo_for_compact_Hausdorff_spaces is defined ***) Definition approximable_by_homeos (f:X->Y): Prop:= forall eps:R, eps>0 -> exists h:point_set Xt -> point_set Yt, homeomorphism h /\ (forall x:X, d' (f x) (h x) < eps). Definition Bing_shrinkable (f:X->Y): Prop:= forall eps:R, eps>0 -> exists h : point_set Xt -> point_set Xt, homeomorphism h /\ (forall x:X, d' (f x) (f (h x)) < eps) /\ (forall x1 x2:X, (f x1) = (f x2) -> d (h x1) (h x2) < eps). Theorem Bing_Shrinking_Theorem: forall f: point_set Xt -> point_set Yt, continuous f -> surjective f -> (Bing_shrinkable f -> approximable_by_homeos f). Proof. move=> f f_conti f_surj f_BS eps eps_pos. have f_bdd_conti: bound (Im Full_set (fun x:X=> d' (y0 x) (f x)))/\ @continuous Xt Yt f by split =>//; apply: continuous_bounded. pose fP := exist (fun f: X->Y => bound (Im Full_set (fun x:X=> d' (y0 x) (f x))) /\ @continuous Xt Yt f) f f_bdd_conti. pose fH : Ensemble (point_set CMapt) := fun gP: CMap => exists hx: point_set Xt -> point_set Xt, homeomorphism hx /\ forall x: point_set Xt, (proj1_sig gP) x = f (hx x). have InfHfP: In fH fP by exists (id_map Xt); split=>//; apply: id_map_homeomorphism. pose CfH := closure fH. pose CfHt := SubspaceTopology CfH. (* Caution: point_set CfHt = { gP:CfH | In CfH gP } *) pose um_restriction (f1PP f2PP: point_set CfHt) := um (proj1_sig f1PP) (proj1_sig f2PP). have um_restriction_metric: metric um_restriction by apply: d_restriction_metric; apply: um_metric. have CfHt_baire: baire_space CfHt. { apply: (BaireCategoryTheorem _ um_restriction) => //; first by exact: d_restriction_metrizes. have um_is_metric: metric um by apply: um_metric. have um_is_complete: complete um um_metric by apply: um_complete; apply: compact_complete; exact: Y_compact. apply: closed_subset_of_complete_is_complete => //. exact: closure_closed. } pose Wn: IndexedFamily nat (point_set CfHt) := fun n:nat => inverse_image (subspace_inc CfH) (W (/INR (S n))). have WnOD: forall n:nat, open (Wn n) /\ dense (Wn n). { move=>n; split; first by apply: subspace_inc_continuous; apply: W_is_open; auto with *. (********************************************) apply: meets_every_nonempty_open_impl_dense. move=> U U_open [gPP InUgPP]. case: (subspace_topology_topology _ _ U) => // V [V_open U_iV]. have [r [r_pos IcOB_V]]: exists r:R, r>0 /\ Included (open_ball (point_set CMapt) um (proj1_sig gPP) r) V by apply: open_ball_in_open; move: InUgPP; by rewrite U_iV => -[]. have [fh0 h1_fh0]: Inhabited (Intersection fH (open_ball (point_set CMapt) um (proj1_sig gPP) (r*/2))). { apply: (closure_impl_meets_every_open_neighborhood _ _ (proj1_sig gPP)). - by case: (gPP). - by apply: open_ball_open; fourier. - constructor. have ->: um (proj1_sig gPP) (proj1_sig gPP) = 0 by apply: metric_zero; apply: um_metric. by fourier. } case: h1_fh0 => {fh0} fh0 [h0 [[h' h0_conti h'_conti h_h'h0 h_h0h'] h_fh0]] [umgPfh0]. pose eps1 := Rmin (r*/2) (/ INR (S n)). have [delta [pos_delta h_delta]]: exists delta:R, delta > 0 /\ forall x1 x2 : X, d x1 x2 < delta -> d (h' x1) (h' x2) < eps1. { apply: dist_uniform_on_compact => //. apply: Rmin_pos; by [fourier | auto with *]. } case: (f_BS (Rmin delta (r*/2))); first by apply: Rmin_pos => //; fourier. move=> k [[k' k_conti k'_conti h_k'k h_kk'] [h1_k h2_k]]. pose k'h (x: point_set Xt) := k' (h0 x). pose h'k (x: point_set Xt) := h' (k x). pose fk'h (x: point_set Xt) := f (k'h x). have k'h_homeo: homeomorphism k'h. { exists h'k. - exact: continuous_composition. - exact: continuous_composition. - by move=> x; rewrite /h'k /k'h h_kk' h_h'h0. - by move=> x; rewrite /k'h /h'k h_h0h' h_k'k. } have fk'h_conti: continuous fk'h by apply: continuous_composition => //; exact: continuous_composition. have fk'h_bdd_conti: bound (Im Full_set (fun x:X => d' (y0 x) (fk'h x))) /\ continuous fk'h by split=> //; exact: continuous_bounded. pose fk'hP := exist (fun f: X->Y => bound (Im Full_set (fun x:X=> d' (y0 x) (f x)))/\ @continuous Xt Yt f) fk'h fk'h_bdd_conti. have InCfHfk'hP: In CfH fk'hP by apply: closure_inflationary; exists k'h; split. pose fk'hPP := exist (fun f0P: point_set CMapt => In CfH f0P) fk'hP InCfHfk'hP. exists fk'hPP; split. - constructor => x1 x2 fk'hx1_fk'hx2. rewrite /= /fk'h in fk'hx1_fk'hx2. have: d (k (k'h x1)) (k (k'h x2)) < delta by apply: Rlt_le_trans (_ : Rmin delta (r * / 2) <= _); [apply: h2_k | apply: Rmin_l]. rewrite /k'h !h_kk' => /h_delta. rewrite !h_h'h0 => /Rlt_le_trans. apply. apply: Rmin_r. - rewrite U_iV. constructor. rewrite /fk'hPP /=. apply: IcOB_V. constructor. apply: Rle_lt_trans (_ : um (proj1_sig gPP) fh0 + um fh0 fk'hP < _); first by apply: triangle_inequality; by apply: um_metric. rewrite (_ : r = r * /2 + r* /2); last by field. apply: Rplus_lt_le_compat => //. apply: Rle_um_all_d'; first by fourier. move=> x. rewrite h_fh0 /= /fk'h /k'h -[X in d' (f X)]h_kk' metric_sym //. apply: Rlt_le. apply: Rlt_le_trans (_ : Rmin delta (r * / 2) <= _); first by apply: h1_k. exact: Rmin_r. } have IWn_dense: dense (IndexedIntersection Wn) by apply: CfHt_baire; apply: WnOD. have InCfHfP: In CfH fP. by apply: closure_inflationary. pose fPP := exist (fun gP : CMap => In CfH gP) fP InCfHfP. pose OBeps := open_ball CMap um fP eps. have [hPP H_h]: Inhabited (Intersection (IndexedIntersection Wn) (inverse_image (subspace_inc CfH) OBeps)). { apply: dense_meets_every_nonempty_open. - exact: IWn_dense. - apply: subspace_inc_continuous. exact: open_ball_open. - exists fPP. constructor. constructor. rewrite metric_zero //. exact: um_metric. } case: H_h => {hPP} _ [hPP Wn_h] [OB_h]. (***) pose hP := proj1_sig hPP. pose h := proj1_sig hP. case: (proj2_sig hP) => _. rewrite -/h => c_h. move: OB_h. rewrite /subspace_inc -/hP => -[umfh]. exists h. split. apply: bij_conti_is_homeo_for_compact_Hausdorff_spaces => //. - exact: MetricTopology_Hausdorff. - exact: MetricTopology_Hausdorff. - split. + move=> x1 x2 hx1_hx2. apply: (metric_strict _ d) => //. apply: Rle_antisym; last by apply: Rge_le; apply: metric_nonneg. apply: Rnot_gt_le => H. case: (inverses_of_nats_approach_0 (d x1 x2)) => // n [n_pos]. apply: Rle_not_gt. case: (Wn_h n). rewrite /W /In /subspace_inc -/hP -/h => h_Wn. apply: Rlt_le. apply: Rlt_trans (_ : / INR (S n) < _); first by apply: h_Wn. apply: Rinv_lt_contravar. apply: Rmult_lt_0_compat; auto with *. exact: lt_INR. + move=> y. apply: NNPP => h_nx. have InCImhy: In (Complement (Im Full_set h)) y. { case E: _ / => [x InXx y' y_hx]. apply: h_nx. exists x. by rewrite E y_hx. } have CImh_open: open (@Complement (point_set Yt) (Im Full_set h)). { apply: closed_complement_open. rewrite Complement_Complement. apply: compact_closed; first by apply: MetricTopology_Hausdorff. have h_img: forall x:point_set Xt, In (Im Full_set h) (h x) by move=>x; exists x. pose hf := @continuous_factorization Xt Yt h (Im Full_set h) h_img. apply: (compact_image hf) => //; first by apply: factorization_is_continuous. case=> _ [x1 InF_x y1 y1_hx1]. exists x1. exact: subset_eq_compatT. } (*******************************) have [r [r_pos IncObCImh]]: exists r:R, r > 0 /\ Included (open_ball (point_set Yt) d' y r) (Complement (Im Full_set h)) by apply: open_ball_in_open. have fH_hP_r: Inhabited ( Intersection fH (open_ball (point_set CMapt) um hP r)). { apply: (closure_impl_meets_every_open_neighborhood _ _ hP). - rewrite /hP. by case: (proj2_sig hPP). - exact: open_ball_open. - constructor. rewrite metric_zero //. by apply: um_metric. } case: fH_hP_r => _ [fh1 [h1 [h1_homeo fh1_f_h1]] [umhPfh1_r]]. have [x1 y_fh1x1]: exists x1:point_set Xt, y = f (h1 x1). { case: h1_homeo => h1' h1_cont h1'_cont h1'h1 h1h1'. case: (f_surj y) => x2 fx2_y. exists (h1' x2). by rewrite h1h1'. } have InObyr_hx1: In (open_ball (point_set Yt) d' y r) (h x1). { constructor. rewrite /h y_fh1x1 -fh1_f_h1 metric_sym //. apply: Rle_lt_trans (_ : um hP fh1 < _) => //. exact: Rle_d'_um. } have: In (Complement (Im Full_set h)) (h x1) by apply: IncObCImh. apply. by exists x1. - move=> x. rewrite -[f]/(proj1_sig fP) /h. apply: Rle_lt_trans (_ : um fP hP < _) => //. exact: Rle_d'_um. Qed. (* Bing_Shrinking_Theorem *) End BingShrinkingTheorem.
import tactic.linarith example {Ξ± : Type} (_inst : Ξ  (a : Prop), decidable a) [linear_ordered_field Ξ±] {a b c : Ξ±} (ha : a < 0) (hb : Β¬b = 0) (hc' : c = 0) (h : (1 - a) * (b * b) ≀ 0) (hc : 0 ≀ 0) (this : -(a * -b * -b + b * -b + 0) = (1 - a) * (b * b)) (h : (1 - a) * (b * b) ≀ 0) : 0 < 1 - a := begin linarith end example (e b c a v0 v1 : β„š) (h1 : v0 = 5*a) (h2 : v1 = 3*b) (h3 : v0 + v1 + c = 10) : v0 + 5 + (v1 - 3) + (c - 2) = 10 := by linarith example (u v r s t : β„š) (h : 0 < u*(t*v + t*r + s)) : 0 < (t*(r + v) + s)*3*u := by linarith example (A B : β„š) (h : 0 < A * B) : 0 < 8*A*B := begin linarith end example (A B : β„š) (h : 0 < A * B) : 0 < A*8*B := begin linarith end example (A B : β„š) (h : 0 < A * B) : 0 < A*B/8 := begin linarith end example (A B : β„š) (h : 0 < A * B) : 0 < A/8*B := begin linarith end example (Ξ΅ : β„š) (h1 : Ξ΅ > 0) : Ξ΅ / 2 + Ξ΅ / 3 + Ξ΅ / 7 < Ξ΅ := by linarith example (x y z : β„š) (h1 : 2*x < 3*y) (h2 : -4*x + z/2 < 0) (h3 : 12*y - z < 0) : false := by linarith example (Ξ΅ : β„š) (h1 : Ξ΅ > 0) : Ξ΅ / 2 < Ξ΅ := by linarith example (Ξ΅ : β„š) (h1 : Ξ΅ > 0) : Ξ΅ / 3 + Ξ΅ / 3 + Ξ΅ / 3 = Ξ΅ := by linarith example (a b c : β„š) (h2 : b + 2 > 3 + b) : false := by linarith {discharger := `[ring]} example (a b c : β„š) (h2 : b + 2 > 3 + b) : false := by linarith example (a b c : β„š) (x y : β„€) (h1 : x ≀ 3*y) (h2 : b + 2 > 3 + b) : false := by linarith {restrict_type := β„š} example (g v V c h : β„š) (h1 : h = 0) (h2 : v = V) (h3 : V > 0) (h4 : g > 0) (h5 : 0 ≀ c) (h6 : c < 1) : v ≀ V := by linarith constant nat.prime : β„• β†’ Prop example (x y z : β„š) (h1 : 2*x + ((-3)*y) < 0) (h2 : (-4)*x + 2*z < 0) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : β„š) (h1 : 2*1*x + (3)*(y*(-1)) < 0) (h2 : (-2)*x*2 < -(z + z)) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : β„€) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : β„€) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : β„€) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) : Β¬ 12*y - 4* z < 0 := by linarith example (w x y z : β„€) (h1 : 4*x + (-3)*y + 6*w ≀ 0) (h2 : (-1)*x < 0) (h3 : y < 0) (h4 : w β‰₯ 0) (h5 : nat.prime x.nat_abs) : false := by linarith example (a b c : β„š) (h1 : a > 0) (h2 : b > 5) (h3 : c < -10) (h4 : a + b - c < 3) : false := by linarith example (a b c : β„š) (h2 : b > 0) (h3 : Β¬ b β‰₯ 0) : false := by linarith example (a b c : β„š) (h2 : (2 : β„š) > 3) : a + b - c β‰₯ 3 := by linarith {exfalso := ff} example (x : β„š) (hx : x > 0) (h : x.num < 0) : false := by linarith [rat.num_pos_iff_pos.mpr hx, h] example (x : β„š) (hx : x > 0) (h : x.num < 0) : false := by linarith only [rat.num_pos_iff_pos.mpr hx, h] example (x y z : β„š) (hx : x ≀ 3*y) (h2 : y ≀ 2*z) (h3 : x β‰₯ 6*z) : x = 3*y := by linarith example (x y z : β„•) (hx : x ≀ 3*y) (h2 : y ≀ 2*z) (h3 : x β‰₯ 6*z) : x = 3*y := by linarith example (x y z : β„š) (hx : Β¬ x > 3*y) (h2 : Β¬ y > 2*z) (h3 : x β‰₯ 6*z) : x = 3*y := by linarith example (h1 : (1 : β„•) < 1) : false := by linarith example (a b c : β„š) (h2 : b > 0) (h3 : b < 0) : nat.prime 10 := by linarith example (a b c : β„•) : a + b β‰₯ a := by linarith example (a b c : β„•) : Β¬ a + b < a := by linarith example (x y : β„š) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) (h' : (x + 4) * x β‰₯ 0) (h'' : (6 + 3 * y) * y β‰₯ 0) : false := by linarith example (x y : β„š) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3 ∧ (x + 4) * x β‰₯ 0 ∧ (6 + 3 * y) * y β‰₯ 0) : false := by linarith example (a b i : β„•) (h1 : Β¬ a < i) (h2 : b < i) (h3 : a ≀ b) : false := by linarith example (n : β„•) (h1 : n ≀ 3) (h2 : n > 2) : n = 3 := by linarith example (z : β„•) (hz : Β¬ z β‰₯ 2) (h2 : Β¬ z + 1 ≀ 2) : false := by linarith example (z : β„•) (hz : Β¬ z β‰₯ 2) : z + 1 ≀ 2 := by linarith example (a b c : β„š) (h1 : 1 / a < b) (h2 : b < c) : 1 / a < c := by linarith example (N : β„•) (n : β„•) (Hirrelevant : n > N) (A : β„š) (l : β„š) (h : A - l ≀ -(A - l)) (h_1 : Β¬A ≀ -A) (h_2 : Β¬l ≀ -l) (h_3 : -(A - l) < 1) : A < l + 1 := by linarith example (d : β„š) (q n : β„•) (h1 : ((q : β„š) - 1)*n β‰₯ 0) (h2 : d = 2/3*(((q : β„š) - 1)*n)) : d ≀ ((q : β„š) - 1)*n := by linarith example (d : β„š) (q n : β„•) (h1 : ((q : β„š) - 1)*n β‰₯ 0) (h2 : d = 2/3*(((q : β„š) - 1)*n)) : ((q : β„š) - 1)*n - d = 1/3 * (((q : β„š) - 1)*n) := by linarith example (a : β„š) (ha : 0 ≀ a) : 0 * 0 ≀ 2 * a := by linarith example (x : β„š) : id x β‰₯ x := by success_if_fail {linarith}; linarith! example (x y z : β„š) (hx : x < 5) (hx2 : x > 5) (hy : y < 5000000000) (hz : z > 34*y) : false := by linarith only [hx, hx2] example (x y z : β„š) (hx : x < 5) (hy : y < 5000000000) (hz : z > 34*y) : x ≀ 5 := by linarith only [hx] example (x y : β„š) (h : x < y) : x β‰  y := by linarith example (x y : β„š) (h : x < y) : Β¬ x = y := by linarith example (u v x y A B : β„š) (a : 0 < A) (a_1 : 0 <= 1 - A) (a_2 : 0 <= B - 1) (a_3 : 0 <= B - x) (a_4 : 0 <= B - y) (a_5 : 0 <= u) (a_6 : 0 <= v) (a_7 : 0 < A - u) (a_8 : 0 < A - v) : u * y + v * x + u * v < 3 * A * B := by nlinarith example (u v x y A B : β„š) : (0 < A) β†’ (A ≀ 1) β†’ (1 ≀ B) β†’ (x ≀ B) β†’ ( y ≀ B) β†’ (0 ≀ u ) β†’ (0 ≀ v ) β†’ (u < A) β†’ ( v < A) β†’ (u * y + v * x + u * v < 3 * A * B) := begin intros, nlinarith end example (a b c z : β„š) (_ : a ≀ z) (E0 : b ≀ c) (E1 : c ≀ a) (E2 : 0 ≀ c) : b ≀ a + c := by linarith example (u v x y A B : β„š) (a_7 : 0 < A - u) (a_8 : 0 < A - v) : (0 ≀ A * (1 - A)) β†’ (0 ≀ A * (B - 1)) β†’ (0 < A * (A - u)) β†’ (0 ≀ (B - 1) * (A - u)) β†’ (0 ≀ (B - 1) * (A - v)) β†’ (0 ≀ (B - x) * v) β†’ (0 ≀ (B - y) * u) β†’ (0 ≀ u * (A - v)) β†’ u * y + v * x + u * v < 3 * A * B := begin intros, linarith end example (u v x y A B : β„š) (a : 0 < A) (a_1 : 0 <= 1 - A) (a_2 : 0 <= B - 1) (a_3 : 0 <= B - x) (a_4 : 0 <= B - y) (a_5 : 0 <= u) (a_6 : 0 <= v) (a_7 : 0 < A - u) (a_8 : 0 < A - v) : (0 < A * A) -> (0 <= A * (1 - A)) -> (0 <= A * (B - 1)) -> (0 <= A * (B - x)) -> (0 <= A * (B - y)) -> (0 <= A * u) -> (0 <= A * v) -> (0 < A * (A - u)) -> (0 < A * (A - v)) -> (0 <= (1 - A) * A) -> (0 <= (1 - A) * (1 - A)) -> (0 <= (1 - A) * (B - 1)) -> (0 <= (1 - A) * (B - x)) -> (0 <= (1 - A) * (B - y)) -> (0 <= (1 - A) * u) -> (0 <= (1 - A) * v) -> (0 <= (1 - A) * (A - u)) -> (0 <= (1 - A) * (A - v)) -> (0 <= (B - 1) * A) -> (0 <= (B - 1) * (1 - A)) -> (0 <= (B - 1) * (B - 1)) -> (0 <= (B - 1) * (B - x)) -> (0 <= (B - 1) * (B - y)) -> (0 <= (B - 1) * u) -> (0 <= (B - 1) * v) -> (0 <= (B - 1) * (A - u)) -> (0 <= (B - 1) * (A - v)) -> (0 <= (B - x) * A) -> (0 <= (B - x) * (1 - A)) -> (0 <= (B - x) * (B - 1)) -> (0 <= (B - x) * (B - x)) -> (0 <= (B - x) * (B - y)) -> (0 <= (B - x) * u) -> (0 <= (B - x) * v) -> (0 <= (B - x) * (A - u)) -> (0 <= (B - x) * (A - v)) -> (0 <= (B - y) * A) -> (0 <= (B - y) * (1 - A)) -> (0 <= (B - y) * (B - 1)) -> (0 <= (B - y) * (B - x)) -> (0 <= (B - y) * (B - y)) -> (0 <= (B - y) * u) -> (0 <= (B - y) * v) -> (0 <= (B - y) * (A - u)) -> (0 <= (B - y) * (A - v)) -> (0 <= u * A) -> (0 <= u * (1 - A)) -> (0 <= u * (B - 1)) -> (0 <= u * (B - x)) -> (0 <= u * (B - y)) -> (0 <= u * u) -> (0 <= u * v) -> (0 <= u * (A - u)) -> (0 <= u * (A - v)) -> (0 <= v * A) -> (0 <= v * (1 - A)) -> (0 <= v * (B - 1)) -> (0 <= v * (B - x)) -> (0 <= v * (B - y)) -> (0 <= v * u) -> (0 <= v * v) -> (0 <= v * (A - u)) -> (0 <= v * (A - v)) -> (0 < (A - u) * A) -> (0 <= (A - u) * (1 - A)) -> (0 <= (A - u) * (B - 1)) -> (0 <= (A - u) * (B - x)) -> (0 <= (A - u) * (B - y)) -> (0 <= (A - u) * u) -> (0 <= (A - u) * v) -> (0 < (A - u) * (A - u)) -> (0 < (A - u) * (A - v)) -> (0 < (A - v) * A) -> (0 <= (A - v) * (1 - A)) -> (0 <= (A - v) * (B - 1)) -> (0 <= (A - v) * (B - x)) -> (0 <= (A - v) * (B - y)) -> (0 <= (A - v) * u) -> (0 <= (A - v) * v) -> (0 < (A - v) * (A - u)) -> (0 < (A - v) * (A - v)) -> u * y + v * x + u * v < 3 * A * B := begin intros, linarith end example (A B : β„š) : (0 < A) β†’ (1 ≀ B) β†’ (0 < A / 8 * B) := begin intros, nlinarith end example (x y : β„š) : 0 ≀ x ^2 + y ^2 := by nlinarith example (x y : β„š) : 0 ≀ x*x + y*y := by nlinarith example (x y : β„š) : x = 0 β†’ y = 0 β†’ x*x + y*y = 0 := by intros; nlinarith lemma norm_eq_zero_iff {x y : β„š} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 := begin split, { intro, split; nlinarith }, { intro, nlinarith } end lemma norm_nonpos_right {x y : β„š} (h1 : x * x + y * y ≀ 0) : y = 0 := by nlinarith lemma norm_nonpos_left (x y : β„š) (h1 : x * x + y * y ≀ 0) : x = 0 := by nlinarith variables {E : Type*} [add_group E] example (f : β„€ β†’ E) (h : 0 = f 0) : 1 ≀ 2 := by nlinarith example (a : E) (h : a = a) : 1 ≀ 2 := by nlinarith -- test that the apply bug doesn't affect linarith preprocessing constant Ξ± : Type variable [fact false] -- we work in an inconsistent context below def leΞ± : Ξ± β†’ Ξ± β†’ Prop := Ξ» a b, βˆ€ c : Ξ±, true noncomputable instance : linear_ordered_field Ξ± := by refine_struct { le := leΞ± }; exact (fact.out false).elim example (a : Ξ±) (ha : a < 2) : a ≀ a := by linarith example (p q r s t u v w : β„•) (h1 : p + u = q + t) (h2 : r + w = s + v) : p * r + q * s + (t * w + u * v) = p * s + q * r + (t * v + u * w) := by nlinarith -- Tests involving a norm, including that squares in a type where `sq_nonneg` does not apply -- do not cause an exception variables {R : Type*} [ring R] (abs : R β†’ β„š) lemma abs_nonneg' : βˆ€ r, 0 ≀ abs r := (fact.out false).elim example (t : R) (a b : β„š) (h : a ≀ b) : abs (t^2) * a ≀ abs (t^2) * b := by nlinarith [abs_nonneg' abs (t^2)] example (t : R) (a b : β„š) (h : a ≀ b) : a ≀ abs (t^2) + b := by linarith [abs_nonneg' abs (t^2)] example (t : R) (a b : β„š) (h : a ≀ b) : abs t * a ≀ abs t * b := by nlinarith [abs_nonneg' abs t] constant T : Type attribute [instance] constant T_zero : ordered_ring T namespace T lemma zero_lt_one : (0 : T) < 1 := (fact.out false).elim lemma works {a b : β„•} (hab : a ≀ b) (h : b < a) : false := begin linarith, end end T example (a b c : β„š) (h : a β‰  b) (h3 : b β‰  c) (h2 : a β‰₯ b) : b β‰  c := by linarith {split_ne := tt} example (a b c : β„š) (h : a β‰  b) (h2 : a β‰₯ b) (h3 : b β‰  c) : a > b := by linarith {split_ne := tt} example (x y : β„š) (h₁ : 0 ≀ y) (hβ‚‚ : y ≀ x) : y * x ≀ x * x := by nlinarith example (x y : β„š) (h₁ : 0 ≀ y) (hβ‚‚ : y ≀ x) : y * x ≀ x ^ 2 := by nlinarith axiom foo {x : int} : 1 ≀ x β†’ 1 ≀ x*x lemma bar (x y: int)(h : 0 ≀ y ∧ 1 ≀ x) : 1 ≀ y + x*x := by linarith [foo h.2] -- issue #9822 lemma mytest (j : β„•) (h : 0 < j) : j-1 < j:= begin linarith, end
Formal statement is: lemma closure_UNIV [simp]: "closure UNIV = UNIV" Informal statement is: The closure of the entire space is the entire space.
[STATEMENT] lemma ClassMain_is_no_class [dest!]: "is_class (PROG P) (ClassMain P) \<Longrightarrow> False" [PROOF STATE] proof (prove) goal (1 subgoal): 1. is_class (PROG P) (ClassMain P) \<Longrightarrow> False [PROOF STEP] proof (erule rev_notE) [PROOF STATE] proof (state) goal (1 subgoal): 1. \<not> False \<Longrightarrow> \<not> is_class (PROG P) (ClassMain P) [PROOF STEP] from ex_new_class_name [of "PROG P"] [PROOF STATE] proof (chain) picking this: \<exists>C. \<not> is_class (PROG P) C [PROOF STEP] obtain C where "\<not> is_class (PROG P) C" [PROOF STATE] proof (prove) using this: \<exists>C. \<not> is_class (PROG P) C goal (1 subgoal): 1. (\<And>C. \<not> is_class (PROG P) C \<Longrightarrow> thesis) \<Longrightarrow> thesis [PROOF STEP] by blast [PROOF STATE] proof (state) this: \<not> is_class (PROG P) C goal (1 subgoal): 1. \<not> False \<Longrightarrow> \<not> is_class (PROG P) (ClassMain P) [PROOF STEP] thus "\<not> is_class (PROG P) (ClassMain P)" [PROOF STATE] proof (prove) using this: \<not> is_class (PROG P) C goal (1 subgoal): 1. \<not> is_class (PROG P) (ClassMain P) [PROOF STEP] unfolding ClassMain_def [PROOF STATE] proof (prove) using this: \<not> is_class (PROG P) C goal (1 subgoal): 1. \<not> is_class (PROG P) (SOME Name. \<not> is_class (PROG P) Name) [PROOF STEP] by (rule someI) [PROOF STATE] proof (state) this: \<not> is_class (PROG P) (ClassMain P) goal: No subgoals! [PROOF STEP] qed
Shaina received her Bachelors of Arts in Psychology from Rutgers Newark in 2014. Thereafter, she moved on to attend Kean University, Nathan Weiss Graduate College where she earned her Masters of Arts in Psychology in 2016. In May of 2017 she completed a professional Diploma in Marriage & Family Therapy. Shaina is currently working towards becoming a fully Licensed Marriage & Family Therapist. Shaina believes in the ideas of Family Systems Theory and that in order to fully understand a client, it is essential to see how they interact with their environment and family unit. She loves family therapy because she feels working collaboratively with the family not only becomes an enriching and fulfilling journey for them, but can also aid in the healing process. Some of her favorite books include: The Family Crucible, Manic, Awareness and The Diary of an Oxygen Thief . She looks forward to completing her clinical hours at Integrated Care Concepts & Consultation and expanding her understanding of families and couples therapy.
example (h : βˆ€ x, x + 1 = x.succ) : (x + 1) + 1 = 1 + (x + 1) := by rewrite [h] done
From Fiat.Narcissus Require Import Examples.TutorialPrelude. Record msg := { data : list (word 8) }. Definition format := format_nat 8 β—¦ length β—¦ data ++ format_list format_word β—¦ data. Definition invariant (_ : msg) := True. Definition dec : CorrectAlignedDecoderFor invariant format. Proof. synthesize_aligned_decoder. Defined.
module Rewrite where open import Common.Equality data _β‰ˆ_ {A : Set}(x : A) : A β†’ Set where refl : βˆ€ {y} β†’ x β‰ˆ y lem : βˆ€ {A} (x y : A) β†’ x β‰ˆ y lem x y = refl thm : {A : Set}(P : A β†’ Set)(x y : A) β†’ P x β†’ P y thm P x y px rewrite lem x y = {!!}
For every $c \geq 0$, there exists a vector $y$ such that $||x - y|| = c$.
lemma continuous_on_setdist [continuous_intros]: "continuous_on T (\<lambda>y. (setdist {y} S))"
[STATEMENT] theorem pick_eqv [intro, simp]: "pick \<lfloor>a\<rfloor> \<sim> (a::'a::equiv)" [PROOF STATE] proof (prove) goal (1 subgoal): 1. pick \<lfloor>a\<rfloor> \<sim> a [PROOF STEP] proof (rule pick_eqv') [PROOF STATE] proof (state) goal (1 subgoal): 1. a \<in> domain [PROOF STEP] show "a \<in> domain" [PROOF STATE] proof (prove) goal (1 subgoal): 1. a \<in> domain [PROOF STEP] .. [PROOF STATE] proof (state) this: a \<in> domain goal: No subgoals! [PROOF STEP] qed
import Pkg Pkg.add(path="https://github.com/jmurga/MKtest.jl") Pkg.add(["Fire","IJulia","Parameters", "SparseArrays", "Distributed", "CSV", "JLD2", "DataFrames", "ProgressMeter", "Quadmath", "GZip", "StatsBase", "Unzip", "ThreadsX","Roots","NLsolve","SpecialFunctions","PoissonRandom","Distributions","Parsers","OrderedCollections","FastaIO","Random","LsqFit","HypothesisTests"])
lemmas scaleR_right_imp_eq = real_vector.scale_right_imp_eq
# Run the code below to set up the data # Add theme_minimal() to the plot. # Increase the font size of the title. # Bonus: What else can you change? library(tidyverse) # Load and reshape data unisex_data <- read_csv("data/unisex.csv") unisex_data <- unisex_data %>% select(-prop) %>% spread(sex, n, fill=0) unisex_data # Add theme_minimal() to the plot. # Increase the font size of the title. # See what other theme elements you can change # Look up the help for theme to see the options ggplot(unisex_data, aes(x=Male, y=Female, color=name)) + geom_point() + geom_smooth(method="lm", se=FALSE) + labs(title="Unisex Name Popularity", x="# of Male Babies per Year", y="# of Female Babies per Year", color="Name")
import data.set import data.int.basic def odd : β„€ β†’ Prop := Ξ» n, βˆƒ m, n = 2 * m + 1 def even : β„€ β†’ Prop := Ξ» n, βˆƒ m, n = 2 * m theorem even_or_odd : βˆ€ n, (even n ∨ odd n) := begin assume n, apply @int.induction_on (Ξ» n, even n ∨ odd n), left, exact ⟨0, rfl⟩, assume k, assume h, cases h, right, apply exists.elim h, assume m, assume f, rw f, exact ⟨m, rfl⟩, left, apply exists.elim h, assume m, assume f, rw f, apply exists.intro (m + 1), calc 2 * m + 1 + 1 = 2 * m + (1 + 1) : by simp ... = 2 * m + 2 * 1 : by trivial ... = 2 * (m + 1) : by rw mul_add, assume k h, cases h, right, apply exists.elim h, assume m, assume f, rw f, apply exists.intro (m - 1), apply eq.symm, calc 2 * (m - 1) + 1 = 2 * m - 2 * 1 + 1 : by rw mul_sub ... = 2 * m - (2 * 1 - 1) : by simp, left, apply exists.elim h, assume m, assume f, rw f, apply exists.intro m, calc 2 * m + 1 - 1 = 2 * m + (1 - 1) : by rw add_sub_assoc ... = 2 * m : by simp, end example : βˆ€ n, Β¬ (even n ∧ odd n) := begin assume n, apply @int.induction_on (Ξ» n, Β¬ (even n ∧ odd n)), assume h, cases h.2 with _ pf, cases w, repeat {cases pf}, assume k h, assume h1, cases h1 with evenk oddk, have : even k ∧ odd k, split, apply exists.elim oddk, assume m eq1, let := congr_arg (Ξ» n: β„€, n - 1) eq1, simp at this, apply exists.intro m, rw this, simp [add_comm (2*m) (-1), add_assoc], apply exists.elim evenk, assume m, assume eq1, apply exists.intro (m - 1), let := congr_arg (Ξ» n: β„€, n - 1) eq1, simp at this, rw this, apply eq.symm, calc 2 * (m - 1) + 1 = 2 * m - 2 * 1 + 1 : by rw mul_sub ... = -(2-1) + 2 * m : by simp, contradiction, assume k h, assume h1, cases h1, have : even k ∧ odd k, split, apply exists.elim h1_right, assume m eq1, let := congr_arg (Ξ» n: β„€, n + 1) eq1, simp at this, apply exists.intro (m + 1), rw this, calc 1 + (1 + 2 * m) = 1 + 1 + 2 * m : by rw add_assoc ... = 2 + 2 * m : by trivial ... = 2 * m + 2 * 1 : by simp ... = 2 * (m + 1) : by rw mul_add, apply exists.elim h1_left, assume m eq1, let := congr_arg (Ξ» n: β„€, n + 1) eq1, simp at this, have eq2 : 1 + 2 * m = 2 * m + 1 := by simp, exact ⟨m, eq.trans this eq2⟩, contradiction, end
(** * Testcases for [take.v] Authors: - Jim Portegies Creation date: 31 Oct 2021 Testcases for the [simplify_chains] tactic. Tests pass if they can be run without unhandled errors. -------------------------------------------------------------------------------- This file is part of Waterproof-lib. Waterproof-lib is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Waterproof-lib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Waterproof-lib. If not, see <https://www.gnu.org/licenses/>. *) From Ltac2 Require Import Ltac2. From Ltac2 Require Option. Require Import Waterproof.message. Require Import Waterproof.test_auxiliary. Require Import Waterproof.definitions.inequality_chains. Load simplify_chains. Require Import Reals. Require Import micromega.Lra. Open Scope R_scope. (** Test 0: Go from a chain of inequalities to the statement *) Goal forall x : R, (& x < 4 <= 5 = 2 + 3 < 10) -> (x < 10). intro x. intro H. Fail ltac1:(lra). (* at this stage, lra does not work yet *) simpl_ineq_chains (). ltac1:(lra). Qed. Goal forall X : Type, forall (a b c d e f g h : X), (& a = b = c) -> (& c = d = e = f) -> (& f = g = h) -> a = h. intro X. intros a b c d e f g h. intros chain_1 chain_2 chain_3. Fail ltac1:(congruence). (* at this stage, congruence does not work yet *) simpl_ineq_chains (). Fail ltac1:(congruence). (* at this stage, congruence still does not work *) split_conjunctions (). ltac1:(congruence). (* now congruence should work *) Qed.
module TwoVarDiophEq import ZZ import ZZUtils import Divisors import GCDZZ import decDivZ %access public export %default total |||Any integer is a solution for c=xa+yb when a=b=c=0 zeroLinCombZeroZero:a=0->b=0->c=0->(x:ZZ)->(y:ZZ)->c=x*a+y*b zeroLinCombZeroZero az bz cz x y = rewrite az in rewrite bz in rewrite cz in rewrite multZeroRightZeroZ x in rewrite multZeroRightZeroZ y in Refl |||If a=b=0 and c is not zero, it is impossible that c = xa +yb notZeroNotLinCombZeroZero:a=0->b=0->NotZero c -> (x:ZZ)->(y:ZZ)->c=x*a+y*b->Void notZeroNotLinCombZeroZero aZ bZ cnotz x y = rewrite aZ in rewrite bZ in rewrite multZeroRightZeroZ x in rewrite multZeroRightZeroZ y in (notZeroNonZero cnotz) |||Proves that if d = gcd (a,b) and c= xa +yb , then d|c gcdDivLinComb:GCDZ a b d->c=x*a+y*b -> IsDivisibleZ c d gcdDivLinComb (dPos,dcommonfactab,fd) prf = linCombDivLeftWithProof prf dcommonfactab gcdDivLinCombContra:GCDZ a b d ->((IsDivisibleZ c d) ->Void)->(x:ZZ)->(y:ZZ)->c=x*a+y*b->Void gcdDivLinCombContra gcdpf f x y prf = f (gcdDivLinComb gcdpf prf) |||Proves that if d = gcd (a,b) and d|c, then there exists integers x and y |||such that c = xa +yb multipleOfGcdLinComb:GCDZ a b d -> IsDivisibleZ c d -> (x:ZZ**y:ZZ** (c = x*a + y*b)) multipleOfGcdLinComb{a} {d}{b} gcdpf (n**eqpf) = (case checkNotBothZero a b of (Left (aZ, bZ)) => void (gcdZeroZeroContra (gcdzReplace aZ bZ gcdpf)) (Right r) => (case gcdIsLinComb gcdpf of (j**l**gcdlc) => ((n*j)**(n*l)** (rewrite sym $multAssociativeZ n j a in rewrite sym $multAssociativeZ n l b in rewrite sym $ multDistributesOverPlusRightZ n (j*a) (l*b) in rewrite eqpf in rewrite multCommutativeZ d n in cong gcdlc) ))) |||Extracts a/gcd(a,b) from the definition of GCDZ aByd:GCDZ a b d ->ZZ aByd dGcdab = (fst (fst (fst (snd dGcdab)))) |||Extracts b/gcd(a,b) from the definition of GCDZ bByd:GCDZ a b d ->ZZ bByd dGcdab = (fst (snd (fst (snd dGcdab)))) |||Proves that (b/gcd(a,b))*a = (a/gcd(a,b))*b divByGcdMultByOtherIsSame:(gcdpf:GCDZ a b d) ->(bByd gcdpf)*a =(aByd gcdpf)*b divByGcdMultByOtherIsSame {a}{b}{d} (dPos, ((abyd**apf),(bbyd**bpf)),fd) = multLeftCancelZ d (bbyd*a) (abyd *b) (posThenNotZero dPos) (rewrite multAssociativeZ d bbyd a in rewrite multAssociativeZ d abyd b in rewrite sym $ bpf in rewrite sym $ apf in rewrite multCommutativeZ b a in Refl) |||Proves that for any integer k, k*(-b/(gcd(a,b))) and k*(a/(gcd(a,b))) |||are solutions of 0 = xa + yb homoSolution:(gcdpf:GCDZ a b d)->{k:ZZ}-> (0 = (k*(-(bByd gcdpf)))*a + (k*((aByd gcdpf)))*b) homoSolution {a}{b}{d}{k} (dPos, ((abyd**apf),(bbyd**bpf)),fd) = rewrite sym $ multAssociativeZ k (-bbyd) a in rewrite sym $ multAssociativeZ k (abyd) b in rewrite sym $ multDistributesOverPlusRightZ k ((-bbyd)*a) ((abyd)*b) in rewrite multNegateLeftZ bbyd a in rewrite divByGcdMultByOtherIsSame (dPos, ((abyd**apf),(bbyd**bpf)),fd) in rewrite plusNegateInverseRZ (abyd*b) in rewrite multZeroRightZeroZ k in Refl |||Proves that if 0 = xa +yb, then (-b/gcd(a,b)))*y = (a/(gcd(a,b)))*x divHomoEqByGcd:(gcdpf:GCDZ a b d)->(0 = x*a + y*b)->((-(bByd gcdpf))*y = (aByd gcdpf)*x) divHomoEqByGcd {x}{y}{a}{b}{d}(dPos, ((abyd**apf),(bbyd**bpf)),fd) prf = rewrite sym $plusZeroLeftNeutralZ ((-bbyd)*y) in rewrite multNegateLeftZ bbyd y in subOnBothSides 0 (abyd*x) ((bbyd)*y) (multLeftCancelZ d 0 ((abyd*x)+(bbyd*y)) (posThenNotZero dPos) (rewrite multZeroRightZeroZ d in rewrite multDistributesOverPlusRightZ d (abyd*x) (bbyd*y) in rewrite multAssociativeZ d abyd x in rewrite multAssociativeZ d bbyd y in rewrite sym $ apf in rewrite sym $ bpf in rewrite multCommutativeZ a x in rewrite multCommutativeZ b y in prf)) |||Proves that if 0 = xa + yb then there exists an integer k such that ||| y = k* (a/(gcd(a,b))) homoOnlySolnForY:(gcdpf:GCDZ a b d)->(0 = x*a + y*b) -> (k:ZZ**(y = k * (aByd gcdpf))) homoOnlySolnForY{a}{b}{d}{x}{y} (dPos, ((abyd**apf),(bbyd**bpf)),fd) prf = (case divHomoEqByGcd (dPos, ((abyd**apf),(bbyd**bpf)),fd) prf of eqpf => (case caCoprimeThencDivb (x**eqpf) (negatingPreservesGcdLeft1 (gcdSymZ (divideByGcdThenGcdOne (dPos, ((abyd**apf),(bbyd**bpf)),fd)))) of (quot**divpf) => (quot** (rewrite multCommutativeZ quot abyd in divpf)))) |||Proves that if a is not zero then a/(gcd(a,b)) is not zero divByGcdNotZero:(gcdpf:(GCDZ a b d))->NotZero a ->NotZero (aByd gcdpf) divByGcdNotZero {a = (Pos (S k))}{b = b}{d = d} (dPos, ((abyd**apf),(bbyd**bpf)),fd) PositiveZ = posThenNotZero (posDivByPosIsPos{c=(Pos (S k))} Positive dPos apf) divByGcdNotZero {a = (NegS k)}{b = b}{d = d} (dPos, ((abyd**apf),(bbyd**bpf)),fd) NegativeZ = NegSThenNotZero (negDivByPosIsNeg Negative dPos apf) -- The arguments to the function homoOnlySoln need to be explicit because Idris will not -- infer the correct arguments in the functions below. |||Proves that if a is not zero and 0 =xa + yb, then there exists an integer k such that ||| x = k* (-b/(gcd(a,b))) and y = k* (a/(gcd(a,b))) homoOnlySoln:(x: ZZ) -> (y: ZZ) -> (gcdpf:GCDZ a b d)->(0 = x*a + y*b) ->NotZero a-> (k:ZZ**((x = k * (-(bByd gcdpf))),(y = k * (aByd gcdpf)))) homoOnlySoln {a}{b}{d} x y (dPos, ((abyd**apf),(bbyd**bpf)),fd) prf anotz = (case divHomoEqByGcd (dPos, ((abyd**apf),(bbyd**bpf)),fd) prf of divgpf => (case homoOnlySolnForY (dPos, ((abyd**apf),(bbyd**bpf)),fd) prf of (k**adivy) => (k**((multLeftCancelZ abyd x (k*(-bbyd)) (divByGcdNotZero (dPos, ((abyd**apf),(bbyd**bpf)),fd) anotz) (rewrite multAssociativeZ abyd k (-bbyd) in rewrite multCommutativeZ abyd k in rewrite sym $ adivy in rewrite multCommutativeZ y (-bbyd) in sym $ divgpf)),adivy)))) gcdSymZwithproof:(gcdpf:GCDZ a b d)->(gcdpf2:(GCDZ b a d)**((aByd gcdpf)=(bByd gcdpf2),(bByd gcdpf)=(aByd gcdpf2))) gcdSymZwithproof (dPos, ((abyd**apf),(bbyd**bpf)),fd) = ((dPos, ((bbyd**bpf),(abyd**apf)),(genFunctionForGcdSym fd))**(Refl,Refl)) |||Same as homoOnlySolution, NotZero a is replaced with NotBothZeroZ a b homoOnlySolnGen:(x: ZZ) -> (y: ZZ) -> (gcdpf:GCDZ a b d)->(0 = x*a + y*b) ->NotBothZeroZ a b-> (k:ZZ**((x = k * (-(bByd gcdpf))),(y = k * (aByd gcdpf)))) homoOnlySolnGen {a = (Pos (S k))}{b = b}{d = d} x y gcdpf prf LeftPositive = homoOnlySoln x y gcdpf prf PositiveZ homoOnlySolnGen {a = (NegS k)}{b = b}{d = d} x y gcdpf prf LeftNegative = homoOnlySoln x y gcdpf prf NegativeZ homoOnlySolnGen {a = a}{b = (Pos (S k))}{d = d} x y (dPos, ((abyd**apf),(bbyd**bpf)),fd) prf RightPositive = (case gcdSymZwithproof ((dPos, ((abyd**apf),(bbyd**bpf)),fd)) of (gcdpf2**(eqpf1,eqpf2)) => (case homoOnlySoln {a=(Pos (S k))}{b=a} {d=d} y x gcdpf2 (rewrite plusCommutativeZ (y*(Pos (S k))) (x*a) in prf ) PositiveZ of (j**(ypf,xpf)) => ((-j)**((rewrite multNegNegNeutralZ j bbyd in rewrite eqpf2 in xpf ),(rewrite multNegateLeftZ j abyd in rewrite sym $ multNegateRightZ j abyd in rewrite eqpf1 in ypf))))) homoOnlySolnGen {a = a}{b = (NegS k)}{d = d} x y (dPos, ((abyd**apf),(bbyd**bpf)),fd) prf RightNegative = (case gcdSymZwithproof ((dPos, ((abyd**apf),(bbyd**bpf)),fd)) of (gcdpf2**(eqpf1,eqpf2)) => (case homoOnlySoln {a=(NegS k)}{b=a} {d=d} y x gcdpf2 (rewrite plusCommutativeZ (y*(NegS k)) (x*a) in prf ) NegativeZ of (j**(ypf,xpf)) => ((-j)**((rewrite multNegNegNeutralZ j bbyd in rewrite eqpf2 in xpf ),(rewrite multNegateLeftZ j abyd in rewrite sym $ multNegateRightZ j abyd in rewrite eqpf1 in ypf))))) -- The goal of the following section is to show that the non-homogeneous equation is uniquely solved by the family of -- solutions ((x_p+k*x_0), (y_p+k*y_0)). ||| Produces the difference of two solutions. It will used to show that the difference of two particular ||| solutions satisfies the homogeneous equation. solDifference: (a: ZZ) -> (b: ZZ) -> (c: ZZ) -> (x1: ZZ) -> (y1: ZZ) -> (x2: ZZ) -> (y2: ZZ) -> (c=(x1*a+y1*b)) -> (c=(x2*a+y2*b)) -> ( 0= ( ((x1-x2)*a) + ((y1-y2)*b) )) solDifference a b c x1 y1 x2 y2 prf prf1 = rewrite (multDistributesOverPlusLeftZ (x1) (-x2) (a)) in rewrite (multDistributesOverPlusLeftZ (y1) (-y2) (b)) in rewrite sym (plusAssociativeZ (x1*a) ((-x2)*a) (y1*b+((-y2)*b)) ) in rewrite (plusCommutativeZ (y1*b) ((-y2)*b)) in rewrite (plusAssociativeZ ((-x2)*a) ((-y2)*b) (y1*b)) in rewrite (plusCommutativeZ (((-x2)*a) + ((-y2)*b)) (y1*b)) in rewrite (plusAssociativeZ (x1*a) (y1*b) (((-x2)*a) + ((-y2)*b))) in rewrite (multNegateLeftZ (x2) (a)) in rewrite (multNegateLeftZ (y2) (b)) in rewrite sym (negateDistributesPlus (x2*a) (y2*b)) in rewrite sym (plusNegateInverseLZ (x2*a+y2*b)) in rewrite sym prf in rewrite sym prf1 in rewrite (plusNegateInverseLZ c) in Refl ||| Adding x2 to both sides of the equation: addToSol: (x1: ZZ) -> (x2: ZZ) -> (x1-x2=d) -> (x1=x2+d) addToSol x1 x2 prf = rewrite sym prf in rewrite (plusCommutativeZ (x1) (-x2)) in rewrite (plusAssociativeZ (x2) (-x2) (x1)) in rewrite (plusNegateInverseLZ (x2)) in rewrite (plusZeroLeftNeutralZ (x1)) in Refl ||| Proves that two particular solutions differ by a solution of the homogeneous equation. diffIsHomogeneous: {a: ZZ} -> {b: ZZ} -> {c: ZZ} -> {d: ZZ} -> {x1: ZZ} -> {y1: ZZ} -> {x2: ZZ} -> {y2: ZZ} -> (IsDivisibleZ c d) -> (gcdpf:GCDZ a b d) -> NotBothZeroZ a b -> (c=x1*a+y1*b) -> (c=x2*a+y2*b) -> (k:ZZ** (( (x1-x2) = (k * (-(bByd gcdpf)))),( (y1-y2) = (k * (aByd gcdpf))))) diffIsHomogeneous {a}{b}{c}{d}{x1}{y1}{x2}{y2} x gcdpf abnotZ prf prf1 = homoOnlySolnGen {a}{b}{d} (x1-x2) (y1-y2) (gcdpf) (solDifference a b c x1 y1 x2 y2 prf prf1) abnotZ ||| Proves that any solution is a particular solution plus a constant multiple of the solution of ||| the homogeneous equation. differByHomogeneous: {a: ZZ} -> {b: ZZ} -> {c: ZZ} -> {d: ZZ} -> {x1: ZZ} -> {y1: ZZ} -> (IsDivisibleZ c d) -> (gcdpf:GCDZ a b d) -> NotBothZeroZ a b -> (c=x1*a+y1*b) ->(x2:ZZ)->(y2:ZZ)-> (c=x2*a+y2*b) -> (k:ZZ** (( x2 = x1 + (k * (-(bByd gcdpf)))),( y2 = y1 + (k * (aByd gcdpf))))) differByHomogeneous {x1}{y1} x gcdpf y prf x2 y2 prf1 = (case diffIsHomogeneous x gcdpf y prf1 prf of (k**(xpf,ypf)) => (k**((addToSol x2 x1 xpf),(addToSol y2 y1 ypf)))) |||A helper function for allsolutions. |||It proves that all x and y of the given form satisfies the equation. helpallsolutions:(gcdpf:(GCDZ a b d))->(bbyd*a=abyd*b)->c = x1*a+y1*b->{k:ZZ}-> (x3:ZZ)->(y3:ZZ)->(x3=x1+k*(-bbyd))->(y3=y1+k*abyd)->(c=(x3)*a+(y3)*b) helpallsolutions{bbyd}{abyd} {x1}{y1}{a}{b}{d}{c}{k} gcdpf bydpf eqpf x3 y3 xpf ypf = rewrite xpf in rewrite ypf in rewrite multDistributesOverPlusLeftZ x1 (k*(-bbyd)) a in rewrite sym $ plusAssociativeZ (x1*a) ((k*(-bbyd))*a) (multZ (plusZ y1 (multZ k abyd)) b) in rewrite plusCommutativeZ ((k*(-bbyd))*a) ((y1+ (k*abyd))*b) in rewrite plusAssociativeZ (x1*a) ((y1+ (k*abyd))*b) ((k*(-bbyd))*a) in rewrite multDistributesOverPlusLeftZ y1 (k*abyd) b in rewrite plusAssociativeZ (x1*a) (y1*b) ((k*abyd)*b) in rewrite sym $ eqpf in rewrite sym $ plusAssociativeZ c ((k*abyd)*b) ((k*(-bbyd))*a) in rewrite sym $ multAssociativeZ k abyd b in rewrite sym $ multAssociativeZ k (-bbyd) a in rewrite sym $ bydpf in rewrite sym $ multDistributesOverPlusRightZ k (bbyd*a) ((-bbyd)*a) in rewrite multNegateLeftZ bbyd a in rewrite plusNegateInverseLZ (bbyd*a) in rewrite multZeroRightZeroZ k in rewrite plusZeroRightNeutralZ c in Refl |||The function that generates the third case in findAllSolutions function. allSolutions:(gcdpf:(GCDZ a b d)) -> IsDivisibleZ c d ->NotBothZeroZ a b -> (x1:ZZ**y1:ZZ**pa:ZZ**pb:ZZ**(({k:ZZ}->(x3:ZZ)->(y3:ZZ)->(x3=x1+k*pa)->(y3=y1+k*pb)->(c=x3*a+y3*b)), ((x2:ZZ)->(y2:ZZ)->(c=x2*a+y2*b)->(k**((x2=x1+k*pa),(y2=y1+k*pb)))))) allSolutions{a}{b}{d} (dPos, ((abyd**apf),(bbyd**bpf)),fd) dDivc abnotZ = (case ((multipleOfGcdLinComb (dPos, ((abyd**apf),(bbyd**bpf)),fd) dDivc), (divByGcdMultByOtherIsSame (dPos, ((abyd**apf),(bbyd**bpf)),fd))) of ((x1**y1**eqpf),bydpf) =>(x1**y1**(-bbyd)**abyd**( (helpallsolutions (dPos, ((abyd**apf),(bbyd**bpf)),fd) bydpf eqpf ), ( (differByHomogeneous dDivc (dPos, ((abyd**apf),(bbyd**bpf)),fd) abnotZ eqpf ) )))) |||Given three integers a, b and c, it outputs either |||a proof that c = xa +yb is impossible or |||a proof that all integers x and y satisfy the equation (this happens when a=b=c=0) |||or 4 integers x1 , y1 , pa and pb such that for any integer k, |||x=x1+k*pa y=y1+k*pb is a solution of c=xa+yb |||and whenever c=xa+yb ,there exists an integer, k such that ||| x=x1+k*pa y=y1+k*pb findAllSolutions: (a:ZZ)->(b:ZZ)->(c:ZZ)-> Either ((x:ZZ)->(y:ZZ)->c=x*a+y*b->Void) (Either ((x:ZZ)->(y:ZZ)->c=x*a+y*b) (x1:ZZ**y1:ZZ**pa:ZZ**pb:ZZ**(({k:ZZ}->(x:ZZ)->(y:ZZ)->(x=x1+k*pa)->(y=y1+k*pb)->(c=x*a+y*b)), ((x:ZZ)->(y:ZZ)->(c=x*a+y*b)->(k**((x=x1+k*pa),(y=y1+k*pb))))))) findAllSolutions a b c = (case checkNotBothZero a b of (Left (aZ,bZ)) => (case decZero c of (Yes cnotz) => Left (notZeroNotLinCombZeroZero aZ bZ cnotz) (No ciszero) => Right (Left (zeroLinCombZeroZero aZ bZ (notNotZeroThenZero ciszero)))) (Right abnotZ) => (case gcdZZ a b abnotZ of (g**gcdpf) => (case decDivisibleZ c g of (Yes prf) => Right (Right (allSolutions gcdpf prf abnotZ )) (No contra) => Left (gcdDivLinCombContra gcdpf contra ))))
lemma emeasure_lborel_box_eq: "emeasure lborel (box l u) = (if \<forall>b\<in>Basis. l \<bullet> b \<le> u \<bullet> b then \<Prod>b\<in>Basis. (u - l) \<bullet> b else 0)"
import ircbot.types ircbot.support open types support namespace modules.ping_pong def ping_pong_func (input : irc_text) : list irc_text := match input with | irc_text.parsed_normal { object := some ~nick!ident, type := message.privmsg, args := [subject], text := "\\ping" } := let new_subject := if subject.front = '#' then subject else nick in [privmsg new_subject $ sformat! "{nick}, pong"] | _ := [] end /-- For testing: send β€œpong” after β€œ\ping”. -/ def ping_pong : bot_function := { name := "ping-pong", syntax := some "\\ping", description := "ping-pong game!", func := pure ∘ ping_pong_func } theorem ping_pong_is_correct_on_channel (nick ident subject: string) (on_channel : subject.front = '#') : (ping_pong_func $ irc_text.parsed_normal { object := some ~nick!ident, type := message.privmsg, args := [subject], text := "\\ping" }) = [privmsg subject $ sformat! "{nick}, pong"] := begin intros, simp [ping_pong_func], rw [on_channel], trivial end theorem ping_pong_is_correct_on_priv (nick ident subject bot_nickname : string) (bot_nickname_is_correct : bot_nickname.front β‰  '#') (not_on_channel : subject = bot_nickname): (ping_pong_func $ irc_text.parsed_normal { object := some ~nick!ident, type := message.privmsg, args := [bot_nickname], text := "\\ping" }) = [privmsg nick $ sformat! "{nick}, pong"] := begin intros, simp [privmsg], simp [ping_pong_func], simp [privmsg], simp [bot_nickname_is_correct] end end modules.ping_pong
If $f$ is small compared to $g$, then $f$ is in $l F (g)$.
! PR tree-optimization/70043 ! { dg-do compile } ! { dg-additional-options "-Ofast -g" } ! { dg-additional-options "-march=haswell" { target i?86-*-* x86_64-*-* } } subroutine fn1(a, b) real(8), intent(in) :: b(100) real(8), intent(inout) :: a(100) real(8) c do i=1,100 if( a(i) < 0.0 ) then c = a(i) * b(i) a(i) = a(i) - c / b(i) endif enddo end subroutine fn1
State Before: Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal ⊒ a < ord c β†’ derivFamily f a < ord c State After: Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ a < ord c β†’ derivFamily f a < ord c Tactic: have hΟ‰ : β„΅β‚€ < c.ord.cof := by rw [hc.cof_eq] exact lt_of_le_of_ne hc.1 hc'.symm State Before: Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ a < ord c β†’ derivFamily f a < ord c State After: case H₁ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ 0 < ord c β†’ derivFamily f 0 < ord c case Hβ‚‚ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ βˆ€ (o : Ordinal), (o < ord c β†’ derivFamily f o < ord c) β†’ succ o < ord c β†’ derivFamily f (succ o) < ord c case H₃ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ βˆ€ (o : Ordinal), Ordinal.IsLimit o β†’ (βˆ€ (o' : Ordinal), o' < o β†’ o' < ord c β†’ derivFamily f o' < ord c) β†’ o < ord c β†’ derivFamily f o < ord c Tactic: apply a.limitRecOn State Before: Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal ⊒ β„΅β‚€ < Ordinal.cof (ord c) State After: Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal ⊒ β„΅β‚€ < c Tactic: rw [hc.cof_eq] State Before: Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal ⊒ β„΅β‚€ < c State After: no goals Tactic: exact lt_of_le_of_ne hc.1 hc'.symm State Before: case H₁ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ 0 < ord c β†’ derivFamily f 0 < ord c State After: case H₁ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ 0 < ord c β†’ nfpFamily f 0 < ord c Tactic: rw [derivFamily_zero] State Before: case H₁ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ 0 < ord c β†’ nfpFamily f 0 < ord c State After: no goals Tactic: exact nfpFamily_lt_ord_lift hΟ‰ (by rwa [hc.cof_eq]) hf State Before: Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ lift (#ΞΉ) < Ordinal.cof (ord c) State After: no goals Tactic: rwa [hc.cof_eq] State Before: case Hβ‚‚ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ βˆ€ (o : Ordinal), (o < ord c β†’ derivFamily f o < ord c) β†’ succ o < ord c β†’ derivFamily f (succ o) < ord c State After: case Hβ‚‚ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : b < ord c β†’ derivFamily f b < ord c hb' : succ b < ord c ⊒ derivFamily f (succ b) < ord c Tactic: intro b hb hb' State Before: case Hβ‚‚ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : b < ord c β†’ derivFamily f b < ord c hb' : succ b < ord c ⊒ derivFamily f (succ b) < ord c State After: case Hβ‚‚ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : b < ord c β†’ derivFamily f b < ord c hb' : succ b < ord c ⊒ nfpFamily f (succ (derivFamily f b)) < ord c Tactic: rw [derivFamily_succ] State Before: case Hβ‚‚ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : b < ord c β†’ derivFamily f b < ord c hb' : succ b < ord c ⊒ nfpFamily f (succ (derivFamily f b)) < ord c State After: no goals Tactic: exact nfpFamily_lt_ord_lift hΟ‰ (by rwa [hc.cof_eq]) hf ((ord_isLimit hc.1).2 _ (hb ((lt_succ b).trans hb'))) State Before: Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : b < ord c β†’ derivFamily f b < ord c hb' : succ b < ord c ⊒ lift (#ΞΉ) < Ordinal.cof (ord c) State After: no goals Tactic: rwa [hc.cof_eq] State Before: case H₃ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) ⊒ βˆ€ (o : Ordinal), Ordinal.IsLimit o β†’ (βˆ€ (o' : Ordinal), o' < o β†’ o' < ord c β†’ derivFamily f o' < ord c) β†’ o < ord c β†’ derivFamily f o < ord c State After: case H₃ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : Ordinal.IsLimit b H : βˆ€ (o' : Ordinal), o' < b β†’ o' < ord c β†’ derivFamily f o' < ord c hb' : b < ord c ⊒ derivFamily f b < ord c Tactic: intro b hb H hb' State Before: case H₃ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : Ordinal.IsLimit b H : βˆ€ (o' : Ordinal), o' < b β†’ o' < ord c β†’ derivFamily f o' < ord c hb' : b < ord c ⊒ derivFamily f b < ord c State After: case H₃ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : Ordinal.IsLimit b H : βˆ€ (o' : Ordinal), o' < b β†’ o' < ord c β†’ derivFamily f o' < ord c hb' : b < ord c ⊒ (bsup b fun a x => derivFamily f a) < ord c Tactic: rw [derivFamily_limit f hb] State Before: case H₃ Ξ± : Type ?u.164801 r : Ξ± β†’ Ξ± β†’ Prop ΞΉ : Type u f : ΞΉ β†’ Ordinal β†’ Ordinal c : Cardinal hc : IsRegular c hΞΉ : lift (#ΞΉ) < c hc' : c β‰  β„΅β‚€ hf : βˆ€ (i : ΞΉ) (b : Ordinal), b < ord c β†’ f i b < ord c a : Ordinal hΟ‰ : β„΅β‚€ < Ordinal.cof (ord c) b : Ordinal hb : Ordinal.IsLimit b H : βˆ€ (o' : Ordinal), o' < b β†’ o' < ord c β†’ derivFamily f o' < ord c hb' : b < ord c ⊒ (bsup b fun a x => derivFamily f a) < ord c State After: no goals Tactic: exact bsup_lt_ord_of_isRegular.{u, v} hc (ord_lt_ord.1 ((ord_card_le b).trans_lt hb')) fun o' ho' => H o' ho' (ho'.trans hb')
# take input from the user n = as.integer(readline(prompt="Enter Number of Terms ")) # first two terms a = 0 b = 1 count = 2 # check if the number of terms is valid if(n <= 0) { print("Plese enter a positive integer") } else { if(n == 1) { print("Fibonacci sequence:") print(a) } else { print("Fibonacci sequence:") print(a) print(b) while(count < n) { c = a + b print(c) # update values a = b b = c count = count + 1 } } }
State Before: x : ℝ hx : 0 ≀ x y : ℝ ⊒ sqrt (x / y) = sqrt x / sqrt y State After: no goals Tactic: rw [division_def, sqrt_mul hx, sqrt_inv, division_def]
/* * This file is part of the ProVANT simulator project. * Licensed under the terms of the MIT open source license. More details at * https://github.com/Guiraffo/ProVANT-Simulator/blob/master/LICENSE.md */ /** * @file sdf_parser.h * @brief This file contains the declaration of the SDFParser class. * * @author JΓΊnio Eduardo de Morais Aquino */ #ifndef PROVANT_SDF_READER_H #define PROVANT_SDF_READER_H #include <sdf/sdf.hh> #include <gsl/gsl> #include "sdf_status.h" /** * @brief The SDFParser class is a helper class to read properties from a SDF * Element object. * * This class is specially usefull during the loading of Gazebo plugins where * is necessary to read parameters from the plugin SDF description. * * A SDF (Simulation Description Format) Element, is a specialized XML element * that may contain attributes and child elements, as any other XML element * can. * * Gazebo provides a SDF Reader, but many times it has a confusing syntax and * very lax documentation, which is very hard to understand and discourages the * usage of the SDF class to read its own elements. * * As there were many instances in the ProVANT Simulator source code that * users and developers were using the now depreacted XMLRead class to read the * values of SDF elements, this class was designed as a substitution for the * XMLRead class, that wraps the methods provided by gazebo into well documented * and tested methods. * * So far, this class provides methods to check the existence, and returns the * string values of attributes and elements. In the future, it is very likely * that this class will also contain methods to read the values of elements in * other types, checking for adequate conversion and validity. * * @todo It turns out that is really hard to create SDF elements in memory * in order to do the testing of this class, so by now THIS CLASS IS UNTESTED! * It is imperative that we develop tests for this class. The tests are not * hard to create, the problem is that it creating SDF descriptions in memory * throws a series of undocumented errors and leads the test executable to a * segmentation fault, rendering the test procedure unfeasible. */ class SDFParser { public: /** * @brief Construct a new SDFParser object and initializes the SDF pointer. * * @param sdf SDF element to read data from. */ SDFParser(sdf::ElementPtr sdf); /** * @brief Destroy the SDFParser object. * Releases the shared pointer to the SDF element. */ virtual ~SDFParser(); /** * @brief Get the SDF ptr from which this class reads data from. * @return sdf::ElementPtr */ virtual sdf::ElementPtr GetElementPtr() const; /** * @brief Checks if the SDF contain an attribute with the specified name. * * Attributes are data specific to a element, that are usually strings. * For example, the following XML element: * @code{.xml} * <test type="example"> * <other_data/> * </test> * @endcode * * Contains an attribute named type and with value example. * * Usage example: * Suppose a plugin SDF description as follows: * @code{.xml} * <plugin name="example" filename="provant_simulator_step_plugin"> * <plugin_data/> * </plugin> * @endcode * * Assuming this plugin was read, the following code has res=true, because * the plugin contains an attribute "name". * @code{.cpp} * SDFParser reader(_sdf); * bool res = reader.HasAttribute("name"); * @endcode * * And the following code results in res=false, because the plugin does not * contain an attribute "type". * @code{.cpp} * SDFParser reader(_sdf); * bool res = reader.HasAttribute("type"); * @endcode * * @param name * @return true If the SDF element contains an attribute with the specified * name. * @return false Otherwise. */ virtual bool HasAttribute(const std::string& name) const; /** * @brief Checks if the SDF has an element with the specified name. * * Usage example: * Suppose a plugin SDF description as follows: * @code{.xml} * <plugin name="example" filename="provant_simulator_step_plugin"> * <plugin_data/> * </plugin> * @endcode * * Assuming this plugin was read, the following code has res=true, because * the plugin contains an element "plugin_data". * @code{.cpp} * SDFParser reader(_sdf); * bool res = reader.HasElement("plugin_data"); * @endcode * * And the following code results in res=false, because the plugin does not * contain an attribute "type". * @code{.cpp} * SDFParser reader(_sdf); * bool res = reader.HasElement("type"); * @endcode * * @param name Name of the element to look for. * @return true If the SDF contains an element with the specified name. * @return false Otherwise. */ virtual bool HasElement(const std::string& name) const; /** * @brief Get an attribute value. * * This checks if an attribute with the specified value exists, and if it * exists, copies its value to the value parameter. * * If the attribute does not exists, or any other occurs, an SDFStatus with an * error and a message describing the problem is returned, and the value is * left emtpy. * * @param name Name of the attribute to return. * @param value Buffer string that will contain the value of the attribute with the specified name. * @return SDFStatus that informs the status of the operation. If the * attribute is found and returned, a OkStatus object is returned, in the case * the attribute does not exist an AttributeNotFoundError object is returned, * containing an error status and a message informing the problem. */ virtual SDFStatus GetAttributeValue(const std::string& name, gsl::not_null<std::string*> value) const noexcept; /** * @brief Get an attribute value. * * This method checks if an attribute with the specified name exists, and if * it exists, returns its value. * * @param name Name of the attribute to locate. * @return std::string Value of the attribute. * @throw AttributeNotFoundError If an attribute with the specifed name does * not exist. */ virtual std::string GetAttributeValue(const std::string& name) const; /** * @brief Get the text value of a SDF element. * * Check if an element with the specified name exists, and if it exists, * copies its value to the value parameter. * * If an element with the specified name does not exist, or any other * error occurs, the value string is left empty, and a SDFStatus error is * returned with a message identifying the problem. * * @param name Name of the element to read the value. * @param value Buffer string that will contain the value of the element * @return SDFStatus that informs the status of the operation. If at least one * element with the specified name exists, a OkStatus object is returned. * In case an error occurs, an object identifying the problem is returned * with an errors status and a message describing the problem. */ virtual SDFStatus GetElementText(const std::string& name, gsl::not_null<std::string*> value) const noexcept; /** * @brief Get the text value of a SDF element. * * Check if an element with the specified name exists, and return is value. * * @sa GetElementText(const std::string& name, std::string* value) * * @param name Name of the element to read the value. * @return std::string Content of the element. * @throw ElementNotFoundError if the element cannot be found as a child of * the SDF element this class reads from. */ virtual std::string GetElementText(const std::string& name) const; /** * @brief Get the value of a boolean SDF element. * * The valid options are: * For true elements: true in any case combination, or 1. * For false elements: false in any case combinatation, or 0. * * @sa GetElementBool(const std::string&) * * @param name Name of the element to read. * @param value Content of the element. * @return SDFStatus Status of the parse operation. If the element was found * and had a valid value, an OKStatus is returned, otherwise an object * containing and error status and error message is returned. */ virtual SDFStatus GetElementBool(const std::string& name, gsl::not_null<bool*> value) const noexcept; /** * @brief Get the value of a boolean SDF element. * * The valid options are: * For true elements: true in any case combination, or 1. * For false elements: false in any case combinatation, or 0. * * @sa GetElementBool(const std::string&, gsl::not_null<int*>) * * @param name Name of the element to read. * @return Value of the element. * @throw SDFStatus exception in case the element is not found or has * invalid content, ie. content that cannot be parsed as a boolean. */ virtual bool GetElementBool(const std::string& name) const; /** * @brief Get the value of a int SDF element. * * This method can read integer values in the binary (0b), octal (0), decimal * (no prefix), or hexadecimal (0x) formats, identified by their prefixes. * It can also parse signed values with either a - or + signal. * * @sa GetElementInt(const std::string&) * * @param name Name of the element to read. * @param value Value of the element. * @return SDFStatus Status of the parse operation. If the element was found * and had a valid value, an OKStatus is returned, otherwise an object * containing and error status and error message is returned. */ virtual SDFStatus GetElementInt(const std::string& name, gsl::not_null<int*> value) const noexcept; /** * @brief Get the value of a int SDF element. * * This method can read integer values in the binary (0b), octal (0), decimal * (no prefix), or hexadecimal (0x) bases, identified by their prefixes. * It can also parse signed values with either a - or + signal. * * @sa GetElementInt(const std::string&, gsl::not_null<int*>) * * @param name Name of the element to read. * @return int Value of the element. * @throw SDFStatus exception in case the element is not found or has * invalid content, ie. content that cannot be parsed as an integer such as * string, or if the value of the element is beyond the current platform limits * for an integer value. */ virtual int GetElementInt(const std::string& name) const; /** * @brief Get the value of an unsigned int SDF element. * * This method can read unsigned integer values in the binary (0b), octal(0), * decimal (no prefix) or hexadecimal (0x) bases. * In case this methods find a signed integer, it returns an error. * * @sa GetElementUnsignedInt(const std::string&) * * @param name Name of the element to read. * @param value Value of the element. * @return SDFStatus Status of the parse operation. If the element was found * and had a valid value, an OKStatus is returned, otherwise an object * containing and error status and error message is returned. */ virtual SDFStatus GetElementUnsignedInt(const std::string& name, gsl::not_null<unsigned int*> value) const noexcept; /** * @brief Get the value of an unsigned int SDF element. * * This method can read unsigned integer values in the binary (0b), octal(0), * decimal (no prefix) or hexadecimal (0x) bases. * In case this methods find a signed integer, it throws an exception. * * @sa GetElementUnsignedInt(const std::string&, gsl::not_null<unsigned int*>) * * @param name Name of the element to read. * @return unsigned int Value of the element. * @throws SDFStatus exception in case the element is not found or has * invalid content, ie. content that cannot be parsed as an integer such as * string, if the value is signed, or if the value is beyond the limits of an * unsigned integer for the current platform. */ virtual unsigned int GetElementUnsignedInt(const std::string& name) const; /** * @brief Get the value of a single precision (float) SDF Element. * * This method can read real values as single precision floating point values * (float). It is also possible to read floats specified in the hexadecimal * format with the 0x prefix. * * @sa GetElementFloat(const std::string&) * * @param name Name of the element to read. * @param value Value of the element. * @return SDFStatus Status of the parse operation. If the element was found * and had a valid value, an OKStatus is returned, otherwise an object * containing and error status and error message is returned. */ virtual SDFStatus GetElementFloat(const std::string& name, gsl::not_null<float*> value) const noexcept; /** * @brief Get the value of a single precision (float) SDF Element. * * This method can read real values as single precision floating point values * (float). It is also possible to read floats specified in the hexadecimal * format with the 0x prefix. * * @sa GetElementFloat(const std::string&, gsl::not_null<float*>) * * @param name Name of the element to read. * @return float Value of the element. * @throws SDFStatus exception in case the element is not found or has * invalid content, ie. content that cannot be parsed as a float or is beyond * the limits of a float for the current platform. */ virtual float GetElementFloat(const std::string& name) const; /** * @brief Get the value of a double precision floating point (double) SDF Element. * * This method can read real values as double precision floating point values * (double). It is also possible to read doubles specified in the hexadecimal * format with the 0x prefix. * * @sa GetElementDouble(const std::string&) * * @param name Name of the element to read. * @param value Value of the element. * @return SDFStatus Status of the parse operation. If the element was found * and had a valid value, an OKStatus is returned, otherwise an object * containing and error status and error message is returned. */ virtual SDFStatus GetElementDouble(const std::string& name, gsl::not_null<double*> value) const noexcept; /** * @brief Get the value of a double precision floating point (double) SDF Element. * * This method can read real values as double precision floating point values * (double). It is also possible to read doubles specified in the hexadecimal * format with the 0x prefix. * * @sa GetElementDouble(const std::string&, gsl::not_null<double*>) * * @param name Name of the element to read. * @return double Value of the element. * @throws SDFStatus exception in case the element is not found or has * invalid content, ie. content that cannot be parsed as a double or is beyond * the limits of a double for the current platform. */ virtual double GetElementDouble(const std::string& name) const; private: //! Stores the pointer of the SDF from which this class reads the values of attributes and elements. sdf::ElementPtr _sdf; }; #endif // PROVANT_SDF_READER_H
theory Automation imports Main begin lemma "\<forall>x. \<exists>y. x = y" by auto lemma "A \<subseteq> B \<inter> C \<Longrightarrow> A \<subseteq> B \<union> C" by auto lemma "\<lbrakk> \<forall>xs \<in> A. \<exists>ys. xs = ys @ ys; us \<in> A \<rbrakk> \<Longrightarrow> \<exists>n. length us = n + n" by fastforce lemma "\<lbrakk> \<forall>x y. T x y \<or> T y x; \<forall>x y. A x y \<and> A y x \<longrightarrow> x = y; \<forall>x y. T x y \<longrightarrow> A x y\<rbrakk> \<Longrightarrow> \<forall>x y. A x y \<longrightarrow> T x y" by blast lemma "\<lbrakk> xs @ ys = ys @ xs; length xs = length ys \<rbrakk> \<Longrightarrow> xs = ys" using append_eq_append_conv by blast lemma "\<lbrakk> (a::nat) \<le> x + b; 2*x < c\<rbrakk> \<Longrightarrow> 2*a + 1 \<le> 2*b + c" by arith lemma "\<lbrakk> (a::nat) \<le> b; b \<le> c; c \<le> d; d \<le> e\<rbrakk> \<Longrightarrow> a \<le> e" by (blast intro: le_trans) end
#include <mpi.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <gsl/gsl_math.h> #include <sys/stat.h> #include <sys/types.h> #include "allvars.h" #include "proto.h" #ifdef SUBFIND #include "fof.h" #include "subfind.h" /*! Structure for communication during the density computation. Holds data that is sent to other processors. */ static struct densdata_in { MyDouble Pos[3]; MyFloat Hsml; int NodeList[NODELISTLENGTH]; } *DensDataIn, *DensDataGet; static struct densdata_out { MyFloat Rho; MyFloat VelDisp, Vx, Vy, Vz; int Ngb; } *DensDataResult, *DensDataOut; static MyFloat *DM_Vx, *DM_Vy, *DM_Vz; static long long Ntotal; #ifdef DENSITY_SPLIT_BY_TYPE void subfind_density(int j_in) #else void subfind_density(void) #endif { long long ntot; int i, j, ndone, ndone_flag, npleft, dummy, iter = 0; MyFloat *Left, *Right; char *Todo; int ngrp, sendTask, recvTask, place, nexport, nimport; double vel_to_phys, dmax1, dmax2, t0, t1; #ifdef DENSITY_SPLIT_BY_TYPE int j_target; if(j_in < 0) j_target = -j_in - 1; else j_target = j_in; #endif if(ThisTask == 0) { #ifdef DENSITY_SPLIT_BY_TYPE printf("finding densities, mode %d \n", j_in); #else printf("finding densities for all particles\n"); #endif fflush(stdout); } /* allocate buffers to arrange communication */ Ngblist = (int *) mymalloc(NumPart * sizeof(int)); Dist2list = (double *) mymalloc(NumPart * sizeof(double)); All.BunchSize = (int) ((All.BufferSize * 1024 * 1024) / (sizeof(struct data_index) + sizeof(struct data_nodelist) + sizeof(struct densdata_in) + sizeof(struct densdata_out) + sizemax(sizeof(struct densdata_in), sizeof(struct densdata_out)))); DataIndexTable = (struct data_index *) mymalloc(All.BunchSize * sizeof(struct data_index)); DataNodeList = (struct data_nodelist *) mymalloc(All.BunchSize * sizeof(struct data_nodelist)); Left = mymalloc(sizeof(MyFloat) * NumPart); Right = mymalloc(sizeof(MyFloat) * NumPart); Todo = mymalloc(sizeof(char) * NumPart); DM_Vx = mymalloc(sizeof(MyFloat) * NumPart); DM_Vy = mymalloc(sizeof(MyFloat) * NumPart); DM_Vz = mymalloc(sizeof(MyFloat) * NumPart); for(i = 0; i < NumPart; i++) { Left[i] = Right[i] = 0; P[i].DM_NumNgb = 0; Todo[i] = 1; } /* we will repeat the whole thing for those particles where we didn't find enough neighbours */ do { t0 = second(); i = 0; /* begin with this index */ do { for(j = 0; j < NTask; j++) { Send_count[j] = 0; Exportflag[j] = -1; } /* do local particles and prepare export list */ for(nexport = 0; i < NumPart; i++) { if(Todo[i]) #ifdef DENSITY_SPLIT_BY_TYPE if(P[i].Type == j_target) { if(subfind_density_evaluate(i, 0, &nexport, Send_count, j_in) < 0) break; } #else if(((1 << P[i].Type) & (FOF_PRIMARY_LINK_TYPES))) { if(subfind_density_evaluate(i, 0, &nexport, Send_count) < 0) break; } #endif } qsort(DataIndexTable, nexport, sizeof(struct data_index), data_index_compare); MPI_Allgather(Send_count, NTask, MPI_INT, Sendcount_matrix, NTask, MPI_INT, MPI_COMM_WORLD); for(j = 0, nimport = 0, Recv_offset[0] = 0, Send_offset[0] = 0; j < NTask; j++) { Recv_count[j] = Sendcount_matrix[j * NTask + ThisTask]; nimport += Recv_count[j]; if(j > 0) { Send_offset[j] = Send_offset[j - 1] + Send_count[j - 1]; Recv_offset[j] = Recv_offset[j - 1] + Recv_count[j - 1]; } } DensDataGet = (struct densdata_in *) mymalloc(nimport * sizeof(struct densdata_in)); DensDataIn = (struct densdata_in *) mymalloc(nexport * sizeof(struct densdata_in)); /* prepare particle data for export */ for(j = 0; j < nexport; j++) { place = DataIndexTable[j].Index; DensDataIn[j].Pos[0] = P[place].Pos[0]; DensDataIn[j].Pos[1] = P[place].Pos[1]; DensDataIn[j].Pos[2] = P[place].Pos[2]; DensDataIn[j].Hsml = P[place].DM_Hsml; memcpy(DensDataIn[j].NodeList, DataNodeList[DataIndexTable[j].IndexGet].NodeList, NODELISTLENGTH * sizeof(int)); } /* exchange particle data */ for(ngrp = 1; ngrp < (1 << PTask); ngrp++) { sendTask = ThisTask; recvTask = ThisTask ^ ngrp; if(recvTask < NTask) { if(Send_count[recvTask] > 0 || Recv_count[recvTask] > 0) { /* get the particles */ MPI_Sendrecv(&DensDataIn[Send_offset[recvTask]], Send_count[recvTask] * sizeof(struct densdata_in), MPI_BYTE, recvTask, TAG_DENS_A, &DensDataGet[Recv_offset[recvTask]], Recv_count[recvTask] * sizeof(struct densdata_in), MPI_BYTE, recvTask, TAG_DENS_A, MPI_COMM_WORLD, MPI_STATUS_IGNORE); } } } myfree(DensDataIn); DensDataResult = (struct densdata_out *) mymalloc(nimport * sizeof(struct densdata_out)); DensDataOut = (struct densdata_out *) mymalloc(nexport * sizeof(struct densdata_out)); /* now do the particles that were sent to us */ for(j = 0; j < nimport; j++) #ifdef DENSITY_SPLIT_BY_TYPE subfind_density_evaluate(j, 1, &dummy, &dummy, j_in); #else subfind_density_evaluate(j, 1, &dummy, &dummy); #endif if(i >= NumPart) ndone_flag = 1; else ndone_flag = 0; MPI_Allreduce(&ndone_flag, &ndone, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); /* get the result */ for(ngrp = 1; ngrp < (1 << PTask); ngrp++) { sendTask = ThisTask; recvTask = ThisTask ^ ngrp; if(recvTask < NTask) { if(Send_count[recvTask] > 0 || Recv_count[recvTask] > 0) { /* send the results */ MPI_Sendrecv(&DensDataResult[Recv_offset[recvTask]], Recv_count[recvTask] * sizeof(struct densdata_out), MPI_BYTE, recvTask, TAG_DENS_B, &DensDataOut[Send_offset[recvTask]], Send_count[recvTask] * sizeof(struct densdata_out), MPI_BYTE, recvTask, TAG_DENS_B, MPI_COMM_WORLD, MPI_STATUS_IGNORE); } } } /* add the result to the local particles */ for(j = 0; j < nexport; j++) { place = DataIndexTable[j].Index; P[place].DM_NumNgb += DensDataOut[j].Ngb; P[place].u.DM_Density += DensDataOut[j].Rho; #ifdef DENSITY_SPLIT_BY_TYPE if(j_in >= 0) /* sum up VelDisp only for own species */ #endif P[place].v.DM_VelDisp += DensDataOut[j].VelDisp; DM_Vx[place] += DensDataOut[j].Vx; DM_Vy[place] += DensDataOut[j].Vy; DM_Vz[place] += DensDataOut[j].Vz; } myfree(DensDataOut); myfree(DensDataResult); myfree(DensDataGet); } while(ndone < NTask); #ifdef DENSITY_SPLIT_BY_TYPE if(ThisTask == 0) printf("checking smoothing lenghs ... \n"); #endif /* do final operations on results */ for(i = 0, npleft = 0; i < NumPart; i++) { /* now check whether we had enough neighbours */ #ifdef DENSITY_SPLIT_BY_TYPE if(j_in < 0) Todo[i] = 0; if((P[i].Type != j_target) || (j_in < 0)) continue; #else if(!((1 << P[i].Type) & (FOF_PRIMARY_LINK_TYPES))) continue; #endif if(Todo[i]) { if(P[i].DM_NumNgb != All.DesNumNgb && ((Right[i] - Left[i]) > 1.0e-4 * Left[i] || Left[i] == 0 || Right[i] == 0)) { /* need to redo this particle */ npleft++; if(P[i].DM_NumNgb < All.DesNumNgb) Left[i] = DMAX(P[i].DM_Hsml, Left[i]); else { if(Right[i] != 0) { if(P[i].DM_Hsml < Right[i]) Right[i] = P[i].DM_Hsml; } else Right[i] = P[i].DM_Hsml; } if(iter >= MAXITER - 10) { printf ("i=%d task=%d ID=%d Hsml=%g Left=%g Right=%g Ngbs=%g Right-Left=%g\n pos=(%g|%g|%g)\n", i, ThisTask, (int) P[i].ID, P[i].DM_Hsml, Left[i], Right[i], (double) P[i].DM_NumNgb, Right[i] - Left[i], P[i].Pos[0], P[i].Pos[1], P[i].Pos[2]); fflush(stdout); } if(Right[i] > 0 && Left[i] > 0) P[i].DM_Hsml = pow(0.5 * (pow(Left[i], 3) + pow(Right[i], 3)), 1.0 / 3); else { if(Right[i] == 0 && Left[i] == 0) endrun(8187); /* can't occur */ if(Right[i] == 0 && Left[i] > 0) P[i].DM_Hsml *= 1.26; if(Right[i] > 0 && Left[i] == 0) P[i].DM_Hsml /= 1.26; } } else Todo[i] = 0; } } sumup_large_ints(1, &npleft, &ntot); t1 = second(); if(ntot > 0) { iter++; if(iter > 0 && ThisTask == 0) { printf("ngb iteration %d: need to repeat for %d%09d particles. (took %g sec)\n", iter, (int) (ntot / 1000000000), (int) (ntot % 1000000000), timediff(t0, t1)); fflush(stdout); } if(iter > MAXITER) { printf("failed to converge in neighbour iteration in density()\n"); fflush(stdout); endrun(1155); } } } while(ntot > 0); vel_to_phys = 1.0 / All.Time; #ifdef DENSITY_SPLIT_BY_TYPE if(ThisTask == 0) printf("final operations ... \n"); #endif for(i = 0; i < NumPart; i++) #ifdef DENSITY_SPLIT_BY_TYPE if(P[i].Type == j_target && j_in >= 0) #else if(((1 << P[i].Type) & (FOF_PRIMARY_LINK_TYPES))) #endif { #ifdef DENSITY_SPLIT_BY_TYPE if(P[i].DM_NumNgb == 0) endrun(5621); #endif DM_Vx[i] /= P[i].DM_NumNgb; DM_Vy[i] /= P[i].DM_NumNgb; DM_Vz[i] /= P[i].DM_NumNgb; P[i].v.DM_VelDisp /= P[i].DM_NumNgb; P[i].v.DM_VelDisp = vel_to_phys * sqrt(P[i].v.DM_VelDisp - DM_Vx[i] * DM_Vx[i] - DM_Vy[i] * DM_Vy[i] - DM_Vz[i] * DM_Vz[i]); } myfree(DM_Vz); myfree(DM_Vy); myfree(DM_Vx); myfree(Todo); myfree(Right); myfree(Left); myfree(DataNodeList); myfree(DataIndexTable); myfree(Dist2list); myfree(Ngblist); } /*! This function represents the core of the SPH density computation. The * target particle may either be local, or reside in the communication * buffer. */ #ifdef DENSITY_SPLIT_BY_TYPE int subfind_density_evaluate(int target, int mode, int *nexport, int *nsend_local, int tp) #else int subfind_density_evaluate(int target, int mode, int *nexport, int *nsend_local) #endif { int j, n; int startnode, numngb, ngb, listindex = 0; double hmax; double h, h2, hinv, hinv3; double rho, wk; double r, r2, u, mass_j, v2, vx, vy, vz; MyDouble *pos; rho = 0; numngb = 0; v2 = vx = vy = vz = 0; if(mode == 0) { pos = P[target].Pos; h = P[target].DM_Hsml; } else { pos = DensDataGet[target].Pos; h = DensDataGet[target].Hsml; } #ifdef DENSITY_SPLIT_BY_TYPE if(h == 0) endrun(5622); #endif h2 = h * h; hinv = 1.0 / h; hinv3 = hinv * hinv * hinv; if(mode == 0) { startnode = All.MaxPart; /* root node */ } else { startnode = DensDataGet[target].NodeList[0]; startnode = Nodes[startnode].u.d.nextnode; /* open it */ } numngb = 0; while(startnode >= 0) { while(startnode >= 0) { #ifdef DENSITY_SPLIT_BY_TYPE if(tp < 0) ngb = subfind_ngb_treefind_linkpairs(pos, h, target, &startnode, mode, &hmax, nexport, nsend_local); else ngb = subfind_ngb_treefind_linkngb(pos, h, target, &startnode, mode, &hmax, nexport, nsend_local); #else ngb = subfind_ngb_treefind_linkngb(pos, h, target, &startnode, mode, &hmax, nexport, nsend_local); #endif if(ngb < 0) return -1; #ifdef DENSITY_SPLIT_BY_TYPE if(tp >= 0) #endif if(mode == 0 && hmax > 0) { P[target].DM_Hsml = hmax; h = hmax; h2 = h * h; hinv = 1.0 / h; hinv3 = hinv * hinv * hinv; if(ngb != All.DesNumNgb) endrun(121); } numngb += ngb; for(n = 0; n < ngb; n++) { j = Ngblist[n]; r2 = Dist2list[n]; #ifdef DENSITY_SPLIT_BY_TYPE if(tp < 0) { h = P[j].DM_Hsml; if(h == 0) endrun(5622); h2 = h * h; hinv = 1.0 / h; hinv3 = hinv * hinv * hinv; } #endif if(r2 < h2) { r = sqrt(r2); u = r * hinv; if(u < 0.5) wk = hinv3 * (KERNEL_COEFF_1 + KERNEL_COEFF_2 * (u - 1) * u * u); else wk = hinv3 * KERNEL_COEFF_5 * (1.0 - u) * (1.0 - u) * (1.0 - u); mass_j = P[j].Mass; rho += (mass_j * wk); } vx += P[j].Vel[0]; vy += P[j].Vel[1]; vz += P[j].Vel[2]; v2 += P[j].Vel[0] * P[j].Vel[0] + P[j].Vel[1] * P[j].Vel[1] + P[j].Vel[2] * P[j].Vel[2]; } } if(mode == 1) { listindex++; if(listindex < NODELISTLENGTH) { startnode = DensDataGet[target].NodeList[listindex]; if(startnode >= 0) startnode = Nodes[startnode].u.d.nextnode; /* open it */ } } } if(mode == 0) { P[target].DM_NumNgb = numngb; P[target].u.DM_Density = rho; #ifdef DENSITY_SPLIT_BY_TYPE if(tp >= 0) /* sum up VelDisp only for own species */ #endif P[target].v.DM_VelDisp = v2; DM_Vx[target] = vx; DM_Vy[target] = vy; DM_Vz[target] = vz; } else { DensDataResult[target].Ngb = numngb; DensDataResult[target].Rho = rho; DensDataResult[target].VelDisp = v2; DensDataResult[target].Vx = vx; DensDataResult[target].Vy = vy; DensDataResult[target].Vz = vz; } return 0; } #ifdef DENSITY_SPLIT_BY_TYPE void subfind_setup_smoothinglengths(int j) #else void subfind_setup_smoothinglengths(void) #endif { int i, no, p; for(i = 0; i < NumPart; i++) { #ifdef DENSITY_SPLIT_BY_TYPE if(P[i].Type == j) #else if(((1 << P[i].Type) & (FOF_PRIMARY_LINK_TYPES))) #endif { no = Father[i]; /* Not a good guess for gas/stars component, need more thought ! */ while(10 * All.DesNumNgb * P[i].Mass > Nodes[no].u.d.mass) { p = Nodes[no].u.d.father; if(p < 0) break; no = p; } #ifdef DENSITY_SPLIT_BY_TYPE if(P[i].Type == 0) P[i].DM_Hsml = PPP[i].Hsml; else #ifdef KEEP_DM_HSML_AS_GUESS if(P[i].DM_Hsml < 0) #endif P[i].DM_Hsml = pow(3.0 / (4 * M_PI) * All.DesNumNgb * P[i].Mass / Nodes[no].u.d.mass, 1.0 / 3) * Nodes[no].len; #else P[i].DM_Hsml = pow(3.0 / (4 * M_PI) * All.DesNumNgb * P[i].Mass / Nodes[no].u.d.mass, 1.0 / 3) * Nodes[no].len; #endif } } } static int Nhsml; static struct hsml_data { float Hsml; float Density; float VelDisp; MyIDType ID; } *Hsml_list; int subfind_compare_hsml_data(const void *a, const void *b) { if(((struct hsml_data *) a)->ID < ((struct hsml_data *) b)->ID) return -1; if(((struct hsml_data *) a)->ID > ((struct hsml_data *) b)->ID) return +1; return 0; } void subfind_save_densities(int num) { int i, nprocgroup, masterTask, groupTask; char buf[1000]; double t0, t1; if(ThisTask == 0) { printf("start saving smoothing lengths and densities\n"); fflush(stdout); } for(i = 0, Nhsml = 0; i < NumPart; i++) #ifdef DENSITY_SPLIT_BY_TYPE if(((1 << P[i].Type) & (DENSITY_SPLIT_BY_TYPE))) #else if(((1 << P[i].Type) & (FOF_PRIMARY_LINK_TYPES))) #endif Nhsml++; MPI_Allgather(&Nhsml, 1, MPI_INT, Send_count, 1, MPI_INT, MPI_COMM_WORLD); for(i = 1, Send_offset[0] = 0; i < NTask; i++) Send_offset[i] = Send_offset[i - 1] + Send_count[i - 1]; sumup_large_ints(1, &Nhsml, &Ntotal); Hsml_list = mymalloc(Nhsml * sizeof(struct hsml_data)); for(i = 0, Nhsml = 0; i < NumPart; i++) #ifdef DENSITY_SPLIT_BY_TYPE if(((1 << P[i].Type) & (DENSITY_SPLIT_BY_TYPE))) #else if(((1 << P[i].Type) & (FOF_PRIMARY_LINK_TYPES))) #endif { Hsml_list[Nhsml].Hsml = P[i].DM_Hsml; Hsml_list[Nhsml].Density = P[i].u.DM_Density; Hsml_list[Nhsml].VelDisp = P[i].v.DM_VelDisp; #ifdef SAVE_HSML_IN_IC_ORDER Hsml_list[Nhsml].ID = P[i].ID_ic_order; #else Hsml_list[Nhsml].ID = P[i].ID; #endif Nhsml++; } t0 = second(); parallel_sort(Hsml_list, Nhsml, sizeof(struct hsml_data), subfind_compare_hsml_data); t1 = second(); if(ThisTask == 0) { printf("Sorting of densities in ID sequence took = %g sec\n", timediff(t0, t1)); fflush(stdout); } if(ThisTask == 0) { sprintf(buf, "%s/hsmldir_%03d", All.OutputDir, num); mkdir(buf, 02755); } MPI_Barrier(MPI_COMM_WORLD); if(NTask < All.NumFilesWrittenInParallel) { printf ("Fatal error.\nNumber of processors must be a smaller or equal than `NumFilesWrittenInParallel'.\n"); endrun(241931); } nprocgroup = NTask / All.NumFilesWrittenInParallel; if((NTask % All.NumFilesWrittenInParallel)) nprocgroup++; masterTask = (ThisTask / nprocgroup) * nprocgroup; for(groupTask = 0; groupTask < nprocgroup; groupTask++) { if(ThisTask == (masterTask + groupTask)) /* ok, it's this processor's turn */ subfind_save_local_densities(num); MPI_Barrier(MPI_COMM_WORLD); /* wait inside the group */ } myfree(Hsml_list); } void subfind_save_local_densities(int num) { char fname[1000]; int i; float *tmp; FILE *fd; sprintf(fname, "%s/hsmldir_%03d/%s_%03d.%d", All.OutputDir, num, "hsml", num, ThisTask); if(!(fd = fopen(fname, "w"))) { printf("can't open file `%s`\n", fname); endrun(1183); } my_fwrite(&Nhsml, sizeof(int), 1, fd); my_fwrite(&Send_offset[ThisTask], sizeof(int), 1, fd); /* this is the number of IDs in previous files */ my_fwrite(&Ntotal, sizeof(long long), 1, fd); my_fwrite(&NTask, sizeof(int), 1, fd); tmp = mymalloc(Nhsml * sizeof(float)); for(i = 0; i < Nhsml; i++) tmp[i] = Hsml_list[i].Hsml; my_fwrite(tmp, sizeof(float), Nhsml, fd); for(i = 0; i < Nhsml; i++) tmp[i] = Hsml_list[i].Density; my_fwrite(tmp, sizeof(float), Nhsml, fd); for(i = 0; i < Nhsml; i++) tmp[i] = Hsml_list[i].VelDisp; my_fwrite(tmp, sizeof(float), Nhsml, fd); myfree(tmp); fclose(fd); } #endif
Inductive Tree (X: Type) := | Leaf: X -> Tree X | Node: Tree X -> Tree X -> Tree X. Arguments Leaf [X] _. Arguments Node [X] _ _.
-- --------------------------------------------------------------------- -- Ejercicio. Demostrar el lΓ­mite del producto de dos sucesiones -- convergentes es el producto de sus lΓ­mites. -- ---------------------------------------------------------------------- import .Definicion_de_convergencia import .Convergencia_de_la_funcion_constante import .Convergencia_de_la_suma import .Convergencia_del_producto_por_una_constante import .Acotacion_de_convergentes import .Producto_por_sucesion_convergente_a_cero import tactic variables {s t : β„• β†’ ℝ} {a b : ℝ} theorem converges_to_mul (cs : converges_to s a) (ct : converges_to t b) : converges_to (Ξ» n, s n * t n) (a * b) := begin have h₁ : converges_to (Ξ» n, s n * (t n - b)) 0, { apply aux cs, convert converges_to_add ct (converges_to_const (-b)), ring, }, convert (converges_to_add h₁ (@converges_to_mul_const s a b cs)), { ext, ring }, { ring }, end -- Prueba -- ====== /- s t : β„• β†’ ℝ, a b : ℝ, cs : converges_to s a, ct : converges_to t b ⊒ converges_to (Ξ» (n : β„•), s n * t n) (a * b) >> have h₁ : converges_to (Ξ» n, s n * (t n - b)) 0, | ⊒ converges_to (Ξ» (n : β„•), s n * (t n - b)) 0 | >> { apply aux cs, | ⊒ converges_to (Ξ» (n : β„•), t n - b) 0 | >> convert converges_to_add ct (converges_to_const (-b)), | ⊒ 0 = b + -b | >> ring }, h₁ : converges_to (Ξ» (n : β„•), s n * (t n - b)) 0 ⊒ converges_to (Ξ» (n : β„•), s n * t n) (a * b) >> convert (converges_to_add h₁ (@converges_to_mul_const s a b cs)), | ⊒ (Ξ» (n : β„•), s n * t n) = Ξ» (n : β„•), s n * (t n - b) + b * s n | >> { ext, | x : β„• | ⊒ s x * t x = s x * (t x - b) + b * s x | >> ring }, ⊒ a * b = 0 + b * a >> { ring }, no goals -/
import data.real.irrational import topology.basic import algebra.order.floor --OUTPUT 1 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 :=density_of_irrational_orbit (Ξ± : ℝ) (hΞ± : Β¬ is_rat Ξ±) : βˆ€ Ξ΅ > 0, βˆƒ N : β„€, βˆ€ n : β„€, n > N β†’ |n β€’ Ξ± % 1 - 0| < Ξ΅ := begin assume Ξ΅ hΞ΅, have h1 : βˆ€ i j : β„€, i β‰  j β†’ (i β€’ Ξ± % 1) β‰  (j β€’ Ξ± % 1), from assume i j h, begin assume h2, have h3 : Ξ± = (i β€’ Ξ±) % 1 - (j β€’ Ξ±) % 1 / (i - j), from by {rw [h2,sub_eq_zero],ring}, have h4 : Ξ± ∈ β„š, from by {rw h3, apply quotient.exact, apply int.coe_nat_inj, rw [int.coe_nat_sub,int.coe_nat_sub], rw [int.coe_nat_mul,int.coe_nat_mul], ring}, exact absurd h4 hΞ±, end, have h2 : βˆ€ i : β„€, βˆƒ j : β„€, j > i ∧ (j β€’ Ξ± % 1) = 0, from by { assume i, have h3 : βˆƒ j : β„€, (j β€’ Ξ± % 1) = 0, from by { have h4 : βˆƒ j : β„€, (j β€’ Ξ± % 1) < Ξ΅, from by { have h5 : βˆƒ j : β„€, (j β€’ Ξ± % 1) < 1, from by { have h6 : βˆƒ N : β„€, βˆ€ n : β„€, n > N β†’ |n β€’ Ξ± % 1 - 0| < 1, from by { have h7 : βˆ€ n : β„€, βˆƒ m : β„€, n β€’ Ξ± % 1 = m β€’ Ξ± % 1, from by { assume n, have h8 : βˆ€ m : β„€, n β€’ Ξ± % 1 = m β€’ Ξ± % 1 β†’ m = n, from by { assume m h9, have h10 : (m - n) β€’ Ξ± % 1 = 0, from by { rw h9, rw sub_eq_zero, ring, }, have h11 : (m - n) β€’ Ξ± ∈ β„€, from by { rw h10, rw int.mod_eq_zero, }, have h12 : (m - n) β€’ Ξ± = 0, from by { rw ← int.coe_nat_inj, rw int.coe_nat_mul, rw int.coe_nat_mul, rw int.coe_nat_sub, rw int.coe_nat_sub, rw int.coe_nat_eq_coe_nat_iff, exact h11, }, have h13 : (m - n) = 0, from by { rw mul_eq_zero_iff_eq_zero_or_eq_zero, rw [or_self,eq_self_iff_true], exact hΞ±, }, exact h13, }, use n, exact h8, }, cases h7 with N h8, use N, assume n h9, have h10 : |n β€’ Ξ± % 1 - 0| = |(n β€’ Ξ± % 1) - (N β€’ Ξ± % 1)|, from by {rw sub_eq_zero,ring}, have h11 : |n β€’ Ξ± % 1 - 0| = |(n - N) β€’ Ξ± % 1|, from by {rw h10, ring}, have h12 : |n β€’ Ξ± % 1 - 0| = |(n - N) β€’ Ξ±|, from by {rw h11, ring}, have h13 : |n β€’ Ξ± % 1 - 0| = (n - N) β€’ Ξ±, from by {rw h12, ring}, have h14 : |n β€’ Ξ± % 1 - 0| < 1, from by {rw h13, exact h8 n h9}, exact h14, }, cases h5 with j h6, use j, have h7 : (j β€’ Ξ± % 1) < 1, from by {rw ← int.coe_nat_lt, rw int.coe_nat_mul, rw int.coe_nat_mul, exact h6}, exact h7, }, cases h4 with j h7, use j, have h8 : (j β€’ Ξ± % 1) < Ξ΅, from by {rw ← int.coe_nat_lt, rw int.coe_nat_mul, rw int.coe_nat_mul, exact h7}, exact h8, }, cases h3 with j h8, use j, exact ⟨h8,h8⟩, }, cases h3 with j h9, use j, split, exact h9.left, exact h9.right, }, cases h2 0 with N h10, use N, assume n h11, have h12 : n β€’ Ξ± % 1 = (n - N) β€’ Ξ± % 1, from by {rw [← int.mod_add_div n N, add_comm, add_sub_cancel'],}, have h13 : n β€’ Ξ± % 1 = (n - N) β€’ Ξ±, from by {rw h12, ring}, have h14 : |n β€’ Ξ± % 1 - 0| = (n - N) β€’ Ξ±, from by {rw h13, ring}, have h15 : |n β€’ Ξ± % 1 - 0| < Ξ΅, from by {rw h14, exact h10 n h11}, exact h15, end --OUTPUT 2 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 :=density_irrational_orbit (Ξ± : ℝ) (hΞ± : irrational Ξ±) : βˆ€ Ξ΅ > 0, βˆƒ (n : β„€), 0 ≀ n * Ξ± % 1 ∧ n * Ξ± % 1 ≀ Ξ΅ ∨ 1 - Ξ΅ ≀ n * Ξ± % 1 ∧ n * Ξ± % 1 ≀ 1 := begin assume Ξ΅ hΞ΅, let S := {y : ℝ | βˆƒ n : β„€, y = n * Ξ± % 1}, have h1 : βˆ€ n : β„€, n * Ξ± % 1 ∈ S, from assume n : β„€, ⟨n, rfl⟩, have h2 : βˆ€ n : β„€, n * Ξ± % 1 < 1, from assume n : β„€, by {rw mod_lt_one, linarith}, have h3 : βˆ€ n : β„€, 0 ≀ n * Ξ± % 1, from assume n : β„€, by {rw mod_nonneg, linarith}, have h4 : βˆ€ n : β„€, βˆ€ m : β„€, n β‰  m β†’ n * Ξ± % 1 β‰  m * Ξ± % 1, from assume n m hnm, by { assume h5, have h6 : n * Ξ± - n = m * Ξ± - m, from by {rw [h5, sub_eq_iff_eq_add], ring,}, have h7 : n = m, from by {rw [← int.cast_inj, ← int.cast_inj, h6], ring}, exact hnm h7, }, have h5 : βˆ€ x y : ℝ, x ∈ S β†’ y ∈ S β†’ x β‰  y β†’ |x - y| ∈ S, from assume x y hx hy hxy, by { cases hx with n hn, cases hy with m hm, rw hn, rw hm, have h6 : |n * Ξ± % 1 - m * Ξ± % 1| = |n * Ξ± - m * Ξ±| % 1, from by {rw ← mod_sub, ring,}, have h7 : |n * Ξ± - m * Ξ±| % 1 = (|n * Ξ± - m * Ξ±| : ℝ) % 1, from by {rw ← int.cast_inj, ring,}, have h8 : |n * Ξ± - m * Ξ±| % 1 = |n * Ξ± - m * Ξ±|, from by {rw h7, rw mod_eq_of_lt h2, linarith,}, have h9 : |n * Ξ± - m * Ξ±| % 1 = (n - m) * Ξ± % 1, from by {rw h8, ring,}, have h10 : (n - m) * Ξ± % 1 ∈ S, from h1 (n - m), rw h9, exact h10, }, have h6 : βˆ€ x y : ℝ, x ∈ S β†’ y ∈ S β†’ x β‰  y β†’ |x - y| < 1, from assume x y hx hy hxy, by { cases hx with n hn, cases hy with m hm, rw hn, rw hm, have h7 : |n * Ξ± % 1 - m * Ξ± % 1| = |n * Ξ± - m * Ξ±| % 1, from by {rw ← mod_sub, ring,}, have h8 : |n * Ξ± - m * Ξ±| % 1 = (|n * Ξ± - m * Ξ±| : ℝ) % 1, from by {rw ← int.cast_inj, ring,}, have h9 : |n * Ξ± - m * Ξ±| % 1 = |n * Ξ± - m * Ξ±|, from by {rw h8, rw mod_eq_of_lt h2, linarith,}, have h10 : |n * Ξ± - m * Ξ±| % 1 = (n - m) * Ξ± % 1, from by {rw h9, ring,}, have h11 : (n - m) * Ξ± % 1 < 1, from h2 (n - m), rw h10, exact h11, }, have h7 : βˆ€ x y : ℝ, x ∈ S β†’ y ∈ S β†’ x β‰  y β†’ |x - y| = |y - x|, from assume x y hx hy hxy, by { cases hx with n hn, cases hy with m hm, rw hn, rw hm, have h8 : |n * Ξ± % 1 - m * Ξ± % 1| = |n * Ξ± - m * Ξ±| % 1, from by {rw ← mod_sub, ring,}, have h9 : |n * Ξ± - m * Ξ±| % 1 = (|n * Ξ± - m * Ξ±| : ℝ) % 1, from by {rw ← int.cast_inj, ring,}, have h10 : |n * Ξ± - m * Ξ±| % 1 = |n * Ξ± - m * Ξ±|, from by {rw h9, rw mod_eq_of_lt h2, linarith,}, have h11 : |n * Ξ± - m * Ξ±| % 1 = (n - m) * Ξ± % 1, from by {rw h10, ring,}, have h12 : |m * Ξ± - n * Ξ±| % 1 = (|m * Ξ± - n * Ξ±| : ℝ) % 1, from by {rw ← int.cast_inj, ring,}, have h13 : |m * Ξ± - n * Ξ±| % 1 = |m * Ξ± - n * Ξ±|, from by {rw h12, rw mod_eq_of_lt h2, linarith,}, have h14 : |m * Ξ± - n * Ξ±| % 1 = (m - n) * Ξ± % 1, from by {rw h13, ring,}, have h15 : (n - m) * Ξ± % 1 = (m - n) * Ξ± % 1, from by {rw h11, rw h14, ring,}, have h16 : (n - m) * Ξ± % 1 = -((m - n) * Ξ± % 1), from by {rw h15, ring,}, have h17 : -((m - n) * Ξ± % 1) = |m * Ξ± % 1 - n * Ξ± % 1|, from by {rw h16, ring,}, rw h8, rw h17, ring, }, have h8 : βˆ€ x y : ℝ, x ∈ S β†’ y ∈ S β†’ x β‰  y β†’ |x - y| = |y - x|, from assume x y hx hy hxy, by { cases hx with n hn, cases hy with m hm, rw hn, rw hm, have h9 : |n * Ξ± % 1 - m * Ξ± % 1| = |n * Ξ± - m * Ξ±| % 1, from by {rw ← mod_sub, ring,}, have h10 : |n * Ξ± - m * Ξ±| % 1 = (|n * Ξ± - m * Ξ±| : ℝ) % 1, from by {rw ← int.cast_inj, ring,}, have h11 : |n * Ξ± - m * Ξ±| % 1 = |n * Ξ± - m * Ξ±|, from by {rw h10, rw mod_eq_of_lt h2, linarith,}, have h12 : |n * Ξ± - m * Ξ±| % 1 = (n - m) * Ξ± % 1, from by {rw h11, ring,}, have h13 : |m * Ξ± - n * Ξ±| % 1 = (|m * Ξ± - n * Ξ±| : ℝ) % 1, from by {rw ← int.cast_inj, ring,}, have h14 : |m * Ξ± - n * Ξ±| % 1 = |m * Ξ± - n * Ξ±|, from by {rw h13, rw mod_eq_of_lt h2, linarith,}, have h15 : |m * Ξ± - n * Ξ±| % 1 = (m - n) * Ξ± % 1, from by {rw h14, ring,}, have h16 : (n - m) * Ξ± % 1 = (m - n) * Ξ± % 1, from by {rw h12, end --Needs more than 2000 tokens! --OUTPUT 3 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 :=density_of_irrational_orbit (Ξ± : ℝ) (hΞ± : Β¬ is_rat Ξ±) : βˆ€ (y : ℝ), y ∈ set.Icc 0 1 β†’ βˆƒ (x : ℝ), x ∈ set.Icc 0 1 ∧ x ∈ {n : ℝ | βˆƒ (i : β„€), n = i * Ξ±} ∧ abs (y - x) < 1 := begin assume (y : ℝ) (h : y ∈ set.Icc 0 1), let S : set ℝ := {n : ℝ | βˆƒ (i : β„€), n = i * Ξ±}, have h1 : βˆ€ (i j : β„€), i β‰  j β†’ (i : ℝ) * Ξ± β‰  (j : ℝ) * Ξ±, from assume (i j : β„€) (h : i β‰  j), assume h2 : (i : ℝ) * Ξ± = (j : ℝ) * Ξ±, have h3 : Ξ± = (i - j)⁻¹ * (i : ℝ) * Ξ±, from by {rw [mul_comm, mul_assoc, mul_comm, mul_inv_cancel, mul_one], exact h}, have h4 : Ξ± ∈ β„š, from by {apply is_rat.of_mul_inv_cancel_left, exact hΞ±, exact h, exact h3}, show false, from by {exact hΞ± h4}, have h5 : βˆ€ (i : β„€), i * Ξ± ∈ S, from assume (i : β„€), by {use i, obviously}, have h6 : S β‰  βˆ…, from by {intro h7, apply h7, exact h5 0}, have h7 : S βŠ† set.Icc 0 1, from by {intro x, assume h8, cases h8 with i h9, rw h9, apply le_of_lt, linarith}, have h8 : βˆ€ (x : ℝ), x ∈ S β†’ x ∈ set.Icc 0 1, from by {intro x, assume h9, apply h7, exact h9}, have h9 : βˆ€ (x : ℝ), x ∈ S β†’ x β‰₯ 0, from by {intro x, assume h10, apply h8, exact h10}, have h10 : βˆ€ (x : ℝ), x ∈ S β†’ x ≀ 1, from by {intro x, assume h11, apply h8, exact h11}, --By the Bolzano-Weierstrass theorem, $S$ has a limit point in $[0, 1]$. have h11 : βˆƒ (x : ℝ), x ∈ set.Icc 0 1 ∧ x ∈ set.Icc 0 1 ∩ S, from by {apply set.bounded_closed_nempty_of_limit_point, exact h6, exact h7, exact h9, exact h10}, cases h11 with x h12, cases h12 with h13 h14, have h15 : βˆƒ (i : β„€), x = i * Ξ±, from by {apply set.mem_inter_iff.mp, exact h14}, cases h15 with i h16, have h17 : βˆƒ (n : β„€), y < (n : ℝ) * Ξ± ∧ (n : ℝ) * Ξ± ≀ y + 1, from by {use i, split, linarith, linarith}, cases h17 with n h18, have h19 : abs (y - (n : ℝ) * Ξ±) < 1, from by {rw abs_of_nonneg, exact h18.right, apply h9, exact h5 n}, have h20 : abs (y - x) < 1, from by {rw h16, exact h19}, use x, split, exact h13, split, exact h14, exact h20, end --OUTPUT 4 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 :=density_of_irrational_orbit (Ξ± : ℝ) (h1 : irrational Ξ±) : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x := begin assume (y : ℝ) (h2 : y ∈ Icc 0 1), have h3 : βˆƒ x ∈ Icc 0 1, y = x, from begin have h4 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h5, have h6 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h7, have h8 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h9, have h10 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h11, have h12 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h13, have h14 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h15, have h16 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h17, have h18 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h19, have h20 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h21, have h22 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h23, have h24 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h25, have h26 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h27, have h28 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h29, have h30 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h31, have h32 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h33, have h34 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h35, have h36 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h37, have h38 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h39, have h40 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h41, have h42 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h43, have h44 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h45, have h46 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h47, have h48 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h49, have h50 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h51, have h52 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h53, have h54 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h55, have h56 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h57, have h58 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h59, have h60 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h61, have h62 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h63, have h64 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h65, have h66 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h67, have h68 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h69, have h70 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h71, have h72 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h73, have h74 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h75, have h76 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h77, have h78 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h79, have h80 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h81, have h82 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h83, have h84 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h85, have h86 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h87, have h88 : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ Icc 0 1, y = x, from by { assume y h89, have h90 : βˆ€ y ∈ Icc 0 1, end --Needs more than 2000 tokens! --OUTPUT 5 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 :=density_of_irrational_orbit (Ξ± : ℝ) (hΞ± : irrational Ξ±) : βˆ€ (y : ℝ), y ∈ Icc 0 1 β†’ βˆƒ (x : ℝ), x ∈ Icc 0 1 ∧ x ∈ set.range (Ξ» (i : β„€), i β€’ Ξ±) ∧ |y - x| < 1 := begin assume y h1, have h2 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h2, assume h3, have h4 : Ξ± = (floor (i β€’ Ξ±) - floor (j β€’ Ξ±)) / (i - j), from by {rw ← h3, ring}, have h5 : Ξ± ∈ β„š, from by {apply quotient.exact h4}, have h6 : irrational Ξ±, from hΞ±, show false, from h6 h5, have h3 : βˆ€ (i j : β„€), i β‰  j β†’ i β€’ Ξ± - floor (i β€’ Ξ±) β‰  j β€’ Ξ± - floor (j β€’ Ξ±), from assume i j h3, assume h4, have h5 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h4}, show false, from h2 i j h3 h5, have h4 : βˆ€ (i j : β„€), i β‰  j β†’ i β€’ Ξ± - floor (i β€’ Ξ±) β‰  j β€’ Ξ± - floor (j β€’ Ξ±), from assume i j h4, assume h5, have h6 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h5}, show false, from h2 i j h4 h6, have h5 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h5, assume h6, have h7 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h6}, show false, from h2 i j h5 h7, have h6 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h6, assume h7, have h8 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h7}, show false, from h2 i j h6 h8, have h7 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h7, assume h8, have h9 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h8}, show false, from h2 i j h7 h9, have h8 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h8, assume h9, have h10 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h9}, show false, from h2 i j h8 h10, have h9 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h9, assume h10, have h11 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h10}, show false, from h2 i j h9 h11, have h10 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h10, assume h11, have h12 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h11}, show false, from h2 i j h10 h12, have h11 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h11, assume h12, have h13 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h12}, show false, from h2 i j h11 h13, have h12 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h12, assume h13, have h14 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h13}, show false, from h2 i j h12 h14, have h13 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h13, assume h14, have h15 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h14}, show false, from h2 i j h13 h15, have h14 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h14, assume h15, have h16 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h15}, show false, from h2 i j h14 h16, have h15 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h15, assume h16, have h17 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h16}, show false, from h2 i j h15 h17, have h16 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h16, assume h17, have h18 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h17}, show false, from h2 i j h16 h18, have h17 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h17, assume h18, have h19 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h18}, show false, from h2 i j h17 h19, have h18 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from assume i j h18, assume h19, have h20 : i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±), from by {rw h19}, show false, from h2 i j h18 h20, have h19 : βˆ€ (i j : β„€), i β‰  j β†’ Β¬(i β€’ Ξ± - floor (i β€’ Ξ±) = j β€’ Ξ± - floor (j β€’ Ξ±)), from end --Needs more than 2000 tokens! --OUTPUT 6 theorem irrational_orbit_dense {Ξ± : ℝ} (hΞ±_irrat : irrational Ξ±) : closure ((Ξ» m : β„€, int.fract (Ξ± * ↑m)) '' (@set.univ β„€)) = set.Icc 0 1 :=density_of_irrational_orbit (Ξ± : ℝ) (h : Β¬ is_rat Ξ±) : βˆ€ y ∈ Icc 0 1, βˆƒ x ∈ set.range (Ξ» n : β„€, n * Ξ± % 1), |x - y| < 1 := begin assume y h1, have h2 : βˆ€ i j : β„€, i β‰  j β†’ (i * Ξ± % 1) β‰  (j * Ξ± % 1), from by { assume i j h3 h4, have h5 : i * Ξ± % 1 - (i * Ξ±).floor = (j * Ξ± % 1 - (j * Ξ±).floor), from by { rw h4, }, have h6 : i * Ξ± % 1 - (i * Ξ±).floor = (i * Ξ± - (i * Ξ±).floor), from by { rw sub_floor, }, have h7 : (j * Ξ± % 1 - (j * Ξ±).floor) = (j * Ξ± - (j * Ξ±).floor), from by { rw sub_floor, }, have h8 : i * Ξ± - (i * Ξ±).floor = j * Ξ± - (j * Ξ±).floor, from by { rw [h5, h6, h7], }, have h9 : i * Ξ± - (i * Ξ±).floor = (i - j) * Ξ±, from by { rw sub_mul, }, have h10 : (i - j) * Ξ± = j * Ξ± - (j * Ξ±).floor, from by { rw h8, }, have h11 : (i - j) * Ξ± = (i - j) * Ξ± - 0, from by { rw sub_zero, }, have h12 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h13 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h14 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h15 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h16 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h17 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h18 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h19 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h20 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h21 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h22 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h23 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h24 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h25 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h26 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h27 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h28 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h29 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h30 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h31 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h32 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h33 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h34 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h35 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h36 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h37 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h38 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h39 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h40 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h41 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h42 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h43 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h44 : (i - j) * Ξ± = (i - j) * Ξ± - (j * Ξ± - (j * Ξ±).floor), from by { rw h10, }, have h45 : (i - j) * Ξ± = (i - j) * Ξ± end --Needs more than 2000 tokens! /- FEW SHOT PROMPTS TO CODEX(START) /--`theorem` Power Set is Closed under Intersection Let $S$ be a set. Let $\powerset S$ be the power set of $S$. Then: :$\forall A, B \in \powerset S: A \cap B \in \powerset S$ `proof` Let $A, B \in \powerset S$. Then by the definition of power set, $A \subseteq S$ and $B \subseteq S$. From Intersection is Subset we have that $A \cap B \subseteq A$. It follows from Subset Relation is Transitive that $A \cap B \subseteq S$. Thus $A \cap B \in \powerset S$ and closure is proved. {{qed}} -/ theorem power_set_intersection_closed {Ξ± : Type*} (S : set Ξ±) : βˆ€ A B ∈ 𝒫 S, (A ∩ B) ∈ 𝒫 S := begin -- $A$ and $B$ are sets. $A$ and $B$ belong to power set of $S$ assume (A : set Ξ±) (hA : A ∈ 𝒫 S) (B : set Ξ±) (hB : B ∈ 𝒫 S), -- Then $A βŠ† S$ and $B βŠ† S$, by power set definition have h1 : (A βŠ† S) ∧ (B βŠ† S), from by {split,apply set.subset_of_mem_powerset,exact hA,apply set.subset_of_mem_powerset,exact hB}, -- Then $(A ∩ B) βŠ† A$, by intersection of set is a subset have h2 : (A ∩ B) βŠ† A, from by apply set.inter_subset_left, -- Then $(A ∩ B) βŠ† S$, by subset relation is transitive have h3 : (A ∩ B) βŠ† S, from by {apply set.subset.trans h2 h1.left}, -- Hence $(A ∩ B) ∈ 𝒫 S$, by power set definition show (A ∩ B) ∈ 𝒫 S, from by {apply set.mem_powerset h3}, end /--`theorem` Square of Sum :$\forall x, y \in \R: \paren {x + y}^2 = x^2 + 2 x y + y^2$ `proof` Follows from the distribution of multiplication over addition: {{begin-eqn}} {{eqn | l = \left({x + y}\right)^2 | r = \left({x + y}\right) \cdot \left({x + y}\right) }} {{eqn | r = x \cdot \left({x + y}\right) + y \cdot \left({x + y}\right) | c = Real Multiplication Distributes over Addition }} {{eqn | r = x \cdot x + x \cdot y + y \cdot x + y \cdot y | c = Real Multiplication Distributes over Addition }} {{eqn | r = x^2 + 2xy + y^2 | c = }} {{end-eqn}} {{qed}} -/ theorem square_of_sum (x y : ℝ) : (x + y)^2 = (x^2 + 2*x*y + y^2) := begin -- expand the power calc (x + y)^2 = (x+y)*(x+y) : by rw sq -- distributive property of multiplication over addition gives: ... = x*(x+y) + y*(x+y) : by rw add_mul -- applying the above property further gives: ... = x*x + x*y + y*x + y*y : by {rw [mul_comm x (x+y),mul_comm y (x+y)], rw [add_mul,add_mul], ring} -- rearranging the terms using commutativity and adding gives: ... = x^2 + 2*x*y + y^2 : by {repeat {rw ← sq}, rw mul_comm y x, ring} end /--`theorem` Identity of Group is Unique Let $\struct {G, \circ}$ be a group. Then there is a unique identity element $e \in G$. `proof` From Group has Latin Square Property, there exists a unique $x \in G$ such that: :$a x = b$ and there exists a unique $y \in G$ such that: :$y a = b$ Setting $b = a$, this becomes: There exists a unique $x \in G$ such that: :$a x = a$ and there exists a unique $y \in G$ such that: :$y a = a$ These $x$ and $y$ are both $e$, by definition of identity element. {{qed}} -/ theorem group_identity_unique {G : Type*} [group G] : βˆƒ! e : G, βˆ€ a : G, e * a = a ∧ a * e = a := begin -- Group has Latin Square Property have h1 : βˆ€ a b : G, βˆƒ! x : G, a * x = b, from by { assume a b : G, use a⁻¹ * b, obviously, }, have h2 : βˆ€ a b : G, βˆƒ! y : G, y * a = b, from by { assume a b : G, use b * a⁻¹, obviously, }, -- Setting $b = a$, this becomes: have h3 : βˆ€ a : G, βˆƒ! x : G, a * x = a, from assume a : G, h1 a a, have h4 : βˆ€ a : G, βˆƒ! y : G, y * a = a, from assume a : G, h2 a a, -- These $x$ and $y$ are both $(1 : G)$, by definition of identity element have h5 : βˆ€ a : G, classical.some (h3 a).exists = (1 : G), from assume a :G, exists_unique.unique (h3 a) (classical.some_spec (exists_unique.exists (h3 a))) (mul_one a), have h6 : βˆ€ a : G, classical.some (h4 a).exists = (1 : G), from assume a : G, exists_unique.unique (h4 a) (classical.some_spec (exists_unique.exists (h4 a))) (one_mul a), show βˆƒ! e : G, βˆ€ a : G, e * a = a ∧ a * e = a, from by { use (1 : G), have h7 : βˆ€ e : G, (βˆ€ a : G, e * a = a ∧ a * e = a) β†’ e = 1, from by { assume (e : G) (hident : βˆ€ a : G, e * a = a ∧ a * e = a), have h8 : βˆ€ a : G, e = classical.some (h3 a).exists, from assume (a : G), exists_unique.unique (h3 a) (hident a).right (classical.some_spec (exists_unique.exists (h3 a))), have h9 : βˆ€ a : G, e = classical.some (h4 a).exists, from assume (a : G), exists_unique.unique (h4 a) (hident a).left (classical.some_spec (exists_unique.exists (h4 a))), show e = (1 : G), from eq.trans (h9 e) (h6 _), }, exact ⟨by obviously, h7⟩, } end /--`theorem` Squeeze Theorem for Real Numbers Let $\sequence {x_n}$, $\sequence {y_n}$ and $\sequence {z_n}$ be sequences in $\R$. Let $\sequence {y_n}$ and $\sequence {z_n}$ both be convergent to the following limit: :$\ds \lim_{n \mathop \to \infty} y_n = l, \lim_{n \mathop \to \infty} z_n = l$ Suppose that: :$\forall n \in \N: y_n \le x_n \le z_n$ Then: :$x_n \to l$ as $n \to \infty$ that is: :$\ds \lim_{n \mathop \to \infty} x_n = l$ `proof` From Negative of Absolute Value: :$\size {x - l} < \epsilon \iff l - \epsilon < x < l + \epsilon$ Let $\epsilon > 0$. We need to prove that: :$\exists N: \forall n > N: \size {x_n - l} < \epsilon$ As $\ds \lim_{n \mathop \to \infty} y_n = l$ we know that: :$\exists N_1: \forall n > N_1: \size {y_n - l} < \epsilon$ As $\ds \lim_{n \mathop \to \infty} z_n = l$ we know that: :$\exists N_2: \forall n > N_2: \size {z_n - l} < \epsilon$ Let $N = \max \set {N_1, N_2}$. Then if $n > N$, it follows that $n > N_1$ and $n > N_2$. So: :$\forall n > N: l - \epsilon < y_n < l + \epsilon$ :$\forall n > N: l - \epsilon < z_n < l + \epsilon$ But: :$\forall n \in \N: y_n \le x_n \le z_n$ So: :$\forall n > N: l - \epsilon < y_n \le x_n \le z_n < l + \epsilon$ and so: :$\forall n > N: l - \epsilon < x_n < l + \epsilon$ So: :$\forall n > N: \size {x_n - l} < \epsilon$ Hence the result. {{qed}} -/ theorem squeeze_theorem_real_numbers (x y z : β„• β†’ ℝ) (l : ℝ) : let seq_limit : (β„• β†’ ℝ) β†’ ℝ β†’ Prop := Ξ» (u : β„• β†’ ℝ) (l : ℝ), βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ n > N, |u n - l| < Ξ΅ in seq_limit y l β†’ seq_limit z l β†’ (βˆ€ n : β„•, (y n) ≀ (x n) ∧ (x n) ≀ (z n)) β†’ seq_limit x l := begin assume seq_limit (h2 : seq_limit y l) (h3 : seq_limit z l) (h4 : βˆ€ (n : β„•), y n ≀ x n ∧ x n ≀ z n) (Ξ΅), --From Negative of Absolute Value: $\size {x - l} < \epsilon \iff l - \epsilon < x < l + \epsilon$ have h5 : βˆ€ x, |x - l| < Ξ΅ ↔ (((l - Ξ΅) < x) ∧ (x < (l + Ξ΅))), from by { intro x0, have h6 : |x0 - l| < Ξ΅ ↔ ((x0 - l) < Ξ΅) ∧ ((l - x0) < Ξ΅), from abs_sub_lt_iff, rw h6, split, rintro ⟨ S_1, S_2 ⟩, split; linarith, rintro ⟨ S_3, S_4 ⟩, split; linarith, }, --Let $\epsilon > 0$. assume (h7 : Ξ΅ > 0), --As $\ds \lim_{n \mathop \to \infty} y_n = l$ we know that $\exists N_1: \forall n > N_1: \size {y_n - l} < \epsilon$ cases h2 Ξ΅ h7 with N1 h8, --As $\ds \lim_{n \mathop \to \infty} z_n = l$ we know that $\exists N_2: \forall n > N_2: \size {z_n - l} < \epsilon$ cases h3 Ξ΅ h7 with N2 h9, --Let $N = \max \set {N_1, N_2}$. let N := max N1 N2, use N, --Then if $n > N$, it follows that $n > N_1$ and $n > N_2$. have h10 : βˆ€ n > N, n > N1 ∧ n > N2 := by { assume n h, split, exact lt_of_le_of_lt (le_max_left N1 N2) h, exact lt_of_le_of_lt (le_max_right N1 N2) h, }, --$\forall n > N: l - \epsilon < y_n < l + \epsilon$ --$\forall n > N: l - \epsilon < z_n < l + \epsilon$ --$\forall n \in \N: y_n \le x_n \le z_n$ --So $\forall n > N: l - \epsilon < y_n \le x_n \le z_n < l + \epsilon$ have h11 : βˆ€ n > N, (((l - Ξ΅) < (y n)) ∧ ((y n) ≀ (x n))) ∧ (((x n) ≀ (z n)) ∧ ((z n) < l+Ξ΅)), from by { intros n h12, split, { have h13 := (h8 n (h10 n h12).left), rw h5 (y n) at h13, split, exact h13.left, exact (h4 n).left, }, { have h14 := (h9 n (h10 n h12).right),rw h5 (z n) at h14, split, exact (h4 n).right, exact h14.right, }, }, --$\forall n > N: l - \epsilon < x_n < l + \epsilon$ have h15 : βˆ€ n > N, ((l - Ξ΅) < (x n)) ∧ ((x n) < (l+Ξ΅)), from by { intros n1 h16, cases (h11 n1 h16); split; linarith, }, --So $\forall n > N: \size {x_n - l} < \epsilon$ --Hence the result show βˆ€ (n : β„•), n > N β†’ |x n - l| < Ξ΅, from by { intros n h17, cases h5 (x n) with h18 h19, apply h19, exact h15 n h17, }, end /--`theorem` Density of irrational orbit The fractional parts of the integer multiples of an irrational number form a dense subset of the unit interval `proof` Let $\alpha$ be an irrational number. Then for distinct $i, j \in \mathbb{Z}$, we must have $\{i \alpha\} \neq\{j \alpha\}$. If this were not true, then $$ i \alpha-\lfloor i \alpha\rfloor=\{i \alpha\}=\{j \alpha\}=j \alpha-\lfloor j \alpha\rfloor, $$ which yields the false statement $\alpha=\frac{\lfloor i \alpha\rfloor-\lfloor j \alpha\rfloor}{i-j} \in \mathbb{Q}$. Hence, $$ S:=\{\{i \alpha\} \mid i \in \mathbb{Z}\} $$ is an infinite subset of $\left[0,1\right]$. By the Bolzano-Weierstrass theorem, $S$ has a limit point in $[0, 1]$. One can thus find pairs of elements of $S$ that are arbitrarily close. Since (the absolute value of) the difference of any two elements of $S$ is also an element of $S$, it follows that $0$ is a limit point of $S$. To show that $S$ is dense in $[0, 1]$, consider $y \in[0,1]$, and $\epsilon>0$. Then by selecting $x \in S$ such that $\{x\}<\epsilon$ (which exists as $0$ is a limit point), and $N$ such that $N \cdot\{x\} \leq y<(N+1) \cdot\{x\}$, we get: $|y-\{N x\}|<\epsilon$. QED -/ theorem FEW SHOT PROMPTS TO CODEX(END)-/
(**************************************************************************) (* *) (* DTBitVector *) (* Copyright (C) 2011 - 2016 *) (* *) (* Chantal Keller * *) (* Alain Mebsout β™― *) (* Burak Ekici β™― *) (* *) (* * Inria - Γ‰cole Polytechnique - UniversitΓ© Paris-Sud *) (* β™― The University of Iowa *) (* *) (* This file is distributed under the terms of the CeCILL-C licence *) (* *) (**************************************************************************) Require Import List Bool NArith Psatz (*Int63*) ZArith Nnat. Require Import Lia. Require Import Coq.Structures.Equalities. Require Import ClassicalFacts. Require Import Coq.Logic.Classical_Prop. Require Import Coq.Reals.ArithProp. (*Require Import Misc.*) Import ListNotations. Local Open Scope list_scope. Local Open Scope N_scope. Local Open Scope bool_scope. Set Implicit Arguments. Unset Strict Implicit. From Hammer Require Import Hammer Reconstr. (* We temporarily assume proof irrelevance to handle dependently typed bit vectors *) Axiom proof_irrelevance : forall (P : Prop) (p1 p2 : P), p1 = p2. Lemma inj a a' : N.to_nat a = N.to_nat a' -> a = a'. Proof. intros. lia. Qed. Fixpoint leb (n m: nat) : bool := match n with | O => match m with | O => true | S m' => true end | S n' => match m with | O => false | S m' => leb n' m' end end. (* The following allows us to use the lemma Lemma eqb_sym x y : (x =? y) = (y =? x). This lemma is in functor BoolEqualityFacts which is parametrized by a BooleanEqualityType' *) Module N <: BooleanEqualityType'. Definition t := N. Definition eq := @eq N. Lemma eq_equiv : Equivalence eq. Proof. split; congruence. Qed. Definition eqb := N.eqb. Lemma eqb_eq : forall n m, eqb n m = true <-> eq n m. Proof. apply N.eqb_eq. Qed. End N. Module Import NBoolEqualityFacts := BoolEqualityFacts(N). Module Type BITVECTOR. Parameter bitvector : N -> Type. Parameter bits : forall n, bitvector n -> list bool. Parameter of_bits : forall (l:list bool), bitvector (N.of_nat (List.length l)). Parameter bitOf : forall n, nat -> bitvector n -> bool. (* Constants *) Parameter zeros : forall n, bitvector n. Parameter one : forall n, bitvector n. (* Equality *) Parameter bv_eq : forall n, bitvector n -> bitvector n -> bool. (* Binary Operations *) Parameter bv_concat : forall n m, bitvector n -> bitvector m -> bitvector (n + m). Parameter bv_and : forall n, bitvector n -> bitvector n -> bitvector n. Parameter bv_or : forall n, bitvector n -> bitvector n -> bitvector n. Parameter bv_xor : forall n, bitvector n -> bitvector n -> bitvector n. Parameter bv_add : forall n, bitvector n -> bitvector n -> bitvector n. Parameter bv_subt : forall n, bitvector n -> bitvector n -> bitvector n. Parameter bv_mult : forall n, bitvector n -> bitvector n -> bitvector n. Parameter bv_ult : forall n, bitvector n -> bitvector n -> bool. Parameter bv_ultP : forall n, bitvector n -> bitvector n -> Prop. Parameter bv_ugt : forall n, bitvector n -> bitvector n -> bool. Parameter bv_ugtP : forall n, bitvector n -> bitvector n -> Prop. Parameter bv_slt : forall n, bitvector n -> bitvector n -> bool. Parameter bv_sltP : forall n, bitvector n -> bitvector n -> Prop. Parameter bv_ule : forall n, bitvector n -> bitvector n -> bool. Parameter bv_uleP : forall n, bitvector n -> bitvector n -> Prop. Parameter bv_shl : forall n, bitvector n -> bitvector n -> bitvector n. Parameter bv_shr : forall n, bitvector n -> bitvector n -> bitvector n. Parameter bv_ashr : forall n, bitvector n -> bitvector n -> bitvector n. (* Unary Operations *) Parameter bv_not : forall n, bitvector n -> bitvector n. Parameter bv_neg : forall n, bitvector n -> bitvector n. Parameter bv_extr : forall (i n0 n1 : N), bitvector n1 -> bitvector n0. Parameter nat2bv : forall (n: nat) (s: N), bitvector s. (* Parameter bv_extr : forall (n i j : N) {H0: n >= j} {H1: j >= i}, bitvector n -> bitvector (j - i). *) Parameter bv_zextn : forall (n i: N), bitvector n -> bitvector (i + n). Parameter bv_sextn : forall (n i: N), bitvector n -> bitvector (i + n). (* Parameter bv_extr : forall n i j : N, bitvector n -> n >= j -> j >= i -> bitvector (j - i). *) (* Specification *) Axiom bits_size : forall n (bv:bitvector n), List.length (bits bv) = N.to_nat n. Axiom bv_eq_reflect : forall n (a b:bitvector n), bv_eq a b = true <-> a = b. Axiom bv_eq_refl : forall n (a:bitvector n), bv_eq a a = true. Axiom bv_ult_B2P : forall n (a b:bitvector n), bv_ult a b = true <-> bv_ultP a b. Axiom bv_slt_B2P : forall n (a b:bitvector n), bv_slt a b = true <-> bv_sltP a b. Axiom bv_ult_not_eq : forall n (a b:bitvector n), bv_ult a b = true -> a <> b. Axiom bv_slt_not_eq : forall n (a b:bitvector n), bv_slt a b = true -> a <> b. Axiom bv_ult_not_eqP: forall n (a b:bitvector n), bv_ultP a b -> a <> b. Axiom bv_slt_not_eqP: forall n (a b:bitvector n), bv_sltP a b -> a <> b. Axiom bv_ugt_B2P : forall n (a b:bitvector n), bv_ugt a b = true <-> bv_ugtP a b. Axiom bv_ugt_not_eq : forall n (a b:bitvector n), bv_ugt a b = true -> a <> b. Axiom bv_ugt_not_eqP: forall n (a b:bitvector n), bv_ugtP a b -> a <> b. Axiom bv_ule_B2P : forall n (a b:bitvector n), bv_ule a b = true <-> bv_uleP a b. Axiom bv_and_comm : forall n (a b:bitvector n), bv_eq (bv_and a b) (bv_and b a) = true. Axiom bv_or_comm : forall n (a b:bitvector n), bv_eq (bv_or a b) (bv_or b a) = true. Axiom bv_add_comm : forall n (a b:bitvector n), bv_eq (bv_add a b) (bv_add b a) = true. Axiom bv_and_assoc : forall n (a b c: bitvector n), bv_eq (bv_and a (bv_and b c)) (bv_and (bv_and a b) c) = true. Axiom bv_or_assoc : forall n (a b c: bitvector n), bv_eq (bv_or a (bv_or b c)) (bv_or (bv_or a b) c) = true. Axiom bv_xor_assoc : forall n (a b c: bitvector n), bv_eq (bv_xor a (bv_xor b c)) (bv_xor (bv_xor a b) c) = true. Axiom bv_add_assoc : forall n (a b c: bitvector n), bv_eq (bv_add a (bv_add b c)) (bv_add (bv_add a b) c) = true. Axiom bv_not_involutive: forall n (a: bitvector n), bv_eq (bv_not (bv_not a)) a = true. Parameter _of_bits : forall (l: list bool) (s : N), bitvector s. End BITVECTOR. Module Type RAWBITVECTOR. Parameter bitvector : Type. Parameter size : bitvector -> N. Parameter bits : bitvector -> list bool. Parameter of_bits : list bool -> bitvector. Parameter _of_bits : list bool -> N -> bitvector. Parameter bitOf : nat -> bitvector -> bool. (* Constants *) Parameter zeros : N -> bitvector. Parameter one : N -> bitvector. (* Equality *) Parameter bv_eq : bitvector -> bitvector -> bool. (* Binary Operations *) Parameter bv_concat : bitvector -> bitvector -> bitvector. Parameter bv_and : bitvector -> bitvector -> bitvector. Parameter bv_or : bitvector -> bitvector -> bitvector. Parameter bv_xor : bitvector -> bitvector -> bitvector. Parameter bv_add : bitvector -> bitvector -> bitvector. Parameter bv_mult : bitvector -> bitvector -> bitvector. Parameter bv_subt : bitvector -> bitvector -> bitvector. Parameter bv_ult : bitvector -> bitvector -> bool. Parameter bv_ultP : bitvector -> bitvector -> Prop. Parameter bv_slt : bitvector -> bitvector -> bool. Parameter bv_sltP : bitvector -> bitvector -> Prop. Parameter bv_ugt : bitvector -> bitvector -> bool. Parameter bv_ugtP : bitvector -> bitvector -> Prop. Parameter bv_ule : bitvector -> bitvector -> bool. Parameter bv_uleP : bitvector -> bitvector -> Prop. Parameter bv_shl : bitvector -> bitvector -> bitvector. Parameter bv_shr : bitvector -> bitvector -> bitvector. Parameter bv_ashr : bitvector -> bitvector -> bitvector. (* Unary Operations *) Parameter bv_not : bitvector -> bitvector. Parameter bv_neg : bitvector -> bitvector. Parameter bv_extr : forall (i n0 n1: N), bitvector -> bitvector. (*Parameter bv_extr : forall (n i j: N) {H0: n >= j} {H1: j >= i}, bitvector -> bitvector.*) Parameter bv_zextn : forall (n i: N), bitvector -> bitvector. Parameter bv_sextn : forall (n i: N), bitvector -> bitvector. Parameter nat2bv : forall (n: nat) (s: N), bitvector. (* All the operations are size-preserving *) Axiom bits_size : forall bv, List.length (bits bv) = N.to_nat (size bv). Axiom of_bits_size : forall l, N.to_nat (size (of_bits l)) = List.length l. Axiom _of_bits_size : forall l s,(size (_of_bits l s)) = s. Axiom zeros_size : forall n, size (zeros n) = n. Axiom one_size : forall n, size (one n) = n. Axiom bv_concat_size : forall n m a b, size a = n -> size b = m -> size (bv_concat a b) = n + m. Axiom bv_and_size : forall n a b, size a = n -> size b = n -> size (bv_and a b) = n. Axiom bv_or_size : forall n a b, size a = n -> size b = n -> size (bv_or a b) = n. Axiom bv_xor_size : forall n a b, size a = n -> size b = n -> size (bv_xor a b) = n. Axiom bv_add_size : forall n a b, size a = n -> size b = n -> size (bv_add a b) = n. Axiom bv_subt_size : forall n a b, size a = n -> size b = n -> size (bv_subt a b) = n. Axiom bv_mult_size : forall n a b, size a = n -> size b = n -> size (bv_mult a b) = n. Axiom bv_not_size : forall n a, size a = n -> size (bv_not a) = n. Axiom bv_neg_size : forall n a, size a = n -> size (bv_neg a) = n. Axiom bv_shl_size : forall n a b, size a = n -> size b = n -> size (bv_shl a b) = n. Axiom bv_shr_size : forall n a b, size a = n -> size b = n -> size (bv_shr a b) = n. Axiom bv_ashr_size : forall n a b, size a = n -> size b = n -> size (bv_ashr a b) = n. Axiom bv_extr_size : forall i n0 n1 a, size a = n1 -> size (@bv_extr i n0 n1 a) = n0. (* Axiom bv_extr_size : forall n (i j: N) a (H0: n >= j) (H1: j >= i), size a = n -> size (@bv_extr n i j H0 H1 a) = (j - i). *) Axiom bv_zextn_size : forall (n i: N) a, size a = n -> size (@bv_zextn n i a) = (i + n). Axiom bv_sextn_size : forall (n i: N) a, size a = n -> size (@bv_sextn n i a) = (i + n). (* Specification *) Axiom bv_eq_reflect : forall a b, bv_eq a b = true <-> a = b. Axiom bv_eq_refl : forall a, bv_eq a a = true. Axiom bv_ult_not_eq : forall a b, bv_ult a b = true -> a <> b. Axiom bv_slt_not_eq : forall a b, bv_slt a b = true -> a <> b. Axiom bv_ult_not_eqP : forall a b, bv_ultP a b -> a <> b. Axiom bv_slt_not_eqP : forall a b, bv_sltP a b -> a <> b. Axiom bv_ult_B2P : forall a b, bv_ult a b = true <-> bv_ultP a b. Axiom bv_slt_B2P : forall a b, bv_slt a b = true <-> bv_sltP a b. Axiom bv_ugt_not_eq : forall a b, bv_ugt a b = true -> a <> b. Axiom bv_ugt_not_eqP : forall a b, bv_ugtP a b -> a <> b. Axiom bv_ugt_B2P : forall a b, bv_ugt a b = true <-> bv_ugtP a b. Axiom bv_ule_B2P : forall a b, bv_ule a b = true <-> bv_uleP a b. Axiom bv_and_comm : forall n a b, size a = n -> size b = n -> bv_and a b = bv_and b a. Axiom bv_or_comm : forall n a b, size a = n -> size b = n -> bv_or a b = bv_or b a. Axiom bv_add_comm : forall n a b, size a = n -> size b = n -> bv_add a b = bv_add b a. Axiom bv_and_assoc : forall n a b c, size a = n -> size b = n -> size c = n -> (bv_and a (bv_and b c)) = (bv_and (bv_and a b) c). Axiom bv_or_assoc : forall n a b c, size a = n -> size b = n -> size c = n -> (bv_or a (bv_or b c)) = (bv_or (bv_or a b) c). Axiom bv_xor_assoc : forall n a b c, size a = n -> size b = n -> size c = n -> (bv_xor a (bv_xor b c)) = (bv_xor (bv_xor a b) c). Axiom bv_add_assoc : forall n a b c, size a = n -> size b = n -> size c = n -> (bv_add a (bv_add b c)) = (bv_add (bv_add a b) c). Axiom bv_not_involutive: forall a, bv_not (bv_not a) = a. Axiom nat2bv_size : forall (n: nat) (s: N), size (nat2bv n s) = s. End RAWBITVECTOR. Module RAW2BITVECTOR (M:RAWBITVECTOR) <: BITVECTOR. Record bitvector_ (n:N) : Type := MkBitvector { bv :> M.bitvector; wf : M.size bv = n }. Definition bitvector := bitvector_. Definition bits n (bv:bitvector n) := M.bits bv. Lemma of_bits_size l : M.size (M.of_bits l) = N.of_nat (List.length l). Proof. now rewrite <- M.of_bits_size, N2Nat.id. Qed. Lemma _of_bits_size l s: M.size (M._of_bits l s) = s. Proof. apply (M._of_bits_size l s). Qed. Definition of_bits (l:list bool) : bitvector (N.of_nat (List.length l)) := @MkBitvector _ (M.of_bits l) (of_bits_size l). Definition _of_bits (l: list bool) (s : N): bitvector s := @MkBitvector _ (M._of_bits l s) (_of_bits_size l s). Definition bitOf n p (bv:bitvector n) : bool := M.bitOf p bv. Definition zeros (n:N) : bitvector n := @MkBitvector _ (M.zeros n) (M.zeros_size n). Definition one (n:N) : bitvector n := @MkBitvector _ (M.one n) (M.one_size n). Definition nat2bv (n: nat) (s: N): bitvector s. Proof. specialize (@MkBitvector s (M.nat2bv n s)); intros. apply X. now rewrite M.nat2bv_size. Defined. Definition bv_eq n (bv1 bv2:bitvector n) := M.bv_eq bv1 bv2. Definition bv_ultP n (bv1 bv2:bitvector n) := M.bv_ultP bv1 bv2. Definition bv_uleP n (bv1 bv2:bitvector n) := M.bv_uleP bv1 bv2. Definition bv_sltP n (bv1 bv2:bitvector n) := M.bv_sltP bv1 bv2. Definition bv_ugtP n (bv1 bv2:bitvector n) := M.bv_ugtP bv1 bv2. Definition bv_and n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_and bv1 bv2) (M.bv_and_size (wf bv1) (wf bv2)). Definition bv_or n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_or bv1 bv2) (M.bv_or_size (wf bv1) (wf bv2)). Definition bv_add n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_add bv1 bv2) (M.bv_add_size (wf bv1) (wf bv2)). Definition bv_subt n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_subt bv1 bv2) (M.bv_subt_size (wf bv1) (wf bv2)). Definition bv_mult n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_mult bv1 bv2) (M.bv_mult_size (wf bv1) (wf bv2)). Definition bv_xor n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_xor bv1 bv2) (M.bv_xor_size (wf bv1) (wf bv2)). Definition bv_ult n (bv1 bv2:bitvector n) : bool := M.bv_ult bv1 bv2. Definition bv_slt n (bv1 bv2:bitvector n) : bool := M.bv_slt bv1 bv2. Definition bv_ugt n (bv1 bv2:bitvector n) : bool := M.bv_ugt bv1 bv2. Definition bv_ule n (bv1 bv2:bitvector n) : bool := M.bv_ule bv1 bv2. Definition bv_not n (bv1: bitvector n) : bitvector n := @MkBitvector n (M.bv_not bv1) (M.bv_not_size (wf bv1)). Definition bv_neg n (bv1: bitvector n) : bitvector n := @MkBitvector n (M.bv_neg bv1) (M.bv_neg_size (wf bv1)). Definition bv_concat n m (bv1:bitvector n) (bv2: bitvector m) : bitvector (n + m) := @MkBitvector (n + m) (M.bv_concat bv1 bv2) (M.bv_concat_size (wf bv1) (wf bv2)). Definition bv_extr (i n0 n1: N) (bv1: bitvector n1) : bitvector n0 := @MkBitvector n0 (@M.bv_extr i n0 n1 bv1) (@M.bv_extr_size i n0 n1 bv1 (wf bv1)). (* Definition bv_extr n (i j: N) (H0: n >= j) (H1: j >= i) (bv1: bitvector n) : bitvector (j - i) := @MkBitvector (j - i) (@M.bv_extr n i j H0 H1 bv1) (@M.bv_extr_size n i j bv1 H0 H1 (wf bv1)). *) Definition bv_zextn n (i: N) (bv1: bitvector n) : bitvector (i + n) := @MkBitvector (i + n) (@M.bv_zextn n i bv1) (@M.bv_zextn_size n i bv1 (wf bv1)). Definition bv_sextn n (i: N) (bv1: bitvector n) : bitvector (i + n) := @MkBitvector (i + n) (@M.bv_sextn n i bv1) (@M.bv_sextn_size n i bv1 (wf bv1)). Definition bv_shl n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_shl bv1 bv2) (M.bv_shl_size (wf bv1) (wf bv2)). Definition bv_shr n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_shr bv1 bv2) (M.bv_shr_size (wf bv1) (wf bv2)). Definition bv_ashr n (bv1 bv2:bitvector n) : bitvector n := @MkBitvector n (M.bv_ashr bv1 bv2) (M.bv_ashr_size (wf bv1) (wf bv2)). Lemma bits_size n (bv:bitvector n) : List.length (bits bv) = N.to_nat n. Proof. unfold bits. now rewrite M.bits_size, wf. Qed. (* The next lemma is provable only if we assume proof irrelevance *) Lemma bv_eq_reflect n (a b: bitvector n) : bv_eq a b = true <-> a = b. Proof. unfold bv_eq. rewrite M.bv_eq_reflect. split. - revert a b. intros [a Ha] [b Hb]. simpl. intros ->. rewrite (proof_irrelevance Ha Hb). reflexivity. - intros. case a in *. case b in *. simpl in *. now inversion H. (* now intros ->. *) Qed. Lemma bv_eq_refl n (a : bitvector n) : bv_eq a a = true. Proof. unfold bv_eq. now rewrite M.bv_eq_reflect. Qed. Lemma bv_ult_not_eqP: forall n (a b: bitvector n), bv_ultP a b -> a <> b. Proof. unfold bv_ultP, bv_ult. intros n a b H. apply M.bv_ult_not_eqP in H. unfold not in *; intros. apply H. apply M.bv_eq_reflect. rewrite H0. apply M.bv_eq_refl. Qed. Lemma bv_slt_not_eqP: forall n (a b: bitvector n), bv_sltP a b -> a <> b. Proof. unfold bv_sltP, bv_slt. intros n a b H. apply M.bv_slt_not_eqP in H. unfold not in *; intros. apply H. apply M.bv_eq_reflect. rewrite H0. apply M.bv_eq_refl. Qed. Lemma bv_ugt_not_eqP: forall n (a b: bitvector n), bv_ugtP a b -> a <> b. Proof. unfold bv_ugtP, bv_ugt. intros n a b H. apply M.bv_ugt_not_eqP in H. unfold not in *; intros. apply H. apply M.bv_eq_reflect. rewrite H0. apply M.bv_eq_refl. Qed. Lemma bv_ult_not_eq: forall n (a b: bitvector n), bv_ult a b = true -> a <> b. Proof. unfold bv_ult. intros n a b H. apply M.bv_ult_not_eq in H. unfold not in *; intros. apply H. apply M.bv_eq_reflect. rewrite H0. apply M.bv_eq_refl. Qed. Lemma bv_slt_not_eq: forall n (a b: bitvector n), bv_slt a b = true -> a <> b. Proof. unfold bv_slt. intros n a b H. apply M.bv_slt_not_eq in H. unfold not in *; intros. apply H. apply M.bv_eq_reflect. rewrite H0. apply M.bv_eq_refl. Qed. Lemma bv_ugt_not_eq: forall n (a b: bitvector n), bv_ugt a b = true -> a <> b. Proof. unfold bv_ugt. intros n a b H. apply M.bv_ugt_not_eq in H. unfold not in *; intros. apply H. apply M.bv_eq_reflect. rewrite H0. apply M.bv_eq_refl. Qed. Lemma bv_ult_B2P: forall n (a b: bitvector n), bv_ult a b = true <-> bv_ultP a b. Proof. unfold bv_ultP, bv_ult; intros; split; intros; now apply M.bv_ult_B2P. Qed. Lemma bv_ule_B2P: forall n (a b: bitvector n), bv_ule a b = true <-> bv_uleP a b. Proof. unfold bv_uleP, bv_ule; intros; split; intros; now apply M.bv_ule_B2P. Qed. Lemma bv_slt_B2P: forall n (a b: bitvector n), bv_slt a b = true <-> bv_sltP a b. Proof. unfold bv_sltP, bv_slt; intros; split; intros; now apply M.bv_slt_B2P. Qed. Lemma bv_ugt_B2P: forall n (a b: bitvector n), bv_ugt a b = true <-> bv_ugtP a b. Proof. unfold bv_ugtP, bv_ugt; intros; split; intros; now apply M.bv_ugt_B2P. Qed. Lemma bv_and_comm n (a b:bitvector n) : bv_eq (bv_and a b) (bv_and b a) = true. Proof. unfold bv_eq. rewrite M.bv_eq_reflect. apply (@M.bv_and_comm n); now rewrite wf. Qed. Lemma bv_or_comm n (a b:bitvector n) : bv_eq (bv_or a b) (bv_or b a) = true. Proof. unfold bv_eq. rewrite M.bv_eq_reflect. apply (@M.bv_or_comm n); now rewrite wf. Qed. Lemma bv_add_comm n (a b:bitvector n) : bv_eq (bv_add a b) (bv_add b a) = true. Proof. unfold bv_eq. rewrite M.bv_eq_reflect. apply (@M.bv_add_comm n); now rewrite wf. Qed. Lemma bv_and_assoc : forall n (a b c :bitvector n), bv_eq (bv_and a (bv_and b c)) (bv_and (bv_and a b) c) = true. Proof. intros n a b c. unfold bv_eq. rewrite M.bv_eq_reflect. simpl. apply (@M.bv_and_assoc n a b c); now rewrite wf. Qed. Lemma bv_or_assoc : forall n (a b c :bitvector n), bv_eq (bv_or a (bv_or b c)) (bv_or (bv_or a b) c) = true. Proof. intros n a b c. unfold bv_eq. rewrite M.bv_eq_reflect. simpl. apply (@M.bv_or_assoc n a b c); now rewrite wf. Qed. Lemma bv_xor_assoc : forall n (a b c :bitvector n), bv_eq (bv_xor a (bv_xor b c)) (bv_xor (bv_xor a b) c) = true. Proof. intros n a b c. unfold bv_eq. rewrite M.bv_eq_reflect. simpl. apply (@M.bv_xor_assoc n a b c); now rewrite wf. Qed. Lemma bv_add_assoc : forall n (a b c :bitvector n), bv_eq (bv_add a (bv_add b c)) (bv_add (bv_add a b) c) = true. Proof. intros n a b c. unfold bv_eq. rewrite M.bv_eq_reflect. simpl. apply (@M.bv_add_assoc n a b c); now rewrite wf. Qed. Lemma bv_not_involutive: forall n (a: bitvector n), bv_eq (bv_not (bv_not a)) a = true. Proof. intros n a. unfold bv_eq. rewrite M.bv_eq_reflect. simpl. apply (@M.bv_not_involutive a); now rewrite wf. Qed. End RAW2BITVECTOR. Module RAWBITVECTOR_LIST <: RAWBITVECTOR. Definition bitvector := list bool. Definition bits (a:bitvector) : list bool := a. Definition size (a:bitvector) := N.of_nat (List.length a). Definition of_bits (a:list bool) : bitvector := a. Definition bitOf (n: nat) (a: bitvector): bool := nth n a false. Lemma bits_size bv : List.length (bits bv) = N.to_nat (size bv). Proof. unfold bits, size. now rewrite Nat2N.id. Qed. Lemma of_bits_size l : N.to_nat (size (of_bits l)) = List.length l. Proof. unfold of_bits, size. now rewrite Nat2N.id. Qed. (* forall x : bitvector, size(x) >= 0 *) Theorem length_of_tail : forall (h : bool) (t : list bool), length (h :: t) = S (length t). intros h t. Proof. induction t; reflexivity. Qed. Theorem non_empty_list_size : forall (h : bool) (t : list bool), N.to_nat (size (h :: t)) = S (N.to_nat (size t)). Proof. intros h t. induction t as [| h' t' IHt]. + reflexivity. + unfold size in *. rewrite -> length_of_tail. rewrite -> length_of_tail. rewrite -> Nat2N.id in *. rewrite -> Nat2N.id. reflexivity. Qed. Theorem succ_gt_pred : forall (n : nat), (n >= 0)%nat -> (S n >= 0)%nat. Proof. intros n. induction n as [| n' IHn]. + unfold ge. intros H. apply le_0_n. + unfold ge. intros H. auto. Qed. Theorem bv_size_nonnegative : forall (x : bitvector), (N.to_nat(size x) >= 0)%nat. Proof. intros x. induction x. - auto. - rewrite -> non_empty_list_size. unfold size in *. rewrite -> Nat2N.id in *. apply succ_gt_pred. apply IHx. Qed. Fixpoint mk_list_false_acc (t: nat) (acc: list bool) : list bool := match t with | O => acc | S t' => mk_list_false_acc t' (false::acc) end. Fixpoint mk_list_false (t: nat) : list bool := match t with | O => [] | S t' => false::(mk_list_false t') end. Definition zeros (n : N) : bitvector := mk_list_false (N.to_nat n). Lemma length_mk_list_false: forall n, length (mk_list_false n) = n. Proof. intro n. induction n as [ | n' IHn]. - simpl. auto. - simpl. apply f_equal. exact IHn. Qed. Lemma zeros_size (n : N) : size (zeros n) = n. Proof. unfold size, zeros. now rewrite length_mk_list_false, N2Nat.id. Qed. Definition _of_bits (a:list bool) (s: N) := if (N.of_nat (length a) =? s) then a else zeros s. Lemma _of_bits_size l s: (size (_of_bits l s)) = s. Proof. unfold of_bits, size. unfold _of_bits. case_eq ( N.of_nat (length l) =? s). intros. now rewrite N.eqb_eq in H. intros. unfold zeros. rewrite length_mk_list_false. now rewrite N2Nat.id. Qed. Section Map2. Variables A B C: Type. Variable f : A -> B -> C. Fixpoint map2 (l1 : list A) (l2 : list B) {struct l1} : list C := match l1, l2 with | b1::tl1, b2::tl2 => (f b1 b2)::(map2 tl1 tl2) | _, _ => nil end. End Map2. Section Fold_left2. Variables A B: Type. Variable f : A -> B -> B -> A. Fixpoint fold_left2 (xs ys: list B) (acc:A) {struct xs} : A := match xs, ys with | nil, _ | _, nil => acc | x::xs, y::ys => fold_left2 xs ys (f acc x y) end. Lemma foo : forall (I: A -> Prop) acc, I acc -> (forall a b1 b2, I a -> I (f a b1 b2)) -> forall xs ys, I (fold_left2 xs ys acc). Proof. intros I acc H0 H1 xs. revert acc H0. induction xs as [ | a xs IHxs]; intros acc H. simpl. auto. intros [ | b ys]. + simpl. exact H. + simpl. apply IHxs, H1. exact H. Qed. Fixpoint mk_list_true_acc (t: nat) (acc: list bool) : list bool := match t with | O => acc | S t' => mk_list_true_acc t' (true::acc) end. Fixpoint mk_list_true (t: nat) : list bool := match t with | S t' => true::(mk_list_true t') | O => [] end. End Fold_left2. Lemma length_mk_list_true: forall n, length (mk_list_true n) = n. Proof. intro n. induction n as [ | n' IHn]. - simpl. auto. - simpl. apply f_equal. exact IHn. Qed. Fixpoint mk_list_one (t: nat) : list bool := match t with | O => [] | S O => [true] | S t' => false :: (mk_list_one t') end. Definition one (n : N) : bitvector := rev (mk_list_one (N.to_nat n)). Lemma length_mk_list_one: forall n, length (rev (mk_list_one n)) = n. Proof. intro n. induction n as [| n' IHn]. - reflexivity. - assert (H: forall n, length (rev (mk_list_one (S n))) = S (length (rev(mk_list_one n)))). { intros n. rewrite -> rev_length. rewrite -> rev_length. induction n as [ | n'' IHn']. + reflexivity. + reflexivity. } rewrite -> H. rewrite -> IHn. reflexivity. Qed. Lemma one_size (n : N) : size (one n) = n. Proof. unfold size. unfold one. rewrite length_mk_list_one. rewrite N2Nat.id. reflexivity. Qed. Definition bv_concat (a b: bitvector) : bitvector := b ++ a. Proof. Lemma bv_concat_size n m a b : size a = n -> size b = m -> size (bv_concat a b) = (n + m)%N. unfold bv_concat, size. intros H0 H1. rewrite app_length, Nat2N.inj_add, H0, H1; now rewrite N.add_comm. Qed. (* rev (mk_list_true n) = mk_list_true n *) Lemma mk_list_true_succ : forall (n : nat), mk_list_true (S n) = true :: mk_list_true n. Proof. intros. easy. Qed. Lemma mk_list_true_app : forall (n : nat), mk_list_true (S n) = (mk_list_true n) ++ [true]. Proof. intros. induction n. + easy. + rewrite mk_list_true_succ. rewrite mk_list_true_succ. assert (forall (a b : bool) (l : list bool), (b :: l) ++ [a] = b :: (l ++ [a])). { easy. } rewrite H. rewrite <- IHn. rewrite mk_list_true_succ. easy. Qed. Lemma rev_mk_list_true : forall n : nat, rev (mk_list_true n) = mk_list_true n. Proof. intros. induction n. + easy. + simpl. rewrite IHn. induction n. - easy. - rewrite mk_list_true_app at 2. rewrite mk_list_true_succ at 1. easy. Qed. (* rev (mk_list_false n) = mk_list_false n *) Lemma mk_list_false_succ : forall (n : nat), mk_list_false (S n) = false :: mk_list_false n. Proof. intros. easy. Qed. Lemma mk_list_false_app : forall (n : nat), mk_list_false (S n) = (mk_list_false n) ++ [false]. Proof. intros. induction n. + easy. + rewrite mk_list_false_succ. rewrite mk_list_false_succ. assert (forall (a b : bool) (l : list bool), (b :: l) ++ [a] = b :: (l ++ [a])). { easy. } rewrite H. rewrite <- IHn. rewrite mk_list_false_succ. easy. Qed. Lemma rev_mk_list_false : forall n : nat, rev (mk_list_false n) = mk_list_false n. Proof. intros. induction n. + easy. + simpl. rewrite IHn. induction n. - easy. - rewrite mk_list_false_app at 2. rewrite mk_list_false_succ at 1. easy. Qed. (*-------------------- equality --------------------*) (* Definitions *) (* eq-defs *) Fixpoint beq_list (l m : list bool) {struct l} := match l, m with | nil, nil => true | x :: l', y :: m' => (Bool.eqb x y) && (beq_list l' m') | _, _ => false end. Definition bv_eq (a b: bitvector): bool:= if ((size a) =? (size b)) then beq_list (bits a) (bits b) else false. Fixpoint beq_listP (l m : list bool) {struct l} := match l, m with | nil, nil => True | x :: l', y :: m' => (x = y) /\ (beq_listP l' m') | _, _ => False end. (* Theorems *) (* eq-thrms *) Lemma bv_mk_eq l1 l2 : bv_eq l1 l2 = beq_list l1 l2. Proof. unfold bv_eq, size, bits. case_eq (Nat.eqb (length l1) (length l2)); intro Heq. - now rewrite (EqNat.beq_nat_true _ _ Heq), N.eqb_refl. - replace (N.of_nat (length l1) =? N.of_nat (length l2)) with false. * revert l2 Heq. induction l1 as [ |b1 l1 IHl1]; intros [ |b2 l2]; simpl in *; auto. intro Heq. now rewrite <- (IHl1 _ Heq), andb_false_r. * symmetry. rewrite N.eqb_neq. intro H. apply Nat2N.inj in H. rewrite H in Heq. rewrite <- EqNat.beq_nat_refl in Heq. discriminate. Qed. Lemma List_eq : forall (l m: list bool), beq_list l m = true <-> l = m. Proof. induction l; destruct m; simpl; split; intro; try (reflexivity || discriminate). - rewrite andb_true_iff in H. destruct H. rewrite eqb_true_iff in H. rewrite H. apply f_equal. apply IHl. exact H0. - inversion H. subst b. subst m. rewrite andb_true_iff. split. + apply eqb_reflx. + apply IHl; reflexivity. Qed. Lemma List_eqP : forall (l m: list bool), beq_listP l m <-> l = m. Proof. induction l; destruct m; simpl; split; intro; try (reflexivity || discriminate); try now contradict H. - destruct H. rewrite H. apply f_equal. apply IHl. exact H0. - inversion H. subst b. subst m. split. + reflexivity. + apply IHl; reflexivity. Qed. Lemma List_eq_refl : forall (l: list bool), beq_list l l = true. Proof. induction l; simpl; try (reflexivity || discriminate). - rewrite andb_true_iff. split. apply eqb_reflx. apply IHl. Qed. Lemma List_eqP_refl : forall (l: list bool), beq_listP l l <-> l = l. Proof. intro l. induction l as [ | xl xsl IHl ]; intros. - easy. - simpl. repeat split. now apply IHl. Qed. Lemma List_neq : forall (l m: list bool), beq_list l m = false -> l <> m. Proof. intro l. induction l. - intros. case m in *; simpl. now contradict H. easy. - intros. simpl in H. case_eq m; intros; rewrite H0 in H. easy. simpl. case_eq (Bool.eqb a b); intros. rewrite H1 in H. rewrite andb_true_l in H. apply Bool.eqb_prop in H1. specialize (IHl l0 H). rewrite H1. unfold not in *. intros. apply IHl. now inversion H2. apply Bool.eqb_false_iff in H1. unfold not in *. intros. apply H1. now inversion H2. Qed. Lemma List_neqP : forall (l m: list bool), ~beq_listP l m -> l <> m. Proof. intro l. induction l. - intros. case m in *; simpl. now contradict H. easy. - intros. unfold not in H. simpl in H. case_eq m; intros. easy. rewrite H0 in H. unfold not. intros. apply H. inversion H1. split; try easy. now apply List_eqP_refl. Qed. Lemma bv_eq_reflect a b : bv_eq a b = true <-> a = b. Proof. unfold bv_eq. case_eq (size a =? size b); intro Heq; simpl. - apply List_eq. - split; try discriminate. intro H. rewrite H, N.eqb_refl in Heq. discriminate. Qed. Lemma bv_eq_refl a: bv_eq a a = true. Proof. unfold bv_eq. rewrite N.eqb_refl. now apply List_eq. Qed. Lemma rev_eq: forall x y, beq_list x y = true -> beq_list (List.rev x) (List.rev y) = true. Proof. intros. apply List_eq in H. rewrite H. now apply List_eq_refl. Qed. Lemma rev_neq: forall x y, beq_list x y = false -> beq_list (List.rev x) (List.rev y) = false. Proof. intros. specialize (@List_neq x y H). intros. apply not_true_is_false. unfold not in *. intros. apply H0. apply List_eq in H1. specialize (f_equal (@List.rev bool) H1 ). intros. now rewrite !rev_involutive in H2. Qed. Lemma list_cases_all_true: forall l, l = mk_list_true (length l) \/ l <> mk_list_true (length l). Proof. induction l; intros. - now left. - cbn. destruct IHl as [IHl | IHl]. + case_eq a; intros. left. now rewrite IHl at 1. now right. + case_eq a; intros. right. Reconstr.reasy Reconstr.Empty Reconstr.Empty. right. easy. Qed. Lemma list_cases_all_false: forall l, l = mk_list_false (length l) \/ l <> mk_list_false (length l). Proof. induction l; intros. - now left. - cbn. destruct IHl as [IHl | IHl]. + case_eq a; intros. now right. left. now rewrite IHl at 1. + case_eq a; intros. right. Reconstr.reasy Reconstr.Empty Reconstr.Empty. right. Reconstr.reasy Reconstr.Empty Reconstr.Empty. Qed. (*-------------------- Logical Operations --------------------*) (*-------------------- and --------------------*) (* Definitions *) (* and-defs *) Definition bv_and (a b : bitvector) : bitvector := match (@size a) =? (@size b) with | true => map2 andb (@bits a) (@bits b) | _ => nil end. (* Theorems *) (* and-thrms *) (* list bitwise AND properties *) Lemma map2_and_comm: forall (a b: list bool), (map2 andb a b) = (map2 andb b a). Proof. intros a. induction a as [ | a' xs IHxs]. intros [ | b' ys]. - simpl. auto. - simpl. auto. - intros [ | b' ys]. + simpl. auto. + intros. simpl. cut (a' && b' = b' && a'). intro H. rewrite <- H. apply f_equal. apply IHxs. apply andb_comm. Qed. Lemma map2_and_assoc: forall (a b c: list bool), (map2 andb a (map2 andb b c)) = (map2 andb (map2 andb a b) c). Proof. intro a. induction a as [ | a' xs IHxs]. simpl. auto. intros [ | b' ys]. - simpl. auto. - intros [ | c' zs]. + simpl. auto. + simpl. cut (a' && (b' && c') = a' && b' && c'). intro H. rewrite <- H. apply f_equal. apply IHxs. apply andb_assoc. Qed. Lemma map2_and_idem1: forall (a b: list bool), (map2 andb (map2 andb a b) a) = (map2 andb a b). Proof. intros a. induction a as [ | a' xs IHxs]. intros [ | b' ys]. - simpl. auto. - simpl. auto. - intros [ | b' ys]. + simpl. auto. + intros. simpl. cut (a' && b' && a' = a' && b'). intro H. rewrite H. apply f_equal. apply IHxs. rewrite andb_comm, andb_assoc, andb_diag. reflexivity. Qed. Lemma map2_and_idem_comm: forall (a b: list bool), (map2 andb (map2 andb a b) a) = (map2 andb b a). Proof. intros a b. symmetry. rewrite <- map2_and_comm. symmetry; apply map2_and_idem1. Qed. Lemma map2_and_idem2: forall (a b: list bool), (map2 andb (map2 andb a b) b) = (map2 andb a b). Proof. intros a. induction a as [ | a' xs IHxs]. intros [ | b' ys]. - simpl. auto. - simpl. auto. - intros [ | b' ys]. + simpl. auto. + intros. simpl. cut (a' && b' && b' = a' && b'). intro H. rewrite H. apply f_equal. apply IHxs. rewrite <- andb_assoc. rewrite andb_diag. reflexivity. Qed. Lemma map2_and_idem_comm2: forall (a b: list bool), (map2 andb (map2 andb a b) b) = (map2 andb b a). Proof. intros a b. symmetry. rewrite <- map2_and_comm. symmetry; apply map2_and_idem2. Qed. Lemma map2_and_empty_empty1: forall (a: list bool), (map2 andb a []) = []. Proof. intros a. induction a as [ | a' xs IHxs]; simpl; auto. Qed. Lemma map2_and_empty_empty2: forall (a: list bool), (map2 andb [] a) = []. Proof. intros a. rewrite map2_and_comm. apply map2_and_empty_empty1. Qed. Lemma map2_nth_empty_false: forall (i: nat), nth i [] false = false. Proof. intros i. induction i as [ | IHi]; simpl; reflexivity. Qed. Lemma mk_list_true_equiv: forall t acc, mk_list_true_acc t acc = (List.rev (mk_list_true t)) ++ acc. Proof. induction t as [ |t IHt]; auto; intro acc; simpl; rewrite IHt. rewrite app_assoc_reverse. apply f_equal. simpl. reflexivity. Qed. Lemma mk_list_false_equiv: forall t acc, mk_list_false_acc t acc = (List.rev (mk_list_false t)) ++ acc. Proof. induction t as [ |t IHt]; auto; intro acc; simpl; rewrite IHt. rewrite app_assoc_reverse. apply f_equal. simpl. reflexivity. Qed. Lemma len_mk_list_true_empty: length (mk_list_true_acc 0 []) = 0%nat. Proof. simpl. reflexivity. Qed. Lemma add_mk_list_true: forall n acc, length (mk_list_true_acc n acc) = (n + length acc)%nat. Proof. intros n. induction n as [ | n' IHn]. + auto. + intro acc. simpl. rewrite IHn. simpl. lia. Qed. Lemma map2_and_nth_bitOf: forall (a b: list bool) (i: nat), (length a) = (length b) -> (i <= (length a))%nat -> nth i (map2 andb a b) false = (nth i a false) && (nth i b false). Proof. intro a. induction a as [ | a xs IHxs]. - intros [ | b ys]. + intros i H0 H1. do 2 rewrite map2_nth_empty_false. reflexivity. + intros i H0 H1. rewrite map2_and_empty_empty2. rewrite map2_nth_empty_false. reflexivity. - intros [ | b ys]. + intros i H0 H1. rewrite map2_and_empty_empty1. rewrite map2_nth_empty_false. rewrite andb_false_r. reflexivity. + intros i H0 H1. simpl. revert i H1. induction i as [ | i IHi]. * simpl. auto. * intros. apply IHxs. inversion H0; reflexivity. inversion H1; lia. Qed. Lemma length_mk_list_true_full: forall n, length (mk_list_true_acc n []) = n. Proof. intro n. rewrite (@add_mk_list_true n []). auto. Qed. Lemma mk_list_app: forall n acc, mk_list_true_acc n acc = mk_list_true_acc n [] ++ acc. Proof. intro n. induction n as [ | n IHn]. + auto. + intro acc. simpl in *. rewrite IHn. cut (mk_list_true_acc n [] ++ [true] = mk_list_true_acc n [true]). intro H. rewrite <- H. rewrite <- app_assoc. unfold app. reflexivity. rewrite <- IHn. reflexivity. Qed. Lemma mk_list_ltrue: forall n, mk_list_true_acc n [true] = mk_list_true_acc (S n) []. Proof. intro n. induction n as [ | n IHn]; auto. Qed. Lemma map2_and_1_neutral: forall (a: list bool), (map2 andb a (mk_list_true (length a))) = a. Proof. intro a. induction a as [ | a xs IHxs]. + auto. + simpl. rewrite IHxs. rewrite andb_true_r. reflexivity. Qed. Lemma map2_and_0_absorb: forall (a: list bool), (map2 andb a (mk_list_false (length a))) = (mk_list_false (length a)). Proof. intro a. induction a as [ | a' xs IHxs]. - simpl. reflexivity. - simpl. rewrite IHxs. rewrite andb_false_r; reflexivity. Qed. Lemma map2_and_length: forall (a b: list bool), length a = length b -> length a = length (map2 andb a b). Proof. induction a as [ | a' xs IHxs]. simpl. auto. intros [ | b ys]. - simpl. intros. exact H. - intros. simpl in *. apply f_equal. apply IHxs. inversion H; auto. Qed. (* bitvector AND properties *) Lemma bv_and_size n a b : size a = n -> size b = n -> size (bv_and a b) = n. Proof. unfold bv_and. intros H1 H2. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size in *. rewrite <- map2_and_length. - exact H1. - unfold bits. now rewrite <- Nat2N.inj_iff, H1. Qed. Lemma bv_and_comm n a b : size a = n -> size b = n -> bv_and a b = bv_and b a. Proof. intros H1 H2. unfold bv_and. rewrite H1, H2. rewrite N.eqb_compare, N.compare_refl. rewrite map2_and_comm. reflexivity. Qed. Lemma bv_and_assoc: forall n a b c, size a = n -> size b = n -> size c = n -> (bv_and a (bv_and b c)) = (bv_and (bv_and a b) c). Proof. intros n a b c H0 H1 H2. unfold bv_and, size, bits in *. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite H0. rewrite N.compare_refl. rewrite <- (@map2_and_length a b). rewrite <- map2_and_length. rewrite H0, H1. rewrite N.compare_refl. rewrite map2_and_assoc; reflexivity. now rewrite <- Nat2N.inj_iff, H1. now rewrite <- Nat2N.inj_iff, H0. Qed. Lemma bv_and_idem1: forall a b n, size a = n -> size b = n -> (bv_and (bv_and a b) a) = (bv_and a b). Proof. intros a b n H0 H1. unfold bv_and. rewrite H0. do 2 rewrite N.eqb_compare. unfold size in *. rewrite H1. rewrite N.compare_refl. rewrite <- H0. unfold bits. rewrite <- map2_and_length. rewrite N.compare_refl. rewrite map2_and_idem1; reflexivity. now rewrite <- Nat2N.inj_iff, H1. Qed. Lemma bv_and_idem2: forall a b n, size a = n -> size b = n -> (bv_and (bv_and a b) b) = (bv_and a b). Proof. intros a b n H0 H1. unfold bv_and. rewrite H0. do 2 rewrite N.eqb_compare. unfold size in *. rewrite H1. rewrite N.compare_refl. rewrite <- H0. unfold bits. rewrite <- map2_and_length. rewrite N.compare_refl. rewrite map2_and_idem2; reflexivity. now rewrite <- Nat2N.inj_iff, H1. Qed. Definition bv_empty: bitvector := nil. Lemma bv_and_empty_empty1: forall a, (bv_and a bv_empty) = bv_empty. Proof. intros a. unfold bv_empty, bv_and, size, bits. simpl. rewrite map2_and_empty_empty1. case_eq (N.compare (N.of_nat (length a)) 0); intro H; simpl. - apply (N.compare_eq (N.of_nat (length a))) in H. rewrite H. simpl. reflexivity. - rewrite N.eqb_compare. rewrite H; reflexivity. - rewrite N.eqb_compare. rewrite H; reflexivity. Qed. Lemma bv_and_nth_bitOf: forall a b n (i: nat), (size a) = n -> (size b) = n -> (i <= (nat_of_N (size a)))%nat -> nth i (bits (bv_and a b)) false = (nth i (bits a) false) && (nth i (bits b) false). Proof. intros a b n i H0 H1 H2. unfold bv_and. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. apply map2_and_nth_bitOf; unfold size in *; unfold bits. now rewrite <- Nat2N.inj_iff, H1. rewrite Nat2N.id in H2; exact H2. Qed. Lemma bv_and_empty_empty2: forall a, (bv_and bv_empty a) = bv_empty. Proof. intro a. unfold bv_and, bv_empty, size. case (length a); simpl; auto. Qed. Lemma bv_and_1_neutral: forall a, (bv_and a (mk_list_true (length (bits a)))) = a. Proof. intro a. unfold bv_and. rewrite N.eqb_compare. unfold size, bits. rewrite length_mk_list_true. rewrite N.compare_refl. rewrite map2_and_1_neutral. reflexivity. Qed. Lemma bv_and_0_absorb: forall a, (bv_and a (mk_list_false (length (bits a)))) = (mk_list_false (length (bits a))). Proof. intro a. unfold bv_and. rewrite N.eqb_compare. unfold size, bits. rewrite length_mk_list_false. rewrite N.compare_refl. rewrite map2_and_0_absorb. reflexivity. Qed. (* or *) (* Definitions *) (* or-defs *) Definition bv_or (a b : bitvector) : bitvector := match (@size a) =? (@size b) with | true => map2 orb (@bits a) (@bits b) | _ => nil end. (* Theorems *) (* or-thrms *) (* lists bitwise OR properties *) Lemma map2_or_comm: forall (a b: list bool), (map2 orb a b) = (map2 orb b a). Proof. intros a. induction a as [ | a' xs IHxs]. intros [ | b' ys]. - simpl. auto. - simpl. auto. - intros [ | b' ys]. + simpl. auto. + intros. simpl. cut (a' || b' = b' || a'). intro H. rewrite <- H. apply f_equal. apply IHxs. apply orb_comm. Qed. Lemma map2_or_assoc: forall (a b c: list bool), (map2 orb a (map2 orb b c)) = (map2 orb (map2 orb a b) c). Proof. intro a. induction a as [ | a' xs IHxs]. simpl. auto. intros [ | b' ys]. - simpl. auto. - intros [ | c' zs]. + simpl. auto. + simpl. cut (a' || (b' || c') = a' || b' || c'). intro H. rewrite <- H. apply f_equal. apply IHxs. apply orb_assoc. Qed. Lemma map2_or_length: forall (a b: list bool), length a = length b -> length a = length (map2 orb a b). Proof. induction a as [ | a' xs IHxs]. simpl. auto. intros [ | b ys]. - simpl. intros. exact H. - intros. simpl in *. apply f_equal. apply IHxs. inversion H; auto. Qed. Lemma map2_or_empty_empty1: forall (a: list bool), (map2 orb a []) = []. Proof. intros a. induction a as [ | a' xs IHxs]; simpl; auto. Qed. Lemma map2_or_empty_empty2: forall (a: list bool), (map2 orb [] a) = []. Proof. intros a. rewrite map2_or_comm. apply map2_or_empty_empty1. Qed. Lemma map2_or_nth_bitOf: forall (a b: list bool) (i: nat), (length a) = (length b) -> (i <= (length a))%nat -> nth i (map2 orb a b) false = (nth i a false) || (nth i b false). Proof. intro a. induction a as [ | a xs IHxs]. - intros [ | b ys]. + intros i H0 H1. do 2 rewrite map2_nth_empty_false. reflexivity. + intros i H0 H1. rewrite map2_or_empty_empty2. rewrite map2_nth_empty_false. contradict H1. simpl. unfold not. intros. easy. - intros [ | b ys]. + intros i H0 H1. rewrite map2_or_empty_empty1. rewrite map2_nth_empty_false. rewrite orb_false_r. rewrite H0 in H1. contradict H1. simpl. unfold not. intros. easy. + intros i H0 H1. simpl. revert i H1. induction i as [ | i IHi]. * simpl. auto. * intros. apply IHxs. inversion H0; reflexivity. inversion H1; lia. Qed. Lemma map2_or_0_neutral: forall (a: list bool), (map2 orb a (mk_list_false (length a))) = a. Proof. intro a. induction a as [ | a xs IHxs]. + auto. + simpl. rewrite IHxs. rewrite orb_false_r. reflexivity. Qed. Lemma map2_or_1_true: forall (a: list bool), (map2 orb a (mk_list_true (length a))) = (mk_list_true (length a)). Proof. intro a. induction a as [ | a' xs IHxs]. - simpl. reflexivity. - simpl. rewrite IHxs. rewrite orb_true_r; reflexivity. Qed. Lemma map2_or_idem1: forall (a b: list bool), (map2 orb (map2 orb a b) a) = (map2 orb a b). Proof. intros a. induction a as [ | a' xs IHxs]. intros [ | b' ys]. - simpl. auto. - simpl. auto. - intros [ | b' ys]. + simpl. auto. + intros. simpl. cut (a' || b' || a' = a' || b'). intro H. rewrite H. apply f_equal. apply IHxs. rewrite orb_comm, orb_assoc, orb_diag. reflexivity. Qed. Lemma map2_or_idem2: forall (a b: list bool), (map2 orb (map2 orb a b) b) = (map2 orb a b). Proof. intros a. induction a as [ | a' xs IHxs]. intros [ | b' ys]. - simpl. auto. - simpl. auto. - intros [ | b' ys]. + simpl. auto. + intros. simpl. cut (a' || b' || b' = a' || b'). intro H. rewrite H. apply f_equal. apply IHxs. rewrite <- orb_assoc. rewrite orb_diag. reflexivity. Qed. Lemma map2_or_neg_true: forall (a: list bool), (map2 orb (map negb a) a) = mk_list_true (length a). Proof. intro a. induction a; intros. - now cbn. - cbn. assert (negb a || a = true). case a; easy. now rewrite H, IHa. Qed. (* bitvector OR properties *) Lemma bv_or_size n a b : size a = n -> size b = n -> size (bv_or a b) = n. Proof. unfold bv_or. intros H1 H2. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size in *. rewrite <- map2_or_length. - exact H1. - unfold bits. now rewrite <- Nat2N.inj_iff, H1. Qed. Lemma bv_or_comm: forall n a b, (size a) = n -> (size b) = n -> bv_or a b = bv_or b a. Proof. intros a b n H0 H1. unfold bv_or. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite map2_or_comm. reflexivity. Qed. Lemma bv_or_assoc: forall n a b c, (size a) = n -> (size b) = n -> (size c) = n -> (bv_or a (bv_or b c)) = (bv_or (bv_or a b) c). Proof. intros n a b c H0 H1 H2. unfold bv_or. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size, bits in *. rewrite <- (@map2_or_length b c). rewrite H0, H1. rewrite N.compare_refl. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite <- (@map2_or_length a b). rewrite H0. rewrite N.compare_refl. rewrite map2_or_assoc; reflexivity. now rewrite <- Nat2N.inj_iff, H0. now rewrite <- Nat2N.inj_iff, H1. Qed. Lemma bv_or_empty_empty1: forall a, (bv_or a bv_empty) = bv_empty. Proof. intros a. unfold bv_empty. unfold bv_or, bits, size. simpl. case_eq (N.compare (N.of_nat (length a)) 0); intro H; simpl. - apply (N.compare_eq (N.of_nat (length a)) 0) in H. rewrite H. simpl. rewrite map2_or_empty_empty1; reflexivity. - rewrite N.eqb_compare. rewrite H; reflexivity. - rewrite N.eqb_compare. rewrite H; reflexivity. Qed. Lemma bv_or_nth_bitOf: forall a b n (i: nat), (size a) = n -> (size b) = n -> (i <= (nat_of_N (size a)))%nat -> nth i (bits (bv_or a b)) false = (nth i (bits a) false) || (nth i (bits b) false). Proof. intros a b n i H0 H1 H2. unfold bv_or. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. apply map2_or_nth_bitOf; unfold size in *; unfold bits. now rewrite <- Nat2N.inj_iff, H1. rewrite Nat2N.id in H2; exact H2. Qed. Lemma bv_or_0_neutral: forall a, (bv_or a (mk_list_false (length (bits a)))) = a. Proof. intro a. unfold bv_or. rewrite N.eqb_compare. unfold size, bits. rewrite length_mk_list_false. rewrite N.compare_refl. rewrite map2_or_0_neutral. reflexivity. Qed. Lemma bv_or_1_true: forall a, (bv_or a (mk_list_true (length (bits a)))) = (mk_list_true (length (bits a))). Proof. intro a. unfold bv_or. rewrite N.eqb_compare. unfold size, bits. rewrite length_mk_list_true. rewrite N.compare_refl. rewrite map2_or_1_true. reflexivity. Qed. Lemma bv_or_idem1: forall a b n, size a = n -> size b = n -> (bv_or (bv_or a b) a) = (bv_or a b). Proof. intros a b n H0 H1. unfold bv_or. rewrite H0. do 2 rewrite N.eqb_compare. unfold size in *. rewrite H1. rewrite N.compare_refl. rewrite <- H0. unfold bits. rewrite <- map2_or_length. rewrite N.compare_refl. rewrite map2_or_idem1; reflexivity. now rewrite <- Nat2N.inj_iff, H1. Qed. Lemma bv_or_idem2: forall a b n, size a = n -> size b = n -> (bv_or (bv_or a b) b) = (bv_or a b). Proof. intros a b n H0 H1. unfold bv_or. rewrite H0. do 2 rewrite N.eqb_compare. unfold size in *. rewrite H1. rewrite N.compare_refl. rewrite <- H0. unfold bits. rewrite <- map2_or_length. rewrite N.compare_refl. rewrite map2_or_idem2; reflexivity. now rewrite <- Nat2N.inj_iff, H1. Qed. (* xor *) (* Definitions *) (* xor-defs *) Definition bv_xor (a b : bitvector) : bitvector := match (@size a) =? (@size b) with | true => map2 xorb (@bits a) (@bits b) | _ => nil end. (* Theorems *) (* xor-thrms *) (* lists bitwise XOR properties *) Lemma map2_xor_comm: forall (a b: list bool), (map2 xorb a b) = (map2 xorb b a). Proof. intros a. induction a as [ | a' xs IHxs]. intros [ | b' ys]. - simpl. auto. - simpl. auto. - intros [ | b' ys]. + simpl. auto. + intros. simpl. cut (xorb a' b' = xorb b' a'). intro H. rewrite <- H. apply f_equal. apply IHxs. apply xorb_comm. Qed. Lemma map2_xor_assoc: forall (a b c: list bool), (map2 xorb a (map2 xorb b c)) = (map2 xorb (map2 xorb a b) c). Proof. intro a. induction a as [ | a' xs IHxs]. simpl. auto. intros [ | b' ys]. - simpl. auto. - intros [ | c' zs]. + simpl. auto. + simpl. cut (xorb a' (xorb b' c') = (xorb (xorb a' b') c')). intro H. rewrite <- H. apply f_equal. apply IHxs. rewrite xorb_assoc_reverse. reflexivity. Qed. Lemma map2_xor_length: forall (a b: list bool), length a = length b -> length a = length (map2 xorb a b). Proof. induction a as [ | a' xs IHxs]. simpl. auto. intros [ | b ys]. - simpl. intros. exact H. - intros. simpl in *. apply f_equal. apply IHxs. inversion H; auto. Qed. Lemma map2_xor_empty_empty1: forall (a: list bool), (map2 xorb a []) = []. Proof. intros a. induction a as [ | a' xs IHxs]; simpl; auto. Qed. Lemma map2_xor_empty_empty2: forall (a: list bool), (map2 xorb [] a) = []. Proof. intros a. rewrite map2_xor_comm. apply map2_xor_empty_empty1. Qed. Lemma map2_xor_nth_bitOf: forall (a b: list bool) (i: nat), (length a) = (length b) -> (i <= (length a))%nat -> nth i (map2 xorb a b) false = xorb (nth i a false) (nth i b false). Proof. intro a. induction a as [ | a xs IHxs]. - intros [ | b ys]. + intros i H0 H1. do 2 rewrite map2_nth_empty_false. reflexivity. + intros i H0 H1. rewrite map2_xor_empty_empty2. rewrite map2_nth_empty_false. contradict H1. simpl. unfold not. intros. easy. - intros [ | b ys]. + intros i H0 H1. rewrite map2_xor_empty_empty1. rewrite map2_nth_empty_false. rewrite xorb_false_r. rewrite H0 in H1. contradict H1. simpl. unfold not. intros. easy. + intros i H0 H1. simpl. revert i H1. induction i as [ | i IHi]. * simpl. auto. * intros. apply IHxs. inversion H0; reflexivity. inversion H1; lia. Qed. Lemma map2_xor_0_neutral: forall (a: list bool), (map2 xorb a (mk_list_false (length a))) = a. Proof. intro a. induction a as [ | a xs IHxs]. + auto. + simpl. rewrite IHxs. rewrite xorb_false_r. reflexivity. Qed. Lemma map2_xor_1_true: forall (a: list bool), (map2 xorb a (mk_list_true (length a))) = map negb a. Proof. intro a. induction a as [ | a' xs IHxs]. - simpl. reflexivity. - simpl. rewrite IHxs. rewrite <- IHxs. rewrite xorb_true_r; reflexivity. Qed. (* bitvector OR properties *) Lemma bv_xor_size n a b : size a = n -> size b = n -> size (bv_xor a b) = n. Proof. unfold bv_xor. intros H1 H2. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size in *. rewrite <- map2_xor_length. - exact H1. - unfold bits. now rewrite <- Nat2N.inj_iff, H1. Qed. Lemma bv_xor_comm: forall n a b, (size a) = n -> (size b) = n -> bv_xor a b = bv_xor b a. Proof. intros n a b H0 H1. unfold bv_xor. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite map2_xor_comm. reflexivity. Qed. Lemma bv_xor_assoc: forall n a b c, (size a) = n -> (size b) = n -> (size c) = n -> (bv_xor a (bv_xor b c)) = (bv_xor (bv_xor a b) c). Proof. intros n a b c H0 H1 H2. unfold bv_xor. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size, bits in *. rewrite <- (@map2_xor_length b c). rewrite H0, H1. rewrite N.compare_refl. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite <- (@map2_xor_length a b). rewrite H0. rewrite N.compare_refl. rewrite map2_xor_assoc; reflexivity. now rewrite <- Nat2N.inj_iff, H0. now rewrite <- Nat2N.inj_iff, H1. Qed. Lemma bv_xor_empty_empty1: forall a, (bv_xor a bv_empty) = bv_empty. Proof. intros a. unfold bv_empty. unfold bv_xor, bits, size. simpl. case_eq (N.compare (N.of_nat (length a)) 0); intro H; simpl. - apply (N.compare_eq (N.of_nat (length a)) 0) in H. rewrite H. simpl. rewrite map2_xor_empty_empty1; reflexivity. - rewrite N.eqb_compare. rewrite H; reflexivity. - rewrite N.eqb_compare. rewrite H; reflexivity. Qed. Lemma bv_xor_nth_bitOf: forall a b n (i: nat), (size a) = n -> (size b) = n -> (i <= (nat_of_N (size a)))%nat -> nth i (bits (bv_xor a b)) false = xorb (nth i (bits a) false) (nth i (bits b) false). Proof. intros a b n i H0 H1 H2. unfold bv_xor. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. apply map2_xor_nth_bitOf; unfold size in *; unfold bits. now rewrite <- Nat2N.inj_iff, H1. rewrite Nat2N.id in H2; exact H2. Qed. Lemma bv_xor_0_neutral: forall a, (bv_xor a (mk_list_false (length (bits a)))) = a. Proof. intro a. unfold bv_xor. rewrite N.eqb_compare. unfold size, bits. rewrite length_mk_list_false. rewrite N.compare_refl. rewrite map2_xor_0_neutral. reflexivity. Qed. Lemma bv_xor_1_true: forall a, (bv_xor a (mk_list_true (length (bits a)))) = map negb a. Proof. intro a. unfold bv_xor. rewrite N.eqb_compare. unfold size, bits. rewrite length_mk_list_true. rewrite N.compare_refl. rewrite map2_xor_1_true. reflexivity. Qed. (* not *) (* Definitions *) (* not-defs *) Definition bv_not (a: bitvector) : bitvector := map negb (@bits a). (* Theorems *) (* not-thrms *) (* bitwise NOT properties *) Lemma not_list_length: forall a, length a = length (map negb a). Proof. intro a. induction a as [ | a xs IHxs]. - auto. - simpl. apply f_equal. exact IHxs. Qed. Lemma not_list_involutative: forall a, map negb (map negb a) = a. Proof. intro a. induction a as [ | a xs IHxs]; auto. simpl. rewrite negb_involutive. apply f_equal. exact IHxs. Qed. Lemma not_list_false_true: forall n, map negb (mk_list_false n) = mk_list_true n. Proof. intro n. induction n as [ | n IHn]. - auto. - simpl. apply f_equal. exact IHn. Qed. Lemma not_list_true_false: forall n, map negb (mk_list_true n) = mk_list_false n. Proof. intro n. induction n as [ | n IHn]. - auto. - simpl. apply f_equal. exact IHn. Qed. Lemma not_list_and_or: forall a b, map negb (map2 andb a b) = map2 orb (map negb a) (map negb b). Proof. intro a. induction a as [ | a xs IHxs]. - auto. - intros [ | b ys]. + auto. + simpl. rewrite negb_andb. apply f_equal. apply IHxs. Qed. Lemma not_list_or_and: forall a b, map negb (map2 orb a b) = map2 andb (map negb a) (map negb b). Proof. intro a. induction a as [ | a xs IHxs]. - auto. - intros [ | b ys]. + auto. + simpl. rewrite negb_orb. apply f_equal. apply IHxs. Qed. (* bitvector NOT properties *) Lemma bv_not_size: forall n a, (size a) = n -> size (bv_not a) = n. Proof. intros n a H. unfold bv_not. unfold size, bits in *. rewrite <- not_list_length. exact H. Qed. Lemma bv_not_involutive: forall a, bv_not (bv_not a) = a. Proof. intro a. unfold bv_not. unfold size, bits. rewrite not_list_involutative. reflexivity. Qed. Lemma bv_not_false_true: forall n, bv_not (mk_list_false n) = (mk_list_true n). Proof. intros n. unfold bv_not. unfold size, bits. rewrite not_list_false_true. reflexivity. Qed. Lemma bv_not_true_false: forall n, bv_not (mk_list_true n) = (mk_list_false n). Proof. intros n. unfold bv_not. unfold size, bits. rewrite not_list_true_false. reflexivity. Qed. Lemma bv_not_and_or: forall n a b, (size a) = n -> (size b) = n -> bv_not (bv_and a b) = bv_or (bv_not a) (bv_not b). Proof. intros n a b H0 H1. unfold bv_and in *. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. unfold bv_or, size, bits in *. do 2 rewrite <- not_list_length. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. unfold bv_not, size, bits in *. rewrite not_list_and_or. reflexivity. Qed. Lemma bv_not_or_and: forall n a b, (size a) = n -> (size b) = n -> bv_not (bv_or a b) = bv_and (bv_not a) (bv_not b). Proof. intros n a b H0 H1. unfold bv_and, size, bits in *. do 2 rewrite <- not_list_length. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. unfold bv_or, size, bits in *. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. unfold bv_not, size, bits in *. rewrite not_list_or_and. reflexivity. Qed. Lemma bvdm: forall a b: bitvector, size a = size b -> (bv_not (bv_and a b)) = (bv_or (bv_not a) (bv_not b)). Proof. intros. unfold bv_and, bv_or, bv_not. rewrite H, N.eqb_refl. unfold bits, size in *. rewrite !map_length, H, N.eqb_refl. now rewrite not_list_and_or. Qed. Lemma bv_not_not_eq : forall (h : bool) (t : bitvector), bv_not (h :: t) <> (h :: t). Proof. intros. unfold not. induction h; easy. Qed. (*-------------------- Arithmetic Operations --------------------*) (*-------------------- addition --------------------*) (* Definitions *) (* add-defs *) Definition add_carry b1 b2 c := match b1, b2, c with | true, true, true => (true, true) | true, true, false | true, false, true | false, true, true => (false, true) | false, false, true | false, true, false | true, false, false => (true, false) | false, false, false => (false, false) end. (* Truncating addition in little-endian, direct style *) Fixpoint add_list_ingr bs1 bs2 c {struct bs1} := match bs1, bs2 with | nil, _ => nil | _ , nil => nil | b1 :: bs1, b2 :: bs2 => let (r, c) := add_carry b1 b2 c in r :: (add_list_ingr bs1 bs2 c) end. Definition add_list (a b: list bool) := add_list_ingr a b false. Definition bv_add (a b : bitvector) : bitvector := match (@size a) =? (@size b) with | true => add_list a b | _ => nil end. (* Theorems *) (* add-thrms *) (* list bitwise ADD properties *) Lemma add_carry_ff: forall a, add_carry a false false = (a, false). Proof. intros a. case a; simpl; auto. Qed. Lemma add_carry_neg_f: forall a, add_carry a (negb a) false = (true, false). Proof. intros a. case a; simpl; auto. Qed. Lemma add_carry_neg_f_r: forall a, add_carry (negb a) a false = (true, false). Proof. intros a. case a; simpl; auto. Qed. Lemma add_carry_neg_t: forall a, add_carry a (negb a) true = (false, true). Proof. intros a. case a; simpl; auto. Qed. Lemma add_carry_tt: forall a, add_carry a true true = (a, true). Proof. intro a. case a; auto. Qed. Lemma add_list_empty_l: forall (a: list bool), (add_list [] a) = []. Proof. intro a. induction a as [ | a xs IHxs]. - unfold add_list. simpl. reflexivity. - apply IHxs. Qed. Lemma add_list_empty_r: forall (a: list bool), (add_list a []) = []. Proof. intro a. induction a as [ | a xs IHxs]; unfold add_list; simpl; reflexivity. Qed. Lemma add_list_ingr_l: forall (a: list bool) (c: bool), (add_list_ingr [] a c) = []. Proof. intro a. induction a as [ | a xs IHxs]; unfold add_list; simpl; reflexivity. Qed. Lemma add_list_ingr_r: forall (a: list bool) (c: bool), (add_list_ingr a [] c) = []. Proof. intro a. induction a as [ | a xs IHxs]; unfold add_list; simpl; reflexivity. Qed. Lemma add_list_carry_comm: forall (a b: list bool) (c: bool), add_list_ingr a b c = add_list_ingr b a c. Proof. intros a. induction a as [ | a' xs IHxs]; intros b c. - simpl. rewrite add_list_ingr_r. reflexivity. - case b as [ | b' ys]. + simpl. auto. + simpl in *. cut (add_carry a' b' c = add_carry b' a' c). * intro H. rewrite H. destruct (add_carry b' a' c) as (r, c0). rewrite IHxs. reflexivity. * case a', b', c; auto. Qed. Lemma add_list_comm: forall (a b: list bool), (add_list a b) = (add_list b a). Proof. intros a b. unfold add_list. apply (add_list_carry_comm a b false). Qed. Lemma add_list_carry_assoc: forall (a b c: list bool) (d1 d2 d3 d4: bool), add_carry d1 d2 false = add_carry d3 d4 false -> (add_list_ingr (add_list_ingr a b d1) c d2) = (add_list_ingr a (add_list_ingr b c d3) d4). Proof. intros a. induction a as [ | a' xs IHxs]; intros b c d1 d2 d3 d4. - simpl. reflexivity. - case b as [ | b' ys]. + simpl. auto. + case c as [ | c' zs]. * simpl. rewrite add_list_ingr_r. auto. * simpl. case_eq (add_carry a' b' d1); intros r0 c0 Heq0. simpl. case_eq (add_carry r0 c' d2); intros r1 c1 Heq1. case_eq (add_carry b' c' d3); intros r3 c3 Heq3. case_eq (add_carry a' r3 d4); intros r2 c2 Heq2. intro H. rewrite (IHxs _ _ c0 c1 c3 c2); revert Heq0 Heq1 Heq3 Heq2; case a', b', c', d1, d2, d3, d4; simpl; do 4 (intros H'; inversion_clear H'); try reflexivity; simpl in H; discriminate. Qed. Lemma add_list_carry_length_eq: forall (a b: list bool) c, length a = length b -> length a = length (add_list_ingr a b c). Proof. induction a as [ | a' xs IHxs]. simpl. auto. intros [ | b ys]. - simpl. intros. exact H. - intros. simpl in *. case_eq (add_carry a' b c); intros r c0 Heq. simpl. apply f_equal. specialize (@IHxs ys). apply IHxs. inversion H; reflexivity. Qed. Lemma add_list_carry_length_ge: forall (a b: list bool) c, (length a >= length b)%nat -> length b = length (add_list_ingr a b c). Proof. induction a as [ | a' xs IHxs]. simpl. intros b H0 H1. lia. intros [ | b ys]. - simpl. intros. auto. - intros. simpl in *. case_eq (add_carry a' b c); intros r c0 Heq. simpl. apply f_equal. specialize (@IHxs ys). apply IHxs. lia. Qed. Lemma add_list_carry_length_le: forall (a b: list bool) c, (length b >= length a)%nat -> length a = length (add_list_ingr a b c). Proof. induction a as [ | a' xs IHxs]. simpl. intros b H0 H1. reflexivity. intros [ | b ys]. - simpl. intros. contradict H. lia. - intros. simpl in *. case_eq (add_carry a' b c); intros r c0 Heq. simpl. apply f_equal. specialize (@IHxs ys). apply IHxs. lia. Qed. Lemma length_add_list_eq: forall (a b: list bool), length a = length b -> length a = length (add_list a b). Proof. intros a b H. unfold add_list. apply (@add_list_carry_length_eq a b false). exact H. Qed. Lemma length_add_list_ge: forall (a b: list bool), (length a >= length b)%nat -> length b = length (add_list a b). Proof. intros a b H. unfold add_list. apply (@add_list_carry_length_ge a b false). exact H. Qed. Lemma length_add_list_le: forall (a b: list bool), (length b >= length a)%nat -> length a = length (add_list a b). Proof. intros a b H. unfold add_list. apply (@add_list_carry_length_le a b false). exact H. Qed. Lemma add_list_assoc: forall (a b c: list bool), (add_list (add_list a b) c) = (add_list a (add_list b c)). Proof. intros a b c. unfold add_list. apply (@add_list_carry_assoc a b c false false false false). simpl; reflexivity. Qed. Lemma add_list_carry_empty_neutral_n_l: forall (a: list bool) n, (n >= (length a))%nat -> (add_list_ingr (mk_list_false n) a false) = a. Proof. intro a. induction a as [ | a' xs IHxs]. - intro n. rewrite add_list_ingr_r. reflexivity. - intros [ | n]. + simpl. intro H. contradict H. easy. + simpl. intro H. case a'; apply f_equal; apply IHxs; lia. Qed. Lemma add_list_carry_empty_neutral_n_r: forall (a: list bool) n, (n >= (length a))%nat -> (add_list_ingr a (mk_list_false n) false) = a. Proof. intro a. induction a as [ | a' xs IHxs]. - intro n. rewrite add_list_ingr_l. reflexivity. - intros [ | n]. + simpl. intro H. contradict H. easy. + simpl. intro H. case a'; apply f_equal; apply IHxs; lia. Qed. Lemma add_list_carry_empty_neutral_l: forall (a: list bool), (add_list_ingr (mk_list_false (length a)) a false) = a. Proof. intro a. rewrite add_list_carry_empty_neutral_n_l; auto. Qed. Lemma add_list_carry_empty_neutral_r: forall (a: list bool), (add_list_ingr a (mk_list_false (length a)) false) = a. Proof. intro a. rewrite add_list_carry_empty_neutral_n_r; auto. Qed. Lemma add_list_empty_neutral_n_l: forall (a: list bool) n, (n >= (length a))%nat -> (add_list (mk_list_false n) a) = a. Proof. intros a. unfold add_list. apply (@add_list_carry_empty_neutral_n_l a). Qed. Lemma add_list_empty_neutral_n_r: forall (a: list bool) n, (n >= (length a))%nat -> (add_list a (mk_list_false n)) = a. Proof. intros a. unfold add_list. apply (@add_list_carry_empty_neutral_n_r a). Qed. Lemma add_list_empty_neutral_r: forall (a: list bool), (add_list a (mk_list_false (length a))) = a. Proof. intros a. unfold add_list. apply (@add_list_carry_empty_neutral_r a). Qed. Lemma add_list_empty_neutral_l: forall (a: list bool), (add_list (mk_list_false (length a)) a) = a. Proof. intros a. unfold add_list. apply (@add_list_carry_empty_neutral_l a). Qed. Lemma add_list_carry_unit_t : forall a, add_list_ingr a (mk_list_true (length a)) true = a. Proof. intro a. induction a as [ | a xs IHxs]. - simpl. reflexivity. - simpl. case_eq (add_carry a true true). intros r0 c0 Heq0. rewrite add_carry_tt in Heq0. inversion Heq0. apply f_equal. exact IHxs. Qed. Lemma add_list_carry_twice: forall a c, add_list_ingr a a c = removelast (c :: a). Proof. intro a. induction a as [ | a xs IHxs]. - intros c. simpl. reflexivity. - intros [ | ]. + simpl. case a. * simpl. rewrite IHxs. case_eq xs. intro Heq0. simpl. reflexivity. reflexivity. * simpl. rewrite IHxs. case_eq xs. intro Heq0. simpl. reflexivity. reflexivity. + simpl. case a. * simpl. rewrite IHxs. case_eq xs. intro Heq0. simpl. reflexivity. reflexivity. * simpl. rewrite IHxs. case_eq xs. intro Heq0. simpl. reflexivity. reflexivity. Qed. Lemma add_list_twice: forall a, add_list a a = removelast (false :: a). Proof. intro a. unfold add_list. rewrite add_list_carry_twice. reflexivity. Qed. (* bitvector ADD properties *) Lemma bv_add_size: forall n a b, (size a) = n -> (@size b) = n -> size (bv_add a b) = n. Proof. intros n a b H0 H1. unfold bv_add. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size, bits in *. rewrite <- (@length_add_list_eq a b). auto. now rewrite <- Nat2N.inj_iff, H0. Qed. Lemma bv_add_comm: forall n a b, (size a) = n -> (size b) = n -> bv_add a b = bv_add b a. Proof. intros n a b H0 H1. unfold bv_add, size, bits in *. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite add_list_comm. reflexivity. Qed. Lemma bv_add_assoc: forall n a b c, (size a) = n -> (size b) = n -> (size c) = n -> (bv_add a (bv_add b c)) = (bv_add (bv_add a b) c). Proof. intros n a b c H0 H1 H2. unfold bv_add, size, bits in *. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite <- (@length_add_list_eq b c). rewrite H0, H1. rewrite N.compare_refl. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite <- (@length_add_list_eq a b). rewrite H0. rewrite N.compare_refl. rewrite add_list_assoc. reflexivity. now rewrite <- Nat2N.inj_iff, H0. now rewrite <- Nat2N.inj_iff, H1. Qed. Lemma bv_add_empty_neutral_l: forall a, (bv_add (mk_list_false (length (bits a))) a) = a. Proof. intro a. unfold bv_add, size, bits. rewrite N.eqb_compare. rewrite length_mk_list_false. rewrite N.compare_refl. rewrite add_list_empty_neutral_l. reflexivity. Qed. Lemma bv_add_empty_neutral_r: forall a, (bv_add a (mk_list_false (length (bits a)))) = a. Proof. intro a. unfold bv_add, size, bits. rewrite N.eqb_compare. rewrite length_mk_list_false. rewrite N.compare_refl. rewrite add_list_empty_neutral_r. reflexivity. Qed. Lemma bv_add_twice: forall a, bv_add a a = removelast (false :: a). Proof. intro a. unfold bv_add, size, bits. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite add_list_twice. reflexivity. Qed. (* subtraction *) (* Definitions *) (* sub-defs *) (* Using 2's Complement *) Definition twos_complement b := add_list_ingr (map negb b) (mk_list_false (length b)) true. Definition bv_neg (a: bitvector) : bitvector := (twos_complement a). Definition subst_list' a b := add_list a (twos_complement b). Definition bv_subt' (a b : bitvector) : bitvector := match (@size a) =? (@size b) with | true => (subst_list' (@bits a) (@bits b)) | _ => nil end. (* Using Borrow *) Definition subst_borrow b1 b2 b := match b1, b2, b with | true, true, true => (true, true) | true, true, false => (false, false) | true, false, true => (false, false) | false, true, true => (false, true) | false, false, true => (true, true) | false, true, false => (true, true) | true, false, false => (true, false) | false, false, false => (false, false) end. Fixpoint subst_list_borrow bs1 bs2 b {struct bs1} := match bs1, bs2 with | nil, _ => nil | _ , nil => nil | b1 :: bs1, b2 :: bs2 => let (r, b) := subst_borrow b1 b2 b in r :: (subst_list_borrow bs1 bs2 b) end. Definition subst_list (a b: list bool) := subst_list_borrow a b false. Definition bv_subt (a b : bitvector) : bitvector := match (@size a) =? (@size b) with | true => subst_list (@bits a) (@bits b) | _ => nil end. (* Theorems *) (* sub-thrms *) (* bitwise SUBST properties *) Lemma subst_list_empty_empty_l: forall a, (subst_list [] a) = []. Proof. intro a. unfold subst_list; auto. Qed. Lemma subst_list_empty_empty_r: forall a, (subst_list a []) = []. Proof. intro a. induction a as [ | a xs IHxs]. - auto. - unfold subst_list; auto. Qed. Lemma subst_list'_empty_empty_r: forall a, (subst_list' a []) = []. Proof. intro a. induction a as [ | a xs IHxs]. - auto. - unfold subst_list' in *. unfold twos_complement. simpl. reflexivity. Qed. Lemma subst_list_borrow_length: forall (a b: list bool) c, length a = length b -> length a = length (subst_list_borrow a b c). Proof. induction a as [ | a' xs IHxs]. simpl. auto. intros [ | b ys]. - simpl. intros. exact H. - intros. simpl in *. case_eq (subst_borrow a' b c); intros r c0 Heq. simpl. apply f_equal. specialize (@IHxs ys). apply IHxs. inversion H; reflexivity. Qed. Lemma length_twos_complement: forall (a: list bool), length a = length (twos_complement a). Proof. intro a. induction a as [ | a' xs IHxs]. - auto. - unfold twos_complement. specialize (@add_list_carry_length_eq (map negb (a' :: xs)) (mk_list_false (length (a' :: xs))) true). intro H. rewrite <- H. simpl. apply f_equal. rewrite <- not_list_length. reflexivity. rewrite length_mk_list_false. rewrite <- not_list_length. reflexivity. Qed. Lemma subst_list_length: forall (a b: list bool), length a = length b -> length a = length (subst_list a b). Proof. intros a b H. unfold subst_list. apply (@subst_list_borrow_length a b false). exact H. Qed. Lemma subst_list'_length: forall (a b: list bool), length a = length b -> length a = length (subst_list' a b). Proof. intros a b H. unfold subst_list'. rewrite <- (@length_add_list_eq a (twos_complement b)). - reflexivity. - rewrite <- (@length_twos_complement b). exact H. Qed. Lemma subst_list_borrow_empty_neutral: forall (a: list bool), (subst_list_borrow a (mk_list_false (length a)) false) = a. Proof. intro a. induction a as [ | a' xs IHxs]. - simpl. reflexivity. - simpl. cut(subst_borrow a' false false = (a', false)). + intro H. rewrite H. rewrite IHxs. reflexivity. + unfold subst_borrow. case a'; auto. Qed. Lemma subst_list_empty_neutral: forall (a: list bool), (subst_list a (mk_list_false (length a))) = a. Proof. intros a. unfold subst_list. apply (@subst_list_borrow_empty_neutral a). Qed. Lemma twos_complement_cons_false: forall a, false :: twos_complement a = twos_complement (false :: a). Proof. intro a. induction a as [ | a xs IHxs]; unfold twos_complement; simpl; reflexivity. Qed. Lemma twos_complement_false_false: forall n, twos_complement (mk_list_false n) = mk_list_false n. Proof. intro n. induction n as [ | n IHn]. - auto. - simpl. rewrite <- twos_complement_cons_false. apply f_equal. exact IHn. Qed. Lemma subst_list'_empty_neutral: forall (a: list bool), (subst_list' a (mk_list_false (length a))) = a. Proof. intros a. unfold subst_list'. rewrite (@twos_complement_false_false (length a)). rewrite add_list_empty_neutral_r. reflexivity. Qed. (* bitvector SUBT properties *) Lemma bv_subt_size: forall n a b, size a = n -> size b = n -> size (bv_subt a b) = n. Proof. intros n a b H0 H1. unfold bv_subt, size, bits in *. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite <- subst_list_length. exact H0. now rewrite <- Nat2N.inj_iff, H0. Qed. Lemma bv_subt_empty_neutral_r: forall a, (bv_subt a (mk_list_false (length (bits a)))) = a. Proof. intro a. unfold bv_subt, size, bits. rewrite N.eqb_compare. rewrite length_mk_list_false. rewrite N.compare_refl. rewrite subst_list_empty_neutral. reflexivity. Qed. Lemma bv_subt'_size: forall n a b, (size a) = n -> (size b) = n -> size (bv_subt' a b) = n. Proof. intros n a b H0 H1. unfold bv_subt', size, bits in *. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite <- subst_list'_length. exact H0. now rewrite <- Nat2N.inj_iff, H0. Qed. Lemma bv_subt'_empty_neutral_r: forall a, (bv_subt' a (mk_list_false (length (bits a)))) = a. Proof. intro a. unfold bv_subt', size, bits. rewrite N.eqb_compare. rewrite length_mk_list_false. rewrite N.compare_refl. rewrite subst_list'_empty_neutral. reflexivity. Qed. (* bitvector NEG properties *) Lemma bv_neg_size: forall n a, (size a) = n -> size (bv_neg a) = n. Proof. intros n a H. unfold bv_neg. unfold size, bits in *. unfold twos_complement. specialize (@add_list_carry_length_eq (map negb a) (mk_list_false (length a)) true). intros. rewrite <- H0. now rewrite map_length. rewrite map_length. now rewrite length_mk_list_false. Qed. (* addition-subtraction *) (* Theorems *) (* addsub-thrms *) (* bitwise ADD-NEG properties *) Lemma add_neg_list_carry_false: forall a b c, add_list_ingr a (add_list_ingr b c true) false = add_list_ingr a (add_list_ingr b c false) true. Proof. intro a. induction a as [ | a xs IHxs]. - simpl. auto. - case b as [ | b ys]. + simpl. auto. + case c as [ | c zs]. * simpl. auto. * simpl. case_eq (add_carry b c false); intros r0 c0 Heq0. case_eq (add_carry b c true); intros r1 c1 Heq1. case_eq (add_carry a r1 false); intros r2 c2 Heq2. case_eq (add_carry a r0 true); intros r3 c3 Heq3. case a, b, c; inversion Heq0; inversion Heq1; inversion Heq2; inversion Heq3; rewrite <- H2 in H4; rewrite <- H0 in H5; inversion H4; inversion H5; apply f_equal; try reflexivity; rewrite IHxs; reflexivity. Qed. Lemma add_neg_list_carry_neg_f: forall a, (add_list_ingr a (map negb a) false) = mk_list_true (length a). Proof. intro a. induction a as [ | a xs IHxs]. - simpl. reflexivity. - simpl. case_eq (add_carry a (negb a) false); intros r0 c0 Heq0. rewrite add_carry_neg_f in Heq0. inversion Heq0. rewrite IHxs. reflexivity. Qed. Lemma add_neg_list_carry_neg_f_r: forall a, (add_list_ingr (map negb a) a false) = mk_list_true (length a). Proof. intro a. induction a as [ | a xs IHxs]. - simpl. reflexivity. - simpl. case_eq (add_carry (negb a) a false); intros r0 c0 Heq0. rewrite add_carry_neg_f_r in Heq0. inversion Heq0. rewrite IHxs. reflexivity. Qed. Lemma add_neg_list_carry_neg_t: forall a, (add_list_ingr a (map negb a) true) = mk_list_false (length a). Proof. intro a. induction a as [ | a xs IHxs]. - simpl. reflexivity. - simpl. case_eq (add_carry a (negb a) true); intros r0 c0 Heq0. rewrite add_carry_neg_t in Heq0. inversion Heq0. rewrite IHxs. reflexivity. Qed. Lemma add_neg_list_carry: forall a, add_list_ingr a (twos_complement a) false = mk_list_false (length a). Proof. intro a. induction a as [ | a xs IHxs]. - simpl. reflexivity. - unfold twos_complement. rewrite add_neg_list_carry_false. rewrite not_list_length at 1. rewrite add_list_carry_empty_neutral_r. rewrite add_neg_list_carry_neg_t. reflexivity. Qed. Lemma add_neg_list_absorb: forall a, add_list a (twos_complement a) = mk_list_false (length a). Proof. intro a. unfold add_list. rewrite add_neg_list_carry. reflexivity. Qed. Lemma subt'_add_list: forall (a b : bitvector) (n : N), N.of_nat (length a) = n -> N.of_nat (length b) = n -> subst_list' (add_list_ingr a b false) b = a. Proof. intros. unfold subst_list', twos_complement, add_list. rewrite add_neg_list_carry_false. rewrite not_list_length at 1. rewrite add_list_carry_empty_neutral_r. specialize (@add_list_carry_assoc a b (map negb b) false true false true). intro H2. rewrite H2; try auto. rewrite add_neg_list_carry_neg_f. assert (length b = length a). { rewrite <- H in H0. now apply Nat2N.inj in H0. } rewrite H1. now rewrite add_list_carry_unit_t. Qed. (* bitvector ADD-NEG properties *) Lemma bv_add_neg_unit: forall a, bv_add a (bv_not a) = mk_list_true (nat_of_N (size a)). Proof. intro a. unfold bv_add, bv_not, size, bits. rewrite not_list_length. rewrite N.eqb_compare. rewrite N.compare_refl. unfold add_list. rewrite add_neg_list_carry_neg_f. rewrite Nat2N.id, not_list_length. reflexivity. Qed. (* bitwise ADD-SUBST properties *) Lemma add_subst_list_carry_opp: forall n a b, (length a) = n -> (length b) = n -> (add_list_ingr (subst_list' a b) b false) = a. Proof. intros n a b H0 H1. unfold subst_list', twos_complement, add_list. rewrite add_neg_list_carry_false. rewrite not_list_length at 1. rewrite add_list_carry_empty_neutral_r. specialize (@add_list_carry_assoc a (map negb b) b true false false true). intro H2. rewrite H2; try auto. rewrite add_neg_list_carry_neg_f_r. rewrite H1. rewrite <- H0. rewrite add_list_carry_unit_t; reflexivity. Qed. Lemma add_subst_opp: forall n a b, (length a) = n -> (length b) = n -> (add_list (subst_list' a b) b) = a. Proof. intros n a b H0 H1. unfold add_list, size, bits. apply (@add_subst_list_carry_opp n a b); easy. Qed. (* bitvector ADD-SUBT properties *) Lemma bv_add_subst_opp: forall n a b, (size a) = n -> (size b) = n -> (bv_add (bv_subt' a b) b) = a. Proof. intros n a b H0 H1. unfold bv_add, bv_subt', add_list, size, bits in *. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite <- (@subst_list'_length a b). rewrite H0. rewrite N.compare_refl. rewrite (@add_subst_list_carry_opp (nat_of_N n) a b); auto; inversion H0; rewrite Nat2N.id; auto. symmetry. now rewrite <- Nat2N.inj_iff, H0. now rewrite <- Nat2N.inj_iff, H0. Qed. (* a + b - b = a *) Lemma bv_subt'_add: forall n a b, (size a) = n -> (size b) = n -> (bv_subt' (bv_add a b) b) = a. Proof. intros n a b H0 H1. unfold bv_add, bv_subt', add_list, size, bits in *. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.eqb_compare. rewrite N.compare_refl. rewrite <- add_list_carry_length_eq, H0. rewrite N.compare_refl. apply (@subt'_add_list a b n); easy. rewrite <- H0 in H1. now apply Nat2N.inj in H1. Qed. Theorem bvadd_U: forall (n : N), forall (s t x: bitvector), (size s) = n /\ (size t) = n /\ (size x) = n -> (bv_add x s) = t <-> (x = (bv_subt' t s)). Proof. intros n s t x (Hs, (Ht, Hx)). split; intro A. - rewrite <- A. symmetry. exact (@bv_subt'_add n x s Hx Hs). - rewrite A. exact (bv_add_subst_opp Ht Hs). Qed. (* multiplication *) (* Definitions *) (* mul-defs *) Fixpoint mult_list_carry (a b :list bool) n {struct a}: list bool := match a with | nil => mk_list_false n | a' :: xs => if a' then add_list b (mult_list_carry xs (false :: b) n) else mult_list_carry xs (false :: b) n end. Fixpoint mult_list_carry2 (a b :list bool) n {struct a}: list bool := match a with | nil => mk_list_false n | a' :: xs => if a' then add_list b (mult_list_carry2 xs (false :: (removelast b)) n) else mult_list_carry2 xs (false :: (removelast b)) n end. Fixpoint and_with_bool (a: list bool) (bt: bool) : list bool := match a with | nil => nil | ai :: a' => (bt && ai) :: and_with_bool a' bt end. Fixpoint mult_bool_step_k_h (a b: list bool) (c: bool) (k: Z) : list bool := match a, b with | nil , _ => nil | ai :: a', bi :: b' => if ((k - 1)%Z <? 0)%Z then let carry_out := (ai && bi) || ((xorb ai bi) && c) in let curr := xorb (xorb ai bi) c in curr :: mult_bool_step_k_h a' b' carry_out (k - 1) else ai :: mult_bool_step_k_h a' b c (k - 1) | ai :: a' , nil => ai :: mult_bool_step_k_h a' b c k end. Fixpoint top_k_bools (a: list bool) (k: Z) : list bool := if (Z.eqb k 0) then nil else match a with | nil => nil | ai :: a' => ai :: top_k_bools a' (k - 1) end. Fixpoint mult_bool_step (a b: list bool) (res: list bool) (k k': nat) : list bool := let ak := List.firstn (S k') a in let b' := and_with_bool ak (nth k b false) in let res' := mult_bool_step_k_h res b' false (Z.of_nat k) in match k' with | O => res' (* | S O => res' *) | S pk' => mult_bool_step a b res' (S k) pk' end. Definition bvmult_bool (a b: list bool) (n: nat) : list bool := let res := and_with_bool a (nth 0 b false) in match n with | O => res | S O => res | S (S k) => mult_bool_step a b res 1 k end. Definition mult_list a b := bvmult_bool a b (length a). Definition bv_mult (a b : bitvector) : bitvector := if ((@size a) =? (@size b)) then mult_list a b else nil. (* Theorems *) (* mul-thrms *) (* bitvector MULT properties *) Lemma prop_mult_bool_step_k_h_len: forall a b c k, length (mult_bool_step_k_h a b c k) = length a. Proof. intro a. induction a as [ | xa xsa IHa ]. - intros. simpl. easy. - intros. case b in *. simpl. rewrite IHa. simpl. omega. simpl. case (k - 1 <? 0)%Z; simpl; now rewrite IHa. Qed. Lemma empty_list_length: forall {A: Type} (a: list A), (length a = 0)%nat <-> a = []. Proof. intros A a. induction a; split; intros; auto; contradict H; easy. Qed. Lemma prop_mult_bool_step: forall k' a b res k, length (mult_bool_step a b res k k') = (length res)%nat. Proof. intro k'. induction k'. - intros. simpl. rewrite prop_mult_bool_step_k_h_len. simpl. omega. - intros. simpl. rewrite IHk'. rewrite prop_mult_bool_step_k_h_len. simpl; omega. Qed. Lemma and_with_bool_len: forall a b, length (and_with_bool a (nth 0 b false)) = length a. Proof. intro a. - induction a. intros. now simpl. intros. simpl. now rewrite IHa. Qed. Lemma bv_mult_size: forall n a b, (size a) = n -> (@size b) = n -> size (bv_mult a b) = n. Proof. intros n a b H0 H1. unfold bv_mult, size, bits in *. rewrite H0, H1. rewrite N.eqb_compare. rewrite N.compare_refl. unfold mult_list, bvmult_bool. case_eq (length a). intros. + rewrite empty_list_length in H. rewrite H in *. now simpl in *. + intros. case n0 in *. now rewrite and_with_bool_len. rewrite prop_mult_bool_step. now rewrite and_with_bool_len. Qed. (*-------------------- Conversion Operations --------------------*) (* nat *) (* Theorems *) (* nat-thrms *) Lemma PosSizeNPos: forall p, (N.pos (Pos.size p) <=? N.pos p) = true. Proof. intro p. induction p; intros. - cbn. rewrite N.leb_le in *. lia. - cbn. rewrite N.leb_le in *. lia. - now cbn. Qed. Lemma Nsize_lt: forall a, ((N.size a) <=? a) = true. Proof. intro a. induction a; intros. - now cbn. - cbn in *. now rewrite PosSizeNPos. Qed. Lemma PosSuc: forall a, (Pos.of_succ_nat a) = Pos.of_nat (S a). Proof. intro a. Reconstr.reasy (@Coq.PArith.BinPos.Pos.of_nat_succ) Reconstr.Empty. Qed. Lemma NPos_size: forall a, a <> O -> (Pos.to_nat (Pos.size (Pos.of_nat a))) = (N.to_nat (N.size (N.of_nat a))) . Proof. intro a. induction a; intros. - cbn. easy. - cbn. case_eq a; intros. + now cbn. + rewrite Coq.PArith.BinPos.Pos.succ_of_nat. easy. lia. Qed. Lemma size_gt: forall a, ((N.to_nat (N.size (N.of_nat a)))%nat <=? a)%nat = true. Proof. intro a. induction a; intros. - now cbn. - cbn in *. rewrite PosSuc. rewrite NPos_size. specialize (Nsize_lt (N.of_nat (S a))); intro HH. assert ( (N.to_nat (N.size (N.of_nat (S a))) <=? N.to_nat (N.of_nat (S a)))%nat = true). Reconstr.rcrush (@Coq.NArith.Nnat.Nat2N.id, @Coq.NArith.Nnat.N2Nat.inj_compare, @Coq.NArith.BinNat.N.leb_le, @Coq.Arith.Compare_dec.leb_compare) (@Coq.NArith.BinNat.N.le). rewrite Nat2N.id in H. easy. lia. Qed. Lemma n_cases_all: forall n, n = O \/ n <> O. Proof. intro n. case_eq n; intros; [ now left | now right ]. Qed. Lemma n_cases_all_gt: forall n, n = O \/ (0 <? n)%nat = true. Proof. intros. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Arith.PeanoNat.Nat.eq_0_gt_0_cases) Reconstr.Empty. Qed. Lemma pos_pow: forall n: nat, (n > 0)%nat -> (2^n - 1 >= n)%nat. Proof. intro n. induction n; intros. - easy. - cbn. rewrite <- plus_n_O. case_eq n; intros. + cbn. lia. + assert ( (n > 0)%nat ). lia. specialize (IHn H1). rewrite H0 in IHn. cbn in IHn. rewrite <- plus_n_O in IHn. cbn. lia. Qed. Lemma pos_powN: forall n: N, (N.to_nat n > 0)%nat -> (2^(N.to_nat n) - 1 >= (N.to_nat n))%nat. Proof. intros. Reconstr.rsimple (@RAWBITVECTOR_LIST.pos_pow) Reconstr.Empty. Qed. (* bv -> nat *) (* Definitions *) (* bv2nat-defs *) Fixpoint pow2 (n: nat): nat := match n with | O => 1%nat | S n' => (2 * pow2 n')%nat end. Fixpoint _list2nat_be (a: list bool) (n i: nat) : nat := match a with | [] => n | xa :: xsa => if xa then _list2nat_be xsa (n + (pow2 i)) (i + 1) else _list2nat_be xsa n (i + 1) end. Definition list2nat_be (a: list bool) := _list2nat_be a 0 0. Definition bv2nat (a: list bool) := list2nat_be a. Fixpoint list2N (a: list bool) := match a with | [] => 0 | x :: xs => if x then N.succ_double (list2N xs) else N.double (list2N xs) end. Definition list2nat_be_a (a: list bool) := N.to_nat (list2N a). Definition bv2nat_a (a: list bool) := list2nat_be_a a. (* Theorems *) (* bv2nat-thrms *) Lemma list2N_mk_list_false: forall n, (list2N (mk_list_false n)) = 0%N. Proof. intro n. induction n; intros. + now cbn. + cbn. now rewrite IHn. Qed. Lemma list2N_app_true: forall a, N.to_nat (list2N (a ++ [true])) = ((N.to_nat (list2N a))%nat + Nat.pow 2 (length a))%nat. Proof. intro a. induction a; intros. - cbn. Reconstr.reasy (@Coq.PArith.Pnat.Pos2Nat.inj_1) Reconstr.Empty. - cbn. case_eq a; intros. + rewrite !N2Nat.inj_succ_double, IHa. cbn. rewrite <- !plus_n_O. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.add_shuffle1) Reconstr.Empty. + rewrite !N2Nat.inj_double. rewrite IHa. cbn. rewrite <- !plus_n_O. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.add_shuffle1) Reconstr.Empty. Qed. Lemma list2N_app_false: forall a, N.to_nat (list2N (a ++ [false])) = (N.to_nat (list2N a)). Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq a; intros. + rewrite !N2Nat.inj_succ_double, IHa. now cbn. + rewrite !N2Nat.inj_double. now rewrite IHa. Qed. Lemma list_lt_false: forall a, (N.to_nat (list2N a) <? N.to_nat (list2N (mk_list_false (length a))))%nat = false. Proof. intro a. induction a; intros. - now cbn. - cbn. rewrite list2N_mk_list_false. now cbn. Qed. Lemma skip1: forall n (s: list bool) a, skipn n s = skipn (S n) (a :: s). Proof. intro n. induction n; intros. - now cbn. - cbn. case_eq s; intros; easy. Qed. Lemma skipn_same_mktr: forall n, skipn n (mk_list_true n) ++ mk_list_true n = mk_list_true n. Proof. induction n; intros. - now cbn. - cbn in *. assert (true :: mk_list_true n = mk_list_true n ++ [true]). Reconstr.reasy (@RAWBITVECTOR_LIST.mk_list_true_succ, @RAWBITVECTOR_LIST.mk_list_true_app) Reconstr.Empty. now rewrite H, app_assoc, IHn. Qed. Lemma skipn_gt_false: forall n, (N.to_nat (list2N (mk_list_true n)) <? N.to_nat (list2N (skipn n (mk_list_true n) ++ mk_list_true n)))%nat = false. Proof. intros. rewrite skipn_same_mktr. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.ltb_irrefl) Reconstr.Empty. Qed. Lemma true_val_list: forall l, (N.to_nat (list2N (true :: l)) = (N.to_nat (N.succ_double (list2N l))))%nat. Proof. induction l; intros; now cbn. Qed. Lemma false_val_list: forall l, (N.to_nat (list2N (false :: l)) = (N.to_nat (N.double (list2N l))))%nat. Proof. induction l; intros; now cbn. Qed. Lemma skipn_nil: forall {A: Type} n, @skipn A n nil = nil. Proof. intros. induction n; intros; now cbn. Qed. Lemma skip0: forall {A: Type} l, @skipn A 0 l = l. Proof. intros. induction l; intros; now cbn. Qed. Lemma skipn_true_list: forall n l, (skipn n l ++ true :: mk_list_true n) = (skipn n l ++ mk_list_true n) ++ [true]. Proof. intro n. induction n; intros. - cbn. Reconstr.reasy (@Coq.Lists.List.app_nil_end) Reconstr.Empty. - cbn. case_eq l; intros. + cbn. Reconstr.reasy (@RAWBITVECTOR_LIST.mk_list_true_succ, @RAWBITVECTOR_LIST.mk_list_true_app) Reconstr.Empty. + Reconstr.reasy (@RAWBITVECTOR_LIST.mk_list_true_app, @RAWBITVECTOR_LIST.mk_list_true_succ, @Coq.Lists.List.app_comm_cons, @Coq.Lists.List.app_assoc) Reconstr.Empty. Qed. Lemma length_skipn: forall n (a: list bool), length (skipn n a) = (length a - n)%nat. Proof. intro n. induction n; intros. - cbn. now rewrite <- minus_n_O. - cbn. case_eq a; intros. now cbn. cbn. now rewrite <- IHn. Qed. Lemma skipn_true_val_list: forall n l, (n <? length l)%nat = true -> (N.to_nat (list2N (skipn n l ++ true :: mk_list_true n)) = N.to_nat (list2N (skipn n l ++ mk_list_true n)) + Nat.pow 2 (length l))%nat. Proof. intros. rewrite skipn_true_list, list2N_app_true. rewrite app_length, length_skipn, length_mk_list_true. Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.sub_add, @Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Arith.PeanoNat.Nat.lt_le_incl) Reconstr.Empty. Qed. Lemma mk_list_false_cons: forall n, mk_list_false n ++ [false] = false :: mk_list_false n. Proof. intro n. induction n; intros. now cbn. cbn. now rewrite <- IHn. Qed. Lemma skipn_false_list: forall n l, (skipn n l ++ false :: mk_list_false n) = (skipn n l ++ mk_list_false n) ++ [false]. Proof. intro n. induction n; intros. - cbn. Reconstr.reasy (@Coq.Lists.List.app_nil_end) Reconstr.Empty. - cbn. case_eq l; intros. + cbn. f_equal. Reconstr.reasy (@RAWBITVECTOR_LIST.mk_list_false_cons) Reconstr.Empty. + cbn. Reconstr.reasy (@Coq.Lists.List.app_assoc, @Coq.Lists.List.app_comm_cons, @RAWBITVECTOR_LIST.mk_list_false_cons) Reconstr.Empty. Qed. Lemma skipn_false_val_list: forall n l, (n <? length l)%nat = true -> (N.to_nat (list2N (skipn n l ++ false :: mk_list_false n)) = N.to_nat (list2N (skipn n l ++ mk_list_false n)))%nat. Proof. intros. now rewrite skipn_false_list, list2N_app_false. Qed. Lemma pow_eqb_1: forall n, ((S (N.to_nat (list2N (mk_list_true n))))%nat =? (2 ^ n))%nat = true. Proof. intros. induction n; intros. - now cbn. - cbn in *. case_eq ((2 ^ n)%nat); intros. + rewrite H in *. easy. + rewrite H in *. cbn. rewrite N2Nat.inj_succ_double. apply Nat.eqb_eq in IHn. rewrite IHn. apply Nat.eqb_eq. Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.add_succ_r, @Coq.Arith.PeanoNat.Nat.mul_1_l, @Coq.Arith.PeanoNat.Nat.add_0_r, @Coq.Arith.PeanoNat.Nat.mul_succ_l, @Coq.PArith.Pnat.Pos2Nat.inj_1) Reconstr.Empty. Qed. Lemma pow_eqb_0: forall n, (((N.to_nat (list2N (mk_list_true n))))%nat = (2 ^ n) - 1)%nat. Proof. intros. specialize (pow_eqb_1 n); intros. rewrite Nat.eqb_eq in H. rewrite <- H. cbn. lia. Qed. Lemma pow_gt: forall a, ((N.to_nat (list2N a))%nat <? (2 ^ length a)%nat)%nat = true. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq a; intros. + rewrite <- plus_n_O. case_eq ((2 ^ length a0 + 2 ^ length a0)%nat); intros. ++ contradict H0. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.pow_nonzero, @Coq.PArith.Pnat.Pos2Nat.inj_1) (@Coq.Init.Nat.add). ++ rewrite !N2Nat.inj_succ_double. cbn. case_eq n; intros. +++ subst. contradict H0. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.add_0_l, @Coq.Arith.PeanoNat.Nat.add_succ_r, @Coq.PArith.Pnat.Pos2Nat.inj_1) (@Coq.Init.Nat.add). +++ rewrite <- plus_n_O. rewrite H1 in *. assert (((S (S (N.to_nat (list2N a0) + N.to_nat (list2N a0))))%nat <=? (S (S n0))%nat)%nat = true). rewrite <- H0. rewrite Nat.ltb_lt in IHa. rewrite Nat.leb_le. lia. rewrite Nat.leb_le in H2. rewrite Nat.leb_le. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.add_1_l, @Coq.Arith.PeanoNat.Nat.lt_succ_r, @Coq.PArith.Pnat.Pos2Nat.inj_1) (@Coq.Init.Peano.lt). + rewrite <- plus_n_O. case_eq ((2 ^ length a0 + 2 ^ length a0)%nat); intros. ++ contradict H0. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.pow_nonzero, @Coq.PArith.Pnat.Pos2Nat.inj_1) (@Coq.Init.Nat.add). ++ rewrite !N2Nat.inj_double. cbn. case_eq n; intros. +++ subst. contradict H0. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.add_0_l, @Coq.Arith.PeanoNat.Nat.add_succ_r, @Coq.PArith.Pnat.Pos2Nat.inj_1) (@Coq.Init.Nat.add). +++ rewrite <- plus_n_O. rewrite H1 in *. assert (((S (S (N.to_nat (list2N a0) + N.to_nat (list2N a0))))%nat <=? (S (S n0))%nat)%nat = true). rewrite <- H0. rewrite Nat.ltb_lt in IHa. rewrite Nat.leb_le. lia. rewrite Nat.leb_le in H2. rewrite Nat.leb_le. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.add_1_l, @Coq.Arith.PeanoNat.Nat.lt_succ_r, @Coq.PArith.Pnat.Pos2Nat.inj_1) (@Coq.Init.Peano.lt). Qed. Lemma mk_list_true_cons : forall (b : bool) (l : list bool), mk_list_true (length (b :: l)) = true :: mk_list_true (length l). Proof. unfold mk_list_true. simpl. induction l; simpl; easy. Qed. Lemma pow_gtb_1: forall l, l <> mk_list_true (length l) -> ((S (N.to_nat (list2N l))) <? 2 ^ length l = true)%nat. Proof. intro l. induction l; intros. - cbn in *. easy. - case_eq a; intros. + assert ( l <> mk_list_true (length l)). subst. cbn in H. Reconstr.reasy Reconstr.Empty Reconstr.Empty. specialize (IHl H1). rewrite true_val_list. rewrite N2Nat.inj_succ_double. apply Nat.ltb_lt. apply Nat.ltb_lt in IHl. cbn. lia. + destruct (list_cases_all_true l). * rewrite H1. specialize (pow_eqb_1 (length l)); intros. rewrite !false_val_list. rewrite !N2Nat.inj_double. assert (length (false :: mk_list_true (length l)) = S (length l)). Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.add_0_l) (@Coq.Init.Datatypes.length, @RAWBITVECTOR_LIST.mk_list_true). rewrite H3. apply Nat.eqb_eq in H2. Reconstr.reasy (@RAWBITVECTOR_LIST.length_mk_list_true, @Coq.NArith.Nnat.N2Nat.inj_succ_double, @RAWBITVECTOR_LIST.true_val_list, @RAWBITVECTOR_LIST.pow_gt, @RAWBITVECTOR_LIST.mk_list_true_cons) Reconstr.Empty. * rewrite !false_val_list. rewrite !N2Nat.inj_double. assert (length (false :: l) = S (length l)). Reconstr.reasy Reconstr.Empty (@Coq.Init.Datatypes.length). rewrite H2. specialize (IHl H1). apply Nat.ltb_lt. apply Nat.ltb_lt in IHl. cbn. lia. Qed. Lemma ltb_plus: forall (a b n: nat), (a <? b)%nat = (a + n <? b + n)%nat. Proof. intros. revert a b. induction n; intros. + now rewrite <- !plus_n_O. + cbn. case_eq b; intros. cbn. symmetry. rewrite leb_iff_conv. lia. cbn. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.add_succ_r, @Coq.Arith.PeanoNat.Nat.leb_antisym, @Coq.Arith.PeanoNat.Nat.ltb_antisym, @Coq.Arith.PeanoNat.Nat.add_succ_l) (@Coq.Init.Nat.ltb). Qed. Lemma skipn_nm: forall n m, (n <? m)%nat = true -> (skipn n (mk_list_true m) ++ mk_list_true n) = (mk_list_true m). Proof. intro n. induction n; intros. - cbn. now rewrite app_nil_r. - cbn. case_eq m; intros. + subst. easy. + cbn. rewrite <- IHn at 2. Reconstr.rcrush (@Coq.PArith.Pnat.Pos2Nat.inj_1, @RAWBITVECTOR_LIST.ltb_plus, @RAWBITVECTOR_LIST.mk_list_true_app, @RAWBITVECTOR_LIST.mk_list_true_succ, @RAWBITVECTOR_LIST.skipn_true_list, @Coq.Arith.PeanoNat.Nat.add_1_r) Reconstr.Empty. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.ltb_irrefl) (@Coq.Init.Nat.leb, @Coq.Init.Nat.ltb). Qed. Lemma skipn_nm_false: forall n m, (n <? m)%nat = true -> (skipn n (mk_list_false m) ++ mk_list_false n) = (mk_list_false m). Proof. intro n. induction n; intros. - cbn. now rewrite app_nil_r. - cbn. case_eq m; intros. + subst. easy. + cbn. rewrite <- IHn at 2. Reconstr.rcrush (@Coq.PArith.Pnat.Pos2Nat.inj_1, @RAWBITVECTOR_LIST.ltb_plus, @RAWBITVECTOR_LIST.mk_list_false_cons, @RAWBITVECTOR_LIST.skipn_false_list, @Coq.Arith.PeanoNat.Nat.add_1_r) Reconstr.Empty. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.ltb_irrefl) (@Coq.Init.Nat.leb, @Coq.Init.Nat.ltb). Qed. Lemma skipn_gt: forall n s, (n <> 0)%nat -> (n <? length s)%nat = true -> s <> mk_list_true (length s) -> (N.to_nat (list2N s) <? N.to_nat (list2N (skipn n s ++ mk_list_true n)))%nat = true. Proof. intro n. induction n as [ | n IHn ]; intros. - simpl in *. easy. - simpl in *. case_eq s; intros. + subst. easy. + rewrite skipn_true_val_list. specialize (IHn l). case_eq b; intros. subst. rewrite true_val_list. rewrite N2Nat.inj_succ_double. destruct (n_cases_all n). rewrite H2. rewrite skip0. assert (mk_list_true 0 = nil) by easy. rewrite H3, app_nil_r. specialize (pow_gt l); intros. apply Nat.ltb_lt. apply Nat.ltb_lt in H4. cbn. specialize (@pow_gtb_1 l); intros. assert (l <> mk_list_true (length l)). cbn in H1. Reconstr.reasy Reconstr.Empty Reconstr.Empty. specialize (H5 H6). rewrite <- plus_n_O. apply Nat.ltb_lt in H5. cbn. lia. assert (l <> mk_list_true (length l)). cbn in H1. Reconstr.reasy Reconstr.Empty Reconstr.Empty. assert ((n <? length l)%nat = true). cbn in H0. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.lt_succ_r, @Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Arith.PeanoNat.Nat.leb_le) Reconstr.Empty. specialize (IHn H2 H4 H3). specialize (pow_gt l); intros. apply Nat.ltb_lt. apply Nat.ltb_lt in IHn. apply Nat.ltb_lt in H5. cbn. lia. destruct (list_cases_all_true l). rewrite false_val_list. rewrite H4. rewrite N2Nat.inj_double. apply Nat.ltb_lt. rewrite skipn_nm. Reconstr.rexhaustive1 (@Coq.NArith.Nnat.N2Nat.inj_succ_double, @RAWBITVECTOR_LIST.list2N_app_true, @RAWBITVECTOR_LIST.mk_list_true_app, @RAWBITVECTOR_LIST.mk_list_true_cons, @RAWBITVECTOR_LIST.true_val_list, @Coq.Init.Peano.le_n) (@Coq.Init.Peano.lt, @Coq.Init.Datatypes.length). simpl in H0. Reconstr.reasy (@RAWBITVECTOR_LIST.length_mk_list_true) (@Coq.Init.Datatypes.length, @Coq.Init.Nat.leb, @RAWBITVECTOR_LIST.mk_list_true, @Coq.Init.Nat.ltb). destruct (n_cases_all n). subst. rewrite false_val_list. rewrite N2Nat.inj_double. rewrite skip0. assert (mk_list_true 0 = nil) by easy. rewrite H2, app_nil_r. specialize (pow_gt l); intros. apply Nat.ltb_lt. apply Nat.ltb_lt in H3. cbn. lia. rewrite false_val_list. assert ((n <? length l)%nat = true). subst. cbn in H0. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.leb_le, @Coq.Arith.PeanoNat.Nat.lt_succ_r, @Coq.Arith.PeanoNat.Nat.ltb_lt) Reconstr.Empty. specialize (IHn H5 H6 H4). specialize (pow_gt l); intros. apply Nat.ltb_lt. apply Nat.ltb_lt in IHn. apply Nat.ltb_lt in H7. rewrite N2Nat.inj_double. cbn. lia. subst. cbn in *. easy. Qed. Lemma pow_ltb: forall l, l <> mk_list_true (length l) -> (N.to_nat (list2N l) <? N.to_nat (list2N (mk_list_true (length l))) = true)%nat. Proof. intros. rewrite pow_eqb_0. specialize (@pow_gtb_1 l H); intros. apply Nat.ltb_lt. apply Nat.ltb_lt in H0. lia. Qed. Lemma pow_ltb_false: forall l, (N.to_nat (list2N (mk_list_true (length l))) <? N.to_nat (list2N l) = false)%nat. Proof. intros. rewrite pow_eqb_0. destruct (list_cases_all_true l). - rewrite H at 2. Reconstr.reasy (@RAWBITVECTOR_LIST.pow_eqb_0, @RAWBITVECTOR_LIST.skipn_same_mktr, @RAWBITVECTOR_LIST.skipn_gt_false) Reconstr.Empty. - specialize (@pow_gtb_1 l H); intros. apply Nat.ltb_lt in H0. Reconstr.rscrush (@Coq.Arith.PeanoNat.Nat.add_1_r, @Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Arith.PeanoNat.Nat.neq_0_lt_0, @Coq.Arith.PeanoNat.Nat.pow_nonzero, @Coq.Arith.PeanoNat.Nat.sub_add, @Coq.PArith.Pnat.Pos2Nat.inj_1, @RAWBITVECTOR_LIST.pow_gt, @Coq.Arith.Compare_dec.leb_correct_conv) (@Coq.Init.Peano.lt, @RAWBITVECTOR_LIST.list2nat_be_a, @Coq.Init.Nat.ltb). Qed. Lemma bv2nat_gt0: forall t a, (a <? bv2nat_a t)%nat = true -> t <> mk_list_false (length t). Proof. intro t. induction t; intros. - cbn in *. easy. - unfold bv2nat_a, list2nat_be_a in *. cbn. case_eq a; intros. + easy. + rewrite H0 in *. rewrite false_val_list in H. rewrite N2Nat.inj_double in H. assert ((Nat.div a0 2 <? N.to_nat (list2N t))%nat = true). apply Nat.ltb_lt. apply Nat.ltb_lt in H. Reconstr.rexhaustive1 (@Coq.Arith.PeanoNat.Nat.div_lt_upper_bound, @Coq.PArith.Pnat.Pos2Nat.inj_1) Reconstr.Empty. specialize (IHt (Nat.div a0 2 ) H1). Reconstr.reasy Reconstr.Empty Reconstr.Empty. Qed. Lemma gt0_nmk_list_false: forall l, l <> mk_list_false (length l) -> (0 <? (N.to_nat (list2N l)))%nat = true. Proof. intro l. induction l; intros. - cbn in *. easy. - case_eq a; intros. + subst. rewrite true_val_list. rewrite N2Nat.inj_succ_double. easy. + subst. rewrite false_val_list. assert (l <> mk_list_false (length l)). cbn in H. Reconstr.reasy Reconstr.Empty Reconstr.Empty. specialize (IHl H0). rewrite N2Nat.inj_double. Reconstr.reasy (@Coq.NArith.Nnat.N2Nat.inj_double) (@Coq.Init.Nat.ltb, @Coq.Init.Nat.mul, @Coq.Init.Nat.leb, @RAWBITVECTOR_LIST.list2N, @Coq.Init.Nat.add). Qed. Lemma skipn_lt: forall n s, (n <> 0)%nat -> (n <? length s)%nat = true -> s <> mk_list_false (length s) -> (N.to_nat (list2N (skipn n s ++ mk_list_false n)) <? N.to_nat (list2N s))%nat = true. Proof. intro n. induction n as [ | n IHn ]; intros. - simpl in *. easy. - simpl in *. case_eq s; intros. + subst. easy. + rewrite skipn_false_val_list. specialize (IHn l). case_eq b; intros. subst. rewrite true_val_list. rewrite N2Nat.inj_succ_double. destruct (n_cases_all n). rewrite H2. rewrite skip0. assert (mk_list_false 0 = nil) by easy. rewrite H3, app_nil_r. apply Nat.ltb_lt. cbn. lia. destruct (list_cases_all_false l). rewrite H3. rewrite skipn_nm_false. Reconstr.rsimple (@Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Init.Peano.le_n, @Coq.NArith.Nnat.N2Nat.inj_double, @Coq.PArith.Pnat.Pos2Nat.inj_1, @RAWBITVECTOR_LIST.false_val_list, @RAWBITVECTOR_LIST.list2N_app_false, @RAWBITVECTOR_LIST.list2N_mk_list_false, @RAWBITVECTOR_LIST.mk_list_false_cons, @Coq.Arith.PeanoNat.Nat.lt_succ_r) (@Coq.NArith.BinNatDef.N.to_nat). cbn in H0. Reconstr.reasy Reconstr.Empty (@Coq.Init.Nat.leb, @Coq.Init.Nat.ltb). assert ((n <? length l)%nat = true ). Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.lt_succ_r, @RAWBITVECTOR_LIST.mk_list_true_cons, @Coq.Arith.PeanoNat.Nat.ltb_lt) (@Coq.Init.Peano.lt, @Coq.Init.Datatypes.length, @RAWBITVECTOR_LIST.mk_list_true, @RAWBITVECTOR_LIST.mk_list_false). specialize (IHn H2 H4 H3). apply Nat.ltb_lt. apply Nat.ltb_lt in IHn. lia. rewrite false_val_list. rewrite N2Nat.inj_double. destruct (n_cases_all n). rewrite H4. rewrite skip0. assert (mk_list_false 0 = nil) by easy. rewrite H5, app_nil_r. assert (l <> mk_list_false (length l)). subst. cbn in H1. Reconstr.reasy Reconstr.Empty Reconstr.Empty. specialize (@gt0_nmk_list_false l H6); intros. apply Nat.ltb_lt. apply Nat.ltb_lt in H7. lia. assert (l <> mk_list_false (length l)). cbn in H1. Reconstr.reasy Reconstr.Empty Reconstr.Empty. assert ((n <? length l)%nat = true). cbn in H0. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.lt_succ_r, @Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Arith.PeanoNat.Nat.leb_le) Reconstr.Empty. specialize (IHn H4 H6 H5). apply Nat.ltb_lt. apply Nat.ltb_lt in IHn. cbn. lia. Reconstr.reasy Reconstr.Empty (@Coq.Init.Datatypes.length, @Coq.Init.Nat.ltb, @Coq.Init.Nat.leb). Qed. Lemma mk_list_false_not_true: forall l, l <> mk_list_true (length l) -> bv_not l <> mk_list_false (length l). Proof. intro l. induction l; intros. - cbn in *. easy. - cbn. Reconstr.rsimple (@RAWBITVECTOR_LIST.mk_list_true_cons, @RAWBITVECTOR_LIST.add_neg_list_carry_neg_f, @RAWBITVECTOR_LIST.add_list_carry_empty_neutral_r) (@Coq.Init.Datatypes.negb). Qed. Lemma not_mk_list_false: forall l, l <> mk_list_false (length l) -> (0 <? N.to_nat (list2N l))%nat = true. Proof. intro l. induction l; intros. - cbn in *. easy. - cbn in H. Reconstr.reasy (@RAWBITVECTOR_LIST.gt0_nmk_list_false, @Coq.Lists.List.app_nil_r) (@RAWBITVECTOR_LIST.list2N, @RAWBITVECTOR_LIST.mk_list_false, @Coq.Init.Datatypes.length). Qed. (* nat -> bv *) (* Definitions *) (* nat2bv-defs *) (* positive binary nat to bool list *) Fixpoint pos2list (n: positive) acc := match n with | xI m => pos2list m (acc ++ [true]) | xO m => pos2list m (acc ++ [false]) | xH => (acc ++ [true]) end. (* binary nat to bool list *) Definition N2list (n: N) s := match n with | N0 => mk_list_false s | Npos p => if (s <? (N.to_nat (N.size n)))%nat then (firstn s (pos2list p [])) else (pos2list p []) ++ mk_list_false (s - (N.to_nat (N.size n))) end. (* nat to bv *) Definition nat2bv (n: nat) (s: N): bitvector := N2list (N.of_nat n) (N.to_nat s). (* Theorems *) (* nat2bv-thrms *) Lemma pos2list_acc: forall p a, (pos2list p a) = a ++ (pos2list p []). Proof. intro p. induction p; intros. - cbn. rewrite IHp. specialize (IHp [true]). rewrite IHp. now rewrite app_assoc. - cbn. rewrite IHp. specialize (IHp [false]). rewrite IHp. now rewrite app_assoc. - now cbn. Qed. Lemma length_pos2list_acc: forall p a, (length (pos2list p a)) = (length a + length (pos2list p []))%nat. Proof. intros p a. now rewrite pos2list_acc, app_length. Qed. Lemma length_pos2list_nil: forall p, length (pos2list p []) = Pos.to_nat (Pos.size p). Proof. intro p. induction p; intros. - cbn. rewrite length_pos2list_acc. cbn. rewrite IHp. admit. (* Reconstr.reasy (@Coq.PArith.Pnat.Pos2Nat.inj_succ) Reconstr.Empty. *) - cbn. rewrite length_pos2list_acc. cbn. rewrite IHp. admit. (* Reconstr.reasy (@Coq.PArith.Pnat.Pos2Nat.inj_succ) Reconstr.Empty. *) - now cbn. Admitted. Lemma length_pos2list: forall p a, (length (pos2list p a)) = (length a + N.to_nat (N.size (Npos p)))%nat. Proof. intros. rewrite pos2list_acc, app_length. f_equal. rewrite length_pos2list_nil. admit. (* Reconstr.reasy (@Coq.ZArith.Znat.positive_N_nat) (@Coq.NArith.BinNatDef.N.size). *) Admitted. Lemma length_N2list: forall n s, length (N2list n s) = s. Proof. intro n. induction n; intros. - cbn. now rewrite length_mk_list_false. - cbn. case_eq (Pos.to_nat (Pos.size p)); intros. + contradict H. admit. (* Reconstr.reasy (@Coq.PArith.Pnat.Pos2Nat.is_pos, @Coq.Arith.PeanoNat.Nat.neq_0_lt_0) Reconstr.Empty. *) + case_eq ( (s <=? n)%nat); intros. * rewrite firstn_length. rewrite length_pos2list_nil, H. admit. (* Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.lt_eq_cases, @Coq.Arith.PeanoNat.Nat.min_l, @Coq.Arith.PeanoNat.Nat.leb_le, @Coq.Arith.PeanoNat.Nat.succ_le_mono) (@Coq.Init.Nat.min, @Coq.Init.Peano.lt). *) * rewrite app_length, length_pos2list_nil, H, length_mk_list_false. admit. (* Reconstr.reasy (@Coq.Arith.Minus.le_plus_minus, @Coq.Arith.Compare_dec.leb_complete_conv) (@Coq.Init.Peano.lt). *) Admitted. Lemma length_nat2bv: forall n s, length (nat2bv n s) = N.to_nat s. Proof. intros. unfold nat2bv. now rewrite length_N2list. Qed. Lemma nat2bv_size: forall (n: nat) (s: N), size (nat2bv n s) = s. Proof. intros. Reconstr.reasy (@Coq.NArith.Nnat.N2Nat.id, @RAWBITVECTOR_LIST.length_nat2bv) (@RAWBITVECTOR_LIST.size). Qed. Lemma N2list_S_true: forall n m, N2list (N.succ_double n) (S m) = true :: N2list n m. Proof. intro n. induction n; intros. - cbn. rewrite Pos2Nat.inj_1. assert ((m - 0)%nat = m) by lia. now rewrite H. - cbn. case_eq (Pos.to_nat (Pos.size p)); intros. + cbn. contradict H. admit. (* Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.neq_0_lt_0, @Coq.PArith.Pnat.Pos2Nat.is_pos) Reconstr.Empty. *) + assert ((Pos.to_nat (Pos.succ (Pos.size p))%nat = S (S n))). { admit. (* Reconstr.reasy (@Coq.PArith.Pnat.Pos2Nat.inj_succ) Reconstr.Empty. *) } rewrite H0. case_eq (m <=? n)%nat; intros; rewrite pos2list_acc; now cbn. Admitted. Lemma N2list_S_false: forall n m, N2list (N.double n) (S m) = false :: N2list n m. Proof. intro n. induction n; intros. - now cbn. - cbn. case_eq (Pos.to_nat (Pos.size p)); intros. + cbn. contradict H. admit. (* Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.neq_0_lt_0, @Coq.PArith.Pnat.Pos2Nat.is_pos) Reconstr.Empty. *) + assert ((Pos.to_nat (Pos.succ (Pos.size p))%nat = S (S n))). { admit. (* Reconstr.reasy (@Coq.PArith.Pnat.Pos2Nat.inj_succ) Reconstr.Empty. *) } rewrite H0. case_eq (m <=? n)%nat; intros; rewrite pos2list_acc; now cbn. Admitted. Lemma N2List_list2N: forall a, N2list (list2N a) (length a) = a. Proof. intro a. induction a; intros. - now cbn. - cbn in *. case_eq a; intros. + now rewrite N2list_S_true, IHa. + now rewrite N2list_S_false, IHa. Qed. Lemma list2N_pos2list: forall p, list2N (pos2list p []) = N.pos p. Proof. intro p. induction p; intros. - cbn. rewrite pos2list_acc. cbn. rewrite IHp. Reconstr.reasy Reconstr.Empty (@Coq.NArith.BinNatDef.N.succ_double). - cbn. rewrite pos2list_acc. cbn. rewrite IHp. Reconstr.reasy Reconstr.Empty (@Coq.NArith.BinNatDef.N.succ_double). - now cbn. Qed. Lemma list2N_N2List: forall a, list2N (N2list a (N.to_nat (N.size a))) = a. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq (Pos.to_nat (Pos.size p)); intros. + contradict H. Reconstr.rcrush (@Coq.PArith.Pnat.Pos2Nat.is_pos, @Coq.Arith.PeanoNat.Nat.neq_0_lt_0) Reconstr.Empty. + assert ((S n <=? n)%nat = false). rewrite Nat.leb_gt. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.lt_succ_diag_r) Reconstr.Empty. rewrite H0. cbn. assert (mk_list_false (n - n) = nil). { Reconstr.rsimple (@Coq.Init.Peano.plus_n_O, @Coq.Init.Peano.le_n, @Coq.Arith.PeanoNat.Nat.sub_0_le) (@RAWBITVECTOR_LIST.mk_list_false). } now rewrite H1, app_nil_r, list2N_pos2list. Qed. Lemma listE: forall n, (list2N (mk_list_false n)) = 0. Proof. intro n. induction n; intros; try now cbn. cbn. rewrite IHn. easy. Qed. Lemma pos2list_mk_list_false: forall p n, list2N (pos2list p [] ++ mk_list_false n) = N.pos p. Proof. intro p. induction p; intros. - cbn. rewrite pos2list_acc. cbn. rewrite IHp. easy. - cbn. rewrite pos2list_acc. cbn. rewrite IHp. easy. - cbn. rewrite listE. easy. Qed. Lemma list2N_N2List_s: forall a n, ((N.to_nat (N.size a)) <=? n)%nat = true -> list2N (N2list a n) = a. Proof. intro a. induction a; intros. - cbn. now rewrite listE. - cbn. case_eq (Pos.to_nat (Pos.size p)); intros. + contradict H0. Reconstr.rcrush (@Coq.PArith.Pnat.Pos2Nat.is_pos, @Coq.Arith.PeanoNat.Nat.neq_0_lt_0) Reconstr.Empty. + assert ((n <=? n0)%nat = false). unfold N.size in *. Reconstr.rsimple (@Coq.ZArith.Znat.positive_N_nat, @Coq.Arith.PeanoNat.Nat.leb_le, @Coq.Arith.Compare_dec.leb_correct_conv) (@Coq.Init.Peano.lt). now rewrite H1, pos2list_mk_list_false. Qed. Lemma list2N_N2List_eq: forall a, list2N (N2list a (N.to_nat a)) = a. Proof. intros. rewrite list2N_N2List_s. easy. specialize (size_gt (N.to_nat a)); intro HH. rewrite N2Nat.id in HH. easy. Qed. (* forall b, toNat(b) >= 0 *) Lemma bvgez: forall a: bitvector, (bv2nat_a a = 0%nat) \/ (bv2nat_a a > 0)%nat. Proof. intro a. induction a. - cbn. left. easy. - case_eq a; intros. + right. unfold bv2nat_a, list2nat_be_a. Reconstr.rsimple (@Coq.Arith.Gt.gt_0_eq, @Coq.Arith.PeanoNat.Nat.add_0_l, @Coq.NArith.Nnat.N2Nat.inj_succ_double) (@list2nat_be_a, @list2N). + unfold bv2nat_a, list2nat_be_a. destruct IHa. * left. Reconstr.rblast (@Coq.NArith.Nnat.N2Nat.id, @list2N_N2List, @Coq.Init.Peano.O_S, @Coq.NArith.Nnat.Nat2N.id) (@Coq.NArith.BinNatDef.N.of_nat, @list2nat_be_a, @bv2nat_a, @Coq.NArith.BinNatDef.N.double, @list2N). * right. cbn. Reconstr.rsimple (@Coq.NArith.Nnat.Nat2N.id, @Coq.PArith.Pnat.Pos2Nat.is_pos, @Coq.Arith.PeanoNat.Nat.lt_irrefl, @list2N_N2List) (@list2nat_be_a, @Coq.NArith.BinNatDef.N.to_nat, @Coq.NArith.BinNatDef.N.of_nat, @Coq.Init.Peano.gt, @Coq.NArith.BinNatDef.N.double, @bv2nat_a). Qed. (*-------------------- Comparison Operators --------------------*) (* Definitions *) (* comp-defs *) (* unsigned greater than *) (* ugt-defs *) Fixpoint ugt_list_big_endian (x y: list bool) := match x, y with | nil, _ => false | _ , nil => false | xi :: nil, yi :: nil => andb xi (negb yi) | xi :: x', yi :: y' => orb (andb (Bool.eqb xi yi) (ugt_list_big_endian x' y')) (andb xi (negb yi)) end. (* bool output *) Definition ugt_list (x y: list bool) := (ugt_list_big_endian (List.rev x) (List.rev y)). Definition bv_ugt (a b : bitvector) : bool := if @size a =? @size b then ugt_list a b else false. (* Prop output *) Definition ugt_listP (x y: list bool) := if ugt_list x y then True else False. Definition bv_ugtP (a b : bitvector) : Prop := if @size a =? @size b then ugt_listP a b else False. (* unsigned less than *) (* ult-defs *) Fixpoint ult_list_big_endian (x y: list bool) := match x, y with | nil, _ => false | _ , nil => false | xi :: nil, yi :: nil => andb (negb xi) yi | xi :: x', yi :: y' => orb (andb (Bool.eqb xi yi) (ult_list_big_endian x' y')) (andb (negb xi) yi) end. (* bool output *) Definition ult_list (x y: list bool) := (ult_list_big_endian (List.rev x) (List.rev y)). Definition bv_ult (a b : bitvector) : bool := if @size a =? @size b then ult_list a b else false. (* Prop output *) Definition ult_listP (x y: list bool) := if ult_list x y then True else False. Definition bv_ultP (a b : bitvector) : Prop := if @size a =? @size b then ult_listP a b else False. (* unsigned less than or equal to*) (* ule-defs *) Fixpoint ule_list_big_endian (x y : list bool) := match x, y with | nil, nil => true | nil, _ => false | _, nil => false | xi :: nil, yi :: nil => orb (eqb xi yi) (andb (negb xi) yi) | xi :: x', yi :: y' => orb (andb (Bool.eqb xi yi) (ule_list_big_endian x' y')) (andb (negb xi) yi) end. (* bool output *) Definition ule_list (x y: list bool) := (ule_list_big_endian (List.rev x) (List.rev y)). Definition bv_ule (a b : bitvector) : bool := if @size a =? @size b then ule_list a b else false. (* Prop output *) Definition ule_listP (x y: list bool) := if ule_list x y then True else False. Definition bv_uleP (a b : bitvector) : Prop := if @size a =? @size b then ule_listP a b else False. (* Signed less than *) (* slt-defs *) Fixpoint slt_list_big_endian (x y: list bool) := match x, y with | nil, _ => false | _ , nil => false | xi :: nil, yi :: nil => andb xi (negb yi) | xi :: x', yi :: y' => orb (andb (Bool.eqb xi yi) (ult_list_big_endian x' y')) (andb xi (negb yi)) end. (* bool output *) Definition slt_list (x y: list bool) := slt_list_big_endian (List.rev x) (List.rev y). Definition bv_slt (a b : bitvector) : bool := if @size a =? @size b then slt_list a b else false. (* Prop output *) Definition slt_listP (x y: list bool) := if slt_list x y then True else False. Definition bv_sltP (a b : bitvector) : Prop := if @size a =? @size b then slt_listP a b else False. (* Theorems *) (* comp-thrms *) (* Unsigned greater than *) (* ugt-thrms *) (* forall x y, x > y => x != y *) Lemma ugt_list_big_endian_not_eq : forall x y, ugt_list_big_endian x y = true -> x <> y. Proof. intros x. induction x. + simpl. easy. + intros y. case y. - easy. - intros b l. simpl. specialize (IHx l). case x in *. * simpl. case l in *. { case a; case b; simpl; easy. } { easy. } * rewrite !orb_true_iff, !andb_true_iff. intro. destruct H. { destruct H. apply IHx in H0. apply Bool.eqb_prop in H. rewrite H. unfold not in *; intro. inversion H1; subst. now apply H0. } { destruct H. apply negb_true_iff in H0. subst. easy. } Qed. (* Boolean comparison *) Lemma ugt_list_not_eq : forall x y, ugt_list x y = true -> x <> y. Proof. unfold ugt_list. unfold not. intros. apply ugt_list_big_endian_not_eq in H. subst. auto. Qed. Lemma bv_ugt_not_eq : forall x y, bv_ugt x y = true -> x <> y. Proof. intros x y. unfold bv_ugt. case_eq (size x =? size y); intros. - now apply ugt_list_not_eq in H0. - now contradict H0. Qed. (* Prop comparison *) Lemma ugt_list_not_eqP : forall x y, ugt_listP x y -> x <> y. Proof. unfold ugt_listP. unfold not. intros. unfold ugt_list in H. case_eq (ugt_list_big_endian (List.rev x) (List.rev y)). + intros. apply ugt_list_big_endian_not_eq in H1. subst. now contradict H1. + intros. now rewrite H1 in H. Qed. Lemma bv_ugt_not_eqP : forall x y, bv_ugtP x y -> x <> y. Proof. intros x y. unfold bv_ugtP. case_eq (size x =? size y); intros. - now apply ugt_list_not_eqP in H0. - now contradict H0. Qed. (* Equivalence of boolean and Prop comparisons *) Lemma bv_ugt_B2P: forall x y, bv_ugt x y = true <-> bv_ugtP x y. Proof. intros. split; intros; unfold bv_ugt, bv_ugtP in *. + case_eq (size x =? size y); intros. - rewrite H0 in H. unfold ugt_listP. now rewrite H. - rewrite H0 in H. unfold ugt_listP. now contradict H. + unfold ugt_listP in *. case_eq (size x =? size y); intros. - rewrite H0 in H. case_eq (ugt_list x y); intros. * easy. * rewrite H1 in H. now contradict H. - rewrite H0 in H. now contradict H. Qed. (* a >u b -> ~(a <u b) *) Lemma ugt_list_big_endian_not_ult_list_big_endian : forall x y, ugt_list_big_endian x y = true -> ult_list_big_endian x y = false. Proof. intros x. induction x. + simpl. easy. + intros y. case y. - intros. case a; case x; easy. - intros b l. simpl. specialize (IHx l). case x in *. * simpl. case l in *. { case a; case b; simpl; easy. } { case a; case b; simpl; easy. } * rewrite !orb_true_iff, !andb_true_iff. intro. destruct H. { destruct H. apply IHx in H0. apply Bool.eqb_prop in H. rewrite H. rewrite H0. case b; easy. } { destruct H. apply negb_true_iff in H0. subst. easy. } Qed. Lemma ugt_list_not_ult_list : forall x y, ugt_list x y = true -> ult_list x y = false. Proof. intros x y. unfold ugt_list. intros. apply ugt_list_big_endian_not_ult_list_big_endian in H. unfold ult_list. apply H. Qed. Lemma bv_ugt_not_bv_ult : forall x y, bv_ugt x y = true -> bv_ult x y = false. Proof. intros x y. unfold bv_ugt. case_eq (size x =? size y); intros. - apply ugt_list_not_ult_list in H0. unfold bv_ult. rewrite H. apply H0. - now contradict H0. Qed. Lemma ugt_listP_not_ult_listP : forall x y, ugt_listP x y -> ~ (ult_listP x y). Proof. unfold ugt_listP. unfold not. intros. unfold ugt_list in H. case_eq (ugt_list_big_endian (List.rev x) (List.rev y)). + intros. apply ugt_list_big_endian_not_ult_list_big_endian in H1. unfold ult_listP in H0. unfold ult_list in H0. rewrite H1 in H0. now contradict H0. + intros. now rewrite H1 in H. Qed. Lemma bv_ugtP_not_bv_ultP : forall x y, bv_ugtP x y -> ~ (bv_ultP x y). Proof. intros x y. unfold bv_ugtP. unfold not. case_eq (size x =? size y); intros. - unfold ugt_listP in H0. unfold bv_ultP in H1. rewrite H in H1. unfold ult_listP in H1. now apply ugt_listP_not_ult_listP in H0. - now contradict H0. Qed. (*a >u b -> b <u a *) Lemma ugt_list_big_endian_ult_list_big_endian : forall x y, ugt_list_big_endian x y = true -> ult_list_big_endian y x = true. Proof. intros x. induction x. + simpl. easy. + intros y. case y. - intros. case a; case x in *; simpl in H; now contradict H. - intros b l. simpl. specialize (IHx l). case x in *. * simpl. case l in *. { case a; case b; simpl; easy. } { case a; case b; simpl; easy. } * rewrite !orb_true_iff, !andb_true_iff. intro. destruct H. { destruct H. apply IHx in H0. apply Bool.eqb_prop in H. rewrite H. rewrite H0. case b; case l; easy. } { destruct H. apply negb_true_iff in H0. subst. case l; easy. } Qed. Lemma ugt_list_ult_list : forall x y, ugt_list x y = true -> ult_list y x = true. Proof. intros x y. unfold ugt_list. intros. apply ugt_list_big_endian_ult_list_big_endian in H. unfold ult_list. apply H. Qed. Lemma bv_ugt_bv_ult : forall x y, bv_ugt x y = true -> bv_ult y x = true. Proof. intros x y. unfold bv_ugt. case_eq (size x =? size y); intros. - apply ugt_list_ult_list in H0. unfold bv_ult. rewrite (@eqb_sym (size x) (size y)) in H. rewrite H. apply H0. - now contradict H0. Qed. Lemma ugt_listP_ult_listP : forall x y, ugt_listP x y -> ult_listP y x. Proof. unfold ugt_listP. intros. unfold ugt_list in H. case_eq (ugt_list_big_endian (List.rev x) (List.rev y)). + intros. unfold ult_listP. unfold ult_list. apply (@ugt_list_big_endian_ult_list_big_endian (List.rev x) (List.rev y)) in H0. rewrite H0. easy. + intros. rewrite H0 in H. now contradict H. Qed. Lemma bv_ugtP_bv_ultP : forall x y, bv_ugtP x y -> (bv_ultP y x). Proof. intros x y. unfold bv_ugtP, bv_ultP. rewrite (@eqb_sym (size y) (size x)). case_eq (size x =? size y); intros. - apply ugt_listP_ult_listP. apply H0. - apply H0. Qed. (* forall a, (exists b, b > a) => (a != 1) *) Lemma not_ugt_list_big_endian_ones : forall (b : bitvector), ugt_list_big_endian (rev b) (rev (bv_not (zeros (size b)))) = false. Proof. intros. unfold zeros. unfold size. rewrite Nat2N.id. rewrite bv_not_false_true. assert (forall n : nat, rev (mk_list_true n) = mk_list_true n). { apply rev_mk_list_true. } rewrite H. rewrite <- rev_length. induction (rev b). + easy. + simpl. destruct l. - simpl. apply andb_false_r. - rewrite IHl. rewrite andb_false_r. rewrite andb_false_r. easy. Qed. Lemma not_ugt_list_ones : forall (b : bitvector), ugt_list b (bv_not (zeros (size b))) = false. Proof. intros. unfold ugt_list. assert (forall (b : bitvector), ugt_list_big_endian (rev b) (rev (bv_not (zeros (size b)))) = false). { apply not_ugt_list_big_endian_ones. } apply H. Qed. Lemma not_ugt_listP_ones : forall (b : bitvector), ~ ugt_listP b (bv_not (zeros (size b))). Proof. intros. unfold not. intros. unfold ugt_listP in H. assert (forall (b : bitvector), ugt_list b (bv_not (zeros (size b))) = false). { apply not_ugt_list_ones. } specialize (@H0 b). rewrite H0 in H. apply H. Qed. Lemma bv_ugtP_not_ones : forall (a : bitvector), (exists (b : bitvector), bv_ugtP b a) -> ~ (a = bv_not (zeros (size a))). Proof. intros. destruct H as (b, H). unfold not. intros. unfold bv_ugtP in H. case_eq (size b =? size a). + intros. pose proof H1 as H1Prop. apply Neqb_ok in H1Prop. rewrite <- H1Prop in H0. rewrite H1 in H. rewrite H0 in H. pose proof (@not_ugt_listP_ones b). unfold not in H2. apply H2. apply H. + intros. rewrite H1 in H. apply H. Qed. (* b != 0 -> b > 0 *) Theorem rev_func : forall (b1 b2 : bitvector), b1 = b2 -> rev b1 = rev b2. Proof. intros. rewrite -> H. reflexivity. Qed. Theorem rev_inj : forall (b1 b2 : bitvector), rev b1 = rev b2 -> b1 = b2. Proof. intros. rewrite <- rev_involutive with (l := b1). rewrite <- rev_involutive with (l := b2). apply rev_func. apply H. Qed. Lemma rev_neg_func : forall (b1 b2 : bitvector), b1 <> b2 -> rev b1 <> rev b2. Proof. intros. unfold not. intros. apply rev_inj in H0. unfold not in H. apply H in H0. apply H0. Qed. Theorem rev_neg_inj : forall (b1 b2 : bitvector), rev b1 <> rev b2 -> b1 <> b2. Proof. intros. rewrite <- rev_involutive with (l := b1). rewrite <- rev_involutive with (l := b2). apply rev_neg_func. apply H. Qed. Lemma rev_uneq2 : forall b : bitvector, b <> mk_list_false (length b) -> (rev b) <> (rev (mk_list_false (length b))). Proof. intros. apply rev_neg_inj. rewrite rev_involutive. rewrite rev_involutive. apply H. Qed. Lemma ugt_list_big_endian_false : forall (b1 b2 : list bool), ugt_list_big_endian b1 b2 = true -> ugt_list_big_endian (false :: b1) (false :: b2) = true. Proof. intros. unfold ugt_list_big_endian. case b1 in *. + case b2 in *; easy. + case b2 in *. - simpl in H. case b1 in *; easy. - rewrite orb_true_iff, andb_true_iff. left. simpl. split. * easy. * fold ugt_list_big_endian. apply H. Qed. Lemma mk_list_false_cons2 : forall (b : bool) (l : list bool), mk_list_false (length (b :: l)) = false :: mk_list_false (length l). Proof. unfold mk_list_false. simpl. induction l; simpl; easy. Qed. Lemma bv_not_0_ugt_list_big_endian_0 : forall (b : bitvector), b <> mk_list_false (length b) -> ugt_list_big_endian (rev b) (rev (mk_list_false (length b))) = true. Proof. intros. rewrite rev_mk_list_false. assert (rev_uneq : forall b : bitvector, b <> mk_list_false (length b) -> (rev b) <> (rev (mk_list_false (length b)))). { apply rev_uneq2. } apply rev_uneq2 in H. rewrite <- rev_length in *. rewrite rev_mk_list_false in H. destruct (rev b) as [| h t]. + now contradict H. + assert (ugt_cons_true : forall (b : list bool), ugt_list_big_endian (true :: b) (mk_list_false (length (true :: b))) = true). { intros. induction b0; easy. } destruct h. - apply ugt_cons_true. - induction t as [| h2 t2 IH]. * easy. * assert (ugt_cons_false : forall (b : list bool), ugt_list_big_endian b (mk_list_false (length b)) = true -> ugt_list_big_endian (false :: b) (mk_list_false (length (false :: b))) = true). { pose proof ugt_list_big_endian_false as ugt_list_big_endian_false. intros. specialize (@ugt_list_big_endian_false b0 (mk_list_false (length b0)) H0). pose proof mk_list_false_cons2 as mk_list_false_cons2. specialize (@mk_list_false_cons2 false b0). rewrite mk_list_false_cons2. apply ugt_list_big_endian_false. } destruct h2. { assert (forall (b : list bool), false :: b <> mk_list_false (length (false :: b)) -> b <> mk_list_false (length b)). { intros. rewrite mk_list_false_cons2 in H0. unfold not. intros. unfold not in H0. rewrite <- H1 in H0. now contradict H0. } apply H0 in H. apply ugt_cons_false. apply ugt_cons_true. } { assert (forall (b : list bool), false :: b <> mk_list_false (length (false :: b)) -> b <> mk_list_false (length b)). { intros. rewrite mk_list_false_cons2 in H0. unfold not. intros. unfold not in H0. rewrite <- H1 in H0. now contradict H0. } apply H0 in H. apply IH in H. apply ugt_cons_false. apply H. } Qed. Lemma bv_not_eq_0_ugt_listP :forall b : bitvector, b <> (zeros (size b)) -> ugt_listP b (zeros (size b)). Proof. intros. unfold ugt_listP. case_eq (ugt_list b (zeros (size b))). + intros. easy. + intros. unfold ugt_list in H0. unfold zeros in *. unfold size in *. rewrite Nat2N.id in *. assert (forall (b : bitvector), b <> mk_list_false (length b) -> ugt_list_big_endian (rev b) (rev (mk_list_false (length b))) = true). { apply bv_not_0_ugt_list_big_endian_0. } specialize (@H1 b). specialize (@H1 H). rewrite H0 in H1. now contradict H1. Qed. Lemma bv_not_eq_0_ugtP_0 : forall (b : bitvector), b <> (zeros (size b)) -> bv_ugtP b (zeros (size b)). Proof. intros. unfold bv_ugtP. rewrite zeros_size. rewrite eqb_refl. assert (forall b : bitvector, b <> (zeros (size b)) -> ugt_listP b (zeros (size b))). { apply bv_not_eq_0_ugt_listP. } apply H0. apply H. Qed. Lemma bv_not_eq_0_ugt_0 : forall (b : bitvector), b <> (zeros (size b)) -> bv_ugt b (zeros (size b)) = true. Proof. intros. unfold bv_ugt. rewrite zeros_size. rewrite eqb_refl. unfold ugt_list. unfold size, zeros in *. rewrite Nat2N.id in *. apply bv_not_0_ugt_list_big_endian_0. apply H. Qed. (* Unsigned less than *) (* ult-thrms *) (* Transitivity : x < y => y < z => x < z *) Lemma ult_list_big_endian_trans : forall x y z, ult_list_big_endian x y = true -> ult_list_big_endian y z = true -> ult_list_big_endian x z = true. Proof. intros x. induction x. + simpl. easy. + intros y z. case y. - simpl. case x; easy. - intros b l. intros. simpl in *. case x in *. * case z in *. { case l in *; easy. } { case l in *. + rewrite andb_true_iff in H. destruct H. apply negb_true_iff in H. subst. simpl. case z in *. * easy. * rewrite !orb_true_iff, !andb_true_iff in H0. destruct H0. - destruct H. apply Bool.eqb_prop in H. subst. rewrite orb_true_iff. now right. - destruct H. easy. + rewrite !orb_true_iff, !andb_true_iff in H, H0. destruct H. * simpl in H. easy. * destruct H. apply negb_true_iff in H. subst. simpl. destruct H0. - destruct H. apply Bool.eqb_prop in H. subst. case z; easy. - destruct H. easy. } * case l in *. { rewrite !orb_true_iff, !andb_true_iff in H. simpl in H. destruct H. + destruct H. case x in H1; easy. + destruct H. apply negb_true_iff in H. subst. simpl in H0. case z in *. * easy. * case b in H0. - simpl in H0. case z in *; easy. - simpl in H0. case z in *; easy. } { case z in *. + easy. + rewrite !orb_true_iff, !andb_true_iff in *. destruct H. * destruct H. destruct H0. - destruct H0. apply Bool.eqb_prop in H. apply Bool.eqb_prop in H0. subst. left. split. { apply Bool.eqb_reflx. } { now apply (IHx (b1 :: l) z H1 H2). } - right. apply Bool.eqb_prop in H. now subst. * right. destruct H0. { destruct H0. apply Bool.eqb_prop in H0. now subst. } { split; easy. } } Qed. (* bool output *) Lemma ult_list_trans : forall x y z, ult_list x y = true -> ult_list y z = true -> ult_list x z = true. Proof. unfold ult_list. intros x y z. apply ult_list_big_endian_trans. Qed. Lemma bv_ult_trans : forall (b1 b2 b3 : bitvector), bv_ult b1 b2 = true -> bv_ult b2 b3 = true -> bv_ult b1 b3 = true. Proof. intros. unfold bv_ult in *. case_eq (size b1 =? size b2). + intros. pose proof H as bv_ult_b1_b2. rewrite H1 in H. case_eq (size b2 =? size b3). - intros. pose proof H0 as bv_ult_b2_b3. rewrite H2 in H0. case_eq (size b1 =? size b3). * intros. pose proof ult_list_trans as ult_list_trans. specialize (@ult_list_trans b1 b2 b3 H H0). apply ult_list_trans. * intros. apply Neqb_ok in H1. apply Neqb_ok in H2. rewrite <- H1 in H2. rewrite H2 in H3. pose proof eqb_refl as eqb_refl. specialize (@eqb_refl (size b3)). rewrite H3 in eqb_refl. now contradict eqb_refl. - intros. rewrite H2 in H0. now contradict H0. + intros. rewrite H1 in H. now contradict H. Qed. (* Prop output *) Lemma ult_listP_trans : forall (b1 b2 b3 : bitvector), ult_listP b1 b2 -> ult_listP b2 b3 -> ult_listP b1 b3. Proof. unfold ult_listP. unfold ult_list. intros. case_eq (ult_list_big_endian (rev b1) (rev b3)). + intros. easy. + intros. case_eq (ult_list_big_endian (rev b1) (rev b2)). - intros. case_eq (ult_list_big_endian (rev b2) (rev b3)). * intros. pose proof ult_list_big_endian_trans. specialize (@H4 (rev b1) (rev b2) (rev b3) H2 H3). rewrite H4 in H1. now contradict H1. * intros. rewrite H3 in H0. apply H0. - intros. rewrite H2 in H. apply H. Qed. Lemma bv_ultP_trans : forall (b1 b2 b3 : bitvector), bv_ultP b1 b2 -> bv_ultP b2 b3 -> bv_ultP b1 b3. Proof. intros. unfold bv_ultP in *. case_eq (size b1 =? size b2). + intros. pose proof H as bv_ultP_b1_b2. rewrite H1 in bv_ultP_b1_b2. case_eq (size b2 =? size b3). - intros. pose proof H0 as bv_ultP_b2_b3. rewrite H2 in bv_ultP_b2_b3. case_eq (size b1 =? size b3). * intros. pose proof ult_listP_trans as ult_listP_trans. specialize (@ult_listP_trans b1 b2 b3 bv_ultP_b1_b2 bv_ultP_b2_b3). apply ult_listP_trans. * intros. apply Neqb_ok in H1. apply Neqb_ok in H2. rewrite <- H1 in H2. rewrite H2 in H3. pose proof eqb_refl as eqb_refl. specialize (@eqb_refl (size b3)). rewrite H3 in eqb_refl. now contradict eqb_refl. - intros. rewrite H2 in H0. now contradict H0. + intros. rewrite H1 in H. now contradict H. Qed. (* forall x y, x < y => x != y *) Lemma ult_list_big_endian_not_eq : forall x y, ult_list_big_endian x y = true -> x <> y. Proof. intros x. induction x. + simpl. easy. + intros y. case y. - easy. - intros b l. simpl. specialize (IHx l). case x in *. * simpl. case l in *. { case a; case b; simpl; easy. } { easy. } * rewrite !orb_true_iff, !andb_true_iff. intro. destruct H. { destruct H. apply IHx in H0. apply Bool.eqb_prop in H. rewrite H in *. unfold not in *; intro. inversion H1; subst. now apply H0. } { destruct H. apply negb_true_iff in H. subst. easy. } Qed. (* Boolean comparison *) Lemma ult_list_not_eq : forall x y, ult_list x y = true -> x <> y. Proof. unfold ult_list. unfold not. intros. apply ult_list_big_endian_not_eq in H. subst. auto. Qed. Lemma bv_ult_not_eq : forall x y, bv_ult x y = true -> x <> y. Proof. intros x y. unfold bv_ult. case_eq (size x =? size y); intros. - now apply ult_list_not_eq in H0. - now contradict H0. Qed. (* Prop comparison *) Lemma ult_list_not_eqP : forall x y, ult_listP x y -> x <> y. Proof. unfold ult_listP. unfold not. intros. unfold ult_list in H. case_eq (ult_list_big_endian (List.rev x) (List.rev y)). + intros. apply ult_list_big_endian_not_eq in H1. subst. now contradict H1. + intros. now rewrite H1 in H. Qed. Lemma bv_ult_not_eqP : forall x y, bv_ultP x y -> x <> y. Proof. intros x y. unfold bv_ultP. case_eq (size x =? size y); intros. - now apply ult_list_not_eqP in H0. - now contradict H0. Qed. (* Equivalence of boolean and Prop comparisons *) Lemma bv_ult_B2P: forall x y, bv_ult x y = true <-> bv_ultP x y. Proof. intros. split; intros; unfold bv_ult, bv_ultP in *. + case_eq (size x =? size y). - intros. rewrite H0 in H. unfold ult_listP. now rewrite H. - intros. rewrite H0 in H. now contradict H. + unfold ult_listP in *. case_eq (size x =? size y); intros. - rewrite H0 in H. case_eq (ult_list x y); intros. * easy. * rewrite H1 in H. now contradict H. - rewrite H0 in H. now contradict H. Qed. (* a <u b -> ~(a >u b) *) Lemma ult_list_big_endian_not_ugt_list_big_endian : forall x y, ult_list_big_endian x y = true -> ugt_list_big_endian x y = false. Proof. intros x. induction x. + simpl. easy. + intros y. case y. - intros. case a; case x; easy. - intros b l. simpl. specialize (IHx l). case x in *. * simpl. case l in *. { case a; case b; simpl; easy. } { case a; case b; simpl; easy. } * rewrite !orb_true_iff, !andb_true_iff. intro. destruct H. { destruct H. apply IHx in H0. apply Bool.eqb_prop in H. rewrite H. rewrite H0. case b; easy. } { destruct H. apply negb_true_iff in H. subst. easy. } Qed. Lemma ult_list_not_ugt_list : forall x y, ult_list x y = true -> ugt_list x y = false. Proof. intros x y. unfold ult_list. intros. apply ult_list_big_endian_not_ugt_list_big_endian in H. unfold ugt_list. apply H. Qed. Lemma bv_ult_not_bv_ugt : forall x y, bv_ult x y = true -> bv_ugt x y = false. Proof. intros x y. unfold bv_ult. case_eq (size x =? size y); intros. - apply ult_list_not_ugt_list in H0. unfold bv_ugt. rewrite H. apply H0. - now contradict H0. Qed. Lemma ult_listP_not_ugt_listP : forall x y, ult_listP x y -> ~ (ugt_listP x y). Proof. unfold ult_listP. unfold not. intros. unfold ult_list in H. case_eq (ult_list_big_endian (List.rev x) (List.rev y)). + intros. apply ult_list_big_endian_not_ugt_list_big_endian in H1. unfold ugt_listP in H0. unfold ugt_list in H0. rewrite H1 in H0. now contradict H0. + intros. now rewrite H1 in H. Qed. Lemma bv_ultP_not_bv_ugtP : forall x y, bv_ultP x y -> ~ (bv_ugtP x y). Proof. intros x y. unfold bv_ultP. unfold not. case_eq (size x =? size y); intros. - unfold ult_listP in H0. unfold bv_ugtP in H1. rewrite H in H1. unfold ugt_listP in H1. now apply ugt_listP_not_ult_listP in H0. - now contradict H0. Qed. (* a <u b -> b >u a *) Lemma ult_list_big_endian_ugt_list_big_endian : forall x y, ult_list_big_endian x y = true -> ugt_list_big_endian y x = true. Proof. intros x. induction x. + simpl. easy. + intros y. case y. - intros. case a; case x in *; simpl in H; now contradict H. - intros b l. simpl. specialize (IHx l). case x in *. * simpl. case l in *. { case a; case b; simpl; easy. } { case a; case b; simpl; easy. } * rewrite !orb_true_iff, !andb_true_iff. intro. destruct H. { destruct H. apply IHx in H0. apply Bool.eqb_prop in H. rewrite H. rewrite H0. case b; case l; easy. } { destruct H. apply negb_true_iff in H. subst. case l; easy. } Qed. Lemma ult_list_ugt_list : forall x y, ult_list x y = true -> ugt_list y x = true. Proof. intros x y. unfold ult_list. intros. apply ult_list_big_endian_ugt_list_big_endian in H. unfold ugt_list. apply H. Qed. Lemma bv_ult_bv_ugt : forall x y, bv_ult x y = true -> bv_ugt y x = true. Proof. intros x y. unfold bv_ult. case_eq (size x =? size y); intros. - apply ult_list_ugt_list in H0. unfold bv_ugt. rewrite (@eqb_sym (size x) (size y)) in H. rewrite H. apply H0. - now contradict H0. Qed. Lemma ult_listP_ugt_listP : forall x y, ult_listP x y -> ugt_listP y x. Proof. unfold ult_listP. intros. unfold ult_list in H. case_eq (ult_list_big_endian (List.rev x) (List.rev y)). + intros. unfold ugt_listP. unfold ugt_list. apply (@ult_list_big_endian_ugt_list_big_endian (List.rev x) (List.rev y)) in H0. rewrite H0. easy. + intros. rewrite H0 in H. now contradict H. Qed. Lemma bv_ultP_bv_ugtP : forall x y, bv_ultP x y -> (bv_ugtP y x). Proof. intros x y. unfold bv_ultP, bv_ugtP. rewrite (@eqb_sym (size y) (size x)). case_eq (size x =? size y); intros. - apply ult_listP_ugt_listP. apply H0. - apply H0. Qed. Lemma nlt_be_neq_gt: forall x y, length x = length y -> ult_list_big_endian x y = false -> beq_list x y = false -> ult_list_big_endian y x = true. Proof. intro x. induction x as [ | x xs IHxs ]. - intros. simpl in *. case y in *; now contradict H. - intros. simpl in H1. case_eq y; intros. rewrite H2 in H. now contradict H. simpl. case_eq l. intros. case_eq xs. intros. rewrite H2 in H1. rewrite H4 in H0, H. simpl in H0, H. rewrite H2, H3 in H0, H. rewrite H4, H3 in H1. simpl in H1. rewrite andb_true_r in H1. case b in *; case x in *; easy. intros. rewrite H4, H2, H3 in H. now contradict H. intros. rewrite H2, H3 in H0, H1. simpl in H0. case_eq xs. intros. rewrite H4, H2, H3 in H. now contradict H. intros. rewrite H4 in H0. rewrite <- H3, <- H4. rewrite <- H3, <- H4 in H0. rewrite <- H3 in H1. rewrite orb_false_iff in H0. destruct H0. case_eq (Bool.eqb x b); intros. rewrite H6 in H0, H1. rewrite andb_true_l in H0, H1. assert (Bool.eqb b x = true). { case b in *; case x in *; easy. } rewrite H7. rewrite andb_true_l. rewrite orb_true_iff. left. apply IHxs. rewrite H2 in H. now inversion H. easy. easy. assert (Bool.eqb b x = false). { case b in *; case x in *; easy. } rewrite H7. rewrite orb_false_l. case x in *. case b in *. now contradict H6. now easy. case b in *. now contradict H5. now contradict H6. Qed. Lemma nlt_neq_gt: forall x y, length x = length y -> ult_list x y = false -> beq_list x y = false -> ult_list y x = true. Proof. intros. unfold ult_list in *. apply nlt_be_neq_gt. now rewrite !rev_length. easy. now apply rev_neq in H1. Qed. Lemma ult_list_be_cons_false: forall a b c, length a = length b -> ult_list_big_endian (c :: a) (c :: b) = false -> ult_list_big_endian a b = false. Proof. intro a. induction a; intros. - now cbn. - case_eq b; intros. subst. now contradict H. subst. cbn. case_eq a0; intros. subst. assert (l = nil) . admit. (* { Reconstr.reasy Reconstr.Empty (@Coq.Init.Datatypes.length). } *) subst. cbn in *. admit. (* Reconstr.rsimple Reconstr.Empty (@Coq.Init.Datatypes.negb, @Coq.Init.Datatypes.orb, @Coq.Init.Datatypes.andb, @Coq.Bool.Bool.eqb). *) rewrite <- H1 in *. cbn in H0. case_eq a0; intros. subst. now contradict H2. subst. rewrite <- H2. assert ( eqb c c = true) by admit. (* Reconstr.reasy Reconstr.Empty (@Coq.Bool.Bool.eqb). *) rewrite H1 in H0. assert (negb c && c = false) by admit. (* Reconstr.reasy Reconstr.Empty (@Coq.Init.Datatypes.negb, @Coq.Init.Datatypes.andb). *) rewrite H3 in H0. admit. (* Reconstr.rcrush (@Coq.Bool.Bool.negb_true_iff, @Coq.Bool.Bool.orb_false_iff, @Coq.Bool.Bool.andb_true_l) (@Coq.Init.Datatypes.negb, @Coq.Init.Datatypes.orb, @Coq.Init.Datatypes.andb, @Coq.Bool.Bool.eqb). *) Admitted. Lemma ult_list_big_endian_unf: forall a b c d, ult_list_big_endian (c :: a) (d :: b) = orb (andb (Bool.eqb c d) (ult_list_big_endian a b)) (andb (negb c) d). Proof. intro a. case_eq a; intros. + cbn in *. case_eq b; intros. admit. (* Reconstr.reasy (@Coq.Bool.Bool.andb_false_r) (@Coq.Init.Datatypes.andb, @Coq.Bool.Bool.eqb, @Coq.Init.Datatypes.orb). *) admit. (* Reconstr.reasy Reconstr.Empty Reconstr.Empty. *) + cbn in *. case_eq b0; intros; easy. Admitted. Lemma bv_ult_nat: forall a b, ((length a)%nat =? (length b)%nat)%nat = true -> bv_ult a b = (((bv2nat_a a)%nat <? (bv2nat_a b)%nat))%nat. Proof. intros. unfold bv_ult, size in *. rewrite Nat.eqb_eq in H. rewrite H, N.eqb_refl. unfold bv2nat_a, list2nat_be_a, ult_list. revert b H. induction a using rev_ind; intros. - cbn in *. Reconstr.rsimple (@Coq.NArith.Nnat.Nat2N.id, @RAWBITVECTOR_LIST.list2N_mk_list_false) (@RAWBITVECTOR_LIST.mk_list_false, @Coq.Init.Datatypes.length, @Coq.NArith.BinNatDef.N.of_nat). - induction b using rev_ind; intros. + cbn in *. contradict H. Reconstr.rcrush Reconstr.Empty (@Coq.Init.Datatypes.length, @Coq.Init.Datatypes.app). + rewrite !rev_app_distr. cbn. case_eq (rev a); intros. ++ assert (rev b = nil). { rewrite !app_length in H. cbn in H. assert (a = nil) by Reconstr.rsimple (@Coq.Lists.List.app_nil_r, @Coq.Lists.List.rev_app_distr, @Coq.Lists.List.rev_involutive) Reconstr.Empty. assert (length b = O). subst. cbn in *. lia. Reconstr.rcrush Reconstr.Empty (@Coq.Init.Datatypes.length). } rewrite H1. assert (a = nil). Reconstr.reasy (@Coq.Lists.List.rev_involutive, @Coq.Lists.List.rev_app_distr, @Coq.Lists.List.app_nil_r) Reconstr.Empty. assert (b = nil). Reconstr.reasy (@Coq.Lists.List.rev_involutive, @Coq.Lists.List.rev_app_distr, @Coq.Lists.List.app_nil_r) Reconstr.Empty. rewrite H2, H3. cbn. case_eq x; case_eq x0; intros; cbn; try easy. ++ rewrite <- H0, IHa. case_eq ((N.to_nat (list2N a) <? N.to_nat (list2N b))%nat); intro HH. * case_eq x; case_eq x0; intros. ** cbn. rewrite !list2N_app_true. specialize (ltb_plus (N.to_nat (list2N a)) (N.to_nat (list2N b)) (2 ^ length b)); intros. rewrite H3 in HH. case_eq ((N.to_nat (list2N b) + 2 ^ length b)%nat); intros. contradict H4. Reconstr.reasy (@Coq.PArith.Pnat.Pos2Nat.inj_1, @Coq.Arith.PeanoNat.Nat.pow_nonzero) (@Coq.Init.Nat.add). apply Nat.ltb_lt in HH. rewrite H4 in HH. assert (length a = length b). rewrite !app_length in H. cbn in H. Reconstr.reasy (@Coq.Init.Peano.eq_add_S, @Coq.Arith.PeanoNat.Nat.add_1_r) Reconstr.Empty. rewrite H5. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.leb_le, @Coq.Arith.PeanoNat.Nat.lt_succ_r, @Coq.PArith.Pnat.Pos2Nat.inj_1) Reconstr.Empty. ** cbn. rewrite list2N_app_true, list2N_app_false. case_eq (N.to_nat (list2N b)); intros. easy. rewrite H3 in HH. assert (length a = length b). rewrite !app_length in H. cbn in H. Reconstr.reasy (@Coq.Init.Peano.eq_add_S, @Coq.Arith.PeanoNat.Nat.add_1_r) Reconstr.Empty. rewrite H4. specialize (pow_gt b); intro Hb. rewrite H3 in Hb. rewrite Nat.ltb_lt in Hb. symmetry. rewrite Nat.leb_gt. lia. ** cbn. rewrite list2N_app_true, list2N_app_false. case_eq ((N.to_nat (list2N b) + 2 ^ length b)%nat); intros. contradict H3. Reconstr.reasy (@Coq.PArith.Pnat.Pos2Nat.inj_1, @Coq.Arith.PeanoNat.Nat.pow_nonzero) (@Coq.Init.Nat.add). symmetry. rewrite Nat.leb_le. rewrite Nat.ltb_lt in HH. specialize (pow_gt a); intro Ha. assert (S (N.to_nat (list2N a)) <= S n)%nat. rewrite <- H3. rewrite Nat.ltb_lt in Ha. assert (length a = length b). rewrite !app_length in H. cbn in H. Reconstr.reasy (@Coq.Init.Peano.eq_add_S, @Coq.Arith.PeanoNat.Nat.add_1_r) Reconstr.Empty. rewrite H4 in Ha. lia. lia. ** cbn. rewrite !list2N_app_false. case_eq (N.to_nat (list2N b)); intros. contradict HH. rewrite H3. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.ltb_lt) (@Coq.Init.Peano.lt). symmetry. rewrite Nat.leb_le. rewrite Nat.ltb_lt in HH. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.lt_succ_r) Reconstr.Empty. * case_eq x; case_eq x0; intros. ** cbn. rewrite !list2N_app_true. case_eq ((N.to_nat (list2N b) + 2 ^ length b)%nat); intros. easy. symmetry. rewrite Nat.leb_gt. assert ((S n < S (N.to_nat (list2N a) + 2 ^ length a))%nat). rewrite <- H3. rewrite Nat.ltb_ge in HH. assert (( (N.to_nat (list2N a)) < S (N.to_nat (list2N a)))%nat). lia. assert (length a = length b). rewrite !app_length in H. cbn in H. Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.add_succ_r, @Coq.Init.Peano.plus_n_O) Reconstr.Empty. rewrite H5. lia. lia. ** cbn. rewrite list2N_app_true, list2N_app_false. case_eq (N.to_nat (list2N b)); intros. easy. rewrite H3 in HH. assert (length a = length b). rewrite !app_length in H. cbn in H. Reconstr.reasy (@Coq.Init.Peano.eq_add_S, @Coq.Arith.PeanoNat.Nat.add_1_r) Reconstr.Empty. rewrite H4. specialize (pow_gt b); intro Hb. rewrite H3 in Hb. rewrite Nat.ltb_lt in Hb. symmetry. rewrite Nat.leb_gt. lia. ** cbn. rewrite list2N_app_true, list2N_app_false. case_eq ((N.to_nat (list2N b) + 2 ^ length b)%nat); intros. contradict H3. specialize (pow_gt b); intro Hb. rewrite Nat.ltb_lt in Hb. Reconstr.rsimple (@Coq.Arith.PeanoNat.Nat.pow_nonzero, @Coq.PArith.Pnat.Pos2Nat.inj_1) (@Coq.Init.Nat.add). symmetry. rewrite Nat.leb_le. rewrite Nat.ltb_ge in HH. specialize (pow_gt a); intro Ha. assert (S (N.to_nat (list2N a)) <= S n)%nat. rewrite <- H3. rewrite Nat.ltb_lt in Ha. assert (length a = length b). rewrite !app_length in H. cbn in H. Reconstr.reasy (@Coq.Init.Peano.eq_add_S, @Coq.Arith.PeanoNat.Nat.add_1_r) Reconstr.Empty. rewrite H4 in Ha. lia. lia. ** cbn. rewrite !list2N_app_false. case_eq (N.to_nat (list2N b)); intros. easy. symmetry. rewrite Nat.leb_gt. rewrite Nat.ltb_ge in HH. Reconstr.reasy Reconstr.Empty (@Coq.Init.Peano.lt). * rewrite !app_length in H. cbn in H. Reconstr.reasy (@Coq.Init.Peano.plus_n_Sm, @Coq.Init.Peano.plus_n_O) Reconstr.Empty. Qed. (* b != 1 -> b < 1 *) Lemma rev_uneq : forall b : bitvector, b <> mk_list_true (length b) -> (rev b) <> (rev (mk_list_true (length b))). Proof. intros. apply rev_neg_inj. rewrite rev_involutive. rewrite rev_involutive. apply H. Qed. Lemma ult_list_big_endian_true : forall (b1 b2 : list bool), ult_list_big_endian b1 b2 = true -> ult_list_big_endian (true :: b1) (true :: b2) = true. Proof. intros. unfold ult_list_big_endian. case b1 in *. + case b2 in *; easy. + case b2 in *. - simpl in H. case b1 in *; easy. - rewrite orb_true_iff, andb_true_iff. left. simpl. split. * easy. * fold ult_list_big_endian. apply H. Qed. Lemma bv_not_1_ult_list_big_endian_1 : forall (b : bitvector), b <> mk_list_true (length b) -> ult_list_big_endian (rev b) (rev (mk_list_true (length b))) = true. Proof. intros. rewrite rev_mk_list_true. assert (rev_uneq : forall b : bitvector, b <> mk_list_true (length b) -> (rev b) <> (rev (mk_list_true (length b)))). { apply rev_uneq. } apply rev_uneq in H. rewrite <- rev_length in *. rewrite rev_mk_list_true in H. destruct (rev b) as [| h t]. (*induction (rev b) as [| a l IHrev].*) + now contradict H. + assert (ult_cons_false : forall (b : list bool), ult_list_big_endian (false :: b) (mk_list_true (length (false :: b))) = true). { intros. induction b0; easy. } destruct h. - induction t as [| h2 t2 IH]. * easy. * assert (ult_cons_true : forall (b : list bool), ult_list_big_endian b (mk_list_true (length b)) = true -> ult_list_big_endian (true :: b) (mk_list_true (length (true :: b))) = true). { pose proof ult_list_big_endian_true as ult_list_big_endian_true. intros. specialize (@ult_list_big_endian_true b0 (mk_list_true (length b0)) H0). pose proof mk_list_true_cons as mk_list_true_cons. specialize (@mk_list_true_cons true b0). rewrite mk_list_true_cons. apply ult_list_big_endian_true. } destruct h2. { assert (forall (b : list bool), true :: b <> mk_list_true (length (true :: b)) -> b <> mk_list_true (length b)). { intros. rewrite mk_list_true_cons in H0. unfold not. intros. unfold not in H0. rewrite <- H1 in H0. now contradict H0. } apply H0 in H. apply IH in H. apply ult_cons_true in H. apply H. } { apply ult_cons_true. apply ult_cons_false. } - apply ult_cons_false. Qed. Lemma bv_not_eq_1_ult_listP :forall b : bitvector, b <> bv_not (zeros (size b)) -> ult_listP b (bv_not (zeros (size b))). Proof. intros. unfold ult_listP. case_eq (ult_list b (bv_not (zeros (size b)))). + intros. easy. + intros. unfold ult_list in H0. unfold zeros in *. unfold size in *. rewrite Nat2N.id in *. rewrite bv_not_false_true in *. assert (forall (b : bitvector), b <> mk_list_true (length b) -> ult_list_big_endian (rev b) (rev (mk_list_true (length b))) = true). { apply bv_not_1_ult_list_big_endian_1. } specialize (@H1 b). specialize (@H1 H). rewrite H0 in H1. now contradict H1. Qed. Lemma bv_not_eq_1_ultP_1 : forall (b : bitvector), b <> (bv_not (zeros (size b))) -> bv_ultP b (bv_not (zeros (size b))). Proof. intros. unfold bv_ultP. assert (size b = size (bv_not (zeros (size b)))). { rewrite (@bv_not_size (size b) (zeros (size b))). + easy. + rewrite zeros_size. easy. } case_eq (size b =? size (bv_not (zeros (size b)))). + intros. assert (forall b : bitvector, b <> bv_not (zeros (size b)) -> ult_listP b (bv_not (zeros (size b)))). { apply bv_not_eq_1_ult_listP. } apply H2. apply H. + intros. rewrite <- H0 in H1. rewrite eqb_neq in H1. now contradict H1. Qed. (* forall x, ~(x < 0) *) Lemma not_ult_list_big_endian_x_0 : forall (x : list bool), ult_list_big_endian x (mk_list_false (length x)) = false. Proof. intros x. induction x. + easy. + case a. - assert (simpl : mk_list_false (length (true :: x)) = false :: mk_list_false (length x)). { easy. } rewrite simpl. simpl. case x; easy. - simpl. rewrite IHx. case x; easy. Qed. Lemma not_ult_list_x_zero : forall (x : bitvector), ult_list x (mk_list_false (length x)) = false. Proof. intros x. unfold ult_list. rewrite rev_mk_list_false. rewrite <- rev_length. apply not_ult_list_big_endian_x_0. Qed. Lemma not_bv_ultP_x_zero : forall (x : bitvector), ~(bv_ultP x (zeros (size x))). Proof. intros x. unfold not. intros contr_x_0. unfold bv_ultP in contr_x_0. rewrite zeros_size in contr_x_0. rewrite eqb_refl in contr_x_0. unfold ult_listP in contr_x_0. unfold zeros in contr_x_0. unfold size in contr_x_0. rewrite Nat2N.id in contr_x_0. now rewrite not_ult_list_x_zero in contr_x_0. Qed. Lemma not_bv_ult_x_zero : forall (x : bitvector), bv_ult x (zeros (size x)) = false. Proof. intros x. unfold bv_ult. rewrite zeros_size. rewrite eqb_refl. unfold zeros. unfold size. rewrite Nat2N.id. apply not_ult_list_x_zero. Qed. (* ~(a < a) *) Lemma ult_list_be_nrefl: forall a, ult_list_big_endian a a = false. Proof. intro a. induction a; intros. - easy. - cbn. case_eq a0; intros. + Reconstr.reasy (@Coq.Bool.Bool.andb_negb_r) Reconstr.Empty. + rewrite <- H. rewrite IHa. Reconstr.rsimple (@Coq.Bool.Bool.andb_false_r, @Coq.Bool.Bool.negb_true_iff, @Coq.Bool.Bool.andb_false_l, @Coq.Bool.Bool.eqb_reflx) (@Coq.Init.Datatypes.orb, @Coq.Init.Datatypes.negb). Qed. (* Unsigned less than or equal to *) (* ule-thrms *) (* x < y => y <= z => x < z *) (* x < y => x <= y *) Lemma ult_list_big_endian_implies_ule : forall x y, ult_list_big_endian x y = true -> ule_list_big_endian x y = true. Proof. intros x. induction x as [| h t]. + simpl. easy. + intros y. case y. - easy. - intros b l. simpl. case t in *. * simpl. case l in *. { case h; case b; simpl; easy. } { easy. } * rewrite !orb_true_iff, !andb_true_iff. intro. destruct H. { destruct H. specialize (@IHt l H0). rewrite IHt. apply Bool.eqb_prop in H. rewrite H in *. left. split. + apply eqb_reflx. + easy. } { destruct H. rewrite negb_true_iff in *. subst. right. easy. } Qed. Lemma ult_ule_list_big_endian_trans : forall x y z, ult_list_big_endian x y = true -> ule_list_big_endian y z = true -> ult_list_big_endian x z = true. Proof. intros x. induction x. + simpl. easy. + intros y z. case y. - simpl. case x; easy. - intros b l. intros. simpl in *. case x in *. * case z in *. { case l in *; easy. } { case l in *. + rewrite andb_true_iff in H. destruct H. apply negb_true_iff in H. subst. simpl. case z in *. * rewrite orb_true_iff in H0. destruct H0. - apply eqb_prop in H. symmetry. apply H. - rewrite andb_true_iff in H. destruct H. simpl in H. now contradict H. * rewrite !orb_true_iff, !andb_true_iff in H0. destruct H0. - destruct H. apply Bool.eqb_prop in H. subst. rewrite orb_true_iff. now right. - destruct H. easy. + rewrite !orb_true_iff, !andb_true_iff in H, H0. destruct H. * simpl in H. easy. * destruct H. apply negb_true_iff in H. subst. simpl. destruct H0. - destruct H. apply Bool.eqb_prop in H. subst. case z; easy. - destruct H. easy. } * case l in *. { rewrite !orb_true_iff, !andb_true_iff in H. simpl in H. destruct H. + destruct H. case x in H1; easy. + destruct H. apply negb_true_iff in H. subst. simpl in H0. case z in *. * easy. * case b in *. - simpl in H0. case z in *; easy. - simpl in H0. case z in *; easy. } { case z in *; try easy. rewrite !orb_true_iff, !andb_true_iff in *. destruct H. + destruct H. destruct H0. * destruct H0. apply Bool.eqb_prop in H. apply Bool.eqb_prop in H0. subst. left. split. - apply Bool.eqb_reflx. - specialize (@IHx (b1 :: l) z H1 H2). apply IHx. * right. apply Bool.eqb_prop in H. now subst. + right. destruct H0. * destruct H0. apply Bool.eqb_prop in H0. now subst. * split; easy. } Qed. (* bool output *) Lemma ult_ule_list_trans : forall x y z, ult_list x y = true -> ule_list y z = true -> ult_list x z = true. Proof. unfold ult_list, ule_list. intros x y z. apply ult_ule_list_big_endian_trans. Qed. Lemma bv_ult_ule_list_trans : forall (b1 b2 b3 : bitvector), bv_ult b1 b2 = true -> bv_ule b2 b3 = true -> bv_ult b1 b3 = true. Proof. intros. unfold bv_ult, bv_ule in *. case_eq (size b1 =? size b2). + intros. pose proof H as bv_ult_b1_b2. rewrite H1 in bv_ult_b1_b2. case_eq (size b2 =? size b3). - intros. pose proof H0 as bv_ule_b2_b3. rewrite H2 in bv_ule_b2_b3. case_eq (size b1 =? size b3). * intros. pose proof ult_ule_list_trans as ult_ule_list_trans. specialize (@ult_ule_list_trans b1 b2 b3 bv_ult_b1_b2 bv_ule_b2_b3). apply ult_ule_list_trans. * intros. apply Neqb_ok in H1. apply Neqb_ok in H2. rewrite <- H1 in H2. rewrite H2 in H3. pose proof eqb_refl as eqb_refl. specialize (@eqb_refl (size b3)). rewrite H3 in eqb_refl. now contradict eqb_refl. - intros. rewrite H2 in H0. now contradict H0. + intros. rewrite H1 in H. now contradict H. Qed. (* Prop output *) Lemma ult_ule_listP_trans : forall (b1 b2 b3 : bitvector), ult_listP b1 b2 -> ule_listP b2 b3 -> ult_listP b1 b3. Proof. unfold ult_listP. unfold ult_list. intros. case_eq (ult_list_big_endian (rev b1) (rev b3)). + intros. easy. + intros. case_eq (ult_list_big_endian (rev b1) (rev b2)). - intros. case_eq (ule_list_big_endian (rev b2) (rev b3)). * intros. pose proof ult_ule_list_big_endian_trans. specialize (@H4 (rev b1) (rev b2) (rev b3) H2 H3). rewrite H4 in H1. now contradict H1. * intros. unfold ule_listP in H0. unfold ule_list in H0. rewrite H3 in H0. apply H0. - intros. rewrite H2 in H. apply H. Qed. Lemma bv_ult_uleP_trans : forall (b1 b2 b3 : bitvector), bv_ultP b1 b2 -> bv_uleP b2 b3 -> bv_ultP b1 b3. Proof. intros. unfold bv_ultP, bv_uleP in *. case_eq (size b1 =? size b2). + intros. pose proof H as bv_ultP_b1_b2. rewrite H1 in bv_ultP_b1_b2. case_eq (size b2 =? size b3). - intros. pose proof H0 as bv_uleP_b2_b3. rewrite H2 in bv_uleP_b2_b3. case_eq (size b1 =? size b3). * intros. pose proof ult_ule_listP_trans as ult_ule_listP_trans. specialize (@ult_ule_listP_trans b1 b2 b3 bv_ultP_b1_b2 bv_uleP_b2_b3). apply ult_ule_listP_trans. * intros. apply Neqb_ok in H1. apply Neqb_ok in H2. rewrite <- H1 in H2. rewrite H2 in H3. pose proof eqb_refl as eqb_refl. specialize (@eqb_refl (size b3)). rewrite H3 in eqb_refl. now contradict eqb_refl. - intros. rewrite H2 in H0. now contradict H0. + intros. rewrite H1 in H. now contradict H. Qed. (* Equivalence of boolean and Prop comparisons *) Lemma bv_ule_B2P: forall x y, bv_ule x y = true <-> bv_uleP x y. Proof. intros. split; intros; unfold bv_ule, bv_uleP in *. + case_eq (size x =? size y); intros. - rewrite H0 in H. unfold ule_listP. now rewrite H. - rewrite H0 in H. now contradict H. + unfold ule_listP in *. case_eq (size x =? size y); intros. - rewrite H0 in H. case_eq (ule_list x y); intros. * easy. * rewrite H1 in H. now contradict H. - rewrite H0 in H. now contradict H. Qed. (* forall b : BV, b <= 1 *) Lemma ule_list_big_endian_true : forall (b1 b2 : list bool), ule_list_big_endian b1 b2 = true -> ule_list_big_endian (true :: b1) (true :: b2) = true. Proof. intros. unfold ule_list_big_endian. case b1 in *. + case b2 in *; easy. + case b2 in *. - simpl in H. case b1 in *; easy. - rewrite orb_true_iff, andb_true_iff. left. simpl. split. * easy. * fold ult_list_big_endian. apply H. Qed. Lemma ule_list_big_endian_1 : forall (l : list bool), ule_list_big_endian l (mk_list_true (length l)) = true. Proof. intros. induction l. + easy. + case_eq a. - intros. assert (forall (n : nat), mk_list_true (S n) = true :: mk_list_true n). { intros. induction n; easy. } assert (forall (b : bool) (l : list bool), length (b :: l) = S (length l)). { intros. induction l0; easy. } rewrite H1. rewrite H0. apply (@ule_list_big_endian_true l (mk_list_true (length l)) IHl). - intros. unfold ule_list_big_endian. case_eq l; easy. Qed. Lemma ule_list_1 : forall (b : list bool), ule_list b (mk_list_true (N.to_nat (size b))) = true. Proof. intros. unfold size. rewrite Nat2N.id. unfold ule_list. rewrite rev_mk_list_true. rewrite <- rev_length. apply (@ule_list_big_endian_1 (rev b)). Qed. Lemma bv_ule_1_size : forall (x : bitvector), bv_ule x (mk_list_true (N.to_nat (size x))) = true. Proof. intros. induction x. + easy. + unfold bv_ule. case_eq (size (a :: x) =? size (mk_list_true (N.to_nat (size (a :: x))))). - intros. rewrite ule_list_1. easy. - intros. unfold size in H. rewrite Nat2N.id in H. rewrite length_mk_list_true in H. now rewrite N.eqb_refl in H. Qed. Lemma bv_ule_1_length : forall (x : bitvector), bv_ule x (mk_list_true (length x)) = true. Proof. intros. pose proof (@bv_ule_1_size x). unfold size in H. rewrite Nat2N.id in H. apply H. Qed. Lemma bv_uleP_1_size : forall (x : bitvector), bv_uleP x (mk_list_true (N.to_nat (size x))). Proof. intros. induction x. + easy. + unfold bv_uleP. case_eq (size (a :: x) =? size (mk_list_true (N.to_nat (size (a :: x))))). - intros. unfold ule_listP. rewrite ule_list_1. easy. - intros. unfold size in H. rewrite Nat2N.id in H. rewrite length_mk_list_true in H. pose proof (@eqb_refl (N.of_nat (length (a :: x)))) as eqb_refl. rewrite eqb_refl in H. now contradict H. Qed. Lemma bv_uleP_1_length : forall (x : bitvector), bv_uleP x (mk_list_true (length x)). Proof. intros. pose proof (@bv_uleP_1_size x). unfold size in H. rewrite Nat2N.id in H. apply H. Qed. (* x <= x *) Lemma ule_list_big_endian_refl : forall (b : list bool), ule_list_big_endian b b = true. Proof. induction b. + easy. + case a; simpl; rewrite IHb; case b; easy. Qed. Lemma bv_uleP_refl : forall (b : bitvector), bv_uleP b b. Proof. intros. unfold bv_uleP. pose proof (@eqb_refl (size b)). rewrite H. unfold ule_listP. unfold ule_list. induction (rev b). + easy. + rewrite (@ule_list_big_endian_refl (a :: l)). easy. Qed. Lemma bv_ule_refl : forall (b : bitvector), bv_ule b b = true. Proof. intros. unfold bv_ule. pose proof (@eqb_refl (size b)). rewrite H. unfold ule_list. induction (rev b). + easy. + now rewrite (@ule_list_big_endian_refl (a :: l)). Qed. (* firstn n x <= firstn n 1 *) Lemma eqb_N : forall (a b : N), a = b -> a =? b = true. Proof. intros. induction a; rewrite H; apply eqb_refl. Qed. Lemma length_eq_firstn_eq : forall (n : nat) (x y : bitvector), length x = length y -> (n < length x)%nat -> length (firstn n x) = length (firstn n y). Proof. intros n x y len_xy lt_n_lenx. induction n. + easy. + destruct x. - simpl. destruct y. * easy. * now contradict len_xy. - destruct y. * now contradict len_xy. * pose proof lt_n_lenx as lt_n_leny. rewrite len_xy in lt_n_leny. apply Nat.lt_le_incl in lt_n_lenx. apply Nat.lt_le_incl in lt_n_leny. rewrite (@firstn_length_le bool (b :: x) (S n) lt_n_lenx). rewrite (@firstn_length_le bool (b0 :: y) (S n) lt_n_leny). easy. Qed. Lemma prefix_mk_list_true : forall x y : nat, (x < y)%nat -> firstn x (mk_list_true y) = mk_list_true x. Proof. intros x y ltxy. induction x. + easy. + induction y. - easy. - simpl. pose proof ltxy as ltxy2. apply lt_S_n in ltxy2. pose proof ltxy2 as ltxSy. apply Nat.lt_lt_succ_r in ltxSy. apply IHx in ltxSy. rewrite <- ltxSy. rewrite mk_list_true_app. rewrite firstn_app. rewrite length_mk_list_true. assert (forall (n m : nat), (n < m)%nat -> Nat.sub n m = O). { induction n. + easy. + induction m. - intros. now contradict H. - intros. simpl. apply lt_S_n in H. specialize (@IHn m H). apply IHn. } specialize (@H x y ltxy2). rewrite H. simpl. rewrite app_nil_r. easy. Qed. Lemma bv_ule_1_firstn : forall (n : nat) (x : bitvector), (n < length x)%nat -> bv_ule (firstn n x) (firstn n (mk_list_true (length x))) = true. Proof. intros. unfold bv_ule. case_eq (size (firstn n x) =? size (firstn n (mk_list_true (length x)))). + intros. pose proof bv_ule_1_length as bv_leq_1. unfold ule_list. case_eq (ule_list_big_endian (rev (firstn n x)) (rev (firstn n (mk_list_true (length x))))). - intros. easy. - intros. assert ((n < length x)%nat -> firstn n (mk_list_true (length x)) = mk_list_true n). { apply prefix_mk_list_true. } assert (ule_list_big_endian (rev (firstn n x)) (rev (firstn n (mk_list_true (length x)))) = true). { specialize (@H2 H). rewrite H2. rewrite rev_mk_list_true. specialize (@bv_leq_1 (rev (firstn n x))). assert (n = length (rev (firstn n x))). { rewrite rev_length. pose proof firstn_length_le as firstn_length_le. specialize (@firstn_length_le bool x n). apply Nat.lt_le_incl in H. specialize (@firstn_length_le H). rewrite firstn_length_le; easy. } rewrite H3 at 2. apply ule_list_big_endian_1. } rewrite H3 in H1. now contradict H1. + intros size_x_mlt. assert (length_x_mlt : length x = length (mk_list_true (length x))). { rewrite length_mk_list_true. easy. } pose proof length_eq_firstn_eq as length_eq_firstn_eq. unfold size in size_x_mlt. pose proof eqb_refl as eqb_refl. specialize (@N.eqb_refl (N.of_nat (length (firstn n (mk_list_true (length x)))))). specialize (@length_eq_firstn_eq n x (mk_list_true (length x)) length_x_mlt H). rewrite length_eq_firstn_eq in size_x_mlt. rewrite N.eqb_refl in size_x_mlt. now contradict size_x_mlt. Qed. Lemma bv_uleP_1_firstn : forall (n : nat) (x : bitvector), (n < length x)%nat -> bv_uleP (firstn n x) (firstn n (mk_list_true (length x))). Proof. intros. unfold bv_uleP. case_eq (size (firstn n x) =? size (firstn n (mk_list_true (length x)))). + intros. pose proof bv_uleP_1_length as bv_leq_1. unfold ule_listP. unfold ule_list. case_eq (ule_list_big_endian (rev (firstn n x)) (rev (firstn n (mk_list_true (length x))))). - intros. easy. - intros. assert ((n < length x)%nat -> firstn n (mk_list_true (length x)) = mk_list_true n). { apply prefix_mk_list_true. } assert (ule_list_big_endian (rev (firstn n x)) (rev (firstn n (mk_list_true (length x)))) = true). { specialize (@H2 H). rewrite H2. rewrite rev_mk_list_true. specialize (@bv_leq_1 (rev (firstn n x))). assert (n = length (rev (firstn n x))). { rewrite rev_length. pose proof firstn_length_le as firstn_length_le. specialize (@firstn_length_le bool x n). apply Nat.lt_le_incl in H. specialize (@firstn_length_le H). rewrite firstn_length_le; easy. } rewrite H3 at 2. apply ule_list_big_endian_1. } rewrite H3 in H1. now contradict H1. + intros size_x_mlt. assert (length_x_mlt : length x = length (mk_list_true (length x))). { rewrite length_mk_list_true. easy. } pose proof length_eq_firstn_eq as length_eq_firstn_eq. unfold size in size_x_mlt. pose proof eqb_refl as eqb_refl. specialize (@eqb_refl (N.of_nat (length (firstn n (mk_list_true (length x)))))). specialize (@length_eq_firstn_eq n x (mk_list_true (length x)) length_x_mlt H). rewrite length_eq_firstn_eq in size_x_mlt. rewrite eqb_refl in size_x_mlt. now contradict size_x_mlt. Qed. (* x <= y -> z ++ x <= z ++ y *) Lemma preappend_length : forall (x y z : bitvector), length x = length y -> length (z ++ x) = length (z ++ y). Proof. intros. induction z. + easy. + simpl. pose proof eq_S. specialize (@H0 (length (z ++ x)) (length (z ++ y))). apply H0 in IHz. apply IHz. Qed. Lemma ule_list_big_endian_cons_nil : forall (b : bool) (l : list bool), ule_list_big_endian (b :: l) [] = false. Proof. intros. destruct l; easy. Qed. Lemma cons_disjunct_ule_list_big_endian : forall (h1 h2 : bool) (t1 t2 : list bool), (orb (andb (negb h1) h2) (andb (eqb h1 h2) (ule_list_big_endian t1 t2))) = true -> ule_list_big_endian (h1 :: t1) (h2 :: t2) = true. Proof. intros. rewrite orb_true_iff, andb_true_iff in H. destruct H. + unfold ule_list_big_endian. simpl. fold ule_list_big_endian. destruct t1. - destruct t2; rewrite orb_true_iff; right; rewrite andb_true_iff; apply H. - rewrite orb_true_iff; right; rewrite andb_true_iff; apply H. + unfold ule_list_big_endian. simpl. fold ule_list_big_endian. destruct t1. - rewrite andb_true_iff in H. destruct H. destruct t2. * rewrite orb_true_iff. left. apply H. * now contradict H0. - rewrite orb_true_iff. left. apply H. Qed. Lemma ule_list_big_endian_cons_disjunct : forall (h1 h2 : bool) (t1 t2 : list bool), ule_list_big_endian (h1 :: t1) (h2 :: t2) = true -> (orb (andb (negb h1) h2) (andb (eqb h1 h2) (ule_list_big_endian t1 t2))) = true. Proof. intros. unfold ule_list_big_endian in H. simpl in H. fold ule_list_big_endian in H. destruct t1 in *. + destruct t2 in *. - rewrite orb_true_iff in *. rewrite andb_true_iff in *. destruct H. * right. rewrite andb_true_iff. split. { apply H. } { easy. } * left. apply H. - rewrite orb_true_iff, andb_true_iff in *. destruct H. * destruct H. now contradict H0. * left. rewrite andb_true_iff in H. apply H. + rewrite orb_true_iff, andb_true_iff in *. destruct H. - destruct H. right. rewrite andb_true_iff. split. * apply H. * apply H0. - left. rewrite andb_true_iff in H. apply H. Qed. Lemma ule_list_big_endian_app : forall (x y z : bitvector), ule_list_big_endian x y = true -> ule_list_big_endian (x ++ z) (y ++ z) = true. Proof. induction x, y; intros. + simpl. apply ule_list_big_endian_refl. + now contradict H. + pose proof (@ule_list_big_endian_cons_nil a x). rewrite H0 in H. now contradict H. + pose proof (@ule_list_big_endian_cons_disjunct a b x y H). rewrite orb_true_iff, andb_true_iff in H0. destruct H0. - rewrite <- app_comm_cons. rewrite <- app_comm_cons. apply cons_disjunct_ule_list_big_endian. rewrite orb_true_iff. left. rewrite andb_true_iff. apply H0. - rewrite andb_true_iff in H0. destruct H0. specialize (@IHx y z H1). rewrite <- app_comm_cons. rewrite <- app_comm_cons. apply cons_disjunct_ule_list_big_endian. rewrite orb_true_iff. right. rewrite andb_true_iff. split. * apply H0. * apply IHx. Qed. Lemma ule_list_big_endian_rev_app : forall (x y z : bitvector), ule_list_big_endian (rev x) (rev y) = true -> ule_list_big_endian (rev (z ++ x)) (rev (z ++ y)) = true. Proof. intros x y z ule_rx_ry. rewrite rev_app_distr. rewrite rev_app_distr. apply ule_list_big_endian_app. apply ule_rx_ry. Qed. Lemma bv_ule_pre_append : forall (x y z : bitvector), bv_ule x y = true -> bv_ule (z ++ x) (z ++ y) = true. Proof. intros. unfold bv_ule in *. case_eq (size x =? size y). + intros. rewrite H0 in H. apply Neqb_ok in H0. unfold size in H0. apply Nat2N.inj in H0. pose proof (@preappend_length x y z). apply H1 in H0. rewrite <- Nat2N.id in H0 at 1. rewrite <- Nat2N.id in H0. apply N2Nat.inj in H0. unfold size. apply eqb_N in H0. rewrite H0. unfold ule_list in *. assert (forall (x y z : bitvector), ule_list_big_endian (rev x) (rev y) = true -> ule_list_big_endian (rev (z ++ x)) (rev (z ++ y)) = true). { apply ule_list_big_endian_rev_app. } specialize (@H2 x y z). case_eq (ule_list_big_endian (rev x) (rev y)). - intros. apply H2 in H3. rewrite H3. easy. - intros. rewrite H3 in H. now contradict H. + intros. rewrite H0 in H. now contradict H. Qed. Lemma bv_uleP_pre_append : forall (x y z : bitvector), bv_uleP x y -> bv_uleP (z ++ x) (z ++ y). Proof. intros. unfold bv_uleP in *. case_eq (size x =? size y). + intros. rewrite H0 in H. apply Neqb_ok in H0. unfold size in H0. apply Nat2N.inj in H0. pose proof (@preappend_length x y z). apply H1 in H0. rewrite <- Nat2N.id in H0 at 1. rewrite <- Nat2N.id in H0. apply N2Nat.inj in H0. unfold size. apply eqb_N in H0. rewrite H0. unfold ule_listP in *. unfold ule_list in *. assert (forall (x y z : bitvector), ule_list_big_endian (rev x) (rev y) = true -> ule_list_big_endian (rev (z ++ x)) (rev (z ++ y)) = true). { apply ule_list_big_endian_rev_app. } specialize (@H2 x y z). case_eq (ule_list_big_endian (rev x) (rev y)). - intros. apply H2 in H3. rewrite H3. easy. - intros. rewrite H3 in H. now contradict H. + intros. rewrite H0 in H. now contradict H. Qed. (* x <= y -> x ++ z <= y ++ z *) Lemma postappend_length : forall (x y z : bitvector), length x = length y -> length (x ++ z) = length (y ++ z). Proof. induction x, y. + easy. + intros. now contradict H. + intros. now contradict H. + intros. specialize (@IHx y z). simpl in H. apply eq_add_S in H. specialize (@IHx H). simpl. apply eq_S. apply IHx. Qed. Lemma app_ule_list_big_endian : forall (x y z : bitvector), ule_list_big_endian x y = true -> ule_list_big_endian (z ++ x) (z ++ y) = true. Proof. induction z; intros Hxy. + easy. + specialize (@IHz Hxy). rewrite <- app_comm_cons. rewrite <- app_comm_cons. apply cons_disjunct_ule_list_big_endian. rewrite orb_true_iff. right. rewrite andb_true_iff. split. - apply eqb_reflx. - apply IHz. Qed. Lemma rev_app_ule_list_big_endian : forall (x y z : bitvector), ule_list_big_endian (rev x) (rev y) = true -> ule_list_big_endian (rev (x ++ z)) (rev (y ++ z)) = true. Proof. intros x y z ule_rx_ry. rewrite rev_app_distr. rewrite rev_app_distr. apply app_ule_list_big_endian. apply ule_rx_ry. Qed. Lemma bv_uleP_post_append : forall (x y z : bitvector), bv_uleP x y -> bv_uleP (x ++ z) (y ++ z). Proof. intros x y z Hlexy. unfold bv_uleP in *. case_eq (size x =? size y). + intros Hxy. rewrite Hxy in Hlexy. apply Neqb_ok in Hxy. unfold size in Hxy. apply Nat2N.inj in Hxy. pose proof (@postappend_length x y z) as app_len. apply app_len in Hxy. rewrite <- Nat2N.id in Hxy at 1. rewrite <- Nat2N.id in Hxy. apply N2Nat.inj in Hxy. unfold size. apply eqb_N in Hxy. rewrite Hxy. unfold ule_listP in *. unfold ule_list in *. pose proof (@rev_app_ule_list_big_endian x y z) as rev_app. case_eq (ule_list_big_endian (rev x) (rev y)). - intros. apply rev_app in H. rewrite H. easy. - intros. rewrite H in Hlexy. now contradict Hlexy. + intros Hxy. rewrite Hxy in Hlexy. now contradict Hlexy. Qed. Lemma bv_ule_post_append : forall (x y z : bitvector), bv_ule x y = true -> bv_ule (x ++ z) (y ++ z) = true. Proof. intros x y z Hlexy. unfold bv_ule in *. case_eq (size x =? size y). + intros Hxy. rewrite Hxy in Hlexy. apply Neqb_ok in Hxy. unfold size in Hxy. apply Nat2N.inj in Hxy. pose proof (@postappend_length x y z) as app_len. apply app_len in Hxy. rewrite <- Nat2N.id in Hxy at 1. rewrite <- Nat2N.id in Hxy. apply N2Nat.inj in Hxy. unfold size. apply eqb_N in Hxy. rewrite Hxy. unfold ule_listP in *. unfold ule_list in *. pose proof (@rev_app_ule_list_big_endian x y z) as rev_app. case_eq (ule_list_big_endian (rev x) (rev y)). - intros. apply rev_app in H. rewrite H. easy. - intros. rewrite H in Hlexy. now contradict Hlexy. + intros Hxy. rewrite Hxy in Hlexy. now contradict Hlexy. Qed. (* Signed less than *) (* slt-thrms *) (* forall x y, x < y => x != y *) Lemma slt_list_big_endian_not_eq : forall x y, slt_list_big_endian x y = true -> x <> y. Proof. intros x. induction x. + simpl. easy. + intros y. case y. - simpl. case x; easy. - intros b l. simpl. specialize (IHx l). case x in *. * simpl. case l in *. { case a; case b; simpl; easy. } { easy. } * rewrite !orb_true_iff, !andb_true_iff. intro. destruct H. { destruct H. apply ult_list_big_endian_not_eq in H0. apply Bool.eqb_prop in H. rewrite H in *. unfold not in *. intros. apply H0. now inversion H1. } { destruct H. apply negb_true_iff in H0. subst. easy. } Qed. (* Boolean comparison *) Lemma slt_list_not_eq : forall x y, slt_list x y = true -> x <> y. Proof. unfold slt_list. unfold not. intros. apply slt_list_big_endian_not_eq in H. subst. auto. Qed. Lemma bv_slt_not_eq : forall x y, bv_slt x y = true -> x <> y. Proof. intros x y. unfold bv_slt. case_eq (size x =? size y); intros. - now apply slt_list_not_eq in H0. - now contradict H0. Qed. (* Prop comparison *) Lemma slt_list_not_eqP : forall x y, slt_listP x y -> x <> y. Proof. unfold slt_listP. unfold not. intros. unfold slt_list in H. case_eq (slt_list_big_endian (List.rev x) (List.rev y)); intros. apply slt_list_big_endian_not_eq in H1. subst. now contradict H1. now rewrite H1 in H. Qed. Lemma bv_slt_not_eqP : forall x y, bv_sltP x y -> x <> y. Proof. intros x y. unfold bv_sltP. case_eq (size x =? size y); intros. - now apply slt_list_not_eqP in H0. - now contradict H0. Qed. (* Equivalence of boolean and Prop comparisons *) Lemma bv_slt_B2P: forall x y, bv_slt x y = true <-> bv_sltP x y. Proof. intros. split; intros; unfold bv_slt, bv_sltP in *. + case_eq (size x =? size y); intros; rewrite H0 in H; unfold slt_listP. - now rewrite H. - now contradict H. + unfold slt_listP in *. case_eq (size x =? size y); intros. - rewrite H0 in H. case_eq (slt_list x y); intros. * easy. * rewrite H1 in H. now contradict H. - rewrite H0 in H. now contradict H. Qed. Lemma slt_list_be_ft: forall a b d, length a = length b -> hd d a = true -> hd d b = false -> slt_list_big_endian a b = true. Proof. intro a. induction a; intros. - subst. cbn in *. assert (b = nil). Reconstr.reasy (@Coq.Lists.List.length_zero_iff_nil) Reconstr.Empty. subst. now cbn in *. - case_eq b; intros. + subst. cbn in *. easy. + subst. cbn. case_eq a0; intros. * subst. cbn in *. case_eq l; intros. ++ subst. easy. ++ subst. easy. * cbn in *. subst. now cbn. Qed. Lemma last_app: forall (a b: list bool) c, b <> nil -> last (a ++ b) c = last b c. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq (a0 ++ b); intros. + contradict H0. Reconstr.rsimple Reconstr.Empty (@Coq.Init.Datatypes.app). + specialize (IHa b c). rewrite H0 in IHa. apply IHa. easy. Qed. Lemma hd_rev: forall a, hd false (rev a) = last a false. Proof. intro a. induction a using rev_ind; intros. - now cbn. - Reconstr.rblast (@Coq.Lists.List.rev_unit, @RAWBITVECTOR_LIST.last_app) (@Coq.Lists.List.hd). Qed. Lemma bv_slt_tf: forall a b, size a = size b -> last a false = true -> last b false = false -> bv_slt a b = true. Proof. intros. unfold bv_slt. rewrite H, N.eqb_refl. apply slt_list_be_ft with (d := false). - Reconstr.reasy (@Coq.NArith.Nnat.Nat2N.id, @Coq.Lists.List.rev_length) (@RAWBITVECTOR_LIST.size, @RAWBITVECTOR_LIST.bitvector). - now rewrite hd_rev. - now rewrite hd_rev. Qed. Lemma bv_slt_false_zeros: forall a, bv_slt a (zeros (size a)) = false -> eqb (last a false) false = true. Proof. intros. unfold bv_slt in H. rewrite zeros_size, N.eqb_refl in H. unfold slt_list in H. induction a using rev_ind; intros. - now cbn. - cbn in *. case_eq x; intros. + subst. assert ((rev (a ++ [true])) = (true :: rev a)). Reconstr.reasy (@Coq.Lists.List.rev_unit) Reconstr.Empty. rewrite H0 in H. assert ((rev (zeros (size (a ++ [true])))) = (zeros (size (a ++ [true])))). Reconstr.reasy (@RAWBITVECTOR_LIST.rev_mk_list_false) (@RAWBITVECTOR_LIST.zeros, @RAWBITVECTOR_LIST.bitvector). rewrite slt_list_be_ft with (d := false) in H. easy. cbn. rewrite !rev_length. unfold zeros. rewrite length_mk_list_false. unfold size. rewrite Nat2N.id. Reconstr.reasy (@RAWBITVECTOR_LIST.mk_list_true_app, @RAWBITVECTOR_LIST.length_mk_list_true, @Coq.Lists.List.app_length) Reconstr.Empty. now cbn. rewrite H1. unfold size, zeros. rewrite Nat2N.id. Reconstr.rsimple (@Coq.NArith.Nnat.Nat2N.id, @Coq.Lists.List.length_zero_iff_nil) (@RAWBITVECTOR_LIST.mk_list_false, @RAWBITVECTOR_LIST.size, @RAWBITVECTOR_LIST.zeros, @Coq.Lists.List.hd). + Reconstr.reasy (@RAWBITVECTOR_LIST.last_app) (@Coq.Bool.Bool.eqb). Qed. Lemma bv_slt_be_nrefl: forall a, slt_list_big_endian a a = false. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq a0; intros. + Reconstr.reasy (@Coq.Bool.Bool.andb_negb_r) Reconstr.Empty. + rewrite ult_list_be_nrefl. Reconstr.reasy (@Coq.Bool.Bool.andb_false_r, @Coq.Bool.Bool.andb_negb_r) (@Coq.Init.Datatypes.is_true, @Coq.Init.Datatypes.andb, @Coq.Bool.Bool.eqb, @Coq.Init.Datatypes.orb). Qed. Lemma bv_slt_nrefl: forall a, bv_slt a a = false. Proof. intro a. unfold bv_slt. rewrite N.eqb_refl. induction a; intros. - easy. - cbn in *. now rewrite bv_slt_be_nrefl. Qed. (*-------------------- Shift Operations --------------------*) (* shift left *) (* Definitions *) (* shl-defs *) Definition shl_one_bit (a: list bool) : list bool := match a with | [] => [] | _ => false :: removelast a end. Fixpoint shl_n_bits (a: list bool) (n: nat): list bool := match n with | O => a | S n' => shl_n_bits (shl_one_bit a) n' end. Definition shl_n_bits_a (a: list bool) (n: nat): list bool := if (n <? length a)%nat then mk_list_false n ++ firstn (length a - n) a else mk_list_false (length a). Definition shl_aux (a b: list bool): list bool := shl_n_bits a (list2nat_be_a b). Definition bv_shl (a b : bitvector) : bitvector := if ((@size a) =? (@size b)) then shl_aux a b else nil. Definition bv_shl_a (a b : bitvector) : bitvector := if ((@size a) =? (@size b)) then shl_n_bits_a a (list2nat_be_a b) else nil. (* Theorems *) (* shl-thrms *) Lemma length_shl_one_bit : forall a, length (shl_one_bit a) = length a. Proof. intro a. induction a; intros. - now simpl. - simpl. rewrite <- IHa. case_eq a0; easy. Qed. Lemma length_shl_n_bits : forall n a, length (shl_n_bits a n) = length a. Proof. intro n. induction n; intros; simpl. - reflexivity. - now rewrite (IHn (shl_one_bit a)), length_shl_one_bit. Qed. Lemma length_shl_aux : forall a b n, n = (length a) -> n = (length b)%nat -> n = (length (shl_aux a b)). Proof. intros. unfold shl_aux. now rewrite length_shl_n_bits. Qed. Lemma bv_shl_size n a b : size a = n -> size b = n -> size (bv_shl a b) = n. Proof. unfold bv_shl. intros H1 H2. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size in *. rewrite <- (@length_shl_aux a b (nat_of_N n)). now rewrite N2Nat.id. now apply (f_equal (N.to_nat)) in H1; rewrite Nat2N.id in H1. now apply (f_equal (N.to_nat)) in H2; rewrite Nat2N.id in H2. Qed. Lemma length_shl_n_bits_a: forall n a, length (shl_n_bits_a a n) = length a. Proof. intro n. induction n; intros; simpl. - unfold shl_n_bits_a. case_eq a; intros. now cbn. cbn. rewrite firstn_all. easy. - unfold shl_n_bits_a. case_eq ( (S n <? length a)%nat); intros. cbn. rewrite app_length, length_mk_list_false. rewrite firstn_length. apply Nat.ltb_lt in H. assert ((Init.Nat.min (length a - S n) (length a))%nat = (length a - S n)%nat). { lia. } rewrite H0. lia. now rewrite length_mk_list_false. Qed. Lemma shl_one: forall a, a <> nil -> shl_one_bit a = false :: removelast a. Proof. intros. unfold shl_one_bit. induction a; intros. - now contradict H. - easy. Qed. Lemma shl_one_bit_app: forall a b, b <> nil -> shl_one_bit (a ++ b) = false :: a ++ removelast b. Proof. intro a. induction a; intros. - cbn. rewrite shl_one; easy. - cbn. case_eq a0; case_eq b; intros. subst. now contradict H. subst. rewrite !app_nil_l. easy. subst. now contradict H. subst. rewrite <- app_comm_cons. f_equal. f_equal. rewrite <- removelast_app. easy. easy. Qed. Lemma removelast_one: forall (a: list bool), removelast (firstn 1 a) = nil. Proof. intro a. induction a; intros; now cbn. Qed. Lemma shl_one_bit_all_false: forall (a: list bool), shl_one_bit (mk_list_false (length a)) = mk_list_false (length a). Proof. intro a. induction a; intros. - now cbn. - (* cbn in *. hammer. Reconstr.reasy Reconstr.Empty Reconstr.Empty. *) Admitted. Lemma firstn_neq_nil: forall n (a: list bool), a <> nil -> (n%nat <> 0%nat) -> firstn n a <> []. Proof. intro n. induction n; intros. - cbn in *. easy. - admit. (* - Reconstr.reasy Reconstr.Empty (@Coq.Lists.List.firstn). *) Admitted. Lemma shl_n_shl_one_comm: forall n a, (shl_n_bits (shl_one_bit a) n) = shl_one_bit (shl_n_bits a n). Proof. intro n. induction n; intros. - now cbn. - cbn. now rewrite IHn. Qed. Theorem bv_shl_aux_eq: forall n a, shl_n_bits a n = shl_n_bits_a a n. Proof. intro n. induction n; intros. - cbn. case_eq a; intros. now cbn. cbn. now rewrite firstn_all. - cbn. rewrite shl_n_shl_one_comm, IHn. unfold shl_n_bits_a. case_eq ((S n <? length a)%nat); intros. assert ((n <? length a)%nat = true). { apply Nat.ltb_lt in H. apply Nat.ltb_lt. lia. } rewrite H0. cbn. case_eq a; intros. subst. cbn in *. now contradict H. rewrite shl_one_bit_app. f_equal. f_equal. set (m := (length (b :: l) - S n)%nat). assert (((length (b :: l) - n)%nat = (S m)%nat)). { unfold m in *. case_eq n; intros. cbn. now rewrite Nat.sub_0_r. cbn. (* Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Arith.PeanoNat.Nat.sub_succ, @Coq.Init.Peano.le_S_n, @Coq.Arith.Minus.minus_Sn_m) (@Coq.Init.Peano.lt, @Coq.Init.Datatypes.length). *) admit. } rewrite H2, removelast_firstn. easy. unfold m. (* Reconstr.rexhaustive1 (@Coq.Arith.PeanoNat.Nat.le_sub_l) (@Coq.Init.Peano.lt, @m). *) admit. cbn. case_eq n; intros. subst; cbn in *. easy. subst. apply Nat.ltb_lt in H. apply Nat.ltb_lt in H0. apply firstn_neq_nil. easy. (* Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.sub_gt, @Coq.Arith.PeanoNat.Nat.sub_succ) (@Coq.Init.Datatypes.length). *) admit. case_eq ((n <? length a)%nat); intros. assert (length a = S n). { apply Nat.ltb_lt in H0. apply Nat.ltb_ge in H. lia. } rewrite H1. rewrite shl_one_bit_app. assert ((S n - n)%nat = 1%nat) by lia. rewrite H2, removelast_one, app_nil_r. easy. apply firstn_neq_nil. admit. (* Reconstr.reasy Reconstr.Empty (@Coq.Init.Datatypes.length). *) lia. now rewrite shl_one_bit_all_false. Admitted. Theorem bv_shl_eq: forall (a b : bitvector), bv_shl a b = bv_shl_a a b. Proof. intros. unfold bv_shl, bv_shl_a. case_eq (size a =? size b); intros; try easy. unfold shl_aux. now rewrite bv_shl_aux_eq. Qed. Lemma last_mk_list_false: forall n, last (mk_list_false n) false = false. Proof. intro n. induction n; intros. - now cbn. - cbn. case_eq ( mk_list_false n ); intros. + easy. + now rewrite <- H. Qed. Lemma last_mk_list_true: forall n, (n <> 0)%nat -> last (mk_list_true n) false = true. Proof. intro n. induction n; intros. - now cbn. - cbn. case_eq ( mk_list_true n ); intros. + easy. + rewrite <- H0. apply IHn. Reconstr.reasy Reconstr.Empty (@RAWBITVECTOR_LIST.mk_list_true). Qed. Lemma last_shl_zeros: forall n, last (shl_n_bits_a (mk_list_false n) n) false = false. Proof. intro n. induction n; intros. + now cbn. + cbn. unfold shl_n_bits_a. cbn. rewrite length_mk_list_false. case_eq n; intros. easy. assert ((S n0 <=? n0)%nat = false). rewrite Nat.leb_gt. lia. rewrite H0. cbn. case_eq (mk_list_false n0); intros. easy. now rewrite <- H1, last_mk_list_false. Qed. Lemma last_append: forall (a b: list bool) c, b <> nil -> last (a ++ b) c = last b c. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq (a0 ++ b); intros. + contradict H0. Reconstr.rsimple Reconstr.Empty (@Coq.Init.Datatypes.app). + specialize (IHa b c). rewrite H0 in IHa. apply IHa. easy. Qed. Lemma last_firstn_skipn: forall a n, (n <? length a)%nat = true -> last (firstn (length a - n) (skipn n a)) false = last a false. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq n; intros. + cbn. rewrite firstn_all. easy. + cbn. rewrite IHa. case_eq a0; intros. ++ cbn in H. subst. now contradict H. ++ easy. ++ subst. Reconstr.reasy (@Coq.Arith.Lt.lt_S_n, @Coq.Arith.PeanoNat.Nat.ltb_lt) (@Coq.Init.Datatypes.length). Qed. Lemma last_skipn_false: forall a n, (n <? length a)%nat = true -> last (shl_n_bits_a (skipn n a ++ mk_list_false n) n) false = last a false. Proof. intros. unfold shl_n_bits_a. assert (length (skipn n a ++ mk_list_false n) = length a). rewrite app_length, length_skipn, length_mk_list_false. rewrite Nat.ltb_lt in H. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.sub_add, @Coq.Arith.PeanoNat.Nat.lt_le_incl) Reconstr.Empty. rewrite H0, H. rewrite last_app. rewrite firstn_app. rewrite length_skipn. assert ((length a - n - (length a - n))%nat = O). Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.sub_diag) Reconstr.Empty. rewrite H1. cbn. rewrite app_nil_r. rewrite last_firstn_skipn. easy. easy. rewrite Nat.ltb_lt in H. apply firstn_neq_nil. Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.leb_le, @Coq.Init.Peano.le_n, @Coq.Lists.List.length_zero_iff_nil, @RAWBITVECTOR_LIST.length_mk_list_false, @Coq.Arith.PeanoNat.Nat.leb_gt) (@Coq.Init.Datatypes.app). Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.sub_gt) Reconstr.Empty. Qed. Lemma last_skipn_true: forall a n, (n <? length a)%nat = true -> last (shl_n_bits_a (skipn n a ++ mk_list_true n) n) false = last a false. Proof. intros. unfold shl_n_bits_a. assert (length (skipn n a ++ mk_list_true n) = length a). rewrite app_length, length_skipn, length_mk_list_true. rewrite Nat.ltb_lt in H. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.sub_add, @Coq.Arith.PeanoNat.Nat.lt_le_incl) Reconstr.Empty. rewrite H0, H. rewrite last_app. rewrite firstn_app. rewrite length_skipn. assert ((length a - n - (length a - n))%nat = O). Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.sub_diag) Reconstr.Empty. rewrite H1. cbn. rewrite app_nil_r. rewrite last_firstn_skipn. easy. easy. rewrite Nat.ltb_lt in H. apply firstn_neq_nil. Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.leb_le, @Coq.Init.Peano.le_n, @Coq.Lists.List.length_zero_iff_nil, @RAWBITVECTOR_LIST.length_mk_list_false, @Coq.Arith.PeanoNat.Nat.leb_gt) (@Coq.Init.Datatypes.app). Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.sub_gt) Reconstr.Empty. Qed. (* x << s <= 1 << s *) Lemma bv_shl_n_bits_a_1_leq : forall (n : N) (x s : bitvector), size x = n -> size s = n -> bv_ule (shl_n_bits_a x (list2nat_be_a s)) (shl_n_bits_a (mk_list_true (length s)) (list2nat_be_a s)) = true. Proof. intros n x s Hx Hs. assert (length x = length (mk_list_true (length s))). { pose proof Hx as Hxx. pose proof Hs as Hss. unfold size in Hxx, Hss. rewrite <- N2Nat.id in Hxx. apply Nat2N.inj in Hxx. rewrite <- N2Nat.id in Hss. apply Nat2N.inj in Hss. rewrite Hxx, Hss. rewrite length_mk_list_true. easy. } unfold shl_n_bits_a. rewrite <- H. case_eq ((list2nat_be_a s <? length x)%nat). + intros. assert (length s = length x). { pose proof Hx as Hxx. pose proof Hs as Hss. unfold size in Hxx, Hss. rewrite <- N2Nat.id in Hxx. apply Nat2N.inj in Hxx. rewrite <- N2Nat.id in Hss. apply Nat2N.inj in Hss. rewrite <- Hxx in Hss. apply Hss. } induction (list2nat_be_a s). - simpl. rewrite Nat.sub_0_r. rewrite firstn_all. rewrite H. rewrite firstn_all. rewrite H1. apply bv_ule_1_length. - rewrite Nat.ltb_lt in H0. pose proof Nat.lt_succ_l as lt_succ_l. specialize (@lt_succ_l n0 (length x) H0). apply Nat.ltb_lt in lt_succ_l. apply IHn0 in lt_succ_l. assert (bv_ule_1_firstn : forall (n : nat) (x : bitvector), (n < length x)%nat -> bv_ule (firstn n x) (firstn n (mk_list_true (length x))) = true). { apply bv_ule_1_firstn. } rewrite H1. specialize (@bv_ule_1_firstn ((length x) - (S n0))%nat x). assert (bv_ule_pre_append : forall (x y z : bitvector), bv_ule x y = true -> bv_ule (z ++ x) (z ++ y) = true). { apply bv_ule_pre_append. } specialize (@bv_ule_pre_append (firstn (length x - S n0) x) (firstn (length x - S n0) (mk_list_true (length x))) (mk_list_false (S n0))). apply bv_ule_pre_append. apply bv_ule_1_firstn. assert (forall m n : nat, (S m < n)%nat -> (n - (S m) < n)%nat). { intros. apply Nat.sub_lt. apply Nat.lt_le_incl in H2. apply H2. apply Nat.lt_0_succ. } specialize (@H2 n0 (length x) H0). apply H2. + intros. apply bv_ule_refl. Qed. Lemma bv_shl_a_1_leq : forall (n : N) (x s : bitvector), size x = n -> size s = n -> bv_ule (bv_shl_a x s) (bv_shl_a (bv_not (zeros (size s))) s) = true. Proof. intros n x s Hx Hs. unfold zeros. unfold size. rewrite Nat2N.id. rewrite bv_not_false_true. unfold bv_shl_a. rewrite Hx, Hs. unfold size. rewrite length_mk_list_true. pose proof Hs as Hss. unfold size in Hss. rewrite Hss. pose proof (@N.eqb_refl n). rewrite H. apply (@bv_shl_n_bits_a_1_leq n x s Hx Hs). Qed. (* 0 << b = 0 *) Lemma length_cons : forall (t : list bool) (h : bool) , length (h :: t) = S (length t). Proof. induction t; easy. Qed. Lemma firstn_mk_list_false : forall (x y: nat), (x < y)%nat -> firstn x (mk_list_false y) = mk_list_false x. Proof. intros x y ltxy. induction x. + easy. + induction y. - easy. - simpl. pose proof ltxy as ltxy2. apply lt_S_n in ltxy2. pose proof ltxy2 as ltxSy. apply Nat.lt_lt_succ_r in ltxSy. apply IHx in ltxSy. rewrite <- ltxSy. rewrite mk_list_false_app. rewrite firstn_app. rewrite length_mk_list_false. assert (forall (n m : nat), (n < m)%nat -> Nat.sub n m = O). { induction n. + easy. + induction m. - intros. now contradict H. - intros. simpl. apply lt_S_n in H. specialize (@IHn m H). apply IHn. } specialize (@H x y ltxy2). rewrite H. simpl. rewrite app_nil_r. easy. Qed. Lemma succ_minus_succ : forall x y : nat, (y < x)%nat -> S (x - S y) = (x - y)%nat. Proof. intros x y ltyx. pose proof (@Nat.sub_succ_r x y). rewrite H. pose proof (@Nat.lt_succ_pred O (x - y)). pose proof lt_minus_O_lt. pose proof (@lt_minus_O_lt y x ltyx). now apply H0 in H1. Qed. Lemma mk_list_false_app_minus : forall x y : nat, (y < x)%nat -> mk_list_false (y) ++ mk_list_false (x - y) = mk_list_false (x). Proof. intros x y ltyx. induction y. + simpl. now rewrite Nat.sub_0_r. + pose proof ltyx as ltyx2. apply Nat.lt_succ_l in ltyx2. specialize (@IHy ltyx2). rewrite mk_list_false_app. pose proof (@succ_minus_succ x y ltyx2). pose proof mk_list_false_app. rewrite <- app_assoc. simpl. assert (false :: mk_list_false (x - S y) = mk_list_false (S (x - S y))) by easy. rewrite H1. rewrite H. apply IHy. Qed. Lemma bvshl_zeros : forall (b : bitvector), bv_shl (zeros (size b)) b = zeros (size b). Proof. intros b. induction b. + easy. + rewrite bv_shl_eq in *. unfold bv_shl_a in *. rewrite zeros_size in *. rewrite eqb_refl in *. unfold shl_n_bits_a in *. unfold size in *. unfold zeros in *. rewrite Nat2N.id in *. case_eq ((list2nat_be_a (a :: b) <? length (mk_list_false (length (a :: b))))%nat); intros comp_b_lenb. - pose proof firstn_mk_list_false as firstn_mlf. destruct (@gt_0_eq (list2nat_be_a (a :: b))). * rewrite length_mk_list_false. specialize (@firstn_mlf (length (a :: b) - list2nat_be_a (a :: b))%nat (length (a :: b))). assert (lt : (length (a :: b) - list2nat_be_a (a :: b) < length (a :: b))%nat). { pose proof Nat.sub_lt. rewrite Nat.ltb_lt in comp_b_lenb. apply Nat.lt_le_incl in comp_b_lenb. specialize (@H0 (length (mk_list_false (length (a :: b)))) (list2nat_be_a (a :: b)) comp_b_lenb). rewrite length_mk_list_false in H0. apply H0. easy. } specialize (@firstn_mlf lt). rewrite firstn_mlf. apply mk_list_false_app_minus. rewrite length_mk_list_false in comp_b_lenb. rewrite Nat.ltb_lt in comp_b_lenb. apply comp_b_lenb. * rewrite <- H. rewrite Nat.sub_0_r. rewrite length_mk_list_false. assert (mk_list_false 0 = []) by easy. rewrite H0. rewrite app_nil_l. pose proof (@firstn_all bool ((mk_list_false (length (a :: b))))). rewrite length_mk_list_false in H1. apply H1. - now rewrite length_mk_list_false. Qed. (* b << 0 = b *) Lemma length_zero_nil : forall (b : bitvector), 0%nat = length b -> [] = b. Proof. intros. induction b; easy. Qed. Lemma bvshl_b_zeros : forall (b : bitvector), bv_shl_a b (zeros (size b)) = b. Proof. intros. unfold bv_shl_a. rewrite zeros_size. rewrite eqb_refl. unfold list2nat_be_a, zeros, size. rewrite Nat2N.id. rewrite list2N_mk_list_false. simpl. unfold shl_n_bits_a. case_eq (0 <? length b)%nat; intros. + simpl. rewrite Nat.sub_0_r. now rewrite firstn_all. + rewrite Nat.ltb_ge in H. apply (@le_n_0_eq (length b)) in H. rewrite <- H. simpl. now apply length_zero_nil. Qed. Lemma mk_list_false_true_app : forall x y : nat, (y < x)%nat -> mk_list_false y ++ mk_list_true (x - y) <> mk_list_false x. Proof. intros x y ltyx. induction y. + simpl. rewrite Nat.sub_0_r. induction x; easy. + pose proof ltyx as ltyx2. apply Nat.lt_succ_l in ltyx2. specialize (@IHy ltyx2). unfold not. intros. apply rev_func in H. rewrite rev_app_distr in H. rewrite rev_mk_list_false in H. rewrite rev_mk_list_false in H. rewrite rev_mk_list_true in H. case_eq (x - S y)%nat. - intros. apply minus_neq_O in ltyx. unfold not in ltyx. apply ltyx. apply H0. - intros. rewrite H0 in H. simpl in H. induction x. * easy. * simpl in H. now contradict H. Qed. Lemma bvshl_ones_neq_zero : forall (n : nat) (b : bitvector), length b = n -> bv_ult b (nat2bv (N.to_nat (size b)) (size b)) = true -> b <> mk_list_false (length b) -> bv_shl (mk_list_true n) b <> mk_list_false n. Proof. intros n b Hb H bnot0. induction n. + symmetry in Hb. pose proof (@length_zero_nil b Hb). symmetry in H0. rewrite H0 in *. now contradict H. + rewrite bv_shl_eq. unfold bv_shl_a. unfold size. rewrite length_mk_list_true. rewrite Hb. rewrite eqb_refl. unfold shl_n_bits_a. rewrite length_mk_list_true. rewrite <- Hb. pose proof (@bv_ult_nat b (nat2bv (N.to_nat (size b)) (size b))) as ult_eq. rewrite (@length_nat2bv (N.to_nat (size b)) (size b)) in ult_eq. unfold size in ult_eq at 1. rewrite Nat2N.id in ult_eq. rewrite Nat.eqb_refl in ult_eq. assert (true: true = true) by easy. specialize (@ult_eq true). rewrite ult_eq in H. unfold bv2nat_a, list2nat_be_a, nat2bv in H. rewrite N2Nat.id in H. rewrite (@list2N_N2List_eq (size b)) in H. unfold size in H. rewrite Nat2N.id in H. unfold list2nat_be_a. rewrite H. assert (sub_le : ((length b - N.to_nat (list2N b))%nat < (length b))%nat). { rewrite Nat.ltb_lt in H. pose proof H as Hleq. apply Nat.lt_le_incl in Hleq. pose proof (@not_mk_list_false b bnot0). rewrite Nat.ltb_lt in H0. pose proof Nat.sub_lt. pose proof (@Nat.sub_lt (length b) (N.to_nat (list2N b)) Hleq H0). apply H2. } rewrite (@prefix_mk_list_true (length b - N.to_nat (list2N b))%nat (length b) sub_le). apply mk_list_false_true_app. rewrite Nat.ltb_lt in H. apply H. Qed. (* logical shift right *) (* Definitions *) (* shr-defs *) Definition shr_one_bit (a: list bool) : list bool := match a with | [] => [] | xa :: xsa => xsa ++ [false] end. Definition shr_n_bits_a (a: list bool) (n: nat): list bool := if (n <? length a)%nat then skipn n a ++ mk_list_false n else mk_list_false (length a). Fixpoint shr_n_bits (a: list bool) (n: nat): list bool := match n with | O => a | S n' => shr_n_bits (shr_one_bit a) n' end. Definition shr_aux (a b: list bool): list bool := shr_n_bits a (list2nat_be_a b). Definition shr_aux_a (a b: list bool): list bool := shr_n_bits_a a (list2nat_be_a b). Definition bv_shr (a b : bitvector) : bitvector := if ((@size a) =? (@size b)) then shr_aux a b else nil. Definition bv_shr_a (a b : bitvector) : bitvector := if ((@size a) =? (@size b)) then shr_n_bits_a a (list2nat_be_a b) else nil. (* Theorems *) (* shr-thrms *) Lemma help_same: forall a n, (n <? length a)%nat = true -> firstn (length a - n) (skipn n a ++ false :: false :: mk_list_false n) = firstn (length a - n) (skipn n a ++ false :: mk_list_false n). Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq n; intros. cbn. f_equal. rewrite !firstn_app, !firstn_all. rewrite Nat.sub_diag. now cbn. cbn. rewrite !firstn_app. f_equal. rewrite length_skipn. assert ((length a0 - n0 - (length a0 - n0))%nat = 0%nat) by lia. rewrite H1. now cbn. Qed. Lemma fs: forall a n, (n <? length a)%nat = true -> skipn n (mk_list_false n ++ firstn (length a - n) (skipn n a ++ false :: mk_list_false n)) = skipn n a. Proof. intro a. induction a; intros. - cbn. now rewrite app_nil_r. - cbn. case_eq n; intros. cbn. rewrite firstn_app. now rewrite Nat.sub_diag, firstn_all, app_nil_r. cbn. specialize (IHa n0). rewrite help_same. apply IHa. apply Nat.ltb_lt in H. apply Nat.ltb_lt. subst. cbn in H. lia. apply Nat.ltb_lt in H. apply Nat.ltb_lt. subst. cbn in H. lia. Qed. Lemma shr_n_shl_a: forall a n, shr_n_bits_a (shl_n_bits_a (shr_n_bits_a a n) n) n = shr_n_bits_a a n. Proof. intro a. induction a; intros. - now cbn. - unfold shr_n_bits_a. case_eq ((n <? length (a :: a0))%nat); intros. assert ( length (shl_n_bits_a (skipn n (a :: a0) ++ mk_list_false n) n) = length (a :: a0)). { rewrite length_shl_n_bits_a, app_length, length_mk_list_false, length_skipn. apply Nat.ltb_lt in H. lia. } rewrite H0, H. unfold shl_n_bits_a. assert ((length (skipn n (a :: a0) ++ mk_list_false n) = length (a :: a0))). { rewrite app_length, length_skipn, length_mk_list_false. apply Nat.ltb_lt in H. lia. } rewrite H1, H. cbn. case_eq n; intros. cbn. assert (firstn (length a0) (a0 ++ []) = a0). { now rewrite app_nil_r, firstn_all. } now rewrite H3. cbn. f_equal. rewrite fs. easy. apply Nat.ltb_lt in H. apply Nat.ltb_lt. subst. cbn in *. lia. assert (length (shl_n_bits_a (mk_list_false (length (a :: a0))) n) = length (a :: a0)). { now rewrite length_shl_n_bits_a, length_mk_list_false. } now rewrite H0, H. Qed. Lemma skipn_all: forall (A: Type) n (l: list A), ((length l <=? n)%nat = true)-> skipn n l = []. Proof. intros A n. induction n; intros. - cbn. assert (length l = 0%nat). { apply Nat.leb_le in H. lia. } now apply length_zero_iff_nil in H0. - cbn. case_eq l; intros. + easy. + apply IHn. subst. cbn in H. apply Nat.leb_le in H. apply Nat.leb_le. lia. Qed. (* skipn n0 (mk_list_false n0 ++ firstn (length a0 - n0) (a :: a0)) = firstn (length a0 - n0) (a :: a0) *) Lemma skipn_jo: forall n (a: list bool), skipn n (mk_list_false n ++ a) = a. Proof. intro n. induction n; intros. - now cbn. - cbn. apply IHn. Qed. Lemma shl_n_shr_a: forall a n, shl_n_bits_a (shr_n_bits_a (shl_n_bits_a a n) n) n = shl_n_bits_a a n. Proof. intro a. induction a; intros. - now cbn. - unfold shr_n_bits_a, shl_n_bits_a. case_eq ((n <? length (a :: a0))%nat); intros. rewrite !app_length, !length_mk_list_false, !firstn_length. assert (Hone: Init.Nat.min (length (a :: a0) - n) (length (a :: a0))%nat = (length (a :: a0) - n)%nat). { rewrite Min.min_l. easy. lia. } rewrite Hone. assert (Htwo: (n + (length (a :: a0) - n))%nat = (length (a :: a0))%nat). { rewrite le_plus_minus_r. easy. apply Nat.ltb_lt in H. lia. } rewrite Htwo, H. assert (length (skipn n (mk_list_false n ++ firstn (length (a :: a0) - n) (a :: a0)) ++ mk_list_false n) = length (a :: a0)). { rewrite skipn_jo. rewrite app_length, firstn_length, length_mk_list_false. rewrite Min.min_l, Nat.sub_add. easy. apply Nat.ltb_lt in H. lia. lia. } rewrite H0, H. f_equal. rewrite firstn_app, length_skipn, app_length, length_mk_list_false. assert ((n + length (firstn (length (a :: a0) - n) (a :: a0)) - n)%nat = (length (a :: a0) - n)%nat). { rewrite firstn_length. rewrite Min.min_l, minus_plus. easy. apply Nat.ltb_lt in H. lia. } rewrite H1. assert ((length (a :: a0) - n - (length (a :: a0) - n))%nat = 0%nat). { lia. } rewrite H2. assert (firstn 0 (mk_list_false n) = []). { now cbn. } rewrite H3, app_nil_r. assert ((skipn n (mk_list_false n ++ firstn (length (a :: a0) - n) (a :: a0) )) = (firstn (length (a :: a0) - n) (a :: a0))). { cbn. case_eq n; intros. now cbn. cbn. rewrite skipn_jo. easy. } rewrite H4. now rewrite firstn_firstn, Nat.min_id. now rewrite !length_mk_list_false, H, length_mk_list_false, H. Qed. Lemma length_shr_one_bit: forall a, length (shr_one_bit a) = length a. Proof. intro a. induction a; intros. - now simpl. - simpl. rewrite <- IHa. case_eq a0; easy. Qed. Lemma length_shr_n_bits_a: forall n a, length (shr_n_bits_a a n) = length a. Proof. intro n. induction n; intros; simpl. - unfold shr_n_bits_a. case_eq a; intros. now cbn. cbn. rewrite app_length. f_equal. easy. - unfold shr_n_bits_a. case_eq ( (S n <? length a)%nat); intros. cbn. case_eq a; intros. subst. cbn in *. now contradict H. unfold shr_n_bits_a. subst. rewrite !app_length. cbn. rewrite length_mk_list_false. rewrite length_skipn. rewrite <- plus_n_Sm. f_equal. rewrite Nat.sub_add; try easy. apply Nat.ltb_lt in H. cbn in H. inversion H. rewrite H1. lia. lia. now rewrite length_mk_list_false. Qed. Lemma length_shr_n_bits: forall n a, length (shr_n_bits a n) = length a. Proof. intro n. induction n; intros; simpl. - reflexivity. - now rewrite (IHn (shr_one_bit a)), length_shr_one_bit. Qed. Lemma length_shr_aux: forall a b n, n = (length a) -> n = (length b)%nat -> n = (length (shr_aux a b)). Proof. intros. unfold shr_aux. now rewrite length_shr_n_bits. Qed. Lemma bv_shr_size n a b : size a = n -> size b = n -> size (bv_shr a b) = n. Proof. unfold bv_shr. intros H1 H2. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size in *. rewrite <- (@length_shr_aux a b (nat_of_N n)). now rewrite N2Nat.id. now apply (f_equal (N.to_nat)) in H1; rewrite Nat2N.id in H1. now apply (f_equal (N.to_nat)) in H2; rewrite Nat2N.id in H2. Qed. Lemma shl_n_shl_one: forall n a, (shl_n_bits (shl_one_bit a) n) = shl_n_bits a (S n). Proof. intro n. induction n; intros. - now cbn. - cbn. now rewrite IHn. Qed. Lemma shr_n_shr_one: forall n a, (shr_n_bits (shr_one_bit a) n) = shr_n_bits a (S n). Proof. intro n. induction n; intros. - now cbn. - cbn. now rewrite IHn. Qed. Lemma shr_n_shr_one_comm: forall n a, (shr_n_bits (shr_one_bit a) n) = shr_one_bit (shr_n_bits a n). Proof. intro n. induction n; intros. - now cbn. - cbn. now rewrite IHn. Qed. Lemma rl_fact: forall (a: list bool) b, removelast (a ++ [b]) = a. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq (a0 ++ [b]); intros. contradict H. case_eq a0; intros; subst; easy. now rewrite <- H, IHa. Qed. Lemma shl_one_b: forall a b, (shl_one_bit (a ++ [b])) = false :: a. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq (a0 ++ [b]); intros. contradict H. case_eq a0; intros; subst; easy. rewrite <- H. f_equal. f_equal. apply rl_fact. Qed. Lemma shr_one_shl: forall a, shr_one_bit (shl_one_bit (shr_one_bit a)) = shr_one_bit a. Proof. intro a. induction a; intros. - now cbn. - cbn. rewrite shl_one_b. now cbn. Qed. Lemma shr_one_bit_append_false: forall a n, shr_one_bit (a ++ (mk_list_false n)) = (shr_one_bit a) ++ (mk_list_false n). Proof. intro a. induction a; intros. - cbn. case_eq n; intros. now cbn. cbn. now rewrite mk_list_false_cons. - cbn. rewrite !app_assoc_reverse. assert (mk_list_false n ++ [false] = [false] ++ mk_list_false n). { cbn. induction n; intros. now cbn. cbn. now rewrite <- IHn. } now rewrite H. Qed. Lemma shr_one_bit_skipn_false1: forall n a, length a = S n -> shr_one_bit (skipn n a) = [false]. Proof. intro n. induction n; intros. - cbn. case_eq a; intros. subst. now contradict H. cbn. assert (l = nil). subst. cbn in *. inversion H. apply length_zero_iff_nil in H1. now subst. now rewrite H1, app_nil_l. - cbn. case_eq a; intros. subst. now contradict H. apply IHn. subst. cbn in H. lia. Qed. Lemma shr_one_bit_skipn_false: forall n a, length a = S n -> shr_one_bit (skipn n a ++ mk_list_false n) = mk_list_false n ++ [false]. Proof. intro n. induction n; intros. - cbn. rewrite app_nil_r. case_eq a; intros. subst. now contradict H. subst. cbn in *. inversion H. apply length_zero_iff_nil in H1. subst. now rewrite app_nil_l. - cbn. case_eq a; intros. subst. now contradict H. rewrite <- (IHn l). rewrite shr_one_bit_append_false. assert (false :: mk_list_false n = mk_list_false (S n)). { easy. } rewrite H1, shr_one_bit_append_false. cbn. rewrite shr_one_bit_skipn_false1. easy. subst. cbn in *. lia. subst. cbn in *. lia. Qed. Lemma shr_one_bit_all_false: forall (a: list bool), shr_one_bit (mk_list_false (length a)) = mk_list_false (length a). Proof. intro a. induction a; intros. - now cbn. - cbn. now rewrite mk_list_false_cons. Qed. Lemma skip_n_one_bit: forall n l b, ((n <? length (b :: l))%nat = true) -> shr_one_bit (skipn n (b :: l)) = skipn n l ++ [false]. Proof. intro n. induction n; intros. - now cbn. - cbn. case_eq l; intros. subst. cbn in *. now contradict H. rewrite <- (IHn l0 b0). easy. apply Nat.ltb_lt in H. apply Nat.ltb_lt. subst. cbn in *. lia. Qed. Theorem bv_shr_aux_eq: forall n a, shr_n_bits a n = shr_n_bits_a a n. Proof. intro n. induction n; intros. - cbn. case_eq a; intros. now cbn. cbn. now rewrite app_nil_r. - cbn. rewrite shr_n_shr_one_comm, IHn. unfold shr_n_bits_a. case_eq ((S n <? length a)%nat); intros. assert ((n <? length a)%nat = true). { apply Nat.ltb_lt in H. apply Nat.ltb_lt. lia. } rewrite H0. cbn. case_eq a; intros. subst. cbn in *. now contradict H. cbn. assert (skipn n l ++ false :: mk_list_false n = skipn n l ++ [false] ++ mk_list_false n). { easy. } rewrite H2. rewrite app_assoc. specialize (@skip_n_one_bit n l b); intros. rewrite <- H3. rewrite shr_one_bit_append_false. easy. apply Nat.ltb_lt in H. apply Nat.ltb_lt. subst. cbn in *. lia. case_eq ((n <? length a)%nat); intros. assert (length a = S n). { apply Nat.ltb_lt in H0. apply Nat.ltb_ge in H. lia. } rewrite H1, shr_one_bit_skipn_false; try easy. cbn. now rewrite mk_list_false_cons. now rewrite shr_one_bit_all_false. Qed. Theorem bv_shr_eq: forall (a b : bitvector), bv_shr a b = bv_shr_a a b. Proof. intros. unfold bv_shr, bv_shr_a. case_eq (size a =? size b); intros; try easy. unfold shr_aux. now rewrite bv_shr_aux_eq. Qed. (* arithmetic shift right *) (* Definitions *) (* ashr-defs *) Definition ashr_one_bit (a: list bool) (sign: bool) : list bool := match a with | [] => [] | h :: t => t ++ [sign] end. Definition ashr_n_bits_a (a: list bool) (n: nat) (sign: bool): list bool := if (n <? length a)%nat then if (Bool.eqb sign false) then skipn n a ++ mk_list_false n else skipn n a ++ mk_list_true n else if (Bool.eqb sign false) then mk_list_false (length a) else mk_list_true (length a). Fixpoint ashr_n_bits (a: list bool) (n: nat) (sign: bool): list bool := match n with | O => a | S n' => ashr_n_bits (ashr_one_bit a sign) n' sign end. Definition ashr_aux_a (a b: list bool): list bool := ashr_n_bits_a a (list2nat_be_a b) (last a false). Definition ashr_aux (a b: list bool): list bool := ashr_n_bits a (list2nat_be_a b) (last a false). Definition bv_ashr_a (a b : bitvector) : bitvector := if ((@size a) =? (@size b)) then ashr_aux_a a b else nil. Definition bv_ashr (a b : bitvector) : bitvector := if ((@size a) =? (@size b)) then ashr_aux a b else nil. (* Theorems *) (* ashr-thrms *) Lemma length_ashr_one_bit: forall a sign, length (ashr_one_bit a sign) = length a. Proof. intro a. destruct sign. + induction a; intros. - now simpl. - simpl. rewrite <- IHa. case_eq a0; easy. + induction a; intros. - now simpl. - simpl. rewrite <- IHa. case_eq a0; easy. Qed. Lemma length_ashr_n_bits: forall n a sign, length (ashr_n_bits a n sign) = length a. Proof. intros n. induction n; intros; simpl. - reflexivity. - now rewrite (IHn (ashr_one_bit a sign)), length_ashr_one_bit. Qed. Lemma length_ashr_n_bits_a: forall a n sign, length (ashr_n_bits_a a n sign) = length a. Proof. intro n. induction n; intros. - cbn. Reconstr.reasy (@RAWBITVECTOR_LIST.length_mk_list_false) (@RAWBITVECTOR_LIST.mk_list_false). - cbn. unfold ashr_n_bits_a. cbn. case_eq ((n0 <=? length n)%nat); intros. + case_eq (eqb sign false); intros. rewrite app_length ,length_mk_list_false. rewrite length_skipn. Reconstr.reasy (@Coq.Arith.Compare_dec.leb_correct_conv, @Coq.Arith.PeanoNat.Nat.sub_add, @Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Arith.PeanoNat.Nat.lt_eq_cases, @Coq.Arith.PeanoNat.Nat.ltb_ge) (@RAWBITVECTOR_LIST.shl_n_bits_a, @Coq.Init.Datatypes.length, @Coq.Init.Peano.lt). rewrite app_length ,length_mk_list_true. rewrite length_skipn. Reconstr.reasy (@Coq.Arith.Compare_dec.leb_correct_conv, @Coq.Arith.PeanoNat.Nat.sub_add, @Coq.Arith.PeanoNat.Nat.ltb_lt, @Coq.Arith.PeanoNat.Nat.lt_eq_cases, @Coq.Arith.PeanoNat.Nat.ltb_ge) (@RAWBITVECTOR_LIST.shl_n_bits_a, @Coq.Init.Datatypes.length, @Coq.Init.Peano.lt). + case_eq (eqb sign false); intros. cbn. now rewrite length_mk_list_false. cbn. now rewrite length_mk_list_true. Qed. Lemma length_ashr_aux_a: forall a b n, n = (length a) -> n = (length b)%nat -> n = (length (ashr_aux_a a b)). Proof. intros. unfold ashr_aux_a. now rewrite length_ashr_n_bits_a. Qed. Lemma length_ashr_aux: forall a b n, n = (length a) -> n = (length b)%nat -> n = (length (ashr_aux a b)). Proof. intros. unfold ashr_aux. now rewrite length_ashr_n_bits. Qed. Lemma bv_ashr_size n a b : size a = n -> size b = n -> size (bv_ashr a b) = n. Proof. unfold bv_ashr. intros H1 H2. rewrite H1, H2. rewrite N.eqb_compare. rewrite N.compare_refl. unfold size in *. rewrite <- (@length_ashr_aux a b (nat_of_N n)). now rewrite N2Nat.id. now apply (f_equal (N.to_nat)) in H1; rewrite Nat2N.id in H1. now apply (f_equal (N.to_nat)) in H2; rewrite Nat2N.id in H2. Qed. Lemma ashr_shr_false: forall a n, ashr_n_bits_a a n false = shr_n_bits_a a n. Proof. intro a. induction a; intros. - now cbn. - unfold ashr_n_bits_a, shr_n_bits_a. cbn. case_eq ((n <=? length a0)%nat); intros; easy. Qed. Lemma ashr_n_shl_a: forall a n b, ashr_n_bits_a (shl_n_bits_a (ashr_n_bits_a a n b) n) n b = ashr_n_bits_a a n b. Proof. intros. case_eq b; intros. - revert n. induction a; intros. + now cbn. + unfold ashr_n_bits_a, shl_n_bits_a. case_eq ( (n <? length (a :: a0))%nat); intros. * cbn. rewrite !app_length, !length_mk_list_true. rewrite !length_skipn. assert (Hone: ((length (a :: a0) - n + n))%nat = (length (a :: a0))%nat). { apply Nat.ltb_lt in H0. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.lt_le_incl, @Coq.Arith.PeanoNat.Nat.sub_add) Reconstr.Empty. } rewrite Hone. cbn. assert (Htwo: (n <=? length a0)%nat = true). { Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.leb_antisym, @Coq.Arith.PeanoNat.Nat.leb_nle, @Coq.Arith.PeanoNat.Nat.ltb_antisym, @Coq.Bool.Bool.negb_true_iff, @Coq.Arith.Compare_dec.leb_correct) (@Coq.Init.Nat.ltb, @Coq.Init.Datatypes.length). } rewrite Htwo. rewrite !app_length, !length_mk_list_false, !firstn_length. case_eq n; intros. cbn. Reconstr.reasy (@Coq.Lists.List.firstn_all, @Coq.Lists.List.app_nil_r) Reconstr.Empty. subst. cbn. rewrite !app_length. cbn. rewrite !length_mk_list_true. rewrite Min.min_l. assert ((n0 + (length a0 - n0))%nat = (length a0)). { rewrite le_plus_minus_r. easy. apply Nat.leb_le in Htwo. Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.lt_le_incl) (@Coq.Init.Peano.lt). } rewrite H. case_eq a0; intros. subst. cbn in *. easy. subst. cbn in *. rewrite H0. case_eq n0; intros. cbn. Reconstr.rcrush (@Coq.Lists.List.app_nil_r, @Coq.Lists.List.firstn_all, @Coq.Arith.PeanoNat.Nat.sub_diag, @Coq.Lists.List.app_comm_cons, @Coq.Lists.List.firstn_app) (@Coq.Lists.List.firstn). cbn. rewrite skipn_jo. rewrite firstn_app, length_skipn. assert (firstn (length l - n) (skipn n l) = skipn n l). { Reconstr.rsimple (@Coq.Lists.List.firstn_all2, @Coq.Arith.PeanoNat.Nat.lt_eq_cases, @RAWBITVECTOR_LIST.length_skipn) Reconstr.Empty. } rewrite H2. assert (((length l - n - (length l - n)))%nat = O). { Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.sub_diag) Reconstr.Empty. } rewrite H3. cbn. now rewrite app_nil_r. Reconstr.rcrush (@Coq.Arith.PeanoNat.Nat.le_add_r, @RAWBITVECTOR_LIST.length_skipn) Reconstr.Empty. * cbn. rewrite length_mk_list_true. assert ((n <=? length a0)%nat = false). { Reconstr.reasy (@Coq.Arith.PeanoNat.Nat.ltb_ge, @Coq.Arith.PeanoNat.Nat.leb_gt) (@Coq.Init.Peano.lt, @Coq.Init.Datatypes.length). } rewrite H1. cbn. now rewrite length_mk_list_false, H1. - now rewrite !ashr_shr_false, shr_n_shl_a. Qed. Lemma last_bv_ashr_gt0: forall t s, size t = size s -> (0 <? N.to_nat (list2N t))%nat = true -> last (bv_shr_a s t) false = false. Proof. intros. unfold bv_shr_a,shr_n_bits_a,list2nat_be_a. rewrite H, N.eqb_refl. case_eq ( (N.to_nat (list2N t) <? length s)%nat); intros. - rewrite last_append. + Reconstr.reasy (@RAWBITVECTOR_LIST.last_mk_list_false) (@RAWBITVECTOR_LIST.bitvector). + Reconstr.reasy (@RAWBITVECTOR_LIST.length_mk_list_false, @Coq.Arith.PeanoNat.Nat.ltb_irrefl) (@Coq.Init.Datatypes.negb, @RAWBITVECTOR_LIST.mk_list_false, @RAWBITVECTOR_LIST.bitvector). - Reconstr.reasy (@RAWBITVECTOR_LIST.last_mk_list_false) (@RAWBITVECTOR_LIST.bitvector). Qed. (* b >>a 0 = b *) Lemma bvashr_zero : forall (b : bitvector), bv_ashr_a b (zeros (size b)) = b. Proof. intros b. unfold bv_ashr_a. rewrite zeros_size. rewrite NBoolEqualityFacts.eqb_refl. unfold ashr_aux_a. unfold ashr_n_bits_a. unfold size, zeros. rewrite Nat2N.id. unfold list2nat_be_a. rewrite list2N_mk_list_false. simpl. case_eq ((0 <? length b)%nat); intros. + case_eq (eqb (last b false) false); intros; simpl; now rewrite app_nil_r. + case_eq (eqb (last b false) false); intros; rewrite Nat.ltb_ge in H; apply le_n_0_eq in H; pose proof (@empty_list_length bool b); symmetry in H; apply H1 in H; rewrite H; easy. Qed. (*-------------------- Other Operations --------------------*) (* extraction *) (* Definitions *) (* extr-defs *) Fixpoint extract (x: list bool) (i j: nat) : list bool := match x with | [] => [] | bx :: x' => match i with | O => match j with | O => [] | S j' => bx :: extract x' i j' end | S i' => match j with | O => [] | S j' => extract x' i' j' end end end. (* bit-vector extraction *) Definition bv_extr (i n0 n1: N) a : bitvector := if (N.ltb n1 (n0 + i)) then mk_list_false (nat_of_N n0) else extract a (nat_of_N i) (nat_of_N (n0 + i)). (* Definition bv_extr (n i j: N) {H0: n >= j} {H1: j >= i} {a: bitvector} : bitvector := extract a (nat_of_N i) (nat_of_N j). Lemma bv_extr_size: forall n (i j: N) a (H0: n >= j) (H1: j >= i), size a = n -> size (@bv_extr n i j H0 H1 a) = (j - i)%N. Proof. intros. unfold bv_extr, size in *. rewrite <- N2Nat.id. apply f_equal. rewrite <- H in H0. specialize (@length_extract a i j H0 H1); intros; apply H2. Qed. *) (* Theorems *) (* extr-thrms *) Lemma zero_false: forall p, ~ 0 >= Npos p. Proof. intro p. induction p; lia. Qed. Lemma min_distr: forall i j: N, N.to_nat (j - i) = ((N.to_nat j) - (N.to_nat i))%nat. Proof. intros i j; case i; case j in *; try intros; lia. Qed. Lemma posSn: forall n, (Pos.to_nat (Pos.of_succ_nat n)) = S n. Proof. intros; case n; [easy | intros; lia ]. Qed. Lemma _length_extract: forall a (i j: N) (H0: (N.of_nat (length a)) >= j) (H1: j >= i), length (extract a 0 (N.to_nat j)) = (N.to_nat j). Proof. intro a. induction a as [ | xa xsa IHa ]. - simpl. case i in *. case j in *. easy. lia. case j in *; lia. - intros. simpl. case_eq j. intros. now simpl. intros. rewrite <- H. case_eq (N.to_nat j). easy. intros. simpl. apply f_equal. specialize (@IHa 0%N (N.of_nat n)). rewrite Nat2N.id in IHa. apply IHa. apply (f_equal (N.of_nat)) in H2. rewrite N2Nat.id in H2. rewrite H2 in H0. simpl in *. lia. lia. Qed. Lemma length_extract: forall a (i j: N) (H0: (N.of_nat (length a)) >= j) (H1: j >= i), length (extract a (N.to_nat i) (N.to_nat j)) = (N.to_nat (j - i)). Proof. intro a. induction a as [ | xa xsa IHa]. - intros. simpl. case i in *. case j in *. easy. simpl in *. contradict H0. apply zero_false. case j in *. now simpl. apply zero_false in H0; now contradict H0. - intros. simpl. case_eq (N.to_nat i). intros. case_eq (N.to_nat j). intros. rewrite min_distr. now rewrite H, H2. intros. simpl. rewrite min_distr. rewrite H, H2. simpl. apply f_equal. specialize (@IHa 0%N (N.of_nat n)). rewrite Nat2N.id in IHa. simpl in *. rewrite IHa. lia. lia. lia. intros. case_eq (N.to_nat j). simpl. intros. rewrite min_distr. rewrite H, H2. now simpl. intros. rewrite min_distr. rewrite H, H2. simpl. specialize (@IHa (N.of_nat n) (N.of_nat n0)). rewrite !Nat2N.id in IHa. rewrite IHa. lia. apply (f_equal (N.of_nat)) in H2. rewrite N2Nat.id in H2. rewrite H2 in H0. simpl in H0. lia. lia. Qed. Lemma not_ltb: forall (n0 n1 i: N), (n1 <? n0 + i)%N = false -> n1 >= n0 + i. Proof. intro n0. induction n0. intros. simpl in *. apply N.ltb_nlt in H. apply N.nlt_ge in H. lia. intros. simpl. case_eq i. intros. subst. simpl in H. apply N.ltb_nlt in H. apply N.nlt_ge in H. intros. simpl in H. lia. intros. subst. apply N.ltb_nlt in H. apply N.nlt_ge in H. lia. Qed. Lemma bv_extr_size: forall (i n0 n1 : N) a, size a = n1 -> size (@bv_extr i n0 n1 a) = n0%N. Proof. intros. unfold bv_extr, size in *. case_eq (n1 <? n0 + i). intros. now rewrite length_mk_list_false, N2Nat.id. intros. specialize (@length_extract a i (n0 + i)). intros. assert ((n0 + i - i) = n0)%N. { lia. } rewrite H2 in H1. rewrite H1. now rewrite N2Nat.id. rewrite H. now apply not_ltb. lia. Qed. Lemma extract_app: forall (a b: list bool), extract (a ++ b) 0 (length a) = a. Proof. intro a. induction a; intros. - cbn. case_eq b; intros. + now cbn. + now cbn. - cbn. now rewrite (IHa b). Qed. Lemma extract_all: forall (a: list bool), extract a 0 (length a) = a. Proof. intro a. induction a; intros. - now cbn. - cbn. now rewrite IHa at 1. Qed. Lemma extract_app_all: forall (a b: list bool), extract a 0 (length b) ++ extract a (length b) (length a) = a. Proof. intro a. induction a; intros. - now cbn. - cbn. case_eq b; intros. cbn. now rewrite (extract_all a0). cbn. f_equal. now rewrite IHa. Qed. (* extension *) (* Definitions *) (* extn-defs *) Fixpoint extend (x: list bool) (i: nat) (b: bool) {struct i}: list bool := match i with | O => x | S i' => b :: extend x i' b end. Definition zextend (x: list bool) (i: nat): list bool := extend x i false. Definition sextend (x: list bool) (i: nat): list bool := match x with | [] => mk_list_false i | xb :: x' => extend x i xb end. (* bit-vector extension *) Definition bv_zextn (n i: N) (a: bitvector): bitvector := zextend a (nat_of_N i). Definition bv_sextn (n i: N) (a: bitvector): bitvector := sextend a (nat_of_N i). (* Theorems *) (* extn-thrms *) Lemma extend_size_zero: forall i b, (length (extend [] i b)) = i. Proof. intros. induction i as [ | xi IHi]. - now simpl. - simpl. now rewrite IHi. Qed. Lemma extend_size_one: forall i a b, length (extend [a] i b) = S i. Proof. intros. induction i. - now simpl. - simpl. now rewrite IHi. Qed. Lemma length_extend: forall a i b, length (extend a i b) = ((length a) + i)%nat. Proof. intro a. induction a. - intros. simpl. now rewrite extend_size_zero. - intros. induction i. + intros. simpl. lia. + intros. simpl. apply f_equal. rewrite IHi. simpl. lia. Qed. Lemma zextend_size_zero: forall i, (length (zextend [] i)) = i. Proof. intros. unfold zextend. apply extend_size_zero. Qed. Lemma zextend_size_one: forall i a, length (zextend [a] i) = S i. Proof. intros. unfold zextend. apply extend_size_one. Qed. Lemma length_zextend: forall a i, length (zextend a i) = ((length a) + i)%nat. Proof. intros. unfold zextend. apply length_extend. Qed. Lemma sextend_size_zero: forall i, (length (sextend [] i)) = i. Proof. intros. unfold sextend. now rewrite length_mk_list_false. Qed. Lemma sextend_size_one: forall i a, length (sextend [a] i) = S i. Proof. intros. unfold sextend. apply extend_size_one. Qed. Lemma length_sextend: forall a i, length (sextend a i) = ((length a) + i)%nat. Proof. intros. unfold sextend. case_eq a. intros. rewrite length_mk_list_false. easy. intros. apply length_extend. Qed. Lemma plus_distr: forall i j: N, N.to_nat (j + i) = ((N.to_nat j) + (N.to_nat i))%nat. Proof. intros i j; case i; case j in *; try intros; lia. Qed. Lemma bv_zextn_size: forall n (i: N) a, size a = n -> size (@bv_zextn n i a) = (i + n)%N. Proof. intros. unfold bv_zextn, zextend, size in *. rewrite <- N2Nat.id. apply f_equal. specialize (@length_extend a (nat_of_N i) false). intros. rewrite H0. rewrite plus_distr. rewrite plus_comm. apply f_equal. apply (f_equal (N.to_nat)) in H. now rewrite Nat2N.id in H. Qed. Lemma bv_sextn_size: forall n (i: N) a, size a = n -> size (@bv_sextn n i a) = (i + n)%N. Proof. intros. unfold bv_sextn, sextend, size in *. rewrite <- N2Nat.id. apply f_equal. case_eq a. intros. rewrite length_mk_list_false. rewrite H0 in H. simpl in H. rewrite <- H. lia. intros. specialize (@length_extend a (nat_of_N i) b). intros. subst. rewrite plus_distr. rewrite plus_comm. rewrite Nat2N.id. now rewrite <- H1. Qed. End RAWBITVECTOR_LIST. Module BITVECTOR_LIST <: BITVECTOR. Include RAW2BITVECTOR(RAWBITVECTOR_LIST). Notation "x |0" := (cons false x) (left associativity, at level 73, format "x |0"): bv_scope. Notation "x |1" := (cons true x) (left associativity, at level 73, format "x |1"): bv_scope. Notation "'b|0'" := [false] (at level 70): bv_scope. Notation "'b|1'" := [true] (at level 70): bv_scope. Notation "# x |" := (@of_bits x) (no associativity, at level 1, format "# x |"): bv_scope. Notation "v @ p" := (bitOf p v) (at level 1, format "v @ p ") : bv_scope. End BITVECTOR_LIST. (* Local Variables: coq-load-path: ((rec ".." "SMTCoq")) End: *)
#print add_group /- structure add_group : Type u β†’ Type u fields: add_group.add : Ξ  {Ξ± : Type u} [c : add_group Ξ±], Ξ± β†’ Ξ± β†’ Ξ± add_group.add_assoc : βˆ€ {Ξ± : Type u} [c : add_group Ξ±] (a b c_1 : Ξ±), a + b + c_1 = a + (b + c_1) add_group.zero : Ξ  (Ξ± : Type u) [c : add_group Ξ±], Ξ± add_group.zero_add : βˆ€ {Ξ± : Type u} [c : add_group Ξ±] (a : Ξ±), 0 + a = a add_group.add_zero : βˆ€ {Ξ± : Type u} [c : add_group Ξ±] (a : Ξ±), a + 0 = a add_group.neg : Ξ  {Ξ± : Type u} [c : add_group Ξ±], Ξ± β†’ Ξ± add_group.add_left_neg : βˆ€ {Ξ± : Type u} [c : add_group Ξ±] (a : Ξ±), -a + a = 0 -/ structure add_group' {G : Type} [has_add G] [has_zero G] [has_neg G] := -- those lines mean -- "we have a function + : G x G β†’ G written Ξ» a b, a + b" -- "we have something called 0 : G" -- "we have a map neg : G β†’ G written Ξ» a, -a" (add_assoc : βˆ€ (a b c : G), a + b + c = a + (b + c)) (zero_add : βˆ€ (a : G), 0 + a = a) (add_zero : βˆ€ (a : G), a + 0 = a) (add_left_neg : βˆ€ (a : G), -a + a = 0) #print has_add #print has_zero #print has_neg variables {G : Type} [has_add G] [has_zero G] [has_neg G] theorem add_group.add_zero' (add_assoc : βˆ€ a b c : G, a + b + c = a + (b + c)) (zero : G) (zero_add : βˆ€ a : G, 0 + a = a) (neg : G β†’ G) (add_left_neg : βˆ€ a : G, -a + a = 0) : βˆ€ a : G, a + 0 = a := begin have left_cancel : βˆ€ (a b c : G), a + b = a + c β†’ b = c, { intros a b c Habac, exact calc b = 0 + b : (zero_add _).symm ... = (-a + a) + b : by rw [←add_left_neg] ... = -a + (a + b) : add_assoc _ _ _ ... = -a + (a + c) : by rw Habac ... = (-a + a) + c : (add_assoc _ _ _).symm ... = 0 + c : by rw add_left_neg ... = c : zero_add _ }, -- a + 0 = a intro a, apply (left_cancel (-a) _ _), rw [←add_assoc,add_left_neg,zero_add], end #print group
theory TA_More imports Timed_Automata TA_DBM_Operations Refine_Imperative_HOL.IICF TA_Library.Graphs begin chapter \<open>Mixed Additional Material on Timed Automata\<close> text \<open> Additional Lemmas on Timed Automata that do not belong to a particular other part of the formalization. \<close> fun extend_cc where "extend_cc cc [] = cc" | "extend_cc cc (c # cs) = GE c 0 # extend_cc cc cs" lemma collect_clock_pairs_extend_cc: "collect_clock_pairs (extend_cc cc cs) = collect_clock_pairs cc \<union> {(c, 0) | c. c \<in> set cs}" by (induction cs) (auto simp: collect_clock_pairs_def) definition "extend_ta A xs \<equiv> (trans_of A, \<lambda> l. extend_cc (inv_of A l) xs)" (* XXX Interestingly, this does not go through without the IICF simpset *) lemma clk_set_extend_ta: "clk_set (extend_ta A xs) = clk_set A \<union> set xs" unfolding extend_ta_def clkp_set_def inv_of_def trans_of_def by (auto simp: collect_clki_def collect_clock_pairs_extend_cc) lemma extend_cc_iff: "u \<turnstile> extend_cc cc cs \<longleftrightarrow> u \<turnstile> cc" if "\<forall> c. u c \<ge> 0" using that by (induction cs) (force simp: clock_val_def)+ lemma [simp]: "inv_of (extend_ta A cs) l = extend_cc (inv_of A l) cs" unfolding extend_ta_def inv_of_def by simp lemma reset_V: "\<forall> c. ([r\<rightarrow>0]u) c \<ge> 0" if "\<forall> c. u c \<ge> 0" using that apply safe subgoal for c by (cases "c \<in> set r") (auto simp add: reset_set1 reset_set11) done lemma delay_V: "\<forall> c. (u \<oplus> d) c \<ge> 0" if "\<forall> c. u c \<ge> 0" "(d :: 't :: time) \<ge> 0" using that unfolding cval_add_def by auto context notes [elim!] = step.cases step'.cases step_t.cases step_z.cases begin lemma extend_ta_iff: "A \<turnstile> \<langle>l, u\<rangle> \<rightarrow> \<langle>l', u'\<rangle> \<longleftrightarrow> extend_ta A cs \<turnstile> \<langle>l, u\<rangle> \<rightarrow> \<langle>l', u'\<rangle>" if "\<forall> c. u c \<ge> 0" using that apply safe apply (force simp: extend_cc_iff reset_V intro: step_a.intros elim!: step_a.cases) apply (force simp add: extend_cc_iff delay_V) apply (cases rule: step_a.cases, assumption) apply (rule step.intros, rule step_a.intros) unfolding extend_ta_def apply (force simp: trans_of_def) apply assumption+ prefer 2 apply assumption subgoal by (metis extend_cc_iff inv_of_def prod.sel(2) reset_V) subgoal by (metis delay_V extend_cc_iff inv_of_def prod.sel(2) step_t step_t.intros) done (* apply simp apply (force simp add: extend_cc_iff) apply (auto simp add: reset_V extend_cc_iff intro: step_a.intros) using extend_cc_iff delay_V by fas *) lemma steps_iffI: "A \<turnstile> \<langle>l, u\<rangle> \<rightarrow>* \<langle>l', u'\<rangle> \<longleftrightarrow> A' \<turnstile> \<langle>l, u\<rangle> \<rightarrow>* \<langle>l', u'\<rangle>" if "\<And> l u l' u'. P u \<Longrightarrow> A \<turnstile> \<langle>l, u\<rangle> \<rightarrow> \<langle>l', u'\<rangle> \<longleftrightarrow> A' \<turnstile> \<langle>l, u\<rangle> \<rightarrow> \<langle>l', u'\<rangle>" "\<And> (A :: ('a, 'c, 't :: time, 's) ta) l u l' u'. A \<turnstile> \<langle>l, u\<rangle> \<rightarrow> \<langle>l', u'\<rangle> \<Longrightarrow> P u \<Longrightarrow> P u'" "P u" for A A' :: "('a, 'c, 't :: time, 's) ta" apply standard apply (insert that(3)) subgoal by (induction A\<equiv> A _ _ _ _ rule: steps.induct) (auto simp: that(1,2)) by (induction A\<equiv> A' _ _ _ _ rule: steps.induct) (auto simp: that(2) that(1)[symmetric]) lemma step_V: "\<forall> c. u' c \<ge> 0" if "A \<turnstile> \<langle>l, u\<rangle> \<rightarrow> \<langle>l', u'\<rangle>" "\<forall> c. u c \<ge> 0" using that by (auto simp: reset_V delay_V elim: step_a.cases) end (* End of context for aggressive elimination rules *) lemma extend_ta_iff_steps: "A \<turnstile> \<langle>l, u\<rangle> \<rightarrow>* \<langle>l', u'\<rangle> \<longleftrightarrow> extend_ta A cs \<turnstile> \<langle>l, u\<rangle> \<rightarrow>* \<langle>l', u'\<rangle>" if "\<forall> c. u c \<ge> 0" apply (intro steps_iffI extend_ta_iff) apply assumption by (rule step_V, assumption+, rule that) lemma collect_clkvt_alt_def: "collect_clkvt T = \<Union>((set o fst \<circ> snd \<circ> snd \<circ> snd) ` T)" unfolding collect_clkvt_def by fastforce lemma ta_collect_clkt_alt_def: "collect_clkt S = \<Union> (collect_clock_pairs ` (fst o snd) ` S)" unfolding Timed_Automata.collect_clkt_def by fastforce lemma ta_collect_clki_alt_def: "collect_clki I = \<Union> (collect_clock_pairs ` I ` UNIV)" unfolding Timed_Automata.collect_clki_def by auto lemma constraint_clk_constraint_pair: "constraint_clk ac = fst (constraint_pair ac)" by (cases ac) auto lemma collect_clks_inv_clk_set: "collect_clks (inv_of A l) \<subseteq> clk_set A" unfolding clkp_set_def collect_clki_def collect_clks_def collect_clock_pairs_def by (auto simp: constraint_clk_constraint_pair) lemma collect_clocks_clk_set: assumes "A \<turnstile> l \<longrightarrow>\<^bsup>g,a,r\<^esup> l'" shows "collect_clks g \<subseteq> clk_set A" using assms unfolding clkp_set_def collect_clks_id collect_clkt_def by fastforce lemma reset_clk_set: assumes "A \<turnstile> l \<longrightarrow>\<^bsup>g,a,r\<^esup> l'" shows "set r \<subseteq> clk_set A" using assms by (fastforce simp: clkp_set_def collect_clkvt_def) lemma ac_iff: "u1 \<turnstile>\<^sub>a ac \<longleftrightarrow> u2 \<turnstile>\<^sub>a ac" if "u1 (fst (constraint_pair ac)) = u2 (fst (constraint_pair ac))" using that by (cases ac) auto lemma ac_iff': "u1 \<turnstile>\<^sub>a ac \<longleftrightarrow> u2 \<turnstile>\<^sub>a ac" if "u1 (constraint_clk ac) = u2 (constraint_clk ac)" using that by (cases ac) auto lemma cc_iff: "u1 \<turnstile> cc \<longleftrightarrow> u2 \<turnstile> cc" if "\<forall> (c, d) \<in> collect_clock_pairs cc. u1 c = u2 c" using that by (auto 4 3 simp: ac_iff[of u1 _ u2] list_all_iff collect_clock_pairs_def clock_val_def) lemma cc_iff': "u1 \<turnstile> cc \<longleftrightarrow> u2 \<turnstile> cc" if "\<forall> c \<in> collect_clks cc. u1 c = u2 c" using that by (auto simp: ac_iff'[of u1 _ u2] list_all_iff collect_clks_def clock_val_def) lemma step_t_bisim: "\<exists> u2'. A \<turnstile> \<langle>l, u2\<rangle> \<rightarrow>\<^bsup>d\<^esup> \<langle>l', u2'\<rangle> \<and> (\<forall> c. c \<in> clk_set A \<longrightarrow> u1' c = u2' c)" if assms: "A \<turnstile> \<langle>l, u1\<rangle> \<rightarrow>\<^bsup>d\<^esup> \<langle>l', u1'\<rangle>" "\<forall> c. c \<in> clk_set A \<longrightarrow> u1 c = u2 c" using that(1) apply cases apply (subst (asm) cc_iff'[of _ _ "u2 \<oplus> d"]) subgoal using collect_clks_inv_clk_set[of A l] assms(2) by (auto simp: cval_add_def) using assms(2) by (force simp: cval_add_def) lemma step_a_bisim: "\<exists> u2'. A \<turnstile> \<langle>l, u2\<rangle> \<rightarrow>\<^bsub>a\<^esub> \<langle>l', u2'\<rangle> \<and> (\<forall> c. c \<in> clk_set A \<longrightarrow> u1' c = u2' c)" if assms: "A \<turnstile> \<langle>l, u1\<rangle> \<rightarrow>\<^bsub>a\<^esub> \<langle>l', u1'\<rangle>" "\<forall> c. c \<in> clk_set A \<longrightarrow> u1 c = u2 c" using that(1) apply cases subgoal for g r apply (subst (asm) cc_iff'[of _ _ "u2"]) subgoal using assms(2) by (force dest: collect_clocks_clk_set) apply (subst (asm) (2) cc_iff'[of _ _ "[r\<rightarrow>0]u2"]) subgoal apply rule subgoal for c using collect_clks_inv_clk_set[of A l'] assms(2) by (cases "c \<in> set r"; force) done apply (intro exI conjI) apply (rule, assumption+) apply (simp; fail) apply rule subgoal for c using collect_clks_inv_clk_set[of A l'] assms(2) by (cases "c \<in> set r"; force) done done lemma step'_bisim: "\<exists> u2'. A \<turnstile>' \<langle>l, u2\<rangle> \<rightarrow> \<langle>l', u2'\<rangle> \<and> (\<forall> c. c \<in> clk_set A \<longrightarrow> u1' c = u2' c)" if assms: "A \<turnstile>' \<langle>l, u1\<rangle> \<rightarrow> \<langle>l', u1'\<rangle>" "\<forall> c. c \<in> clk_set A \<longrightarrow> u1 c = u2 c" using that(1) apply cases apply (drule step_t_bisim[OF _ that(2)]) apply clarify apply (drule step_a_bisim, assumption) apply auto done lemma ta_bisimulation: "Bisimulation_Invariant (\<lambda> (l, u) (l', u'). A \<turnstile>' \<langle>l, u\<rangle> \<rightarrow> \<langle>l', u'\<rangle>) (\<lambda> (l, u) (l', u'). A \<turnstile>' \<langle>l, u\<rangle> \<rightarrow> \<langle>l', u'\<rangle>) (\<lambda> (l, u) (l', u'). l' = l \<and> (\<forall> c. c \<in> clk_set A \<longrightarrow> u c = u' c)) (\<lambda> _. True) (\<lambda> _. True) " apply standard subgoal for a b a' by clarify (drule step'_bisim; auto) subgoal for a b a' by clarify (drule step'_bisim; auto) .. end (* Theory *)
[STATEMENT] lemma true_not_false [simp]: "true \<noteq> false" "false \<noteq> true" [PROOF STATE] proof (prove) goal (1 subgoal): 1. \<not> true = false &&& \<not> false = true [PROOF STEP] by (pred_auto)+
mortgages more expensive," Rep. Ackerman said. 42 states, according to estimates. the GSEs' grip on the mortgage market. much needed votes to pass the measure.
Formal statement is: lemma\<^marker>\<open>tag important\<close> emeasure_lborel_cbox[simp]: assumes [simp]: "\<And>b. b \<in> Basis \<Longrightarrow> l \<bullet> b \<le> u \<bullet> b" shows "emeasure lborel (cbox l u) = (\<Prod>b\<in>Basis. (u - l) \<bullet> b)" Informal statement is: The Lebesgue measure of a box is the product of the lengths of its sides.
foo : String foo = """\{"hello"} """
lemma abs_norm_cancel [simp]: "\<bar>norm a\<bar> = norm a"