blob_id
stringlengths
40
40
directory_id
stringlengths
40
40
path
stringlengths
7
139
content_id
stringlengths
40
40
detected_licenses
listlengths
0
16
license_type
stringclasses
2 values
repo_name
stringlengths
7
55
snapshot_id
stringlengths
40
40
revision_id
stringlengths
40
40
branch_name
stringclasses
6 values
visit_date
int64
1,471B
1,694B
revision_date
int64
1,378B
1,694B
committer_date
int64
1,378B
1,694B
github_id
float64
1.33M
604M
star_events_count
int64
0
43.5k
fork_events_count
int64
0
1.5k
gha_license_id
stringclasses
6 values
gha_event_created_at
int64
1,402B
1,695B
gha_created_at
int64
1,359B
1,637B
gha_language
stringclasses
19 values
src_encoding
stringclasses
2 values
language
stringclasses
1 value
is_vendor
bool
1 class
is_generated
bool
1 class
length_bytes
int64
3
6.4M
extension
stringclasses
4 values
content
stringlengths
3
6.12M
840580d45cb771fa7eb50ba1f055a5428ef6e5ad
83c8119e3298c0bfc53fc195c41a6afb63d01513
/library/data/stream.lean
68d29dbf1761694591926fba78af38d174b09d91
[ "Apache-2.0" ]
permissive
anfelor/lean
584b91c4e87a6d95f7630c2a93fb082a87319ed0
31cfc2b6bf7d674f3d0f73848b842c9c9869c9f1
refs/heads/master
1,610,067,141,310
1,585,992,232,000
1,585,992,232,000
251,683,543
0
0
Apache-2.0
1,585,676,570,000
1,585,676,569,000
null
UTF-8
Lean
false
false
21,693
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ open nat function option universes u v w def stream (α : Type u) := nat → α namespace stream variables {α : Type u} {β : Type v} {δ : Type w} def cons (a : α) (s : stream α) : stream α := λ i, match i with | 0 := a | succ n := s n end notation h :: t := cons h t @[reducible] def head (s : stream α) : α := s 0 def tail (s : stream α) : stream α := λ i, s (i+1) def drop (n : nat) (s : stream α) : stream α := λ i, s (i+n) @[reducible] def nth (n : nat) (s : stream α) : α := s n protected theorem eta (s : stream α) : head s :: tail s = s := funext (λ i, begin cases i; refl end) theorem nth_zero_cons (a : α) (s : stream α) : nth 0 (a :: s) = a := rfl theorem head_cons (a : α) (s : stream α) : head (a :: s) = a := rfl theorem tail_cons (a : α) (s : stream α) : tail (a :: s) = s := rfl theorem tail_drop (n : nat) (s : stream α) : tail (drop n s) = drop n (tail s) := funext (λ i, begin unfold tail drop, simp [add_comm, add_left_comm] end) theorem nth_drop (n m : nat) (s : stream α) : nth n (drop m s) = nth (n+m) s := rfl theorem tail_eq_drop (s : stream α) : tail s = drop 1 s := rfl theorem drop_drop (n m : nat) (s : stream α) : drop n (drop m s) = drop (n+m) s := funext (λ i, begin unfold drop, rw add_assoc end) theorem nth_succ (n : nat) (s : stream α) : nth (succ n) s = nth n (tail s) := rfl theorem drop_succ (n : nat) (s : stream α) : drop (succ n) s = drop n (tail s) := rfl protected theorem ext {s₁ s₂ : stream α} : (∀ n, nth n s₁ = nth n s₂) → s₁ = s₂ := assume h, funext h def all (p : α → Prop) (s : stream α) := ∀ n, p (nth n s) def any (p : α → Prop) (s : stream α) := ∃ n, p (nth n s) theorem all_def (p : α → Prop) (s : stream α) : all p s = ∀ n, p (nth n s) := rfl theorem any_def (p : α → Prop) (s : stream α) : any p s = ∃ n, p (nth n s) := rfl protected def mem (a : α) (s : stream α) := any (λ b, a = b) s instance : has_mem α (stream α) := ⟨stream.mem⟩ theorem mem_cons (a : α) (s : stream α) : a ∈ (a::s) := exists.intro 0 rfl theorem mem_cons_of_mem {a : α} {s : stream α} (b : α) : a ∈ s → a ∈ b :: s := assume ⟨n, h⟩, exists.intro (succ n) (by rw [nth_succ, tail_cons, h]) theorem eq_or_mem_of_mem_cons {a b : α} {s : stream α} : a ∈ b::s → a = b ∨ a ∈ s := assume ⟨n, h⟩, begin cases n with n', {left, exact h}, {right, rw [nth_succ, tail_cons] at h, exact ⟨n', h⟩} end theorem mem_of_nth_eq {n : nat} {s : stream α} {a : α} : a = nth n s → a ∈ s := assume h, exists.intro n h section map variable (f : α → β) def map (s : stream α) : stream β := λ n, f (nth n s) theorem drop_map (n : nat) (s : stream α) : drop n (map f s) = map f (drop n s) := stream.ext (λ i, rfl) theorem nth_map (n : nat) (s : stream α) : nth n (map f s) = f (nth n s) := rfl theorem tail_map (s : stream α) : tail (map f s) = map f (tail s) := begin rw tail_eq_drop, refl end theorem head_map (s : stream α) : head (map f s) = f (head s) := rfl theorem map_eq (s : stream α) : map f s = f (head s) :: map f (tail s) := by rw [← stream.eta (map f s), tail_map, head_map] theorem map_cons (a : α) (s : stream α) : map f (a :: s) = f a :: map f s := begin rw [← stream.eta (map f (a :: s)), map_eq], refl end theorem map_id (s : stream α) : map id s = s := rfl theorem map_map (g : β → δ) (f : α → β) (s : stream α) : map g (map f s) = map (g ∘ f) s := rfl theorem map_tail (s : stream α) : map f (tail s) = tail (map f s) := rfl theorem mem_map {a : α} {s : stream α} : a ∈ s → f a ∈ map f s := assume ⟨n, h⟩, exists.intro n (by rw [nth_map, h]) theorem exists_of_mem_map {f} {b : β} {s : stream α} : b ∈ map f s → ∃ a, a ∈ s ∧ f a = b := assume ⟨n, h⟩, ⟨nth n s, ⟨n, rfl⟩, h.symm⟩ end map section zip variable (f : α → β → δ) def zip (s₁ : stream α) (s₂ : stream β) : stream δ := λ n, f (nth n s₁) (nth n s₂) theorem drop_zip (n : nat) (s₁ : stream α) (s₂ : stream β) : drop n (zip f s₁ s₂) = zip f (drop n s₁) (drop n s₂) := stream.ext (λ i, rfl) theorem nth_zip (n : nat) (s₁ : stream α) (s₂ : stream β) : nth n (zip f s₁ s₂) = f (nth n s₁) (nth n s₂) := rfl theorem head_zip (s₁ : stream α) (s₂ : stream β) : head (zip f s₁ s₂) = f (head s₁) (head s₂) := rfl theorem tail_zip (s₁ : stream α) (s₂ : stream β) : tail (zip f s₁ s₂) = zip f (tail s₁) (tail s₂) := rfl theorem zip_eq (s₁ : stream α) (s₂ : stream β) : zip f s₁ s₂ = f (head s₁) (head s₂) :: zip f (tail s₁) (tail s₂) := begin rw [← stream.eta (zip f s₁ s₂)], refl end end zip def const (a : α) : stream α := λ n, a theorem mem_const (a : α) : a ∈ const a := exists.intro 0 rfl theorem const_eq (a : α) : const a = a :: const a := begin apply stream.ext, intro n, cases n; refl end theorem tail_const (a : α) : tail (const a) = const a := suffices tail (a :: const a) = const a, by rwa [← const_eq] at this, rfl theorem map_const (f : α → β) (a : α) : map f (const a) = const (f a) := rfl theorem nth_const (n : nat) (a : α) : nth n (const a) = a := rfl theorem drop_const (n : nat) (a : α) : drop n (const a) = const a := stream.ext (λ i, rfl) def iterate (f : α → α) (a : α) : stream α := λ n, nat.rec_on n a (λ n r, f r) theorem head_iterate (f : α → α) (a : α) : head (iterate f a) = a := rfl theorem tail_iterate (f : α → α) (a : α) : tail (iterate f a) = iterate f (f a) := begin funext n, induction n with n' ih, {refl}, {unfold tail iterate, unfold tail iterate at ih, rw add_one at ih, dsimp at ih, rw add_one, dsimp, rw ih} end theorem iterate_eq (f : α → α) (a : α) : iterate f a = a :: iterate f (f a) := begin rw [← stream.eta (iterate f a)], rw tail_iterate, refl end theorem nth_zero_iterate (f : α → α) (a : α) : nth 0 (iterate f a) = a := rfl theorem nth_succ_iterate (n : nat) (f : α → α) (a : α) : nth (succ n) (iterate f a) = nth n (iterate f (f a)) := by rw [nth_succ, tail_iterate] section bisim variable (R : stream α → stream α → Prop) local infix ~ := R def is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → head s₁ = head s₂ ∧ tail s₁ ~ tail s₂ theorem nth_of_bisim (bisim : is_bisimulation R) : ∀ {s₁ s₂} n, s₁ ~ s₂ → nth n s₁ = nth n s₂ ∧ drop (n+1) s₁ ~ drop (n+1) s₂ | s₁ s₂ 0 h := bisim h | s₁ s₂ (n+1) h := match bisim h with | ⟨h₁, trel⟩ := nth_of_bisim n trel end -- If two streams are bisimilar, then they are equal theorem eq_of_bisim (bisim : is_bisimulation R) : ∀ {s₁ s₂}, s₁ ~ s₂ → s₁ = s₂ := λ s₁ s₂ r, stream.ext (λ n, and.elim_left (nth_of_bisim R bisim n r)) end bisim theorem bisim_simple (s₁ s₂ : stream α) : head s₁ = head s₂ → s₁ = tail s₁ → s₂ = tail s₂ → s₁ = s₂ := assume hh ht₁ ht₂, eq_of_bisim (λ s₁ s₂, head s₁ = head s₂ ∧ s₁ = tail s₁ ∧ s₂ = tail s₂) (λ s₁ s₂ ⟨h₁, h₂, h₃⟩, begin constructor, exact h₁, rw [← h₂, ← h₃], repeat { constructor }; assumption end) (and.intro hh (and.intro ht₁ ht₂)) theorem coinduction {s₁ s₂ : stream α} : head s₁ = head s₂ → (∀ (β : Type u) (fr : stream α → β), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) → s₁ = s₂ := assume hh ht, eq_of_bisim (λ s₁ s₂, head s₁ = head s₂ ∧ ∀ (β : Type u) (fr : stream α → β), fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) (λ s₁ s₂ h, have h₁ : head s₁ = head s₂, from and.elim_left h, have h₂ : head (tail s₁) = head (tail s₂), from and.elim_right h α (@head α) h₁, have h₃ : ∀ (β : Type u) (fr : stream α → β), fr (tail s₁) = fr (tail s₂) → fr (tail (tail s₁)) = fr (tail (tail s₂)), from λ β fr, and.elim_right h β (λ s, fr (tail s)), and.intro h₁ (and.intro h₂ h₃)) (and.intro hh ht) theorem iterate_id (a : α) : iterate id a = const a := coinduction rfl (λ β fr ch, begin rw [tail_iterate, tail_const], exact ch end) local attribute [reducible] stream theorem map_iterate (f : α → α) (a : α) : iterate f (f a) = map f (iterate f a) := begin funext n, induction n with n' ih, {refl}, { unfold map iterate nth, dsimp, unfold map iterate nth at ih, dsimp at ih, rw ih } end section corec def corec (f : α → β) (g : α → α) : α → stream β := λ a, map f (iterate g a) def corec_on (a : α) (f : α → β) (g : α → α) : stream β := corec f g a theorem corec_def (f : α → β) (g : α → α) (a : α) : corec f g a = map f (iterate g a) := rfl theorem corec_eq (f : α → β) (g : α → α) (a : α) : corec f g a = f a :: corec f g (g a) := begin rw [corec_def, map_eq, head_iterate, tail_iterate], refl end theorem corec_id_id_eq_const (a : α) : corec id id a = const a := by rw [corec_def, map_id, iterate_id] theorem corec_id_f_eq_iterate (f : α → α) (a : α) : corec id f a = iterate f a := rfl end corec section corec' def corec' (f : α → β × α) : α → stream β := corec (prod.fst ∘ f) (prod.snd ∘ f) theorem corec'_eq (f : α → β × α) (a : α) : corec' f a = (f a).1 :: corec' f (f a).2 := corec_eq _ _ _ end corec' -- corec is also known as unfold def unfolds (g : α → β) (f : α → α) (a : α) : stream β := corec g f a theorem unfolds_eq (g : α → β) (f : α → α) (a : α) : unfolds g f a = g a :: unfolds g f (f a) := begin unfold unfolds, rw [corec_eq] end theorem nth_unfolds_head_tail : ∀ (n : nat) (s : stream α), nth n (unfolds head tail s) = nth n s := begin intro n, induction n with n' ih, {intro s, refl}, {intro s, rw [nth_succ, nth_succ, unfolds_eq, tail_cons, ih]} end theorem unfolds_head_eq : ∀ (s : stream α), unfolds head tail s = s := λ s, stream.ext (λ n, nth_unfolds_head_tail n s) def interleave (s₁ s₂ : stream α) : stream α := corec_on (s₁, s₂) (λ ⟨s₁, s₂⟩, head s₁) (λ ⟨s₁, s₂⟩, (s₂, tail s₁)) infix `⋈`:65 := interleave theorem interleave_eq (s₁ s₂ : stream α) : s₁ ⋈ s₂ = head s₁ :: head s₂ :: (tail s₁ ⋈ tail s₂) := begin unfold interleave corec_on, rw corec_eq, dsimp, rw corec_eq, refl end theorem tail_interleave (s₁ s₂ : stream α) : tail (s₁ ⋈ s₂) = s₂ ⋈ (tail s₁) := begin unfold interleave corec_on, rw corec_eq, refl end theorem interleave_tail_tail (s₁ s₂ : stream α) : tail s₁ ⋈ tail s₂ = tail (tail (s₁ ⋈ s₂)) := begin rw [interleave_eq s₁ s₂], refl end theorem nth_interleave_left : ∀ (n : nat) (s₁ s₂ : stream α), nth (2*n) (s₁ ⋈ s₂) = nth n s₁ | 0 s₁ s₂ := rfl | (succ n) s₁ s₂ := begin change nth (succ (succ (2*n))) (s₁ ⋈ s₂) = nth (succ n) s₁, rw [nth_succ, nth_succ, interleave_eq, tail_cons, tail_cons, nth_interleave_left], refl end theorem nth_interleave_right : ∀ (n : nat) (s₁ s₂ : stream α), nth (2*n+1) (s₁ ⋈ s₂) = nth n s₂ | 0 s₁ s₂ := rfl | (succ n) s₁ s₂ := begin change nth (succ (succ (2*n+1))) (s₁ ⋈ s₂) = nth (succ n) s₂, rw [nth_succ, nth_succ, interleave_eq, tail_cons, tail_cons, nth_interleave_right], refl end theorem mem_interleave_left {a : α} {s₁ : stream α} (s₂ : stream α) : a ∈ s₁ → a ∈ s₁ ⋈ s₂ := assume ⟨n, h⟩, exists.intro (2*n) (by rw [h, nth_interleave_left]) theorem mem_interleave_right {a : α} {s₁ : stream α} (s₂ : stream α) : a ∈ s₂ → a ∈ s₁ ⋈ s₂ := assume ⟨n, h⟩, exists.intro (2*n+1) (by rw [h, nth_interleave_right]) def even (s : stream α) : stream α := corec (λ s, head s) (λ s, tail (tail s)) s def odd (s : stream α) : stream α := even (tail s) theorem odd_eq (s : stream α) : odd s = even (tail s) := rfl theorem head_even (s : stream α) : head (even s) = head s := rfl theorem tail_even (s : stream α) : tail (even s) = even (tail (tail s)) := begin unfold even, rw corec_eq, refl end theorem even_cons_cons (a₁ a₂ : α) (s : stream α) : even (a₁ :: a₂ :: s) = a₁ :: even s := begin unfold even, rw corec_eq, refl end theorem even_tail (s : stream α) : even (tail s) = odd s := rfl theorem even_interleave (s₁ s₂ : stream α) : even (s₁ ⋈ s₂) = s₁ := eq_of_bisim (λ s₁' s₁, ∃ s₂, s₁' = even (s₁ ⋈ s₂)) (λ s₁' s₁ ⟨s₂, h₁⟩, begin rw h₁, constructor, {refl}, {exact ⟨tail s₂, by rw [interleave_eq, even_cons_cons, tail_cons]⟩} end) (exists.intro s₂ rfl) theorem interleave_even_odd (s₁ : stream α) : even s₁ ⋈ odd s₁ = s₁ := eq_of_bisim (λ s' s, s' = even s ⋈ odd s) (λ s' s (h : s' = even s ⋈ odd s), begin rw h, constructor, {refl}, {simp [odd_eq, odd_eq, tail_interleave, tail_even]} end) rfl theorem nth_even : ∀ (n : nat) (s : stream α), nth n (even s) = nth (2*n) s | 0 s := rfl | (succ n) s := begin change nth (succ n) (even s) = nth (succ (succ (2 * n))) s, rw [nth_succ, nth_succ, tail_even, nth_even], refl end theorem nth_odd : ∀ (n : nat) (s : stream α), nth n (odd s) = nth (2*n + 1) s := λ n s, begin rw [odd_eq, nth_even], refl end theorem mem_of_mem_even (a : α) (s : stream α) : a ∈ even s → a ∈ s := assume ⟨n, h⟩, exists.intro (2*n) (by rw [h, nth_even]) theorem mem_of_mem_odd (a : α) (s : stream α) : a ∈ odd s → a ∈ s := assume ⟨n, h⟩, exists.intro (2*n+1) (by rw [h, nth_odd]) def append_stream : list α → stream α → stream α | [] s := s | (list.cons a l) s := a :: append_stream l s theorem nil_append_stream (s : stream α) : append_stream [] s = s := rfl theorem cons_append_stream (a : α) (l : list α) (s : stream α) : append_stream (a::l) s = a :: append_stream l s := rfl infix `++ₛ`:65 := append_stream theorem append_append_stream : ∀ (l₁ l₂ : list α) (s : stream α), (l₁ ++ l₂) ++ₛ s = l₁ ++ₛ (l₂ ++ₛ s) | [] l₂ s := rfl | (list.cons a l₁) l₂ s := by rw [list.cons_append, cons_append_stream, cons_append_stream, append_append_stream] theorem map_append_stream (f : α → β) : ∀ (l : list α) (s : stream α), map f (l ++ₛ s) = list.map f l ++ₛ map f s | [] s := rfl | (list.cons a l) s := by rw [cons_append_stream, list.map_cons, map_cons, cons_append_stream, map_append_stream] theorem drop_append_stream : ∀ (l : list α) (s : stream α), drop l.length (l ++ₛ s) = s | [] s := by refl | (list.cons a l) s := by rw [list.length_cons, add_one, drop_succ, cons_append_stream, tail_cons, drop_append_stream] theorem append_stream_head_tail (s : stream α) : [head s] ++ₛ tail s = s := by rw [cons_append_stream, nil_append_stream, stream.eta] theorem mem_append_stream_right : ∀ {a : α} (l : list α) {s : stream α}, a ∈ s → a ∈ l ++ₛ s | a [] s h := h | a (list.cons b l) s h := have ih : a ∈ l ++ₛ s, from mem_append_stream_right l h, mem_cons_of_mem _ ih theorem mem_append_stream_left : ∀ {a : α} {l : list α} (s : stream α), a ∈ l → a ∈ l ++ₛ s | a [] s h := absurd h (list.not_mem_nil _) | a (list.cons b l) s h := or.elim (list.eq_or_mem_of_mem_cons h) (λ (aeqb : a = b), exists.intro 0 aeqb) (λ (ainl : a ∈ l), mem_cons_of_mem b (mem_append_stream_left s ainl)) def approx : nat → stream α → list α | 0 s := [] | (n+1) s := list.cons (head s) (approx n (tail s)) theorem approx_zero (s : stream α) : approx 0 s = [] := rfl theorem approx_succ (n : nat) (s : stream α) : approx (succ n) s = head s :: approx n (tail s) := rfl theorem nth_approx : ∀ (n : nat) (s : stream α), list.nth (approx (succ n) s) n = some (nth n s) | 0 s := rfl | (n+1) s := begin rw [approx_succ, add_one, list.nth, nth_approx], refl end theorem append_approx_drop : ∀ (n : nat) (s : stream α), append_stream (approx n s) (drop n s) = s := begin intro n, induction n with n' ih, {intro s, refl}, {intro s, rw [approx_succ, drop_succ, cons_append_stream, ih (tail s), stream.eta]} end -- Take theorem reduces a proof of equality of infinite streams to an -- induction over all their finite approximations. theorem take_theorem (s₁ s₂ : stream α) : (∀ (n : nat), approx n s₁ = approx n s₂) → s₁ = s₂ := begin intro h, apply stream.ext, intro n, induction n with n ih, { have aux := h 1, simp [approx] at aux, exact aux }, { have h₁ : some (nth (succ n) s₁) = some (nth (succ n) s₂), { rw [← nth_approx, ← nth_approx, h (succ (succ n))] }, injection h₁ } end -- auxiliary def for cycle corecursive def private def cycle_f : α × list α × α × list α → α | (v, _, _, _) := v -- auxiliary def for cycle corecursive def private def cycle_g : α × list α × α × list α → α × list α × α × list α | (v₁, [], v₀, l₀) := (v₀, l₀, v₀, l₀) | (v₁, list.cons v₂ l₂, v₀, l₀) := (v₂, l₂, v₀, l₀) private lemma cycle_g_cons (a : α) (a₁ : α) (l₁ : list α) (a₀ : α) (l₀ : list α) : cycle_g (a, a₁::l₁, a₀, l₀) = (a₁, l₁, a₀, l₀) := rfl def cycle : Π (l : list α), l ≠ [] → stream α | [] h := absurd rfl h | (list.cons a l) h := corec cycle_f cycle_g (a, l, a, l) theorem cycle_eq : ∀ (l : list α) (h : l ≠ []), cycle l h = l ++ₛ cycle l h | [] h := absurd rfl h | (list.cons a l) h := have gen : ∀ l' a', corec cycle_f cycle_g (a', l', a, l) = (a' :: l') ++ₛ corec cycle_f cycle_g (a, l, a, l), begin intro l', induction l' with a₁ l₁ ih, {intros, rw [corec_eq], refl}, {intros, rw [corec_eq, cycle_g_cons, ih a₁], refl} end, gen l a theorem mem_cycle {a : α} {l : list α} : ∀ (h : l ≠ []), a ∈ l → a ∈ cycle l h := assume h ainl, begin rw [cycle_eq], exact mem_append_stream_left _ ainl end theorem cycle_singleton (a : α) (h : [a] ≠ []) : cycle [a] h = const a := coinduction rfl (λ β fr ch, by rwa [cycle_eq, const_eq]) def tails (s : stream α) : stream (stream α) := corec id tail (tail s) theorem tails_eq (s : stream α) : tails s = tail s :: tails (tail s) := by unfold tails; rw [corec_eq]; refl theorem nth_tails : ∀ (n : nat) (s : stream α), nth n (tails s) = drop n (tail s) := begin intro n, induction n with n' ih, {intros, refl}, {intro s, rw [nth_succ, drop_succ, tails_eq, tail_cons, ih]} end theorem tails_eq_iterate (s : stream α) : tails s = iterate tail (tail s) := rfl def inits_core (l : list α) (s : stream α) : stream (list α) := corec_on (l, s) (λ ⟨a, b⟩, a) (λ p, match p with (l', s') := (l' ++ [head s'], tail s') end) def inits (s : stream α) : stream (list α) := inits_core [head s] (tail s) theorem inits_core_eq (l : list α) (s : stream α) : inits_core l s = l :: inits_core (l ++ [head s]) (tail s) := begin unfold inits_core corec_on, rw [corec_eq], refl end theorem tail_inits (s : stream α) : tail (inits s) = inits_core [head s, head (tail s)] (tail (tail s)) := begin unfold inits, rw inits_core_eq, refl end theorem inits_tail (s : stream α) : inits (tail s) = inits_core [head (tail s)] (tail (tail s)) := rfl theorem cons_nth_inits_core : ∀ (a : α) (n : nat) (l : list α) (s : stream α), a :: nth n (inits_core l s) = nth n (inits_core (a::l) s) := begin intros a n, induction n with n' ih, {intros, refl}, {intros l s, rw [nth_succ, inits_core_eq, tail_cons, ih, inits_core_eq (a::l) s], refl } end theorem nth_inits : ∀ (n : nat) (s : stream α), nth n (inits s) = approx (succ n) s := begin intro n, induction n with n' ih, {intros, refl}, {intros, rw [nth_succ, approx_succ, ← ih, tail_inits, inits_tail, cons_nth_inits_core]} end theorem inits_eq (s : stream α) : inits s = [head s] :: map (list.cons (head s)) (inits (tail s)) := begin apply stream.ext, intro n, cases n, {refl}, {rw [nth_inits, nth_succ, tail_cons, nth_map, nth_inits], refl} end theorem zip_inits_tails (s : stream α) : zip append_stream (inits s) (tails s) = const s := begin apply stream.ext, intro n, rw [nth_zip, nth_inits, nth_tails, nth_const, approx_succ, cons_append_stream, append_approx_drop, stream.eta] end def pure (a : α) : stream α := const a def apply (f : stream (α → β)) (s : stream α) : stream β := λ n, (nth n f) (nth n s) infix `⊛`:75 := apply -- input as \o* theorem identity (s : stream α) : pure id ⊛ s = s := rfl theorem composition (g : stream (β → δ)) (f : stream (α → β)) (s : stream α) : pure comp ⊛ g ⊛ f ⊛ s = g ⊛ (f ⊛ s) := rfl theorem homomorphism (f : α → β) (a : α) : pure f ⊛ pure a = pure (f a) := rfl theorem interchange (fs : stream (α → β)) (a : α) : fs ⊛ pure a = pure (λ f : α → β, f a) ⊛ fs := rfl theorem map_eq_apply (f : α → β) (s : stream α) : map f s = pure f ⊛ s := rfl def nats : stream nat := λ n, n theorem nth_nats (n : nat) : nth n nats = n := rfl theorem nats_eq : nats = 0 :: map succ nats := begin apply stream.ext, intro n, cases n, refl, rw [nth_succ], refl end end stream
2fb92b1357022e25ed762fc29de3496afc1da2dd
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/init/ordering.lean
e9ed1634808056c33865f190dc2a82f0fe4d8d72
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
2,041
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.to_string init.prod init.sum inductive ordering | lt | eq | gt open ordering attribute [instance] definition ordering.has_to_string : has_to_string ordering := has_to_string.mk (λ s, match s with | ordering.lt := "lt" | ordering.eq := "eq" | ordering.gt := "gt" end) structure [class] has_ordering (A : Type) := (cmp : A → A → ordering) definition nat.cmp (a b : nat) : ordering := if a < b then ordering.lt else if a = b then ordering.eq else ordering.gt attribute [instance] definition nat_has_ordering : has_ordering nat := ⟨nat.cmp⟩ section open prod variables {A B : Type} [has_ordering A] [has_ordering B] definition prod.cmp : A × B → A × B → ordering | (a₁, b₁) (a₂, b₂) := match (has_ordering.cmp a₁ a₂) with | ordering.lt := lt | ordering.eq := has_ordering.cmp b₁ b₂ | ordering.gt := gt end attribute [instance] definition prod_has_ordering {A B : Type} [has_ordering A] [has_ordering B] : has_ordering (A × B) := ⟨prod.cmp⟩ end section open sum variables {A B : Type} [has_ordering A] [has_ordering B] definition sum.cmp : A ⊕ B → A ⊕ B → ordering | (inl a₁) (inl a₂) := has_ordering.cmp a₁ a₂ | (inr b₁) (inr b₂) := has_ordering.cmp b₁ b₂ | (inl a₁) (inr b₂) := lt | (inr b₁) (inl a₂) := gt attribute [instance] definition sum_has_ordering {A B : Type} [has_ordering A] [has_ordering B] : has_ordering (A ⊕ B) := ⟨sum.cmp⟩ end section open option variables {A : Type} [has_ordering A] definition option.cmp : option A → option A → ordering | (some a₁) (some a₂) := has_ordering.cmp a₁ a₂ | (some a₁) none := gt | none (some a₂) := lt | none none := eq attribute [instance] definition option_has_ordering {A : Type} [has_ordering A] : has_ordering (option A) := ⟨option.cmp⟩ end
9ed0a480865bafdf2a8b9d3c3a38319d48d2eca5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/order/ring/abs.lean
05e6c1a7efc1125b062e08ef0c1be9313bdbca13
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
3,577
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import algebra.order.ring.defs import algebra.ring.divisibility import algebra.order.group.abs /-! # Absolute values in linear ordered rings. -/ variables {α : Type*} section linear_ordered_ring variables [linear_ordered_ring α] {a b c : α} @[simp] lemma abs_one : |(1 : α)| = 1 := abs_of_pos zero_lt_one @[simp] lemma abs_two : |(2 : α)| = 2 := abs_of_pos zero_lt_two lemma abs_mul (a b : α) : |a * b| = |a| * |b| := begin rw [abs_eq (mul_nonneg (abs_nonneg a) (abs_nonneg b))], cases le_total a 0 with ha ha; cases le_total b 0 with hb hb; simp only [abs_of_nonpos, abs_of_nonneg, true_or, or_true, eq_self_iff_true, neg_mul, mul_neg, neg_neg, *] end /-- `abs` as a `monoid_with_zero_hom`. -/ def abs_hom : α →*₀ α := ⟨abs, abs_zero, abs_one, abs_mul⟩ @[simp] lemma abs_mul_abs_self (a : α) : |a| * |a| = a * a := abs_by_cases (λ x, x * x = a * a) rfl (neg_mul_neg a a) @[simp] lemma abs_mul_self (a : α) : |a * a| = a * a := by rw [abs_mul, abs_mul_abs_self] @[simp] lemma abs_eq_self : |a| = a ↔ 0 ≤ a := by simp [abs_eq_max_neg] @[simp] lemma abs_eq_neg_self : |a| = -a ↔ a ≤ 0 := by simp [abs_eq_max_neg] /-- For an element `a` of a linear ordered ring, either `abs a = a` and `0 ≤ a`, or `abs a = -a` and `a < 0`. Use cases on this lemma to automate linarith in inequalities -/ lemma abs_cases (a : α) : (|a| = a ∧ 0 ≤ a) ∨ (|a| = -a ∧ a < 0) := begin by_cases 0 ≤ a, { left, exact ⟨abs_eq_self.mpr h, h⟩ }, { right, push_neg at h, exact ⟨abs_eq_neg_self.mpr (le_of_lt h), h⟩ } end @[simp] lemma max_zero_add_max_neg_zero_eq_abs_self (a : α) : max a 0 + max (-a) 0 = |a| := begin symmetry, rcases le_total 0 a with ha|ha; simp [ha], end lemma abs_eq_iff_mul_self_eq : |a| = |b| ↔ a * a = b * b := begin rw [← abs_mul_abs_self, ← abs_mul_abs_self b], exact (mul_self_inj (abs_nonneg a) (abs_nonneg b)).symm, end lemma abs_lt_iff_mul_self_lt : |a| < |b| ↔ a * a < b * b := begin rw [← abs_mul_abs_self, ← abs_mul_abs_self b], exact mul_self_lt_mul_self_iff (abs_nonneg a) (abs_nonneg b) end lemma abs_le_iff_mul_self_le : |a| ≤ |b| ↔ a * a ≤ b * b := begin rw [← abs_mul_abs_self, ← abs_mul_abs_self b], exact mul_self_le_mul_self_iff (abs_nonneg a) (abs_nonneg b) end lemma abs_le_one_iff_mul_self_le_one : |a| ≤ 1 ↔ a * a ≤ 1 := by simpa only [abs_one, one_mul] using @abs_le_iff_mul_self_le α _ a 1 end linear_ordered_ring section linear_ordered_comm_ring variables [linear_ordered_comm_ring α] {a b c d : α} lemma abs_sub_sq (a b : α) : |a - b| * |a - b| = a * a + b * b - (1 + 1) * a * b := begin rw abs_mul_abs_self, simp only [mul_add, add_comm, add_left_comm, mul_comm, sub_eq_add_neg, mul_one, mul_neg, neg_add_rev, neg_neg], end end linear_ordered_comm_ring section variables [ring α] [linear_order α] {a b : α} @[simp] lemma abs_dvd (a b : α) : |a| ∣ b ↔ a ∣ b := by { cases abs_choice a with h h; simp only [h, neg_dvd] } lemma abs_dvd_self (a : α) : |a| ∣ a := (abs_dvd a a).mpr (dvd_refl a) @[simp] lemma dvd_abs (a b : α) : a ∣ |b| ↔ a ∣ b := by { cases abs_choice b with h h; simp only [h, dvd_neg] } lemma self_dvd_abs (a : α) : a ∣ |a| := (dvd_abs a a).mpr (dvd_refl a) lemma abs_dvd_abs (a b : α) : |a| ∣ |b| ↔ a ∣ b := (abs_dvd _ _).trans (dvd_abs _ _) end
556490eda02dc03b4b3a3fff869d7daeca42799d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/prod.lean
5e90c5d6079131ee86fa7560a7ea20ab2dd37b07
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,789
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.basic import Mathlib.PostPort universes u_1 u_2 u_3 u_4 u_5 u_6 namespace Mathlib /-! # Extra facts about `prod` This file defines `prod.swap : α × β → β × α` and proves various simple lemmas about `prod`. -/ @[simp] theorem prod_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : α → γ) (g : β → δ) (p : α × β) : prod.map f g p = (f (prod.fst p), g (prod.snd p)) := rfl namespace prod @[simp] theorem forall {α : Type u_1} {β : Type u_2} {p : α × β → Prop} : (∀ (x : α × β), p x) ↔ ∀ (a : α) (b : β), p (a, b) := sorry @[simp] theorem exists {α : Type u_1} {β : Type u_2} {p : α × β → Prop} : (∃ (x : α × β), p x) ↔ ∃ (a : α), ∃ (b : β), p (a, b) := sorry theorem forall' {α : Type u_1} {β : Type u_2} {p : α → β → Prop} : (∀ (x : α × β), p (fst x) (snd x)) ↔ ∀ (a : α) (b : β), p a b := forall theorem exists' {α : Type u_1} {β : Type u_2} {p : α → β → Prop} : (∃ (x : α × β), p (fst x) (snd x)) ↔ ∃ (a : α), ∃ (b : β), p a b := exists @[simp] theorem map_mk {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : α → γ) (g : β → δ) (a : α) (b : β) : map f g (a, b) = (f a, g b) := rfl theorem map_fst {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : α → γ) (g : β → δ) (p : α × β) : fst (map f g p) = f (fst p) := rfl theorem map_snd {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : α → γ) (g : β → δ) (p : α × β) : snd (map f g p) = g (snd p) := rfl theorem map_fst' {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : α → γ) (g : β → δ) : fst ∘ map f g = f ∘ fst := funext (map_fst f g) theorem map_snd' {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : α → γ) (g : β → δ) : snd ∘ map f g = g ∘ snd := funext (map_snd f g) /-- Composing a `prod.map` with another `prod.map` is equal to a single `prod.map` of composed functions. -/ theorem map_comp_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {ε : Type u_5} {ζ : Type u_6} (f : α → β) (f' : γ → δ) (g : β → ε) (g' : δ → ζ) : map g g' ∘ map f f' = map (g ∘ f) (g' ∘ f') := rfl /-- Composing a `prod.map` with another `prod.map` is equal to a single `prod.map` of composed functions, fully applied. -/ theorem map_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {ε : Type u_5} {ζ : Type u_6} (f : α → β) (f' : γ → δ) (g : β → ε) (g' : δ → ζ) (x : α × γ) : map g g' (map f f' x) = map (g ∘ f) (g' ∘ f') x := rfl @[simp] theorem mk.inj_iff {α : Type u_1} {β : Type u_2} {a₁ : α} {a₂ : α} {b₁ : β} {b₂ : β} : (a₁, b₁) = (a₂, b₂) ↔ a₁ = a₂ ∧ b₁ = b₂ := sorry theorem mk.inj_left {α : Type u_1} {β : Type u_2} (a : α) : function.injective (Prod.mk a) := sorry theorem mk.inj_right {α : Type u_1} {β : Type u_2} (b : β) : function.injective fun (a : α) => (a, b) := sorry theorem ext_iff {α : Type u_1} {β : Type u_2} {p : α × β} {q : α × β} : p = q ↔ fst p = fst q ∧ snd p = snd q := sorry theorem ext {α : Type u_1} {β : Type u_2} {p : α × β} {q : α × β} (h₁ : fst p = fst q) (h₂ : snd p = snd q) : p = q := iff.mpr ext_iff { left := h₁, right := h₂ } theorem map_def {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {f : α → γ} {g : β → δ} : map f g = fun (p : α × β) => (f (fst p), g (snd p)) := funext fun (p : α × β) => ext (map_fst f g p) (map_snd f g p) theorem id_prod {α : Type u_1} : (fun (p : α × α) => (fst p, snd p)) = id := sorry theorem fst_surjective {α : Type u_1} {β : Type u_2} [h : Nonempty β] : function.surjective fst := fun (x : α) => nonempty.elim h fun (y : β) => Exists.intro (x, y) rfl theorem snd_surjective {α : Type u_1} {β : Type u_2} [h : Nonempty α] : function.surjective snd := fun (y : β) => nonempty.elim h fun (x : α) => Exists.intro (x, y) rfl theorem fst_injective {α : Type u_1} {β : Type u_2} [subsingleton β] : function.injective fst := fun (x y : α × β) (h : fst x = fst y) => ext h (subsingleton.elim (snd x) (snd y)) theorem snd_injective {α : Type u_1} {β : Type u_2} [subsingleton α] : function.injective snd := fun (x y : α × β) (h : snd x = snd y) => ext (subsingleton.elim (fst x) (fst y)) h /-- Swap the factors of a product. `swap (a, b) = (b, a)` -/ def swap {α : Type u_1} {β : Type u_2} : α × β → β × α := fun (p : α × β) => (snd p, fst p) @[simp] theorem swap_swap {α : Type u_1} {β : Type u_2} (x : α × β) : swap (swap x) = x := cases_on x fun (x_fst : α) (x_snd : β) => idRhs (swap (swap (x_fst, x_snd)) = swap (swap (x_fst, x_snd))) rfl @[simp] theorem fst_swap {α : Type u_1} {β : Type u_2} {p : α × β} : fst (swap p) = snd p := rfl @[simp] theorem snd_swap {α : Type u_1} {β : Type u_2} {p : α × β} : snd (swap p) = fst p := rfl @[simp] theorem swap_prod_mk {α : Type u_1} {β : Type u_2} {a : α} {b : β} : swap (a, b) = (b, a) := rfl @[simp] theorem swap_swap_eq {α : Type u_1} {β : Type u_2} : swap ∘ swap = id := funext swap_swap @[simp] theorem swap_left_inverse {α : Type u_1} {β : Type u_2} : function.left_inverse swap swap := swap_swap @[simp] theorem swap_right_inverse {α : Type u_1} {β : Type u_2} : function.right_inverse swap swap := swap_swap theorem swap_injective {α : Type u_1} {β : Type u_2} : function.injective swap := function.left_inverse.injective swap_left_inverse theorem swap_surjective {α : Type u_1} {β : Type u_2} : function.surjective swap := function.left_inverse.surjective swap_left_inverse theorem swap_bijective {α : Type u_1} {β : Type u_2} : function.bijective swap := { left := swap_injective, right := swap_surjective } @[simp] theorem swap_inj {α : Type u_1} {β : Type u_2} {p : α × β} {q : α × β} : swap p = swap q ↔ p = q := function.injective.eq_iff swap_injective theorem eq_iff_fst_eq_snd_eq {α : Type u_1} {β : Type u_2} {p : α × β} {q : α × β} : p = q ↔ fst p = fst q ∧ snd p = snd q := sorry theorem fst_eq_iff {α : Type u_1} {β : Type u_2} {p : α × β} {x : α} : fst p = x ↔ p = (x, snd p) := sorry theorem snd_eq_iff {α : Type u_1} {β : Type u_2} {p : α × β} {x : β} : snd p = x ↔ p = (fst p, x) := sorry theorem lex_def {α : Type u_1} {β : Type u_2} (r : α → α → Prop) (s : β → β → Prop) {p : α × β} {q : α × β} : lex r s p q ↔ r (fst p) (fst q) ∨ fst p = fst q ∧ s (snd p) (snd q) := sorry protected instance lex.decidable {α : Type u_1} {β : Type u_2} [DecidableEq α] (r : α → α → Prop) (s : β → β → Prop) [DecidableRel r] [DecidableRel s] : DecidableRel (lex r s) := fun (p q : α × β) => decidable_of_decidable_of_iff or.decidable sorry end prod theorem function.injective.prod_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {f : α → γ} {g : β → δ} (hf : function.injective f) (hg : function.injective g) : function.injective (prod.map f g) := fun (x y : α × β) (h : prod.map f g x = prod.map f g y) => prod.ext (hf (and.left (iff.mp prod.ext_iff h))) (hg (and.right (iff.mp prod.ext_iff h))) theorem function.surjective.prod_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {f : α → γ} {g : β → δ} (hf : function.surjective f) (hg : function.surjective g) : function.surjective (prod.map f g) := sorry
8ff83f1217e6a705d07862a4cbe3595d5150353e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finset/pairwise.lean
4cdbc55112e2a68a744f6ad3290b902f3605048b
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,772
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.finset.lattice /-! # Relations holding pairwise on finite sets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove a few results about the interaction of `set.pairwise_disjoint` and `finset`, as well as the interaction of `list.pairwise disjoint` and the condition of `disjoint` on `list.to_finset`, in `set` form. -/ open finset variables {α ι ι' : Type*} instance [decidable_eq α] {r : α → α → Prop} [decidable_rel r] {s : finset α} : decidable ((s : set α).pairwise r) := decidable_of_iff' (∀ a ∈ s, ∀ b ∈ s, a ≠ b → r a b) iff.rfl lemma finset.pairwise_disjoint_range_singleton : (set.range (singleton : α → finset α)).pairwise_disjoint id := begin rintro _ ⟨a, rfl⟩ _ ⟨b, rfl⟩ h, exact disjoint_singleton.2 (ne_of_apply_ne _ h), end namespace set lemma pairwise_disjoint.elim_finset {s : set ι} {f : ι → finset α} (hs : s.pairwise_disjoint f) {i j : ι} (hi : i ∈ s) (hj : j ∈ s) (a : α) (hai : a ∈ f i) (haj : a ∈ f j) : i = j := hs.elim hi hj (finset.not_disjoint_iff.2 ⟨a, hai, haj⟩) section semilattice_inf variables [semilattice_inf α] [order_bot α] {s : finset ι} {f : ι → α} lemma pairwise_disjoint.image_finset_of_le [decidable_eq ι] {s : finset ι} {f : ι → α} (hs : (s : set ι).pairwise_disjoint f) {g : ι → ι} (hf : ∀ a, f (g a) ≤ f a) : (s.image g : set ι).pairwise_disjoint f := begin rw coe_image, exact hs.image_of_le hf, end lemma pairwise_disjoint.attach (hs : (s : set ι).pairwise_disjoint f) : (s.attach : set {x // x ∈ s}).pairwise_disjoint (f ∘ subtype.val) := λ i _ j _ hij, hs i.2 j.2 $ mt subtype.ext_val hij end semilattice_inf variables [lattice α] [order_bot α] /-- Bind operation for `set.pairwise_disjoint`. In a complete lattice, you can use `set.pairwise_disjoint.bUnion`. -/ lemma pairwise_disjoint.bUnion_finset {s : set ι'} {g : ι' → finset ι} {f : ι → α} (hs : s.pairwise_disjoint (λ i' : ι', (g i').sup f)) (hg : ∀ i ∈ s, (g i : set ι).pairwise_disjoint f) : (⋃ i ∈ s, ↑(g i)).pairwise_disjoint f := begin rintro a ha b hb hab, simp_rw set.mem_Union at ha hb, obtain ⟨c, hc, ha⟩ := ha, obtain ⟨d, hd, hb⟩ := hb, obtain hcd | hcd := eq_or_ne (g c) (g d), { exact hg d hd (by rwa hcd at ha) hb hab }, { exact (hs hc hd (ne_of_apply_ne _ hcd)).mono (finset.le_sup ha) (finset.le_sup hb) } end end set namespace list variables {β : Type*} [decidable_eq α] {r : α → α → Prop} {l : list α} lemma pairwise_of_coe_to_finset_pairwise (hl : (l.to_finset : set α).pairwise r) (hn : l.nodup) : l.pairwise r := by { rw coe_to_finset at hl, exact hn.pairwise_of_set_pairwise hl } lemma pairwise_iff_coe_to_finset_pairwise (hn : l.nodup) (hs : symmetric r) : (l.to_finset : set α).pairwise r ↔ l.pairwise r := by { rw [coe_to_finset, hn.pairwise_coe], exact ⟨hs⟩ } lemma pairwise_disjoint_of_coe_to_finset_pairwise_disjoint {α ι} [semilattice_inf α] [order_bot α] [decidable_eq ι] {l : list ι} {f : ι → α} (hl : (l.to_finset : set ι).pairwise_disjoint f) (hn : l.nodup) : l.pairwise (_root_.disjoint on f) := pairwise_of_coe_to_finset_pairwise hl hn lemma pairwise_disjoint_iff_coe_to_finset_pairwise_disjoint {α ι} [semilattice_inf α] [order_bot α] [decidable_eq ι] {l : list ι} {f : ι → α} (hn : l.nodup) : (l.to_finset : set ι).pairwise_disjoint f ↔ l.pairwise (_root_.disjoint on f) := pairwise_iff_coe_to_finset_pairwise hn (symmetric_disjoint.comap f) end list
073714b934c8cf69add754c4d963e7891495bdd5
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/uniform_space/abstract_completion_auto.lean
c9818ec226e018af86cb137b632417ed6777bf56
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
14,425
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.uniform_space.uniform_embedding import Mathlib.PostPort universes u l u_1 u_2 u_3 u_4 namespace Mathlib /-! # Abstract theory of Hausdorff completions of uniform spaces This file characterizes Hausdorff completions of a uniform space α as complete Hausdorff spaces equipped with a map from α which has dense image and induce the original uniform structure on α. Assuming these properties we "extend" uniformly continuous maps from α to complete Hausdorff spaces to the completions of α. This is the universal property expected from a completion. It is then used to extend uniformly continuous maps from α to α' to maps between completions of α and α'. This file does not construct any such completion, it only study consequences of their existence. The first advantage is that formal properties are clearly highlighted without interference from construction details. The second advantage is that this framework can then be used to compare different completion constructions. See `topology/uniform_space/compare_reals` for an example. Of course the comparison comes from the universal property as usual. A general explicit construction of completions is done in `uniform_space/completion`, leading to a functor from uniform spaces to complete Hausdorff uniform spaces that is left adjoint to the inclusion, see `uniform_space/UniformSpace` for the category packaging. ## Implementation notes A tiny technical advantage of using a characteristic predicate such as the properties listed in `abstract_completion` instead of stating the universal property is that the universal property derived from the predicate is more universe polymorphic. ## References We don't know any traditional text discussing this. Real world mathematics simply silently identify the results of any two constructions that lead to something one could reasonnably call a completion. ## Tags uniform spaces, completion, universal property -/ /-- A completion of `α` is the data of a complete separated uniform space (from the same universe) and a map from `α` with dense range and inducing the original uniform structure on `α`. -/ structure abstract_completion (α : Type u) [uniform_space α] where space : Type u coe : α → space uniform_struct : uniform_space space complete : complete_space space separation : separated_space space uniform_inducing : uniform_inducing coe dense : dense_range coe namespace abstract_completion theorem closure_range {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) : closure (set.range (coe pkg)) = set.univ := dense_range.closure_range (dense pkg) theorem dense_inducing {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) : dense_inducing (coe pkg) := dense_inducing.mk (uniform_inducing.inducing (uniform_inducing pkg)) (dense pkg) theorem uniform_continuous_coe {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) : uniform_continuous (coe pkg) := uniform_inducing.uniform_continuous (uniform_inducing pkg) theorem continuous_coe {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) : continuous (coe pkg) := uniform_continuous.continuous (uniform_continuous_coe pkg) theorem induction_on {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {p : space pkg → Prop} (a : space pkg) (hp : is_closed (set_of fun (a : space pkg) => p a)) (ih : ∀ (a : α), p (coe pkg a)) : p a := is_closed_property (dense pkg) hp ih a protected theorem funext {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] [t2_space β] {f : space pkg → β} {g : space pkg → β} (hf : continuous f) (hg : continuous g) (h : ∀ (a : α), f (coe pkg a) = g (coe pkg a)) : f = g := funext fun (a : space pkg) => induction_on pkg a (is_closed_eq hf hg) h /-- Extension of maps to completions -/ protected def extend {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (f : α → β) : space pkg → β := ite (uniform_continuous f) (dense_inducing.extend (dense_inducing pkg) f) fun (x : space pkg) => f (dense_range.some (dense pkg) x) theorem extend_def {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] {f : α → β} (hf : uniform_continuous f) : abstract_completion.extend pkg f = dense_inducing.extend (dense_inducing pkg) f := if_pos hf theorem extend_coe {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] {f : α → β} [t2_space β] (hf : uniform_continuous f) (a : α) : abstract_completion.extend pkg f (coe pkg a) = f a := eq.mpr (id (Eq._oldrec (Eq.refl (abstract_completion.extend pkg f (coe pkg a) = f a)) (extend_def pkg hf))) (dense_inducing.extend_eq (dense_inducing pkg) (uniform_continuous.continuous hf) a) theorem uniform_continuous_extend {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] {f : α → β} [complete_space β] [separated_space β] : uniform_continuous (abstract_completion.extend pkg f) := sorry theorem continuous_extend {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] {f : α → β} [complete_space β] [separated_space β] : continuous (abstract_completion.extend pkg f) := uniform_continuous.continuous (uniform_continuous_extend pkg) theorem extend_unique {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] {f : α → β} [complete_space β] [separated_space β] (hf : uniform_continuous f) {g : space pkg → β} (hg : uniform_continuous g) (h : ∀ (a : α), f a = g (coe pkg a)) : abstract_completion.extend pkg f = g := sorry @[simp] theorem extend_comp_coe {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] [complete_space β] [separated_space β] {f : space pkg → β} (hf : uniform_continuous f) : abstract_completion.extend pkg (f ∘ coe pkg) = f := sorry /-- Lifting maps to completions -/ protected def map {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) (f : α → β) : space pkg → space pkg' := abstract_completion.extend pkg (coe pkg' ∘ f) theorem uniform_continuous_map {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) (f : α → β) : uniform_continuous (abstract_completion.map pkg pkg' f) := uniform_continuous_extend pkg theorem continuous_map {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) (f : α → β) : continuous (abstract_completion.map pkg pkg' f) := continuous_extend pkg @[simp] theorem map_coe {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {f : α → β} (hf : uniform_continuous f) (a : α) : abstract_completion.map pkg pkg' f (coe pkg a) = coe pkg' (f a) := extend_coe pkg (uniform_continuous.comp (uniform_continuous_coe pkg') hf) a theorem map_unique {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {f : α → β} {g : space pkg → space pkg'} (hg : uniform_continuous g) (h : ∀ (a : α), coe pkg' (f a) = g (coe pkg a)) : abstract_completion.map pkg pkg' f = g := sorry @[simp] theorem map_id {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) : abstract_completion.map pkg pkg id = id := map_unique pkg pkg uniform_continuous_id fun (a : α) => rfl theorem extend_map {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] [complete_space γ] [separated_space γ] {f : β → γ} {g : α → β} (hf : uniform_continuous f) (hg : uniform_continuous g) : abstract_completion.extend pkg' f ∘ abstract_completion.map pkg pkg' g = abstract_completion.extend pkg (f ∘ g) := sorry theorem map_comp {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] (pkg'' : abstract_completion γ) {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) : abstract_completion.map pkg' pkg'' g ∘ abstract_completion.map pkg pkg' f = abstract_completion.map pkg pkg'' (g ∘ f) := extend_map pkg pkg' (uniform_continuous.comp (uniform_continuous_coe pkg'') hg) hf -- We can now compare two completion packages for the same uniform space /-- The comparison map between two completions of the same uniform space. -/ def compare {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) (pkg' : abstract_completion α) : space pkg → space pkg' := abstract_completion.extend pkg (coe pkg') theorem uniform_continuous_compare {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) (pkg' : abstract_completion α) : uniform_continuous (compare pkg pkg') := uniform_continuous_extend pkg theorem compare_coe {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) (pkg' : abstract_completion α) (a : α) : compare pkg pkg' (coe pkg a) = coe pkg' a := extend_coe pkg (uniform_continuous_coe pkg') a theorem inverse_compare {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) (pkg' : abstract_completion α) : compare pkg pkg' ∘ compare pkg' pkg = id := sorry /-- The bijection between two completions of the same uniform space. -/ def compare_equiv {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) (pkg' : abstract_completion α) : space pkg ≃ space pkg' := equiv.mk (compare pkg pkg') (compare pkg' pkg) sorry sorry theorem uniform_continuous_compare_equiv {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) (pkg' : abstract_completion α) : uniform_continuous ⇑(compare_equiv pkg pkg') := uniform_continuous_compare pkg pkg' theorem uniform_continuous_compare_equiv_symm {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) (pkg' : abstract_completion α) : uniform_continuous ⇑(equiv.symm (compare_equiv pkg pkg')) := uniform_continuous_compare pkg' pkg /-- Products of completions -/ protected def prod {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) : abstract_completion (α × β) := mk (space pkg × space pkg') (fun (p : α × β) => (coe pkg (prod.fst p), coe pkg' (prod.snd p))) prod.uniform_space sorry sorry sorry sorry /-- Extend two variable map to completions. -/ protected def extend₂ {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] (f : α → β → γ) : space pkg → space pkg' → γ := function.curry (abstract_completion.extend (abstract_completion.prod pkg pkg') (function.uncurry f)) theorem extension₂_coe_coe {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] [separated_space γ] {f : α → β → γ} (hf : uniform_continuous (function.uncurry f)) (a : α) (b : β) : abstract_completion.extend₂ pkg pkg' f (coe pkg a) (coe pkg' b) = f a b := sorry theorem uniform_continuous_extension₂ {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] [separated_space γ] (f : α → β → γ) [complete_space γ] : uniform_continuous₂ (abstract_completion.extend₂ pkg pkg' f) := sorry /-- Lift two variable maps to completions. -/ protected def map₂ {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] (pkg'' : abstract_completion γ) (f : α → β → γ) : space pkg → space pkg' → space pkg'' := abstract_completion.extend₂ pkg pkg' (function.bicompr (coe pkg'') f) theorem uniform_continuous_map₂ {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] (pkg'' : abstract_completion γ) (f : α → β → γ) : uniform_continuous₂ (abstract_completion.map₂ pkg pkg' pkg'' f) := uniform_continuous_extension₂ pkg pkg' (function.bicompr (coe pkg'') f) theorem continuous_map₂ {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] (pkg'' : abstract_completion γ) {δ : Type u_4} [topological_space δ] {f : α → β → γ} {a : δ → space pkg} {b : δ → space pkg'} (ha : continuous a) (hb : continuous b) : continuous fun (d : δ) => abstract_completion.map₂ pkg pkg' pkg'' f (a d) (b d) := continuous.comp (uniform_continuous.continuous (uniform_continuous_map₂ pkg pkg' pkg'' f)) (continuous.prod_mk ha hb) theorem map₂_coe_coe {α : Type u_1} [uniform_space α] (pkg : abstract_completion α) {β : Type u_2} [uniform_space β] (pkg' : abstract_completion β) {γ : Type u_3} [uniform_space γ] (pkg'' : abstract_completion γ) (a : α) (b : β) (f : α → β → γ) (hf : uniform_continuous₂ f) : abstract_completion.map₂ pkg pkg' pkg'' f (coe pkg a) (coe pkg' b) = coe pkg'' (f a b) := extension₂_coe_coe pkg pkg' (uniform_continuous.comp (uniform_continuous_coe pkg'') hf) a b end Mathlib
38f0cbe29ad394e00fbd49ed77117c17e0e2b610
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category_theory/limits/limits.lean
7fcbbde57583fb586e76aefa0d4300c19b181de3
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
31,725
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Scott Morrison, Floris van Doorn -/ import category_theory.whiskering import category_theory.yoneda import category_theory.limits.cones import category_theory.eq_to_hom open category_theory category_theory.category category_theory.functor opposite namespace category_theory.limits universes v u u' u'' w -- declare the `v`'s first; see `category_theory.category` for an explanation -- See the notes at the top of cones.lean, explaining why we can't allow `J : Prop` here. variables {J K : Type v} [small_category J] [small_category K] variables {C : Type u} [𝒞 : category.{v+1} C] include 𝒞 variables {F : J ⥤ C} /-- A cone `t` on `F` is a limit cone if each cone on `F` admits a unique cone morphism to `t`. -/ structure is_limit (t : cone F) := (lift : Π (s : cone F), s.X ⟶ t.X) (fac' : ∀ (s : cone F) (j : J), lift s ≫ t.π.app j = s.π.app j . obviously) (uniq' : ∀ (s : cone F) (m : s.X ⟶ t.X) (w : ∀ j : J, m ≫ t.π.app j = s.π.app j), m = lift s . obviously) restate_axiom is_limit.fac' attribute [simp] is_limit.fac restate_axiom is_limit.uniq' attribute [class] is_limit namespace is_limit instance subsingleton {t : cone F} : subsingleton (is_limit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ def lift_cone_morphism {t : cone F} (h : is_limit t) (s : cone F) : s ⟶ t := { hom := h.lift s } lemma uniq_cone_morphism {s t : cone F} (h : is_limit t) {f f' : s ⟶ t} : f = f' := have ∀ {g : s ⟶ t}, g = h.lift_cone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm def mk_cone_morphism {t : cone F} (lift : Π (s : cone F), s ⟶ t) (uniq' : ∀ (s : cone F) (m : s ⟶ t), m = lift s) : is_limit t := { lift := λ s, (lift s).hom, uniq' := λ s m w, have cone_morphism.mk m w = lift s, by apply uniq', congr_arg cone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique {s t : cone F} (P : is_limit s) (Q : is_limit t) : s ≅ t := { hom := Q.lift_cone_morphism s, inv := P.lift_cone_morphism t, hom_inv_id' := P.uniq_cone_morphism, inv_hom_id' := Q.uniq_cone_morphism } def of_iso_limit {r t : cone F} (P : is_limit r) (i : r ≅ t) : is_limit t := is_limit.mk_cone_morphism (λ s, P.lift_cone_morphism s ≫ i.hom) (λ s m, by rw ←i.comp_inv_eq; apply P.uniq_cone_morphism) variables {t : cone F} lemma hom_lift (h : is_limit t) {W : C} (m : W ⟶ t.X) : m = h.lift { X := W, π := { app := λ b, m ≫ t.π.app b } } := h.uniq { X := W, π := { app := λ b, m ≫ t.π.app b } } m (λ b, rfl) /-- Two morphisms into a limit are equal if their compositions with each cone morphism are equal. -/ lemma hom_ext (h : is_limit t) {W : C} {f f' : W ⟶ t.X} (w : ∀ j, f ≫ t.π.app j = f' ≫ t.π.app j) : f = f' := by rw [h.hom_lift f, h.hom_lift f']; congr; exact funext w /-- The universal property of a limit cone: a map `W ⟶ X` is the same as a cone on `F` with vertex `W`. -/ def hom_iso (h : is_limit t) (W : C) : (W ⟶ t.X) ≅ ((const J).obj W ⟶ F) := { hom := λ f, (t.extend f).π, inv := λ π, h.lift { X := W, π := π }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_limit t) {W : C} (f : W ⟶ t.X) : (is_limit.hom_iso h W).hom f = (t.extend f).π := rfl /-- The limit of `F` represents the functor taking `W` to the set of cones on `F` with vertex `W`. -/ def nat_iso (h : is_limit t) : yoneda.obj t.X ≅ F.cones := nat_iso.of_components (λ W, is_limit.hom_iso h (unop W)) (by tidy) def hom_iso' (h : is_limit t) (W : C) : ((W ⟶ t.X) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j'} (f : j ⟶ j'), p j ≫ F.map f = p j' } := h.hom_iso W ≪≫ { hom := λ π, ⟨λ j, π.app j, λ j j' f, by convert ←(π.naturality f).symm; apply id_comp⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [id_comp], exact (p.2 f).symm end } } /-- If G : C → D is a faithful functor which sends t to a limit cone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cone F} {D : Type u'} [category.{v+1} D] (G : C ⥤ D) [faithful G] (ht : is_limit (G.map_cone t)) (lift : Π (s : cone F), s.X ⟶ t.X) (h : ∀ s, G.map (lift s) = ht.lift (G.map_cone s)) : is_limit t := { lift := lift, fac' := λ s j, by apply G.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } end is_limit def is_limit_iso_unique_cone_morphism {t : cone F} : is_limit t ≅ Π s, unique (s ⟶ t) := { hom := λ h s, { default := h.lift_cone_morphism s, uniq := λ _, h.uniq_cone_morphism }, inv := λ h, { lift := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cone_morphism.hom ((h s).uniq ⟨f, w⟩) } } /-- A cocone `t` on `F` is a colimit cocone if each cocone on `F` admits a unique cocone morphism from `t`. -/ structure is_colimit (t : cocone F) := (desc : Π (s : cocone F), t.X ⟶ s.X) (fac' : ∀ (s : cocone F) (j : J), t.ι.app j ≫ desc s = s.ι.app j . obviously) (uniq' : ∀ (s : cocone F) (m : t.X ⟶ s.X) (w : ∀ j : J, t.ι.app j ≫ m = s.ι.app j), m = desc s . obviously) restate_axiom is_colimit.fac' attribute [simp] is_colimit.fac restate_axiom is_colimit.uniq' attribute [class] is_colimit namespace is_colimit instance subsingleton {t : cocone F} : subsingleton (is_colimit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ def desc_cocone_morphism {t : cocone F} (h : is_colimit t) (s : cocone F) : t ⟶ s := { hom := h.desc s } lemma uniq_cocone_morphism {s t : cocone F} (h : is_colimit t) {f f' : t ⟶ s} : f = f' := have ∀ {g : t ⟶ s}, g = h.desc_cocone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm def mk_cocone_morphism {t : cocone F} (desc : Π (s : cocone F), t ⟶ s) (uniq' : ∀ (s : cocone F) (m : t ⟶ s), m = desc s) : is_colimit t := { desc := λ s, (desc s).hom, uniq' := λ s m w, have cocone_morphism.mk m w = desc s, by apply uniq', congr_arg cocone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) : s ≅ t := { hom := P.desc_cocone_morphism t, inv := Q.desc_cocone_morphism s, hom_inv_id' := P.uniq_cocone_morphism, inv_hom_id' := Q.uniq_cocone_morphism } def of_iso_colimit {r t : cocone F} (P : is_colimit r) (i : r ≅ t) : is_colimit t := is_colimit.mk_cocone_morphism (λ s, i.inv ≫ P.desc_cocone_morphism s) (λ s m, by rw i.eq_inv_comp; apply P.uniq_cocone_morphism) variables {t : cocone F} lemma hom_desc (h : is_colimit t) {W : C} (m : t.X ⟶ W) : m = h.desc { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := by intros; erw [←assoc, t.ι.naturality, comp_id, comp_id] } } := h.uniq { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := _ } } m (λ b, rfl) /-- Two morphisms out of a colimit are equal if their compositions with each cocone morphism are equal. -/ lemma hom_ext (h : is_colimit t) {W : C} {f f' : t.X ⟶ W} (w : ∀ j, t.ι.app j ≫ f = t.ι.app j ≫ f') : f = f' := by rw [h.hom_desc f, h.hom_desc f']; congr; exact funext w /-- The universal property of a colimit cocone: a map `X ⟶ W` is the same as a cocone on `F` with vertex `W`. -/ def hom_iso (h : is_colimit t) (W : C) : (t.X ⟶ W) ≅ (F ⟶ (const J).obj W) := { hom := λ f, (t.extend f).ι, inv := λ ι, h.desc { X := W, ι := ι }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_colimit t) {W : C} (f : t.X ⟶ W) : (is_colimit.hom_iso h W).hom f = (t.extend f).ι := rfl /-- The colimit of `F` represents the functor taking `W` to the set of cocones on `F` with vertex `W`. -/ def nat_iso (h : is_colimit t) : coyoneda.obj (op t.X) ≅ F.cocones := nat_iso.of_components (is_colimit.hom_iso h) (by intros; ext; dsimp; rw ←assoc; refl) def hom_iso' (h : is_colimit t) (W : C) : ((t.X ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j' : J} (f : j ⟶ j'), F.map f ≫ p j' = p j } := h.hom_iso W ≪≫ { hom := λ ι, ⟨λ j, ι.app j, λ j j' f, by convert ←(ι.naturality f); apply comp_id⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [comp_id], exact (p.2 f) end } } /-- If G : C → D is a faithful functor which sends t to a colimit cocone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cocone F} {D : Type u'} [category.{v+1} D] (G : C ⥤ D) [faithful G] (ht : is_colimit (G.map_cocone t)) (desc : Π (s : cocone F), t.X ⟶ s.X) (h : ∀ s, G.map (desc s) = ht.desc (G.map_cocone s)) : is_colimit t := { desc := desc, fac' := λ s j, by apply G.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cocone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } end is_colimit def is_colimit_iso_unique_cocone_morphism {t : cocone F} : is_colimit t ≅ Π s, unique (t ⟶ s) := { hom := λ h s, { default := h.desc_cocone_morphism s, uniq := λ _, h.uniq_cocone_morphism }, inv := λ h, { desc := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cocone_morphism.hom ((h s).uniq ⟨f, w⟩) } } section limit /-- `has_limit F` represents a particular chosen limit of the diagram `F`. -/ class has_limit (F : J ⥤ C) := (cone : cone F) (is_limit : is_limit cone . tactic.apply_instance) variables (J C) /-- `C` has limits of shape `J` if we have chosen a particular limit of every functor `F : J ⥤ C`. -/ class has_limits_of_shape := (has_limit : Π F : J ⥤ C, has_limit F) /-- `C` has all (small) limits if it has limits of every shape. -/ class has_limits := (has_limits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_limits_of_shape J C) variables {J C} instance has_limit_of_has_limits_of_shape {J : Type v} [small_category J] [H : has_limits_of_shape J C] (F : J ⥤ C) : has_limit F := has_limits_of_shape.has_limit F instance has_limits_of_shape_of_has_limits {J : Type v} [small_category J] [H : has_limits.{v} C] : has_limits_of_shape J C := has_limits.has_limits_of_shape C J /- Interface to the `has_limit` class. -/ def limit.cone (F : J ⥤ C) [has_limit F] : cone F := has_limit.cone F def limit (F : J ⥤ C) [has_limit F] := (limit.cone F).X def limit.π (F : J ⥤ C) [has_limit F] (j : J) : limit F ⟶ F.obj j := (limit.cone F).π.app j @[simp] lemma limit.cone_π {F : J ⥤ C} [has_limit F] (j : J) : (limit.cone F).π.app j = limit.π _ j := rfl @[simp] lemma limit.w (F : J ⥤ C) [has_limit F] {j j' : J} (f : j ⟶ j') : limit.π F j ≫ F.map f = limit.π F j' := (limit.cone F).w f instance limit.is_limit (F : J ⥤ C) [has_limit F] : is_limit (limit.cone F) := has_limit.is_limit.{v} F def limit.lift (F : J ⥤ C) [has_limit F] (c : cone F) : c.X ⟶ limit F := (limit.is_limit F).lift c @[simp] lemma limit.is_limit_lift {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.is_limit F).lift c = limit.lift F c := rfl @[simp] lemma limit.lift_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : limit.lift F c ≫ limit.π F j = c.π.app j := is_limit.fac _ c j def limit.cone_morphism {F : J ⥤ C} [has_limit F] (c : cone F) : cone_morphism c (limit.cone F) := (limit.is_limit F).lift_cone_morphism c @[simp] lemma limit.cone_morphism_hom {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.cone_morphism c).hom = limit.lift F c := rfl @[simp] lemma limit.cone_morphism_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : (limit.cone_morphism c).hom ≫ limit.π F j = c.π.app j := by erw is_limit.fac @[extensionality] lemma limit.hom_ext {F : J ⥤ C} [has_limit F] {X : C} {f f' : X ⟶ limit F} (w : ∀ j, f ≫ limit.π F j = f' ≫ limit.π F j) : f = f' := (limit.is_limit F).hom_ext w def limit.hom_iso (F : J ⥤ C) [has_limit F] (W : C) : (W ⟶ limit F) ≅ (F.cones.obj (op W)) := (limit.is_limit F).hom_iso W @[simp] lemma limit.hom_iso_hom (F : J ⥤ C) [has_limit F] {W : C} (f : W ⟶ limit F) : (limit.hom_iso F W).hom f = (const J).map f ≫ (limit.cone F).π := (limit.is_limit F).hom_iso_hom f def limit.hom_iso' (F : J ⥤ C) [has_limit F] (W : C) : ((W ⟶ limit F) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j' : J} (f : j ⟶ j'), p j ≫ F.map f = p j' } := (limit.is_limit F).hom_iso' W lemma limit.lift_extend {F : J ⥤ C} [has_limit F] (c : cone F) {X : C} (f : X ⟶ c.X) : limit.lift F (c.extend f) = f ≫ limit.lift F c := by obviously def has_limit_of_iso {F G : J ⥤ C} [has_limit F] (α : F ≅ G) : has_limit G := { cone := (cones.postcompose α.hom).obj (limit.cone F), is_limit := { lift := λ s, limit.lift F ((cones.postcompose α.inv).obj s), fac' := λ s j, begin rw [cones.postcompose_obj_π, nat_trans.comp_app, limit.cone_π], rw [category.assoc_symm, limit.lift_π], simp end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, rw [limit.lift_π, cones.postcompose_obj_π, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_comp_inv], simpa using w j end } } section pre variables (F) [has_limit F] (E : K ⥤ J) [has_limit (E ⋙ F)] def limit.pre : limit F ⟶ limit (E ⋙ F) := limit.lift (E ⋙ F) { X := limit F, π := { app := λ k, limit.π F (E.obj k) } } @[simp] lemma limit.pre_π (k : K) : limit.pre F E ≫ limit.π (E ⋙ F) k = limit.π F (E.obj k) := by erw is_limit.fac @[simp] lemma limit.lift_pre (c : cone F) : limit.lift F c ≫ limit.pre F E = limit.lift (E ⋙ F) (c.whisker E) := by ext; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_limit (D ⋙ E ⋙ F)] @[simp] lemma limit.pre_pre : limit.pre F E ≫ limit.pre (E ⋙ F) D = limit.pre F (D ⋙ E) := by ext j; erw [assoc, limit.pre_π, limit.pre_π, limit.pre_π]; refl end pre section post variables {D : Type u'} [𝒟 : category.{v+1} D] include 𝒟 variables (F) [has_limit F] (G : C ⥤ D) [has_limit (F ⋙ G)] def limit.post : G.obj (limit F) ⟶ limit (F ⋙ G) := limit.lift (F ⋙ G) { X := G.obj (limit F), π := { app := λ j, G.map (limit.π F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cone.w, id_comp]; refl } } @[simp] lemma limit.post_π (j : J) : limit.post F G ≫ limit.π (F ⋙ G) j = G.map (limit.π F j) := by erw is_limit.fac @[simp] lemma limit.lift_post (c : cone F) : G.map (limit.lift F c) ≫ limit.post F G = limit.lift (F ⋙ G) (G.map_cone c) := by ext; rw [assoc, limit.post_π, ←G.map_comp, limit.lift_π, limit.lift_π]; refl @[simp] lemma limit.post_post {E : Type u''} [category.{v+1} E] (H : D ⥤ E) [has_limit ((F ⋙ G) ⋙ H)] : /- H G (limit F) ⟶ H (limit (F ⋙ G)) ⟶ limit ((F ⋙ G) ⋙ H) equals -/ /- H G (limit F) ⟶ limit (F ⋙ (G ⋙ H)) -/ H.map (limit.post F G) ≫ limit.post (F ⋙ G) H = limit.post F (G ⋙ H) := by ext; erw [assoc, limit.post_π, ←H.map_comp, limit.post_π, limit.post_π]; refl end post lemma limit.pre_post {D : Type u'} [category.{v+1} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_limit F] [has_limit (E ⋙ F)] [has_limit (F ⋙ G)] [has_limit ((E ⋙ F) ⋙ G)] : /- G (limit F) ⟶ G (limit (E ⋙ F)) ⟶ limit ((E ⋙ F) ⋙ G) vs -/ /- G (limit F) ⟶ limit F ⋙ G ⟶ limit (E ⋙ (F ⋙ G)) or -/ G.map (limit.pre F E) ≫ limit.post (E ⋙ F) G = limit.post F G ≫ limit.pre (F ⋙ G) E := by ext; erw [assoc, limit.post_π, ←G.map_comp, limit.pre_π, assoc, limit.pre_π, limit.post_π]; refl open category_theory.equivalence instance has_limit_equivalence_comp (e : K ≌ J) [has_limit F] : has_limit (e.functor ⋙ F) := { cone := cone.whisker e.functor (limit.cone F), is_limit := let e' := cones.postcompose (e.inv_fun_id_assoc F).hom in { lift := λ s, limit.lift F (e'.obj (cone.whisker e.inverse s)), fac' := λ s j, begin dsimp, rw [limit.lift_π], dsimp [e'], erw [inv_fun_id_assoc_hom_app, counit_functor, ←s.π.naturality, id_comp] end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, erw [limit.lift_π, ←limit.w F (e.counit_iso.hom.app j)], slice_lhs 1 2 { erw [w (e.inverse.obj j)] }, simp end } } def has_limit_of_equivalence_comp (e : K ≌ J) [has_limit (e.functor ⋙ F)] : has_limit F := begin haveI : has_limit (e.inverse ⋙ e.functor ⋙ F) := limits.has_limit_equivalence_comp e.symm, apply has_limit_of_iso (e.inv_fun_id_assoc F), end -- `has_limit_comp_equivalence` and `has_limit_of_comp_equivalence` -- are proved in `category_theory/adjunction/limits.lean`. section lim_functor variables [has_limits_of_shape J C] /-- `limit F` is functorial in `F`, when `C` has all limits of shape `J`. -/ def lim : (J ⥤ C) ⥤ C := { obj := λ F, limit F, map := λ F G α, limit.lift G { X := limit F, π := { app := λ j, limit.π F j ≫ α.app j, naturality' := λ j j' f, by erw [id_comp, assoc, ←α.naturality, ←assoc, limit.w] } }, map_comp' := λ F G H α β, by ext; erw [assoc, is_limit.fac, is_limit.fac, ←assoc, is_limit.fac, assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp] lemma lim.map_π (j : J) : lim.map α ≫ limit.π G j = limit.π F j ≫ α.app j := by apply is_limit.fac @[simp] lemma limit.lift_map (c : cone F) : limit.lift F c ≫ lim.map α = limit.lift G ((cones.postcompose α).obj c) := by ext; rw [assoc, lim.map_π, ←assoc, limit.lift_π, limit.lift_π]; refl lemma limit.map_pre [has_limits_of_shape K C] (E : K ⥤ J) : lim.map α ≫ limit.pre G E = limit.pre F E ≫ lim.map (whisker_left E α) := by ext; rw [assoc, limit.pre_π, lim.map_π, assoc, lim.map_π, ←assoc, limit.pre_π]; refl lemma limit.map_pre' [has_limits_of_shape.{v} K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : limit.pre F E₂ = limit.pre F E₁ ≫ lim.map (whisker_right α F) := by ext1; simp [(category.assoc _ _ _ _).symm] lemma limit.id_pre (F : J ⥤ C) : limit.pre F (functor.id _) = lim.map (functor.left_unitor F).inv := by tidy lemma limit.map_post {D : Type u'} [category.{v+1} D] [has_limits_of_shape J D] (H : C ⥤ D) : /- H (limit F) ⟶ H (limit G) ⟶ limit (G ⋙ H) vs H (limit F) ⟶ limit (F ⋙ H) ⟶ limit (G ⋙ H) -/ H.map (lim.map α) ≫ limit.post G H = limit.post F H ≫ lim.map (whisker_right α H) := begin ext, rw [assoc, limit.post_π, ←H.map_comp, lim.map_π, H.map_comp], rw [assoc, lim.map_π, ←assoc, limit.post_π], refl end def lim_yoneda : lim ⋙ yoneda ≅ category_theory.cones J C := nat_iso.of_components (λ F, nat_iso.of_components (λ W, limit.hom_iso F (unop W)) (by tidy)) (by tidy) end lim_functor def has_limits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_limits_of_shape J C] : has_limits_of_shape J' C := by { constructor, intro F, apply has_limit_of_equivalence_comp e, apply_instance } end limit section colimit /-- `has_colimit F` represents a particular chosen colimit of the diagram `F`. -/ class has_colimit (F : J ⥤ C) := (cocone : cocone F) (is_colimit : is_colimit cocone . tactic.apply_instance) variables (J C) /-- `C` has colimits of shape `J` if we have chosen a particular colimit of every functor `F : J ⥤ C`. -/ class has_colimits_of_shape := (has_colimit : Π F : J ⥤ C, has_colimit F) /-- `C` has all (small) colimits if it has colimits of every shape. -/ class has_colimits := (has_colimits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_colimits_of_shape J C) variables {J C} instance has_colimit_of_has_colimits_of_shape {J : Type v} [small_category J] [H : has_colimits_of_shape J C] (F : J ⥤ C) : has_colimit F := has_colimits_of_shape.has_colimit F instance has_colimits_of_shape_of_has_colimits {J : Type v} [small_category J] [H : has_colimits.{v} C] : has_colimits_of_shape J C := has_colimits.has_colimits_of_shape C J /- Interface to the `has_colimit` class. -/ def colimit.cocone (F : J ⥤ C) [has_colimit F] : cocone F := has_colimit.cocone F def colimit (F : J ⥤ C) [has_colimit F] := (colimit.cocone F).X def colimit.ι (F : J ⥤ C) [has_colimit F] (j : J) : F.obj j ⟶ colimit F := (colimit.cocone F).ι.app j @[simp] lemma colimit.cocone_ι {F : J ⥤ C} [has_colimit F] (j : J) : (colimit.cocone F).ι.app j = colimit.ι _ j := rfl @[simp] lemma colimit.w (F : J ⥤ C) [has_colimit F] {j j' : J} (f : j ⟶ j') : F.map f ≫ colimit.ι F j' = colimit.ι F j := (colimit.cocone F).w f instance colimit.is_colimit (F : J ⥤ C) [has_colimit F] : is_colimit (colimit.cocone F) := has_colimit.is_colimit.{v} F def colimit.desc (F : J ⥤ C) [has_colimit F] (c : cocone F) : colimit F ⟶ c.X := (colimit.is_colimit F).desc c @[simp] lemma colimit.is_colimit_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.is_colimit F).desc c = colimit.desc F c := rfl @[simp] lemma colimit.ι_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ colimit.desc F c = c.ι.app j := is_colimit.fac _ c j /-- We have lots of lemmas describing how to simplify `colimit.ι F j ≫ _`, and combined with `colimit.ext` we rely on these lemmas for many calculations. However, since `category.assoc` is a `@[simp]` lemma, often expressions are right associated, and it's hard to apply these lemmas about `colimit.ι`. We thus define some additional `@[simp]` lemmas, with an arbitrary extra morphism. -/ @[simp] lemma colimit.ι_desc_assoc {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) {Y : C} (f : c.X ⟶ Y) : colimit.ι F j ≫ colimit.desc F c ≫ f = c.ι.app j ≫ f := by rw [←category.assoc, colimit.ι_desc] def colimit.cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) : cocone_morphism (colimit.cocone F) c := (colimit.is_colimit F).desc_cocone_morphism c @[simp] lemma colimit.cocone_morphism_hom {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.cocone_morphism c).hom = colimit.desc F c := rfl @[simp] lemma colimit.ι_cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ (colimit.cocone_morphism c).hom = c.ι.app j := by erw is_colimit.fac @[extensionality] lemma colimit.hom_ext {F : J ⥤ C} [has_colimit F] {X : C} {f f' : colimit F ⟶ X} (w : ∀ j, colimit.ι F j ≫ f = colimit.ι F j ≫ f') : f = f' := (colimit.is_colimit F).hom_ext w def colimit.hom_iso (F : J ⥤ C) [has_colimit F] (W : C) : (colimit F ⟶ W) ≅ (F.cocones.obj W) := (colimit.is_colimit F).hom_iso W @[simp] lemma colimit.hom_iso_hom (F : J ⥤ C) [has_colimit F] {W : C} (f : colimit F ⟶ W) : (colimit.hom_iso F W).hom f = (colimit.cocone F).ι ≫ (const J).map f := (colimit.is_colimit F).hom_iso_hom f def colimit.hom_iso' (F : J ⥤ C) [has_colimit F] (W : C) : ((colimit F ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j'} (f : j ⟶ j'), F.map f ≫ p j' = p j } := (colimit.is_colimit F).hom_iso' W lemma colimit.desc_extend (F : J ⥤ C) [has_colimit F] (c : cocone F) {X : C} (f : c.X ⟶ X) : colimit.desc F (c.extend f) = colimit.desc F c ≫ f := begin ext1, rw [←category.assoc], simp end def has_colimit_of_iso {F G : J ⥤ C} [has_colimit F] (α : G ≅ F) : has_colimit G := { cocone := (cocones.precompose α.hom).obj (colimit.cocone F), is_colimit := { desc := λ s, colimit.desc F ((cocones.precompose α.inv).obj s), fac' := λ s j, begin rw [cocones.precompose_obj_ι, nat_trans.comp_app, colimit.cocone_ι], rw [category.assoc, colimit.ι_desc, ←nat_iso.app_hom, ←iso.eq_inv_comp], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, rw [colimit.ι_desc, cocones.precompose_obj_ι, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_inv_comp], simpa using w j end } } section pre variables (F) [has_colimit F] (E : K ⥤ J) [has_colimit (E ⋙ F)] def colimit.pre : colimit (E ⋙ F) ⟶ colimit F := colimit.desc (E ⋙ F) { X := colimit F, ι := { app := λ k, colimit.ι F (E.obj k) } } @[simp] lemma colimit.ι_pre (k : K) : colimit.ι (E ⋙ F) k ≫ colimit.pre F E = colimit.ι F (E.obj k) := by erw is_colimit.fac @[simp] lemma colimit.ι_pre_assoc (k : K) {Z : C} (f : colimit F ⟶ Z) : colimit.ι (E ⋙ F) k ≫ (colimit.pre F E) ≫ f = ((colimit.ι F (E.obj k)) : (E ⋙ F).obj k ⟶ colimit F) ≫ f := by rw [←category.assoc, colimit.ι_pre] @[simp] lemma colimit.pre_desc (c : cocone F) : colimit.pre F E ≫ colimit.desc F c = colimit.desc (E ⋙ F) (c.whisker E) := by ext; rw [←assoc, colimit.ι_pre]; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_colimit (D ⋙ E ⋙ F)] @[simp] lemma colimit.pre_pre : colimit.pre (E ⋙ F) D ≫ colimit.pre F E = colimit.pre F (D ⋙ E) := begin ext j, rw [←assoc, colimit.ι_pre, colimit.ι_pre], letI : has_colimit ((D ⋙ E) ⋙ F) := show has_colimit (D ⋙ E ⋙ F), by apply_instance, exact (colimit.ι_pre F (D ⋙ E) j).symm end end pre section post variables {D : Type u'} [𝒟 : category.{v+1} D] include 𝒟 variables (F) [has_colimit F] (G : C ⥤ D) [has_colimit (F ⋙ G)] def colimit.post : colimit (F ⋙ G) ⟶ G.obj (colimit F) := colimit.desc (F ⋙ G) { X := G.obj (colimit F), ι := { app := λ j, G.map (colimit.ι F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cocone.w, comp_id]; refl } } @[simp] lemma colimit.ι_post (j : J) : colimit.ι (F ⋙ G) j ≫ colimit.post F G = G.map (colimit.ι F j) := by erw is_colimit.fac @[simp] lemma colimit.ι_post_assoc (j : J) {Y : D} (f : G.obj (colimit F) ⟶ Y) : colimit.ι (F ⋙ G) j ≫ colimit.post F G ≫ f = G.map (colimit.ι F j) ≫ f := by rw [←category.assoc, colimit.ι_post] @[simp] lemma colimit.post_desc (c : cocone F) : colimit.post F G ≫ G.map (colimit.desc F c) = colimit.desc (F ⋙ G) (G.map_cocone c) := by ext; rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_desc, colimit.ι_desc]; refl @[simp] lemma colimit.post_post {E : Type u''} [category.{v+1} E] (H : D ⥤ E) [has_colimit ((F ⋙ G) ⋙ H)] : /- H G (colimit F) ⟶ H (colimit (F ⋙ G)) ⟶ colimit ((F ⋙ G) ⋙ H) equals -/ /- H G (colimit F) ⟶ colimit (F ⋙ (G ⋙ H)) -/ colimit.post (F ⋙ G) H ≫ H.map (colimit.post F G) = colimit.post F (G ⋙ H) := begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colimit.ι_post], exact (colimit.ι_post F (G ⋙ H) j).symm end end post lemma colimit.pre_post {D : Type u'} [category.{v+1} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_colimit F] [has_colimit (E ⋙ F)] [has_colimit (F ⋙ G)] [has_colimit ((E ⋙ F) ⋙ G)] : /- G (colimit F) ⟶ G (colimit (E ⋙ F)) ⟶ colimit ((E ⋙ F) ⋙ G) vs -/ /- G (colimit F) ⟶ colimit F ⋙ G ⟶ colimit (E ⋙ (F ⋙ G)) or -/ colimit.post (E ⋙ F) G ≫ G.map (colimit.pre F E) = colimit.pre (F ⋙ G) E ≫ colimit.post F G := begin ext, rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_pre, ←assoc], letI : has_colimit (E ⋙ F ⋙ G) := show has_colimit ((E ⋙ F) ⋙ G), by apply_instance, erw [colimit.ι_pre (F ⋙ G) E j, colimit.ι_post] end open category_theory.equivalence instance has_colimit_equivalence_comp (e : K ≌ J) [has_colimit F] : has_colimit (e.functor ⋙ F) := { cocone := cocone.whisker e.functor (colimit.cocone F), is_colimit := let e' := cocones.precompose (e.inv_fun_id_assoc F).inv in { desc := λ s, colimit.desc F (e'.obj (cocone.whisker e.inverse s)), fac' := λ s j, begin dsimp, rw [colimit.ι_desc], dsimp [e'], erw [inv_fun_id_assoc_inv_app, ←functor_unit, s.ι.naturality, comp_id], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, erw [colimit.ι_desc], have := w (e.inverse.obj j), simp at this, erw [←colimit.w F (e.counit_iso.hom.app j)] at this, erw [assoc, ←iso.eq_inv_comp (F.map_iso $ e.counit_iso.app j)] at this, erw [this], simp end } } def has_colimit_of_equivalence_comp (e : K ≌ J) [has_colimit (e.functor ⋙ F)] : has_colimit F := begin haveI : has_colimit (e.inverse ⋙ e.functor ⋙ F) := limits.has_colimit_equivalence_comp e.symm, apply has_colimit_of_iso (e.inv_fun_id_assoc F).symm, end section colim_functor variables [has_colimits_of_shape J C] /-- `colimit F` is functorial in `F`, when `C` has all colimits of shape `J`. -/ def colim : (J ⥤ C) ⥤ C := { obj := λ F, colimit F, map := λ F G α, colimit.desc F { X := colimit G, ι := { app := λ j, α.app j ≫ colimit.ι G j, naturality' := λ j j' f, by erw [comp_id, ←assoc, α.naturality, assoc, colimit.w] } }, map_comp' := λ F G H α β, by ext; erw [←assoc, is_colimit.fac, is_colimit.fac, assoc, is_colimit.fac, ←assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp] lemma colim.ι_map (j : J) : colimit.ι F j ≫ colim.map α = α.app j ≫ colimit.ι G j := by apply is_colimit.fac @[simp] lemma colim.ι_map_assoc (j : J) {Y : C} (f : colimit G ⟶ Y) : colimit.ι F j ≫ colim.map α ≫ f = α.app j ≫ colimit.ι G j ≫ f := by rw [←category.assoc, colim.ι_map, category.assoc] @[simp] lemma colimit.map_desc (c : cocone G) : colim.map α ≫ colimit.desc G c = colimit.desc F ((cocones.precompose α).obj c) := by ext; rw [←assoc, colim.ι_map, assoc, colimit.ι_desc, colimit.ι_desc]; refl lemma colimit.pre_map [has_colimits_of_shape K C] (E : K ⥤ J) : colimit.pre F E ≫ colim.map α = colim.map (whisker_left E α) ≫ colimit.pre G E := by ext; rw [←assoc, colimit.ι_pre, colim.ι_map, ←assoc, colim.ι_map, assoc, colimit.ι_pre]; refl lemma colimit.pre_map' [has_colimits_of_shape.{v} K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : colimit.pre F E₁ = colim.map (whisker_right α F) ≫ colimit.pre F E₂ := by ext1; simp [(category.assoc _ _ _ _).symm] lemma colimit.pre_id (F : J ⥤ C) : colimit.pre F (functor.id _) = colim.map (functor.left_unitor F).hom := by tidy lemma colimit.map_post {D : Type u'} [category.{v+1} D] [has_colimits_of_shape J D] (H : C ⥤ D) : /- H (colimit F) ⟶ H (colimit G) ⟶ colimit (G ⋙ H) vs H (colimit F) ⟶ colimit (F ⋙ H) ⟶ colimit (G ⋙ H) -/ colimit.post F H ≫ H.map (colim.map α) = colim.map (whisker_right α H) ≫ colimit.post G H:= begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colim.ι_map, H.map_comp], rw [←assoc, colim.ι_map, assoc, colimit.ι_post], refl end def colim_coyoneda : colim.op ⋙ coyoneda ≅ category_theory.cocones J C := nat_iso.of_components (λ F, nat_iso.of_components (colimit.hom_iso (unop F)) (by tidy)) (by tidy) end colim_functor def has_colimits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_colimits_of_shape J C] : has_colimits_of_shape J' C := by { constructor, intro F, apply has_colimit_of_equivalence_comp e, apply_instance } end colimit end category_theory.limits
46552a8dfccdc005cd9494cd16df9bb32623434a
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/algebra/category/Group/adjunctions.lean
5919d9b72a963b9804684f07e49893863835a73d
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
1,801
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl -/ import algebra.category.Group import group_theory.free_abelian_group /-! The free abelian group on a type is the left adjoint of the forgetful functor from abelian groups to types. -/ noncomputable theory universe u open category_theory namespace AddCommGroup open_locale classical /-- The free functor `Type u ⥤ AddCommGroup` sending a type `X` to the free abelian group with generators `x : X`. -/ def free : Type u ⥤ AddCommGroup := { obj := λ α, of (free_abelian_group α), map := λ X Y f, add_monoid_hom.of (λ x : free_abelian_group X, f <$> x), map_id' := λ X, add_monoid_hom.ext $ by simp [types_id], map_comp' := λ X Y Z f g, add_monoid_hom.ext $ by { intro x, simp [is_lawful_functor.comp_map, types_comp], } } @[simp] lemma free_obj_coe {α : Type u} : (free.obj α : Type u) = (free_abelian_group α) := rfl @[simp] lemma free_map_coe {α β : Type u} {f : α → β} (x : free_abelian_group α) : (free.map f) x = f <$> x := rfl /-- The free-forgetful adjunction for abelian groups. -/ def adj : free ⊣ forget AddCommGroup.{u} := adjunction.mk_of_hom_equiv { hom_equiv := λ X G, free_abelian_group.hom_equiv X G, hom_equiv_naturality_left_symm' := by { intros, ext, simp [types_comp, free_abelian_group.lift_comp], } } /-- As an example, we now give a high-powered proof that the monomorphisms in `AddCommGroup` are just the injective functions. (This proof works in all universes.) -/ example {G H : AddCommGroup.{u}} (f : G ⟶ H) [mono f] : function.injective f := (mono_iff_injective f).1 (right_adjoint_preserves_mono adj (by apply_instance : mono f)) end AddCommGroup
c62f07921896b573d840ea064544c132f33dbda9
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/computability/primrec.lean
d64520ccf17c706952b890eb7f3d9a3a3107f642
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
51,983
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.equiv.list import logic.function.iterate /-! # The primitive recursive functions The primitive recursive functions are the least collection of functions `nat → nat` which are closed under projections (using the mkpair pairing function), composition, zero, successor, and primitive recursion (i.e. nat.rec where the motive is C n := nat). We can extend this definition to a large class of basic types by using canonical encodings of types as natural numbers (Gödel numbering), which we implement through the type class `encodable`. (More precisely, we need that the composition of encode with decode yields a primitive recursive function, so we have the `primcodable` type class for this.) ## References * [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019] -/ open denumerable encodable namespace nat def elim {C : Sort*} : C → (ℕ → C → C) → ℕ → C := @nat.rec (λ _, C) @[simp] theorem elim_zero {C} (a f) : @nat.elim C a f 0 = a := rfl @[simp] theorem elim_succ {C} (a f n) : @nat.elim C a f (succ n) = f n (nat.elim a f n) := rfl def cases {C : Sort*} (a : C) (f : ℕ → C) : ℕ → C := nat.elim a (λ n _, f n) @[simp] theorem cases_zero {C} (a f) : @nat.cases C a f 0 = a := rfl @[simp] theorem cases_succ {C} (a f n) : @nat.cases C a f (succ n) = f n := rfl @[simp, reducible] def unpaired {α} (f : ℕ → ℕ → α) (n : ℕ) : α := f n.unpair.1 n.unpair.2 /-- The primitive recursive functions `ℕ → ℕ`. -/ inductive primrec : (ℕ → ℕ) → Prop | zero : primrec (λ n, 0) | succ : primrec succ | left : primrec (λ n, n.unpair.1) | right : primrec (λ n, n.unpair.2) | pair {f g} : primrec f → primrec g → primrec (λ n, mkpair (f n) (g n)) | comp {f g} : primrec f → primrec g → primrec (λ n, f (g n)) | prec {f g} : primrec f → primrec g → primrec (unpaired (λ z n, n.elim (f z) (λ y IH, g $ mkpair z $ mkpair y IH))) namespace primrec theorem of_eq {f g : ℕ → ℕ} (hf : primrec f) (H : ∀ n, f n = g n) : primrec g := (funext H : f = g) ▸ hf theorem const : ∀ (n : ℕ), primrec (λ _, n) | 0 := zero | (n+1) := succ.comp (const n) protected theorem id : primrec id := (left.pair right).of_eq $ λ n, by simp theorem prec1 {f} (m : ℕ) (hf : primrec f) : primrec (λ n, n.elim m (λ y IH, f $ mkpair y IH)) := ((prec (const m) (hf.comp right)).comp (zero.pair primrec.id)).of_eq $ λ n, by simp; dsimp; rw [unpair_mkpair] theorem cases1 {f} (m : ℕ) (hf : primrec f) : primrec (nat.cases m f) := (prec1 m (hf.comp left)).of_eq $ by simp [cases] theorem cases {f g} (hf : primrec f) (hg : primrec g) : primrec (unpaired (λ z n, n.cases (f z) (λ y, g $ mkpair z y))) := (prec hf (hg.comp (pair left (left.comp right)))).of_eq $ by simp [cases] protected theorem swap : primrec (unpaired (function.swap mkpair)) := (pair right left).of_eq $ λ n, by simp theorem swap' {f} (hf : primrec (unpaired f)) : primrec (unpaired (function.swap f)) := (hf.comp primrec.swap).of_eq $ λ n, by simp theorem pred : primrec pred := (cases1 0 primrec.id).of_eq $ λ n, by cases n; simp * theorem add : primrec (unpaired (+)) := (prec primrec.id ((succ.comp right).comp right)).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, -add_comm, add_succ] theorem sub : primrec (unpaired has_sub.sub) := (prec primrec.id ((pred.comp right).comp right)).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, -add_comm, sub_succ] theorem mul : primrec (unpaired (*)) := (prec zero (add.comp (pair left (right.comp right)))).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, mul_succ, add_comm] theorem pow : primrec (unpaired (^)) := (prec (const 1) (mul.comp (pair (right.comp right) left))).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, nat.pow_succ] end primrec end nat /-- A `primcodable` type is an `encodable` type for which the encode/decode functions are primitive recursive. -/ class primcodable (α : Type*) extends encodable α := (prim [] : nat.primrec (λ n, encodable.encode (decode n))) namespace primcodable open nat.primrec @[priority 10] instance of_denumerable (α) [denumerable α] : primcodable α := ⟨succ.of_eq $ by simp⟩ def of_equiv (α) {β} [primcodable α] (e : β ≃ α) : primcodable β := { prim := (primcodable.prim α).of_eq $ λ n, show encode (decode α n) = (option.cases_on (option.map e.symm (decode α n)) 0 (λ a, nat.succ (encode (e a))) : ℕ), by cases decode α n; dsimp; simp, ..encodable.of_equiv α e } instance empty : primcodable empty := ⟨zero⟩ instance unit : primcodable punit := ⟨(cases1 1 zero).of_eq $ λ n, by cases n; simp⟩ instance option {α : Type*} [h : primcodable α] : primcodable (option α) := ⟨(cases1 1 ((cases1 0 (succ.comp succ)).comp (primcodable.prim α))).of_eq $ λ n, by cases n; simp; cases decode α n; refl⟩ instance bool : primcodable bool := ⟨(cases1 1 (cases1 2 zero)).of_eq $ λ n, begin cases n, {refl}, cases n, {refl}, rw decode_ge_two, {refl}, exact dec_trivial end⟩ end primcodable /-- `primrec f` means `f` is primitive recursive (after encoding its input and output as natural numbers). -/ def primrec {α β} [primcodable α] [primcodable β] (f : α → β) : Prop := nat.primrec (λ n, encode ((decode α n).map f)) namespace primrec variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] open nat.primrec protected theorem encode : primrec (@encode α _) := (primcodable.prim α).of_eq $ λ n, by cases decode α n; refl protected theorem decode : primrec (decode α) := succ.comp (primcodable.prim α) theorem dom_denumerable {α β} [denumerable α] [primcodable β] {f : α → β} : primrec f ↔ nat.primrec (λ n, encode (f (of_nat α n))) := ⟨λ h, (pred.comp h).of_eq $ λ n, by simp; refl, λ h, (succ.comp h).of_eq $ λ n, by simp; refl⟩ theorem nat_iff {f : ℕ → ℕ} : primrec f ↔ nat.primrec f := dom_denumerable theorem encdec : primrec (λ n, encode (decode α n)) := nat_iff.2 (primcodable.prim α) theorem option_some : primrec (@some α) := ((cases1 0 (succ.comp succ)).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; simp theorem of_eq {f g : α → σ} (hf : primrec f) (H : ∀ n, f n = g n) : primrec g := (funext H : f = g) ▸ hf theorem const (x : σ) : primrec (λ a : α, x) := ((cases1 0 (const (encode x).succ)).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; refl protected theorem id : primrec (@id α) := (primcodable.prim α).of_eq $ by simp theorem comp {f : β → σ} {g : α → β} (hf : primrec f) (hg : primrec g) : primrec (λ a, f (g a)) := ((cases1 0 (hf.comp $ pred.comp hg)).comp (primcodable.prim α)).of_eq $ λ n, begin cases decode α n, {refl}, simp [encodek] end theorem succ : primrec nat.succ := nat_iff.2 nat.primrec.succ theorem pred : primrec nat.pred := nat_iff.2 nat.primrec.pred theorem encode_iff {f : α → σ} : primrec (λ a, encode (f a)) ↔ primrec f := ⟨λ h, nat.primrec.of_eq h $ λ n, by cases decode α n; refl, primrec.encode.comp⟩ theorem of_nat_iff {α β} [denumerable α] [primcodable β] {f : α → β} : primrec f ↔ primrec (λ n, f (of_nat α n)) := dom_denumerable.trans $ nat_iff.symm.trans encode_iff protected theorem of_nat (α) [denumerable α] : primrec (of_nat α) := of_nat_iff.1 primrec.id theorem option_some_iff {f : α → σ} : primrec (λ a, some (f a)) ↔ primrec f := ⟨λ h, encode_iff.1 $ pred.comp $ encode_iff.2 h, option_some.comp⟩ theorem of_equiv {β} {e : β ≃ α} : by haveI := primcodable.of_equiv α e; exact primrec e := (primcodable.prim α).of_eq $ λ n, show _ = encode (option.map e (option.map _ _)), by cases decode α n; simp theorem of_equiv_symm {β} {e : β ≃ α} : by haveI := primcodable.of_equiv α e; exact primrec e.symm := by letI := primcodable.of_equiv α e; exact encode_iff.1 (show primrec (λ a, encode (e (e.symm a))), by simp [primrec.encode]) theorem of_equiv_iff {β} (e : β ≃ α) {f : σ → β} : by haveI := primcodable.of_equiv α e; exact primrec (λ a, e (f a)) ↔ primrec f := by letI := primcodable.of_equiv α e; exact ⟨λ h, (of_equiv_symm.comp h).of_eq (λ a, by simp), of_equiv.comp⟩ theorem of_equiv_symm_iff {β} (e : β ≃ α) {f : σ → α} : by haveI := primcodable.of_equiv α e; exact primrec (λ a, e.symm (f a)) ↔ primrec f := by letI := primcodable.of_equiv α e; exact ⟨λ h, (of_equiv.comp h).of_eq (λ a, by simp), of_equiv_symm.comp⟩ end primrec namespace primcodable open nat.primrec instance prod {α β} [primcodable α] [primcodable β] : primcodable (α × β) := ⟨((cases zero ((cases zero succ).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp [nat.unpaired], cases decode α n.unpair.1, { simp }, cases decode β n.unpair.2; simp end⟩ end primcodable namespace primrec variables {α : Type*} {σ : Type*} [primcodable α] [primcodable σ] open nat.primrec theorem fst {α β} [primcodable α] [primcodable β] : primrec (@prod.fst α β) := ((cases zero ((cases zero (nat.primrec.succ.comp left)).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp, cases decode α n.unpair.1; simp, cases decode β n.unpair.2; simp end theorem snd {α β} [primcodable α] [primcodable β] : primrec (@prod.snd α β) := ((cases zero ((cases zero (nat.primrec.succ.comp right)).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp, cases decode α n.unpair.1; simp, cases decode β n.unpair.2; simp end theorem pair {α β γ} [primcodable α] [primcodable β] [primcodable γ] {f : α → β} {g : α → γ} (hf : primrec f) (hg : primrec g) : primrec (λ a, (f a, g a)) := ((cases1 0 (nat.primrec.succ.comp $ pair (nat.primrec.pred.comp hf) (nat.primrec.pred.comp hg))).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; simp [encodek]; refl theorem unpair : primrec nat.unpair := (pair (nat_iff.2 nat.primrec.left) (nat_iff.2 nat.primrec.right)).of_eq $ λ n, by simp theorem list_nth₁ : ∀ (l : list α), primrec l.nth | [] := dom_denumerable.2 zero | (a::l) := dom_denumerable.2 $ (cases1 (encode a).succ $ dom_denumerable.1 $ list_nth₁ l).of_eq $ λ n, by cases n; simp end primrec /-- `primrec₂ f` means `f` is a binary primitive recursive function. This is technically unnecessary since we can always curry all the arguments together, but there are enough natural two-arg functions that it is convenient to express this directly. -/ def primrec₂ {α β σ} [primcodable α] [primcodable β] [primcodable σ] (f : α → β → σ) := primrec (λ p : α × β, f p.1 p.2) /-- `primrec_pred p` means `p : α → Prop` is a (decidable) primitive recursive predicate, which is to say that `to_bool ∘ p : α → bool` is primitive recursive. -/ def primrec_pred {α} [primcodable α] (p : α → Prop) [decidable_pred p] := primrec (λ a, to_bool (p a)) /-- `primrec_rel p` means `p : α → β → Prop` is a (decidable) primitive recursive relation, which is to say that `to_bool ∘ p : α → β → bool` is primitive recursive. -/ def primrec_rel {α β} [primcodable α] [primcodable β] (s : α → β → Prop) [∀ a b, decidable (s a b)] := primrec₂ (λ a b, to_bool (s a b)) namespace primrec₂ variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] theorem of_eq {f g : α → β → σ} (hg : primrec₂ f) (H : ∀ a b, f a b = g a b) : primrec₂ g := (by funext a b; apply H : f = g) ▸ hg theorem const (x : σ) : primrec₂ (λ (a : α) (b : β), x) := primrec.const _ protected theorem pair : primrec₂ (@prod.mk α β) := primrec.pair primrec.fst primrec.snd theorem left : primrec₂ (λ (a : α) (b : β), a) := primrec.fst theorem right : primrec₂ (λ (a : α) (b : β), b) := primrec.snd theorem mkpair : primrec₂ nat.mkpair := by simp [primrec₂, primrec]; constructor theorem unpaired {f : ℕ → ℕ → α} : primrec (nat.unpaired f) ↔ primrec₂ f := ⟨λ h, by simpa using h.comp mkpair, λ h, h.comp primrec.unpair⟩ theorem unpaired' {f : ℕ → ℕ → ℕ} : nat.primrec (nat.unpaired f) ↔ primrec₂ f := primrec.nat_iff.symm.trans unpaired theorem encode_iff {f : α → β → σ} : primrec₂ (λ a b, encode (f a b)) ↔ primrec₂ f := primrec.encode_iff theorem option_some_iff {f : α → β → σ} : primrec₂ (λ a b, some (f a b)) ↔ primrec₂ f := primrec.option_some_iff theorem of_nat_iff {α β σ} [denumerable α] [denumerable β] [primcodable σ] {f : α → β → σ} : primrec₂ f ↔ primrec₂ (λ m n : ℕ, f (of_nat α m) (of_nat β n)) := (primrec.of_nat_iff.trans $ by simp).trans unpaired theorem uncurry {f : α → β → σ} : primrec (function.uncurry f) ↔ primrec₂ f := by rw [show function.uncurry f = λ (p : α × β), f p.1 p.2, from funext $ λ ⟨a, b⟩, rfl]; refl theorem curry {f : α × β → σ} : primrec₂ (function.curry f) ↔ primrec f := by rw [← uncurry, function.uncurry_curry] end primrec₂ section comp variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem primrec.comp₂ {f : γ → σ} {g : α → β → γ} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a b, f (g a b)) := hf.comp hg theorem primrec₂.comp {f : β → γ → σ} {g : α → β} {h : α → γ} (hf : primrec₂ f) (hg : primrec g) (hh : primrec h) : primrec (λ a, f (g a) (h a)) := hf.comp (hg.pair hh) theorem primrec₂.comp₂ {f : γ → δ → σ} {g : α → β → γ} {h : α → β → δ} (hf : primrec₂ f) (hg : primrec₂ g) (hh : primrec₂ h) : primrec₂ (λ a b, f (g a b) (h a b)) := hf.comp hg hh theorem primrec_pred.comp {p : β → Prop} [decidable_pred p] {f : α → β} : primrec_pred p → primrec f → primrec_pred (λ a, p (f a)) := primrec.comp theorem primrec_rel.comp {R : β → γ → Prop} [∀ a b, decidable (R a b)] {f : α → β} {g : α → γ} : primrec_rel R → primrec f → primrec g → primrec_pred (λ a, R (f a) (g a)) := primrec₂.comp theorem primrec_rel.comp₂ {R : γ → δ → Prop} [∀ a b, decidable (R a b)] {f : α → β → γ} {g : α → β → δ} : primrec_rel R → primrec₂ f → primrec₂ g → primrec_rel (λ a b, R (f a b) (g a b)) := primrec_rel.comp end comp theorem primrec_pred.of_eq {α} [primcodable α] {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (H : ∀ a, p a ↔ q a) : primrec_pred q := primrec.of_eq hp (λ a, to_bool_congr (H a)) theorem primrec_rel.of_eq {α β} [primcodable α] [primcodable β] {r s : α → β → Prop} [∀ a b, decidable (r a b)] [∀ a b, decidable (s a b)] (hr : primrec_rel r) (H : ∀ a b, r a b ↔ s a b) : primrec_rel s := primrec₂.of_eq hr (λ a b, to_bool_congr (H a b)) namespace primrec₂ variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] open nat.primrec theorem swap {f : α → β → σ} (h : primrec₂ f) : primrec₂ (function.swap f) := h.comp₂ primrec₂.right primrec₂.left theorem nat_iff {f : α → β → σ} : primrec₂ f ↔ nat.primrec (nat.unpaired $ λ m n : ℕ, encode $ (decode α m).bind $ λ a, (decode β n).map (f a)) := have ∀ (a : option α) (b : option β), option.map (λ (p : α × β), f p.1 p.2) (option.bind a (λ (a : α), option.map (prod.mk a) b)) = option.bind a (λ a, option.map (f a) b), by intros; cases a; [refl, {cases b; refl}], by simp [primrec₂, primrec, this] theorem nat_iff' {f : α → β → σ} : primrec₂ f ↔ primrec₂ (λ m n : ℕ, option.bind (decode α m) (λ a, option.map (f a) (decode β n))) := nat_iff.trans $ unpaired'.trans encode_iff end primrec₂ namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem to₂ {f : α × β → σ} (hf : primrec f) : primrec₂ (λ a b, f (a, b)) := hf.of_eq $ λ ⟨a, b⟩, rfl theorem nat_elim {f : α → β} {g : α → ℕ × β → β} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a (n : ℕ), n.elim (f a) (λ n IH, g a (n, IH))) := primrec₂.nat_iff.2 $ ((nat.primrec.cases nat.primrec.zero $ (nat.primrec.prec hf $ nat.primrec.comp hg $ nat.primrec.left.pair $ (nat.primrec.left.comp nat.primrec.right).pair $ nat.primrec.pred.comp $ nat.primrec.right.comp nat.primrec.right).comp $ nat.primrec.right.pair $ nat.primrec.right.comp nat.primrec.left).comp $ nat.primrec.id.pair $ (primcodable.prim α).comp nat.primrec.left).of_eq $ λ n, begin simp, cases decode α n.unpair.1 with a, {refl}, simp [encodek], induction n.unpair.2 with m; simp [encodek], simp [ih, encodek] end theorem nat_elim' {f : α → ℕ} {g : α → β} {h : α → ℕ × β → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).elim (g a) (λ n IH, h a (n, IH))) := (nat_elim hg hh).comp primrec.id hf theorem nat_elim₁ {f : ℕ → α → α} (a : α) (hf : primrec₂ f) : primrec (nat.elim a f) := nat_elim' primrec.id (const a) $ comp₂ hf primrec₂.right theorem nat_cases' {f : α → β} {g : α → ℕ → β} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a, nat.cases (f a) (g a)) := nat_elim hf $ hg.comp₂ primrec₂.left $ comp₂ fst primrec₂.right theorem nat_cases {f : α → ℕ} {g : α → β} {h : α → ℕ → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).cases (g a) (h a)) := (nat_cases' hg hh).comp primrec.id hf theorem nat_cases₁ {f : ℕ → α} (a : α) (hf : primrec f) : primrec (nat.cases a f) := nat_cases primrec.id (const a) (comp₂ hf primrec₂.right) theorem nat_iterate {f : α → ℕ} {g : α → β} {h : α → β → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (h a)^[f a] (g a)) := (nat_elim' hf hg (hh.comp₂ primrec₂.left $ snd.comp₂ primrec₂.right)).of_eq $ λ a, by induction f a; simp [*, function.iterate_succ'] theorem option_cases {o : α → option β} {f : α → σ} {g : α → β → σ} (ho : primrec o) (hf : primrec f) (hg : primrec₂ g) : @primrec _ σ _ _ (λ a, option.cases_on (o a) (f a) (g a)) := encode_iff.1 $ (nat_cases (encode_iff.2 ho) (encode_iff.2 hf) $ pred.comp₂ $ primrec₂.encode_iff.2 $ (primrec₂.nat_iff'.1 hg).comp₂ ((@primrec.encode α _).comp fst).to₂ primrec₂.right).of_eq $ λ a, by cases o a with b; simp [encodek]; refl theorem option_bind {f : α → option β} {g : α → β → option σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).bind (g a)) := (option_cases hf (const none) hg).of_eq $ λ a, by cases f a; refl theorem option_bind₁ {f : α → option σ} (hf : primrec f) : primrec (λ o, option.bind o f) := option_bind primrec.id (hf.comp snd).to₂ theorem option_map {f : α → option β} {g : α → β → σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).map (g a)) := option_bind hf (option_some.comp₂ hg) theorem option_map₁ {f : α → σ} (hf : primrec f) : primrec (option.map f) := option_map primrec.id (hf.comp snd).to₂ theorem option_iget [inhabited α] : primrec (@option.iget α _) := (option_cases primrec.id (const $ default α) primrec₂.right).of_eq $ λ o, by cases o; refl theorem option_is_some : primrec (@option.is_some α) := (option_cases primrec.id (const ff) (const tt).to₂).of_eq $ λ o, by cases o; refl theorem option_get_or_else : primrec₂ (@option.get_or_else α) := primrec.of_eq (option_cases primrec₂.left primrec₂.right primrec₂.right) $ λ ⟨o, a⟩, by cases o; refl theorem bind_decode_iff {f : α → β → option σ} : primrec₂ (λ a n, (decode β n).bind (f a)) ↔ primrec₂ f := ⟨λ h, by simpa [encodek] using h.comp fst ((@primrec.encode β _).comp snd), λ h, option_bind (primrec.decode.comp snd) $ h.comp (fst.comp fst) snd⟩ theorem map_decode_iff {f : α → β → σ} : primrec₂ (λ a n, (decode β n).map (f a)) ↔ primrec₂ f := bind_decode_iff.trans primrec₂.option_some_iff theorem nat_add : primrec₂ ((+) : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.add theorem nat_sub : primrec₂ (has_sub.sub : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.sub theorem nat_mul : primrec₂ ((*) : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.mul theorem cond {c : α → bool} {f : α → σ} {g : α → σ} (hc : primrec c) (hf : primrec f) (hg : primrec g) : primrec (λ a, cond (c a) (f a) (g a)) := (nat_cases (encode_iff.2 hc) hg (hf.comp fst).to₂).of_eq $ λ a, by cases c a; refl theorem ite {c : α → Prop} [decidable_pred c] {f : α → σ} {g : α → σ} (hc : primrec_pred c) (hf : primrec f) (hg : primrec g) : primrec (λ a, if c a then f a else g a) := by simpa using cond hc hf hg theorem nat_le : primrec_rel ((≤) : ℕ → ℕ → Prop) := (nat_cases nat_sub (const tt) (const ff).to₂).of_eq $ λ p, begin dsimp [function.swap], cases e : p.1 - p.2 with n, { simp [nat.sub_eq_zero_iff_le.1 e] }, { simp [not_le.2 (nat.lt_of_sub_eq_succ e)] } end theorem nat_min : primrec₂ (@min ℕ _) := ite nat_le fst snd theorem nat_max : primrec₂ (@max ℕ _) := ite (nat_le.comp primrec.snd primrec.fst) fst snd theorem dom_bool (f : bool → α) : primrec f := (cond primrec.id (const (f tt)) (const (f ff))).of_eq $ λ b, by cases b; refl theorem dom_bool₂ (f : bool → bool → α) : primrec₂ f := (cond fst ((dom_bool (f tt)).comp snd) ((dom_bool (f ff)).comp snd)).of_eq $ λ ⟨a, b⟩, by cases a; refl protected theorem bnot : primrec bnot := dom_bool _ protected theorem band : primrec₂ band := dom_bool₂ _ protected theorem bor : primrec₂ bor := dom_bool₂ _ protected theorem not {p : α → Prop} [decidable_pred p] (hp : primrec_pred p) : primrec_pred (λ a, ¬ p a) := (primrec.bnot.comp hp).of_eq $ λ n, by simp protected theorem and {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (hq : primrec_pred q) : primrec_pred (λ a, p a ∧ q a) := (primrec.band.comp hp hq).of_eq $ λ n, by simp protected theorem or {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (hq : primrec_pred q) : primrec_pred (λ a, p a ∨ q a) := (primrec.bor.comp hp hq).of_eq $ λ n, by simp protected theorem eq [decidable_eq α] : primrec_rel (@eq α) := have primrec_rel (λ a b : ℕ, a = b), from (primrec.and nat_le nat_le.swap).of_eq $ λ a, by simp [le_antisymm_iff], (this.comp₂ (primrec.encode.comp₂ primrec₂.left) (primrec.encode.comp₂ primrec₂.right)).of_eq $ λ a b, encode_injective.eq_iff theorem nat_lt : primrec_rel ((<) : ℕ → ℕ → Prop) := (nat_le.comp snd fst).not.of_eq $ λ p, by simp theorem option_guard {p : α → β → Prop} [∀ a b, decidable (p a b)] (hp : primrec_rel p) {f : α → β} (hf : primrec f) : primrec (λ a, option.guard (p a) (f a)) := ite (hp.comp primrec.id hf) (option_some_iff.2 hf) (const none) theorem option_orelse : primrec₂ ((<|>) : option α → option α → option α) := (option_cases fst snd (fst.comp fst).to₂).of_eq $ λ ⟨o₁, o₂⟩, by cases o₁; cases o₂; refl protected theorem decode2 : primrec (decode2 α) := option_bind primrec.decode $ option_guard ((@primrec.eq _ _ nat.decidable_eq).comp (encode_iff.2 snd) (fst.comp fst)) snd theorem list_find_index₁ {p : α → β → Prop} [∀ a b, decidable (p a b)] (hp : primrec_rel p) : ∀ (l : list β), primrec (λ a, l.find_index (p a)) | [] := const 0 | (a::l) := ite (hp.comp primrec.id (const a)) (const 0) (succ.comp (list_find_index₁ l)) theorem list_index_of₁ [decidable_eq α] (l : list α) : primrec (λ a, l.index_of a) := list_find_index₁ primrec.eq l theorem dom_fintype [fintype α] (f : α → σ) : primrec f := let ⟨l, nd, m⟩ := fintype.exists_univ_list α in option_some_iff.1 $ begin haveI := decidable_eq_of_encodable α, refine ((list_nth₁ (l.map f)).comp (list_index_of₁ l)).of_eq (λ a, _), rw [list.nth_map, list.nth_le_nth (list.index_of_lt_length.2 (m _)), list.index_of_nth_le]; refl end theorem nat_bodd_div2 : primrec nat.bodd_div2 := (nat_elim' primrec.id (const (ff, 0)) (((cond fst (pair (const ff) (succ.comp snd)) (pair (const tt) snd)).comp snd).comp snd).to₂).of_eq $ λ n, begin simp [-nat.bodd_div2_eq], induction n with n IH, {refl}, simp [-nat.bodd_div2_eq, nat.bodd_div2, *], rcases nat.bodd_div2 n with ⟨_|_, m⟩; simp [nat.bodd_div2] end theorem nat_bodd : primrec nat.bodd := fst.comp nat_bodd_div2 theorem nat_div2 : primrec nat.div2 := snd.comp nat_bodd_div2 theorem nat_bit0 : primrec (@bit0 ℕ _) := nat_add.comp primrec.id primrec.id theorem nat_bit1 : primrec (@bit1 ℕ _ _) := nat_add.comp nat_bit0 (const 1) theorem nat_bit : primrec₂ nat.bit := (cond primrec.fst (nat_bit1.comp primrec.snd) (nat_bit0.comp primrec.snd)).of_eq $ λ n, by cases n.1; refl theorem nat_div_mod : primrec₂ (λ n k : ℕ, (n / k, n % k)) := let f (a : ℕ × ℕ) : ℕ × ℕ := a.1.elim (0, 0) (λ _ IH, if nat.succ IH.2 = a.2 then (nat.succ IH.1, 0) else (IH.1, nat.succ IH.2)) in have hf : primrec f, from nat_elim' fst (const (0, 0)) $ ((ite ((@primrec.eq ℕ _ _).comp (succ.comp $ snd.comp snd) fst) (pair (succ.comp $ fst.comp snd) (const 0)) (pair (fst.comp snd) (succ.comp $ snd.comp snd))) .comp (pair (snd.comp fst) (snd.comp snd))).to₂, suffices ∀ k n, (n / k, n % k) = f (n, k), from hf.of_eq $ λ ⟨m, n⟩, by simp [this], λ k n, begin have : (f (n, k)).2 + k * (f (n, k)).1 = n ∧ (0 < k → (f (n, k)).2 < k) ∧ (k = 0 → (f (n, k)).1 = 0), { induction n with n IH, {exact ⟨rfl, id, λ _, rfl⟩}, rw [λ n:ℕ, show f (n.succ, k) = _root_.ite ((f (n, k)).2.succ = k) (nat.succ (f (n, k)).1, 0) ((f (n, k)).1, (f (n, k)).2.succ), from rfl], by_cases h : (f (n, k)).2.succ = k; simp [h], { have := congr_arg nat.succ IH.1, refine ⟨_, λ k0, nat.no_confusion (h.trans k0)⟩, rwa [← nat.succ_add, h, add_comm, ← nat.mul_succ] at this }, { exact ⟨by rw [nat.succ_add, IH.1], λ k0, lt_of_le_of_ne (IH.2.1 k0) h, IH.2.2⟩ } }, revert this, cases f (n, k) with D M, simp, intros h₁ h₂ h₃, cases nat.eq_zero_or_pos k, { simp [h, h₃ h] at h₁ ⊢, simp [h₁] }, { exact (nat.div_mod_unique h).2 ⟨h₁, h₂ h⟩ } end theorem nat_div : primrec₂ ((/) : ℕ → ℕ → ℕ) := fst.comp₂ nat_div_mod theorem nat_mod : primrec₂ ((%) : ℕ → ℕ → ℕ) := snd.comp₂ nat_div_mod end primrec section variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] variable (H : nat.primrec (λ n, encodable.encode (decode (list β) n))) include H open primrec private def prim : primcodable (list β) := ⟨H⟩ private lemma list_cases' {f : α → list β} {g : α → σ} {h : α → β × list β → σ} (hf : by haveI := prim H; exact primrec f) (hg : primrec g) (hh : by haveI := prim H; exact primrec₂ h) : @primrec _ σ _ _ (λ a, list.cases_on (f a) (g a) (λ b l, h a (b, l))) := by letI := prim H; exact have @primrec _ (option σ) _ _ (λ a, (decode (option (β × list β)) (encode (f a))).map (λ o, option.cases_on o (g a) (h a))), from ((@map_decode_iff _ (option (β × list β)) _ _ _ _ _).2 $ to₂ $ option_cases snd (hg.comp fst) (hh.comp₂ (fst.comp₂ primrec₂.left) primrec₂.right)) .comp primrec.id (encode_iff.2 hf), option_some_iff.1 $ this.of_eq $ λ a, by cases f a with b l; simp [encodek]; refl private lemma list_foldl' {f : α → list β} {g : α → σ} {h : α → σ × β → σ} (hf : by haveI := prim H; exact primrec f) (hg : primrec g) (hh : by haveI := prim H; exact primrec₂ h) : primrec (λ a, (f a).foldl (λ s b, h a (s, b)) (g a)) := by letI := prim H; exact let G (a : α) (IH : σ × list β) : σ × list β := list.cases_on IH.2 IH (λ b l, (h a (IH.1, b), l)) in let F (a : α) (n : ℕ) := (G a)^[n] (g a, f a) in have primrec (λ a, (F a (encode (f a))).1), from fst.comp $ nat_iterate (encode_iff.2 hf) (pair hg hf) $ list_cases' H (snd.comp snd) snd $ to₂ $ pair (hh.comp (fst.comp fst) $ pair ((fst.comp snd).comp fst) (fst.comp snd)) (snd.comp snd), this.of_eq $ λ a, begin have : ∀ n, F a n = ((list.take n (f a)).foldl (λ s b, h a (s, b)) (g a), list.drop n (f a)), { intro, simp [F], generalize : f a = l, generalize : g a = x, induction n with n IH generalizing l x, {refl}, simp, cases l with b l; simp [IH] }, rw [this, list.take_all_of_le (length_le_encode _)] end private lemma list_cons' : by haveI := prim H; exact primrec₂ (@list.cons β) := by letI := prim H; exact encode_iff.1 (succ.comp $ primrec₂.mkpair.comp (encode_iff.2 fst) (encode_iff.2 snd)) private lemma list_reverse' : by haveI := prim H; exact primrec (@list.reverse β) := by letI := prim H; exact (list_foldl' H primrec.id (const []) $ to₂ $ ((list_cons' H).comp snd fst).comp snd).of_eq (suffices ∀ l r, list.foldl (λ (s : list β) (b : β), b :: s) r l = list.reverse_core l r, from λ l, this l [], λ l, by induction l; simp [*, list.reverse_core]) end namespace primcodable variables {α : Type*} {β : Type*} variables [primcodable α] [primcodable β] open primrec instance sum : primcodable (α ⊕ β) := ⟨primrec.nat_iff.1 $ (encode_iff.2 (cond nat_bodd (((@primrec.decode β _).comp nat_div2).option_map $ to₂ $ nat_bit.comp (const tt) (primrec.encode.comp snd)) (((@primrec.decode α _).comp nat_div2).option_map $ to₂ $ nat_bit.comp (const ff) (primrec.encode.comp snd)))).of_eq $ λ n, show _ = encode (decode_sum n), begin simp [decode_sum], cases nat.bodd n; simp [decode_sum], { cases decode α n.div2; refl }, { cases decode β n.div2; refl } end⟩ instance list : primcodable (list α) := ⟨ by letI H := primcodable.prim (list ℕ); exact have primrec₂ (λ (a : α) (o : option (list ℕ)), o.map (list.cons (encode a))), from option_map snd $ (list_cons' H).comp ((@primrec.encode α _).comp (fst.comp fst)) snd, have primrec (λ n, (of_nat (list ℕ) n).reverse.foldl (λ o m, (decode α m).bind (λ a, o.map (list.cons (encode a)))) (some [])), from list_foldl' H ((list_reverse' H).comp (primrec.of_nat (list ℕ))) (const (some [])) (primrec.comp₂ (bind_decode_iff.2 $ primrec₂.swap this) primrec₂.right), nat_iff.1 $ (encode_iff.2 this).of_eq $ λ n, begin rw list.foldl_reverse, apply nat.case_strong_induction_on n, {refl}, intros n IH, simp, cases decode α n.unpair.1 with a, {refl}, simp, suffices : ∀ (o : option (list ℕ)) p (_ : encode o = encode p), encode (option.map (list.cons (encode a)) o) = encode (option.map (list.cons a) p), from this _ _ (IH _ (nat.unpair_le_right n)), intros o p IH, cases o; cases p; injection IH with h, exact congr_arg (λ k, (nat.mkpair (encode a) k).succ.succ) h end⟩ end primcodable namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem sum_inl : primrec (@sum.inl α β) := encode_iff.1 $ nat_bit0.comp primrec.encode theorem sum_inr : primrec (@sum.inr α β) := encode_iff.1 $ nat_bit1.comp primrec.encode theorem sum_cases {f : α → β ⊕ γ} {g : α → β → σ} {h : α → γ → σ} (hf : primrec f) (hg : primrec₂ g) (hh : primrec₂ h) : @primrec _ σ _ _ (λ a, sum.cases_on (f a) (g a) (h a)) := option_some_iff.1 $ (cond (nat_bodd.comp $ encode_iff.2 hf) (option_map (primrec.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hh) (option_map (primrec.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hg)).of_eq $ λ a, by cases f a with b c; simp [nat.div2_bit, nat.bodd_bit, encodek]; refl theorem list_cons : primrec₂ (@list.cons α) := list_cons' (primcodable.prim _) theorem list_cases {f : α → list β} {g : α → σ} {h : α → β × list β → σ} : primrec f → primrec g → primrec₂ h → @primrec _ σ _ _ (λ a, list.cases_on (f a) (g a) (λ b l, h a (b, l))) := list_cases' (primcodable.prim _) theorem list_foldl {f : α → list β} {g : α → σ} {h : α → σ × β → σ} : primrec f → primrec g → primrec₂ h → primrec (λ a, (f a).foldl (λ s b, h a (s, b)) (g a)) := list_foldl' (primcodable.prim _) theorem list_reverse : primrec (@list.reverse α) := list_reverse' (primcodable.prim _) theorem list_foldr {f : α → list β} {g : α → σ} {h : α → β × σ → σ} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).foldr (λ b s, h a (b, s)) (g a)) := (list_foldl (list_reverse.comp hf) hg $ to₂ $ hh.comp fst $ (pair snd fst).comp snd).of_eq $ λ a, by simp [list.foldl_reverse] theorem list_head' : primrec (@list.head' α) := (list_cases primrec.id (const none) (option_some_iff.2 $ (fst.comp snd)).to₂).of_eq $ λ l, by cases l; refl theorem list_head [inhabited α] : primrec (@list.head α _) := (option_iget.comp list_head').of_eq $ λ l, l.head_eq_head'.symm theorem list_tail : primrec (@list.tail α) := (list_cases primrec.id (const []) (snd.comp snd).to₂).of_eq $ λ l, by cases l; refl theorem list_rec {f : α → list β} {g : α → σ} {h : α → β × list β × σ → σ} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : @primrec _ σ _ _ (λ a, list.rec_on (f a) (g a) (λ b l IH, h a (b, l, IH))) := let F (a : α) := (f a).foldr (λ (b : β) (s : list β × σ), (b :: s.1, h a (b, s))) ([], g a) in have primrec F, from list_foldr hf (pair (const []) hg) $ to₂ $ pair ((list_cons.comp fst (fst.comp snd)).comp snd) hh, (snd.comp this).of_eq $ λ a, begin suffices : F a = (f a, list.rec_on (f a) (g a) (λ b l IH, h a (b, l, IH))), {rw this}, simp [F], induction f a with b l IH; simp * end theorem list_nth : primrec₂ (@list.nth α) := let F (l : list α) (n : ℕ) := l.foldl (λ (s : ℕ ⊕ α) (a : α), sum.cases_on s (@nat.cases (ℕ ⊕ α) (sum.inr a) sum.inl) sum.inr) (sum.inl n) in have hF : primrec₂ F, from list_foldl fst (sum_inl.comp snd) ((sum_cases fst (nat_cases snd (sum_inr.comp $ snd.comp fst) (sum_inl.comp snd).to₂).to₂ (sum_inr.comp snd).to₂).comp snd).to₂, have @primrec _ (option α) _ _ (λ p : list α × ℕ, sum.cases_on (F p.1 p.2) (λ _, none) some), from sum_cases hF (const none).to₂ (option_some.comp snd).to₂, this.to₂.of_eq $ λ l n, begin dsimp, symmetry, induction l with a l IH generalizing n, {refl}, cases n with n, { rw [(_ : F (a :: l) 0 = sum.inr a)], {refl}, clear IH, dsimp [F], induction l with b l IH; simp * }, { apply IH } end theorem list_inth [inhabited α] : primrec₂ (@list.inth α _) := option_iget.comp₂ list_nth theorem list_append : primrec₂ ((++) : list α → list α → list α) := (list_foldr fst snd $ to₂ $ comp (@list_cons α _) snd).to₂.of_eq $ λ l₁ l₂, by induction l₁; simp * theorem list_concat : primrec₂ (λ l (a:α), l ++ [a]) := list_append.comp fst (list_cons.comp snd (const [])) theorem list_map {f : α → list β} {g : α → β → σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).map (g a)) := (list_foldr hf (const []) $ to₂ $ list_cons.comp (hg.comp fst (fst.comp snd)) (snd.comp snd)).of_eq $ λ a, by induction f a; simp * theorem list_range : primrec list.range := (nat_elim' primrec.id (const []) ((list_concat.comp snd fst).comp snd).to₂).of_eq $ λ n, by simp; induction n; simp [*, list.range_concat]; refl theorem list_join : primrec (@list.join α) := (list_foldr primrec.id (const []) $ to₂ $ comp (@list_append α _) snd).of_eq $ λ l, by dsimp; induction l; simp * theorem list_length : primrec (@list.length α) := (list_foldr (@primrec.id (list α) _) (const 0) $ to₂ $ (succ.comp $ snd.comp snd).to₂).of_eq $ λ l, by dsimp; induction l; simp [*, -add_comm] theorem list_find_index {f : α → list β} {p : α → β → Prop} [∀ a b, decidable (p a b)] (hf : primrec f) (hp : primrec_rel p) : primrec (λ a, (f a).find_index (p a)) := (list_foldr hf (const 0) $ to₂ $ ite (hp.comp fst $ fst.comp snd) (const 0) (succ.comp $ snd.comp snd)).of_eq $ λ a, eq.symm $ by dsimp; induction f a with b l; [refl, simp [*, list.find_index]] theorem list_index_of [decidable_eq α] : primrec₂ (@list.index_of α _) := to₂ $ list_find_index snd $ primrec.eq.comp₂ (fst.comp fst).to₂ snd.to₂ theorem nat_strong_rec (f : α → ℕ → σ) {g : α → list σ → option σ} (hg : primrec₂ g) (H : ∀ a n, g a ((list.range n).map (f a)) = some (f a n)) : primrec₂ f := suffices primrec₂ (λ a n, (list.range n).map (f a)), from primrec₂.option_some_iff.1 $ (list_nth.comp (this.comp fst (succ.comp snd)) snd).to₂.of_eq $ λ a n, by simp [list.nth_range (nat.lt_succ_self n)]; refl, primrec₂.option_some_iff.1 $ (nat_elim (const (some [])) (to₂ $ option_bind (snd.comp snd) $ to₂ $ option_map (hg.comp (fst.comp fst) snd) (to₂ $ list_concat.comp (snd.comp fst) snd))).of_eq $ λ a n, begin simp, induction n with n IH, {refl}, simp [IH, H, list.range_concat] end end primrec namespace primcodable variables {α : Type*} {β : Type*} variables [primcodable α] [primcodable β] open primrec def subtype {p : α → Prop} [decidable_pred p] (hp : primrec_pred p) : primcodable (subtype p) := ⟨have primrec (λ n, (decode α n).bind (λ a, option.guard p a)), from option_bind primrec.decode (option_guard (hp.comp snd) snd), nat_iff.1 $ (encode_iff.2 this).of_eq $ λ n, show _ = encode ((decode α n).bind (λ a, _)), begin cases decode α n with a, {refl}, dsimp [option.guard], by_cases h : p a; simp [h]; refl end⟩ instance fin {n} : primcodable (fin n) := @of_equiv _ _ (subtype $ nat_lt.comp primrec.id (const n)) (equiv.fin_equiv_subtype _) instance vector {n} : primcodable (vector α n) := subtype ((@primrec.eq _ _ nat.decidable_eq).comp list_length (const _)) instance fin_arrow {n} : primcodable (fin n → α) := of_equiv _ (equiv.vector_equiv_fin _ _).symm instance array {n} : primcodable (array n α) := of_equiv _ (equiv.array_equiv_fin _ _) section ulower local attribute [instance, priority 100] encodable.decidable_range_encode encodable.decidable_eq_of_encodable instance ulower : primcodable (ulower α) := have primrec_pred (λ n, encodable.decode2 α n ≠ none), from primrec.not (primrec.eq.comp (primrec.option_bind primrec.decode (primrec.ite (primrec.eq.comp (primrec.encode.comp primrec.snd) primrec.fst) (primrec.option_some.comp primrec.snd) (primrec.const _))) (primrec.const _)), primcodable.subtype $ primrec_pred.of_eq this $ by simp [set.range, option.eq_none_iff_forall_not_mem, encodable.mem_decode2] end ulower end primcodable namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem subtype_val {p : α → Prop} [decidable_pred p] {hp : primrec_pred p} : by haveI := primcodable.subtype hp; exact primrec (@subtype.val α p) := begin letI := primcodable.subtype hp, refine (primcodable.prim (subtype p)).of_eq (λ n, _), rcases decode (subtype p) n with _|⟨a,h⟩; refl end theorem subtype_val_iff {p : β → Prop} [decidable_pred p] {hp : primrec_pred p} {f : α → subtype p} : by haveI := primcodable.subtype hp; exact primrec (λ a, (f a).1) ↔ primrec f := begin letI := primcodable.subtype hp, refine ⟨λ h, _, λ hf, subtype_val.comp hf⟩, refine nat.primrec.of_eq h (λ n, _), cases decode α n with a, {refl}, simp, cases f a; refl end theorem subtype_mk {p : β → Prop} [decidable_pred p] {hp : primrec_pred p} {f : α → β} {h : ∀ a, p (f a)} (hf : primrec f) : by haveI := primcodable.subtype hp; exact primrec (λ a, @subtype.mk β p (f a) (h a)) := subtype_val_iff.1 hf theorem option_get {f : α → option β} {h : ∀ a, (f a).is_some} : primrec f → primrec (λ a, option.get (h a)) := begin intro hf, refine (nat.primrec.pred.comp hf).of_eq (λ n, _), generalize hx : decode α n = x, cases x; simp end theorem ulower_down : primrec (ulower.down : α → ulower α) := by letI : ∀ a, decidable (a ∈ set.range (encode : α → ℕ)) := decidable_range_encode _; exact subtype_mk primrec.encode theorem ulower_up : primrec (ulower.up : ulower α → α) := by letI : ∀ a, decidable (a ∈ set.range (encode : α → ℕ)) := decidable_range_encode _; exact option_get (primrec.decode2.comp subtype_val) theorem fin_val_iff {n} {f : α → fin n} : primrec (λ a, (f a).1) ↔ primrec f := begin let : primcodable {a//id a<n}, swap, exactI (iff.trans (by refl) subtype_val_iff).trans (of_equiv_iff _) end theorem fin_val {n} : primrec (coe : fin n → ℕ) := fin_val_iff.2 primrec.id theorem fin_succ {n} : primrec (@fin.succ n) := fin_val_iff.1 $ by simp [succ.comp fin_val] theorem vector_to_list {n} : primrec (@vector.to_list α n) := subtype_val theorem vector_to_list_iff {n} {f : α → vector β n} : primrec (λ a, (f a).to_list) ↔ primrec f := subtype_val_iff theorem vector_cons {n} : primrec₂ (@vector.cons α n) := vector_to_list_iff.1 $ by simp; exact list_cons.comp fst (vector_to_list_iff.2 snd) theorem vector_length {n} : primrec (@vector.length α n) := const _ theorem vector_head {n} : primrec (@vector.head α n) := option_some_iff.1 $ (list_head'.comp vector_to_list).of_eq $ λ ⟨a::l, h⟩, rfl theorem vector_tail {n} : primrec (@vector.tail α n) := vector_to_list_iff.1 $ (list_tail.comp vector_to_list).of_eq $ λ ⟨l, h⟩, by cases l; refl theorem vector_nth {n} : primrec₂ (@vector.nth α n) := option_some_iff.1 $ (list_nth.comp (vector_to_list.comp fst) (fin_val.comp snd)).of_eq $ λ a, by simp [vector.nth_eq_nth_le]; rw [← list.nth_le_nth] theorem list_of_fn : ∀ {n} {f : fin n → α → σ}, (∀ i, primrec (f i)) → primrec (λ a, list.of_fn (λ i, f i a)) | 0 f hf := const [] | (n+1) f hf := by simp [list.of_fn_succ]; exact list_cons.comp (hf 0) (list_of_fn (λ i, hf i.succ)) theorem vector_of_fn {n} {f : fin n → α → σ} (hf : ∀ i, primrec (f i)) : primrec (λ a, vector.of_fn (λ i, f i a)) := vector_to_list_iff.1 $ by simp [list_of_fn hf] theorem vector_nth' {n} : primrec (@vector.nth α n) := of_equiv_symm theorem vector_of_fn' {n} : primrec (@vector.of_fn α n) := of_equiv theorem fin_app {n} : primrec₂ (@id (fin n → σ)) := (vector_nth.comp (vector_of_fn'.comp fst) snd).of_eq $ λ ⟨v, i⟩, by simp theorem fin_curry₁ {n} {f : fin n → α → σ} : primrec₂ f ↔ ∀ i, primrec (f i) := ⟨λ h i, h.comp (const i) primrec.id, λ h, (vector_nth.comp ((vector_of_fn h).comp snd) fst).of_eq $ λ a, by simp⟩ theorem fin_curry {n} {f : α → fin n → σ} : primrec f ↔ primrec₂ f := ⟨λ h, fin_app.comp (h.comp fst) snd, λ h, (vector_nth'.comp (vector_of_fn (λ i, show primrec (λ a, f a i), from h.comp primrec.id (const i)))).of_eq $ λ a, by funext i; simp⟩ end primrec namespace nat open vector /-- An alternative inductive definition of `primrec` which does not use the pairing function on ℕ, and so has to work with n-ary functions on ℕ instead of unary functions. We prove that this is equivalent to the regular notion in `to_prim` and `of_prim`. -/ inductive primrec' : ∀ {n}, (vector ℕ n → ℕ) → Prop | zero : @primrec' 0 (λ _, 0) | succ : @primrec' 1 (λ v, succ v.head) | nth {n} (i : fin n) : primrec' (λ v, v.nth i) | comp {m n f} (g : fin n → vector ℕ m → ℕ) : primrec' f → (∀ i, primrec' (g i)) → primrec' (λ a, f (of_fn (λ i, g i a))) | prec {n f g} : @primrec' n f → @primrec' (n+2) g → primrec' (λ v : vector ℕ (n+1), v.head.elim (f v.tail) (λ y IH, g (y :: IH :: v.tail))) end nat namespace nat.primrec' open vector primrec nat (primrec') nat.primrec' hide ite theorem to_prim {n f} (pf : @primrec' n f) : primrec f := begin induction pf, case nat.primrec'.zero { exact const 0 }, case nat.primrec'.succ { exact primrec.succ.comp vector_head }, case nat.primrec'.nth : n i { exact vector_nth.comp primrec.id (const i) }, case nat.primrec'.comp : m n f g _ _ hf hg { exact hf.comp (vector_of_fn (λ i, hg i)) }, case nat.primrec'.prec : n f g _ _ hf hg { exact nat_elim' vector_head (hf.comp vector_tail) (hg.comp $ vector_cons.comp (fst.comp snd) $ vector_cons.comp (snd.comp snd) $ (@vector_tail _ _ (n+1)).comp fst).to₂ }, end theorem of_eq {n} {f g : vector ℕ n → ℕ} (hf : primrec' f) (H : ∀ i, f i = g i) : primrec' g := (funext H : f = g) ▸ hf theorem const {n} : ∀ m, @primrec' n (λ v, m) | 0 := zero.comp fin.elim0 (λ i, i.elim0) | (m+1) := succ.comp _ (λ i, const m) theorem head {n : ℕ} : @primrec' n.succ head := (nth 0).of_eq $ λ v, by simp [nth_zero] theorem tail {n f} (hf : @primrec' n f) : @primrec' n.succ (λ v, f v.tail) := (hf.comp _ (λ i, @nth _ i.succ)).of_eq $ λ v, by rw [← of_fn_nth v.tail]; congr; funext i; simp def vec {n m} (f : vector ℕ n → vector ℕ m) := ∀ i, primrec' (λ v, (f v).nth i) protected theorem nil {n} : @vec n 0 (λ _, nil) := λ i, i.elim0 protected theorem cons {n m f g} (hf : @primrec' n f) (hg : @vec n m g) : vec (λ v, f v :: g v) := λ i, fin.cases (by simp *) (λ i, by simp [hg i]) i theorem idv {n} : @vec n n id := nth theorem comp' {n m f g} (hf : @primrec' m f) (hg : @vec n m g) : primrec' (λ v, f (g v)) := (hf.comp _ hg).of_eq $ λ v, by simp theorem comp₁ (f : ℕ → ℕ) (hf : @primrec' 1 (λ v, f v.head)) {n g} (hg : @primrec' n g) : primrec' (λ v, f (g v)) := hf.comp _ (λ i, hg) theorem comp₂ (f : ℕ → ℕ → ℕ) (hf : @primrec' 2 (λ v, f v.head v.tail.head)) {n g h} (hg : @primrec' n g) (hh : @primrec' n h) : primrec' (λ v, f (g v) (h v)) := by simpa using hf.comp' (hg.cons $ hh.cons primrec'.nil) theorem prec' {n f g h} (hf : @primrec' n f) (hg : @primrec' n g) (hh : @primrec' (n+2) h) : @primrec' n (λ v, (f v).elim (g v) (λ (y IH : ℕ), h (y :: IH :: v))) := by simpa using comp' (prec hg hh) (hf.cons idv) theorem pred : @primrec' 1 (λ v, v.head.pred) := (prec' head (const 0) head).of_eq $ λ v, by simp; cases v.head; refl theorem add : @primrec' 2 (λ v, v.head + v.tail.head) := (prec head (succ.comp₁ _ (tail head))).of_eq $ λ v, by simp; induction v.head; simp [*, nat.succ_add] theorem sub : @primrec' 2 (λ v, v.head - v.tail.head) := begin suffices, simpa using comp₂ (λ a b, b - a) this (tail head) head, refine (prec head (pred.comp₁ _ (tail head))).of_eq (λ v, _), simp, induction v.head; simp [*, nat.sub_succ] end theorem mul : @primrec' 2 (λ v, v.head * v.tail.head) := (prec (const 0) (tail (add.comp₂ _ (tail head) (head)))).of_eq $ λ v, by simp; induction v.head; simp [*, nat.succ_mul]; rw add_comm theorem if_lt {n a b f g} (ha : @primrec' n a) (hb : @primrec' n b) (hf : @primrec' n f) (hg : @primrec' n g) : @primrec' n (λ v, if a v < b v then f v else g v) := (prec' (sub.comp₂ _ hb ha) hg (tail $ tail hf)).of_eq $ λ v, begin cases e : b v - a v, { simp [not_lt.2 (nat.le_of_sub_eq_zero e)] }, { simp [nat.lt_of_sub_eq_succ e] } end theorem mkpair : @primrec' 2 (λ v, v.head.mkpair v.tail.head) := if_lt head (tail head) (add.comp₂ _ (tail $ mul.comp₂ _ head head) head) (add.comp₂ _ (add.comp₂ _ (mul.comp₂ _ head head) head) (tail head)) protected theorem encode : ∀ {n}, @primrec' n encode | 0 := (const 0).of_eq (λ v, by rw v.eq_nil; refl) | (n+1) := (succ.comp₁ _ (mkpair.comp₂ _ head (tail encode))) .of_eq $ λ ⟨a::l, e⟩, rfl theorem sqrt : @primrec' 1 (λ v, v.head.sqrt) := begin suffices H : ∀ n : ℕ, n.sqrt = n.elim 0 (λ x y, if x.succ < y.succ*y.succ then y else y.succ), { simp [H], have := @prec' 1 _ _ (λ v, by have x := v.head; have y := v.tail.head; from if x.succ < y.succ*y.succ then y else y.succ) head (const 0) _, { convert this, funext, congr, funext x y, congr; simp }, have x1 := succ.comp₁ _ head, have y1 := succ.comp₁ _ (tail head), exact if_lt x1 (mul.comp₂ _ y1 y1) (tail head) y1 }, intro, symmetry, induction n with n IH, {refl}, dsimp, rw IH, split_ifs, { exact le_antisymm (nat.sqrt_le_sqrt (nat.le_succ _)) (nat.lt_succ_iff.1 $ nat.sqrt_lt.2 h) }, { exact nat.eq_sqrt.2 ⟨not_lt.1 h, nat.sqrt_lt.1 $ nat.lt_succ_iff.2 $ nat.sqrt_succ_le_succ_sqrt _⟩ }, end theorem unpair₁ {n f} (hf : @primrec' n f) : @primrec' n (λ v, (f v).unpair.1) := begin have s := sqrt.comp₁ _ hf, have fss := sub.comp₂ _ hf (mul.comp₂ _ s s), refine (if_lt fss s fss s).of_eq (λ v, _), simp [nat.unpair], split_ifs; refl end theorem unpair₂ {n f} (hf : @primrec' n f) : @primrec' n (λ v, (f v).unpair.2) := begin have s := sqrt.comp₁ _ hf, have fss := sub.comp₂ _ hf (mul.comp₂ _ s s), refine (if_lt fss s s (sub.comp₂ _ fss s)).of_eq (λ v, _), simp [nat.unpair], split_ifs; refl end theorem of_prim : ∀ {n f}, primrec f → @primrec' n f := suffices ∀ f, nat.primrec f → @primrec' 1 (λ v, f v.head), from λ n f hf, (pred.comp₁ _ $ (this _ hf).comp₁ (λ m, encodable.encode $ (decode (vector ℕ n) m).map f) primrec'.encode).of_eq (λ i, by simp [encodek]), λ f hf, begin induction hf, case nat.primrec.zero { exact const 0 }, case nat.primrec.succ { exact succ }, case nat.primrec.left { exact unpair₁ head }, case nat.primrec.right { exact unpair₂ head }, case nat.primrec.pair : f g _ _ hf hg { exact mkpair.comp₂ _ hf hg }, case nat.primrec.comp : f g _ _ hf hg { exact hf.comp₁ _ hg }, case nat.primrec.prec : f g _ _ hf hg { simpa using prec' (unpair₂ head) (hf.comp₁ _ (unpair₁ head)) (hg.comp₁ _ $ mkpair.comp₂ _ (unpair₁ $ tail $ tail head) (mkpair.comp₂ _ head (tail head))) }, end theorem prim_iff {n f} : @primrec' n f ↔ primrec f := ⟨to_prim, of_prim⟩ theorem prim_iff₁ {f : ℕ → ℕ} : @primrec' 1 (λ v, f v.head) ↔ primrec f := prim_iff.trans ⟨ λ h, (h.comp $ vector_of_fn $ λ i, primrec.id).of_eq (λ v, by simp), λ h, h.comp vector_head⟩ theorem prim_iff₂ {f : ℕ → ℕ → ℕ} : @primrec' 2 (λ v, f v.head v.tail.head) ↔ primrec₂ f := prim_iff.trans ⟨ λ h, (h.comp $ vector_cons.comp fst $ vector_cons.comp snd (primrec.const nil)).of_eq (λ v, by simp), λ h, h.comp vector_head (vector_head.comp vector_tail)⟩ theorem vec_iff {m n f} : @vec m n f ↔ primrec f := ⟨λ h, by simpa using vector_of_fn (λ i, to_prim (h i)), λ h i, of_prim $ vector_nth.comp h (primrec.const i)⟩ end nat.primrec' theorem primrec.nat_sqrt : primrec nat.sqrt := nat.primrec'.prim_iff₁.1 nat.primrec'.sqrt
d1fa8569eb85cdd194e4ac844525455d017457f5
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/real/nnreal.lean
3bdef02f30cce5c47aec902f9e6084bc94b3b3b4
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
25,752
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import algebra.linear_ordered_comm_group_with_zero import algebra.big_operators.ring import data.real.basic import data.indicator_function noncomputable theory open_locale classical big_operators /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : nnreal) : n.val = n := rfl instance : can_lift ℝ nnreal := { coe := coe, cond := λ r, r ≥ 0, prf := λ x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≤ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : nnreal) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := subtype.ext_iff_val.symm @[simp, norm_cast] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp, norm_cast] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp, norm_cast] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp, norm_cast] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp, norm_cast] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by norm_cast lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast instance : comm_semiring ℝ≥0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero, add_comm, add_left_comm] } end /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl instance : comm_group_with_zero ℝ≥0 := { exists_pair_ne := ⟨0, 1, assume h, zero_ne_one $ nnreal.eq_iff.2 h⟩, inv_zero := nnreal.eq $ show (0⁻¹ : ℝ) = 0, from inv_zero, mul_inv_cancel := assume x h, nnreal.eq $ mul_inv_cancel $ ne_iff.2 h, .. (by apply_instance : has_inv ℝ≥0), .. (_ : comm_semiring ℝ≥0), .. (_ : semiring ℝ≥0) } @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (λ x, f x) a := (to_real_hom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[simp, norm_cast] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ @[norm_cast] lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n •ℕ r) = n •ℕ (r:ℝ) := to_real_hom.to_add_monoid_hom.map_nsmul _ _ @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := to_real_hom.map_nat_cast n instance : decidable_linear_order ℝ≥0 := decidable_linear_order.lift (coe : ℝ≥0 → ℝ) subtype.val_injective @[norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := λ x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝ≥0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 /-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (λ _, of_real_coe) instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.decidable_linear_order } instance : canonically_ordered_add_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.decidable_linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_lt_one := @zero_lt_one ℝ _, .. nnreal.decidable_linear_order, .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring } instance : linear_ordered_comm_group_with_zero ℝ≥0 := { mul_le_mul_left := assume a b h c, mul_le_mul (le_refl c) h (zero_le a) (zero_le c), zero_le_one := zero_le 1, .. nnreal.linear_ordered_semiring, .. nnreal.comm_group_with_zero } instance : canonically_ordered_comm_semiring ℝ≥0 := { .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring, .. (show no_zero_divisors ℝ≥0, by apply_instance), .. nnreal.comm_group_with_zero } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := dense h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : nnreal → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : nnreal → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : nnreal → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : nnreal → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set nnreal) : (↑(Sup s) : ℝ) = Sup ((coe : nnreal → ℝ) '' s) := rfl lemma coe_Inf (s : set nnreal) : (↑(Inf s) : ℝ) = Inf ((coe : nnreal → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : nnreal → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : nnreal → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_decidable_linear_order, .. nnreal.order_bot } instance : archimedean nnreal := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (n •ℕ y : nnreal), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : nnreal} (h : ∀ε, ε > 0 → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : nnreal) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : nnreal) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, norm_cast] lemma coe_max (x y : nnreal) : ((max x y : nnreal) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : nnreal) : ((min x y : nnreal) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : nnreal} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : nnreal} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : nnreal} {p : ℝ} (hp : p ≥ 0) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : nnreal} (ha : r ≥ 0) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : nnreal} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : nnreal} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact mul_left_cancel' (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, have := max_eq_left (mul_nonneg hp hq), simpa [nnreal.of_real, hp, hq, max_eq_left] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end @[field_simps] theorem mul_ne_zero' {a b : nnreal} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 := mul_ne_zero h₁ h₂ end mul section sub lemma sub_def {r p : ℝ≥0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝ≥0} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝ≥0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝ≥0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝ≥0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : nnreal} : 0 < r → 0 < p → r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : nnreal} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝ≥0} : r - p ≤ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : nnreal} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : nnreal} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : nnreal} (h : b ≤ a) : (a - b) + b = a := nnreal.eq $ by rw [nnreal.coe_add, nnreal.coe_sub h, sub_add_cancel] lemma sub_sub_cancel_of_le {r p : ℝ≥0} (h : r ≤ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : nnreal} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : nnreal) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≤ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end end sub section inv lemma div_def {r p : nnreal} : r / p = r * p⁻¹ := rfl lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := by simp only [nnreal.div_def, finset.sum_mul] @[simp] lemma inv_zero : (0 : nnreal)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : nnreal} : (r : nnreal)⁻¹ = 0 ↔ r = 0 := inv_eq_zero @[simp] lemma inv_pos {r : nnreal} : 0 < r⁻¹ ↔ 0 < r := by simp [zero_lt_iff_ne_zero] lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := mul_pos hr (inv_pos.2 hp) @[simp] lemma inv_one : (1:ℝ≥0)⁻¹ = 1 := nnreal.eq $ inv_one @[simp] lemma div_one {r : ℝ≥0} : r / 1 = r := by rw [div_def, inv_one, mul_one] protected lemma mul_inv {r p : ℝ≥0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv_rev' _ _ protected lemma inv_pow {r : ℝ≥0} {n : ℕ} : (r^n)⁻¹ = (r⁻¹)^n := nnreal.eq $ by { push_cast, exact (inv_pow' _ _).symm } @[simp] lemma inv_mul_cancel {r : ℝ≥0} (h : r ≠ 0) : r⁻¹ * r = 1 := nnreal.eq $ inv_mul_cancel $ mt (@nnreal.eq_iff r 0).1 h @[simp] lemma mul_inv_cancel {r : ℝ≥0} (h : r ≠ 0) : r * r⁻¹ = 1 := by rw [mul_comm, inv_mul_cancel h] @[simp] lemma div_self {r : ℝ≥0} (h : r ≠ 0) : r / r = 1 := mul_inv_cancel h lemma div_self_le (r : ℝ≥0) : r / r ≤ 1 := if h : r = 0 then by simp [h] else by rw [div_self h] @[simp] lemma mul_div_cancel {r p : ℝ≥0} (h : p ≠ 0) : r * p / p = r := by rw [div_def, mul_assoc, mul_inv_cancel h, mul_one] @[simp] lemma mul_div_cancel' {r p : ℝ≥0} (h : r ≠ 0) : r * (p / r) = p := by rw [mul_comm, div_mul_cancel _ h] @[simp] lemma inv_inv {r : ℝ≥0} : r⁻¹⁻¹ = r := nnreal.eq (inv_inv' _) @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_def, mul_comm, ← mul_le_iff_le_inv hr, mul_comm] lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := @div_le_iff ℝ _ a r b $ zero_lt_iff_ne_zero.2 hr lemma le_of_forall_lt_one_mul_lt {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa [div_def] using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝ≥0} (hc : c ≠ 0) : b / c < a ↔ b < a * c := begin rw [← nnreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_div, nnreal.coe_mul], exact div_lt_iff (zero_lt_iff_ne_zero.mpr hc) end lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] theorem div_pow {a b : ℝ≥0} (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := div_pow _ _ _ @[field_simps] lemma mul_div_assoc' (a b c : ℝ≥0) : a * (b / c) = (a * b) / c := by rw [div_def, div_def, mul_assoc] @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma inv_eq_one_div (a : ℝ≥0) : a⁻¹ = 1/a := by rw [div_def, one_mul] @[field_simps] lemma div_mul_eq_mul_div (a b c : ℝ≥0) : (a / b) * c = (a * c) / b := by { rw [div_def, div_def], ac_refl } @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma one_div_eq_inv (a : ℝ≥0) : 1 / a = a⁻¹ := one_mul a⁻¹ lemma one_div_div (a b : ℝ≥0) : 1 / (a / b) = b / a := by { rw ← nnreal.eq_iff, simp [one_div_div] } lemma div_eq_mul_one_div (a b : ℝ≥0) : a / b = a * (1 / b) := by rw [div_def, div_def, one_mul] @[field_simps] lemma div_div_eq_mul_div (a b c : ℝ≥0) : a / (b / c) = (a * c) / b := by { rw ← nnreal.eq_iff, simp [div_div_eq_mul_div] } @[field_simps] lemma div_div_eq_div_mul (a b c : ℝ≥0) : (a / b) / c = a / (b * c) := by { rw ← nnreal.eq_iff, simp [div_div_eq_div_mul] } @[field_simps] lemma div_eq_div_iff {a b c d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := div_eq_div_iff hb hd @[field_simps] lemma div_eq_iff {a b c : ℝ≥0} (hb : b ≠ 0) : a / b = c ↔ a = c * b := by simpa using @div_eq_div_iff a b c 1 hb one_ne_zero @[field_simps] lemma eq_div_iff {a b c : ℝ≥0} (hb : b ≠ 0) : c = a / b ↔ c * b = a := by simpa using @div_eq_div_iff c 1 a b one_ne_zero hb end inv section pow theorem pow_eq_zero {a : ℝ≥0} {n : ℕ} (h : a^n = 0) : a = 0 := begin rw ← nnreal.eq_iff, rw [← nnreal.eq_iff, coe_pow] at h, exact pow_eq_zero h end @[field_simps] theorem pow_ne_zero {a : ℝ≥0} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h end pow end nnreal
04d23fb7acd42d370f2165b671ec73fcc9b2e849
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/topology/algebra/group_completion.lean
eb78726e595e192f0d46e464ac02155a9450dcd9
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
4,854
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl Completion of topological groups: -/ import topology.uniform_space.completion import topology.algebra.uniform_group noncomputable theory universes u v section group open uniform_space Cauchy filter set variables {α : Type u} [uniform_space α] instance [has_zero α] : has_zero (completion α) := ⟨(0 : α)⟩ instance [has_neg α] : has_neg (completion α) := ⟨completion.map (λa, -a : α → α)⟩ instance [has_add α] : has_add (completion α) := ⟨completion.map₂ (+)⟩ instance [has_sub α] : has_sub (completion α) := ⟨completion.map₂ has_sub.sub⟩ -- TODO: switch sides once #1103 is fixed @[norm_cast] lemma uniform_space.completion.coe_zero [has_zero α] : ((0 : α) : completion α) = 0 := rfl end group namespace uniform_space.completion section uniform_add_group open uniform_space uniform_space.completion variables {α : Type*} [uniform_space α] [add_group α] [uniform_add_group α] @[norm_cast] lemma coe_neg (a : α) : ((- a : α) : completion α) = - a := (map_coe uniform_continuous_neg a).symm @[norm_cast] lemma coe_sub (a b : α) : ((a - b : α) : completion α) = a - b := (map₂_coe_coe a b has_sub.sub uniform_continuous_sub).symm @[norm_cast] lemma coe_add (a b : α) : ((a + b : α) : completion α) = a + b := (map₂_coe_coe a b (+) uniform_continuous_add).symm instance : add_monoid (completion α) := { zero_add := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ continuous_const continuous_id) continuous_id) (assume a, show 0 + (a : completion α) = a, by rw_mod_cast zero_add), add_zero := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ continuous_id continuous_const) continuous_id) (assume a, show (a : completion α) + 0 = a, by rw_mod_cast add_zero), add_assoc := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous_map₂ (continuous_map₂ continuous_fst (continuous_fst.comp continuous_snd)) (continuous_snd.comp continuous_snd)) (continuous_map₂ continuous_fst (continuous_map₂ (continuous_fst.comp continuous_snd) (continuous_snd.comp continuous_snd)))) (assume a b c, show (a : completion α) + b + c = a + (b + c), by repeat { rw_mod_cast add_assoc }), .. completion.has_zero, .. completion.has_neg, ..completion.has_add, .. completion.has_sub } instance : sub_neg_monoid (completion α) := { sub_eq_add_neg := λ a b, completion.induction_on₂ a b (is_closed_eq (continuous_map₂ continuous_fst continuous_snd) (continuous_map₂ continuous_fst (continuous_map.comp continuous_snd))) (λ a b, by exact_mod_cast congr_arg coe (sub_eq_add_neg a b)), .. completion.add_monoid, .. completion.has_neg, .. completion.has_sub } instance : add_group (completion α) := { add_left_neg := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ completion.continuous_map continuous_id) continuous_const) (assume a, show - (a : completion α) + a = 0, by { rw_mod_cast add_left_neg, refl }), .. completion.sub_neg_monoid } instance : uniform_add_group (completion α) := ⟨uniform_continuous_map₂ has_sub.sub⟩ instance is_add_group_hom_coe : is_add_group_hom (coe : α → completion α) := { map_add := coe_add } variables {β : Type v} [uniform_space β] [add_group β] [uniform_add_group β] lemma is_add_group_hom_extension [complete_space β] [separated_space β] {f : α → β} [is_add_group_hom f] (hf : continuous f) : is_add_group_hom (completion.extension f) := have hf : uniform_continuous f, from uniform_continuous_of_continuous hf, { map_add := assume a b, completion.induction_on₂ a b (is_closed_eq (continuous_extension.comp continuous_add) ((continuous_extension.comp continuous_fst).add (continuous_extension.comp continuous_snd))) (λ a b, by rw_mod_cast [extension_coe hf, extension_coe hf, extension_coe hf, is_add_hom.map_add f]) } lemma is_add_group_hom_map {f : α → β} [is_add_group_hom f] (hf : continuous f) : is_add_group_hom (completion.map f) := @is_add_group_hom_extension _ _ _ _ _ _ _ _ _ _ _ (is_add_group_hom.comp _ _) ((continuous_coe _).comp hf) instance {α : Type u} [uniform_space α] [add_comm_group α] [uniform_add_group α] : add_comm_group (completion α) := { add_comm := assume a b, completion.induction_on₂ a b (is_closed_eq (continuous_map₂ continuous_fst continuous_snd) (continuous_map₂ continuous_snd continuous_fst)) (assume x y, by { change ↑x + ↑y = ↑y + ↑x, rw [← coe_add, ← coe_add, add_comm]}), .. completion.add_group } end uniform_add_group end uniform_space.completion
e1d2b28af7dc4a9d789256e6ee5d669c5c88b9ac
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/pfun.lean
131feabd7622eba37617afcd25b44123714791f9
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
24,922
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Jeremy Avigad, Simon Hudon -/ import data.rel /-- `roption α` is the type of "partial values" of type `α`. It is similar to `option α` except the domain condition can be an arbitrary proposition, not necessarily decidable. -/ structure {u} roption (α : Type u) : Type u := (dom : Prop) (get : dom → α) namespace roption variables {α : Type*} {β : Type*} {γ : Type*} /-- Convert an `roption α` with a decidable domain to an option -/ def to_option (o : roption α) [decidable o.dom] : option α := if h : dom o then some (o.get h) else none /-- `roption` extensionality -/ theorem ext' : ∀ {o p : roption α} (H1 : o.dom ↔ p.dom) (H2 : ∀h₁ h₂, o.get h₁ = p.get h₂), o = p | ⟨od, o⟩ ⟨pd, p⟩ H1 H2 := have t : od = pd, from propext H1, by cases t; rw [show o = p, from funext $ λp, H2 p p] /-- `roption` eta expansion -/ @[simp] theorem eta : Π (o : roption α), (⟨o.dom, λ h, o.get h⟩ : roption α) = o | ⟨h, f⟩ := rfl /-- `a ∈ o` means that `o` is defined and equal to `a` -/ protected def mem (a : α) (o : roption α) : Prop := ∃ h, o.get h = a instance : has_mem α (roption α) := ⟨roption.mem⟩ theorem mem_eq (a : α) (o : roption α) : (a ∈ o) = (∃ h, o.get h = a) := rfl theorem dom_iff_mem : ∀ {o : roption α}, o.dom ↔ ∃y, y ∈ o | ⟨p, f⟩ := ⟨λh, ⟨f h, h, rfl⟩, λ⟨_, h, rfl⟩, h⟩ theorem get_mem {o : roption α} (h) : get o h ∈ o := ⟨_, rfl⟩ /-- `roption` extensionality -/ @[ext] theorem ext {o p : roption α} (H : ∀ a, a ∈ o ↔ a ∈ p) : o = p := ext' ⟨λ h, ((H _).1 ⟨h, rfl⟩).fst, λ h, ((H _).2 ⟨h, rfl⟩).fst⟩ $ λ a b, ((H _).2 ⟨_, rfl⟩).snd /-- The `none` value in `roption` has a `false` domain and an empty function. -/ def none : roption α := ⟨false, false.rec _⟩ instance : inhabited (roption α) := ⟨none⟩ @[simp] theorem not_mem_none (a : α) : a ∉ @none α := λ h, h.fst /-- The `some a` value in `roption` has a `true` domain and the function returns `a`. -/ def some (a : α) : roption α := ⟨true, λ_, a⟩ theorem mem_unique : ∀ {a b : α} {o : roption α}, a ∈ o → b ∈ o → a = b | _ _ ⟨p, f⟩ ⟨h₁, rfl⟩ ⟨h₂, rfl⟩ := rfl theorem mem.left_unique : relator.left_unique ((∈) : α → roption α → Prop) := ⟨λ a o b, mem_unique⟩ theorem get_eq_of_mem {o : roption α} {a} (h : a ∈ o) (h') : get o h' = a := mem_unique ⟨_, rfl⟩ h @[simp] theorem get_some {a : α} (ha : (some a).dom) : get (some a) ha = a := rfl theorem mem_some (a : α) : a ∈ some a := ⟨trivial, rfl⟩ @[simp] theorem mem_some_iff {a b} : b ∈ (some a : roption α) ↔ b = a := ⟨λ⟨h, e⟩, e.symm, λ e, ⟨trivial, e.symm⟩⟩ theorem eq_some_iff {a : α} {o : roption α} : o = some a ↔ a ∈ o := ⟨λ e, e.symm ▸ mem_some _, λ ⟨h, e⟩, e ▸ ext' (iff_true_intro h) (λ _ _, rfl)⟩ theorem eq_none_iff {o : roption α} : o = none ↔ ∀ a, a ∉ o := ⟨λ e, e.symm ▸ not_mem_none, λ h, ext (by simpa [not_mem_none])⟩ theorem eq_none_iff' {o : roption α} : o = none ↔ ¬ o.dom := ⟨λ e, e.symm ▸ id, λ h, eq_none_iff.2 (λ a h', h h'.fst)⟩ lemma some_ne_none (x : α) : some x ≠ none := by { intro h, change none.dom, rw [← h], trivial } lemma ne_none_iff {o : roption α} : o ≠ none ↔ ∃x, o = some x := begin split, { rw [ne, eq_none_iff], intro h, push_neg at h, cases h with x hx, use x, rwa [eq_some_iff] }, { rintro ⟨x, rfl⟩, apply some_ne_none } end lemma eq_none_or_eq_some (o : roption α) : o = none ∨ ∃ x, o = some x := begin classical, by_cases h : o.dom, { rw dom_iff_mem at h, right, apply exists_imp_exists _ h, simp [eq_some_iff] }, { rw eq_none_iff', exact or.inl h }, end @[simp] lemma some_inj {a b : α} : roption.some a = some b ↔ a = b := function.injective.eq_iff (λ a b h, congr_fun (eq_of_heq (roption.mk.inj h).2) trivial) @[simp] lemma some_get {a : roption α} (ha : a.dom) : roption.some (roption.get a ha) = a := eq.symm (eq_some_iff.2 ⟨ha, rfl⟩) lemma get_eq_iff_eq_some {a : roption α} {ha : a.dom} {b : α} : a.get ha = b ↔ a = some b := ⟨λ h, by simp [h.symm], λ h, by simp [h]⟩ lemma get_eq_get_of_eq (a : roption α) (ha : a.dom) {b : roption α} (h : a = b) : a.get ha = b.get (h ▸ ha) := by { congr, exact h } instance none_decidable : decidable (@none α).dom := decidable.false instance some_decidable (a : α) : decidable (some a).dom := decidable.true def get_or_else (a : roption α) [decidable a.dom] (d : α) := if ha : a.dom then a.get ha else d @[simp] lemma get_or_else_none (d : α) : get_or_else none d = d := dif_neg id @[simp] lemma get_or_else_some (a : α) (d : α) : get_or_else (some a) d = a := dif_pos trivial @[simp] theorem mem_to_option {o : roption α} [decidable o.dom] {a : α} : a ∈ to_option o ↔ a ∈ o := begin unfold to_option, by_cases h : o.dom; simp [h], { exact ⟨λ h, ⟨_, h⟩, λ ⟨_, h⟩, h⟩ }, { exact mt Exists.fst h } end /-- Convert an `option α` into an `roption α` -/ def of_option : option α → roption α | option.none := none | (option.some a) := some a @[simp] theorem mem_of_option {a : α} : ∀ {o : option α}, a ∈ of_option o ↔ a ∈ o | option.none := ⟨λ h, h.fst.elim, λ h, option.no_confusion h⟩ | (option.some b) := ⟨λ h, congr_arg option.some h.snd, λ h, ⟨trivial, option.some.inj h⟩⟩ @[simp] theorem of_option_dom {α} : ∀ (o : option α), (of_option o).dom ↔ o.is_some | option.none := by simp [of_option, none] | (option.some a) := by simp [of_option] theorem of_option_eq_get {α} (o : option α) : of_option o = ⟨_, @option.get _ o⟩ := roption.ext' (of_option_dom o) $ λ h₁ h₂, by cases o; [cases h₁, refl] instance : has_coe (option α) (roption α) := ⟨of_option⟩ @[simp] theorem mem_coe {a : α} {o : option α} : a ∈ (o : roption α) ↔ a ∈ o := mem_of_option @[simp] theorem coe_none : (@option.none α : roption α) = none := rfl @[simp] theorem coe_some (a : α) : (option.some a : roption α) = some a := rfl @[elab_as_eliminator] protected lemma induction_on {P : roption α → Prop} (a : roption α) (hnone : P none) (hsome : ∀ a : α, P (some a)) : P a := (classical.em a.dom).elim (λ h, roption.some_get h ▸ hsome _) (λ h, (eq_none_iff'.2 h).symm ▸ hnone) instance of_option_decidable : ∀ o : option α, decidable (of_option o).dom | option.none := roption.none_decidable | (option.some a) := roption.some_decidable a @[simp] theorem to_of_option (o : option α) : to_option (of_option o) = o := by cases o; refl @[simp] theorem of_to_option (o : roption α) [decidable o.dom] : of_option (to_option o) = o := ext $ λ a, mem_of_option.trans mem_to_option noncomputable def equiv_option : roption α ≃ option α := by haveI := classical.dec; exact ⟨λ o, to_option o, of_option, λ o, of_to_option o, λ o, eq.trans (by dsimp; congr) (to_of_option o)⟩ instance : order_bot (roption α) := { le := λ x y, ∀ i, i ∈ x → i ∈ y, le_refl := λ x y, id, le_trans := λ x y z f g i, g _ ∘ f _, le_antisymm := λ x y f g, roption.ext $ λ z, ⟨f _, g _⟩, bot := none, bot_le := by { introv x, rintro ⟨⟨_⟩,_⟩, } } instance : preorder (roption α) := by apply_instance lemma le_total_of_le_of_le {x y : roption α} (z : roption α) (hx : x ≤ z) (hy : y ≤ z) : x ≤ y ∨ y ≤ x := begin rcases roption.eq_none_or_eq_some x with h | ⟨b, h₀⟩, { rw h, left, apply order_bot.bot_le _ }, right, intros b' h₁, rw roption.eq_some_iff at h₀, replace hx := hx _ h₀, replace hy := hy _ h₁, replace hx := roption.mem_unique hx hy, subst hx, exact h₀ end /-- `assert p f` is a bind-like operation which appends an additional condition `p` to the domain and uses `f` to produce the value. -/ def assert (p : Prop) (f : p → roption α) : roption α := ⟨∃h : p, (f h).dom, λha, (f ha.fst).get ha.snd⟩ /-- The bind operation has value `g (f.get)`, and is defined when all the parts are defined. -/ protected def bind (f : roption α) (g : α → roption β) : roption β := assert (dom f) (λb, g (f.get b)) /-- The map operation for `roption` just maps the value and maintains the same domain. -/ def map (f : α → β) (o : roption α) : roption β := ⟨o.dom, f ∘ o.get⟩ theorem mem_map (f : α → β) {o : roption α} : ∀ {a}, a ∈ o → f a ∈ map f o | _ ⟨h, rfl⟩ := ⟨_, rfl⟩ @[simp] theorem mem_map_iff (f : α → β) {o : roption α} {b} : b ∈ map f o ↔ ∃ a ∈ o, f a = b := ⟨match b with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩, rfl⟩ end, λ ⟨a, h₁, h₂⟩, h₂ ▸ mem_map f h₁⟩ @[simp] theorem map_none (f : α → β) : map f none = none := eq_none_iff.2 $ λ a, by simp @[simp] theorem map_some (f : α → β) (a : α) : map f (some a) = some (f a) := eq_some_iff.2 $ mem_map f $ mem_some _ theorem mem_assert {p : Prop} {f : p → roption α} : ∀ {a} (h : p), a ∈ f h → a ∈ assert p f | _ x ⟨h, rfl⟩ := ⟨⟨x, h⟩, rfl⟩ @[simp] theorem mem_assert_iff {p : Prop} {f : p → roption α} {a} : a ∈ assert p f ↔ ∃ h : p, a ∈ f h := ⟨match a with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩⟩ end, λ ⟨a, h⟩, mem_assert _ h⟩ lemma assert_pos {p : Prop} {f : p → roption α} (h : p) : assert p f = f h := begin dsimp [assert], cases h' : f h, simp only [h', h, true_and, iff_self, exists_prop_of_true, eq_iff_iff], apply function.hfunext, { simp only [h,h',exists_prop_of_true] }, { cc } end lemma assert_neg {p : Prop} {f : p → roption α} (h : ¬ p) : assert p f = none := begin dsimp [assert,none], congr, { simp only [h, not_false_iff, exists_prop_of_false] }, { apply function.hfunext, { simp only [h, not_false_iff, exists_prop_of_false] }, cc }, end theorem mem_bind {f : roption α} {g : α → roption β} : ∀ {a b}, a ∈ f → b ∈ g a → b ∈ f.bind g | _ _ ⟨h, rfl⟩ ⟨h₂, rfl⟩ := ⟨⟨h, h₂⟩, rfl⟩ @[simp] theorem mem_bind_iff {f : roption α} {g : α → roption β} {b} : b ∈ f.bind g ↔ ∃ a ∈ f, b ∈ g a := ⟨match b with _, ⟨⟨h₁, h₂⟩, rfl⟩ := ⟨_, ⟨_, rfl⟩, ⟨_, rfl⟩⟩ end, λ ⟨a, h₁, h₂⟩, mem_bind h₁ h₂⟩ @[simp] theorem bind_none (f : α → roption β) : none.bind f = none := eq_none_iff.2 $ λ a, by simp @[simp] theorem bind_some (a : α) (f : α → roption β) : (some a).bind f = f a := ext $ by simp theorem bind_some_eq_map (f : α → β) (x : roption α) : x.bind (some ∘ f) = map f x := ext $ by simp [eq_comm] theorem bind_assoc {γ} (f : roption α) (g : α → roption β) (k : β → roption γ) : (f.bind g).bind k = f.bind (λ x, (g x).bind k) := ext $ λ a, by simp; exact ⟨λ ⟨_, ⟨_, h₁, h₂⟩, h₃⟩, ⟨_, h₁, _, h₂, h₃⟩, λ ⟨_, h₁, _, h₂, h₃⟩, ⟨_, ⟨_, h₁, h₂⟩, h₃⟩⟩ @[simp] theorem bind_map {γ} (f : α → β) (x) (g : β → roption γ) : (map f x).bind g = x.bind (λ y, g (f y)) := by rw [← bind_some_eq_map, bind_assoc]; simp @[simp] theorem map_bind {γ} (f : α → roption β) (x : roption α) (g : β → γ) : map g (x.bind f) = x.bind (λ y, map g (f y)) := by rw [← bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map] theorem map_map (g : β → γ) (f : α → β) (o : roption α) : map g (map f o) = map (g ∘ f) o := by rw [← bind_some_eq_map, bind_map, bind_some_eq_map] instance : monad roption := { pure := @some, map := @map, bind := @roption.bind } instance : is_lawful_monad roption := { bind_pure_comp_eq_map := @bind_some_eq_map, id_map := λ β f, by cases f; refl, pure_bind := @bind_some, bind_assoc := @bind_assoc } theorem map_id' {f : α → α} (H : ∀ (x : α), f x = x) (o) : map f o = o := by rw [show f = id, from funext H]; exact id_map o @[simp] theorem bind_some_right (x : roption α) : x.bind some = x := by rw [bind_some_eq_map]; simp [map_id'] @[simp] theorem pure_eq_some (a : α) : pure a = some a := rfl @[simp] theorem ret_eq_some (a : α) : return a = some a := rfl @[simp] theorem map_eq_map {α β} (f : α → β) (o : roption α) : f <$> o = map f o := rfl @[simp] theorem bind_eq_bind {α β} (f : roption α) (g : α → roption β) : f >>= g = f.bind g := rfl lemma bind_le {α} (x : roption α) (f : α → roption β) (y : roption β) : x >>= f ≤ y ↔ (∀ a, a ∈ x → f a ≤ y) := begin split; intro h, { intros a h' b, replace h := h b, simp only [and_imp, exists_prop, bind_eq_bind, mem_bind_iff, exists_imp_distrib] at h, apply h _ h' }, { intros b h', simp only [exists_prop, bind_eq_bind, mem_bind_iff] at h', rcases h' with ⟨a,h₀,h₁⟩, apply h _ h₀ _ h₁ }, end instance : monad_fail roption := { fail := λ_ _, none, ..roption.monad } /- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when `p` implies `o` is defined. -/ def restrict (p : Prop) : ∀ (o : roption α), (p → o.dom) → roption α | ⟨d, f⟩ H := ⟨p, λh, f (H h)⟩ @[simp] theorem mem_restrict (p : Prop) (o : roption α) (h : p → o.dom) (a : α) : a ∈ restrict p o h ↔ p ∧ a ∈ o := begin cases o, dsimp [restrict, mem_eq], split, { rintro ⟨h₀, h₁⟩, exact ⟨h₀, ⟨_, h₁⟩⟩ }, rintro ⟨h₀, h₁, h₂⟩, exact ⟨h₀, h₂⟩ end /-- `unwrap o` gets the value at `o`, ignoring the condition. (This function is unsound.) -/ meta def unwrap (o : roption α) : α := o.get undefined theorem assert_defined {p : Prop} {f : p → roption α} : ∀ (h : p), (f h).dom → (assert p f).dom := exists.intro theorem bind_defined {f : roption α} {g : α → roption β} : ∀ (h : f.dom), (g (f.get h)).dom → (f.bind g).dom := assert_defined @[simp] theorem bind_dom {f : roption α} {g : α → roption β} : (f.bind g).dom ↔ ∃ h : f.dom, (g (f.get h)).dom := iff.rfl end roption /-- `pfun α β`, or `α →. β`, is the type of partial functions from `α` to `β`. It is defined as `α → roption β`. -/ def pfun (α : Type*) (β : Type*) := α → roption β infixr ` →. `:25 := pfun namespace pfun variables {α : Type*} {β : Type*} {γ : Type*} instance : inhabited (α →. β) := ⟨λ a, roption.none⟩ /-- The domain of a partial function -/ def dom (f : α →. β) : set α := {a | (f a).dom} theorem mem_dom (f : α →. β) (x : α) : x ∈ dom f ↔ ∃ y, y ∈ f x := by simp [dom, roption.dom_iff_mem] theorem dom_eq (f : α →. β) : dom f = {x | ∃ y, y ∈ f x} := set.ext (mem_dom f) /-- Evaluate a partial function -/ def fn (f : α →. β) (x) (h : dom f x) : β := (f x).get h /-- Evaluate a partial function to return an `option` -/ def eval_opt (f : α →. β) [D : decidable_pred (dom f)] (x : α) : option β := @roption.to_option _ _ (D x) /-- Partial function extensionality -/ theorem ext' {f g : α →. β} (H1 : ∀ a, a ∈ dom f ↔ a ∈ dom g) (H2 : ∀ a p q, f.fn a p = g.fn a q) : f = g := funext $ λ a, roption.ext' (H1 a) (H2 a) theorem ext {f g : α →. β} (H : ∀ a b, b ∈ f a ↔ b ∈ g a) : f = g := funext $ λ a, roption.ext (H a) /-- Turn a partial function into a function out of a subtype -/ def as_subtype (f : α →. β) (s : f.dom) : β := f.fn s s.2 /-- The set of partial functions `α →. β` is equivalent to the set of pairs `(p : α → Prop, f : subtype p → β)`. -/ def equiv_subtype : (α →. β) ≃ (Σ p : α → Prop, subtype p → β) := ⟨λ f, ⟨λ a, (f a).dom, as_subtype f⟩, λ f x, ⟨f.1 x, λ h, f.2 ⟨x, h⟩⟩, λ f, funext $ λ a, roption.eta _, λ ⟨p, f⟩, by dsimp; congr; funext a; cases a; refl⟩ theorem as_subtype_eq_of_mem {f : α →. β} {x : α} {y : β} (fxy : y ∈ f x) (domx : x ∈ f.dom) : f.as_subtype ⟨x, domx⟩ = y := roption.mem_unique (roption.get_mem _) fxy /-- Turn a total function into a partial function -/ protected def lift (f : α → β) : α →. β := λ a, roption.some (f a) instance : has_coe (α → β) (α →. β) := ⟨pfun.lift⟩ @[simp] theorem lift_eq_coe (f : α → β) : pfun.lift f = f := rfl @[simp] theorem coe_val (f : α → β) (a : α) : (f : α →. β) a = roption.some (f a) := rfl /-- The graph of a partial function is the set of pairs `(x, f x)` where `x` is in the domain of `f`. -/ def graph (f : α →. β) : set (α × β) := {p | p.2 ∈ f p.1} def graph' (f : α →. β) : rel α β := λ x y, y ∈ f x /-- The range of a partial function is the set of values `f x` where `x` is in the domain of `f`. -/ def ran (f : α →. β) : set β := {b | ∃a, b ∈ f a} /-- Restrict a partial function to a smaller domain. -/ def restrict (f : α →. β) {p : set α} (H : p ⊆ f.dom) : α →. β := λ x, roption.restrict (x ∈ p) (f x) (@H x) @[simp] theorem mem_restrict {f : α →. β} {s : set α} (h : s ⊆ f.dom) (a : α) (b : β) : b ∈ restrict f h a ↔ a ∈ s ∧ b ∈ f a := by simp [restrict] def res (f : α → β) (s : set α) : α →. β := restrict (pfun.lift f) (set.subset_univ s) theorem mem_res (f : α → β) (s : set α) (a : α) (b : β) : b ∈ res f s a ↔ (a ∈ s ∧ f a = b) := by simp [res, @eq_comm _ b] theorem res_univ (f : α → β) : pfun.res f set.univ = f := rfl theorem dom_iff_graph (f : α →. β) (x : α) : x ∈ f.dom ↔ ∃y, (x, y) ∈ f.graph := roption.dom_iff_mem theorem lift_graph {f : α → β} {a b} : (a, b) ∈ (f : α →. β).graph ↔ f a = b := show (∃ (h : true), f a = b) ↔ f a = b, by simp /-- The monad `pure` function, the total constant `x` function -/ protected def pure (x : β) : α →. β := λ_, roption.some x /-- The monad `bind` function, pointwise `roption.bind` -/ def bind (f : α →. β) (g : β → α →. γ) : α →. γ := λa, roption.bind (f a) (λb, g b a) /-- The monad `map` function, pointwise `roption.map` -/ def map (f : β → γ) (g : α →. β) : α →. γ := λa, roption.map f (g a) instance : monad (pfun α) := { pure := @pfun.pure _, bind := @pfun.bind _, map := @pfun.map _ } instance : is_lawful_monad (pfun α) := { bind_pure_comp_eq_map := λ β γ f x, funext $ λ a, roption.bind_some_eq_map _ _, id_map := λ β f, by funext a; dsimp [functor.map, pfun.map]; cases f a; refl, pure_bind := λ β γ x f, funext $ λ a, roption.bind_some.{u_1 u_2} _ (f x), bind_assoc := λ β γ δ f g k, funext $ λ a, roption.bind_assoc (f a) (λ b, g b a) (λ b, k b a) } theorem pure_defined (p : set α) (x : β) : p ⊆ (@pfun.pure α _ x).dom := set.subset_univ p theorem bind_defined {α β γ} (p : set α) {f : α →. β} {g : β → α →. γ} (H1 : p ⊆ f.dom) (H2 : ∀x, p ⊆ (g x).dom) : p ⊆ (f >>= g).dom := λa ha, (⟨H1 ha, H2 _ ha⟩ : (f >>= g).dom a) def fix (f : α →. β ⊕ α) : α →. β := λ a, roption.assert (acc (λ x y, sum.inr x ∈ f y) a) $ λ h, @well_founded.fix_F _ (λ x y, sum.inr x ∈ f y) _ (λ a IH, roption.assert (f a).dom $ λ hf, by cases e : (f a).get hf with b a'; [exact roption.some b, exact IH _ ⟨hf, e⟩]) a h theorem dom_of_mem_fix {f : α →. β ⊕ α} {a : α} {b : β} (h : b ∈ fix f a) : (f a).dom := let ⟨h₁, h₂⟩ := roption.mem_assert_iff.1 h in by rw well_founded.fix_F_eq at h₂; exact h₂.fst.fst theorem mem_fix_iff {f : α →. β ⊕ α} {a : α} {b : β} : b ∈ fix f a ↔ sum.inl b ∈ f a ∨ ∃ a', sum.inr a' ∈ f a ∧ b ∈ fix f a' := ⟨λ h, let ⟨h₁, h₂⟩ := roption.mem_assert_iff.1 h in begin rw well_founded.fix_F_eq at h₂, simp at h₂, cases h₂ with h₂ h₃, cases e : (f a).get h₂ with b' a'; simp [e] at h₃, { subst b', refine or.inl ⟨h₂, e⟩ }, { exact or.inr ⟨a', ⟨_, e⟩, roption.mem_assert _ h₃⟩ } end, λ h, begin simp [fix], rcases h with ⟨h₁, h₂⟩ | ⟨a', h, h₃⟩, { refine ⟨⟨_, λ y h', _⟩, _⟩, { injection roption.mem_unique ⟨h₁, h₂⟩ h' }, { rw well_founded.fix_F_eq, simp [h₁, h₂] } }, { simp [fix] at h₃, cases h₃ with h₃ h₄, refine ⟨⟨_, λ y h', _⟩, _⟩, { injection roption.mem_unique h h' with e, exact e ▸ h₃ }, { cases h with h₁ h₂, rw well_founded.fix_F_eq, simp [h₁, h₂, h₄] } } end⟩ @[elab_as_eliminator] def fix_induction {f : α →. β ⊕ α} {b : β} {C : α → Sort*} {a : α} (h : b ∈ fix f a) (H : ∀ a, b ∈ fix f a → (∀ a', b ∈ fix f a' → sum.inr a' ∈ f a → C a') → C a) : C a := begin replace h := roption.mem_assert_iff.1 h, have := h.snd, revert this, induction h.fst with a ha IH, intro h₂, refine H a (roption.mem_assert_iff.2 ⟨⟨_, ha⟩, h₂⟩) (λ a' ha' fa', _), have := (roption.mem_assert_iff.1 ha').snd, exact IH _ fa' ⟨ha _ fa', this⟩ this end end pfun namespace pfun variables {α : Type*} {β : Type*} (f : α →. β) def image (s : set α) : set β := rel.image f.graph' s lemma image_def (s : set α) : image f s = {y | ∃ x ∈ s, y ∈ f x} := rfl lemma mem_image (y : β) (s : set α) : y ∈ image f s ↔ ∃ x ∈ s, y ∈ f x := iff.rfl lemma image_mono {s t : set α} (h : s ⊆ t) : f.image s ⊆ f.image t := rel.image_mono _ h lemma image_inter (s t : set α) : f.image (s ∩ t) ⊆ f.image s ∩ f.image t := rel.image_inter _ s t lemma image_union (s t : set α) : f.image (s ∪ t) = f.image s ∪ f.image t := rel.image_union _ s t def preimage (s : set β) : set α := rel.preimage (λ x y, y ∈ f x) s lemma preimage_def (s : set β) : preimage f s = {x | ∃ y ∈ s, y ∈ f x} := rfl lemma mem_preimage (s : set β) (x : α) : x ∈ preimage f s ↔ ∃ y ∈ s, y ∈ f x := iff.rfl lemma preimage_subset_dom (s : set β) : f.preimage s ⊆ f.dom := assume x ⟨y, ys, fxy⟩, roption.dom_iff_mem.mpr ⟨y, fxy⟩ lemma preimage_mono {s t : set β} (h : s ⊆ t) : f.preimage s ⊆ f.preimage t := rel.preimage_mono _ h lemma preimage_inter (s t : set β) : f.preimage (s ∩ t) ⊆ f.preimage s ∩ f.preimage t := rel.preimage_inter _ s t lemma preimage_union (s t : set β) : f.preimage (s ∪ t) = f.preimage s ∪ f.preimage t := rel.preimage_union _ s t lemma preimage_univ : f.preimage set.univ = f.dom := by ext; simp [mem_preimage, mem_dom] def core (s : set β) : set α := rel.core f.graph' s lemma core_def (s : set β) : core f s = {x | ∀ y, y ∈ f x → y ∈ s} := rfl lemma mem_core (x : α) (s : set β) : x ∈ core f s ↔ (∀ y, y ∈ f x → y ∈ s) := iff.rfl lemma compl_dom_subset_core (s : set β) : f.domᶜ ⊆ f.core s := assume x hx y fxy, absurd ((mem_dom f x).mpr ⟨y, fxy⟩) hx lemma core_mono {s t : set β} (h : s ⊆ t) : f.core s ⊆ f.core t := rel.core_mono _ h lemma core_inter (s t : set β) : f.core (s ∩ t) = f.core s ∩ f.core t := rel.core_inter _ s t lemma mem_core_res (f : α → β) (s : set α) (t : set β) (x : α) : x ∈ core (res f s) t ↔ (x ∈ s → f x ∈ t) := by simp [mem_core, mem_res] section open_locale classical lemma core_res (f : α → β) (s : set α) (t : set β) : core (res f s) t = sᶜ ∪ f ⁻¹' t := by { ext, rw mem_core_res, by_cases h : x ∈ s; simp [h] } end lemma core_restrict (f : α → β) (s : set β) : core (f : α →. β) s = set.preimage f s := by ext x; simp [core_def] lemma preimage_subset_core (f : α →. β) (s : set β) : f.preimage s ⊆ f.core s := assume x ⟨y, ys, fxy⟩ y' fxy', have y = y', from roption.mem_unique fxy fxy', this ▸ ys lemma preimage_eq (f : α →. β) (s : set β) : f.preimage s = f.core s ∩ f.dom := set.eq_of_subset_of_subset (set.subset_inter (preimage_subset_core f s) (preimage_subset_dom f s)) (assume x ⟨xcore, xdom⟩, let y := (f x).get xdom in have ys : y ∈ s, from xcore _ (roption.get_mem _), show x ∈ preimage f s, from ⟨(f x).get xdom, ys, roption.get_mem _⟩) lemma core_eq (f : α →. β) (s : set β) : f.core s = f.preimage s ∪ f.domᶜ := by rw [preimage_eq, set.union_distrib_right, set.union_comm (dom f), set.compl_union_self, set.inter_univ, set.union_eq_self_of_subset_right (compl_dom_subset_core f s)] lemma preimage_as_subtype (f : α →. β) (s : set β) : f.as_subtype ⁻¹' s = subtype.val ⁻¹' pfun.preimage f s := begin ext x, simp only [set.mem_preimage, set.mem_set_of_eq, pfun.as_subtype, pfun.mem_preimage], show pfun.fn f (x.val) _ ∈ s ↔ ∃ y ∈ s, y ∈ f (x.val), exact iff.intro (assume h, ⟨_, h, roption.get_mem _⟩) (assume ⟨y, ys, fxy⟩, have f.fn x.val x.property ∈ f x.val := roption.get_mem _, roption.mem_unique fxy this ▸ ys) end end pfun
3f6a46115da551d89192dbfaf9ecb7baa0083155
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/dynamics/circle/rotation_number/translation_number.lean
40bcf7c4e4205952900bfeefcbd8c615041701e3
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
39,615
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.specific_limits import order.iterate import order.semiconj_Sup import algebra.iterate_hom /-! # Translation number of a monotone real map that commutes with `x ↦ x + 1` Let `f : ℝ → ℝ` be a monotone map such that `f (x + 1) = f x + 1` for all `x`. Then the limit $$ \tau(f)=\lim_{n\to\infty}{f^n(x)-x}{n} $$ exists and does not depend on `x`. This number is called the *translation number* of `f`. Different authors use different notation for this number: `τ`, `ρ`, `rot`, etc In this file we define a structure `circle_deg1_lift` for bundled maps with these properties, define translation number of `f : circle_deg1_lift`, prove some estimates relating `f^n(x)-x` to `τ(f)`. In case of a continuous map `f` we also prove that `f` admits a point `x` such that `f^n(x)=x+m` if and only if `τ(f)=m/n`. Maps of this type naturally appear as lifts of orientation preserving circle homeomorphisms. More precisely, let `f` be an orientation preserving homeomorphism of the circle $S^1=ℝ/ℤ$, and consider a real number `a` such that `⟦a⟧ = f 0`, where `⟦⟧` means the natural projection `ℝ → ℝ/ℤ`. Then there exists a unique continuous function `F : ℝ → ℝ` such that `F 0 = a` and `⟦F x⟧ = f ⟦x⟧` for all `x` (this fact is not formalized yet). This function is strictly monotone, continuous, and satisfies `F (x + 1) = F x + 1`. The number `⟦τ F⟧ : ℝ / ℤ` is called the *rotation number* of `f`. It does not depend on the choice of `a`. ## Main definitions * `circle_deg1_lift`: a monotone map `f : ℝ → ℝ` such that `f (x + 1) = f x + 1` for all `x`; the type `circle_deg1_lift` is equipped with `lattice` and `monoid` structures; the multiplication is given by composition: `(f * g) x = f (g x)`. * `circle_deg1_lift.translation_number`: translation number of `f : circle_deg1_lift`. ## Main statements We prove the following properties of `circle_deg1_lift.translation_number`. * `circle_deg1_lift.translation_number_eq_of_dist_bounded`: if the distance between `(f^n) 0` and `(g^n) 0` is bounded from above uniformly in `n : ℕ`, then `f` and `g` have equal translation numbers. * `circle_deg1_lift.translation_number_eq_of_semiconj_by`: if two `circle_deg1_lift` maps `f`, `g` are semiconjugate by a `circle_deg1_lift` map, then `τ f = τ g`. * `circle_deg1_lift.translation_number_units_inv`: if `f` is an invertible `circle_deg1_lift` map (equivalently, `f` is a lift of an orientation-preserving circle homeomorphism), then the translation number of `f⁻¹` is the negative of the translation number of `f`. * `circle_deg1_lift.translation_number_mul_of_commute`: if `f` and `g` commute, then `τ (f * g) = τ f + τ g`. * `circle_deg1_lift.translation_number_eq_rat_iff`: the translation number of `f` is equal to a rational number `m / n` if and only if `(f^n) x = x + m` for some `x`. * `circle_deg1_lift.semiconj_of_bijective_of_translation_number_eq`: if `f` and `g` are two bijective `circle_deg1_lift` maps and their translation numbers are equal, then these maps are semiconjugate to each other. * `circle_deg1_lift.semiconj_of_group_action_of_forall_translation_number_eq`: let `f₁` and `f₂` be two actions of a group `G` on the circle by degree 1 maps (formally, `f₁` and `f₂` are two homomorphisms from `G →* circle_deg1_lift`). If the translation numbers of `f₁ g` and `f₂ g` are equal to each other for all `g : G`, then these two actions are semiconjugate by some `F : circle_deg1_lift`. This is a version of Proposition 5.4 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. ## Notation We use a local notation `τ` for the translation number of `f : circle_deg1_lift`. ## Implementation notes We define the translation number of `f : circle_deg1_lift` to be the limit of the sequence `(f ^ (2 ^ n)) 0 / (2 ^ n)`, then prove that `((f ^ n) x - x) / n` tends to this number for any `x`. This way it is much easier to prove that the limit exists and basic properties of the limit. We define translation number for a wider class of maps `f : ℝ → ℝ` instead of lifts of orientation preserving circle homeomorphisms for two reasons: * non-strictly monotone circle self-maps with discontinuities naturally appear as Poincaré maps for some flows on the two-torus (e.g., one can take a constant flow and glue in a few Cherry cells); * definition and some basic properties still work for this class. ## References * [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes] ## TODO Here are some short-term goals. * Introduce a structure or a typeclass for lifts of circle homeomorphisms. We use `units circle_deg1_lift` for now, but it's better to have a dedicated type (or a typeclass?). * Prove that the `semiconj_by` relation on circle homeomorphisms is an equivalence relation. * Introduce `conditionally_complete_lattice` structure, use it in the proof of `circle_deg1_lift.semiconj_of_group_action_of_forall_translation_number_eq`. * Prove that the orbits of the irrational rotation are dense in the circle. Deduce that a homeomorphism with an irrational rotation is semiconjugate to the corresponding irrational translation by a continuous `circle_deg1_lift`. ## Tags circle homeomorphism, rotation number -/ open filter set function (hiding commute) open_locale topological_space classical /-! ### Definition and monoid structure -/ /-- A lift of a monotone degree one map `S¹ → S¹`. -/ structure circle_deg1_lift : Type := (to_fun : ℝ → ℝ) (monotone' : monotone to_fun) (map_add_one' : ∀ x, to_fun (x + 1) = to_fun x + 1) namespace circle_deg1_lift instance : has_coe_to_fun circle_deg1_lift := ⟨λ _, ℝ → ℝ, circle_deg1_lift.to_fun⟩ @[simp] lemma coe_mk (f h₁ h₂) : ⇑(mk f h₁ h₂) = f := rfl variables (f g : circle_deg1_lift) protected lemma monotone : monotone f := f.monotone' @[mono] lemma mono {x y} (h : x ≤ y) : f x ≤ f y := f.monotone h lemma strict_mono_iff_injective : strict_mono f ↔ injective f := f.monotone.strict_mono_iff_injective @[simp] lemma map_add_one : ∀ x, f (x + 1) = f x + 1 := f.map_add_one' @[simp] lemma map_one_add (x : ℝ) : f (1 + x) = 1 + f x := by rw [add_comm, map_add_one, add_comm] theorem coe_inj : ∀ ⦃f g : circle_deg1_lift ⦄, (f : ℝ → ℝ) = g → f = g := assume ⟨f, fm, fd⟩ ⟨g, gm, gd⟩ h, by congr; exact h @[ext] theorem ext ⦃f g : circle_deg1_lift ⦄ (h : ∀ x, f x = g x) : f = g := coe_inj $ funext h theorem ext_iff {f g : circle_deg1_lift} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ instance : monoid circle_deg1_lift := { mul := λ f g, { to_fun := f ∘ g, monotone' := f.monotone.comp g.monotone, map_add_one' := λ x, by simp [map_add_one] }, one := ⟨id, monotone_id, λ _, rfl⟩, mul_one := λ f, coe_inj $ function.comp.right_id f, one_mul := λ f, coe_inj $ function.comp.left_id f, mul_assoc := λ f₁ f₂ f₃, coe_inj rfl } instance : inhabited circle_deg1_lift := ⟨1⟩ @[simp] lemma coe_mul : ⇑(f * g) = f ∘ g := rfl lemma mul_apply (x) : (f * g) x = f (g x) := rfl @[simp] lemma coe_one : ⇑(1 : circle_deg1_lift) = id := rfl instance units_has_coe_to_fun : has_coe_to_fun (units circle_deg1_lift) := ⟨λ _, ℝ → ℝ, λ f, ⇑(f : circle_deg1_lift)⟩ @[simp, norm_cast] lemma units_coe (f : units circle_deg1_lift) : ⇑(f : circle_deg1_lift) = f := rfl @[simp] lemma units_inv_apply_apply (f : units circle_deg1_lift) (x : ℝ) : (f⁻¹ : units circle_deg1_lift) (f x) = x := by simp only [← units_coe, ← mul_apply, f.inv_mul, coe_one, id] @[simp] lemma units_apply_inv_apply (f : units circle_deg1_lift) (x : ℝ) : f ((f⁻¹ : units circle_deg1_lift) x) = x := by simp only [← units_coe, ← mul_apply, f.mul_inv, coe_one, id] /-- If a lift of a circle map is bijective, then it is an order automorphism of the line. -/ def to_order_iso : units circle_deg1_lift →* ℝ ≃o ℝ := { to_fun := λ f, { to_fun := f, inv_fun := ⇑(f⁻¹), left_inv := units_inv_apply_apply f, right_inv := units_apply_inv_apply f, map_rel_iff' := λ x y, ⟨λ h, by simpa using mono ↑(f⁻¹) h, mono f⟩ }, map_one' := rfl, map_mul' := λ f g, rfl } @[simp] lemma coe_to_order_iso (f : units circle_deg1_lift) : ⇑(to_order_iso f) = f := rfl @[simp] lemma coe_to_order_iso_symm (f : units circle_deg1_lift) : ⇑(to_order_iso f).symm = (f⁻¹ : units circle_deg1_lift) := rfl @[simp] lemma coe_to_order_iso_inv (f : units circle_deg1_lift) : ⇑(to_order_iso f)⁻¹ = (f⁻¹ : units circle_deg1_lift) := rfl lemma is_unit_iff_bijective {f : circle_deg1_lift} : is_unit f ↔ bijective f := ⟨λ ⟨u, h⟩, h ▸ (to_order_iso u).bijective, λ h, units.is_unit { val := f, inv := { to_fun := (equiv.of_bijective f h).symm, monotone' := λ x y hxy, (f.strict_mono_iff_injective.2 h.1).le_iff_le.1 (by simp only [equiv.of_bijective_apply_symm_apply f h, hxy]), map_add_one' := λ x, h.1 $ by simp only [equiv.of_bijective_apply_symm_apply f, f.map_add_one] }, val_inv := ext $ equiv.of_bijective_apply_symm_apply f h, inv_val := ext $ equiv.of_bijective_symm_apply_apply f h }⟩ lemma coe_pow : ∀ n : ℕ, ⇑(f^n) = (f^[n]) | 0 := rfl | (n+1) := by {ext x, simp [coe_pow n, pow_succ'] } lemma semiconj_by_iff_semiconj {f g₁ g₂ : circle_deg1_lift} : semiconj_by f g₁ g₂ ↔ semiconj f g₁ g₂ := ext_iff lemma commute_iff_commute {f g : circle_deg1_lift} : commute f g ↔ function.commute f g := ext_iff /-! ### Translate by a constant -/ /-- The map `y ↦ x + y` as a `circle_deg1_lift`. More precisely, we define a homomorphism from `multiplicative ℝ` to `units circle_deg1_lift`, so the translation by `x` is `translation (multiplicative.of_add x)`. -/ def translate : multiplicative ℝ →* units circle_deg1_lift := by refine (units.map _).comp to_units.to_monoid_hom; exact { to_fun := λ x, ⟨λ y, x.to_add + y, λ y₁ y₂ h, add_le_add_left h _, λ y, (add_assoc _ _ _).symm⟩, map_one' := ext $ zero_add, map_mul' := λ x y, ext $ add_assoc _ _ } @[simp] lemma translate_apply (x y : ℝ) : translate (multiplicative.of_add x) y = x + y := rfl @[simp] lemma translate_inv_apply (x y : ℝ) : (translate $ multiplicative.of_add x)⁻¹ y = -x + y := rfl @[simp] lemma translate_gpow (x : ℝ) (n : ℤ) : (translate (multiplicative.of_add x))^n = translate (multiplicative.of_add $ ↑n * x) := by simp only [← gsmul_eq_mul, of_add_gsmul, monoid_hom.map_gpow] @[simp] lemma translate_pow (x : ℝ) (n : ℕ) : (translate (multiplicative.of_add x))^n = translate (multiplicative.of_add $ ↑n * x) := translate_gpow x n @[simp] lemma translate_iterate (x : ℝ) (n : ℕ) : (translate (multiplicative.of_add x))^[n] = translate (multiplicative.of_add $ ↑n * x) := by rw [← units_coe, ← coe_pow, ← units.coe_pow, translate_pow, units_coe] /-! ### Commutativity with integer translations In this section we prove that `f` commutes with translations by an integer number. First we formulate these statements (for a natural or an integer number, addition on the left or on the right, addition or subtraction) using `function.commute`, then reformulate as `simp` lemmas `map_int_add` etc. -/ lemma commute_nat_add (n : ℕ) : function.commute f ((+) n) := by simpa only [nsmul_one, add_left_iterate] using function.commute.iterate_right f.map_one_add n lemma commute_add_nat (n : ℕ) : function.commute f (λ x, x + n) := by simp only [add_comm _ (n:ℝ), f.commute_nat_add n] lemma commute_sub_nat (n : ℕ) : function.commute f (λ x, x - n) := by simpa only [sub_eq_add_neg] using (f.commute_add_nat n).inverses_right (equiv.add_right _).right_inv (equiv.add_right _).left_inv lemma commute_add_int : ∀ n : ℤ, function.commute f (λ x, x + n) | (n:ℕ) := f.commute_add_nat n | -[1+n] := by simpa only [sub_eq_add_neg] using f.commute_sub_nat (n + 1) lemma commute_int_add (n : ℤ) : function.commute f ((+) n) := by simpa only [add_comm _ (n:ℝ)] using f.commute_add_int n lemma commute_sub_int (n : ℤ) : function.commute f (λ x, x - n) := by simpa only [sub_eq_add_neg] using (f.commute_add_int n).inverses_right (equiv.add_right _).right_inv (equiv.add_right _).left_inv @[simp] lemma map_int_add (m : ℤ) (x : ℝ) : f (m + x) = m + f x := f.commute_int_add m x @[simp] lemma map_add_int (x : ℝ) (m : ℤ) : f (x + m) = f x + m := f.commute_add_int m x @[simp] lemma map_sub_int (x : ℝ) (n : ℤ) : f (x - n) = f x - n := f.commute_sub_int n x @[simp] lemma map_add_nat (x : ℝ) (n : ℕ) : f (x + n) = f x + n := f.map_add_int x n @[simp] lemma map_nat_add (n : ℕ) (x : ℝ) : f (n + x) = n + f x := f.map_int_add n x @[simp] lemma map_sub_nat (x : ℝ) (n : ℕ) : f (x - n) = f x - n := f.map_sub_int x n lemma map_int_of_map_zero (n : ℤ) : f n = f 0 + n := by rw [← f.map_add_int, zero_add] @[simp] lemma map_fract_sub_fract_eq (x : ℝ) : f (fract x) - fract x = f x - x := by conv_rhs { rw [← fract_add_floor x, f.map_add_int, add_sub_comm, sub_self, add_zero] } /-! ### Pointwise order on circle maps -/ /-- Monotone circle maps form a lattice with respect to the pointwise order -/ noncomputable instance : lattice circle_deg1_lift := { sup := λ f g, { to_fun := λ x, max (f x) (g x), monotone' := λ x y h, max_le_max (f.mono h) (g.mono h), -- TODO: generalize to `monotone.max` map_add_one' := λ x, by simp [max_add_add_right] }, le := λ f g, ∀ x, f x ≤ g x, le_refl := λ f x, le_refl (f x), le_trans := λ f₁ f₂ f₃ h₁₂ h₂₃ x, le_trans (h₁₂ x) (h₂₃ x), le_antisymm := λ f₁ f₂ h₁₂ h₂₁, ext $ λ x, le_antisymm (h₁₂ x) (h₂₁ x), le_sup_left := λ f g x, le_max_left (f x) (g x), le_sup_right := λ f g x, le_max_right (f x) (g x), sup_le := λ f₁ f₂ f₃ h₁ h₂ x, max_le (h₁ x) (h₂ x), inf := λ f g, { to_fun := λ x, min (f x) (g x), monotone' := λ x y h, min_le_min (f.mono h) (g.mono h), map_add_one' := λ x, by simp [min_add_add_right] }, inf_le_left := λ f g x, min_le_left (f x) (g x), inf_le_right := λ f g x, min_le_right (f x) (g x), le_inf := λ f₁ f₂ f₃ h₂ h₃ x, le_min (h₂ x) (h₃ x) } @[simp] lemma sup_apply (x : ℝ) : (f ⊔ g) x = max (f x) (g x) := rfl @[simp] lemma inf_apply (x : ℝ) : (f ⊓ g) x = min (f x) (g x) := rfl lemma iterate_monotone (n : ℕ) : monotone (λ f : circle_deg1_lift, f^[n]) := λ f g h, f.monotone.iterate_le_of_le h _ lemma iterate_mono {f g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f^[n] ≤ (g^[n]) := iterate_monotone n h lemma pow_mono {f g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f^n ≤ g^n := λ x, by simp only [coe_pow, iterate_mono h n x] lemma pow_monotone (n : ℕ) : monotone (λ f : circle_deg1_lift, f^n) := λ f g h, pow_mono h n /-! ### Estimates on `(f * g) 0` We prove the estimates `f 0 + ⌊g 0⌋ ≤ f (g 0) ≤ f 0 + ⌈g 0⌉` and some corollaries with added/removed floors and ceils. We also prove that for two semiconjugate maps `g₁`, `g₂`, the distance between `g₁ 0` and `g₂ 0` is less than two. -/ lemma map_le_of_map_zero (x : ℝ) : f x ≤ f 0 + ⌈x⌉ := calc f x ≤ f ⌈x⌉ : f.monotone $ le_ceil _ ... = f 0 + ⌈x⌉ : f.map_int_of_map_zero _ lemma map_map_zero_le : f (g 0) ≤ f 0 + ⌈g 0⌉ := f.map_le_of_map_zero (g 0) lemma floor_map_map_zero_le : ⌊f (g 0)⌋ ≤ ⌊f 0⌋ + ⌈g 0⌉ := calc ⌊f (g 0)⌋ ≤ ⌊f 0 + ⌈g 0⌉⌋ : floor_mono $ f.map_map_zero_le g ... = ⌊f 0⌋ + ⌈g 0⌉ : floor_add_int _ _ lemma ceil_map_map_zero_le : ⌈f (g 0)⌉ ≤ ⌈f 0⌉ + ⌈g 0⌉ := calc ⌈f (g 0)⌉ ≤ ⌈f 0 + ⌈g 0⌉⌉ : ceil_mono $ f.map_map_zero_le g ... = ⌈f 0⌉ + ⌈g 0⌉ : ceil_add_int _ _ lemma map_map_zero_lt : f (g 0) < f 0 + g 0 + 1 := calc f (g 0) ≤ f 0 + ⌈g 0⌉ : f.map_map_zero_le g ... < f 0 + (g 0 + 1) : add_lt_add_left (ceil_lt_add_one _) _ ... = f 0 + g 0 + 1 : (add_assoc _ _ _).symm lemma le_map_of_map_zero (x : ℝ) : f 0 + ⌊x⌋ ≤ f x := calc f 0 + ⌊x⌋ = f ⌊x⌋ : (f.map_int_of_map_zero _).symm ... ≤ f x : f.monotone $ floor_le _ lemma le_map_map_zero : f 0 + ⌊g 0⌋ ≤ f (g 0) := f.le_map_of_map_zero (g 0) lemma le_floor_map_map_zero : ⌊f 0⌋ + ⌊g 0⌋ ≤ ⌊f (g 0)⌋ := calc ⌊f 0⌋ + ⌊g 0⌋ = ⌊f 0 + ⌊g 0⌋⌋ : (floor_add_int _ _).symm ... ≤ ⌊f (g 0)⌋ : floor_mono $ f.le_map_map_zero g lemma le_ceil_map_map_zero : ⌈f 0⌉ + ⌊g 0⌋ ≤ ⌈(f * g) 0⌉ := calc ⌈f 0⌉ + ⌊g 0⌋ = ⌈f 0 + ⌊g 0⌋⌉ : (ceil_add_int _ _).symm ... ≤ ⌈f (g 0)⌉ : ceil_mono $ f.le_map_map_zero g lemma lt_map_map_zero : f 0 + g 0 - 1 < f (g 0) := calc f 0 + g 0 - 1 = f 0 + (g 0 - 1) : add_sub_assoc _ _ _ ... < f 0 + ⌊g 0⌋ : add_lt_add_left (sub_one_lt_floor _) _ ... ≤ f (g 0) : f.le_map_map_zero g lemma dist_map_map_zero_lt : dist (f 0 + g 0) (f (g 0)) < 1 := begin rw [dist_comm, real.dist_eq, abs_lt, lt_sub_iff_add_lt', sub_lt_iff_lt_add', ← sub_eq_add_neg], exact ⟨f.lt_map_map_zero g, f.map_map_zero_lt g⟩ end lemma dist_map_zero_lt_of_semiconj {f g₁ g₂ : circle_deg1_lift} (h : function.semiconj f g₁ g₂) : dist (g₁ 0) (g₂ 0) < 2 := calc dist (g₁ 0) (g₂ 0) ≤ dist (g₁ 0) (f (g₁ 0) - f 0) + dist _ (g₂ 0) : dist_triangle _ _ _ ... = dist (f 0 + g₁ 0) (f (g₁ 0)) + dist (g₂ 0 + f 0) (g₂ (f 0)) : by simp only [h.eq, real.dist_eq, sub_sub, add_comm (f 0), sub_sub_assoc_swap, abs_sub (g₂ (f 0))] ... < 2 : add_lt_add (f.dist_map_map_zero_lt g₁) (g₂.dist_map_map_zero_lt f) lemma dist_map_zero_lt_of_semiconj_by {f g₁ g₂ : circle_deg1_lift} (h : semiconj_by f g₁ g₂) : dist (g₁ 0) (g₂ 0) < 2 := dist_map_zero_lt_of_semiconj $ semiconj_by_iff_semiconj.1 h /-! ### Limits at infinities and continuity -/ protected lemma tendsto_at_bot : tendsto f at_bot at_bot := tendsto_at_bot_mono f.map_le_of_map_zero $ tendsto_at_bot_add_const_left _ _ $ tendsto_at_bot_mono (λ x, (ceil_lt_add_one x).le) $ tendsto_at_bot_add_const_right _ _ tendsto_id protected lemma tendsto_at_top : tendsto f at_top at_top := tendsto_at_top_mono f.le_map_of_map_zero $ tendsto_at_top_add_const_left _ _ $ tendsto_at_top_mono (λ x, (sub_one_lt_floor x).le) $ by simpa [sub_eq_add_neg] using tendsto_at_top_add_const_right _ _ tendsto_id lemma continuous_iff_surjective : continuous f ↔ function.surjective f := ⟨λ h, h.surjective f.tendsto_at_top f.tendsto_at_bot, f.monotone.continuous_of_surjective⟩ /-! ### Estimates on `(f^n) x` If we know that `f x` is `≤`/`<`/`≥`/`>`/`=` to `x + m`, then we have a similar estimate on `f^[n] x` and `x + n * m`. For `≤`, `≥`, and `=` we formulate both `of` (implication) and `iff` versions because implications work for `n = 0`. For `<` and `>` we formulate only `iff` versions. -/ lemma iterate_le_of_map_le_add_int {x : ℝ} {m : ℤ} (h : f x ≤ x + m) (n : ℕ) : f^[n] x ≤ x + n * m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_le_of_map_le f.monotone (monotone_id.add_const m) h n lemma le_iterate_of_add_int_le_map {x : ℝ} {m : ℤ} (h : x + m ≤ f x) (n : ℕ) : x + n * m ≤ (f^[n]) x := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).symm.iterate_le_of_map_le (monotone_id.add_const m) f.monotone h n lemma iterate_eq_of_map_eq_add_int {x : ℝ} {m : ℤ} (h : f x = x + m) (n : ℕ) : f^[n] x = x + n * m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_eq_of_map_eq n h lemma iterate_pos_le_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x ≤ x + n * m ↔ f x ≤ x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_le_iff_map_le f.monotone (strict_mono_id.add_const m) hn lemma iterate_pos_lt_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x < x + n * m ↔ f x < x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_lt_iff_map_lt f.monotone (strict_mono_id.add_const m) hn lemma iterate_pos_eq_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x = x + n * m ↔ f x = x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_eq_iff_map_eq f.monotone (strict_mono_id.add_const m) hn lemma le_iterate_pos_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : x + n * m ≤ (f^[n]) x ↔ x + m ≤ f x := by simpa only [not_lt] using not_congr (f.iterate_pos_lt_iff hn) lemma lt_iterate_pos_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : x + n * m < (f^[n]) x ↔ x + m < f x := by simpa only [not_le] using not_congr (f.iterate_pos_le_iff hn) lemma mul_floor_map_zero_le_floor_iterate_zero (n : ℕ) : ↑n * ⌊f 0⌋ ≤ ⌊(f^[n] 0)⌋ := begin rw [le_floor, int.cast_mul, int.cast_coe_nat, ← zero_add ((n : ℝ) * _)], apply le_iterate_of_add_int_le_map, simp [floor_le] end /-! ### Definition of translation number -/ noncomputable theory /-- An auxiliary sequence used to define the translation number. -/ def transnum_aux_seq (n : ℕ) : ℝ := (f^(2^n)) 0 / 2^n /-- The translation number of a `circle_deg1_lift`, $τ(f)=\lim_{n→∞}\frac{f^n(x)-x}{n}$. We use an auxiliary sequence `\frac{f^{2^n}(0)}{2^n}` to define `τ(f)` because some proofs are simpler this way. -/ def translation_number : ℝ := lim at_top f.transnum_aux_seq -- TODO: choose two different symbols for `circle_deg1_lift.translation_number` and the future -- `circle_mono_homeo.rotation_number`, then make them `localized notation`s local notation `τ` := translation_number lemma transnum_aux_seq_def : f.transnum_aux_seq = λ n : ℕ, (f^(2^n)) 0 / 2^n := rfl lemma translation_number_eq_of_tendsto_aux {τ' : ℝ} (h : tendsto f.transnum_aux_seq at_top (𝓝 τ')) : τ f = τ' := h.lim_eq lemma translation_number_eq_of_tendsto₀ {τ' : ℝ} (h : tendsto (λ n:ℕ, f^[n] 0 / n) at_top (𝓝 τ')) : τ f = τ' := f.translation_number_eq_of_tendsto_aux $ by simpa [(∘), transnum_aux_seq_def, coe_pow] using h.comp (nat.tendsto_pow_at_top_at_top_of_one_lt one_lt_two) lemma translation_number_eq_of_tendsto₀' {τ' : ℝ} (h : tendsto (λ n:ℕ, f^[n + 1] 0 / (n + 1)) at_top (𝓝 τ')) : τ f = τ' := f.translation_number_eq_of_tendsto₀ $ (tendsto_add_at_top_iff_nat 1).1 h lemma transnum_aux_seq_zero : f.transnum_aux_seq 0 = f 0 := by simp [transnum_aux_seq] lemma transnum_aux_seq_dist_lt (n : ℕ) : dist (f.transnum_aux_seq n) (f.transnum_aux_seq (n+1)) < (1 / 2) / (2^n) := begin have : 0 < (2^(n+1):ℝ) := pow_pos zero_lt_two _, rw [div_div_eq_div_mul, ← pow_succ, ← abs_of_pos this], replace := abs_pos.2 (ne_of_gt this), convert (div_lt_div_right this).2 ((f^(2^n)).dist_map_map_zero_lt (f^(2^n))), simp_rw [transnum_aux_seq, real.dist_eq], rw [← abs_div, sub_div, pow_succ', pow_succ, ← two_mul, mul_div_mul_left _ _ (@two_ne_zero ℝ _ _), pow_mul, pow_two, mul_apply] end lemma tendsto_translation_number_aux : tendsto f.transnum_aux_seq at_top (𝓝 $ τ f) := (cauchy_seq_of_le_geometric_two 1 (λ n, le_of_lt $ f.transnum_aux_seq_dist_lt n)).tendsto_lim lemma dist_map_zero_translation_number_le : dist (f 0) (τ f) ≤ 1 := f.transnum_aux_seq_zero ▸ dist_le_of_le_geometric_two_of_tendsto₀ 1 (λ n, le_of_lt $ f.transnum_aux_seq_dist_lt n) f.tendsto_translation_number_aux lemma tendsto_translation_number_of_dist_bounded_aux (x : ℕ → ℝ) (C : ℝ) (H : ∀ n : ℕ, dist ((f^n) 0) (x n) ≤ C) : tendsto (λ n : ℕ, x (2^n) / (2^n)) at_top (𝓝 $ τ f) := begin refine f.tendsto_translation_number_aux.congr_dist (squeeze_zero (λ _, dist_nonneg) _ _), { exact λ n, C / 2^n }, { intro n, have : 0 < (2^n:ℝ) := pow_pos zero_lt_two _, convert (div_le_div_right this).2 (H (2^n)), rw [transnum_aux_seq, real.dist_eq, ← sub_div, abs_div, abs_of_pos this, real.dist_eq] }, { exact mul_zero C ▸ tendsto_const_nhds.mul (tendsto_inv_at_top_zero.comp $ tendsto_pow_at_top_at_top_of_one_lt one_lt_two) } end lemma translation_number_eq_of_dist_bounded {f g : circle_deg1_lift} (C : ℝ) (H : ∀ n : ℕ, dist ((f^n) 0) ((g^n) 0) ≤ C) : τ f = τ g := eq.symm $ g.translation_number_eq_of_tendsto_aux $ f.tendsto_translation_number_of_dist_bounded_aux _ C H @[simp] lemma translation_number_one : τ 1 = 0 := translation_number_eq_of_tendsto₀ _ $ by simp [tendsto_const_nhds] lemma translation_number_eq_of_semiconj_by {f g₁ g₂ : circle_deg1_lift} (H : semiconj_by f g₁ g₂) : τ g₁ = τ g₂ := translation_number_eq_of_dist_bounded 2 $ λ n, le_of_lt $ dist_map_zero_lt_of_semiconj_by $ H.pow_right n lemma translation_number_eq_of_semiconj {f g₁ g₂ : circle_deg1_lift} (H : function.semiconj f g₁ g₂) : τ g₁ = τ g₂ := translation_number_eq_of_semiconj_by $ semiconj_by_iff_semiconj.2 H lemma translation_number_mul_of_commute {f g : circle_deg1_lift} (h : commute f g) : τ (f * g) = τ f + τ g := begin have : tendsto (λ n : ℕ, ((λ k, (f^k) 0 + (g^k) 0) (2^n)) / (2^n)) at_top (𝓝 $ τ f + τ g) := ((f.tendsto_translation_number_aux.add g.tendsto_translation_number_aux).congr $ λ n, (add_div ((f^(2^n)) 0) ((g^(2^n)) 0) ((2:ℝ)^n)).symm), refine tendsto_nhds_unique ((f * g).tendsto_translation_number_of_dist_bounded_aux _ 1 (λ n, _)) this, rw [h.mul_pow, dist_comm], exact le_of_lt ((f^n).dist_map_map_zero_lt (g^n)) end @[simp] lemma translation_number_units_inv (f : units circle_deg1_lift) : τ ↑(f⁻¹) = -τ f := eq_neg_iff_add_eq_zero.2 $ by simp [← translation_number_mul_of_commute (commute.refl _).units_inv_left] @[simp] lemma translation_number_pow : ∀ n : ℕ, τ (f^n) = n * τ f | 0 := by simp | (n+1) := by rw [pow_succ', translation_number_mul_of_commute (commute.pow_self f n), translation_number_pow n, nat.cast_add_one, add_mul, one_mul] @[simp] lemma translation_number_gpow (f : units circle_deg1_lift) : ∀ n : ℤ, τ (f ^ n : units _) = n * τ f | (n : ℕ) := by simp [translation_number_pow f n] | -[1+n] := by { simp, ring } @[simp] lemma translation_number_conj_eq (f : units circle_deg1_lift) (g : circle_deg1_lift) : τ (↑f * g * ↑(f⁻¹)) = τ g := (translation_number_eq_of_semiconj_by (f.mk_semiconj_by g)).symm @[simp] lemma translation_number_conj_eq' (f : units circle_deg1_lift) (g : circle_deg1_lift) : τ (↑(f⁻¹) * g * f) = τ g := translation_number_conj_eq f⁻¹ g lemma dist_pow_map_zero_mul_translation_number_le (n:ℕ) : dist ((f^n) 0) (n * f.translation_number) ≤ 1 := f.translation_number_pow n ▸ (f^n).dist_map_zero_translation_number_le lemma tendsto_translation_number₀' : tendsto (λ n:ℕ, (f^(n+1)) 0 / (n+1)) at_top (𝓝 $ τ f) := begin refine (tendsto_iff_dist_tendsto_zero.2 $ squeeze_zero (λ _, dist_nonneg) (λ n, _) ((tendsto_const_div_at_top_nhds_0_nat 1).comp (tendsto_add_at_top_nat 1))), dsimp, have : (0:ℝ) < n + 1 := n.cast_add_one_pos, rw [real.dist_eq, div_sub' _ _ _ (ne_of_gt this), abs_div, ← real.dist_eq, abs_of_pos this, div_le_div_right this, ← nat.cast_add_one], apply dist_pow_map_zero_mul_translation_number_le end lemma tendsto_translation_number₀ : tendsto (λ n:ℕ, ((f^n) 0) / n) at_top (𝓝 $ τ f) := (tendsto_add_at_top_iff_nat 1).1 f.tendsto_translation_number₀' /-- For any `x : ℝ` the sequence $\frac{f^n(x)-x}{n}$ tends to the translation number of `f`. In particular, this limit does not depend on `x`. -/ lemma tendsto_translation_number (x : ℝ) : tendsto (λ n:ℕ, ((f^n) x - x) / n) at_top (𝓝 $ τ f) := begin rw [← translation_number_conj_eq' (translate $ multiplicative.of_add x)], convert tendsto_translation_number₀ _, ext n, simp [sub_eq_neg_add, units.conj_pow'] end lemma tendsto_translation_number' (x : ℝ) : tendsto (λ n:ℕ, ((f^(n+1)) x - x) / (n+1)) at_top (𝓝 $ τ f) := (tendsto_add_at_top_iff_nat 1).2 (f.tendsto_translation_number x) lemma translation_number_mono : monotone τ := λ f g h, le_of_tendsto_of_tendsto' f.tendsto_translation_number₀ g.tendsto_translation_number₀ $ λ n, div_le_div_of_le_of_nonneg (pow_mono h n 0) n.cast_nonneg lemma translation_number_translate (x : ℝ) : τ (translate $ multiplicative.of_add x) = x := translation_number_eq_of_tendsto₀' _ $ by simp [nat.cast_add_one_ne_zero, mul_div_cancel_left, tendsto_const_nhds] lemma translation_number_le_of_le_add {z : ℝ} (hz : ∀ x, f x ≤ x + z) : τ f ≤ z := translation_number_translate z ▸ translation_number_mono (λ x, trans_rel_left _ (hz x) (add_comm _ _)) lemma le_translation_number_of_add_le {z : ℝ} (hz : ∀ x, x + z ≤ f x) : z ≤ τ f := translation_number_translate z ▸ translation_number_mono (λ x, trans_rel_right _ (add_comm _ _) (hz x)) lemma translation_number_le_of_le_add_int {x : ℝ} {m : ℤ} (h : f x ≤ x + m) : τ f ≤ m := le_of_tendsto' (f.tendsto_translation_number' x) $ λ n, (div_le_iff' (n.cast_add_one_pos : (0 : ℝ) < _)).mpr $ sub_le_iff_le_add'.2 $ (coe_pow f (n + 1)).symm ▸ f.iterate_le_of_map_le_add_int h (n + 1) lemma translation_number_le_of_le_add_nat {x : ℝ} {m : ℕ} (h : f x ≤ x + m) : τ f ≤ m := @translation_number_le_of_le_add_int f x m h lemma le_translation_number_of_add_int_le {x : ℝ} {m : ℤ} (h : x + m ≤ f x) : ↑m ≤ τ f := ge_of_tendsto' (f.tendsto_translation_number' x) $ λ n, (le_div_iff (n.cast_add_one_pos : (0 : ℝ) < _)).mpr $ le_sub_iff_add_le'.2 $ by simp only [coe_pow, mul_comm (m:ℝ), ← nat.cast_add_one, f.le_iterate_of_add_int_le_map h] lemma le_translation_number_of_add_nat_le {x : ℝ} {m : ℕ} (h : x + m ≤ f x) : ↑m ≤ τ f := @le_translation_number_of_add_int_le f x m h /-- If `f x - x` is an integer number `m` for some point `x`, then `τ f = m`. On the circle this means that a map with a fixed point has rotation number zero. -/ lemma translation_number_of_eq_add_int {x : ℝ} {m : ℤ} (h : f x = x + m) : τ f = m := le_antisymm (translation_number_le_of_le_add_int f $ le_of_eq h) (le_translation_number_of_add_int_le f $ le_of_eq h.symm) lemma floor_sub_le_translation_number (x : ℝ) : ↑⌊f x - x⌋ ≤ τ f := le_translation_number_of_add_int_le f $ le_sub_iff_add_le'.1 (floor_le $ f x - x) lemma translation_number_le_ceil_sub (x : ℝ) : τ f ≤ ⌈f x - x⌉ := translation_number_le_of_le_add_int f $ sub_le_iff_le_add'.1 (le_ceil $ f x - x) lemma map_lt_of_translation_number_lt_int {n : ℤ} (h : τ f < n) (x : ℝ) : f x < x + n := not_le.1 $ mt f.le_translation_number_of_add_int_le $ not_le.2 h lemma map_lt_of_translation_number_lt_nat {n : ℕ} (h : τ f < n) (x : ℝ) : f x < x + n := @map_lt_of_translation_number_lt_int f n h x lemma map_lt_add_floor_translation_number_add_one (x : ℝ) : f x < x + ⌊τ f⌋ + 1 := begin rw [add_assoc], norm_cast, refine map_lt_of_translation_number_lt_int _ _ _, push_cast, exact lt_floor_add_one _ end lemma map_lt_add_translation_number_add_one (x : ℝ) : f x < x + τ f + 1 := calc f x < x + ⌊τ f⌋ + 1 : f.map_lt_add_floor_translation_number_add_one x ... ≤ x + τ f + 1 : by { mono*, exact floor_le (τ f) } lemma lt_map_of_int_lt_translation_number {n : ℤ} (h : ↑n < τ f) (x : ℝ) : x + n < f x := not_le.1 $ mt f.translation_number_le_of_le_add_int $ not_le.2 h lemma lt_map_of_nat_lt_translation_number {n : ℕ} (h : ↑n < τ f) (x : ℝ) : x + n < f x := @lt_map_of_int_lt_translation_number f n h x /-- If `f^n x - x`, `n > 0`, is an integer number `m` for some point `x`, then `τ f = m / n`. On the circle this means that a map with a periodic orbit has a rational rotation number. -/ lemma translation_number_of_map_pow_eq_add_int {x : ℝ} {n : ℕ} {m : ℤ} (h : (f^n) x = x + m) (hn : 0 < n) : τ f = m / n := begin have := (f^n).translation_number_of_eq_add_int h, rwa [translation_number_pow, mul_comm, ← eq_div_iff] at this, exact nat.cast_ne_zero.2 (ne_of_gt hn) end /-- If a predicate depends only on `f x - x` and holds for all `0 ≤ x ≤ 1`, then it holds for all `x`. -/ lemma forall_map_sub_of_Icc (P : ℝ → Prop) (h : ∀ x ∈ Icc (0:ℝ) 1, P (f x - x)) (x : ℝ) : P (f x - x) := f.map_fract_sub_fract_eq x ▸ h _ ⟨fract_nonneg _, le_of_lt (fract_lt_one _)⟩ lemma translation_number_lt_of_forall_lt_add (hf : continuous f) {z : ℝ} (hz : ∀ x, f x < x + z) : τ f < z := begin obtain ⟨x, xmem, hx⟩ : ∃ x ∈ Icc (0:ℝ) 1, ∀ y ∈ Icc (0:ℝ) 1, f y - y ≤ f x - x, from compact_Icc.exists_forall_ge (nonempty_Icc.2 zero_le_one) (hf.sub continuous_id).continuous_on, refine lt_of_le_of_lt _ (sub_lt_iff_lt_add'.2 $ hz x), apply translation_number_le_of_le_add, simp only [← sub_le_iff_le_add'], exact f.forall_map_sub_of_Icc (λ a, a ≤ f x - x) hx end lemma lt_translation_number_of_forall_add_lt (hf : continuous f) {z : ℝ} (hz : ∀ x, x + z < f x) : z < τ f := begin obtain ⟨x, xmem, hx⟩ : ∃ x ∈ Icc (0:ℝ) 1, ∀ y ∈ Icc (0:ℝ) 1, f x - x ≤ f y - y, from compact_Icc.exists_forall_le (nonempty_Icc.2 zero_le_one) (hf.sub continuous_id).continuous_on, refine lt_of_lt_of_le (lt_sub_iff_add_lt'.2 $ hz x) _, apply le_translation_number_of_add_le, simp only [← le_sub_iff_add_le'], exact f.forall_map_sub_of_Icc _ hx end /-- If `f` is a continuous monotone map `ℝ → ℝ`, `f (x + 1) = f x + 1`, then there exists `x` such that `f x = x + τ f`. -/ lemma exists_eq_add_translation_number (hf : continuous f) : ∃ x, f x = x + τ f := begin obtain ⟨a, ha⟩ : ∃ x, f x ≤ x + f.translation_number, { by_contradiction H, push_neg at H, exact lt_irrefl _ (f.lt_translation_number_of_forall_add_lt hf H) }, obtain ⟨b, hb⟩ : ∃ x, x + τ f ≤ f x, { by_contradiction H, push_neg at H, exact lt_irrefl _ (f.translation_number_lt_of_forall_lt_add hf H) }, exact intermediate_value_univ₂ hf (continuous_id.add continuous_const) ha hb end lemma translation_number_eq_int_iff (hf : continuous f) {m : ℤ} : τ f = m ↔ ∃ x, f x = x + m := begin refine ⟨λ h, h ▸ f.exists_eq_add_translation_number hf, _⟩, rintros ⟨x, hx⟩, exact f.translation_number_of_eq_add_int hx end lemma continuous_pow (hf : continuous f) (n : ℕ) : continuous ⇑(f^n : circle_deg1_lift) := by { rw coe_pow, exact hf.iterate n } lemma translation_number_eq_rat_iff (hf : continuous f) {m : ℤ} {n : ℕ} (hn : 0 < n) : τ f = m / n ↔ ∃ x, (f^n) x = x + m := begin rw [eq_div_iff, mul_comm, ← translation_number_pow]; [skip, exact ne_of_gt (nat.cast_pos.2 hn)], exact (f^n).translation_number_eq_int_iff (f.continuous_pow hf n) end /-- Consider two actions `f₁ f₂ : G →* circle_deg1_lift` of a group on the real line by lifts of orientation preserving circle homeomorphisms. Suppose that for each `g : G` the homeomorphisms `f₁ g` and `f₂ g` have equal rotation numbers. Then there exists `F : circle_deg1_lift` such that `F * f₁ g = f₂ g * F` for all `g : G`. This is a version of Proposition 5.4 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. -/ lemma semiconj_of_group_action_of_forall_translation_number_eq {G : Type*} [group G] (f₁ f₂ : G →* circle_deg1_lift) (h : ∀ g, τ (f₁ g) = τ (f₂ g)) : ∃ F : circle_deg1_lift, ∀ g, semiconj F (f₁ g) (f₂ g) := begin -- Equality of translation number guarantees that for each `x` -- the set `{f₂ g⁻¹ (f₁ g x) | g : G}` is bounded above. have : ∀ x, bdd_above (range $ λ g, f₂ g⁻¹ (f₁ g x)), { refine λ x, ⟨x + 2, _⟩, rintro _ ⟨g, rfl⟩, have : τ (f₂ g⁻¹) = -τ (f₂ g), by rw [← monoid_hom.coe_to_hom_units, monoid_hom.map_inv, translation_number_units_inv, monoid_hom.coe_to_hom_units], calc f₂ g⁻¹ (f₁ g x) ≤ f₂ g⁻¹ (x + τ (f₁ g) + 1) : mono _ (map_lt_add_translation_number_add_one _ _).le ... = f₂ g⁻¹ (x + τ (f₂ g)) + 1 : by rw [h, map_add_one] ... ≤ x + τ (f₂ g) + τ (f₂ g⁻¹) + 1 + 1 : by { mono, exact (map_lt_add_translation_number_add_one _ _).le } ... = x + 2 : by simp [this, bit0, add_assoc] }, -- We have a theorem about actions by `order_iso`, so we introduce auxiliary maps -- to `ℝ ≃o ℝ`. set F₁ := to_order_iso.comp f₁.to_hom_units, set F₂ := to_order_iso.comp f₂.to_hom_units, have hF₁ : ∀ g, ⇑(F₁ g) = f₁ g := λ _, rfl, have hF₂ : ∀ g, ⇑(F₂ g) = f₂ g := λ _, rfl, simp only [← hF₁, ← hF₂], -- Now we apply `cSup_div_semiconj` and go back to `f₁` and `f₂`. refine ⟨⟨_, λ x y hxy, _, λ x, _⟩, cSup_div_semiconj F₂ F₁ (λ x, _)⟩; simp only [hF₁, hF₂, ← monoid_hom.map_inv, coe_mk], { refine csupr_le_csupr (this y) (λ g, _), exact mono _ (mono _ hxy) }, { simp only [map_add_one], exact (map_csupr_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) (monotone_id.add_const (1 : ℝ)) (this x)).symm }, { exact this x } end /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses arguments `f₁ f₂ : units circle_deg1_lift` to assume that `f₁` and `f₂` are homeomorphisms. -/ lemma units_semiconj_of_translation_number_eq {f₁ f₂ : units circle_deg1_lift} (h : τ f₁ = τ f₂) : ∃ F : circle_deg1_lift, semiconj F f₁ f₂ := begin have : ∀ n : multiplicative ℤ, τ ((units.coe_hom _).comp (gpowers_hom _ f₁) n) = τ ((units.coe_hom _).comp (gpowers_hom _ f₂) n), { intro n, simp [h] }, exact (semiconj_of_group_action_of_forall_translation_number_eq _ _ this).imp (λ F hF, hF (multiplicative.of_add 1)) end /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses assumptions `is_unit f₁` and `is_unit f₂` to assume that `f₁` and `f₂` are homeomorphisms. -/ lemma semiconj_of_is_unit_of_translation_number_eq {f₁ f₂ : circle_deg1_lift} (h₁ : is_unit f₁) (h₂ : is_unit f₂) (h : τ f₁ = τ f₂) : ∃ F : circle_deg1_lift, semiconj F f₁ f₂ := by { rcases ⟨h₁, h₂⟩ with ⟨⟨f₁, rfl⟩, ⟨f₂, rfl⟩⟩, exact units_semiconj_of_translation_number_eq h } /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses assumptions `bijective f₁` and `bijective f₂` to assume that `f₁` and `f₂` are homeomorphisms. -/ lemma semiconj_of_bijective_of_translation_number_eq {f₁ f₂ : circle_deg1_lift} (h₁ : bijective f₁) (h₂ : bijective f₂) (h : τ f₁ = τ f₂) : ∃ F : circle_deg1_lift, semiconj F f₁ f₂ := semiconj_of_is_unit_of_translation_number_eq (is_unit_iff_bijective.2 h₁) (is_unit_iff_bijective.2 h₂) h end circle_deg1_lift
21be3f5fc4e7dad0824da2dee16ac0a3b6c5f7d4
4727251e0cd73359b15b664c3170e5d754078599
/src/order/jordan_holder.lean
69d1ef44e76ac37d7da935055dd41705b01b4144
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
30,871
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import order.lattice import data.list.sort import logic.equiv.fin import logic.equiv.functor /-! # Jordan-Hölder Theorem This file proves the Jordan Hölder theorem for a `jordan_holder_lattice`, a class also defined in this file. Examples of `jordan_holder_lattice` include `subgroup G` if `G` is a group, and `submodule R M` if `M` is an `R`-module. Using this approach the theorem need not be proved seperately for both groups and modules, the proof in this file can be applied to both. ## Main definitions The main definitions in this file are `jordan_holder_lattice` and `composition_series`, and the relation `equivalent` on `composition_series` A `jordan_holder_lattice` is the class for which the Jordan Hölder theorem is proved. A Jordan Hölder lattice is a lattice equipped with a notion of maximality, `is_maximal`, and a notion of isomorphism of pairs `iso`. In the example of subgroups of a group, `is_maximal H K` means that `H` is a maximal normal subgroup of `K`, and `iso (H₁, K₁) (H₂, K₂)` means that the quotient `H₁ / K₁` is isomorphic to the quotient `H₂ / K₂`. `iso` must be symmetric and transitive and must satisfy the second isomorphism theorem `iso (H, H ⊔ K) (H ⊓ K, K)`. A `composition_series X` is a finite nonempty series of elements of the lattice `X` such that each element is maximal inside the next. The length of a `composition_series X` is one less than the number of elements in the series. Note that there is no stipulation that a series start from the bottom of the lattice and finish at the top. For a composition series `s`, `s.top` is the largest element of the series, and `s.bot` is the least element. Two `composition_series X`, `s₁` and `s₂` are equivalent if there is a bijection `e : fin s₁.length ≃ fin s₂.length` such that for any `i`, `iso (s₁ i, s₁ i.succ) (s₂ (e i), s₂ (e i.succ))` ## Main theorems The main theorem is `composition_series.jordan_holder`, which says that if two composition series have the same least element and the same largest element, then they are `equivalent`. ## TODO Provide instances of `jordan_holder_lattice` for both submodules and subgroups, and potentially for modular lattices. It is not entirely clear how this should be done. Possibly there should be no global instances of `jordan_holder_lattice`, and the instances should only be defined locally in order to prove the Jordan-Hölder theorem for modules/groups and the API should be transferred because many of the theorems in this file will have stronger versions for modules. There will also need to be an API for mapping composition series across homomorphisms. It is also probably possible to provide an instance of `jordan_holder_lattice` for any `modular_lattice`, and in this case the Jordan-Hölder theorem will say that there is a well defined notion of length of a modular lattice. However an instance of `jordan_holder_lattice` for a modular lattice will not be able to contain the correct notion of isomorphism for modules, so a separate instance for modules will still be required and this will clash with the instance for modular lattices, and so at least one of these instances should not be a global instance. -/ universe u open set /-- A `jordan_holder_lattice` is the class for which the Jordan Hölder theorem is proved. A Jordan Hölder lattice is a lattice equipped with a notion of maximality, `is_maximal`, and a notion of isomorphism of pairs `iso`. In the example of subgroups of a group, `is_maximal H K` means that `H` is a maximal normal subgroup of `K`, and `iso (H₁, K₁) (H₂, K₂)` means that the quotient `H₁ / K₁` is isomorphic to the quotient `H₂ / K₂`. `iso` must be symmetric and transitive and must satisfy the second isomorphism theorem `iso (H, H ⊔ K) (H ⊓ K, K)`. Examples include `subgroup G` if `G` is a group, and `submodule R M` if `M` is an `R`-module. -/ class jordan_holder_lattice (X : Type u) [lattice X] := (is_maximal : X → X → Prop) (lt_of_is_maximal : ∀ {x y}, is_maximal x y → x < y) (sup_eq_of_is_maximal : ∀ {x y z}, is_maximal x z → is_maximal y z → x ≠ y → x ⊔ y = z) (is_maximal_inf_left_of_is_maximal_sup : ∀ {x y}, is_maximal x (x ⊔ y) → is_maximal y (x ⊔ y) → is_maximal (x ⊓ y) x) (iso : (X × X) → (X × X) → Prop) (iso_symm : ∀ {x y}, iso x y → iso y x) (iso_trans : ∀ {x y z}, iso x y → iso y z → iso x z) (second_iso : ∀ {x y}, is_maximal x (x ⊔ y) → iso (x, x ⊔ y) (x ⊓ y, y)) namespace jordan_holder_lattice variables {X : Type u} [lattice X] [jordan_holder_lattice X] lemma is_maximal_inf_right_of_is_maximal_sup {x y : X} (hxz : is_maximal x (x ⊔ y)) (hyz : is_maximal y (x ⊔ y)) : is_maximal (x ⊓ y) y := begin rw [inf_comm], rw [sup_comm] at hxz hyz, exact is_maximal_inf_left_of_is_maximal_sup hyz hxz end lemma is_maximal_of_eq_inf (x b : X) {a y : X} (ha : x ⊓ y = a) (hxy : x ≠ y) (hxb : is_maximal x b) (hyb : is_maximal y b) : is_maximal a y := begin have hb : x ⊔ y = b, from sup_eq_of_is_maximal hxb hyb hxy, substs a b, exact is_maximal_inf_right_of_is_maximal_sup hxb hyb end lemma second_iso_of_eq {x y a b : X} (hm : is_maximal x a) (ha : x ⊔ y = a) (hb : x ⊓ y = b) : iso (x, a) (b, y) := by substs a b; exact second_iso hm lemma is_maximal.iso_refl {x y : X} (h : is_maximal x y) : iso (x, y) (x, y) := second_iso_of_eq h (sup_eq_right.2 (le_of_lt (lt_of_is_maximal h))) (inf_eq_left.2 (le_of_lt (lt_of_is_maximal h))) end jordan_holder_lattice open jordan_holder_lattice attribute [symm] iso_symm attribute [trans] iso_trans /-- A `composition_series X` is a finite nonempty series of elements of a `jordan_holder_lattice` such that each element is maximal inside the next. The length of a `composition_series X` is one less than the number of elements in the series. Note that there is no stipulation that a series start from the bottom of the lattice and finish at the top. For a composition series `s`, `s.top` is the largest element of the series, and `s.bot` is the least element. -/ structure composition_series (X : Type u) [lattice X] [jordan_holder_lattice X] : Type u := (length : ℕ) (series : fin (length + 1) → X) (step' : ∀ i : fin length, is_maximal (series i.cast_succ) (series i.succ)) namespace composition_series variables {X : Type u} [lattice X] [jordan_holder_lattice X] instance : has_coe_to_fun (composition_series X) (λ x, fin (x.length + 1) → X) := { coe := composition_series.series } instance [inhabited X] : inhabited (composition_series X) := ⟨{ length := 0, series := λ _, default, step' := λ x, x.elim0 }⟩ variables {X} lemma step (s : composition_series X) : ∀ i : fin s.length, is_maximal (s i.cast_succ) (s i.succ) := s.step' @[simp] lemma coe_fn_mk (length : ℕ) (series step) : (@composition_series.mk X _ _ length series step : fin length.succ → X) = series := rfl theorem lt_succ (s : composition_series X) (i : fin s.length) : s i.cast_succ < s i.succ := lt_of_is_maximal (s.step _) protected theorem strict_mono (s : composition_series X) : strict_mono s := fin.strict_mono_iff_lt_succ.2 (λ i h, s.lt_succ ⟨i, nat.lt_of_succ_lt_succ h⟩) protected theorem injective (s : composition_series X) : function.injective s := s.strict_mono.injective @[simp] protected theorem inj (s : composition_series X) {i j : fin s.length.succ} : s i = s j ↔ i = j := s.injective.eq_iff instance : has_mem X (composition_series X) := ⟨λ x s, x ∈ set.range s⟩ lemma mem_def {x : X} {s : composition_series X} : x ∈ s ↔ x ∈ set.range s := iff.rfl lemma total {s : composition_series X} {x y : X} (hx : x ∈ s) (hy : y ∈ s) : x ≤ y ∨ y ≤ x := begin rcases set.mem_range.1 hx with ⟨i, rfl⟩, rcases set.mem_range.1 hy with ⟨j, rfl⟩, rw [s.strict_mono.le_iff_le, s.strict_mono.le_iff_le], exact le_total i j end /-- The ordered `list X` of elements of a `composition_series X`. -/ def to_list (s : composition_series X) : list X := list.of_fn s /-- Two `composition_series` are equal if they are the same length and have the same `i`th element for every `i` -/ lemma ext_fun {s₁ s₂ : composition_series X} (hl : s₁.length = s₂.length) (h : ∀ i, s₁ i = s₂ (fin.cast (congr_arg nat.succ hl) i)) : s₁ = s₂ := begin cases s₁, cases s₂, dsimp at *, subst hl, simpa [function.funext_iff] using h end @[simp] lemma length_to_list (s : composition_series X) : s.to_list.length = s.length + 1 := by rw [to_list, list.length_of_fn] lemma to_list_ne_nil (s : composition_series X) : s.to_list ≠ [] := by rw [← list.length_pos_iff_ne_nil, length_to_list]; exact nat.succ_pos _ lemma to_list_injective : function.injective (@composition_series.to_list X _ _) := λ s₁ s₂ (h : list.of_fn s₁ = list.of_fn s₂), have h₁ : s₁.length = s₂.length, from nat.succ_injective ((list.length_of_fn s₁).symm.trans $ (congr_arg list.length h).trans $ list.length_of_fn s₂), have h₂ : ∀ i : fin s₁.length.succ, (s₁ i) = s₂ (fin.cast (congr_arg nat.succ h₁) i), begin assume i, rw [← list.nth_le_of_fn s₁ i, ← list.nth_le_of_fn s₂], simp [h] end, begin cases s₁, cases s₂, dsimp at *, subst h₁, simp only [heq_iff_eq, eq_self_iff_true, true_and], simp only [fin.cast_refl] at h₂, exact funext h₂ end lemma chain'_to_list (s : composition_series X) : list.chain' is_maximal s.to_list := list.chain'_iff_nth_le.2 begin assume i hi, simp only [to_list, list.nth_le_of_fn'], rw [length_to_list] at hi, exact s.step ⟨i, hi⟩ end lemma to_list_sorted (s : composition_series X) : s.to_list.sorted (<) := list.pairwise_iff_nth_le.2 (λ i j hi hij, begin dsimp [to_list], rw [list.nth_le_of_fn', list.nth_le_of_fn'], exact s.strict_mono hij end) lemma to_list_nodup (s : composition_series X) : s.to_list.nodup := list.nodup_iff_nth_le_inj.2 (λ i j hi hj, begin delta to_list, rw [list.nth_le_of_fn', list.nth_le_of_fn', s.injective.eq_iff, fin.ext_iff, fin.coe_mk, fin.coe_mk], exact id end) @[simp] lemma mem_to_list {s : composition_series X} {x : X} : x ∈ s.to_list ↔ x ∈ s := by rw [to_list, list.mem_of_fn, mem_def] /-- Make a `composition_series X` from the ordered list of its elements. -/ def of_list (l : list X) (hl : l ≠ []) (hc : list.chain' is_maximal l) : composition_series X := { length := l.length - 1, series := λ i, l.nth_le i begin conv_rhs { rw ← tsub_add_cancel_of_le (nat.succ_le_of_lt (list.length_pos_of_ne_nil hl)) }, exact i.2 end, step' := λ ⟨i, hi⟩, list.chain'_iff_nth_le.1 hc i hi } lemma length_of_list (l : list X) (hl : l ≠ []) (hc : list.chain' is_maximal l) : (of_list l hl hc).length = l.length - 1 := rfl lemma of_list_to_list (s : composition_series X) : of_list s.to_list s.to_list_ne_nil s.chain'_to_list = s := begin refine ext_fun _ _, { rw [length_of_list, length_to_list, nat.succ_sub_one] }, { rintros ⟨i, hi⟩, dsimp [of_list, to_list], rw [list.nth_le_of_fn'] } end @[simp] lemma of_list_to_list' (s : composition_series X) : of_list s.to_list s.to_list_ne_nil s.chain'_to_list = s := of_list_to_list s @[simp] lemma to_list_of_list (l : list X) (hl : l ≠ []) (hc : list.chain' is_maximal l) : to_list (of_list l hl hc) = l := begin refine list.ext_le _ _, { rw [length_to_list, length_of_list, tsub_add_cancel_of_le (nat.succ_le_of_lt $ list.length_pos_of_ne_nil hl)] }, { assume i hi hi', dsimp [of_list, to_list], rw [list.nth_le_of_fn'], refl } end /-- Two `composition_series` are equal if they have the same elements. See also `ext_fun`. -/ @[ext] lemma ext {s₁ s₂ : composition_series X} (h : ∀ x, x ∈ s₁ ↔ x ∈ s₂) : s₁ = s₂ := to_list_injective $ list.eq_of_perm_of_sorted (by classical; exact list.perm_of_nodup_nodup_to_finset_eq s₁.to_list_nodup s₂.to_list_nodup (finset.ext $ by simp *)) s₁.to_list_sorted s₂.to_list_sorted /-- The largest element of a `composition_series` -/ def top (s : composition_series X) : X := s (fin.last _) lemma top_mem (s : composition_series X) : s.top ∈ s := mem_def.2 (set.mem_range.2 ⟨fin.last _, rfl⟩) @[simp] lemma le_top {s : composition_series X} (i : fin (s.length + 1)) : s i ≤ s.top := s.strict_mono.monotone (fin.le_last _) lemma le_top_of_mem {s : composition_series X} {x : X} (hx : x ∈ s) : x ≤ s.top := let ⟨i, hi⟩ := set.mem_range.2 hx in hi ▸ le_top _ /-- The smallest element of a `composition_series` -/ def bot (s : composition_series X) : X := s 0 lemma bot_mem (s : composition_series X) : s.bot ∈ s := mem_def.2 (set.mem_range.2 ⟨0, rfl⟩) @[simp] lemma bot_le {s : composition_series X} (i : fin (s.length + 1)) : s.bot ≤ s i := s.strict_mono.monotone (fin.zero_le _) lemma bot_le_of_mem {s : composition_series X} {x : X} (hx : x ∈ s) : s.bot ≤ x := let ⟨i, hi⟩ := set.mem_range.2 hx in hi ▸ bot_le _ lemma length_pos_of_mem_ne {s : composition_series X} {x y : X} (hx : x ∈ s) (hy : y ∈ s) (hxy : x ≠ y) : 0 < s.length := let ⟨i, hi⟩ := hx, ⟨j, hj⟩ := hy in have hij : i ≠ j, from mt s.inj.2 $ λ h, hxy (hi ▸ hj ▸ h), hij.lt_or_lt.elim (λ hij, (lt_of_le_of_lt (zero_le i) (lt_of_lt_of_le hij (nat.le_of_lt_succ j.2)))) (λ hji, (lt_of_le_of_lt (zero_le j) (lt_of_lt_of_le hji (nat.le_of_lt_succ i.2)))) lemma forall_mem_eq_of_length_eq_zero {s : composition_series X} (hs : s.length = 0) {x y} (hx : x ∈ s) (hy : y ∈ s) : x = y := by_contradiction (λ hxy, pos_iff_ne_zero.1 (length_pos_of_mem_ne hx hy hxy) hs) /-- Remove the largest element from a `composition_series`. If the series `s` has length zero, then `s.erase_top = s` -/ @[simps] def erase_top (s : composition_series X) : composition_series X := { length := s.length - 1, series := λ i, s ⟨i, lt_of_lt_of_le i.2 (nat.succ_le_succ tsub_le_self)⟩, step' := λ i, begin have := s.step ⟨i, lt_of_lt_of_le i.2 tsub_le_self⟩, cases i, exact this end } lemma top_erase_top (s : composition_series X) : s.erase_top.top = s ⟨s.length - 1, lt_of_le_of_lt tsub_le_self (nat.lt_succ_self _)⟩ := show s _ = s _, from congr_arg s begin ext, simp only [erase_top_length, fin.coe_last, fin.coe_cast_succ, fin.coe_of_nat_eq_mod, fin.coe_mk, coe_coe] end lemma erase_top_top_le (s : composition_series X) : s.erase_top.top ≤ s.top := by simp [erase_top, top, s.strict_mono.le_iff_le, fin.le_iff_coe_le_coe, tsub_le_self] @[simp] lemma bot_erase_top (s : composition_series X) : s.erase_top.bot = s.bot := rfl lemma mem_erase_top_of_ne_of_mem {s : composition_series X} {x : X} (hx : x ≠ s.top) (hxs : x ∈ s) : x ∈ s.erase_top := begin rcases hxs with ⟨i, rfl⟩, have hi : (i : ℕ) < (s.length - 1).succ, { conv_rhs { rw [← nat.succ_sub (length_pos_of_mem_ne ⟨i, rfl⟩ s.top_mem hx), nat.succ_sub_one] }, exact lt_of_le_of_ne (nat.le_of_lt_succ i.2) (by simpa [top, s.inj, fin.ext_iff] using hx) }, refine ⟨i.cast_succ, _⟩, simp [fin.ext_iff, nat.mod_eq_of_lt hi] end lemma mem_erase_top {s : composition_series X} {x : X} (h : 0 < s.length) : x ∈ s.erase_top ↔ x ≠ s.top ∧ x ∈ s := begin simp only [mem_def], dsimp only [erase_top, coe_fn_mk], split, { rintros ⟨i, rfl⟩, have hi : (i : ℕ) < s.length, { conv_rhs { rw [← nat.succ_sub_one s.length, nat.succ_sub h] }, exact i.2 }, simp [top, fin.ext_iff, (ne_of_lt hi)] }, { intro h, exact mem_erase_top_of_ne_of_mem h.1 h.2 } end lemma lt_top_of_mem_erase_top {s : composition_series X} {x : X} (h : 0 < s.length) (hx : x ∈ s.erase_top) : x < s.top := lt_of_le_of_ne (le_top_of_mem ((mem_erase_top h).1 hx).2) ((mem_erase_top h).1 hx).1 lemma is_maximal_erase_top_top {s : composition_series X} (h : 0 < s.length) : is_maximal s.erase_top.top s.top := have s.length - 1 + 1 = s.length, by conv_rhs { rw [← nat.succ_sub_one s.length] }; rw nat.succ_sub h, begin rw [top_erase_top, top], convert s.step ⟨s.length - 1, nat.sub_lt h zero_lt_one⟩; ext; simp [this] end lemma append_cast_add_aux {s₁ s₂ : composition_series X} (i : fin s₁.length) : fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂ (fin.cast_add s₂.length i).cast_succ = s₁ i.cast_succ := by { cases i, simp [fin.append, *] } lemma append_succ_cast_add_aux {s₁ s₂ : composition_series X} (i : fin s₁.length) (h : s₁ (fin.last _) = s₂ 0) : fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂ (fin.cast_add s₂.length i).succ = s₁ i.succ := begin cases i with i hi, simp only [fin.append, hi, fin.succ_mk, function.comp_app, fin.cast_succ_mk, fin.coe_mk, fin.cast_add_mk], split_ifs, { refl }, { have : i + 1 = s₁.length, from le_antisymm hi (le_of_not_gt h_1), calc s₂ ⟨i + 1 - s₁.length, by simp [this]⟩ = s₂ 0 : congr_arg s₂ (by simp [fin.ext_iff, this]) ... = s₁ (fin.last _) : h.symm ... = _ : congr_arg s₁ (by simp [fin.ext_iff, this]) } end lemma append_nat_add_aux {s₁ s₂ : composition_series X} (i : fin s₂.length) : fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂ (fin.nat_add s₁.length i).cast_succ = s₂ i.cast_succ := begin cases i, simp only [fin.append, nat.not_lt_zero, fin.nat_add_mk, add_lt_iff_neg_left, add_tsub_cancel_left, dif_neg, fin.cast_succ_mk, not_false_iff, fin.coe_mk] end lemma append_succ_nat_add_aux {s₁ s₂ : composition_series X} (i : fin s₂.length) : fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂ (fin.nat_add s₁.length i).succ = s₂ i.succ := begin cases i with i hi, simp only [fin.append, add_assoc, nat.not_lt_zero, fin.nat_add_mk, add_lt_iff_neg_left, add_tsub_cancel_left, fin.succ_mk, dif_neg, not_false_iff, fin.coe_mk] end /-- Append two composition series `s₁` and `s₂` such that the least element of `s₁` is the maximum element of `s₂`. -/ @[simps length] def append (s₁ s₂ : composition_series X) (h : s₁.top = s₂.bot) : composition_series X := { length := s₁.length + s₂.length, series := fin.append (nat.add_succ _ _).symm (s₁ ∘ fin.cast_succ) s₂, step' := λ i, begin refine fin.add_cases _ _ i, { intro i, rw [append_succ_cast_add_aux _ h, append_cast_add_aux], exact s₁.step i }, { intro i, rw [append_nat_add_aux, append_succ_nat_add_aux], exact s₂.step i } end } @[simp] lemma append_cast_add {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) (i : fin s₁.length) : append s₁ s₂ h (fin.cast_add s₂.length i).cast_succ = s₁ i.cast_succ := append_cast_add_aux i @[simp] lemma append_succ_cast_add {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) (i : fin s₁.length) : append s₁ s₂ h (fin.cast_add s₂.length i).succ = s₁ i.succ := append_succ_cast_add_aux i h @[simp] lemma append_nat_add {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) (i : fin s₂.length) : append s₁ s₂ h (fin.nat_add s₁.length i).cast_succ = s₂ i.cast_succ := append_nat_add_aux i @[simp] lemma append_succ_nat_add {s₁ s₂ : composition_series X} (h : s₁.top = s₂.bot) (i : fin s₂.length) : append s₁ s₂ h (fin.nat_add s₁.length i).succ = s₂ i.succ := append_succ_nat_add_aux i /-- Add an element to the top of a `composition_series` -/ @[simps length] def snoc (s : composition_series X) (x : X) (hsat : is_maximal s.top x) : composition_series X := { length := s.length + 1, series := fin.snoc s x, step' := λ i, begin refine fin.last_cases _ _ i, { rwa [fin.snoc_cast_succ, fin.succ_last, fin.snoc_last, ← top] }, { intro i, rw [fin.snoc_cast_succ, ← fin.cast_succ_fin_succ, fin.snoc_cast_succ], exact s.step _ } end } @[simp] lemma top_snoc (s : composition_series X) (x : X) (hsat : is_maximal s.top x) : (snoc s x hsat).top = x := fin.snoc_last _ _ @[simp] lemma snoc_last (s : composition_series X) (x : X) (hsat : is_maximal s.top x) : snoc s x hsat (fin.last (s.length + 1)) = x := fin.snoc_last _ _ @[simp] lemma snoc_cast_succ (s : composition_series X) (x : X) (hsat : is_maximal s.top x) (i : fin (s.length + 1)) : snoc s x hsat (i.cast_succ) = s i := fin.snoc_cast_succ _ _ _ @[simp] lemma bot_snoc (s : composition_series X) (x : X) (hsat : is_maximal s.top x) : (snoc s x hsat).bot = s.bot := by rw [bot, bot, ← fin.cast_succ_zero, snoc_cast_succ] lemma mem_snoc {s : composition_series X} {x y: X} {hsat : is_maximal s.top x} : y ∈ snoc s x hsat ↔ y ∈ s ∨ y = x := begin simp only [snoc, mem_def], split, { rintros ⟨i, rfl⟩, refine fin.last_cases _ (λ i, _) i, { right, simp }, { left, simp } }, { intro h, rcases h with ⟨i, rfl⟩ | rfl, { use i.cast_succ, simp }, { use (fin.last _), simp } } end lemma eq_snoc_erase_top {s : composition_series X} (h : 0 < s.length) : s = snoc (erase_top s) s.top (is_maximal_erase_top_top h) := begin ext x, simp [mem_snoc, mem_erase_top h], by_cases h : x = s.top; simp [*, s.top_mem] end @[simp] lemma snoc_erase_top_top {s : composition_series X} (h : is_maximal s.erase_top.top s.top) : s.erase_top.snoc s.top h = s := have h : 0 < s.length, from nat.pos_of_ne_zero begin assume hs, refine ne_of_gt (lt_of_is_maximal h) _, simp [top, fin.ext_iff, hs] end, (eq_snoc_erase_top h).symm /-- Two `composition_series X`, `s₁` and `s₂` are equivalent if there is a bijection `e : fin s₁.length ≃ fin s₂.length` such that for any `i`, `iso (s₁ i) (s₁ i.succ) (s₂ (e i), s₂ (e i.succ))` -/ def equivalent (s₁ s₂ : composition_series X) : Prop := ∃ f : fin s₁.length ≃ fin s₂.length, ∀ i : fin s₁.length, iso (s₁ i.cast_succ, s₁ i.succ) (s₂ (f i).cast_succ, s₂ (f i).succ) namespace equivalent @[refl] lemma refl (s : composition_series X) : equivalent s s := ⟨equiv.refl _, λ _, (s.step _).iso_refl⟩ @[symm] lemma symm {s₁ s₂ : composition_series X} (h : equivalent s₁ s₂) : equivalent s₂ s₁ := ⟨h.some.symm, λ i, iso_symm (by simpa using h.some_spec (h.some.symm i))⟩ @[trans] lemma trans {s₁ s₂ s₃ : composition_series X} (h₁ : equivalent s₁ s₂) (h₂ : equivalent s₂ s₃) : equivalent s₁ s₃ := ⟨h₁.some.trans h₂.some, λ i, iso_trans (h₁.some_spec i) (h₂.some_spec (h₁.some i))⟩ lemma append {s₁ s₂ t₁ t₂ : composition_series X} (hs : s₁.top = s₂.bot) (ht : t₁.top = t₂.bot) (h₁ : equivalent s₁ t₁) (h₂ : equivalent s₂ t₂) : equivalent (append s₁ s₂ hs) (append t₁ t₂ ht) := let e : fin (s₁.length + s₂.length) ≃ fin (t₁.length + t₂.length) := calc fin (s₁.length + s₂.length) ≃ fin s₁.length ⊕ fin s₂.length : fin_sum_fin_equiv.symm ... ≃ fin t₁.length ⊕ fin t₂.length : equiv.sum_congr h₁.some h₂.some ... ≃ fin (t₁.length + t₂.length) : fin_sum_fin_equiv in ⟨e, begin assume i, refine fin.add_cases _ _ i, { assume i, simpa [top, bot] using h₁.some_spec i }, { assume i, simpa [top, bot] using h₂.some_spec i } end⟩ protected lemma snoc {s₁ s₂ : composition_series X} {x₁ x₂ : X} {hsat₁ : is_maximal s₁.top x₁} {hsat₂ : is_maximal s₂.top x₂} (hequiv : equivalent s₁ s₂) (htop : iso (s₁.top, x₁) (s₂.top, x₂)) : equivalent (s₁.snoc x₁ hsat₁) (s₂.snoc x₂ hsat₂) := let e : fin s₁.length.succ ≃ fin s₂.length.succ := calc fin (s₁.length + 1) ≃ option (fin s₁.length) : fin_succ_equiv_last ... ≃ option (fin s₂.length) : functor.map_equiv option hequiv.some ... ≃ fin (s₂.length + 1) : fin_succ_equiv_last.symm in ⟨e, λ i, begin refine fin.last_cases _ _ i, { simpa [top] using htop }, { assume i, simpa [fin.succ_cast_succ] using hequiv.some_spec i } end⟩ lemma length_eq {s₁ s₂ : composition_series X} (h : equivalent s₁ s₂) : s₁.length = s₂.length := by simpa using fintype.card_congr h.some lemma snoc_snoc_swap {s : composition_series X} {x₁ x₂ y₁ y₂ : X} {hsat₁ : is_maximal s.top x₁} {hsat₂ : is_maximal s.top x₂} {hsaty₁ : is_maximal (snoc s x₁ hsat₁).top y₁} {hsaty₂ : is_maximal (snoc s x₂ hsat₂).top y₂} (hr₁ : iso (s.top, x₁) (x₂, y₂)) (hr₂ : iso (x₁, y₁) (s.top, x₂)) : equivalent (snoc (snoc s x₁ hsat₁) y₁ hsaty₁) (snoc (snoc s x₂ hsat₂) y₂ hsaty₂) := let e : fin (s.length + 1 + 1) ≃ fin (s.length + 1 + 1) := equiv.swap (fin.last _) (fin.cast_succ (fin.last _)) in have h1 : ∀ {i : fin s.length}, i.cast_succ.cast_succ ≠ (fin.last _).cast_succ, from λ _, ne_of_lt (by simp [fin.cast_succ_lt_last]), have h2 : ∀ {i : fin s.length}, i.cast_succ.cast_succ ≠ (fin.last _), from λ _, ne_of_lt (by simp [fin.cast_succ_lt_last]), ⟨e, begin intro i, dsimp only [e], refine fin.last_cases _ (λ i, _) i, { erw [equiv.swap_apply_left, snoc_cast_succ, snoc_last, fin.succ_last, snoc_last, snoc_cast_succ, snoc_cast_succ, fin.succ_cast_succ, snoc_cast_succ, fin.succ_last, snoc_last], exact hr₂ }, { refine fin.last_cases _ (λ i, _) i, { erw [equiv.swap_apply_right, snoc_cast_succ, snoc_cast_succ, snoc_cast_succ, fin.succ_cast_succ, snoc_cast_succ, fin.succ_last, snoc_last, snoc_last, fin.succ_last, snoc_last], exact hr₁ }, { erw [equiv.swap_apply_of_ne_of_ne h2 h1, snoc_cast_succ, snoc_cast_succ, snoc_cast_succ, snoc_cast_succ, fin.succ_cast_succ, snoc_cast_succ, fin.succ_cast_succ, snoc_cast_succ, snoc_cast_succ, snoc_cast_succ], exact (s.step i).iso_refl } } end⟩ end equivalent lemma length_eq_zero_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero {s₁ s₂ : composition_series X} (hb : s₁.bot = s₂.bot) (ht : s₁.top = s₂.top) (hs₁ : s₁.length = 0) : s₂.length = 0 := begin have : s₁.bot = s₁.top, from congr_arg s₁ (fin.ext (by simp [hs₁])), have : (fin.last s₂.length) = (0 : fin s₂.length.succ), from s₂.injective (hb.symm.trans (this.trans ht)).symm, simpa [fin.ext_iff] end lemma length_pos_of_bot_eq_bot_of_top_eq_top_of_length_pos {s₁ s₂ : composition_series X} (hb : s₁.bot = s₂.bot) (ht : s₁.top = s₂.top) : 0 < s₁.length → 0 < s₂.length := not_imp_not.1 begin simp only [pos_iff_ne_zero, ne.def, not_iff_not, not_not], exact length_eq_zero_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero hb.symm ht.symm end lemma eq_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero {s₁ s₂ : composition_series X} (hb : s₁.bot = s₂.bot) (ht : s₁.top = s₂.top) (hs₁0 : s₁.length = 0) : s₁ = s₂ := have ∀ x, x ∈ s₁ ↔ x = s₁.top, from λ x, ⟨λ hx, forall_mem_eq_of_length_eq_zero hs₁0 hx s₁.top_mem, λ hx, hx.symm ▸ s₁.top_mem⟩, have ∀ x, x ∈ s₂ ↔ x = s₂.top, from λ x, ⟨λ hx, forall_mem_eq_of_length_eq_zero (length_eq_zero_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero hb ht hs₁0) hx s₂.top_mem, λ hx, hx.symm ▸ s₂.top_mem⟩, by { ext, simp * } /-- Given a `composition_series`, `s`, and an element `x` such that `x` is maximal inside `s.top` there is a series, `t`, such that `t.top = x`, `t.bot = s.bot` and `snoc t s.top _` is equivalent to `s`. -/ lemma exists_top_eq_snoc_equivalant (s : composition_series X) (x : X) (hm : is_maximal x s.top) (hb : s.bot ≤ x) : ∃ t : composition_series X, t.bot = s.bot ∧ t.length + 1 = s.length ∧ ∃ htx : t.top = x, equivalent s (snoc t s.top (htx.symm ▸ hm)) := begin induction hn : s.length with n ih generalizing s x, { exact (ne_of_gt (lt_of_le_of_lt hb (lt_of_is_maximal hm)) (forall_mem_eq_of_length_eq_zero hn s.top_mem s.bot_mem)).elim }, { have h0s : 0 < s.length, from hn.symm ▸ nat.succ_pos _, by_cases hetx : s.erase_top.top = x, { use s.erase_top, simp [← hetx, hn] }, { have imxs : is_maximal (x ⊓ s.erase_top.top) s.erase_top.top, from is_maximal_of_eq_inf x s.top rfl (ne.symm hetx) hm (is_maximal_erase_top_top h0s), have := ih _ _ imxs (le_inf (by simpa) (le_top_of_mem s.erase_top.bot_mem)) (by simp [hn]), rcases this with ⟨t, htb, htl, htt, hteqv⟩, have hmtx : is_maximal t.top x, from is_maximal_of_eq_inf s.erase_top.top s.top (by rw [inf_comm, htt]) hetx (is_maximal_erase_top_top h0s) hm, use snoc t x hmtx, refine ⟨by simp [htb], by simp [htl], by simp, _⟩, have : s.equivalent ((snoc t s.erase_top.top (htt.symm ▸ imxs)).snoc s.top (by simpa using is_maximal_erase_top_top h0s)), { conv_lhs { rw eq_snoc_erase_top h0s }, exact equivalent.snoc hteqv (by simpa using (is_maximal_erase_top_top h0s).iso_refl) }, refine this.trans _, refine equivalent.snoc_snoc_swap _ _, { exact iso_symm (second_iso_of_eq hm (sup_eq_of_is_maximal hm (is_maximal_erase_top_top h0s) (ne.symm hetx)) htt.symm) }, { exact second_iso_of_eq (is_maximal_erase_top_top h0s) (sup_eq_of_is_maximal (is_maximal_erase_top_top h0s) hm hetx) (by rw [inf_comm, htt]) } } } end /-- The **Jordan-Hölder** theorem, stated for any `jordan_holder_lattice`. If two composition series start and finish at the same place, they are equivalent. -/ theorem jordan_holder (s₁ s₂ : composition_series X) (hb : s₁.bot = s₂.bot) (ht : s₁.top = s₂.top) : equivalent s₁ s₂ := begin induction hle : s₁.length with n ih generalizing s₁ s₂, { rw [eq_of_bot_eq_bot_of_top_eq_top_of_length_eq_zero hb ht hle] }, { have h0s₂ : 0 < s₂.length, from length_pos_of_bot_eq_bot_of_top_eq_top_of_length_pos hb ht (hle.symm ▸ nat.succ_pos _), rcases exists_top_eq_snoc_equivalant s₁ s₂.erase_top.top (ht.symm ▸ is_maximal_erase_top_top h0s₂) (hb.symm ▸ s₂.bot_erase_top ▸ bot_le_of_mem (top_mem _)) with ⟨t, htb, htl, htt, hteq⟩, have := ih t s₂.erase_top (by simp [htb, ← hb]) htt (nat.succ_inj'.1 (htl.trans hle)), refine hteq.trans _, conv_rhs { rw [eq_snoc_erase_top h0s₂] }, simp only [ht], exact equivalent.snoc this (by simp [htt, (is_maximal_erase_top_top h0s₂).iso_refl]) } end end composition_series
c2f9f56ad847840e69d43ef24c005aca1a6c491f
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/metric_space/emetric_paracompact.lean
801c60c72932cd0cda501a8c0842309658d0db0d
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
8,366
lean
/- Copyright (c) 202 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import set_theory.ordinal.basic import topology.metric_space.emetric_space import topology.paracompact /-! # (Extended) metric spaces are paracompact In this file we provide two instances: * `emetric.paracompact_space`: a `pseudo_emetric_space` is paracompact; formalization is based on [MR0236876]; * `emetric.normal_of_metric`: an `emetric_space` is a normal topological space. ## Tags metric space, paracompact space, normal space -/ variable {α : Type*} open_locale ennreal topological_space open set namespace emetric /-- A `pseudo_emetric_space` is always a paracompact space. Formalization is based on [MR0236876]. -/ @[priority 100] -- See note [lower instance priority] instance [pseudo_emetric_space α] : paracompact_space α := begin classical, /- We start with trivial observations about `1 / 2 ^ k`. Here and below we use `1 / 2 ^ k` in the comments and `2⁻¹ ^ k` in the code. -/ have pow_pos : ∀ k : ℕ, (0 : ℝ≥0∞) < 2⁻¹ ^ k, from λ k, ennreal.pow_pos (ennreal.inv_pos.2 ennreal.two_ne_top) _, have hpow_le : ∀ {m n : ℕ}, m ≤ n → (2⁻¹ : ℝ≥0∞) ^ n ≤ 2⁻¹ ^ m, from λ m n h, ennreal.pow_le_pow_of_le_one (ennreal.inv_le_one.2 ennreal.one_lt_two.le) h, have h2pow : ∀ n : ℕ, 2 * (2⁻¹ : ℝ≥0∞) ^ (n + 1) = 2⁻¹ ^ n, by { intro n, simp [pow_succ, ← mul_assoc, ennreal.mul_inv_cancel] }, -- Consider an open covering `S : set (set α)` refine ⟨λ ι s ho hcov, _⟩, simp only [Union_eq_univ_iff] at hcov, -- choose a well founded order on `S` letI : linear_order ι := linear_order_of_STO' well_ordering_rel, have wf : well_founded ((<) : ι → ι → Prop) := @is_well_order.wf ι well_ordering_rel _, -- Let `ind x` be the minimal index `s : S` such that `x ∈ s`. set ind : α → ι := λ x, wf.min {i : ι | x ∈ s i} (hcov x), have mem_ind : ∀ x, x ∈ s (ind x), from λ x, wf.min_mem _ (hcov x), have nmem_of_lt_ind : ∀ {x i}, i < (ind x) → x ∉ s i, from λ x i hlt hxi, wf.not_lt_min _ (hcov x) hxi hlt, /- The refinement `D : ℕ → ι → set α` is defined recursively. For each `n` and `i`, `D n i` is the union of balls `ball x (1 / 2 ^ n)` over all points `x` such that * `ind x = i`; * `x` does not belong to any `D m j`, `m < n`; * `ball x (3 / 2 ^ n) ⊆ s i`; We define this sequence using `nat.strong_rec_on'`, then restate it as `Dn` and `memD`. -/ set D : ℕ → ι → set α := λ n, nat.strong_rec_on' n (λ n D' i, ⋃ (x : α) (hxs : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i) (hlt : ∀ (m < n) (j : ι), x ∉ D' m ‹_› j), ball x (2⁻¹ ^ n)), have Dn : ∀ n i, D n i = ⋃ (x : α) (hxs : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i) (hlt : ∀ (m < n) (j : ι), x ∉ D m j), ball x (2⁻¹ ^ n), from λ n s, by { simp only [D], rw nat.strong_rec_on_beta' }, have memD : ∀ {n i y}, y ∈ D n i ↔ ∃ x (hi : ind x = i) (hb : ball x (3 * 2⁻¹ ^ n) ⊆ s i) (hlt : ∀ (m < n) (j : ι), x ∉ D m j), edist y x < 2⁻¹ ^ n, { intros n i y, rw [Dn n i], simp only [mem_Union, mem_ball] }, -- The sets `D n i` cover the whole space. Indeed, for each `x` we can choose `n` such that -- `ball x (3 / 2 ^ n) ⊆ s (ind x)`, then either `x ∈ D n i`, or `x ∈ D m i` for some `m < n`. have Dcov : ∀ x, ∃ n i, x ∈ D n i, { intro x, obtain ⟨n, hn⟩ : ∃ n : ℕ, ball x (3 * 2⁻¹ ^ n) ⊆ s (ind x), { -- This proof takes 5 lines because we can't import `specific_limits` here rcases is_open_iff.1 (ho $ ind x) x (mem_ind x) with ⟨ε, ε0, hε⟩, have : 0 < ε / 3 := ennreal.div_pos_iff.2 ⟨ε0.lt.ne', ennreal.coe_ne_top⟩, rcases ennreal.exists_inv_two_pow_lt this.ne' with ⟨n, hn⟩, refine ⟨n, subset.trans (ball_subset_ball _) hε⟩, simpa only [div_eq_mul_inv, mul_comm] using (ennreal.mul_lt_of_lt_div hn).le }, by_contra' h, apply h n (ind x), exact memD.2 ⟨x, rfl, hn, λ _ _ _, h _ _, mem_ball_self (pow_pos _)⟩ }, -- Each `D n i` is a union of open balls, hence it is an open set have Dopen : ∀ n i, is_open (D n i), { intros n i, rw Dn, iterate 4 { refine is_open_Union (λ _, _) }, exact is_open_ball }, -- the covering `D n i` is a refinement of the original covering: `D n i ⊆ s i` have HDS : ∀ n i, D n i ⊆ s i, { intros n s x, rw memD, rintro ⟨y, rfl, hsub, -, hyx⟩, refine hsub (lt_of_lt_of_le hyx _), calc 2⁻¹ ^ n = 1 * 2⁻¹ ^ n : (one_mul _).symm ... ≤ 3 * 2⁻¹ ^ n : ennreal.mul_le_mul _ le_rfl, -- TODO: use `norm_num` have : ((1 : ℕ) : ℝ≥0∞) ≤ (3 : ℕ), from ennreal.coe_nat_le_coe_nat.2 (by norm_num1), exact_mod_cast this }, -- Let us show the rest of the properties. Since the definition expects a family indexed -- by a single parameter, we use `ℕ × ι` as the domain. refine ⟨ℕ × ι, λ ni, D ni.1 ni.2, λ _, Dopen _ _, _, _, λ ni, ⟨ni.2, HDS _ _⟩⟩, -- The sets `D n i` cover the whole space as we proved earlier { refine Union_eq_univ_iff.2 (λ x, _), rcases Dcov x with ⟨n, i, h⟩, exact ⟨⟨n, i⟩, h⟩ }, { /- Let us prove that the covering `D n i` is locally finite. Take a point `x` and choose `n`, `i` so that `x ∈ D n i`. Since `D n i` is an open set, we can choose `k` so that `B = ball x (1 / 2 ^ (n + k + 1)) ⊆ D n i`. -/ intro x, rcases Dcov x with ⟨n, i, hn⟩, have : D n i ∈ 𝓝 x, from is_open.mem_nhds (Dopen _ _) hn, rcases (nhds_basis_uniformity uniformity_basis_edist_inv_two_pow).mem_iff.1 this with ⟨k, -, hsub : ball x (2⁻¹ ^ k) ⊆ D n i⟩, set B := ball x (2⁻¹ ^ (n + k + 1)), refine ⟨B, ball_mem_nhds _ (pow_pos _), _⟩, -- The sets `D m i`, `m > n + k`, are disjoint with `B` have Hgt : ∀ (m ≥ n + k + 1) (i : ι), disjoint (D m i) B, { rintros m hm i y ⟨hym, hyx⟩, rcases memD.1 hym with ⟨z, rfl, hzi, H, hz⟩, have : z ∉ ball x (2⁻¹ ^ k), from λ hz, H n (by linarith) i (hsub hz), apply this, calc edist z x ≤ edist y z + edist y x : edist_triangle_left _ _ _ ... < (2⁻¹ ^ m) + (2⁻¹ ^ (n + k + 1)) : ennreal.add_lt_add hz hyx ... ≤ (2⁻¹ ^ (k + 1)) + (2⁻¹ ^ (k + 1)) : add_le_add (hpow_le $ by linarith) (hpow_le $ by linarith) ... = (2⁻¹ ^ k) : by rw [← two_mul, h2pow] }, -- For each `m ≤ n + k` there is at most one `j` such that `D m j ∩ B` is nonempty. have Hle : ∀ m ≤ n + k, set.subsingleton {j | (D m j ∩ B).nonempty}, { rintros m hm j₁ ⟨y, hyD, hyB⟩ j₂ ⟨z, hzD, hzB⟩, by_contra h, wlog h : j₁ < j₂ := ne.lt_or_lt h using [j₁ j₂ y z, j₂ j₁ z y], rcases memD.1 hyD with ⟨y', rfl, hsuby, -, hdisty⟩, rcases memD.1 hzD with ⟨z', rfl, -, -, hdistz⟩, suffices : edist z' y' < 3 * 2⁻¹ ^ m, from nmem_of_lt_ind h (hsuby this), calc edist z' y' ≤ edist z' x + edist x y' : edist_triangle _ _ _ ... ≤ (edist z z' + edist z x) + (edist y x + edist y y') : add_le_add (edist_triangle_left _ _ _) (edist_triangle_left _ _ _) ... < (2⁻¹ ^ m + 2⁻¹ ^ (n + k + 1)) + (2⁻¹ ^ (n + k + 1) + 2⁻¹ ^ m) : by apply_rules [ennreal.add_lt_add] ... = 2 * (2⁻¹ ^ m + 2⁻¹ ^ (n + k + 1)) : by simp only [two_mul, add_comm] ... ≤ 2 * (2⁻¹ ^ m + 2⁻¹ ^ (m + 1)) : ennreal.mul_le_mul le_rfl $ add_le_add le_rfl $ hpow_le (add_le_add hm le_rfl) ... = 3 * 2⁻¹ ^ m : by rw [mul_add, h2pow, bit1, add_mul, one_mul] }, -- Finally, we glue `Hgt` and `Hle` have : (⋃ (m ≤ n + k) (i ∈ {i : ι | (D m i ∩ B).nonempty}), {(m, i)}).finite, from (finite_le_nat _).bUnion (λ i hi, (Hle i hi).finite.bUnion (λ _ _, finite_singleton _)), refine this.subset (λ I hI, _), simp only [mem_Union], refine ⟨I.1, _, I.2, hI, prod.mk.eta.symm⟩, exact not_lt.1 (λ hlt, Hgt I.1 hlt I.2 hI.some_spec) } end @[priority 100] -- see Note [lower instance priority] instance normal_of_emetric [emetric_space α] : normal_space α := normal_of_paracompact_t2 end emetric
6b2bd43b7cdfab212411cefc7652f0417eb32d4f
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/Vec.lean
e628e71e55c4342245fe19c4d5e2ca792d424ae3
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
2,477
lean
universe u open nat inductive Vec (α : Type u) : ℕ → Type u | nil {} : Vec 0 | cons {n : ℕ} (x : α) (xs : Vec n) : Vec (succ n) inductive eq' (α : Sort u) (a : α) : α → Prop | refl : eq' a --#check @eq.rec_on --#check @eq.rec lemma L1 : ∀ (α : Type u) (a b : α) (p : α → Prop), a = b → p a → p b := λ α a b p E H, eq.rec_on E H --#check L1 definition subst {α : Type u} {a b : α} (p : α → Prop)(E : a = b) (H:p a) : p b := eq.rec_on E H --#check @subst --#check@eq.refl definition P {α : Type u} (a : α) (x : α) : Prop := x = a definition symm2 {α : Type u} {a b : α} (E:a = b) : b = a := @subst α a b (λ (x:α), x = a) E (eq.refl a) definition trans2 {α : Type u} (a b c : α) (Eab : a = b) (Ebc : b = c) : a = c := @subst α b c (λ x, a = x) Ebc Eab definition congr2 {α β : Type u} {a b : α} (f : α → β) (E : a = b) : f a = f b := @subst α a b (λ x, f a = f x) E (eq.refl (f a)) open Vec local notation x :: xs := cons x xs --#check @Vec.cases_on def tail_aux {α : Type u} {n m : ℕ} (v : Vec α m) : m = n + 1 → Vec α n := Vec.cases_on v (begin assume H, cases H end) (begin assume m x w H, cases H, exact w end) def tail1 {α : Type u} {n : ℕ} (v : Vec α (n+1)) : Vec α n := tail_aux v rfl --#check @nat.no_confusion def head {α : Type u} : ∀ {n : ℕ}, Vec α (n + 1) → α | _ (x :: xs) := x def tail {α : Type u} : ∀ {n : ℕ}, Vec α (n + 1) → Vec α n | _ (x :: xs) := xs lemma eta : ∀ {α : Type u} {n : ℕ} (vs : Vec α (n + 1)), head vs :: tail vs = vs := begin intros α n vs, cases vs with v vs, reflexivity end def map2 {α β γ : Type u} (f : α → β → γ) : ∀ {n : ℕ}, Vec α n → Vec β n → Vec γ n | 0 nil nil := nil | (n + 1) (a :: as) (b :: bs) := f a b :: map2 as bs def zip {α β : Type u} : ∀ {n : ℕ}, Vec α n → Vec β n → Vec (α × β) n | 0 nil nil := nil | (n + 1) (a :: as) (b :: bs) := (a, b) :: zip as bs --#print map2 --#print map2._main -- scary stuff def map {α β : Type u} (f : α → β) : ∀ {n : ℕ}, Vec α n → Vec β n | 0 nil := nil | (n + 1) (x :: xs) := f x :: map xs --#print map --#print map._main --#check @map._main /- How do we do this? def map1 : ∀ {α β : Type u}, (α → β) → ∀ {n : ℕ}, Vec α n → Vec β n := λ (α β:Type u) (f:α → β), nat.rec (λ (_:Vec α 0), _) _ -/ --#check @nat.rec --#check @nat.rec_on --#check @nat.brec_on
e2f6a661883158e8a9b84f45bec15bbc9b8f84c4
74a02dffce22907d2b61b8e226f1d9aa8384c7c0
/Cli/Tests.lean
b253a4c879efe42875ec0a55cb142d3471384cc3
[ "MIT" ]
permissive
mhuisi/lean4-cli-docker-test
7e0566c7397746701162e2e22fbad38afcf1e9c4
6b3881eaa22596f6f430654f61fc03719ee18c62
refs/heads/main
1,680,104,973,886
1,616,975,353,000
1,616,975,353,000
352,411,347
0
0
null
null
null
null
UTF-8
Lean
false
false
15,721
lean
import AssertCmd import Cli.Basic import Cli.Extensions namespace Cli section Utils instance [BEq α] [BEq β] : BEq (Except α β) where beq | Except.ok a, Except.ok a' => a == a' | Except.error b, Except.error b' => b == b' | _, _ => false instance [Repr α] [Repr β] : Repr (Except α β) where reprPrec | Except.ok a, n => s!"Except.ok ({repr a})" | Except.error b, n => s!"Except.error ({repr b})" def Cmd.processParsed (c : Cmd) (args : String) : String := do let mut args := args.splitOn if args = [""] then args := [] match c.process args with | Except.ok (cmd, parsed) => return toString parsed | Except.error (cmd, error) => return error end Utils def doNothing (p : Parsed) : IO UInt32 := return 0 def testSubSubCmd : Cmd := `[Cli| testsubsubcommand VIA doNothing; ["0.0.2"] "does this even do anything?" ] def testSubCmd1 : Cmd := `[Cli| testsubcommand1 VIA doNothing; ["0.0.1"] "a properly short description" FLAGS: "launch-the-nukes"; "please avoid passing this flag at all costs.\nif you like, you can have newlines in descriptions." ARGS: "city-location" : String; "can also use hyphens" SUBCOMMANDS: testSubSubCmd ] def testSubCmd2 : Cmd := `[Cli| testsubcommand2 VIA doNothing; ["0.0.-1"] "does not do anything interesting" FLAGS: r, run; "really, this does not do anything. trust me." ARGS: "ominous-input" : Array String; "what could this be for?" ] def testCmd : Cmd := `[Cli| testcommand VIA doNothing; ["0.0.0"] "some short description that happens to be much longer than necessary and hence needs to be wrapped to fit into an 80 character width limit" FLAGS: verbose; "a very verbose flag description that also needs to be wrapped to fit into an 80 character width limit" x, unknown1; "this flag has a short name" xn, unknown2; "short names do not need to be prefix-free" ny, unknown3; "-xny will parse as -x -ny and not fail to parse as -xn -y" t, typed1 : String; "flags can have typed parameters" ty, typed2; "-ty parsed as --typed2, not -t=y" "p-n", "level-param" : Nat; "hyphens work, too" ARGS: input1 : String; "another very verbose description that also needs to be wrapped to fit into an 80 character width limit" input2 : Array Nat; "arrays!" ...outputs : Nat; "varargs!" SUBCOMMANDS: testSubCmd1; testSubCmd2 EXTENSIONS: author "mhuisi"; longDescription "this could be really long, but i'm too lazy to type it out."; defaultValues! #[⟨"level-param", "0"⟩]; require! #["typed1"] ] section ValidInputs #assert (testCmd.processParsed "foo 1 -ta") == "cmd: testcommand; flags: #[--typed1=a, --level-param=0]; positionalArgs: #[<input1=foo>, <input2=1>]; variableArgs: #[]" #assert (testCmd.processParsed "-h") == "cmd: testcommand; flags: #[--help, --level-param=0]; positionalArgs: #[]; variableArgs: #[]" #assert (testCmd.processParsed "--version") == "cmd: testcommand; flags: #[--version, --level-param=0]; positionalArgs: #[]; variableArgs: #[]" #assert (testCmd.processParsed "foo --verbose -p-n 2 1,2,3 1 -xnx 2 --typed1=foo 3") == "cmd: testcommand; flags: #[--verbose, --level-param=2, --unknown2, --unknown1, --typed1=foo]; positionalArgs: #[<input1=foo>, <input2=1,2,3>]; variableArgs: #[<outputs=1>, <outputs=2>, <outputs=3>]" #assert (testCmd.processParsed "foo -xny 1 -t 3") == "cmd: testcommand; flags: #[--unknown1, --unknown3, --typed1=3, --level-param=0]; positionalArgs: #[<input1=foo>, <input2=1>]; variableArgs: #[]" #assert (testCmd.processParsed "-t 3 -- --input 2") == "cmd: testcommand; flags: #[--typed1=3, --level-param=0]; positionalArgs: #[<input1=--input>, <input2=2>]; variableArgs: #[]" #assert (testCmd.processParsed "-t1 - 2") == "cmd: testcommand; flags: #[--typed1=1, --level-param=0]; positionalArgs: #[<input1=->, <input2=2>]; variableArgs: #[]" #assert (testCmd.processParsed "-ty -t1 foo 1,2") == "cmd: testcommand; flags: #[--typed2, --typed1=1, --level-param=0]; positionalArgs: #[<input1=foo>, <input2=1,2>]; variableArgs: #[]" #assert (testCmd.processParsed "testsubcommand1 -- testsubsubcommand") == "cmd: testcommand testsubcommand1; flags: #[]; positionalArgs: #[<city-location=testsubsubcommand>]; variableArgs: #[]" #assert (testCmd.processParsed "testsubcommand1 --launch-the-nukes x") == "cmd: testcommand testsubcommand1; flags: #[--launch-the-nukes]; positionalArgs: #[<city-location=x>]; variableArgs: #[]" #assert (testCmd.processParsed "testsubcommand1 -- --launch-the-nukes") == "cmd: testcommand testsubcommand1; flags: #[]; positionalArgs: #[<city-location=--launch-the-nukes>]; variableArgs: #[]" #assert (testCmd.processParsed "testsubcommand1 testsubsubcommand") == "cmd: testcommand testsubcommand1 testsubsubcommand; flags: #[]; positionalArgs: #[]; variableArgs: #[]" #assert (testCmd.processParsed "testsubcommand2 --run asdf,geh") == "cmd: testcommand testsubcommand2; flags: #[--run]; positionalArgs: #[<ominous-input=asdf,geh>]; variableArgs: #[]" end ValidInputs section InvalidInputs #assert (testCmd.processParsed "") == "Missing positional argument `<input1>.`" #assert (testCmd.processParsed "foo") == "Missing positional argument `<input2>.`" #assert (testCmd.processParsed "foo asdf") == "Invalid type of argument `asdf` for positional argument `<input2 : Array Nat>`." #assert (testCmd.processParsed "foo 1,2,3") == "Missing required flag `--typed1`." #assert (testCmd.processParsed "foo 1,2,3 -t") == "Missing argument for flag `-t`." #assert (testCmd.processParsed "foo 1,2,3 -t1 --level-param=") == "Invalid type of argument `` for flag `--level-param : Nat`." #assert (testCmd.processParsed "foo 1,2,3 -t1 -p-n=") == "Invalid type of argument `` for flag `-p-n : Nat`." #assert (testCmd.processParsed "foo 1,2,3 -t1 --asdf") == "Unknown flag `--asdf`." #assert (testCmd.processParsed "foo 1,2,3 -t1 -t2") == "Duplicate flag `-t` (`--typed1`)." #assert (testCmd.processParsed "foo 1,2,3 -t1 --typed1=2") == "Duplicate flag `--typed1` (`-t`)." #assert (testCmd.processParsed "foo 1,2,3 --typed12") == "Unknown flag `--typed12`." #assert (testCmd.processParsed "foo 1,2,3 -t1 -x=1") == "Redundant argument `1` for flag `-x` that takes no arguments." #assert (testCmd.processParsed "foo 1,2,3 -t1 bar") == "Invalid type of argument `bar` for variable argument `<outputs : Nat>...`." #assert (testCmd.processParsed "foo 1,2,3 -t1 -xxn=1") == "Unknown flag `-xxn`." #assert (testCmd.processParsed "foo 1,2,3 --t=1") == "Unknown flag `--t`." #assert (testCmd.processParsed "testsubcommand1 asdf geh") == "Redundant positional argument `geh`." end InvalidInputs section Info /- testcommand [0.0.0] some short description that happens to be much longer than necessary and hence needs to be wrapped to fit into an 80 character width limit USAGE: testcommand [SUBCOMMAND] [FLAGS] <input1> <input2> <outputs>... FLAGS: -h, --help Prints this message. --version Prints the version. --verbose a very verbose flag description that also needs to be wrapped to fit into an 80 character width limit -x, --unknown1 this flag has a short name -xn, --unknown2 short names do not need to be prefix-free -ny, --unknown3 -xny will parse as -x -ny and not fail to parse as -xn -y -t, --typed1 : String flags can have typed parameters -ty, --typed2 -ty parsed as --typed2, not -t=y -p-n, --level-param : Nat hyphens work, too ARGS: input1 : String another very verbose description that also needs to be wrapped to fit into an 80 character width limit input2 : Array Nat arrays! outputs : Nat varargs! SUBCOMMANDS: testsubcommand1 a properly short description testsubcommand2 does not do anything interesting -/ #assert testCmd.help == "testcommand [0.0.0]\nsome short description that happens to be much longer than necessary and hence\nneeds to be wrapped to fit into an 80 character width limit\n\nUSAGE:\n testcommand [SUBCOMMAND] [FLAGS] <input1> <input2> <outputs>...\n\nFLAGS:\n -h, --help Prints this message.\n --version Prints the version.\n --verbose a very verbose flag description that also needs\n to be wrapped to fit into an 80 character width\n limit\n -x, --unknown1 this flag has a short name\n -xn, --unknown2 short names do not need to be prefix-free\n -ny, --unknown3 -xny will parse as -x -ny and not fail to parse\n as -xn -y\n -t, --typed1 : String flags can have typed parameters\n -ty, --typed2 -ty parsed as --typed2, not -t=y\n -p-n, --level-param : Nat hyphens work, too\n\nARGS:\n input1 : String another very verbose description that also needs to be\n wrapped to fit into an 80 character width limit\n input2 : Array Nat arrays!\n outputs : Nat varargs!\n\nSUBCOMMANDS:\n testsubcommand1 a properly short description\n testsubcommand2 does not do anything interesting" #assert testCmd.version == "0.0.0" /- some exceedingly long error that needs to be wrapped to fit within an 80 character width limit. none of our errors are really that long, but flag names might be. Run `testcommand -h` for further information. -/ #assert (testCmd.error "some exceedingly long error that needs to be wrapped to fit within an 80 character width limit. none of our errors are really that long, but flag names might be.") == "some exceedingly long error that needs to be wrapped to fit within an 80\ncharacter width limit. none of our errors are really that long, but flag names\nmight be.\nRun `testcommand -h` for further information." /- testsubcommand2 [0.0.-1] does not do anything interesting USAGE: testsubcommand2 [FLAGS] <ominous-input> FLAGS: -h, --help Prints this message. --version Prints the version. -r, --run really, this does not do anything. trust me. ARGS: ominous-input : Array String what could this be for? -/ #assert testSubCmd2.help == "testsubcommand2 [0.0.-1]\ndoes not do anything interesting\n\nUSAGE:\n testsubcommand2 [FLAGS] <ominous-input>\n\nFLAGS:\n -h, --help Prints this message.\n --version Prints the version.\n -r, --run really, this does not do anything. trust me.\n\nARGS:\n ominous-input : Array String what could this be for?" /- testcommand testsubcommand2 [0.0.-1] does not do anything interesting USAGE: testcommand testsubcommand2 [FLAGS] <ominous-input> FLAGS: -h, --help Prints this message. --version Prints the version. -r, --run really, this does not do anything. trust me. ARGS: ominous-input : Array String what could this be for? -/ #assert (testCmd.subCmd! "testsubcommand2").help == "testcommand testsubcommand2 [0.0.-1]\ndoes not do anything interesting\n\nUSAGE:\n testcommand testsubcommand2 [FLAGS] <ominous-input>\n\nFLAGS:\n -h, --help Prints this message.\n --version Prints the version.\n -r, --run really, this does not do anything. trust me.\n\nARGS:\n ominous-input : Array String what could this be for?" /- testcommand testsubcommand1 testsubsubcommand [0.0.2] does this even do anything? USAGE: testcommand testsubcommand1 testsubsubcommand [FLAGS] FLAGS: -h, --help Prints this message. --version Prints the version. -/ #assert (testCmd.subCmd! "testsubcommand1" |>.subCmd! "testsubsubcommand").help == "testcommand testsubcommand1 testsubsubcommand [0.0.2]\ndoes this even do anything?\n\nUSAGE:\n testcommand testsubcommand1 testsubsubcommand [FLAGS]\n\nFLAGS:\n -h, --help Prints this message.\n --version Prints the version." /- testcommand [0.0.0] mhuisi some short description that happens to be much longer than necessary and hence needs to be wrapped to fit into an 80 character width limit USAGE: testcommand [SUBCOMMAND] [FLAGS] <input1> <input2> <outputs>... FLAGS: -h, --help Prints this message. --version Prints the version. --verbose a very verbose flag description that also needs to be wrapped to fit into an 80 character width limit -x, --unknown1 this flag has a short name -xn, --unknown2 short names do not need to be prefix-free -ny, --unknown3 -xny will parse as -x -ny and not fail to parse as -xn -y -t, --typed1 : String [Required] flags can have typed parameters -ty, --typed2 -ty parsed as --typed2, not -t=y -p-n, --level-param : Nat hyphens work, too [Default: `0`] ARGS: input1 : String another very verbose description that also needs to be wrapped to fit into an 80 character width limit input2 : Array Nat arrays! outputs : Nat varargs! SUBCOMMANDS: testsubcommand1 a properly short description testsubcommand2 does not do anything interesting DESCRIPTION: this could be really long, but i'm too lazy to type it out. -/ #assert (testCmd.update' (meta := testCmd.extension!.extendMeta testCmd.meta)).help == "testcommand [0.0.0]\nmhuisi\nsome short description that happens to be much longer than necessary and hence\nneeds to be wrapped to fit into an 80 character width limit\n\nUSAGE:\n testcommand [SUBCOMMAND] [FLAGS] <input1> <input2> <outputs>...\n\nFLAGS:\n -h, --help Prints this message.\n --version Prints the version.\n --verbose a very verbose flag description that also needs\n to be wrapped to fit into an 80 character width\n limit\n -x, --unknown1 this flag has a short name\n -xn, --unknown2 short names do not need to be prefix-free\n -ny, --unknown3 -xny will parse as -x -ny and not fail to parse\n as -xn -y\n -t, --typed1 : String [Required] flags can have typed parameters\n -ty, --typed2 -ty parsed as --typed2, not -t=y\n -p-n, --level-param : Nat hyphens work, too [Default: `0`]\n\nARGS:\n input1 : String another very verbose description that also needs to be\n wrapped to fit into an 80 character width limit\n input2 : Array Nat arrays!\n outputs : Nat varargs!\n\nSUBCOMMANDS:\n testsubcommand1 a properly short description\n testsubcommand2 does not do anything interesting\n\nDESCRIPTION:\n this could be really long, but i'm too lazy to type it out." end Info end Cli
669219af571f46c6d821892c5ca04ecb9265881e
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/option/defs.lean
a05ca5b7ef699f64f145b1376ff165c6f8fd8a79
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
3,400
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Extra definitions on option. -/ namespace option variables {α : Type*} {β : Type*} instance has_mem : has_mem α (option α) := ⟨λ a b, b = some a⟩ @[simp] theorem mem_def {a : α} {b : option α} : a ∈ b ↔ b = some a := iff.rfl theorem is_none_iff_eq_none {o : option α} : o.is_none = tt ↔ o = none := ⟨option.eq_none_of_is_none, λ e, e.symm ▸ rfl⟩ theorem some_inj {a b : α} : some a = some b ↔ a = b := by simp instance decidable_eq_none {o : option α} : decidable (o = none) := decidable_of_decidable_of_iff (bool.decidable_eq _ _) is_none_iff_eq_none instance decidable_forall_mem {p : α → Prop} [decidable_pred p] : ∀ o : option α, decidable (∀ a ∈ o, p a) | none := is_true (by simp) | (some a) := if h : p a then is_true $ λ o e, some_inj.1 e ▸ h else is_false $ mt (λ H, H _ rfl) h instance decidable_exists_mem {p : α → Prop} [decidable_pred p] : ∀ o : option α, decidable (∃ a ∈ o, p a) | none := is_false (λ ⟨a, ⟨h, _⟩⟩, by cases h) | (some a) := if h : p a then is_true $ ⟨_, rfl, h⟩ else is_false $ λ ⟨_, ⟨rfl, hn⟩⟩, h hn /-- inhabited `get` function. Returns `a` if the input is `some a`, otherwise returns `default`. -/ @[reducible] def iget [inhabited α] : option α → α | (some x) := x | none := default α @[simp] theorem iget_some [inhabited α] {a : α} : (some a).iget = a := rfl /-- `guard p a` returns `some a` if `p a` holds, otherwise `none`. -/ def guard (p : α → Prop) [decidable_pred p] (a : α) : option α := if p a then some a else none /-- `filter p o` returns `some a` if `o` is `some a` and `p a` holds, otherwise `none`. -/ def filter (p : α → Prop) [decidable_pred p] (o : option α) : option α := o.bind (guard p) def to_list : option α → list α | none := [] | (some a) := [a] @[simp] theorem mem_to_list {a : α} {o : option α} : a ∈ to_list o ↔ a ∈ o := by cases o; simp [to_list, eq_comm] def lift_or_get (f : α → α → α) : option α → option α → option α | none none := none | (some a) none := some a -- get a | none (some b) := some b -- get b | (some a) (some b) := some (f a b) -- lift f instance lift_or_get_comm (f : α → α → α) [h : is_commutative α f] : is_commutative (option α) (lift_or_get f) := ⟨λ a b, by cases a; cases b; simp [lift_or_get, h.comm]⟩ instance lift_or_get_assoc (f : α → α → α) [h : is_associative α f] : is_associative (option α) (lift_or_get f) := ⟨λ a b c, by cases a; cases b; cases c; simp [lift_or_get, h.assoc]⟩ instance lift_or_get_idem (f : α → α → α) [h : is_idempotent α f] : is_idempotent (option α) (lift_or_get f) := ⟨λ a, by cases a; simp [lift_or_get, h.idempotent]⟩ instance lift_or_get_is_left_id (f : α → α → α) : is_left_id (option α) (lift_or_get f) none := ⟨λ a, by cases a; simp [lift_or_get]⟩ instance lift_or_get_is_right_id (f : α → α → α) : is_right_id (option α) (lift_or_get f) none := ⟨λ a, by cases a; simp [lift_or_get]⟩ inductive rel (r : α → β → Prop) : option α → option β → Prop | some {a b} : r a b → rel (some a) (some b) | none {} : rel none none end option
407f0d77975c28e9b9d854ea7761b528dabb96d6
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/real/ennreal.lean
3d0809ea6634b2c41b71d134ae6f060ddc805529
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
80,512
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import data.real.nnreal /-! # Extended non-negative reals We define `ennreal = ℝ≥0∞ := with_top ℝ≥0` to be the type of extended nonnegative real numbers, i.e., the interval `[0, +∞]`. This type is used as the codomain of a `measure_theory.measure`, and of the extended distance `edist` in a `emetric_space`. In this file we define some algebraic operations and a linear order on `ℝ≥0∞` and prove basic properties of these operations, order, and conversions to/from `ℝ`, `ℝ≥0`, and `ℕ`. ## Main definitions * `ℝ≥0∞`: the extended nonnegative real numbers `[0, ∞]`; defined as `with_top ℝ≥0`; it is equipped with the following structures: - coercion from `ℝ≥0` defined in the natural way; - the natural structure of a complete dense linear order: `↑p ≤ ↑q ↔ p ≤ q` and `∀ a, a ≤ ∞`; - `a + b` is defined so that `↑p + ↑q = ↑(p + q)` for `(p q : ℝ≥0)` and `a + ∞ = ∞ + a = ∞`; - `a * b` is defined so that `↑p * ↑q = ↑(p * q)` for `(p q : ℝ≥0)`, `0 * ∞ = ∞ * 0 = 0`, and `a * ∞ = ∞ * a = ∞` for `a ≠ 0`; - `a - b` is defined as the minimal `d` such that `a ≤ d + b`; this way we have `↑p - ↑q = ↑(p - q)`, `∞ - ↑p = ∞`, `↑p - ∞ = ∞ - ∞ = 0`; note that there is no negation, only subtraction; - `a⁻¹` is defined as `Inf {b | 1 ≤ a * b}`. This way we have `(↑p)⁻¹ = ↑(p⁻¹)` for `p : ℝ≥0`, `p ≠ 0`, `0⁻¹ = ∞`, and `∞⁻¹ = 0`. - `a / b` is defined as `a * b⁻¹`. The addition and multiplication defined this way together with `0 = ↑0` and `1 = ↑1` turn `ℝ≥0∞` into a canonically ordered commutative semiring of characteristic zero. * Coercions to/from other types: - coercion `ℝ≥0 → ℝ≥0∞` is defined as `has_coe`, so one can use `(p : ℝ≥0)` in a context that expects `a : ℝ≥0∞`, and Lean will apply `coe` automatically; - `ennreal.to_nnreal` sends `↑p` to `p` and `∞` to `0`; - `ennreal.to_real := coe ∘ ennreal.to_nnreal` sends `↑p`, `p : ℝ≥0` to `(↑p : ℝ)` and `∞` to `0`; - `ennreal.of_real := coe ∘ real.to_nnreal` sends `x : ℝ` to `↑⟨max x 0, _⟩` - `ennreal.ne_top_equiv_nnreal` is an equivalence between `{a : ℝ≥0∞ // a ≠ 0}` and `ℝ≥0`. ## Implementation notes We define a `can_lift ℝ≥0∞ ℝ≥0` instance, so one of the ways to prove theorems about an `ℝ≥0∞` number `a` is to consider the cases `a = ∞` and `a ≠ ∞`, and use the tactic `lift a to ℝ≥0 using ha` in the second case. This instance is even more useful if one already has `ha : a ≠ ∞` in the context, or if we have `(f : α → ℝ≥0∞) (hf : ∀ x, f x ≠ ∞)`. ## Notations * `ℝ≥0∞`: the type of the extended nonnegative real numbers; * `ℝ≥0`: the type of nonnegative real numbers `[0, ∞)`; defined in `data.real.nnreal`; * `∞`: a localized notation in `ℝ≥0∞` for `⊤ : ℝ≥0∞`. -/ open classical set open_locale classical big_operators nnreal variables {α : Type*} {β : Type*} /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ @[derive [ has_zero, add_comm_monoid_with_one, canonically_ordered_comm_semiring, complete_linear_order, densely_ordered, nontrivial, canonically_linear_ordered_add_monoid, has_sub, has_ordered_sub, linear_ordered_add_comm_monoid_with_top]] def ennreal := with_top ℝ≥0 localized "notation `ℝ≥0∞` := ennreal" in ennreal localized "notation `∞` := (⊤ : ennreal)" in ennreal namespace ennreal variables {a b c d : ℝ≥0∞} {r p q : ℝ≥0} -- TODO: why are the two covariant instances necessary? why aren't they inferred? instance covariant_class_mul_le : covariant_class ℝ≥0∞ ℝ≥0∞ (*) (≤) := canonically_ordered_comm_semiring.to_covariant_mul_le instance covariant_class_add_le : covariant_class ℝ≥0∞ ℝ≥0∞ (+) (≤) := ordered_add_comm_monoid.to_covariant_class_left ℝ≥0∞ noncomputable instance : linear_ordered_comm_monoid_with_zero ℝ≥0∞ := { mul_le_mul_left := λ a b, mul_le_mul_left', zero_le_one := zero_le 1, .. ennreal.linear_ordered_add_comm_monoid_with_top, .. (show comm_semiring ℝ≥0∞, from infer_instance) } instance : inhabited ℝ≥0∞ := ⟨0⟩ instance : has_coe ℝ≥0 ℝ≥0∞ := ⟨ option.some ⟩ instance : can_lift ℝ≥0∞ ℝ≥0 := { coe := coe, cond := λ r, r ≠ ∞, prf := λ x hx, ⟨option.get $ option.ne_none_iff_is_some.1 hx, option.some_get _⟩ } @[simp] lemma none_eq_top : (none : ℝ≥0∞) = ∞ := rfl @[simp] lemma some_eq_coe (a : ℝ≥0) : (some a : ℝ≥0∞) = (↑a : ℝ≥0∞) := rfl /-- `to_nnreal x` returns `x` if it is real, otherwise 0. -/ protected def to_nnreal : ℝ≥0∞ → ℝ≥0 := with_top.untop' 0 /-- `to_real x` returns `x` if it is real, `0` otherwise. -/ protected def to_real (a : ℝ≥0∞) : real := coe (a.to_nnreal) /-- `of_real x` returns `x` if it is nonnegative, `0` otherwise. -/ protected noncomputable def of_real (r : real) : ℝ≥0∞ := coe (real.to_nnreal r) @[simp, norm_cast] lemma to_nnreal_coe : (r : ℝ≥0∞).to_nnreal = r := rfl @[simp] lemma coe_to_nnreal : ∀{a:ℝ≥0∞}, a ≠ ∞ → ↑(a.to_nnreal) = a | (some r) h := rfl | none h := (h rfl).elim @[simp] lemma of_real_to_real {a : ℝ≥0∞} (h : a ≠ ∞) : ennreal.of_real (a.to_real) = a := by simp [ennreal.to_real, ennreal.of_real, h] @[simp] lemma to_real_of_real {r : ℝ} (h : 0 ≤ r) : ennreal.to_real (ennreal.of_real r) = r := by simp [ennreal.to_real, ennreal.of_real, real.coe_to_nnreal _ h] lemma to_real_of_real' {r : ℝ} : ennreal.to_real (ennreal.of_real r) = max r 0 := rfl lemma coe_to_nnreal_le_self : ∀{a:ℝ≥0∞}, ↑(a.to_nnreal) ≤ a | (some r) := by rw [some_eq_coe, to_nnreal_coe]; exact le_rfl | none := le_top lemma coe_nnreal_eq (r : ℝ≥0) : (r : ℝ≥0∞) = ennreal.of_real r := by { rw [ennreal.of_real, real.to_nnreal], cases r with r h, congr, dsimp, rw max_eq_left h } lemma of_real_eq_coe_nnreal {x : ℝ} (h : 0 ≤ x) : ennreal.of_real x = @coe ℝ≥0 ℝ≥0∞ _ (⟨x, h⟩ : ℝ≥0) := by { rw [coe_nnreal_eq], refl } @[simp] lemma of_real_coe_nnreal : ennreal.of_real p = p := (coe_nnreal_eq p).symm @[simp, norm_cast] lemma coe_zero : ↑(0 : ℝ≥0) = (0 : ℝ≥0∞) := rfl @[simp, norm_cast] lemma coe_one : ↑(1 : ℝ≥0) = (1 : ℝ≥0∞) := rfl @[simp] lemma to_real_nonneg {a : ℝ≥0∞} : 0 ≤ a.to_real := by simp [ennreal.to_real] @[simp] lemma top_to_nnreal : ∞.to_nnreal = 0 := rfl @[simp] lemma top_to_real : ∞.to_real = 0 := rfl @[simp] lemma one_to_real : (1 : ℝ≥0∞).to_real = 1 := rfl @[simp] lemma one_to_nnreal : (1 : ℝ≥0∞).to_nnreal = 1 := rfl @[simp] lemma coe_to_real (r : ℝ≥0) : (r : ℝ≥0∞).to_real = r := rfl @[simp] lemma zero_to_nnreal : (0 : ℝ≥0∞).to_nnreal = 0 := rfl @[simp] lemma zero_to_real : (0 : ℝ≥0∞).to_real = 0 := rfl @[simp] lemma of_real_zero : ennreal.of_real (0 : ℝ) = 0 := by simp [ennreal.of_real]; refl @[simp] lemma of_real_one : ennreal.of_real (1 : ℝ) = (1 : ℝ≥0∞) := by simp [ennreal.of_real] lemma of_real_to_real_le {a : ℝ≥0∞} : ennreal.of_real (a.to_real) ≤ a := if ha : a = ∞ then ha.symm ▸ le_top else le_of_eq (of_real_to_real ha) lemma forall_ennreal {p : ℝ≥0∞ → Prop} : (∀a, p a) ↔ (∀r:ℝ≥0, p r) ∧ p ∞ := ⟨assume h, ⟨assume r, h _, h _⟩, assume ⟨h₁, h₂⟩ a, match a with some r := h₁ _ | none := h₂ end⟩ lemma forall_ne_top {p : ℝ≥0∞ → Prop} : (∀ a ≠ ∞, p a) ↔ ∀ r : ℝ≥0, p r := option.ball_ne_none lemma exists_ne_top {p : ℝ≥0∞ → Prop} : (∃ a ≠ ∞, p a) ↔ ∃ r : ℝ≥0, p r := option.bex_ne_none lemma to_nnreal_eq_zero_iff (x : ℝ≥0∞) : x.to_nnreal = 0 ↔ x = 0 ∨ x = ∞ := ⟨begin cases x, { simp [none_eq_top] }, { rintro (rfl : x = 0), exact or.inl rfl }, end, by rintro (h | h); simp [h]⟩ lemma to_real_eq_zero_iff (x : ℝ≥0∞) : x.to_real = 0 ↔ x = 0 ∨ x = ∞ := by simp [ennreal.to_real, to_nnreal_eq_zero_iff] @[simp] lemma coe_ne_top : (r : ℝ≥0∞) ≠ ∞ := with_top.coe_ne_top @[simp] lemma top_ne_coe : ∞ ≠ (r : ℝ≥0∞) := with_top.top_ne_coe @[simp] lemma of_real_ne_top {r : ℝ} : ennreal.of_real r ≠ ∞ := by simp [ennreal.of_real] @[simp] lemma of_real_lt_top {r : ℝ} : ennreal.of_real r < ∞ := lt_top_iff_ne_top.2 of_real_ne_top @[simp] lemma top_ne_of_real {r : ℝ} : ∞ ≠ ennreal.of_real r := by simp [ennreal.of_real] @[simp] lemma zero_ne_top : 0 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_zero : ∞ ≠ 0 := top_ne_coe @[simp] lemma one_ne_top : 1 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_one : ∞ ≠ 1 := top_ne_coe @[simp, norm_cast] lemma coe_eq_coe : (↑r : ℝ≥0∞) = ↑q ↔ r = q := with_top.coe_eq_coe @[simp, norm_cast] lemma coe_le_coe : (↑r : ℝ≥0∞) ≤ ↑q ↔ r ≤ q := with_top.coe_le_coe @[simp, norm_cast] lemma coe_lt_coe : (↑r : ℝ≥0∞) < ↑q ↔ r < q := with_top.coe_lt_coe lemma coe_mono : monotone (coe : ℝ≥0 → ℝ≥0∞) := λ _ _, coe_le_coe.2 @[simp, norm_cast] lemma coe_eq_zero : (↑r : ℝ≥0∞) = 0 ↔ r = 0 := coe_eq_coe @[simp, norm_cast] lemma zero_eq_coe : 0 = (↑r : ℝ≥0∞) ↔ 0 = r := coe_eq_coe @[simp, norm_cast] lemma coe_eq_one : (↑r : ℝ≥0∞) = 1 ↔ r = 1 := coe_eq_coe @[simp, norm_cast] lemma one_eq_coe : 1 = (↑r : ℝ≥0∞) ↔ 1 = r := coe_eq_coe @[simp, norm_cast] lemma coe_nonneg : 0 ≤ (↑r : ℝ≥0∞) ↔ 0 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_pos : 0 < (↑r : ℝ≥0∞) ↔ 0 < r := coe_lt_coe lemma coe_ne_zero : (r : ℝ≥0∞) ≠ 0 ↔ r ≠ 0 := not_congr coe_eq_coe @[simp, norm_cast] lemma coe_add : ↑(r + p) = (r + p : ℝ≥0∞) := with_top.coe_add @[simp, norm_cast] lemma coe_mul : ↑(r * p) = (r * p : ℝ≥0∞) := with_top.coe_mul @[simp, norm_cast] lemma coe_bit0 : (↑(bit0 r) : ℝ≥0∞) = bit0 r := coe_add @[simp, norm_cast] lemma coe_bit1 : (↑(bit1 r) : ℝ≥0∞) = bit1 r := by simp [bit1] lemma coe_two : ((2:ℝ≥0) : ℝ≥0∞) = 2 := by norm_cast protected lemma zero_lt_one : 0 < (1 : ℝ≥0∞) := canonically_ordered_comm_semiring.zero_lt_one @[simp] lemma one_lt_two : (1 : ℝ≥0∞) < 2 := coe_one ▸ coe_two ▸ by exact_mod_cast (@one_lt_two ℕ _ _) @[simp] lemma zero_lt_two : (0:ℝ≥0∞) < 2 := lt_trans ennreal.zero_lt_one one_lt_two lemma two_ne_zero : (2:ℝ≥0∞) ≠ 0 := (ne_of_lt zero_lt_two).symm lemma two_ne_top : (2:ℝ≥0∞) ≠ ∞ := coe_two ▸ coe_ne_top /-- `(1 : ℝ≥0∞) ≤ 1`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_one_ennreal : fact ((1 : ℝ≥0∞) ≤ 1) := ⟨le_rfl⟩ /-- `(1 : ℝ≥0∞) ≤ 2`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_two_ennreal : fact ((1 : ℝ≥0∞) ≤ 2) := ⟨one_le_two⟩ /-- `(1 : ℝ≥0∞) ≤ ∞`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_top_ennreal : fact ((1 : ℝ≥0∞) ≤ ∞) := ⟨le_top⟩ /-- The set of numbers in `ℝ≥0∞` that are not equal to `∞` is equivalent to `ℝ≥0`. -/ def ne_top_equiv_nnreal : {a | a ≠ ∞} ≃ ℝ≥0 := { to_fun := λ x, ennreal.to_nnreal x, inv_fun := λ x, ⟨x, coe_ne_top⟩, left_inv := λ ⟨x, hx⟩, subtype.eq $ coe_to_nnreal hx, right_inv := λ x, to_nnreal_coe } lemma cinfi_ne_top [has_Inf α] (f : ℝ≥0∞ → α) : (⨅ x : {x // x ≠ ∞}, f x) = ⨅ x : ℝ≥0, f x := eq.symm $ ne_top_equiv_nnreal.symm.surjective.infi_congr _$ λ x, rfl lemma infi_ne_top [complete_lattice α] (f : ℝ≥0∞ → α) : (⨅ x ≠ ∞, f x) = ⨅ x : ℝ≥0, f x := by rw [infi_subtype', cinfi_ne_top] lemma csupr_ne_top [has_Sup α] (f : ℝ≥0∞ → α) : (⨆ x : {x // x ≠ ∞}, f x) = ⨆ x : ℝ≥0, f x := @cinfi_ne_top αᵒᵈ _ _ lemma supr_ne_top [complete_lattice α] (f : ℝ≥0∞ → α) : (⨆ x ≠ ∞, f x) = ⨆ x : ℝ≥0, f x := @infi_ne_top αᵒᵈ _ _ lemma infi_ennreal {α : Type*} [complete_lattice α] {f : ℝ≥0∞ → α} : (⨅ n, f n) = (⨅ n : ℝ≥0, f n) ⊓ f ∞ := le_antisymm (le_inf (le_infi $ assume i, infi_le _ _) (infi_le _ _)) (le_infi $ forall_ennreal.2 ⟨λ r, inf_le_of_left_le $ infi_le _ _, inf_le_right⟩) lemma supr_ennreal {α : Type*} [complete_lattice α] {f : ℝ≥0∞ → α} : (⨆ n, f n) = (⨆ n : ℝ≥0, f n) ⊔ f ∞ := @infi_ennreal αᵒᵈ _ _ @[simp] lemma add_top : a + ∞ = ∞ := add_top _ @[simp] lemma top_add : ∞ + a = ∞ := top_add _ /-- Coercion `ℝ≥0 → ℝ≥0∞` as a `ring_hom`. -/ def of_nnreal_hom : ℝ≥0 →+* ℝ≥0∞ := ⟨coe, coe_one, λ _ _, coe_mul, coe_zero, λ _ _, coe_add⟩ @[simp] lemma coe_of_nnreal_hom : ⇑of_nnreal_hom = coe := rfl section actions /-- A `mul_action` over `ℝ≥0∞` restricts to a `mul_action` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [mul_action ℝ≥0∞ M] : mul_action ℝ≥0 M := mul_action.comp_hom M of_nnreal_hom.to_monoid_hom lemma smul_def {M : Type*} [mul_action ℝ≥0∞ M] (c : ℝ≥0) (x : M) : c • x = (c : ℝ≥0∞) • x := rfl instance {M N : Type*} [mul_action ℝ≥0∞ M] [mul_action ℝ≥0∞ N] [has_smul M N] [is_scalar_tower ℝ≥0∞ M N] : is_scalar_tower ℝ≥0 M N := { smul_assoc := λ r, (smul_assoc (r : ℝ≥0∞) : _)} instance smul_comm_class_left {M N : Type*} [mul_action ℝ≥0∞ N] [has_smul M N] [smul_comm_class ℝ≥0∞ M N] : smul_comm_class ℝ≥0 M N := { smul_comm := λ r, (smul_comm (r : ℝ≥0∞) : _)} instance smul_comm_class_right {M N : Type*} [mul_action ℝ≥0∞ N] [has_smul M N] [smul_comm_class M ℝ≥0∞ N] : smul_comm_class M ℝ≥0 N := { smul_comm := λ m r, (smul_comm m (r : ℝ≥0∞) : _)} /-- A `distrib_mul_action` over `ℝ≥0∞` restricts to a `distrib_mul_action` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [add_monoid M] [distrib_mul_action ℝ≥0∞ M] : distrib_mul_action ℝ≥0 M := distrib_mul_action.comp_hom M of_nnreal_hom.to_monoid_hom /-- A `module` over `ℝ≥0∞` restricts to a `module` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [add_comm_monoid M] [module ℝ≥0∞ M] : module ℝ≥0 M := module.comp_hom M of_nnreal_hom /-- An `algebra` over `ℝ≥0∞` restricts to an `algebra` over `ℝ≥0`. -/ noncomputable instance {A : Type*} [semiring A] [algebra ℝ≥0∞ A] : algebra ℝ≥0 A := { smul := (•), commutes' := λ r x, by simp [algebra.commutes], smul_def' := λ r x, by simp [←algebra.smul_def (r : ℝ≥0∞) x, smul_def], to_ring_hom := ((algebra_map ℝ≥0∞ A).comp (of_nnreal_hom : ℝ≥0 →+* ℝ≥0∞)) } -- verify that the above produces instances we might care about noncomputable example : algebra ℝ≥0 ℝ≥0∞ := infer_instance noncomputable example : distrib_mul_action ℝ≥0ˣ ℝ≥0∞ := infer_instance lemma coe_smul {R} (r : R) (s : ℝ≥0) [has_smul R ℝ≥0] [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0 ℝ≥0] [is_scalar_tower R ℝ≥0 ℝ≥0∞] : (↑(r • s) : ℝ≥0∞) = r • ↑s := by rw [←smul_one_smul ℝ≥0 r (s: ℝ≥0∞), smul_def, smul_eq_mul, ←ennreal.coe_mul, smul_mul_assoc, one_mul] end actions @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ≥0∞) = s.indicator (λ x, f x) a := (of_nnreal_hom : ℝ≥0 →+ ℝ≥0∞).map_indicator _ _ _ @[simp, norm_cast] lemma coe_pow (n : ℕ) : (↑(r^n) : ℝ≥0∞) = r^n := of_nnreal_hom.map_pow r n @[simp] lemma add_eq_top : a + b = ∞ ↔ a = ∞ ∨ b = ∞ := with_top.add_eq_top @[simp] lemma add_lt_top : a + b < ∞ ↔ a < ∞ ∧ b < ∞ := with_top.add_lt_top lemma to_nnreal_add {r₁ r₂ : ℝ≥0∞} (h₁ : r₁ ≠ ∞) (h₂ : r₂ ≠ ∞) : (r₁ + r₂).to_nnreal = r₁.to_nnreal + r₂.to_nnreal := by { lift r₁ to ℝ≥0 using h₁, lift r₂ to ℝ≥0 using h₂, refl } lemma not_lt_top {x : ℝ≥0∞} : ¬ x < ∞ ↔ x = ∞ := by rw [lt_top_iff_ne_top, not_not] lemma add_ne_top : a + b ≠ ∞ ↔ a ≠ ∞ ∧ b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using add_lt_top lemma mul_top : a * ∞ = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.mul_top h } end lemma top_mul : ∞ * a = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.top_mul h } end @[simp] lemma top_mul_top : ∞ * ∞ = ∞ := with_top.top_mul_top lemma top_pow {n:ℕ} (h : 0 < n) : ∞^n = ∞ := nat.le_induction (pow_one _) (λ m hm hm', by rw [pow_succ, hm', top_mul_top]) _ (nat.succ_le_of_lt h) lemma mul_eq_top : a * b = ∞ ↔ (a ≠ 0 ∧ b = ∞) ∨ (a = ∞ ∧ b ≠ 0) := with_top.mul_eq_top_iff lemma mul_lt_top : a ≠ ∞ → b ≠ ∞ → a * b < ∞ := with_top.mul_lt_top lemma mul_ne_top : a ≠ ∞ → b ≠ ∞ → a * b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using mul_lt_top lemma lt_top_of_mul_ne_top_left (h : a * b ≠ ∞) (hb : b ≠ 0) : a < ∞ := lt_top_iff_ne_top.2 $ λ ha, h $ mul_eq_top.2 (or.inr ⟨ha, hb⟩) lemma lt_top_of_mul_ne_top_right (h : a * b ≠ ∞) (ha : a ≠ 0) : b < ∞ := lt_top_of_mul_ne_top_left (by rwa [mul_comm]) ha lemma mul_lt_top_iff {a b : ℝ≥0∞} : a * b < ∞ ↔ (a < ∞ ∧ b < ∞) ∨ a = 0 ∨ b = 0 := begin split, { intro h, rw [← or_assoc, or_iff_not_imp_right, or_iff_not_imp_right], intros hb ha, exact ⟨lt_top_of_mul_ne_top_left h.ne hb, lt_top_of_mul_ne_top_right h.ne ha⟩ }, { rintro (⟨ha, hb⟩|rfl|rfl); [exact mul_lt_top ha.ne hb.ne, simp, simp] } end lemma mul_self_lt_top_iff {a : ℝ≥0∞} : a * a < ⊤ ↔ a < ⊤ := by { rw [ennreal.mul_lt_top_iff, and_self, or_self, or_iff_left_iff_imp], rintro rfl, norm_num } lemma mul_pos_iff : 0 < a * b ↔ 0 < a ∧ 0 < b := canonically_ordered_comm_semiring.mul_pos lemma mul_pos (ha : a ≠ 0) (hb : b ≠ 0) : 0 < a * b := mul_pos_iff.2 ⟨pos_iff_ne_zero.2 ha, pos_iff_ne_zero.2 hb⟩ @[simp] lemma pow_eq_top_iff {n : ℕ} : a ^ n = ∞ ↔ a = ∞ ∧ n ≠ 0 := begin induction n with n ihn, { simp }, rw [pow_succ, mul_eq_top, ihn], fsplit, { rintro (⟨-,rfl,h0⟩|⟨rfl,h0⟩); exact ⟨rfl, n.succ_ne_zero⟩ }, { rintro ⟨rfl, -⟩, exact or.inr ⟨rfl, pow_ne_zero n top_ne_zero⟩ } end lemma pow_eq_top (n : ℕ) (h : a ^ n = ∞) : a = ∞ := (pow_eq_top_iff.1 h).1 lemma pow_ne_top (h : a ≠ ∞) {n:ℕ} : a^n ≠ ∞ := mt (pow_eq_top n) h lemma pow_lt_top : a < ∞ → ∀ n:ℕ, a^n < ∞ := by simpa only [lt_top_iff_ne_top] using pow_ne_top @[simp, norm_cast] lemma coe_finset_sum {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = (∑ a in s, f a : ℝ≥0∞) := of_nnreal_hom.map_sum f s @[simp, norm_cast] lemma coe_finset_prod {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ((∏ a in s, f a) : ℝ≥0∞) := of_nnreal_hom.map_prod f s section order @[simp] lemma bot_eq_zero : (⊥ : ℝ≥0∞) = 0 := rfl @[simp] lemma coe_lt_top : coe r < ∞ := with_top.coe_lt_top r @[simp] lemma not_top_le_coe : ¬ ∞ ≤ ↑r := with_top.not_top_le_coe r @[simp, norm_cast] lemma one_le_coe_iff : (1:ℝ≥0∞) ≤ ↑r ↔ 1 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_le_one_iff : ↑r ≤ (1:ℝ≥0∞) ↔ r ≤ 1 := coe_le_coe @[simp, norm_cast] lemma coe_lt_one_iff : (↑p : ℝ≥0∞) < 1 ↔ p < 1 := coe_lt_coe @[simp, norm_cast] lemma one_lt_coe_iff : 1 < (↑p : ℝ≥0∞) ↔ 1 < p := coe_lt_coe @[simp, norm_cast] lemma coe_nat (n : ℕ) : ((n : ℝ≥0) : ℝ≥0∞) = n := with_top.coe_nat n @[simp] lemma of_real_coe_nat (n : ℕ) : ennreal.of_real n = n := by simp [ennreal.of_real] @[simp] lemma nat_ne_top (n : ℕ) : (n : ℝ≥0∞) ≠ ∞ := with_top.nat_ne_top n @[simp] lemma top_ne_nat (n : ℕ) : ∞ ≠ n := with_top.top_ne_nat n @[simp] lemma one_lt_top : 1 < ∞ := coe_lt_top @[simp, norm_cast] lemma to_nnreal_nat (n : ℕ) : (n : ℝ≥0∞).to_nnreal = n := by conv_lhs { rw [← ennreal.coe_nat n, ennreal.to_nnreal_coe] } @[simp, norm_cast] lemma to_real_nat (n : ℕ) : (n : ℝ≥0∞).to_real = n := by conv_lhs { rw [← ennreal.of_real_coe_nat n, ennreal.to_real_of_real (nat.cast_nonneg _)] } lemma le_coe_iff : a ≤ ↑r ↔ (∃p:ℝ≥0, a = p ∧ p ≤ r) := with_top.le_coe_iff lemma coe_le_iff : ↑r ≤ a ↔ (∀p:ℝ≥0, a = p → r ≤ p) := with_top.coe_le_iff lemma lt_iff_exists_coe : a < b ↔ (∃p:ℝ≥0, a = p ∧ ↑p < b) := with_top.lt_iff_exists_coe lemma to_real_le_coe_of_le_coe {a : ℝ≥0∞} {b : ℝ≥0} (h : a ≤ b) : a.to_real ≤ b := show ↑a.to_nnreal ≤ ↑b, begin have : ↑a.to_nnreal = a := ennreal.coe_to_nnreal (lt_of_le_of_lt h coe_lt_top).ne, rw ← this at h, exact_mod_cast h end @[simp, norm_cast] lemma coe_finset_sup {s : finset α} {f : α → ℝ≥0} : ↑(s.sup f) = s.sup (λ x, (f x : ℝ≥0∞)) := finset.comp_sup_eq_sup_comp_of_is_total _ coe_mono rfl lemma pow_le_pow {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := begin cases a, { cases m, { rw eq_bot_iff.mpr h, exact le_rfl }, { rw [none_eq_top, top_pow (nat.succ_pos m)], exact le_top } }, { rw [some_eq_coe, ← coe_pow, ← coe_pow, coe_le_coe], exact pow_le_pow (by simpa using ha) h } end lemma one_le_pow_of_one_le (ha : 1 ≤ a) (n : ℕ) : 1 ≤ a ^ n := by simpa using pow_le_pow ha (zero_le n) @[simp] lemma max_eq_zero_iff : max a b = 0 ↔ a = 0 ∧ b = 0 := by simp only [nonpos_iff_eq_zero.symm, max_le_iff] @[simp] lemma max_zero_left : max 0 a = a := max_eq_right (zero_le a) @[simp] lemma max_zero_right : max a 0 = a := max_eq_left (zero_le a) @[simp] lemma sup_eq_max : a ⊔ b = max a b := rfl protected lemma pow_pos : 0 < a → ∀ n : ℕ, 0 < a^n := canonically_ordered_comm_semiring.pow_pos protected lemma pow_ne_zero : a ≠ 0 → ∀ n : ℕ, a^n ≠ 0 := by simpa only [pos_iff_ne_zero] using ennreal.pow_pos @[simp] lemma not_lt_zero : ¬ a < 0 := by simp protected lemma le_of_add_le_add_left : a ≠ ∞ → a + b ≤ a + c → b ≤ c := with_top.le_of_add_le_add_left protected lemma le_of_add_le_add_right : a ≠ ∞ → b + a ≤ c + a → b ≤ c := with_top.le_of_add_le_add_right protected lemma add_lt_add_left : a ≠ ∞ → b < c → a + b < a + c := with_top.add_lt_add_left protected lemma add_lt_add_right : a ≠ ∞ → b < c → b + a < c + a := with_top.add_lt_add_right protected lemma add_le_add_iff_left : a ≠ ∞ → (a + b ≤ a + c ↔ b ≤ c) := with_top.add_le_add_iff_left protected lemma add_le_add_iff_right : a ≠ ∞ → (b + a ≤ c + a ↔ b ≤ c) := with_top.add_le_add_iff_right protected lemma add_lt_add_iff_left : a ≠ ∞ → (a + b < a + c ↔ b < c) := with_top.add_lt_add_iff_left protected lemma add_lt_add_iff_right : a ≠ ∞ → (b + a < c + a ↔ b < c) := with_top.add_lt_add_iff_right protected lemma add_lt_add_of_le_of_lt : a ≠ ∞ → a ≤ b → c < d → a + c < b + d := with_top.add_lt_add_of_le_of_lt protected lemma add_lt_add_of_lt_of_le : c ≠ ∞ → a < b → c ≤ d → a + c < b + d := with_top.add_lt_add_of_lt_of_le instance contravariant_class_add_lt : contravariant_class ℝ≥0∞ ℝ≥0∞ (+) (<) := with_top.contravariant_class_add_lt lemma lt_add_right (ha : a ≠ ∞) (hb : b ≠ 0) : a < a + b := by rwa [← pos_iff_ne_zero, ←ennreal.add_lt_add_iff_left ha, add_zero] at hb lemma le_of_forall_pos_le_add : ∀{a b : ℝ≥0∞}, (∀ε : ℝ≥0, 0 < ε → b < ∞ → a ≤ b + ε) → a ≤ b | a none h := le_top | none (some a) h := have ∞ ≤ ↑a + ↑(1:ℝ≥0), from h 1 zero_lt_one coe_lt_top, by rw [← coe_add] at this; exact (not_top_le_coe this).elim | (some a) (some b) h := by simp only [none_eq_top, some_eq_coe, coe_add.symm, coe_le_coe, coe_lt_top, true_implies_iff] at *; exact nnreal.le_of_forall_pos_le_add h lemma lt_iff_exists_rat_btwn : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < real.to_nnreal q ∧ (real.to_nnreal q:ℝ≥0∞) < b) := ⟨λ h, begin rcases lt_iff_exists_coe.1 h with ⟨p, rfl, _⟩, rcases exists_between h with ⟨c, pc, cb⟩, rcases lt_iff_exists_coe.1 cb with ⟨r, rfl, _⟩, rcases (nnreal.lt_iff_exists_rat_btwn _ _).1 (coe_lt_coe.1 pc) with ⟨q, hq0, pq, qr⟩, exact ⟨q, hq0, coe_lt_coe.2 pq, lt_trans (coe_lt_coe.2 qr) cb⟩ end, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_real_btwn : a < b ↔ (∃r:ℝ, 0 ≤ r ∧ a < ennreal.of_real r ∧ (ennreal.of_real r:ℝ≥0∞) < b) := ⟨λ h, let ⟨q, q0, aq, qb⟩ := ennreal.lt_iff_exists_rat_btwn.1 h in ⟨q, rat.cast_nonneg.2 q0, aq, qb⟩, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_nnreal_btwn : a < b ↔ (∃r:ℝ≥0, a < r ∧ (r : ℝ≥0∞) < b) := with_top.lt_iff_exists_coe_btwn lemma lt_iff_exists_add_pos_lt : a < b ↔ (∃ r : ℝ≥0, 0 < r ∧ a + r < b) := begin refine ⟨λ hab, _, λ ⟨r, rpos, hr⟩, lt_of_le_of_lt (le_self_add) hr⟩, cases a, { simpa using hab }, rcases lt_iff_exists_real_btwn.1 hab with ⟨c, c_nonneg, ac, cb⟩, let d : ℝ≥0 := ⟨c, c_nonneg⟩, have ad : a < d, { rw of_real_eq_coe_nnreal c_nonneg at ac, exact coe_lt_coe.1 ac }, refine ⟨d-a, tsub_pos_iff_lt.2 ad, _⟩, rw [some_eq_coe, ← coe_add], convert cb, have : real.to_nnreal c = d, by { rw [← nnreal.coe_eq, real.coe_to_nnreal _ c_nonneg], refl }, rw [add_comm, this], exact tsub_add_cancel_of_le ad.le end lemma coe_nat_lt_coe {n : ℕ} : (n : ℝ≥0∞) < r ↔ ↑n < r := ennreal.coe_nat n ▸ coe_lt_coe lemma coe_lt_coe_nat {n : ℕ} : (r : ℝ≥0∞) < n ↔ r < n := ennreal.coe_nat n ▸ coe_lt_coe @[simp, norm_cast] lemma coe_nat_lt_coe_nat {m n : ℕ} : (m : ℝ≥0∞) < n ↔ m < n := ennreal.coe_nat n ▸ coe_nat_lt_coe.trans nat.cast_lt lemma coe_nat_mono : strict_mono (coe : ℕ → ℝ≥0∞) := λ _ _, coe_nat_lt_coe_nat.2 @[simp, norm_cast] lemma coe_nat_le_coe_nat {m n : ℕ} : (m : ℝ≥0∞) ≤ n ↔ m ≤ n := coe_nat_mono.le_iff_le instance : char_zero ℝ≥0∞ := ⟨coe_nat_mono.injective⟩ protected lemma exists_nat_gt {r : ℝ≥0∞} (h : r ≠ ∞) : ∃n:ℕ, r < n := begin lift r to ℝ≥0 using h, rcases exists_nat_gt r with ⟨n, hn⟩, exact ⟨n, coe_lt_coe_nat.2 hn⟩, end @[simp] lemma Union_Iio_coe_nat : (⋃ n : ℕ, Iio (n : ℝ≥0∞)) = {∞}ᶜ := begin ext x, rw [mem_Union], exact ⟨λ ⟨n, hn⟩, ne_top_of_lt hn, ennreal.exists_nat_gt⟩ end @[simp] lemma Union_Iic_coe_nat : (⋃ n : ℕ, Iic (n : ℝ≥0∞)) = {∞}ᶜ := subset.antisymm (Union_subset $ λ n x hx, ne_top_of_le_ne_top (nat_ne_top n) hx) $ Union_Iio_coe_nat ▸ Union_mono (λ n, Iio_subset_Iic_self) @[simp] lemma Union_Ioc_coe_nat : (⋃ n : ℕ, Ioc a n) = Ioi a \ {∞} := by simp only [← Ioi_inter_Iic, ← inter_Union, Union_Iic_coe_nat, diff_eq] @[simp] lemma Union_Ioo_coe_nat : (⋃ n : ℕ, Ioo a n) = Ioi a \ {∞} := by simp only [← Ioi_inter_Iio, ← inter_Union, Union_Iio_coe_nat, diff_eq] @[simp] lemma Union_Icc_coe_nat : (⋃ n : ℕ, Icc a n) = Ici a \ {∞} := by simp only [← Ici_inter_Iic, ← inter_Union, Union_Iic_coe_nat, diff_eq] @[simp] lemma Union_Ico_coe_nat : (⋃ n : ℕ, Ico a n) = Ici a \ {∞} := by simp only [← Ici_inter_Iio, ← inter_Union, Union_Iio_coe_nat, diff_eq] @[simp] lemma Inter_Ici_coe_nat : (⋂ n : ℕ, Ici (n : ℝ≥0∞)) = {∞} := by simp only [← compl_Iio, ← compl_Union, Union_Iio_coe_nat, compl_compl] @[simp] lemma Inter_Ioi_coe_nat : (⋂ n : ℕ, Ioi (n : ℝ≥0∞)) = {∞} := by simp only [← compl_Iic, ← compl_Union, Union_Iic_coe_nat, compl_compl] lemma add_lt_add (ac : a < c) (bd : b < d) : a + b < c + d := begin lift a to ℝ≥0 using ne_top_of_lt ac, lift b to ℝ≥0 using ne_top_of_lt bd, cases c, { simp }, cases d, { simp }, simp only [← coe_add, some_eq_coe, coe_lt_coe] at *, exact add_lt_add ac bd end @[norm_cast] lemma coe_min : ((min r p:ℝ≥0):ℝ≥0∞) = min r p := coe_mono.map_min @[norm_cast] lemma coe_max : ((max r p:ℝ≥0):ℝ≥0∞) = max r p := coe_mono.map_max lemma le_of_top_imp_top_of_to_nnreal_le {a b : ℝ≥0∞} (h : a = ⊤ → b = ⊤) (h_nnreal : a ≠ ⊤ → b ≠ ⊤ → a.to_nnreal ≤ b.to_nnreal) : a ≤ b := begin by_cases ha : a = ⊤, { rw h ha, exact le_top, }, by_cases hb : b = ⊤, { rw hb, exact le_top, }, rw [←coe_to_nnreal hb, ←coe_to_nnreal ha, coe_le_coe], exact h_nnreal ha hb, end end order section complete_lattice lemma coe_Sup {s : set ℝ≥0} : bdd_above s → (↑(Sup s) : ℝ≥0∞) = (⨆a∈s, ↑a) := with_top.coe_Sup lemma coe_Inf {s : set ℝ≥0} : s.nonempty → (↑(Inf s) : ℝ≥0∞) = (⨅a∈s, ↑a) := with_top.coe_Inf @[simp] lemma top_mem_upper_bounds {s : set ℝ≥0∞} : ∞ ∈ upper_bounds s := assume x hx, le_top lemma coe_mem_upper_bounds {s : set ℝ≥0} : ↑r ∈ upper_bounds ((coe : ℝ≥0 → ℝ≥0∞) '' s) ↔ r ∈ upper_bounds s := by simp [upper_bounds, ball_image_iff, -mem_image, *] {contextual := tt} end complete_lattice section mul @[mono] lemma mul_le_mul : a ≤ b → c ≤ d → a * c ≤ b * d := mul_le_mul' @[mono] lemma mul_lt_mul (ac : a < c) (bd : b < d) : a * b < c * d := begin rcases lt_iff_exists_nnreal_btwn.1 ac with ⟨a', aa', a'c⟩, lift a to ℝ≥0 using ne_top_of_lt aa', rcases lt_iff_exists_nnreal_btwn.1 bd with ⟨b', bb', b'd⟩, lift b to ℝ≥0 using ne_top_of_lt bb', norm_cast at *, calc ↑(a * b) < ↑(a' * b') : coe_lt_coe.2 (mul_lt_mul' aa'.le bb' (zero_le _) ((zero_le a).trans_lt aa')) ... = ↑a' * ↑b' : coe_mul ... ≤ c * d : mul_le_mul a'c.le b'd.le end lemma mul_left_mono : monotone ((*) a) := λ b c, mul_le_mul le_rfl lemma mul_right_mono : monotone (λ x, x * a) := λ b c h, mul_le_mul h le_rfl lemma pow_strict_mono {n : ℕ} (hn : n ≠ 0) : strict_mono (λ (x : ℝ≥0∞), x^n) := begin assume x y hxy, obtain ⟨n, rfl⟩ := nat.exists_eq_succ_of_ne_zero hn, induction n with n IH, { simp only [hxy, pow_one] }, { simp only [pow_succ _ n.succ, mul_lt_mul hxy (IH (nat.succ_pos _).ne')] } end lemma max_mul : max a b * c = max (a * c) (b * c) := mul_right_mono.map_max lemma mul_max : a * max b c = max (a * b) (a * c) := mul_left_mono.map_max lemma mul_eq_mul_left : a ≠ 0 → a ≠ ∞ → (a * b = a * c ↔ b = c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm, nnreal.mul_eq_mul_left] {contextual := tt}, end lemma mul_eq_mul_right : c ≠ 0 → c ≠ ∞ → (a * c = b * c ↔ a = b) := mul_comm c a ▸ mul_comm c b ▸ mul_eq_mul_left lemma mul_le_mul_left : a ≠ 0 → a ≠ ∞ → (a * b ≤ a * c ↔ b ≤ c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm] {contextual := tt}, assume h, exact mul_le_mul_left (pos_iff_ne_zero.2 h) end lemma mul_le_mul_right : c ≠ 0 → c ≠ ∞ → (a * c ≤ b * c ↔ a ≤ b) := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left lemma mul_lt_mul_left : a ≠ 0 → a ≠ ∞ → (a * b < a * c ↔ b < c) := λ h0 ht, by simp only [mul_le_mul_left h0 ht, lt_iff_le_not_le] lemma mul_lt_mul_right : c ≠ 0 → c ≠ ∞ → (a * c < b * c ↔ a < b) := mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_left end mul section cancel /-- An element `a` is `add_le_cancellable` if `a + b ≤ a + c` implies `b ≤ c` for all `b` and `c`. This is true in `ℝ≥0∞` for all elements except `∞`. -/ lemma add_le_cancellable_iff_ne {a : ℝ≥0∞} : add_le_cancellable a ↔ a ≠ ∞ := begin split, { rintro h rfl, refine ennreal.zero_lt_one.not_le (h _), simp, }, { rintro h b c hbc, apply ennreal.le_of_add_le_add_left h hbc } end /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_ne {a : ℝ≥0∞} (h : a ≠ ∞) : add_le_cancellable a := add_le_cancellable_iff_ne.mpr h /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_lt {a : ℝ≥0∞} (h : a < ∞) : add_le_cancellable a := cancel_of_ne h.ne /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_lt' {a b : ℝ≥0∞} (h : a < b) : add_le_cancellable a := cancel_of_ne h.ne_top /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_coe {a : ℝ≥0} : add_le_cancellable (a : ℝ≥0∞) := cancel_of_ne coe_ne_top lemma add_right_inj (h : a ≠ ∞) : a + b = a + c ↔ b = c := (cancel_of_ne h).inj lemma add_left_inj (h : a ≠ ∞) : b + a = c + a ↔ b = c := (cancel_of_ne h).inj_left end cancel section sub lemma sub_eq_Inf {a b : ℝ≥0∞} : a - b = Inf {d | a ≤ d + b} := le_antisymm (le_Inf $ λ c, tsub_le_iff_right.mpr) $ Inf_le le_tsub_add /-- This is a special case of `with_top.coe_sub` in the `ennreal` namespace -/ lemma coe_sub : (↑(r - p) : ℝ≥0∞) = ↑r - ↑p := with_top.coe_sub /-- This is a special case of `with_top.top_sub_coe` in the `ennreal` namespace -/ lemma top_sub_coe : ∞ - ↑r = ∞ := with_top.top_sub_coe /-- This is a special case of `with_top.sub_top` in the `ennreal` namespace -/ lemma sub_top : a - ∞ = 0 := with_top.sub_top lemma sub_eq_top_iff : a - b = ∞ ↔ a = ∞ ∧ b ≠ ∞ := by { cases a; cases b; simp [← with_top.coe_sub] } lemma sub_ne_top (ha : a ≠ ∞) : a - b ≠ ∞ := mt sub_eq_top_iff.mp $ mt and.left ha protected lemma sub_eq_of_eq_add (hb : b ≠ ∞) : a = c + b → a - b = c := (cancel_of_ne hb).tsub_eq_of_eq_add protected lemma eq_sub_of_add_eq (hc : c ≠ ∞) : a + c = b → a = b - c := (cancel_of_ne hc).eq_tsub_of_add_eq protected lemma sub_eq_of_eq_add_rev (hb : b ≠ ∞) : a = b + c → a - b = c := (cancel_of_ne hb).tsub_eq_of_eq_add_rev lemma sub_eq_of_add_eq (hb : b ≠ ∞) (hc : a + b = c) : c - b = a := ennreal.sub_eq_of_eq_add hb hc.symm @[simp] protected lemma add_sub_cancel_left (ha : a ≠ ∞) : a + b - a = b := (cancel_of_ne ha).add_tsub_cancel_left @[simp] protected lemma add_sub_cancel_right (hb : b ≠ ∞) : a + b - b = a := (cancel_of_ne hb).add_tsub_cancel_right protected lemma lt_add_of_sub_lt_left (h : a ≠ ∞ ∨ b ≠ ∞) : a - b < c → a < b + c := begin obtain rfl | hb := eq_or_ne b ∞, { rw [top_add, lt_top_iff_ne_top], exact λ _, h.resolve_right (not_not.2 rfl) }, { exact (cancel_of_ne hb).lt_add_of_tsub_lt_left } end protected lemma lt_add_of_sub_lt_right (h : a ≠ ∞ ∨ c ≠ ∞) : a - c < b → a < b + c := add_comm c b ▸ ennreal.lt_add_of_sub_lt_left h lemma le_sub_of_add_le_left (ha : a ≠ ∞) : a + b ≤ c → b ≤ c - a := (cancel_of_ne ha).le_tsub_of_add_le_left lemma le_sub_of_add_le_right (hb : b ≠ ∞) : a + b ≤ c → a ≤ c - b := (cancel_of_ne hb).le_tsub_of_add_le_right protected lemma sub_lt_of_lt_add (hac : c ≤ a) (h : a < b + c) : a - c < b := ((cancel_of_lt' $ hac.trans_lt h).tsub_lt_iff_right hac).mpr h protected lemma sub_lt_iff_lt_right (hb : b ≠ ∞) (hab : b ≤ a) : a - b < c ↔ a < c + b := (cancel_of_ne hb).tsub_lt_iff_right hab protected lemma sub_lt_self (ha : a ≠ ∞) (ha₀ : a ≠ 0) (hb : b ≠ 0) : a - b < a := (cancel_of_ne ha).tsub_lt_self (pos_iff_ne_zero.2 ha₀) (pos_iff_ne_zero.2 hb) protected lemma sub_lt_self_iff (ha : a ≠ ∞) : a - b < a ↔ 0 < a ∧ 0 < b := (cancel_of_ne ha).tsub_lt_self_iff lemma sub_lt_of_sub_lt (h₂ : c ≤ a) (h₃ : a ≠ ∞ ∨ b ≠ ∞) (h₁ : a - b < c) : a - c < b := ennreal.sub_lt_of_lt_add h₂ (add_comm c b ▸ ennreal.lt_add_of_sub_lt_right h₃ h₁) lemma sub_sub_cancel (h : a ≠ ∞) (h2 : b ≤ a) : a - (a - b) = b := (cancel_of_ne $ sub_ne_top h).tsub_tsub_cancel_of_le h2 lemma sub_right_inj {a b c : ℝ≥0∞} (ha : a ≠ ∞) (hb : b ≤ a) (hc : c ≤ a) : a - b = a - c ↔ b = c := (cancel_of_ne ha).tsub_right_inj (cancel_of_ne $ ne_top_of_le_ne_top ha hb) (cancel_of_ne $ ne_top_of_le_ne_top ha hc) hb hc lemma sub_mul (h : 0 < b → b < a → c ≠ ∞) : (a - b) * c = a * c - b * c := begin cases le_or_lt a b with hab hab, { simp [hab, mul_right_mono hab] }, rcases eq_or_lt_of_le (zero_le b) with rfl|hb, { simp }, exact (cancel_of_ne $ mul_ne_top hab.ne_top (h hb hab)).tsub_mul end lemma mul_sub (h : 0 < c → c < b → a ≠ ∞) : a * (b - c) = a * b - a * c := by { simp only [mul_comm a], exact sub_mul h } end sub section sum open finset /-- A product of finite numbers is still finite -/ lemma prod_lt_top {s : finset α} {f : α → ℝ≥0∞} (h : ∀ a ∈ s, f a ≠ ∞) : (∏ a in s, f a) < ∞ := with_top.prod_lt_top h /-- A sum of finite numbers is still finite -/ lemma sum_lt_top {s : finset α} {f : α → ℝ≥0∞} (h : ∀ a ∈ s, f a ≠ ∞) : ∑ a in s, f a < ∞ := with_top.sum_lt_top h /-- A sum of finite numbers is still finite -/ lemma sum_lt_top_iff {s : finset α} {f : α → ℝ≥0∞} : ∑ a in s, f a < ∞ ↔ (∀ a ∈ s, f a < ∞) := with_top.sum_lt_top_iff /-- A sum of numbers is infinite iff one of them is infinite -/ lemma sum_eq_top_iff {s : finset α} {f : α → ℝ≥0∞} : (∑ x in s, f x) = ∞ ↔ (∃ a ∈ s, f a = ∞) := with_top.sum_eq_top_iff lemma lt_top_of_sum_ne_top {s : finset α} {f : α → ℝ≥0∞} (h : (∑ x in s, f x) ≠ ∞) {a : α} (ha : a ∈ s) : f a < ∞ := sum_lt_top_iff.1 h.lt_top a ha /-- seeing `ℝ≥0∞` as `ℝ≥0` does not change their sum, unless one of the `ℝ≥0∞` is infinity -/ lemma to_nnreal_sum {s : finset α} {f : α → ℝ≥0∞} (hf : ∀a∈s, f a ≠ ∞) : ennreal.to_nnreal (∑ a in s, f a) = ∑ a in s, ennreal.to_nnreal (f a) := begin rw [← coe_eq_coe, coe_to_nnreal, coe_finset_sum, sum_congr rfl], { intros x hx, exact (coe_to_nnreal (hf x hx)).symm }, { exact (sum_lt_top hf).ne } end /-- seeing `ℝ≥0∞` as `real` does not change their sum, unless one of the `ℝ≥0∞` is infinity -/ lemma to_real_sum {s : finset α} {f : α → ℝ≥0∞} (hf : ∀ a ∈ s, f a ≠ ∞) : ennreal.to_real (∑ a in s, f a) = ∑ a in s, ennreal.to_real (f a) := by { rw [ennreal.to_real, to_nnreal_sum hf, nnreal.coe_sum], refl } lemma of_real_sum_of_nonneg {s : finset α} {f : α → ℝ} (hf : ∀ i, i ∈ s → 0 ≤ f i) : ennreal.of_real (∑ i in s, f i) = ∑ i in s, ennreal.of_real (f i) := begin simp_rw [ennreal.of_real, ←coe_finset_sum, coe_eq_coe], exact real.to_nnreal_sum_of_nonneg hf, end theorem sum_lt_sum_of_nonempty {s : finset α} (hs : s.nonempty) {f g : α → ℝ≥0∞} (Hlt : ∀ i ∈ s, f i < g i) : ∑ i in s, f i < ∑ i in s, g i := begin induction hs using finset.nonempty.cons_induction with a a s as hs IH, { simp [Hlt _ (finset.mem_singleton_self _)] }, { simp only [as, finset.sum_cons, not_false_iff], exact ennreal.add_lt_add (Hlt _ (finset.mem_cons_self _ _)) (IH (λ i hi, Hlt _ (finset.mem_cons.2 $ or.inr hi))) } end theorem exists_le_of_sum_le {s : finset α} (hs : s.nonempty) {f g : α → ℝ≥0∞} (Hle : ∑ i in s, f i ≤ ∑ i in s, g i) : ∃ i ∈ s, f i ≤ g i := begin contrapose! Hle, apply ennreal.sum_lt_sum_of_nonempty hs Hle, end end sum section interval variables {x y z : ℝ≥0∞} {ε ε₁ ε₂ : ℝ≥0∞} {s : set ℝ≥0∞} protected lemma Ico_eq_Iio : (Ico 0 y) = (Iio y) := Ico_bot lemma mem_Iio_self_add : x ≠ ∞ → ε ≠ 0 → x ∈ Iio (x + ε) := assume xt ε0, lt_add_right xt ε0 lemma mem_Ioo_self_sub_add : x ≠ ∞ → x ≠ 0 → ε₁ ≠ 0 → ε₂ ≠ 0 → x ∈ Ioo (x - ε₁) (x + ε₂) := assume xt x0 ε0 ε0', ⟨ennreal.sub_lt_self xt x0 ε0, lt_add_right xt ε0'⟩ end interval section bit @[mono] lemma bit0_strict_mono : strict_mono (bit0 : ℝ≥0∞ → ℝ≥0∞) := λ a b h, add_lt_add h h lemma bit0_injective : function.injective (bit0 : ℝ≥0∞ → ℝ≥0∞) := bit0_strict_mono.injective @[simp] lemma bit0_lt_bit0 : bit0 a < bit0 b ↔ a < b := bit0_strict_mono.lt_iff_lt @[simp, mono] lemma bit0_le_bit0 : bit0 a ≤ bit0 b ↔ a ≤ b := bit0_strict_mono.le_iff_le @[simp] lemma bit0_inj : bit0 a = bit0 b ↔ a = b := bit0_injective.eq_iff @[simp] lemma bit0_eq_zero_iff : bit0 a = 0 ↔ a = 0 := bit0_injective.eq_iff' bit0_zero @[simp] lemma bit0_top : bit0 ∞ = ∞ := add_top @[simp] lemma bit0_eq_top_iff : bit0 a = ∞ ↔ a = ∞ := bit0_injective.eq_iff' bit0_top @[mono] lemma bit1_strict_mono : strict_mono (bit1 : ℝ≥0∞ → ℝ≥0∞) := λ a b h, ennreal.add_lt_add_right one_ne_top (bit0_strict_mono h) lemma bit1_injective : function.injective (bit1 : ℝ≥0∞ → ℝ≥0∞) := bit1_strict_mono.injective @[simp] lemma bit1_lt_bit1 : bit1 a < bit1 b ↔ a < b := bit1_strict_mono.lt_iff_lt @[simp, mono] lemma bit1_le_bit1 : bit1 a ≤ bit1 b ↔ a ≤ b := bit1_strict_mono.le_iff_le @[simp] lemma bit1_inj : bit1 a = bit1 b ↔ a = b := bit1_injective.eq_iff @[simp] lemma bit1_ne_zero : bit1 a ≠ 0 := by simp [bit1] @[simp] lemma bit1_top : bit1 ∞ = ∞ := by rw [bit1, bit0_top, top_add] @[simp] lemma bit1_eq_top_iff : bit1 a = ∞ ↔ a = ∞ := bit1_injective.eq_iff' bit1_top @[simp] lemma bit1_eq_one_iff : bit1 a = 1 ↔ a = 0 := bit1_injective.eq_iff' bit1_zero end bit section inv noncomputable theory instance : has_inv ℝ≥0∞ := ⟨λa, Inf {b | 1 ≤ a * b}⟩ instance : div_inv_monoid ℝ≥0∞ := { inv := has_inv.inv, .. (infer_instance : monoid ℝ≥0∞) } lemma div_eq_inv_mul : a / b = b⁻¹ * a := by rw [div_eq_mul_inv, mul_comm] @[simp] lemma inv_zero : (0 : ℝ≥0∞)⁻¹ = ∞ := show Inf {b : ℝ≥0∞ | 1 ≤ 0 * b} = ∞, by simp; refl @[simp] lemma inv_top : ∞⁻¹ = 0 := bot_unique $ le_of_forall_le_of_dense $ λ a (h : a > 0), Inf_le $ by simp [*, ne_of_gt h, top_mul] lemma coe_inv_le : (↑r⁻¹ : ℝ≥0∞) ≤ (↑r)⁻¹ := le_Inf $ assume b (hb : 1 ≤ ↑r * b), coe_le_iff.2 $ by { rintro b rfl, apply nnreal.inv_le_of_le_mul, rwa [← coe_mul, ← coe_one, coe_le_coe] at hb } @[simp, norm_cast] lemma coe_inv (hr : r ≠ 0) : (↑r⁻¹ : ℝ≥0∞) = (↑r)⁻¹ := coe_inv_le.antisymm $ Inf_le $ le_of_eq $ by rw [← coe_mul, mul_inv_cancel hr, coe_one] @[norm_cast] lemma coe_inv_two : ((2⁻¹ : ℝ≥0) : ℝ≥0∞) = 2⁻¹ := by rw [coe_inv _root_.two_ne_zero, coe_two] @[simp, norm_cast] lemma coe_div (hr : r ≠ 0) : (↑(p / r) : ℝ≥0∞) = p / r := by rw [div_eq_mul_inv, div_eq_mul_inv, coe_mul, coe_inv hr] lemma div_zero (h : a ≠ 0) : a / 0 = ∞ := by simp [div_eq_mul_inv, h] @[simp] lemma inv_one : (1 : ℝ≥0∞)⁻¹ = 1 := by simpa only [coe_inv one_ne_zero, coe_one] using coe_eq_coe.2 inv_one @[simp] lemma div_one {a : ℝ≥0∞} : a / 1 = a := by rw [div_eq_mul_inv, inv_one, mul_one] protected lemma inv_pow {n : ℕ} : (a^n)⁻¹ = (a⁻¹)^n := begin cases n, { simp only [pow_zero, inv_one] }, induction a using with_top.rec_top_coe, { simp [top_pow n.succ_pos] }, rcases eq_or_ne a 0 with rfl|ha, { simp [top_pow, zero_pow, n.succ_pos] }, rw [← coe_inv ha, ← coe_pow, ← coe_inv (pow_ne_zero _ ha), ← inv_pow, coe_pow] end lemma mul_inv_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a * a⁻¹ = 1 := begin lift a to ℝ≥0 using ht, norm_cast at *, exact mul_inv_cancel h0 end lemma inv_mul_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a⁻¹ * a = 1 := mul_comm a a⁻¹ ▸ mul_inv_cancel h0 ht lemma div_mul_cancel (h0 : a ≠ 0) (hI : a ≠ ∞) : (b / a) * a = b := by rw [div_eq_mul_inv, mul_assoc, inv_mul_cancel h0 hI, mul_one] lemma mul_div_cancel' (h0 : a ≠ 0) (hI : a ≠ ∞) : a * (b / a) = b := by rw [mul_comm, div_mul_cancel h0 hI] instance : has_involutive_inv ℝ≥0∞ := { inv := has_inv.inv, inv_inv := λ a, by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] at * } @[simp] lemma inv_eq_top : a⁻¹ = ∞ ↔ a = 0 := inv_zero ▸ inv_inj lemma inv_ne_top : a⁻¹ ≠ ∞ ↔ a ≠ 0 := by simp @[simp] lemma inv_lt_top {x : ℝ≥0∞} : x⁻¹ < ∞ ↔ 0 < x := by { simp only [lt_top_iff_ne_top, inv_ne_top, pos_iff_ne_zero] } lemma div_lt_top {x y : ℝ≥0∞} (h1 : x ≠ ∞) (h2 : y ≠ 0) : x / y < ∞ := mul_lt_top h1 (inv_ne_top.mpr h2) @[simp] lemma inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ := inv_top ▸ inv_inj lemma inv_ne_zero : a⁻¹ ≠ 0 ↔ a ≠ ∞ := by simp lemma mul_inv {a b : ℝ≥0∞} (ha : a ≠ 0 ∨ b ≠ ∞) (hb : a ≠ ∞ ∨ b ≠ 0) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := begin induction b using with_top.rec_top_coe, { replace ha : a ≠ 0 := ha.neg_resolve_right rfl, simp [ha], }, induction a using with_top.rec_top_coe, { replace hb : b ≠ 0 := coe_ne_zero.1 (hb.neg_resolve_left rfl), simp [hb] }, by_cases h'a : a = 0, { simp only [h'a, with_top.top_mul, ennreal.inv_zero, ennreal.coe_ne_top, zero_mul, ne.def, not_false_iff, ennreal.coe_zero, ennreal.inv_eq_zero] }, by_cases h'b : b = 0, { simp only [h'b, ennreal.inv_zero, ennreal.coe_ne_top, with_top.mul_top, ne.def, not_false_iff, mul_zero, ennreal.coe_zero, ennreal.inv_eq_zero] }, rw [← ennreal.coe_mul, ← ennreal.coe_inv, ← ennreal.coe_inv h'a, ← ennreal.coe_inv h'b, ← ennreal.coe_mul, mul_inv_rev, mul_comm], simp [h'a, h'b], end @[simp] lemma inv_pos : 0 < a⁻¹ ↔ a ≠ ∞ := pos_iff_ne_zero.trans inv_ne_zero lemma inv_strict_anti : strict_anti (has_inv.inv : ℝ≥0∞ → ℝ≥0∞) := begin intros a b h, lift a to ℝ≥0 using h.ne_top, induction b using with_top.rec_top_coe, { simp }, rw [coe_lt_coe] at h, rcases eq_or_ne a 0 with rfl|ha, { simp [h] }, rw [← coe_inv h.ne_bot, ← coe_inv ha, coe_lt_coe], exact nnreal.inv_lt_inv ha h end @[simp] lemma inv_lt_inv : a⁻¹ < b⁻¹ ↔ b < a := inv_strict_anti.lt_iff_lt lemma inv_lt_iff_inv_lt : a⁻¹ < b ↔ b⁻¹ < a := by simpa only [inv_inv] using @inv_lt_inv a b⁻¹ lemma lt_inv_iff_lt_inv : a < b⁻¹ ↔ b < a⁻¹ := by simpa only [inv_inv] using @inv_lt_inv a⁻¹ b @[simp, priority 1100] -- higher than le_inv_iff_mul_le lemma inv_le_inv : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := inv_strict_anti.le_iff_le lemma inv_le_iff_inv_le : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := by simpa only [inv_inv] using @inv_le_inv a b⁻¹ lemma le_inv_iff_le_inv : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := by simpa only [inv_inv] using @inv_le_inv a⁻¹ b @[simp] lemma inv_le_one : a⁻¹ ≤ 1 ↔ 1 ≤ a := inv_le_iff_inv_le.trans $ by rw inv_one lemma one_le_inv : 1 ≤ a⁻¹ ↔ a ≤ 1 := le_inv_iff_le_inv.trans $ by rw inv_one @[simp] lemma inv_lt_one : a⁻¹ < 1 ↔ 1 < a := inv_lt_iff_inv_lt.trans $ by rw [inv_one] /-- The inverse map `λ x, x⁻¹` is an order isomorphism between `ℝ≥0∞` and its `order_dual` -/ @[simps apply] def _root_.order_iso.inv_ennreal : ℝ≥0∞ ≃o ℝ≥0∞ᵒᵈ := { map_rel_iff' := λ a b, ennreal.inv_le_inv, to_equiv := (equiv.inv ℝ≥0∞).trans order_dual.to_dual } @[simp] lemma _root_.order_iso.inv_ennreal_symm_apply : order_iso.inv_ennreal.symm a = (order_dual.of_dual a)⁻¹ := rfl lemma pow_le_pow_of_le_one {n m : ℕ} (ha : a ≤ 1) (h : n ≤ m) : a ^ m ≤ a ^ n := begin rw [←inv_inv a, ← ennreal.inv_pow, ← @ennreal.inv_pow a⁻¹, inv_le_inv], exact pow_le_pow (one_le_inv.2 ha) h end @[simp] lemma div_top : a / ∞ = 0 := by rw [div_eq_mul_inv, inv_top, mul_zero] @[simp] lemma top_div_coe : ∞ / p = ∞ := by simp [div_eq_mul_inv, top_mul] lemma top_div_of_ne_top (h : a ≠ ∞) : ∞ / a = ∞ := by { lift a to ℝ≥0 using h, exact top_div_coe } lemma top_div_of_lt_top (h : a < ∞) : ∞ / a = ∞ := top_div_of_ne_top h.ne lemma top_div : ∞ / a = if a = ∞ then 0 else ∞ := by by_cases a = ∞; simp [top_div_of_ne_top, *] @[simp] lemma zero_div : 0 / a = 0 := zero_mul a⁻¹ lemma div_eq_top : a / b = ∞ ↔ (a ≠ 0 ∧ b = 0) ∨ (a = ∞ ∧ b ≠ ∞) := by simp [div_eq_mul_inv, ennreal.mul_eq_top] lemma le_div_iff_mul_le (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ∞ ∨ c ≠ ∞) : a ≤ c / b ↔ a * b ≤ c := begin induction b using with_top.rec_top_coe, { lift c to ℝ≥0 using ht.neg_resolve_left rfl, rw [div_top, nonpos_iff_eq_zero, mul_top], rcases eq_or_ne a 0 with rfl|ha; simp * }, rcases eq_or_ne b 0 with (rfl | hb), { have hc : c ≠ 0, from h0.neg_resolve_left rfl, simp [div_zero hc] }, { rw [← coe_ne_zero] at hb, rw [← ennreal.mul_le_mul_right hb coe_ne_top, div_mul_cancel hb coe_ne_top] }, end lemma div_le_iff_le_mul (hb0 : b ≠ 0 ∨ c ≠ ∞) (hbt : b ≠ ∞ ∨ c ≠ 0) : a / b ≤ c ↔ a ≤ c * b := begin suffices : a * b⁻¹ ≤ c ↔ a ≤ c / b⁻¹, by simpa [div_eq_mul_inv], refine (le_div_iff_mul_le _ _).symm; simpa end lemma lt_div_iff_mul_lt (hb0 : b ≠ 0 ∨ c ≠ ∞) (hbt : b ≠ ∞ ∨ c ≠ 0) : c < a / b ↔ c * b < a := lt_iff_lt_of_le_iff_le (div_le_iff_le_mul hb0 hbt) lemma div_le_of_le_mul (h : a ≤ b * c) : a / c ≤ b := begin by_cases h0 : c = 0, { have : a = 0, by simpa [h0] using h, simp [*] }, by_cases hinf : c = ∞, by simp [hinf], exact (div_le_iff_le_mul (or.inl h0) (or.inl hinf)).2 h end lemma div_le_of_le_mul' (h : a ≤ b * c) : a / b ≤ c := div_le_of_le_mul $ mul_comm b c ▸ h lemma mul_le_of_le_div (h : a ≤ b / c) : a * c ≤ b := begin rw [← inv_inv c], exact div_le_of_le_mul h, end lemma mul_le_of_le_div' (h : a ≤ b / c) : c * a ≤ b := mul_comm a c ▸ mul_le_of_le_div h protected lemma div_lt_iff (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ∞ ∨ c ≠ ∞) : c / b < a ↔ c < a * b := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le h0 ht lemma mul_lt_of_lt_div (h : a < b / c) : a * c < b := by { contrapose! h, exact ennreal.div_le_of_le_mul h } lemma mul_lt_of_lt_div' (h : a < b / c) : c * a < b := mul_comm a c ▸ mul_lt_of_lt_div h lemma inv_le_iff_le_mul (h₁ : b = ∞ → a ≠ 0) (h₂ : a = ∞ → b ≠ 0) : a⁻¹ ≤ b ↔ 1 ≤ a * b := begin rw [← one_div, div_le_iff_le_mul, mul_comm], exacts [or_not_of_imp h₁, not_or_of_imp h₂] end @[simp] lemma le_inv_iff_mul_le : a ≤ b⁻¹ ↔ a * b ≤ 1 := by rw [← one_div, le_div_iff_mul_le]; { right, simp } lemma div_le_div {a b c d : ℝ≥0∞} (hab : a ≤ b) (hdc : d ≤ c) : a / c ≤ b / d := div_eq_mul_inv b d ▸ div_eq_mul_inv a c ▸ ennreal.mul_le_mul hab (ennreal.inv_le_inv.mpr hdc) lemma eq_inv_of_mul_eq_one_left (h : a * b = 1) : a = b⁻¹ := begin have hb : b ≠ ∞, { rintro rfl, simpa [left_ne_zero_of_mul_eq_one h] using h }, rw [← mul_one a, ← mul_inv_cancel (right_ne_zero_of_mul_eq_one h) hb, ← mul_assoc, h, one_mul] end lemma mul_le_iff_le_inv {a b r : ℝ≥0∞} (hr₀ : r ≠ 0) (hr₁ : r ≠ ∞) : (r * a ≤ b ↔ a ≤ r⁻¹ * b) := by rw [← @ennreal.mul_le_mul_left _ a _ hr₀ hr₁, ← mul_assoc, mul_inv_cancel hr₀ hr₁, one_mul] lemma le_of_forall_nnreal_lt {x y : ℝ≥0∞} (h : ∀ r : ℝ≥0, ↑r < x → ↑r ≤ y) : x ≤ y := begin refine le_of_forall_ge_of_dense (λ r hr, _), lift r to ℝ≥0 using ne_top_of_lt hr, exact h r hr end lemma le_of_forall_pos_nnreal_lt {x y : ℝ≥0∞} (h : ∀ r : ℝ≥0, 0 < r → ↑r < x → ↑r ≤ y) : x ≤ y := le_of_forall_nnreal_lt $ λ r hr, (zero_le r).eq_or_lt.elim (λ h, h ▸ zero_le _) (λ h0, h r h0 hr) lemma eq_top_of_forall_nnreal_le {x : ℝ≥0∞} (h : ∀ r : ℝ≥0, ↑r ≤ x) : x = ∞ := top_unique $ le_of_forall_nnreal_lt $ λ r hr, h r lemma add_div : (a + b) / c = a / c + b / c := right_distrib a b (c⁻¹) lemma div_add_div_same {a b c : ℝ≥0∞} : a / c + b / c = (a + b) / c := add_div.symm lemma div_self (h0 : a ≠ 0) (hI : a ≠ ∞) : a / a = 1 := mul_inv_cancel h0 hI lemma mul_div_le : a * (b / a) ≤ b := mul_le_of_le_div' le_rfl -- TODO: add this lemma for an `is_unit` in any `division_monoid` lemma eq_div_iff (ha : a ≠ 0) (ha' : a ≠ ∞) : b = c / a ↔ a * b = c := ⟨λ h, by rw [h, mul_div_cancel' ha ha'], λ h, by rw [← h, mul_div_assoc, mul_div_cancel' ha ha']⟩ lemma div_eq_div_iff (ha : a ≠ 0) (ha' : a ≠ ∞) (hb : b ≠ 0) (hb' : b ≠ ∞) : c / b = d / a ↔ a * c = b * d := begin rw eq_div_iff ha ha', conv_rhs { rw eq_comm }, rw [← eq_div_iff hb hb', mul_div_assoc, eq_comm], end lemma inv_two_add_inv_two : (2:ℝ≥0∞)⁻¹ + 2⁻¹ = 1 := by rw [← two_mul, ← div_eq_mul_inv, div_self two_ne_zero two_ne_top] lemma inv_three_add_inv_three : (3 : ℝ≥0∞)⁻¹ + 3⁻¹ + 3⁻¹ = 1 := begin rw [show (3 : ℝ≥0∞)⁻¹ + 3⁻¹ + 3⁻¹ = 3 * 3⁻¹, by ring, ← div_eq_mul_inv, ennreal.div_self]; simp, end @[simp] lemma add_halves (a : ℝ≥0∞) : a / 2 + a / 2 = a := by rw [div_eq_mul_inv, ← mul_add, inv_two_add_inv_two, mul_one] @[simp] lemma add_thirds (a : ℝ≥0∞) : a / 3 + a / 3 + a / 3 = a := by rw [div_eq_mul_inv, ← mul_add, ← mul_add, inv_three_add_inv_three, mul_one] @[simp] lemma div_zero_iff : a / b = 0 ↔ a = 0 ∨ b = ∞ := by simp [div_eq_mul_inv] @[simp] lemma div_pos_iff : 0 < a / b ↔ a ≠ 0 ∧ b ≠ ∞ := by simp [pos_iff_ne_zero, not_or_distrib] lemma half_pos {a : ℝ≥0∞} (h : a ≠ 0) : 0 < a / 2 := by simp [h] lemma one_half_lt_one : (2⁻¹:ℝ≥0∞) < 1 := inv_lt_one.2 $ one_lt_two lemma half_lt_self {a : ℝ≥0∞} (hz : a ≠ 0) (ht : a ≠ ∞) : a / 2 < a := begin lift a to ℝ≥0 using ht, rw coe_ne_zero at hz, rw [← coe_two, ← coe_div, coe_lt_coe], exacts [nnreal.half_lt_self hz, two_ne_zero'] end lemma half_le_self : a / 2 ≤ a := le_add_self.trans_eq (add_halves _) lemma sub_half (h : a ≠ ∞) : a - a / 2 = a / 2 := begin lift a to ℝ≥0 using h, exact sub_eq_of_add_eq (mul_ne_top coe_ne_top $ by simp) (add_halves a) end @[simp] lemma one_sub_inv_two : (1:ℝ≥0∞) - 2⁻¹ = 2⁻¹ := by simpa only [div_eq_mul_inv, one_mul] using sub_half one_ne_top /-- The birational order isomorphism between `ℝ≥0∞` and the unit interval `set.Iic (1 : ℝ≥0∞)`. -/ @[simps apply_coe] def order_iso_Iic_one_birational : ℝ≥0∞ ≃o Iic (1 : ℝ≥0∞) := begin refine strict_mono.order_iso_of_right_inverse (λ x, ⟨(x⁻¹ + 1)⁻¹, inv_le_one.2 $ le_add_self⟩) (λ x y hxy, _) (λ x, (x⁻¹ - 1)⁻¹) (λ x, subtype.ext _), { simpa only [subtype.mk_lt_mk, inv_lt_inv, ennreal.add_lt_add_iff_right one_ne_top] }, { have : (1 : ℝ≥0∞) ≤ x⁻¹, from one_le_inv.2 x.2, simp only [inv_inv, subtype.coe_mk, tsub_add_cancel_of_le this] } end @[simp] lemma order_iso_Iic_one_birational_symm_apply (x : Iic (1 : ℝ≥0∞)) : order_iso_Iic_one_birational.symm x = (x⁻¹ - 1)⁻¹ := rfl /-- Order isomorphism between an initial interval in `ℝ≥0∞` and an initial interval in `ℝ≥0`. -/ @[simps apply_coe] def order_iso_Iic_coe (a : ℝ≥0) : Iic (a : ℝ≥0∞) ≃o Iic a := order_iso.symm { to_fun := λ x, ⟨x, coe_le_coe.2 x.2⟩, inv_fun := λ x, ⟨ennreal.to_nnreal x, coe_le_coe.1 $ coe_to_nnreal_le_self.trans x.2⟩, left_inv := λ x, subtype.ext $ to_nnreal_coe, right_inv := λ x, subtype.ext $ coe_to_nnreal (ne_top_of_le_ne_top coe_ne_top x.2), map_rel_iff' := λ x y, by simp only [equiv.coe_fn_mk, subtype.mk_le_mk, coe_coe, coe_le_coe, subtype.coe_le_coe] } @[simp] lemma order_iso_Iic_coe_symm_apply_coe (a : ℝ≥0) (b : Iic a) : ((order_iso_Iic_coe a).symm b : ℝ≥0∞) = b := rfl /-- An order isomorphism between the extended nonnegative real numbers and the unit interval. -/ def order_iso_unit_interval_birational : ℝ≥0∞ ≃o Icc (0 : ℝ) 1 := order_iso_Iic_one_birational.trans $ (order_iso_Iic_coe 1).trans $ (nnreal.order_iso_Icc_zero_coe 1).symm @[simp] lemma order_iso_unit_interval_birational_apply_coe (x : ℝ≥0∞) : (order_iso_unit_interval_birational x : ℝ) = (x⁻¹ + 1)⁻¹.to_real := rfl lemma exists_inv_nat_lt {a : ℝ≥0∞} (h : a ≠ 0) : ∃n:ℕ, (n:ℝ≥0∞)⁻¹ < a := inv_inv a ▸ by simp only [inv_lt_inv, ennreal.exists_nat_gt (inv_ne_top.2 h)] lemma exists_nat_pos_mul_gt (ha : a ≠ 0) (hb : b ≠ ∞) : ∃ n > 0, b < (n : ℕ) * a := begin have : b / a ≠ ∞, from mul_ne_top hb (inv_ne_top.2 ha), refine (ennreal.exists_nat_gt this).imp (λ n hn, _), have : ↑0 < (n : ℝ≥0∞), from lt_of_le_of_lt (by simp) hn, refine ⟨coe_nat_lt_coe_nat.1 this, _⟩, rwa [← ennreal.div_lt_iff (or.inl ha) (or.inr hb)] end lemma exists_nat_mul_gt (ha : a ≠ 0) (hb : b ≠ ∞) : ∃ n : ℕ, b < n * a := (exists_nat_pos_mul_gt ha hb).imp $ λ n, Exists.snd lemma exists_nat_pos_inv_mul_lt (ha : a ≠ ∞) (hb : b ≠ 0) : ∃ n > 0, ((n : ℕ) : ℝ≥0∞)⁻¹ * a < b := begin rcases exists_nat_pos_mul_gt hb ha with ⟨n, npos, hn⟩, have : (n : ℝ≥0∞) ≠ 0 := nat.cast_ne_zero.2 npos.lt.ne', use [n, npos], rwa [← one_mul b, ← inv_mul_cancel this (nat_ne_top n), mul_assoc, mul_lt_mul_left (inv_ne_zero.2 $ nat_ne_top _) (inv_ne_top.2 this)] end lemma exists_nnreal_pos_mul_lt (ha : a ≠ ∞) (hb : b ≠ 0) : ∃ n > 0, ↑(n : ℝ≥0) * a < b := begin rcases exists_nat_pos_inv_mul_lt ha hb with ⟨n, npos : 0 < n, hn⟩, use (n : ℝ≥0)⁻¹, simp [*, npos.ne', zero_lt_one] end lemma exists_inv_two_pow_lt (ha : a ≠ 0) : ∃ n : ℕ, 2⁻¹ ^ n < a := begin rcases exists_inv_nat_lt ha with ⟨n, hn⟩, refine ⟨n, lt_trans _ hn⟩, rw [← ennreal.inv_pow, inv_lt_inv], norm_cast, exact n.lt_two_pow end @[simp, norm_cast] lemma coe_zpow (hr : r ≠ 0) (n : ℤ) : (↑(r^n) : ℝ≥0∞) = r^n := begin cases n, { simp only [int.of_nat_eq_coe, coe_pow, zpow_coe_nat] }, { have : r ^ n.succ ≠ 0 := pow_ne_zero (n+1) hr, simp only [zpow_neg_succ_of_nat, coe_inv this, coe_pow] } end lemma zpow_pos (ha : a ≠ 0) (h'a : a ≠ ∞) (n : ℤ) : 0 < a ^ n := begin cases n, { exact ennreal.pow_pos ha.bot_lt n }, { simp only [h'a, pow_eq_top_iff, zpow_neg_succ_of_nat, ne.def, not_false_iff, inv_pos, false_and] } end lemma zpow_lt_top (ha : a ≠ 0) (h'a : a ≠ ∞) (n : ℤ) : a ^ n < ∞ := begin cases n, { exact ennreal.pow_lt_top h'a.lt_top _ }, { simp only [ennreal.pow_pos ha.bot_lt (n + 1), zpow_neg_succ_of_nat, inv_lt_top] } end lemma exists_mem_Ico_zpow {x y : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (hy : 1 < y) (h'y : y ≠ ⊤) : ∃ n : ℤ, x ∈ Ico (y ^ n) (y ^ (n + 1)) := begin lift x to ℝ≥0 using h'x, lift y to ℝ≥0 using h'y, have A : y ≠ 0, { simpa only [ne.def, coe_eq_zero] using (ennreal.zero_lt_one.trans hy).ne' }, obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, y ^ n ≤ x ∧ x < y ^ (n + 1), { refine nnreal.exists_mem_Ico_zpow _ (one_lt_coe_iff.1 hy), simpa only [ne.def, coe_eq_zero] using hx }, refine ⟨n, _, _⟩, { rwa [← ennreal.coe_zpow A, ennreal.coe_le_coe] }, { rwa [← ennreal.coe_zpow A, ennreal.coe_lt_coe] } end lemma exists_mem_Ioc_zpow {x y : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (hy : 1 < y) (h'y : y ≠ ⊤) : ∃ n : ℤ, x ∈ Ioc (y ^ n) (y ^ (n + 1)) := begin lift x to ℝ≥0 using h'x, lift y to ℝ≥0 using h'y, have A : y ≠ 0, { simpa only [ne.def, coe_eq_zero] using (ennreal.zero_lt_one.trans hy).ne' }, obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, y ^ n < x ∧ x ≤ y ^ (n + 1), { refine nnreal.exists_mem_Ioc_zpow _ (one_lt_coe_iff.1 hy), simpa only [ne.def, coe_eq_zero] using hx }, refine ⟨n, _, _⟩, { rwa [← ennreal.coe_zpow A, ennreal.coe_lt_coe] }, { rwa [← ennreal.coe_zpow A, ennreal.coe_le_coe] } end lemma Ioo_zero_top_eq_Union_Ico_zpow {y : ℝ≥0∞} (hy : 1 < y) (h'y : y ≠ ⊤) : Ioo (0 : ℝ≥0∞) (∞ : ℝ≥0∞) = ⋃ (n : ℤ), Ico (y^n) (y^(n+1)) := begin ext x, simp only [mem_Union, mem_Ioo, mem_Ico], split, { rintros ⟨hx, h'x⟩, exact exists_mem_Ico_zpow hx.ne' h'x.ne hy h'y }, { rintros ⟨n, hn, h'n⟩, split, { apply lt_of_lt_of_le _ hn, exact ennreal.zpow_pos (ennreal.zero_lt_one.trans hy).ne' h'y _ }, { apply lt_trans h'n _, exact ennreal.zpow_lt_top (ennreal.zero_lt_one.trans hy).ne' h'y _ } } end lemma zpow_le_of_le {x : ℝ≥0∞} (hx : 1 ≤ x) {a b : ℤ} (h : a ≤ b) : x ^ a ≤ x ^ b := begin induction a with a a; induction b with b b, { simp only [int.of_nat_eq_coe, zpow_coe_nat], exact pow_le_pow hx (int.le_of_coe_nat_le_coe_nat h), }, { apply absurd h (not_le_of_gt _), exact lt_of_lt_of_le (int.neg_succ_lt_zero _) (int.of_nat_nonneg _) }, { simp only [zpow_neg_succ_of_nat, int.of_nat_eq_coe, zpow_coe_nat], refine le_trans (inv_le_one.2 _) _; exact ennreal.one_le_pow_of_one_le hx _, }, { simp only [zpow_neg_succ_of_nat, inv_le_inv], apply pow_le_pow hx, simpa only [←int.coe_nat_le_coe_nat_iff, neg_le_neg_iff, int.coe_nat_add, int.coe_nat_one, int.neg_succ_of_nat_eq] using h } end lemma monotone_zpow {x : ℝ≥0∞} (hx : 1 ≤ x) : monotone ((^) x : ℤ → ℝ≥0∞) := λ a b h, zpow_le_of_le hx h lemma zpow_add {x : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (m n : ℤ) : x ^ (m + n) = x ^ m * x ^ n := begin lift x to ℝ≥0 using h'x, replace hx : x ≠ 0, by simpa only [ne.def, coe_eq_zero] using hx, simp only [← coe_zpow hx, zpow_add₀ hx, coe_mul] end end inv section real lemma to_real_add (ha : a ≠ ∞) (hb : b ≠ ∞) : (a+b).to_real = a.to_real + b.to_real := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, refl end lemma to_real_sub_of_le {a b : ℝ≥0∞} (h : b ≤ a) (ha : a ≠ ∞): (a - b).to_real = a.to_real - b.to_real := begin lift b to ℝ≥0 using ne_top_of_le_ne_top ha h, lift a to ℝ≥0 using ha, simp only [← ennreal.coe_sub, ennreal.coe_to_real, nnreal.coe_sub (ennreal.coe_le_coe.mp h)], end lemma le_to_real_sub {a b : ℝ≥0∞} (hb : b ≠ ∞) : a.to_real - b.to_real ≤ (a - b).to_real := begin lift b to ℝ≥0 using hb, induction a using with_top.rec_top_coe, { simp }, { simp only [←coe_sub, nnreal.sub_def, real.coe_to_nnreal', coe_to_real], exact le_max_left _ _ } end lemma to_real_add_le : (a+b).to_real ≤ a.to_real + b.to_real := if ha : a = ∞ then by simp only [ha, top_add, top_to_real, zero_add, to_real_nonneg] else if hb : b = ∞ then by simp only [hb, add_top, top_to_real, add_zero, to_real_nonneg] else le_of_eq (to_real_add ha hb) lemma of_real_add {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) : ennreal.of_real (p + q) = ennreal.of_real p + ennreal.of_real q := by rw [ennreal.of_real, ennreal.of_real, ennreal.of_real, ← coe_add, coe_eq_coe, real.to_nnreal_add hp hq] lemma of_real_add_le {p q : ℝ} : ennreal.of_real (p + q) ≤ ennreal.of_real p + ennreal.of_real q := coe_le_coe.2 real.to_nnreal_add_le @[simp] lemma to_real_le_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_real ≤ b.to_real ↔ a ≤ b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, norm_cast end lemma to_real_mono (hb : b ≠ ∞) (h : a ≤ b) : a.to_real ≤ b.to_real := (to_real_le_to_real (h.trans_lt (lt_top_iff_ne_top.2 hb)).ne hb).2 h @[simp] lemma to_real_lt_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_real < b.to_real ↔ a < b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, norm_cast end lemma to_real_strict_mono (hb : b ≠ ∞) (h : a < b) : a.to_real < b.to_real := (to_real_lt_to_real (h.trans (lt_top_iff_ne_top.2 hb)).ne hb).2 h lemma to_nnreal_mono (hb : b ≠ ∞) (h : a ≤ b) : a.to_nnreal ≤ b.to_nnreal := by simpa [←ennreal.coe_le_coe, hb, (h.trans_lt hb.lt_top).ne] @[simp] lemma to_nnreal_le_to_nnreal (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_nnreal ≤ b.to_nnreal ↔ a ≤ b := ⟨λ h, by rwa [←coe_to_nnreal ha, ←coe_to_nnreal hb, coe_le_coe], to_nnreal_mono hb⟩ lemma to_nnreal_strict_mono (hb : b ≠ ∞) (h : a < b) : a.to_nnreal < b.to_nnreal := by simpa [←ennreal.coe_lt_coe, hb, (h.trans hb.lt_top).ne] @[simp] lemma to_nnreal_lt_to_nnreal (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_nnreal < b.to_nnreal ↔ a < b := ⟨λ h, by rwa [←coe_to_nnreal ha, ←coe_to_nnreal hb, coe_lt_coe], to_nnreal_strict_mono hb⟩ lemma to_real_max (hr : a ≠ ∞) (hp : b ≠ ∞) : ennreal.to_real (max a b) = max (ennreal.to_real a) (ennreal.to_real b) := (le_total a b).elim (λ h, by simp only [h, (ennreal.to_real_le_to_real hr hp).2 h, max_eq_right]) (λ h, by simp only [h, (ennreal.to_real_le_to_real hp hr).2 h, max_eq_left]) lemma to_nnreal_pos_iff : 0 < a.to_nnreal ↔ (0 < a ∧ a < ∞) := by { induction a using with_top.rec_top_coe; simp } lemma to_nnreal_pos {a : ℝ≥0∞} (ha₀ : a ≠ 0) (ha_top : a ≠ ∞) : 0 < a.to_nnreal := to_nnreal_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr ha₀, lt_top_iff_ne_top.mpr ha_top⟩ lemma to_real_pos_iff : 0 < a.to_real ↔ (0 < a ∧ a < ∞):= (nnreal.coe_pos).trans to_nnreal_pos_iff lemma to_real_pos {a : ℝ≥0∞} (ha₀ : a ≠ 0) (ha_top : a ≠ ∞) : 0 < a.to_real := to_real_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr ha₀, lt_top_iff_ne_top.mpr ha_top⟩ lemma of_real_le_of_real {p q : ℝ} (h : p ≤ q) : ennreal.of_real p ≤ ennreal.of_real q := by simp [ennreal.of_real, real.to_nnreal_le_to_nnreal h] lemma of_real_le_of_le_to_real {a : ℝ} {b : ℝ≥0∞} (h : a ≤ ennreal.to_real b) : ennreal.of_real a ≤ b := (of_real_le_of_real h).trans of_real_to_real_le @[simp] lemma of_real_le_of_real_iff {p q : ℝ} (h : 0 ≤ q) : ennreal.of_real p ≤ ennreal.of_real q ↔ p ≤ q := by rw [ennreal.of_real, ennreal.of_real, coe_le_coe, real.to_nnreal_le_to_nnreal_iff h] @[simp] lemma of_real_lt_of_real_iff {p q : ℝ} (h : 0 < q) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, real.to_nnreal_lt_to_nnreal_iff h] lemma of_real_lt_of_real_iff_of_nonneg {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, real.to_nnreal_lt_to_nnreal_iff_of_nonneg hp] @[simp] lemma of_real_pos {p : ℝ} : 0 < ennreal.of_real p ↔ 0 < p := by simp [ennreal.of_real] @[simp] lemma of_real_eq_zero {p : ℝ} : ennreal.of_real p = 0 ↔ p ≤ 0 := by simp [ennreal.of_real] @[simp] lemma zero_eq_of_real {p : ℝ} : 0 = ennreal.of_real p ↔ p ≤ 0 := eq_comm.trans of_real_eq_zero alias of_real_eq_zero ↔ _ of_real_of_nonpos lemma of_real_sub (p : ℝ) (hq : 0 ≤ q) : ennreal.of_real (p - q) = ennreal.of_real p - ennreal.of_real q := begin obtain h | h := le_total p q, { rw [of_real_of_nonpos (sub_nonpos_of_le h), tsub_eq_zero_of_le (of_real_le_of_real h)] }, refine ennreal.eq_sub_of_add_eq of_real_ne_top _, rw [←of_real_add (sub_nonneg_of_le h) hq, sub_add_cancel], end lemma of_real_le_iff_le_to_real {a : ℝ} {b : ℝ≥0∞} (hb : b ≠ ∞) : ennreal.of_real a ≤ b ↔ a ≤ ennreal.to_real b := begin lift b to ℝ≥0 using hb, simpa [ennreal.of_real, ennreal.to_real] using real.to_nnreal_le_iff_le_coe end lemma of_real_lt_iff_lt_to_real {a : ℝ} {b : ℝ≥0∞} (ha : 0 ≤ a) (hb : b ≠ ∞) : ennreal.of_real a < b ↔ a < ennreal.to_real b := begin lift b to ℝ≥0 using hb, simpa [ennreal.of_real, ennreal.to_real] using real.to_nnreal_lt_iff_lt_coe ha end lemma le_of_real_iff_to_real_le {a : ℝ≥0∞} {b : ℝ} (ha : a ≠ ∞) (hb : 0 ≤ b) : a ≤ ennreal.of_real b ↔ ennreal.to_real a ≤ b := begin lift a to ℝ≥0 using ha, simpa [ennreal.of_real, ennreal.to_real] using real.le_to_nnreal_iff_coe_le hb end lemma to_real_le_of_le_of_real {a : ℝ≥0∞} {b : ℝ} (hb : 0 ≤ b) (h : a ≤ ennreal.of_real b) : ennreal.to_real a ≤ b := have ha : a ≠ ∞, from ne_top_of_le_ne_top of_real_ne_top h, (le_of_real_iff_to_real_le ha hb).1 h lemma lt_of_real_iff_to_real_lt {a : ℝ≥0∞} {b : ℝ} (ha : a ≠ ∞) : a < ennreal.of_real b ↔ ennreal.to_real a < b := begin lift a to ℝ≥0 using ha, simpa [ennreal.of_real, ennreal.to_real] using real.lt_to_nnreal_iff_coe_lt end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real (p * q) = ennreal.of_real p * ennreal.of_real q := by simp only [ennreal.of_real, ← coe_mul, real.to_nnreal_mul hp] lemma of_real_mul' {p q : ℝ} (hq : 0 ≤ q) : ennreal.of_real (p * q) = ennreal.of_real p * ennreal.of_real q := by rw [mul_comm, of_real_mul hq, mul_comm] lemma of_real_pow {p : ℝ} (hp : 0 ≤ p) (n : ℕ) : ennreal.of_real (p ^ n) = ennreal.of_real p ^ n := by rw [of_real_eq_coe_nnreal hp, ← coe_pow, ← of_real_coe_nnreal, nnreal.coe_pow, nnreal.coe_mk] lemma of_real_inv_of_pos {x : ℝ} (hx : 0 < x) : (ennreal.of_real x)⁻¹ = ennreal.of_real x⁻¹ := by rw [ennreal.of_real, ennreal.of_real, ←@coe_inv (real.to_nnreal x) (by simp [hx]), coe_eq_coe, real.to_nnreal_inv.symm] lemma of_real_div_of_pos {x y : ℝ} (hy : 0 < y) : ennreal.of_real (x / y) = ennreal.of_real x / ennreal.of_real y := by rw [div_eq_mul_inv, div_eq_mul_inv, of_real_mul' (inv_nonneg.2 hy.le), of_real_inv_of_pos hy] @[simp] lemma to_nnreal_mul {a b : ℝ≥0∞} : (a * b).to_nnreal = a.to_nnreal * b.to_nnreal := with_top.untop'_zero_mul a b lemma to_nnreal_mul_top (a : ℝ≥0∞) : ennreal.to_nnreal (a * ∞) = 0 := by simp lemma to_nnreal_top_mul (a : ℝ≥0∞) : ennreal.to_nnreal (∞ * a) = 0 := by simp @[simp] lemma smul_to_nnreal (a : ℝ≥0) (b : ℝ≥0∞) : (a • b).to_nnreal = a * b.to_nnreal := begin change ((a : ℝ≥0∞) * b).to_nnreal = a * b.to_nnreal, simp only [ennreal.to_nnreal_mul, ennreal.to_nnreal_coe], end /-- `ennreal.to_nnreal` as a `monoid_hom`. -/ def to_nnreal_hom : ℝ≥0∞ →* ℝ≥0 := { to_fun := ennreal.to_nnreal, map_one' := to_nnreal_coe, map_mul' := λ _ _, to_nnreal_mul } @[simp] lemma to_nnreal_pow (a : ℝ≥0∞) (n : ℕ) : (a ^ n).to_nnreal = a.to_nnreal ^ n := to_nnreal_hom.map_pow a n @[simp] lemma to_nnreal_prod {ι : Type*} {s : finset ι} {f : ι → ℝ≥0∞} : (∏ i in s, f i).to_nnreal = ∏ i in s, (f i).to_nnreal := to_nnreal_hom.map_prod _ _ /-- `ennreal.to_real` as a `monoid_hom`. -/ def to_real_hom : ℝ≥0∞ →* ℝ := (nnreal.to_real_hom : ℝ≥0 →* ℝ).comp to_nnreal_hom @[simp] lemma to_real_mul : (a * b).to_real = a.to_real * b.to_real := to_real_hom.map_mul a b @[simp] lemma to_real_pow (a : ℝ≥0∞) (n : ℕ) : (a ^ n).to_real = a.to_real ^ n := to_real_hom.map_pow a n @[simp] lemma to_real_prod {ι : Type*} {s : finset ι} {f : ι → ℝ≥0∞} : (∏ i in s, f i).to_real = ∏ i in s, (f i).to_real := to_real_hom.map_prod _ _ lemma to_real_of_real_mul (c : ℝ) (a : ℝ≥0∞) (h : 0 ≤ c) : ennreal.to_real ((ennreal.of_real c) * a) = c * ennreal.to_real a := by rw [ennreal.to_real_mul, ennreal.to_real_of_real h] lemma to_real_mul_top (a : ℝ≥0∞) : ennreal.to_real (a * ∞) = 0 := by rw [to_real_mul, top_to_real, mul_zero] lemma to_real_top_mul (a : ℝ≥0∞) : ennreal.to_real (∞ * a) = 0 := by { rw mul_comm, exact to_real_mul_top _ } lemma to_real_eq_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : ennreal.to_real a = ennreal.to_real b ↔ a = b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, simp only [coe_eq_coe, nnreal.coe_eq, coe_to_real], end lemma to_real_smul (r : ℝ≥0) (s : ℝ≥0∞) : (r • s).to_real = r • s.to_real := by { rw [ennreal.smul_def, smul_eq_mul, to_real_mul, coe_to_real], refl } protected lemma trichotomy (p : ℝ≥0∞) : p = 0 ∨ p = ∞ ∨ 0 < p.to_real := by simpa only [or_iff_not_imp_left] using to_real_pos protected lemma trichotomy₂ {p q : ℝ≥0∞} (hpq : p ≤ q) : (p = 0 ∧ q = 0) ∨ (p = 0 ∧ q = ∞) ∨ (p = 0 ∧ 0 < q.to_real) ∨ (p = ∞ ∧ q = ∞) ∨ (0 < p.to_real ∧ q = ∞) ∨ (0 < p.to_real ∧ 0 < q.to_real ∧ p.to_real ≤ q.to_real) := begin rcases eq_or_lt_of_le (bot_le : 0 ≤ p) with (rfl : 0 = p) | (hp : 0 < p), { simpa using q.trichotomy }, rcases eq_or_lt_of_le (le_top : q ≤ ∞) with rfl | hq, { simpa using p.trichotomy }, repeat { right }, have hq' : 0 < q := lt_of_lt_of_le hp hpq, have hp' : p < ∞ := lt_of_le_of_lt hpq hq, simp [ennreal.to_real_le_to_real hp'.ne hq.ne, ennreal.to_real_pos_iff, hpq, hp, hp', hq', hq], end protected lemma dichotomy (p : ℝ≥0∞) [fact (1 ≤ p)] : p = ∞ ∨ 1 ≤ p.to_real := begin have : p = ⊤ ∨ 0 < p.to_real ∧ 1 ≤ p.to_real, { simpa using ennreal.trichotomy₂ (fact.out _ : 1 ≤ p) }, exact this.imp_right (λ h, h.2) end lemma to_real_pos_iff_ne_top (p : ℝ≥0∞) [fact (1 ≤ p)] : 0 < p.to_real ↔ p ≠ ∞ := ⟨λ h hp, let this : (0 : ℝ) ≠ 0 := top_to_real ▸ (hp ▸ h.ne : 0 ≠ ∞.to_real) in this rfl, λ h, zero_lt_one.trans_le (p.dichotomy.resolve_left h)⟩ lemma to_nnreal_inv (a : ℝ≥0∞) : (a⁻¹).to_nnreal = (a.to_nnreal)⁻¹ := begin induction a using with_top.rec_top_coe, { simp }, rcases eq_or_ne a 0 with rfl|ha, { simp }, rw [← coe_inv ha, to_nnreal_coe, to_nnreal_coe] end lemma to_nnreal_div (a b : ℝ≥0∞) : (a / b).to_nnreal = a.to_nnreal / b.to_nnreal := by rw [div_eq_mul_inv, to_nnreal_mul, to_nnreal_inv, div_eq_mul_inv] lemma to_real_inv (a : ℝ≥0∞) : (a⁻¹).to_real = (a.to_real)⁻¹ := by { simp_rw ennreal.to_real, norm_cast, exact to_nnreal_inv a, } lemma to_real_div (a b : ℝ≥0∞) : (a / b).to_real = a.to_real / b.to_real := by rw [div_eq_mul_inv, to_real_mul, to_real_inv, div_eq_mul_inv] lemma of_real_prod_of_nonneg {s : finset α} {f : α → ℝ} (hf : ∀ i, i ∈ s → 0 ≤ f i) : ennreal.of_real (∏ i in s, f i) = ∏ i in s, ennreal.of_real (f i) := begin simp_rw [ennreal.of_real, ←coe_finset_prod, coe_eq_coe], exact real.to_nnreal_prod_of_nonneg hf, end @[simp] lemma to_nnreal_bit0 {x : ℝ≥0∞} : (bit0 x).to_nnreal = bit0 (x.to_nnreal) := begin induction x using with_top.rec_top_coe, { simp }, { exact to_nnreal_add coe_ne_top coe_ne_top } end @[simp] lemma to_nnreal_bit1 {x : ℝ≥0∞} (hx_top : x ≠ ∞) : (bit1 x).to_nnreal = bit1 (x.to_nnreal) := by simp [bit1, bit1, to_nnreal_add (by rwa [ne.def, bit0_eq_top_iff]) ennreal.one_ne_top] @[simp] lemma to_real_bit0 {x : ℝ≥0∞} : (bit0 x).to_real = bit0 (x.to_real) := by simp [ennreal.to_real] @[simp] lemma to_real_bit1 {x : ℝ≥0∞} (hx_top : x ≠ ∞) : (bit1 x).to_real = bit1 (x.to_real) := by simp [ennreal.to_real, hx_top] @[simp] lemma of_real_bit0 (r : ℝ) : ennreal.of_real (bit0 r) = bit0 (ennreal.of_real r) := by simp [ennreal.of_real] @[simp] lemma of_real_bit1 {r : ℝ} (hr : 0 ≤ r) : ennreal.of_real (bit1 r) = bit1 (ennreal.of_real r) := (of_real_add (by simp [hr]) zero_le_one).trans (by simp [real.to_nnreal_one, bit1]) end real section infi variables {ι : Sort*} {f g : ι → ℝ≥0∞} lemma infi_add : infi f + a = ⨅i, f i + a := le_antisymm (le_infi $ assume i, add_le_add (infi_le _ _) $ le_rfl) (tsub_le_iff_right.1 $ le_infi $ assume i, tsub_le_iff_right.2 $ infi_le _ _) lemma supr_sub : (⨆i, f i) - a = (⨆i, f i - a) := le_antisymm (tsub_le_iff_right.2 $ supr_le $ assume i, tsub_le_iff_right.1 $ le_supr _ i) (supr_le $ assume i, tsub_le_tsub (le_supr _ _) (le_refl a)) lemma sub_infi : a - (⨅i, f i) = (⨆i, a - f i) := begin refine (eq_of_forall_ge_iff $ λ c, _), rw [tsub_le_iff_right, add_comm, infi_add], simp [tsub_le_iff_right, sub_eq_add_neg, add_comm], end lemma Inf_add {s : set ℝ≥0∞} : Inf s + a = ⨅b∈s, b + a := by simp [Inf_eq_infi, infi_add] lemma add_infi {a : ℝ≥0∞} : a + infi f = ⨅b, a + f b := by rw [add_comm, infi_add]; simp [add_comm] lemma infi_add_infi (h : ∀i j, ∃k, f k + g k ≤ f i + g j) : infi f + infi g = (⨅a, f a + g a) := suffices (⨅a, f a + g a) ≤ infi f + infi g, from le_antisymm (le_infi $ assume a, add_le_add (infi_le _ _) (infi_le _ _)) this, calc (⨅a, f a + g a) ≤ (⨅ a a', f a + g a') : le_infi $ assume a, le_infi $ assume a', let ⟨k, h⟩ := h a a' in infi_le_of_le k h ... = infi f + infi g : by simp [add_infi, infi_add] lemma infi_sum {f : ι → α → ℝ≥0∞} {s : finset α} [nonempty ι] (h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) : (⨅i, ∑ a in s, f i a) = ∑ a in s, ⨅i, f i a := begin induction s using finset.induction_on with a s ha ih, { simp }, have : ∀ (i j : ι), ∃ (k : ι), f k a + ∑ b in s, f k b ≤ f i a + ∑ b in s, f j b, { intros i j, obtain ⟨k, hk⟩ := h (insert a s) i j, exact ⟨k, add_le_add (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum $ λ a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩ }, simp [ha, ih.symm, infi_add_infi this] end /-- If `x ≠ 0` and `x ≠ ∞`, then right multiplication by `x` maps infimum to infimum. See also `ennreal.infi_mul` that assumes `[nonempty ι]` but does not require `x ≠ 0`. -/ lemma infi_mul_of_ne {ι} {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h0 : x ≠ 0) (h : x ≠ ∞) : infi f * x = ⨅ i, f i * x := le_antisymm mul_right_mono.map_infi_le ((div_le_iff_le_mul (or.inl h0) $ or.inl h).mp $ le_infi $ λ i, (div_le_iff_le_mul (or.inl h0) $ or.inl h).mpr $ infi_le _ _) /-- If `x ≠ ∞`, then right multiplication by `x` maps infimum over a nonempty type to infimum. See also `ennreal.infi_mul_of_ne` that assumes `x ≠ 0` but does not require `[nonempty ι]`. -/ lemma infi_mul {ι} [nonempty ι] {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h : x ≠ ∞) : infi f * x = ⨅ i, f i * x := begin by_cases h0 : x = 0, { simp only [h0, mul_zero, infi_const] }, { exact infi_mul_of_ne h0 h } end /-- If `x ≠ ∞`, then left multiplication by `x` maps infimum over a nonempty type to infimum. See also `ennreal.mul_infi_of_ne` that assumes `x ≠ 0` but does not require `[nonempty ι]`. -/ lemma mul_infi {ι} [nonempty ι] {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h : x ≠ ∞) : x * infi f = ⨅ i, x * f i := by simpa only [mul_comm] using infi_mul h /-- If `x ≠ 0` and `x ≠ ∞`, then left multiplication by `x` maps infimum to infimum. See also `ennreal.mul_infi` that assumes `[nonempty ι]` but does not require `x ≠ 0`. -/ lemma mul_infi_of_ne {ι} {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h0 : x ≠ 0) (h : x ≠ ∞) : x * infi f = ⨅ i, x * f i := by simpa only [mul_comm] using infi_mul_of_ne h0 h /-! `supr_mul`, `mul_supr` and variants are in `topology.instances.ennreal`. -/ end infi section supr @[simp] lemma supr_eq_zero {ι : Sort*} {f : ι → ℝ≥0∞} : (⨆ i, f i) = 0 ↔ ∀ i, f i = 0 := supr_eq_bot @[simp] lemma supr_zero_eq_zero {ι : Sort*} : (⨆ i : ι, (0 : ℝ≥0∞)) = 0 := by simp lemma sup_eq_zero {a b : ℝ≥0∞} : a ⊔ b = 0 ↔ a = 0 ∧ b = 0 := sup_eq_bot_iff lemma supr_coe_nat : (⨆n:ℕ, (n : ℝ≥0∞)) = ∞ := (supr_eq_top _).2 $ assume b hb, ennreal.exists_nat_gt (lt_top_iff_ne_top.1 hb) end supr end ennreal namespace set namespace ord_connected variables {s : set ℝ} {t : set ℝ≥0} {u : set ℝ≥0∞} lemma preimage_coe_nnreal_ennreal (h : u.ord_connected) : (coe ⁻¹' u : set ℝ≥0).ord_connected := h.preimage_mono ennreal.coe_mono lemma image_coe_nnreal_ennreal (h : t.ord_connected) : (coe '' t : set ℝ≥0∞).ord_connected := begin refine ⟨ball_image_iff.2 $ λ x hx, ball_image_iff.2 $ λ y hy z hz, _⟩, rcases ennreal.le_coe_iff.1 hz.2 with ⟨z, rfl, hzy⟩, exact mem_image_of_mem _ (h.out hx hy ⟨ennreal.coe_le_coe.1 hz.1, ennreal.coe_le_coe.1 hz.2⟩) end lemma preimage_ennreal_of_real (h : u.ord_connected) : (ennreal.of_real ⁻¹' u).ord_connected := h.preimage_coe_nnreal_ennreal.preimage_real_to_nnreal lemma image_ennreal_of_real (h : s.ord_connected) : (ennreal.of_real '' s).ord_connected := by simpa only [image_image] using h.image_real_to_nnreal.image_coe_nnreal_ennreal end ord_connected end set
66de5757b74e01dae6fd8886a355f7442450510a
618003631150032a5676f229d13a079ac875ff77
/src/linear_algebra/finite_dimensional.lean
702b4c53dc5c66433b038860f4c0f691aaa1e0e1
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
16,908
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import linear_algebra.dimension import ring_theory.principal_ideal_domain /-! # Finite dimensional vector spaces Definition and basic properties of finite dimensional vector spaces, of their dimensions, and of linear maps on such spaces. ## Main definitions Assume `V` is a vector space over a field `K`. There are (at least) three equivalent definitions of finite-dimensionality of `V`: - it admits a finite basis. - it is finitely generated. - it is noetherian, i.e., every subspace is finitely generated. We introduce a typeclass `finite_dimensional K V` capturing this property. For ease of transfer of proof, it is defined using the third point of view, i.e., as `is_noetherian`. However, we prove that all these points of view are equivalent, with the following lemmas (in the namespace `finite_dimensional`): - `exists_is_basis_finite` states that a finite-dimensional vector space has a finite basis - `of_finite_basis` states that the existence of a finite basis implies finite-dimensionality - `iff_fg` states that the space is finite-dimensional if and only if it is finitely generated Also defined is `findim`, the dimension of a finite dimensional space, returning a `nat`, as opposed to `dim`, which returns a `cardinal`. When the space has infinite dimension, its `findim` is by convention set to `0`. Preservation of finite-dimensionality and formulas for the dimension are given for - submodules - quotients (for the dimension of a quotient, see `findim_quotient_add_findim`) - linear equivs, in `linear_equiv.finite_dimensional` and `linear_equiv.findim_eq` - image under a linear map (the rank-nullity formula is in `findim_range_add_findim_ker`) Basic properties of linear maps of a finite-dimensional vector space are given. Notably, the equivalence of injectivity and surjectivity is proved in `linear_map.injective_iff_surjective`, and the equivalence between left-inverse and right-inverse in `mul_eq_one_comm` and `comp_eq_id_comm`. ## Implementation notes Most results are deduced from the corresponding results for the general dimension (as a cardinal), in `dimension.lean`. Not all results have been ported yet. One of the characterizations of finite-dimensionality is in terms of finite generation. This property is currently defined only for submodules, so we express it through the fact that the maximal submodule (which, as a set, coincides with the whole space) is finitely generated. This is not very convenient to use, although there are some helper functions. However, this becomes very convenient when speaking of submodules which are finite-dimensional, as this notion coincides with the fact that the submodule is finitely generated (as a submodule of the whole space). This equivalence is proved in `submodule.fg_iff_finite_dimensional`. -/ universes u v v' w open_locale classical open vector_space cardinal submodule module function variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V] {V₂ : Type v'} [add_comm_group V₂] [vector_space K V₂] /-- `finite_dimensional` vector spaces are defined to be noetherian modules. Use `finite_dimensional.iff_fg` or `finite_dimensional.of_finite_basis` to prove finite dimension from a conventional definition. -/ @[reducible] def finite_dimensional (K V : Type*) [field K] [add_comm_group V] [vector_space K V] := is_noetherian K V namespace finite_dimensional open is_noetherian /-- A vector space is finite-dimensional if and only if its dimension (as a cardinal) is strictly less than the first infinite cardinal `omega`. -/ lemma finite_dimensional_iff_dim_lt_omega : finite_dimensional K V ↔ dim K V < omega.{v} := begin cases exists_is_basis K V with b hb, have := is_basis.mk_eq_dim hb, simp only [lift_id] at this, rw [← this, lt_omega_iff_fintype, ← @set.set_of_mem_eq _ b, ← subtype.val_range], split, { intro, resetI, convert finite_of_linear_independent hb.1, simp }, { assume hbfinite, refine @is_noetherian_of_linear_equiv K (⊤ : submodule K V) V _ _ _ _ _ (linear_equiv.of_top _ rfl) (id _), refine is_noetherian_of_fg_of_noetherian _ ⟨set.finite.to_finset hbfinite, _⟩, rw [set.finite.coe_to_finset, ← hb.2], refl } end /-- The dimension of a finite-dimensional vector space, as a cardinal, is strictly less than the first infinite cardinal `omega`. -/ lemma dim_lt_omega (K V : Type*) [field K] [add_comm_group V] [vector_space K V] : ∀ [finite_dimensional K V], dim K V < omega.{v} := finite_dimensional_iff_dim_lt_omega.1 /-- In a finite dimensional space, there exists a finite basis. A basis is in general given as a function from an arbitrary type to the vector space. Here, we think of a basis as a set (instead of a function), and use as parametrizing type this set (and as a function the function `subtype.val`). -/ variables (K V) lemma exists_is_basis_finite [finite_dimensional K V] : ∃ s : set V, (is_basis K (subtype.val : s → V)) ∧ s.finite := begin cases exists_is_basis K V with s hs, exact ⟨s, hs, finite_of_linear_independent hs.1⟩ end variables {K V} /-- A vector space is finite-dimensional if and only if it is finitely generated. As the finitely-generated property is a property of submodules, we formulate this in terms of the maximal submodule, equal to the whole space as a set by definition.-/ lemma iff_fg : finite_dimensional K V ↔ (⊤ : submodule K V).fg := begin split, { introI h, rcases exists_is_basis_finite K V with ⟨s, s_basis, s_finite⟩, exact ⟨s_finite.to_finset, by { convert s_basis.2, simp }⟩ }, { rintros ⟨s, hs⟩, rw [finite_dimensional_iff_dim_lt_omega, ← dim_top, ← hs], exact lt_of_le_of_lt (dim_span_le _) (lt_omega_iff_finite.2 (set.finite_mem_finset s)) } end /-- If a vector space has a finite basis, then it is finite-dimensional. -/ lemma of_finite_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) : finite_dimensional K V := iff_fg.2 $ ⟨finset.univ.image b, by {convert h.2, simp} ⟩ /-- A subspace of a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_submodule [finite_dimensional K V] (S : submodule K V) : finite_dimensional K S := finite_dimensional_iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_submodule_le _) (dim_lt_omega K V)) /-- A quotient of a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_quotient [finite_dimensional K V] (S : submodule K V) : finite_dimensional K (quotient S) := finite_dimensional_iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_quotient_le _) (dim_lt_omega K V)) /-- The dimension of a finite-dimensional vector space as a natural number. Defined by convention to be `0` if the space is infinite-dimensional. -/ noncomputable def findim (K V : Type*) [field K] [add_comm_group V] [vector_space K V] : ℕ := if h : dim K V < omega.{v} then classical.some (lt_omega.1 h) else 0 /-- In a finite-dimensional space, its dimension (seen as a cardinal) coincides with its `findim`. -/ lemma findim_eq_dim (K : Type u) (V : Type v) [field K] [add_comm_group V] [vector_space K V] [finite_dimensional K V] : (findim K V : cardinal.{v}) = dim K V := begin have : findim K V = classical.some (lt_omega.1 (dim_lt_omega K V)) := dif_pos (dim_lt_omega K V), rw this, exact (classical.some_spec (lt_omega.1 (dim_lt_omega K V))).symm end /-- If a vector space has a finite basis, then its dimension (seen as a cardinal) is equal to the cardinality of the basis. -/ lemma dim_eq_card_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) : dim K V = fintype.card ι := by rw [←h.mk_range_eq_dim, cardinal.fintype_card, set.card_range_of_injective (h.injective zero_ne_one)] /-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the basis. -/ lemma findim_eq_card_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) : findim K V = fintype.card ι := begin haveI : finite_dimensional K V := of_finite_basis h, have := dim_eq_card_basis h, rw ← findim_eq_dim at this, exact_mod_cast this end /-- If a vector space is finite-dimensional, then the cardinality of any basis is equal to its `findim`. -/ lemma findim_eq_card_basis' [finite_dimensional K V] {ι : Type w} {b : ι → V} (h : is_basis K b) : (findim K V : cardinal.{w}) = cardinal.mk ι := begin rcases exists_is_basis_finite K V with ⟨s, s_basis, s_finite⟩, letI: fintype s := s_finite.fintype, have A : cardinal.mk s = fintype.card s := fintype_card _, have B : findim K V = fintype.card s := findim_eq_card_basis s_basis, have C : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (cardinal.mk s) := mk_eq_mk_of_basis h s_basis, rw [A, ← B, lift_nat_cast] at C, have : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{w v} (findim K V), by { simp, exact C }, exact (lift_inj.mp this).symm end /-- If a submodule has maximal dimension in a finite dimensional space, then it is equal to the whole space. -/ lemma eq_top_of_findim_eq [finite_dimensional K V] {S : submodule K V} (h : findim K S = findim K V) : S = ⊤ := begin cases exists_is_basis K S with bS hbS, have : linear_independent K (subtype.val : (subtype.val '' bS : set V) → V), from @linear_independent.image_subtype _ _ _ _ _ _ _ _ _ (submodule.subtype S) hbS.1 (by simp), cases exists_subset_is_basis this with b hb, letI : fintype b := classical.choice (finite_of_linear_independent hb.2.1), letI : fintype (subtype.val '' bS) := classical.choice (finite_of_linear_independent this), letI : fintype bS := classical.choice (finite_of_linear_independent hbS.1), have : subtype.val '' bS = b, from set.eq_of_subset_of_card_le hb.1 (by rw [set.card_image_of_injective _ subtype.val_injective, ← findim_eq_card_basis hbS, ← findim_eq_card_basis hb.2, h]; apply_instance), erw [← hb.2.2, subtype.val_range, ← this, set.set_of_mem_eq, ← subtype_eq_val, span_image], have := hbS.2, erw [subtype.val_range, set.set_of_mem_eq] at this, rw [this, map_top (submodule.subtype S), range_subtype], end variable (K) /-- A field is one-dimensional as a vector space over itself. -/ @[simp] lemma findim_of_field : findim K K = 1 := begin have := dim_of_field K, rw [← findim_eq_dim] at this, exact_mod_cast this end /-- The vector space of functions on a fintype has finite dimension. -/ instance finite_dimensional_fintype_fun {ι : Type*} [fintype ι] : finite_dimensional K (ι → K) := by { rw [finite_dimensional_iff_dim_lt_omega, dim_fun'], exact nat_lt_omega _ } /-- The vector space of functions on a fintype ι has findim equal to the cardinality of ι. -/ @[simp] lemma findim_fintype_fun_eq_card {ι : Type v} [fintype ι] : findim K (ι → K) = fintype.card ι := begin have : vector_space.dim K (ι → K) = fintype.card ι := dim_fun', rwa [← findim_eq_dim, nat_cast_inj] at this, end /-- The vector space of functions on `fin n` has findim equal to `n`. -/ @[simp] lemma findim_fin_fun {n : ℕ} : findim K (fin n → K) = n := by simp /-- The submodule generated by a finite set is finite-dimensional. -/ theorem span_of_finite {A : set V} (hA : set.finite A) : finite_dimensional K (submodule.span K A) := is_noetherian_span_of_finite K hA end finite_dimensional namespace submodule open finite_dimensional /-- A submodule is finitely generated if and only if it is finite-dimensional -/ theorem fg_iff_finite_dimensional (s : submodule K V) : s.fg ↔ finite_dimensional K s := ⟨λh, is_noetherian_of_fg_of_noetherian s h, λh, by { rw ← map_subtype_top s, exact fg_map (iff_fg.1 h) }⟩ /-- In a finite-dimensional vector space, the dimensions of a submodule and of the corresponding quotient add up to the dimension of the space. -/ theorem findim_quotient_add_findim [finite_dimensional K V] (s : submodule K V) : findim K s.quotient + findim K s = findim K V := begin have := dim_quotient_add_dim s, rw [← findim_eq_dim, ← findim_eq_dim, ← findim_eq_dim] at this, exact_mod_cast this end /-- The dimension of a submodule is bounded by the dimension of the ambient space. -/ lemma findim_le [finite_dimensional K V] (s : submodule K V) : findim K s ≤ findim K V := by { rw ← s.findim_quotient_add_findim, exact nat.le_add_left _ _ } /-- The dimension of a quotient is bounded by the dimension of the ambient space. -/ lemma findim_quotient_le [finite_dimensional K V] (s : submodule K V) : findim K s.quotient ≤ findim K V := by { rw ← s.findim_quotient_add_findim, exact nat.le_add_right _ _ } end submodule namespace linear_equiv open finite_dimensional /-- Finite dimensionality is preserved under linear equivalence. -/ protected theorem finite_dimensional (f : V ≃ₗ[K] V₂) [finite_dimensional K V] : finite_dimensional K V₂ := is_noetherian_of_linear_equiv f /-- The dimension of a finite dimensional space is preserved under linear equivalence. -/ theorem findim_eq (f : V ≃ₗ[K] V₂) [finite_dimensional K V] : findim K V = findim K V₂ := begin haveI : finite_dimensional K V₂ := f.finite_dimensional, rcases exists_is_basis_finite K V with ⟨s, s_basis, s_finite⟩, letI : fintype s := s_finite.fintype, have A : findim K V = fintype.card s := findim_eq_card_basis s_basis, have : is_basis K (λx:s, f (subtype.val x)) := f.is_basis s_basis, have B : findim K V₂ = fintype.card s := findim_eq_card_basis this, rw [A, B] end end linear_equiv namespace linear_map open finite_dimensional /-- On a finite-dimensional space, an injective linear map is surjective. -/ lemma surjective_of_injective [finite_dimensional K V] {f : V →ₗ[K] V} (hinj : injective f) : surjective f := begin have h := dim_eq_injective _ hinj, rw [← findim_eq_dim, ← findim_eq_dim, nat_cast_inj] at h, exact range_eq_top.1 (eq_top_of_findim_eq h.symm) end /-- On a finite-dimensional space, a linear map is injective if and only if it is surjective. -/ lemma injective_iff_surjective [finite_dimensional K V] {f : V →ₗ[K] V} : injective f ↔ surjective f := ⟨surjective_of_injective, λ hsurj, let ⟨g, hg⟩ := f.exists_right_inverse_of_surjective (range_eq_top.2 hsurj) in have function.right_inverse g f, from linear_map.ext_iff.1 hg, (left_inverse_of_surjective_of_right_inverse (surjective_of_injective this.injective) this).injective⟩ lemma ker_eq_bot_iff_range_eq_top [finite_dimensional K V] {f : V →ₗ[K] V} : f.ker = ⊥ ↔ f.range = ⊤ := by rw [range_eq_top, ker_eq_bot, injective_iff_surjective] /-- In a finite-dimensional space, if linear maps are inverse to each other on one side then they are also inverse to each other on the other side. -/ lemma mul_eq_one_of_mul_eq_one [finite_dimensional K V] {f g : V →ₗ[K] V} (hfg : f * g = 1) : g * f = 1 := have ginj : injective g, from has_left_inverse.injective ⟨f, (λ x, show (f * g) x = (1 : V →ₗ[K] V) x, by rw hfg; refl)⟩, let ⟨i, hi⟩ := g.exists_right_inverse_of_surjective (range_eq_top.2 (injective_iff_surjective.1 ginj)) in have f * (g * i) = f * 1, from congr_arg _ hi, by rw [← mul_assoc, hfg, one_mul, mul_one] at this; rwa ← this /-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if they are inverse to each other on the other side. -/ lemma mul_eq_one_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f * g = 1 ↔ g * f = 1 := ⟨mul_eq_one_of_mul_eq_one, mul_eq_one_of_mul_eq_one⟩ /-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if they are inverse to each other on the other side. -/ lemma comp_eq_id_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f.comp g = id ↔ g.comp f = id := mul_eq_one_comm /-- The image under an onto linear map of a finite-dimensional space is also finite-dimensional. -/ lemma finite_dimensional_of_surjective [h : finite_dimensional K V] (f : V →ₗ[K] V₂) (hf : f.range = ⊤) : finite_dimensional K V₂ := is_noetherian_of_surjective V f hf /-- The range of a linear map defined on a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_range [h : finite_dimensional K V] (f : V →ₗ[K] V₂) : finite_dimensional K f.range := f.quot_ker_equiv_range.finite_dimensional /-- rank-nullity theorem : the dimensions of the kernel and the range of a linear map add up to the dimension of the source space. -/ theorem findim_range_add_findim_ker [finite_dimensional K V] (f : V →ₗ[K] V₂) : findim K f.range + findim K f.ker = findim K V := by { rw [← f.quot_ker_equiv_range.findim_eq], exact submodule.findim_quotient_add_findim _ } end linear_map
c5133c6054993b281cf5b6cb4d1a0bdaaadedd9a
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/gcd.lean
365b97b8b659743af3caf42fb93a7ab952be8fb3
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
212
lean
open Nat #eval gcd 8 12 #reduce gcd 8 12 #eval gcd 0 9 #reduce gcd 0 9 #eval gcd 4 0 #reduce gcd 4 0 #eval gcd 1 10 #reduce gcd 1 10 #eval gcd (2*3*7*11*17) (2*7*23) #eval gcd (2*7*23) (2*3*7*11*17)
9c5926656f4194632294f2737f21bc23ce1fa70f
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/app_builder_issue1.lean
c220cae27cc768fa22091b147d53a4074aa0c016
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
127
lean
constant f {A : Type} (a : A) {B : Type} (b : B) : nat example (a b c d : nat) : a = c → b = d → f a b = f c d := by simp
17bdfe9c053aea94b4041054b8fcc97720430daf
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/limits/shapes/default.lean
1b0f473ba9398903b00777439148d72124416b73
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
148
lean
import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.pullbacks
8df06b71cac329b54849e356a063eb0d5f983e61
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/integral/bochner.lean
6c728afa447d55722d36f67c9caf31e7b5d8342c
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
70,942
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import measure_theory.integral.set_to_l1 import analysis.normed_space.bounded_linear_maps import topology.sequences /-! # Bochner integral The Bochner integral extends the definition of the Lebesgue integral to functions that map from a measure space into a Banach space (complete normed vector space). It is constructed here by extending the integral on simple functions. ## Main definitions The Bochner integral is defined through the extension process described in the file `set_to_L1`, which follows these steps: 1. Define the integral of the indicator of a set. This is `weighted_smul μ s x = (μ s).to_real * x`. `weighted_smul μ` is shown to be linear in the value `x` and `dominated_fin_meas_additive` (defined in the file `set_to_L1`) with respect to the set `s`. 2. Define the integral on simple functions of the type `simple_func α E` (notation : `α →ₛ E`) where `E` is a real normed space. (See `simple_func.integral` for details.) 3. Transfer this definition to define the integral on `L1.simple_func α E` (notation : `α →₁ₛ[μ] E`), see `L1.simple_func.integral`. Show that this integral is a continuous linear map from `α →₁ₛ[μ] E` to `E`. 4. Define the Bochner integral on L1 functions by extending the integral on integrable simple functions `α →₁ₛ[μ] E` using `continuous_linear_map.extend` and the fact that the embedding of `α →₁ₛ[μ] E` into `α →₁[μ] E` is dense. 5. Define the Bochner integral on functions as the Bochner integral of its equivalence class in L1 space, if it is in L1, and 0 otherwise. The result of that construction is `∫ a, f a ∂μ`, which is definitionally equal to `set_to_fun (dominated_fin_meas_additive_weighted_smul μ) f`. Some basic properties of the integral (like linearity) are particular cases of the properties of `set_to_fun` (which are described in the file `set_to_L1`). ## Main statements 1. Basic properties of the Bochner integral on functions of type `α → E`, where `α` is a measure space and `E` is a real normed space. * `integral_zero` : `∫ 0 ∂μ = 0` * `integral_add` : `∫ x, f x + g x ∂μ = ∫ x, f ∂μ + ∫ x, g x ∂μ` * `integral_neg` : `∫ x, - f x ∂μ = - ∫ x, f x ∂μ` * `integral_sub` : `∫ x, f x - g x ∂μ = ∫ x, f x ∂μ - ∫ x, g x ∂μ` * `integral_smul` : `∫ x, r • f x ∂μ = r • ∫ x, f x ∂μ` * `integral_congr_ae` : `f =ᵐ[μ] g → ∫ x, f x ∂μ = ∫ x, g x ∂μ` * `norm_integral_le_integral_norm` : `∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ` 2. Basic properties of the Bochner integral on functions of type `α → ℝ`, where `α` is a measure space. * `integral_nonneg_of_ae` : `0 ≤ᵐ[μ] f → 0 ≤ ∫ x, f x ∂μ` * `integral_nonpos_of_ae` : `f ≤ᵐ[μ] 0 → ∫ x, f x ∂μ ≤ 0` * `integral_mono_ae` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ` * `integral_nonneg` : `0 ≤ f → 0 ≤ ∫ x, f x ∂μ` * `integral_nonpos` : `f ≤ 0 → ∫ x, f x ∂μ ≤ 0` * `integral_mono` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ` 3. Propositions connecting the Bochner integral with the integral on `ℝ≥0∞`-valued functions, which is called `lintegral` and has the notation `∫⁻`. * `integral_eq_lintegral_max_sub_lintegral_min` : `∫ x, f x ∂μ = ∫⁻ x, f⁺ x ∂μ - ∫⁻ x, f⁻ x ∂μ`, where `f⁺` is the positive part of `f` and `f⁻` is the negative part of `f`. * `integral_eq_lintegral_of_nonneg_ae` : `0 ≤ᵐ[μ] f → ∫ x, f x ∂μ = ∫⁻ x, f x ∂μ` 4. `tendsto_integral_of_dominated_convergence` : the Lebesgue dominated convergence theorem 5. (In the file `set_integral`) integration commutes with continuous linear maps. * `continuous_linear_map.integral_comp_comm` * `linear_isometry.integral_comp_comm` ## Notes Some tips on how to prove a proposition if the API for the Bochner integral is not enough so that you need to unfold the definition of the Bochner integral and go back to simple functions. One method is to use the theorem `integrable.induction` in the file `simple_func_dense_lp` (or one of the related results, like `Lp.induction` for functions in `Lp`), which allows you to prove something for an arbitrary integrable function. Another method is using the following steps. See `integral_eq_lintegral_max_sub_lintegral_min` for a complicated example, which proves that `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, with the first integral sign being the Bochner integral of a real-valued function `f : α → ℝ`, and second and third integral sign being the integral on `ℝ≥0∞`-valued functions (called `lintegral`). The proof of `integral_eq_lintegral_max_sub_lintegral_min` is scattered in sections with the name `pos_part`. Here are the usual steps of proving that a property `p`, say `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, holds for all functions : 1. First go to the `L¹` space. For example, if you see `ennreal.to_real (∫⁻ a, ennreal.of_real $ ∥f a∥)`, that is the norm of `f` in `L¹` space. Rewrite using `L1.norm_of_fun_eq_lintegral_norm`. 2. Show that the set `{f ∈ L¹ | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}` is closed in `L¹` using `is_closed_eq`. 3. Show that the property holds for all simple functions `s` in `L¹` space. Typically, you need to convert various notions to their `simple_func` counterpart, using lemmas like `L1.integral_coe_eq_integral`. 4. Since simple functions are dense in `L¹`, ``` univ = closure {s simple} = closure {s simple | ∫ s = ∫⁻ s⁺ - ∫⁻ s⁻} : the property holds for all simple functions ⊆ closure {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} = {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} : closure of a closed set is itself ``` Use `is_closed_property` or `dense_range.induction_on` for this argument. ## Notations * `α →ₛ E` : simple functions (defined in `measure_theory/integration`) * `α →₁[μ] E` : functions in L1 space, i.e., equivalence classes of integrable functions (defined in `measure_theory/lp_space`) * `α →₁ₛ[μ] E` : simple functions in L1 space, i.e., equivalence classes of integrable simple functions (defined in `measure_theory/simple_func_dense`) * `∫ a, f a ∂μ` : integral of `f` with respect to a measure `μ` * `∫ a, f a` : integral of `f` with respect to `volume`, the default measure on the ambient type We also define notations for integral on a set, which are described in the file `measure_theory/set_integral`. Note : `ₛ` is typed using `\_s`. Sometimes it shows as a box if the font is missing. ## Tags Bochner integral, simple function, function space, Lebesgue dominated convergence theorem -/ noncomputable theory open_locale topological_space big_operators nnreal ennreal measure_theory open set filter topological_space ennreal emetric namespace measure_theory variables {α E F 𝕜 : Type*} section weighted_smul open continuous_linear_map variables [normed_group F] [normed_space ℝ F] {m : measurable_space α} {μ : measure α} /-- Given a set `s`, return the continuous linear map `λ x, (μ s).to_real • x`. The extension of that set function through `set_to_L1` gives the Bochner integral of L1 functions. -/ def weighted_smul {m : measurable_space α} (μ : measure α) (s : set α) : F →L[ℝ] F := (μ s).to_real • (continuous_linear_map.id ℝ F) lemma weighted_smul_apply {m : measurable_space α} (μ : measure α) (s : set α) (x : F) : weighted_smul μ s x = (μ s).to_real • x := by simp [weighted_smul] @[simp] lemma weighted_smul_zero_measure {m : measurable_space α} : weighted_smul (0 : measure α) = (0 : set α → F →L[ℝ] F) := by { ext1, simp [weighted_smul], } @[simp] lemma weighted_smul_empty {m : measurable_space α} (μ : measure α) : weighted_smul μ ∅ = (0 : F →L[ℝ] F) := by { ext1 x, rw [weighted_smul_apply], simp, } lemma weighted_smul_add_measure {m : measurable_space α} (μ ν : measure α) {s : set α} (hμs : μ s ≠ ∞) (hνs : ν s ≠ ∞) : (weighted_smul (μ + ν) s : F →L[ℝ] F) = weighted_smul μ s + weighted_smul ν s := begin ext1 x, push_cast, simp_rw [pi.add_apply, weighted_smul_apply], push_cast, rw [pi.add_apply, ennreal.to_real_add hμs hνs, add_smul], end lemma weighted_smul_smul_measure {m : measurable_space α} (μ : measure α) (c : ℝ≥0∞) {s : set α} : (weighted_smul (c • μ) s : F →L[ℝ] F) = c.to_real • weighted_smul μ s := begin ext1 x, push_cast, simp_rw [pi.smul_apply, weighted_smul_apply], push_cast, simp_rw [pi.smul_apply, smul_eq_mul, to_real_mul, smul_smul], end lemma weighted_smul_congr (s t : set α) (hst : μ s = μ t) : (weighted_smul μ s : F →L[ℝ] F) = weighted_smul μ t := by { ext1 x, simp_rw weighted_smul_apply, congr' 2, } lemma weighted_smul_null {s : set α} (h_zero : μ s = 0) : (weighted_smul μ s : F →L[ℝ] F) = 0 := by { ext1 x, rw [weighted_smul_apply, h_zero], simp, } lemma weighted_smul_union' (s t : set α) (ht : measurable_set t) (hs_finite : μ s ≠ ∞) (ht_finite : μ t ≠ ∞) (h_inter : s ∩ t = ∅) : (weighted_smul μ (s ∪ t) : F →L[ℝ] F) = weighted_smul μ s + weighted_smul μ t := begin ext1 x, simp_rw [add_apply, weighted_smul_apply, measure_union (set.disjoint_iff_inter_eq_empty.mpr h_inter) ht, ennreal.to_real_add hs_finite ht_finite, add_smul], end @[nolint unused_arguments] lemma weighted_smul_union (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (hs_finite : μ s ≠ ∞) (ht_finite : μ t ≠ ∞) (h_inter : s ∩ t = ∅) : (weighted_smul μ (s ∪ t) : F →L[ℝ] F) = weighted_smul μ s + weighted_smul μ t := weighted_smul_union' s t ht hs_finite ht_finite h_inter lemma weighted_smul_smul [normed_field 𝕜] [normed_space 𝕜 F] [smul_comm_class ℝ 𝕜 F] (c : 𝕜) (s : set α) (x : F) : weighted_smul μ s (c • x) = c • weighted_smul μ s x := by { simp_rw [weighted_smul_apply, smul_comm], } lemma norm_weighted_smul_le (s : set α) : ∥(weighted_smul μ s : F →L[ℝ] F)∥ ≤ (μ s).to_real := calc ∥(weighted_smul μ s : F →L[ℝ] F)∥ = ∥(μ s).to_real∥ * ∥continuous_linear_map.id ℝ F∥ : norm_smul _ _ ... ≤ ∥(μ s).to_real∥ : (mul_le_mul_of_nonneg_left norm_id_le (norm_nonneg _)).trans (mul_one _).le ... = abs (μ s).to_real : real.norm_eq_abs _ ... = (μ s).to_real : abs_eq_self.mpr ennreal.to_real_nonneg lemma dominated_fin_meas_additive_weighted_smul {m : measurable_space α} (μ : measure α) : dominated_fin_meas_additive μ (weighted_smul μ : set α → F →L[ℝ] F) 1 := ⟨weighted_smul_union, λ s _ _, (norm_weighted_smul_le s).trans (one_mul _).symm.le⟩ lemma weighted_smul_nonneg (s : set α) (x : ℝ) (hx : 0 ≤ x) : 0 ≤ weighted_smul μ s x := begin simp only [weighted_smul, algebra.id.smul_eq_mul, coe_smul', id.def, coe_id', pi.smul_apply], exact mul_nonneg to_real_nonneg hx, end end weighted_smul local infixr ` →ₛ `:25 := simple_func namespace simple_func section pos_part variables [linear_order E] [has_zero E] [measurable_space α] /-- Positive part of a simple function. -/ def pos_part (f : α →ₛ E) : α →ₛ E := f.map (λ b, max b 0) /-- Negative part of a simple function. -/ def neg_part [has_neg E] (f : α →ₛ E) : α →ₛ E := pos_part (-f) lemma pos_part_map_norm (f : α →ₛ ℝ) : (pos_part f).map norm = pos_part f := by { ext, rw [map_apply, real.norm_eq_abs, abs_of_nonneg], exact le_max_right _ _ } lemma neg_part_map_norm (f : α →ₛ ℝ) : (neg_part f).map norm = neg_part f := by { rw neg_part, exact pos_part_map_norm _ } lemma pos_part_sub_neg_part (f : α →ₛ ℝ) : f.pos_part - f.neg_part = f := begin simp only [pos_part, neg_part], ext a, rw coe_sub, exact max_zero_sub_eq_self (f a) end end pos_part section integral /-! ### The Bochner integral of simple functions Define the Bochner integral of simple functions of the type `α →ₛ β` where `β` is a normed group, and prove basic property of this integral. -/ open finset variables [normed_group E] [normed_group F] [normed_space ℝ F] {p : ℝ≥0∞} {G F' : Type*} [normed_group G] [normed_group F'] [normed_space ℝ F'] {m : measurable_space α} {μ : measure α} /-- Bochner integral of simple functions whose codomain is a real `normed_space`. This is equal to `∑ x in f.range, (μ (f ⁻¹' {x})).to_real • x` (see `integral_eq`). -/ def integral {m : measurable_space α} (μ : measure α) (f : α →ₛ F) : F := f.set_to_simple_func (weighted_smul μ) lemma integral_def {m : measurable_space α} (μ : measure α) (f : α →ₛ F) : f.integral μ = f.set_to_simple_func (weighted_smul μ) := rfl lemma integral_eq {m : measurable_space α} (μ : measure α) (f : α →ₛ F) : f.integral μ = ∑ x in f.range, (μ (f ⁻¹' {x})).to_real • x := by simp [integral, set_to_simple_func, weighted_smul_apply] lemma integral_eq_sum_filter [decidable_pred (λ x : F, x ≠ 0)] {m : measurable_space α} (f : α →ₛ F) (μ : measure α) : f.integral μ = ∑ x in f.range.filter (λ x, x ≠ 0), (μ (f ⁻¹' {x})).to_real • x := by { rw [integral_def, set_to_simple_func_eq_sum_filter], simp_rw weighted_smul_apply, congr } /-- The Bochner integral is equal to a sum over any set that includes `f.range` (except `0`). -/ lemma integral_eq_sum_of_subset [decidable_pred (λ x : F, x ≠ 0)] {f : α →ₛ F} {s : finset F} (hs : f.range.filter (λ x, x ≠ 0) ⊆ s) : f.integral μ = ∑ x in s, (μ (f ⁻¹' {x})).to_real • x := begin rw [simple_func.integral_eq_sum_filter, finset.sum_subset hs], rintro x - hx, rw [finset.mem_filter, not_and_distrib, ne.def, not_not] at hx, rcases hx with hx|rfl; [skip, simp], rw [simple_func.mem_range] at hx, rw [preimage_eq_empty]; simp [set.disjoint_singleton_left, hx] end @[simp] lemma integral_const {m : measurable_space α} (μ : measure α) (y : F) : (const α y).integral μ = (μ univ).to_real • y := by classical; calc (const α y).integral μ = ∑ z in {y}, (μ ((const α y) ⁻¹' {z})).to_real • z : integral_eq_sum_of_subset $ (filter_subset _ _).trans (range_const_subset _ _) ... = (μ univ).to_real • y : by simp @[simp] lemma integral_piecewise_zero {m : measurable_space α} (f : α →ₛ F) (μ : measure α) {s : set α} (hs : measurable_set s) : (piecewise s hs f 0).integral μ = f.integral (μ.restrict s) := begin classical, refine (integral_eq_sum_of_subset _).trans ((sum_congr rfl $ λ y hy, _).trans (integral_eq_sum_filter _ _).symm), { intros y hy, simp only [mem_filter, mem_range, coe_piecewise, coe_zero, piecewise_eq_indicator, mem_range_indicator] at *, rcases hy with ⟨⟨rfl, -⟩|⟨x, hxs, rfl⟩, h₀⟩, exacts [(h₀ rfl).elim, ⟨set.mem_range_self _, h₀⟩] }, { dsimp, rw [set.piecewise_eq_indicator, indicator_preimage_of_not_mem, measure.restrict_apply (f.measurable_set_preimage _)], exact λ h₀, (mem_filter.1 hy).2 (eq.symm h₀) } end /-- Calculate the integral of `g ∘ f : α →ₛ F`, where `f` is an integrable function from `α` to `E` and `g` is a function from `E` to `F`. We require `g 0 = 0` so that `g ∘ f` is integrable. -/ lemma map_integral (f : α →ₛ E) (g : E → F) (hf : integrable f μ) (hg : g 0 = 0) : (f.map g).integral μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • (g x) := map_set_to_simple_func _ weighted_smul_union hf hg /-- `simple_func.integral` and `simple_func.lintegral` agree when the integrand has type `α →ₛ ℝ≥0∞`. But since `ℝ≥0∞` is not a `normed_space`, we need some form of coercion. See `integral_eq_lintegral` for a simpler version. -/ lemma integral_eq_lintegral' {f : α →ₛ E} {g : E → ℝ≥0∞} (hf : integrable f μ) (hg0 : g 0 = 0) (ht : ∀ b, g b ≠ ∞) : (f.map (ennreal.to_real ∘ g)).integral μ = ennreal.to_real (∫⁻ a, g (f a) ∂μ) := begin have hf' : f.fin_meas_supp μ := integrable_iff_fin_meas_supp.1 hf, simp only [← map_apply g f, lintegral_eq_lintegral], rw [map_integral f _ hf, map_lintegral, ennreal.to_real_sum], { refine finset.sum_congr rfl (λb hb, _), rw [smul_eq_mul, to_real_mul, mul_comm] }, { assume a ha, by_cases a0 : a = 0, { rw [a0, hg0, zero_mul], exact with_top.zero_ne_top }, { apply mul_ne_top (ht a) (hf'.meas_preimage_singleton_ne_zero a0).ne } }, { simp [hg0] } end variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E] lemma integral_congr {f g : α →ₛ E} (hf : integrable f μ) (h : f =ᵐ[μ] g) : f.integral μ = g.integral μ := set_to_simple_func_congr (weighted_smul μ) (λ s hs, weighted_smul_null) weighted_smul_union hf h /-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type `α →ₛ ℝ≥0∞`. But since `ℝ≥0∞` is not a `normed_space`, we need some form of coercion. -/ lemma integral_eq_lintegral {f : α →ₛ ℝ} (hf : integrable f μ) (h_pos : 0 ≤ᵐ[μ] f) : f.integral μ = ennreal.to_real (∫⁻ a, ennreal.of_real (f a) ∂μ) := begin have : f =ᵐ[μ] f.map (ennreal.to_real ∘ ennreal.of_real) := h_pos.mono (λ a h, (ennreal.to_real_of_real h).symm), rw [← integral_eq_lintegral' hf], exacts [integral_congr hf this, ennreal.of_real_zero, λ b, ennreal.of_real_ne_top] end lemma integral_add {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f + g) = integral μ f + integral μ g := set_to_simple_func_add _ weighted_smul_union hf hg lemma integral_neg {f : α →ₛ E} (hf : integrable f μ) : integral μ (-f) = - integral μ f := set_to_simple_func_neg _ weighted_smul_union hf lemma integral_sub {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f - g) = integral μ f - integral μ g := set_to_simple_func_sub _ weighted_smul_union hf hg lemma integral_smul (c : 𝕜) {f : α →ₛ E} (hf : integrable f μ) : integral μ (c • f) = c • integral μ f := set_to_simple_func_smul _ weighted_smul_union weighted_smul_smul c hf lemma norm_set_to_simple_func_le_integral_norm (T : set α → E →L[ℝ] F) {C : ℝ} (hT_norm : ∀ s, measurable_set s → μ s < ∞ → ∥T s∥ ≤ C * (μ s).to_real) {f : α →ₛ E} (hf : integrable f μ) : ∥f.set_to_simple_func T∥ ≤ C * (f.map norm).integral μ := calc ∥f.set_to_simple_func T∥ ≤ C * ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) * ∥x∥ : norm_set_to_simple_func_le_sum_mul_norm_of_integrable T hT_norm f hf ... = C * (f.map norm).integral μ : by { rw map_integral f norm hf norm_zero, simp_rw smul_eq_mul, } lemma norm_integral_le_integral_norm (f : α →ₛ E) (hf : integrable f μ) : ∥f.integral μ∥ ≤ (f.map norm).integral μ := begin refine (norm_set_to_simple_func_le_integral_norm _ (λ s _ _, _) hf).trans (one_mul _).le, exact (norm_weighted_smul_le s).trans (one_mul _).symm.le, end lemma integral_add_measure {ν} (f : α →ₛ E) (hf : integrable f (μ + ν)) : f.integral (μ + ν) = f.integral μ + f.integral ν := begin simp_rw [integral_def], refine set_to_simple_func_add_left' (weighted_smul μ) (weighted_smul ν) (weighted_smul (μ + ν)) (λ s hs hμνs, _) hf, rw [lt_top_iff_ne_top, measure.coe_add, pi.add_apply, ennreal.add_ne_top] at hμνs, rw weighted_smul_add_measure _ _ hμνs.1 hμνs.2, end end integral end simple_func namespace L1 open ae_eq_fun Lp.simple_func Lp variables [normed_group E] [normed_group F] {m : measurable_space α} {μ : measure α} variables {α E μ} namespace simple_func lemma norm_eq_integral (f : α →₁ₛ[μ] E) : ∥f∥ = ((to_simple_func f).map norm).integral μ := begin rw [norm_eq_sum_mul f, (to_simple_func f).map_integral norm (simple_func.integrable f) norm_zero], simp_rw smul_eq_mul, end section pos_part /-- Positive part of a simple function in L1 space. -/ def pos_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := ⟨Lp.pos_part (f : α →₁[μ] ℝ), begin rcases f with ⟨f, s, hsf⟩, use s.pos_part, simp only [subtype.coe_mk, Lp.coe_pos_part, ← hsf, ae_eq_fun.pos_part_mk, simple_func.pos_part, simple_func.coe_map, mk_eq_mk], end ⟩ /-- Negative part of a simple function in L1 space. -/ def neg_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : α →₁ₛ[μ] ℝ) : (pos_part f : α →₁[μ] ℝ) = Lp.pos_part (f : α →₁[μ] ℝ) := rfl @[norm_cast] lemma coe_neg_part (f : α →₁ₛ[μ] ℝ) : (neg_part f : α →₁[μ] ℝ) = Lp.neg_part (f : α →₁[μ] ℝ) := rfl end pos_part section simple_func_integral /-! ### The Bochner integral of `L1` Define the Bochner integral on `α →₁ₛ[μ] E` by extension from the simple functions `α →₁ₛ[μ] E`, and prove basic properties of this integral. -/ variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E] {F' : Type*} [normed_group F'] [normed_space ℝ F'] local attribute [instance] simple_func.normed_space /-- The Bochner integral over simple functions in L1 space. -/ def integral (f : α →₁ₛ[μ] E) : E := ((to_simple_func f)).integral μ lemma integral_eq_integral (f : α →₁ₛ[μ] E) : integral f = ((to_simple_func f)).integral μ := rfl lemma integral_eq_lintegral {f : α →₁ₛ[μ] ℝ} (h_pos : 0 ≤ᵐ[μ] (to_simple_func f)) : integral f = ennreal.to_real (∫⁻ a, ennreal.of_real ((to_simple_func f) a) ∂μ) := by rw [integral, simple_func.integral_eq_lintegral (simple_func.integrable f) h_pos] lemma integral_eq_set_to_L1s (f : α →₁ₛ[μ] E) : integral f = set_to_L1s (weighted_smul μ) f := rfl lemma integral_congr {f g : α →₁ₛ[μ] E} (h : to_simple_func f =ᵐ[μ] to_simple_func g) : integral f = integral g := simple_func.integral_congr (simple_func.integrable f) h lemma integral_add (f g : α →₁ₛ[μ] E) : integral (f + g) = integral f + integral g := set_to_L1s_add _ (λ _ _, weighted_smul_null) weighted_smul_union _ _ lemma integral_smul (c : 𝕜) (f : α →₁ₛ[μ] E) : integral (c • f) = c • integral f := set_to_L1s_smul _ (λ _ _, weighted_smul_null) weighted_smul_union weighted_smul_smul c f lemma norm_integral_le_norm (f : α →₁ₛ[μ] E) : ∥integral f∥ ≤ ∥f∥ := begin rw [integral, norm_eq_integral], exact (to_simple_func f).norm_integral_le_integral_norm (simple_func.integrable f) end variables {E' : Type*} [normed_group E'] [normed_space ℝ E'] [normed_space 𝕜 E'] variables (α E μ 𝕜) /-- The Bochner integral over simple functions in L1 space as a continuous linear map. -/ def integral_clm' : (α →₁ₛ[μ] E) →L[𝕜] E := linear_map.mk_continuous ⟨integral, integral_add, integral_smul⟩ 1 (λf, le_trans (norm_integral_le_norm _) $ by rw one_mul) /-- The Bochner integral over simple functions in L1 space as a continuous linear map over ℝ. -/ def integral_clm : (α →₁ₛ[μ] E) →L[ℝ] E := integral_clm' α E ℝ μ variables {α E μ 𝕜} local notation `Integral` := integral_clm α E μ open continuous_linear_map lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := linear_map.mk_continuous_norm_le _ (zero_le_one) _ section pos_part lemma pos_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : to_simple_func (pos_part f) =ᵐ[μ] (to_simple_func f).pos_part := begin have eq : ∀ a, (to_simple_func f).pos_part a = max ((to_simple_func f) a) 0 := λa, rfl, have ae_eq : ∀ᵐ a ∂μ, to_simple_func (pos_part f) a = max ((to_simple_func f) a) 0, { filter_upwards [to_simple_func_eq_to_fun (pos_part f), Lp.coe_fn_pos_part (f : α →₁[μ] ℝ), to_simple_func_eq_to_fun f] with _ _ h₂ _, convert h₂, }, refine ae_eq.mono (assume a h, _), rw [h, eq], end lemma neg_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : to_simple_func (neg_part f) =ᵐ[μ] (to_simple_func f).neg_part := begin rw [simple_func.neg_part, measure_theory.simple_func.neg_part], filter_upwards [pos_part_to_simple_func (-f), neg_to_simple_func f], assume a h₁ h₂, rw h₁, show max _ _ = max _ _, rw h₂, refl end lemma integral_eq_norm_pos_part_sub (f : α →₁ₛ[μ] ℝ) : integral f = ∥pos_part f∥ - ∥neg_part f∥ := begin -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₁ : (to_simple_func f).pos_part =ᵐ[μ] (to_simple_func (pos_part f)).map norm, { filter_upwards [pos_part_to_simple_func f] with _ h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.pos_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₂ : (to_simple_func f).neg_part =ᵐ[μ] (to_simple_func (neg_part f)).map norm, { filter_upwards [neg_part_to_simple_func f] with _ h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.neg_part_map_norm, simple_func.map_apply], }, }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq : ∀ᵐ a ∂μ, (to_simple_func f).pos_part a - (to_simple_func f).neg_part a = (to_simple_func (pos_part f)).map norm a - (to_simple_func (neg_part f)).map norm a, { filter_upwards [ae_eq₁, ae_eq₂] with _ h₁ h₂, rw [h₁, h₂], }, rw [integral, norm_eq_integral, norm_eq_integral, ← simple_func.integral_sub], { show (to_simple_func f).integral μ = ((to_simple_func (pos_part f)).map norm - (to_simple_func (neg_part f)).map norm).integral μ, apply measure_theory.simple_func.integral_congr (simple_func.integrable f), filter_upwards [ae_eq₁, ae_eq₂] with _ h₁ h₂, show _ = _ - _, rw [← h₁, ← h₂], have := (to_simple_func f).pos_part_sub_neg_part, conv_lhs {rw ← this}, refl, }, { exact (simple_func.integrable f).pos_part.congr ae_eq₁ }, { exact (simple_func.integrable f).neg_part.congr ae_eq₂ } end end pos_part end simple_func_integral end simple_func open simple_func local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ variables [normed_space ℝ E] [nondiscrete_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E] [normed_space ℝ F] [complete_space E] section integration_in_L1 local attribute [instance] simple_func.normed_space open continuous_linear_map variables (𝕜) /-- The Bochner integral in L1 space as a continuous linear map. -/ def integral_clm' : (α →₁[μ] E) →L[𝕜] E := (integral_clm' α E 𝕜 μ).extend (coe_to_Lp α E 𝕜) (simple_func.dense_range one_ne_top) simple_func.uniform_inducing variables {𝕜} /-- The Bochner integral in L1 space as a continuous linear map over ℝ. -/ def integral_clm : (α →₁[μ] E) →L[ℝ] E := integral_clm' ℝ /-- The Bochner integral in L1 space -/ def integral (f : α →₁[μ] E) : E := integral_clm f lemma integral_eq (f : α →₁[μ] E) : integral f = integral_clm f := rfl lemma integral_eq_set_to_L1 (f : α →₁[μ] E) : integral f = set_to_L1 (dominated_fin_meas_additive_weighted_smul μ) f := rfl @[norm_cast] lemma simple_func.integral_L1_eq_integral (f : α →₁ₛ[μ] E) : integral (f : α →₁[μ] E) = (simple_func.integral f) := set_to_L1_eq_set_to_L1s_clm (dominated_fin_meas_additive_weighted_smul μ) f variables (α E) @[simp] lemma integral_zero : integral (0 : α →₁[μ] E) = 0 := map_zero integral_clm variables {α E} lemma integral_add (f g : α →₁[μ] E) : integral (f + g) = integral f + integral g := map_add integral_clm f g lemma integral_neg (f : α →₁[μ] E) : integral (-f) = - integral f := map_neg integral_clm f lemma integral_sub (f g : α →₁[μ] E) : integral (f - g) = integral f - integral g := map_sub integral_clm f g lemma integral_smul (c : 𝕜) (f : α →₁[μ] E) : integral (c • f) = c • integral f := map_smul (integral_clm' 𝕜) c f local notation `Integral` := @integral_clm α E _ _ μ _ _ local notation `sIntegral` := @simple_func.integral_clm α E _ _ μ _ lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := norm_set_to_L1_le (dominated_fin_meas_additive_weighted_smul μ) zero_le_one lemma norm_integral_le (f : α →₁[μ] E) : ∥integral f∥ ≤ ∥f∥ := calc ∥integral f∥ = ∥Integral f∥ : rfl ... ≤ ∥Integral∥ * ∥f∥ : le_op_norm _ _ ... ≤ 1 * ∥f∥ : mul_le_mul_of_nonneg_right norm_Integral_le_one $ norm_nonneg _ ... = ∥f∥ : one_mul _ @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), integral f) := L1.integral_clm.continuous section pos_part lemma integral_eq_norm_pos_part_sub (f : α →₁[μ] ℝ) : integral f = ∥Lp.pos_part f∥ - ∥Lp.neg_part f∥ := begin -- Use `is_closed_property` and `is_closed_eq` refine @is_closed_property _ _ _ (coe : (α →₁ₛ[μ] ℝ) → (α →₁[μ] ℝ)) (λ f : α →₁[μ] ℝ, integral f = ∥Lp.pos_part f∥ - ∥Lp.neg_part f∥) (simple_func.dense_range one_ne_top) (is_closed_eq _ _) _ f, { exact cont _ }, { refine continuous.sub (continuous_norm.comp Lp.continuous_pos_part) (continuous_norm.comp Lp.continuous_neg_part) }, -- Show that the property holds for all simple functions in the `L¹` space. { assume s, norm_cast, exact simple_func.integral_eq_norm_pos_part_sub _ } end end pos_part end integration_in_L1 end L1 /-! ## The Bochner integral on functions Define the Bochner integral on functions generally to be the `L1` Bochner integral, for integrable functions, and 0 otherwise; prove its basic properties. -/ variables [normed_group E] [normed_space ℝ E] [complete_space E] [nondiscrete_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E] [normed_group F] [normed_space ℝ F] [complete_space F] section open_locale classical /-- The Bochner integral -/ def integral {m : measurable_space α} (μ : measure α) (f : α → E) : E := if hf : integrable f μ then L1.integral (hf.to_L1 f) else 0 end /-! In the notation for integrals, an expression like `∫ x, g ∥x∥ ∂μ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫ x, f x = 0` will be parsed incorrectly. -/ notation `∫` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral μ r notation `∫` binders `, ` r:(scoped:60 f, integral volume f) := r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral (measure.restrict μ s) r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, integral (measure.restrict volume s) f) := r section properties open continuous_linear_map measure_theory.simple_func variables {f g : α → E} {m : measurable_space α} {μ : measure α} lemma integral_eq (f : α → E) (hf : integrable f μ) : ∫ a, f a ∂μ = L1.integral (hf.to_L1 f) := @dif_pos _ (id _) hf _ _ _ lemma integral_eq_set_to_fun (f : α → E) : ∫ a, f a ∂μ = set_to_fun μ (weighted_smul μ) (dominated_fin_meas_additive_weighted_smul μ) f := rfl lemma L1.integral_eq_integral (f : α →₁[μ] E) : L1.integral f = ∫ a, f a ∂μ := (L1.set_to_fun_eq_set_to_L1 (dominated_fin_meas_additive_weighted_smul μ) f).symm lemma integral_undef (h : ¬ integrable f μ) : ∫ a, f a ∂μ = 0 := @dif_neg _ (id _) h _ _ _ lemma integral_non_ae_strongly_measurable (h : ¬ ae_strongly_measurable f μ) : ∫ a, f a ∂μ = 0 := integral_undef $ not_and_of_not_left _ h variables (α E) lemma integral_zero : ∫ a : α, (0:E) ∂μ = 0 := set_to_fun_zero (dominated_fin_meas_additive_weighted_smul μ) @[simp] lemma integral_zero' : integral μ (0 : α → E) = 0 := integral_zero α E variables {α E} lemma integral_add (hf : integrable f μ) (hg : integrable g μ) : ∫ a, f a + g a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := set_to_fun_add (dominated_fin_meas_additive_weighted_smul μ) hf hg lemma integral_add' (hf : integrable f μ) (hg : integrable g μ) : ∫ a, (f + g) a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := integral_add hf hg lemma integral_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, integrable (f i) μ) : ∫ a, ∑ i in s, f i a ∂μ = ∑ i in s, ∫ a, f i a ∂μ := set_to_fun_finset_sum (dominated_fin_meas_additive_weighted_smul _) s hf lemma integral_neg (f : α → E) : ∫ a, -f a ∂μ = - ∫ a, f a ∂μ := set_to_fun_neg (dominated_fin_meas_additive_weighted_smul μ) f lemma integral_neg' (f : α → E) : ∫ a, (-f) a ∂μ = - ∫ a, f a ∂μ := integral_neg f lemma integral_sub (hf : integrable f μ) (hg : integrable g μ) : ∫ a, f a - g a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := set_to_fun_sub (dominated_fin_meas_additive_weighted_smul μ) hf hg lemma integral_sub' (hf : integrable f μ) (hg : integrable g μ) : ∫ a, (f - g) a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := integral_sub hf hg lemma integral_smul (c : 𝕜) (f : α → E) : ∫ a, c • (f a) ∂μ = c • ∫ a, f a ∂μ := set_to_fun_smul (dominated_fin_meas_additive_weighted_smul μ) weighted_smul_smul c f lemma integral_mul_left (r : ℝ) (f : α → ℝ) : ∫ a, r * (f a) ∂μ = r * ∫ a, f a ∂μ := integral_smul r f lemma integral_mul_right (r : ℝ) (f : α → ℝ) : ∫ a, (f a) * r ∂μ = ∫ a, f a ∂μ * r := by { simp only [mul_comm], exact integral_mul_left r f } lemma integral_div (r : ℝ) (f : α → ℝ) : ∫ a, (f a) / r ∂μ = ∫ a, f a ∂μ / r := integral_mul_right r⁻¹ f lemma integral_congr_ae (h : f =ᵐ[μ] g) : ∫ a, f a ∂μ = ∫ a, g a ∂μ := set_to_fun_congr_ae (dominated_fin_meas_additive_weighted_smul μ) h @[simp] lemma L1.integral_of_fun_eq_integral {f : α → E} (hf : integrable f μ) : ∫ a, (hf.to_L1 f) a ∂μ = ∫ a, f a ∂μ := set_to_fun_to_L1 (dominated_fin_meas_additive_weighted_smul μ) hf @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), ∫ a, f a ∂μ) := continuous_set_to_fun (dominated_fin_meas_additive_weighted_smul μ) lemma norm_integral_le_lintegral_norm (f : α → E) : ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, ← integrable.norm_to_L1_eq_lintegral_norm f hf], exact L1.norm_integral_le _ }, { rw [integral_undef hf, norm_zero], exact to_real_nonneg } end lemma ennnorm_integral_le_lintegral_ennnorm (f : α → E) : (∥∫ a, f a ∂μ∥₊ : ℝ≥0∞) ≤ ∫⁻ a, ∥f a∥₊ ∂μ := by { simp_rw [← of_real_norm_eq_coe_nnnorm], apply ennreal.of_real_le_of_le_to_real, exact norm_integral_le_lintegral_norm f } lemma integral_eq_zero_of_ae {f : α → E} (hf : f =ᵐ[μ] 0) : ∫ a, f a ∂μ = 0 := by simp [integral_congr_ae hf, integral_zero] /-- If `f` has finite integral, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma has_finite_integral.tendsto_set_integral_nhds_zero {ι} {f : α → E} (hf : has_finite_integral f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) := begin rw [tendsto_zero_iff_norm_tendsto_zero], simp_rw [← coe_nnnorm, ← nnreal.coe_zero, nnreal.tendsto_coe, ← ennreal.tendsto_coe, ennreal.coe_zero], exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds (tendsto_set_lintegral_zero (ne_of_lt hf) hs) (λ i, zero_le _) (λ i, ennnorm_integral_le_lintegral_ennnorm _) end /-- If `f` is integrable, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma integrable.tendsto_set_integral_nhds_zero {ι} {f : α → E} (hf : integrable f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) := hf.2.tendsto_set_integral_nhds_zero hs /-- If `F i → f` in `L1`, then `∫ x, F i x ∂μ → ∫ x, f x ∂μ`. -/ lemma tendsto_integral_of_L1 {ι} (f : α → E) (hfi : integrable f μ) {F : ι → α → E} {l : filter ι} (hFi : ∀ᶠ i in l, integrable (F i) μ) (hF : tendsto (λ i, ∫⁻ x, ∥F i x - f x∥₊ ∂μ) l (𝓝 0)) : tendsto (λ i, ∫ x, F i x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := begin rw [tendsto_iff_norm_tendsto_zero], replace hF : tendsto (λ i, ennreal.to_real $ ∫⁻ x, ∥F i x - f x∥₊ ∂μ) l (𝓝 0) := (ennreal.tendsto_to_real zero_ne_top).comp hF, refine squeeze_zero_norm' (hFi.mp $ hFi.mono $ λ i hFi hFm, _) hF, simp only [norm_norm, ← integral_sub hFi hfi], convert norm_integral_le_lintegral_norm (λ x, F i x - f x), ext1 x, exact coe_nnreal_eq _ end /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their integrals. We could weaken the condition `bound_integrable` to require `has_finite_integral bound μ` instead (i.e. not requiring that `bound` is measurable), but in all applications proving integrability is easier. -/ theorem tendsto_integral_of_dominated_convergence {F : ℕ → α → E} {f : α → E} (bound : α → ℝ) (F_measurable : ∀ n, ae_strongly_measurable (F n) μ) (bound_integrable : integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) at_top (𝓝 $ ∫ a, f a ∂μ) := tendsto_set_to_fun_of_dominated_convergence (dominated_fin_meas_additive_weighted_smul μ) bound F_measurable bound_integrable h_bound h_lim /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι} [l.is_countably_generated] {F : ι → α → E} {f : α → E} (bound : α → ℝ) (hF_meas : ∀ᶠ n in l, ae_strongly_measurable (F n) μ) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) l (𝓝 $ ∫ a, f a ∂μ) := tendsto_set_to_fun_filter_of_dominated_convergence (dominated_fin_meas_additive_weighted_smul μ) bound hF_meas h_bound bound_integrable h_lim /-- Lebesgue dominated convergence theorem for series. -/ lemma has_sum_integral_of_dominated_convergence {ι} [encodable ι] {F : ι → α → E} {f : α → E} (bound : ι → α → ℝ) (hF_meas : ∀ n, ae_strongly_measurable (F n) μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound n a) (bound_summable : ∀ᵐ a ∂μ, summable (λ n, bound n a)) (bound_integrable : integrable (λ a, ∑' n, bound n a) μ) (h_lim : ∀ᵐ a ∂μ, has_sum (λ n, F n a) (f a)) : has_sum (λn, ∫ a, F n a ∂μ) (∫ a, f a ∂μ) := begin have hb_nonneg : ∀ᵐ a ∂μ, ∀ n, 0 ≤ bound n a := eventually_countable_forall.2 (λ n, (h_bound n).mono $ λ a, (norm_nonneg _).trans), have hb_le_tsum : ∀ n, bound n ≤ᵐ[μ] (λ a, ∑' n, bound n a), { intro n, filter_upwards [hb_nonneg, bound_summable] with _ ha0 ha_sum using le_tsum ha_sum _ (λ i _, ha0 i) }, have hF_integrable : ∀ n, integrable (F n) μ, { refine λ n, bound_integrable.mono' (hF_meas n) _, exact eventually_le.trans (h_bound n) (hb_le_tsum n) }, simp only [has_sum, ← integral_finset_sum _ (λ n _, hF_integrable n)], refine tendsto_integral_filter_of_dominated_convergence (λ a, ∑' n, bound n a) _ _ bound_integrable h_lim, { exact eventually_of_forall (λ s, s.ae_strongly_measurable_sum $ λ n hn, hF_meas n) }, { refine eventually_of_forall (λ s, _), filter_upwards [eventually_countable_forall.2 h_bound, hb_nonneg, bound_summable] with a hFa ha0 has, calc ∥∑ n in s, F n a∥ ≤ ∑ n in s, bound n a : norm_sum_le_of_le _ (λ n hn, hFa n) ... ≤ ∑' n, bound n a : sum_le_tsum _ (λ n hn, ha0 n) has }, end variables {X : Type*} [topological_space X] [first_countable_topology X] lemma continuous_at_of_dominated {F : X → α → E} {x₀ : X} {bound : α → ℝ} (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_strongly_measurable (F x) μ) (h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ a ∂μ, ∥F x a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous_at (λ x, F x a) x₀) : continuous_at (λ x, ∫ a, F x a ∂μ) x₀ := continuous_at_set_to_fun_of_dominated (dominated_fin_meas_additive_weighted_smul μ) hF_meas h_bound bound_integrable h_cont lemma continuous_of_dominated {F : X → α → E} {bound : α → ℝ} (hF_meas : ∀ x, ae_strongly_measurable (F x) μ) (h_bound : ∀ x, ∀ᵐ a ∂μ, ∥F x a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous (λ x, F x a)) : continuous (λ x, ∫ a, F x a ∂μ) := continuous_set_to_fun_of_dominated (dominated_fin_meas_additive_weighted_smul μ) hF_meas h_bound bound_integrable h_cont /-- The Bochner integral of a real-valued function `f : α → ℝ` is the difference between the integral of the positive part of `f` and the integral of the negative part of `f`. -/ lemma integral_eq_lintegral_pos_part_sub_lintegral_neg_part {f : α → ℝ} (hf : integrable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) - ennreal.to_real (∫⁻ a, (ennreal.of_real $ - f a) ∂μ) := let f₁ := hf.to_L1 f in -- Go to the `L¹` space have eq₁ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) = ∥Lp.pos_part f₁∥ := begin rw L1.norm_def, congr' 1, apply lintegral_congr_ae, filter_upwards [Lp.coe_fn_pos_part f₁, hf.coe_fn_to_L1] with _ h₁ h₂, rw [h₁, h₂, ennreal.of_real], congr' 1, apply nnreal.eq, rw real.nnnorm_of_nonneg (le_max_right _ _), simp only [real.coe_to_nnreal', subtype.coe_mk], end, -- Go to the `L¹` space have eq₂ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ - f a) ∂μ) = ∥Lp.neg_part f₁∥ := begin rw L1.norm_def, congr' 1, apply lintegral_congr_ae, filter_upwards [Lp.coe_fn_neg_part f₁, hf.coe_fn_to_L1] with _ h₁ h₂, rw [h₁, h₂, ennreal.of_real], congr' 1, apply nnreal.eq, simp only [real.coe_to_nnreal', coe_nnnorm, nnnorm_neg], rw [real.norm_of_nonpos (min_le_right _ _), ← max_neg_neg, neg_zero], end, begin rw [eq₁, eq₂, integral, dif_pos], exact L1.integral_eq_norm_pos_part_sub _ end lemma integral_eq_lintegral_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfm : ae_strongly_measurable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) := begin by_cases hfi : integrable f μ, { rw integral_eq_lintegral_pos_part_sub_lintegral_neg_part hfi, have h_min : ∫⁻ a, ennreal.of_real (-f a) ∂μ = 0, { rw lintegral_eq_zero_iff', { refine hf.mono _, simp only [pi.zero_apply], assume a h, simp only [h, neg_nonpos, of_real_eq_zero], }, { exact measurable_of_real.comp_ae_measurable hfm.ae_measurable.neg } }, rw [h_min, zero_to_real, _root_.sub_zero] }, { rw integral_undef hfi, simp_rw [integrable, hfm, has_finite_integral_iff_norm, lt_top_iff_ne_top, ne.def, true_and, not_not] at hfi, have : ∫⁻ (a : α), ennreal.of_real (f a) ∂μ = ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ, { refine lintegral_congr_ae (hf.mono $ assume a h, _), rw [real.norm_eq_abs, abs_of_nonneg h] }, rw [this, hfi], refl } end lemma integral_norm_eq_lintegral_nnnorm {G} [normed_group G] {f : α → G} (hf : ae_strongly_measurable f μ) : ∫ x, ∥f x∥ ∂μ = ennreal.to_real ∫⁻ x, ∥f x∥₊ ∂μ := begin rw integral_eq_lintegral_of_nonneg_ae _ hf.norm, { simp_rw [of_real_norm_eq_coe_nnnorm], }, { refine ae_of_all _ _, simp_rw [pi.zero_apply, norm_nonneg, imp_true_iff] }, end lemma of_real_integral_norm_eq_lintegral_nnnorm {G} [normed_group G] {f : α → G} (hf : integrable f μ) : ennreal.of_real ∫ x, ∥f x∥ ∂μ = ∫⁻ x, ∥f x∥₊ ∂μ := by rw [integral_norm_eq_lintegral_nnnorm hf.ae_strongly_measurable, ennreal.of_real_to_real (lt_top_iff_ne_top.mp hf.2)] lemma integral_eq_integral_pos_part_sub_integral_neg_part {f : α → ℝ} (hf : integrable f μ) : ∫ a, f a ∂μ = (∫ a, real.to_nnreal (f a) ∂μ) - (∫ a, real.to_nnreal (-f a) ∂μ) := begin rw [← integral_sub hf.real_to_nnreal], { simp }, { exact hf.neg.real_to_nnreal } end lemma integral_nonneg_of_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a, f a ∂μ := set_to_fun_nonneg (dominated_fin_meas_additive_weighted_smul μ) (λ s _ _, weighted_smul_nonneg s) hf lemma lintegral_coe_eq_integral (f : α → ℝ≥0) (hfi : integrable (λ x, (f x : ℝ)) μ) : ∫⁻ a, f a ∂μ = ennreal.of_real ∫ a, f a ∂μ := begin simp_rw [integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (λ x, (f x).coe_nonneg)) hfi.ae_strongly_measurable, ← ennreal.coe_nnreal_eq], rw [ennreal.of_real_to_real], rw [← lt_top_iff_ne_top], convert hfi.has_finite_integral, ext1 x, rw [nnreal.nnnorm_eq] end lemma of_real_integral_eq_lintegral_of_real {f : α → ℝ} (hfi : integrable f μ) (f_nn : 0 ≤ᵐ[μ] f) : ennreal.of_real (∫ x, f x ∂μ) = ∫⁻ x, ennreal.of_real (f x) ∂μ := begin simp_rw [integral_congr_ae (show f =ᵐ[μ] λ x, ∥f x∥, by { filter_upwards [f_nn] with x hx, rw [real.norm_eq_abs, abs_eq_self.mpr hx], }), of_real_integral_norm_eq_lintegral_nnnorm hfi, ←of_real_norm_eq_coe_nnnorm], apply lintegral_congr_ae, filter_upwards [f_nn] with x hx, exact congr_arg ennreal.of_real (by rw [real.norm_eq_abs, abs_eq_self.mpr hx]), end lemma integral_to_real {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) (hf : ∀ᵐ x ∂μ, f x < ∞) : ∫ a, (f a).to_real ∂μ = (∫⁻ a, f a ∂μ).to_real := begin rw [integral_eq_lintegral_of_nonneg_ae _ hfm.ennreal_to_real.ae_strongly_measurable], { rw lintegral_congr_ae, refine hf.mp (eventually_of_forall _), intros x hx, rw [lt_top_iff_ne_top] at hx, simp [hx] }, { exact (eventually_of_forall $ λ x, ennreal.to_real_nonneg) } end lemma lintegral_coe_le_coe_iff_integral_le {f : α → ℝ≥0} (hfi : integrable (λ x, (f x : ℝ)) μ) {b : ℝ≥0} : ∫⁻ a, f a ∂μ ≤ b ↔ ∫ a, (f a : ℝ) ∂μ ≤ b := by rw [lintegral_coe_eq_integral f hfi, ennreal.of_real, ennreal.coe_le_coe, real.to_nnreal_le_iff_le_coe] lemma integral_coe_le_of_lintegral_coe_le {f : α → ℝ≥0} {b : ℝ≥0} (h : ∫⁻ a, f a ∂μ ≤ b) : ∫ a, (f a : ℝ) ∂μ ≤ b := begin by_cases hf : integrable (λ a, (f a : ℝ)) μ, { exact (lintegral_coe_le_coe_iff_integral_le hf).1 h }, { rw integral_undef hf, exact b.2 } end lemma integral_nonneg {f : α → ℝ} (hf : 0 ≤ f) : 0 ≤ ∫ a, f a ∂μ := integral_nonneg_of_ae $ eventually_of_forall hf lemma integral_nonpos_of_ae {f : α → ℝ} (hf : f ≤ᵐ[μ] 0) : ∫ a, f a ∂μ ≤ 0 := begin have hf : 0 ≤ᵐ[μ] (-f) := hf.mono (assume a h, by rwa [pi.neg_apply, pi.zero_apply, neg_nonneg]), have : 0 ≤ ∫ a, -f a ∂μ := integral_nonneg_of_ae hf, rwa [integral_neg, neg_nonneg] at this, end lemma integral_nonpos {f : α → ℝ} (hf : f ≤ 0) : ∫ a, f a ∂μ ≤ 0 := integral_nonpos_of_ae $ eventually_of_forall hf lemma integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) : ∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 := by simp_rw [integral_eq_lintegral_of_nonneg_ae hf hfi.1, ennreal.to_real_eq_zero_iff, lintegral_eq_zero_iff' (ennreal.measurable_of_real.comp_ae_measurable hfi.1.ae_measurable), ← ennreal.not_lt_top, ← has_finite_integral_iff_of_real hf, hfi.2, not_true, or_false, ← hf.le_iff_eq, filter.eventually_eq, filter.eventually_le, (∘), pi.zero_apply, ennreal.of_real_eq_zero] lemma integral_eq_zero_iff_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) : ∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 := integral_eq_zero_iff_of_nonneg_ae (eventually_of_forall hf) hfi lemma integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) : (0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) := by simp_rw [(integral_nonneg_of_ae hf).lt_iff_ne, pos_iff_ne_zero, ne.def, @eq_comm ℝ 0, integral_eq_zero_iff_of_nonneg_ae hf hfi, filter.eventually_eq, ae_iff, pi.zero_apply, function.support] lemma integral_pos_iff_support_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) : (0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) := integral_pos_iff_support_of_nonneg_ae (eventually_of_forall hf) hfi section normed_group variables {H : Type*} [normed_group H] lemma L1.norm_eq_integral_norm (f : α →₁[μ] H) : ∥f∥ = ∫ a, ∥f a∥ ∂μ := begin simp only [snorm, snorm', ennreal.one_to_real, ennreal.rpow_one, Lp.norm_def, if_false, ennreal.one_ne_top, one_ne_zero, _root_.div_one], rw integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (by simp [norm_nonneg])) (Lp.ae_strongly_measurable f).norm, simp [of_real_norm_eq_coe_nnnorm] end lemma L1.norm_of_fun_eq_integral_norm {f : α → H} (hf : integrable f μ) : ∥hf.to_L1 f∥ = ∫ a, ∥f a∥ ∂μ := begin rw L1.norm_eq_integral_norm, refine integral_congr_ae _, apply hf.coe_fn_to_L1.mono, intros a ha, simp [ha] end lemma mem_ℒp.snorm_eq_integral_rpow_norm {f : α → H} {p : ℝ≥0∞} (hp1 : p ≠ 0) (hp2 : p ≠ ∞) (hf : mem_ℒp f p μ) : snorm f p μ = ennreal.of_real ((∫ a, ∥f a∥ ^ p.to_real ∂μ) ^ (p.to_real ⁻¹)) := begin have A : ∫⁻ (a : α), ennreal.of_real (∥f a∥ ^ p.to_real) ∂μ = ∫⁻ (a : α), ∥f a∥₊ ^ p.to_real ∂μ, { apply lintegral_congr (λ x, _), rw [← of_real_rpow_of_nonneg (norm_nonneg _) to_real_nonneg, of_real_norm_eq_coe_nnnorm] }, simp only [snorm_eq_lintegral_rpow_nnnorm hp1 hp2, one_div], rw integral_eq_lintegral_of_nonneg_ae, rotate, { exact eventually_of_forall (λ x, real.rpow_nonneg_of_nonneg (norm_nonneg _) _) }, { exact (hf.ae_strongly_measurable.norm.ae_measurable.pow_const _).ae_strongly_measurable }, rw [A, ← of_real_rpow_of_nonneg to_real_nonneg (inv_nonneg.2 to_real_nonneg), of_real_to_real], exact (lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp1 hp2 hf.2).ne end end normed_group lemma integral_mono_ae {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := set_to_fun_mono (dominated_fin_meas_additive_weighted_smul μ) (λ s _ _, weighted_smul_nonneg s) hf hg h @[mono] lemma integral_mono {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := integral_mono_ae hf hg $ eventually_of_forall h lemma integral_mono_of_nonneg {f g : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hgi : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := begin by_cases hfm : ae_strongly_measurable f μ, { refine integral_mono_ae ⟨hfm, _⟩ hgi h, refine (hgi.has_finite_integral.mono $ h.mp $ hf.mono $ λ x hf hfg, _), simpa [real.norm_eq_abs, abs_of_nonneg hf, abs_of_nonneg (le_trans hf hfg)] }, { rw [integral_non_ae_strongly_measurable hfm], exact integral_nonneg_of_ae (hf.trans h) } end lemma integral_mono_measure {f : α → ℝ} {ν} (hle : μ ≤ ν) (hf : 0 ≤ᵐ[ν] f) (hfi : integrable f ν) : ∫ a, f a ∂μ ≤ ∫ a, f a ∂ν := begin have hfi' : integrable f μ := hfi.mono_measure hle, have hf' : 0 ≤ᵐ[μ] f := hle.absolutely_continuous hf, rw [integral_eq_lintegral_of_nonneg_ae hf' hfi'.1, integral_eq_lintegral_of_nonneg_ae hf hfi.1, ennreal.to_real_le_to_real], exacts [lintegral_mono' hle le_rfl, ((has_finite_integral_iff_of_real hf').1 hfi'.2).ne, ((has_finite_integral_iff_of_real hf).1 hfi.2).ne] end lemma norm_integral_le_integral_norm (f : α → E) : ∥(∫ a, f a ∂μ)∥ ≤ ∫ a, ∥f a∥ ∂μ := have le_ae : ∀ᵐ a ∂μ, 0 ≤ ∥f a∥ := eventually_of_forall (λa, norm_nonneg _), classical.by_cases ( λh : ae_strongly_measurable f μ, calc ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) : norm_integral_le_lintegral_norm _ ... = ∫ a, ∥f a∥ ∂μ : (integral_eq_lintegral_of_nonneg_ae le_ae $ h.norm).symm ) ( λh : ¬ae_strongly_measurable f μ, begin rw [integral_non_ae_strongly_measurable h, norm_zero], exact integral_nonneg_of_ae le_ae end ) lemma norm_integral_le_of_norm_le {f : α → E} {g : α → ℝ} (hg : integrable g μ) (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ g x) : ∥∫ x, f x ∂μ∥ ≤ ∫ x, g x ∂μ := calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ : norm_integral_le_integral_norm f ... ≤ ∫ x, g x ∂μ : integral_mono_of_nonneg (eventually_of_forall $ λ x, norm_nonneg _) hg h lemma simple_func.integral_eq_integral (f : α →ₛ E) (hfi : integrable f μ) : f.integral μ = ∫ x, f x ∂μ := begin rw [integral_eq f hfi, ← L1.simple_func.to_Lp_one_eq_to_L1, L1.simple_func.integral_L1_eq_integral, L1.simple_func.integral_eq_integral], exact simple_func.integral_congr hfi (Lp.simple_func.to_simple_func_to_Lp _ _).symm end lemma simple_func.integral_eq_sum (f : α →ₛ E) (hfi : integrable f μ) : ∫ x, f x ∂μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • x := by { rw [← f.integral_eq_integral hfi, simple_func.integral, ← simple_func.integral_eq], refl, } @[simp] lemma integral_const (c : E) : ∫ x : α, c ∂μ = (μ univ).to_real • c := begin cases (@le_top _ _ _ (μ univ)).lt_or_eq with hμ hμ, { haveI : is_finite_measure μ := ⟨hμ⟩, exact set_to_fun_const (dominated_fin_meas_additive_weighted_smul _) _, }, { by_cases hc : c = 0, { simp [hc, integral_zero] }, { have : ¬integrable (λ x : α, c) μ, { simp only [integrable_const_iff, not_or_distrib], exact ⟨hc, hμ.not_lt⟩ }, simp [integral_undef, *] } } end lemma norm_integral_le_of_norm_le_const [is_finite_measure μ] {f : α → E} {C : ℝ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : ∥∫ x, f x ∂μ∥ ≤ C * (μ univ).to_real := calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, C ∂μ : norm_integral_le_of_norm_le (integrable_const C) h ... = C * (μ univ).to_real : by rw [integral_const, smul_eq_mul, mul_comm] lemma tendsto_integral_approx_on_of_measurable [measurable_space E] [borel_space E] {f : α → E} {s : set E} [separable_space s] (hfi : integrable f μ) (hfm : measurable f) (hs : ∀ᵐ x ∂μ, f x ∈ closure s) {y₀ : E} (h₀ : y₀ ∈ s) (h₀i : integrable (λ x, y₀) μ) : tendsto (λ n, (simple_func.approx_on f hfm s y₀ h₀ n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ) := begin have hfi' := simple_func.integrable_approx_on hfm hfi h₀ h₀i, simp only [simple_func.integral_eq_integral _ (hfi' _)], exact tendsto_integral_of_L1 _ hfi (eventually_of_forall hfi') (simple_func.tendsto_approx_on_L1_nnnorm hfm _ hs (hfi.sub h₀i).2) end lemma tendsto_integral_approx_on_of_measurable_of_range_subset [measurable_space E] [borel_space E] {f : α → E} (fmeas : measurable f) (hf : integrable f μ) (s : set E) [separable_space s] (hs : range f ∪ {0} ⊆ s) : tendsto (λ n, (simple_func.approx_on f fmeas s 0 (hs $ by simp) n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ) := begin apply tendsto_integral_approx_on_of_measurable hf fmeas _ _ (integrable_zero _ _ _), apply eventually_of_forall (λ x, _), apply subset_closure, apply hs, simp, end variable {ν : measure α} lemma integral_add_measure {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin have hfi := hμ.add_measure hν, simp_rw [integral_eq_set_to_fun], have hμ_dfma : dominated_fin_meas_additive (μ + ν) (weighted_smul μ : set α → E →L[ℝ] E) 1, from dominated_fin_meas_additive.add_measure_right μ ν (dominated_fin_meas_additive_weighted_smul μ) zero_le_one, have hν_dfma : dominated_fin_meas_additive (μ + ν) (weighted_smul ν : set α → E →L[ℝ] E) 1, from dominated_fin_meas_additive.add_measure_left μ ν (dominated_fin_meas_additive_weighted_smul ν) zero_le_one, rw [← set_to_fun_congr_measure_of_add_right hμ_dfma (dominated_fin_meas_additive_weighted_smul μ) f hfi, ← set_to_fun_congr_measure_of_add_left hν_dfma (dominated_fin_meas_additive_weighted_smul ν) f hfi], refine set_to_fun_add_left' _ _ _ (λ s hs hμνs, _) f, rw [measure.coe_add, pi.add_apply, add_lt_top] at hμνs, rw [weighted_smul, weighted_smul, weighted_smul, ← add_smul, measure.coe_add, pi.add_apply, to_real_add hμνs.1.ne hμνs.2.ne], end @[simp] lemma integral_zero_measure {m : measurable_space α} (f : α → E) : ∫ x, f x ∂(0 : measure α) = 0 := set_to_fun_measure_zero (dominated_fin_meas_additive_weighted_smul _) rfl theorem integral_finset_sum_measure {ι} {m : measurable_space α} {f : α → E} {μ : ι → measure α} {s : finset ι} (hf : ∀ i ∈ s, integrable f (μ i)) : ∫ a, f a ∂(∑ i in s, μ i) = ∑ i in s, ∫ a, f a ∂μ i := begin classical, refine finset.induction_on' s _ _, -- `induction s using finset.induction_on'` fails { simp }, { intros i t hi ht hit iht, simp only [finset.sum_insert hit, ← iht], exact integral_add_measure (hf _ hi) (integrable_finset_sum_measure.2 $ λ j hj, hf j (ht hj)) } end lemma nndist_integral_add_measure_le_lintegral (h₁ : integrable f μ) (h₂ : integrable f ν) : (nndist (∫ x, f x ∂μ) (∫ x, f x ∂(μ + ν)) : ℝ≥0∞) ≤ ∫⁻ x, ∥f x∥₊ ∂ν := begin rw [integral_add_measure h₁ h₂, nndist_comm, nndist_eq_nnnorm, add_sub_cancel'], exact ennnorm_integral_le_lintegral_ennnorm _ end theorem has_sum_integral_measure {ι} {m : measurable_space α} {f : α → E} {μ : ι → measure α} (hf : integrable f (measure.sum μ)) : has_sum (λ i, ∫ a, f a ∂μ i) (∫ a, f a ∂measure.sum μ) := begin have hfi : ∀ i, integrable f (μ i) := λ i, hf.mono_measure (measure.le_sum _ _), simp only [has_sum, ← integral_finset_sum_measure (λ i _, hfi i)], refine metric.nhds_basis_ball.tendsto_right_iff.mpr (λ ε ε0, _), lift ε to ℝ≥0 using ε0.le, have hf_lt : ∫⁻ x, ∥f x∥₊ ∂(measure.sum μ) < ∞ := hf.2, have hmem : ∀ᶠ y in 𝓝 ∫⁻ x, ∥f x∥₊ ∂(measure.sum μ), ∫⁻ x, ∥f x∥₊ ∂(measure.sum μ) < y + ε, { refine tendsto_id.add tendsto_const_nhds (lt_mem_nhds $ ennreal.lt_add_right _ _), exacts [hf_lt.ne, ennreal.coe_ne_zero.2 (nnreal.coe_ne_zero.1 ε0.ne')] }, refine ((has_sum_lintegral_measure (λ x, ∥f x∥₊) μ).eventually hmem).mono (λ s hs, _), obtain ⟨ν, hν⟩ : ∃ ν, (∑ i in s, μ i) + ν = measure.sum μ, { refine ⟨measure.sum (λ i : ↥(sᶜ : set ι), μ i), _⟩, simpa only [← measure.sum_coe_finset] using measure.sum_add_sum_compl (s : set ι) μ }, rw [metric.mem_ball, ← coe_nndist, nnreal.coe_lt_coe, ← ennreal.coe_lt_coe, ← hν], rw [← hν, integrable_add_measure] at hf, refine (nndist_integral_add_measure_le_lintegral hf.1 hf.2).trans_lt _, rw [← hν, lintegral_add_measure, lintegral_finset_sum_measure] at hs, exact lt_of_add_lt_add_left hs end theorem integral_sum_measure {ι} {m : measurable_space α} {f : α → E} {μ : ι → measure α} (hf : integrable f (measure.sum μ)) : ∫ a, f a ∂measure.sum μ = ∑' i, ∫ a, f a ∂μ i := (has_sum_integral_measure hf).tsum_eq.symm @[simp] lemma integral_smul_measure (f : α → E) (c : ℝ≥0∞) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin -- First we consider the “degenerate” case `c = ∞` rcases eq_or_ne c ∞ with rfl|hc, { rw [ennreal.top_to_real, zero_smul, integral_eq_set_to_fun, set_to_fun_top_smul_measure], }, -- Main case: `c ≠ ∞` simp_rw [integral_eq_set_to_fun, ← set_to_fun_smul_left], have hdfma : dominated_fin_meas_additive μ (weighted_smul (c • μ) : set α → E →L[ℝ] E) c.to_real, from mul_one c.to_real ▸ (dominated_fin_meas_additive_weighted_smul (c • μ)).of_smul_measure c hc, have hdfma_smul := (dominated_fin_meas_additive_weighted_smul (c • μ)), rw ← set_to_fun_congr_smul_measure c hc hdfma hdfma_smul f, exact set_to_fun_congr_left' _ _ (λ s hs hμs, weighted_smul_smul_measure μ c) f, end lemma integral_map_of_strongly_measurable {β} [measurable_space β] {φ : α → β} (hφ : measurable φ) {f : β → E} (hfm : strongly_measurable f) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := begin by_cases hfi : integrable f (measure.map φ μ), swap, { rw [integral_undef hfi, integral_undef], rwa [← integrable_map_measure hfm.ae_strongly_measurable hφ.ae_measurable] }, borelize E, haveI : separable_space (range f ∪ {0} : set E) := hfm.separable_space_range_union_singleton, refine tendsto_nhds_unique (tendsto_integral_approx_on_of_measurable_of_range_subset hfm.measurable hfi _ subset.rfl) _, convert tendsto_integral_approx_on_of_measurable_of_range_subset (hfm.measurable.comp hφ) ((integrable_map_measure hfm.ae_strongly_measurable hφ.ae_measurable).1 hfi) (range f ∪ {0}) (by simp [insert_subset_insert, set.range_comp_subset_range]) using 1, ext1 i, simp only [simple_func.approx_on_comp, simple_func.integral_eq, measure.map_apply, hφ, simple_func.measurable_set_preimage, ← preimage_comp, simple_func.coe_comp], refine (finset.sum_subset (simple_func.range_comp_subset_range _ hφ) (λ y _ hy, _)).symm, rw [simple_func.mem_range, ← set.preimage_singleton_eq_empty, simple_func.coe_comp] at hy, rw [hy], simp, end lemma integral_map {β} [measurable_space β] {φ : α → β} (hφ : ae_measurable φ μ) {f : β → E} (hfm : ae_strongly_measurable f (measure.map φ μ)) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := let g := hfm.mk f in calc ∫ y, f y ∂(measure.map φ μ) = ∫ y, g y ∂(measure.map φ μ) : integral_congr_ae hfm.ae_eq_mk ... = ∫ y, g y ∂(measure.map (hφ.mk φ) μ) : by { congr' 1, exact measure.map_congr hφ.ae_eq_mk } ... = ∫ x, g (hφ.mk φ x) ∂μ : integral_map_of_strongly_measurable hφ.measurable_mk hfm.strongly_measurable_mk ... = ∫ x, g (φ x) ∂μ : integral_congr_ae (hφ.ae_eq_mk.symm.fun_comp _) ... = ∫ x, f (φ x) ∂μ : integral_congr_ae $ ae_eq_comp hφ (hfm.ae_eq_mk).symm lemma _root_.measurable_embedding.integral_map {β} {_ : measurable_space β} {f : α → β} (hf : measurable_embedding f) (g : β → E) : ∫ y, g y ∂(measure.map f μ) = ∫ x, g (f x) ∂μ := begin by_cases hgm : ae_strongly_measurable g (measure.map f μ), { exact integral_map hf.measurable.ae_measurable hgm }, { rw [integral_non_ae_strongly_measurable hgm, integral_non_ae_strongly_measurable], rwa ← hf.ae_strongly_measurable_map_iff } end lemma _root_.closed_embedding.integral_map {β} [topological_space α] [borel_space α] [topological_space β] [measurable_space β] [borel_space β] {φ : α → β} (hφ : closed_embedding φ) (f : β → E) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := hφ.measurable_embedding.integral_map _ lemma integral_map_equiv {β} [measurable_space β] (e : α ≃ᵐ β) (f : β → E) : ∫ y, f y ∂(measure.map e μ) = ∫ x, f (e x) ∂μ := e.measurable_embedding.integral_map f lemma measure_preserving.integral_comp {β} {_ : measurable_space β} {f : α → β} {ν} (h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) : ∫ x, g (f x) ∂μ = ∫ y, g y ∂ν := h₁.map_eq ▸ (h₂.integral_map g).symm lemma set_integral_eq_subtype {α} [measure_space α] {s : set α} (hs : measurable_set s) (f : α → E) : ∫ x in s, f x = ∫ x : s, f x := by { rw ← map_comap_subtype_coe hs, exact (measurable_embedding.subtype_coe hs).integral_map _ } @[simp] lemma integral_dirac' [measurable_space α] (f : α → E) (a : α) (hfm : strongly_measurable f) : ∫ x, f x ∂(measure.dirac a) = f a := begin borelize E, calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae $ ae_eq_dirac' hfm.measurable ... = f a : by simp [measure.dirac_apply_of_mem] end @[simp] lemma integral_dirac [measurable_space α] [measurable_singleton_class α] (f : α → E) (a : α) : ∫ x, f x ∂(measure.dirac a) = f a := calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae $ ae_eq_dirac f ... = f a : by simp [measure.dirac_apply_of_mem] end properties mk_simp_attribute integral_simps "Simp set for integral rules." attribute [integral_simps] integral_neg integral_smul L1.integral_add L1.integral_sub L1.integral_smul L1.integral_neg attribute [irreducible] integral L1.integral section integral_trim variables {H β γ : Type*} [normed_group H] {m m0 : measurable_space β} {μ : measure β} /-- Simple function seen as simple function of a larger `measurable_space`. -/ def simple_func.to_larger_space (hm : m ≤ m0) (f : @simple_func β m γ) : simple_func β γ := ⟨@simple_func.to_fun β m γ f, λ x, hm _ (@simple_func.measurable_set_fiber β γ m f x), @simple_func.finite_range β γ m f⟩ lemma simple_func.coe_to_larger_space_eq (hm : m ≤ m0) (f : @simple_func β m γ) : ⇑(f.to_larger_space hm) = f := rfl lemma integral_simple_func_larger_space (hm : m ≤ m0) (f : @simple_func β m F) (hf_int : integrable f μ) : ∫ x, f x ∂μ = ∑ x in (@simple_func.range β F m f), (ennreal.to_real (μ (f ⁻¹' {x}))) • x := begin simp_rw ← f.coe_to_larger_space_eq hm, have hf_int : integrable (f.to_larger_space hm) μ, by rwa simple_func.coe_to_larger_space_eq, rw simple_func.integral_eq_sum _ hf_int, congr, end lemma integral_trim_simple_func (hm : m ≤ m0) (f : @simple_func β m F) (hf_int : integrable f μ) : ∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) := begin have hf : strongly_measurable[m] f, from @simple_func.strongly_measurable β F m _ f, have hf_int_m := hf_int.trim hm hf, rw [integral_simple_func_larger_space (le_refl m) f hf_int_m, integral_simple_func_larger_space hm f hf_int], congr' with x, congr, exact (trim_measurable_set_eq hm (@simple_func.measurable_set_fiber β F m f x)).symm, end lemma integral_trim (hm : m ≤ m0) {f : β → F} (hf : strongly_measurable[m] f) : ∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) := begin borelize F, by_cases hf_int : integrable f μ, swap, { have hf_int_m : ¬ integrable f (μ.trim hm), from λ hf_int_m, hf_int (integrable_of_integrable_trim hm hf_int_m), rw [integral_undef hf_int, integral_undef hf_int_m], }, haveI : separable_space (range f ∪ {0} : set F) := hf.separable_space_range_union_singleton, let f_seq := @simple_func.approx_on F β _ _ _ m _ hf.measurable (range f ∪ {0}) 0 (by simp) _, have hf_seq_meas : ∀ n, strongly_measurable[m] (f_seq n), from λ n, @simple_func.strongly_measurable β F m _ (f_seq n), have hf_seq_int : ∀ n, integrable (f_seq n) μ, from simple_func.integrable_approx_on_range (hf.mono hm).measurable hf_int, have hf_seq_int_m : ∀ n, integrable (f_seq n) (μ.trim hm), from λ n, (hf_seq_int n).trim hm (hf_seq_meas n) , have hf_seq_eq : ∀ n, ∫ x, f_seq n x ∂μ = ∫ x, f_seq n x ∂(μ.trim hm), from λ n, integral_trim_simple_func hm (f_seq n) (hf_seq_int n), have h_lim_1 : at_top.tendsto (λ n, ∫ x, f_seq n x ∂μ) (𝓝 (∫ x, f x ∂μ)), { refine tendsto_integral_of_L1 f hf_int (eventually_of_forall hf_seq_int) _, exact simple_func.tendsto_approx_on_range_L1_nnnorm (hf.mono hm).measurable hf_int, }, have h_lim_2 : at_top.tendsto (λ n, ∫ x, f_seq n x ∂μ) (𝓝 (∫ x, f x ∂(μ.trim hm))), { simp_rw hf_seq_eq, refine @tendsto_integral_of_L1 β F _ _ _ m (μ.trim hm) _ f (hf_int.trim hm hf) _ _ (eventually_of_forall hf_seq_int_m) _, exact @simple_func.tendsto_approx_on_range_L1_nnnorm β F m _ _ _ f _ _ hf.measurable (hf_int.trim hm hf), }, exact tendsto_nhds_unique h_lim_1 h_lim_2, end lemma integral_trim_ae (hm : m ≤ m0) {f : β → F} (hf : ae_strongly_measurable f (μ.trim hm)) : ∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) := begin rw [integral_congr_ae (ae_eq_of_ae_eq_trim hf.ae_eq_mk), integral_congr_ae hf.ae_eq_mk], exact integral_trim hm hf.strongly_measurable_mk, end lemma ae_eq_trim_of_strongly_measurable [topological_space γ] [metrizable_space γ] (hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) (hfg : f =ᵐ[μ] g) : f =ᵐ[μ.trim hm] g := begin rwa [eventually_eq, ae_iff, trim_measurable_set_eq hm _], exact (hf.measurable_set_eq_fun hg).compl end lemma ae_eq_trim_iff [topological_space γ] [metrizable_space γ] (hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) : f =ᵐ[μ.trim hm] g ↔ f =ᵐ[μ] g := ⟨ae_eq_of_ae_eq_trim, ae_eq_trim_of_strongly_measurable hm hf hg⟩ lemma ae_le_trim_of_strongly_measurable [linear_order γ] [topological_space γ] [order_closed_topology γ] [metrizable_space γ] (hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) (hfg : f ≤ᵐ[μ] g) : f ≤ᵐ[μ.trim hm] g := begin rwa [eventually_le, ae_iff, trim_measurable_set_eq hm _], exact (hf.measurable_set_le hg).compl, end lemma ae_le_trim_iff [linear_order γ] [topological_space γ] [order_closed_topology γ] [metrizable_space γ] (hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) : f ≤ᵐ[μ.trim hm] g ↔ f ≤ᵐ[μ] g := ⟨ae_le_of_ae_le_trim, ae_le_trim_of_strongly_measurable hm hf hg⟩ end integral_trim end measure_theory
068894213524dacc59fc487ac96de14926b0fe78
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/dfinsupp/basic.lean
f5327b314b0bb0cd01fde7eec081a0affa5aaaff
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
84,020
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau -/ import algebra.module.linear_map import algebra.big_operators.basic import data.set.finite import group_theory.submonoid.membership import group_theory.group_action.big_operators import data.finset.preimage /-! # Dependent functions with finite support > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. For a non-dependent version see `data/finsupp.lean`. ## Notation This file introduces the notation `Π₀ a, β a` as notation for `dfinsupp β`, mirroring the `α →₀ β` notation used for `finsupp`. This works for nested binders too, with `Π₀ a b, γ a b` as notation for `dfinsupp (λ a, dfinsupp (γ a))`. ## Implementation notes The support is internally represented (in the primed `dfinsupp.support'`) as a `multiset` that represents a superset of the true support of the function, quotiented by the always-true relation so that this does not impact equality. This approach has computational benefits over storing a `finset`; it allows us to add together two finitely-supported functions (`dfinsupp.has_add`) without having to evaluate the resulting function to recompute its support (which would required decidability of `b = 0` for `b : β i`). The true support of the function can still be recovered with `dfinsupp.support`; but these decidability obligations are now postponed to when the support is actually needed. As a consequence, there are two ways to sum a `dfinsupp`: with `dfinsupp.sum` which works over an arbitrary function but requires recomputation of the support and therefore a `decidable` argument; and with `dfinsupp.sum_add_hom` which requires an additive morphism, using its properties to show that summing over a superset of the support is sufficient. `finsupp` takes an altogether different approach here; it uses `classical.decidable` and declares `finsupp.has_add` as noncomputable. This design difference is independent of the fact that `dfinsupp` is dependently-typed and `finsupp` is not; in future, we may want to align these two definitions, or introduce two more definitions for the other combinations of decisions. -/ universes u u₁ u₂ v v₁ v₂ v₃ w x y l open_locale big_operators variables {ι : Type u} {γ : Type w} {β : ι → Type v} {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} variable (β) /-- A dependent function `Π i, β i` with finite support, with notation `Π₀ i, β i`. Note that `dfinsupp.support` is the preferred API for accessing the support of the function, `dfinsupp.support'` is a implementation detail that aids computability; see the implementation notes in this file for more information. -/ structure dfinsupp [Π i, has_zero (β i)] : Type (max u v) := mk' :: (to_fun : Π i, β i) (support' : trunc {s : multiset ι // ∀ i, i ∈ s ∨ to_fun i = 0}) variable {β} notation `Π₀` binders `, ` r:(scoped f, dfinsupp f) := r infix ` →ₚ `:25 := dfinsupp namespace dfinsupp section basic variables [Π i, has_zero (β i)] [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] instance fun_like : fun_like (Π₀ i, β i) ι β := ⟨λ f, f.to_fun, λ ⟨f₁, s₁⟩ ⟨f₂, s₁⟩ (h : f₁= f₂), by { subst h, congr'} ⟩ /-- Helper instance for when there are too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (Π₀ i, β i) (λ _, Π i, β i) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe (f : Π₀ i, β i) : f.to_fun = f := rfl @[ext] lemma ext {f g : Π₀ i, β i} (h : ∀ i, f i = g i) : f = g := fun_like.ext _ _ h /-- Deprecated. Use `fun_like.ext_iff` instead. -/ lemma ext_iff {f g : Π₀ i, β i} : f = g ↔ ∀ i, f i = g i := fun_like.ext_iff /-- Deprecated. Use `fun_like.coe_injective` instead. -/ lemma coe_fn_injective : @function.injective (Π₀ i, β i) (Π i, β i) coe_fn := fun_like.coe_injective instance : has_zero (Π₀ i, β i) := ⟨⟨0, trunc.mk $ ⟨∅, λ i, or.inr rfl⟩⟩⟩ instance : inhabited (Π₀ i, β i) := ⟨0⟩ @[simp] lemma coe_mk' (f : Π i, β i) (s) : ⇑(⟨f, s⟩ : Π₀ i, β i) = f := rfl @[simp] lemma coe_zero : ⇑(0 : Π₀ i, β i) = 0 := rfl lemma zero_apply (i : ι) : (0 : Π₀ i, β i) i = 0 := rfl /-- The composition of `f : β₁ → β₂` and `g : Π₀ i, β₁ i` is `map_range f hf g : Π₀ i, β₂ i`, well defined when `f 0 = 0`. This preserves the structure on `f`, and exists in various bundled forms for when `f` is itself bundled: * `dfinsupp.map_range.add_monoid_hom` * `dfinsupp.map_range.add_equiv` * `dfinsupp.map_range.linear_map` * `dfinsupp.map_range.linear_equiv` -/ def map_range (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (x : Π₀ i, β₁ i) : Π₀ i, β₂ i := ⟨λ i, f i (x i), x.support'.map $ λ s, ⟨s, λ i, (s.2 i).imp_right $ λ h : x i = 0, h.symm ▸ hf i⟩⟩ @[simp] lemma map_range_apply (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (g : Π₀ i, β₁ i) (i : ι) : map_range f hf g i = f i (g i) := rfl @[simp] lemma map_range_id (h : ∀ i, id (0 : β₁ i) = 0 := λ i, rfl) (g : Π₀ (i : ι), β₁ i) : map_range (λ i, (id : β₁ i → β₁ i)) h g = g := by { ext, refl } lemma map_range_comp (f : Π i, β₁ i → β₂ i) (f₂ : Π i, β i → β₁ i) (hf : ∀ i, f i 0 = 0) (hf₂ : ∀ i, f₂ i 0 = 0) (h : ∀ i, (f i ∘ f₂ i) 0 = 0) (g : Π₀ (i : ι), β i) : map_range (λ i, f i ∘ f₂ i) h g = map_range f hf (map_range f₂ hf₂ g) := by { ext, simp only [map_range_apply] } @[simp] lemma map_range_zero (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) : map_range f hf (0 : Π₀ i, β₁ i) = 0 := by { ext, simp only [map_range_apply, coe_zero, pi.zero_apply, hf] } /-- Let `f i` be a binary operation `β₁ i → β₂ i → β i` such that `f i 0 0 = 0`. Then `zip_with f hf` is a binary operation `Π₀ i, β₁ i → Π₀ i, β₂ i → Π₀ i, β i`. -/ def zip_with (f : Π i, β₁ i → β₂ i → β i) (hf : ∀ i, f i 0 0 = 0) (x : Π₀ i, β₁ i) (y : Π₀ i, β₂ i) : (Π₀ i, β i) := ⟨λ i, f i (x i) (y i), begin refine x.support'.bind (λ xs, _), refine y.support'.map (λ ys, _), refine ⟨xs + ys, λ i, _⟩, obtain h1 | (h1 : x i = 0) := xs.prop i, { left, rw multiset.mem_add, left, exact h1 }, obtain h2 | (h2 : y i = 0) := ys.prop i, { left, rw multiset.mem_add, right, exact h2 }, right, rw [h1, h2, hf] end⟩ @[simp] lemma zip_with_apply (f : Π i, β₁ i → β₂ i → β i) (hf : ∀ i, f i 0 0 = 0) (g₁ : Π₀ i, β₁ i) (g₂ : Π₀ i, β₂ i) (i : ι) : zip_with f hf g₁ g₂ i = f i (g₁ i) (g₂ i) := rfl section piecewise variables (x y : Π₀ i, β i) (s : set ι) [Π i, decidable (i ∈ s)] /-- `x.piecewise y s` is the finitely supported function equal to `x` on the set `s`, and to `y` on its complement. -/ def piecewise : Π₀ i, β i := zip_with (λ i x y, if i ∈ s then x else y) (λ _, if_t_t _ 0) x y lemma piecewise_apply (i : ι) : x.piecewise y s i = if i ∈ s then x i else y i := zip_with_apply _ _ x y i @[simp, norm_cast] lemma coe_piecewise : ⇑(x.piecewise y s) = s.piecewise x y := by { ext, apply piecewise_apply } end piecewise end basic section algebra instance [Π i, add_zero_class (β i)] : has_add (Π₀ i, β i) := ⟨zip_with (λ _, (+)) (λ _, add_zero 0)⟩ lemma add_apply [Π i, add_zero_class (β i)] (g₁ g₂ : Π₀ i, β i) (i : ι) : (g₁ + g₂) i = g₁ i + g₂ i := rfl @[simp] lemma coe_add [Π i, add_zero_class (β i)] (g₁ g₂ : Π₀ i, β i) : ⇑(g₁ + g₂) = g₁ + g₂ := rfl instance [Π i, add_zero_class (β i)] : add_zero_class (Π₀ i, β i) := fun_like.coe_injective.add_zero_class _ coe_zero coe_add /-- Note the general `dfinsupp.has_smul` instance doesn't apply as `ℕ` is not distributive unless `β i`'s addition is commutative. -/ instance has_nat_scalar [Π i, add_monoid (β i)] : has_smul ℕ (Π₀ i, β i) := ⟨λc v, v.map_range (λ _, (•) c) (λ _, nsmul_zero _)⟩ lemma nsmul_apply [Π i, add_monoid (β i)] (b : ℕ) (v : Π₀ i, β i) (i : ι) : (b • v) i = b • (v i) := rfl @[simp] lemma coe_nsmul [Π i, add_monoid (β i)] (b : ℕ) (v : Π₀ i, β i) : ⇑(b • v) = b • v := rfl instance [Π i, add_monoid (β i)] : add_monoid (Π₀ i, β i) := fun_like.coe_injective.add_monoid _ coe_zero coe_add (λ _ _, coe_nsmul _ _) /-- Coercion from a `dfinsupp` to a pi type is an `add_monoid_hom`. -/ def coe_fn_add_monoid_hom [Π i, add_zero_class (β i)] : (Π₀ i, β i) →+ (Π i, β i) := { to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add } /-- Evaluation at a point is an `add_monoid_hom`. This is the finitely-supported version of `pi.eval_add_monoid_hom`. -/ def eval_add_monoid_hom [Π i, add_zero_class (β i)] (i : ι) : (Π₀ i, β i) →+ β i := (pi.eval_add_monoid_hom β i).comp coe_fn_add_monoid_hom instance [Π i, add_comm_monoid (β i)] : add_comm_monoid (Π₀ i, β i) := fun_like.coe_injective.add_comm_monoid _ coe_zero coe_add (λ _ _, coe_nsmul _ _) @[simp] lemma coe_finset_sum {α} [Π i, add_comm_monoid (β i)] (s : finset α) (g : α → Π₀ i, β i) : ⇑(∑ a in s, g a) = ∑ a in s, g a := (coe_fn_add_monoid_hom : _ →+ (Π i, β i)).map_sum g s @[simp] lemma finset_sum_apply {α} [Π i, add_comm_monoid (β i)] (s : finset α) (g : α → Π₀ i, β i) (i : ι) : (∑ a in s, g a) i = ∑ a in s, g a i := (eval_add_monoid_hom i : _ →+ β i).map_sum g s instance [Π i, add_group (β i)] : has_neg (Π₀ i, β i) := ⟨λ f, f.map_range (λ _, has_neg.neg) (λ _, neg_zero)⟩ lemma neg_apply [Π i, add_group (β i)] (g : Π₀ i, β i) (i : ι) : (- g) i = - g i := rfl @[simp] lemma coe_neg [Π i, add_group (β i)] (g : Π₀ i, β i) : ⇑(- g) = - g := rfl instance [Π i, add_group (β i)] : has_sub (Π₀ i, β i) := ⟨zip_with (λ _, has_sub.sub) (λ _, sub_zero 0)⟩ lemma sub_apply [Π i, add_group (β i)] (g₁ g₂ : Π₀ i, β i) (i : ι) : (g₁ - g₂) i = g₁ i - g₂ i := rfl @[simp] lemma coe_sub [Π i, add_group (β i)] (g₁ g₂ : Π₀ i, β i) : ⇑(g₁ - g₂) = g₁ - g₂ := rfl /-- Note the general `dfinsupp.has_smul` instance doesn't apply as `ℤ` is not distributive unless `β i`'s addition is commutative. -/ instance has_int_scalar [Π i, add_group (β i)] : has_smul ℤ (Π₀ i, β i) := ⟨λc v, v.map_range (λ _, (•) c) (λ _, zsmul_zero _)⟩ lemma zsmul_apply [Π i, add_group (β i)] (b : ℤ) (v : Π₀ i, β i) (i : ι) : (b • v) i = b • (v i) := rfl @[simp] lemma coe_zsmul [Π i, add_group (β i)] (b : ℤ) (v : Π₀ i, β i) : ⇑(b • v) = b • v := rfl instance [Π i, add_group (β i)] : add_group (Π₀ i, β i) := fun_like.coe_injective.add_group _ coe_zero coe_add coe_neg coe_sub (λ _ _, coe_nsmul _ _) (λ _ _, coe_zsmul _ _) instance [Π i, add_comm_group (β i)] : add_comm_group (Π₀ i, β i) := fun_like.coe_injective.add_comm_group _ coe_zero coe_add coe_neg coe_sub (λ _ _, coe_nsmul _ _) (λ _ _, coe_zsmul _ _) /-- Dependent functions with finite support inherit a semiring action from an action on each coordinate. -/ instance [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] : has_smul γ (Π₀ i, β i) := ⟨λc v, v.map_range (λ _, (•) c) (λ _, smul_zero _)⟩ lemma smul_apply [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] (b : γ) (v : Π₀ i, β i) (i : ι) : (b • v) i = b • (v i) := rfl @[simp] lemma coe_smul [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] (b : γ) (v : Π₀ i, β i) : ⇑(b • v) = b • v := rfl instance {δ : Type*} [monoid γ] [monoid δ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] [Π i, distrib_mul_action δ (β i)] [Π i, smul_comm_class γ δ (β i)] : smul_comm_class γ δ (Π₀ i, β i) := { smul_comm := λ r s m, ext $ λ i, by simp only [smul_apply, smul_comm r s (m i)] } instance {δ : Type*} [monoid γ] [monoid δ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] [Π i, distrib_mul_action δ (β i)] [has_smul γ δ] [Π i, is_scalar_tower γ δ (β i)] : is_scalar_tower γ δ (Π₀ i, β i) := { smul_assoc := λ r s m, ext $ λ i, by simp only [smul_apply, smul_assoc r s (m i)] } instance [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] [Π i, distrib_mul_action γᵐᵒᵖ (β i)] [∀ i, is_central_scalar γ (β i)] : is_central_scalar γ (Π₀ i, β i) := { op_smul_eq_smul := λ r m, ext $ λ i, by simp only [smul_apply, op_smul_eq_smul r (m i)] } /-- Dependent functions with finite support inherit a `distrib_mul_action` structure from such a structure on each coordinate. -/ instance [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] : distrib_mul_action γ (Π₀ i, β i) := function.injective.distrib_mul_action coe_fn_add_monoid_hom fun_like.coe_injective coe_smul /-- Dependent functions with finite support inherit a module structure from such a structure on each coordinate. -/ instance [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, module γ (β i)] : module γ (Π₀ i, β i) := { zero_smul := λ c, ext $ λ i, by simp only [smul_apply, zero_smul, zero_apply], add_smul := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, add_smul], ..dfinsupp.distrib_mul_action } end algebra section filter_and_subtype_domain /-- `filter p f` is the function which is `f i` if `p i` is true and 0 otherwise. -/ def filter [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] (x : Π₀ i, β i) : Π₀ i, β i := ⟨λ i, if p i then x i else 0, x.support'.map (λ xs, ⟨xs, λ i, (xs.prop i).imp_right $ λ H : x i = 0, by rw [H, if_t_t]⟩)⟩ @[simp] lemma filter_apply [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] (i : ι) (f : Π₀ i, β i) : f.filter p i = if p i then f i else 0 := rfl lemma filter_apply_pos [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] (f : Π₀ i, β i) {i : ι} (h : p i) : f.filter p i = f i := by simp only [filter_apply, if_pos h] lemma filter_apply_neg [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] (f : Π₀ i, β i) {i : ι} (h : ¬ p i) : f.filter p i = 0 := by simp only [filter_apply, if_neg h] lemma filter_pos_add_filter_neg [Π i, add_zero_class (β i)] (f : Π₀ i, β i) (p : ι → Prop) [decidable_pred p] : f.filter p + f.filter (λi, ¬ p i) = f := ext $ λ i, by simp only [add_apply, filter_apply]; split_ifs; simp only [add_zero, zero_add] @[simp] lemma filter_zero [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] : (0 : Π₀ i, β i).filter p = 0 := by { ext, simp } @[simp] lemma filter_add [Π i, add_zero_class (β i)] (p : ι → Prop) [decidable_pred p] (f g : Π₀ i, β i) : (f + g).filter p = f.filter p + g.filter p := by { ext, simp [ite_add_zero] } @[simp] lemma filter_smul [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] (p : ι → Prop) [decidable_pred p] (r : γ) (f : Π₀ i, β i) : (r • f).filter p = r • f.filter p := by { ext, simp [smul_ite] } variables (γ β) /-- `dfinsupp.filter` as an `add_monoid_hom`. -/ @[simps] def filter_add_monoid_hom [Π i, add_zero_class (β i)] (p : ι → Prop) [decidable_pred p] : (Π₀ i, β i) →+ (Π₀ i, β i) := { to_fun := filter p, map_zero' := filter_zero p, map_add' := filter_add p } /-- `dfinsupp.filter` as a `linear_map`. -/ @[simps] def filter_linear_map [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, module γ (β i)] (p : ι → Prop) [decidable_pred p] : (Π₀ i, β i) →ₗ[γ] (Π₀ i, β i) := { to_fun := filter p, map_add' := filter_add p, map_smul' := filter_smul p } variables {γ β} @[simp] lemma filter_neg [Π i, add_group (β i)] (p : ι → Prop) [decidable_pred p] (f : Π₀ i, β i) : (-f).filter p = -f.filter p := (filter_add_monoid_hom β p).map_neg f @[simp] lemma filter_sub [Π i, add_group (β i)] (p : ι → Prop) [decidable_pred p] (f g : Π₀ i, β i) : (f - g).filter p = f.filter p - g.filter p := (filter_add_monoid_hom β p).map_sub f g /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] (x : Π₀ i, β i) : Π₀ i : subtype p, β i := ⟨λ i, x (i : ι), x.support'.map (λ xs, ⟨(multiset.filter p xs).attach.map $ λ j, ⟨j, (multiset.mem_filter.1 j.2).2⟩, λ i, (xs.prop i).imp_left $ λ H, multiset.mem_map.2 ⟨⟨i, multiset.mem_filter.2 ⟨H, i.2⟩⟩, multiset.mem_attach _ _, subtype.eta _ _⟩⟩)⟩ @[simp] lemma subtype_domain_zero [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] : subtype_domain p (0 : Π₀ i, β i) = 0 := rfl @[simp] lemma subtype_domain_apply [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {i : subtype p} {v : Π₀ i, β i} : (subtype_domain p v) i = v i := rfl @[simp] lemma subtype_domain_add [Π i, add_zero_class (β i)] {p : ι → Prop} [decidable_pred p] (v v' : Π₀ i, β i) : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := coe_fn_injective rfl @[simp] lemma subtype_domain_smul [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] {p : ι → Prop} [decidable_pred p] (r : γ) (f : Π₀ i, β i) : (r • f).subtype_domain p = r • f.subtype_domain p := coe_fn_injective rfl variables (γ β) /-- `subtype_domain` but as an `add_monoid_hom`. -/ @[simps] def subtype_domain_add_monoid_hom [Π i, add_zero_class (β i)] (p : ι → Prop) [decidable_pred p] : (Π₀ i : ι, β i) →+ Π₀ i : subtype p, β i := { to_fun := subtype_domain p, map_zero' := subtype_domain_zero, map_add' := subtype_domain_add } /-- `dfinsupp.subtype_domain` as a `linear_map`. -/ @[simps] def subtype_domain_linear_map [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, module γ (β i)] (p : ι → Prop) [decidable_pred p] : (Π₀ i, β i) →ₗ[γ] (Π₀ i : subtype p, β i) := { to_fun := subtype_domain p, map_add' := subtype_domain_add, map_smul' := subtype_domain_smul } variables {γ β} @[simp] lemma subtype_domain_neg [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p] {v : Π₀ i, β i} : (- v).subtype_domain p = - v.subtype_domain p := coe_fn_injective rfl @[simp] lemma subtype_domain_sub [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p] {v v' : Π₀ i, β i} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := coe_fn_injective rfl end filter_and_subtype_domain variable [dec : decidable_eq ι] include dec section basic variable [Π i, has_zero (β i)] omit dec lemma finite_support (f : Π₀ i, β i) : set.finite {i | f i ≠ 0} := begin classical, exact trunc.induction_on f.support' (λ xs, (multiset.to_finset ↑xs).finite_to_set.subset (λ i H, multiset.mem_to_finset.2 ((xs.prop i).resolve_right H))) end include dec /-- Create an element of `Π₀ i, β i` from a finset `s` and a function `x` defined on this `finset`. -/ def mk (s : finset ι) (x : Π i : (↑s : set ι), β (i : ι)) : Π₀ i, β i := ⟨λ i, if H : i ∈ s then x ⟨i, H⟩ else 0, trunc.mk ⟨s.1, λ i, if H : i ∈ s then or.inl H else or.inr $ dif_neg H⟩⟩ variables {s : finset ι} {x : Π i : (↑s : set ι), β i} {i : ι} @[simp] lemma mk_apply : (mk s x : Π i, β i) i = if H : i ∈ s then x ⟨i, H⟩ else 0 := rfl lemma mk_of_mem (hi : i ∈ s) : (mk s x : Π i, β i) i = x ⟨i, hi⟩ := dif_pos hi lemma mk_of_not_mem (hi : i ∉ s) : (mk s x : Π i, β i) i = 0 := dif_neg hi theorem mk_injective (s : finset ι) : function.injective (@mk ι β _ _ s) := begin intros x y H, ext i, have h1 : (mk s x : Π i, β i) i = (mk s y : Π i, β i) i, {rw H}, cases i with i hi, change i ∈ s at hi, dsimp only [mk_apply, subtype.coe_mk] at h1, simpa only [dif_pos hi] using h1 end omit dec instance unique [∀ i, subsingleton (β i)] : unique (Π₀ i, β i) := fun_like.coe_injective.unique instance unique_of_is_empty [is_empty ι] : unique (Π₀ i, β i) := fun_like.coe_injective.unique /-- Given `fintype ι`, `equiv_fun_on_fintype` is the `equiv` between `Π₀ i, β i` and `Π i, β i`. (All dependent functions on a finite type are finitely supported.) -/ @[simps apply] def equiv_fun_on_fintype [fintype ι] : (Π₀ i, β i) ≃ (Π i, β i) := { to_fun := coe_fn, inv_fun := λ f, ⟨f, trunc.mk ⟨finset.univ.1, λ i, or.inl $ finset.mem_univ_val _⟩⟩, left_inv := λ x, coe_fn_injective rfl, right_inv := λ x, rfl } @[simp] lemma equiv_fun_on_fintype_symm_coe [fintype ι] (f : Π₀ i, β i) : equiv_fun_on_fintype.symm f = f := equiv.symm_apply_apply _ _ include dec /-- The function `single i b : Π₀ i, β i` sends `i` to `b` and all other points to `0`. -/ def single (i : ι) (b : β i) : Π₀ i, β i := ⟨pi.single i b, trunc.mk ⟨{i}, λ j, (decidable.eq_or_ne j i).imp (by simp) (λ h, pi.single_eq_of_ne h _)⟩⟩ lemma single_eq_pi_single {i b} : ⇑(single i b : Π₀ i, β i) = pi.single i b := rfl @[simp] lemma single_apply {i i' b} : (single i b : Π₀ i, β i) i' = (if h : i = i' then eq.rec_on h b else 0) := begin rw [single_eq_pi_single, pi.single, function.update], simp [@eq_comm _ i i'], end @[simp] lemma single_zero (i) : (single i 0 : Π₀ i, β i) = 0 := fun_like.coe_injective $ pi.single_zero _ @[simp] lemma single_eq_same {i b} : (single i b : Π₀ i, β i) i = b := by simp only [single_apply, dif_pos rfl] lemma single_eq_of_ne {i i' b} (h : i ≠ i') : (single i b : Π₀ i, β i) i' = 0 := by simp only [single_apply, dif_neg h] lemma single_injective {i} : function.injective (single i : β i → Π₀ i, β i) := λ x y H, pi.single_injective β i $ coe_fn_injective.eq_iff.mpr H /-- Like `finsupp.single_eq_single_iff`, but with a `heq` due to dependent types -/ lemma single_eq_single_iff (i j : ι) (xi : β i) (xj : β j) : dfinsupp.single i xi = dfinsupp.single j xj ↔ i = j ∧ xi == xj ∨ xi = 0 ∧ xj = 0 := begin split, { intro h, by_cases hij : i = j, { subst hij, exact or.inl ⟨rfl, heq_of_eq (dfinsupp.single_injective h)⟩, }, { have h_coe : ⇑(dfinsupp.single i xi) = dfinsupp.single j xj := congr_arg coe_fn h, have hci := congr_fun h_coe i, have hcj := congr_fun h_coe j, rw dfinsupp.single_eq_same at hci hcj, rw dfinsupp.single_eq_of_ne (ne.symm hij) at hci, rw dfinsupp.single_eq_of_ne (hij) at hcj, exact or.inr ⟨hci, hcj.symm⟩, }, }, { rintros (⟨rfl, hxi⟩ | ⟨hi, hj⟩), { rw eq_of_heq hxi, }, { rw [hi, hj, dfinsupp.single_zero, dfinsupp.single_zero], }, }, end /-- `dfinsupp.single a b` is injective in `a`. For the statement that it is injective in `b`, see `dfinsupp.single_injective` -/ lemma single_left_injective {b : Π (i : ι), β i} (h : ∀ i, b i ≠ 0) : function.injective (λ i, single i (b i) : ι → Π₀ i, β i) := λ a a' H, (((single_eq_single_iff _ _ _ _).mp H).resolve_right $ λ hb, h _ hb.1).left @[simp] lemma single_eq_zero {i : ι} {xi : β i} : single i xi = 0 ↔ xi = 0 := begin rw [←single_zero i, single_eq_single_iff], simp, end lemma filter_single (p : ι → Prop) [decidable_pred p] (i : ι) (x : β i) : (single i x).filter p = if p i then single i x else 0 := begin ext j, have := apply_ite (λ x : Π₀ i, β i, x j) (p i) (single i x) 0, dsimp at this, rw [filter_apply, this], obtain rfl | hij := decidable.eq_or_ne i j, { refl, }, { rw [single_eq_of_ne hij, if_t_t, if_t_t], }, end @[simp] lemma filter_single_pos {p : ι → Prop} [decidable_pred p] (i : ι) (x : β i) (h : p i) : (single i x).filter p = single i x := by rw [filter_single, if_pos h] @[simp] lemma filter_single_neg {p : ι → Prop} [decidable_pred p] (i : ι) (x : β i) (h : ¬p i) : (single i x).filter p = 0 := by rw [filter_single, if_neg h] /-- Equality of sigma types is sufficient (but not necessary) to show equality of `dfinsupp`s. -/ lemma single_eq_of_sigma_eq {i j} {xi : β i} {xj : β j} (h : (⟨i, xi⟩ : sigma β) = ⟨j, xj⟩) : dfinsupp.single i xi = dfinsupp.single j xj := by { cases h, refl } @[simp] lemma equiv_fun_on_fintype_single [fintype ι] (i : ι) (m : β i) : (@dfinsupp.equiv_fun_on_fintype ι β _ _) (dfinsupp.single i m) = pi.single i m := by { ext, simp [dfinsupp.single_eq_pi_single], } @[simp] lemma equiv_fun_on_fintype_symm_single [fintype ι] (i : ι) (m : β i) : (@dfinsupp.equiv_fun_on_fintype ι β _ _).symm (pi.single i m) = dfinsupp.single i m := by { ext i', simp only [← single_eq_pi_single, equiv_fun_on_fintype_symm_coe] } /-- Redefine `f i` to be `0`. -/ def erase (i : ι) (x : Π₀ i, β i) : Π₀ i, β i := ⟨λ j, if j = i then 0 else x.1 j, x.support'.map $ λ xs, ⟨xs, λ j, (xs.prop j).imp_right $ λ H, by simp only [H, if_t_t]⟩⟩ @[simp] lemma erase_apply {i j : ι} {f : Π₀ i, β i} : (f.erase i) j = if j = i then 0 else f j := rfl @[simp] lemma erase_same {i : ι} {f : Π₀ i, β i} : (f.erase i) i = 0 := by simp lemma erase_ne {i i' : ι} {f : Π₀ i, β i} (h : i' ≠ i) : (f.erase i) i' = f i' := by simp [h] lemma piecewise_single_erase (x : Π₀ i, β i) (i : ι) : (single i (x i)).piecewise (x.erase i) {i} = x := begin ext j, rw piecewise_apply, split_ifs, { rw [(id h : j = i), single_eq_same] }, { exact erase_ne h }, end lemma erase_eq_sub_single {β : ι → Type*} [Π i, add_group (β i)] (f : Π₀ i, β i) (i : ι) : f.erase i = f - single i (f i) := begin ext j, rcases eq_or_ne i j with rfl|h, { simp }, { simp [erase_ne h.symm, single_eq_of_ne h] } end @[simp] lemma erase_zero (i : ι) : erase i (0 : Π₀ i, β i) = 0 := ext $ λ _, if_t_t _ _ @[simp] lemma filter_ne_eq_erase (f : Π₀ i, β i) (i : ι) : f.filter (≠ i) = f.erase i := begin ext1 j, simp only [dfinsupp.filter_apply, dfinsupp.erase_apply, ite_not], end @[simp] lemma filter_ne_eq_erase' (f : Π₀ i, β i) (i : ι) : f.filter ((≠) i) = f.erase i := begin rw ←filter_ne_eq_erase f i, congr' with j, exact ne_comm, end lemma erase_single (j : ι) (i : ι) (x : β i) : (single i x).erase j = if i = j then 0 else single i x := by rw [←filter_ne_eq_erase, filter_single, ite_not] @[simp] lemma erase_single_same (i : ι) (x : β i) : (single i x).erase i = 0 := by rw [erase_single, if_pos rfl] @[simp] lemma erase_single_ne {i j : ι} (x : β i) (h : i ≠ j) : (single i x).erase j = single i x := by rw [erase_single, if_neg h] section update variables (f : Π₀ i, β i) (i) (b : β i) /-- Replace the value of a `Π₀ i, β i` at a given point `i : ι` by a given value `b : β i`. If `b = 0`, this amounts to removing `i` from the support. Otherwise, `i` is added to it. This is the (dependent) finitely-supported version of `function.update`. -/ def update : Π₀ i, β i := ⟨function.update f i b, f.support'.map $ λ s, ⟨i ::ₘ s, λ j, begin rcases eq_or_ne i j with rfl|hi, { simp, }, { obtain hj | (hj : f j = 0) := s.prop j, { exact or.inl (multiset.mem_cons_of_mem hj), }, { exact or.inr ((function.update_noteq hi.symm b _).trans hj) } } end⟩⟩ variables (j : ι) @[simp] lemma coe_update : (f.update i b : Π (i : ι), β i) = function.update f i b := rfl @[simp] lemma update_self : f.update i (f i) = f := by { ext, simp } @[simp] lemma update_eq_erase : f.update i 0 = f.erase i := begin ext j, rcases eq_or_ne i j with rfl|hi, { simp }, { simp [hi.symm] } end lemma update_eq_single_add_erase {β : ι → Type*} [Π i, add_zero_class (β i)] (f : Π₀ i, β i) (i : ι) (b : β i) : f.update i b = single i b + f.erase i := begin ext j, rcases eq_or_ne i j with rfl|h, { simp }, { simp [function.update_noteq h.symm, h, erase_ne, h.symm] } end lemma update_eq_erase_add_single {β : ι → Type*} [Π i, add_zero_class (β i)] (f : Π₀ i, β i) (i : ι) (b : β i) : f.update i b = f.erase i + single i b := begin ext j, rcases eq_or_ne i j with rfl|h, { simp }, { simp [function.update_noteq h.symm, h, erase_ne, h.symm] } end lemma update_eq_sub_add_single {β : ι → Type*} [Π i, add_group (β i)] (f : Π₀ i, β i) (i : ι) (b : β i) : f.update i b = f - single i (f i) + single i b := by rw [update_eq_erase_add_single f i b, erase_eq_sub_single f i] end update end basic section add_monoid variable [Π i, add_zero_class (β i)] @[simp] lemma single_add (i : ι) (b₁ b₂ : β i) : single i (b₁ + b₂) = single i b₁ + single i b₂ := ext $ assume i', begin by_cases h : i = i', { subst h, simp only [add_apply, single_eq_same] }, { simp only [add_apply, single_eq_of_ne h, zero_add] } end @[simp] lemma erase_add (i : ι) (f₁ f₂ : Π₀ i, β i) : erase i (f₁ + f₂) = erase i f₁ + erase i f₂ := ext $ λ _, by simp [ite_zero_add] variables (β) /-- `dfinsupp.single` as an `add_monoid_hom`. -/ @[simps] def single_add_hom (i : ι) : β i →+ Π₀ i, β i := { to_fun := single i, map_zero' := single_zero i, map_add' := single_add i } /-- `dfinsupp.erase` as an `add_monoid_hom`. -/ @[simps] def erase_add_hom (i : ι) : (Π₀ i, β i) →+ Π₀ i, β i := { to_fun := erase i, map_zero' := erase_zero i, map_add' := erase_add i } variables {β} @[simp] lemma single_neg {β : ι → Type v} [Π i, add_group (β i)] (i : ι) (x : β i) : single i (-x) = -single i x := (single_add_hom β i).map_neg x @[simp] lemma single_sub {β : ι → Type v} [Π i, add_group (β i)] (i : ι) (x y : β i) : single i (x - y) = single i x - single i y := (single_add_hom β i).map_sub x y @[simp] lemma erase_neg {β : ι → Type v} [Π i, add_group (β i)] (i : ι) (f : Π₀ i, β i) : (-f).erase i = -f.erase i := (erase_add_hom β i).map_neg f @[simp] lemma erase_sub {β : ι → Type v} [Π i, add_group (β i)] (i : ι) (f g : Π₀ i, β i) : (f - g).erase i = f.erase i - g.erase i := (erase_add_hom β i).map_sub f g lemma single_add_erase (i : ι) (f : Π₀ i, β i) : single i (f i) + f.erase i = f := ext $ λ i', if h : i = i' then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, add_zero] else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), zero_add] lemma erase_add_single (i : ι) (f : Π₀ i, β i) : f.erase i + single i (f i) = f := ext $ λ i', if h : i = i' then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, zero_add] else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), add_zero] protected theorem induction {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i) (h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (single i b + f)) : p f := begin cases f with f s, induction s using trunc.induction_on, cases s with s H, induction s using multiset.induction_on with i s ih generalizing f, { have : f = 0 := funext (λ i, (H i).resolve_left id), subst this, exact h0 }, have H2 : p (erase i ⟨f, trunc.mk ⟨i ::ₘ s, H⟩⟩), { dsimp only [erase, trunc.map, trunc.bind, trunc.lift_on, trunc.lift_mk, function.comp, subtype.coe_mk], have H2 : ∀ j, j ∈ s ∨ ite (j = i) 0 (f j) = 0, { intro j, cases H j with H2 H2, { cases multiset.mem_cons.1 H2 with H3 H3, { right, exact if_pos H3 }, { left, exact H3 } }, right, split_ifs; [refl, exact H2] }, have H3 : (⟨λ (j : ι), ite (j = i) 0 (f j), trunc.mk ⟨i ::ₘ s, _⟩⟩ : Π₀ i, β i) = ⟨λ (j : ι), ite (j = i) 0 (f j), trunc.mk ⟨s, H2⟩⟩ := ext (λ _, rfl), rw H3, apply ih }, have H3 : single i _ + _ = (⟨f, trunc.mk ⟨i ::ₘ s, H⟩⟩ : Π₀ i, β i) := single_add_erase _ _, rw ← H3, change p (single i (f i) + _), cases classical.em (f i = 0) with h h, { rw [h, single_zero, zero_add], exact H2 }, refine ha _ _ _ _ h H2, rw erase_same end lemma induction₂ {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i) (h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (f + single i b)) : p f := dfinsupp.induction f h0 $ λ i b f h1 h2 h3, have h4 : f + single i b = single i b + f, { ext j, by_cases H : i = j, { subst H, simp [h1] }, { simp [H] } }, eq.rec_on h4 $ ha i b f h1 h2 h3 @[simp] lemma add_closure_Union_range_single : add_submonoid.closure (⋃ i : ι, set.range (single i : β i → (Π₀ i, β i))) = ⊤ := top_unique $ λ x hx, (begin apply dfinsupp.induction x, exact add_submonoid.zero_mem _, exact λ a b f ha hb hf, add_submonoid.add_mem _ (add_submonoid.subset_closure $ set.mem_Union.2 ⟨a, set.mem_range_self _⟩) hf end) /-- If two additive homomorphisms from `Π₀ i, β i` are equal on each `single a b`, then they are equal. -/ lemma add_hom_ext {γ : Type w} [add_zero_class γ] ⦃f g : (Π₀ i, β i) →+ γ⦄ (H : ∀ (i : ι) (y : β i), f (single i y) = g (single i y)) : f = g := begin refine add_monoid_hom.eq_of_eq_on_mdense add_closure_Union_range_single (λ f hf, _), simp only [set.mem_Union, set.mem_range] at hf, rcases hf with ⟨x, y, rfl⟩, apply H end /-- If two additive homomorphisms from `Π₀ i, β i` are equal on each `single a b`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma add_hom_ext' {γ : Type w} [add_zero_class γ] ⦃f g : (Π₀ i, β i) →+ γ⦄ (H : ∀ x, f.comp (single_add_hom β x) = g.comp (single_add_hom β x)) : f = g := add_hom_ext $ λ x, add_monoid_hom.congr_fun (H x) end add_monoid @[simp] lemma mk_add [Π i, add_zero_class (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i} : mk s (x + y) = mk s x + mk s y := ext $ λ i, by simp only [add_apply, mk_apply]; split_ifs; [refl, rw zero_add] @[simp] lemma mk_zero [Π i, has_zero (β i)] {s : finset ι} : mk s (0 : Π i : (↑s : set ι), β i.1) = 0 := ext $ λ i, by simp only [mk_apply]; split_ifs; refl @[simp] lemma mk_neg [Π i, add_group (β i)] {s : finset ι} {x : Π i : (↑s : set ι), β i.1} : mk s (-x) = -mk s x := ext $ λ i, by simp only [neg_apply, mk_apply]; split_ifs; [refl, rw neg_zero] @[simp] lemma mk_sub [Π i, add_group (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i.1} : mk s (x - y) = mk s x - mk s y := ext $ λ i, by simp only [sub_apply, mk_apply]; split_ifs; [refl, rw sub_zero] /-- If `s` is a subset of `ι` then `mk_add_group_hom s` is the canonical additive group homomorphism from $\prod_{i\in s}\beta_i$ to $\prod_{\mathtt{i : \iota}}\beta_i.$-/ def mk_add_group_hom [Π i, add_group (β i)] (s : finset ι) : (Π (i : (s : set ι)), β ↑i) →+ (Π₀ (i : ι), β i) := { to_fun := mk s, map_zero' := mk_zero, map_add' := λ _ _, mk_add } section variables [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] @[simp] lemma mk_smul {s : finset ι} (c : γ) (x : Π i : (↑s : set ι), β (i : ι)) : mk s (c • x) = c • mk s x := ext $ λ i, by simp only [smul_apply, mk_apply]; split_ifs; [refl, rw smul_zero] @[simp] lemma single_smul {i : ι} (c : γ) (x : β i) : single i (c • x) = c • single i x := ext $ λ i, by simp only [smul_apply, single_apply]; split_ifs; [cases h, rw smul_zero]; refl end section support_basic variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] /-- Set `{i | f x ≠ 0}` as a `finset`. -/ def support (f : Π₀ i, β i) : finset ι := f.support'.lift (λ xs, (multiset.to_finset ↑xs).filter $ λ i, f i ≠ 0) $ begin rintros ⟨sx, hx⟩ ⟨sy, hy⟩, dsimp only [subtype.coe_mk, to_fun_eq_coe] at *, ext i, split, { intro H, rcases finset.mem_filter.1 H with ⟨h1, h2⟩, exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (hy i).resolve_right h2, h2⟩ }, { intro H, rcases finset.mem_filter.1 H with ⟨h1, h2⟩, exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (hx i).resolve_right h2, h2⟩ }, end @[simp] theorem support_mk_subset {s : finset ι} {x : Π i : (↑s : set ι), β i.1} : (mk s x).support ⊆ s := λ i H, multiset.mem_to_finset.1 (finset.mem_filter.1 H).1 @[simp] theorem support_mk'_subset {f : Π i, β i} {s : multiset ι} {h} : (mk' f $ trunc.mk ⟨s, h⟩).support ⊆ s.to_finset := λ i H, multiset.mem_to_finset.1 $ by simpa using (finset.mem_filter.1 H).1 @[simp] theorem mem_support_to_fun (f : Π₀ i, β i) (i) : i ∈ f.support ↔ f i ≠ 0 := begin cases f with f s, induction s using trunc.induction_on, dsimp only [support, trunc.lift_mk], rw [finset.mem_filter, multiset.mem_to_finset, coe_mk'], exact and_iff_right_of_imp (s.prop i).resolve_right end theorem eq_mk_support (f : Π₀ i, β i) : f = mk f.support (λ i, f i) := begin change f = mk f.support (λ i, f i.1), ext i, by_cases h : f i ≠ 0; [skip, rw [not_not] at h]; simp [h] end @[simp] lemma support_zero : (0 : Π₀ i, β i).support = ∅ := rfl lemma mem_support_iff {f : Π₀ i, β i} {i : ι} : i ∈ f.support ↔ f i ≠ 0 := f.mem_support_to_fun _ lemma not_mem_support_iff {f : Π₀ i, β i} {i : ι} : i ∉ f.support ↔ f i = 0 := not_iff_comm.1 mem_support_iff.symm @[simp] lemma support_eq_empty {f : Π₀ i, β i} : f.support = ∅ ↔ f = 0 := ⟨λ H, ext $ by simpa [finset.ext_iff] using H, by simp {contextual:=tt}⟩ instance decidable_zero : decidable_pred (eq (0 : Π₀ i, β i)) := λ f, decidable_of_iff _ $ support_eq_empty.trans eq_comm lemma support_subset_iff {s : set ι} {f : Π₀ i, β i} : ↑f.support ⊆ s ↔ (∀i∉s, f i = 0) := by simp [set.subset_def]; exact forall_congr (assume i, not_imp_comm) lemma support_single_ne_zero {i : ι} {b : β i} (hb : b ≠ 0) : (single i b).support = {i} := begin ext j, by_cases h : i = j, { subst h, simp [hb] }, simp [ne.symm h, h] end lemma support_single_subset {i : ι} {b : β i} : (single i b).support ⊆ {i} := support_mk'_subset section map_range_and_zip_with variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] lemma map_range_def [Π i (x : β₁ i), decidable (x ≠ 0)] {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} : map_range f hf g = mk g.support (λ i, f i.1 (g i.1)) := begin ext i, by_cases h : g i ≠ 0; simp at h; simp [h, hf] end @[simp] lemma map_range_single {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {i : ι} {b : β₁ i} : map_range f hf (single i b) = single i (f i b) := dfinsupp.ext $ λ i', by by_cases i = i'; [{subst i', simp}, simp [h, hf]] variables [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)] lemma support_map_range {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} : (map_range f hf g).support ⊆ g.support := by simp [map_range_def] lemma zip_with_def {ι : Type u} {β : ι → Type v} {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} [dec : decidable_eq ι] [Π (i : ι), has_zero (β i)] [Π (i : ι), has_zero (β₁ i)] [Π (i : ι), has_zero (β₂ i)] [Π (i : ι) (x : β₁ i), decidable (x ≠ 0)] [Π (i : ι) (x : β₂ i), decidable (x ≠ 0)] {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} : zip_with f hf g₁ g₂ = mk (g₁.support ∪ g₂.support) (λ i, f i.1 (g₁ i.1) (g₂ i.1)) := begin ext i, by_cases h1 : g₁ i ≠ 0; by_cases h2 : g₂ i ≠ 0; simp only [not_not, ne.def] at h1 h2; simp [h1, h2, hf] end lemma support_zip_with {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := by simp [zip_with_def] end map_range_and_zip_with lemma erase_def (i : ι) (f : Π₀ i, β i) : f.erase i = mk (f.support.erase i) (λ j, f j.1) := by { ext j, by_cases h1 : j = i; by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] } @[simp] lemma support_erase (i : ι) (f : Π₀ i, β i) : (f.erase i).support = f.support.erase i := by { ext j, by_cases h1 : j = i, simp [h1], by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] } lemma support_update_ne_zero (f : Π₀ i, β i) (i : ι) {b : β i} (h : b ≠ 0) : support (f.update i b) = insert i f.support := begin ext j, rcases eq_or_ne i j with rfl|hi, { simp [h] }, { simp [hi.symm] } end lemma support_update (f : Π₀ i, β i) (i : ι) (b : β i) [decidable (b = 0)] : support (f.update i b) = if b = 0 then support (f.erase i) else insert i f.support := begin ext j, split_ifs with hb, { substI hb, simp [update_eq_erase, support_erase] }, { rw [support_update_ne_zero f _ hb] } end section filter_and_subtype_domain variables {p : ι → Prop} [decidable_pred p] lemma filter_def (f : Π₀ i, β i) : f.filter p = mk (f.support.filter p) (λ i, f i.1) := by ext i; by_cases h1 : p i; by_cases h2 : f i ≠ 0; simp at h2; simp [h1, h2] @[simp] lemma support_filter (f : Π₀ i, β i) : (f.filter p).support = f.support.filter p := by ext i; by_cases h : p i; simp [h] lemma subtype_domain_def (f : Π₀ i, β i) : f.subtype_domain p = mk (f.support.subtype p) (λ i, f i) := by ext i; by_cases h2 : f i ≠ 0; try {simp at h2}; dsimp; simp [h2] @[simp] lemma support_subtype_domain {f : Π₀ i, β i} : (subtype_domain p f).support = f.support.subtype p := by { ext i, simp, } end filter_and_subtype_domain end support_basic lemma support_add [Π i, add_zero_class (β i)] [Π i (x : β i), decidable (x ≠ 0)] {g₁ g₂ : Π₀ i, β i} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with @[simp] lemma support_neg [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)] {f : Π₀ i, β i} : support (-f) = support f := by ext i; simp lemma support_smul {γ : Type w} [semiring γ] [Π i, add_comm_monoid (β i)] [Π i, module γ (β i)] [Π ( i : ι) (x : β i), decidable (x ≠ 0)] (b : γ) (v : Π₀ i, β i) : (b • v).support ⊆ v.support := support_map_range instance [Π i, has_zero (β i)] [Π i, decidable_eq (β i)] : decidable_eq (Π₀ i, β i) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀i∈f.support, f i = g i)) ⟨assume ⟨h₁, h₂⟩, ext $ assume i, if h : i ∈ f.support then h₂ i h else have hf : f i = 0, by rwa [mem_support_iff, not_not] at h, have hg : g i = 0, by rwa [h₁, mem_support_iff, not_not] at h, by rw [hf, hg], by { rintro rfl, simp }⟩ section equiv open finset variables {κ : Type*} /--Reindexing (and possibly removing) terms of a dfinsupp.-/ noncomputable def comap_domain [Π i, has_zero (β i)] (h : κ → ι) (hh : function.injective h) (f : Π₀ i, β i) : Π₀ k, β (h k) := { to_fun := λ x, f (h x), support' := f.support'.map $ λ s, ⟨((multiset.to_finset ↑s).preimage h (hh.inj_on _)).val, λ x, (s.prop (h x)).imp_left $ λ hx, mem_preimage.mpr $ multiset.mem_to_finset.mpr hx ⟩ } @[simp] lemma comap_domain_apply [Π i, has_zero (β i)] (h : κ → ι) (hh : function.injective h) (f : Π₀ i, β i) (k : κ) : comap_domain h hh f k = f (h k) := rfl @[simp] lemma comap_domain_zero [Π i, has_zero (β i)] (h : κ → ι) (hh : function.injective h) : comap_domain h hh (0 : Π₀ i, β i) = 0 := by { ext, rw [zero_apply, comap_domain_apply, zero_apply] } @[simp] lemma comap_domain_add [Π i, add_zero_class (β i)] (h : κ → ι) (hh : function.injective h) (f g : Π₀ i, β i) : comap_domain h hh (f + g) = comap_domain h hh f + comap_domain h hh g := by { ext, rw [add_apply, comap_domain_apply, comap_domain_apply, comap_domain_apply, add_apply] } @[simp] lemma comap_domain_smul [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] (h : κ → ι) (hh : function.injective h) (r : γ) (f : Π₀ i, β i) : comap_domain h hh (r • f) = r • comap_domain h hh f := by { ext, rw [smul_apply, comap_domain_apply, smul_apply, comap_domain_apply] } @[simp] lemma comap_domain_single [decidable_eq κ] [Π i, has_zero (β i)] (h : κ → ι) (hh : function.injective h) (k : κ) (x : β (h k)) : comap_domain h hh (single (h k) x) = single k x := begin ext, rw comap_domain_apply, obtain rfl | hik := decidable.eq_or_ne i k, { rw [single_eq_same, single_eq_same] }, { rw [single_eq_of_ne hik.symm, single_eq_of_ne (hh.ne hik.symm)] }, end omit dec /--A computable version of comap_domain when an explicit left inverse is provided.-/ def comap_domain' [Π i, has_zero (β i)] (h : κ → ι) {h' : ι → κ} (hh' : function.left_inverse h' h) (f : Π₀ i, β i) : (Π₀ k, β (h k)) := { to_fun := λ x, f (h x), support' := f.support'.map $ λ s, ⟨multiset.map h' s, λ x, (s.prop (h x)).imp_left $ λ hx, multiset.mem_map.mpr ⟨_, hx, hh' _⟩⟩ } @[simp] lemma comap_domain'_apply [Π i, has_zero (β i)] (h : κ → ι) {h' : ι → κ} (hh' : function.left_inverse h' h) (f : Π₀ i, β i) (k : κ) : comap_domain' h hh' f k = f (h k) := rfl @[simp] lemma comap_domain'_zero [Π i, has_zero (β i)] (h : κ → ι) {h' : ι → κ} (hh' : function.left_inverse h' h) : comap_domain' h hh' (0 : Π₀ i, β i) = 0 := by { ext, rw [zero_apply, comap_domain'_apply, zero_apply] } @[simp] lemma comap_domain'_add [Π i, add_zero_class (β i)] (h : κ → ι) {h' : ι → κ} (hh' : function.left_inverse h' h) (f g : Π₀ i, β i) : comap_domain' h hh' (f + g) = comap_domain' h hh' f + comap_domain' h hh' g := by { ext, rw [add_apply, comap_domain'_apply, comap_domain'_apply, comap_domain'_apply, add_apply] } @[simp] lemma comap_domain'_smul [monoid γ] [Π i, add_monoid (β i)] [Π i, distrib_mul_action γ (β i)] (h : κ → ι) {h' : ι → κ} (hh' : function.left_inverse h' h) (r : γ) (f : Π₀ i, β i) : comap_domain' h hh' (r • f) = r • comap_domain' h hh' f := by { ext, rw [smul_apply, comap_domain'_apply, smul_apply, comap_domain'_apply] } @[simp] lemma comap_domain'_single [decidable_eq ι] [decidable_eq κ] [Π i, has_zero (β i)] (h : κ → ι) {h' : ι → κ} (hh' : function.left_inverse h' h) (k : κ) (x : β (h k)) : comap_domain' h hh' (single (h k) x) = single k x := begin ext, rw comap_domain'_apply, obtain rfl | hik := decidable.eq_or_ne i k, { rw [single_eq_same, single_eq_same] }, { rw [single_eq_of_ne hik.symm, single_eq_of_ne (hh'.injective.ne hik.symm)] }, end /-- Reindexing terms of a dfinsupp. This is the dfinsupp version of `equiv.Pi_congr_left'`. -/ @[simps apply] def equiv_congr_left [Π i, has_zero (β i)] (h : ι ≃ κ) : (Π₀ i, β i) ≃ (Π₀ k, β (h.symm k)) := { to_fun := comap_domain' h.symm h.right_inv, inv_fun := λ f, map_range (λ i, equiv.cast $ congr_arg β $ h.symm_apply_apply i) (λ i, (equiv.cast_eq_iff_heq _).mpr $ by { convert heq.rfl, repeat { exact (h.symm_apply_apply i).symm } }) (@comap_domain' _ _ _ _ h _ h.left_inv f), left_inv := λ f, by { ext i, rw [map_range_apply, comap_domain'_apply, comap_domain'_apply, equiv.cast_eq_iff_heq, h.symm_apply_apply] }, right_inv := λ f, by { ext k, rw [comap_domain'_apply, map_range_apply, comap_domain'_apply, equiv.cast_eq_iff_heq, h.apply_symm_apply] } } section curry variables {α : ι → Type*} {δ : Π i, α i → Type v} -- lean can't find these instances instance has_add₂ [Π i j, add_zero_class (δ i j)] : has_add (Π₀ (i : ι) (j : α i), δ i j) := @dfinsupp.has_add ι (λ i, Π₀ j, δ i j) _ instance add_zero_class₂ [Π i j, add_zero_class (δ i j)] : add_zero_class (Π₀ (i : ι) (j : α i), δ i j) := @dfinsupp.add_zero_class ι (λ i, Π₀ j, δ i j) _ instance add_monoid₂ [Π i j, add_monoid (δ i j)] : add_monoid (Π₀ (i : ι) (j : α i), δ i j) := @dfinsupp.add_monoid ι (λ i, Π₀ j, δ i j) _ instance distrib_mul_action₂ [monoid γ] [Π i j, add_monoid (δ i j)] [Π i j, distrib_mul_action γ (δ i j)] : distrib_mul_action γ (Π₀ (i : ι) (j : α i), δ i j) := @dfinsupp.distrib_mul_action ι _ (λ i, Π₀ j, δ i j) _ _ _ /--The natural map between `Π₀ (i : Σ i, α i), δ i.1 i.2` and `Π₀ i (j : α i), δ i j`. -/ noncomputable def sigma_curry [Π i j, has_zero (δ i j)] (f : Π₀ (i : Σ i, _), δ i.1 i.2) : Π₀ i j, δ i j := by { classical, exact mk (f.support.image $ λ i, i.1) (λ i, mk (f.support.preimage (sigma.mk i) $ sigma_mk_injective.inj_on _) $ λ j, f ⟨i, j⟩) } @[simp] lemma sigma_curry_apply [Π i j, has_zero (δ i j)] (f : Π₀ (i : Σ i, _), δ i.1 i.2) (i : ι) (j : α i) : sigma_curry f i j = f ⟨i, j⟩ := begin dunfold sigma_curry, by_cases h : f ⟨i, j⟩ = 0, { rw [h, mk_apply], split_ifs, { rw mk_apply, split_ifs, { exact h }, { refl } }, { refl } }, { rw [mk_of_mem, mk_of_mem], { refl }, { rw [mem_preimage, mem_support_to_fun], exact h }, { rw mem_image, refine ⟨⟨i, j⟩, _, rfl⟩, rw mem_support_to_fun, exact h } } end @[simp] lemma sigma_curry_zero [Π i j, has_zero (δ i j)] : sigma_curry (0 : Π₀ (i : Σ i, _), δ i.1 i.2) = 0 := by { ext i j, rw sigma_curry_apply, refl } @[simp] lemma sigma_curry_add [Π i j, add_zero_class (δ i j)] (f g : Π₀ (i : Σ i, α i), δ i.1 i.2) : @sigma_curry _ _ δ _ (f + g) = (@sigma_curry _ _ δ _ f + @sigma_curry ι α δ _ g) := begin ext i j, rw [@add_apply _ (λ i, Π₀ j, δ i j) _ (sigma_curry _), add_apply, sigma_curry_apply, sigma_curry_apply, sigma_curry_apply, add_apply] end @[simp] lemma sigma_curry_smul [monoid γ] [Π i j, add_monoid (δ i j)] [Π i j, distrib_mul_action γ (δ i j)] (r : γ) (f : Π₀ (i : Σ i, α i), δ i.1 i.2) : @sigma_curry _ _ δ _ (r • f) = r • @sigma_curry _ _ δ _ f := begin ext i j, rw [@smul_apply _ _ (λ i, Π₀ j, δ i j) _ _ _ _ (sigma_curry _), smul_apply, sigma_curry_apply, sigma_curry_apply, smul_apply] end @[simp] lemma sigma_curry_single [decidable_eq ι] [Π i, decidable_eq (α i)] [Π i j, has_zero (δ i j)] (ij : Σ i, α i) (x : δ ij.1 ij.2) : @sigma_curry _ _ _ _ (single ij x) = single ij.1 (single ij.2 x : Π₀ j, δ ij.1 j) := begin obtain ⟨i, j⟩ := ij, ext i' j', dsimp only, rw sigma_curry_apply, obtain rfl | hi := eq_or_ne i i', { rw single_eq_same, obtain rfl | hj := eq_or_ne j j', { rw [single_eq_same, single_eq_same] }, { rw [single_eq_of_ne, single_eq_of_ne hj], simpa using hj }, }, { rw [single_eq_of_ne, single_eq_of_ne hi, zero_apply], simpa using hi }, end /--The natural map between `Π₀ i (j : α i), δ i j` and `Π₀ (i : Σ i, α i), δ i.1 i.2`, inverse of `curry`.-/ def sigma_uncurry [Π i j, has_zero (δ i j)] [Π i, decidable_eq (α i)] [Π i j (x : δ i j), decidable (x ≠ 0)] (f : Π₀ i j, δ i j) : Π₀ (i : Σ i, _), δ i.1 i.2 := { to_fun := λ i, f i.1 i.2, support' := f.support'.map $ λ s, ⟨(multiset.bind ↑s $ λ i, ((f i).support.map ⟨sigma.mk i, sigma_mk_injective⟩).val), λ i, begin simp_rw [multiset.mem_bind, map_val, multiset.mem_map, function.embedding.coe_fn_mk, ←finset.mem_def, mem_support_to_fun], obtain hi | (hi : f i.1 = 0) := s.prop i.1, { by_cases hi' : f i.1 i.2 = 0, { exact or.inr hi' }, { exact or.inl ⟨_, hi, i.2, hi', sigma.eta _⟩ } }, { right, rw [hi, zero_apply] } end⟩ } @[simp] lemma sigma_uncurry_apply [Π i j, has_zero (δ i j)] [Π i, decidable_eq (α i)] [Π i j (x : δ i j), decidable (x ≠ 0)] (f : Π₀ i j, δ i j) (i : ι) (j : α i) : sigma_uncurry f ⟨i, j⟩ = f i j := rfl @[simp] lemma sigma_uncurry_zero [Π i j, has_zero (δ i j)] [Π i, decidable_eq (α i)] [Π i j (x : δ i j), decidable (x ≠ 0)]: sigma_uncurry (0 : Π₀ i j, δ i j) = 0 := rfl @[simp] lemma sigma_uncurry_add [Π i j, add_zero_class (δ i j)] [Π i, decidable_eq (α i)] [Π i j (x : δ i j), decidable (x ≠ 0)] (f g : Π₀ i j, δ i j) : sigma_uncurry (f + g) = sigma_uncurry f + sigma_uncurry g := coe_fn_injective rfl @[simp] lemma sigma_uncurry_smul [monoid γ] [Π i j, add_monoid (δ i j)] [Π i, decidable_eq (α i)] [Π i j (x : δ i j), decidable (x ≠ 0)] [Π i j, distrib_mul_action γ (δ i j)] (r : γ) (f : Π₀ i j, δ i j) : sigma_uncurry (r • f) = r • sigma_uncurry f := coe_fn_injective rfl @[simp] lemma sigma_uncurry_single [Π i j, has_zero (δ i j)] [decidable_eq ι] [Π i, decidable_eq (α i)] [Π i j (x : δ i j), decidable (x ≠ 0)] (i) (j : α i) (x : δ i j) : sigma_uncurry (single i (single j x : Π₀ (j : α i), δ i j)) = single ⟨i, j⟩ x:= begin ext ⟨i', j'⟩, dsimp only, rw sigma_uncurry_apply, obtain rfl | hi := eq_or_ne i i', { rw single_eq_same, obtain rfl | hj := eq_or_ne j j', { rw [single_eq_same, single_eq_same] }, { rw [single_eq_of_ne hj, single_eq_of_ne], simpa using hj }, }, { rw [single_eq_of_ne hi, single_eq_of_ne, zero_apply], simpa using hi }, end /--The natural bijection between `Π₀ (i : Σ i, α i), δ i.1 i.2` and `Π₀ i (j : α i), δ i j`. This is the dfinsupp version of `equiv.Pi_curry`. -/ noncomputable def sigma_curry_equiv [Π i j, has_zero (δ i j)] [Π i, decidable_eq (α i)] [Π i j (x : δ i j), decidable (x ≠ 0)] : (Π₀ (i : Σ i, _), δ i.1 i.2) ≃ Π₀ i j, δ i j := { to_fun := sigma_curry, inv_fun := sigma_uncurry, left_inv := λ f, by { ext ⟨i, j⟩, rw [sigma_uncurry_apply, sigma_curry_apply] }, right_inv := λ f, by { ext i j, rw [sigma_curry_apply, sigma_uncurry_apply] } } end curry variables {α : option ι → Type v} /-- Adds a term to a dfinsupp, making a dfinsupp indexed by an `option`. This is the dfinsupp version of `option.rec`. -/ def extend_with [Π i, has_zero (α i)] (a : α none) (f : Π₀ i, α (some i)) : Π₀ i, α i := { to_fun := option.rec a f, support' := f.support'.map $ λ s, ⟨none ::ₘ multiset.map some s, λ i, option.rec (or.inl $ multiset.mem_cons_self _ _) (λ i, (s.prop i).imp_left $ λ h, multiset.mem_cons_of_mem $ multiset.mem_map_of_mem _ h) i⟩ } @[simp] lemma extend_with_none [Π i, has_zero (α i)] (f : Π₀ i, α (some i)) (a : α none) : f.extend_with a none = a := rfl @[simp] lemma extend_with_some [Π i, has_zero (α i)] (f : Π₀ i, α (some i)) (a : α none) (i : ι) : f.extend_with a (some i) = f i := rfl @[simp] lemma extend_with_single_zero [decidable_eq ι] [Π i, has_zero (α i)] (i : ι) (x : α (some i)) : (single i x).extend_with 0 = single (some i) x := begin ext (_ | j), { rw [extend_with_none, single_eq_of_ne (option.some_ne_none _)] }, { rw extend_with_some, obtain rfl | hij := decidable.eq_or_ne i j, { rw [single_eq_same, single_eq_same] }, { rw [single_eq_of_ne hij, single_eq_of_ne ((option.some_injective _).ne hij)] }, }, end @[simp] lemma extend_with_zero [decidable_eq ι] [Π i, has_zero (α i)] (x : α none) : (0 : Π₀ i, α (some i)).extend_with x = single none x := begin ext (_ | j), { rw [extend_with_none, single_eq_same] }, { rw [extend_with_some, single_eq_of_ne (option.some_ne_none _).symm, zero_apply] }, end include dec /-- Bijection obtained by separating the term of index `none` of a dfinsupp over `option ι`. This is the dfinsupp version of `equiv.pi_option_equiv_prod`. -/ @[simps] noncomputable def equiv_prod_dfinsupp [Π i, has_zero (α i)] : (Π₀ i, α i) ≃ α none × Π₀ i, α (some i) := { to_fun := λ f, (f none, comap_domain some (option.some_injective _) f), inv_fun := λ f, f.2.extend_with f.1, left_inv := λ f, begin ext i, cases i with i, { rw extend_with_none }, { rw [extend_with_some, comap_domain_apply] } end, right_inv := λ x, begin dsimp only, ext, { exact extend_with_none x.snd _ }, { rw [comap_domain_apply, extend_with_some] } end } lemma equiv_prod_dfinsupp_add [Π i, add_zero_class (α i)] (f g : Π₀ i, α i) : equiv_prod_dfinsupp (f + g) = equiv_prod_dfinsupp f + equiv_prod_dfinsupp g := prod.ext (add_apply _ _ _) (comap_domain_add _ _ _ _) lemma equiv_prod_dfinsupp_smul [monoid γ] [Π i, add_monoid (α i)] [Π i, distrib_mul_action γ (α i)] (r : γ) (f : Π₀ i, α i) : equiv_prod_dfinsupp (r • f) = r • equiv_prod_dfinsupp f := prod.ext (smul_apply _ _ _) (comap_domain_smul _ _ _ _) end equiv section prod_and_sum /-- `prod f g` is the product of `g i (f i)` over the support of `f`. -/ @[to_additive "`sum f g` is the sum of `g i (f i)` over the support of `f`."] def prod [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] (f : Π₀ i, β i) (g : Π i, β i → γ) : γ := ∏ i in f.support, g i (f i) @[to_additive] lemma prod_map_range_index {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)] [comm_monoid γ] {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} {h : Π i, β₂ i → γ} (h0 : ∀i, h i 0 = 1) : (map_range f hf g).prod h = g.prod (λi b, h i (f i b)) := begin rw [map_range_def], refine (finset.prod_subset support_mk_subset _).trans _, { intros i h1 h2, dsimp, simp [h1] at h2, dsimp at h2, simp [h1, h2, h0] }, { refine finset.prod_congr rfl _, intros i h1, simp [h1] } end @[to_additive] lemma prod_zero_index [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {h : Π i, β i → γ} : (0 : Π₀ i, β i).prod h = 1 := rfl @[to_additive] lemma prod_single_index [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {i : ι} {b : β i} {h : Π i, β i → γ} (h_zero : h i 0 = 1) : (single i b).prod h = h i b := begin by_cases h : b ≠ 0, { simp [dfinsupp.prod, support_single_ne_zero h] }, { rw [not_not] at h, simp [h, prod_zero_index, h_zero], refl } end @[to_additive] lemma prod_neg_index [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {g : Π₀ i, β i} {h : Π i, β i → γ} (h0 : ∀i, h i 0 = 1) : (-g).prod h = g.prod (λi b, h i (- b)) := prod_map_range_index h0 omit dec @[to_additive] lemma prod_comm {ι₁ ι₂ : Sort*} {β₁ : ι₁ → Type*} {β₂ : ι₂ → Type*} [decidable_eq ι₁] [decidable_eq ι₂] [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)] [comm_monoid γ] (f₁ : Π₀ i, β₁ i) (f₂ : Π₀ i, β₂ i) (h : Π i, β₁ i → Π i, β₂ i → γ) : f₁.prod (λ i₁ x₁, f₂.prod $ λ i₂ x₂, h i₁ x₁ i₂ x₂) = f₂.prod (λ i₂ x₂, f₁.prod $ λ i₁ x₁, h i₁ x₁ i₂ x₂) := finset.prod_comm @[simp] lemma sum_apply {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i, add_comm_monoid (β i)] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} {i₂ : ι} : (f.sum g) i₂ = f.sum (λi₁ b, g i₁ b i₂) := (eval_add_monoid_hom i₂ : (Π₀ i, β i) →+ β i₂).map_sum _ f.support include dec lemma support_sum {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} : (f.sum g).support ⊆ f.support.bUnion (λi, (g i (f i)).support) := have ∀i₁ : ι, f.sum (λ (i : ι₁) (b : β₁ i), (g i b) i₁) ≠ 0 → (∃ (i : ι₁), f i ≠ 0 ∧ ¬ (g i (f i)) i₁ = 0), from assume i₁ h, let ⟨i, hi, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨i, mem_support_iff.1 hi, ne⟩, by simpa [finset.subset_iff, mem_support_iff, finset.mem_bUnion, sum_apply] using this @[simp, to_additive] lemma prod_one [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {f : Π₀ i, β i} : f.prod (λi b, (1 : γ)) = 1 := finset.prod_const_one @[simp, to_additive] lemma prod_mul [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {f : Π₀ i, β i} {h₁ h₂ : Π i, β i → γ} : f.prod (λi b, h₁ i b * h₂ i b) = f.prod h₁ * f.prod h₂ := finset.prod_mul_distrib @[simp, to_additive] lemma prod_inv [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_group γ] {f : Π₀ i, β i} {h : Π i, β i → γ} : f.prod (λi b, (h i b)⁻¹) = (f.prod h)⁻¹ := ((inv_monoid_hom : γ →* γ).map_prod _ f.support).symm @[to_additive] lemma prod_eq_one [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {f : Π₀ i, β i} {h : Π i, β i → γ} (hyp : ∀ i, h i (f i) = 1) : f.prod h = 1 := finset.prod_eq_one $ λ i hi, hyp i lemma smul_sum {α : Type*} [monoid α] [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_monoid γ] [distrib_mul_action α γ] {f : Π₀ i, β i} {h : Π i, β i → γ} {c : α} : c • f.sum h = f.sum (λ a b, c • h a b) := finset.smul_sum @[to_additive] lemma prod_add_index [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {f g : Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : (f + g).prod h = f.prod h * g.prod h := have f_eq : ∏ i in f.support ∪ g.support, h i (f i) = f.prod h, from (finset.prod_subset (finset.subset_union_left _ _) $ by simp [mem_support_iff, h_zero] {contextual := tt}).symm, have g_eq : ∏ i in f.support ∪ g.support, h i (g i) = g.prod h, from (finset.prod_subset (finset.subset_union_right _ _) $ by simp [mem_support_iff, h_zero] {contextual := tt}).symm, calc ∏ i in (f + g).support, h i ((f + g) i) = ∏ i in f.support ∪ g.support, h i ((f + g) i) : finset.prod_subset support_add $ by simp [mem_support_iff, h_zero] {contextual := tt} ... = (∏ i in f.support ∪ g.support, h i (f i)) * (∏ i in f.support ∪ g.support, h i (g i)) : by simp [h_add, finset.prod_mul_distrib] ... = _ : by rw [f_eq, g_eq] @[to_additive] lemma _root_.dfinsupp_prod_mem [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {S : Type*} [set_like S γ] [submonoid_class S γ] (s : S) (f : Π₀ i, β i) (g : Π i, β i → γ) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ s) : f.prod g ∈ s := prod_mem $ λ i hi, h _ $ mem_support_iff.1 hi @[simp, to_additive] lemma prod_eq_prod_fintype [fintype ι] [Π i, has_zero (β i)] [Π (i : ι) (x : β i), decidable (x ≠ 0)] [comm_monoid γ] (v : Π₀ i, β i) [f : Π i, β i → γ] (hf : ∀ i, f i 0 = 1) : v.prod f = ∏ i, f i (dfinsupp.equiv_fun_on_fintype v i) := begin suffices : ∏ i in v.support, f i (v i) = ∏ i, f i (v i), { simp [dfinsupp.prod, this] }, apply finset.prod_subset v.support.subset_univ, intros i hi' hi, rw [mem_support_iff, not_not] at hi, rw [hi, hf], end /-- When summing over an `add_monoid_hom`, the decidability assumption is not needed, and the result is also an `add_monoid_hom`. -/ def sum_add_hom [Π i, add_zero_class (β i)] [add_comm_monoid γ] (φ : Π i, β i →+ γ) : (Π₀ i, β i) →+ γ := { to_fun := (λ f, f.support'.lift (λ s, ∑ i in multiset.to_finset ↑s, φ i (f i)) $ begin rintros ⟨sx, hx⟩ ⟨sy, hy⟩, dsimp only [subtype.coe_mk, to_fun_eq_coe] at *, have H1 : sx.to_finset ∩ sy.to_finset ⊆ sx.to_finset, from finset.inter_subset_left _ _, have H2 : sx.to_finset ∩ sy.to_finset ⊆ sy.to_finset, from finset.inter_subset_right _ _, refine (finset.sum_subset H1 _).symm.trans ((finset.sum_congr rfl _).trans (finset.sum_subset H2 _)), { intros i H1 H2, rw finset.mem_inter at H2, simp only [multiset.mem_to_finset] at H1 H2, rw [(hy i).resolve_left (mt (and.intro H1) H2), add_monoid_hom.map_zero] }, { intros i H1, refl }, { intros i H1 H2, rw finset.mem_inter at H2, simp only [multiset.mem_to_finset] at H1 H2, rw [(hx i).resolve_left (mt (λ H3, and.intro H3 H1) H2), add_monoid_hom.map_zero] } end), map_add' := begin rintros ⟨f, sf, hf⟩ ⟨g, sg, hg⟩, change ∑ i in _, _ = (∑ i in _, _) + (∑ i in _, _), simp only [coe_add, coe_mk', subtype.coe_mk, pi.add_apply, map_add, finset.sum_add_distrib], congr' 1, { refine (finset.sum_subset _ _).symm, { intro i, simp only [multiset.mem_to_finset, multiset.mem_add], exact or.inl }, { intros i H1 H2, simp only [multiset.mem_to_finset, multiset.mem_add] at H2, rw [(hf i).resolve_left H2, add_monoid_hom.map_zero] } }, { refine (finset.sum_subset _ _).symm, { intro i, simp only [multiset.mem_to_finset, multiset.mem_add], exact or.inr }, { intros i H1 H2, simp only [multiset.mem_to_finset, multiset.mem_add] at H2, rw [(hg i).resolve_left H2, add_monoid_hom.map_zero] } } end, map_zero' := rfl } @[simp] lemma sum_add_hom_single [Π i, add_zero_class (β i)] [add_comm_monoid γ] (φ : Π i, β i →+ γ) (i) (x : β i) : sum_add_hom φ (single i x) = φ i x := begin dsimp [sum_add_hom, single, trunc.lift_mk], rw [multiset.to_finset_singleton, finset.sum_singleton, pi.single_eq_same], end @[simp] lemma sum_add_hom_comp_single [Π i, add_zero_class (β i)] [add_comm_monoid γ] (f : Π i, β i →+ γ) (i : ι) : (sum_add_hom f).comp (single_add_hom β i) = f i := add_monoid_hom.ext $ λ x, sum_add_hom_single f i x /-- While we didn't need decidable instances to define it, we do to reduce it to a sum -/ lemma sum_add_hom_apply [Π i, add_zero_class (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_monoid γ] (φ : Π i, β i →+ γ) (f : Π₀ i, β i) : sum_add_hom φ f = f.sum (λ x, φ x) := begin rcases f with ⟨f, s, hf⟩, change ∑ i in _, _ = (∑ i in finset.filter _ _, _), rw [finset.sum_filter, finset.sum_congr rfl], intros i _, dsimp only [coe_mk', subtype.coe_mk] at *, split_ifs, refl, rw [(not_not.mp h), add_monoid_hom.map_zero], end lemma _root_.dfinsupp_sum_add_hom_mem [Π i, add_zero_class (β i)] [add_comm_monoid γ] {S : Type*} [set_like S γ] [add_submonoid_class S γ] (s : S) (f : Π₀ i, β i) (g : Π i, β i →+ γ) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ s) : dfinsupp.sum_add_hom g f ∈ s := begin classical, rw dfinsupp.sum_add_hom_apply, convert dfinsupp_sum_mem _ _ _ _, { apply_instance }, exact h end /-- The supremum of a family of commutative additive submonoids is equal to the range of `dfinsupp.sum_add_hom`; that is, every element in the `supr` can be produced from taking a finite number of non-zero elements of `S i`, coercing them to `γ`, and summing them. -/ lemma _root_.add_submonoid.supr_eq_mrange_dfinsupp_sum_add_hom [add_comm_monoid γ] (S : ι → add_submonoid γ) : supr S = (dfinsupp.sum_add_hom (λ i, (S i).subtype)).mrange := begin apply le_antisymm, { apply supr_le _, intros i y hy, exact ⟨dfinsupp.single i ⟨y, hy⟩, dfinsupp.sum_add_hom_single _ _ _⟩, }, { rintros x ⟨v, rfl⟩, exact dfinsupp_sum_add_hom_mem _ v _ (λ i _, (le_supr S i : S i ≤ _) (v i).prop) } end /-- The bounded supremum of a family of commutative additive submonoids is equal to the range of `dfinsupp.sum_add_hom` composed with `dfinsupp.filter_add_monoid_hom`; that is, every element in the bounded `supr` can be produced from taking a finite number of non-zero elements from the `S i` that satisfy `p i`, coercing them to `γ`, and summing them. -/ lemma _root_.add_submonoid.bsupr_eq_mrange_dfinsupp_sum_add_hom (p : ι → Prop) [decidable_pred p] [add_comm_monoid γ] (S : ι → add_submonoid γ) : (⨆ i (h : p i), S i) = ((sum_add_hom (λ i, (S i).subtype)).comp (filter_add_monoid_hom _ p)).mrange := begin apply le_antisymm, { refine supr₂_le (λ i hi y hy, ⟨dfinsupp.single i ⟨y, hy⟩, _⟩), rw [add_monoid_hom.comp_apply, filter_add_monoid_hom_apply, filter_single_pos _ _ hi], exact sum_add_hom_single _ _ _, }, { rintros x ⟨v, rfl⟩, refine dfinsupp_sum_add_hom_mem _ _ _ (λ i hi, _), refine add_submonoid.mem_supr_of_mem i _, by_cases hp : p i, { simp [hp], }, { simp [hp] }, } end lemma _root_.add_submonoid.mem_supr_iff_exists_dfinsupp [add_comm_monoid γ] (S : ι → add_submonoid γ) (x : γ) : x ∈ supr S ↔ ∃ f : Π₀ i, S i, dfinsupp.sum_add_hom (λ i, (S i).subtype) f = x := set_like.ext_iff.mp (add_submonoid.supr_eq_mrange_dfinsupp_sum_add_hom S) x /-- A variant of `add_submonoid.mem_supr_iff_exists_dfinsupp` with the RHS fully unfolded. -/ lemma _root_.add_submonoid.mem_supr_iff_exists_dfinsupp' [add_comm_monoid γ] (S : ι → add_submonoid γ) [Π i (x : S i), decidable (x ≠ 0)] (x : γ) : x ∈ supr S ↔ ∃ f : Π₀ i, S i, f.sum (λ i xi, ↑xi) = x := begin rw add_submonoid.mem_supr_iff_exists_dfinsupp, simp_rw sum_add_hom_apply, congr', end lemma _root_.add_submonoid.mem_bsupr_iff_exists_dfinsupp (p : ι → Prop) [decidable_pred p] [add_comm_monoid γ] (S : ι → add_submonoid γ) (x : γ) : x ∈ (⨆ i (h : p i), S i) ↔ ∃ f : Π₀ i, S i, dfinsupp.sum_add_hom (λ i, (S i).subtype) (f.filter p) = x := set_like.ext_iff.mp (add_submonoid.bsupr_eq_mrange_dfinsupp_sum_add_hom p S) x omit dec lemma sum_add_hom_comm {ι₁ ι₂ : Sort*} {β₁ : ι₁ → Type*} {β₂ : ι₂ → Type*} {γ : Type*} [decidable_eq ι₁] [decidable_eq ι₂] [Π i, add_zero_class (β₁ i)] [Π i, add_zero_class (β₂ i)] [add_comm_monoid γ] (f₁ : Π₀ i, β₁ i) (f₂ : Π₀ i, β₂ i) (h : Π i j, β₁ i →+ β₂ j →+ γ) : sum_add_hom (λ i₂, sum_add_hom (λ i₁, h i₁ i₂) f₁) f₂ = sum_add_hom (λ i₁, sum_add_hom (λ i₂, (h i₁ i₂).flip) f₂) f₁ := begin obtain ⟨⟨f₁, s₁, h₁⟩, ⟨f₂, s₂, h₂⟩⟩ := ⟨f₁, f₂⟩, simp only [sum_add_hom, add_monoid_hom.finset_sum_apply, quotient.lift_on_mk, add_monoid_hom.coe_mk, add_monoid_hom.flip_apply, trunc.lift], exact finset.sum_comm, end include dec /-- The `dfinsupp` version of `finsupp.lift_add_hom`,-/ @[simps apply symm_apply] def lift_add_hom [Π i, add_zero_class (β i)] [add_comm_monoid γ] : (Π i, β i →+ γ) ≃+ ((Π₀ i, β i) →+ γ) := { to_fun := sum_add_hom, inv_fun := λ F i, F.comp (single_add_hom β i), left_inv := λ x, by { ext, simp }, right_inv := λ ψ, by { ext, simp }, map_add' := λ F G, by { ext, simp } } /-- The `dfinsupp` version of `finsupp.lift_add_hom_single_add_hom`,-/ @[simp] lemma lift_add_hom_single_add_hom [Π i, add_comm_monoid (β i)] : lift_add_hom (single_add_hom β) = add_monoid_hom.id (Π₀ i, β i) := lift_add_hom.to_equiv.apply_eq_iff_eq_symm_apply.2 rfl /-- The `dfinsupp` version of `finsupp.lift_add_hom_apply_single`,-/ lemma lift_add_hom_apply_single [Π i, add_zero_class (β i)] [add_comm_monoid γ] (f : Π i, β i →+ γ) (i : ι) (x : β i) : lift_add_hom f (single i x) = f i x := by simp /-- The `dfinsupp` version of `finsupp.lift_add_hom_comp_single`,-/ lemma lift_add_hom_comp_single [Π i, add_zero_class (β i)] [add_comm_monoid γ] (f : Π i, β i →+ γ) (i : ι) : (lift_add_hom f).comp (single_add_hom β i) = f i := by simp /-- The `dfinsupp` version of `finsupp.comp_lift_add_hom`,-/ lemma comp_lift_add_hom {δ : Type*} [Π i, add_zero_class (β i)] [add_comm_monoid γ] [add_comm_monoid δ] (g : γ →+ δ) (f : Π i, β i →+ γ) : g.comp (lift_add_hom f) = lift_add_hom (λ a, g.comp (f a)) := lift_add_hom.symm_apply_eq.1 $ funext $ λ a, by rw [lift_add_hom_symm_apply, add_monoid_hom.comp_assoc, lift_add_hom_comp_single] @[simp] lemma sum_add_hom_zero [Π i, add_zero_class (β i)] [add_comm_monoid γ] : sum_add_hom (λ i, (0 : β i →+ γ)) = 0 := (lift_add_hom : (Π i, β i →+ γ) ≃+ _).map_zero @[simp] lemma sum_add_hom_add [Π i, add_zero_class (β i)] [add_comm_monoid γ] (g : Π i, β i →+ γ) (h : Π i, β i →+ γ) : sum_add_hom (λ i, g i + h i) = sum_add_hom g + sum_add_hom h := lift_add_hom.map_add _ _ @[simp] lemma sum_add_hom_single_add_hom [Π i, add_comm_monoid (β i)] : sum_add_hom (single_add_hom β) = add_monoid_hom.id _ := lift_add_hom_single_add_hom lemma comp_sum_add_hom {δ : Type*} [Π i, add_zero_class (β i)] [add_comm_monoid γ] [add_comm_monoid δ] (g : γ →+ δ) (f : Π i, β i →+ γ) : g.comp (sum_add_hom f) = sum_add_hom (λ a, g.comp (f a)) := comp_lift_add_hom _ _ lemma sum_sub_index [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_group γ] {f g : Π₀ i, β i} {h : Π i, β i → γ} (h_sub : ∀i b₁ b₂, h i (b₁ - b₂) = h i b₁ - h i b₂) : (f - g).sum h = f.sum h - g.sum h := begin have := (lift_add_hom (λ a, add_monoid_hom.of_map_sub (h a) (h_sub a))).map_sub f g, rw [lift_add_hom_apply, sum_add_hom_apply, sum_add_hom_apply, sum_add_hom_apply] at this, exact this, end @[to_additive] lemma prod_finset_sum_index {γ : Type w} {α : Type x} [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {s : finset α} {g : α → Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : ∏ i in s, (g i).prod h = (∑ i in s, g i).prod h := begin classical, exact finset.induction_on s (by simp [prod_zero_index]) (by simp [prod_add_index, h_zero, h_add] {contextual := tt}) end @[to_additive] lemma prod_sum_index {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : (f.sum g).prod h = f.prod (λi b, (g i b).prod h) := (prod_finset_sum_index h_zero h_add).symm @[simp] lemma sum_single [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] {f : Π₀ i, β i} : f.sum single = f := begin have := add_monoid_hom.congr_fun lift_add_hom_single_add_hom f, rw [lift_add_hom_apply, sum_add_hom_apply] at this, exact this, end @[to_additive] lemma prod_subtype_domain_index [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {v : Π₀ i, β i} {p : ι → Prop} [decidable_pred p] {h : Π i, β i → γ} (hp : ∀ x ∈ v.support, p x) : (v.subtype_domain p).prod (λi b, h i b) = v.prod h := finset.prod_bij (λp _, p) (by simp) (by simp) (assume ⟨a₀, ha₀⟩ ⟨a₁, ha₁⟩, by simp) (λ i hi, ⟨⟨i, hp i hi⟩, by simpa using hi, rfl⟩) omit dec lemma subtype_domain_sum [Π i, add_comm_monoid (β i)] {s : finset γ} {h : γ → Π₀ i, β i} {p : ι → Prop} [decidable_pred p] : (∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p := (subtype_domain_add_monoid_hom β p).map_sum _ s lemma subtype_domain_finsupp_sum {δ : γ → Type x} [decidable_eq γ] [Π c, has_zero (δ c)] [Π c (x : δ c), decidable (x ≠ 0)] [Π i, add_comm_monoid (β i)] {p : ι → Prop} [decidable_pred p] {s : Π₀ c, δ c} {h : Π c, δ c → Π₀ i, β i} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum end prod_and_sum /-! ### Bundled versions of `dfinsupp.map_range` The names should match the equivalent bundled `finsupp.map_range` definitions. -/ section map_range omit dec variables [Π i, add_zero_class (β i)] [Π i, add_zero_class (β₁ i)] [Π i, add_zero_class (β₂ i)] lemma map_range_add (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (hf' : ∀ i x y, f i (x + y) = f i x + f i y) (g₁ g₂ : Π₀ i, β₁ i): map_range f hf (g₁ + g₂) = map_range f hf g₁ + map_range f hf g₂ := begin ext, simp only [map_range_apply f, coe_add, pi.add_apply, hf'] end /-- `dfinsupp.map_range` as an `add_monoid_hom`. -/ @[simps apply] def map_range.add_monoid_hom (f : Π i, β₁ i →+ β₂ i) : (Π₀ i, β₁ i) →+ (Π₀ i, β₂ i) := { to_fun := map_range (λ i x, f i x) (λ i, (f i).map_zero), map_zero' := map_range_zero _ _, map_add' := map_range_add _ _ (λ i, (f i).map_add) } @[simp] lemma map_range.add_monoid_hom_id : map_range.add_monoid_hom (λ i, add_monoid_hom.id (β₂ i)) = add_monoid_hom.id _ := add_monoid_hom.ext map_range_id lemma map_range.add_monoid_hom_comp (f : Π i, β₁ i →+ β₂ i) (f₂ : Π i, β i →+ β₁ i): map_range.add_monoid_hom (λ i, (f i).comp (f₂ i)) = (map_range.add_monoid_hom f).comp (map_range.add_monoid_hom f₂) := add_monoid_hom.ext $ map_range_comp (λ i x, f i x) (λ i x, f₂ i x) _ _ _ /-- `dfinsupp.map_range.add_monoid_hom` as an `add_equiv`. -/ @[simps apply] def map_range.add_equiv (e : Π i, β₁ i ≃+ β₂ i) : (Π₀ i, β₁ i) ≃+ (Π₀ i, β₂ i) := { to_fun := map_range (λ i x, e i x) (λ i, (e i).map_zero), inv_fun := map_range (λ i x, (e i).symm x) (λ i, (e i).symm.map_zero), left_inv := λ x, by rw ←map_range_comp; { simp_rw add_equiv.symm_comp_self, simp }, right_inv := λ x, by rw ←map_range_comp; { simp_rw add_equiv.self_comp_symm, simp }, .. map_range.add_monoid_hom (λ i, (e i).to_add_monoid_hom) } @[simp] lemma map_range.add_equiv_refl : (map_range.add_equiv $ λ i, add_equiv.refl (β₁ i)) = add_equiv.refl _ := add_equiv.ext map_range_id lemma map_range.add_equiv_trans (f : Π i, β i ≃+ β₁ i) (f₂ : Π i, β₁ i ≃+ β₂ i): map_range.add_equiv (λ i, (f i).trans (f₂ i)) = (map_range.add_equiv f).trans (map_range.add_equiv f₂) := add_equiv.ext $ map_range_comp (λ i x, f₂ i x) (λ i x, f i x) _ _ _ @[simp] lemma map_range.add_equiv_symm (e : Π i, β₁ i ≃+ β₂ i) : (map_range.add_equiv e).symm = map_range.add_equiv (λ i, (e i).symm) := rfl end map_range end dfinsupp /-! ### Product and sum lemmas for bundled morphisms. In this section, we provide analogues of `add_monoid_hom.map_sum`, `add_monoid_hom.coe_finset_sum`, and `add_monoid_hom.finset_sum_apply` for `dfinsupp.sum` and `dfinsupp.sum_add_hom` instead of `finset.sum`. We provide these for `add_monoid_hom`, `monoid_hom`, `ring_hom`, `add_equiv`, and `mul_equiv`. Lemmas for `linear_map` and `linear_equiv` are in another file. -/ section variables [decidable_eq ι] namespace monoid_hom variables {R S : Type*} variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] @[simp, to_additive] lemma map_dfinsupp_prod [comm_monoid R] [comm_monoid S] (h : R →* S) (f : Π₀ i, β i) (g : Π i, β i → R) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ @[to_additive] lemma coe_dfinsupp_prod [monoid R] [comm_monoid S] (f : Π₀ i, β i) (g : Π i, β i → R →* S) : ⇑(f.prod g) = f.prod (λ a b, (g a b)) := coe_finset_prod _ _ @[simp, to_additive] lemma dfinsupp_prod_apply [monoid R] [comm_monoid S] (f : Π₀ i, β i) (g : Π i, β i → R →* S) (r : R) : (f.prod g) r = f.prod (λ a b, (g a b) r) := finset_prod_apply _ _ _ end monoid_hom namespace ring_hom variables {R S : Type*} variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] @[simp] lemma map_dfinsupp_prod [comm_semiring R] [comm_semiring S] (h : R →+* S) (f : Π₀ i, β i) (g : Π i, β i → R) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ @[simp] lemma map_dfinsupp_sum [non_assoc_semiring R] [non_assoc_semiring S] (h : R →+* S) (f : Π₀ i, β i) (g : Π i, β i → R) : h (f.sum g) = f.sum (λ a b, h (g a b)) := h.map_sum _ _ end ring_hom namespace mul_equiv variables {R S : Type*} variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] @[simp, to_additive] lemma map_dfinsupp_prod [comm_monoid R] [comm_monoid S] (h : R ≃* S) (f : Π₀ i, β i) (g : Π i, β i → R) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ end mul_equiv /-! The above lemmas, repeated for `dfinsupp.sum_add_hom`. -/ namespace add_monoid_hom variables {R S : Type*} open dfinsupp @[simp] lemma map_dfinsupp_sum_add_hom [add_comm_monoid R] [add_comm_monoid S] [Π i, add_zero_class (β i)] (h : R →+ S) (f : Π₀ i, β i) (g : Π i, β i →+ R) : h (sum_add_hom g f) = sum_add_hom (λ i, h.comp (g i)) f := congr_fun (comp_lift_add_hom h g) f @[simp] lemma dfinsupp_sum_add_hom_apply [add_zero_class R] [add_comm_monoid S] [Π i, add_zero_class (β i)] (f : Π₀ i, β i) (g : Π i, β i →+ R →+ S) (r : R) : (sum_add_hom g f) r = sum_add_hom (λ i, (eval r).comp (g i)) f := map_dfinsupp_sum_add_hom (eval r) f g lemma coe_dfinsupp_sum_add_hom [add_zero_class R] [add_comm_monoid S] [Π i, add_zero_class (β i)] (f : Π₀ i, β i) (g : Π i, β i →+ R →+ S) : ⇑(sum_add_hom g f) = sum_add_hom (λ i, (coe_fn R S).comp (g i)) f := map_dfinsupp_sum_add_hom (coe_fn R S) f g end add_monoid_hom namespace ring_hom variables {R S : Type*} open dfinsupp @[simp] lemma map_dfinsupp_sum_add_hom [non_assoc_semiring R] [non_assoc_semiring S] [Π i, add_zero_class (β i)] (h : R →+* S) (f : Π₀ i, β i) (g : Π i, β i →+ R) : h (sum_add_hom g f) = sum_add_hom (λ i, h.to_add_monoid_hom.comp (g i)) f := add_monoid_hom.congr_fun (comp_lift_add_hom h.to_add_monoid_hom g) f end ring_hom namespace add_equiv variables {R S : Type*} open dfinsupp @[simp] lemma map_dfinsupp_sum_add_hom [add_comm_monoid R] [add_comm_monoid S] [Π i, add_zero_class (β i)] (h : R ≃+ S) (f : Π₀ i, β i) (g : Π i, β i →+ R) : h (sum_add_hom g f) = sum_add_hom (λ i, h.to_add_monoid_hom.comp (g i)) f := add_monoid_hom.congr_fun (comp_lift_add_hom h.to_add_monoid_hom g) f end add_equiv end section finite_infinite instance dfinsupp.fintype {ι : Sort*} {π : ι → Sort*} [decidable_eq ι] [Π i, has_zero (π i)] [fintype ι] [∀ i, fintype (π i)] : fintype (Π₀ i, π i) := fintype.of_equiv (Π i, π i) dfinsupp.equiv_fun_on_fintype.symm instance dfinsupp.infinite_of_left {ι : Sort*} {π : ι → Sort*} [∀ i, nontrivial (π i)] [Π i, has_zero (π i)] [infinite ι] : infinite (Π₀ i, π i) := by letI := classical.dec_eq ι; choose m hm using (λ i, exists_ne (0 : π i)); exact infinite.of_injective _ (dfinsupp.single_left_injective hm) /-- See `dfinsupp.infinite_of_right` for this in instance form, with the drawback that it needs all `π i` to be infinite. -/ lemma dfinsupp.infinite_of_exists_right {ι : Sort*} {π : ι → Sort*} (i : ι) [infinite (π i)] [Π i, has_zero (π i)] : infinite (Π₀ i, π i) := by letI := classical.dec_eq ι; exact infinite.of_injective (λ j, dfinsupp.single i j) dfinsupp.single_injective /-- See `dfinsupp.infinite_of_exists_right` for the case that only one `π ι` is infinite. -/ instance dfinsupp.infinite_of_right {ι : Sort*} {π : ι → Sort*} [∀ i, infinite (π i)] [Π i, has_zero (π i)] [nonempty ι] : infinite (Π₀ i, π i) := dfinsupp.infinite_of_exists_right (classical.arbitrary ι) end finite_infinite
b76fe8b593785f754cd5bdb4e8d91b43e0a587b1
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/hott/init/num.hlean
ed635b009dee322bad6e2a225b837953bfb5f0f4
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,620
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.logic init.bool open bool definition pos_num.is_inhabited [instance] : inhabited pos_num := inhabited.mk pos_num.one namespace pos_num notation a + b := add a b definition mul (a b : pos_num) : pos_num := pos_num.rec_on a b (λn r, bit0 r + b) (λn r, bit0 r) notation a * b := mul a b definition lt (a b : pos_num) : bool := pos_num.rec_on a (λ b, pos_num.cases_on b ff (λm, tt) (λm, tt)) (λn f b, pos_num.cases_on b ff (λm, f m) (λm, f m)) (λn f b, pos_num.cases_on b ff (λm, f (succ m)) (λm, f m)) b definition le (a b : pos_num) : bool := lt a (succ b) definition equal (a b : pos_num) : bool := le a b && le b a end pos_num definition num.is_inhabited [instance] : inhabited num := inhabited.mk num.zero namespace num open pos_num definition pred (a : num) : num := num.rec_on a zero (λp, cond (is_one p) zero (pos (pred p))) definition size (a : num) : num := num.rec_on a (pos one) (λp, pos (size p)) definition mul (a b : num) : num := num.rec_on a zero (λpa, num.rec_on b zero (λpb, pos (pos_num.mul pa pb))) notation a + b := add a b notation a * b := mul a b definition le (a b : num) : bool := num.rec_on a tt (λpa, num.rec_on b ff (λpb, pos_num.le pa pb)) private definition psub (a b : pos_num) : num := pos_num.rec_on a (λb, zero) (λn f b, cond (pos_num.le (bit1 n) b) zero (pos_num.cases_on b (pos (bit0 n)) (λm, 2 * f m) (λm, 2 * f m + 1))) (λn f b, cond (pos_num.le (bit0 n) b) zero (pos_num.cases_on b (pos (pos_num.pred (bit0 n))) (λm, pred (2 * f m)) (λm, 2 * f m))) b definition sub (a b : num) : num := num.rec_on a zero (λpa, num.rec_on b a (λpb, psub pa pb)) notation a ≤ b := le a b notation a - b := sub a b end num -- the coercion from num to nat is defined here, -- so that it can already be used in init.trunc and init.tactic namespace nat definition add (a b : nat) : nat := nat.rec_on b a (λ b₁ r, succ r) notation a + b := add a b definition of_num [coercion] (n : num) : nat := num.rec zero (λ n, pos_num.rec (succ zero) (λ n r, r + r + (succ zero)) (λ n r, r + r) n) n end nat attribute nat.of_num [reducible] -- of_num is also reducible if namespace "nat" is not opened
71f244b3d0ac772623446b143f420e3b5f7d13af
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Util/Trace.lean
57d85ef5c914394c2a9b74978bf42abfbdc6d1a0
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,421
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich, Leonardo de Moura -/ import Lean.Message import Lean.MonadEnv universe u namespace Lean open Std (PersistentArray) structure TraceElem := (ref : Syntax) (msg : MessageData) instance traceElem.inhabited : Inhabited TraceElem := ⟨⟨arbitrary _, arbitrary _⟩⟩ structure TraceState := (enabled : Bool := true) (traces : PersistentArray TraceElem := {}) namespace TraceState instance : Inhabited TraceState := ⟨{}⟩ private def toFormat (traces : PersistentArray TraceElem) (sep : Format) : IO Format := traces.size.foldM (fun i r => do curr ← (traces.get! i).msg.format; pure $ if i > 0 then r ++ sep ++ curr else r ++ curr) Format.nil end TraceState class MonadTrace (m : Type → Type) := (modifyTraceState : (TraceState → TraceState) → m Unit) (getTraceState : m TraceState) export MonadTrace (getTraceState modifyTraceState) instance monadTraceTrans (m n) [MonadTrace m] [MonadLift m n] : MonadTrace n := { modifyTraceState := fun f => liftM (modifyTraceState f : m _), getTraceState := liftM (getTraceState : m _) } variables {α : Type} {m : Type → Type} [Monad m] [MonadTrace m] def printTraces {m} [Monad m] [MonadTrace m] [MonadIO m] : m Unit := do traceState ← getTraceState; traceState.traces.forM $ fun m => do d ← liftIO m.msg.format; liftIO $ IO.println d def resetTraceState {m} [MonadTrace m] : m Unit := modifyTraceState (fun _ => {}) private def checkTraceOptionAux (opts : Options) : Name → Bool | n@(Name.str p _ _) => opts.getBool n || (!opts.contains n && checkTraceOptionAux p) | _ => false def checkTraceOption (opts : Options) (cls : Name) : Bool := if opts.isEmpty then false else checkTraceOptionAux opts (`trace ++ cls) private def checkTraceOptionM [MonadOptions m] (cls : Name) : m Bool := do opts ← getOptions; pure $ checkTraceOption opts cls @[inline] def isTracingEnabledFor [MonadOptions m] (cls : Name) : m Bool := do s ← getTraceState; if !s.enabled then pure false else checkTraceOptionM cls @[inline] def enableTracing (b : Bool) : m Bool := do s ← getTraceState; let oldEnabled := s.enabled; modifyTraceState $ fun s => { s with enabled := b }; pure oldEnabled @[inline] def getTraces : m (PersistentArray TraceElem) := do s ← getTraceState; pure s.traces @[inline] def modifyTraces (f : PersistentArray TraceElem → PersistentArray TraceElem) : m Unit := modifyTraceState $ fun s => { s with traces := f s.traces } @[inline] def setTraceState (s : TraceState) : m Unit := modifyTraceState $ fun _ => s private def addNode (oldTraces : PersistentArray TraceElem) (cls : Name) (ref : Syntax) : m Unit := modifyTraces $ fun traces => let d := MessageData.tagged cls (MessageData.node (traces.toArray.map fun elem => elem.msg)); oldTraces.push { ref := ref, msg := d } private def getResetTraces : m (PersistentArray TraceElem) := do oldTraces ← getTraces; modifyTraces $ fun _ => {}; pure oldTraces section variables [Ref m] [AddMessageContext m] [MonadOptions m] def addTrace (cls : Name) (msg : MessageData) : m Unit := do ref ← getRef; msg ← addMessageContext msg; modifyTraces $ fun traces => traces.push { ref := ref, msg := MessageData.tagged cls msg } @[inline] def trace (cls : Name) (msg : Unit → MessageData) : m Unit := whenM (isTracingEnabledFor cls) (addTrace cls (msg ())) @[inline] def traceM (cls : Name) (mkMsg : m MessageData) : m Unit := whenM (isTracingEnabledFor cls) (do msg ← mkMsg; addTrace cls msg) @[inline] def traceCtx [MonadFinally m] (cls : Name) (ctx : m α) : m α := do b ← isTracingEnabledFor cls; if !b then do old ← enableTracing false; finally ctx (enableTracing old) else do ref ← getRef; oldCurrTraces ← getResetTraces; finally ctx (addNode oldCurrTraces cls ref) -- TODO: delete after fix old frontend def MonadTracer.trace (cls : Name) (msg : Unit → MessageData) : m Unit := trace cls msg end def registerTraceClass (traceClassName : Name) : IO Unit := registerOption (`trace ++ traceClassName) { group := "trace", defValue := false, descr := "enable/disable tracing for the given module and submodules" } end Lean new_frontend namespace Lean macro:max "trace!" id:term:max msg:term : term => `(trace $id fun _ => ($msg : MessageData)) end Lean
81ba4f6ec7a5aaa3a0f477607fae798debeff0c4
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebraic_geometry/prime_spectrum/is_open_comap_C.lean
b578d12dbb96603c5c6011d08364d8357e814a56
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
3,060
lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import algebraic_geometry.prime_spectrum.basic import ring_theory.polynomial.basic /-! The morphism `Spec R[x] --> Spec R` induced by the natural inclusion `R --> R[x]` is an open map. The main result is the first part of the statement of Lemma 00FB in the Stacks Project. https://stacks.math.columbia.edu/tag/00FB -/ open ideal polynomial prime_spectrum set open_locale polynomial namespace algebraic_geometry namespace polynomial variables {R : Type*} [comm_ring R] {f : R[X]} /-- Given a polynomial `f ∈ R[x]`, `image_of_Df` is the subset of `Spec R` where at least one of the coefficients of `f` does not vanish. Lemma `image_of_Df_eq_comap_C_compl_zero_locus` proves that `image_of_Df` is the image of `(zero_locus {f})ᶜ` under the morphism `comap C : Spec R[x] → Spec R`. -/ def image_of_Df (f) : set (prime_spectrum R) := {p : prime_spectrum R | ∃ i : ℕ , (coeff f i) ∉ p.as_ideal} lemma is_open_image_of_Df : is_open (image_of_Df f) := begin rw [image_of_Df, set_of_exists (λ i (x : prime_spectrum R), coeff f i ∉ x.as_ideal)], exact is_open_Union (λ i, is_open_basic_open), end /-- If a point of `Spec R[x]` is not contained in the vanishing set of `f`, then its image in `Spec R` is contained in the open set where at least one of the coefficients of `f` is non-zero. This lemma is a reformulation of `exists_C_coeff_not_mem`. -/ lemma comap_C_mem_image_of_Df {I : prime_spectrum R[X]} (H : I ∈ (zero_locus {f} : set (prime_spectrum R[X]))ᶜ ) : prime_spectrum.comap (polynomial.C : R →+* R[X]) I ∈ image_of_Df f := exists_C_coeff_not_mem (mem_compl_zero_locus_iff_not_mem.mp H) /-- The open set `image_of_Df f` coincides with the image of `basic_open f` under the morphism `C⁺ : Spec R[x] → Spec R`. -/ lemma image_of_Df_eq_comap_C_compl_zero_locus : image_of_Df f = prime_spectrum.comap (C : R →+* R[X]) '' (zero_locus {f})ᶜ := begin ext x, refine ⟨λ hx, ⟨⟨map C x.as_ideal, (is_prime_map_C_of_is_prime x.is_prime)⟩, ⟨_, _⟩⟩, _⟩, { rw [mem_compl_iff, mem_zero_locus, singleton_subset_iff], cases hx with i hi, exact λ a, hi (mem_map_C_iff.mp a i) }, { ext x, refine ⟨λ h, _, λ h, subset_span (mem_image_of_mem C.1 h)⟩, rw ← @coeff_C_zero R x _, exact mem_map_C_iff.mp h 0 }, { rintro ⟨xli, complement, rfl⟩, exact comap_C_mem_image_of_Df complement } end /-- The morphism `C⁺ : Spec R[x] → Spec R` is open. Stacks Project "Lemma 00FB", first part. https://stacks.math.columbia.edu/tag/00FB -/ theorem is_open_map_comap_C : is_open_map (prime_spectrum.comap (C : R →+* R[X])) := begin rintros U ⟨s, z⟩, rw [← compl_compl U, ← z, ← Union_of_singleton_coe s, zero_locus_Union, compl_Inter, image_Union], simp_rw [← image_of_Df_eq_comap_C_compl_zero_locus], exact is_open_Union (λ f, is_open_image_of_Df), end end polynomial end algebraic_geometry
43aaa31ed8e2c86060f1d52be00b6e8b443c00a8
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Lean/Elab/BuiltinCommand.lean
9a39fe8feef8cf03a6077ad3e5d7d6e8a784591f
[ "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "Apache-2.0", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
gebner/lean4
d51c4922640a52a6f7426536ea669ef18a1d9af5
8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f
refs/heads/master
1,685,732,780,391
1,672,962,627,000
1,673,459,398,000
373,307,283
0
0
Apache-2.0
1,691,316,730,000
1,622,669,271,000
Lean
UTF-8
Lean
false
false
19,722
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.CollectLevelParams import Lean.Meta.Reduce import Lean.Elab.DeclarationRange import Lean.Elab.Eval import Lean.Elab.Command import Lean.Elab.Open import Lean.Elab.SetOption import Lean.PrettyPrinter namespace Lean.Elab.Command @[builtin_command_elab moduleDoc] def elabModuleDoc : CommandElab := fun stx => do match stx[1] with | Syntax.atom _ val => let doc := val.extract 0 (val.endPos - ⟨2⟩) let range ← Elab.getDeclarationRange stx modifyEnv fun env => addMainModuleDoc env ⟨doc, range⟩ | _ => throwErrorAt stx "unexpected module doc string{indentD stx[1]}" private def addScope (isNewNamespace : Bool) (isNoncomputable : Bool) (header : String) (newNamespace : Name) : CommandElabM Unit := do modify fun s => { s with env := s.env.registerNamespace newNamespace, scopes := { s.scopes.head! with header := header, currNamespace := newNamespace, isNoncomputable := s.scopes.head!.isNoncomputable || isNoncomputable } :: s.scopes } pushScope if isNewNamespace then activateScoped newNamespace private def addScopes (isNewNamespace : Bool) (isNoncomputable : Bool) : Name → CommandElabM Unit | .anonymous => pure () | .str p header => do addScopes isNewNamespace isNoncomputable p let currNamespace ← getCurrNamespace addScope isNewNamespace isNoncomputable header (if isNewNamespace then Name.mkStr currNamespace header else currNamespace) | _ => throwError "invalid scope" private def addNamespace (header : Name) : CommandElabM Unit := addScopes (isNewNamespace := true) (isNoncomputable := false) header def withNamespace {α} (ns : Name) (elabFn : CommandElabM α) : CommandElabM α := do addNamespace ns let a ← elabFn modify fun s => { s with scopes := s.scopes.drop ns.getNumParts } pure a private def popScopes (numScopes : Nat) : CommandElabM Unit := for _ in [0:numScopes] do popScope private def checkAnonymousScope : List Scope → Bool | { header := "", .. } :: _ => true | _ => false private def checkEndHeader : Name → List Scope → Bool | .anonymous, _ => true | .str p s, { header := h, .. } :: scopes => h == s && checkEndHeader p scopes | _, _ => false @[builtin_command_elab «namespace»] def elabNamespace : CommandElab := fun stx => match stx with | `(namespace $n) => addNamespace n.getId | _ => throwUnsupportedSyntax @[builtin_command_elab «section»] def elabSection : CommandElab := fun stx => do match stx with | `(section $header:ident) => addScopes (isNewNamespace := false) (isNoncomputable := false) header.getId | `(section) => addScope (isNewNamespace := false) (isNoncomputable := false) "" (← getCurrNamespace) | _ => throwUnsupportedSyntax @[builtin_command_elab noncomputableSection] def elabNonComputableSection : CommandElab := fun stx => do match stx with | `(noncomputable section $header:ident) => addScopes (isNewNamespace := false) (isNoncomputable := true) header.getId | `(noncomputable section) => addScope (isNewNamespace := false) (isNoncomputable := true) "" (← getCurrNamespace) | _ => throwUnsupportedSyntax @[builtin_command_elab «end»] def elabEnd : CommandElab := fun stx => do let header? := (stx.getArg 1).getOptionalIdent?; let endSize := match header? with | none => 1 | some n => n.getNumParts let scopes ← getScopes if endSize < scopes.length then modify fun s => { s with scopes := s.scopes.drop endSize } popScopes endSize else -- we keep "root" scope let n := (← get).scopes.length - 1 modify fun s => { s with scopes := s.scopes.drop n } popScopes n throwError "invalid 'end', insufficient scopes" match header? with | none => unless checkAnonymousScope scopes do throwError "invalid 'end', name is missing" | some header => unless checkEndHeader header scopes do addCompletionInfo <| CompletionInfo.endSection stx (scopes.map fun scope => scope.header) throwError "invalid 'end', name mismatch" private partial def elabChoiceAux (cmds : Array Syntax) (i : Nat) : CommandElabM Unit := if h : i < cmds.size then let cmd := cmds.get ⟨i, h⟩; catchInternalId unsupportedSyntaxExceptionId (elabCommand cmd) (fun _ => elabChoiceAux cmds (i+1)) else throwUnsupportedSyntax @[builtin_command_elab choice] def elabChoice : CommandElab := fun stx => elabChoiceAux stx.getArgs 0 @[builtin_command_elab «universe»] def elabUniverse : CommandElab := fun n => do n[1].forArgsM addUnivLevel @[builtin_command_elab «init_quot»] def elabInitQuot : CommandElab := fun _ => do match (← getEnv).addDecl Declaration.quotDecl with | Except.ok env => setEnv env | Except.error ex => throwError (ex.toMessageData (← getOptions)) @[builtin_command_elab «export»] def elabExport : CommandElab := fun stx => do let `(export $ns ($ids*)) := stx | throwUnsupportedSyntax let nss ← resolveNamespace ns let currNamespace ← getCurrNamespace if nss == [currNamespace] then throwError "invalid 'export', self export" let mut aliases := #[] for idStx in ids do let id := idStx.getId let declName ← resolveNameUsingNamespaces nss idStx aliases := aliases.push (currNamespace ++ id, declName) modify fun s => { s with env := aliases.foldl (init := s.env) fun env p => addAlias env p.1 p.2 } @[builtin_command_elab «open»] def elabOpen : CommandElab | `(open $decl:openDecl) => do let openDecls ← elabOpenDecl decl modifyScope fun scope => { scope with openDecls := openDecls } | _ => throwUnsupportedSyntax open Lean.Parser.Term private def typelessBinder? : Syntax → Option (Array (TSyntax [`ident, `Lean.Parser.Term.hole]) × Bool) | `(bracketedBinderF|($ids*)) => some (ids, true) | `(bracketedBinderF|{$ids*}) => some (ids, false) | _ => none /-- If `id` is an identifier, return true if `ids` contains `id`. -/ private def containsId (ids : Array (TSyntax [`ident, ``Parser.Term.hole])) (id : TSyntax [`ident, ``Parser.Term.hole]) : Bool := id.raw.isIdent && ids.any fun id' => id'.raw.getId == id.raw.getId /-- Auxiliary method for processing binder annotation update commands: `variable (α)` and `variable {α}`. The argument `binder` is the binder of the `variable` command. The method retuns an array containing the "residue", that is, variables that do not correspond to updates. Recall that a `bracketedBinder` can be of the form `(x y)`. ``` variable {α β : Type} variable (α γ) ``` The second `variable` command updates the binder annotation for `α`, and returns "residue" `γ`. -/ private def replaceBinderAnnotation (binder : TSyntax ``Parser.Term.bracketedBinder) : CommandElabM (Array (TSyntax ``Parser.Term.bracketedBinder)) := do let some (binderIds, explicit) := typelessBinder? binder | return #[binder] let varDecls := (← getScope).varDecls let mut varDeclsNew := #[] let mut binderIds := binderIds let mut binderIdsIniSize := binderIds.size let mut modifiedVarDecls := false for varDecl in varDecls do let (ids, ty?, explicit') ← match varDecl with | `(bracketedBinderF|($ids* $[: $ty?]? $(annot?)?)) => if annot?.isSome then for binderId in binderIds do if containsId ids binderId then throwErrorAt binderId "cannot update binder annotation of variables with default values/tactics" pure (ids, ty?, true) | `(bracketedBinderF|{$ids* $[: $ty?]?}) => pure (ids, ty?, false) | `(bracketedBinderF|[$id : $_]) => for binderId in binderIds do if binderId.raw.isIdent && binderId.raw.getId == id.getId then throwErrorAt binderId "cannot change the binder annotation of the previously declared local instance `{id.getId}`" varDeclsNew := varDeclsNew.push varDecl; continue | _ => varDeclsNew := varDeclsNew.push varDecl; continue if explicit == explicit' then -- no update, ensure we don't have redundant annotations. for binderId in binderIds do if containsId ids binderId then throwErrorAt binderId "redundant binder annotation update" varDeclsNew := varDeclsNew.push varDecl else if binderIds.all fun binderId => !containsId ids binderId then -- `binderIds` and `ids` are disjoint varDeclsNew := varDeclsNew.push varDecl else let mkBinder (id : TSyntax [`ident, ``Parser.Term.hole]) (explicit : Bool) : CommandElabM (TSyntax ``Parser.Term.bracketedBinder) := if explicit then `(bracketedBinderF| ($id $[: $ty?]?)) else `(bracketedBinderF| {$id $[: $ty?]?}) for id in ids do if let some idx := binderIds.findIdx? fun binderId => binderId.raw.isIdent && binderId.raw.getId == id.raw.getId then binderIds := binderIds.eraseIdx idx modifiedVarDecls := true varDeclsNew := varDeclsNew.push (← mkBinder id explicit) else varDeclsNew := varDeclsNew.push (← mkBinder id explicit') if modifiedVarDecls then modifyScope fun scope => { scope with varDecls := varDeclsNew } if binderIds.size != binderIdsIniSize then binderIds.mapM fun binderId => if explicit then `(bracketedBinderF| ($binderId)) else `(bracketedBinderF| {$binderId}) else return #[binder] @[builtin_command_elab «variable»] def elabVariable : CommandElab | `(variable $binders*) => do -- Try to elaborate `binders` for sanity checking runTermElabM fun _ => Term.withAutoBoundImplicit <| Term.elabBinders binders fun _ => pure () for binder in binders do let binders ← replaceBinderAnnotation binder -- Remark: if we want to produce error messages when variables shadow existing ones, here is the place to do it. for binder in binders do let varUIds ← getBracketedBinderIds binder |>.mapM (withFreshMacroScope ∘ MonadQuotation.addMacroScope) modifyScope fun scope => { scope with varDecls := scope.varDecls.push binder, varUIds := scope.varUIds ++ varUIds } | _ => throwUnsupportedSyntax open Meta def elabCheckCore (ignoreStuckTC : Bool) : CommandElab | `(#check%$tk $term) => withoutModifyingEnv <| runTermElabM fun _ => Term.withDeclName `_check do -- show signature for `#check id`/`#check @id` if let `($_:ident) := term then try for c in (← resolveGlobalConstWithInfos term) do addCompletionInfo <| .id term c (danglingDot := false) {} none logInfoAt tk <| .ofPPFormat { pp := fun | some ctx => ctx.runMetaM <| PrettyPrinter.ppSignature c | none => return f!"{c}" -- should never happen } return catch _ => pure () -- identifier might not be a constant but constant + projection let e ← Term.elabTerm term none Term.synthesizeSyntheticMVarsNoPostponing (ignoreStuckTC := ignoreStuckTC) let e ← Term.levelMVarToParam (← instantiateMVars e) let type ← inferType e if e.isSyntheticSorry then return logInfoAt tk m!"{e} : {type}" | _ => throwUnsupportedSyntax @[builtin_command_elab Lean.Parser.Command.check] def elabCheck : CommandElab := elabCheckCore (ignoreStuckTC := true) @[builtin_command_elab Lean.Parser.Command.reduce] def elabReduce : CommandElab | `(#reduce%$tk $term) => withoutModifyingEnv <| runTermElabM fun _ => Term.withDeclName `_reduce do let e ← Term.elabTerm term none Term.synthesizeSyntheticMVarsNoPostponing let e ← Term.levelMVarToParam (← instantiateMVars e) -- TODO: add options or notation for setting the following parameters withTheReader Core.Context (fun ctx => { ctx with options := ctx.options.setBool `smartUnfolding false }) do let e ← withTransparency (mode := TransparencyMode.all) <| reduce e (skipProofs := false) (skipTypes := false) logInfoAt tk e | _ => throwUnsupportedSyntax def hasNoErrorMessages : CommandElabM Bool := do return !(← get).messages.hasErrors def failIfSucceeds (x : CommandElabM Unit) : CommandElabM Unit := do let resetMessages : CommandElabM MessageLog := do let s ← get let messages := s.messages; modify fun s => { s with messages := {} }; pure messages let restoreMessages (prevMessages : MessageLog) : CommandElabM Unit := do modify fun s => { s with messages := prevMessages ++ s.messages.errorsToWarnings } let prevMessages ← resetMessages let succeeded ← try x hasNoErrorMessages catch | ex@(Exception.error _ _) => do logException ex; pure false | Exception.internal id _ => do logError (← id.getName); pure false finally restoreMessages prevMessages if succeeded then throwError "unexpected success" @[builtin_command_elab «check_failure»] def elabCheckFailure : CommandElab | `(#check_failure $term) => do failIfSucceeds <| elabCheckCore (ignoreStuckTC := false) (← `(#check $term)) | _ => throwUnsupportedSyntax private def mkEvalInstCore (evalClassName : Name) (e : Expr) : MetaM Expr := do let α ← inferType e let u ← getDecLevel α let inst := mkApp (Lean.mkConst evalClassName [u]) α try synthInstance inst catch _ => -- Put `α` in WHNF and try again try let α ← whnf α synthInstance (mkApp (Lean.mkConst evalClassName [u]) α) catch _ => -- Fully reduce `α` and try again try let α ← reduce (skipTypes := false) α synthInstance (mkApp (Lean.mkConst evalClassName [u]) α) catch _ => throwError "expression{indentExpr e}\nhas type{indentExpr α}\nbut instance{indentExpr inst}\nfailed to be synthesized, this instance instructs Lean on how to display the resulting value, recall that any type implementing the `Repr` class also implements the `{evalClassName}` class" private def mkRunMetaEval (e : Expr) : MetaM Expr := withLocalDeclD `env (mkConst ``Lean.Environment) fun env => withLocalDeclD `opts (mkConst ``Lean.Options) fun opts => do let α ← inferType e let u ← getDecLevel α let instVal ← mkEvalInstCore ``Lean.MetaEval e let e := mkAppN (mkConst ``Lean.runMetaEval [u]) #[α, instVal, env, opts, e] instantiateMVars (← mkLambdaFVars #[env, opts] e) private def mkRunEval (e : Expr) : MetaM Expr := do let α ← inferType e let u ← getDecLevel α let instVal ← mkEvalInstCore ``Lean.Eval e instantiateMVars (mkAppN (mkConst ``Lean.runEval [u]) #[α, instVal, mkSimpleThunk e]) unsafe def elabEvalUnsafe : CommandElab | `(#eval%$tk $term) => do let declName := `_eval let addAndCompile (value : Expr) : TermElabM Unit := do let value ← Term.levelMVarToParam (← instantiateMVars value) let type ← inferType value let us := collectLevelParams {} value |>.params let value ← instantiateMVars value let decl := Declaration.defnDecl { name := declName levelParams := us.toList type := type value := value hints := ReducibilityHints.opaque safety := DefinitionSafety.unsafe } Term.ensureNoUnassignedMVars decl addAndCompile decl -- Elaborate `term` let elabEvalTerm : TermElabM Expr := do let e ← Term.elabTerm term none Term.synthesizeSyntheticMVarsNoPostponing if (← Term.logUnassignedUsingErrorInfos (← getMVars e)) then throwAbortTerm if (← isProp e) then mkDecide e else return e -- Evaluate using term using `MetaEval` class. let elabMetaEval : CommandElabM Unit := do -- act? is `some act` if elaborated `term` has type `CommandElabM α` let act? ← runTermElabM fun _ => Term.withDeclName declName do let e ← elabEvalTerm let eType ← instantiateMVars (← inferType e) if eType.isAppOfArity ``CommandElabM 1 then let mut stx ← Term.exprToSyntax e unless (← isDefEq eType.appArg! (mkConst ``Unit)) do stx ← `($stx >>= fun v => IO.println (repr v)) let act ← Lean.Elab.Term.evalTerm (CommandElabM Unit) (mkApp (mkConst ``CommandElabM) (mkConst ``Unit)) stx pure <| some act else let e ← mkRunMetaEval e let env ← getEnv let opts ← getOptions let act ← try addAndCompile e; evalConst (Environment → Options → IO (String × Except IO.Error Environment)) declName finally setEnv env let (out, res) ← act env opts -- we execute `act` using the environment logInfoAt tk out match res with | Except.error e => throwError e.toString | Except.ok env => do setEnv env; pure none let some act := act? | return () act -- Evaluate using term using `Eval` class. let elabEval : CommandElabM Unit := runTermElabM fun _ => Term.withDeclName declName do -- fall back to non-meta eval if MetaEval hasn't been defined yet -- modify e to `runEval e` let e ← mkRunEval (← elabEvalTerm) let env ← getEnv let act ← try addAndCompile e; evalConst (IO (String × Except IO.Error Unit)) declName finally setEnv env let (out, res) ← liftM (m := IO) act logInfoAt tk out match res with | Except.error e => throwError e.toString | Except.ok _ => pure () if (← getEnv).contains ``Lean.MetaEval then do elabMetaEval else elabEval | _ => throwUnsupportedSyntax @[builtin_command_elab «eval», implemented_by elabEvalUnsafe] opaque elabEval : CommandElab @[builtin_command_elab «synth»] def elabSynth : CommandElab := fun stx => do let term := stx[1] withoutModifyingEnv <| runTermElabM fun _ => Term.withDeclName `_synth_cmd do let inst ← Term.elabTerm term none Term.synthesizeSyntheticMVarsNoPostponing let inst ← instantiateMVars inst let val ← synthInstance inst logInfo val pure () @[builtin_command_elab «set_option»] def elabSetOption : CommandElab := fun stx => do let options ← Elab.elabSetOption stx[1] stx[2] modify fun s => { s with maxRecDepth := maxRecDepth.get options } modifyScope fun scope => { scope with opts := options } @[builtin_macro Lean.Parser.Command.«in»] def expandInCmd : Macro | `($cmd₁ in $cmd₂) => `(section $cmd₁:command $cmd₂ end) | _ => Macro.throwUnsupported @[builtin_command_elab Parser.Command.addDocString] def elabAddDeclDoc : CommandElab := fun stx => do match stx with | `($doc:docComment add_decl_doc $id) => let declName ← resolveGlobalConstNoOverloadWithInfo id if let .none ← findDeclarationRangesCore? declName then -- this is only relevant for declarations added without a declaration range -- in particular `Quot.mk` et al which are added by `init_quot` addAuxDeclarationRanges declName stx id addDocString declName (← getDocStringText doc) | _ => throwUnsupportedSyntax @[builtin_command_elab Parser.Command.exit] def elabExit : CommandElab := fun _ => logWarning "using 'exit' to interrupt Lean" @[builtin_command_elab Parser.Command.import] def elabImport : CommandElab := fun _ => throwError "invalid 'import' command, it must be used in the beginning of the file" end Lean.Elab.Command
7bb8f8549fc53549ce3b9587ea3599c0e421a4a4
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/tests/lean/local_ref_bugs.lean
73e2241650ce3dd7ba8986c87300d8d228a171c1
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
708
lean
set_option pp.all true section parameter α : Type inductive foo : Type | a : α → foo | b #check (foo.b : foo) open foo #check (foo.b : foo) #check (b : foo) open tactic include α example : true := by do e ← to_expr `(b), t ← infer_type e, trace "-------", trace e, trace t, trace "-------", triv def ex : foo := begin trace_state, exact b end end namespace bla section parameter α : Type inductive foo : Type | a : α → foo | b #check (foo.b : foo) open foo #check (foo.b : foo) #check (b : foo) end end bla namespace boo section parameter α : Type inductive foo : Type | a : α → foo | b #check (foo.b : foo) open foo (b) #check (foo.b : foo) #check (b : foo) end end boo
8b7dfbe76c3c0ae2ec2aa04eda862ecf3bfa77a5
b561a44b48979a98df50ade0789a21c79ee31288
/stage0/src/Lean/Elab/SyntheticMVars.lean
6c37c15bd5ce63ba3eb98e88b0e5096964dc0416
[ "Apache-2.0" ]
permissive
3401ijk/lean4
97659c475ebd33a034fed515cb83a85f75ccfb06
a5b1b8de4f4b038ff752b9e607b721f15a9a4351
refs/heads/master
1,693,933,007,651
1,636,424,845,000
1,636,424,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,988
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Util.ForEachExpr import Lean.Elab.Term import Lean.Elab.Tactic.Basic namespace Lean.Elab.Term open Tactic (TacticM evalTactic getUnsolvedGoals withTacticInfoContext) open Meta /-- Auxiliary function used to implement `synthesizeSyntheticMVars`. -/ private def resumeElabTerm (stx : Syntax) (expectedType? : Option Expr) (errToSorry := true) : TermElabM Expr := -- Remark: if `ctx.errToSorry` is already false, then we don't enable it. Recall tactics disable `errToSorry` withReader (fun ctx => { ctx with errToSorry := ctx.errToSorry && errToSorry }) do elabTerm stx expectedType? false /-- Try to elaborate `stx` that was postponed by an elaboration method using `Expection.postpone`. It returns `true` if it succeeded, and `false` otherwise. It is used to implement `synthesizeSyntheticMVars`. -/ private def resumePostponed (savedContext : SavedContext) (stx : Syntax) (mvarId : MVarId) (postponeOnError : Bool) : TermElabM Bool := withRef stx <| withMVarContext mvarId do let s ← get try withSavedContext savedContext do let mvarDecl ← getMVarDecl mvarId let expectedType ← instantiateMVars mvarDecl.type withInfoHole mvarId do let result ← resumeElabTerm stx expectedType (!postponeOnError) /- We must ensure `result` has the expected type because it is the one expected by the method that postponed stx. That is, the method does not have an opportunity to check whether `result` has the expected type or not. -/ let result ← withRef stx <| ensureHasType expectedType result /- We must perform `occursCheck` here since `result` may contain `mvarId` when it has synthetic `sorry`s. -/ if (← occursCheck mvarId result) then assignExprMVar mvarId result return true else return false catch | ex@(Exception.internal id _) => if id == postponeExceptionId then set s return false else throw ex | ex@(Exception.error _ _) => if postponeOnError then set s return false else logException ex return true /-- Similar to `synthesizeInstMVarCore`, but makes sure that `instMVar` local context and instances are used. It also logs any error message produced. -/ private def synthesizePendingInstMVar (instMVar : MVarId) : TermElabM Bool := withMVarContext instMVar do try synthesizeInstMVarCore instMVar catch | ex@(Exception.error _ _) => logException ex; return true | _ => unreachable! /-- Similar to `synthesizePendingInstMVar`, but generates type mismatch error message. Remark: `eNew` is of the form `@coe ... mvar`, where `mvar` is the metavariable for the `CoeT ...` instance. If `mvar` can be synthesized, then assign `auxMVarId := (expandCoe eNew)`. -/ private def synthesizePendingCoeInstMVar (auxMVarId : MVarId) (errorMsgHeader? : Option String) (eNew : Expr) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) : TermElabM Bool := do let instMVarId := eNew.appArg!.mvarId! withMVarContext instMVarId do if (← isDefEq expectedType eType) then /- This case may seem counterintuitive since we created the coercion because the `isDefEq expectedType eType` test failed before. However, it may succeed here because we have more information, for example, metavariables occurring at `expectedType` and `eType` may have been assigned. -/ if (← occursCheck auxMVarId e) then assignExprMVar auxMVarId e return true else return false try if (← synthesizeCoeInstMVarCore instMVarId) then let eNew ← expandCoe eNew if (← occursCheck auxMVarId eNew) then assignExprMVar auxMVarId eNew return true return false catch | Exception.error _ msg => throwTypeMismatchError errorMsgHeader? expectedType eType e f? msg | _ => unreachable! /-- Try to synthesize a value for `mvarId` using the given default instance. Return `some (val, mvarDecls)` if successful, where `val` is the value assigned to `mvarId`, and `mvarDecls` is a list of new type class instances that need to be synthesized. -/ private def tryToSynthesizeUsingDefaultInstance (mvarId : MVarId) (defaultInstance : Name) : TermElabM (Option (Expr × List SyntheticMVarDecl)) := commitWhenSome? do let candidate ← mkConstWithFreshMVarLevels defaultInstance let (mvars, bis, _) ← forallMetaTelescopeReducing (← inferType candidate) let candidate := mkAppN candidate mvars trace[Elab.resume] "trying default instance for {mkMVar mvarId} := {candidate}" if (← isDefEqGuarded (mkMVar mvarId) candidate) then -- Succeeded. Collect new TC problems let mut result := [] for i in [:bis.size] do if bis[i] == BinderInfo.instImplicit then result := { mvarId := mvars[i].mvarId!, stx := (← getRef), kind := SyntheticMVarKind.typeClass } :: result trace[Elab.resume] "worked" return some (candidate, result) else return none private def tryToSynthesizeUsingDefaultInstances (mvarId : MVarId) (prio : Nat) : TermElabM (Option (Expr × List SyntheticMVarDecl)) := withMVarContext mvarId do let mvarType := (← Meta.getMVarDecl mvarId).type match (← isClass? mvarType) with | none => return none | some className => match (← getDefaultInstances className) with | [] => return none | defaultInstances => for (defaultInstance, instPrio) in defaultInstances do if instPrio == prio then match (← tryToSynthesizeUsingDefaultInstance mvarId defaultInstance) with | some result => return some result | none => continue return none /- Used to implement `synthesizeUsingDefault`. This method only consider default instances with the given priority. -/ private def synthesizeUsingDefaultPrio (prio : Nat) : TermElabM Bool := do let rec visit (syntheticMVars : List SyntheticMVarDecl) (syntheticMVarsNew : List SyntheticMVarDecl) : TermElabM Bool := do match syntheticMVars with | [] => return false | mvarDecl :: mvarDecls => match mvarDecl.kind with | SyntheticMVarKind.typeClass => match (← withRef mvarDecl.stx <| tryToSynthesizeUsingDefaultInstances mvarDecl.mvarId prio) with | none => visit mvarDecls (mvarDecl :: syntheticMVarsNew) | some (val, newMVarDecls) => for newMVarDecl in newMVarDecls do -- Register that `newMVarDecl.mvarId`s are implicit arguments of the value assigned to `mvarDecl.mvarId` registerMVarErrorImplicitArgInfo newMVarDecl.mvarId (← getRef) val let syntheticMVarsNew := newMVarDecls ++ syntheticMVarsNew let syntheticMVarsNew := mvarDecls.reverse ++ syntheticMVarsNew modify fun s => { s with syntheticMVars := syntheticMVarsNew } return true | _ => visit mvarDecls (mvarDecl :: syntheticMVarsNew) /- Recall that s.syntheticMVars is essentially a stack. The first metavariable was the last one created. We want to apply the default instance in reverse creation order. Otherwise, `toString 0` will produce a `OfNat String _` cannot be synthesized error. -/ visit (← get).syntheticMVars.reverse [] /-- Apply default value to any pending synthetic metavariable of kind `SyntheticMVarKind.withDefault` Return true if something was synthesized. -/ private def synthesizeUsingDefault : TermElabM Bool := do let prioSet ← getDefaultInstancesPriorities /- Recall that `prioSet` is stored in descending order -/ for prio in prioSet do if (← synthesizeUsingDefaultPrio prio) then return true return false /-- Report an error for each synthetic metavariable that could not be resolved. Remark: we set `ignoreStuckTC := true` when elaborating `simp` arguments. -/ private def reportStuckSyntheticMVars (ignoreStuckTC := false) : TermElabM Unit := do let syntheticMVars ← modifyGet fun s => (s.syntheticMVars, { s with syntheticMVars := [] }) for mvarSyntheticDecl in syntheticMVars do withRef mvarSyntheticDecl.stx do match mvarSyntheticDecl.kind with | SyntheticMVarKind.typeClass => unless ignoreStuckTC do withMVarContext mvarSyntheticDecl.mvarId do let mvarDecl ← getMVarDecl mvarSyntheticDecl.mvarId unless (← get).messages.hasErrors do throwError "typeclass instance problem is stuck, it is often due to metavariables{indentExpr mvarDecl.type}" | SyntheticMVarKind.coe header eNew expectedType eType e f? => let mvarId := eNew.appArg!.mvarId! withMVarContext mvarId do let mvarDecl ← getMVarDecl mvarId throwTypeMismatchError header expectedType eType e f? (some ("failed to create type class instance for " ++ indentExpr mvarDecl.type)) | _ => unreachable! -- TODO handle other cases. private def getSomeSynthethicMVarsRef : TermElabM Syntax := do let s ← get match s.syntheticMVars.find? fun (mvarDecl : SyntheticMVarDecl) => !mvarDecl.stx.getPos?.isNone with | some mvarDecl => return mvarDecl.stx | none => return Syntax.missing mutual partial def runTactic (mvarId : MVarId) (tacticCode : Syntax) : TermElabM Unit := do /- Recall, `tacticCode` is the whole `by ...` expression. -/ let byTk := tacticCode[0] let code := tacticCode[1] modifyThe Meta.State fun s => { s with mctx := s.mctx.instantiateMVarDeclMVars mvarId } let remainingGoals ← withInfoHole mvarId <| Tactic.run mvarId do withTacticInfoContext tacticCode (evalTactic code) synthesizeSyntheticMVars (mayPostpone := false) unless remainingGoals.isEmpty do reportUnsolvedGoals remainingGoals /-- Try to synthesize the given pending synthetic metavariable. -/ private partial def synthesizeSyntheticMVar (mvarSyntheticDecl : SyntheticMVarDecl) (postponeOnError : Bool) (runTactics : Bool) : TermElabM Bool := withRef mvarSyntheticDecl.stx do match mvarSyntheticDecl.kind with | SyntheticMVarKind.typeClass => synthesizePendingInstMVar mvarSyntheticDecl.mvarId | SyntheticMVarKind.coe header? eNew expectedType eType e f? => synthesizePendingCoeInstMVar mvarSyntheticDecl.mvarId header? eNew expectedType eType e f? -- NOTE: actual processing at `synthesizeSyntheticMVarsAux` | SyntheticMVarKind.postponed savedContext => resumePostponed savedContext mvarSyntheticDecl.stx mvarSyntheticDecl.mvarId postponeOnError | SyntheticMVarKind.tactic tacticCode savedContext => withSavedContext savedContext do if runTactics then runTactic mvarSyntheticDecl.mvarId tacticCode return true else return false /-- Try to synthesize the current list of pending synthetic metavariables. Return `true` if at least one of them was synthesized. -/ private partial def synthesizeSyntheticMVarsStep (postponeOnError : Bool) (runTactics : Bool) : TermElabM Bool := do let ctx ← read traceAtCmdPos `Elab.resuming fun _ => m!"resuming synthetic metavariables, mayPostpone: {ctx.mayPostpone}, postponeOnError: {postponeOnError}" let syntheticMVars := (← get).syntheticMVars let numSyntheticMVars := syntheticMVars.length -- We reset `syntheticMVars` because new synthetic metavariables may be created by `synthesizeSyntheticMVar`. modify fun s => { s with syntheticMVars := [] } -- Recall that `syntheticMVars` is a list where head is the most recent pending synthetic metavariable. -- We use `filterRevM` instead of `filterM` to make sure we process the synthetic metavariables using the order they were created. -- It would not be incorrect to use `filterM`. let remainingSyntheticMVars ← syntheticMVars.filterRevM fun mvarDecl => do -- We use `traceM` because we want to make sure the metavar local context is used to trace the message traceM `Elab.postpone (withMVarContext mvarDecl.mvarId do addMessageContext m!"resuming {mkMVar mvarDecl.mvarId}") let succeeded ← synthesizeSyntheticMVar mvarDecl postponeOnError runTactics trace[Elab.postpone] if succeeded then format "succeeded" else format "not ready yet" pure !succeeded -- Merge new synthetic metavariables with `remainingSyntheticMVars`, i.e., metavariables that still couldn't be synthesized modify fun s => { s with syntheticMVars := s.syntheticMVars ++ remainingSyntheticMVars } return numSyntheticMVars != remainingSyntheticMVars.length /-- Try to process pending synthetic metavariables. If `mayPostpone == false`, then `syntheticMVars` is `[]` after executing this method. It keeps executing `synthesizeSyntheticMVarsStep` while progress is being made. If `mayPostpone == false`, then it applies default instances to `SyntheticMVarKind.typeClass` (if available) metavariables that are still unresolved, and then tries to resolve metavariables with `mayPostpone == false`. That is, we force them to produce error messages and/or commit to a "best option". If, after that, we still haven't made progress, we report "stuck" errors. Remark: we set `ignoreStuckTC := true` when elaborating `simp` arguments. Then, pending TC problems become implicit parameters for the simp theorem. -/ partial def synthesizeSyntheticMVars (mayPostpone := true) (ignoreStuckTC := false) : TermElabM Unit := let rec loop (u : Unit) : TermElabM Unit := do withRef (← getSomeSynthethicMVarsRef) <| withIncRecDepth do unless (← get).syntheticMVars.isEmpty do if ← synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := false) then loop () else if !mayPostpone then /- Resume pending metavariables with "elaboration postponement" disabled. We postpone elaboration errors in this step by setting `postponeOnError := true`. Example: ``` #check let x := ⟨1, 2⟩; Prod.fst x ``` The term `⟨1, 2⟩` can't be elaborated because the expected type is not know. The `x` at `Prod.fst x` is not elaborated because the type of `x` is not known. When we execute the following step with "elaboration postponement" disabled, the elaborator fails at `⟨1, 2⟩` and postpones it, and succeeds at `x` and learns that its type must be of the form `Prod ?α ?β`. Recall that we postponed `x` at `Prod.fst x` because its type it is not known. We the type of `x` may learn later its type and it may contain implicit and/or auto arguments. By disabling postponement, we are essentially giving up the opportunity of learning `x`s type and assume it does not have implict and/or auto arguments. -/ if ← withoutPostponing <| synthesizeSyntheticMVarsStep (postponeOnError := true) (runTactics := false) then loop () else if ← synthesizeUsingDefault then loop () else if ← withoutPostponing <| synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := false) then loop () else if ← synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := true) then loop () else reportStuckSyntheticMVars ignoreStuckTC loop () end def synthesizeSyntheticMVarsNoPostponing (ignoreStuckTC := false) : TermElabM Unit := synthesizeSyntheticMVars (mayPostpone := false) (ignoreStuckTC := ignoreStuckTC) /- Keep invoking `synthesizeUsingDefault` until it returns false. -/ private partial def synthesizeUsingDefaultLoop : TermElabM Unit := do if (← synthesizeUsingDefault) then synthesizeSyntheticMVars (mayPostpone := true) synthesizeUsingDefaultLoop def synthesizeSyntheticMVarsUsingDefault : TermElabM Unit := do synthesizeSyntheticMVars (mayPostpone := true) synthesizeUsingDefaultLoop private partial def withSynthesizeImp {α} (k : TermElabM α) (mayPostpone : Bool) (synthesizeDefault : Bool) : TermElabM α := do let syntheticMVarsSaved := (← get).syntheticMVars modify fun s => { s with syntheticMVars := [] } try let a ← k synthesizeSyntheticMVars mayPostpone if mayPostpone && synthesizeDefault then synthesizeUsingDefaultLoop return a finally modify fun s => { s with syntheticMVars := s.syntheticMVars ++ syntheticMVarsSaved } /-- Execute `k`, and synthesize pending synthetic metavariables created while executing `k` are solved. If `mayPostpone == false`, then all of them must be synthesized. Remark: even if `mayPostpone == true`, the method still uses `synthesizeUsingDefault` -/ @[inline] def withSynthesize [MonadFunctorT TermElabM m] [Monad m] (k : m α) (mayPostpone := false) : m α := monadMap (m := TermElabM) (withSynthesizeImp . mayPostpone (synthesizeDefault := true)) k /-- Similar to `withSynthesize`, but sets `mayPostpone` to `true`, and do not use `synthesizeUsingDefault` -/ @[inline] def withSynthesizeLight [MonadFunctorT TermElabM m] [Monad m] (k : m α) : m α := monadMap (m := TermElabM) (withSynthesizeImp . (mayPostpone := true) (synthesizeDefault := false)) k /-- Elaborate `stx`, and make sure all pending synthetic metavariables created while elaborating `stx` are solved. -/ def elabTermAndSynthesize (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := withRef stx do instantiateMVars (← withSynthesize <| elabTerm stx expectedType?) end Lean.Elab.Term
d97012ae81d3e4763740fb83039c8b6348e3d866
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/category/LinearOrder.lean
9e77eaac4f59ac088182c79f2f93244d2c419722
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
867
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import order.category.PartialOrder /-! # Category of linearly ordered types -/ open category_theory /-- The category of linearly ordered types. -/ def LinearOrder := bundled linear_order namespace LinearOrder instance : bundled_hom.parent_projection @linear_order.to_partial_order := ⟨⟩ attribute [derive [large_category, concrete_category]] LinearOrder instance : has_coe_to_sort LinearOrder Type* := bundled.has_coe_to_sort /-- Construct a bundled LinearOrder from the underlying type and typeclass. -/ def of (α : Type*) [linear_order α] : LinearOrder := bundled.of α instance : inhabited LinearOrder := ⟨of punit⟩ instance (α : LinearOrder) : linear_order α := α.str end LinearOrder
5b3d6c21357a094802ea9f0c43f27e1ef5fd61c4
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/category/CompHaus/projective.lean
6ff261e92f42c095bc93072d461e7721cedb7799
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
2,085
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import topology.category.CompHaus import topology.stone_cech import category_theory.preadditive.projective /-! # CompHaus has enough projectives In this file we show that `CompHaus` has enough projectives. ## Main results Let `X` be a compact Hausdorff space. * `CompHaus.projective_ultrafilter`: the space `ultrafilter X` is a projective object * `CompHaus.projective_presentation`: the natural map `ultrafilter X → X` is a projective presentation ## Reference See [miraglia2006introduction] Chapter 21 for a proof that `CompHaus` has enough projectives. -/ noncomputable theory open category_theory function namespace CompHaus instance projective_ultrafilter (X : Type*) : projective (of $ ultrafilter X) := { factors := λ Y Z f g hg, begin rw epi_iff_surjective at hg, obtain ⟨g', hg'⟩ := hg.has_right_inverse, let t : X → Y := g' ∘ f ∘ (pure : X → ultrafilter X), let h : ultrafilter X → Y := ultrafilter.extend t, have hh : continuous h := continuous_ultrafilter_extend _, use ⟨h, hh⟩, apply faithful.map_injective (forget CompHaus), simp only [forget_map_eq_coe, continuous_map.coe_mk, coe_comp], convert dense_range_pure.equalizer (g.continuous.comp hh) f.continuous _, rw [comp.assoc, ultrafilter_extend_extends, ← comp.assoc, hg'.comp_eq_id, comp.left_id], end } /-- For any compact Hausdorff space `X`, the natural map `ultrafilter X → X` is a projective presentation. -/ def projective_presentation (X : CompHaus) : projective_presentation X := { P := of $ ultrafilter X, f := ⟨_, continuous_ultrafilter_extend id⟩, projective := CompHaus.projective_ultrafilter X, epi := concrete_category.epi_of_surjective _ $ λ x, ⟨(pure x : ultrafilter X), congr_fun (ultrafilter_extend_extends (𝟙 X)) x⟩ } instance : enough_projectives CompHaus := { presentation := λ X, ⟨projective_presentation X⟩ } end CompHaus
9bdd156e819ef414c152640d189b3fd8295f9067
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/category_theory/monad/limits.lean
0a3cc1396f151344332c005c6348d04922ec1f7f
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,973
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.monad.adjunction import category_theory.adjunction.limits import category_theory.limits.preserves.shapes.terminal namespace category_theory open category open category_theory.limits universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation namespace monad variables {C : Type u₁} [category.{v₁} C] variables {T : monad C} variables {J : Type v₁} [small_category J] namespace forget_creates_limits variables (D : J ⥤ algebra T) (c : cone (D ⋙ forget T)) (t : is_limit c) /-- (Impl) The natural transformation used to define the new cone -/ @[simps] def γ : (D ⋙ forget T ⋙ ↑T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a } /-- (Impl) This new cone is used to construct the algebra structure -/ @[simps] def new_cone : cone (D ⋙ forget T) := { X := T.obj c.X, π := (functor.const_comp _ _ ↑T).inv ≫ whisker_right c.π T ≫ (γ D) } /-- The algebra structure which will be the apex of the new limit cone for `D`. -/ @[simps] def cone_point : algebra T := { A := c.X, a := t.lift (new_cone D c), unit' := begin apply t.hom_ext, intro j, erw [category.assoc, t.fac (new_cone D c), id_comp], dsimp, erw [id_comp, ← category.assoc, ← T.η.naturality, functor.id_map, category.assoc, (D.obj j).unit, comp_id], end, assoc' := begin apply t.hom_ext, intro j, rw [category.assoc, category.assoc, t.fac (new_cone D c)], dsimp, erw id_comp, slice_lhs 1 2 {rw ← T.μ.naturality}, slice_lhs 2 3 {rw (D.obj j).assoc}, slice_rhs 1 2 {rw ← (T : C ⥤ C).map_comp}, rw t.fac (new_cone D c), dsimp, erw [id_comp, functor.map_comp, category.assoc] end } /-- (Impl) Construct the lifted cone in `algebra T` which will be limiting. -/ @[simps] def lifted_cone : cone D := { X := cone_point D c t, π := { app := λ j, { f := c.π.app j }, naturality' := λ X Y f, by { ext1, dsimp, erw c.w f, simp } } } /-- (Impl) Prove that the lifted cone is limiting. -/ @[simps] def lifted_cone_is_limit : is_limit (lifted_cone D c t) := { lift := λ s, { f := t.lift ((forget T).map_cone s), h' := begin apply t.hom_ext, intro j, slice_rhs 2 3 {rw t.fac ((forget T).map_cone s) j}, dsimp, slice_lhs 2 3 {rw t.fac (new_cone D c) j}, dsimp, rw category.id_comp, slice_lhs 1 2 {rw ← (T : C ⥤ C).map_comp}, rw t.fac ((forget T).map_cone s) j, exact (s.π.app j).h end }, uniq' := λ s m J, begin ext1, apply t.hom_ext, intro j, simpa [t.fac (functor.map_cone (forget T) s) j] using congr_arg algebra.hom.f (J j), end } end forget_creates_limits -- Theorem 5.6.5 from [Riehl][riehl2017] /-- The forgetful functor from the Eilenberg-Moore category creates limits. -/ noncomputable instance forget_creates_limits : creates_limits (forget T) := { creates_limits_of_shape := λ J 𝒥, by exactI { creates_limit := λ D, creates_limit_of_reflects_iso (λ c t, { lifted_cone := forget_creates_limits.lifted_cone D c t, valid_lift := cones.ext (iso.refl _) (λ j, (id_comp _).symm), makes_limit := forget_creates_limits.lifted_cone_is_limit _ _ _ } ) } } /-- `D ⋙ forget T` has a limit, then `D` has a limit. -/ lemma has_limit_of_comp_forget_has_limit (D : J ⥤ algebra T) [has_limit (D ⋙ forget T)] : has_limit D := has_limit_of_created D (forget T) namespace forget_creates_colimits -- Let's hide the implementation details in a namespace variables {D : J ⥤ algebra T} (c : cocone (D ⋙ forget T)) (t : is_colimit c) -- We have a diagram D of shape J in the category of algebras, and we assume that we are given a -- colimit for its image D ⋙ forget T under the forgetful functor, say its apex is L. -- We'll construct a colimiting coalgebra for D, whose carrier will also be L. -- To do this, we must find a map TL ⟶ L. Since T preserves colimits, TL is also a colimit. -- In particular, it is a colimit for the diagram `(D ⋙ forget T) ⋙ T` -- so to construct a map TL ⟶ L it suffices to show that L is the apex of a cocone for this diagram. -- In other words, we need a natural transformation from const L to `(D ⋙ forget T) ⋙ T`. -- But we already know that L is the apex of a cocone for the diagram `D ⋙ forget T`, so it -- suffices to give a natural transformation `((D ⋙ forget T) ⋙ T) ⟶ (D ⋙ forget T)`: /-- (Impl) The natural transformation given by the algebra structure maps, used to construct a cocone `c` with apex `colimit (D ⋙ forget T)`. -/ @[simps] def γ : ((D ⋙ forget T) ⋙ ↑T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a } /-- (Impl) A cocone for the diagram `(D ⋙ forget T) ⋙ T` found by composing the natural transformation `γ` with the colimiting cocone for `D ⋙ forget T`. -/ @[simps] def new_cocone : cocone ((D ⋙ forget T) ⋙ ↑T) := { X := c.X, ι := γ ≫ c.ι } variables [preserves_colimit (D ⋙ forget T) (T : C ⥤ C)] /-- (Impl) Define the map `λ : TL ⟶ L`, which will serve as the structure of the coalgebra on `L`, and we will show is the colimiting object. We use the cocone constructed by `c` and the fact that `T` preserves colimits to produce this morphism. -/ @[reducible] def lambda : ((T : C ⥤ C).map_cocone c).X ⟶ c.X := (preserves_colimit.preserves t).desc (new_cocone c) /-- (Impl) The key property defining the map `λ : TL ⟶ L`. -/ lemma commuting (j : J) : T.map (c.ι.app j) ≫ lambda c t = (D.obj j).a ≫ c.ι.app j := is_colimit.fac (preserves_colimit.preserves t) (new_cocone c) j variables [preserves_colimit ((D ⋙ forget T) ⋙ ↑T) (T : C ⥤ C)] /-- (Impl) Construct the colimiting algebra from the map `λ : TL ⟶ L` given by `lambda`. We are required to show it satisfies the two algebra laws, which follow from the algebra laws for the image of `D` and our `commuting` lemma. -/ @[simps] def cocone_point : algebra T := { A := c.X, a := lambda c t, unit' := begin apply t.hom_ext, intro j, erw [comp_id, ← category.assoc, T.η.naturality, category.assoc, commuting, ← category.assoc], erw algebra.unit, apply id_comp end, assoc' := begin apply is_colimit.hom_ext (preserves_colimit.preserves (preserves_colimit.preserves t)), intro j, erw [← category.assoc, T.μ.naturality, ← functor.map_cocone_ι_app, category.assoc, is_colimit.fac _ (new_cocone c) j], rw ← category.assoc, erw [← functor.map_comp, commuting], dsimp, erw [← category.assoc, algebra.assoc, category.assoc, functor.map_comp, category.assoc, commuting], apply_instance, apply_instance end } /-- (Impl) Construct the lifted cocone in `algebra T` which will be colimiting. -/ @[simps] def lifted_cocone : cocone D := { X := cocone_point c t, ι := { app := λ j, { f := c.ι.app j, h' := commuting _ _ _ }, naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } } /-- (Impl) Prove that the lifted cocone is colimiting. -/ @[simps] def lifted_cocone_is_colimit : is_colimit (lifted_cocone c t) := { desc := λ s, { f := t.desc ((forget T).map_cocone s), h' := begin dsimp, apply is_colimit.hom_ext (preserves_colimit.preserves t), intro j, rw ← category.assoc, erw ← functor.map_comp, erw t.fac', rw ← category.assoc, erw forget_creates_colimits.commuting, rw category.assoc, rw t.fac', apply algebra.hom.h, apply_instance end }, uniq' := λ s m J, by { ext1, apply t.hom_ext, intro j, simpa using congr_arg algebra.hom.f (J j) } } end forget_creates_colimits open forget_creates_colimits -- TODO: the converse of this is true as well /-- The forgetful functor from the Eilenberg-Moore category for a monad creates any colimit which the monad itself preserves. -/ noncomputable instance forget_creates_colimit (D : J ⥤ algebra T) [preserves_colimit (D ⋙ forget T) (T : C ⥤ C)] [preserves_colimit ((D ⋙ forget T) ⋙ ↑T) (T : C ⥤ C)] : creates_colimit D (forget T) := creates_colimit_of_reflects_iso $ λ c t, { lifted_cocone := { X := cocone_point c t, ι := { app := λ j, { f := c.ι.app j, h' := commuting _ _ _ }, naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } }, valid_lift := cocones.ext (iso.refl _) (by tidy), makes_colimit := lifted_cocone_is_colimit _ _ } noncomputable instance forget_creates_colimits_of_shape [preserves_colimits_of_shape J (T : C ⥤ C)] : creates_colimits_of_shape J (forget T) := { creates_colimit := λ K, by apply_instance } noncomputable instance forget_creates_colimits [preserves_colimits (T : C ⥤ C)] : creates_colimits (forget T) := { creates_colimits_of_shape := λ J 𝒥₁, by apply_instance } /-- For `D : J ⥤ algebra T`, `D ⋙ forget T` has a colimit, then `D` has a colimit provided colimits of shape `J` are preserved by `T`. -/ lemma forget_creates_colimits_of_monad_preserves [preserves_colimits_of_shape J (T : C ⥤ C)] (D : J ⥤ algebra T) [has_colimit (D ⋙ forget T)] : has_colimit D := has_colimit_of_created D (forget T) end monad variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₁} D] variables {J : Type v₁} [small_category J] instance comp_comparison_forget_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] : has_limit ((F ⋙ monad.comparison (adjunction.of_right_adjoint R)) ⋙ monad.forget _) := @has_limit_of_iso _ _ _ _ (F ⋙ R) _ _ (iso_whisker_left F (monad.comparison_forget (adjunction.of_right_adjoint R)).symm) instance comp_comparison_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] : has_limit (F ⋙ monad.comparison (adjunction.of_right_adjoint R)) := monad.has_limit_of_comp_forget_has_limit (F ⋙ monad.comparison (adjunction.of_right_adjoint R)) /-- Any monadic functor creates limits. -/ noncomputable def monadic_creates_limits (R : D ⥤ C) [monadic_right_adjoint R] : creates_limits R := creates_limits_of_nat_iso (monad.comparison_forget (adjunction.of_right_adjoint R)) /-- The forgetful functor from the Eilenberg-Moore category for a monad creates any colimit which the monad itself preserves. -/ noncomputable def monadic_creates_colimit_of_preserves_colimit (R : D ⥤ C) (K : J ⥤ D) [monadic_right_adjoint R] [preserves_colimit (K ⋙ R) (left_adjoint R ⋙ R)] [preserves_colimit ((K ⋙ R) ⋙ left_adjoint R ⋙ R) (left_adjoint R ⋙ R)] : creates_colimit K R := begin apply creates_colimit_of_nat_iso (monad.comparison_forget (adjunction.of_right_adjoint R)), apply category_theory.comp_creates_colimit _ _, apply_instance, let i : ((K ⋙ monad.comparison (adjunction.of_right_adjoint R)) ⋙ monad.forget _) ≅ K ⋙ R := functor.associator _ _ _ ≪≫ iso_whisker_left K (monad.comparison_forget (adjunction.of_right_adjoint R)), apply category_theory.monad.forget_creates_colimit _, { dsimp, refine preserves_colimit_of_iso_diagram _ i.symm }, { dsimp, refine preserves_colimit_of_iso_diagram _ (iso_whisker_right i (left_adjoint R ⋙ R)).symm }, end /-- A monadic functor creates any colimits of shapes it preserves. -/ noncomputable def monadic_creates_colimits_of_shape_of_preserves_colimits_of_shape (R : D ⥤ C) [monadic_right_adjoint R] [preserves_colimits_of_shape J R] : creates_colimits_of_shape J R := begin have : preserves_colimits_of_shape J (left_adjoint R ⋙ R), { apply category_theory.limits.comp_preserves_colimits_of_shape _ _, { haveI := adjunction.left_adjoint_preserves_colimits (adjunction.of_right_adjoint R), apply_instance }, apply_instance }, exactI ⟨λ K, monadic_creates_colimit_of_preserves_colimit _ _⟩, end /-- A monadic functor creates colimits if it preserves colimits. -/ noncomputable def monadic_creates_colimits_of_preserves_colimits (R : D ⥤ C) [monadic_right_adjoint R] [preserves_colimits R] : creates_colimits R := { creates_colimits_of_shape := λ J 𝒥₁, by exactI monadic_creates_colimits_of_shape_of_preserves_colimits_of_shape _ } section lemma has_limit_of_reflective (F : J ⥤ D) (R : D ⥤ C) [has_limit (F ⋙ R)] [reflective R] : has_limit F := by { haveI := monadic_creates_limits R, exact has_limit_of_created F R } /-- If `C` has limits of shape `J` then any reflective subcategory has limits of shape `J`. -/ lemma has_limits_of_shape_of_reflective [has_limits_of_shape J C] (R : D ⥤ C) [reflective R] : has_limits_of_shape J D := { has_limit := λ F, has_limit_of_reflective F R } /-- If `C` has limits then any reflective subcategory has limits. -/ lemma has_limits_of_reflective (R : D ⥤ C) [has_limits C] [reflective R] : has_limits D := { has_limits_of_shape := λ J 𝒥₁, by exactI has_limits_of_shape_of_reflective R } /-- The reflector always preserves terminal objects. Note this in general doesn't apply to any other limit. -/ noncomputable def left_adjoint_preserves_terminal_of_reflective (R : D ⥤ C) [reflective R] [has_terminal C] : preserves_limits_of_shape (discrete pempty) (left_adjoint R) := { preserves_limit := λ K, begin letI : has_terminal D := has_limits_of_shape_of_reflective R, letI := monadic_creates_limits R, letI := category_theory.preserves_limit_of_creates_limit_and_has_limit (functor.empty _) R, letI : preserves_limit (functor.empty _) (left_adjoint R), { apply preserves_terminal_of_iso, apply _ ≪≫ as_iso ((adjunction.of_right_adjoint R).counit.app (⊤_ D)), apply (left_adjoint R).map_iso (preserves_terminal.iso R).symm }, apply preserves_limit_of_iso_diagram (left_adjoint R) (functor.unique_from_empty _).symm, end } end end category_theory
f898cf9187ed040426f386214e00f2662c71f0ed
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/topology/continuous_function/basic.lean
7aac9108a69421e39c515e0e87ac6a163ca696b6
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,218
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri -/ import topology.subset_properties import topology.tactic import topology.algebra.ordered.proj_Icc /-! # Continuous bundled map In this file we define the type `continuous_map` of continuous bundled maps. -/ /-- Bundled continuous maps. -/ @[protect_proj] structure continuous_map (α : Type*) (β : Type*) [topological_space α] [topological_space β] := (to_fun : α → β) (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') notation `C(` α `, ` β `)` := continuous_map α β namespace continuous_map attribute [continuity] continuous_map.continuous_to_fun variables {α : Type*} {β : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] instance : has_coe_to_fun (C(α, β)) := ⟨_, continuous_map.to_fun⟩ @[simp] lemma to_fun_eq_coe {f : C(α, β)} : f.to_fun = (f : α → β) := rfl variables {α β} {f g : continuous_map α β} @[continuity] protected lemma continuous (f : C(α, β)) : continuous f := f.continuous_to_fun @[continuity] lemma continuous_set_coe (s : set C(α, β)) (f : s) : continuous f := by { cases f, dsimp, continuity, } protected lemma continuous_at (f : C(α, β)) (x : α) : continuous_at f x := f.continuous.continuous_at protected lemma continuous_within_at (f : C(α, β)) (s : set α) (x : α) : continuous_within_at f s x := f.continuous.continuous_within_at protected lemma congr_fun {f g : C(α, β)} (H : f = g) (x : α) : f x = g x := H ▸ rfl protected lemma congr_arg (f : C(α, β)) {x y : α} (h : x = y) : f x = f y := h ▸ rfl @[ext] theorem ext (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H lemma ext_iff : f = g ↔ ∀ x, f x = g x := ⟨continuous_map.congr_fun, ext⟩ instance [inhabited β] : inhabited C(α, β) := ⟨{ to_fun := λ _, default _, }⟩ lemma coe_inj ⦃f g : C(α, β)⦄ (h : (f : α → β) = g) : f = g := by cases f; cases g; cases h; refl @[simp] lemma coe_mk (f : α → β) (h : continuous f) : ⇑(⟨f, h⟩ : continuous_map α β) = f := rfl section variables (α β) /-- The continuous functions from `α` to `β` are the same as the plain functions when `α` is discrete. -/ @[simps] def equiv_fn_of_discrete [discrete_topology α] : C(α, β) ≃ (α → β) := ⟨(λ f, f), (λ f, ⟨f, continuous_of_discrete_topology⟩), λ f, by { ext, refl, }, λ f, by { ext, refl, }⟩ end /-- The identity as a continuous map. -/ def id : C(α, α) := ⟨id⟩ @[simp] lemma id_coe : (id : α → α) = _root_.id := rfl lemma id_apply (a : α) : id a = a := rfl /-- The composition of continuous maps, as a continuous map. -/ def comp (f : C(β, γ)) (g : C(α, β)) : C(α, γ) := ⟨f ∘ g⟩ @[simp] lemma comp_coe (f : C(β, γ)) (g : C(α, β)) : (comp f g : α → γ) = f ∘ g := rfl lemma comp_apply (f : C(β, γ)) (g : C(α, β)) (a : α) : comp f g a = f (g a) := rfl /-- Constant map as a continuous map -/ def const (b : β) : C(α, β) := ⟨λ x, b⟩ @[simp] lemma const_coe (b : β) : (const b : α → β) = (λ x, b) := rfl lemma const_apply (b : β) (a : α) : const b a = b := rfl instance [nonempty α] [nontrivial β] : nontrivial C(α, β) := { exists_pair_ne := begin obtain ⟨b₁, b₂, hb⟩ := exists_pair_ne β, refine ⟨const b₁, const b₂, _⟩, contrapose! hb, inhabit α, change const b₁ (default α) = const b₂ (default α), simp [hb] end } section variables [linear_ordered_add_comm_group β] [order_topology β] /-- The pointwise absolute value of a continuous function as a continuous function. -/ def abs (f : C(α, β)) : C(α, β) := { to_fun := λ x, abs (f x), } @[simp] lemma abs_apply (f : C(α, β)) (x : α) : f.abs x = _root_.abs (f x) := rfl end /-! We now set up the partial order and lattice structure (given by pointwise min and max) on continuous functions. -/ section lattice instance partial_order [partial_order β] : partial_order C(α, β) := partial_order.lift (λ f, f.to_fun) (by tidy) lemma le_def [partial_order β] {f g : C(α, β)} : f ≤ g ↔ ∀ a, f a ≤ g a := pi.le_def lemma lt_def [partial_order β] {f g : C(α, β)} : f < g ↔ (∀ a, f a ≤ g a) ∧ (∃ a, f a < g a) := pi.lt_def instance has_sup [linear_order β] [order_closed_topology β] : has_sup C(α, β) := { sup := λ f g, { to_fun := λ a, max (f a) (g a), } } @[simp, norm_cast] lemma sup_coe [linear_order β] [order_closed_topology β] (f g : C(α, β)) : ((f ⊔ g : C(α, β)) : α → β) = (f ⊔ g : α → β) := rfl @[simp] lemma sup_apply [linear_order β] [order_closed_topology β] (f g : C(α, β)) (a : α) : (f ⊔ g) a = max (f a) (g a) := rfl instance [linear_order β] [order_closed_topology β] : semilattice_sup C(α, β) := { le_sup_left := λ f g, le_def.mpr (by simp [le_refl]), le_sup_right := λ f g, le_def.mpr (by simp [le_refl]), sup_le := λ f₁ f₂ g w₁ w₂, le_def.mpr (λ a, by simp [le_def.mp w₁ a, le_def.mp w₂ a]), ..continuous_map.partial_order, ..continuous_map.has_sup, } instance has_inf [linear_order β] [order_closed_topology β] : has_inf C(α, β) := { inf := λ f g, { to_fun := λ a, min (f a) (g a), } } @[simp, norm_cast] lemma inf_coe [linear_order β] [order_closed_topology β] (f g : C(α, β)) : ((f ⊓ g : C(α, β)) : α → β) = (f ⊓ g : α → β) := rfl @[simp] lemma inf_apply [linear_order β] [order_closed_topology β] (f g : C(α, β)) (a : α) : (f ⊓ g) a = min (f a) (g a) := rfl instance [linear_order β] [order_closed_topology β] : semilattice_inf C(α, β) := { inf_le_left := λ f g, le_def.mpr (by simp [le_refl]), inf_le_right := λ f g, le_def.mpr (by simp [le_refl]), le_inf := λ f₁ f₂ g w₁ w₂, le_def.mpr (λ a, by simp [le_def.mp w₁ a, le_def.mp w₂ a]), ..continuous_map.partial_order, ..continuous_map.has_inf, } instance [linear_order β] [order_closed_topology β] : lattice C(α, β) := { ..continuous_map.semilattice_inf, ..continuous_map.semilattice_sup } -- TODO transfer this lattice structure to `bounded_continuous_function` section sup' variables [linear_order γ] [order_closed_topology γ] lemma sup'_apply {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) (b : β) : s.sup' H f b = s.sup' H (λ a, f a b) := finset.comp_sup'_eq_sup'_comp H (λ f : C(β, γ), f b) (λ i j, rfl) @[simp, norm_cast] lemma sup'_coe {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) : ((s.sup' H f : C(β, γ)) : ι → β) = s.sup' H (λ a, (f a : β → γ)) := by { ext, simp [sup'_apply], } end sup' section inf' variables [linear_order γ] [order_closed_topology γ] lemma inf'_apply {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) (b : β) : s.inf' H f b = s.inf' H (λ a, f a b) := @sup'_apply _ (order_dual γ) _ _ _ _ _ _ H f b @[simp, norm_cast] lemma inf'_coe {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) : ((s.inf' H f : C(β, γ)) : ι → β) = s.inf' H (λ a, (f a : β → γ)) := @sup'_coe _ (order_dual γ) _ _ _ _ _ _ H f end inf' end lattice section restrict variables (s : set α) /-- The restriction of a continuous function `α → β` to a subset `s` of `α`. -/ def restrict (f : C(α, β)) : C(s, β) := ⟨f ∘ coe⟩ @[simp] lemma coe_restrict (f : C(α, β)) : ⇑(f.restrict s) = f ∘ coe := rfl end restrict section extend variables [linear_order α] [order_topology α] {a b : α} (h : a ≤ b) /-- Extend a continuous function `f : C(set.Icc a b, β)` to a function `f : C(α, β)`. -/ def Icc_extend (f : C(set.Icc a b, β)) : C(α, β) := ⟨set.Icc_extend h f⟩ @[simp] lemma coe_Icc_extend (f : C(set.Icc a b, β)) : ((Icc_extend h f : C(α, β)) : α → β) = set.Icc_extend h f := rfl end extend end continuous_map /-- The forward direction of a homeomorphism, as a bundled continuous map. -/ @[simps] def homeomorph.to_continuous_map {α β : Type*} [topological_space α] [topological_space β] (e : α ≃ₜ β) : C(α, β) := ⟨e⟩
f116070e8e61afada53abdbdd36574835619ac41
b147e1312077cdcfea8e6756207b3fa538982e12
/data/equiv/encodable.lean
abd1f7ae4c2d39002af178f2bb6759981779de95
[ "Apache-2.0" ]
permissive
SzJS/mathlib
07836ee708ca27cd18347e1e11ce7dd5afb3e926
23a5591fca0d43ee5d49d89f6f0ee07a24a6ca29
refs/heads/master
1,584,980,332,064
1,532,063,841,000
1,532,063,841,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,174
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Mario Carneiro Type class for encodable Types. Note that every encodable Type is countable. -/ import data.equiv.nat open option list nat function /-- An encodable type is a "constructively countable" type. This is where we have an explicit injection `encode : α → nat` and a partial inverse `decode : nat → option α`. This makes the range of `encode` decidable, although it is not decidable if `α` is finite or not. -/ class encodable (α : Type*) := (encode : α → nat) (decode : nat → option α) (encodek : ∀ a, decode (encode a) = some a) namespace encodable variables {α : Type*} {β : Type*} universe u open encodable theorem encode_injective [encodable α] : function.injective (@encode α _) | x y e := option.some.inj $ by rw [← encodek, e, encodek] /- This is not set as an instance because this is usually not the best way to infer decidability. -/ def decidable_eq_of_encodable (α) [encodable α] : decidable_eq α | a b := decidable_of_iff _ encode_injective.eq_iff def of_left_injection [encodable α] (f : β → α) (finv : α → option β) (linv : ∀ b, finv (f b) = some b) : encodable β := ⟨λ b, encode (f b), λ n, (decode α n).bind finv, λ b, by simp [encodable.encodek, option.bind, linv]⟩ def of_left_inverse [encodable α] (f : β → α) (finv : α → β) (linv : ∀ b, finv (f b) = b) : encodable β := of_left_injection f (some ∘ finv) (λ b, congr_arg some (linv b)) def of_equiv (α) [encodable α] (e : β ≃ α) : encodable β := of_left_inverse e e.symm e.left_inv @[simp] theorem encode_of_equiv {α β} [encodable α] (e : β ≃ α) (b : β) : @encode _ (of_equiv _ e) b = encode (e b) := rfl @[simp] theorem decode_of_equiv {α β} [encodable α] (e : β ≃ α) (n : ℕ) : @decode _ (of_equiv _ e) n = (decode α n).map e.symm := rfl instance nat : encodable nat := ⟨id, some, λ a, rfl⟩ @[simp] theorem encode_nat (n : ℕ) : encode n = n := rfl @[simp] theorem decode_nat (n : ℕ) : decode ℕ n = some n := rfl instance empty : encodable empty := ⟨λ a, a.rec _, λ n, none, λ a, a.rec _⟩ instance unit : encodable punit := ⟨λ_, zero, λn, nat.cases_on n (some punit.star) (λ _, none), λ⟨⟩, by simp⟩ @[simp] theorem encode_star : encode punit.star = 0 := rfl @[simp] theorem decode_unit_zero : decode punit 0 = some punit.star := rfl @[simp] theorem decode_unit_succ (n) : decode punit (succ n) = none := rfl instance option {α : Type*} [h : encodable α] : encodable (option α) := ⟨λ o, option.cases_on o nat.zero (λ a, succ (encode a)), λ n, nat.cases_on n (some none) (λ m, (decode α m).map some), λ o, by cases o; dsimp; simp [encodek, nat.succ_ne_zero]⟩ @[simp] theorem encode_none [encodable α] : encode (@none α) = 0 := rfl @[simp] theorem encode_some [encodable α] (a : α) : encode (some a) = succ (encode a) := rfl @[simp] theorem decode_option_zero [encodable α] : decode (option α) 0 = some none := rfl @[simp] theorem decode_option_succ [encodable α] (n) : decode (option α) (succ n) = (decode α n).map some := rfl def decode2 (α) [encodable α] (n : ℕ) : option α := (decode α n).bind (option.guard (λ a, encode a = n)) theorem mem_decode2' [encodable α] {n : ℕ} {a : α} : a ∈ decode2 α n ↔ a ∈ decode α n ∧ encode a = n := by simp [decode2]; exact ⟨λ ⟨_, h₁, rfl, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨_, h₁, rfl, h₂⟩⟩ theorem mem_decode2 [encodable α] {n : ℕ} {a : α} : a ∈ decode2 α n ↔ encode a = n := mem_decode2'.trans (and_iff_right_of_imp $ λ e, e ▸ encodek _) theorem decode2_is_partial_inv [encodable α] : is_partial_inv encode (decode2 α) := λ a n, mem_decode2 theorem decode2_inj [encodable α] {n : ℕ} {a₁ a₂ : α} (h₁ : a₁ ∈ decode2 α n) (h₂ : a₂ ∈ decode2 α n) : a₁ = a₂ := encode_injective $ (mem_decode2.1 h₁).trans (mem_decode2.1 h₂).symm theorem encodek2 [encodable α] (a : α) : decode2 α (encode a) = some a := mem_decode2.2 rfl section sum variables [encodable α] [encodable β] def encode_sum : α ⊕ β → nat | (sum.inl a) := bit0 $ encode a | (sum.inr b) := bit1 $ encode b def decode_sum (n : nat) : option (α ⊕ β) := match bodd_div2 n with | (ff, m) := (decode α m).map sum.inl | (tt, m) := (decode β m).map sum.inr end instance sum : encodable (α ⊕ β) := ⟨encode_sum, decode_sum, λ s, by cases s; simp [encode_sum, decode_sum, encodek]; refl⟩ @[simp] theorem encode_inl (a : α) : @encode (α ⊕ β) _ (sum.inl a) = bit0 (encode a) := rfl @[simp] theorem encode_inr (b : β) : @encode (α ⊕ β) _ (sum.inr b) = bit1 (encode b) := rfl @[simp] theorem decode_sum_val (n : ℕ) : decode (α ⊕ β) n = decode_sum n := rfl end sum instance bool : encodable bool := of_equiv (unit ⊕ unit) equiv.bool_equiv_unit_sum_unit @[simp] theorem encode_tt : encode tt = 1 := rfl @[simp] theorem encode_ff : encode ff = 0 := rfl @[simp] theorem decode_zero : decode bool 0 = some ff := rfl @[simp] theorem decode_one : decode bool 1 = some tt := rfl theorem decode_ge_two (n) (h : 2 ≤ n) : decode bool n = none := begin suffices : decode_sum n = none, { change (decode_sum n).map _ = none, rw this, refl }, have : 1 ≤ div2 n, { rw [div2_val, nat.le_div_iff_mul_le], exacts [h, dec_trivial] }, cases exists_eq_succ_of_ne_zero (ne_of_gt this) with m e, simp [decode_sum]; cases bodd n; simp [decode_sum]; rw e; refl end section sigma variables {γ : α → Type*} [encodable α] [∀ a, encodable (γ a)] def encode_sigma : sigma γ → ℕ | ⟨a, b⟩ := mkpair (encode a) (encode b) def decode_sigma (n : ℕ) : option (sigma γ) := let (n₁, n₂) := unpair n in (decode α n₁).bind $ λ a, (decode (γ a) n₂).map $ sigma.mk a instance sigma : encodable (sigma γ) := ⟨encode_sigma, decode_sigma, λ ⟨a, b⟩, by simp [encode_sigma, decode_sigma, option.bind, option.map, unpair_mkpair, encodek]⟩ @[simp] theorem decode_sigma_val (n : ℕ) : decode (sigma γ) n = (decode α n.unpair.1).bind (λ a, (decode (γ a) n.unpair.2).map $ sigma.mk a) := show decode_sigma._match_1 _ = _, by cases n.unpair; refl @[simp] theorem encode_sigma_val (a b) : @encode (sigma γ) _ ⟨a, b⟩ = mkpair (encode a) (encode b) := rfl end sigma section prod variables [encodable α] [encodable β] instance prod : encodable (α × β) := of_equiv _ (equiv.sigma_equiv_prod α β).symm @[simp] theorem decode_prod_val (n : ℕ) : decode (α × β) n = (decode α n.unpair.1).bind (λ a, (decode β n.unpair.2).map $ prod.mk a) := show (decode (sigma (λ _, β)) n).map (equiv.sigma_equiv_prod α β) = _, by simp; cases decode α n.unpair.1; simp [option.bind]; cases decode β n.unpair.2; refl @[simp] theorem encode_prod_val (a b) : @encode (α × β) _ (a, b) = mkpair (encode a) (encode b) := rfl end prod section subtype open subtype decidable variable {P : α → Prop} variable [encA : encodable α] variable [decP : decidable_pred P] include encA def encode_subtype : {a : α // P a} → nat | ⟨v, h⟩ := encode v include decP def decode_subtype (v : nat) : option {a : α // P a} := (decode α v).bind $ λ a, if h : P a then some ⟨a, h⟩ else none instance subtype : encodable {a : α // P a} := ⟨encode_subtype, decode_subtype, λ ⟨v, h⟩, by simp [encode_subtype, decode_subtype, encodek, h]⟩ end subtype instance fin (n) : encodable (fin n) := of_equiv _ (equiv.fin_equiv_subtype _) instance int : encodable ℤ := of_equiv _ equiv.int_equiv_nat instance ulift [encodable α] : encodable (ulift α) := of_equiv _ equiv.ulift instance plift [encodable α] : encodable (plift α) := of_equiv _ equiv.plift noncomputable def of_inj [encodable β] (f : α → β) (hf : injective f) : encodable α := of_left_injection f (partial_inv f) (λ x, (partial_inv_of_injective hf _ _).2 rfl) end encodable /- Choice function for encodable types and decidable predicates. We provide the following API choose {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] : (∃ x, p x) → α := choose_spec {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] (ex : ∃ x, p x) : p (choose ex) := -/ namespace encodable section find_a variables {α : Type*} (p : α → Prop) [encodable α] [decidable_pred p] private def good : option α → Prop | (some a) := p a | none := false private def decidable_good : decidable_pred (good p) | n := by cases n; unfold good; apply_instance local attribute [instance] decidable_good open encodable variable {p} def choose_x (h : ∃ x, p x) : {a:α // p a} := have ∃ n, good p (decode α n), from let ⟨w, pw⟩ := h in ⟨encode w, by simp [good, encodek, pw]⟩, match _, nat.find_spec this : ∀ o, good p o → {a // p a} with | some a, h := ⟨a, h⟩ end def choose (h : ∃ x, p x) : α := (choose_x h).1 lemma choose_spec (h : ∃ x, p x) : p (choose h) := (choose_x h).2 end find_a theorem axiom_of_choice {α : Type*} {β : α → Type*} {R : Π x, β x → Prop} [Π a, encodable (β a)] [∀ x y, decidable (R x y)] (H : ∀x, ∃y, R x y) : ∃f:Πa, β a, ∀x, R x (f x) := ⟨λ x, choose (H x), λ x, choose_spec (H x)⟩ theorem skolem {α : Type*} {β : α → Type*} {P : Π x, β x → Prop} [c : Π a, encodable (β a)] [d : ∀ x y, decidable (P x y)] : (∀x, ∃y, P x y) ↔ ∃f : Π a, β a, (∀x, P x (f x)) := ⟨axiom_of_choice, λ ⟨f, H⟩ x, ⟨_, H x⟩⟩ end encodable namespace quot open encodable variables {α : Type*} {s : setoid α} [@decidable_rel α (≈)] [encodable α] -- Choose equivalence class representative def rep (q : quotient s) : α := choose (exists_rep q) theorem rep_spec (q : quotient s) : ⟦rep q⟧ = q := choose_spec (exists_rep q) def encodable_quotient : encodable (quotient s) := ⟨λ q, encode (rep q), λ n, quotient.mk <$> decode α n, λ q, quot.induction_on q $ λ l, by rw encodek; exact congr_arg some (rep_spec _)⟩ end quot
9893b57816c422a3ac5b8338fb0b609cce359ae8
6214e13b31733dc9aeb4833db6a6466005763162
/src/definitions1.lean
e1079d0874157b773c4199eaedc1f5aaa6e4e3fd
[]
no_license
joshua0pang/esverify-theory
272a250445f3aeea49a7e72d1ab58c2da6618bbe
8565b123c87b0113f83553d7732cd6696c9b5807
refs/heads/master
1,585,873,849,081
1,527,304,393,000
1,527,304,393,000
154,901,199
1
0
null
1,540,593,067,000
1,540,593,067,000
null
UTF-8
Lean
false
false
21,505
lean
-- first part of definitions import .syntax .sizeof .eqdec -- ####################################### -- ### MINOR DEFINITIONS AND NOTATIONS ### -- ####################################### -- P → Q @[reducible] def spec.implies(P Q: spec): spec := spec.or (spec.not P) Q @[reducible] def prop.implies(P Q: prop): prop := prop.or (prop.not P) Q @[reducible] def propctx.implies(P Q: propctx): propctx := propctx.or (propctx.not P) Q @[reducible] def vc.implies(P Q: vc): vc := vc.or (vc.not P) Q -- P ↔ Q @[reducible] def spec.iff(P Q: spec): spec := spec.and (spec.implies P Q) (spec.implies Q P) @[reducible] def prop.iff(P Q: prop): prop := prop.and (prop.implies P Q) (prop.implies Q P) @[reducible] def propctx.iff(P Q: propctx): propctx := propctx.and (propctx.implies P Q) (propctx.implies Q P) @[reducible] def vc.iff(P Q: vc): vc := vc.and (vc.implies P Q) (vc.implies Q P) -- P ⋀ Q class has_and (α : Type) := (and : α → α → α) instance : has_and spec := ⟨spec.and⟩ instance : has_and prop := ⟨prop.and⟩ instance : has_and propctx := ⟨propctx.and⟩ instance : has_and vc := ⟨vc.and⟩ infixr `⋀`:35 := has_and.and -- P ⋁ Q class has_or (α : Type) := (or : α → α → α) instance : has_or spec := ⟨spec.or⟩ instance : has_or prop := ⟨prop.or⟩ instance : has_or propctx := ⟨propctx.or⟩ instance : has_or vc := ⟨vc.or⟩ infixr `⋁`:30 := has_or.or -- use • as hole notation `•` := termctx.hole -- simple coercions instance value_to_term : has_coe value term := ⟨term.value⟩ instance var_to_term : has_coe var term := ⟨term.var⟩ instance term_to_prop : has_coe term prop := ⟨prop.term⟩ instance termctx_to_propctx : has_coe termctx propctx := ⟨propctx.term⟩ instance term_to_vc : has_coe term vc := ⟨vc.term⟩ -- use (t ≡ t)/(t ≣ t) to construct equality comparison infix ≡ := term.binop binop.eq infix `≣`:50 := termctx.binop binop.eq -- syntax for let expressions notation `lett` x `=`:1 `true` `in` e := exp.true x e notation `letf` x `=`:1 `false` `in` e := exp.false x e notation `letn` x `=`:1 n`in` e := exp.num x n e notation `letf` f `[`:1 x `]` `req` R `ens` S `{`:1 e `}`:1 `in` e' := exp.func f x R S e e' notation `letop` y `=`:1 op `[`:1 x `]`:1 `in` e := exp.unop y op x e notation `letop2` z `=`:1 op `[`:1 x `,` y `]`:1 `in` e := exp.binop z op x y e notation `letapp` y `=`:1 f `[`:1 x `]`:1 `in` e := exp.app y f x e -- σ[x ↦ v] notation e `[` x `↦` v `]` := env.cons e x v -- (σ, e) : stack instance : has_coe (env × exp) stack := ⟨λe, stack.top e.1 e.2⟩ -- κ · [σ, let y = f [ x ] in e] notation st `·` `[` env `,` `letapp` y `=`:1 f `[` x `]` `in` e `]` := stack.cons st env y f x e -- env lookup as function application def env.apply: env → var → option value | env.empty _ := none | (σ[x↦v]) y := have σ.sizeof < (σ[x↦v]).sizeof, from sizeof_env_rest, if x = y ∧ option.is_none (σ.apply y) then v else σ.apply y using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ e, e.1.sizeof)⟩], dec_tac := tactic.assumption } instance : has_coe_to_fun env := ⟨λ _, var → option value, env.apply⟩ def env.rest: env → env | env.empty := env.empty | (σ[x↦v]) := σ -- x ∈ σ inductive env.contains: env → var → Prop | same {e: env} {x: var} {v: value} : env.contains (e[x↦v]) x | rest {e: env} {x y: var} {v: value} : env.contains e x → env.contains (e[y↦v]) x instance : has_mem var env := ⟨λx σ, σ.contains x⟩ -- dom(σ) def env.dom (σ: env): set var := λx, x ∈ σ -- spec to prop coercion @[reducible] def prop.func (f: term) (x: var) (P: prop) (Q: prop): prop := term.unop unop.isFunc f ⋀ prop.forallc x (prop.implies P (prop.pre f x) ⋀ prop.implies (prop.post f x) Q) def spec.to_prop : spec → prop | (spec.term t) := prop.term t | (spec.not S) := have S.sizeof < S.not.sizeof, from sizeof_spec_not, prop.not S.to_prop | (spec.and R S) := have R.sizeof < (R ⋀ S).sizeof, from sizeof_spec_and₁, have S.sizeof < (R ⋀ S).sizeof, from sizeof_spec_and₂, R.to_prop ⋀ S.to_prop | (spec.or R S) := have R.sizeof < (R ⋁ S).sizeof, from sizeof_spec_or₁, have S.sizeof < (R ⋁ S).sizeof, from sizeof_spec_or₂, R.to_prop ⋁ S.to_prop | (spec.func f x R S) := have R.sizeof < (spec.func f x R S).sizeof, from sizeof_spec_func_R, have S.sizeof < (spec.func f x R S).sizeof, from sizeof_spec_func_S, prop.func f x R.to_prop S.to_prop using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ e, e.sizeof)⟩], dec_tac := tactic.assumption } instance spec_to_prop : has_coe spec prop := ⟨spec.to_prop⟩ -- term to termctx coercion def term.to_termctx : term → termctx | (term.value v) := termctx.value v | (term.var x) := termctx.var x | (term.unop op t) := termctx.unop op t.to_termctx | (term.binop op t₁ t₂) := termctx.binop op t₁.to_termctx t₂.to_termctx | (term.app t₁ t₂) := termctx.app t₁.to_termctx t₂.to_termctx instance term_to_termctx : has_coe term termctx := ⟨term.to_termctx⟩ -- term to termctx coercion def prop.to_propctx : prop → propctx | (prop.term t) := propctx.term t | (prop.not P) := propctx.not P.to_propctx | (prop.and P₁ P₂) := P₁.to_propctx ⋀ P₂.to_propctx | (prop.or P₁ P₂) := P₁.to_propctx ⋁ P₂.to_propctx | (prop.pre t₁ t₂) := propctx.pre t₁ t₂ | (prop.pre₁ op t) := propctx.pre₁ op t | (prop.pre₂ op t₁ t₂) := propctx.pre₂ op t₁ t₂ | (prop.post t₁ t₂) := propctx.post t₁ t₂ | (prop.call t) := propctx.call t | (prop.forallc x P) := propctx.forallc x P.to_propctx | (prop.exis x P) := propctx.exis x P.to_propctx instance prop_to_propctx : has_coe prop propctx := ⟨prop.to_propctx⟩ -- termctx substituttion as function application def termctx.apply: termctx → term → term | • t := t | (termctx.value v) _ := term.value v | (termctx.var x) _ := term.var x | (termctx.unop op t₁) t := term.unop op (t₁.apply t) | (termctx.binop op t₁ t₂) t := term.binop op (t₁.apply t) (t₂.apply t) | (termctx.app t₁ t₂) t := term.app (t₁.apply t) (t₂.apply t) instance : has_coe_to_fun termctx := ⟨λ _, term → term, termctx.apply⟩ -- propctx substituttion as function application def propctx.apply: propctx → term → prop | (propctx.term t₁) t := t₁ t | (propctx.not P) t := prop.not (P.apply t) | (propctx.and P₁ P₂) t := P₁.apply t ⋀ P₂.apply t | (propctx.or P₁ P₂) t := P₁.apply t ⋁ P₂.apply t | (propctx.pre t₁ t₂) t := prop.pre (t₁ t) (t₂ t) | (propctx.pre₁ op t₁) t := prop.pre₁ op (t₁ t) | (propctx.pre₂ op t₁ t₂) t := prop.pre₂ op (t₁ t) (t₂ t) | (propctx.post t₁ t₂) t := prop.post (t₁ t) (t₂ t) | (propctx.call t₁) t := prop.call (t₁ t) | (propctx.forallc x P) t := prop.forallc x (P.apply t) | (propctx.exis x P) t := prop.exis x (P.apply t) instance : has_coe_to_fun propctx := ⟨λ _, term → prop, propctx.apply⟩ -- ############################# -- ### VARIABLE SUBSTITUTION ### -- ############################# -- free variables in terms, propositions and vcs inductive free_in_term (x: var) : term → Prop | var : free_in_term x | unop {t: term} {op: unop} : free_in_term t → free_in_term (term.unop op t) | binop₁ {t₁ t₂: term} {op: binop} : free_in_term t₁ → free_in_term (term.binop op t₁ t₂) | binop₂ {t₁ t₂: term} {op: binop} : free_in_term t₂ → free_in_term (term.binop op t₁ t₂) | app₁ {t₁ t₂: term} : free_in_term t₁ → free_in_term (term.app t₁ t₂) | app₂ {t₁ t₂: term} : free_in_term t₂ → free_in_term (term.app t₁ t₂) inductive free_in_prop (x: var) : prop → Prop | term {t: term} : free_in_term x t → free_in_prop t | not {p: prop} : free_in_prop p → free_in_prop (prop.not p) | and₁ {p₁ p₂: prop} : free_in_prop p₁ → free_in_prop (prop.and p₁ p₂) | and₂ {p₁ p₂: prop} : free_in_prop p₂ → free_in_prop (prop.and p₁ p₂) | or₁ {p₁ p₂: prop} : free_in_prop p₁ → free_in_prop (prop.or p₁ p₂) | or₂ {p₁ p₂: prop} : free_in_prop p₂ → free_in_prop (prop.or p₁ p₂) | pre₁ {t₁ t₂: term} : free_in_term x t₁ → free_in_prop (prop.pre t₁ t₂) | pre₂ {t₁ t₂: term} : free_in_term x t₂ → free_in_prop (prop.pre t₁ t₂) | preop {t: term} {op: unop} : free_in_term x t → free_in_prop (prop.pre₁ op t) | preop₁ {t₁ t₂: term} {op: binop} : free_in_term x t₁ → free_in_prop (prop.pre₂ op t₁ t₂) | preop₂ {t₁ t₂: term} {op: binop} : free_in_term x t₂ → free_in_prop (prop.pre₂ op t₁ t₂) | post₁ {t₁ t₂: term} : free_in_term x t₁ → free_in_prop (prop.post t₁ t₂) | post₂ {t₁ t₂: term} : free_in_term x t₂ → free_in_prop (prop.post t₁ t₂) | call {t: term} : free_in_term x t → free_in_prop (prop.call t) | forallc {y: var} {p: prop} : (x ≠ y) → free_in_prop p → free_in_prop (prop.forallc y p) | exis {y: var} {p: prop} : (x ≠ y) → free_in_prop p → free_in_prop (prop.exis y p) inductive free_in_vc (x: var) : vc → Prop | term {t: term} : free_in_term x t → free_in_vc t | not {P: vc} : free_in_vc P → free_in_vc (vc.not P) | and₁ {P₁ P₂: vc} : free_in_vc P₁ → free_in_vc (vc.and P₁ P₂) | and₂ {P₁ P₂: vc} : free_in_vc P₂ → free_in_vc (vc.and P₁ P₂) | or₁ {P₁ P₂: vc} : free_in_vc P₁ → free_in_vc (vc.or P₁ P₂) | or₂ {P₁ P₂: vc} : free_in_vc P₂ → free_in_vc (vc.or P₁ P₂) | pre₁ {t₁ t₂: term} : free_in_term x t₁ → free_in_vc (vc.pre t₁ t₂) | pre₂ {t₁ t₂: term} : free_in_term x t₂ → free_in_vc (vc.pre t₁ t₂) | preop {t: term} {op: unop} : free_in_term x t → free_in_vc (vc.pre₁ op t) | preop₁ {t₁ t₂: term} {op: binop} : free_in_term x t₁ → free_in_vc (vc.pre₂ op t₁ t₂) | preop₂ {t₁ t₂: term} {op: binop} : free_in_term x t₂ → free_in_vc (vc.pre₂ op t₁ t₂) | post₁ {t₁ t₂: term} : free_in_term x t₁ → free_in_vc (vc.post t₁ t₂) | post₂ {t₁ t₂: term} : free_in_term x t₂ → free_in_vc (vc.post t₁ t₂) | univ {y: var} {P: vc} : (x ≠ y) → free_in_vc P → free_in_vc (vc.univ y P) -- notation x ∈ FV t/P inductive freevars | term: term → freevars | prop: prop → freevars | vc: vc → freevars class has_fv (α: Type) := (fv : α → freevars) instance : has_fv term := ⟨freevars.term⟩ instance : has_fv prop := ⟨freevars.prop⟩ instance : has_fv vc := ⟨freevars.vc⟩ def freevars.to_set: freevars → set var | (freevars.term t) := λx, free_in_term x t | (freevars.prop P) := λx, free_in_prop x P | (freevars.vc P) := λx, free_in_vc x P @[reducible] def FV {α: Type} [h: has_fv α] (a: α): set var := (has_fv.fv a).to_set @[reducible] def closed {α: Type} [h: has_fv α] (a: α): Prop := ∀x, x ∉ FV a -- fresh variables (not used in the provided term/prop) def term.fresh_var : term → var | (term.value v) := 0 | (term.var x) := x + 1 | (term.unop op t) := t.fresh_var | (term.binop op t₁ t₂) := max t₁.fresh_var t₂.fresh_var | (term.app t₁ t₂) := max t₁.fresh_var t₂.fresh_var def prop.fresh_var : prop → var | (prop.term t) := t.fresh_var | (prop.not P) := P.fresh_var | (prop.and P₁ P₂) := max P₁.fresh_var P₂.fresh_var | (prop.or P₁ P₂) := max P₁.fresh_var P₂.fresh_var | (prop.pre t₁ t₂) := max t₁.fresh_var t₂.fresh_var | (prop.pre₁ op t) := t.fresh_var | (prop.pre₂ op t₁ t₂) := max t₁.fresh_var t₂.fresh_var | (prop.post t₁ t₂) := max t₁.fresh_var t₂.fresh_var | (prop.call t) := t.fresh_var | (prop.forallc x P) := max (x + 1) P.fresh_var | (prop.exis x P) := max (x + 1) P.fresh_var -- substituation in terms, propositions and vcs def term.subst (x: var) (v: value): term → term | (term.value v') := v' | (term.var y) := if x = y then v else y | (term.unop op t) := term.unop op t.subst | (term.binop op t₁ t₂) := term.binop op t₁.subst t₂.subst | (term.app t₁ t₂) := term.app t₁.subst t₂.subst def term.subst_env: env → term → term | env.empty t := t | (σ[x↦v]) t := have σ.sizeof < (σ[x ↦ v]).sizeof, from sizeof_env_rest, term.subst x v (term.subst_env σ t) using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ e, e.1.sizeof)⟩], dec_tac := tactic.assumption } def prop.subst (x: var) (v: value): prop → prop | (prop.term t) := term.subst x v t | (prop.not P) := P.subst.not | (prop.and P Q) := P.subst ⋀ Q.subst | (prop.or P Q) := P.subst ⋁ Q.subst | (prop.pre t₁ t₂) := prop.pre (term.subst x v t₁) (term.subst x v t₂) | (prop.pre₁ op t) := prop.pre₁ op (term.subst x v t) | (prop.pre₂ op t₁ t₂) := prop.pre₂ op (term.subst x v t₁) (term.subst x v t₂) | (prop.call t) := prop.call (term.subst x v t) | (prop.post t₁ t₂) := prop.post (term.subst x v t₁) (term.subst x v t₂) | (prop.forallc y P) := prop.forallc y (if x = y then P else P.subst) | (prop.exis y P) := prop.exis y (if x = y then P else P.subst) def prop.subst_env: env → prop → prop | env.empty P := P | (σ[x↦v]) P := have σ.sizeof < (σ[x ↦ v]).sizeof, from sizeof_env_rest, prop.subst x v (prop.subst_env σ P) using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ e, e.1.sizeof)⟩], dec_tac := tactic.assumption } def vc.subst (x: var) (v: value): vc → vc | (vc.term t) := term.subst x v t | (vc.not P) := vc.not P.subst | (vc.and P Q) := P.subst ⋀ Q.subst | (vc.or P Q) := P.subst ⋁ Q.subst | (vc.pre t₁ t₂) := vc.pre (term.subst x v t₁) (term.subst x v t₂) | (vc.pre₁ op t) := vc.pre₁ op (term.subst x v t) | (vc.pre₂ op t₁ t₂) := vc.pre₂ op (term.subst x v t₁) (term.subst x v t₂) | (vc.post t₁ t₂) := vc.post (term.subst x v t₁) (term.subst x v t₂) | (vc.univ y P) := vc.univ y (if x = y then P else P.subst) def vc.subst_env: env → vc → vc | env.empty P := P | (σ[x↦v]) P := have σ.sizeof < (σ[x ↦ v]).sizeof, from sizeof_env_rest, vc.subst x v (vc.subst_env σ P) using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ e, e.1.sizeof)⟩], dec_tac := tactic.assumption } -- σ ∖ { x } def env.without: env → var → env | env.empty y := env.empty | (σ[x↦v]) y := have σ.sizeof < (σ[x ↦ v]).sizeof, from sizeof_env_rest, if x = y then env.without σ y else ((env.without σ y)[x↦v]) using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ e, e.1.sizeof)⟩], dec_tac := tactic.assumption } -- closed under substitution @[reducible] def closed_subst {α: Type} [h: has_fv α] (σ: env) (a: α): Prop := FV a ⊆ σ.dom -- subst term def term.substt (x: var) (t: term): term → term | (term.value v') := v' | (term.var y) := if x = y then t else y | (term.unop op t₁) := term.unop op t₁.substt | (term.binop op t₁ t₂) := term.binop op t₁.substt t₂.substt | (term.app t₁ t₂) := term.app t₁.substt t₂.substt def prop.substt (x: var) (t: term): prop → prop | (prop.term t₁) := term.substt x t t₁ | (prop.not P) := P.substt.not | (prop.and P Q) := P.substt ⋀ Q.substt | (prop.or P Q) := P.substt ⋁ Q.substt | (prop.pre t₁ t₂) := prop.pre (term.substt x t t₁) (term.substt x t t₂) | (prop.pre₁ op t₁) := prop.pre₁ op (term.substt x t t₁) | (prop.pre₂ op t₁ t₂) := prop.pre₂ op (term.substt x t t₁) (term.substt x t t₂) | (prop.call t₁) := prop.call (term.substt x t t₁) | (prop.post t₁ t₂) := prop.post (term.substt x t t₁) (term.substt x t t₂) | (prop.forallc y P) := prop.forallc y (if x = y then P else P.substt) | (prop.exis y P) := prop.exis y (if x = y then P else P.substt) def vc.substt (x: var) (t: term): vc → vc | (vc.term t₁) := term.substt x t t₁ | (vc.not P) := P.substt.not | (vc.and P Q) := P.substt ⋀ Q.substt | (vc.or P Q) := P.substt ⋁ Q.substt | (vc.pre t₁ t₂) := vc.pre (term.substt x t t₁) (term.substt x t t₂) | (vc.pre₁ op t₁) := vc.pre₁ op (term.substt x t t₁) | (vc.pre₂ op t₁ t₂) := vc.pre₂ op (term.substt x t t₁) (term.substt x t t₂) | (vc.post t₁ t₂) := vc.post (term.substt x t t₁) (term.substt x t t₂) | (vc.univ y P) := vc.univ y (if x = y then P else P.substt) -- ################################ -- ### QUANTIFIER INSTANTIATION ### -- ################################ -- simple conversion of propositions to verification conditions -- (no quantifier instantiation) def prop.to_vc: prop → vc | (prop.term t) := vc.term t | (prop.not P) := vc.not P.to_vc | (prop.and P₁ P₂) := P₁.to_vc ⋀ P₂.to_vc | (prop.or P₁ P₂) := P₁.to_vc ⋁ P₂.to_vc | (prop.pre t₁ t₂) := vc.pre t₁ t₂ | (prop.pre₁ op t) := vc.pre₁ op t | (prop.pre₂ op t₁ t₂) := vc.pre₂ op t₁ t₂ | (prop.post t₁ t₂) := vc.post t₁ t₂ | (prop.call _) := vc.term value.true | (prop.forallc x P) := vc.univ x P.to_vc | (prop.exis x P) := have P.sizeof < (prop.exis x P).sizeof, from sizeof_prop_exis, vc.not (vc.univ x (vc.not P.to_vc)) -- lift_p(P) / lift_n(P) lifts quantifiers in either positive or negative position -- to become a top-level quantifier by using a fresh (unbound) variable mutual def prop.lift_p, prop.lift_n with prop.lift_p: prop → var → option prop | (prop.term t) y := none | (prop.not P) y := have P.sizeof < P.not.sizeof, from sizeof_prop_not, prop.not <$> P.lift_n y | (prop.and P₁ P₂) y := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁, have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂, match P₁.lift_p y with | some P₁' := some (P₁' ⋀ P₂) | none := (λP₂', P₁ ⋀ P₂') <$> P₂.lift_p y end | (prop.or P₁ P₂) y := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁, have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂, match P₁.lift_p y with | some P₁' := some (P₁' ⋁ P₂) | none := (λP₂', P₁ ⋁ P₂') <$> P₂.lift_p y end | (prop.pre t₁ t₂) y := none | (prop.pre₁ op t) y := none | (prop.pre₂ op t₁ t₂) y := none | (prop.post t₁ t₂) y := none | (prop.call t) y := none | (prop.forallc x P) y := some (prop.implies (prop.call y) (P.substt x y)) | (prop.exis x P) y := none with prop.lift_n: prop → var → option prop | (prop.term t) y := none | (prop.not P) y := have P.sizeof < P.not.sizeof, from sizeof_prop_not, prop.not <$> P.lift_p y | (prop.and P₁ P₂) y := have P₁.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₁, have P₂.sizeof < (P₁ ⋀ P₂).sizeof, from sizeof_prop_and₂, match P₁.lift_n y with | some P₁' := some (P₁' ⋀ P₂) | none := (λP₂', P₁ ⋀ P₂') <$> P₂.lift_n y end | (prop.or P₁ P₂) y := have P₁.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₁, have P₂.sizeof < (P₁ ⋁ P₂).sizeof, from sizeof_prop_or₂, match P₁.lift_n y with | some P₁' := some (P₁' ⋁ P₂) | none := (λP₂', P₁ ⋁ P₂') <$> P₂.lift_n y end | (prop.pre t₁ t₂) y := none | (prop.pre₁ op t) y := none | (prop.pre₂ op t₁ t₂) y := none | (prop.post t₁ t₂) y := none | (prop.call t) y := none | (prop.forallc x P) y := none | (prop.exis x P) y := none using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf $ λ s, match s with | psum.inl a := a.1.sizeof | psum.inr a := a.1.sizeof end⟩], dec_tac := tactic.assumption } -- remaining definitions need some additional lemmas in qiaux.lean to prove termination -- definitions continue in definitions2.lean
ae63652c0b705b10630375a180ef0ebeaf3194ca
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/multiplicity.lean
5823e572d478f27228c32e4c615da47d6b8f3315
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
17,519
lean
/- Copyright (c) 2022 Tian Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tian Chen, Mantas Bakšys -/ import algebra.geom_sum import data.int.parity import data.zmod.basic import number_theory.padics.padic_val import ring_theory.ideal.quotient_operations /-! # Multiplicity in Number Theory This file contains results in number theory relating to multiplicity. ## Main statements * `multiplicity.int.pow_sub_pow` is the lifting the exponent lemma for odd primes. We also prove several variations of the lemma. ## References * [Wikipedia, *Lifting-the-exponent lemma*] (https://en.wikipedia.org/wiki/Lifting-the-exponent_lemma) -/ open ideal ideal.quotient finset open_locale big_operators variables {R : Type*} {n : ℕ} section comm_ring variables [comm_ring R] {a b x y : R} lemma dvd_geom_sum₂_iff_of_dvd_sub {x y p : R} (h : p ∣ x - y) : p ∣ ∑ i in range n, x ^ i * y ^ (n - 1 - i) ↔ p ∣ n * y ^ (n - 1) := begin rw [← mem_span_singleton, ← ideal.quotient.eq] at h, simp only [← mem_span_singleton, ← eq_zero_iff_mem, ring_hom.map_geom_sum₂, h, geom_sum₂_self, _root_.map_mul, map_pow, map_nat_cast] end lemma dvd_geom_sum₂_iff_of_dvd_sub' {x y p : R} (h : p ∣ x - y) : p ∣ ∑ i in range n, x ^ i * y ^ (n - 1 - i) ↔ p ∣ n * x ^ (n - 1) := by rw [geom_sum₂_comm, dvd_geom_sum₂_iff_of_dvd_sub]; simpa using h.neg_right lemma dvd_geom_sum₂_self {x y : R} (h : ↑n ∣ x - y) : ↑n ∣ ∑ i in range n, x ^ i * y ^ (n - 1 - i):= (dvd_geom_sum₂_iff_of_dvd_sub h).mpr (dvd_mul_right _ _) lemma sq_dvd_add_pow_sub_sub (p x : R) (n : ℕ) : p ^ 2 ∣ (x + p) ^ n - x ^ (n - 1) * p * n - x ^ n := begin cases n, { simp only [pow_zero, nat.cast_zero, mul_zero, sub_zero, sub_self, dvd_zero]}, { simp only [nat.succ_sub_succ_eq_sub, tsub_zero, nat.cast_succ, add_pow, finset.sum_range_succ, nat.choose_self, nat.succ_sub _, tsub_self, pow_one, nat.choose_succ_self_right, pow_zero, mul_one, nat.cast_zero, zero_add, nat.succ_eq_add_one], suffices : p ^ 2 ∣ ∑ (i : ℕ) in range n, x ^ i * p ^ (n + 1 - i) * ↑((n + 1).choose i), { convert this; abel }, { apply finset.dvd_sum, intros y hy, calc p ^ 2 ∣ p ^ (n + 1 - y) : pow_dvd_pow p (le_tsub_of_add_le_left (by linarith [finset.mem_range.mp hy])) ... ∣ x ^ y * p ^ (n + 1 - y) * ↑((n + 1).choose y) : dvd_mul_of_dvd_left (dvd_mul_left _ _) ((n + 1).choose y) }} end lemma not_dvd_geom_sum₂ {p : R} (hp : prime p) (hxy : p ∣ x - y) (hx : ¬p ∣ x) (hn : ¬p ∣ n) : ¬p ∣ ∑ i in range n, x ^ i * y ^ (n - 1 - i) := λ h, hx $ hp.dvd_of_dvd_pow $ (hp.dvd_or_dvd $ (dvd_geom_sum₂_iff_of_dvd_sub' hxy).mp h).resolve_left hn variables {p : ℕ} (a b) lemma odd_sq_dvd_geom_sum₂_sub (hp : odd p) : ↑p ^ 2 ∣ ∑ i in range p, (a + p * b) ^ i * a ^ (p - 1 - i) - p * a ^ (p - 1) := begin have h1 : ∀ i, ↑p ^ 2 ∣ (a + ↑p * b) ^ i - (a ^ (i - 1) * (↑p * b) * ↑i + a ^ i), { intro i, calc ↑p ^ 2 ∣ (↑p * b) ^ 2 : by simp only [mul_pow, dvd_mul_right] ... ∣ (a + ↑p * b) ^ i - (a ^ (i - 1) * (↑p * b) * ↑i + a ^ i) : by simp only [sq_dvd_add_pow_sub_sub (↑p * b) a i, ← sub_sub] }, simp_rw [← mem_span_singleton, ← ideal.quotient.eq] at *, calc ideal.quotient.mk (span {↑p ^ 2}) (∑ i in range p, (a + ↑p * b) ^ i * a ^ (p - 1 - i)) = ∑ (i : ℕ) in finset.range p, mk (span {↑p ^ 2}) ((a ^ (i - 1) * (↑p * b) * ↑i + a ^ i) * a ^ (p - 1 - i)) : by simp_rw [ring_hom.map_geom_sum₂, ← map_pow, h1, ← _root_.map_mul] ... = mk (span {↑p ^ 2}) (∑ (x : ℕ) in finset.range p, a ^ (x - 1) * (a ^ (p - 1 - x) * (↑p * (b * ↑x)))) + mk (span {↑p ^ 2}) (∑ (x : ℕ) in finset.range p, a ^ (x + (p - 1 - x))) : by { ring_exp, simp only [← pow_add, map_add, finset.sum_add_distrib, ← map_sum] } ... = mk (span {↑p ^ 2}) (∑ (x : ℕ) in finset.range p, a ^ (x - 1) * (a ^ (p - 1 - x) * (↑p * (b * ↑x)))) + mk (span {↑ p ^ 2}) ∑ (x : ℕ) in finset.range p, a ^ (p - 1) : by { rw [add_right_inj, finset.sum_congr rfl], intros x hx, rw [← nat.add_sub_assoc _ x, nat.add_sub_cancel_left], exact nat.le_pred_of_lt (finset.mem_range.mp hx) } ... = mk (span {↑p ^ 2}) (∑ (x : ℕ) in finset.range p, a ^ (x - 1) * (a ^ (p - 1 - x) * (↑p * (b * ↑x)))) + mk (span {↑ p ^ 2}) (↑p * a ^ (p - 1)) : by simp only [add_right_inj, finset.sum_const, finset.card_range, nsmul_eq_mul] ... = mk (span {↑p ^ 2}) (↑p * b * ∑ (x : ℕ) in finset.range p, a ^ (p - 2) * x) + mk (span {↑p ^ 2}) (↑p * a ^ (p - 1)) : by { simp only [finset.mul_sum, ← mul_assoc, ← pow_add], rw finset.sum_congr rfl, rintros (⟨⟩|⟨x⟩) hx, { rw [nat.cast_zero, mul_zero, mul_zero] }, { have : x.succ - 1 + (p - 1 - x.succ) = p - 2, { rw ← nat.add_sub_assoc (nat.le_pred_of_lt (finset.mem_range.mp hx)), exact congr_arg nat.pred (nat.add_sub_cancel_left _ _)}, rw this, ring_exp_eq }} ... = mk (span {↑p ^ 2}) (↑p * a ^ (p - 1)) : by { simp only [add_left_eq_self, ← finset.mul_sum], norm_cast, simp only [finset.sum_range_id, nat.cast_mul, _root_.map_mul, nat.mul_div_assoc _ (even_iff_two_dvd.mp (nat.odd.sub_odd hp odd_one))], ring_exp, simp only [← map_pow, mul_eq_zero_of_left, ideal.quotient.eq_zero_iff_mem, mem_span_singleton] } end namespace multiplicity section integral_domain variables [is_domain R] [@decidable_rel R (∣)] lemma pow_sub_pow_of_prime {p : R} (hp : prime p) {x y : R} (hxy : p ∣ x - y) (hx : ¬p ∣ x) {n : ℕ} (hn : ¬p ∣ n) : multiplicity p (x ^ n - y ^ n) = multiplicity p (x - y) := by rw [←geom_sum₂_mul, multiplicity.mul hp, multiplicity_eq_zero.2 (not_dvd_geom_sum₂ hp hxy hx hn), zero_add] variables (hp : prime (p : R)) (hp1 : odd p) (hxy : ↑p ∣ x - y) (hx : ¬↑p ∣ x) include hp hp1 hxy hx lemma geom_sum₂_eq_one : multiplicity ↑p (∑ i in range p, x ^ i * y ^ (p - 1 - i)) = 1 := begin rw ← nat.cast_one, refine multiplicity.eq_coe_iff.2 ⟨_, _⟩, { rw pow_one, exact dvd_geom_sum₂_self hxy }, rw dvd_iff_dvd_of_dvd_sub hxy at hx, cases hxy with k hk, rw [one_add_one_eq_two, eq_add_of_sub_eq' hk], refine mt (dvd_iff_dvd_of_dvd_sub (@odd_sq_dvd_geom_sum₂_sub _ _ y k _ hp1)).mp _, rw [pow_two, mul_dvd_mul_iff_left hp.ne_zero], exact mt hp.dvd_of_dvd_pow hx end lemma pow_prime_sub_pow_prime : multiplicity ↑p (x ^ p - y ^ p) = multiplicity ↑p (x - y) + 1 := by rw [←geom_sum₂_mul, multiplicity.mul hp, geom_sum₂_eq_one hp hp1 hxy hx, add_comm] lemma pow_prime_pow_sub_pow_prime_pow (a : ℕ) : multiplicity ↑p (x ^ p ^ a - y ^ p ^ a) = multiplicity ↑p (x - y) + a := begin induction a with a h_ind, { rw [nat.cast_zero, add_zero, pow_zero, pow_one, pow_one] }, rw [←nat.add_one, nat.cast_add, nat.cast_one, ←add_assoc, ←h_ind, pow_succ', pow_mul, pow_mul], apply pow_prime_sub_pow_prime hp hp1, { rw ←geom_sum₂_mul, exact dvd_mul_of_dvd_right hxy _ }, { exact λ h, hx (hp.dvd_of_dvd_pow h) } end end integral_domain section lifting_the_exponent variables (hp : nat.prime p) (hp1 : odd p) include hp hp1 /-- **Lifting the exponent lemma** for odd primes. -/ lemma int.pow_sub_pow {x y : ℤ} (hxy : ↑p ∣ x - y) (hx : ¬↑p ∣ x) (n : ℕ) : multiplicity ↑p (x ^ n - y ^ n) = multiplicity ↑p (x - y) + multiplicity p n := begin cases n, { simp only [multiplicity.zero, add_top, pow_zero, sub_self] }, have h : (multiplicity _ _).dom := finite_nat_iff.mpr ⟨hp.ne_one, n.succ_pos⟩, rcases eq_coe_iff.mp (part_enat.coe_get h).symm with ⟨⟨k, hk⟩, hpn⟩, conv_lhs { rw [hk, pow_mul, pow_mul] }, rw nat.prime_iff_prime_int at hp, rw [pow_sub_pow_of_prime hp, pow_prime_pow_sub_pow_prime_pow hp hp1 hxy hx, part_enat.coe_get], { rw ←geom_sum₂_mul, exact dvd_mul_of_dvd_right hxy _ }, { exact λ h, hx (hp.dvd_of_dvd_pow h) }, { rw int.coe_nat_dvd, rintro ⟨c, rfl⟩, refine hpn ⟨c, _⟩, rwa [pow_succ', mul_assoc] } end lemma int.pow_add_pow {x y : ℤ} (hxy : ↑p ∣ x + y) (hx : ¬↑p ∣ x) {n : ℕ} (hn : odd n) : multiplicity ↑p (x ^ n + y ^ n) = multiplicity ↑p (x + y) + multiplicity p n := begin rw ←sub_neg_eq_add at hxy, rw [←sub_neg_eq_add, ←sub_neg_eq_add, ←odd.neg_pow hn], exact int.pow_sub_pow hp hp1 hxy hx n end lemma nat.pow_sub_pow {x y : ℕ} (hxy : p ∣ x - y) (hx : ¬p ∣ x) (n : ℕ) : multiplicity p (x ^ n - y ^ n) = multiplicity p (x - y) + multiplicity p n := begin obtain hyx | hyx := le_total y x, { iterate 2 { rw ← int.coe_nat_multiplicity }, rw int.coe_nat_sub (nat.pow_le_pow_of_le_left hyx n), rw ← int.coe_nat_dvd at hxy hx, push_cast at *, exact int.pow_sub_pow hp hp1 hxy hx n }, { simp only [nat.sub_eq_zero_iff_le.mpr hyx, nat.sub_eq_zero_iff_le.mpr (nat.pow_le_pow_of_le_left hyx n), multiplicity.zero, part_enat.top_add] } end lemma nat.pow_add_pow {x y : ℕ} (hxy : p ∣ x + y) (hx : ¬p ∣ x) {n : ℕ} (hn : odd n) : multiplicity p (x ^ n + y ^ n) = multiplicity p (x + y) + multiplicity p n := begin iterate 2 { rw [←int.coe_nat_multiplicity] }, rw ←int.coe_nat_dvd at hxy hx, push_cast at *, exact int.pow_add_pow hp hp1 hxy hx hn end end lifting_the_exponent end multiplicity end comm_ring lemma pow_two_pow_sub_pow_two_pow [comm_ring R] {x y : R} (n : ℕ) : x ^ (2 ^ n) - y ^ (2 ^ n) = (∏ i in finset.range n, (x ^ (2 ^ i) + y ^ (2 ^ i))) * (x - y) := begin induction n with d hd, { simp only [pow_zero, pow_one, finset.range_zero, finset.prod_empty, one_mul] }, { suffices : x ^ 2 ^ d.succ - y ^ 2 ^ d.succ = (x ^ 2 ^ d + y ^ 2 ^ d) * (x ^ 2 ^ d - y ^ 2 ^ d), { rw [this, hd, finset.prod_range_succ, ← mul_assoc, mul_comm (x ^ 2 ^ d + y ^ 2 ^ d)] }, { ring_exp_eq } } end lemma _root_.int.sq_mod_four_eq_one_of_odd {x : ℤ} : odd x → x ^ 2 % 4 = 1 := begin intro hx, -- Replace `x : ℤ` with `y : zmod 4` replace hx : x % (2 : ℕ) = 1 % (2 : ℕ), { rw int.odd_iff at hx, norm_num [hx] }, calc x^2 % (4 : ℕ) = 1 % (4 : ℕ) : _ ... = 1 : by norm_num, rw ← zmod.int_coe_eq_int_coe_iff' at hx ⊢, push_cast, rw [← map_int_cast (zmod.cast_hom (show 2 ∣ 4, by norm_num) (zmod 2)) x] at hx, set y : zmod 4 := x, change zmod.cast_hom _ (zmod 2) y = _ at hx, -- Now we can just consider each of the 4 possible values for y fin_cases y using hy; rw hy at ⊢ hx; revert hx; dec_trivial end lemma int.two_pow_two_pow_add_two_pow_two_pow {x y : ℤ} (hx : ¬ 2 ∣ x) (hxy : 4 ∣ (x - y)) (i : ℕ) : multiplicity 2 (x ^ 2 ^ i + y ^ 2 ^ i) = ↑(1 : ℕ) := begin have hx_odd : odd x, { rwa [int.odd_iff_not_even, even_iff_two_dvd] }, have hxy_even : even (x - y) := even_iff_two_dvd.mpr (dvd_trans (by norm_num) hxy), have hy_odd : odd y := by simpa using hx_odd.sub_even hxy_even, refine multiplicity.eq_coe_iff.mpr ⟨_, _⟩, { rw [pow_one, ← even_iff_two_dvd], exact (hx_odd.pow).add_odd hy_odd.pow }, cases i with i, { intro hxy', have : 2 * 2 ∣ 2 * x, { convert dvd_add hxy hxy', ring_exp }, have : 2 ∣ x := (mul_dvd_mul_iff_left (by norm_num)).mp this, contradiction }, suffices : ∀ (x : ℤ), odd x → x ^ (2 ^ (i + 1)) % 4 = 1, { rw [show (2 ^ (1 + 1) : ℤ) = 4, by norm_num, int.dvd_iff_mod_eq_zero, int.add_mod, this _ hx_odd, this _ hy_odd], norm_num }, intros x hx, rw [pow_succ, mul_comm, pow_mul, int.sq_mod_four_eq_one_of_odd hx.pow] end lemma int.two_pow_two_pow_sub_pow_two_pow {x y : ℤ} (n : ℕ) (hxy : 4 ∣ x - y) (hx : ¬ 2 ∣ x) : multiplicity 2 (x ^ (2 ^ n) - y ^ (2 ^ n)) = multiplicity 2 (x - y) + n := by simp only [pow_two_pow_sub_pow_two_pow n, multiplicity.mul int.prime_two, multiplicity.finset.prod (int.prime_two), add_comm, nat.cast_one, finset.sum_const, finset.card_range, nsmul_one, int.two_pow_two_pow_add_two_pow_two_pow hx hxy] lemma int.two_pow_sub_pow' {x y : ℤ} (n : ℕ) (hxy : 4 ∣ x - y) (hx : ¬ 2 ∣ x) : multiplicity 2 (x ^ n - y ^ n) = multiplicity 2 (x - y) + multiplicity (2 : ℤ) n := begin have hx_odd : odd x, { rwa [int.odd_iff_not_even, even_iff_two_dvd] }, have hxy_even : even (x - y) := even_iff_two_dvd.mpr (dvd_trans (by norm_num) hxy), have hy_odd : odd y := by simpa using hx_odd.sub_even hxy_even, cases n, { simp only [pow_zero, sub_self, multiplicity.zero, int.coe_nat_zero, part_enat.add_top] }, have h : (multiplicity 2 n.succ).dom := multiplicity.finite_nat_iff.mpr ⟨by norm_num, n.succ_pos⟩, rcases multiplicity.eq_coe_iff.mp (part_enat.coe_get h).symm with ⟨⟨k, hk⟩, hpn⟩, rw [hk, pow_mul, pow_mul, multiplicity.pow_sub_pow_of_prime, int.two_pow_two_pow_sub_pow_two_pow _ hxy hx, ← hk, part_enat.coe_get], { norm_cast }, { exact int.prime_two }, { simpa only [even_iff_two_dvd] using hx_odd.pow.sub_odd hy_odd.pow }, { simpa only [even_iff_two_dvd, int.odd_iff_not_even] using hx_odd.pow }, erw [int.coe_nat_dvd], -- `erw` to deal with `2 : ℤ` vs `(2 : ℕ) : ℤ` contrapose! hpn, rw pow_succ', conv_rhs { rw hk }, exact mul_dvd_mul_left _ hpn end /-- **Lifting the exponent lemma** for `p = 2` -/ lemma int.two_pow_sub_pow {x y : ℤ} {n : ℕ} (hxy : 2 ∣ x - y) (hx : ¬ 2 ∣ x) (hn : even n) : multiplicity 2 (x ^ n - y ^ n) + 1 = multiplicity 2 (x + y) + multiplicity 2 (x - y) + multiplicity (2 : ℤ) n := begin have hy : odd y, { rw [← even_iff_two_dvd, ← int.odd_iff_not_even] at hx, replace hxy := (@even_neg _ _ (x - y)).mpr (even_iff_two_dvd.mpr hxy), convert even.add_odd hxy hx, abel }, cases hn with d hd, subst hd, simp only [← two_mul, pow_mul], have hxy4 : 4 ∣ x ^ 2 - y ^ 2, { rw [int.dvd_iff_mod_eq_zero, int.sub_mod, int.sq_mod_four_eq_one_of_odd _, int.sq_mod_four_eq_one_of_odd hy], { norm_num }, { simp only [int.odd_iff_not_even, even_iff_two_dvd, hx, not_false_iff] }}, rw [int.two_pow_sub_pow' d hxy4 _, sq_sub_sq, ← int.coe_nat_mul_out, multiplicity.mul (int.prime_two), multiplicity.mul (int.prime_two)], suffices : multiplicity (2 : ℤ) ↑(2 : ℕ) = 1, { rw [this, add_comm (1 : part_enat), ← add_assoc] }, { norm_cast, rw multiplicity.multiplicity_self _ _, { apply prime.not_unit, simp only [← nat.prime_iff, nat.prime_two] }, { exact two_ne_zero }}, { rw [← even_iff_two_dvd, ← int.odd_iff_not_even], apply odd.pow, simp only [int.odd_iff_not_even, even_iff_two_dvd, hx, not_false_iff] } end lemma nat.two_pow_sub_pow {x y : ℕ} (hxy : 2 ∣ x - y) (hx : ¬2 ∣ x) {n : ℕ} (hn : even n) : multiplicity 2 (x ^ n - y ^ n) + 1 = multiplicity 2 (x + y) + multiplicity 2 (x - y) + multiplicity 2 n := begin obtain hyx | hyx := le_total y x, { iterate 3 { rw ←multiplicity.int.coe_nat_multiplicity }, have hxyn : y ^ n ≤ x ^ n := pow_le_pow_of_le_left' hyx _, simp only [int.coe_nat_sub hyx, int.coe_nat_sub (pow_le_pow_of_le_left' hyx _), int.coe_nat_add, int.coe_nat_pow], rw ←int.coe_nat_dvd at hx, rw [←int.coe_nat_dvd, int.coe_nat_sub hyx] at hxy, convert int.two_pow_sub_pow hxy hx hn using 2, rw ← multiplicity.int.coe_nat_multiplicity, refl }, { simp only [nat.sub_eq_zero_iff_le.mpr hyx, nat.sub_eq_zero_iff_le.mpr (pow_le_pow_of_le_left' hyx n), multiplicity.zero, part_enat.top_add, part_enat.add_top] } end namespace padic_val_nat variables {x y : ℕ} lemma pow_two_sub_pow (hyx : y < x) (hxy : 2 ∣ x - y) (hx : ¬ 2 ∣ x) {n : ℕ} (hn : 0 < n) (hneven : even n) : padic_val_nat 2 (x ^ n - y ^ n) + 1 = padic_val_nat 2 (x + y) + padic_val_nat 2 (x - y) + padic_val_nat 2 n := begin simp only [←part_enat.coe_inj, nat.cast_add], iterate 4 { rw [padic_val_nat_def, part_enat.coe_get] }, { convert nat.two_pow_sub_pow hxy hx hneven using 2 }, { exact hn }, { exact (nat.sub_pos_of_lt hyx) }, { linarith }, { simp only [tsub_pos_iff_lt, pow_lt_pow_of_lt_left hyx (@zero_le' _ y _) hn] } end variables {p : ℕ} [hp : fact p.prime] (hp1 : odd p) include hp hp1 lemma pow_sub_pow (hyx : y < x) (hxy : p ∣ x - y) (hx : ¬p ∣ x) {n : ℕ} (hn : 0 < n) : padic_val_nat p (x ^ n - y ^ n) = padic_val_nat p (x - y) + padic_val_nat p n := begin rw [←part_enat.coe_inj, nat.cast_add], iterate 3 { rw [padic_val_nat_def, part_enat.coe_get] }, { exact multiplicity.nat.pow_sub_pow hp.out hp1 hxy hx n }, { exact hn }, { exact nat.sub_pos_of_lt hyx }, { exact nat.sub_pos_of_lt (nat.pow_lt_pow_of_lt_left hyx hn) } end lemma pow_add_pow (hxy : p ∣ x + y) (hx : ¬p ∣ x) {n : ℕ} (hn : odd n) : padic_val_nat p (x ^ n + y ^ n) = padic_val_nat p (x + y) + padic_val_nat p n := begin cases y, { have := dvd_zero p, contradiction }, rw [←part_enat.coe_inj, nat.cast_add], iterate 3 { rw [padic_val_nat_def, part_enat.coe_get] }, { exact multiplicity.nat.pow_add_pow hp.out hp1 hxy hx hn }, { exact (odd.pos hn) }, { simp only [add_pos_iff, nat.succ_pos', or_true] }, { exact (nat.lt_add_left _ _ _ (pow_pos y.succ_pos _)) } end end padic_val_nat
903a531afce625a8e895abbe9b455df35ee60f6b
076f5040b63237c6dd928c6401329ed5adcb0e44
/instructor-notes/2019.09.26.lean
d6e5e59b66e78c30f649695c86afd9048086fc59
[]
no_license
kevinsullivan/uva-cs-dm-f19
0f123689cf6cb078f263950b18382a7086bf30be
09a950752884bd7ade4be33e9e89a2c4b1927167
refs/heads/master
1,594,771,841,541
1,575,853,850,000
1,575,853,850,000
205,433,890
4
9
null
1,571,592,121,000
1,567,188,539,000
Lean
UTF-8
Lean
false
false
4,496
lean
namespace my_list /- inductive mlist (T : Type) : Type | nil : mlist | cons (hd : T) (tl : mlist) : mlist open mlist #check mlist #check nil -- for any type T, mlist T #check nil bool #check nil nat #check nil string #check cons -/ inductive mlist (T : Type) : Type | nil {} : mlist | cons (hd : T) (tl : mlist) : mlist open mlist #check mlist #check nil -- for any type T, mlist T #check @nil bool #check @nil nat #check @nil string #check cons /- We represent an empty list by a term, nil. -/ def e : mlist nat := nil -- type inference! #check e #reduce e -- The list, [1] def l1 : mlist nat := cons 1 -- head element nil -- tail list #reduce l1 -- The list, [1, 2] def l2 : mlist nat := cons 1 ( cons 2 e ) def l3 : mlist nat := cons 1 ( cons 2 ( cons 3 nil ) ) #reduce l3 -- note use of nice notation! def a_list : mlist bool := cons tt ( cons ff ( cons tt (nil) ) ) #reduce a_list -- Nested lists, response to question -- represent [ [1,2], [3,4], [0,10] ] -- notice the *type* of this list def nested_list : list (list nat) := cons ( ) -- [1,2] ( cons (_) -- [3,4] ( cons (_) -- [0,10] nil ) ) -- you can (and should) fill in _s #check e #eval e #check l3 #eval l3 -- Lean supports list notation! def l4 : list nat := [1, 2, 3, 4] #check l4 #eval l4 /- OK, now that we've seen how to use nil and cons terms to represent lists, it's time to define functions that operate on lists represented by such terms. -/ -- A function to prepend nat h to list t def prepend : nat → list nat → list nat | h t := cons h t #eval prepend 1 [2, 3, 4] /- Head takes a value of type list nat and returns the nat head of the list if it is not empty otherwise it returns zero. -/ def mhead : list nat → nat | [] := 0 | (cons h t) := h #eval mhead [] #eval mhead [1, 2, 3] def mtail : list nat → list nat | [] := [] | (cons h t) := t #eval mtail [] #eval mtail [1, 2, 3] -- return length of list def mlength : list nat → nat | [] := 0 | (cons h t) := 1 + (mlength t) #eval mlength [] #eval mlength l2 -- mlength [1 , 2] -- 1 + (mlength [1]) -- 1 + (1 + (mlength [])) -- 1 + (1 + 0) -- 1 + 1 -- 2 -- the final result #eval mlength l4 /- mlength [1, 2, 3, 4] 1 + (mlength [2, 3, 4]) 1 + (1 + mlength [3, 4]) 1 + (1 + (1 + mlength [4])) 1 + (1 + (1 + (1 + mlength []))) 1 + (1 + (1 + (1 + 0))) 1 + (1 + (1 + 1)) 1 + (1 + 2) 1 + 3 4 -- the final result -/ def mdouble : list nat → list nat | [] := [] | (cons h t) := cons (h*2) (mdouble t) #eval mdouble [] #eval mdouble l4 def mmap : (ℕ → ℕ) → list ℕ → list ℕ | f [] := [] | f (cons h t) := cons (f h) (mmap f t) #eval mmap nat.succ [1,2,3,4] #eval mmap (λ n : ℕ, n*n) [1,2,3,4] -- double every element of a list def mdouble : list nat → list nat | [] := [] | (cons h t) := cons (2*h) (mdouble t) #eval mdouble l4 -- apply a fun to every list element def mmap : (nat → nat) → list nat → list nat | f nil := nil | f (cons h t) := cons (f h) (mmap f t) #eval mmap nat.succ l4 -- Lean nat.succ #eval mmap (λ n: nat, n*n) l4 --square -- A function that computes the sum of -- all the numbers in a list of nats. def mlist_sum : list nat → nat | nil := 0 | (cons h t) := h + mlist_sum t #eval mlist_sum [] #eval mlist_sum l4 -- A function that computes the product -- of the numbers in a list of nats. def mlist_prod : list nat → nat | nil := 1 | (cons h t) := h * mlist_prod t #eval mlist_prod [] #eval mlist_prod l4 -- Can we generalize? /- Yes. Fold. Take identity element, binary function, and list of nat, as arguments, and "reduce" the list using the given operator along with its correct identity value. -/ def mlist_fold : -- also called "reduce" nat → (ℕ → ℕ → ℕ) → list nat → nat | id f nil := id | id f (cons h t) := f h (mlist_fold id f t) #eval mlist_fold 0 nat.add l4 #eval mlist_fold 1 nat.mul l4 -- recursion problems -- 1. args in wrong order (in add) -- 2. *must* use by-cases style end mlist
d6d838d8af58baa8d0fb7474176360bb1dd1dcb8
8e31b9e0d8cec76b5aa1e60a240bbd557d01047c
/scratch/transfer_test.lean
773f414062e80c7738d28d9bdfb39a6ddd5331bb
[]
no_license
ChrisHughes24/LP
7bdd62cb648461c67246457f3ddcb9518226dd49
e3ed64c2d1f642696104584e74ae7226d8e916de
refs/heads/master
1,685,642,642,858
1,578,070,602,000
1,578,070,602,000
195,268,102
4
3
null
1,569,229,518,000
1,562,255,287,000
Lean
UTF-8
Lean
false
false
1,891
lean
import .transfer_mathlib inductive xnat : Type | zero : xnat | succ : xnat → xnat instance : has_zero xnat := ⟨xnat.zero⟩ instance : has_one xnat := ⟨xnat.succ 0⟩ def to_xnat : ℕ → xnat | 0 := 0 | (nat.succ n) := (to_xnat n).succ def of_xnat : xnat → ℕ | 0 := 0 | (xnat.succ n) := (of_xnat n).succ theorem to_of_xnat : ∀ n, (to_xnat (of_xnat n)) = n | 0 := rfl | (xnat.succ n) := congr_arg xnat.succ (to_of_xnat n) theorem of_to_xnat : ∀ n, (of_xnat (to_xnat n)) = n | 0 := rfl | (nat.succ n) := congr_arg nat.succ (of_to_xnat n) def rel (x : xnat) (n : ℕ) : Prop := to_xnat n = x lemma rel_zero : rel 0 0 := eq.refl _ run_cmd do rds ← transfer.analyse_decls [``rel_zero], rds_fmt ← rds.mmap has_to_tactic_format.to_tactic_format, tactic.trace (((rds_fmt.map to_string).intersperse "; ").to_string), transfer.compute_transfer rds [] `(rel (0 : xnat)) >>= tactic.trace lemma rel_succ : (rel ⇒ rel) xnat.succ nat.succ := sorry lemma rel_one : rel 1 1 := eq.refl _ instance : has_add xnat := ⟨λ m n, by induction n; [exact m, exact n_ih.succ]⟩ theorem to_xnat_add (m) : ∀ n, to_xnat (m + n) = to_xnat m + to_xnat n | 0 := rfl | (nat.succ n) := congr_arg xnat.succ (to_xnat_add n) lemma rel_add : (rel ⇒ rel ⇒ rel) (+) (+) := sorry lemma rel_eq : (rel ⇒ rel ⇒ iff) (=) (=) := sorry instance : relator.bi_total rel := ⟨λ a, ⟨_, to_of_xnat _⟩, λ a, ⟨_, rfl⟩⟩ example : ∀ x y : xnat, x + y = y + x := begin transfer.transfer [``relator.rel_forall_of_total, ``rel_eq, ``rel_add], simp [add_comm] end run_cmd do rds ← transfer.analyse_decls [``relator.rel_forall_of_total, ``rel_eq, ``rel_add], rds_fmt ← rds.mmap has_to_tactic_format.to_tactic_format, tactic.trace (((rds_fmt.map to_string).intersperse "; ").to_string), transfer.compute_transfer rds [] `(∀ x y : xnat, x + y = y + x)
883b8931beb736f97eb6aca6a01214beb71b8d49
94e33a31faa76775069b071adea97e86e218a8ee
/src/number_theory/cyclotomic/basic.lean
272d708e4da324cf770d066f1981bf341e18f000
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
27,260
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import ring_theory.polynomial.cyclotomic.basic import number_theory.number_field import algebra.char_p.algebra import field_theory.galois /-! # Cyclotomic extensions Let `A` and `B` be commutative rings with `algebra A B`. For `S : set ℕ+`, we define a class `is_cyclotomic_extension S A B` expressing the fact that `B` is obtained from `A` by adding `n`-th primitive roots of unity, for all `n ∈ S`. ## Main definitions * `is_cyclotomic_extension S A B` : means that `B` is obtained from `A` by adding `n`-th primitive roots of unity, for all `n ∈ S`. * `cyclotomic_field`: given `n : ℕ+` and a field `K`, we define `cyclotomic n K` as the splitting field of `cyclotomic n K`. If `n` is nonzero in `K`, it has the instance `is_cyclotomic_extension {n} K (cyclotomic_field n K)`. * `cyclotomic_ring` : if `A` is a domain with fraction field `K` and `n : ℕ+`, we define `cyclotomic_ring n A K` as the `A`-subalgebra of `cyclotomic_field n K` generated by the roots of `X ^ n - 1`. If `n` is nonzero in `A`, it has the instance `is_cyclotomic_extension {n} A (cyclotomic_ring n A K)`. ## Main results * `is_cyclotomic_extension.trans` : if `is_cyclotomic_extension S A B` and `is_cyclotomic_extension T B C`, then `is_cyclotomic_extension (S ∪ T) A C` if `no_zero_smul_divisors B C` and `nontrivial C`. * `is_cyclotomic_extension.union_right` : given `is_cyclotomic_extension (S ∪ T) A B`, then `is_cyclotomic_extension T (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }) B`. * `is_cyclotomic_extension.union_right` : given `is_cyclotomic_extension T A B` and `S ⊆ T`, then `is_cyclotomic_extension S A (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 })`. * `is_cyclotomic_extension.finite` : if `S` is finite and `is_cyclotomic_extension S A B`, then `B` is a finite `A`-algebra. * `is_cyclotomic_extension.number_field` : a finite cyclotomic extension of a number field is a number field. * `is_cyclotomic_extension.splitting_field_X_pow_sub_one` : if `is_cyclotomic_extension {n} K L`, then `L` is the splitting field of `X ^ n - 1`. * `is_cyclotomic_extension.splitting_field_cyclotomic` : if `is_cyclotomic_extension {n} K L`, then `L` is the splitting field of `cyclotomic n K`. ## Implementation details Our definition of `is_cyclotomic_extension` is very general, to allow rings of any characteristic and infinite extensions, but it will mainly be used in the case `S = {n}` and for integral domains. All results are in the `is_cyclotomic_extension` namespace. Note that some results, for example `is_cyclotomic_extension.trans`, `is_cyclotomic_extension.finite`, `is_cyclotomic_extension.number_field`, `is_cyclotomic_extension.finite_dimensional`, `is_cyclotomic_extension.is_galois` and `cyclotomic_field.algebra_base` are lemmas, but they can be made local instances. Some of them are included in the `cyclotomic` locale. -/ open polynomial algebra finite_dimensional module set open_locale big_operators universes u v w z variables (n : ℕ+) (S T : set ℕ+) (A : Type u) (B : Type v) (K : Type w) (L : Type z) variables [comm_ring A] [comm_ring B] [algebra A B] variables [field K] [field L] [algebra K L] noncomputable theory /-- Given an `A`-algebra `B` and `S : set ℕ+`, we define `is_cyclotomic_extension S A B` requiring that there is a `a`-th primitive root of unity in `B` for all `a ∈ S` and that `B` is generated over `A` by the roots of `X ^ n - 1`. -/ @[mk_iff] class is_cyclotomic_extension : Prop := (exists_prim_root {a : ℕ+} (ha : a ∈ S) : ∃ r : B, is_primitive_root r a) (adjoin_roots : ∀ (x : B), x ∈ adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }) namespace is_cyclotomic_extension section basic /-- A reformulation of `is_cyclotomic_extension` that uses `⊤`. -/ lemma iff_adjoin_eq_top : is_cyclotomic_extension S A B ↔ (∀ (a : ℕ+), a ∈ S → ∃ r : B, is_primitive_root r a) ∧ (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 } = ⊤) := ⟨λ h, ⟨h.exists_prim_root, algebra.eq_top_iff.2 h.adjoin_roots⟩, λ h, ⟨h.1, algebra.eq_top_iff.1 h.2⟩⟩ /-- A reformulation of `is_cyclotomic_extension` in the case `S` is a singleton. -/ lemma iff_singleton : is_cyclotomic_extension {n} A B ↔ (∃ r : B, is_primitive_root r n) ∧ (∀ x, x ∈ adjoin A { b : B | b ^ (n : ℕ) = 1 }) := by simp [is_cyclotomic_extension_iff] /-- If `is_cyclotomic_extension ∅ A B`, then the image of `A` in `B` equals `B`. -/ lemma empty [h : is_cyclotomic_extension ∅ A B] : (⊥ : subalgebra A B) = ⊤ := by simpa [algebra.eq_top_iff, is_cyclotomic_extension_iff] using h /-- If `is_cyclotomic_extension {1} A B`, then the image of `A` in `B` equals `B`. -/ lemma singleton_one [h : is_cyclotomic_extension {1} A B] : (⊥ : subalgebra A B) = ⊤ := algebra.eq_top_iff.2 (λ x, by simpa [adjoin_singleton_one] using ((is_cyclotomic_extension_iff _ _ _).1 h).2 x) /-- Transitivity of cyclotomic extensions. -/ lemma trans (C : Type w) [comm_ring C] [nontrivial C] [algebra A C] [algebra B C] [is_scalar_tower A B C] [hS : is_cyclotomic_extension S A B] [hT : is_cyclotomic_extension T B C] [no_zero_smul_divisors B C] : is_cyclotomic_extension (S ∪ T) A C := begin refine ⟨λ n hn, _, λ x, _⟩, { cases hn, { obtain ⟨b, hb⟩ := ((is_cyclotomic_extension_iff _ _ _).1 hS).1 hn, refine ⟨algebra_map B C b, _⟩, exact hb.map_of_injective (no_zero_smul_divisors.algebra_map_injective B C) }, { exact ((is_cyclotomic_extension_iff _ _ _).1 hT).1 hn } }, { refine adjoin_induction (((is_cyclotomic_extension_iff _ _ _).1 hT).2 x) (λ c ⟨n, hn⟩, subset_adjoin ⟨n, or.inr hn.1, hn.2⟩) (λ b, _) (λ x y hx hy, subalgebra.add_mem _ hx hy) (λ x y hx hy, subalgebra.mul_mem _ hx hy), { let f := is_scalar_tower.to_alg_hom A B C, have hb : f b ∈ (adjoin A { b : B | ∃ (a : ℕ+), a ∈ S ∧ b ^ (a : ℕ) = 1 }).map f := ⟨b, ((is_cyclotomic_extension_iff _ _ _).1 hS).2 b, rfl⟩, rw [is_scalar_tower.to_alg_hom_apply, ← adjoin_image] at hb, refine adjoin_mono (λ y hy, _) hb, obtain ⟨b₁, ⟨⟨n, hn⟩, h₁⟩⟩ := hy, exact ⟨n, ⟨mem_union_left T hn.1, by rw [← h₁, ← alg_hom.map_pow, hn.2, alg_hom.map_one]⟩⟩ } } end @[nontriviality] lemma subsingleton_iff [subsingleton B] : is_cyclotomic_extension S A B ↔ S = {} ∨ S = {1} := begin split, { rintro ⟨hprim, -⟩, rw ←subset_singleton_iff_eq, intros t ht, obtain ⟨ζ, hζ⟩ := hprim ht, rw [mem_singleton_iff, ←pnat.coe_eq_one_iff], exact_mod_cast hζ.unique (is_primitive_root.of_subsingleton ζ) }, { rintro (rfl|rfl), { refine ⟨λ _ h, h.elim, λ x, by convert subalgebra.zero_mem _⟩ }, { rw iff_singleton, refine ⟨⟨0, is_primitive_root.of_subsingleton 0⟩, λ x, by convert subalgebra.zero_mem _⟩ } } end /-- If `B` is a cyclotomic extension of `A` given by roots of unity of order in `S ∪ T`, then `B` is a cyclotomic extension of `adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 } ` given by roots of unity of order in `T`. -/ lemma union_right [h : is_cyclotomic_extension (S ∪ T) A B] : is_cyclotomic_extension T (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }) B := begin have : { b : B | ∃ (n : ℕ+), n ∈ S ∪ T ∧ b ^ (n : ℕ) = 1 } = { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 } ∪ { b : B | ∃ (n : ℕ+), n ∈ T ∧ b ^ (n : ℕ) = 1 }, { refine le_antisymm (λ x hx, _) (λ x hx, _), { rcases hx with ⟨n, hn₁ | hn₂, hnpow⟩, { left, exact ⟨n, hn₁, hnpow⟩ }, { right, exact ⟨n, hn₂, hnpow⟩ } }, { rcases hx with ⟨n, hn⟩ | ⟨n, hn⟩, { exact ⟨n, or.inl hn.1, hn.2⟩ }, { exact ⟨n, or.inr hn.1, hn.2⟩ } } }, refine ⟨λ n hn, ((is_cyclotomic_extension_iff _ _ _).1 h).1 (mem_union_right S hn), λ b, _⟩, replace h := ((is_cyclotomic_extension_iff _ _ _).1 h).2 b, rwa [this, adjoin_union_eq_adjoin_adjoin, subalgebra.mem_restrict_scalars] at h end /-- If `B` is a cyclotomic extension of `A` given by roots of unity of order in `T` and `S ⊆ T`, then `adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }` is a cyclotomic extension of `B` given by roots of unity of order in `S`. -/ lemma union_left [h : is_cyclotomic_extension T A B] (hS : S ⊆ T) : is_cyclotomic_extension S A (adjoin A { b : B | ∃ a : ℕ+, a ∈ S ∧ b ^ (a : ℕ) = 1 }) := begin refine ⟨λ n hn, _, λ b, _⟩, { obtain ⟨b, hb⟩ := ((is_cyclotomic_extension_iff _ _ _).1 h).1 (hS hn), refine ⟨⟨b, subset_adjoin ⟨n, hn, hb.pow_eq_one⟩⟩, _⟩, rwa [← is_primitive_root.coe_submonoid_class_iff, subtype.coe_mk] }, { convert mem_top, rw [← adjoin_adjoin_coe_preimage, preimage_set_of_eq], norm_cast } end @[protected] lemma ne_zero [h : is_cyclotomic_extension {n} A B] [is_domain B] : ne_zero ((n : ℕ) : B) := begin obtain ⟨⟨r, hr⟩, -⟩ := (iff_singleton n A B).1 h, exact hr.ne_zero' end @[protected] lemma ne_zero' [is_cyclotomic_extension {n} A B] [is_domain B] : ne_zero ((n : ℕ) : A) := begin apply ne_zero.nat_of_ne_zero (algebra_map A B), exact ne_zero n A B, end end basic section fintype lemma finite_of_singleton [is_domain B] [h : is_cyclotomic_extension {n} A B] : finite A B := begin classical, rw [module.finite_def, ← top_to_submodule, ← ((iff_adjoin_eq_top _ _ _).1 h).2], refine fg_adjoin_of_finite _ (λ b hb, _), { simp only [mem_singleton_iff, exists_eq_left], have : {b : B | b ^ (n : ℕ) = 1} = (nth_roots n (1 : B)).to_finset := set.ext (λ x, ⟨λ h, by simpa using h, λ h, by simpa using h⟩), rw [this], exact (nth_roots ↑n 1).to_finset.finite_to_set }, { simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq] at hb, refine ⟨X ^ (n : ℕ) - 1, ⟨monic_X_pow_sub_C _ n.pos.ne.symm, by simp [hb]⟩⟩ } end /-- If `S` is finite and `is_cyclotomic_extension S A B`, then `B` is a finite `A`-algebra. -/ lemma finite [is_domain B] [h₁ : fintype S] [h₂ : is_cyclotomic_extension S A B] : finite A B := begin unfreezingI {revert h₂ A B}, refine set.finite.induction_on (set.finite.intro h₁) (λ A B, _) (λ n S hn hS H A B, _), { introsI _ _ _ _ _, refine module.finite_def.2 ⟨({1} : finset B), _⟩, simp [← top_to_submodule, ← empty, to_submodule_bot] }, { introsI _ _ _ _ h, haveI : is_cyclotomic_extension S A (adjoin A { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 }) := union_left _ (insert n S) _ _ (subset_insert n S), haveI := H A (adjoin A { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 }), haveI : finite (adjoin A { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 }) B, { rw [← union_singleton] at h, letI := @union_right S {n} A B _ _ _ h, exact finite_of_singleton n _ _ }, exact finite.trans (adjoin A { b : B | ∃ (n : ℕ+), n ∈ S ∧ b ^ (n : ℕ) = 1 }) _ } end /-- A cyclotomic finite extension of a number field is a number field. -/ lemma number_field [h : number_field K] [fintype S] [is_cyclotomic_extension S K L] : number_field L := { to_char_zero := char_zero_of_injective_algebra_map (algebra_map K L).injective, to_finite_dimensional := @finite.trans _ K L _ _ _ _ (@algebra_rat L _ (char_zero_of_injective_algebra_map (algebra_map K L).injective)) _ _ h.to_finite_dimensional (finite S K L) } localized "attribute [instance] is_cyclotomic_extension.number_field" in cyclotomic /-- A finite cyclotomic extension of an integral noetherian domain is integral -/ lemma integral [is_domain B] [is_noetherian_ring A] [fintype S] [is_cyclotomic_extension S A B] : algebra.is_integral A B := is_integral_of_noetherian $ is_noetherian_of_fg_of_noetherian' $ (finite S A B).out /-- If `S` is finite and `is_cyclotomic_extension S K A`, then `finite_dimensional K A`. -/ lemma finite_dimensional (C : Type z) [fintype S] [comm_ring C] [algebra K C] [is_domain C] [is_cyclotomic_extension S K C] : finite_dimensional K C := finite S K C localized "attribute [instance] is_cyclotomic_extension.finite_dimensional" in cyclotomic end fintype section variables {A B} lemma adjoin_roots_cyclotomic_eq_adjoin_nth_roots [decidable_eq B] [is_domain B] {ζ : B} {n : ℕ+} (hζ : is_primitive_root ζ n) : adjoin A ↑((map (algebra_map A B) (cyclotomic n A)).roots.to_finset) = adjoin A {b : B | ∃ (a : ℕ+), a ∈ ({n} : set ℕ+) ∧ b ^ (a : ℕ) = 1} := begin simp only [mem_singleton_iff, exists_eq_left, map_cyclotomic], refine le_antisymm (adjoin_mono (λ x hx, _)) (adjoin_le (λ x hx, _)), { simp only [multiset.mem_to_finset, finset.mem_coe, map_cyclotomic, mem_roots (cyclotomic_ne_zero n B)] at hx, simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq], rw is_root_of_unity_iff n.pos, exact ⟨n, nat.mem_divisors_self n n.ne_zero, hx⟩ }, { simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq] at hx, obtain ⟨i, hin, rfl⟩ := hζ.eq_pow_of_pow_eq_one hx n.pos, refine set_like.mem_coe.2 (subalgebra.pow_mem _ (subset_adjoin _) _), rwa [finset.mem_coe, multiset.mem_to_finset, mem_roots $ cyclotomic_ne_zero n B], exact hζ.is_root_cyclotomic n.pos } end lemma adjoin_roots_cyclotomic_eq_adjoin_root_cyclotomic {n : ℕ+} [decidable_eq B] [is_domain B] {ζ : B} (hζ : is_primitive_root ζ n) : adjoin A (((map (algebra_map A B) (cyclotomic n A)).roots.to_finset) : set B) = adjoin A ({ζ}) := begin refine le_antisymm (adjoin_le (λ x hx, _)) (adjoin_mono (λ x hx, _)), { suffices hx : x ^ ↑n = 1, obtain ⟨i, hin, rfl⟩ := hζ.eq_pow_of_pow_eq_one hx n.pos, exact set_like.mem_coe.2 (subalgebra.pow_mem _ (subset_adjoin $ mem_singleton ζ) _), rw is_root_of_unity_iff n.pos, refine ⟨n, nat.mem_divisors_self n n.ne_zero, _⟩, rwa [finset.mem_coe, multiset.mem_to_finset, map_cyclotomic, mem_roots $ cyclotomic_ne_zero n B] at hx }, { simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq] at hx, simpa only [hx, multiset.mem_to_finset, finset.mem_coe, map_cyclotomic, mem_roots (cyclotomic_ne_zero n B)] using hζ.is_root_cyclotomic n.pos } end lemma adjoin_primitive_root_eq_top {n : ℕ+} [is_domain B] [h : is_cyclotomic_extension {n} A B] {ζ : B} (hζ : is_primitive_root ζ n) : adjoin A ({ζ} : set B) = ⊤ := begin classical, rw ←adjoin_roots_cyclotomic_eq_adjoin_root_cyclotomic hζ, rw adjoin_roots_cyclotomic_eq_adjoin_nth_roots hζ, exact ((iff_adjoin_eq_top {n} A B).mp h).2, end variable (A) lemma _root_.is_primitive_root.adjoin_is_cyclotomic_extension {ζ : B} {n : ℕ+} (h : is_primitive_root ζ n) : is_cyclotomic_extension {n} A (adjoin A ({ζ} : set B)) := { exists_prim_root := λ i hi, begin rw [set.mem_singleton_iff] at hi, refine ⟨⟨ζ, subset_adjoin $ set.mem_singleton ζ⟩, _⟩, rwa [← is_primitive_root.coe_submonoid_class_iff, subtype.coe_mk, hi], end, adjoin_roots := λ x, begin refine adjoin_induction' (λ b hb, _) (λ a, _) (λ b₁ b₂ hb₁ hb₂, _) (λ b₁ b₂ hb₁ hb₂, _) x, { rw [set.mem_singleton_iff] at hb, refine subset_adjoin _, simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq, hb], rw [← subalgebra.coe_eq_one, subalgebra.coe_pow, set_like.coe_mk], exact ((is_primitive_root.iff_def ζ n).1 h).1 }, { exact subalgebra.algebra_map_mem _ _ }, { exact subalgebra.add_mem _ hb₁ hb₂ }, { exact subalgebra.mul_mem _ hb₁ hb₂ } end } end section field variables {n S} /-- A cyclotomic extension splits `X ^ n - 1` if `n ∈ S`.-/ lemma splits_X_pow_sub_one [H : is_cyclotomic_extension S K L] (hS : n ∈ S) : splits (algebra_map K L) (X ^ (n : ℕ) - 1) := begin rw [← splits_id_iff_splits, polynomial.map_sub, polynomial.map_one, polynomial.map_pow, polynomial.map_X], obtain ⟨z, hz⟩ := ((is_cyclotomic_extension_iff _ _ _).1 H).1 hS, exact X_pow_sub_one_splits hz, end /-- A cyclotomic extension splits `cyclotomic n K` if `n ∈ S` and `ne_zero (n : K)`.-/ lemma splits_cyclotomic [is_cyclotomic_extension S K L] (hS : n ∈ S) : splits (algebra_map K L) (cyclotomic n K) := begin refine splits_of_splits_of_dvd _ (X_pow_sub_C_ne_zero n.pos _) (splits_X_pow_sub_one K L hS) _, use (∏ (i : ℕ) in (n : ℕ).proper_divisors, polynomial.cyclotomic i K), rw [(eq_cyclotomic_iff n.pos _).1 rfl, ring_hom.map_one], end variables (n S) section singleton variables [is_cyclotomic_extension {n} K L] /-- If `is_cyclotomic_extension {n} K L`, then `L` is the splitting field of `X ^ n - 1`. -/ lemma splitting_field_X_pow_sub_one : is_splitting_field K L (X ^ (n : ℕ) - 1) := { splits := splits_X_pow_sub_one K L (mem_singleton n), adjoin_roots := begin rw [← ((iff_adjoin_eq_top {n} K L).1 infer_instance).2], congr, refine set.ext (λ x, _), simp only [polynomial.map_pow, mem_singleton_iff, multiset.mem_to_finset, exists_eq_left, mem_set_of_eq, polynomial.map_X, polynomial.map_one, finset.mem_coe, polynomial.map_sub], rwa [← ring_hom.map_one C, mem_roots (@X_pow_sub_C_ne_zero L _ _ _ n.pos _), is_root.def, eval_sub, eval_pow, eval_C, eval_X, sub_eq_zero] end } localized "attribute [instance] is_cyclotomic_extension.splitting_field_X_pow_sub_one" in cyclotomic include n lemma is_galois : is_galois K L := begin letI := splitting_field_X_pow_sub_one n K L, exact is_galois.of_separable_splitting_field (X_pow_sub_one_separable_iff.2 ((ne_zero' n K L).1)) end localized "attribute [instance] is_cyclotomic_extension.is_galois" in cyclotomic /-- If `is_cyclotomic_extension {n} K L`, then `L` is the splitting field of `cyclotomic n K`. -/ lemma splitting_field_cyclotomic : is_splitting_field K L (cyclotomic n K) := { splits := splits_cyclotomic K L (mem_singleton n), adjoin_roots := begin rw [← ((iff_adjoin_eq_top {n} K L).1 infer_instance).2], letI := classical.dec_eq L, obtain ⟨ζ, hζ⟩ := @is_cyclotomic_extension.exists_prim_root {n} K L _ _ _ _ _ (mem_singleton n), exact adjoin_roots_cyclotomic_eq_adjoin_nth_roots hζ end } localized "attribute [instance] is_cyclotomic_extension.splitting_field_cyclotomic" in cyclotomic end singleton end field end is_cyclotomic_extension section cyclotomic_field /-- Given `n : ℕ+` and a field `K`, we define `cyclotomic_field n K` as the splitting field of `cyclotomic n K`. If `n` is nonzero in `K`, it has the instance `is_cyclotomic_extension {n} K (cyclotomic_field n K)`. -/ @[derive [field, algebra K, inhabited]] def cyclotomic_field : Type w := (cyclotomic n K).splitting_field namespace cyclotomic_field instance [char_zero K] : char_zero (cyclotomic_field n K) := char_zero_of_injective_algebra_map ((algebra_map K _).injective) instance is_cyclotomic_extension [ne_zero ((n : ℕ) : K)] : is_cyclotomic_extension {n} K (cyclotomic_field n K) := { exists_prim_root := λ a han, begin rw mem_singleton_iff at han, subst a, obtain ⟨r, hr⟩ := exists_root_of_splits (algebra_map K (cyclotomic_field n K)) (splitting_field.splits _) (degree_cyclotomic_pos n K (n.pos)).ne', refine ⟨r, _⟩, haveI := ne_zero.of_no_zero_smul_divisors K (cyclotomic_field n K) n, rwa [← eval_map, ← is_root.def, map_cyclotomic, is_root_cyclotomic_iff] at hr end, adjoin_roots := begin rw [←algebra.eq_top_iff, ←splitting_field.adjoin_roots, eq_comm], letI := classical.dec_eq (cyclotomic_field n K), obtain ⟨ζ, hζ⟩ := exists_root_of_splits _ (splitting_field.splits (cyclotomic n K)) (degree_cyclotomic_pos n _ n.pos).ne', haveI : ne_zero ((n : ℕ) : (cyclotomic_field n K)) := ne_zero.nat_of_injective (algebra_map K _).injective, rw [eval₂_eq_eval_map, map_cyclotomic, ← is_root.def, is_root_cyclotomic_iff] at hζ, exact is_cyclotomic_extension.adjoin_roots_cyclotomic_eq_adjoin_nth_roots hζ, end } end cyclotomic_field end cyclotomic_field section is_domain variables [is_domain A] [algebra A K] [is_fraction_ring A K] section cyclotomic_ring /-- If `K` is the fraction field of `A`, the `A`-algebra structure on `cyclotomic_field n K`. This is not an instance since it causes diamonds when `A = ℤ`. -/ @[nolint unused_arguments] def cyclotomic_field.algebra_base : algebra A (cyclotomic_field n K) := ((algebra_map K (cyclotomic_field n K)).comp (algebra_map A K)).to_algebra local attribute [instance] cyclotomic_field.algebra_base instance cyclotomic_field.no_zero_smul_divisors : no_zero_smul_divisors A (cyclotomic_field n K) := no_zero_smul_divisors.of_algebra_map_injective $ function.injective.comp (no_zero_smul_divisors.algebra_map_injective _ _) $ is_fraction_ring.injective A K /-- If `A` is a domain with fraction field `K` and `n : ℕ+`, we define `cyclotomic_ring n A K` as the `A`-subalgebra of `cyclotomic_field n K` generated by the roots of `X ^ n - 1`. If `n` is nonzero in `A`, it has the instance `is_cyclotomic_extension {n} A (cyclotomic_ring n A K)`. -/ @[derive [comm_ring, is_domain, inhabited]] def cyclotomic_ring : Type w := adjoin A { b : (cyclotomic_field n K) | b ^ (n : ℕ) = 1 } namespace cyclotomic_ring /-- The `A`-algebra structure on `cyclotomic_ring n A K`. This is not an instance since it causes diamonds when `A = ℤ`. -/ def algebra_base : algebra A (cyclotomic_ring n A K) := (adjoin A _).algebra local attribute [instance] cyclotomic_ring.algebra_base instance : no_zero_smul_divisors A (cyclotomic_ring n A K) := (adjoin A _).no_zero_smul_divisors_bot lemma algebra_base_injective : function.injective $ algebra_map A (cyclotomic_ring n A K) := no_zero_smul_divisors.algebra_map_injective _ _ instance : algebra (cyclotomic_ring n A K) (cyclotomic_field n K) := (adjoin A _).to_algebra lemma adjoin_algebra_injective : function.injective $ algebra_map (cyclotomic_ring n A K) (cyclotomic_field n K) := subtype.val_injective instance : no_zero_smul_divisors (cyclotomic_ring n A K) (cyclotomic_field n K) := no_zero_smul_divisors.of_algebra_map_injective (adjoin_algebra_injective n A K) instance : is_scalar_tower A (cyclotomic_ring n A K) (cyclotomic_field n K) := is_scalar_tower.subalgebra' _ _ _ _ instance is_cyclotomic_extension [ne_zero ((n : ℕ) : A)] : is_cyclotomic_extension {n} A (cyclotomic_ring n A K) := { exists_prim_root := λ a han, begin rw mem_singleton_iff at han, subst a, haveI := ne_zero.of_no_zero_smul_divisors A K n, haveI := ne_zero.of_no_zero_smul_divisors A (cyclotomic_field n K) n, obtain ⟨μ, hμ⟩ := let h := (cyclotomic_field.is_cyclotomic_extension n K).exists_prim_root in h $ mem_singleton n, refine ⟨⟨μ, subset_adjoin _⟩, _⟩, { apply (is_root_of_unity_iff n.pos (cyclotomic_field n K)).mpr, refine ⟨n, nat.mem_divisors_self _ n.ne_zero, _⟩, rwa [← is_root_cyclotomic_iff] at hμ }, { rwa [← is_primitive_root.coe_submonoid_class_iff, subtype.coe_mk] } end, adjoin_roots := λ x, begin refine adjoin_induction' (λ y hy, _) (λ a, _) (λ y z hy hz, _) (λ y z hy hz, _) x, { refine subset_adjoin _, simp only [mem_singleton_iff, exists_eq_left, mem_set_of_eq], rwa [← subalgebra.coe_eq_one, subalgebra.coe_pow, subtype.coe_mk] }, { exact subalgebra.algebra_map_mem _ a }, { exact subalgebra.add_mem _ hy hz }, { exact subalgebra.mul_mem _ hy hz }, end } instance [ne_zero ((n : ℕ) : A)] : is_fraction_ring (cyclotomic_ring n A K) (cyclotomic_field n K) := { map_units := λ ⟨x, hx⟩, begin rw is_unit_iff_ne_zero, apply map_ne_zero_of_mem_non_zero_divisors, apply adjoin_algebra_injective, exact hx end, surj := λ x, begin letI : ne_zero ((n : ℕ) : K) := ne_zero.nat_of_injective (is_fraction_ring.injective A K), refine algebra.adjoin_induction (((is_cyclotomic_extension.iff_singleton n K _).1 (cyclotomic_field.is_cyclotomic_extension n K)).2 x) (λ y hy, _) (λ k, _) _ _, { exact ⟨⟨⟨y, subset_adjoin hy⟩, 1⟩, by simpa⟩ }, { have : is_localization (non_zero_divisors A) K := infer_instance, replace := this.surj, obtain ⟨⟨z, w⟩, hw⟩ := this k, refine ⟨⟨algebra_map A _ z, algebra_map A _ w, map_mem_non_zero_divisors _ (algebra_base_injective n A K) w.2⟩, _⟩, letI : is_scalar_tower A K (cyclotomic_field n K) := is_scalar_tower.of_algebra_map_eq (congr_fun rfl), rw [set_like.coe_mk, ← is_scalar_tower.algebra_map_apply, ← is_scalar_tower.algebra_map_apply, @is_scalar_tower.algebra_map_apply A K _ _ _ _ _ (_root_.cyclotomic_field.algebra n K) _ _ w, ← ring_hom.map_mul, hw, ← is_scalar_tower.algebra_map_apply] }, { rintro y z ⟨a, ha⟩ ⟨b, hb⟩, refine ⟨⟨a.1 * b.2 + b.1 * a.2, a.2 * b.2, mul_mem_non_zero_divisors.2 ⟨a.2.2, b.2.2⟩⟩, _⟩, rw [set_like.coe_mk, ring_hom.map_mul, add_mul, ← mul_assoc, ha, mul_comm ((algebra_map _ _) ↑a.2), ← mul_assoc, hb], simp }, { rintro y z ⟨a, ha⟩ ⟨b, hb⟩, refine ⟨⟨a.1 * b.1, a.2 * b.2, mul_mem_non_zero_divisors.2 ⟨a.2.2, b.2.2⟩⟩, _⟩, rw [set_like.coe_mk, ring_hom.map_mul, mul_comm ((algebra_map _ _) ↑a.2), mul_assoc, ← mul_assoc z, hb, ← mul_comm ((algebra_map _ _) ↑a.2), ← mul_assoc, ha], simp } end, eq_iff_exists := λ x y, ⟨λ h, ⟨1, by rw adjoin_algebra_injective n A K h⟩, λ ⟨c, hc⟩, by rw mul_right_cancel₀ (non_zero_divisors.ne_zero c.prop) hc⟩ } lemma eq_adjoin_primitive_root {μ : (cyclotomic_field n K)} (h : is_primitive_root μ n) : cyclotomic_ring n A K = adjoin A ({μ} : set ((cyclotomic_field n K))) := begin letI := classical.prop_decidable, rw [←is_cyclotomic_extension.adjoin_roots_cyclotomic_eq_adjoin_root_cyclotomic h, is_cyclotomic_extension.adjoin_roots_cyclotomic_eq_adjoin_nth_roots h], simp [cyclotomic_ring] end end cyclotomic_ring end cyclotomic_ring end is_domain section is_alg_closed variables [is_alg_closed K] /-- Algebraically closed fields are `S`-cyclotomic extensions over themselves if `ne_zero ((a : ℕ) : K))` for all `a ∈ S`. -/ lemma is_alg_closed.is_cyclotomic_extension (h : ∀ a ∈ S, ne_zero ((a : ℕ) : K)) : is_cyclotomic_extension S K K := begin refine ⟨λ a ha, _, algebra.eq_top_iff.mp $ subsingleton.elim _ _ ⟩, obtain ⟨r, hr⟩ := is_alg_closed.exists_aeval_eq_zero K _ (degree_cyclotomic_pos a K a.pos).ne', refine ⟨r, _⟩, haveI := h a ha, rwa [coe_aeval_eq_eval, ← is_root.def, is_root_cyclotomic_iff] at hr, end instance is_alg_closed_of_char_zero.is_cyclotomic_extension [char_zero K] : ∀ S, is_cyclotomic_extension S K K := λ S, is_alg_closed.is_cyclotomic_extension S K (λ a ha, infer_instance) end is_alg_closed
91fe4fa1143ceab7808b59f479613c9d188c84c3
00c000939652bc85fffcfe8ba5dd194580a13c4b
/src/mvpfunctor/W.lean
2ae4cf587242ba9087720a7fabe22472e97dfa4f
[ "Apache-2.0" ]
permissive
johoelzl/qpf
a795220c4e872014a62126800313b74ba3b06680
d93ab1fb41d085e49ae476fa364535f40388f44d
refs/heads/master
1,587,372,400,745
1,548,633,467,000
1,548,633,467,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,342
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad The W construction as a multivariate polynomial functor. -/ import .basic universe u namespace mvpfunctor open typevec variables {n : ℕ} (P : mvpfunctor.{u} (n+1)) def drop : mvpfunctor n := { A := P.A, B := λ a, (P.B a).drop } def last : pfunctor := { A := P.A, B := λ a, (P.B a).last } def append_contents {α : typevec n} {β : Type*} {a : P.A} (f' : P.drop.B a ⟹ α) (f : P.last.B a → β) : P.B a ⟹ α.append1 β := append_fun f' f ⊚ to_append1_drop_last def contents_dest_left {α : typevec n} {β : Type*} {a : P.A} (f'f : P.B a ⟹ α.append1 β) : P.drop.B a ⟹ α := from_drop_append ⊚ drop_fun f'f def contents_dest_right {α : typevec n} {β : Type*} {a : P.A} (f'f : P.B a ⟹ α.append1 β) : P.last.B a → β := from_last_append ∘ last_fun f'f theorem contents_dest_left_append_contents {α : typevec n} {β : Type*} {a : P.A} (f' : P.drop.B a ⟹ α) (f : P.last.B a → β) : P.contents_dest_left (P.append_contents f' f) = f' := begin rw [contents_dest_left, append_contents, drop_fun_comp, drop_fun_append_fun], rw [drop_fun_to_append1_drop_last], simp only [typevec.comp, from_drop_append_to_drop_append] end theorem contents_dest_right_append_contents {α : typevec n} {β : Type*} {a : P.A} (f' : P.drop.B a ⟹ α) (f : P.last.B a → β) : P.contents_dest_right (P.append_contents f' f) = f := begin rw [contents_dest_right, append_contents, last_fun_comp, last_fun_append_fun], rw [last_fun_to_append1_drop_last], simp only [function.comp, from_last_append_to_last_append] end theorem append_contents_eta {α : typevec n} {β : Type*} {a : P.A} (f : P.B a ⟹ α.append1 β) : append_contents P (contents_dest_left P f) (contents_dest_right P f) = f := by rw [append_contents, contents_dest_left, contents_dest_right, append_fun_aux] theorem append_fun_comp_append_contents {α γ : typevec n} {β δ : Type*} {a : P.A} (g' : α ⟹ γ) (g : β → δ) (f' : P.drop.B a ⟹ α) (f : P.last.B a → β) : append_fun g' g ⊚ P.append_contents f' f = P.append_contents (g' ⊚ f') (g ∘ f) := by rw [append_contents, append_contents, append_fun_comp, comp_assoc] /- defines a typevec of labels to assign to each node of P.last.W -/ inductive W_path : P.last.W → fin n → Type u | root (a : P.A) (f : P.last.B a → P.last.W) (i : fin n) (c : P.drop.B a i) : W_path ⟨a, f⟩ i | child (a : P.A) (f : P.last.B a → P.last.W) (i : fin n) (j : P.last.B a) (c : W_path (f j) i) : W_path ⟨a, f⟩ i def W_path_cases_on {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) : P.W_path ⟨a, f⟩ ⟹ α := begin intros i x, cases x, case W_path.root : c { exact g' i c }, case W_path.child : j c { exact g j i c} end def W_path_dest_left {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.W_path ⟨a, f⟩ ⟹ α) : P.drop.B a ⟹ α := λ i c, h i (W_path.root a f i c) def W_path_dest_right {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.W_path ⟨a, f⟩ ⟹ α) : Π j : P.last.B a, P.W_path (f j) ⟹ α := λ j i c, h i (W_path.child a f i j c) theorem W_path_dest_left_W_path_cases_on {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) : P.W_path_dest_left (P.W_path_cases_on g' g) = g' := rfl theorem W_path_dest_right_W_path_cases_on {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) : P.W_path_dest_right (P.W_path_cases_on g' g) = g := rfl theorem W_path_cases_on_eta {α : typevec n} {a : P.A} {f : P.last.B a → P.last.W} (h : P.W_path ⟨a, f⟩ ⟹ α) : P.W_path_cases_on (P.W_path_dest_left h) (P.W_path_dest_right h) = h := by ext i x; cases x; reflexivity theorem comp_W_path_cases_on {α β : typevec n} (h : α ⟹ β) {a : P.A} {f : P.last.B a → P.last.W} (g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) : h ⊚ P.W_path_cases_on g' g = P.W_path_cases_on (h ⊚ g') (λ i, h ⊚ g i) := by ext i x; cases x; reflexivity def Wp : mvpfunctor n := { A := P.last.W, B := P.W_path } def W (α : typevec n) : Type* := P.Wp.apply α instance mvfunctor_W : mvfunctor P.W := by delta W; apply_instance /- First, describe operations on `W` as a polynomial functor. -/ def Wp_mk {α : typevec n} (a : P.A) (f : P.last.B a → P.last.W) (f' : P.W_path ⟨a, f⟩ ⟹ α) : P.W α := ⟨⟨a, f⟩, f'⟩ def Wp_rec {α : typevec n} {C : Type*} (g : Π (a : P.A) (f : P.last.B a → P.last.W), (P.W_path ⟨a, f⟩ ⟹ α) → (P.last.B a → C) → C) : Π (x : P.last.W) (f' : P.W_path x ⟹ α), C | ⟨a, f⟩ f' := g a f f' (λ i, Wp_rec (f i) (P.W_path_dest_right f' i)) theorem Wp_rec_eq {α : typevec n} {C : Type*} (g : Π (a : P.A) (f : P.last.B a → P.last.W), (P.W_path ⟨a, f⟩ ⟹ α) → (P.last.B a → C) → C) (a : P.A) (f : P.last.B a → P.last.W) (f' : P.W_path ⟨a, f⟩ ⟹ α) : P.Wp_rec g ⟨a, f⟩ f' = g a f f' (λ i, P.Wp_rec g (f i) (P.W_path_dest_right f' i)) := rfl -- Note: we could replace Prop by Type* and obtain a dependent recursor theorem Wp_ind {α : typevec n} {C : Π x : P.last.W, P.W_path x ⟹ α → Prop} (ih : ∀ (a : P.A) (f : P.last.B a → P.last.W) (f' : P.W_path ⟨a, f⟩ ⟹ α), (∀ i : P.last.B a, C (f i) (P.W_path_dest_right f' i)) → C ⟨a, f⟩ f') : Π (x : P.last.W) (f' : P.W_path x ⟹ α), C x f' | ⟨a, f⟩ f' := ih a f f' (λ i, Wp_ind _ _) /- Now think of W as defined inductively by the data ⟨a, f', f⟩ where - `a : P.A` is the shape of the top node - `f' : P.drop.B a ⟹ α` is the contents of the top node - `f : P.last.B a → P.last.W` are the subtrees -/ def W_mk {α : typevec n} (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) : P.W α := let g : P.last.B a → P.last.W := λ i, (f i).fst, g' : P.W_path ⟨a, g⟩ ⟹ α := P.W_path_cases_on f' (λ i, (f i).snd) in ⟨⟨a, g⟩, g'⟩ def W_rec {α : typevec n} {C : Type*} (g : Π a : P.A, ((P.drop).B a ⟹ α) → ((P.last).B a → P.W α) → ((P.last).B a → C) → C) : P.W α → C | ⟨a, f'⟩ := let g' (a : P.A) (f : P.last.B a → P.last.W) (h : P.W_path ⟨a, f⟩ ⟹ α) (h' : P.last.B a → C) : C := g a (P.W_path_dest_left h) (λ i, ⟨f i, P.W_path_dest_right h i⟩) h' in P.Wp_rec g' a f' theorem W_rec_eq {α : typevec n} {C : Type*} (g : Π a : P.A, ((P.drop).B a ⟹ α) → ((P.last).B a → P.W α) → ((P.last).B a → C) → C) (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) : P.W_rec g (P.W_mk a f' f) = g a f' f (λ i, P.W_rec g (f i)) := begin rw [W_mk, W_rec], dsimp, rw [Wp_rec_eq], dsimp only [W_path_dest_left_W_path_cases_on, W_path_dest_right_W_path_cases_on], congr; ext1 i; cases (f i); refl end theorem W_ind {α : typevec n} {C : P.W α → Prop} (ih : ∀ (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α), (∀ i, C (f i)) → C (P.W_mk a f' f)) : ∀ x, C x := begin intro x, cases x with a f, apply @Wp_ind n P α (λ a f, C ⟨a, f⟩), dsimp, intros a f f' ih', dsimp [W_mk] at ih, let ih'' := ih a (P.W_path_dest_left f') (λ i, ⟨f i, P.W_path_dest_right f' i⟩), dsimp at ih'', rw W_path_cases_on_eta at ih'', apply ih'', apply ih' end theorem W_cases {α : typevec n} {C : P.W α → Prop} (ih : ∀ (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α), C (P.W_mk a f' f)) : ∀ x, C x := P.W_ind (λ a f' f ih', ih a f' f) def W_map {α β : typevec n} (g : α ⟹ β) : P.W α → P.W β := λ x, g <$$> x theorem W_mk_eq {α : typevec n} (a : P.A) (f : P.last.B a → P.last.W) (g' : P.drop.B a ⟹ α) (g : Π j : P.last.B a, P.W_path (f j) ⟹ α) : P.W_mk a g' (λ i, ⟨f i, g i⟩) = ⟨⟨a, f⟩, P.W_path_cases_on g' g⟩ := rfl theorem W_map_W_mk {α β : typevec n} (g : α ⟹ β) (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) : g <$$> P.W_mk a f' f = P.W_mk a (g ⊚ f') (λ i, g <$$> f i) := begin show _ = P.W_mk a (g ⊚ f') (mvfunctor.map g ∘ f), have : mvfunctor.map g ∘ f = λ i, ⟨(f i).fst, g ⊚ ((f i).snd)⟩, { ext i, dsimp [function.comp], cases (f i), refl }, rw this, have : f = λ i, ⟨(f i).fst, (f i).snd⟩, { ext1, cases (f x), refl }, rw this, dsimp, rw [W_mk_eq, W_mk_eq], have h := mvpfunctor.map_eq P.Wp g, rw [h, comp_W_path_cases_on] end -- TODO: this technical theorem is used in one place below. Can it be avoided? @[reducible] def apply_append1 {α : typevec n} {β : Type*} (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → β) : P.apply (append1 α β) := ⟨a, P.append_contents f' f⟩ theorem map_apply_append1 {α γ : typevec n} (g : α ⟹ γ) (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) : append_fun g (P.W_map g) <$$> P.apply_append1 a f' f = P.apply_append1 a (g ⊚ f') (λ x, P.W_map g (f x)) := begin rw [apply_append1, apply_append1, append_contents, append_contents, map_eq, append_fun_comp], reflexivity end /- Yet another view of the W type: as a fixed point for a multivariate polynomial functor. These are needed to use the W-construction to construct a fixed point of a qpf, since the qpf axioms are expressed in terms of `map` on `P`. -/ def W_mk' {α : typevec n} : P.apply (α.append1 (P.W α)) → P.W α | ⟨a, f⟩ := P.W_mk a (P.contents_dest_left f) (P.contents_dest_right f) def W_dest' {α : typevec n} : P.W α → P.apply (α.append1 (P.W α)) := P.W_rec (λ a f' f _, ⟨a, P.append_contents f' f⟩) theorem W_dest'_W_mk {α : typevec n} (a : P.A) (f' : P.drop.B a ⟹ α) (f : P.last.B a → P.W α) : P.W_dest' (P.W_mk a f' f) = ⟨a, P.append_contents f' f⟩ := by rw [W_dest', W_rec_eq] theorem W_dest'_W_mk' {α : typevec n} (x : P.apply (α.append1 (P.W α))) : P.W_dest' (P.W_mk' x) = x := by cases x with a f; rw [W_mk', W_dest'_W_mk, append_contents_eta] end mvpfunctor
bd19ff38dfa0cc8e9b8fcdd117535bb5862b5ac8
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/init/meta/smt/smt_tactic.lean
4e693bf44d1af496f4f8fc01d561fa8bcb05c99a
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,199
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.simp_tactic import init.meta.smt.congruence_closure import init.meta.smt.ematch universe u run_command mk_simp_attr `pre_smt run_command mk_hinst_lemma_attr_set `ematch [] [`ematch_lhs] /-- Configuration for the smt tactic preprocessor. The preprocessor is applied whenever a new hypothesis is introduced. - simp_attr: is the attribute name for the simplification lemmas that are used during the preprocessing step. - max_steps: it is the maximum number of steps performed by the simplifier. - zeta: if tt, then zeta reduction (i.e., unfolding let-expressions) is used during preprocessing. -/ structure smt_pre_config := (simp_attr : name := `pre_smt) (max_steps : nat := 1000000) (zeta : bool := ff) /-- Configuration for the smt_state object. - em_attr: is the attribute name for the hinst_lemmas that are used for ematching -/ structure smt_config := (cc_cfg : cc_config := {}) (em_cfg : ematch_config := {}) (pre_cfg : smt_pre_config := {}) (em_attr : name := `ematch) meta def smt_config.set_classical (c : smt_config) (b : bool) : smt_config := {c with cc_cfg := { (c^.cc_cfg) with em := b}} meta constant smt_goal : Type meta def smt_state := list smt_goal meta constant smt_state.mk : smt_config → tactic smt_state meta constant smt_state.to_format : smt_state → tactic_state → format /-- Return tt iff classical excluded middle was enabled at smt_state.mk -/ meta constant smt_state.classical : smt_state → bool meta def smt_tactic := state_t smt_state tactic meta instance : has_append smt_state := list.has_append meta instance : monad smt_tactic := state_t.monad _ _ /- We don't use the default state_t lift operation because only tactics that do not change hypotheses can be automatically lifted to smt_tactic. -/ meta constant tactic_to_smt_tactic (α : Type) : tactic α → smt_tactic α meta instance : monad.has_monad_lift tactic smt_tactic := ⟨tactic_to_smt_tactic⟩ meta instance (α : Type) : has_coe (tactic α) (smt_tactic α) := ⟨monad.monad_lift⟩ meta def smt_tactic_orelse {α : Type} (t₁ t₂ : smt_tactic α) : smt_tactic α := λ ss ts, result.cases_on (t₁ ss ts) result.success (λ e₁ ref₁ s', result.cases_on (t₂ ss ts) result.success result.exception) meta instance : monad_fail smt_tactic := { smt_tactic.monad with fail := λ α s, (tactic.fail (to_fmt s) : smt_tactic α) } meta instance : alternative smt_tactic := {failure := λ α, @tactic.failed α, orelse := @smt_tactic_orelse, pure := @return _ _, seq := @fapp _ _, map := @fmap _ _} namespace smt_tactic open tactic (transparency) meta constant intros : smt_tactic unit meta constant intron : nat → smt_tactic unit meta constant intro_lst : list name → smt_tactic unit /-- Try to close main goal by using equalities implied by the congruence closure module. -/ meta constant close : smt_tactic unit /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from lemmas in the main goal with terms in the main goal. The set of lemmas is populated with theorems tagged with the attribute specified at smt_config.em_attr, and lemmas added using tactics such as `smt_tactic.add_lemmas`. The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`. Remark: the given predicate is applied to every new instance. The instance is only added to the state if the predicate returns tt. -/ meta constant ematch_core : (expr → bool) → smt_tactic unit /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from the given lemmas with terms in the main goal. -/ meta constant ematch_using : hinst_lemmas → smt_tactic unit meta constant mk_ematch_eqn_lemmas_for_core : transparency → name → smt_tactic hinst_lemmas meta constant to_cc_state : smt_tactic cc_state meta constant to_em_state : smt_tactic ematch_state meta constant get_config : smt_tactic smt_config /-- Preprocess the given term using the same simplifications rules used when we introduce a new hypothesis. The result is pair containing the resulting term and a proof that it is equal to the given one. -/ meta constant preprocess : expr → smt_tactic (expr × expr) meta constant get_lemmas : smt_tactic hinst_lemmas meta constant set_lemmas : hinst_lemmas → smt_tactic unit meta constant add_lemmas : hinst_lemmas → smt_tactic unit meta def add_ematch_lemma_core (md : transparency) (as_simp : bool) (e : expr) : smt_tactic unit := do h ← hinst_lemma.mk_core md e as_simp, add_lemmas (mk_hinst_singleton h) meta def add_ematch_lemma_from_decl_core (md : transparency) (as_simp : bool) (n : name) : smt_tactic unit := do h ← hinst_lemma.mk_from_decl_core md n as_simp, add_lemmas (mk_hinst_singleton h) meta def add_ematch_eqn_lemmas_for_core (md : transparency) (n : name) : smt_tactic unit := do hs ← mk_ematch_eqn_lemmas_for_core md n, add_lemmas hs meta def ematch : smt_tactic unit := ematch_core (λ _, tt) meta def failed {α} : smt_tactic α := tactic.failed meta def fail {α : Type} {β : Type u} [has_to_format β] (msg : β) : tactic α := tactic.fail msg meta def try {α : Type} (t : smt_tactic α) : smt_tactic unit := λ ss ts, result.cases_on (t ss ts) (λ ⟨a, new_ss⟩, result.success ((), new_ss)) (λ e ref s', result.success ((), ss) ts) /- (repeat_at_most n t): repeat the given tactic at most n times or until t fails -/ meta def repeat_at_most : nat → smt_tactic unit → smt_tactic unit | 0 t := return () | (n+1) t := (do t, repeat_at_most n t) <|> return () /-- (repeat_exactly n t) : execute t n times -/ meta def repeat_exactly : nat → smt_tactic unit → smt_tactic unit | 0 t := return () | (n+1) t := do t, repeat_exactly n t meta def repeat : smt_tactic unit → smt_tactic unit := repeat_at_most 100000 meta def eblast : smt_tactic unit := repeat (ematch >> try close) open tactic protected meta def read : smt_tactic (smt_state × tactic_state) := do s₁ ← state_t.read, s₂ ← tactic.read, return (s₁, s₂) protected meta def write : smt_state × tactic_state → smt_tactic unit := λ ⟨ss, ts⟩ _ _, result.success ((), ss) ts private meta def mk_smt_goals_for (cfg : smt_config) : list expr → list smt_goal → list expr → tactic (list smt_goal × list expr) | [] sr tr := return (sr^.reverse, tr^.reverse) | (tg::tgs) sr tr := do tactic.set_goals [tg], [new_sg] ← smt_state.mk cfg | tactic.failed, [new_tg] ← get_goals | tactic.failed, mk_smt_goals_for tgs (new_sg::sr) (new_tg::tr) /- See slift -/ meta def slift_aux {α : Type} (t : tactic α) (cfg : smt_config) : smt_tactic α := λ ss, do _::sgs ← return ss | fail "slift tactic failed, there no smt goals to be solved", tg::tgs ← tactic.get_goals | tactic.failed, tactic.set_goals [tg], a ← t, new_tgs ← tactic.get_goals, (new_sgs, new_tgs) ← mk_smt_goals_for cfg new_tgs [] [], tactic.set_goals (new_tgs ++ tgs), return (a, new_sgs ++ sgs) /-- This lift operation will restart the SMT state. It is useful for using tactics that change the set of hypotheses. -/ meta def slift {α : Type} (t : tactic α) : smt_tactic α := get_config >>= slift_aux t meta def trace_state : smt_tactic unit := do (s₁, s₂) ← smt_tactic.read, trace (smt_state.to_format s₁ s₂) meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit := tactic.trace a meta def to_expr (q : pexpr) (allow_mvars := tt) (report_errors := ff) : smt_tactic expr := tactic.to_expr q allow_mvars report_errors meta def classical : smt_tactic bool := do s ← state_t.read, return s^.classical meta def num_goals : smt_tactic nat := λ ss, return (ss^.length, ss) /- Low level primitives for managing set of goals -/ meta def get_goals : smt_tactic (list smt_goal × list expr) := do (g₁, _) ← smt_tactic.read, g₂ ← tactic.get_goals, return (g₁, g₂) meta def set_goals : list smt_goal → list expr → smt_tactic unit := λ g₁ g₂ ss, tactic.set_goals g₂ >> return ((), g₁) private meta def all_goals_core (tac : smt_tactic unit) : list smt_goal → list expr → list smt_goal → list expr → smt_tactic unit | [] ts acs act := set_goals acs (ts ++ act) | (s :: ss) [] acs act := fail "ill-formed smt_state" | (s :: ss) (t :: ts) acs act := do set_goals [s] [t], tac, (new_ss, new_ts) ← get_goals, all_goals_core ss ts (acs ++ new_ss) (act ++ new_ts) /- Apply the given tactic to all goals. -/ meta def all_goals (tac : smt_tactic unit) : smt_tactic unit := do (ss, ts) ← get_goals, all_goals_core tac ss ts [] [] /- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/ meta def seq (tac1 : smt_tactic unit) (tac2 : smt_tactic unit) : smt_tactic unit := do (s::ss, t::ts) ← get_goals, set_goals [s] [t], tac1, all_goals tac2, (new_ss, new_ts) ← get_goals, set_goals (new_ss ++ ss) (new_ts ++ ts) meta instance : has_andthen (smt_tactic unit) := ⟨seq⟩ meta def focus1 {α} (tac : smt_tactic α) : smt_tactic α := do (s::ss, t::ts) ← get_goals, match ss with | [] := tac | _ := do set_goals [s] [t], a ← tac, (ss', ts') ← get_goals, set_goals (ss' ++ ss) (ts' ++ ts), return a end meta def solve1 (tac : smt_tactic unit) : smt_tactic unit := do (ss, gs) ← get_goals, match ss, gs with | [], _ := fail "solve1 tactic failed, there isn't any goal left to focus" | _, [] := fail "solve1 tactic failed, there isn't any smt goal left to focus" | s::ss, g::gs := do set_goals [s] [g], tac, (ss', gs') ← get_goals, match ss', gs' with | [], [] := set_goals ss gs | _, _ := fail "solve1 tactic failed, focused goal has not been solved" end end meta def swap : smt_tactic unit := do (ss, ts) ← get_goals, match ss, ts with | (s₁ :: s₂ :: ss), (t₁ :: t₂ :: ts) := set_goals (s₂ :: s₁ :: ss) (t₂ :: t₁ :: ts) | _, _ := failed end /-- Add a new goal for t, and the hypothesis (h : t) in the current goal. -/ meta def assert (h : name) (t : expr) : smt_tactic unit := tactic.assert_core h t >> swap >> intros >> swap >> try close /-- Add the hypothesis (h : t) in the current goal if v has type t. -/ meta def assertv (h : name) (t : expr) (v : expr) : smt_tactic unit := tactic.assertv_core h t v >> intros >> return () /-- Add a new goal for t, and the hypothesis (h : t := ?M) in the current goal. -/ meta def define (h : name) (t : expr) : smt_tactic unit := tactic.define_core h t >> swap >> intros >> swap >> try close /-- Add the hypothesis (h : t := v) in the current goal if v has type t. -/ meta def definev (h : name) (t : expr) (v : expr) : smt_tactic unit := tactic.definev_core h t v >> intros >> return () /-- Add (h : t := pr) to the current goal -/ meta def pose (h : name) (pr : expr) : smt_tactic unit := do t ← tactic.infer_type pr, definev h t pr /- Add (h : t) to the current goal, given a proof (pr : t) -/ meta def note (n : name) (pr : expr) : smt_tactic unit := do t ← tactic.infer_type pr, assertv n t pr meta def destruct (e : expr) : smt_tactic unit := smt_tactic.seq (tactic.destruct e) smt_tactic.intros meta def by_cases (e : expr) : smt_tactic unit := do c ← classical, if c then destruct (expr.app (expr.const `classical.em []) e) else do dec_e ← (mk_app `decidable [e] <|> fail "by_cases smt_tactic failed, type is not a proposition"), inst ← (mk_instance dec_e <|> fail "by_cases smt_tactic failed, type of given expression is not decidable"), em ← mk_app `decidable.em [e, inst], destruct em meta def by_contradiction : smt_tactic unit := do t ← target, c ← classical, if t^.is_false then skip else if c then do apply (expr.app (expr.const `classical.by_contradiction []) t), intros else do dec_t ← (mk_app `decidable [t] <|> fail "by_contradiction smt_tactic failed, target is not a proposition"), inst ← (mk_instance dec_t <|> fail "by_contradiction smt_tactic failed, target is not decidable"), a ← mk_mapp `decidable.by_contradiction [some t, some inst], apply a, intros /- Return a proof for e, if 'e' is a known fact in the main goal. -/ meta def proof_for (e : expr) : smt_tactic expr := do cc ← to_cc_state, cc^.proof_for e /- Return a refutation for e (i.e., a proof for (not e)), if 'e' has been refuted in the main goal. -/ meta def refutation_for (e : expr) : smt_tactic expr := do cc ← to_cc_state, cc^.refutation_for e meta def get_facts : smt_tactic (list expr) := do cc ← to_cc_state, return $ cc^.eqc_of expr.mk_true meta def get_refuted_facts : smt_tactic (list expr) := do cc ← to_cc_state, return $ cc^.eqc_of expr.mk_false meta def add_ematch_lemma : expr → smt_tactic unit := add_ematch_lemma_core reducible ff meta def add_ematch_lhs_lemma : expr → smt_tactic unit := add_ematch_lemma_core reducible tt meta def add_ematch_lemma_from_decl : name → smt_tactic unit := add_ematch_lemma_from_decl_core reducible ff meta def add_ematch_lhs_lemma_from_decl : name → smt_tactic unit := add_ematch_lemma_from_decl_core reducible ff meta def add_ematch_eqn_lemmas_for : name → smt_tactic unit := add_ematch_eqn_lemmas_for_core reducible meta def add_lemmas_from_facts_core : list expr → smt_tactic unit | [] := return () | (f::fs) := do try (is_prop f >> guard (f^.is_pi && bnot (f^.is_arrow)) >> proof_for f >>= add_ematch_lemma_core reducible ff), add_lemmas_from_facts_core fs meta def add_lemmas_from_facts : smt_tactic unit := get_facts >>= add_lemmas_from_facts_core meta def induction (e : expr) (ids : list name := []) (rec : option name := none) : smt_tactic unit := slift (tactic.induction e ids rec >> return ()) -- pass on the information? meta def when (c : Prop) [decidable c] (tac : smt_tactic unit) : smt_tactic unit := if c then tac else skip meta def when_tracing (n : name) (tac : smt_tactic unit) : smt_tactic unit := when (is_trace_enabled_for n = tt) tac end smt_tactic open smt_tactic meta def using_smt {α} (t : smt_tactic α) (cfg : smt_config := {}) : tactic α := do ss ← smt_state.mk cfg, (a, _) ← (do a ← t, repeat close, return a) ss, return a meta def using_smt_with {α} (cfg : smt_config) (t : smt_tactic α) : tactic α := using_smt t cfg
4c06732214dda4ee114b9e83a3a10805e512a0ff
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/converter.lean
75cb8651cce5679e30f4ac6245290c943cb6bd8a
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,684
lean
open tactic conv open tactic run_command mk_simp_attr `foo run_command mk_simp_attr `bla constant f : nat → nat → nat @[foo] lemma f_lemma : ∀ x, f x x = 0 := sorry constant g : nat → nat @[bla] lemma g_lemma : ∀ x, g x = x := sorry example (a b c : nat) : (λ x, g (f (a + 0) (sizeof x))) a = 0 := by conversion $ whnf >> trace_lhs >> apply_simp_set `bla >> dsimp >> trace "after defeq simplifier" >> trace_lhs >> change `(f a a) >> trace_lhs >> apply_simp_set `foo >> trace_lhs set_option trace.app_builder true attribute [simp] sizeof_nat_eq example (a b c : nat) : (λ x, g (f x (sizeof x))) = (λ x, 0) := by conversion $ funext $ do trace_lhs, apply_simp_set `bla, dsimp, apply_simp_set `foo constant h : nat → nat → nat lemma ex (a : nat) : (λ a, h (f a (sizeof a)) (g a)) = (λ a, h 0 a) := by conversion $ bottom_up $ (apply_simp_set `foo <|> apply_simp_set `bla <|> dsimp) lemma ex2 {A : Type} [comm_group A] (a b : A) : b * 1 * a = a * b := by conversion $ bottom_up (apply_simp_set `default) lemma ex3 (p q r : Prop) : (p ∧ true ∧ p) = p := by conversion $ bottom_up (apply_propext_simp_set `default) print "---------" lemma ex4 (a b c : nat) : g (g (g (f (f (g (g a)) (g (g a))) a))) = g (g (g (f (f a a) a))) := by conversion $ findp `(λ x, f (g x) (g x)) $ trace "found pattern" >> trace_lhs >> bottom_up (apply_simp_set `bla) lemma ex5 (a b c : nat) : g (g (g (f (f (g (g a)) (g (g a))) a))) = g (g (g (f (f a a) a))) := by conversion $ find $ match_expr `(λ x, f (g x) (g x)) >> trace "found pattern" >> trace_lhs >> bottom_up (apply_simp_set `bla)
10c18a85853a2cf1cf220058a323b6681974f580
9dc8cecdf3c4634764a18254e94d43da07142918
/src/tactic/lint/type_classes.lean
db71eccca7d85162d91c7a4c0a28a3d7a635a6ec
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
24,729
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import data.bool.basic import meta.rb_map import tactic.lint.basic /-! # Linters about type classes This file defines several linters checking the correct usage of type classes and the appropriate definition of instances: * `instance_priority` ensures that blanket instances have low priority. * `has_nonempty_instances` checks that every type has a `nonempty` instance, an `inhabited` instance, or a `unique` instance. * `impossible_instance` checks that there are no instances which can never apply. * `incorrect_type_class_argument` checks that only type classes are used in instance-implicit arguments. * `dangerous_instance` checks for instances that generate subproblems with metavariables. * `fails_quickly` checks that type class resolution finishes quickly. * `class_structure` checks that every `class` is a structure, i.e. `@[class] def` is forbidden. * `has_coe_variable` checks that there is no instance of type `has_coe α t`. * `inhabited_nonempty` checks whether `[inhabited α]` arguments could be generalized to `[nonempty α]`. * `decidable_classical` checks propositions for `[decidable_... p]` hypotheses that are not used in the statement, and could thus be removed by using `classical` in the proof. * `linter.has_coe_to_fun` checks whether necessary `has_coe_to_fun` instances are declared. * `linter.check_reducibility` checks whether non-instances with a class as type are reducible. -/ open tactic /-- Pretty prints a list of arguments of a declaration. Assumes `l` is a list of argument positions and binders (or any other element that can be pretty printed). `l` can be obtained e.g. by applying `list.indexes_values` to a list obtained by `get_pi_binders`. -/ meta def print_arguments {α} [has_to_tactic_format α] (l : list (ℕ × α)) : tactic string := do fs ← l.mmap (λ ⟨n, b⟩, (λ s, to_fmt "argument " ++ to_fmt (n+1) ++ ": " ++ s) <$> pp b), return $ fs.to_string_aux tt /-- checks whether an instance that always applies has priority ≥ 1000. -/ private meta def instance_priority (d : declaration) : tactic (option string) := do let nm := d.to_name, b ← is_instance nm, /- return `none` if `d` is not an instance -/ if ¬ b then return none else do (is_persistent, prio) ← has_attribute `instance nm, /- return `none` if `d` is has low priority -/ if prio < 1000 then return none else do (_, tp) ← open_pis d.type, tp ← whnf tp transparency.none, let (fn, args) := tp.get_app_fn_args, cls ← get_decl fn.const_name, let (pi_args, _) := cls.type.pi_binders, guard (args.length = pi_args.length), /- List all the arguments of the class that block type-class inference from firing (if they are metavariables). These are all the arguments except instance-arguments and out-params. -/ let relevant_args := (args.zip pi_args).filter_map $ λ⟨e, ⟨_, info, tp⟩⟩, if info = binder_info.inst_implicit ∨ tp.get_app_fn.is_constant_of `out_param then none else some e, let always_applies := relevant_args.all expr.is_local_constant ∧ relevant_args.nodup, if always_applies then return $ some "set priority below 1000" else return none /-- There are places where typeclass arguments are specified with implicit `{}` brackets instead of the usual `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type of one of the other arguments. When they can be inferred from these other arguments, it is faster to use this method than to use type class inference. For example, when writing lemmas about `(f : α →+* β)`, it is faster to specify the fact that `α` and `β` are `semiring`s as `{rα : semiring α} {rβ : semiring β}` rather than the usual `[semiring α] [semiring β]`. -/ library_note "implicit instance arguments" /-- Certain instances always apply during type-class resolution. For example, the instance `add_comm_group.to_add_group {α} [add_comm_group α] : add_group α` applies to all type-class resolution problems of the form `add_group _`, and type-class inference will then do an exhaustive search to find a commutative group. These instances take a long time to fail. Other instances will only apply if the goal has a certain shape. For example `int.add_group : add_group ℤ` or `add_group.prod {α β} [add_group α] [add_group β] : add_group (α × β)`. Usually these instances will fail quickly, and when they apply, they are almost always the desired instance. For this reason, we want the instances of the second type (that only apply in specific cases) to always have higher priority than the instances of the first type (that always apply). See also #1561. Therefore, if we create an instance that always applies, we set the priority of these instances to 100 (or something similar, which is below the default value of 1000). -/ library_note "lower instance priority" /-- A linter object for checking instance priorities of instances that always apply. This is in the default linter set. -/ @[linter] meta def linter.instance_priority : linter := { test := instance_priority, no_errors_found := "All instance priorities are good.", errors_found := "DANGEROUS INSTANCE PRIORITIES. The following instances always apply, and therefore should have a priority < 1000. If you don't know what priority to choose, use priority 100. See note [lower instance priority] for instructions to change the priority.", auto_decls := tt } /-- Reports declarations of types that do not have an nonemptiness instance. A `nonempty`, `inhabited` or `unique` instance suffices, and we prefer a computable `inhabited` or `unique` instance if possible. -/ private meta def has_nonempty_instance (d : declaration) : tactic (option string) := do tt ← pure d.is_trusted | pure none, ff ← has_attribute' `reducible d.to_name | pure none, ff ← has_attribute' `class d.to_name | pure none, (_, ty) ← open_pis d.type, ty ← whnf ty, if ty = `(Prop) then pure none else do `(Sort _) ← whnf ty | pure none, insts ← attribute.get_instances `instance, insts_tys ← insts.mmap $ λ i, expr.pi_codomain <$> declaration.type <$> get_decl i, let nonempty_insts := insts_tys.filter (λ i, i.app_fn.const_name ∈ [``nonempty, ``inhabited, `unique]), let nonempty_tys := nonempty_insts.map (λ i, i.app_arg.get_app_fn.const_name), if d.to_name ∈ nonempty_tys then pure none else pure "nonempty/inhabited/unique instance missing" /-- A linter for missing `nonempty` instances. -/ @[linter] meta def linter.has_nonempty_instance : linter := { test := has_nonempty_instance, auto_decls := ff, no_errors_found := "No types have missing nonempty instances.", errors_found := "TYPES ARE MISSING NONEMPTY INSTANCES. The following types should have an associated instance of the class `nonempty`, or if computably possible `inhabited` or `unique`:", is_fast := ff } attribute [nolint has_nonempty_instance] pempty /-- Checks whether an instance can never be applied. -/ private meta def impossible_instance (d : declaration) : tactic (option string) := do tt ← is_instance d.to_name | return none, (binders, _) ← get_pi_binders_nondep d.type, let bad_arguments := binders.filter $ λ nb, nb.2.info ≠ binder_info.inst_implicit, _ :: _ ← return bad_arguments | return none, (λ s, some $ "Impossible to infer " ++ s) <$> print_arguments bad_arguments /-- A linter object for `impossible_instance`. -/ @[linter] meta def linter.impossible_instance : linter := { test := impossible_instance, auto_decls := tt, no_errors_found := "All instances are applicable.", errors_found := "IMPOSSIBLE INSTANCES FOUND. These instances have an argument that cannot be found during type-class resolution, and " ++ "therefore can never succeed. Either mark the arguments with square brackets (if it is a " ++ "class), or don't make it an instance." } /-- Checks whether an instance can never be applied. -/ private meta def incorrect_type_class_argument (d : declaration) : tactic (option string) := do (binders, _) ← get_pi_binders d.type, let instance_arguments := binders.indexes_values $ λ b : binder, b.info = binder_info.inst_implicit, /- the head of the type should either unfold to a class, or be a local constant. A local constant is allowed, because that could be a class when applied to the proper arguments. -/ bad_arguments ← instance_arguments.mfilter (λ ⟨_, b⟩, do (_, head) ← open_pis b.type, if head.get_app_fn.is_local_constant then return ff else do bnot <$> is_class head), _ :: _ ← return bad_arguments | return none, (λ s, some $ "These are not classes. " ++ s) <$> print_arguments bad_arguments /-- A linter object for `incorrect_type_class_argument`. -/ @[linter] meta def linter.incorrect_type_class_argument : linter := { test := incorrect_type_class_argument, auto_decls := tt, no_errors_found := "All declarations have correct type-class arguments.", errors_found := "INCORRECT TYPE-CLASS ARGUMENTS. Some declarations have non-classes between [square brackets]:" } /-- Checks whether an instance is dangerous: it creates a new type-class problem with metavariable arguments. -/ private meta def dangerous_instance (d : declaration) : tactic (option string) := do tt ← is_instance d.to_name | return none, (local_constants, target) ← open_pis d.type, let instance_arguments := local_constants.indexes_values $ λ e : expr, e.local_binding_info = binder_info.inst_implicit, let bad_arguments := local_constants.indexes_values $ λ x, !target.has_local_constant x && (x.local_binding_info ≠ binder_info.inst_implicit) && instance_arguments.any (λ nb, nb.2.local_type.has_local_constant x), let bad_arguments : list (ℕ × binder) := bad_arguments.map $ λ ⟨n, e⟩, ⟨n, e.to_binder⟩, _ :: _ ← return bad_arguments | return none, (λ s, some $ "The following arguments become metavariables. " ++ s) <$> print_arguments bad_arguments /-- A linter object for `dangerous_instance`. -/ @[linter] meta def linter.dangerous_instance : linter := { test := dangerous_instance, no_errors_found := "No dangerous instances.", errors_found := "DANGEROUS INSTANCES FOUND.\nThese instances are recursive, and create a new " ++ "type-class problem which will have metavariables. Possible solution: remove the instance attribute or make it a local instance instead. Currently this linter does not check whether the metavariables only occur in arguments marked " ++ "with `out_param`, in which case this linter gives a false positive.", auto_decls := tt } /-- Auxilliary definition for `find_nondep` -/ meta def find_nondep_aux : list expr → expr_set → tactic expr_set | [] r := return r | (h::hs) r := do type ← infer_type h, find_nondep_aux hs $ r.union type.list_local_consts' /-- Finds all hypotheses that don't occur in the target or other hypotheses. -/ meta def find_nondep : tactic (list expr) := do ctx ← local_context, tgt ← target, lconsts ← find_nondep_aux ctx tgt.list_local_consts', return $ ctx.filter $ λ e, !lconsts.contains e /-- Tests whether type-class inference search will end quickly on certain unsolvable type-class problems. This is to detect loops or very slow searches, which are problematic (recall that normal type-class search often creates unsolvable subproblems, which have to fail quickly for type-class inference to perform well. We create these type-class problems by taking an instance, and removing the last hypothesis that doesn't appear in the goal (or a later hypothesis). Note: this argument is necessarily an instance-implicit argument if it passes the `linter.incorrect_type_class_argument`. This tactic succeeds if `mk_instance` succeeds quickly or fails quickly with the error message that it cannot find an instance. It fails if the tactic takes too long, or if any other error message is raised (usually a maximum depth in the search). -/ meta def fails_quickly (max_steps : ℕ) (d : declaration) : tactic (option string) := retrieve $ do tt ← is_instance d.to_name | return none, let e := d.type, g ← mk_meta_var e, set_goals [g], intros, l@(_::_) ← find_nondep | return none, -- if all arguments occur in the goal, this instance is ok clear l.ilast, reset_instance_cache, state ← read, let state_msg := "\nState:\n" ++ to_string state, tgt ← target >>= instantiate_mvars, sum.inr msg ← retrieve_or_report_error $ tactic.try_for max_steps $ mk_instance tgt | return none, /- it's ok if type-class inference can find an instance with fewer hypotheses. This happens a lot for `has_sizeof` and `has_well_founded`, but can also happen if there is a noncomputable instance with fewer assumptions. -/ return $ if "tactic.mk_instance failed to generate instance for".is_prefix_of msg then none else some $ (++ state_msg) $ if msg = "try_for tactic failed, timeout" then "type-class inference timed out" else msg /-- A linter object for `fails_quickly`. We currently set the number of steps in the type-class search pretty high. Some instances take quite some time to fail, and we seem to run against the caching issue in https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/odd.20repeated.20type.20class.20search -/ @[linter] meta def linter.fails_quickly : linter := { test := fails_quickly 20000, auto_decls := tt, no_errors_found := "No type-class searches timed out.", errors_found := "TYPE CLASS SEARCHES TIMED OUT. The following instances are part of a loop, or an excessively long search. It is common that the loop occurs in a different class than the one flagged below, but usually an instance that is part of the loop is also flagged. To debug: (1) run `scripts/mk_all.sh` and create a file with `import all` and `set_option trace.class_instances true` (2) Recreate the state shown in the error message. You can do this easily by copying the type of the instance (the output of `#check @my_instance`), turning this into an example and removing the last argument in square brackets. Prove the example using `by apply_instance`. For example, if `additive.topological_add_group` raises an error, run ``` example {G : Type*} [topological_space G] [group G] : topological_add_group (additive G) := by apply_instance ``` (3) What error do you get? (3a) If the error is \"tactic.mk_instance failed to generate instance\", there might be nothing wrong. But it might take unreasonably long for the type-class inference to fail. Check the trace to see if type-class inference takes any unnecessary long unexpected turns. If not, feel free to increase the value in the definition of the linter `fails_quickly`. (3b) If the error is \"maximum class-instance resolution depth has been reached\" there is almost certainly a loop in the type-class inference. Find which instance causes the type-class inference to go astray, and fix that instance.", is_fast := ff } /-- Checks that all uses of the `@[class]` attribute apply to structures or inductive types. This is future-proofing for lean 4, which no longer supports `@[class] def`. -/ private meta def class_structure (n : name) : tactic (option string) := do is_class ← has_attribute' `class n, if is_class then do env ← get_env, pure $ if env.is_inductive n then none else "is a non-structure or inductive type marked @[class]" else pure none /-- A linter object for `class_structure`. -/ @[linter] meta def linter.class_structure : linter := { test := λ d, class_structure d.to_name, auto_decls := tt, no_errors_found := "All classes are structures.", errors_found := "USE OF @[class] def IS DISALLOWED:" } /-- Tests whether there is no instance of type `has_coe α t` where `α` is a variable, or `has_coe t α` where `α` does not occur in `t`. See note [use has_coe_t]. -/ private meta def has_coe_variable (d : declaration) : tactic (option string) := do tt ← is_instance d.to_name | return none, `(has_coe %%a %%b) ← return d.type.pi_codomain | return none, if a.is_var then return $ some $ "illegal instance, first argument is variable" else if b.is_var ∧ ¬ b.occurs a then return $ some $ "illegal instance, second argument is variable not occurring in first argument" else return none /-- A linter object for `has_coe_variable`. -/ @[linter] meta def linter.has_coe_variable : linter := { test := has_coe_variable, auto_decls := tt, no_errors_found := "No invalid `has_coe` instances.", errors_found := "INVALID `has_coe` INSTANCES. Make the following declarations instances of the class `has_coe_t` instead of `has_coe`." } /-- Checks whether a declaration is prop-valued and takes an `inhabited _` argument that is unused elsewhere in the type. In this case, that argument can be replaced with `nonempty _`. -/ private meta def inhabited_nonempty (d : declaration) : tactic (option string) := do tt ← is_prop d.type | return none, (binders, _) ← get_pi_binders_nondep d.type, let inhd_binders := binders.filter $ λ pr, pr.2.type.is_app_of `inhabited, if inhd_binders.length = 0 then return none else (λ s, some $ "The following `inhabited` instances should be `nonempty`. " ++ s) <$> print_arguments inhd_binders /-- A linter object for `inhabited_nonempty`. -/ @[linter] meta def linter.inhabited_nonempty : linter := { test := inhabited_nonempty, auto_decls := ff, no_errors_found := "No uses of `inhabited` arguments should be replaced with `nonempty`.", errors_found := "USES OF `inhabited` SHOULD BE REPLACED WITH `nonempty`." } /-- Checks whether a declaration is `Prop`-valued and takes a `decidable* _` hypothesis that is unused elsewhere in the type. In this case, that hypothesis can be replaced with `classical` in the proof. Theorems in the `decidable` namespace are exempt from the check. -/ private meta def decidable_classical (d : declaration) : tactic (option string) := do tt ← is_prop d.type | return none, ff ← pure $ (`decidable).is_prefix_of d.to_name | return none, (binders, _) ← get_pi_binders_nondep d.type, let deceq_binders := binders.filter $ λ pr, pr.2.type.is_app_of `decidable_eq ∨ pr.2.type.is_app_of `decidable_pred ∨ pr.2.type.is_app_of `decidable_rel ∨ pr.2.type.is_app_of `decidable, if deceq_binders.length = 0 then return none else (λ s, some $ "The following `decidable` hypotheses should be replaced with `classical` in the proof. " ++ s) <$> print_arguments deceq_binders /-- A linter object for `decidable_classical`. -/ @[linter] meta def linter.decidable_classical : linter := { test := decidable_classical, auto_decls := ff, no_errors_found := "No uses of `decidable` arguments should be replaced with `classical`.", errors_found := "USES OF `decidable` SHOULD BE REPLACED WITH `classical` IN THE PROOF." } /- The file `logic/basic.lean` emphasizes the differences between what holds under classical and non-classical logic. It makes little sense to make all these lemmas classical, so we add them to the list of lemmas which are not checked by the linter `decidable_classical`. -/ attribute [nolint decidable_classical] dec_em dec_em' not.decidable_imp_symm /-- Checks whether a declaration is `Prop`-valued and takes a `fintype _` hypothesis that is unused elsewhere in the type. In this case, that hypothesis can be replaced with `casesI nonempty_fintype _` in the proof. -/ meta def linter.fintype_finite_fun (d : declaration) : tactic (option string) := do tt ← is_prop d.type | return none, (binders, _) ← get_pi_binders_nondep d.type, let fintype_binders := binders.filter $ λ pr, pr.2.type.is_app_of `fintype, if fintype_binders.length = 0 then return none else (λ s, some $ "The following `fintype` hypotheses should be replaced with `casesI nonempty_fintype _` in the proof. " ++ s) <$> print_arguments fintype_binders /-- A linter object for `fintype` vs `finite`. -/ @[linter] meta def linter.fintype_finite : linter := { test := linter.fintype_finite_fun, auto_decls := ff, no_errors_found := "No uses of `fintype` arguments should be replaced with `casesI nonempty_fintype _`.", errors_found := "USES OF `fintype` SHOULD BE REPLACED WITH `casesI nonempty_fintype _` IN THE PROOF." } private meta def has_coe_to_fun_linter (d : declaration) : tactic (option string) := retrieve $ do tt ← return d.is_trusted | pure none, mk_meta_var d.type >>= set_goals ∘ pure, args ← unfreezing intros, expr.sort _ ← target | pure none, let ty : expr := (expr.const d.to_name d.univ_levels).mk_app args, some coe_fn_inst ← try_core $ to_expr ``(_root_.has_coe_to_fun %%ty _) >>= mk_instance | pure none, set_bool_option `pp.all true, some trans_inst@(expr.app (expr.app _ trans_inst_1) trans_inst_2) ← try_core $ to_expr ``(@_root_.coe_fn_trans %%ty _ _ _ _) | pure none, tt ← succeeds $ unify trans_inst coe_fn_inst transparency.reducible | pure none, set_bool_option `pp.all true, trans_inst_1 ← pp trans_inst_1, trans_inst_2 ← pp trans_inst_2, pure $ format.to_string $ "`has_coe_to_fun` instance is definitionally equal to a transitive instance composed of: " ++ trans_inst_1.group.indent 2 ++ format.line ++ "and" ++ trans_inst_2.group.indent 2 /-- Linter that checks whether `has_coe_to_fun` instances comply with Note [function coercion]. -/ @[linter] meta def linter.has_coe_to_fun : linter := { test := has_coe_to_fun_linter, auto_decls := tt, no_errors_found := "has_coe_to_fun is used correctly", errors_found := "INVALID/MISSING `has_coe_to_fun` instances. You should add a `has_coe_to_fun` instance for the following types. See Note [function coercion]." } /-- Checks whether an instance contains a semireducible non-instance with a class as type in its value. We add some restrictions to get not too many false positives: * We only consider classes with an `add` or `mul` field, since those classes are most likely to occur as a field to another class, and be an extension of another class. * We only consider instances of type-valued classes and non-instances that are definitions. * We currently ignore declarations `foo` that have a `foo._main` declaration. We could look inside, or at the generated equation lemmas, but it's unlikely that there are many problematic instances defined using the equation compiler. -/ meta def check_reducible_non_instances (d : declaration) : tactic (option string) := do tt ← is_instance d.to_name | return none, ff ← is_prop d.type | return none, env ← get_env, -- We only check if the class of the instance contains an `add` or a `mul` field. let cls := d.type.pi_codomain.get_app_fn.const_name, some constrs ← return $ env.structure_fields cls | return none, tt ← return $ constrs.mem `add || constrs.mem `mul | return none, l ← d.value.list_constant.mfilter $ λ nm, do { d ← env.get nm, ff ← is_instance nm | return ff, tt ← is_class d.type | return ff, tt ← return d.is_definition | return ff, -- We only check if the class of the non-instance contains an `add` or a `mul` field. let cls := d.type.pi_codomain.get_app_fn.const_name, some constrs ← return $ env.structure_fields cls | return ff, tt ← return $ constrs.mem `add || constrs.mem `mul | return ff, ff ← has_attribute' `reducible nm | return ff, return tt }, if l.empty then return none else -- we currently ignore declarations that have a `foo._main` declaration. if l.to_list = [d.to_name ++ `_main] then return none else return $ some $ "This instance contains the declarations " ++ to_string l.to_list ++ ", which are semireducible non-instances." /-- A linter that checks whether an instance contains a semireducible non-instance. -/ @[linter] meta def linter.check_reducibility : linter := { test := check_reducible_non_instances, auto_decls := ff, no_errors_found := "All non-instances are reducible.", errors_found := "THE FOLLOWING INSTANCES MIGHT NOT REDUCE. These instances contain one or more declarations that are not instances and are also not marked `@[reducible]`. This means that type-class inference cannot unfold these declarations, " ++ "which might mean that type-class inference cannot infer that two instances are definitionally " ++ "equal. This can cause unexpected errors when this class occurs " ++ "as an *argument* to a type-class problem. See note [reducible non-instances].", is_fast := tt }
be4a82deba3e2da0ec43f46cf63c95321fb6b061
e4a7c8ab8b68ca0e53d2c21397320ea590fa01c6
/src/data/default.lean
1b32e0b72e11138c6eecefad5c1079db6160c70d
[]
no_license
lean-forward/field
3ff5dc5f43de40f35481b375f8c871cd0a07c766
7e2127ad485aec25e58a1b9c82a6bb74a599467a
refs/heads/master
1,590,947,010,909
1,563,811,881,000
1,563,811,881,000
190,415,651
1
0
null
1,563,643,371,000
1,559,746,688,000
Lean
UTF-8
Lean
false
false
13
lean
import .polya
120e27ecf9996f6a496aebab72db0913906cd9e5
63abd62053d479eae5abf4951554e1064a4c45b4
/src/linear_algebra/tensor_algebra.lean
f3cd4283895af7b27ab844f0e4daabec77de07bc
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
4,459
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Adam Topaz. -/ import algebra.free_algebra import algebra.ring_quot /-! # Tensor Algebras Given a commutative semiring `R`, and an `R`-module `M`, we construct the tensor algebra of `M`. This is the free `R`-algebra generated (`R`-linearly) by the module `M`. ## Notation 1. `tensor_algebra R M` is the tensor algebra itself. It is endowed with an R-algebra structure. 2. `tensor_algebra.ι R` is the canonical R-linear map `M → tensor_algebra R M`. 3. Given a linear map `f : M → A` to an R-algebra `A`, `lift R f` is the lift of `f` to an `R`-algebra morphism `tensor_algebra R M → A`. ## Theorems 1. `ι_comp_lift` states that the composition `(lift R f) ∘ (ι R)` is identical to `f`. 2. `lift_unique` states that whenever an R-algebra morphism `g : tensor_algebra R M → A` is given whose composition with `ι R` is `f`, then one has `g = lift R f`. 3. `hom_ext` is a variant of `lift_unique` in the form of an extensionality theorem. 4. `lift_comp_ι` is a combination of `ι_comp_lift` and `lift_unique`. It states that the lift of the composition of an algebra morphism with `ι` is the algebra morphism itself. ## Implementation details As noted above, the tensor algebra of `M` is constructed as the free `R`-algebra generated by `M`, modulo the additional relations making the inclusion of `M` into an `R`-linear map. -/ variables (R : Type*) [comm_semiring R] variables (M : Type*) [add_comm_monoid M] [semimodule R M] namespace tensor_algebra /-- An inductively defined relation on `pre R M` used to force the initial algebra structure on the associated quotient. -/ inductive rel : free_algebra R M → free_algebra R M → Prop -- force `ι` to be linear | add {a b : M} : rel (free_algebra.ι R (a+b)) (free_algebra.ι R a + free_algebra.ι R b) | smul {r : R} {a : M} : rel (free_algebra.ι R (r • a)) (algebra_map R (free_algebra R M) r * free_algebra.ι R a) end tensor_algebra /-- The tensor algebra of the module `M` over the commutative semiring `R`. -/ @[derive [inhabited, semiring, algebra R]] def tensor_algebra := ring_quot (tensor_algebra.rel R M) namespace tensor_algebra instance {S : Type*} [comm_ring S] [semimodule S M] : ring (tensor_algebra S M) := ring_quot.ring _ variables {M} /-- The canonical linear map `M →ₗ[R] tensor_algebra R M`. -/ def ι : M →ₗ[R] (tensor_algebra R M) := { to_fun := λ m, (ring_quot.mk_alg_hom R _ (free_algebra.ι R m)), map_add' := λ x y, by { rw [←alg_hom.map_add], exact ring_quot.mk_alg_hom_rel R rel.add, }, map_smul' := λ r x, by { rw [←alg_hom.map_smul], exact ring_quot.mk_alg_hom_rel R rel.smul, } } lemma ring_quot_mk_alg_hom_free_algebra_ι_eq_ι (m : M) : ring_quot.mk_alg_hom R (rel R M) (free_algebra.ι R m) = ι R m := rfl /-- Given a linear map `f : M → A` where `A` is an `R`-algebra, `lift R f` is the unique lift of `f` to a morphism of `R`-algebras `tensor_algebra R M → A`. -/ def lift {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) : tensor_algebra R M →ₐ[R] A := ring_quot.lift_alg_hom R (free_algebra.lift R ⇑f) (λ x y h, by induction h; simp [algebra.smul_def]) variables {R} @[simp] theorem ι_comp_lift {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) : (lift R f).to_linear_map.comp (ι R) = f := by { ext, simp [lift, ι], } @[simp] theorem lift_ι_apply {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) (x) : lift R f (ι R x) = f x := by { dsimp [lift, ι], refl, } @[simp] theorem lift_unique {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) (g : tensor_algebra R M →ₐ[R] A) : g.to_linear_map.comp (ι R) = f ↔ g = lift R f := begin refine ⟨λ hyp, _, λ hyp, by rw [hyp, ι_comp_lift]⟩, ext, rw ←hyp, simp [lift], refl, end attribute [irreducible] tensor_algebra ι lift @[simp] theorem lift_comp_ι {A : Type*} [semiring A] [algebra R A] (g : tensor_algebra R M →ₐ[R] A) : lift R (g.to_linear_map.comp (ι R)) = g := by {symmetry, rw ←lift_unique} @[ext] theorem hom_ext {A : Type*} [semiring A] [algebra R A] {f g : tensor_algebra R M →ₐ[R] A} (w : f.to_linear_map.comp (ι R) = g.to_linear_map.comp (ι R)) : f = g := begin let h := g.to_linear_map.comp (ι R), have : g = lift R h, by rw ←lift_unique, rw [this, ←lift_unique, w], end end tensor_algebra
301098e7fbac1089240fadfeb5178429b7b47f1e
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/run/have6.lean
277f5f43bdd48a1a93bfbe251563516e6758007c
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
620
lean
prelude definition Prop : Type.{1} := Type.{0} constant and : Prop → Prop → Prop infixl `∧`:25 := and constant and_intro : forall (a b : Prop), a → b → a ∧ b constants a b c d : Prop axiom Ha : a axiom Hb : b axiom Hc : c #check have a ∧ b, from and_intro a b Ha Hb, have b ∧ a, from and_intro b a Hb Ha, have H : a ∧ b, from and_intro a b Ha Hb, have H : a ∧ b, from and_intro a b Ha Hb, then have a ∧ b, from and_intro a b Ha Hb, then have b ∧ a, from and_intro b a Hb Ha, then have H : a ∧ b, from and_intro a b Ha Hb, then have H : a ∧ b, from and_intro a b Ha Hb, Ha
3ed6ef4d8b200715049ed4ba1f5c38a023d1752c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/pi/basic.lean
60d19eec88d40c59d3e0a7a195accbf657683fee
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
5,178
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.natural_isomorphism import Mathlib.category_theory.eq_to_hom import Mathlib.PostPort universes u₁ v₁ w₀ w₁ w₂ namespace Mathlib /-! # Categories of indexed families of objects. We define the pointwise category structure on indexed families of objects in a category (and also the dependent generalization). -/ namespace category_theory /-- `pi C` gives the cartesian product of an indexed family of categories. -/ protected instance pi {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] : category ((i : I) → C i) := category.mk /-- This provides some assistance to typeclass search in a common situation, which otherwise fails. (Without this `category_theory.pi.has_limit_of_has_limit_comp_eval` fails.) -/ instance pi' {I : Type v₁} (C : I → Type u₁) [(i : I) → category (C i)] : category ((i : I) → C i) := category_theory.pi C namespace pi @[simp] theorem id_apply {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] (X : (i : I) → C i) (i : I) : 𝟙 = 𝟙 := rfl @[simp] theorem comp_apply {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] {X : (i : I) → C i} {Y : (i : I) → C i} {Z : (i : I) → C i} (f : X ⟶ Y) (g : Y ⟶ Z) (i : I) : category_struct.comp f g i = f i ≫ g i := rfl /-- The evaluation functor at `i : I`, sending an `I`-indexed family of objects to the object over `i`. -/ @[simp] theorem eval_map {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] (i : I) (f : (i : I) → C i) (g : (i : I) → C i) (α : f ⟶ g) : functor.map (eval C i) α = α i := Eq.refl (functor.map (eval C i) α) /-- Pull back an `I`-indexed family of objects to an `J`-indexed family, along a function `J → I`. -/ @[simp] theorem comap_map {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₁} (h : J → I) (f : (i : I) → C i) (g : (i : I) → C i) (α : f ⟶ g) (i : J) : functor.map (comap C h) α i = α (h i) := Eq.refl (functor.map (comap C h) α i) /-- The natural isomorphism between pulling back a grading along the identity function, and the identity functor. -/ def comap_id (I : Type w₀) (C : I → Type u₁) [(i : I) → category (C i)] : comap C id ≅ 𝟭 := iso.mk (nat_trans.mk fun (X : (i : I) → C i) => 𝟙) (nat_trans.mk fun (X : (i : I) → C i) => 𝟙) /-- The natural isomorphism comparing between pulling back along two successive functions, and pulling back along their composition -/ def comap_comp {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₁} {K : Type w₂} (f : K → J) (g : J → I) : comap C g ⋙ comap (C ∘ g) f ≅ comap C (g ∘ f) := iso.mk (nat_trans.mk fun (X : (i : I) → C i) (b : K) => 𝟙) (nat_trans.mk fun (X : (i : I) → C i) (b : K) => 𝟙) /-- The natural isomorphism between pulling back then evaluating, and just evaluating. -/ def comap_eval_iso_eval {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₁} (h : J → I) (j : J) : comap C h ⋙ eval (C ∘ h) j ≅ eval C (h j) := nat_iso.of_components (fun (f : (i : I) → C i) => iso.refl (functor.obj (comap C h ⋙ eval (C ∘ h) j) f)) sorry protected instance sum_elim_category {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₀} {D : J → Type u₁} [(j : J) → category (D j)] (s : I ⊕ J) : category (sum.elim C D s) := sorry /-- The bifunctor combining an `I`-indexed family of objects with a `J`-indexed family of objects to obtain an `I ⊕ J`-indexed family of objects. -/ @[simp] theorem sum_obj_obj {I : Type w₀} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₀} {D : J → Type u₁} [(j : J) → category (D j)] (f : (i : I) → C i) (g : (j : J) → D j) (s : I ⊕ J) : functor.obj (functor.obj (sum C) f) g s = sum.rec f g s := Eq.refl (functor.obj (functor.obj (sum C) f) g s) end pi namespace functor /-- Assemble an `I`-indexed family of functors into a functor between the pi types. -/ def pi {I : Type w₀} {C : I → Type u₁} [(i : I) → category (C i)] {D : I → Type u₁} [(i : I) → category (D i)] (F : (i : I) → C i ⥤ D i) : ((i : I) → C i) ⥤ ((i : I) → D i) := mk (fun (f : (i : I) → C i) (i : I) => obj (F i) (f i)) fun (f g : (i : I) → C i) (α : f ⟶ g) (i : I) => map (F i) (α i) -- One could add some natural isomorphisms showing -- how `functor.pi` commutes with `pi.eval` and `pi.comap`. end functor namespace nat_trans /-- Assemble an `I`-indexed family of natural transformations into a single natural transformation. -/ def pi {I : Type w₀} {C : I → Type u₁} [(i : I) → category (C i)] {D : I → Type u₁} [(i : I) → category (D i)] {F : (i : I) → C i ⥤ D i} {G : (i : I) → C i ⥤ D i} (α : (i : I) → F i ⟶ G i) : functor.pi F ⟶ functor.pi G := mk fun (f : (i : I) → C i) (i : I) => app (α i) (f i)
bdc33c3d296c397b70938c6f991dcd91fbbb2c86
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/tfae.lean
799881160bb48b8ef19425dadb0601c4f62b4949
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
5,001
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Reid Barton, Simon Hudon "The Following Are Equivalent" (tfae) : Tactic for proving the equivalence of a set of proposition using various implications between them. -/ import tactic.interactive data.list.tfae tactic.doc_commands import tactic.scc open expr tactic lean lean.parser namespace tactic open interactive interactive.types expr export list (tfae) namespace tfae @[derive has_reflect, derive inhabited] inductive arrow : Type | right : arrow | left_right : arrow | left : arrow meta def mk_implication : Π (re : arrow) (e₁ e₂ : expr), pexpr | arrow.right e₁ e₂ := ``(%%e₁ → %%e₂) | arrow.left_right e₁ e₂ := ``(%%e₁ ↔ %%e₂) | arrow.left e₁ e₂ := ``(%%e₂ → %%e₁) meta def mk_name : Π (re : arrow) (i₁ i₂ : nat), name | arrow.right i₁ i₂ := ("tfae_" ++ to_string i₁ ++ "_to_" ++ to_string i₂ : string) | arrow.left_right i₁ i₂ := ("tfae_" ++ to_string i₁ ++ "_iff_" ++ to_string i₂ : string) | arrow.left i₁ i₂ := ("tfae_" ++ to_string i₂ ++ "_to_" ++ to_string i₁ : string) end tfae namespace interactive open tactic.tfae list meta def parse_list : expr → option (list expr) | `([]) := pure [] | `(%%e :: %%es) := (::) e <$> parse_list es | _ := none /-- In a goal of the form `tfae [a₀, a₁, a₂]`, `tfae_have : i → j` creates the assertion `aᵢ → aⱼ`. The other possible notations are `tfae_have : i ← j` and `tfae_have : i ↔ j`. The user can also provide a label for the assertion, as with `have`: `tfae_have h : i ↔ j`. -/ meta def tfae_have (h : parse $ optional ident <* tk ":") (i₁ : parse (with_desc "i" small_nat)) (re : parse (((tk "→" <|> tk "->") *> return arrow.right) <|> ((tk "↔" <|> tk "<->") *> return arrow.left_right) <|> ((tk "←" <|> tk "<-") *> return arrow.left))) (i₂ : parse (with_desc "j" small_nat)) (discharger : tactic unit := tactic.solve_by_elim) : tactic unit := do `(tfae %%l) <- target, l ← parse_list l, e₁ ← list.nth l (i₁ - 1) <|> fail format!"index {i₁} is not between 1 and {l.length}", e₂ ← list.nth l (i₂ - 1) <|> fail format!"index {i₂} is not between 1 and {l.length}", type ← to_expr (tfae.mk_implication re e₁ e₂), let h := h.get_or_else (mk_name re i₁ i₂), tactic.assert h type, return () /-- Finds all implications and equivalences in the context to prove a goal of the form `tfae [...]`. -/ meta def tfae_finish : tactic unit := applyc ``tfae_nil <|> closure.with_new_closure (λ cl, do impl_graph.mk_scc cl, `(tfae %%l) ← target, l ← parse_list l, (_,r,_) ← cl.root l.head, refine ``(tfae_of_forall %%r _ _), thm ← mk_const ``forall_mem_cons, l.mmap' (λ e, do rewrite_target thm, split, (_,r',p) ← cl.root e, tactic.exact p ), applyc ``forall_mem_nil, pure ()) end interactive end tactic /-- The `tfae` tactic suite is a set of tactics that help with proving that certain propositions are equivalent. In `data/list/basic.lean` there is a section devoted to propositions of the form ```lean tfae [p1, p2, ..., pn] ``` where `p1`, `p2`, through, `pn` are terms of type `Prop`. This proposition asserts that all the `pi` are pairwise equivalent. There are results that allow to extract the equivalence of two propositions `pi` and `pj`. To prove a goal of the form `tfae [p1, p2, ..., pn]`, there are two tactics. The first tactic is `tfae_have`. As an argument it takes an expression of the form `i arrow j`, where `i` and `j` are two positive natural numbers, and `arrow` is an arrow such as `→`, `->`, `←`, `<-`, `↔`, or `<->`. The tactic `tfae_have : i arrow j` sets up a subgoal in which the user has to prove the equivalence (or implication) of `pi` and `pj`. The remaining tactic, `tfae_finish`, is a finishing tactic. It collects all implications and equivalences from the local context and computes their transitive closure to close the main goal. `tfae_have` and `tfae_finish` can be used together in a proof as follows: ```lean example (a b c d : Prop) : tfae [a,b,c,d] := begin tfae_have : 3 → 1, { /- prove c → a -/ }, tfae_have : 2 → 3, { /- prove b → c -/ }, tfae_have : 2 ← 1, { /- prove a → b -/ }, tfae_have : 4 ↔ 2, { /- prove d ↔ b -/ }, -- a b c d : Prop, -- tfae_3_to_1 : c → a, -- tfae_2_to_3 : b → c, -- tfae_1_to_2 : a → b, -- tfae_4_iff_2 : d ↔ b -- ⊢ tfae [a, b, c, d] tfae_finish, end ``` -/ add_tactic_doc { name := "tfae", category := doc_category.tactic, decl_names := [`tactic.interactive.tfae_have, `tactic.interactive.tfae_finish], tags := ["logic"], inherit_description_from := `tactic.interactive.tfae_finish }
b8062c55a39248fa298318d9c1d3ec079c3bd0f6
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/defInst.lean
2292134a261905e403e7c895a8e4aab3bd8ef923
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
632
lean
def Foo := List Nat def test (x : Nat) : Foo := [x, x+1, x+2] #eval test 4 -- Error #check fun (x y : Foo) => x == y -- Error deriving instance BEq, Repr for Foo #eval test 4 #check fun (x y : Foo) => x == y def Boo := List (String × String) deriving BEq, Repr, DecidableEq def mkBoo (s : String) : Boo := [(s, s)] #eval mkBoo "hello" #eval mkBoo "hell" == mkBoo "hello" #eval mkBoo "hello" == mkBoo "hello" #eval mkBoo "hello" = mkBoo "hello" def M := ReaderT String (StateT Nat IO) deriving Monad, MonadState, MonadReader #print instMMonad def action : M Unit := do modify (. + 1) dbg_trace "{← read}"
71066c6f20459ae093258759c8d1d5f546be6c29
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/isomorphism.lean
0cd80a1715559da1b9473ead7bc5941c9495e0b0
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
2,281
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import .category open tqft.categories namespace tqft.categories.isomorphism structure Isomorphism ( C: Category ) ( X Y : C.Obj ) := (morphism : C.Hom X Y) (inverse : C.Hom Y X) (witness_1 : C.compose morphism inverse = C.identity X) (witness_2 : C.compose inverse morphism = C.identity Y) attribute [simp,ematch] Isomorphism.witness_1 Isomorphism.witness_2 attribute [pointwise] Isomorphism.mk instance Isomorphism_coercion_to_morphism { C : Category } { X Y : C.Obj } : has_coe (Isomorphism C X Y) (C.Hom X Y) := { coe := Isomorphism.morphism } @[pointwise] lemma {u1 v1} Isomorphism_pointwise_equal { C : Category.{u1 v1} } { X Y : C.Obj } ( α β : Isomorphism C X Y ) ( w : α.morphism = β.morphism ) : α = β := begin induction α with f g wα1 wα2, induction β with h k wβ1 wβ2, simp at w, assert p : g = k, begin rewrite - C.left_identity k, rewrite - wα2, rewrite C.associativity, rewrite w, rewrite wβ1, simp end, blast, end definition Isomorphism.reverse { C : Category } { X Y : C.Obj } ( I : Isomorphism C X Y ) : Isomorphism C Y X := { morphism := I.inverse, inverse := I.morphism, witness_1 := I.witness_2, witness_2 := I.witness_1 } structure is_Isomorphism { C : Category } { X Y : C.Obj } ( morphism : C.Hom X Y ) := (inverse : C.Hom Y X) (witness_1 : C.compose morphism inverse = C.identity X) (witness_2 : C.compose inverse morphism = C.identity Y) attribute [simp,ematch] is_Isomorphism.witness_1 is_Isomorphism.witness_2 instance is_Isomorphism_coercion_to_morphism { C : Category } { X Y : C.Obj } ( f : C.Hom X Y ): has_coe (is_Isomorphism f) (C.Hom X Y) := { coe := λ _, f } definition Epimorphism { C : Category } { X Y : C.Obj } ( f : C.Hom X Y ) := Π { Z : C.Obj } ( g h : C.Hom Y Z ) ( w : C.compose f g = C.compose f h), g = h definition Monomorphism { C : Category } { X Y : C.Obj } ( f : C.Hom X Y ) := Π { Z : C.Obj } ( g h : C.Hom Z X ) ( w : C.compose g f = C.compose h f), g = h end tqft.categories.isomorphism
543bcb3f17ca0c2a8e5cefb5ae2936e635f90472
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/eq17.lean
c704120dcc5b6eb1888aa7cb0f1aa3f86d87ddbc
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
206
lean
open nat definition lt_of_succ : ∀ {a b : nat}, succ a < b → a < b | lt_of_succ (lt.base (succ a)) := lt.trans (lt.base a) (lt.base (succ a)) | lt_of_succ (lt.step h) := lt.step (lt_of_succ h)
190f400bc87ec94e5ddca49f77394a80cf1e8c00
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/ring_theory/nullstellensatz.lean
b6fc1cf5b2a15a25e97ebbe74f90ddd49b260cd2
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,151
lean
/- Copyright (c) 2021 Devon Tuma. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Devon Tuma -/ import ring_theory.jacobson import field_theory.algebraic_closure import field_theory.mv_polynomial import algebraic_geometry.prime_spectrum /-! # Nullstellensatz This file establishes a version of Hilbert's classical Nullstellensatz for `mv_polynomial`s. The main statement of the theorem is `vanishing_ideal_zero_locus_eq_radical`. The statement is in terms of new definitions `vanishing_ideal` and `zero_locus`. Mathlib already has versions of these in terms of the prime spectrum of a ring, but those are not well-suited for expressing this result. Suggestions for better ways to state this theorem or organize things are welcome. The machinery around `vanishing_ideal` and `zero_locus` is also minimal, I only added lemmas directly needed in this proof, since I'm not sure if they are the right approach. -/ open ideal noncomputable theory namespace mv_polynomial open mv_polynomial variables {k : Type*} [field k] variables {σ : Type*} /-- Set of points that are zeroes of all polynomials in an ideal -/ def zero_locus (I : ideal (mv_polynomial σ k)) : set (σ → k) := {x : σ → k | ∀ p ∈ I, eval x p = 0} @[simp] lemma mem_zero_locus_iff {I : ideal (mv_polynomial σ k)} {x : σ → k} : x ∈ zero_locus I ↔ ∀ p ∈ I, eval x p = 0 := iff.rfl lemma zero_locus_anti_mono {I J : ideal (mv_polynomial σ k)} (h : I ≤ J) : zero_locus J ≤ zero_locus I := λ x hx p hp, hx p $ h hp lemma zero_locus_bot : zero_locus (⊥ : ideal (mv_polynomial σ k)) = ⊤ := eq_top_iff.2 (λ x hx p hp, trans (congr_arg (eval x) (mem_bot.1 hp)) (eval x).map_zero) lemma zero_locus_top : zero_locus (⊤ : ideal (mv_polynomial σ k)) = ⊥ := eq_bot_iff.2 $ λ x hx, one_ne_zero ((eval x).map_one ▸ (hx 1 submodule.mem_top) : (1 : k) = 0) /-- Ideal of polynomials with common zeroes at all elements of a set -/ def vanishing_ideal (V : set (σ → k)) : ideal (mv_polynomial σ k) := { carrier := {p | ∀ x ∈ V, eval x p = 0}, zero_mem' := λ x hx, ring_hom.map_zero _, add_mem' := λ p q hp hq x hx, by simp only [hq x hx, hp x hx, add_zero, ring_hom.map_add], smul_mem' := λ p q hq x hx, by simp only [hq x hx, algebra.id.smul_eq_mul, mul_zero, ring_hom.map_mul] } @[simp] lemma mem_vanishing_ideal_iff {V : set (σ → k)} {p : mv_polynomial σ k} : p ∈ vanishing_ideal V ↔ ∀ x ∈ V, eval x p = 0 := iff.rfl lemma vanishing_ideal_anti_mono {A B : set (σ → k)} (h : A ≤ B) : vanishing_ideal B ≤ vanishing_ideal A := λ p hp x hx, hp x $ h hx lemma vanishing_ideal_empty : vanishing_ideal (∅ : set (σ → k)) = ⊤ := le_antisymm le_top (λ p hp x hx, absurd hx (set.not_mem_empty x)) lemma le_vanishing_ideal_zero_locus (I : ideal (mv_polynomial σ k)) : I ≤ vanishing_ideal (zero_locus I) := λ p hp x hx, hx p hp lemma zero_locus_vanishing_ideal_le (V : set (σ → k)) : V ≤ zero_locus (vanishing_ideal V) := λ V hV p hp, hp V hV theorem zero_locus_vanishing_ideal_galois_connection : @galois_connection (ideal (mv_polynomial σ k)) (order_dual (set (σ → k))) _ _ zero_locus vanishing_ideal := λ I V, ⟨λ h, le_trans (le_vanishing_ideal_zero_locus I) (vanishing_ideal_anti_mono h), λ h, le_trans (zero_locus_anti_mono h) (zero_locus_vanishing_ideal_le V)⟩ lemma mem_vanishing_ideal_singleton_iff (x : σ → k) (p : mv_polynomial σ k) : p ∈ (vanishing_ideal {x} : ideal (mv_polynomial σ k)) ↔ (eval x p = 0) := ⟨λ h, h x rfl, λ hpx y hy, hy.symm ▸ hpx⟩ instance vanishing_ideal_singleton_is_maximal {x : σ → k} : (vanishing_ideal {x} : ideal (mv_polynomial σ k)).is_maximal := begin have : (vanishing_ideal {x} : ideal (mv_polynomial σ k)).quotient ≃+* k := ring_equiv.of_bijective (ideal.quotient.lift _ (eval x) (λ p h, (mem_vanishing_ideal_singleton_iff x p).mp h)) begin refine ⟨(ring_hom.injective_iff _).mpr (λ p hp, _), λ z, ⟨(ideal.quotient.mk (vanishing_ideal {x} : ideal (mv_polynomial σ k))) (C z), by simp⟩⟩, obtain ⟨q, rfl⟩ := quotient.mk_surjective p, rwa [quotient.lift_mk, ← mem_vanishing_ideal_singleton_iff, ← quotient.eq_zero_iff_mem] at hp, end, rw [← bot_quotient_is_maximal_iff, ring_equiv.bot_maximal_iff this], exact bot_is_maximal, end lemma radical_le_vanishing_ideal_zero_locus (I : ideal (mv_polynomial σ k)) : I.radical ≤ vanishing_ideal (zero_locus I) := begin intros p hp x hx, rw ← mem_vanishing_ideal_singleton_iff, rw radical_eq_Inf at hp, refine (mem_Inf.mp hp) ⟨le_trans (le_vanishing_ideal_zero_locus I) (vanishing_ideal_anti_mono (λ y hy, hy.symm ▸ hx)), is_maximal.is_prime' _⟩, end /-- The point in the prime spectrum assosiated to a given point -/ def point_to_point (x : σ → k) : prime_spectrum (mv_polynomial σ k) := ⟨(vanishing_ideal {x} : ideal (mv_polynomial σ k)), by apply_instance⟩ @[simp] lemma vanishing_ideal_point_to_point (V : set (σ → k)) : prime_spectrum.vanishing_ideal (point_to_point '' V) = mv_polynomial.vanishing_ideal V := le_antisymm (λ p hp x hx, (((prime_spectrum.mem_vanishing_ideal _ _).1 hp) ⟨vanishing_ideal {x}, by apply_instance⟩ ⟨x, ⟨hx, rfl⟩⟩) x rfl) (λ p hp, (prime_spectrum.mem_vanishing_ideal _ _).2 (λ I hI, let ⟨x, hx⟩ := hI in hx.2 ▸ λ x' hx', (set.mem_singleton_iff.1 hx').symm ▸ hp x hx.1)) lemma point_to_point_zero_locus_le (I : ideal (mv_polynomial σ k)) : point_to_point '' (mv_polynomial.zero_locus I) ≤ prime_spectrum.zero_locus ↑I := λ J hJ, let ⟨x, hx⟩ := hJ in (le_trans (le_vanishing_ideal_zero_locus I) (hx.2 ▸ vanishing_ideal_anti_mono (set.singleton_subset_iff.2 hx.1)) : I ≤ J.as_ideal) variables [is_alg_closed k] [fintype σ] lemma is_maximal_iff_eq_vanishing_ideal_singleton (I : ideal (mv_polynomial σ k)) : I.is_maximal ↔ ∃ (x : σ → k), I = vanishing_ideal {x} := begin refine ⟨λ hI, _, λ h, let ⟨x, hx⟩ := h in hx.symm ▸ (mv_polynomial.vanishing_ideal_singleton_is_maximal)⟩, letI : I.is_maximal := hI, letI : field I.quotient := quotient.field I, let ϕ : k →+* I.quotient := (ideal.quotient.mk I).comp C, have hϕ : function.bijective ϕ := ⟨quotient_mk_comp_C_injective _ _ I hI.1, is_alg_closed.algebra_map_surjective_of_is_integral' ϕ (mv_polynomial.comp_C_integral_of_surjective_of_jacobson _ quotient.mk_surjective)⟩, obtain ⟨φ, hφ⟩ := function.surjective.has_right_inverse hϕ.2, let x : σ → k := λ s, φ ((ideal.quotient.mk I) (X s)), have hx : ∀ s : σ, ϕ (x s) = (ideal.quotient.mk I) (X s) := λ s, hφ ((ideal.quotient.mk I) (X s)), refine ⟨x, (is_maximal.eq_of_le (by apply_instance) hI.1 _).symm⟩, intros p hp, rw [← quotient.eq_zero_iff_mem, map_mv_polynomial_eq_eval₂ (ideal.quotient.mk I) p, eval₂_eq'], rw [mem_vanishing_ideal_singleton_iff, eval_eq'] at hp, convert (trans (congr_arg ϕ hp) ϕ.map_zero), simp only [ϕ.map_sum, ϕ.map_mul, ϕ.map_prod, ϕ.map_pow, hx], end /-- Main statement of the Nullstellensatz -/ @[simp] theorem vanishing_ideal_zero_locus_eq_radical (I : ideal (mv_polynomial σ k)) : vanishing_ideal (zero_locus I) = I.radical := begin rw I.radical_eq_jacobson, refine le_antisymm (le_Inf _) (λ p hp x hx, _), { rintros J ⟨hJI, hJ⟩, obtain ⟨x, hx⟩ := (is_maximal_iff_eq_vanishing_ideal_singleton J).1 hJ, refine hx.symm ▸ vanishing_ideal_anti_mono (λ y hy p hp, _), rw [← mem_vanishing_ideal_singleton_iff, set.mem_singleton_iff.1 hy, ← hx], refine hJI hp }, { rw ← mem_vanishing_ideal_singleton_iff x p, refine (mem_Inf.mp hp) ⟨le_trans (le_vanishing_ideal_zero_locus I) (vanishing_ideal_anti_mono (λ y hy, hy.symm ▸ hx)), mv_polynomial.vanishing_ideal_singleton_is_maximal⟩ }, end @[simp] lemma is_prime.vanishing_ideal_zero_locus (P : ideal (mv_polynomial σ k)) [h : P.is_prime] : vanishing_ideal (zero_locus P) = P := trans (vanishing_ideal_zero_locus_eq_radical P) h.radical end mv_polynomial
a03b48504b04dd26dbdfdcb3d7c05b5566b485ec
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/noetherian.lean
9066a23fb6f530d2e113a067e6e7db22ece83c6a
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
39,179
lean
/- Copyright (c) 2018 Mario Carneiro, Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kevin Buzzard -/ import group_theory.finiteness import data.multiset.finset_ops import algebra.algebra.tower import order.order_iso_nat import ring_theory.ideal.operations import order.compactly_generated import linear_algebra.linear_independent import algebra.ring.idempotents /-! # Noetherian rings and modules The following are equivalent for a module M over a ring R: 1. Every increasing chain of submodules M₁ ⊆ M₂ ⊆ M₃ ⊆ ⋯ eventually stabilises. 2. Every submodule is finitely generated. A module satisfying these equivalent conditions is said to be a *Noetherian* R-module. A ring is a *Noetherian ring* if it is Noetherian as a module over itself. (Note that we do not assume yet that our rings are commutative, so perhaps this should be called "left Noetherian". To avoid cumbersome names once we specialize to the commutative case, we don't make this explicit in the declaration names.) ## Main definitions Let `R` be a ring and let `M` and `P` be `R`-modules. Let `N` be an `R`-submodule of `M`. * `submodule.fg N : Prop` is the assertion that `N` is finitely generated as an `R`-module. * `is_noetherian R M` is the proposition that `M` is a Noetherian `R`-module. It is a class, implemented as the predicate that all `R`-submodules of `M` are finitely generated. ## Main statements * `exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul` is Nakayama's lemma, in the following form: if N is a finitely generated submodule of an ambient R-module M and I is an ideal of R such that N ⊆ IN, then there exists r ∈ 1 + I such that rN = 0. * `is_noetherian_iff_well_founded` is the theorem that an R-module M is Noetherian iff `>` is well-founded on `submodule R M`. Note that the Hilbert basis theorem, that if a commutative ring R is Noetherian then so is R[X], is proved in `ring_theory.polynomial`. ## References * [M. F. Atiyah and I. G. Macdonald, *Introduction to commutative algebra*][atiyah-macdonald] * [samuel1967] ## Tags Noetherian, noetherian, Noetherian ring, Noetherian module, noetherian ring, noetherian module -/ open set open_locale big_operators pointwise namespace submodule variables {R : Type*} {M : Type*} [semiring R] [add_comm_monoid M] [module R M] /-- A submodule of `M` is finitely generated if it is the span of a finite subset of `M`. -/ def fg (N : submodule R M) : Prop := ∃ S : finset M, submodule.span R ↑S = N theorem fg_def {N : submodule R M} : N.fg ↔ ∃ S : set M, S.finite ∧ span R S = N := ⟨λ ⟨t, h⟩, ⟨_, finset.finite_to_set t, h⟩, begin rintro ⟨t', h, rfl⟩, rcases finite.exists_finset_coe h with ⟨t, rfl⟩, exact ⟨t, rfl⟩ end⟩ lemma fg_iff_add_submonoid_fg (P : submodule ℕ M) : P.fg ↔ P.to_add_submonoid.fg := ⟨λ ⟨S, hS⟩, ⟨S, by simpa [← span_nat_eq_add_submonoid_closure] using hS⟩, λ ⟨S, hS⟩, ⟨S, by simpa [← span_nat_eq_add_submonoid_closure] using hS⟩⟩ lemma fg_iff_add_subgroup_fg {G : Type*} [add_comm_group G] (P : submodule ℤ G) : P.fg ↔ P.to_add_subgroup.fg := ⟨λ ⟨S, hS⟩, ⟨S, by simpa [← span_int_eq_add_subgroup_closure] using hS⟩, λ ⟨S, hS⟩, ⟨S, by simpa [← span_int_eq_add_subgroup_closure] using hS⟩⟩ lemma fg_iff_exists_fin_generating_family {N : submodule R M} : N.fg ↔ ∃ (n : ℕ) (s : fin n → M), span R (range s) = N := begin rw fg_def, split, { rintros ⟨S, Sfin, hS⟩, obtain ⟨n, f, rfl⟩ := Sfin.fin_embedding, exact ⟨n, f, hS⟩, }, { rintros ⟨n, s, hs⟩, refine ⟨range s, finite_range s, hs⟩ }, end /-- **Nakayama's Lemma**. Atiyah-Macdonald 2.5, Eisenbud 4.7, Matsumura 2.2, [Stacks 00DV](https://stacks.math.columbia.edu/tag/00DV) -/ theorem exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul {R : Type*} [comm_ring R] {M : Type*} [add_comm_group M] [module R M] (I : ideal R) (N : submodule R M) (hn : N.fg) (hin : N ≤ I • N) : ∃ r : R, r - 1 ∈ I ∧ ∀ n ∈ N, r • n = (0 : M) := begin rw fg_def at hn, rcases hn with ⟨s, hfs, hs⟩, have : ∃ r : R, r - 1 ∈ I ∧ N ≤ (I • span R s).comap (linear_map.lsmul R M r) ∧ s ⊆ N, { refine ⟨1, _, _, _⟩, { rw sub_self, exact I.zero_mem }, { rw [hs], intros n hn, rw [mem_comap], change (1:R) • n ∈ I • N, rw one_smul, exact hin hn }, { rw [← span_le, hs], exact le_refl N } }, clear hin hs, revert this, refine set.finite.dinduction_on hfs (λ H, _) (λ i s his hfs ih H, _), { rcases H with ⟨r, hr1, hrn, hs⟩, refine ⟨r, hr1, λ n hn, _⟩, specialize hrn hn, rwa [mem_comap, span_empty, smul_bot, mem_bot] at hrn }, apply ih, rcases H with ⟨r, hr1, hrn, hs⟩, rw [← set.singleton_union, span_union, smul_sup] at hrn, rw [set.insert_subset] at hs, have : ∃ c : R, c - 1 ∈ I ∧ c • i ∈ I • span R s, { specialize hrn hs.1, rw [mem_comap, mem_sup] at hrn, rcases hrn with ⟨y, hy, z, hz, hyz⟩, change y + z = r • i at hyz, rw mem_smul_span_singleton at hy, rcases hy with ⟨c, hci, rfl⟩, use r-c, split, { rw [sub_right_comm], exact I.sub_mem hr1 hci }, { rw [sub_smul, ← hyz, add_sub_cancel'], exact hz } }, rcases this with ⟨c, hc1, hci⟩, refine ⟨c * r, _, _, hs.2⟩, { rw [← ideal.quotient.eq, ring_hom.map_one] at hr1 hc1 ⊢, rw [ring_hom.map_mul, hc1, hr1, mul_one] }, { intros n hn, specialize hrn hn, rw [mem_comap, mem_sup] at hrn, rcases hrn with ⟨y, hy, z, hz, hyz⟩, change y + z = r • n at hyz, rw mem_smul_span_singleton at hy, rcases hy with ⟨d, hdi, rfl⟩, change _ • _ ∈ I • span R s, rw [mul_smul, ← hyz, smul_add, smul_smul, mul_comm, mul_smul], exact add_mem (smul_mem _ _ hci) (smul_mem _ _ hz) } end theorem exists_mem_and_smul_eq_self_of_fg_of_le_smul {R : Type*} [comm_ring R] {M : Type*} [add_comm_group M] [module R M] (I : ideal R) (N : submodule R M) (hn : N.fg) (hin : N ≤ I • N) : ∃ r ∈ I, ∀ n ∈ N, r • n = n := begin obtain ⟨r, hr, hr'⟩ := N.exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul I hn hin, exact ⟨-(r-1), I.neg_mem hr, λ n hn, by simpa [sub_smul] using hr' n hn⟩, end theorem fg_bot : (⊥ : submodule R M).fg := ⟨∅, by rw [finset.coe_empty, span_empty]⟩ lemma _root_.subalgebra.fg_bot_to_submodule {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] : (⊥ : subalgebra R A).to_submodule.fg := ⟨{1}, by simp [algebra.to_submodule_bot] ⟩ theorem fg_span {s : set M} (hs : s.finite) : fg (span R s) := ⟨hs.to_finset, by rw [hs.coe_to_finset]⟩ theorem fg_span_singleton (x : M) : fg (R ∙ x) := fg_span (finite_singleton x) theorem fg.sup {N₁ N₂ : submodule R M} (hN₁ : N₁.fg) (hN₂ : N₂.fg) : (N₁ ⊔ N₂).fg := let ⟨t₁, ht₁⟩ := fg_def.1 hN₁, ⟨t₂, ht₂⟩ := fg_def.1 hN₂ in fg_def.2 ⟨t₁ ∪ t₂, ht₁.1.union ht₂.1, by rw [span_union, ht₁.2, ht₂.2]⟩ lemma fg_finset_sup {ι : Type*} (s : finset ι) (N : ι → submodule R M) (h : ∀ i ∈ s, (N i).fg) : (s.sup N).fg := finset.sup_induction fg_bot (λ a ha b hb, ha.sup hb) h lemma fg_bsupr {ι : Type*} (s : finset ι) (N : ι → submodule R M) (h : ∀ i ∈ s, (N i).fg) : (⨆ i ∈ s, N i).fg := by simpa only [finset.sup_eq_supr] using fg_finset_sup s N h lemma fg_supr {ι : Type*} [finite ι] (N : ι → submodule R M) (h : ∀ i, (N i).fg) : (supr N).fg := by { casesI nonempty_fintype ι, simpa using fg_bsupr finset.univ N (λ i hi, h i) } variables {P : Type*} [add_comm_monoid P] [module R P] variables (f : M →ₗ[R] P) theorem fg.map {N : submodule R M} (hs : N.fg) : (N.map f).fg := let ⟨t, ht⟩ := fg_def.1 hs in fg_def.2 ⟨f '' t, ht.1.image _, by rw [span_image, ht.2]⟩ variables {f} lemma fg_of_fg_map_injective (f : M →ₗ[R] P) (hf : function.injective f) {N : submodule R M} (hfn : (N.map f).fg) : N.fg := let ⟨t, ht⟩ := hfn in ⟨t.preimage f $ λ x _ y _ h, hf h, submodule.map_injective_of_injective hf $ by { rw [f.map_span, finset.coe_preimage, set.image_preimage_eq_inter_range, set.inter_eq_self_of_subset_left, ht], rw [← linear_map.range_coe, ← span_le, ht, ← map_top], exact map_mono le_top }⟩ lemma fg_of_fg_map {R M P : Type*} [ring R] [add_comm_group M] [module R M] [add_comm_group P] [module R P] (f : M →ₗ[R] P) (hf : f.ker = ⊥) {N : submodule R M} (hfn : (N.map f).fg) : N.fg := fg_of_fg_map_injective f (linear_map.ker_eq_bot.1 hf) hfn lemma fg_top (N : submodule R M) : (⊤ : submodule R N).fg ↔ N.fg := ⟨λ h, N.range_subtype ▸ map_top N.subtype ▸ h.map _, λ h, fg_of_fg_map_injective N.subtype subtype.val_injective $ by rwa [map_top, range_subtype]⟩ lemma fg_of_linear_equiv (e : M ≃ₗ[R] P) (h : (⊤ : submodule R P).fg) : (⊤ : submodule R M).fg := e.symm.range ▸ map_top (e.symm : P →ₗ[R] M) ▸ h.map _ theorem fg.prod {sb : submodule R M} {sc : submodule R P} (hsb : sb.fg) (hsc : sc.fg) : (sb.prod sc).fg := let ⟨tb, htb⟩ := fg_def.1 hsb, ⟨tc, htc⟩ := fg_def.1 hsc in fg_def.2 ⟨linear_map.inl R M P '' tb ∪ linear_map.inr R M P '' tc, (htb.1.image _).union (htc.1.image _), by rw [linear_map.span_inl_union_inr, htb.2, htc.2]⟩ theorem fg_pi {ι : Type*} {M : ι → Type*} [finite ι] [Π i, add_comm_monoid (M i)] [Π i, module R (M i)] {p : Π i, submodule R (M i)} (hsb : ∀ i, (p i).fg) : (submodule.pi set.univ p).fg := begin classical, simp_rw fg_def at hsb ⊢, choose t htf hts using hsb, refine ⟨ ⋃ i, (linear_map.single i : _ →ₗ[R] _) '' t i, set.finite_Union $ λ i, (htf i).image _, _⟩, simp_rw [span_Union, span_image, hts, submodule.supr_map_single], end /-- If 0 → M' → M → M'' → 0 is exact and M' and M'' are finitely generated then so is M. -/ theorem fg_of_fg_map_of_fg_inf_ker {R M P : Type*} [ring R] [add_comm_group M] [module R M] [add_comm_group P] [module R P] (f : M →ₗ[R] P) {s : submodule R M} (hs1 : (s.map f).fg) (hs2 : (s ⊓ f.ker).fg) : s.fg := begin haveI := classical.dec_eq R, haveI := classical.dec_eq M, haveI := classical.dec_eq P, cases hs1 with t1 ht1, cases hs2 with t2 ht2, have : ∀ y ∈ t1, ∃ x ∈ s, f x = y, { intros y hy, have : y ∈ map f s, { rw ← ht1, exact subset_span hy }, rcases mem_map.1 this with ⟨x, hx1, hx2⟩, exact ⟨x, hx1, hx2⟩ }, have : ∃ g : P → M, ∀ y ∈ t1, g y ∈ s ∧ f (g y) = y, { choose g hg1 hg2, existsi λ y, if H : y ∈ t1 then g y H else 0, intros y H, split, { simp only [dif_pos H], apply hg1 }, { simp only [dif_pos H], apply hg2 } }, cases this with g hg, clear this, existsi t1.image g ∪ t2, rw [finset.coe_union, span_union, finset.coe_image], apply le_antisymm, { refine sup_le (span_le.2 $ image_subset_iff.2 _) (span_le.2 _), { intros y hy, exact (hg y hy).1 }, { intros x hx, have := subset_span hx, rw ht2 at this, exact this.1 } }, intros x hx, have : f x ∈ map f s, { rw mem_map, exact ⟨x, hx, rfl⟩ }, rw [← ht1,← set.image_id ↑t1, finsupp.mem_span_image_iff_total] at this, rcases this with ⟨l, hl1, hl2⟩, refine mem_sup.2 ⟨(finsupp.total M M R id).to_fun ((finsupp.lmap_domain R R g : (P →₀ R) → M →₀ R) l), _, x - finsupp.total M M R id ((finsupp.lmap_domain R R g : (P →₀ R) → M →₀ R) l), _, add_sub_cancel'_right _ _⟩, { rw [← set.image_id (g '' ↑t1), finsupp.mem_span_image_iff_total], refine ⟨_, _, rfl⟩, haveI : inhabited P := ⟨0⟩, rw [← finsupp.lmap_domain_supported _ _ g, mem_map], refine ⟨l, hl1, _⟩, refl, }, rw [ht2, mem_inf], split, { apply s.sub_mem hx, rw [finsupp.total_apply, finsupp.lmap_domain_apply, finsupp.sum_map_domain_index], refine s.sum_mem _, { intros y hy, exact s.smul_mem _ (hg y (hl1 hy)).1 }, { exact zero_smul _ }, { exact λ _ _ _, add_smul _ _ _ } }, { rw [linear_map.mem_ker, f.map_sub, ← hl2], rw [finsupp.total_apply, finsupp.total_apply, finsupp.lmap_domain_apply], rw [finsupp.sum_map_domain_index, finsupp.sum, finsupp.sum, f.map_sum], rw sub_eq_zero, refine finset.sum_congr rfl (λ y hy, _), unfold id, rw [f.map_smul, (hg y (hl1 hy)).2], { exact zero_smul _ }, { exact λ _ _ _, add_smul _ _ _ } } end lemma fg_induction (R M : Type*) [semiring R] [add_comm_monoid M] [module R M] (P : submodule R M → Prop) (h₁ : ∀ x, P (submodule.span R {x})) (h₂ : ∀ M₁ M₂, P M₁ → P M₂ → P (M₁ ⊔ M₂)) (N : submodule R M) (hN : N.fg) : P N := begin classical, obtain ⟨s, rfl⟩ := hN, induction s using finset.induction, { rw [finset.coe_empty, submodule.span_empty, ← submodule.span_zero_singleton], apply h₁ }, { rw [finset.coe_insert, submodule.span_insert], apply h₂; apply_assumption } end /-- The kernel of the composition of two linear maps is finitely generated if both kernels are and the first morphism is surjective. -/ lemma fg_ker_comp {R M N P : Type*} [ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] [add_comm_group P] [module R P] (f : M →ₗ[R] N) (g : N →ₗ[R] P) (hf1 : f.ker.fg) (hf2 : g.ker.fg) (hsur : function.surjective f) : (g.comp f).ker.fg := begin rw linear_map.ker_comp, apply fg_of_fg_map_of_fg_inf_ker f, { rwa [submodule.map_comap_eq, linear_map.range_eq_top.2 hsur, top_inf_eq] }, { rwa [inf_of_le_right (show f.ker ≤ (comap f g.ker), from comap_mono bot_le)] } end lemma fg_restrict_scalars {R S M : Type*} [comm_semiring R] [semiring S] [algebra R S] [add_comm_group M] [module S M] [module R M] [is_scalar_tower R S M] (N : submodule S M) (hfin : N.fg) (h : function.surjective (algebra_map R S)) : (submodule.restrict_scalars R N).fg := begin obtain ⟨X, rfl⟩ := hfin, use X, exact submodule.span_eq_restrict_scalars R S M X h end /-- Finitely generated submodules are precisely compact elements in the submodule lattice. -/ theorem fg_iff_compact (s : submodule R M) : s.fg ↔ complete_lattice.is_compact_element s := begin classical, -- Introduce shorthand for span of an element let sp : M → submodule R M := λ a, span R {a}, -- Trivial rewrite lemma; a small hack since simp (only) & rw can't accomplish this smoothly. have supr_rw : ∀ t : finset M, (⨆ x ∈ t, sp x) = (⨆ x ∈ (↑t : set M), sp x), from λ t, by refl, split, { rintro ⟨t, rfl⟩, rw [span_eq_supr_of_singleton_spans, ←supr_rw, ←(finset.sup_eq_supr t sp)], apply complete_lattice.finset_sup_compact_of_compact, exact λ n _, singleton_span_is_compact_element n, }, { intro h, -- s is the Sup of the spans of its elements. have sSup : s = Sup (sp '' ↑s), by rw [Sup_eq_supr, supr_image, ←span_eq_supr_of_singleton_spans, eq_comm, span_eq], -- by h, s is then below (and equal to) the sup of the spans of finitely many elements. obtain ⟨u, ⟨huspan, husup⟩⟩ := h (sp '' ↑s) (le_of_eq sSup), have ssup : s = u.sup id, { suffices : u.sup id ≤ s, from le_antisymm husup this, rw [sSup, finset.sup_id_eq_Sup], exact Sup_le_Sup huspan, }, obtain ⟨t, ⟨hts, rfl⟩⟩ := finset.subset_image_iff.mp huspan, rw [finset.sup_finset_image, function.comp.left_id, finset.sup_eq_supr, supr_rw, ←span_eq_supr_of_singleton_spans, eq_comm] at ssup, exact ⟨t, ssup⟩, }, end end submodule namespace ideal variables {R : Type*} {M : Type*} [semiring R] [add_comm_monoid M] [module R M] /-- An ideal of `R` is finitely generated if it is the span of a finite subset of `R`. This is defeq to `submodule.fg`, but unfolds more nicely. -/ def fg (I : ideal R) : Prop := ∃ S : finset R, ideal.span ↑S = I /-- The image of a finitely generated ideal is finitely generated. This is the `ideal` version of `submodule.fg.map`. -/ lemma fg.map {R S : Type*} [semiring R] [semiring S] {I : ideal R} (h : I.fg) (f : R →+* S) : (I.map f).fg := begin classical, obtain ⟨s, hs⟩ := h, refine ⟨s.image f, _⟩, rw [finset.coe_image, ←ideal.map_span, hs], end lemma fg_ker_comp {R S A : Type*} [comm_ring R] [comm_ring S] [comm_ring A] (f : R →+* S) (g : S →+* A) (hf : f.ker.fg) (hg : g.ker.fg) (hsur : function.surjective f) : (g.comp f).ker.fg := begin letI : algebra R S := ring_hom.to_algebra f, letI : algebra R A := ring_hom.to_algebra (g.comp f), letI : algebra S A := ring_hom.to_algebra g, letI : is_scalar_tower R S A := is_scalar_tower.of_algebra_map_eq (λ _, rfl), let f₁ := algebra.linear_map R S, let g₁ := (is_scalar_tower.to_alg_hom R S A).to_linear_map, exact submodule.fg_ker_comp f₁ g₁ hf (submodule.fg_restrict_scalars g.ker hg hsur) hsur end /-- A finitely generated idempotent ideal is generated by an idempotent element -/ lemma is_idempotent_elem_iff_of_fg {R : Type*} [comm_ring R] (I : ideal R) (h : I.fg) : is_idempotent_elem I ↔ ∃ e : R, is_idempotent_elem e ∧ I = R ∙ e := begin split, { intro e, obtain ⟨r, hr, hr'⟩ := submodule.exists_mem_and_smul_eq_self_of_fg_of_le_smul I I h (by { rw [smul_eq_mul], exact e.ge }), simp_rw smul_eq_mul at hr', refine ⟨r, hr' r hr, antisymm _ ((submodule.span_singleton_le_iff_mem _ _).mpr hr)⟩, intros x hx, rw ← hr' x hx, exact ideal.mem_span_singleton'.mpr ⟨_, mul_comm _ _⟩ }, { rintros ⟨e, he, rfl⟩, simp [is_idempotent_elem, ideal.span_singleton_mul_span_singleton, he.eq] } end lemma is_idempotent_elem_iff_eq_bot_or_top {R : Type*} [comm_ring R] [is_domain R] (I : ideal R) (h : I.fg) : is_idempotent_elem I ↔ I = ⊥ ∨ I = ⊤ := begin split, { intro H, obtain ⟨e, he, rfl⟩ := (I.is_idempotent_elem_iff_of_fg h).mp H, simp only [ideal.submodule_span_eq, ideal.span_singleton_eq_bot], apply or_of_or_of_imp_of_imp (is_idempotent_elem.iff_eq_zero_or_one.mp he) id, rintro rfl, simp }, { rintro (rfl|rfl); simp [is_idempotent_elem] } end end ideal /-- `is_noetherian R M` is the proposition that `M` is a Noetherian `R`-module, implemented as the predicate that all `R`-submodules of `M` are finitely generated. -/ class is_noetherian (R M) [semiring R] [add_comm_monoid M] [module R M] : Prop := (noetherian : ∀ (s : submodule R M), s.fg) section variables {R : Type*} {M : Type*} {P : Type*} variables [semiring R] [add_comm_monoid M] [add_comm_monoid P] variables [module R M] [module R P] open is_noetherian include R /-- An R-module is Noetherian iff all its submodules are finitely-generated. -/ lemma is_noetherian_def : is_noetherian R M ↔ ∀ (s : submodule R M), s.fg := ⟨λ h, h.noetherian, is_noetherian.mk⟩ theorem is_noetherian_submodule {N : submodule R M} : is_noetherian R N ↔ ∀ s : submodule R M, s ≤ N → s.fg := begin refine ⟨λ ⟨hn⟩, λ s hs, have s ≤ N.subtype.range, from (N.range_subtype).symm ▸ hs, submodule.map_comap_eq_self this ▸ (hn _).map _, λ h, ⟨λ s, _⟩⟩, have f := (submodule.equiv_map_of_injective N.subtype subtype.val_injective s).symm, have h₁ := h (s.map N.subtype) (submodule.map_subtype_le N s), have h₂ : (⊤ : submodule R (s.map N.subtype)).map f = ⊤ := by simp, have h₃ := ((submodule.fg_top _).2 h₁).map (↑f : _ →ₗ[R] s), exact (submodule.fg_top _).1 (h₂ ▸ h₃), end theorem is_noetherian_submodule_left {N : submodule R M} : is_noetherian R N ↔ ∀ s : submodule R M, (N ⊓ s).fg := is_noetherian_submodule.trans ⟨λ H s, H _ inf_le_left, λ H s hs, (inf_of_le_right hs) ▸ H _⟩ theorem is_noetherian_submodule_right {N : submodule R M} : is_noetherian R N ↔ ∀ s : submodule R M, (s ⊓ N).fg := is_noetherian_submodule.trans ⟨λ H s, H _ inf_le_right, λ H s hs, (inf_of_le_left hs) ▸ H _⟩ instance is_noetherian_submodule' [is_noetherian R M] (N : submodule R M) : is_noetherian R N := is_noetherian_submodule.2 $ λ _ _, is_noetherian.noetherian _ lemma is_noetherian_of_le {s t : submodule R M} [ht : is_noetherian R t] (h : s ≤ t) : is_noetherian R s := is_noetherian_submodule.mpr (λ s' hs', is_noetherian_submodule.mp ht _ (le_trans hs' h)) variable (M) theorem is_noetherian_of_surjective (f : M →ₗ[R] P) (hf : f.range = ⊤) [is_noetherian R M] : is_noetherian R P := ⟨λ s, have (s.comap f).map f = s, from submodule.map_comap_eq_self $ hf.symm ▸ le_top, this ▸ (noetherian _).map _⟩ variable {M} theorem is_noetherian_of_linear_equiv (f : M ≃ₗ[R] P) [is_noetherian R M] : is_noetherian R P := is_noetherian_of_surjective _ f.to_linear_map f.range lemma is_noetherian_top_iff : is_noetherian R (⊤ : submodule R M) ↔ is_noetherian R M := begin unfreezingI { split; assume h }, { exact is_noetherian_of_linear_equiv (linear_equiv.of_top (⊤ : submodule R M) rfl) }, { exact is_noetherian_of_linear_equiv (linear_equiv.of_top (⊤ : submodule R M) rfl).symm }, end lemma is_noetherian_of_injective [is_noetherian R P] (f : M →ₗ[R] P) (hf : function.injective f) : is_noetherian R M := is_noetherian_of_linear_equiv (linear_equiv.of_injective f hf).symm lemma fg_of_injective [is_noetherian R P] {N : submodule R M} (f : M →ₗ[R] P) (hf : function.injective f) : N.fg := @@is_noetherian.noetherian _ _ _ (is_noetherian_of_injective f hf) N end section variables {R : Type*} {M : Type*} {P : Type*} variables [ring R] [add_comm_group M] [add_comm_group P] variables [module R M] [module R P] open is_noetherian include R lemma is_noetherian_of_ker_bot [is_noetherian R P] (f : M →ₗ[R] P) (hf : f.ker = ⊥) : is_noetherian R M := is_noetherian_of_linear_equiv (linear_equiv.of_injective f $ linear_map.ker_eq_bot.mp hf).symm lemma fg_of_ker_bot [is_noetherian R P] {N : submodule R M} (f : M →ₗ[R] P) (hf : f.ker = ⊥) : N.fg := @@is_noetherian.noetherian _ _ _ (is_noetherian_of_ker_bot f hf) N instance is_noetherian_prod [is_noetherian R M] [is_noetherian R P] : is_noetherian R (M × P) := ⟨λ s, submodule.fg_of_fg_map_of_fg_inf_ker (linear_map.snd R M P) (noetherian _) $ have s ⊓ linear_map.ker (linear_map.snd R M P) ≤ linear_map.range (linear_map.inl R M P), from λ x ⟨hx1, hx2⟩, ⟨x.1, prod.ext rfl $ eq.symm $ linear_map.mem_ker.1 hx2⟩, submodule.map_comap_eq_self this ▸ (noetherian _).map _⟩ instance is_noetherian_pi {R ι : Type*} {M : ι → Type*} [ring R] [Π i, add_comm_group (M i)] [Π i, module R (M i)] [finite ι] [∀ i, is_noetherian R (M i)] : is_noetherian R (Π i, M i) := begin casesI nonempty_fintype ι, haveI := classical.dec_eq ι, suffices on_finset : ∀ s : finset ι, is_noetherian R (Π i : s, M i), { let coe_e := equiv.subtype_univ_equiv finset.mem_univ, letI : is_noetherian R (Π i : finset.univ, M (coe_e i)) := on_finset finset.univ, exact is_noetherian_of_linear_equiv (linear_equiv.Pi_congr_left R M coe_e), }, intro s, induction s using finset.induction with a s has ih, { split, intro s, convert submodule.fg_bot, apply eq_bot_iff.2, intros x hx, refine (submodule.mem_bot R).2 _, ext i, cases i.2 }, refine @is_noetherian_of_linear_equiv _ _ _ _ _ _ _ _ _ (@is_noetherian_prod _ (M a) _ _ _ _ _ _ _ ih), fconstructor, { exact λ f i, or.by_cases (finset.mem_insert.1 i.2) (λ h : i.1 = a, show M i.1, from (eq.rec_on h.symm f.1)) (λ h : i.1 ∈ s, show M i.1, from f.2 ⟨i.1, h⟩) }, { intros f g, ext i, unfold or.by_cases, cases i with i hi, rcases finset.mem_insert.1 hi with rfl | h, { change _ = _ + _, simp only [dif_pos], refl }, { change _ = _ + _, have : ¬i = a, { rintro rfl, exact has h }, simp only [dif_neg this, dif_pos h], refl } }, { intros c f, ext i, unfold or.by_cases, cases i with i hi, rcases finset.mem_insert.1 hi with rfl | h, { change _ = c • _, simp only [dif_pos], refl }, { change _ = c • _, have : ¬i = a, { rintro rfl, exact has h }, simp only [dif_neg this, dif_pos h], refl } }, { exact λ f, (f ⟨a, finset.mem_insert_self _ _⟩, λ i, f ⟨i.1, finset.mem_insert_of_mem i.2⟩) }, { intro f, apply prod.ext, { simp only [or.by_cases, dif_pos] }, { ext ⟨i, his⟩, have : ¬i = a, { rintro rfl, exact has his }, simp only [or.by_cases, this, not_false_iff, dif_neg] } }, { intro f, ext ⟨i, hi⟩, rcases finset.mem_insert.1 hi with rfl | h, { simp only [or.by_cases, dif_pos], }, { have : ¬i = a, { rintro rfl, exact has h }, simp only [or.by_cases, dif_neg this, dif_pos h], } } end /-- A version of `is_noetherian_pi` for non-dependent functions. We need this instance because sometimes Lean fails to apply the dependent version in non-dependent settings (e.g., it fails to prove that `ι → ℝ` is finite dimensional over `ℝ`). -/ instance is_noetherian_pi' {R ι M : Type*} [ring R] [add_comm_group M] [module R M] [finite ι] [is_noetherian R M] : is_noetherian R (ι → M) := is_noetherian_pi end open is_noetherian submodule function section universe w variables {R M P : Type*} {N : Type w} [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid N] [module R N] [add_comm_monoid P] [module R P] theorem is_noetherian_iff_well_founded : is_noetherian R M ↔ well_founded ((>) : submodule R M → submodule R M → Prop) := begin rw (complete_lattice.well_founded_characterisations $ submodule R M).out 0 3, exact ⟨λ ⟨h⟩, λ k, (fg_iff_compact k).mp (h k), λ h, ⟨λ k, (fg_iff_compact k).mpr (h k)⟩⟩, end lemma is_noetherian_iff_fg_well_founded : is_noetherian R M ↔ well_founded ((>) : { N : submodule R M // N.fg } → { N : submodule R M // N.fg } → Prop) := begin let α := { N : submodule R M // N.fg }, split, { introI H, let f : α ↪o submodule R M := order_embedding.subtype _, exact order_embedding.well_founded f.dual (is_noetherian_iff_well_founded.mp H) }, { intro H, constructor, intro N, obtain ⟨⟨N₀, h₁⟩, e : N₀ ≤ N, h₂⟩ := well_founded.well_founded_iff_has_max'.mp H { N' : α | N'.1 ≤ N } ⟨⟨⊥, submodule.fg_bot⟩, bot_le⟩, convert h₁, refine (e.antisymm _).symm, by_contra h₃, obtain ⟨x, hx₁ : x ∈ N, hx₂ : x ∉ N₀⟩ := set.not_subset.mp h₃, apply hx₂, have := h₂ ⟨(R ∙ x) ⊔ N₀, _⟩ _ _, { injection this with eq, rw ← eq, exact (le_sup_left : (R ∙ x) ≤ (R ∙ x) ⊔ N₀) (submodule.mem_span_singleton_self _) }, { exact submodule.fg.sup ⟨{x}, by rw [finset.coe_singleton]⟩ h₁ }, { exact sup_le ((submodule.span_singleton_le_iff_mem _ _).mpr hx₁) e }, { show N₀ ≤ (R ∙ x) ⊔ N₀, from le_sup_right } } end variables (R M) lemma well_founded_submodule_gt (R M) [semiring R] [add_comm_monoid M] [module R M] : ∀ [is_noetherian R M], well_founded ((>) : submodule R M → submodule R M → Prop) := is_noetherian_iff_well_founded.mp variables {R M} /-- A module is Noetherian iff every nonempty set of submodules has a maximal submodule among them. -/ theorem set_has_maximal_iff_noetherian : (∀ a : set $ submodule R M, a.nonempty → ∃ M' ∈ a, ∀ I ∈ a, M' ≤ I → I = M') ↔ is_noetherian R M := by rw [is_noetherian_iff_well_founded, well_founded.well_founded_iff_has_max'] /-- A module is Noetherian iff every increasing chain of submodules stabilizes. -/ theorem monotone_stabilizes_iff_noetherian : (∀ (f : ℕ →o submodule R M), ∃ n, ∀ m, n ≤ m → f n = f m) ↔ is_noetherian R M := by rw [is_noetherian_iff_well_founded, well_founded.monotone_chain_condition] /-- If `∀ I > J, P I` implies `P J`, then `P` holds for all submodules. -/ lemma is_noetherian.induction [is_noetherian R M] {P : submodule R M → Prop} (hgt : ∀ I, (∀ J > I, P J) → P I) (I : submodule R M) : P I := well_founded.recursion (well_founded_submodule_gt R M) I hgt end section universe w variables {R M P : Type*} {N : Type w} [ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] [add_comm_group P] [module R P] lemma finite_of_linear_independent [nontrivial R] [is_noetherian R M] {s : set M} (hs : linear_independent R (coe : s → M)) : s.finite := begin refine classical.by_contradiction (λ hf, (rel_embedding.well_founded_iff_no_descending_seq.1 (well_founded_submodule_gt R M)).elim' _), have f : ℕ ↪ s, from set.infinite.nat_embedding s hf, have : ∀ n, (coe ∘ f) '' {m | m ≤ n} ⊆ s, { rintros n x ⟨y, hy₁, rfl⟩, exact (f y).2 }, have : ∀ a b : ℕ, a ≤ b ↔ span R ((coe ∘ f) '' {m | m ≤ a}) ≤ span R ((coe ∘ f) '' {m | m ≤ b}), { assume a b, rw [span_le_span_iff hs (this a) (this b), set.image_subset_image_iff (subtype.coe_injective.comp f.injective), set.subset_def], exact ⟨λ hab x (hxa : x ≤ a), le_trans hxa hab, λ hx, hx a (le_refl a)⟩ }, exact ⟨⟨λ n, span R ((coe ∘ f) '' {m | m ≤ n}), λ x y, by simp [le_antisymm_iff, (this _ _).symm] {contextual := tt}⟩, by dsimp [gt]; simp only [lt_iff_le_not_le, (this _ _).symm]; tauto⟩ end /-- If the first and final modules in a short exact sequence are noetherian, then the middle module is also noetherian. -/ theorem is_noetherian_of_range_eq_ker [is_noetherian R M] [is_noetherian R P] (f : M →ₗ[R] N) (g : N →ₗ[R] P) (hf : function.injective f) (hg : function.surjective g) (h : f.range = g.ker) : is_noetherian R N := is_noetherian_iff_well_founded.2 $ well_founded_gt_exact_sequence (well_founded_submodule_gt R M) (well_founded_submodule_gt R P) f.range (submodule.map f) (submodule.comap f) (submodule.comap g) (submodule.map g) (submodule.gci_map_comap hf) (submodule.gi_map_comap hg) (by simp [submodule.map_comap_eq, inf_comm]) (by simp [submodule.comap_map_eq, h]) /-- For any endomorphism of a Noetherian module, there is some nontrivial iterate with disjoint kernel and range. -/ theorem is_noetherian.exists_endomorphism_iterate_ker_inf_range_eq_bot [I : is_noetherian R M] (f : M →ₗ[R] M) : ∃ n : ℕ, n ≠ 0 ∧ (f ^ n).ker ⊓ (f ^ n).range = ⊥ := begin obtain ⟨n, w⟩ := monotone_stabilizes_iff_noetherian.mpr I (f.iterate_ker.comp ⟨λ n, n+1, λ n m w, by linarith⟩), specialize w (2 * n + 1) (by linarith), dsimp at w, refine ⟨n+1, nat.succ_ne_zero _, _⟩, rw eq_bot_iff, rintros - ⟨h, ⟨y, rfl⟩⟩, rw [mem_bot, ←linear_map.mem_ker, w], erw linear_map.mem_ker at h ⊢, change ((f ^ (n + 1)) * (f ^ (n + 1))) y = 0 at h, rw ←pow_add at h, convert h using 3, linarith, end /-- Any surjective endomorphism of a Noetherian module is injective. -/ theorem is_noetherian.injective_of_surjective_endomorphism [is_noetherian R M] (f : M →ₗ[R] M) (s : surjective f) : injective f := begin obtain ⟨n, ne, w⟩ := is_noetherian.exists_endomorphism_iterate_ker_inf_range_eq_bot f, rw [linear_map.range_eq_top.mpr (linear_map.iterate_surjective s n), inf_top_eq, linear_map.ker_eq_bot] at w, exact linear_map.injective_of_iterate_injective ne w, end /-- Any surjective endomorphism of a Noetherian module is bijective. -/ theorem is_noetherian.bijective_of_surjective_endomorphism [is_noetherian R M] (f : M →ₗ[R] M) (s : surjective f) : bijective f := ⟨is_noetherian.injective_of_surjective_endomorphism f s, s⟩ /-- A sequence `f` of submodules of a noetherian module, with `f (n+1)` disjoint from the supremum of `f 0`, ..., `f n`, is eventually zero. -/ lemma is_noetherian.disjoint_partial_sups_eventually_bot [I : is_noetherian R M] (f : ℕ → submodule R M) (h : ∀ n, disjoint (partial_sups f n) (f (n+1))) : ∃ n : ℕ, ∀ m, n ≤ m → f m = ⊥ := begin -- A little off-by-one cleanup first: suffices t : ∃ n : ℕ, ∀ m, n ≤ m → f (m+1) = ⊥, { obtain ⟨n, w⟩ := t, use n+1, rintros (_|m) p, { cases p, }, { apply w, exact nat.succ_le_succ_iff.mp p }, }, obtain ⟨n, w⟩ := monotone_stabilizes_iff_noetherian.mpr I (partial_sups f), exact ⟨n, λ m p, (h m).eq_bot_of_ge $ sup_eq_left.1 $ (w (m + 1) $ le_add_right p).symm.trans $ w m p⟩ end /-- If `M ⊕ N` embeds into `M`, for `M` noetherian over `R`, then `N` is trivial. -/ noncomputable def is_noetherian.equiv_punit_of_prod_injective [is_noetherian R M] (f : M × N →ₗ[R] M) (i : injective f) : N ≃ₗ[R] punit.{w+1} := begin apply nonempty.some, obtain ⟨n, w⟩ := is_noetherian.disjoint_partial_sups_eventually_bot (f.tailing i) (f.tailings_disjoint_tailing i), specialize w n (le_refl n), apply nonempty.intro, refine (f.tailing_linear_equiv i n).symm ≪≫ₗ _, rw w, exact submodule.bot_equiv_punit, end end /-- A (semi)ring is Noetherian if it is Noetherian as a module over itself, i.e. all its ideals are finitely generated. -/ @[reducible] def is_noetherian_ring (R) [semiring R] := is_noetherian R R theorem is_noetherian_ring_iff {R} [semiring R] : is_noetherian_ring R ↔ is_noetherian R R := iff.rfl /-- A ring is Noetherian if and only if all its ideals are finitely-generated. -/ lemma is_noetherian_ring_iff_ideal_fg (R : Type*) [semiring R] : is_noetherian_ring R ↔ ∀ I : ideal R, I.fg := is_noetherian_ring_iff.trans is_noetherian_def @[priority 80] -- see Note [lower instance priority] instance is_noetherian_of_finite (R M) [finite M] [semiring R] [add_comm_monoid M] [module R M] : is_noetherian R M := ⟨λ s, ⟨(s : set M).to_finite.to_finset, by rw [set.finite.coe_to_finset, submodule.span_eq]⟩⟩ /-- Modules over the trivial ring are Noetherian. -/ @[priority 100] -- see Note [lower instance priority] instance is_noetherian_of_subsingleton (R M) [subsingleton R] [semiring R] [add_comm_monoid M] [module R M] : is_noetherian R M := by { haveI := module.subsingleton R M, exact is_noetherian_of_finite R M } theorem is_noetherian_of_submodule_of_noetherian (R M) [semiring R] [add_comm_monoid M] [module R M] (N : submodule R M) (h : is_noetherian R M) : is_noetherian R N := begin rw is_noetherian_iff_well_founded at h ⊢, exact order_embedding.well_founded (submodule.map_subtype.order_embedding N).dual h, end instance submodule.quotient.is_noetherian {R} [ring R] {M} [add_comm_group M] [module R M] (N : submodule R M) [h : is_noetherian R M] : is_noetherian R (M ⧸ N) := begin rw is_noetherian_iff_well_founded at h ⊢, exact order_embedding.well_founded (submodule.comap_mkq.order_embedding N).dual h, end /-- If `M / S / R` is a scalar tower, and `M / R` is Noetherian, then `M / S` is also noetherian. -/ theorem is_noetherian_of_tower (R) {S M} [semiring R] [semiring S] [add_comm_monoid M] [has_smul R S] [module S M] [module R M] [is_scalar_tower R S M] (h : is_noetherian R M) : is_noetherian S M := begin rw is_noetherian_iff_well_founded at h ⊢, refine (submodule.restrict_scalars_embedding R S M).dual.well_founded h end instance ideal.quotient.is_noetherian_ring {R : Type*} [comm_ring R] [h : is_noetherian_ring R] (I : ideal R) : is_noetherian_ring (R ⧸ I) := is_noetherian_ring_iff.mpr $ is_noetherian_of_tower R $ submodule.quotient.is_noetherian _ theorem is_noetherian_of_fg_of_noetherian {R M} [ring R] [add_comm_group M] [module R M] (N : submodule R M) [is_noetherian_ring R] (hN : N.fg) : is_noetherian R N := let ⟨s, hs⟩ := hN in begin haveI := classical.dec_eq M, haveI := classical.dec_eq R, letI : is_noetherian R R := by apply_instance, have : ∀ x ∈ s, x ∈ N, from λ x hx, hs ▸ submodule.subset_span hx, refine @@is_noetherian_of_surjective ((↑s : set M) → R) _ _ _ (pi.module _ _ _) _ _ _ is_noetherian_pi, { fapply linear_map.mk, { exact λ f, ⟨∑ i in s.attach, f i • i.1, N.sum_mem (λ c _, N.smul_mem _ $ this _ c.2)⟩ }, { intros f g, apply subtype.eq, change ∑ i in s.attach, (f i + g i) • _ = _, simp only [add_smul, finset.sum_add_distrib], refl }, { intros c f, apply subtype.eq, change ∑ i in s.attach, (c • f i) • _ = _, simp only [smul_eq_mul, mul_smul], exact finset.smul_sum.symm } }, rw linear_map.range_eq_top, rintro ⟨n, hn⟩, change n ∈ N at hn, rw [← hs, ← set.image_id ↑s, finsupp.mem_span_image_iff_total] at hn, rcases hn with ⟨l, hl1, hl2⟩, refine ⟨λ x, l x, subtype.ext _⟩, change ∑ i in s.attach, l i • (i : M) = n, rw [@finset.sum_attach M M s _ (λ i, l i • i), ← hl2, finsupp.total_apply, finsupp.sum, eq_comm], refine finset.sum_subset hl1 (λ x _ hx, _), rw [finsupp.not_mem_support_iff.1 hx, zero_smul] end lemma is_noetherian_of_fg_of_noetherian' {R M} [ring R] [add_comm_group M] [module R M] [is_noetherian_ring R] (h : (⊤ : submodule R M).fg) : is_noetherian R M := have is_noetherian R (⊤ : submodule R M), from is_noetherian_of_fg_of_noetherian _ h, by exactI is_noetherian_of_linear_equiv (linear_equiv.of_top (⊤ : submodule R M) rfl) /-- In a module over a noetherian ring, the submodule generated by finitely many vectors is noetherian. -/ theorem is_noetherian_span_of_finite (R) {M} [ring R] [add_comm_group M] [module R M] [is_noetherian_ring R] {A : set M} (hA : A.finite) : is_noetherian R (submodule.span R A) := is_noetherian_of_fg_of_noetherian _ (submodule.fg_def.mpr ⟨A, hA, rfl⟩) theorem is_noetherian_ring_of_surjective (R) [ring R] (S) [ring S] (f : R →+* S) (hf : function.surjective f) [H : is_noetherian_ring R] : is_noetherian_ring S := begin rw [is_noetherian_ring_iff, is_noetherian_iff_well_founded] at H ⊢, exact order_embedding.well_founded (ideal.order_embedding_of_surjective f hf).dual H, end instance is_noetherian_ring_range {R} [ring R] {S} [ring S] (f : R →+* S) [is_noetherian_ring R] : is_noetherian_ring f.range := is_noetherian_ring_of_surjective R f.range f.range_restrict f.range_restrict_surjective theorem is_noetherian_ring_of_ring_equiv (R) [ring R] {S} [ring S] (f : R ≃+* S) [is_noetherian_ring R] : is_noetherian_ring S := is_noetherian_ring_of_surjective R S f.to_ring_hom f.to_equiv.surjective namespace submodule section map₂ variables {R M N P : Type*} variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] variables [module R M] [module R N] [module R P] theorem fg.map₂ (f : M →ₗ[R] N →ₗ[R] P) {p : submodule R M} {q : submodule R N} (hp : p.fg) (hq : q.fg) : (map₂ f p q).fg := let ⟨sm, hfm, hm⟩ := fg_def.1 hp, ⟨sn, hfn, hn⟩ := fg_def.1 hq in fg_def.2 ⟨set.image2 (λ m n, f m n) sm sn, hfm.image2 _ hfn, map₂_span_span R f sm sn ▸ hm ▸ hn ▸ rfl⟩ end map₂ section mul variables {R : Type*} {A : Type*} [comm_semiring R] [semiring A] [algebra R A] variables {M N : submodule R A} theorem fg.mul (hm : M.fg) (hn : N.fg) : (M * N).fg := hm.map₂ _ hn lemma fg.pow (h : M.fg) (n : ℕ) : (M ^ n).fg := nat.rec_on n (⟨{1}, by simp [one_eq_span]⟩) (λ n ih, by simpa [pow_succ] using h.mul ih) end mul end submodule
b2735d2c1b0c774e097564cc8dd79839993b1102
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/meta/interactive.lean
c28ddd6bc246ec291fb7cfb395ba5fdd79958cd6
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
73,567
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jannis Limperg -/ prelude import init.meta.tactic init.meta.type_context init.meta.rewrite_tactic init.meta.simp_tactic import init.meta.smt.congruence_closure init.control.combinators import init.meta.interactive_base init.meta.derive init.meta.match_tactic import init.meta.congr_tactic init.meta.case_tag open lean open lean.parser open native precedence `?` : max local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic /- allows metavars -/ meta def i_to_expr (q : pexpr) : tactic expr := to_expr q tt /- allow metavars and no subgoals -/ meta def i_to_expr_no_subgoals (q : pexpr) : tactic expr := to_expr q tt ff /- doesn't allows metavars -/ meta def i_to_expr_strict (q : pexpr) : tactic expr := to_expr q ff /- Auxiliary version of i_to_expr for apply-like tactics. This is a workaround for comment https://github.com/leanprover/lean/issues/1342#issuecomment-307912291 at issue #1342. In interactive mode, given a tactic apply f we want the apply tactic to create all metavariables. The following definition will return `@f` for `f`. That is, it will **not** create metavariables for implicit arguments. Before we added `i_to_expr_for_apply`, the tactic apply le_antisymm would first elaborate `le_antisymm`, and create @le_antisymm ?m_1 ?m_2 ?m_3 ?m_4 The type class resolution problem ?m_2 : weak_order ?m_1 by the elaborator since ?m_1 is not assigned yet, and the problem is discarded. Then, we would invoke `apply_core`, which would create two new metavariables for the explicit arguments, and try to unify the resulting type with the current target. After the unification, the metavariables ?m_1, ?m_3 and ?m_4 are assigned, but we lost the information about the pending type class resolution problem. With `i_to_expr_for_apply`, `le_antisymm` is elaborate into `@le_antisymm`, the apply_core tactic creates all metavariables, and solves the ones that can be solved by type class resolution. Another possible fix: we modify the elaborator to return pending type class resolution problems, and store them in the tactic_state. -/ meta def i_to_expr_for_apply (q : pexpr) : tactic expr := let aux (n : name) : tactic expr := do p ← resolve_name n, match p with | (expr.const c []) := do r ← mk_const c, save_type_info r q, return r | _ := i_to_expr p end in match q with | (expr.const c []) := aux c | (expr.local_const c _ _ _) := aux c | _ := i_to_expr q end namespace interactive open interactive interactive.types expr /-- itactic: parse a nested "interactive" tactic. That is, parse `{` tactic `}` -/ meta def itactic : Type := tactic unit meta def propagate_tags (tac : itactic) : tactic unit := do tag ← get_main_tag, if tag = [] then tac else focus1 $ do tac, gs ← get_goals, when (bnot gs.empty) $ do new_tag ← get_main_tag, when new_tag.empty $ with_enable_tags (set_main_tag tag) meta def concat_tags (tac : tactic (list (name × expr))) : tactic unit := mcond tags_enabled (do in_tag ← get_main_tag, r ← tac, /- remove assigned metavars -/ r ← r.mfilter $ λ ⟨n, m⟩, bnot <$> is_assigned m, match r with | [(_, m)] := set_tag m in_tag /- if there is only new subgoal, we just propagate `in_tag` -/ | _ := r.mmap' (λ ⟨n, m⟩, set_tag m (n::in_tag)) end) (tac >> skip) /-- If the current goal is a Pi/forall `∀ x : t, u` (resp. `let x := t in u`) then `intro` puts `x : t` (resp. `x := t`) in the local context. The new subgoal target is `u`. If the goal is an arrow `t → u`, then it puts `h : t` in the local context and the new goal target is `u`. If the goal is neither a Pi/forall nor begins with a let binder, the tactic `intro` applies the tactic `whnf` until an introduction can be applied or the goal is not head reducible. In the latter case, the tactic fails. -/ meta def intro : parse ident_? → tactic unit | none := propagate_tags (intro1 >> skip) | (some h) := propagate_tags (tactic.intro h >> skip) /-- Similar to `intro` tactic. The tactic `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall or let binder. The variant `intros h₁ ... hₙ` introduces `n` new hypotheses using the given identifiers to name them. -/ meta def intros : parse ident_* → tactic unit | [] := propagate_tags (tactic.intros >> skip) | hs := propagate_tags (intro_lst hs >> skip) /-- The tactic `introv` allows the user to automatically introduce the variables of a theorem and explicitly name the hypotheses involved. The given names are used to name non-dependent hypotheses. Examples: ``` example : ∀ a b : nat, a = b → b = a := begin introv h, exact h.symm end ``` The state after `introv h` is ``` a b : ℕ, h : a = b ⊢ b = a ``` ``` example : ∀ a b : nat, a = b → ∀ c, b = c → a = c := begin introv h₁ h₂, exact h₁.trans h₂ end ``` The state after `introv h₁ h₂` is ``` a b : ℕ, h₁ : a = b, c : ℕ, h₂ : b = c ⊢ a = c ``` -/ meta def introv (ns : parse ident_*) : tactic unit := propagate_tags (tactic.introv ns >> return ()) /-- Parse a current name and new name for `rename`. -/ private meta def rename_arg_parser : parser (name × name) := prod.mk <$> ident <*> (optional (tk "->") *> ident) /-- Parse the arguments of `rename`. -/ private meta def rename_args_parser : parser (list (name × name)) := (functor.map (λ x, [x]) rename_arg_parser) <|> (tk "[" *> sep_by (tk ",") rename_arg_parser <* tk "]") /-- Rename one or more local hypotheses. The renamings are given as follows: ``` rename x y -- rename x to y rename x → y -- ditto rename [x y, a b] -- rename x to y and a to b rename [x → y, a → b] -- ditto ``` Note that if there are multiple hypotheses called `x` in the context, then `rename x y` will rename *all* of them. If you want to rename only one, use `dedup` first. -/ meta def rename (renames : parse rename_args_parser) : tactic unit := propagate_tags $ tactic.rename_many $ native.rb_map.of_list renames /-- The `apply` tactic tries to match the current goal against the conclusion of the type of term. The argument term should be a term well-formed in the local context of the main goal. If it succeeds, then the tactic returns as many subgoals as the number of premises that have not been fixed by type inference or type class resolution. Non-dependent premises are added before dependent ones. The `apply` tactic uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ meta def apply (q : parse texpr) : tactic unit := concat_tags (do h ← i_to_expr_for_apply q, tactic.apply h) /-- Similar to the `apply` tactic, but does not reorder goals. -/ meta def fapply (q : parse texpr) : tactic unit := concat_tags (i_to_expr_for_apply q >>= tactic.fapply) /-- Similar to the `apply` tactic, but only creates subgoals for non-dependent premises that have not been fixed by type inference or type class resolution. -/ meta def eapply (q : parse texpr) : tactic unit := concat_tags (i_to_expr_for_apply q >>= tactic.eapply) /-- Similar to the `apply` tactic, but allows the user to provide a `apply_cfg` configuration object. -/ meta def apply_with (q : parse parser.pexpr) (cfg : apply_cfg) : tactic unit := concat_tags (do e ← i_to_expr_for_apply q, tactic.apply e cfg) /-- Similar to the `apply` tactic, but uses matching instead of unification. `apply_match t` is equivalent to `apply_with t {unify := ff}` -/ meta def mapply (q : parse texpr) : tactic unit := concat_tags (do e ← i_to_expr_for_apply q, tactic.apply e {unify := ff}) /-- This tactic tries to close the main goal `... ⊢ t` by generating a term of type `t` using type class resolution. -/ meta def apply_instance : tactic unit := tactic.apply_instance /-- This tactic behaves like `exact`, but with a big difference: the user can put underscores `_` in the expression as placeholders for holes that need to be filled, and `refine` will generate as many subgoals as there are holes. Note that some holes may be implicit. The type of each hole must either be synthesized by the system or declared by an explicit type ascription like `(_ : nat → Prop)`. -/ meta def refine (q : parse texpr) : tactic unit := tactic.refine q /-- This tactic looks in the local context for a hypothesis whose type is equal to the goal target. If it finds one, it uses it to prove the goal, and otherwise it fails. -/ meta def assumption : tactic unit := tactic.assumption /-- Try to apply `assumption` to all goals. -/ meta def assumption' : tactic unit := tactic.any_goals' tactic.assumption private meta def change_core (e : expr) : option expr → tactic unit | none := tactic.change e | (some h) := do num_reverted : ℕ ← revert h, expr.pi n bi d b ← target, tactic.change $ expr.pi n bi e b, intron num_reverted /-- `change u` replaces the target `t` of the main goal to `u` provided that `t` is well formed with respect to the local context of the main goal and `t` and `u` are definitionally equal. `change u at h` will change a local hypothesis to `u`. `change t with u at h1 h2 ...` will replace `t` with `u` in all the supplied hypotheses (or `*`), or in the goal if no `at` clause is specified, provided that `t` and `u` are definitionally equal. -/ meta def change (q : parse texpr) : parse (tk "with" *> texpr)? → parse location → tactic unit | none (loc.ns [none]) := do e ← i_to_expr q, change_core e none | none (loc.ns [some h]) := do eq ← i_to_expr q, eh ← get_local h, change_core eq (some eh) | none _ := fail "change-at does not support multiple locations" | (some w) l := do u ← mk_meta_univ, ty ← mk_meta_var (sort u), eq ← i_to_expr ``(%%q : %%ty), ew ← i_to_expr ``(%%w : %%ty), let repl := λe : expr, e.replace (λ a n, if a = eq then some ew else none), l.try_apply (λh, do e ← infer_type h, change_core (repl e) (some h)) (do g ← target, change_core (repl g) none) /-- This tactic provides an exact proof term to solve the main goal. If `t` is the goal and `p` is a term of type `u` then `exact p` succeeds if and only if `t` and `u` can be unified. -/ meta def exact (q : parse texpr) : tactic unit := do tgt : expr ← target, i_to_expr_strict ``(%%q : %%tgt) >>= tactic.exact /-- Like `exact`, but takes a list of terms and checks that all goals are discharged after the tactic. -/ meta def exacts : parse pexpr_list_or_texpr → tactic unit | [] := done | (t :: ts) := exact t >> exacts ts /-- A synonym for `exact` that allows writing `have/suffices/show ..., from ...` in tactic mode. -/ meta def «from» := exact /-- `revert h₁ ... hₙ` applies to any goal with hypotheses `h₁` ... `hₙ`. It moves the hypotheses and their dependencies to the target of the goal. This tactic is the inverse of `intro`. -/ meta def revert (ids : parse ident*) : tactic unit := propagate_tags (do hs ← mmap tactic.get_local ids, revert_lst hs, skip) private meta def resolve_name' (n : name) : tactic expr := do { p ← resolve_name n, match p with | expr.const n _ := mk_const n -- create metavars for universe levels | _ := i_to_expr p end } /- Version of to_expr that tries to bypass the elaborator if `p` is just a constant or local constant. This is not an optimization, by skipping the elaborator we make sure that no unwanted resolution is used. Example: the elaborator will force any unassigned ?A that must have be an instance of (has_one ?A) to nat. Remark: another benefit is that auxiliary temporary metavariables do not appear in error messages. -/ meta def to_expr' (p : pexpr) : tactic expr := match p with | (const c []) := do new_e ← resolve_name' c, save_type_info new_e p, return new_e | (local_const c _ _ _) := do new_e ← resolve_name' c, save_type_info new_e p, return new_e | _ := i_to_expr p end @[derive has_reflect] meta structure rw_rule := (pos : pos) (symm : bool) (rule : pexpr) meta def get_rule_eqn_lemmas (r : rw_rule) : tactic (list name) := let aux (n : name) : tactic (list name) := do { p ← resolve_name n, -- unpack local refs let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := get_eqn_lemmas_for tt n | _ := return [] end } <|> return [] in match r.rule with | const n _ := aux n | local_const n _ _ _ := aux n | _ := return [] end private meta def rw_goal (cfg : rewrite_cfg) (rs : list rw_rule) : tactic unit := rs.mmap' $ λ r, do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do e ← to_expr' r.rule, rewrite_target e {symm := r.symm, ..cfg}) (eq_lemmas.mfirst $ λ n, do e ← mk_const n, rewrite_target e {symm := r.symm, ..cfg}) (eq_lemmas.empty) private meta def uses_hyp (e : expr) (h : expr) : bool := e.fold ff $ λ t _ r, r || to_bool (t = h) private meta def rw_hyp (cfg : rewrite_cfg) : list rw_rule → expr → tactic unit | [] hyp := skip | (r::rs) hyp := do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do e ← to_expr' r.rule, when (not (uses_hyp e hyp)) $ rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs) (eq_lemmas.mfirst $ λ n, do e ← mk_const n, rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs) (eq_lemmas.empty) meta def rw_rule_p (ep : parser pexpr) : parser rw_rule := rw_rule.mk <$> cur_pos <*> (option.is_some <$> (with_desc "←" (tk "←" <|> tk "<-"))?) <*> ep @[derive has_reflect] meta structure rw_rules_t := (rules : list rw_rule) (end_pos : option pos) -- accepts the same content as `pexpr_list_or_texpr`, but with correct goal info pos annotations meta def rw_rules : parser rw_rules_t := (tk "[" *> rw_rules_t.mk <$> sep_by (skip_info (tk ",")) (set_goal_info_pos $ rw_rule_p (parser.pexpr 0)) <*> (some <$> cur_pos <* set_goal_info_pos (tk "]"))) <|> rw_rules_t.mk <$> (list.ret <$> rw_rule_p texpr) <*> return none private meta def rw_core (rs : parse rw_rules) (loca : parse location) (cfg : rewrite_cfg) : tactic unit := match loca with | loc.wildcard := loca.try_apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules) | _ := loca.apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules) end >> try (reflexivity reducible) >> (returnopt rs.end_pos >>= save_info <|> skip) /-- `rewrite e` applies identity `e` as a rewrite rule to the target of the main goal. If `e` is preceded by left arrow (`←` or `<-`), the rewrite is applied in the reverse direction. If `e` is a defined constant, then the equational lemmas associated with `e` are used. This provides a convenient way to unfold `e`. `rewrite [e₁, ..., eₙ]` applies the given rules sequentially. `rewrite e at l` rewrites `e` at location(s) `l`, where `l` is either `*` or a list of hypotheses in the local context. In the latter case, a turnstile `⊢` or `|-` can also be used, to signify the target of the goal. -/ meta def rewrite (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := propagate_tags (rw_core q l cfg) /-- An abbreviation for `rewrite`. -/ meta def rw (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := propagate_tags (rw_core q l cfg) /-- `rewrite` followed by `assumption`. -/ meta def rwa (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit := rewrite q l cfg >> try assumption /-- A variant of `rewrite` that uses the unifier more aggressively, unfolding semireducible definitions. -/ meta def erewrite (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {md := semireducible}) : tactic unit := propagate_tags (rw_core q l cfg) /-- An abbreviation for `erewrite`. -/ meta def erw (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {md := semireducible}) : tactic unit := propagate_tags (rw_core q l cfg) /-- Returns the unique names of all hypotheses (local constants) in the context. -/ private meta def hyp_unique_names : tactic name_set := do ctx ← local_context, pure $ ctx.foldl (λ r h, r.insert h.local_uniq_name) mk_name_set /-- Returns all hypotheses (local constants) from the context except those whose unique names are in `hyp_uids`. -/ private meta def hyps_except (hyp_uids : name_set) : tactic (list expr) := do ctx ← local_context, pure $ ctx.filter (λ (h : expr), ¬ hyp_uids.contains h.local_uniq_name) /-- Apply `t` to the main goal and revert any new hypothesis in the generated goals. If `t` is a supported tactic or chain of supported tactics (e.g. `induction`, `cases`, `apply`, `constructor`), the generated goals are also tagged with case tags. You can then use `case` to focus such tagged goals. Two typical uses of `with_cases`: 1. Applying a custom eliminator: ``` lemma my_nat_rec : ∀ n {P : ℕ → Prop} (zero : P 0) (succ : ∀ n, P n → P (n + 1)), P n := ... example (n : ℕ) : n = n := begin with_cases { apply my_nat_rec n }, case zero { refl }, case succ : m ih { refl } end ``` 2. Enabling the use of `case` after a chain of case-splitting tactics: ``` example (n m : ℕ) : unit := begin with_cases { cases n; induction m }, case nat.zero nat.zero { exact () }, case nat.zero nat.succ : k { exact () }, case nat.succ nat.zero : i { exact () }, case nat.succ nat.succ : k i ih_i { exact () } end ``` -/ meta def with_cases (t : itactic) : tactic unit := with_enable_tags $ focus1 $ do input_hyp_uids ← hyp_unique_names, t, all_goals' $ do in_tag ← get_main_tag, new_hyps ← hyps_except input_hyp_uids, n ← revert_lst new_hyps, set_main_tag (case_tag.from_tag_pi in_tag n).render private meta def generalize_arg_p_aux : pexpr → parser (pexpr × name) | (app (app (macro _ [const `eq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" private meta def generalize_arg_p : parser (pexpr × name) := with_desc "expr = id" $ parser.pexpr 0 >>= generalize_arg_p_aux /-- `generalize : e = x` replaces all occurrences of `e` in the target with a new hypothesis `x` of the same type. `generalize h : e = x` in addition registers the hypothesis `h : e = x`. -/ meta def generalize (h : parse ident?) (_ : parse $ tk ":") (p : parse generalize_arg_p) : tactic unit := propagate_tags $ do let (p, x) := p, e ← i_to_expr p, some h ← pure h | tactic.generalize e x >> intro1 >> skip, tgt ← target, -- if generalizing fails, fall back to not replacing anything tgt' ← do { ⟨tgt', _⟩ ← solve_aux tgt (tactic.generalize e x >> target), to_expr ``(Π x, %%e = x → %%(tgt'.binding_body.lift_vars 0 1)) } <|> to_expr ``(Π x, %%e = x → %%tgt), t ← assert h tgt', swap, exact ``(%%t %%e rfl), intro x, intro h meta def cases_arg_p : parser (option name × pexpr) := with_desc "(id :)? expr" $ do t ← texpr, match t with | (local_const x _ _ _) := (tk ":" *> do t ← texpr, pure (some x, t)) <|> pure (none, t) | _ := pure (none, t) end /-- Updates the tags of new subgoals produced by `cases` or `induction`. `in_tag` is the initial tag, i.e. the tag of the goal on which `cases`/`induction` was applied. `rs` should contain, for each subgoal, the constructor name associated with that goal and the hypotheses that were introduced. -/ private meta def set_cases_tags (in_tag : tag) (rs : list (name × list expr)) : tactic unit := do gs ← get_goals, match gs with -- if only one goal was produced, we should not make the tag longer | [g] := set_tag g in_tag | _ := let tgs : list (name × list expr × expr) := rs.map₂ (λ ⟨n, new_hyps⟩ g, ⟨n, new_hyps, g⟩) gs in tgs.mmap' $ λ ⟨n, new_hyps, g⟩, with_enable_tags $ set_tag g $ (case_tag.from_tag_hyps (n :: in_tag) (new_hyps.map expr.local_uniq_name)).render end precedence `generalizing` : 0 /-- Assuming `x` is a variable in the local context with an inductive type, `induction x` applies induction on `x` to the main goal, producing one goal for each constructor of the inductive type, in which the target is replaced by a general instance of that constructor and an inductive hypothesis is added for each recursive argument to the constructor. If the type of an element in the local context depends on `x`, that element is reverted and reintroduced afterward, so that the inductive hypothesis incorporates that hypothesis as well. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `induction n` produces one goal with hypothesis `h : P 0` and target `Q 0`, and one goal with hypotheses `h : P (nat.succ a)` and `ih₁ : P a → Q a` and target `Q (nat.succ a)`. Here the names `a` and `ih₁` ire chosen automatically. `induction e`, where `e` is an expression instead of a variable, generalizes `e` in the goal, and then performs induction on the resulting variable. `induction e with y₁ ... yₙ`, where `e` is a variable or an expression, specifies that the sequence of names `y₁ ... yₙ` should be used for the arguments to the constructors and inductive hypotheses, including implicit arguments. If the list does not include enough names for all of the arguments, additional names are generated automatically. If too many names are given, the extra ones are ignored. Underscores can be used in the list, in which case the corresponding names are generated automatically. Note that for long sequences of names, the `case` tactic provides a more convenient naming mechanism. `induction e using r` allows the user to specify the principle of induction that should be used. Here `r` should be a theorem whose result type must be of the form `C t`, where `C` is a bound variable and `t` is a (possibly empty) sequence of bound variables `induction e generalizing z₁ ... zₙ`, where `z₁ ... zₙ` are variables in the local context, generalizes over `z₁ ... zₙ` before applying the induction but then introduces them in each goal. In other words, the net effect is that each inductive hypothesis is generalized. `induction h : t` will introduce an equality of the form `h : t = C x y`, asserting that the input term is equal to the current constructor case, to the context. -/ meta def induction (hp : parse cases_arg_p) (rec_name : parse using_ident) (ids : parse with_ident_list) (revert : parse $ (tk "generalizing" *> ident*)?) : tactic unit := do in_tag ← get_main_tag, focus1 $ do { -- process `h : t` case e ← match hp with | (some h, p) := do x ← get_unused_name, generalize h () (p, x), get_local x | (none, p) := i_to_expr p end, -- generalize major premise e ← if e.is_local_constant then pure e else tactic.generalize e >> intro1, -- generalize major premise args (e, newvars, locals) ← do { none ← pure rec_name | pure (e, [], []), t ← infer_type e, t ← whnf_ginductive t, const n _ ← pure t.get_app_fn | pure (e, [], []), env ← get_env, tt ← pure $ env.is_inductive n | pure (e, [], []), let (locals, nonlocals) := (t.get_app_args.drop $ env.inductive_num_params n).partition (λ arg : expr, arg.is_local_constant), _ :: _ ← pure nonlocals | pure (e, [], []), n ← tactic.revert e, newvars ← nonlocals.mmap $ λ arg, do { n ← revert_kdeps arg, tactic.generalize arg, h ← intro1, intron n, -- now try to clear hypotheses that may have been abstracted away let locals := arg.fold [] (λ e _ acc, if e.is_local_constant then e::acc else acc), locals.mmap' (try ∘ clear), pure h }, intron (n-1), e ← intro1, pure (e, newvars, locals) }, -- revert `generalizing` params (and their dependencies, if any) to_generalize ← (revert.get_or_else []).mmap tactic.get_local, num_generalized ← revert_lst to_generalize, -- perform the induction rs ← tactic.induction e ids rec_name, -- re-introduce the generalized hypotheses gen_hyps ← all_goals $ do { new_hyps ← intron' num_generalized, clear_lst (newvars.map local_pp_name), (e::locals).mmap' (try ∘ clear), pure new_hyps }, set_cases_tags in_tag $ @list.map₂ (name × list expr × list (name × expr)) _ (name × list expr) (λ ⟨n, hyps, _⟩ gen_hyps, ⟨n, hyps ++ gen_hyps⟩) rs gen_hyps } open case_tag.match_result private meta def goals_with_matching_tag (ns : list name) : tactic (list (expr × case_tag) × list (expr × case_tag)) := do gs ← get_goals, (gs : list (expr × tag)) ← gs.mmap (λ g, do t ← get_tag g, pure (g, t)), pure $ gs.foldr (λ ⟨g, t⟩ ⟨exact_matches, suffix_matches⟩, match case_tag.parse t with | none := ⟨exact_matches, suffix_matches⟩ | some t := match case_tag.match_tag ns t with | exact_match := ⟨⟨g, t⟩ :: exact_matches, suffix_matches⟩ | fuzzy_match := ⟨exact_matches, ⟨g, t⟩ :: suffix_matches⟩ | no_match := ⟨exact_matches, suffix_matches⟩ end end) ([], []) private meta def goal_with_matching_tag (ns : list name) : tactic (expr × case_tag) := do ⟨exact_matches, suffix_matches⟩ ← goals_with_matching_tag ns, match exact_matches, suffix_matches with | [] , [] := fail format! "Invalid `case`: there is no goal tagged with suffix {ns}." | [] , [g] := pure g | [] , _ := let tags : list (list name) := suffix_matches.map (λ ⟨_, t⟩, t.case_names.reverse) in fail format! "Invalid `case`: there is more than one goal tagged with suffix {ns}.\nMatching tags: {tags}" | [g], _ := pure g | _ , _ := fail format! "Invalid `case`: there is more than one goal tagged with tag {ns}." end meta def case_arg_parser : lean.parser (list name × option (list name)) := prod.mk <$> ident_* <*> (tk ":" *> ident_*)? meta def case_parser : lean.parser (list (list name × option (list name))) := (list_of case_arg_parser) <|> (functor.map (λ x, [x]) case_arg_parser) /-- Focuses on a goal ('case') generated by `induction`, `cases` or `with_cases`. The goal is selected by giving one or more names which must match exactly one goal. A goal is matched if the given names are a suffix of its goal tag. Additionally, each name in the sequence can be abbreviated to a suffix of the corresponding name in the goal tag. Thus, a goal with tag ``` nat.zero, list.nil ``` can be selected with any of these invocations (among others): ``` case nat.zero list.nil {...} case nat.zero nil {...} case zero nil {...} case nil {...} ``` Additionally, the form ``` case C : N₀ ... Nₙ {...} ``` can be used to rename hypotheses introduced by the preceding `cases`/`induction`/`with_cases`, using the names `Nᵢ`. For example: ``` example (xs : list ℕ) : xs = xs := begin induction xs, case nil { reflexivity }, case cons : x xs ih { -- x : ℕ, xs : list ℕ, ih : xs = xs reflexivity } end ``` Note that this renaming functionality only work reliably *directly after* an `induction`/`cases`/`with_cases`. If you need to perform additional work after an `induction` or `cases` (e.g. introduce hypotheses in all goals), use `with_cases`. Multiple cases can be handled by the same tactic block with ``` case [A : N₀ ... Nₙ, B : M₀ ... Mₙ] {...} ``` -/ /- TODO `case` could be generalised to work with zero names as well. The form case : x y z { ... } would select the first goal (or the first goal with a case tag), renaming hypotheses to `x, y, z`. The renaming functionality would be available only if the goal has a case tag. -/ meta def case (args : parse case_parser) (tac : itactic) : tactic unit := do target_goals ← args.mmap (λ ⟨ns, ids⟩, do ⟨goal, tag⟩ ← goal_with_matching_tag ns, let ids := ids.get_or_else [], let num_ids := ids.length, goals ← get_goals, let other_goals := goals.filter (≠ goal), set_goals [goal], match tag with | (case_tag.pi _ num_args) := do intro_lst ids, when (num_ids < num_args) $ intron (num_args - num_ids) | (case_tag.hyps _ new_hyp_names) := do let num_new_hyps := new_hyp_names.length, when (num_ids > num_new_hyps) $ fail format! ("Invalid `case`: You gave {num_ids} names, but the case introduces " ++ "{num_new_hyps} new hypotheses."), let renamings := native.rb_map.of_list (new_hyp_names.zip ids), propagate_tags $ tactic.rename_many renamings tt tt end, goals ← get_goals, set_goals other_goals, match goals with | [g] := return g | _ := fail "Unexpected goals introduced by renaming" end), remaining_goals ← get_goals, set_goals target_goals, tac, unsolved_goals ← get_goals, match unsolved_goals with | [] := set_goals remaining_goals | _ := fail "case tactic failed, focused goals have not been solved" end /-- Assuming `x` is a variable in the local context with an inductive type, `destruct x` splits the main goal, producing one goal for each constructor of the inductive type, in which `x` is assumed to be a general instance of that constructor. In contrast to `cases`, the local context is unchanged, i.e. no elements are reverted or introduced. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `destruct n` produces one goal with target `n = 0 → Q n`, and one goal with target `∀ (a : ℕ), (λ (w : ℕ), n = w → Q n) (nat.succ a)`. Here the name `a` is chosen automatically. -/ meta def destruct (p : parse texpr) : tactic unit := i_to_expr p >>= tactic.destruct meta def cases_core (e : expr) (ids : list name := []) : tactic unit := do in_tag ← get_main_tag, focus1 $ do rs ← tactic.cases e ids, set_cases_tags in_tag rs /-- Assuming `x` is a variable in the local context with an inductive type, `cases x` splits the main goal, producing one goal for each constructor of the inductive type, in which the target is replaced by a general instance of that constructor. If the type of an element in the local context depends on `x`, that element is reverted and reintroduced afterward, so that the case split affects that hypothesis as well. For example, given `n : nat` and a goal with a hypothesis `h : P n` and target `Q n`, `cases n` produces one goal with hypothesis `h : P 0` and target `Q 0`, and one goal with hypothesis `h : P (nat.succ a)` and target `Q (nat.succ a)`. Here the name `a` is chosen automatically. `cases e`, where `e` is an expression instead of a variable, generalizes `e` in the goal, and then cases on the resulting variable. `cases e with y₁ ... yₙ`, where `e` is a variable or an expression, specifies that the sequence of names `y₁ ... yₙ` should be used for the arguments to the constructors, including implicit arguments. If the list does not include enough names for all of the arguments, additional names are generated automatically. If too many names are given, the extra ones are ignored. Underscores can be used in the list, in which case the corresponding names are generated automatically. `cases h : e`, where `e` is a variable or an expression, performs cases on `e` as above, but also adds a hypothesis `h : e = ...` to each hypothesis, where `...` is the constructor instance for that particular case. -/ meta def cases : parse cases_arg_p → parse with_ident_list → tactic unit | (none, p) ids := do e ← i_to_expr p, cases_core e ids | (some h, p) ids := do x ← get_unused_name, generalize h () (p, x), hx ← get_local x, cases_core hx ids private meta def find_matching_hyp (ps : list pattern) : tactic expr := any_hyp $ λ h, do type ← infer_type h, ps.mfirst $ λ p, do match_pattern p type, return h /-- `cases_matching p` applies the `cases` tactic to a hypothesis `h : type` if `type` matches the pattern `p`. `cases_matching [p_1, ..., p_n]` applies the `cases` tactic to a hypothesis `h : type` if `type` matches one of the given patterns. `cases_matching* p` more efficient and compact version of `focus1 { repeat { cases_matching p } }`. It is more efficient because the pattern is compiled once. Example: The following tactic destructs all conjunctions and disjunctions in the current goal. ``` cases_matching* [_ ∨ _, _ ∧ _] ``` -/ meta def cases_matching (rec : parse $ (tk "*")?) (ps : parse pexpr_list_or_texpr) : tactic unit := do ps ← ps.mmap pexpr_to_pattern, if rec.is_none then find_matching_hyp ps >>= cases_core else tactic.focus1 $ tactic.repeat $ find_matching_hyp ps >>= cases_core /-- Shorthand for `cases_matching` -/ meta def casesm (rec : parse $ (tk "*")?) (ps : parse pexpr_list_or_texpr) : tactic unit := cases_matching rec ps private meta def try_cases_for_types (type_names : list name) (at_most_one : bool) : tactic unit := any_hyp $ λ h, do I ← expr.get_app_fn <$> (infer_type h >>= head_beta), guard I.is_constant, guard (I.const_name ∈ type_names), tactic.focus1 (cases_core h >> if at_most_one then do n ← num_goals, guard (n <= 1) else skip) /-- `cases_type I` applies the `cases` tactic to a hypothesis `h : (I ...)` `cases_type I_1 ... I_n` applies the `cases` tactic to a hypothesis `h : (I_1 ...)` or ... or `h : (I_n ...)` `cases_type* I` is shorthand for `focus1 { repeat { cases_type I } }` `cases_type! I` only applies `cases` if the number of resulting subgoals is <= 1. Example: The following tactic destructs all conjunctions and disjunctions in the current goal. ``` cases_type* or and ``` -/ meta def cases_type (one : parse $ (tk "!")?) (rec : parse $ (tk "*")?) (type_names : parse ident*) : tactic unit := do type_names ← type_names.mmap resolve_constant, if rec.is_none then try_cases_for_types type_names (bnot one.is_none) else tactic.focus1 $ tactic.repeat $ try_cases_for_types type_names (bnot one.is_none) /-- Tries to solve the current goal using a canonical proof of `true`, or the `reflexivity` tactic, or the `contradiction` tactic. -/ meta def trivial : tactic unit := tactic.triv <|> tactic.reflexivity <|> tactic.contradiction <|> fail "trivial tactic failed" /-- Closes the main goal using `sorry`. -/ meta def admit : tactic unit := tactic.admit /-- Closes the main goal using `sorry`. -/ meta def «sorry» : tactic unit := tactic.admit /-- The contradiction tactic attempts to find in the current local context a hypothesis that is equivalent to an empty inductive type (e.g. `false`), a hypothesis of the form `c_1 ... = c_2 ...` where `c_1` and `c_2` are distinct constructors, or two contradictory hypotheses. -/ meta def contradiction : tactic unit := tactic.contradiction /-- `iterate { t }` repeatedly applies tactic `t` until `t` fails. `iterate { t }` always succeeds. `iterate n { t }` applies `t` `n` times. -/ meta def iterate (n : parse small_nat?) (t : itactic) : tactic unit := match n with | none := tactic.iterate' t | some n := iterate_exactly' n t end /-- `repeat { t }` applies `t` to each goal. If the application succeeds, the tactic is applied recursively to all the generated subgoals until it eventually fails. The recursion stops in a subgoal when the tactic has failed to make progress. The tactic `repeat { t }` never fails. -/ meta def repeat : itactic → tactic unit := tactic.repeat /-- `try { t }` tries to apply tactic `t`, but succeeds whether or not `t` succeeds. -/ meta def try : itactic → tactic unit := tactic.try /-- A do-nothing tactic that always succeeds. -/ meta def skip : tactic unit := tactic.skip /-- `solve1 { t }` applies the tactic `t` to the main goal and fails if it is not solved. -/ meta def solve1 : itactic → tactic unit := tactic.solve1 /-- `abstract id { t }` tries to use tactic `t` to solve the main goal. If it succeeds, it abstracts the goal as an independent definition or theorem with name `id`. If `id` is omitted, a name is generated automatically. -/ meta def abstract (id : parse ident?) (tac : itactic) : tactic unit := tactic.abstract tac id /-- `all_goals { t }` applies the tactic `t` to every goal, and succeeds if each application succeeds. -/ meta def all_goals : itactic → tactic unit := tactic.all_goals' /-- `any_goals { t }` applies the tactic `t` to every goal, and succeeds if at least one application succeeds. -/ meta def any_goals : itactic → tactic unit := tactic.any_goals' /-- `focus { t }` temporarily hides all goals other than the first, applies `t`, and then restores the other goals. It fails if there are no goals. -/ meta def focus (tac : itactic) : tactic unit := tactic.focus1 tac private meta def assume_core (n : name) (ty : pexpr) := do t ← target, when (not $ t.is_pi ∨ t.is_let) whnf_target, t ← target, when (not $ t.is_pi ∨ t.is_let) $ fail "assume tactic failed, Pi/let expression expected", ty ← i_to_expr ``(%%ty : Sort*), unify ty t.binding_domain, intro_core n >> skip /-- Assuming the target of the goal is a Pi or a let, `assume h : t` unifies the type of the binder with `t` and introduces it with name `h`, just like `intro h`. If `h` is absent, the tactic uses the name `this`. If `t` is omitted, it will be inferred. `assume (h₁ : t₁) ... (hₙ : tₙ)` introduces multiple hypotheses. Any of the types may be omitted, but the names must be present. -/ meta def «assume» : parse (sum.inl <$> (tk ":" *> texpr) <|> sum.inr <$> parse_binders tac_rbp) → tactic unit | (sum.inl ty) := assume_core `this ty | (sum.inr binders) := binders.mmap' $ λ b, assume_core b.local_pp_name b.local_type /-- `have h : t := p` adds the hypothesis `h : t` to the current goal if `p` a term of type `t`. If `t` is omitted, it will be inferred. `have h : t` adds the hypothesis `h : t` to the current goal and opens a new subgoal with target `t`. The new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh metavariable. If `h` is omitted, the name `this` is used. -/ meta def «have» (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := let h := h.get_or_else `this in match q₁, q₂ with | some e, some p := do t ← i_to_expr ``(%%e : Sort*), v ← i_to_expr ``(%%p : %%t), tactic.assertv h t v | none, some p := do p ← i_to_expr p, tactic.note h none p | some e, none := i_to_expr ``(%%e : Sort*) >>= tactic.assert h | none, none := do u ← mk_meta_univ, e ← mk_meta_var (sort u), tactic.assert h e end >> skip /-- `let h : t := p` adds the hypothesis `h : t := p` to the current goal if `p` a term of type `t`. If `t` is omitted, it will be inferred. `let h : t` adds the hypothesis `h : t := ?M` to the current goal and opens a new subgoal `?M : t`. The new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh metavariable. If `h` is omitted, the name `this` is used. -/ meta def «let» (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := let h := h.get_or_else `this in match q₁, q₂ with | some e, some p := do t ← i_to_expr ``(%%e : Sort*), v ← i_to_expr ``(%%p : %%t), tactic.definev h t v | none, some p := do p ← i_to_expr p, tactic.pose h none p | some e, none := i_to_expr ``(%%e : Sort*) >>= tactic.define h | none, none := do u ← mk_meta_univ, e ← mk_meta_var (sort u), tactic.define h e end >> skip /-- `suffices h : t` is the same as `have h : t, tactic.swap`. In other words, it adds the hypothesis `h : t` to the current goal and opens a new subgoal with target `t`. -/ meta def «suffices» (h : parse ident?) (t : parse (tk ":" *> texpr)?) : tactic unit := «have» h t none >> tactic.swap /-- This tactic displays the current state in the tracing buffer. -/ meta def trace_state : tactic unit := tactic.trace_state /-- `trace a` displays `a` in the tracing buffer. -/ meta def trace {α : Type} [has_to_tactic_format α] (a : α) : tactic unit := tactic.trace a /-- `existsi e` will instantiate an existential quantifier in the target with `e` and leave the instantiated body as the new target. More generally, it applies to any inductive type with one constructor and at least two arguments, applying the constructor with `e` as the first argument and leaving the remaining arguments as goals. `existsi [e₁, ..., eₙ]` iteratively does the same for each expression in the list. -/ meta def existsi : parse pexpr_list_or_texpr → tactic unit | [] := return () | (p::ps) := i_to_expr p >>= tactic.existsi >> existsi ps /-- This tactic applies to a goal such that its conclusion is an inductive type (say `I`). It tries to apply each constructor of `I` until it succeeds. -/ meta def constructor : tactic unit := concat_tags tactic.constructor /-- Similar to `constructor`, but only non-dependent premises are added as new goals. -/ meta def econstructor : tactic unit := concat_tags tactic.econstructor /-- Applies the first constructor when the type of the target is an inductive data type with two constructors. -/ meta def left : tactic unit := concat_tags tactic.left /-- Applies the second constructor when the type of the target is an inductive data type with two constructors. -/ meta def right : tactic unit := concat_tags tactic.right /-- Applies the constructor when the type of the target is an inductive data type with one constructor. -/ meta def split : tactic unit := concat_tags tactic.split private meta def constructor_matching_aux (ps : list pattern) : tactic unit := do t ← target, ps.mfirst (λ p, match_pattern p t), constructor meta def constructor_matching (rec : parse $ (tk "*")?) (ps : parse pexpr_list_or_texpr) : tactic unit := do ps ← ps.mmap pexpr_to_pattern, if rec.is_none then constructor_matching_aux ps else tactic.focus1 $ tactic.repeat $ constructor_matching_aux ps /-- Replaces the target of the main goal by `false`. -/ meta def exfalso : tactic unit := tactic.exfalso /-- The `injection` tactic is based on the fact that constructors of inductive data types are injections. That means that if `c` is a constructor of an inductive datatype, and if `(c t₁)` and `(c t₂)` are two terms that are equal then `t₁` and `t₂` are equal too. If `q` is a proof of a statement of conclusion `t₁ = t₂`, then injection applies injectivity to derive the equality of all arguments of `t₁` and `t₂` placed in the same positions. For example, from `(a::b) = (c::d)` we derive `a=c` and `b=d`. To use this tactic `t₁` and `t₂` should be constructor applications of the same constructor. Given `h : a::b = c::d`, the tactic `injection h` adds two new hypothesis with types `a = c` and `b = d` to the main goal. The tactic `injection h with h₁ h₂` uses the names `h₁` and `h₂` to name the new hypotheses. -/ meta def injection (q : parse texpr) (hs : parse with_ident_list) : tactic unit := do e ← i_to_expr q, tactic.injection_with e hs, try assumption /-- `injections with h₁ ... hₙ` iteratively applies `injection` to hypotheses using the names `h₁ ... hₙ`. -/ meta def injections (hs : parse with_ident_list) : tactic unit := do tactic.injections_with hs, try assumption end interactive meta structure simp_config_ext extends simp_config := (discharger : tactic unit := failed) section mk_simp_set open expr interactive.types @[derive has_reflect] meta inductive simp_arg_type : Type | all_hyps : simp_arg_type | except : name → simp_arg_type | expr : pexpr → simp_arg_type | symm_expr : pexpr → simp_arg_type meta instance simp_arg_type_to_tactic_format : has_to_tactic_format simp_arg_type := ⟨λ a, match a with | simp_arg_type.all_hyps := pure "*" | (simp_arg_type.except n) := pure format!"-{n}" | (simp_arg_type.expr e) := i_to_expr_no_subgoals e >>= pp | (simp_arg_type.symm_expr e) := ((++) "←") <$> (i_to_expr_no_subgoals e >>= pp) end⟩ meta def simp_arg : parser simp_arg_type := (tk "*" *> return simp_arg_type.all_hyps) <|> (tk "-" *> simp_arg_type.except <$> ident) <|> (tk "<-" *> simp_arg_type.symm_expr <$> texpr) <|> (simp_arg_type.expr <$> texpr) meta def simp_arg_list : parser (list simp_arg_type) := (tk "*" *> return [simp_arg_type.all_hyps]) <|> list_of simp_arg <|> return [] private meta def resolve_exception_ids (all_hyps : bool) : list name → list name → list name → tactic (list name × list name) | [] gex hex := return (gex.reverse, hex.reverse) | (id::ids) gex hex := do p ← resolve_name id, let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := resolve_exception_ids ids (n::gex) hex | local_const n _ _ _ := when (not all_hyps) (fail $ sformat! "invalid local exception {id}, '*' was not used") >> resolve_exception_ids ids gex (n::hex) | _ := fail $ sformat! "invalid exception {id}, unknown identifier" end /-- Decode a list of `simp_arg_type` into lists for each type. This is a backwards-compatibility version of `decode_simp_arg_list_with_symm`. This version fails when an argument of the form `simp_arg_type.symm_expr` is included, so that `simp`-like tactics that do not (yet) support backwards rewriting should properly report an error but function normally on other inputs. -/ meta def decode_simp_arg_list (hs : list simp_arg_type) : tactic $ list pexpr × list name × list name × bool := do (hs, ex, all) ← hs.mfoldl (λ (r : (list pexpr × list name × bool)) h, do let (es, ex, all) := r, match h with | simp_arg_type.all_hyps := pure (es, ex, tt) | simp_arg_type.except id := pure (es, id::ex, all) | simp_arg_type.expr e := pure (e::es, ex, all) | simp_arg_type.symm_expr _ := fail "arguments of the form '←...' are not supported" end) ([], [], ff), (gex, hex) ← resolve_exception_ids all ex [] [], return (hs.reverse, gex, hex, all) /-- Decode a list of `simp_arg_type` into lists for each type. This is the newer version of `decode_simp_arg_list`, and has a new name for backwards compatibility. This version indicates the direction of a `simp` lemma by including a `bool` with the `pexpr`. -/ meta def decode_simp_arg_list_with_symm (hs : list simp_arg_type) : tactic $ list (pexpr × bool) × list name × list name × bool := do let (hs, ex, all) := hs.foldl (λ r h, match r, h with | (es, ex, all), simp_arg_type.all_hyps := (es, ex, tt) | (es, ex, all), simp_arg_type.except id := (es, id::ex, all) | (es, ex, all), simp_arg_type.expr e := ((e, ff)::es, ex, all) | (es, ex, all), simp_arg_type.symm_expr e := ((e, tt)::es, ex, all) end) ([], [], ff), (gex, hex) ← resolve_exception_ids all ex [] [], return (hs.reverse, gex, hex, all) private meta def add_simps : simp_lemmas → list (name × bool) → tactic simp_lemmas | s [] := return s | s (n::ns) := do s' ← s.add_simp n.fst n.snd, add_simps s' ns private meta def report_invalid_simp_lemma {α : Type} (n : name): tactic α := fail format!"invalid simplification lemma '{n}' (use command 'set_option trace.simp_lemmas true' for more details)" private meta def check_no_overload (p : pexpr) : tactic unit := when p.is_choice_macro $ match p with | macro _ ps := fail $ to_fmt "ambiguous overload, possible interpretations" ++ format.join (ps.map (λ p, (to_fmt p).indent 4)) | _ := failed end private meta def simp_lemmas.resolve_and_add (s : simp_lemmas) (u : list name) (n : name) (ref : pexpr) (symm : bool) : tactic (simp_lemmas × list name) := do p ← resolve_name n, check_no_overload p, -- unpack local refs let e := p.erase_annotations.get_app_fn.erase_annotations, match e with | const n _ := (do b ← is_valid_simp_lemma_cnst n, guard b, save_const_type_info n ref, s ← s.add_simp n symm, return (s, u)) <|> (do eqns ← get_eqn_lemmas_for tt n, guard (eqns.length > 0), save_const_type_info n ref, s ← add_simps s (eqns.map (λ e, (e, ff))), return (s, u)) <|> (do env ← get_env, guard (env.is_projection n).is_some, return (s, n::u)) <|> report_invalid_simp_lemma n | _ := (do e ← i_to_expr_no_subgoals p, b ← is_valid_simp_lemma e, guard b, try (save_type_info e ref), s ← s.add e symm, return (s, u)) <|> report_invalid_simp_lemma n end private meta def simp_lemmas.add_pexpr (s : simp_lemmas) (u : list name) (p : pexpr) (symm : bool) : tactic (simp_lemmas × list name) := match p with | (const c []) := simp_lemmas.resolve_and_add s u c p symm | (local_const c _ _ _) := simp_lemmas.resolve_and_add s u c p symm | _ := do new_e ← i_to_expr_no_subgoals p, s ← s.add new_e symm, return (s, u) end private meta def simp_lemmas.append_pexprs : simp_lemmas → list name → list (pexpr × bool) → tactic (simp_lemmas × list name) | s u [] := return (s, u) | s u (l::ls) := do (s, u) ← simp_lemmas.add_pexpr s u l.fst l.snd, simp_lemmas.append_pexprs s u ls meta def mk_simp_set_core (no_dflt : bool) (attr_names : list name) (hs : list simp_arg_type) (at_star : bool) : tactic (bool × simp_lemmas × list name) := do (hs, gex, hex, all_hyps) ← decode_simp_arg_list_with_symm hs, when (all_hyps ∧ at_star ∧ not hex.empty) $ fail "A tactic of the form `simp [*, -h] at *` is currently not supported", s ← join_user_simp_lemmas no_dflt attr_names, -- Erase `h` from the default simp set for calls of the form `simp [←h]`. let to_erase := hs.foldl (λ l h, match h with | (const id _, tt) := id :: l | (local_const id _ _ _, tt) := id :: l | _ := l end ) [], let s := s.erase to_erase, (s, u) ← simp_lemmas.append_pexprs s [] hs, s ← if not at_star ∧ all_hyps then do ctx ← collect_ctx_simps, let ctx := ctx.filter (λ h, h.local_uniq_name ∉ hex), -- remove local exceptions s.append ctx else return s, -- add equational lemmas, if any gex ← gex.mmap (λ n, list.cons n <$> get_eqn_lemmas_for tt n), return (all_hyps, simp_lemmas.erase s $ gex.join, u) meta def mk_simp_set (no_dflt : bool) (attr_names : list name) (hs : list simp_arg_type) : tactic (simp_lemmas × list name) := prod.snd <$> (mk_simp_set_core no_dflt attr_names hs ff) end mk_simp_set namespace interactive open interactive interactive.types expr meta def simp_core_aux (cfg : simp_config) (discharger : tactic unit) (s : simp_lemmas) (u : list name) (hs : list expr) (tgt : bool) : tactic name_set := do (to_remove, lmss) ← @list.mfoldl tactic _ (list expr × name_set) _ (λ ⟨hs, lms⟩ h, do h_type ← infer_type h, (do (new_h_type, pr, new_lms) ← simplify s u h_type cfg `eq discharger, assert h.local_pp_name new_h_type, mk_eq_mp pr h >>= tactic.exact >> return (h::hs, lms.union new_lms)) <|> (return (hs, lms))) ([], mk_name_set) hs, (lms, goal_simplified) ← if tgt then (simp_target s u cfg discharger >>= λ ns, return (ns, tt)) <|> (return (mk_name_set, ff)) else (return (mk_name_set, ff)), guard (cfg.fail_if_unchanged = ff ∨ to_remove.length > 0 ∨ goal_simplified) <|> fail "simplify tactic failed to simplify", to_remove.reverse.mmap' (λ h, try (clear h)), return (lmss.union lms) meta def simp_core (cfg : simp_config) (discharger : tactic unit) (no_dflt : bool) (hs : list simp_arg_type) (attr_names : list name) (locat : loc) : tactic name_set := do lms ← match locat with | loc.wildcard := do (all_hyps, s, u) ← mk_simp_set_core no_dflt attr_names hs tt, if all_hyps then tactic.simp_all s u cfg discharger else do hyps ← non_dep_prop_hyps, simp_core_aux cfg discharger s u hyps tt | _ := do (s, u) ← mk_simp_set no_dflt attr_names hs, ns ← locat.get_locals, simp_core_aux cfg discharger s u ns locat.include_goal end, try tactic.triv, try (tactic.reflexivity reducible), return lms /-- The `simp` tactic uses lemmas and hypotheses to simplify the main goal target or non-dependent hypotheses. It has many variants. `simp` simplifies the main goal target using lemmas tagged with the attribute `[simp]`. `simp [h₁ h₂ ... hₙ]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and the given `hᵢ`'s, where the `hᵢ`'s are expressions. If `hᵢ` is preceded by left arrow (`←` or `<-`), the simplification is performed in the reverse direction. If an `hᵢ` is a defined constant `f`, then the equational lemmas associated with `f` are used. This provides a convenient way to unfold `f`. `simp [*]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and all hypotheses. `simp *` is a shorthand for `simp [*]`. `simp only [h₁ h₂ ... hₙ]` is like `simp [h₁ h₂ ... hₙ]` but does not use `[simp]` lemmas `simp [-id_1, ... -id_n]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]`, but removes the ones named `idᵢ`. `simp at h₁ h₂ ... hₙ` simplifies the non-dependent hypotheses `h₁ : T₁` ... `hₙ : Tₙ`. The tactic fails if the target or another hypothesis depends on one of them. The token `⊢` or `|-` can be added to the list to include the target. `simp at *` simplifies all the hypotheses and the target. `simp * at *` simplifies target and all (non-dependent propositional) hypotheses using the other hypotheses. `simp with attr₁ ... attrₙ` simplifies the main goal target using the lemmas tagged with any of the attributes `[attr₁]`, ..., `[attrₙ]` or `[simp]`. -/ meta def simp (use_iota_eqn : parse $ (tk "!")?) (trace_lemmas : parse $ (tk "?")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (locat : parse location) (cfg : simp_config_ext := {}) : tactic unit := let cfg := match use_iota_eqn, trace_lemmas with | none , none := cfg | (some _), none := {iota_eqn := tt, ..cfg} | none , (some _) := {trace_lemmas := tt, ..cfg} | (some _), (some _) := {iota_eqn := tt, trace_lemmas := tt, ..cfg} end in propagate_tags $ do lms ← simp_core cfg.to_simp_config cfg.discharger no_dflt hs attr_names locat, if cfg.trace_lemmas then trace (↑"Try this: simp only " ++ to_fmt lms.to_list) else skip /-- Just construct the simp set and trace it. Used for debugging. -/ meta def trace_simp_set (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) : tactic unit := do (s, _) ← mk_simp_set no_dflt attr_names hs, s.pp >>= trace /-- `simp_intros h₁ h₂ ... hₙ` is similar to `intros h₁ h₂ ... hₙ` except that each hypothesis is simplified as it is introduced, and each introduced hypothesis is used to simplify later ones and the final target. As with `simp`, a list of simplification lemmas can be provided. The modifiers `only` and `with` behave as with `simp`. -/ meta def simp_intros (ids : parse ident_*) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (cfg : simp_intros_config := {}) : tactic unit := do (s, u) ← mk_simp_set no_dflt attr_names hs, when (¬u.empty) (fail (sformat! "simp_intros tactic does not support {u}")), tactic.simp_intros s u ids cfg, try triv >> try (reflexivity reducible) private meta def to_simp_arg_list (symms : list bool) (es : list pexpr) : list simp_arg_type := (symms.zip es).map (λ ⟨s, e⟩, if s then simp_arg_type.symm_expr e else simp_arg_type.expr e) /-- `dsimp` is similar to `simp`, except that it only uses definitional equalities. -/ meta def dsimp (no_dflt : parse only_flag) (es : parse simp_arg_list) (attr_names : parse with_ident_list) (l : parse location) (cfg : dsimp_config := {}) : tactic unit := do (s, u) ← mk_simp_set no_dflt attr_names es, match l with | loc.wildcard := /- Remark: we cannot revert frozen local instances. We disable zeta expansion because to prevent `intron n` from failing. Another option is to put a "marker" at the current target, and implement `intro_upto_marker`. -/ do n ← revert_all, dsimp_target s u {zeta := ff ..cfg}, intron n | _ := l.apply (λ h, dsimp_hyp h s u cfg) (dsimp_target s u cfg) end /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a reflexive relation, that is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`. The tactic checks whether `t` and `u` are definitionally equal and then solves the goal. -/ meta def reflexivity : tactic unit := tactic.reflexivity /-- Shorter name for the tactic `reflexivity`. -/ meta def refl : tactic unit := tactic.reflexivity /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a symmetric relation, that is, a relation which has a symmetry lemma tagged with the attribute `[symm]`. It replaces the target with `u ~ t`. -/ meta def symmetry : tactic unit := tactic.symmetry /-- This tactic applies to a goal whose target has the form `t ~ u` where `~` is a transitive relation, that is, a relation which has a transitivity lemma tagged with the attribute `[trans]`. `transitivity s` replaces the goal with the two subgoals `t ~ s` and `s ~ u`. If `s` is omitted, then a metavariable is used instead. -/ meta def transitivity (q : parse texpr?) : tactic unit := tactic.transitivity >> match q with | none := skip | some q := do (r, lhs, rhs) ← target_lhs_rhs, i_to_expr q >>= unify rhs end /-- Proves a goal with target `s = t` when `s` and `t` are equal up to the associativity and commutativity of their binary operations. -/ meta def ac_reflexivity : tactic unit := tactic.ac_refl /-- An abbreviation for `ac_reflexivity`. -/ meta def ac_refl : tactic unit := tactic.ac_refl /-- Tries to prove the main goal using congruence closure. -/ meta def cc : tactic unit := tactic.cc /-- Given hypothesis `h : x = t` or `h : t = x`, where `x` is a local constant, `subst h` substitutes `x` by `t` everywhere in the main goal and then clears `h`. -/ meta def subst (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.subst >> try (tactic.reflexivity reducible) /-- Apply `subst` to all hypotheses of the form `h : x = t` or `h : t = x`. -/ meta def subst_vars : tactic unit := tactic.subst_vars /-- `clear h₁ ... hₙ` tries to clear each hypothesis `hᵢ` from the local context. -/ meta def clear : parse ident* → tactic unit := tactic.clear_lst private meta def to_qualified_name_core : name → list name → tactic name | n [] := fail $ "unknown declaration '" ++ to_string n ++ "'" | n (ns::nss) := do curr ← return $ ns ++ n, env ← get_env, if env.contains curr then return curr else to_qualified_name_core n nss private meta def to_qualified_name (n : name) : tactic name := do env ← get_env, if env.contains n then return n else do ns ← open_namespaces, to_qualified_name_core n ns private meta def to_qualified_names : list name → tactic (list name) | [] := return [] | (c::cs) := do new_c ← to_qualified_name c, new_cs ← to_qualified_names cs, return (new_c::new_cs) /-- Similar to `unfold`, but only uses definitional equalities. -/ meta def dunfold (cs : parse ident*) (l : parse location) (cfg : dunfold_config := {}) : tactic unit := match l with | (loc.wildcard) := do ls ← tactic.local_context, n ← revert_lst ls, new_cs ← to_qualified_names cs, dunfold_target new_cs cfg, intron n | _ := do new_cs ← to_qualified_names cs, l.apply (λ h, dunfold_hyp cs h cfg) (dunfold_target new_cs cfg) end private meta def delta_hyps : list name → list name → tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= delta_hyp cs >> delta_hyps cs hs /-- Similar to `dunfold`, but performs a raw delta reduction, rather than using an equation associated with the defined constants. -/ meta def delta : parse ident* → parse location → tactic unit | cs (loc.wildcard) := do ls ← tactic.local_context, n ← revert_lst ls, new_cs ← to_qualified_names cs, delta_target new_cs, intron n | cs l := do new_cs ← to_qualified_names cs, l.apply (delta_hyp new_cs) (delta_target new_cs) private meta def unfold_projs_hyps (cfg : unfold_proj_config := {}) (hs : list name) : tactic bool := hs.mfoldl (λ r h, do h ← get_local h, (unfold_projs_hyp h cfg >> return tt) <|> return r) ff /-- This tactic unfolds all structure projections. -/ meta def unfold_projs (l : parse location) (cfg : unfold_proj_config := {}) : tactic unit := match l with | loc.wildcard := do ls ← local_context, b₁ ← unfold_projs_hyps cfg (ls.map expr.local_pp_name), b₂ ← (tactic.unfold_projs_target cfg >> return tt) <|> return ff, when (not b₁ ∧ not b₂) (fail "unfold_projs failed to simplify") | _ := l.try_apply (λ h, unfold_projs_hyp h cfg) (tactic.unfold_projs_target cfg) <|> fail "unfold_projs failed to simplify" end end interactive meta def ids_to_simp_arg_list (tac_name : name) (cs : list name) : tactic (list simp_arg_type) := cs.mmap $ λ c, do n ← resolve_name c, hs ← get_eqn_lemmas_for ff n.const_name, env ← get_env, let p := env.is_projection n.const_name, when (hs.empty ∧ p.is_none) (fail (sformat! "{tac_name} tactic failed, {c} does not have equational lemmas nor is a projection")), return $ simp_arg_type.expr (expr.const c []) structure unfold_config extends simp_config := (zeta := ff) (proj := ff) (eta := ff) (canonize_instances := ff) (constructor_eq := ff) namespace interactive open interactive interactive.types expr /-- Given defined constants `e₁ ... eₙ`, `unfold e₁ ... eₙ` iteratively unfolds all occurrences in the target of the main goal, using equational lemmas associated with the definitions. As with `simp`, the `at` modifier can be used to specify locations for the unfolding. -/ meta def unfold (cs : parse ident*) (locat : parse location) (cfg : unfold_config := {}) : tactic unit := do es ← ids_to_simp_arg_list "unfold" cs, let no_dflt := tt, simp_core cfg.to_simp_config failed no_dflt es [] locat, skip /-- Similar to `unfold`, but does not iterate the unfolding. -/ meta def unfold1 (cs : parse ident*) (locat : parse location) (cfg : unfold_config := {single_pass := tt}) : tactic unit := unfold cs locat cfg /-- If the target of the main goal is an `opt_param`, assigns the default value. -/ meta def apply_opt_param : tactic unit := tactic.apply_opt_param /-- If the target of the main goal is an `auto_param`, executes the associated tactic. -/ meta def apply_auto_param : tactic unit := tactic.apply_auto_param /-- Fails if the given tactic succeeds. -/ meta def fail_if_success (tac : itactic) : tactic unit := tactic.fail_if_success tac /-- Succeeds if the given tactic fails. -/ meta def success_if_fail (tac : itactic) : tactic unit := tactic.success_if_fail tac meta def guard_expr_eq (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, guard (alpha_eqv t e) /-- `guard_target t` fails if the target of the main goal is not `t`. We use this tactic for writing tests. -/ meta def guard_target (p : parse texpr) : tactic unit := do t ← target, guard_expr_eq t p /-- `guard_hyp h : t` fails if the hypothesis `h` does not have type `t`. We use this tactic for writing tests. -/ meta def guard_hyp (n : parse ident) (ty : parse (tk ":" *> texpr)?) (val : parse (tk ":=" *> texpr)?) : tactic unit := do h ← get_local n, ldecl ← tactic.unsafe.type_context.run (do lctx ← unsafe.type_context.get_local_context, pure $ lctx.get_local_decl h.local_uniq_name), ldecl ← ldecl | fail format!"hypothesis {h} not found", match ty with | some p := guard_expr_eq ldecl.type p | none := skip end, match ldecl.value, val with | none, some _ := fail format!"{h} is not a let binding" | some _, none := fail format!"{h} is a let binding" | some hval, some val := guard_expr_eq hval val | none, none := skip end /-- `match_target t` fails if target does not match pattern `t`. -/ meta def match_target (t : parse texpr) (m := reducible) : tactic unit := tactic.match_target t m >> skip /-- `by_cases p` splits the main goal into two cases, assuming `h : p` in the first branch, and `h : ¬ p` in the second branch. You can specify the name of the new hypothesis using the syntax `by_cases h : p`. -/ meta def by_cases : parse cases_arg_p → tactic unit | (n, q) := concat_tags $ do p ← tactic.to_expr_strict q, tactic.by_cases p (n.get_or_else `h), pos_g :: neg_g :: rest ← get_goals, return [(`pos, pos_g), (`neg, neg_g)] /-- Apply function extensionality and introduce new hypotheses. The tactic `funext` will keep applying new the `funext` lemma until the goal target is not reducible to ``` |- ((fun x, ...) = (fun x, ...)) ``` The variant `funext h₁ ... hₙ` applies `funext` `n` times, and uses the given identifiers to name the new hypotheses. -/ meta def funext : parse ident_* → tactic unit | [] := tactic.funext >> skip | hs := funext_lst hs >> skip /-- If the target of the main goal is a proposition `p`, `by_contradiction` reduces the goal to proving `false` using the additional hypothesis `h : ¬ p`. `by_contradiction h` can be used to name the hypothesis `h : ¬ p`. This tactic will attempt to use decidability of `p` if available, and will otherwise fall back on classical reasoning. -/ meta def by_contradiction (n : parse ident?) : tactic unit := tactic.by_contradiction (n.get_or_else `h) $> () /-- If the target of the main goal is a proposition `p`, `by_contra` reduces the goal to proving `false` using the additional hypothesis `h : ¬ p`. `by_contra h` can be used to name the hypothesis `h : ¬ p`. This tactic will attempt to use decidability of `p` if available, and will otherwise fall back on classical reasoning. -/ meta def by_contra (n : parse ident?) : tactic unit := by_contradiction n /-- Type check the given expression, and trace its type. -/ meta def type_check (p : parse texpr) : tactic unit := do e ← to_expr p, tactic.type_check e, infer_type e >>= trace /-- Fail if there are unsolved goals. -/ meta def done : tactic unit := tactic.done private meta def show_aux (p : pexpr) : list expr → list expr → tactic unit | [] r := fail "show tactic failed" | (g::gs) r := do do {set_goals [g], g_ty ← target, ty ← i_to_expr p, unify g_ty ty, set_goals (g :: r.reverse ++ gs), tactic.change ty} <|> show_aux gs (g::r) /-- `show t` finds the first goal whose target unifies with `t`. It makes that the main goal, performs the unification, and replaces the target with the unified version of `t`. -/ meta def «show» (q : parse texpr) : tactic unit := do gs ← get_goals, show_aux q gs [] /-- The tactic `specialize h a₁ ... aₙ` works on local hypothesis `h`. The premises of this hypothesis, either universal quantifications or non-dependent implications, are instantiated by concrete terms coming either from arguments `a₁` ... `aₙ`. The tactic adds a new hypothesis with the same name `h := h a₁ ... aₙ` and tries to clear the previous one. -/ meta def specialize (p : parse texpr) : tactic unit := focus1 $ do e ← i_to_expr p, let h := expr.get_app_fn e, if h.is_local_constant then tactic.note h.local_pp_name none e >> try (tactic.clear h) >> rotate 1 else tactic.fail "specialize requires a term of the form `h x_1 .. x_n` where `h` appears in the local context" meta def congr := tactic.congr end interactive end tactic section add_interactive open tactic /- See add_interactive -/ private meta def add_interactive_aux (new_namespace : name) : list name → command | [] := return () | (n::ns) := do env ← get_env, d_name ← resolve_constant n, (declaration.defn _ ls ty val hints trusted) ← env.get d_name, (name.mk_string h _) ← return d_name, let new_name := new_namespace <.> h, add_decl (declaration.defn new_name ls ty (expr.const d_name (ls.map level.param)) hints trusted), do { doc ← doc_string d_name, add_doc_string new_name doc } <|> skip, add_interactive_aux ns /-- Copy a list of meta definitions in the current namespace to tactic.interactive. This command is useful when we want to update tactic.interactive without closing the current namespace. -/ meta def add_interactive (ns : list name) (p : name := `tactic.interactive) : command := add_interactive_aux p ns meta def has_dup : tactic bool := do ctx ← local_context, let p : name_set × bool := ctx.foldl (λ ⟨s, r⟩ h, if r then (s, r) else if s.contains h.local_pp_name then (s, tt) else (s.insert h.local_pp_name, ff)) (mk_name_set, ff), return p.2 /-- Renames hypotheses with the same name. -/ meta def dedup : tactic unit := mwhen has_dup $ do ctx ← local_context, n ← revert_lst ctx, intron n end add_interactive namespace tactic /- Helper tactic for `mk_inj_eq -/ protected meta def apply_inj_lemma : tactic unit := do h ← intro `h, some (lhs, rhs) ← expr.is_eq <$> infer_type h, (expr.const C _) ← return lhs.get_app_fn, -- We disable auto_param and opt_param support to address issue #1943 applyc (name.mk_string "inj" C) {auto_param := ff, opt_param := ff}, assumption /- Auxiliary tactic for proving `I.C.inj_eq` lemmas. These lemmas are automatically generated by the equation compiler. Example: ``` list.cons.inj_eq : forall h1 h2 t1 t2, (h1::t1 = h2::t2) = (h1 = h2 ∧ t1 = t2) := by mk_inj_eq ``` -/ meta def mk_inj_eq : tactic unit := `[ intros, /- We use `_root_.*` in the following tactics because names are resolved at tactic execution time in interactive mode. See PR #1913 TODO(Leo): This is probably not the only instance of this problem. `[ ... ] blocks are convenient to use because they allow us to use the interactive mode to write non interactive tactics. One potential fix for this issue is to resolve names in `[ ... ] at tactic compilation time. After this issue is fixed, we should remove the `_root_.*` workaround. -/ apply _root_.propext, apply _root_.iff.intro, { tactic.apply_inj_lemma }, { intro _, try { cases_matching* _ ∧ _ }, refl <|> { congr; { assumption <|> subst_vars } } } ] end tactic /- Define inj_eq lemmas for inductive datatypes that were declared before `mk_inj_eq` -/ universes u v lemma sum.inl.inj_eq {α : Type u} (β : Type v) (a₁ a₂ : α) : (@sum.inl α β a₁ = sum.inl a₂) = (a₁ = a₂) := by tactic.mk_inj_eq lemma sum.inr.inj_eq (α : Type u) {β : Type v} (b₁ b₂ : β) : (@sum.inr α β b₁ = sum.inr b₂) = (b₁ = b₂) := by tactic.mk_inj_eq lemma psum.inl.inj_eq {α : Sort u} (β : Sort v) (a₁ a₂ : α) : (@psum.inl α β a₁ = psum.inl a₂) = (a₁ = a₂) := by tactic.mk_inj_eq lemma psum.inr.inj_eq (α : Sort u) {β : Sort v} (b₁ b₂ : β) : (@psum.inr α β b₁ = psum.inr b₂) = (b₁ = b₂) := by tactic.mk_inj_eq lemma sigma.mk.inj_eq {α : Type u} {β : α → Type v} (a₁ : α) (b₁ : β a₁) (a₂ : α) (b₂ : β a₂) : (sigma.mk a₁ b₁ = sigma.mk a₂ b₂) = (a₁ = a₂ ∧ b₁ == b₂) := by tactic.mk_inj_eq lemma psigma.mk.inj_eq {α : Sort u} {β : α → Sort v} (a₁ : α) (b₁ : β a₁) (a₂ : α) (b₂ : β a₂) : (psigma.mk a₁ b₁ = psigma.mk a₂ b₂) = (a₁ = a₂ ∧ b₁ == b₂) := by tactic.mk_inj_eq lemma subtype.mk.inj_eq {α : Sort u} {p : α → Prop} (a₁ : α) (h₁ : p a₁) (a₂ : α) (h₂ : p a₂) : (subtype.mk a₁ h₁ = subtype.mk a₂ h₂) = (a₁ = a₂) := by tactic.mk_inj_eq lemma option.some.inj_eq {α : Type u} (a₁ a₂ : α) : (some a₁ = some a₂) = (a₁ = a₂) := by tactic.mk_inj_eq lemma list.cons.inj_eq {α : Type u} (h₁ : α) (t₁ : list α) (h₂ : α) (t₂ : list α) : (list.cons h₁ t₁ = list.cons h₂ t₂) = (h₁ = h₂ ∧ t₁ = t₂) := by tactic.mk_inj_eq lemma nat.succ.inj_eq (n₁ n₂ : nat) : (nat.succ n₁ = nat.succ n₂) = (n₁ = n₂) := by tactic.mk_inj_eq
26c94e67ca4a71bd92e6805e6b625e8f1ae75b19
7cef822f3b952965621309e88eadf618da0c8ae9
/src/tactic/split_ifs.lean
c2a7a6bd140fe1acf533303b0768c3e452412540
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
3,114
lean
/- Copyright (c) 2018 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner. Tactic to split if-then-else-expressions. -/ open expr tactic namespace tactic open interactive meta def find_if_cond : expr → option expr | e := e.fold none $ λ e _ acc, acc <|> do c ← match e with | `(@ite %%c %%_ _ _ _) := some c | `(@dite %%c %%_ _ _ _) := some c | _ := none end, guard ¬c.has_var, find_if_cond c <|> return c meta def find_if_cond_at (at_ : loc) : tactic (option expr) := do lctx ← at_.get_locals, lctx ← lctx.mmap infer_type, tgt ← target, let es := if at_.include_goal then tgt::lctx else lctx, return $ find_if_cond $ es.foldr app (default expr) run_cmd mk_simp_attr `split_if_reduction run_cmd add_doc_string `simp_attr.split_if_reduction "Simp set for if-then-else statements" attribute [split_if_reduction] if_pos if_neg dif_pos dif_neg meta def reduce_ifs_at (at_ : loc) : tactic unit := do sls ← get_user_simp_lemmas `split_if_reduction, let cfg : simp_config := { fail_if_unchanged := ff }, let discharger := assumption <|> (applyc `not_not_intro >> assumption), hs ← at_.get_locals, hs.mmap' (λ h, simp_hyp sls [] h cfg discharger >> skip), when at_.include_goal (simp_target sls [] cfg discharger) meta def split_if1 (c : expr) (n : name) (at_ : loc) : tactic unit := by_cases c n; reduce_ifs_at at_ private meta def get_next_name (names : ref (list name)) : tactic name := do ns ← read_ref names, match ns with | [] := get_unused_name `h | n::ns := do write_ref names ns, return n end private meta def value_known (c : expr) : tactic bool := do lctx ← local_context, lctx ← lctx.mmap infer_type, return $ c ∈ lctx ∨ `(¬%%c) ∈ lctx private meta def split_ifs_core (at_ : loc) (names : ref (list name)) : list expr → tactic unit | done := do some cond ← find_if_cond_at at_ | fail "no if-then-else expressions to split", let cond := match cond with `(¬%%p) := p | p := p end, if cond ∈ done then skip else do no_split ← value_known cond, if no_split then reduce_ifs_at at_; try (split_ifs_core (cond :: done)) else do n ← get_next_name names, split_if1 cond n at_; try (split_ifs_core (cond :: done)) meta def split_ifs (names : list name) (at_ : loc := loc.ns [none]) := using_new_ref names $ λ names, split_ifs_core at_ names [] namespace interactive open interactive interactive.types expr lean.parser /-- Splits all if-then-else-expressions into multiple goals. Given a goal of the form `g (if p then x else y)`, `split_ifs` will produce two goals: `p ⊢ g x` and `¬p ⊢ g y`. If there are multiple ite-expressions, then `split_ifs` will split them all, starting with a top-most one whose condition does not contain another ite-expression. `split_ifs at *` splits all ite-expressions in all hypotheses as well as the goal. `split_ifs with h₁ h₂ h₃` overrides the default names for the hypotheses. -/ meta def split_ifs (at_ : parse location) (names : parse with_ident_list) : tactic unit := tactic.split_ifs names at_ end interactive end tactic
747509028b968a4881f75513b4b3d165319e6268
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/dep_coe_to_fn2.lean
8de9de391b4f9945f61e433b8aadf8845d1ad8b3
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
266
lean
universe variables u v structure Func := (A : Type u) (B : A → Type v) (fn : Π a, B a → B a) instance F_to_fn : has_coe_to_fun Func := { F := λ f, Π a, f^.B a → f^.B a, coe := λ f, f^.fn } variables (f : Func) (a : f^.A) (b : f^.B a) #check (f a b)
1adea0ff02e8c643898adf93e7b253940d40e76b
367134ba5a65885e863bdc4507601606690974c1
/src/data/real/cardinality.lean
3a47fb7f7c9a9f60ef777ba6f3f273672b273ae9
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
9,433
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import set_theory.cardinal_ordinal import analysis.specific_limits import data.rat.denumerable import data.set.intervals.image_preimage /-! # The cardinality of the reals This file shows that the real numbers have cardinality continuum, i.e. `#ℝ = 2^ω`. We shows that `#ℝ ≤ 2^ω` by noting that every real number is determined by a Cauchy-sequence of the form `ℕ → ℚ`, which has cardinality `2^ω`. To show that `#ℝ ≥ 2^ω` we define an injection from `{0, 1} ^ ℕ` to `ℝ` with `f ↦ Σ n, f n * (1 / 3) ^ n`. We conclude that all intervals with distinct endpoints have cardinality continuum. ## Main definitions * `cardinal.cantor_function` is the function that sends `f` in `{0, 1} ^ ℕ` to `ℝ` by `f ↦ Σ' n, f n * (1 / 3) ^ n` ## Main statements * `cardinal.mk_real : #ℝ = 2 ^ omega`: the reals have cardinality continuum. * `cardinal.not_countable_real`: the universal set of real numbers is not countable. We can use this same proof to show that all the other sets in this file are not countable. * 8 lemmas of the form `mk_Ixy_real` for `x,y ∈ {i,o,c}` state that intervals on the reals have cardinality continuum. ## Tags continuum, cardinality, reals, cardinality of the reals -/ open nat set open_locale cardinal noncomputable theory namespace cardinal variables {c : ℝ} {f g : ℕ → bool} {n : ℕ} /-- The body of the sum in `cantor_function`. `cantor_function_aux c f n = c ^ n` if `f n = tt`; `cantor_function_aux c f n = 0` if `f n = ff`. -/ def cantor_function_aux (c : ℝ) (f : ℕ → bool) (n : ℕ) : ℝ := cond (f n) (c ^ n) 0 @[simp] lemma cantor_function_aux_tt (h : f n = tt) : cantor_function_aux c f n = c ^ n := by simp [cantor_function_aux, h] @[simp] lemma cantor_function_aux_ff (h : f n = ff) : cantor_function_aux c f n = 0 := by simp [cantor_function_aux, h] lemma cantor_function_aux_nonneg (h : 0 ≤ c) : 0 ≤ cantor_function_aux c f n := by { cases h' : f n; simp [h'], apply pow_nonneg h } lemma cantor_function_aux_eq (h : f n = g n) : cantor_function_aux c f n = cantor_function_aux c g n := by simp [cantor_function_aux, h] lemma cantor_function_aux_succ (f : ℕ → bool) : (λ n, cantor_function_aux c f (n + 1)) = λ n, c * cantor_function_aux c (λ n, f (n + 1)) n := by { ext n, cases h : f (n + 1); simp [h, pow_succ] } lemma summable_cantor_function (f : ℕ → bool) (h1 : 0 ≤ c) (h2 : c < 1) : summable (cantor_function_aux c f) := begin apply (summable_geometric_of_lt_1 h1 h2).summable_of_eq_zero_or_self, intro n, cases h : f n; simp [h] end /-- `cantor_function c (f : ℕ → bool)` is `Σ n, f n * c ^ n`, where `tt` is interpreted as `1` and `ff` is interpreted as `0`. It is implemented using `cantor_function_aux`. -/ def cantor_function (c : ℝ) (f : ℕ → bool) : ℝ := ∑' n, cantor_function_aux c f n lemma cantor_function_le (h1 : 0 ≤ c) (h2 : c < 1) (h3 : ∀ n, f n → g n) : cantor_function c f ≤ cantor_function c g := begin apply tsum_le_tsum _ (summable_cantor_function f h1 h2) (summable_cantor_function g h1 h2), intro n, cases h : f n, simp [h, cantor_function_aux_nonneg h1], replace h3 : g n = tt := h3 n h, simp [h, h3] end lemma cantor_function_succ (f : ℕ → bool) (h1 : 0 ≤ c) (h2 : c < 1) : cantor_function c f = cond (f 0) 1 0 + c * cantor_function c (λ n, f (n+1)) := begin rw [cantor_function, tsum_eq_zero_add (summable_cantor_function f h1 h2)], rw [cantor_function_aux_succ, tsum_mul_left], refl end /-- `cantor_function c` is strictly increasing with if `0 < c < 1/2`, if we endow `ℕ → bool` with a lexicographic order. The lexicographic order doesn't exist for these infinitary products, so we explicitly write out what it means. -/ lemma increasing_cantor_function (h1 : 0 < c) (h2 : c < 1 / 2) {n : ℕ} {f g : ℕ → bool} (hn : ∀(k < n), f k = g k) (fn : f n = ff) (gn : g n = tt) : cantor_function c f < cantor_function c g := begin have h3 : c < 1, { apply h2.trans, norm_num }, induction n with n ih generalizing f g, { let f_max : ℕ → bool := λ n, nat.rec ff (λ _ _, tt) n, have hf_max : ∀n, f n → f_max n, { intros n hn, cases n, rw [fn] at hn, contradiction, apply rfl }, let g_min : ℕ → bool := λ n, nat.rec tt (λ _ _, ff) n, have hg_min : ∀n, g_min n → g n, { intros n hn, cases n, rw [gn], apply rfl, contradiction }, apply (cantor_function_le (le_of_lt h1) h3 hf_max).trans_lt, refine lt_of_lt_of_le _ (cantor_function_le (le_of_lt h1) h3 hg_min), have : c / (1 - c) < 1, { rw [div_lt_one, lt_sub_iff_add_lt], { convert add_lt_add h2 h2, norm_num }, rwa sub_pos }, convert this, { rw [cantor_function_succ _ (le_of_lt h1) h3, div_eq_mul_inv, ←tsum_geometric_of_lt_1 (le_of_lt h1) h3], apply zero_add }, { apply tsum_eq_single 0, intros n hn, cases n, contradiction, refl, apply_instance }}, rw [cantor_function_succ f (le_of_lt h1) h3, cantor_function_succ g (le_of_lt h1) h3], rw [hn 0 $ zero_lt_succ n], apply add_lt_add_left, rw mul_lt_mul_left h1, exact ih (λ k hk, hn _ $ succ_lt_succ hk) fn gn end /-- `cantor_function c` is injective if `0 < c < 1/2`. -/ lemma cantor_function_injective (h1 : 0 < c) (h2 : c < 1 / 2) : function.injective (cantor_function c) := begin intros f g hfg, classical, by_contra h, revert hfg, have : ∃n, f n ≠ g n, { rw [←not_forall], intro h', apply h, ext, apply h' }, let n := nat.find this, have hn : ∀ (k : ℕ), k < n → f k = g k, { intros k hk, apply of_not_not, exact nat.find_min this hk }, cases fn : f n, { apply ne_of_lt, refine increasing_cantor_function h1 h2 hn fn _, apply eq_tt_of_not_eq_ff, rw [←fn], apply ne.symm, exact nat.find_spec this }, { apply ne_of_gt, refine increasing_cantor_function h1 h2 (λ k hk, (hn k hk).symm) _ fn, apply eq_ff_of_not_eq_tt, rw [←fn], apply ne.symm, exact nat.find_spec this } end /-- The cardinality of the reals, as a type. -/ lemma mk_real : #ℝ = 2 ^ omega.{0} := begin apply le_antisymm, { rw real.equiv_Cauchy.cardinal_eq, apply mk_quotient_le.trans, apply (mk_subtype_le _).trans, rw [←power_def, mk_nat, mk_rat, power_self_eq (le_refl _)] }, { convert mk_le_of_injective (cantor_function_injective _ _), rw [←power_def, mk_bool, mk_nat], exact 1 / 3, norm_num, norm_num } end /-- The cardinality of the reals, as a set. -/ lemma mk_univ_real : #(set.univ : set ℝ) = 2 ^ omega.{0} := by rw [mk_univ, mk_real] /-- The reals are not countable. -/ lemma not_countable_real : ¬ countable (set.univ : set ℝ) := by { rw [countable_iff, not_le, mk_univ_real], apply cantor } /-- The cardinality of the interval (a, ∞). -/ lemma mk_Ioi_real (a : ℝ) : #(Ioi a) = 2 ^ omega.{0} := begin refine le_antisymm (mk_real ▸ mk_set_le _) _, rw [← not_lt], intro h, refine ne_of_lt _ mk_univ_real, have hu : Iio a ∪ {a} ∪ Ioi a = set.univ, { convert Iic_union_Ioi, exact Iio_union_right }, rw ← hu, refine lt_of_le_of_lt (mk_union_le _ _) _, refine lt_of_le_of_lt (add_le_add_right (mk_union_le _ _) _) _, have h2 : (λ x, a + a - x) '' Ioi a = Iio a, { convert image_const_sub_Ioi _ _, simp }, rw ← h2, refine add_lt_of_lt (cantor _).le _ h, refine add_lt_of_lt (cantor _).le (mk_image_le.trans_lt h) _, rw mk_singleton, exact one_lt_omega.trans (cantor _) end /-- The cardinality of the interval [a, ∞). -/ lemma mk_Ici_real (a : ℝ) : #(Ici a) = 2 ^ omega.{0} := le_antisymm (mk_real ▸ mk_set_le _) (mk_Ioi_real a ▸ mk_le_mk_of_subset Ioi_subset_Ici_self) /-- The cardinality of the interval (-∞, a). -/ lemma mk_Iio_real (a : ℝ) : #(Iio a) = 2 ^ omega.{0} := begin refine le_antisymm (mk_real ▸ mk_set_le _) _, have h2 : (λ x, a + a - x) '' Iio a = Ioi a, { convert image_const_sub_Iio _ _, simp }, exact mk_Ioi_real a ▸ h2 ▸ mk_image_le end /-- The cardinality of the interval (-∞, a]. -/ lemma mk_Iic_real (a : ℝ) : #(Iic a) = 2 ^ omega.{0} := le_antisymm (mk_real ▸ mk_set_le _) (mk_Iio_real a ▸ mk_le_mk_of_subset Iio_subset_Iic_self) /-- The cardinality of the interval (a, b). -/ lemma mk_Ioo_real {a b : ℝ} (h : a < b) : #(Ioo a b) = 2 ^ omega.{0} := begin refine le_antisymm (mk_real ▸ mk_set_le _) _, have h1 : #((λ x, x - a) '' Ioo a b) ≤ #(Ioo a b) := mk_image_le, refine le_trans _ h1, rw [image_sub_const_Ioo, sub_self], replace h := sub_pos_of_lt h, have h2 : #(has_inv.inv '' Ioo 0 (b - a)) ≤ #(Ioo 0 (b - a)) := mk_image_le, refine le_trans _ h2, rw [image_inv_Ioo_0_left h, mk_Ioi_real] end /-- The cardinality of the interval [a, b). -/ lemma mk_Ico_real {a b : ℝ} (h : a < b) : #(Ico a b) = 2 ^ omega.{0} := le_antisymm (mk_real ▸ mk_set_le _) (mk_Ioo_real h ▸ mk_le_mk_of_subset Ioo_subset_Ico_self) /-- The cardinality of the interval [a, b]. -/ lemma mk_Icc_real {a b : ℝ} (h : a < b) : #(Icc a b) = 2 ^ omega.{0} := le_antisymm (mk_real ▸ mk_set_le _) (mk_Ioo_real h ▸ mk_le_mk_of_subset Ioo_subset_Icc_self) /-- The cardinality of the interval (a, b]. -/ lemma mk_Ioc_real {a b : ℝ} (h : a < b) : #(Ioc a b) = 2 ^ omega.{0} := le_antisymm (mk_real ▸ mk_set_le _) (mk_Ioo_real h ▸ mk_le_mk_of_subset Ioo_subset_Ioc_self) end cardinal
af5c3eaf30faf84e63905a83f665905c1d18c9fb
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_cc_subsingleton2.lean
fb4f9bb02356a1db8f5363cd119ee22a690237af
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
312
lean
import data.unit open nat unit set_option blast.strategy "cc" constant r {A B : Type} : A → B → A definition ex1 (a b c d : unit) : r a b = r c d := by blast -- The congruence closure module does not automatically merge subsingleton equivalence classes. -- -- example (a b : unit) : a = b := -- by blast
114f9c3524b74d43c90bde470f2d0e7e7a093c9f
37da0369b6c03e380e057bf680d81e6c9fdf9219
/hott/homotopy/hopf.hlean
f3ef47af8fc963a0cb57355fdce9da983d8d78fb
[ "Apache-2.0" ]
permissive
kodyvajjha/lean2
72b120d95c3a1d77f54433fa90c9810e14a931a4
227fcad22ab2bc27bb7471be7911075d101ba3f9
refs/heads/master
1,627,157,512,295
1,501,855,676,000
1,504,809,427,000
109,317,326
0
0
null
1,509,839,253,000
1,509,655,713,000
C++
UTF-8
Lean
false
false
8,074
hlean
/- Copyright (c) 2016 Ulrik Buchholtz and Egbert Rijke. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ulrik Buchholtz, Egbert Rijke H-spaces and the Hopf construction -/ import types.equiv .wedge .join open eq eq.ops equiv is_equiv is_conn is_trunc trunc susp join pointed namespace hopf structure h_space [class] (A : Type) extends has_mul A, has_one A := (one_mul : ∀a, mul one a = a) (mul_one : ∀a, mul a one = a) section variable {A : Type} variable [H : h_space A] include H definition one_mul (a : A) : 1 * a = a := !h_space.one_mul definition mul_one (a : A) : a * 1 = a := !h_space.mul_one definition h_space_equiv_closed {B : Type} (f : A ≃ B) : h_space B := ⦃ h_space, one := f 1, mul := (λb b', f (f⁻¹ b * f⁻¹ b')), one_mul := by intro b; rewrite [to_left_inv,one_mul,to_right_inv], mul_one := by intro b; rewrite [to_left_inv,mul_one,to_right_inv] ⦄ /- Lemma 8.5.5. If A is 0-connected, then left and right multiplication are equivalences -/ variable [K : is_conn 0 A] include K definition is_equiv_mul_left [instance] : Π(a : A), is_equiv (λx, a * x) := begin apply is_conn_fun.elim -1 (is_conn_fun_from_unit -1 A 1) (λa, trunctype.mk' -1 (is_equiv (λx, a * x))), intro z, change is_equiv (λx : A, 1 * x), apply is_equiv.homotopy_closed id, intro x, apply inverse, apply one_mul end definition is_equiv_mul_right [instance] : Π(a : A), is_equiv (λx, x * a) := begin apply is_conn_fun.elim -1 (is_conn_fun_from_unit -1 A 1) (λa, trunctype.mk' -1 (is_equiv (λx, x * a))), intro z, change is_equiv (λx : A, x * 1), apply is_equiv.homotopy_closed id, intro x, apply inverse, apply mul_one end end section variable (A : Type) variables [H : h_space A] [K : is_conn 0 A] include H K definition hopf [unfold 4] : susp A → Type := susp.elim_type A A (λa, equiv.mk (λx, a * x) !is_equiv_mul_left) /- Lemma 8.5.7. The total space is A * A -/ open prod prod.ops protected definition total : sigma (hopf A) ≃ join A A := begin apply equiv.trans (susp.flattening A A A _), unfold join, apply equiv.trans (pushout.symm pr₂ (λz : A × A, z.1 * z.2)), fapply pushout.equiv, { fapply equiv.MK (λz : A × A, (z.1 * z.2, z.2)) (λz : A × A, ((λx, x * z.2)⁻¹ z.1, z.2)), { intro z, induction z with u v, esimp, exact prod_eq (right_inv (λx, x * v) u) idp }, { intro z, induction z with a b, esimp, exact prod_eq (left_inv (λx, x * b) a) idp } }, { reflexivity }, { reflexivity }, { reflexivity }, { reflexivity } end end /- If A is a K(G,1), then A is deloopable. Main lemma of Licata-Finster. -/ section parameters (A : Type) [T : is_trunc 1 A] [K : is_conn 0 A] [H : h_space A] (coh : one_mul 1 = mul_one 1 :> (1 * 1 = 1 :> A)) definition P [reducible] : susp A → Type := λx, trunc 1 (north = x) include K H T local abbreviation codes [reducible] : susp A → Type := hopf A definition transport_codes_merid (a a' : A) : transport codes (merid a) a' = a * a' :> A := ap10 (elim_type_merid _ _ _ a) a' definition is_trunc_codes [instance] (x : susp A) : is_trunc 1 (codes x) := begin induction x with a, do 2 apply T, apply is_prop.elimo end definition encode₀ {x : susp A} : north = x → codes x := λp, transport codes p (by change A; exact 1) definition encode {x : susp A} : P x → codes x := λp, trunc.elim encode₀ p definition decode' : A → P (@north A) := λa, tr (merid a ⬝ (merid 1)⁻¹) definition transport_codes_merid_one_inv (a : A) : transport codes (merid 1)⁻¹ a = a := ap10 (elim_type_merid_inv _ _ _ 1) a ⬝ begin apply to_inv_eq_of_eq, esimp, refine !one_mul⁻¹ end proposition encode_decode' (a : A) : encode (decode' a) = a := begin esimp [encode, decode', encode₀], refine !con_tr ⬝ _, refine (ap (transport _ _) !transport_codes_merid ⬝ !transport_codes_merid_one_inv) ⬝ _, apply mul_one end include coh open pointed proposition homomorphism : Πa a' : A, tr (merid (a * a')) = tr (merid a' ⬝ (merid 1)⁻¹ ⬝ merid a) :> trunc 1 (@north A = @south A) := begin fapply @wedge_extension.ext (pointed.MK A 1) (pointed.MK A 1) 0 0 K K (λa a' : A, tr (merid (a * a')) = tr (merid a' ⬝ (merid 1)⁻¹ ⬝ merid a)), { intros a a', apply is_trunc_eq, apply is_trunc_trunc }, { change Πa : A, tr (merid (a * 1)) = tr (merid 1 ⬝ (merid 1)⁻¹ ⬝ merid a) :> trunc 1 (@north A = @south A), intro a, apply ap tr, exact calc merid (a * 1) = merid a : ap merid (mul_one a) ... = merid 1 ⬝ (merid 1)⁻¹ ⬝ merid a : (idp_con (merid a))⁻¹ ⬝ ap (λw, w ⬝ merid a) (con.right_inv (merid 1))⁻¹ }, { change Πa' : A, tr (merid (1 * a')) = tr (merid a' ⬝ (merid 1)⁻¹ ⬝ merid 1) :> trunc 1 (@north A = @south A), intro a', apply ap tr, exact calc merid (1 * a') = merid a' : ap merid (one_mul a') ... = merid a' ⬝ (merid 1)⁻¹ ⬝ merid 1 : ap (λw, merid a' ⬝ w) (con.left_inv (merid 1))⁻¹ ⬝ (con.assoc' (merid a') (merid 1)⁻¹ (merid 1)) }, { apply ap02 tr, esimp, fapply concat2, { apply ap02 merid, exact coh⁻¹ }, { assert e : Π(X : Type)(x y : X)(p : x = y), (idp_con p)⁻¹ ⬝ ap (λw : x = x, w ⬝ p) (con.right_inv p)⁻¹ = ap (concat p) (con.left_inv p)⁻¹ ⬝ con.assoc' p p⁻¹ p, { intros X x y p, cases p, reflexivity }, apply e } } end definition decode {x : susp A} : codes x → P x := begin induction x, { exact decode' }, { exact (λa, tr (merid a)) }, { apply pi.arrow_pathover_left, esimp, intro a', apply pathover_of_tr_eq, krewrite susp.elim_type_merid, esimp, krewrite [trunc_transport,eq_transport_r], apply inverse, apply homomorphism } end definition decode_encode {x : susp A} : Πt : P x, decode (encode t) = t := begin apply trunc.rec, intro p, cases p, apply ap tr, apply con.right_inv end /- We define main_lemma by first defining its inverse, because normally equiv.MK changes the left_inv-component of an equivalence to adjointify it, but in this case we want the left_inv-component to be encode_decode'. So we adjointify its inverse, so that only the right_inv-component is changed. -/ definition main_lemma : trunc 1 (north = north :> susp A) ≃ A := (equiv.MK decode' encode decode_encode encode_decode')⁻¹ᵉ definition main_lemma_point : ptrunc 1 (Ω(susp A)) ≃* pointed.MK A 1 := pointed.pequiv_of_equiv main_lemma idp protected definition delooping : Ω (ptrunc 2 (susp A)) ≃* pointed.MK A 1 := loop_ptrunc_pequiv 1 (susp A) ⬝e* main_lemma_point /- characterization of the underlying pointed maps -/ definition to_pmap_main_lemma_point_pinv : main_lemma_point⁻¹ᵉ* ~* !ptr ∘* loop_susp_unit (pointed.MK A 1) := begin fapply phomotopy.mk, { intro a, reflexivity }, { reflexivity } end definition to_pmap_delooping_pinv : delooping⁻¹ᵉ* ~* Ω→ !ptr ∘* loop_susp_unit (pointed.MK A 1) := begin refine !trans_pinv ⬝* _, refine pwhisker_left _ !to_pmap_main_lemma_point_pinv ⬝* _, refine !passoc⁻¹* ⬝* _, refine pwhisker_right _ !ap1_ptr⁻¹*, end definition hopf_delooping_elim {B : Type*} (f : pointed.MK A 1 →* Ω B) [H2 : is_trunc 2 B] : Ω→(ptrunc.elim 2 (susp_elim f)) ∘* (hopf.delooping A coh)⁻¹ᵉ* ~* f := begin refine pwhisker_left _ !to_pmap_delooping_pinv ⬝* _, refine !passoc⁻¹* ⬝* _, refine pwhisker_right _ (!ap1_pcompose⁻¹* ⬝* ap1_phomotopy !ptrunc_elim_ptr) ⬝* _, apply ap1_susp_elim end end end hopf
499d614455c1a82c861cf0453111c1c61c797a67
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Elab.lean
7b518d0b23e81ea9031164b2f9a8839ae8978d40
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
1,163
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Import import Lean.Elab.Exception import Lean.Elab.Command import Lean.Elab.Term import Lean.Elab.App import Lean.Elab.Binders import Lean.Elab.LetRec import Lean.Elab.Frontend import Lean.Elab.BuiltinNotation import Lean.Elab.Declaration import Lean.Elab.Tactic import Lean.Elab.Match -- HACK: must come after `Match` because builtin elaborators (for `match` in this case) do not take priorities import Lean.Elab.Quotation import Lean.Elab.Syntax import Lean.Elab.Do import Lean.Elab.StructInst import Lean.Elab.Inductive import Lean.Elab.Structure import Lean.Elab.Print import Lean.Elab.MutualDef import Lean.Elab.PreDefinition import Lean.Elab.Deriving import Lean.Elab.DeclarationRange import Lean.Elab.Extra import Lean.Elab.GenInjective import Lean.Elab.BuiltinTerm import Lean.Elab.Arg import Lean.Elab.PatternVar import Lean.Elab.ElabRules import Lean.Elab.Macro import Lean.Elab.Notation import Lean.Elab.Mixfix import Lean.Elab.MacroRules import Lean.Elab.BuiltinCommand
7d40e79c87ee1cc5a0fc138437c8a28e3b7ad9fb
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/order/algebra.lean
c78a8c6f615bd3950a17fbaef261bf16d79b8dcd
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,394
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.algebra.basic import algebra.order.smul /-! # Ordered algebras An ordered algebra is an ordered semiring, which is an algebra over an ordered commutative semiring, for which scalar multiplication is "compatible" with the two orders. The prototypical example is 2x2 matrices over the reals or complexes (or indeed any C^* algebra) where the ordering the one determined by the positive cone of positive operators, i.e. `A ≤ B` iff `B - A = star R * R` for some `R`. (We don't yet have this example in mathlib.) ## Implementation Because the axioms for an ordered algebra are exactly the same as those for the underlying module being ordered, we don't actually introduce a new class, but just use the `ordered_smul` mixin. ## Tags ordered algebra -/ section ordered_algebra variables {R A : Type*} {a b : A} {r : R} variables [ordered_comm_ring R] [ordered_ring A] [algebra R A] [ordered_smul R A] lemma algebra_map_monotone : monotone (algebra_map R A) := λ a b h, begin rw [algebra.algebra_map_eq_smul_one, algebra.algebra_map_eq_smul_one, ←sub_nonneg, ←sub_smul], transitivity (b - a) • (0 : A), { simp, }, { exact smul_le_smul_of_nonneg zero_le_one (sub_nonneg.mpr h) } end end ordered_algebra
9eebdf7d792adfcd3132ab57dbbe5f10592400b7
f1dc39e1c68f71465c8bf79910c4664d03824751
/library/system/io.lean
6a907d654dd314164b74f3b038c41dfddfe46e1c
[ "Apache-2.0" ]
permissive
kckennylau/lean-2
6504f45da07bc98b098d726b74130103be25885c
c9a9368bc0fd600d832bd56c5cb2124b8a523ef9
refs/heads/master
1,659,140,308,864
1,589,361,166,000
1,589,361,166,000
263,748,786
0
0
null
1,589,405,915,000
1,589,405,915,000
null
UTF-8
Lean
false
false
9,110
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson, Jared Roesch and Leonardo de Moura -/ import system.io_interface /- The following constants have a builtin implementation -/ constant io_core : Type → Type → Type /- Auxiliary definition used in the builtin implementation of monad_io_random_impl -/ def io_rand_nat : std_gen → nat → nat → nat × std_gen := rand_nat @[instance] constant monad_io_impl : monad_io io_core @[instance] constant monad_io_terminal_impl : monad_io_terminal io_core @[instance] constant monad_io_net_system_impl : monad_io_net_system io_core @[instance] constant monad_io_file_system_impl : monad_io_file_system io_core @[instance] meta constant monad_io_serial_impl : monad_io_serial io_core @[instance] constant monad_io_environment_impl : monad_io_environment io_core @[instance] constant monad_io_process_impl : monad_io_process io_core @[instance] constant monad_io_random_impl : monad_io_random io_core instance io_core_is_monad (e : Type) : monad (io_core e) := monad_io_is_monad io_core e instance io_core_is_monad_fail : monad_fail (io_core io.error) := monad_io_is_monad_fail io_core instance io_core_is_alternative : alternative (io_core io.error) := monad_io_is_alternative io_core @[reducible] def io (α : Type) := io_core io.error α namespace io /- Remark: the following definitions can be generalized and defined for any (m : Type -> Type -> Type) that implements the required type classes. However, the generalized versions are very inconvenient to use, (example: `#eval io.put_str "hello world"` does not work because we don't have enough information to infer `m`.). -/ def iterate {e α} (a : α) (f : α → io_core e (option α)) : io_core e α := monad_io.iterate e α a f def forever {e} (a : io_core e unit) : io_core e unit := iterate () $ λ _, a >> return (some ()) -- TODO(Leo): delete after we merge #1881 def catch {e₁ e₂ α} (a : io_core e₁ α) (b : e₁ → io_core e₂ α) : io_core e₂ α := monad_io.catch e₁ e₂ α a b def finally {α e} (a : io_core e α) (cleanup : io_core e unit) : io_core e α := do res ← catch (sum.inr <$> a) (return ∘ sum.inl), cleanup, match res with | sum.inr res := return res | sum.inl error := monad_io.fail _ _ error end protected def fail {α : Type} (s : string) : io α := monad_io.fail _ _ (io.error.other s) def put_str : string → io unit := monad_io_terminal.put_str def put_str_ln (s : string) : io unit := put_str s >> put_str "\n" def get_line : io string := monad_io_terminal.get_line def cmdline_args : io (list string) := return (monad_io_terminal.cmdline_args io_core) def print {α} [has_to_string α] (s : α) : io unit := put_str ∘ to_string $ s def print_ln {α} [has_to_string α] (s : α) : io unit := print s >> put_str "\n" def handle : Type := monad_io.handle io_core def mk_file_handle (s : string) (m : mode) (bin : bool := ff) : io handle := monad_io_file_system.mk_file_handle s m bin def stdin : io handle := monad_io_file_system.stdin def stderr : io handle := monad_io_file_system.stderr def stdout : io handle := monad_io_file_system.stdout meta def serialize : handle → expr → io unit := monad_io_serial.serialize meta def deserialize : handle → io expr := monad_io_serial.deserialize namespace env def get (env_var : string) : io (option string) := monad_io_environment.get_env env_var /-- get the current working directory -/ def get_cwd : io string := monad_io_environment.get_cwd /-- set the current working directory -/ def set_cwd (cwd : string) : io unit := monad_io_environment.set_cwd cwd end env namespace net def socket : Type := monad_io_net_system.socket io_core def listen : string → nat → io socket := monad_io_net_system.listen def accept : socket → io socket := monad_io_net_system.accept def connect : string → io socket := monad_io_net_system.connect def recv : socket → nat → io char_buffer := monad_io_net_system.recv def send : socket → char_buffer → io unit := monad_io_net_system.send def close : socket → io unit := monad_io_net_system.close end net namespace fs def is_eof : handle → io bool := monad_io_file_system.is_eof def flush : handle → io unit := monad_io_file_system.flush def close : handle → io unit := monad_io_file_system.close def read : handle → nat → io char_buffer := monad_io_file_system.read def write : handle → char_buffer → io unit := monad_io_file_system.write def get_char (h : handle) : io char := do b ← read h 1, if h : b.size = 1 then return $ b.read ⟨0, h.symm ▸ zero_lt_one⟩ else io.fail "get_char failed" def get_line : handle → io char_buffer := monad_io_file_system.get_line def put_char (h : handle) (c : char) : io unit := write h (mk_buffer.push_back c) def put_str (h : handle) (s : string) : io unit := write h (mk_buffer.append_string s) def put_str_ln (h : handle) (s : string) : io unit := put_str h s >> put_str h "\n" def read_to_end (h : handle) : io char_buffer := iterate mk_buffer $ λ r, do done ← is_eof h, if done then return none else do c ← read h 1024, return $ some (r ++ c) def read_file (s : string) (bin := ff) : io char_buffer := do h ← mk_file_handle s io.mode.read bin, read_to_end h def file_exists : string → io bool := monad_io_file_system.file_exists def dir_exists : string → io bool := monad_io_file_system.dir_exists def remove : string → io unit := monad_io_file_system.remove def rename : string → string → io unit := monad_io_file_system.rename def mkdir (path : string) (recursive : bool := ff) : io bool := monad_io_file_system.mkdir path recursive def rmdir : string → io bool := monad_io_file_system.rmdir end fs namespace proc def child : Type := monad_io_process.child io_core def child.stdin : child → handle := monad_io_process.stdin def child.stdout : child → handle := monad_io_process.stdout def child.stderr : child → handle := monad_io_process.stderr def spawn (p : io.process.spawn_args) : io child := monad_io_process.spawn p def wait (c : child) : io nat := monad_io_process.wait c def sleep (n : nat) : io unit := monad_io_process.sleep n end proc def set_rand_gen : std_gen → io unit := monad_io_random.set_rand_gen def rand (lo : nat := std_range.1) (hi : nat := std_range.2) : io nat := monad_io_random.rand lo hi end io meta constant format.print_using : format → options → io unit meta definition format.print (fmt : format) : io unit := format.print_using fmt options.mk meta definition pp_using {α : Type} [has_to_format α] (a : α) (o : options) : io unit := format.print_using (to_fmt a) o meta definition pp {α : Type} [has_to_format α] (a : α) : io unit := format.print (to_fmt a) /-- Run the external process specified by `args`. The process will run to completion with its output captured by a pipe, and read into `string` which is then returned. -/ def io.cmd (args : io.process.spawn_args) : io string := do child ← io.proc.spawn { stdout := io.process.stdio.piped, ..args }, buf ← io.fs.read_to_end child.stdout, io.fs.close child.stdout, exitv ← io.proc.wait child, when (exitv ≠ 0) $ io.fail $ "process exited with status " ++ repr exitv, return buf.to_string /-- This is the "back door" into the `io` monad, allowing IO computation to be performed during tactic execution. For this to be safe, the IO computation should be ideally free of side effects and independent of its environment. This primitive is used to invoke external tools (e.g., SAT and SMT solvers) from a tactic. IMPORTANT: this primitive can be used to implement `unsafe_perform_io {α : Type} : io α → option α` or `unsafe_perform_io {α : Type} [inhabited α] : io α → α`. This can be accomplished by executing the resulting tactic using an empty `tactic_state` (we have `tactic_state.mk_empty`). If `unsafe_perform_io` is defined, and used to perform side-effects, users need to take the following precautions: - Use `@[noinline]` attribute in any function to invokes `tactic.unsafe_perform_io`. Reason: if the call is inlined, the IO may be performed more than once. - Set `set_option compiler.cse false` before any function that invokes `tactic.unsafe_perform_io`. This option disables common subexpression elimination. Common subexpression elimination might combine two side effects that were meant to be separate. TODO[Leo]: add `[noinline]` attribute and option `compiler.cse`. -/ meta constant tactic.unsafe_run_io {α : Type} : io α → tactic α /-- Execute the given tactic with a tactic_state object that contains: - The current environment in the virtual machine. - The current set of options in the virtual machine. - Empty metavariable and local contexts. - One single goal of the form `⊢ true`. This action is mainly useful for writing tactics that inspect the environment. -/ meta constant io.run_tactic {α : Type} (a : tactic α) : io α
794706d605e856874cee7ca4ee24723b7f51e5e6
690889011852559ee5ac4dfea77092de8c832e7e
/src/data/finsupp.lean
55d253f6ed0c3f16ae7aa2429c3827521c531849
[ "Apache-2.0" ]
permissive
williamdemeo/mathlib
f6df180148f8acc91de9ba5e558976ab40a872c7
1fa03c29f9f273203bbffb79d10d31f696b3d317
refs/heads/master
1,584,785,260,929
1,572,195,914,000
1,572,195,913,000
138,435,193
0
0
Apache-2.0
1,529,789,739,000
1,529,789,739,000
null
UTF-8
Lean
false
false
61,672
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Type of functions with finite support. Functions with finite support provide the basis for the following concrete instances: * ℕ →₀ α: Polynomials (where α is a ring) * (σ →₀ ℕ) →₀ α: Multivariate Polynomials (again α is a ring, and σ are variable names) * α →₀ ℕ: Multisets * α →₀ ℤ: Abelian groups freely generated by α * β →₀ α: Linear combinations over β where α is the scalar ring Most of the theory assumes that the range is a commutative monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. A general advice is to not use α →₀ β directly, as the type class setup might not be fitting. The best is to define a copy and select the instances best suited. -/ import data.finset data.set.finite algebra.big_operators algebra.module noncomputable theory open_locale classical open finset variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Type*} {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} /-- `finsupp α β`, denoted `α →₀ β`, is the type of functions `f : α → β` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (α : Type*) (β : Type*) [has_zero β] := (support : finset α) (to_fun : α → β) (mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0) infixr ` →₀ `:25 := finsupp namespace finsupp section basic variable [has_zero β] instance : has_coe_to_fun (α →₀ β) := ⟨λ_, α → β, finsupp.to_fun⟩ instance : has_zero (α →₀ β) := ⟨⟨∅, (λ_, 0), λ _, ⟨false.elim, λ H, H rfl⟩⟩⟩ @[simp] lemma zero_apply {a : α} : (0 : α →₀ β) a = 0 := rfl @[simp] lemma support_zero : (0 : α →₀ β).support = ∅ := rfl instance : inhabited (α →₀ β) := ⟨0⟩ @[simp] lemma mem_support_iff {f : α →₀ β} : ∀{a:α}, a ∈ f.support ↔ f a ≠ 0 := f.mem_support_to_fun lemma not_mem_support_iff {f : α →₀ β} {a} : a ∉ f.support ↔ f a = 0 := not_iff_comm.1 mem_support_iff.symm @[extensionality] lemma ext : ∀{f g : α →₀ β}, (∀a, f a = g a) → f = g | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin have : f = g, { funext a, exact h a }, subst this, have : s = t, { ext a, exact (hf a).trans (hg a).symm }, subst this end lemma ext_iff {f g : α →₀ β} : f = g ↔ (∀a:α, f a = g a) := ⟨by rintros rfl a; refl, ext⟩ @[simp] lemma support_eq_empty {f : α →₀ β} : f.support = ∅ ↔ f = 0 := ⟨assume h, ext $ assume a, by_contradiction $ λ H, (finset.ext.1 h a).1 $ mem_support_iff.2 H, by rintro rfl; refl⟩ instance finsupp.decidable_eq [decidable_eq α] [decidable_eq β] : decidable_eq (α →₀ β) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ⟨assume ⟨h₁, h₂⟩, ext $ assume a, if h : a ∈ f.support then h₂ a h else have hf : f a = 0, by rwa [mem_support_iff, not_not] at h, have hg : g a = 0, by rwa [h₁, mem_support_iff, not_not] at h, by rw [hf, hg], by rintro rfl; exact ⟨rfl, λ _ _, rfl⟩⟩ lemma finite_supp (f : α →₀ β) : set.finite {a | f a ≠ 0} := ⟨set.fintype_of_finset f.support (λ _, mem_support_iff)⟩ lemma support_subset_iff {s : set α} {f : α →₀ β} : ↑f.support ⊆ s ↔ (∀a∉s, f a = 0) := by simp only [set.subset_def, mem_coe, mem_support_iff]; exact forall_congr (assume a, @not_imp_comm _ _ (classical.dec _) (classical.dec _)) def equiv_fun_on_fintype [fintype α] : (α →₀ β) ≃ (α → β) := ⟨λf a, f a, λf, mk (finset.univ.filter $ λa, f a ≠ 0) f (by simp), begin intro f, ext a, refl end, begin intro f, ext a, refl end⟩ end basic section single variables [has_zero β] {a a' : α} {b : β} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : α) (b : β) : α →₀ β := ⟨if b = 0 then ∅ else finset.singleton a, λ a', if a = a' then b else 0, λ a', begin by_cases hb : b = 0; by_cases a = a'; simp only [hb, h, if_pos, if_false, mem_singleton], { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨λ _, hb, λ _, rfl⟩ }, { exact ⟨λ H _, h H.symm, λ H, (H rfl).elim⟩ } end⟩ lemma single_apply : (single a b : α →₀ β) a' = if a = a' then b else 0 := rfl @[simp] lemma single_eq_same : (single a b : α →₀ β) a = b := if_pos rfl @[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ β) a' = 0 := if_neg h @[simp] lemma single_zero : (single a 0 : α →₀ β) = 0 := ext $ assume a', begin by_cases h : a = a', { rw [h, single_eq_same, zero_apply] }, { rw [single_eq_of_ne h, zero_apply] } end lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support ⊆ {a} := show ite _ _ _ ⊆ _, by split_ifs; [exact empty_subset _, exact subset.refl _] lemma injective_single (a : α) : function.injective (single a : β → α →₀ β) := assume b₁ b₂ eq, have (single a b₁ : α →₀ β) a = (single a b₂ : α →₀ β) a, by rw eq, by rwa [single_eq_same, single_eq_same] at this lemma single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : β) : single a₁ b₁ = single a₂ b₂ ↔ ((a₁ = a₂ ∧ b₁ = b₂) ∨ (b₁ = 0 ∧ b₂ = 0)) := begin split, { assume eq, by_cases a₁ = a₂, { refine or.inl ⟨h, _⟩, rwa [h, (injective_single a₂).eq_iff] at eq }, { rw [finsupp.ext_iff] at eq, have h₁ := eq a₁, have h₂ := eq a₂, simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ h₂, exact or.inr ⟨h₁, h₂.symm⟩ } }, { rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩), { refl }, { rw [single_zero, single_zero] } } end lemma single_right_inj (h : b ≠ 0) : single a b = single a' b ↔ a = a' := ⟨λ H, by simpa [h, single_eq_single_iff] using H, λ H, by rw [H]⟩ lemma single_eq_zero : single a b = 0 ↔ b = 0 := ⟨λ h, by { rw ext_iff at h, simpa only [finsupp.single_eq_same, finsupp.zero_apply] using h a }, λ h, by rw [h, single_zero]⟩ lemma single_swap {α β : Type*} [has_zero β] (a₁ a₂ : α) (b : β) : (single a₁ b : α → β) a₂ = (single a₂ b : α → β) a₁ := by simp [single_apply]; ac_refl lemma unique_single [unique α] (x : α →₀ β) : x = single (default α) (x (default α)) := by ext i; simp [unique.eq_default i] @[simp] lemma unique_single_eq_iff [unique α] {b' : β} : single a b = single a' b' ↔ b = b' := begin rw [single_eq_single_iff], split, { rintros (⟨_, rfl⟩ | ⟨rfl, rfl⟩); refl }, { intro h, left, exact ⟨subsingleton.elim _ _, h⟩ } end end single section on_finset variables [has_zero β] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the set `s`. The function needs to be 0 outside of `s`. Use this when the set needs filtered anyway, otherwise often better set representation is available. -/ def on_finset (s : finset α) (f : α → β) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ β := ⟨s.filter (λa, f a ≠ 0), f, assume a, classical.by_cases (assume h : f a = 0, by rw mem_filter; exact ⟨and.right, λ H, (H h).elim⟩) (assume h : f a ≠ 0, by rw mem_filter; simp only [iff_true_intro h, hf a h, true_and])⟩ @[simp] lemma on_finset_apply {s : finset α} {f : α → β} {hf a} : (on_finset s f hf : α →₀ β) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset α} {f : α → β} {hf} : (on_finset s f hf).support ⊆ s := filter_subset _ end on_finset section map_range variables [has_zero β₁] [has_zero β₂] /-- The composition of `f : β₁ → β₂` and `g : α →₀ β₁` is `map_range f hf g : α →₀ β₂`, well defined when `f 0 = 0`. -/ def map_range (f : β₁ → β₂) (hf : f 0 = 0) (g : α →₀ β₁) : α →₀ β₂ := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; exact λ H, (congr_arg f H).trans hf @[simp] lemma map_range_apply {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {a : α} : map_range f hf g a = f (g a) := rfl @[simp] lemma map_range_zero {f : β₁ → β₂} {hf : f 0 = 0} : map_range f hf (0 : α →₀ β₁) = 0 := finsupp.ext $ λ a, by simp [hf] lemma support_map_range {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} : (map_range f hf g).support ⊆ g.support := support_on_finset_subset @[simp] lemma map_range_single {f : β₁ → β₂} {hf : f 0 = 0} {a : α} {b : β₁} : map_range f hf (single a b) = single a (f b) := finsupp.ext $ λ a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf] end map_range section emb_domain variables [has_zero β] /-- Given `f : α₁ ↪ α₂` and `v : α₁ →₀ β`, `emb_domain f v : α₂ →₀ β` is the finitely supported function whose value at `f a : α₂` is `v a`. For a `b : α₂` outside the range of `f` it is zero. -/ def emb_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : α₂ →₀ β := begin refine ⟨v.support.map f, λa₂, if h : a₂ ∈ v.support.map f then v (v.support.choose (λa₁, f a₁ = a₂) _) else 0, _⟩, { rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩, exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.inj hb) }, { assume a₂, split_ifs, { simp [h], rw [← finsupp.not_mem_support_iff, not_not], apply finset.choose_mem }, { simp [h] } } end lemma support_emb_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : (emb_domain f v).support = v.support.map f := rfl lemma emb_domain_zero (f : α₁ ↪ α₂) : (emb_domain f 0 : α₂ →₀ β) = 0 := rfl lemma emb_domain_apply (f : α₁ ↪ α₂) (v : α₁ →₀ β) (a : α₁) : emb_domain f v (f a) = v a := begin change dite _ _ _ = _, split_ifs; rw [finset.mem_map' f] at h, { refine congr_arg (v : α₁ → β) (f.inj' _), exact finset.choose_property (λa₁, f a₁ = f a) _ _ }, { exact (finsupp.not_mem_support_iff.1 h).symm } end lemma emb_domain_notin_range (f : α₁ ↪ α₂) (v : α₁ →₀ β) (a : α₂) (h : a ∉ set.range f) : emb_domain f v a = 0 := begin refine dif_neg (mt (assume h, _) h), rcases finset.mem_map.1 h with ⟨a, h, rfl⟩, exact set.mem_range_self a end lemma emb_domain_inj {f : α₁ ↪ α₂} {l₁ l₂ : α₁ →₀ β} : emb_domain f l₁ = emb_domain f l₂ ↔ l₁ = l₂ := ⟨λ h, finsupp.ext $ λ a, by simpa [emb_domain_apply] using finsupp.ext_iff.1 h (f a), λ h, by rw h⟩ lemma emb_domain_map_range {β₁ β₂ : Type*} [has_zero β₁] [has_zero β₂] (f : α₁ ↪ α₂) (g : β₁ → β₂) (p : α₁ →₀ β₁) (hg : g 0 = 0) : emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a', rfl⟩, rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] }, { rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption } end lemma single_of_emb_domain_single (l : α₁ →₀ β) (f : α₁ ↪ α₂) (a : α₂) (b : β) (hb : b ≠ 0) (h : l.emb_domain f = finsupp.single a b) : ∃ x, l = finsupp.single x b ∧ f x = a := begin have h_map_support : finset.map f (l.support) = finset.singleton a, by rw [←finsupp.support_emb_domain, h, finsupp.support_single_ne_zero hb]; refl, have ha : a ∈ finset.map f (l.support), by simp [h_map_support], rcases finset.mem_map.1 ha with ⟨c, hc₁, hc₂⟩, use c, split, { ext d, rw [← finsupp.emb_domain_apply f l, h], by_cases h_cases : c = d, { simp [h_cases.symm, hc₂] }, { rw [finsupp.single_apply, finsupp.single_apply, if_neg, if_neg h_cases], by_contra hfd, exact h_cases (f.inj (hc₂.trans hfd)) } }, { exact hc₂ } end end emb_domain section zip_with variables [has_zero β] [has_zero β₁] [has_zero β₂] /-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying `zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and well defined when `f 0 0 = 0`. -/ def zip_with (f : β₁ → β₂ → β) (hf : f 0 0 = 0) (g₁ : α →₀ β₁) (g₂ : α →₀ β₂) : (α →₀ β) := on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ λ a H, begin simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib], rintro ⟨h₁, h₂⟩, rw [h₁, h₂] at H, exact H hf end @[simp] lemma zip_with_apply {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} {a : α} : zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) := rfl lemma support_zip_with {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := support_on_finset_subset end zip_with section erase def erase [has_zero β] (a : α) (f : α →₀ β) : α →₀ β := ⟨f.support.erase a, (λa', if a' = a then 0 else f a'), assume a', by rw [mem_erase, mem_support_iff]; split_ifs; [exact ⟨λ H _, H.1 h, λ H, (H rfl).elim⟩, exact and_iff_right h]⟩ @[simp] lemma support_erase [has_zero β] {a : α} {f : α →₀ β} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same [has_zero β] {a : α} {f : α →₀ β} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne [has_zero β] {a a' : α} {f : α →₀ β} (h : a' ≠ a) : (f.erase a) a' = f a' := if_neg h end erase -- [to_additive sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero β] [add_comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := f.support.sum (λa, g a (f a)) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive] def prod [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := f.support.prod (λa, g a (f a)) @[to_additive] lemma prod_map_range_index [has_zero β₁] [has_zero β₂] [comm_monoid γ] {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {h : α → β₂ → γ} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ λ _ _ H, by rw [not_mem_support_iff.1 H, h0] @[to_additive] lemma prod_zero_index [add_comm_monoid β] [comm_monoid γ] {h : α → β → γ} : (0 : α →₀ β).prod h = 1 := rfl section nat_sub instance nat_sub : has_sub (α →₀ ℕ) := ⟨zip_with (λ m n, m - n) (nat.sub_zero 0)⟩ @[simp] lemma nat_sub_apply {g₁ g₂ : α →₀ ℕ} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl end nat_sub section add_monoid variables [add_monoid β] @[to_additive] lemma prod_single_index [comm_monoid γ] {a : α} {b : β} {h : α → β → γ} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := begin by_cases h : b = 0, { simp only [h, h_zero, single_zero]; refl }, { simp only [finsupp.prod, support_single_ne_zero h, insert_empty_eq_singleton, prod_singleton, single_eq_same] } end instance : has_add (α →₀ β) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma add_apply {g₁ g₂ : α →₀ β} {a : α} : (g₁ + g₂) a = g₁ a + g₂ a := rfl lemma support_add {g₁ g₂ : α →₀ β} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with lemma support_add_eq {g₁ g₂ : α →₀ β} (h : disjoint g₁.support g₂.support) : (g₁ + g₂).support = g₁.support ∪ g₂.support := le_antisymm support_zip_with $ assume a ha, (finset.mem_union.1 ha).elim (assume ha, have a ∉ g₂.support, from disjoint_left.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, add_zero]) (assume ha, have a ∉ g₁.support, from disjoint_right.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, zero_add]) @[simp] lemma single_add {a : α} {b₁ b₂ : β} : single a (b₁ + b₂) = single a b₁ + single a b₂ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_monoid (α →₀ β) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } instance (a : α) : is_add_monoid_hom (λ g : α →₀ β, g a) := { map_add := λ _ _, add_apply, map_zero := zero_apply } lemma single_add_erase {a : α} {f : α →₀ β} : single a (f a) + f.erase a = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero] else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)] lemma erase_add_single {a : α} {f : α →₀ β} : f.erase a + single a (f a) = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add] else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)] @[elab_as_eliminator] protected theorem induction {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction₂ {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma map_range_add [add_monoid β₁] [add_monoid β₂] {f : β₁ → β₂} {hf : f 0 = 0} (hf' : ∀ x y, f (x + y) = f x + f y) (v₁ v₂ : α →₀ β₁) : map_range f hf (v₁ + v₂) = map_range f hf v₁ + map_range f hf v₂ := finsupp.ext $ λ a, by simp [hf'] end add_monoid instance [add_comm_monoid β] : add_comm_monoid (α →₀ β) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group β] : add_group (α →₀ β) := { neg := map_range (has_neg.neg) neg_zero, add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, .. finsupp.add_monoid } lemma single_multiset_sum [add_comm_monoid β] (s : multiset β) (a : α) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s single_zero $ λ a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid β] (s : finset γ) (f : γ → β) (a : α) : single a (s.sum f) = s.sum (λb, single a (f b)) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero γ] [add_comm_monoid β] (s : δ →₀ γ) (f : δ → γ → β) (a : α) : single a (s.sum f) = s.sum (λd c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive] lemma prod_neg_index [add_group β] [comm_monoid γ] {g : α →₀ β} {h : α → β → γ} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 @[simp] lemma neg_apply [add_group β] {g : α →₀ β} {a : α} : (- g) a = - g a := rfl @[simp] lemma sub_apply [add_group β] {g₁ g₂ : α →₀ β} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma support_neg [add_group β] {f : α →₀ β} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm ... ⊆ support (- f) : support_map_range) instance [add_comm_group β] : add_comm_group (α →₀ β) := { add_comm := add_comm, ..finsupp.add_group } @[simp] lemma sum_apply [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {a₂ : α} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := (finset.sum_hom (λf : α →₀ β, f a₂)).symm lemma support_sum [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → (α →₀ β)} : (f.sum g).support ⊆ f.support.bind (λa, (g a (f a)).support) := have ∀a₁ : α, f.sum (λ (a : α₁) (b : β₁), (g a b) a₁) ≠ 0 → (∃ (a : α₁), f a ≠ 0 ∧ ¬ (g a (f a)) a₁ = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply, exists_prop] using this @[simp] lemma sum_zero [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} : f.sum (λa b, (0 : γ)) = 0 := finset.sum_const_zero @[simp] lemma sum_add [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b + h₂ a b) = f.sum h₁ + f.sum h₂ := finset.sum_add_distrib @[simp] lemma sum_neg [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h : α → β → γ} : f.sum (λa b, - h a b) = - f.sum h := finset.sum_hom (@has_neg.neg γ _) @[simp] lemma sum_sub [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := by rw [sub_eq_add_neg, ←sum_neg, ←sum_add]; refl @[simp] lemma sum_single [add_comm_monoid β] (f : α →₀ β) : f.sum single = f := have ∀a:α, f.sum (λa' b, ite (a' = a) b 0) = ({a} : finset α).sum (λa', ite (a' = a) (f a') 0), begin intro a, by_cases h : a ∈ f.support, { have : (finset.singleton a : finset α) ⊆ f.support, { simpa only [finset.subset_iff, mem_singleton, forall_eq] }, refine (finset.sum_subset this (λ _ _ H, _)).symm, exact if_neg (mt mem_singleton.2 H) }, { transitivity (f.support.sum (λa, (0 : β))), { refine (finset.sum_congr rfl $ λ a' ha', if_neg _), rintro rfl, exact h ha' }, { rw [sum_const_zero, insert_empty_eq_singleton, sum_singleton, if_pos rfl, not_mem_support_iff.1 h] } } end, ext $ assume a, by simp only [sum_apply, single_apply, this, insert_empty_eq_singleton, sum_singleton, if_pos] @[to_additive] lemma prod_add_index [add_comm_monoid β] [comm_monoid γ] {f g : α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := have f_eq : (f.support ∪ g.support).prod (λa, h a (f a)) = f.prod h, from (finset.prod_subset (finset.subset_union_left _ _) $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero]).symm, have g_eq : (f.support ∪ g.support).prod (λa, h a (g a)) = g.prod h, from (finset.prod_subset (finset.subset_union_right _ _) $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero]).symm, calc (f + g).support.prod (λa, h a ((f + g) a)) = (f.support ∪ g.support).prod (λa, h a ((f + g) a)) : finset.prod_subset support_add $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero] ... = (f.support ∪ g.support).prod (λa, h a (f a)) * (f.support ∪ g.support).prod (λa, h a (g a)) : by simp only [add_apply, h_add, finset.prod_mul_distrib] ... = _ : by rw [f_eq, g_eq] lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := have h_zero : ∀a, h a 0 = 0, from assume a, have h a (0 - 0) = h a 0 - h a 0, from h_sub a 0 0, by simpa only [sub_self] using this, have h_neg : ∀a b, h a (- b) = - h a b, from assume a b, have h a (0 - b) = h a 0 - h a b, from h_sub a 0 b, by simpa only [h_zero, zero_sub] using this, have h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ + h a b₂, from assume a b₁ b₂, have h a (b₁ - (- b₂)) = h a b₁ - h a (- b₂), from h_sub a b₁ (-b₂), by simpa only [h_neg, sub_neg_eq_add] using this, calc (f - g).sum h = (f + - g).sum h : rfl ... = f.sum h + - g.sum h : by simp only [sum_add_index h_zero h_add, sum_neg_index h_zero, h_neg, sum_neg] ... = f.sum h - g.sum h : rfl @[to_additive] lemma prod_finset_sum_index [add_comm_monoid β] [comm_monoid γ] {s : finset ι} {g : ι → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : s.prod (λi, (g i).prod h) = (s.sum g).prod h := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add] @[to_additive] lemma prod_sum_index [add_comm_monoid β₁] [add_comm_monoid β] [comm_monoid γ] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [add_comm_monoid β] [add_comm_monoid γ] (f : multiset (α →₀ β)) (h : α → β → γ) (h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : β), h a (b₁ + b₂) = h a b₁ + h a b₂) : (f.sum.sum h) = (f.map $ λg:α →₀ β, g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index h₀ h₁, ih] lemma multiset_map_sum [has_zero β] {f : α →₀ β} {m : γ → δ} {h : α → β → multiset γ} : multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) := (finset.sum_hom _).symm lemma multiset_sum_sum [has_zero β] [add_comm_monoid γ] {f : α →₀ β} {h : α → β → multiset γ} : multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) := (finset.sum_hom multiset.sum).symm section map_range variables [add_comm_monoid β₁] [add_comm_monoid β₂] (f : β₁ → β₂) [hf : is_add_monoid_hom f] instance is_add_monoid_hom_map_range : is_add_monoid_hom (map_range f hf.map_zero : (α →₀ β₁) → (α →₀ β₂)) := { map_zero := map_range_zero, map_add := λ a b, map_range_add hf.map_add _ _ } lemma map_range_multiset_sum (m : multiset (α →₀ β₁)) : map_range f hf.map_zero m.sum = (m.map $ λx, map_range f hf.map_zero x).sum := (m.sum_hom (map_range f hf.map_zero)).symm lemma map_range_finset_sum {ι : Type*} (s : finset ι) (g : ι → (α →₀ β₁)) : map_range f hf.map_zero (s.sum g) = s.sum (λx, map_range f hf.map_zero (g x)) := by rw [finset.sum.equations._eqn_1, map_range_multiset_sum, multiset.map_map]; refl end map_range section map_domain variables [add_comm_monoid β] {v v₁ v₂ : α →₀ β} /-- Given `f : α₁ → α₂` and `v : α₁ →₀ β`, `map_domain f v : α₂ →₀ β` is the finitely supported function whose value at `a : α₂` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α₁ → α₂) (v : α₁ →₀ β) : α₂ →₀ β := v.sum $ λa, single (f a) lemma map_domain_apply {f : α₁ → α₂} (hf : function.injective f) (x : α₁ →₀ β) (a : α₁) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { simp only [(∉), (≠), not_not, mem_support_iff], assume h, rw [h, single_zero], refl } end lemma map_domain_notin_range {f : α₁ → α₂} (x : α₁ →₀ β) (a : α₂) (h : a ∉ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _) end lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : α → α₁} {g : α₁ → α₂} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intros, exact single_zero }, { intros, exact single_add }, refine sum_congr rfl (λ _ _, sum_single_index _), { exact single_zero } end lemma map_domain_single {f : α → α₁} {a : α} {b : β} : map_domain f (single a b) = single (f a) b := sum_single_index single_zero @[simp] lemma map_domain_zero {f : α → α₂} : map_domain f 0 = (0 : α₂ →₀ β) := sum_zero_index lemma map_domain_congr {f g : α → α₂} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ λ _ H, by simp only [h _ H] lemma map_domain_add {f : α → α₂} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_finset_sum {f : α → α₂} {s : finset ι} {v : ι → α →₀ β} : map_domain f (s.sum v) = s.sum (λi, map_domain f (v i)) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_sum [has_zero β₁] {f : α → α₂} {s : α →₀ β₁} {v : α → β₁ → α →₀ β} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_support {f : α → α₂} {s : α →₀ β} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bind_mono $ assume a ha, support_single_subset) $ by rw [finset.bind_singleton]; exact subset.refl _ @[to_additive] lemma prod_map_domain_index [comm_monoid γ] {f : α → α₂} {s : α →₀ β} {h : α₂ → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := (prod_sum_index h_zero h_add).trans $ prod_congr rfl $ λ _ _, prod_single_index (h_zero _) lemma emb_domain_eq_map_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : emb_domain f v = map_domain f v := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply (function.embedding.inj' _), emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end lemma injective_map_domain {f : α₁ → α₂} (hf : function.injective f) : function.injective (map_domain f : (α₁ →₀ β) → (α₂ →₀ β)) := begin assume v₁ v₂ eq, ext a, have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end end map_domain section comap_domain noncomputable def comap_domain {α₁ α₂ γ : Type*} [has_zero γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.inj_on f (f ⁻¹' l.support.to_set)) : α₁ →₀ γ := { support := l.support.preimage hf, to_fun := (λ a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } lemma comap_domain_apply {α₁ α₂ γ : Type*} [has_zero γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.inj_on f (f ⁻¹' l.support.to_set)) (a : α₁) : comap_domain f l hf a = l (f a) := begin unfold_coes, unfold comap_domain, simp, refl end lemma sum_comap_domain {α₁ α₂ β γ : Type*} [has_zero β] [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ β) (g : α₂ → β → γ) (hf : set.bij_on f (f ⁻¹' l.support.to_set) l.support.to_set): (comap_domain f l (set.inj_on_of_bij_on hf)).sum (g ∘ f) = l.sum g := begin unfold sum, haveI := classical.dec_eq α₂, simp only [comap_domain, comap_domain_apply, finset.sum_preimage f _ _ (λ (x : α₂), g x (l x))], end lemma eq_zero_of_comap_domain_eq_zero {α₁ α₂ γ : Type*} [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.bij_on f (f ⁻¹' l.support.to_set) l.support.to_set) : comap_domain f l (set.inj_on_of_bij_on hf) = 0 → l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm ▸ ha) end lemma map_domain_comap_domain {α₁ α₂ γ : Type*} [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : function.injective f) (hl : ↑l.support ⊆ set.range f): map_domain f (comap_domain f l (set.inj_on_of_injective _ hf)) = l := begin ext a, haveI := classical.dec (a ∈ set.range f), by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl (finset.mem_coe.2 (mem_support_iff.2 (λ h, h_contr h.symm)))) } end end comap_domain /-- The product of `f g : α →₀ β` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the monoid of monomial exponents.) -/ instance [has_add α] [semiring β] : has_mul (α →₀ β) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def [has_add α] [semiring β] {f g : α →₀ β} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl lemma support_mul [has_add α] [semiring β] (a b : α →₀ β) : (a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ + a₂}) := subset.trans support_sum $ bind_mono $ assume a₁ _, subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset /-- The unit of the multiplication is `single 0 1`, i.e. the function that is 1 at 0 and zero elsewhere. -/ instance [has_zero α] [has_zero β] [has_one β] : has_one (α →₀ β) := ⟨single 0 1⟩ lemma one_def [has_zero α] [has_zero β] [has_one β] : 1 = (single 0 1 : α →₀ β) := rfl section filter section has_zero variables [has_zero β] (p : α → Prop) (f : α →₀ β) /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) (f : α →₀ β) : α →₀ β := on_finset f.support (λa, if p a then f a else 0) $ λ a H, mem_support_iff.2 $ λ h, by rw [h, if_t_t] at H; exact H rfl @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter : (f.filter p).support = f.support.filter p := finset.ext.mpr $ assume a, if H : p a then by simp only [mem_support_iff, filter_apply_pos _ _ H, mem_filter, H, and_true] else by simp only [mem_support_iff, filter_apply_neg _ _ H, mem_filter, H, and_false, ne.def, ne_self_iff_false] lemma filter_zero : (0 : α →₀ β).filter p = 0 := by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] @[simp] lemma filter_single_of_pos {a : α} {b : β} (h : p a) : (single a b).filter p = single a b := finsupp.ext $ λ x, begin by_cases h' : p x; simp [h'], rw single_eq_of_ne, rintro rfl, exact h' h end @[simp] lemma filter_single_of_neg {a : α} {b : β} (h : ¬ p a) : (single a b).filter p = 0 := finsupp.ext $ λ x, begin by_cases h' : p x; simp [h'], rw single_eq_of_ne, rintro rfl, exact h h' end end has_zero lemma filter_pos_add_filter_neg [add_monoid β] (f : α →₀ β) (p : α → Prop) : f.filter p + f.filter (λa, ¬ p a) = f := finsupp.ext $ assume a, if H : p a then by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_not, add_zero] else by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_false_iff, zero_add] end filter section frange variables [has_zero β] def frange (f : α →₀ β) : finset β := finset.image f f.support theorem mem_frange {f : α →₀ β} {y : β} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ β} : (0:β) ∉ f.frange := λ H, (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : β} : frange (single x y) ⊆ {y} := λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸ (by rw single_apply at ht2 ⊢; split_ifs at ht2 ⊢; [exact finset.mem_singleton_self _, cc]) end frange section subtype_domain variables {α' : Type*} [has_zero δ] {p : α → Prop} section zero variables [has_zero β] {v v' : α' →₀ β} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : α → Prop) (f : α →₀ β) : (subtype p →₀ β) := ⟨f.support.subtype p, f ∘ subtype.val, λ a, by simp only [mem_subtype, mem_support_iff]⟩ @[simp] lemma support_subtype_domain {f : α →₀ β} : (subtype_domain p f).support = f.support.subtype p := rfl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ β} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ β) = 0 := rfl @[to_additive] lemma prod_subtype_domain_index [comm_monoid γ] {v : α →₀ β} {h : α → β → γ} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a.1 b) = v.prod h := prod_bij (λp _, p.val) (λ _, mem_subtype.1) (λ _ _, rfl) (λ _ _ _ _, subtype.eq) (λ b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩) end zero section monoid variables [add_monoid β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_add {v v' : α →₀ β} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ _, rfl instance subtype_domain.is_add_monoid_hom : is_add_monoid_hom (subtype_domain p : (α →₀ β) → subtype p →₀ β) := { map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero } @[simp] lemma filter_add {v v' : α →₀ β} : (v + v').filter p = v.filter p + v'.filter p := ext $ λ a, by by_cases p a; simp [h] instance filter.is_add_monoid_hom (p : α → Prop) : is_add_monoid_hom (filter p : (α →₀ β) → (α →₀ β)) := { map_zero := filter_zero p, map_add := λ x y, filter_add } end monoid section comm_monoid variables [add_comm_monoid β] lemma subtype_domain_sum {s : finset γ} {h : γ → α →₀ β} : (s.sum h).subtype_domain p = s.sum (λc, (h c).subtype_domain p) := eq.symm (finset.sum_hom _) lemma subtype_domain_finsupp_sum {s : γ →₀ δ} {h : γ → δ → α →₀ β} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset γ) (f : γ → α →₀ β) : (s.sum f).filter p = s.sum (λa, filter p (f a)) := (finset.sum_hom (filter p)).symm end comm_monoid section group variables [add_group β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_neg {v : α →₀ β} : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ _, rfl @[simp] lemma subtype_domain_sub {v v' : α →₀ β} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ _, rfl end group end subtype_domain section multiset def to_multiset (f : α →₀ ℕ) : multiset α := f.sum (λa n, add_monoid.smul n {a}) lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 := rfl lemma to_multiset_add (m n : α →₀ ℕ) : (m + n).to_multiset = m.to_multiset + n.to_multiset := sum_add_index (assume a, add_monoid.zero_smul _) (assume a b₁ b₂, add_monoid.add_smul _ _ _) lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = add_monoid.smul n {a} := by rw [to_multiset, sum_single_index]; apply add_monoid.zero_smul instance is_add_monoid_hom.to_multiset : is_add_monoid_hom (to_multiset : _ → multiset α) := { map_zero := to_multiset_zero, map_add := to_multiset_add } lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.card_zero, sum_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.card_add, ih, sum_add_index, to_multiset_single, sum_single_index, multiset.card_smul, multiset.singleton_eq_singleton, multiset.card_singleton, mul_one]; intros; refl } end lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, is_add_monoid_hom.map_smul (multiset.map g)], refl } end lemma prod_to_multiset [comm_monoid α] (f : α →₀ ℕ) : f.to_multiset.prod = f.prod (λa n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_smul, multiset.singleton_eq_singleton, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end lemma to_finset_to_multiset (f : α →₀ ℕ) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_smul _ _ hn, multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero], refl, refine disjoint_mono support_single_subset (subset.refl _) _, rwa [finset.singleton_eq_singleton, finset.singleton_disjoint] } end @[simp] lemma count_to_multiset (f : α →₀ ℕ) (a : α) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (λx n, (add_monoid.smul n {x} : multiset α).count a) : (finset.sum_hom _).symm ... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_smul] ... = f.sum (λx n, n * (x :: 0 : multiset α).count a) : rfl ... = f a * (a :: 0 : multiset α).count a : sum_eq_single _ (λ a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero]) (λ H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by simp only [multiset.count_singleton, mul_one] def of_multiset (m : multiset α) : α →₀ ℕ := on_finset m.to_finset (λa, m.count a) $ λ a H, multiset.mem_to_finset.2 $ by_contradiction (mt multiset.count_eq_zero.2 H) @[simp] lemma of_multiset_apply (m : multiset α) (a : α) : of_multiset m a = m.count a := rfl def equiv_multiset : (α →₀ ℕ) ≃ (multiset α) := ⟨ to_multiset, of_multiset, assume f, finsupp.ext $ λ a, by rw [of_multiset_apply, count_to_multiset], assume m, multiset.ext.2 $ λ a, by rw [count_to_multiset, of_multiset_apply] ⟩ lemma mem_support_multiset_sum [add_comm_monoid β] {s : multiset (α →₀ β)} (a : α) : a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ β).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩, exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ } end lemma mem_support_finset_sum [add_comm_monoid β] {s : finset γ} {h : γ → α →₀ β} (a : α) (ha : a ∈ (s.sum h).support) : ∃c∈s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm ▸ hfa⟩ lemma mem_support_single [has_zero β] (a a' : α) (b : β) : a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 := ⟨λ H : (a ∈ ite _ _ _), if h : b = 0 then by rw if_pos h at H; exact H.elim else ⟨by rw if_neg h at H; exact mem_singleton.1 H, h⟩, λ ⟨h1, h2⟩, show a ∈ ite _ _ _, by rw [if_neg h2]; exact mem_singleton.2 h1⟩ end multiset section curry_uncurry protected def curry [add_comm_monoid γ] (f : (α × β) →₀ γ) : α →₀ (β →₀ γ) := f.sum $ λp c, single p.1 (single p.2 c) lemma sum_curry_index [add_comm_monoid γ] [add_comm_monoid δ] (f : (α × β) →₀ γ) (g : α → β → γ → δ) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a b₀ b₁, sum_add_index (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hg₀ _ _) end protected def uncurry [add_comm_monoid γ] (f : α →₀ (β →₀ γ)) : (α × β) →₀ γ := f.sum $ λa g, g.sum $ λb c, single (a, b) c def finsupp_prod_equiv [add_comm_monoid γ] : ((α × β) →₀ γ) ≃ (α →₀ (β →₀ γ)) := by refine ⟨finsupp.curry, finsupp.uncurry, λ f, _, λ f, _⟩; simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] lemma filter_curry [add_comm_monoid β] (f : α₁ × α₂ →₀ β) (p : α₁ → Prop) : (f.filter (λa:α₁×α₂, p a.1)).curry = f.curry.filter p := begin rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, @filter_sum _ (α₂ →₀ β) _ p _ f.support _], rw [support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, a₂⟩ ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry [add_comm_monoid β] (f : α₁ × α₂ →₀ β) : f.curry.support ⊆ f.support.image prod.fst := begin rw ← finset.bind_singleton, refine finset.subset.trans support_sum _, refine finset.bind_mono (assume a _, support_single_subset) end end curry_uncurry section variables [add_monoid α] [semiring β] -- TODO: the simplifier unfolds 0 in the instance proof! private lemma zero_mul (f : α →₀ β) : 0 * f = 0 := by simp only [mul_def, sum_zero_index] private lemma mul_zero (f : α →₀ β) : f * 0 = 0 := by simp only [mul_def, sum_zero_index, sum_zero] private lemma left_distrib (a b c : α →₀ β) : a * (b + c) = a * b + a * c := by simp only [mul_def, sum_add_index, mul_add, _root_.mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add] private lemma right_distrib (a b c : α →₀ β) : (a + b) * c = a * c + b * c := by simp only [mul_def, sum_add_index, add_mul, _root_.mul_zero, _root_.zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add] instance : semiring (α →₀ β) := { one := 1, mul := (*), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], zero_mul := zero_mul, mul_zero := mul_zero, mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, _root_.zero_mul, _root_.mul_zero, sum_zero, sum_add], left_distrib := left_distrib, right_distrib := right_distrib, .. finsupp.add_comm_monoid } end instance [add_comm_monoid α] [comm_semiring β] : comm_semiring (α →₀ β) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [add_comm] end, .. finsupp.semiring } instance [add_monoid α] [ring β] : ring (α →₀ β) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. finsupp.semiring } instance [add_comm_monoid α] [comm_ring β] : comm_ring (α →₀ β) := { mul_comm := mul_comm, .. finsupp.ring} lemma single_mul_single [has_add α] [semiring β] {a₁ a₂ : α} {b₁ b₂ : β}: single a₁ b₁ * single a₂ b₂ = single (a₁ + a₂) (b₁ * b₂) := (sum_single_index (by simp only [_root_.zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [_root_.mul_zero, single_zero])) lemma prod_single [add_comm_monoid α] [comm_semiring β] {s : finset ι} {a : ι → α} {b : ι → β} : s.prod (λi, single (a i) (b i)) = single (s.sum a) (s.prod b) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, sum_insert has, prod_insert has] section instance [semiring γ] [add_comm_monoid β] [semimodule γ β] : has_scalar γ (α →₀ β) := ⟨λa v, v.map_range ((•) a) (smul_zero _)⟩ variables (α β) @[simp] lemma smul_apply' {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {a : α} {b : γ} {v : α →₀ β} : (b • v) a = b • (v a) := rfl instance [semiring γ] [add_comm_monoid β] [semimodule γ β] : semimodule γ (α →₀ β) := { smul := (•), smul_add := λ a x y, finsupp.ext $ λ _, smul_add _ _ _, add_smul := λ a x y, finsupp.ext $ λ _, add_smul _ _ _, one_smul := λ x, finsupp.ext $ λ _, one_smul _ _, mul_smul := λ r s x, finsupp.ext $ λ _, mul_smul _ _ _, zero_smul := λ x, finsupp.ext $ λ _, zero_smul _ _, smul_zero := λ x, finsupp.ext $ λ _, smul_zero _ } instance [ring γ] [add_comm_group β] [module γ β] : module γ (α →₀ β) := { ..finsupp.semimodule α β } instance [discrete_field γ] [add_comm_group β] [vector_space γ β] : vector_space γ (α →₀ β) := { ..finsupp.module α β } variables {α β} lemma support_smul {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {b : γ} {g : α →₀ β} : (b • g).support ⊆ g.support := λ a, by simp; exact mt (λ h, h.symm ▸ smul_zero _) section variables {α' : Type*} [has_zero δ] {p : α → Prop} @[simp] lemma filter_smul {R : semiring γ} [add_comm_monoid β] [semimodule γ β] {b : γ} {v : α →₀ β} : (b • v).filter p = b • v.filter p := ext $ λ a, by by_cases p a; simp [h] end lemma map_domain_smul {α'} {R : semiring γ} [add_comm_monoid β] [semimodule γ β] {f : α → α'} (b : γ) (v : α →₀ β) : map_domain f (b • v) = b • map_domain f v := begin change map_domain f (map_range _ _ _) = map_range _ _ _, apply finsupp.induction v, {simp}, intros a b v' hv₁ hv₂ IH, rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add, map_range_single, map_domain_single, map_domain_single, map_range_single]; apply smul_add end @[simp] lemma smul_single {R : semiring γ} [add_comm_monoid β] [semimodule γ β] (c : γ) (a : α) (b : β) : c • finsupp.single a b = finsupp.single a (c • b) := ext $ λ a', by by_cases a = a'; [{subst h, simp}, simp [h]] end @[simp] lemma smul_apply [ring β] {a : α} {b : β} {v : α →₀ β} : (b • v) a = b • (v a) := rfl lemma sum_smul_index [ring β] [add_comm_monoid γ] {g : α →₀ β} {b : β} {h : α → β → γ} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 section variables [semiring β] [semiring γ] lemma sum_mul (b : γ) (s : α →₀ β) {f : α → β → γ} : (s.sum f) * b = s.sum (λ a c, (f a (s a)) * b) := by simp only [finsupp.sum, finset.sum_mul] lemma mul_sum (b : γ) (s : α →₀ β) {f : α → β → γ} : b * (s.sum f) = s.sum (λ a c, b * (f a (s a))) := by simp only [finsupp.sum, finset.mul_sum] protected lemma eq_zero_of_zero_eq_one (zero_eq_one : (0 : β) = 1) (l : α →₀ β) : l = 0 := by ext i; simp [eq_zero_of_zero_eq_one β zero_eq_one (l i)] end def restrict_support_equiv [add_comm_monoid β] (s : set α) : {f : α →₀ β // ↑f.support ⊆ s } ≃ (s →₀ β):= begin refine ⟨λf, subtype_domain (λx, x ∈ s) f.1, λ f, ⟨f.map_domain subtype.val, _⟩, _, _⟩, { refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2 }, { rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } }, { assume f, ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] } end protected def dom_congr [add_comm_monoid β] (e : α₁ ≃ α₂) : (α₁ →₀ β) ≃ (α₂ →₀ β) := ⟨map_domain e, map_domain e.symm, begin assume v, simp only [map_domain_comp.symm, (∘), equiv.symm_apply_apply], exact map_domain_id end, begin assume v, simp only [map_domain_comp.symm, (∘), equiv.apply_symm_apply], exact map_domain_id end⟩ section sigma variables {αs : ι → Type*} [has_zero β] (l : (Σ i, αs i) →₀ β) noncomputable def split (i : ι) : αs i →₀ β := l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end def split_support : finset ι := finset.image (sigma.fst) l.support lemma mem_split_support_iff_nonzero (i : ι) : i ∈ split_support l ↔ split l i ≠ 0 := begin classical, rw [split_support, mem_image, ne.def, ← support_eq_empty, ← exists_mem_iff_ne_empty, split, comap_domain], simp end noncomputable def split_comp [has_zero γ] (g : Π i, (αs i →₀ β) → γ) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ γ := { support := split_support l, to_fun := λ i, g i (split l i), mem_support_to_fun := begin intros i, rw mem_split_support_iff_nonzero, haveI := classical.dec, rwa not_iff_not, exact hg _ _, end } lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) := by simp [finset.ext, split_support, split, comap_domain]; tauto lemma sigma_sum [add_comm_monoid γ] (f : (Σ (i : ι), αs i) → β → γ) : l.sum f = (split_support l).sum (λ (i : ι), (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b)) := by simp [sum, sigma_support, sum_sigma,split_apply] end sigma end finsupp namespace multiset def to_finsupp (s : multiset α) : α →₀ ℕ := { support := s.to_finset, to_fun := λ a, s.count a, mem_support_to_fun := λ a, begin rw mem_to_finset, convert not_iff_not_of_iff (count_eq_zero.symm), rw not_not end } @[simp] lemma to_finsupp_support (s : multiset α) : s.to_finsupp.support = s.to_finset := rfl @[simp] lemma to_finsupp_apply (s : multiset α) (a : α) : s.to_finsupp a = s.count a := rfl @[simp] lemma to_finsupp_zero : to_finsupp (0 : multiset α) = 0 := finsupp.ext $ λ a, count_zero a @[simp] lemma to_finsupp_add (s t : multiset α) : to_finsupp (s + t) = to_finsupp s + to_finsupp t := finsupp.ext $ λ a, count_add a s t lemma to_finsupp_singleton (a : α) : to_finsupp {a} = finsupp.single a 1 := finsupp.ext $ λ b, if h : a = b then by simp [finsupp.single_apply, h] else begin rw [to_finsupp_apply, finsupp.single_apply, if_neg h, count_eq_zero, singleton_eq_singleton, mem_singleton], rintro rfl, exact h rfl end namespace to_finsupp instance : is_add_monoid_hom (to_finsupp : multiset α → α →₀ ℕ) := { map_zero := to_finsupp_zero, map_add := to_finsupp_add } end to_finsupp @[simp] lemma to_finsupp_to_multiset (s : multiset α) : s.to_finsupp.to_multiset = s := ext.2 $ λ a, by rw [finsupp.count_to_multiset, to_finsupp_apply] end multiset namespace finsupp variables {σ : Type*} instance [preorder α] [has_zero α] : preorder (σ →₀ α) := { le := λ f g, ∀ s, f s ≤ g s, le_refl := λ f s, le_refl _, le_trans := λ f g h Hfg Hgh s, le_trans (Hfg s) (Hgh s) } instance [partial_order α] [has_zero α] : partial_order (σ →₀ α) := { le_antisymm := λ f g hfg hgf, finsupp.ext $ λ s, le_antisymm (hfg s) (hgf s), .. finsupp.preorder } instance [ordered_cancel_comm_monoid α] : add_left_cancel_semigroup (σ →₀ α) := { add_left_cancel := λ a b c h, finsupp.ext $ λ s, by { rw finsupp.ext_iff at h, exact add_left_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_comm_monoid α] : add_right_cancel_semigroup (σ →₀ α) := { add_right_cancel := λ a b c h, finsupp.ext $ λ s, by { rw finsupp.ext_iff at h, exact add_right_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_comm_monoid α] : ordered_cancel_comm_monoid (σ →₀ α) := { add_le_add_left := λ a b h c s, add_le_add_left (h s) (c s), le_of_add_le_add_left := λ a b c h s, le_of_add_le_add_left (h s), .. finsupp.add_comm_monoid, .. finsupp.partial_order, .. finsupp.add_left_cancel_semigroup, .. finsupp.add_right_cancel_semigroup } lemma le_iff [canonically_ordered_monoid α] (f g : σ →₀ α) : f ≤ g ↔ ∀ s ∈ f.support, f s ≤ g s := ⟨λ h s hs, h s, λ h s, if H : s ∈ f.support then h s H else (not_mem_support_iff.1 H).symm ▸ zero_le (g s)⟩ attribute [simp] to_multiset_zero to_multiset_add @[simp] lemma to_multiset_to_finsupp (f : σ →₀ ℕ) : f.to_multiset.to_finsupp = f := ext $ λ s, by rw [multiset.to_finsupp_apply, count_to_multiset] lemma to_multiset_strict_mono : strict_mono (@to_multiset σ) := λ m n h, begin rw lt_iff_le_and_ne at h ⊢, cases h with h₁ h₂, split, { rw multiset.le_iff_count, intro s, erw [count_to_multiset m s, count_to_multiset], exact h₁ s }, { intro H, apply h₂, replace H := congr_arg multiset.to_finsupp H, simpa using H } end lemma sum_id_lt_of_lt (m n : σ →₀ ℕ) (h : m < n) : m.sum (λ _, id) < n.sum (λ _, id) := begin rw [← card_to_multiset, ← card_to_multiset], apply multiset.card_lt_of_lt, exact to_multiset_strict_mono _ _ h end variable (σ) /-- The order on σ →₀ ℕ is well-founded.-/ lemma lt_wf : well_founded (@has_lt.lt (σ →₀ ℕ) _) := subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf instance decidable_le : decidable_rel (@has_le.le (σ →₀ ℕ) _) := λ m n, by rw le_iff; apply_instance variable {σ} def antidiagonal (f : σ →₀ ℕ) : ((σ →₀ ℕ) × (σ →₀ ℕ)) →₀ ℕ := (f.to_multiset.antidiagonal.map (prod.map multiset.to_finsupp multiset.to_finsupp)).to_finsupp lemma mem_antidiagonal_support {f : σ →₀ ℕ} {p : (σ →₀ ℕ) × (σ →₀ ℕ)} : p ∈ (antidiagonal f).support ↔ p.1 + p.2 = f := begin erw [multiset.mem_to_finset, multiset.mem_map], split, { rintros ⟨⟨a, b⟩, h, rfl⟩, rw multiset.mem_antidiagonal at h, simpa using congr_arg multiset.to_finsupp h }, { intro h, refine ⟨⟨p.1.to_multiset, p.2.to_multiset⟩, _, _⟩, { simpa using congr_arg to_multiset h }, { rw [prod.map, to_multiset_to_finsupp, to_multiset_to_finsupp, prod.mk.eta] } } end @[simp] lemma antidiagonal_zero : antidiagonal (0 : σ →₀ ℕ) = single (0,0) 1 := by rw [← multiset.to_finsupp_singleton]; refl lemma swap_mem_antidiagonal_support {n : σ →₀ ℕ} {f} (hf : f ∈ (antidiagonal n).support) : f.swap ∈ (antidiagonal n).support := by simpa [mem_antidiagonal_support, add_comm] using hf end finsupp
c5f5cabe1677b5924423d0536da846cf68fcfe1b
e898bfefd5cb60a60220830c5eba68cab8d02c79
/uexp/src/uexp/TDP_test.lean
651127f12fd3d22fb760a39be15a1349a380854d
[ "BSD-2-Clause" ]
permissive
kkpapa/Cosette
9ed09e2dc4c1ecdef815c30b5501f64a7383a2ce
fda8fdbbf0de6c1be9b4104b87bbb06cede46329
refs/heads/master
1,584,573,128,049
1,526,370,422,000
1,526,370,422,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,703
lean
import .TDP import .cosette_tactics open list io example {p q r s} {f : Tuple p → Tuple q → Tuple r → Tuple s → usr} : (∑ (a : Tuple p) (b : Tuple q) (c : Tuple r) (d : Tuple s), f a b c d) = (∑ (c : Tuple r) (a : Tuple p) (d : Tuple s) (b : Tuple q), f a b c d) := begin TDP' tactic.ac_refl, end example {p} {R S: Tuple p → usr} : (∑ (a b: Tuple p), R a * S b * S b) = (∑ (a b: Tuple p), R b * S a * S a) := begin TDP' tactic.ac_refl, end example {p} {R: Tuple p → usr} {b t: Tuple p}: (∑ (a: Tuple p), (a ≃ t) * R a * R b) = R t * R b := begin normalize_sig_body_lhs, removal_step, ac_refl, end example {p q} {R S: Tuple p → usr} {t: Tuple p} : (∑ (a b: Tuple p) (c: Tuple q), (a ≃ b) * R a * R b) = (∑ (t: Tuple p) (c: Tuple q), R t * R t) := begin normalize_sig_body_lhs, removal_step, refl, end example {r p} {R: Tuple r → usr} : (∑ (a1 a2 a3: Tuple r) (b c: Tuple p), (a2 ≃ a1) * (a2 ≃ a3) * (c ≃ b) * (R a1)) = (∑ (a: Tuple r)(b: Tuple p), R a) := begin remove_dup_sigs_lhs, refl, end example {p} {R: Tuple p → usr} {b t: Tuple p}: R t * R b = (∑ (a: Tuple p), (a ≃ t) * R a * R b) := begin apply ueq_symm, remove_dup_sigs_lhs, ac_refl, end example {r p} {R: Tuple r → usr} {k: Tuple (r++r)}: (∑ (a1 a2 a3: Tuple r) (b c: Tuple p), (k ≃ (pair a3 a2)) * (c ≃ b) * (R a1)) = (∑ (a: Tuple r)(b: Tuple p), R a) := begin unfold pair, remove_dup_sigs_lhs, print_size, ac_refl end example {r} {R: Tuple r → usr}: (∑ (a1), (∑ (a2: Tuple r), (a2 ≃ a1) * R a2 ) * R a1) = (∑ (t: Tuple r), R t * R t) := begin --remove_dup_sigs_lhs, simp, TDP, end
ff7d2e8173ff453b5135364a266c16a6d0b74f87
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/analysis/normed_space/real_inner_product.lean
1f280f36dbde4f330341709aa9270fde90d6b3ac
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
40,929
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Sébastien Gouëzel -/ import algebra.quadratic_discriminant import linear_algebra.bilinear_form import tactic.apply_fun import tactic.monotonicity import topology.metric_space.pi_Lp /-! # Inner Product Space This file defines real inner product space and proves its basic properties. An inner product space is a vector space endowed with an inner product. It generalizes the notion of dot product in `ℝ^n` and provides the means of defining the length of a vector and the angle between two vectors. In particular vectors `x` and `y` are orthogonal if their inner product equals zero. ## Main statements Existence of orthogonal projection onto nonempty complete subspace: Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`. The point `v` is usually called the orthogonal projection of `u` onto `K`. ## Implementation notes We decide to develop the theory of real inner product spaces and that of complex inner product spaces separately. ## Tags inner product space, norm, orthogonal projection ## References * [Clément & Martin, *The Lax-Milgram Theorem. A detailed proof to be formalized in Coq*] * [Clément & Martin, *A Coq formal proof of the Lax–Milgram theorem*] The Coq code is available at the following address: <http://www.lri.fr/~sboldo/elfic/index.html> -/ noncomputable theory open real set open_locale big_operators open_locale topological_space universes u v w variables {α : Type u} {F : Type v} {G : Type w} /-- Syntactic typeclass for types endowed with an inner product -/ class has_inner (α : Type*) := (inner : α → α → ℝ) export has_inner (inner) section prio set_option default_priority 100 -- see Note [default priority] -- the norm is embedded in the inner product space structure -- to avoid definitional equality issues. See Note [forgetful inheritance]. /-- An inner product space is a real vector space with an additional operation called inner product. Inner product spaces over complex vector space will be defined in another file. The norm could be derived from the inner product, instead we require the existence of a norm and the fact that it is equal to `sqrt (inner x x)` to be able to put instances on `ℝ` or product spaces. To construct a norm from an inner product, see `inner_product_space.of_core`. -/ class inner_product_space (α : Type*) extends normed_group α, normed_space ℝ α, has_inner α := (norm_eq_sqrt_inner : ∀ (x : α), ∥x∥ = sqrt (inner x x)) (comm : ∀ x y, inner x y = inner y x) (add_left : ∀ x y z, inner (x + y) z = inner x z + inner y z) (smul_left : ∀ x y r, inner (r • x) y = r * inner x y) end prio /-! ### Constructing a normed space structure from a scalar product In the definition of an inner product space, we require the existence of a norm, which is equal (but maybe not defeq) to the square root of the scalar product. This makes it possible to put an inner product space structure on spaces with a preexisting norm (for instance `ℝ`), with good properties. However, sometimes, one would like to define the norm starting only from a well-behaved scalar product. This is what we implement in this paragraph, starting from a structure `inner_product_space.core` stating that we have a nice scalar product. Our goal here is not to develop a whole theory with all the supporting API, as this will be done below for `inner_product_space`. Instead, we implement the bare minimum to go as directly as possible to the construction of the norm and the proof of the triangular inequality. -/ /-- A structure requiring that a scalar product is positive definite and symmetric, from which one can construct an `inner_product_space` instance in `inner_product_space.of_core`. -/ @[nolint has_inhabited_instance] structure inner_product_space.core (F : Type*) [add_comm_group F] [semimodule ℝ F] := (inner : F → F → ℝ) (comm : ∀ x y, inner x y = inner y x) (nonneg : ∀ x, 0 ≤ inner x x) (definite : ∀ x, inner x x = 0 → x = 0) (add_left : ∀ x y z, inner (x + y) z = inner x z + inner y z) (smul_left : ∀ x y r, inner (r • x) y = r * inner x y) /- We set `inner_product_space.core` to be a class as we will use it as such in the construction of the normed space structure that it produces. However, all the instances we will use will be local to this proof. -/ attribute [class] inner_product_space.core namespace inner_product_space.of_core variables [add_comm_group F] [semimodule ℝ F] [c : inner_product_space.core F] include c /-- Inner product constructed from an `inner_product_space.core` structure -/ def to_has_inner : has_inner F := { inner := c.inner } local attribute [instance] to_has_inner lemma inner_comm (x y : F) : inner x y = inner y x := c.comm x y lemma inner_add_left (x y z : F) : inner (x + y) z = inner x z + inner y z := c.add_left _ _ _ lemma inner_add_right (x y z : F) : inner x (y + z) = inner x y + inner x z := by { rw [inner_comm, inner_add_left], simp [inner_comm] } lemma inner_smul_left (x y : F) (r : ℝ) : inner (r • x) y = r * inner x y := c.smul_left _ _ _ lemma inner_smul_right (x y : F) (r : ℝ) : inner x (r • y) = r * inner x y := by { rw [inner_comm, inner_smul_left, inner_comm] } /-- Norm constructed from an `inner_product_space.core` structure, defined to be the square root of the scalar product. -/ def to_has_norm : has_norm F := { norm := λ x, sqrt (inner x x) } local attribute [instance] to_has_norm lemma norm_eq_sqrt_inner (x : F) : ∥x∥ = sqrt (inner x x) := rfl lemma inner_self_eq_norm_square (x : F) : inner x x = ∥x∥ * ∥x∥ := (mul_self_sqrt (c.nonneg _)).symm /-- Expand `inner (x + y) (x + y)` -/ lemma inner_add_add_self (x y : F) : inner (x + y) (x + y) = inner x x + 2 * inner x y + inner y y := by simpa [inner_add_left, inner_add_right, two_mul, add_assoc] using inner_comm _ _ /-- Cauchy–Schwarz inequality -/ lemma inner_mul_inner_self_le (x y : F) : inner x y * inner x y ≤ inner x x * inner y y := begin have : ∀ t, 0 ≤ inner y y * t * t + 2 * inner x y * t + inner x x, from assume t, calc 0 ≤ inner (x+t•y) (x+t•y) : c.nonneg _ ... = inner y y * t * t + 2 * inner x y * t + inner x x : by { simp only [inner_add_add_self, inner_smul_right, inner_smul_left], ring }, have := discriminant_le_zero this, rw discrim at this, have h : (2 * inner x y)^2 - 4 * inner y y * inner x x = 4 * (inner x y * inner x y - inner x x * inner y y) := by ring, rw h at this, linarith end /-- Cauchy–Schwarz inequality with norm -/ lemma abs_inner_le_norm (x y : F) : abs (inner x y) ≤ ∥x∥ * ∥y∥ := nonneg_le_nonneg_of_squares_le (mul_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) begin rw abs_mul_abs_self, have : ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥) = inner x x * inner y y, simp only [inner_self_eq_norm_square], ring, rw this, exact inner_mul_inner_self_le _ _ end /-- Normed group structure constructed from an `inner_product_space.core` structure. -/ def to_normed_group : normed_group F := normed_group.of_core F { norm_eq_zero_iff := assume x, begin split, { assume h, rw [norm_eq_sqrt_inner, sqrt_eq_zero] at h, { exact c.definite x h }, { exact c.nonneg x } }, { rintros rfl, have : inner ((0 : ℝ) • (0 : F)) 0 = 0 * inner (0 : F) 0 := inner_smul_left _ _ _, simp at this, simp [norm, this] } end, norm_neg := assume x, begin have A : (- (1 : ℝ)) • x = -x, by simp, rw [norm_eq_sqrt_inner, norm_eq_sqrt_inner, ← A, inner_smul_left, inner_smul_right], simp, end, triangle := assume x y, begin have := calc ∥x + y∥ * ∥x + y∥ = inner (x + y) (x + y) : by rw inner_self_eq_norm_square ... = inner x x + 2 * inner x y + inner y y : inner_add_add_self _ _ ... ≤ inner x x + 2 * (∥x∥ * ∥y∥) + inner y y : by linarith [abs_inner_le_norm x y, le_abs_self (inner x y)] ... = (∥x∥ + ∥y∥) * (∥x∥ + ∥y∥) : by { simp only [inner_self_eq_norm_square], ring }, exact nonneg_le_nonneg_of_squares_le (add_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) this end } local attribute [instance] to_normed_group /-- Normed space structure constructed from an `inner_product_space.core` structure. -/ def to_normed_space : normed_space ℝ F := { norm_smul_le := assume r x, le_of_eq $ begin have A : 0 ≤ ∥r∥ * ∥x∥ := mul_nonneg (abs_nonneg _) (sqrt_nonneg _), rw [norm_eq_sqrt_inner, sqrt_eq_iff_mul_self_eq _ A, inner_smul_left, inner_smul_right, inner_self_eq_norm_square], { calc abs(r) * ∥x∥ * (abs(r) * ∥x∥) = (abs(r) * abs(r)) * (∥x∥ * ∥x∥) : by ring ... = r * (r * (∥x∥ * ∥x∥)) : by { rw abs_mul_abs_self, ring } }, { exact c.nonneg _ } end } end inner_product_space.of_core /-- Given an `inner_product_space.core` structure on a space, one can use it to turn the space into an inner product space, constructing the norm out of the inner product. -/ def inner_product_space.of_core [add_comm_group F] [semimodule ℝ F] (c : inner_product_space.core F) : inner_product_space F := begin letI : normed_group F := @inner_product_space.of_core.to_normed_group F _ _ c, letI : normed_space ℝ F := @inner_product_space.of_core.to_normed_space F _ _ c, exact { norm_eq_sqrt_inner := λ x, rfl, .. c } end /-! ### Properties of inner product spaces -/ variables [inner_product_space α] export inner_product_space (norm_eq_sqrt_inner) section basic_properties lemma inner_comm (x y : α) : inner x y = inner y x := inner_product_space.comm x y lemma inner_add_left {x y z : α} : inner (x + y) z = inner x z + inner y z := inner_product_space.add_left _ _ _ lemma inner_add_right {x y z : α} : inner x (y + z) = inner x y + inner x z := by { rw [inner_comm, inner_add_left], simp [inner_comm] } lemma inner_smul_left {x y : α} {r : ℝ} : inner (r • x) y = r * inner x y := inner_product_space.smul_left _ _ _ lemma inner_smul_right {x y : α} {r : ℝ} : inner x (r • y) = r * inner x y := by { rw [inner_comm, inner_smul_left, inner_comm] } variables (α) /-- The inner product as a bilinear form. -/ def bilin_form_of_inner : bilin_form ℝ α := { bilin := inner, bilin_add_left := λ x y z, inner_add_left, bilin_smul_left := λ a x y, inner_smul_left, bilin_add_right := λ x y z, inner_add_right, bilin_smul_right := λ a x y, inner_smul_right } variables {α} /-- An inner product with a sum on the left. -/ lemma sum_inner {ι : Type*} (s : finset ι) (f : ι → α) (x : α) : inner (∑ i in s, f i) x = ∑ i in s, inner (f i) x := bilin_form.map_sum_left (bilin_form_of_inner α) _ _ _ /-- An inner product with a sum on the right. -/ lemma inner_sum {ι : Type*} (s : finset ι) (f : ι → α) (x : α) : inner x (∑ i in s, f i) = ∑ i in s, inner x (f i) := bilin_form.map_sum_right (bilin_form_of_inner α) _ _ _ @[simp] lemma inner_zero_left {x : α} : inner 0 x = 0 := by { rw [← zero_smul ℝ (0:α), inner_smul_left, zero_mul] } @[simp] lemma inner_zero_right {x : α} : inner x 0 = 0 := by { rw [inner_comm, inner_zero_left] } @[simp] lemma inner_self_eq_zero {x : α} : inner x x = 0 ↔ x = 0 := ⟨λ h, by simpa [h] using norm_eq_sqrt_inner x, λ h, by simp [h]⟩ lemma inner_self_nonneg {x : α} : 0 ≤ inner x x := begin classical, by_cases h : x = 0, { simp [h] }, { have : 0 < sqrt (inner x x), { rw ← norm_eq_sqrt_inner, exact norm_pos_iff.mpr h }, exact le_of_lt (sqrt_pos.1 this) } end @[simp] lemma inner_self_nonpos {x : α} : inner x x ≤ 0 ↔ x = 0 := ⟨λ h, inner_self_eq_zero.1 (le_antisymm h inner_self_nonneg), λ h, h.symm ▸ le_of_eq inner_zero_left⟩ @[simp] lemma inner_neg_left {x y : α} : inner (-x) y = -inner x y := by { rw [← neg_one_smul ℝ x, inner_smul_left], simp } @[simp] lemma inner_neg_right {x y : α} : inner x (-y) = -inner x y := by { rw [inner_comm, inner_neg_left, inner_comm] } @[simp] lemma inner_neg_neg {x y : α} : inner (-x) (-y) = inner x y := by simp lemma inner_sub_left {x y z : α} : inner (x - y) z = inner x z - inner y z := by { simp [sub_eq_add_neg, inner_add_left] } lemma inner_sub_right {x y z : α} : inner x (y - z) = inner x y - inner x z := by { simp [sub_eq_add_neg, inner_add_right] } /-- Expand `inner (x + y) (x + y)` -/ lemma inner_add_add_self {x y : α} : inner (x + y) (x + y) = inner x x + 2 * inner x y + inner y y := by simpa [inner_add_left, inner_add_right, two_mul, add_assoc] using inner_comm _ _ /-- Expand `inner (x - y) (x - y)` -/ lemma inner_sub_sub_self {x y : α} : inner (x - y) (x - y) = inner x x - 2 * inner x y + inner y y := begin simp only [inner_sub_left, inner_sub_right, two_mul], simpa [sub_eq_add_neg, add_comm, add_left_comm] using inner_comm _ _ end /-- Parallelogram law -/ lemma parallelogram_law {x y : α} : inner (x + y) (x + y) + inner (x - y) (x - y) = 2 * (inner x x + inner y y) := by simp [inner_add_add_self, inner_sub_sub_self, two_mul, sub_eq_add_neg, add_comm, add_left_comm] /-- Cauchy–Schwarz inequality -/ lemma inner_mul_inner_self_le (x y : α) : inner x y * inner x y ≤ inner x x * inner y y := begin have : ∀ t, 0 ≤ inner y y * t * t + 2 * inner x y * t + inner x x, from assume t, calc 0 ≤ inner (x+t•y) (x+t•y) : inner_self_nonneg ... = inner y y * t * t + 2 * inner x y * t + inner x x : by { simp only [inner_add_add_self, inner_smul_right, inner_smul_left], ring }, have := discriminant_le_zero this, rw discrim at this, have h : (2 * inner x y)^2 - 4 * inner y y * inner x x = 4 * (inner x y * inner x y - inner x x * inner y y) := by ring, rw h at this, linarith end end basic_properties section norm lemma inner_self_eq_norm_square (x : α) : inner x x = ∥x∥ * ∥x∥ := by { rw norm_eq_sqrt_inner, exact (mul_self_sqrt inner_self_nonneg).symm } /-- Expand the square -/ lemma norm_add_pow_two {x y : α} : ∥x + y∥^2 = ∥x∥^2 + 2 * inner x y + ∥y∥^2 := by { repeat {rw [pow_two, ← inner_self_eq_norm_square]}, exact inner_add_add_self } /-- Same lemma as above but in a different form -/ lemma norm_add_mul_self {x y : α} : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + 2 * inner x y + ∥y∥ * ∥y∥ := by { repeat {rw [← pow_two]}, exact norm_add_pow_two } /-- Expand the square -/ lemma norm_sub_pow_two {x y : α} : ∥x - y∥^2 = ∥x∥^2 - 2 * inner x y + ∥y∥^2 := by { repeat {rw [pow_two, ← inner_self_eq_norm_square]}, exact inner_sub_sub_self } /-- Same lemma as above but in a different form -/ lemma norm_sub_mul_self {x y : α} : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ - 2 * inner x y + ∥y∥ * ∥y∥ := by { repeat {rw [← pow_two]}, exact norm_sub_pow_two } /-- Cauchy–Schwarz inequality with norm -/ lemma abs_inner_le_norm (x y : α) : abs (inner x y) ≤ ∥x∥ * ∥y∥ := nonneg_le_nonneg_of_squares_le (mul_nonneg (norm_nonneg _) (norm_nonneg _)) begin rw abs_mul_abs_self, have : ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥) = inner x x * inner y y, simp only [inner_self_eq_norm_square], ring, rw this, exact inner_mul_inner_self_le _ _ end lemma parallelogram_law_with_norm {x y : α} : ∥x + y∥ * ∥x + y∥ + ∥x - y∥ * ∥x - y∥ = 2 * (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥) := by { simp only [(inner_self_eq_norm_square _).symm], exact parallelogram_law } /-- The inner product, in terms of the norm. -/ lemma inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : α) : inner x y = (∥x + y∥ * ∥x + y∥ - ∥x∥ * ∥x∥ - ∥y∥ * ∥y∥) / 2 := begin rw norm_add_mul_self, ring end /-- The inner product, in terms of the norm. -/ lemma inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : α) : inner x y = (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ - ∥x - y∥ * ∥x - y∥) / 2 := begin rw norm_sub_mul_self, ring end /-- The inner product, in terms of the norm. -/ lemma inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four (x y : α) : inner x y = (∥x + y∥ * ∥x + y∥ - ∥x - y∥ * ∥x - y∥) / 4 := begin rw [norm_add_mul_self, norm_sub_mul_self], ring end /-- Pythagorean theorem, if-and-only-if vector inner product form. -/ lemma norm_add_square_eq_norm_square_add_norm_square_iff_inner_eq_zero (x y : α) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ ↔ inner x y = 0 := begin rw [norm_add_mul_self, add_right_cancel_iff, add_right_eq_self, mul_eq_zero], norm_num end /-- Pythagorean theorem, vector inner product form. -/ lemma norm_add_square_eq_norm_square_add_norm_square {x y : α} (h : inner x y = 0) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := (norm_add_square_eq_norm_square_add_norm_square_iff_inner_eq_zero x y).2 h /-- Pythagorean theorem, subtracting vectors, if-and-only-if vector inner product form. -/ lemma norm_sub_square_eq_norm_square_add_norm_square_iff_inner_eq_zero (x y : α) : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ ↔ inner x y = 0 := begin rw [norm_sub_mul_self, add_right_cancel_iff, sub_eq_add_neg, add_right_eq_self, neg_eq_zero, mul_eq_zero], norm_num end /-- Pythagorean theorem, subtracting vectors, vector inner product form. -/ lemma norm_sub_square_eq_norm_square_add_norm_square {x y : α} (h : inner x y = 0) : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := (norm_sub_square_eq_norm_square_add_norm_square_iff_inner_eq_zero x y).2 h /-- The inner product of two vectors, divided by the product of their norms, has absolute value at most 1. -/ lemma abs_inner_div_norm_mul_norm_le_one (x y : α) : abs (inner x y / (∥x∥ * ∥y∥)) ≤ 1 := begin rw abs_div, by_cases h : 0 = abs (∥x∥ * ∥y∥), { rw [←h, div_zero], norm_num }, { apply div_le_of_le_mul (lt_of_le_of_ne (ge_iff_le.mp (abs_nonneg (∥x∥ * ∥y∥))) h), convert abs_inner_le_norm x y using 1, rw [abs_mul, abs_of_nonneg (norm_nonneg x), abs_of_nonneg (norm_nonneg y), mul_one] } end /-- The inner product of a vector with a multiple of itself. -/ lemma inner_smul_self_left (x : α) (r : ℝ) : inner (r • x) x = r * (∥x∥ * ∥x∥) := by rw [inner_smul_left, ←inner_self_eq_norm_square] /-- The inner product of a vector with a multiple of itself. -/ lemma inner_smul_self_right (x : α) (r : ℝ) : inner x (r • x) = r * (∥x∥ * ∥x∥) := by rw [inner_smul_right, ←inner_self_eq_norm_square] /-- The inner product of a nonzero vector with a nonzero multiple of itself, divided by the product of their norms, has absolute value 1. -/ lemma abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul {x : α} {r : ℝ} (hx : x ≠ 0) (hr : r ≠ 0) : abs (inner x (r • x) / (∥x∥ * ∥r • x∥)) = 1 := begin rw [inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (abs r), mul_assoc, abs_div, abs_mul r, abs_mul (abs r), abs_abs, div_self], exact mul_ne_zero (λ h, hr (eq_zero_of_abs_eq_zero h)) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero (eq_zero_of_abs_eq_zero h)))) end /-- The inner product of a nonzero vector with a positive multiple of itself, divided by the product of their norms, has value 1. -/ lemma inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul {x : α} {r : ℝ} (hx : x ≠ 0) (hr : 0 < r) : inner x (r • x) / (∥x∥ * ∥r • x∥) = 1 := begin rw [inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (abs r), mul_assoc, abs_of_nonneg (le_of_lt hr), div_self], exact mul_ne_zero (ne_of_gt hr) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero h))) end /-- The inner product of a nonzero vector with a negative multiple of itself, divided by the product of their norms, has value -1. -/ lemma inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul {x : α} {r : ℝ} (hx : x ≠ 0) (hr : r < 0) : inner x (r • x) / (∥x∥ * ∥r • x∥) = -1 := begin rw [inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (abs r), mul_assoc, abs_of_neg hr, ←neg_mul_eq_neg_mul, div_neg_eq_neg_div, div_self], exact mul_ne_zero (ne_of_lt hr) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero h))) end /-- The inner product of two vectors, divided by the product of their norms, has absolute value 1 if and only if they are nonzero and one is a multiple of the other. One form of equality case for Cauchy-Schwarz. -/ lemma abs_inner_div_norm_mul_norm_eq_one_iff (x y : α) : abs (inner x y / (∥x∥ * ∥y∥)) = 1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r ≠ 0 ∧ y = r • x) := begin split, { intro h, have hx0 : x ≠ 0, { intro hx0, rw [hx0, inner_zero_left, zero_div] at h, norm_num at h, exact h }, refine and.intro hx0 _, set r := inner x y / (∥x∥ * ∥x∥) with hr, use r, set t := y - r • x with ht, have ht0 : inner x t = 0, { rw [ht, inner_sub_right, inner_smul_right, hr, ←inner_self_eq_norm_square, div_mul_cancel _ (λ h, hx0 (inner_self_eq_zero.1 h)), sub_self] }, rw [←sub_add_cancel y (r • x), ←ht, inner_add_right, ht0, zero_add, inner_smul_right, inner_self_eq_norm_square, ←mul_assoc, mul_comm, mul_div_mul_left _ _ (λ h, hx0 (norm_eq_zero.1 h)), abs_div, abs_mul, abs_of_nonneg (norm_nonneg _), abs_of_nonneg (norm_nonneg _), ←real.norm_eq_abs, ←norm_smul] at h, have hr0 : r ≠ 0, { intro hr0, rw [hr0, zero_smul, norm_zero, zero_div] at h, norm_num at h }, refine and.intro hr0 _, have h2 : ∥r • x∥ ^ 2 = ∥t + r • x∥ ^ 2, { congr' 1, refine eq_of_div_eq_one _ _ h, intro h0, rw [h0, div_zero] at h, norm_num at h }, rw [pow_two, pow_two, ←inner_self_eq_norm_square, ←inner_self_eq_norm_square, inner_add_add_self] at h2, conv_rhs at h2 { congr, congr, skip, rw [inner_smul_right, inner_comm, ht0, mul_zero, mul_zero] }, symmetry' at h2, rw [add_zero, add_left_eq_self, inner_self_eq_zero] at h2, rw h2 at ht, exact eq_of_sub_eq_zero ht.symm }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul hx hr } end /-- The inner product of two vectors, divided by the product of their norms, has value 1 if and only if they are nonzero and one is a positive multiple of the other. -/ lemma inner_div_norm_mul_norm_eq_one_iff (x y : α) : inner x y / (∥x∥ * ∥y∥) = 1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), 0 < r ∧ y = r • x) := begin split, { intro h, have ha := h, apply_fun abs at ha, norm_num at ha, rcases (abs_inner_div_norm_mul_norm_eq_one_iff x y).1 ha with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, use [hx, r], refine and.intro _ hy, by_contradiction hrneg, rw hy at h, rw inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul hx (lt_of_le_of_ne' (le_of_not_lt hrneg) hr) at h, norm_num at h }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul hx hr } end /-- The inner product of two vectors, divided by the product of their norms, has value -1 if and only if they are nonzero and one is a negative multiple of the other. -/ lemma inner_div_norm_mul_norm_eq_neg_one_iff (x y : α) : inner x y / (∥x∥ * ∥y∥) = -1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r < 0 ∧ y = r • x) := begin split, { intro h, have ha := h, apply_fun abs at ha, norm_num at ha, rcases (abs_inner_div_norm_mul_norm_eq_one_iff x y).1 ha with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, use [hx, r], refine and.intro _ hy, by_contradiction hrpos, rw hy at h, rw inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul hx (lt_of_le_of_ne' (le_of_not_lt hrpos) hr.symm) at h, norm_num at h }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul hx hr } end /-- The inner product of two weighted sums, where the weights in each sum add to 0, in terms of the norms of pairwise differences. -/ lemma inner_sum_smul_sum_smul_of_sum_eq_zero {ι₁ : Type*} {s₁ : finset ι₁} {w₁ : ι₁ → ℝ} (v₁ : ι₁ → α) (h₁ : ∑ i in s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : finset ι₂} {w₂ : ι₂ → ℝ} (v₂ : ι₂ → α) (h₂ : ∑ i in s₂, w₂ i = 0) : inner (∑ i₁ in s₁, w₁ i₁ • v₁ i₁) (∑ i₂ in s₂, w₂ i₂ • v₂ i₂) = (-∑ i₁ in s₁, ∑ i₂ in s₂, w₁ i₁ * w₂ i₂ * (∥v₁ i₁ - v₂ i₂∥ * ∥v₁ i₁ - v₂ i₂∥)) / 2 := by simp_rw [sum_inner, inner_sum, inner_smul_left, inner_smul_right, inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two, ←div_sub_div_same, ←div_add_div_same, mul_sub_left_distrib, left_distrib, finset.sum_sub_distrib, finset.sum_add_distrib, ←finset.mul_sum, ←finset.sum_mul, h₁, h₂, zero_mul, mul_zero, finset.sum_const_zero, zero_add, zero_sub, finset.mul_sum, neg_div, finset.sum_div, mul_div_assoc, mul_assoc] end norm /-! ### Inner product space structure on product spaces -/ -- TODO [Lean 3.15]: drop some of these `show`s /-- If `ι` is a finite type and each space `f i`, `i : ι`, is an inner product space, then `Π i, f i` is an inner product space as well. Since `Π i, f i` is endowed with the sup norm, we use instead `pi_Lp 2 one_le_two f` for the product space, which is endowed with the `L^2` norm. -/ instance pi_Lp.inner_product_space (ι : Type*) [fintype ι] (f : ι → Type*) [Π i, inner_product_space (f i)] : inner_product_space (pi_Lp 2 one_le_two f) := { inner := λ x y, ∑ i, inner (x i) (y i), norm_eq_sqrt_inner := begin assume x, have : (2 : ℝ)⁻¹ * 2 = 1, by norm_num, simp [inner, pi_Lp.norm_eq, norm_eq_sqrt_inner, sqrt_eq_rpow, ← rpow_mul (inner_self_nonneg), this], end, comm := λ x y, finset.sum_congr rfl $ λ i hi, inner_comm (x i) (y i), add_left := λ x y z, show ∑ i, inner (x i + y i) (z i) = ∑ i, inner (x i) (z i) + ∑ i, inner (y i) (z i), by simp only [inner_add_left, finset.sum_add_distrib], smul_left := λ x y r, show ∑ (i : ι), inner (r • x i) (y i) = r * ∑ i, inner (x i) (y i), by simp only [finset.mul_sum, inner_smul_left] } /-- The type of real numbers is an inner product space. -/ instance real.inner_product_space : inner_product_space ℝ := { inner := (*), norm_eq_sqrt_inner := λ x, by simp [inner, norm_eq_abs, sqrt_mul_self_eq_abs], comm := mul_comm, add_left := add_mul, smul_left := λ _ _ _, mul_assoc _ _ _ } /-- The standard Euclidean space, functions on a finite type. For an `n`-dimensional space use `euclidean_space (fin n)`. -/ @[reducible, nolint unused_arguments] def euclidean_space (n : Type*) [fintype n] : Type* := pi_Lp 2 one_le_two (λ (i : n), ℝ) section pi_Lp local attribute [reducible] pi_Lp variables (n : Type*) [fintype n] instance : finite_dimensional ℝ (euclidean_space n) := by apply_instance @[simp] lemma findim_euclidean_space : finite_dimensional.findim ℝ (euclidean_space n) = fintype.card n := by simp lemma findim_euclidean_space_fin {n : ℕ} : finite_dimensional.findim ℝ (euclidean_space (fin n)) = n := by simp end pi_Lp /-! ### Orthogonal projection in inner product spaces -/ section orthogonal open filter /-- Existence of minimizers Let `u` be a point in an inner product space, and let `K` be a nonempty complete convex subset. Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`. -/ -- FIXME this monolithic proof causes a deterministic timeout with `-T50000` -- It should be broken in a sequence of more manageable pieces, -- perhaps with individual statements for the three steps below. theorem exists_norm_eq_infi_of_complete_convex {K : set α} (ne : K.nonempty) (h₁ : is_complete K) (h₂ : convex K) : ∀ u : α, ∃ v ∈ K, ∥u - v∥ = ⨅ w : K, ∥u - w∥ := assume u, begin let δ := ⨅ w : K, ∥u - w∥, letI : nonempty K := ne.to_subtype, have zero_le_δ : 0 ≤ δ := le_cinfi (λ _, norm_nonneg _), have δ_le : ∀ w : K, δ ≤ ∥u - w∥, from cinfi_le ⟨0, forall_range_iff.2 $ λ _, norm_nonneg _⟩, have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩, -- Step 1: since `δ` is the infimum, can find a sequence `w : ℕ → K` in `K` -- such that `∥u - w n∥ < δ + 1 / (n + 1)` (which implies `∥u - w n∥ --> δ`); -- maybe this should be a separate lemma have exists_seq : ∃ w : ℕ → K, ∀ n, ∥u - w n∥ < δ + 1 / (n + 1), { have hδ : ∀n:ℕ, δ < δ + 1 / (n + 1), from λ n, lt_add_of_le_of_pos (le_refl _) nat.one_div_pos_of_nat, have h := λ n, exists_lt_of_cinfi_lt (hδ n), let w : ℕ → K := λ n, classical.some (h n), exact ⟨w, λ n, classical.some_spec (h n)⟩ }, rcases exists_seq with ⟨w, hw⟩, have norm_tendsto : tendsto (λ n, ∥u - w n∥) at_top (𝓝 δ), { have h : tendsto (λ n:ℕ, δ) at_top (𝓝 δ) := tendsto_const_nhds, have h' : tendsto (λ n:ℕ, δ + 1 / (n + 1)) at_top (𝓝 δ), { convert h.add tendsto_one_div_add_at_top_nhds_0_nat, simp only [add_zero] }, exact tendsto_of_tendsto_of_tendsto_of_le_of_le h h' (λ x, δ_le _) (λ x, le_of_lt (hw _)) }, -- Step 2: Prove that the sequence `w : ℕ → K` is a Cauchy sequence have seq_is_cauchy : cauchy_seq (λ n, ((w n):α)), { rw cauchy_seq_iff_le_tendsto_0, -- splits into three goals let b := λ n:ℕ, (8 * δ * (1/(n+1)) + 4 * (1/(n+1)) * (1/(n+1))), use (λn, sqrt (b n)), split, -- first goal : `∀ (n : ℕ), 0 ≤ sqrt (b n)` assume n, exact sqrt_nonneg _, split, -- second goal : `∀ (n m N : ℕ), N ≤ n → N ≤ m → dist ↑(w n) ↑(w m) ≤ sqrt (b N)` assume p q N hp hq, let wp := ((w p):α), let wq := ((w q):α), let a := u - wq, let b := u - wp, let half := 1 / (2:ℝ), let div := 1 / ((N:ℝ) + 1), have : 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) := calc 4 * ∥u - half•(wq + wp)∥ * ∥u - half•(wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ = (2*∥u - half•(wq + wp)∥) * (2 * ∥u - half•(wq + wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by ring ... = (abs((2:ℝ)) * ∥u - half•(wq + wp)∥) * (abs((2:ℝ)) * ∥u - half•(wq+wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by { rw abs_of_nonneg, exact add_nonneg zero_le_one zero_le_one } ... = ∥(2:ℝ) • (u - half • (wq + wp))∥ * ∥(2:ℝ) • (u - half • (wq + wp))∥ + ∥wp-wq∥ * ∥wp-wq∥ : by { rw [norm_smul], refl } ... = ∥a + b∥ * ∥a + b∥ + ∥a - b∥ * ∥a - b∥ : begin rw [smul_sub, smul_smul, mul_one_div_cancel (two_ne_zero : (2 : ℝ) ≠ 0), ← one_add_one_eq_two, add_smul], simp only [one_smul], have eq₁ : wp - wq = a - b, show wp - wq = (u - wq) - (u - wp), abel, have eq₂ : u + u - (wq + wp) = a + b, show u + u - (wq + wp) = (u - wq) + (u - wp), abel, rw [eq₁, eq₂], end ... = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) : parallelogram_law_with_norm, have eq : δ ≤ ∥u - half • (wq + wp)∥, { rw smul_add, apply δ_le', apply h₂, repeat {exact subtype.mem _}, repeat {exact le_of_lt one_half_pos}, exact add_halves 1 }, have eq₁ : 4 * δ * δ ≤ 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥, { mono, mono, norm_num, apply mul_nonneg, norm_num, exact norm_nonneg _ }, have eq₂ : ∥a∥ * ∥a∥ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw q) (add_le_add_left (nat.one_div_le_one_div hq) _)), have eq₂' : ∥b∥ * ∥b∥ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw p) (add_le_add_left (nat.one_div_le_one_div hp) _)), rw dist_eq_norm, apply nonneg_le_nonneg_of_squares_le, { exact sqrt_nonneg _ }, rw mul_self_sqrt, exact calc ∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥*∥a∥ + ∥b∥*∥b∥) - 4 * ∥u - half • (wq+wp)∥ * ∥u - half • (wq+wp)∥ : by { rw ← this, simp } ... ≤ 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) - 4 * δ * δ : sub_le_sub_left eq₁ _ ... ≤ 2 * ((δ + div) * (δ + div) + (δ + div) * (δ + div)) - 4 * δ * δ : sub_le_sub_right (mul_le_mul_of_nonneg_left (add_le_add eq₂ eq₂') (by norm_num)) _ ... = 8 * δ * div + 4 * div * div : by ring, exact add_nonneg (mul_nonneg (mul_nonneg (by norm_num) zero_le_δ) (le_of_lt nat.one_div_pos_of_nat)) (mul_nonneg (mul_nonneg (by norm_num) (le_of_lt nat.one_div_pos_of_nat)) (le_of_lt nat.one_div_pos_of_nat)), -- third goal : `tendsto (λ (n : ℕ), sqrt (b n)) at_top (𝓝 0)` apply tendsto.comp, { convert continuous_sqrt.continuous_at, exact sqrt_zero.symm }, have eq₁ : tendsto (λ (n : ℕ), 8 * δ * (1 / (n + 1))) at_top (𝓝 (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (8 * δ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1))) at_top (𝓝 (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (4:ℝ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have eq₂ : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1)) * (1 / (n + 1))) at_top (𝓝 (0:ℝ)), { convert this.mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, convert eq₁.add eq₂, simp only [add_zero] }, -- Step 3: By completeness of `K`, let `w : ℕ → K` converge to some `v : K`. -- Prove that it satisfies all requirements. rcases cauchy_seq_tendsto_of_is_complete h₁ (λ n, _) seq_is_cauchy with ⟨v, hv, w_tendsto⟩, use v, use hv, have h_cont : continuous (λ v, ∥u - v∥) := continuous.comp continuous_norm (continuous.sub continuous_const continuous_id), have : tendsto (λ n, ∥u - w n∥) at_top (𝓝 ∥u - v∥), convert (tendsto.comp h_cont.continuous_at w_tendsto), exact tendsto_nhds_unique at_top_ne_bot this norm_tendsto, exact subtype.mem _ end /-- Characterization of minimizers in the above theorem -/ theorem norm_eq_infi_iff_inner_le_zero {K : set α} (h : convex K) {u : α} {v : α} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : K, ∥u - w∥) ↔ ∀ w ∈ K, inner (u - v) (w - v) ≤ 0 := iff.intro begin assume eq w hw, let δ := ⨅ w : K, ∥u - w∥, let p := inner (u - v) (w - v), let q := ∥w - v∥^2, letI : nonempty K := ⟨⟨v, hv⟩⟩, have zero_le_δ : 0 ≤ δ, apply le_cinfi, intro, exact norm_nonneg _, have δ_le : ∀ w : K, δ ≤ ∥u - w∥, assume w, apply cinfi_le, use (0:ℝ), rintros _ ⟨_, rfl⟩, exact norm_nonneg _, have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩, have : ∀θ:ℝ, 0 < θ → θ ≤ 1 → 2 * p ≤ θ * q, assume θ hθ₁ hθ₂, have : ∥u - v∥^2 ≤ ∥u - v∥^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*∥w - v∥^2 := calc ∥u - v∥^2 ≤ ∥u - (θ•w + (1-θ)•v)∥^2 : begin simp only [pow_two], apply mul_self_le_mul_self (norm_nonneg _), rw [eq], apply δ_le', apply h hw hv, exacts [le_of_lt hθ₁, sub_nonneg.2 hθ₂, add_sub_cancel'_right _ _], end ... = ∥(u - v) - θ • (w - v)∥^2 : begin have : u - (θ•w + (1-θ)•v) = (u - v) - θ • (w - v), { rw [smul_sub, sub_smul, one_smul], simp only [sub_eq_add_neg, add_comm, add_left_comm, add_assoc, neg_add_rev] }, rw this end ... = ∥u - v∥^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*∥w - v∥^2 : begin rw [norm_sub_pow_two, inner_smul_right, norm_smul], simp only [pow_two], show ∥u-v∥*∥u-v∥-2*(θ*inner(u-v)(w-v))+abs(θ)*∥w-v∥*(abs(θ)*∥w-v∥)= ∥u-v∥*∥u-v∥-2*θ*inner(u-v)(w-v)+θ*θ*(∥w-v∥*∥w-v∥), rw abs_of_pos hθ₁, ring end, have eq₁ : ∥u-v∥^2-2*θ*inner(u-v)(w-v)+θ*θ*∥w-v∥^2=∥u-v∥^2+(θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)), abel, rw [eq₁, le_add_iff_nonneg_right] at this, have eq₂ : θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)=θ*(θ*∥w-v∥^2-2*inner(u-v)(w-v)), ring, rw eq₂ at this, have := le_of_sub_nonneg (nonneg_of_mul_nonneg_left this hθ₁), exact this, by_cases hq : q = 0, { rw hq at this, have : p ≤ 0, have := this (1:ℝ) (by norm_num) (by norm_num), linarith, exact this }, { have q_pos : 0 < q, apply lt_of_le_of_ne, exact pow_two_nonneg _, intro h, exact hq h.symm, by_contradiction hp, rw not_le at hp, let θ := min (1:ℝ) (p / q), have eq₁ : θ*q ≤ p := calc θ*q ≤ (p/q) * q : mul_le_mul_of_nonneg_right (min_le_right _ _) (pow_two_nonneg _) ... = p : div_mul_cancel _ hq, have : 2 * p ≤ p := calc 2 * p ≤ θ*q : by { refine this θ (lt_min (by norm_num) (div_pos hp q_pos)) (by norm_num) } ... ≤ p : eq₁, linarith } end begin assume h, letI : nonempty K := ⟨⟨v, hv⟩⟩, apply le_antisymm, { apply le_cinfi, assume w, apply nonneg_le_nonneg_of_squares_le (norm_nonneg _), have := h w w.2, exact calc ∥u - v∥ * ∥u - v∥ ≤ ∥u - v∥ * ∥u - v∥ - 2 * inner (u - v) ((w:α) - v) : by linarith ... ≤ ∥u - v∥^2 - 2 * inner (u - v) ((w:α) - v) + ∥(w:α) - v∥^2 : by { rw pow_two, refine le_add_of_nonneg_right _, exact pow_two_nonneg _ } ... = ∥(u - v) - (w - v)∥^2 : norm_sub_pow_two.symm ... = ∥u - w∥ * ∥u - w∥ : by { have : (u - v) - (w - v) = u - w, abel, rw [this, pow_two] } }, { show (⨅ (w : K), ∥u - w∥) ≤ (λw:K, ∥u - w∥) ⟨v, hv⟩, apply cinfi_le, use 0, rintros y ⟨z, rfl⟩, exact norm_nonneg _ } end /-- Existence of minimizers. Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`. This point `v` is usually called the orthogonal projection of `u` onto `K`. -/ theorem exists_norm_eq_infi_of_complete_subspace (K : subspace ℝ α) (h : is_complete (↑K : set α)) : ∀ u : α, ∃ v ∈ K, ∥u - v∥ = ⨅ w : (↑K : set α), ∥u - w∥ := exists_norm_eq_infi_of_complete_convex ⟨0, K.zero_mem⟩ h K.convex /-- Characterization of minimizers in the above theorem. Let `u` be a point in an inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `∥u - v∥` if and only if for all `w ∈ K`, `inner (u - v) w = 0` (i.e., `u - v` is orthogonal to the subspace `K`) -/ theorem norm_eq_infi_iff_inner_eq_zero (K : subspace ℝ α) {u : α} {v : α} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : (↑K : set α), ∥u - w∥) ↔ ∀ w ∈ K, inner (u - v) w = 0 := iff.intro begin assume h, have h : ∀ w ∈ K, inner (u - v) (w - v) ≤ 0, { rwa [norm_eq_infi_iff_inner_le_zero] at h, exacts [K.convex, hv] }, assume w hw, have le : inner (u - v) w ≤ 0, let w' := w + v, have : w' ∈ K := submodule.add_mem _ hw hv, have h₁ := h w' this, have h₂ : w' - v = w, simp only [add_neg_cancel_right, sub_eq_add_neg], rw h₂ at h₁, exact h₁, have ge : inner (u - v) w ≥ 0, let w'' := -w + v, have : w'' ∈ K := submodule.add_mem _ (submodule.neg_mem _ hw) hv, have h₁ := h w'' this, have h₂ : w'' - v = -w, simp only [neg_inj', add_neg_cancel_right, sub_eq_add_neg], rw [h₂, inner_neg_right] at h₁, linarith, exact le_antisymm le ge end begin assume h, have : ∀ w ∈ K, inner (u - v) (w - v) ≤ 0, assume w hw, let w' := w - v, have : w' ∈ K := submodule.sub_mem _ hw hv, have h₁ := h w' this, exact le_of_eq h₁, rwa norm_eq_infi_iff_inner_le_zero, exacts [submodule.convex _, hv] end end orthogonal
ae615fee78f20dd3989f90ef4b02b8f8e2d80bbb
94637389e03c919023691dcd05bd4411b1034aa5
/src/zzz_junk/04_type_library/06_sum.lean
3baeabd18497f08c06d12b6b1f02f3552fcbfa65
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
590
lean
namespace hidden /- The "sum" type is used to hold either a value of a type, α, or a value of another type, β. A value of the sum type is often used to represent either a correct value or an error value (e.g., an error string or error code. Note: in Haskell this type is called Either. -/ inductive sum (α β : Type) : Type | inl (a : α) : sum | inr (b : β) : sum /- Exercise: define a function, t2z, that takes a boolean argument, b. If b is tt, t2z returns the natural number, 0. If b is not tt, the function returns an error string, "Oops, argument not true." -/ end hidden
e534a4c5584a6b5a5a8c10c0821598df504454a1
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/constructions/prod/integral.lean
e92c1173627f986c379ce17559f80ce211a19e8a
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
23,509
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import measure_theory.constructions.prod.basic import measure_theory.integral.set_integral /-! # Integration with respect to the product measure > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove Fubini's theorem. ## Main results * `measure_theory.integrable_prod_iff` states that a binary function is integrable iff both * `y ↦ f (x, y)` is integrable for almost every `x`, and * the function `x ↦ ∫ ‖f (x, y)‖ dy` is integrable. * `measure_theory.integral_prod`: Fubini's theorem. It states that for a integrable function `α × β → E` (where `E` is a second countable Banach space) we have `∫ z, f z ∂(μ.prod ν) = ∫ x, ∫ y, f (x, y) ∂ν ∂μ`. This theorem has the same variants as Tonelli's theorem (see `measure_theory.lintegral_prod`). The lemma `measure_theory.integrable.integral_prod_right` states that the inner integral of the right-hand side is integrable. ## Tags product measure, Fubini's theorem, Fubini-Tonelli theorem -/ noncomputable theory open_locale classical topology ennreal measure_theory open set function real ennreal open measure_theory measurable_space measure_theory.measure open topological_space open filter (hiding prod_eq map) variables {α α' β β' γ E : Type*} variables [measurable_space α] [measurable_space α'] [measurable_space β] [measurable_space β'] variables [measurable_space γ] variables {μ μ' : measure α} {ν ν' : measure β} {τ : measure γ} variables [normed_add_comm_group E] /-! ### Measurability Before we define the product measure, we can talk about the measurability of operations on binary functions. We show that if `f` is a binary measurable function, then the function that integrates along one of the variables (using either the Lebesgue or Bochner integral) is measurable. -/ lemma measurable_set_integrable [sigma_finite ν] ⦃f : α → β → E⦄ (hf : strongly_measurable (uncurry f)) : measurable_set {x | integrable (f x) ν} := begin simp_rw [integrable, hf.of_uncurry_left.ae_strongly_measurable, true_and], exact measurable_set_lt (measurable.lintegral_prod_right hf.ennnorm) measurable_const end section variables [normed_space ℝ E] [complete_space E] /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is measurable. This version has `f` in curried form. -/ lemma measure_theory.strongly_measurable.integral_prod_right [sigma_finite ν] ⦃f : α → β → E⦄ (hf : strongly_measurable (uncurry f)) : strongly_measurable (λ x, ∫ y, f x y ∂ν) := begin borelize E, haveI : separable_space (range (uncurry f) ∪ {0} : set E) := hf.separable_space_range_union_singleton, let s : ℕ → simple_func (α × β) E := simple_func.approx_on _ hf.measurable (range (uncurry f) ∪ {0}) 0 (by simp), let s' : ℕ → α → simple_func β E := λ n x, (s n).comp (prod.mk x) measurable_prod_mk_left, let f' : ℕ → α → E := λ n, {x | integrable (f x) ν}.indicator (λ x, (s' n x).integral ν), have hf' : ∀ n, strongly_measurable (f' n), { intro n, refine strongly_measurable.indicator _ (measurable_set_integrable hf), have : ∀ x, (s' n x).range.filter (λ x, x ≠ 0) ⊆ (s n).range, { intros x, refine finset.subset.trans (finset.filter_subset _ _) _, intro y, simp_rw [simple_func.mem_range], rintro ⟨z, rfl⟩, exact ⟨(x, z), rfl⟩ }, simp only [simple_func.integral_eq_sum_of_subset (this _)], refine finset.strongly_measurable_sum _ (λ x _, _), refine (measurable.ennreal_to_real _).strongly_measurable.smul_const _, simp only [simple_func.coe_comp, preimage_comp] {single_pass := tt}, apply measurable_measure_prod_mk_left, exact (s n).measurable_set_fiber x }, have h2f' : tendsto f' at_top (𝓝 (λ (x : α), ∫ (y : β), f x y ∂ν)), { rw [tendsto_pi_nhds], intro x, by_cases hfx : integrable (f x) ν, { have : ∀ n, integrable (s' n x) ν, { intro n, apply (hfx.norm.add hfx.norm).mono' (s' n x).ae_strongly_measurable, apply eventually_of_forall, intro y, simp_rw [s', simple_func.coe_comp], exact simple_func.norm_approx_on_zero_le _ _ (x, y) n }, simp only [f', hfx, simple_func.integral_eq_integral _ (this _), indicator_of_mem, mem_set_of_eq], refine tendsto_integral_of_dominated_convergence (λ y, ‖f x y‖ + ‖f x y‖) (λ n, (s' n x).ae_strongly_measurable) (hfx.norm.add hfx.norm) _ _, { exact λ n, eventually_of_forall (λ y, simple_func.norm_approx_on_zero_le _ _ (x, y) n) }, { refine eventually_of_forall (λ y, simple_func.tendsto_approx_on _ _ _), apply subset_closure, simp [-uncurry_apply_pair], } }, { simp [f', hfx, integral_undef], } }, exact strongly_measurable_of_tendsto _ hf' h2f' end /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is measurable. -/ lemma measure_theory.strongly_measurable.integral_prod_right' [sigma_finite ν] ⦃f : α × β → E⦄ (hf : strongly_measurable f) : strongly_measurable (λ x, ∫ y, f (x, y) ∂ν) := by { rw [← uncurry_curry f] at hf, exact hf.integral_prod_right } /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Fubini's theorem is measurable. This version has `f` in curried form. -/ lemma measure_theory.strongly_measurable.integral_prod_left [sigma_finite μ] ⦃f : α → β → E⦄ (hf : strongly_measurable (uncurry f)) : strongly_measurable (λ y, ∫ x, f x y ∂μ) := (hf.comp_measurable measurable_swap).integral_prod_right' /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Fubini's theorem is measurable. -/ lemma measure_theory.strongly_measurable.integral_prod_left' [sigma_finite μ] ⦃f : α × β → E⦄ (hf : strongly_measurable f) : strongly_measurable (λ y, ∫ x, f (x, y) ∂μ) := (hf.comp_measurable measurable_swap).integral_prod_right' end /-! ### The product measure -/ namespace measure_theory namespace measure variables [sigma_finite ν] lemma integrable_measure_prod_mk_left {s : set (α × β)} (hs : measurable_set s) (h2s : (μ.prod ν) s ≠ ∞) : integrable (λ x, (ν (prod.mk x ⁻¹' s)).to_real) μ := begin refine ⟨(measurable_measure_prod_mk_left hs).ennreal_to_real.ae_measurable.ae_strongly_measurable, _⟩, simp_rw [has_finite_integral, ennnorm_eq_of_real to_real_nonneg], convert h2s.lt_top using 1, simp_rw [prod_apply hs], apply lintegral_congr_ae, refine (ae_measure_lt_top hs h2s).mp _, apply eventually_of_forall, intros x hx, rw [lt_top_iff_ne_top] at hx, simp [of_real_to_real, hx], end end measure open measure end measure_theory open measure_theory.measure section lemma measure_theory.ae_strongly_measurable.prod_swap {γ : Type*} [topological_space γ] [sigma_finite μ] [sigma_finite ν] {f : β × α → γ} (hf : ae_strongly_measurable f (ν.prod μ)) : ae_strongly_measurable (λ (z : α × β), f z.swap) (μ.prod ν) := by { rw ← prod_swap at hf, exact hf.comp_measurable measurable_swap } lemma measure_theory.ae_strongly_measurable.fst {γ} [topological_space γ] [sigma_finite ν] {f : α → γ} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ (z : α × β), f z.1) (μ.prod ν) := hf.comp_quasi_measure_preserving quasi_measure_preserving_fst lemma measure_theory.ae_strongly_measurable.snd {γ} [topological_space γ] [sigma_finite ν] {f : β → γ} (hf : ae_strongly_measurable f ν) : ae_strongly_measurable (λ (z : α × β), f z.2) (μ.prod ν) := hf.comp_quasi_measure_preserving quasi_measure_preserving_snd /-- The Bochner integral is a.e.-measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is a.e.-measurable. -/ lemma measure_theory.ae_strongly_measurable.integral_prod_right' [sigma_finite ν] [normed_space ℝ E] [complete_space E] ⦃f : α × β → E⦄ (hf : ae_strongly_measurable f (μ.prod ν)) : ae_strongly_measurable (λ x, ∫ y, f (x, y) ∂ν) μ := ⟨λ x, ∫ y, hf.mk f (x, y) ∂ν, hf.strongly_measurable_mk.integral_prod_right', by { filter_upwards [ae_ae_of_ae_prod hf.ae_eq_mk] with _ hx using integral_congr_ae hx }⟩ lemma measure_theory.ae_strongly_measurable.prod_mk_left {γ : Type*} [sigma_finite ν] [topological_space γ] {f : α × β → γ} (hf : ae_strongly_measurable f (μ.prod ν)) : ∀ᵐ x ∂μ, ae_strongly_measurable (λ y, f (x, y)) ν := begin filter_upwards [ae_ae_of_ae_prod hf.ae_eq_mk] with x hx, exact ⟨λ y, hf.mk f (x, y), hf.strongly_measurable_mk.comp_measurable measurable_prod_mk_left, hx⟩ end end namespace measure_theory variables [sigma_finite ν] /-! ### Integrability on a product -/ section lemma integrable.swap [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (f ∘ prod.swap) (ν.prod μ) := ⟨hf.ae_strongly_measurable.prod_swap, (lintegral_prod_swap _ hf.ae_strongly_measurable.ennnorm : _).le.trans_lt hf.has_finite_integral⟩ lemma integrable_swap_iff [sigma_finite μ] ⦃f : α × β → E⦄ : integrable (f ∘ prod.swap) (ν.prod μ) ↔ integrable f (μ.prod ν) := ⟨λ hf, by { convert hf.swap, ext ⟨x, y⟩, refl }, λ hf, hf.swap⟩ lemma has_finite_integral_prod_iff ⦃f : α × β → E⦄ (h1f : strongly_measurable f) : has_finite_integral f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, has_finite_integral (λ y, f (x, y)) ν) ∧ has_finite_integral (λ x, ∫ y, ‖f (x, y)‖ ∂ν) μ := begin simp only [has_finite_integral, lintegral_prod_of_measurable _ h1f.ennnorm], have : ∀ x, ∀ᵐ y ∂ν, 0 ≤ ‖f (x, y)‖ := λ x, eventually_of_forall (λ y, norm_nonneg _), simp_rw [integral_eq_lintegral_of_nonneg_ae (this _) (h1f.norm.comp_measurable measurable_prod_mk_left).ae_strongly_measurable, ennnorm_eq_of_real to_real_nonneg, of_real_norm_eq_coe_nnnorm], -- this fact is probably too specialized to be its own lemma have : ∀ {p q r : Prop} (h1 : r → p), (r ↔ p ∧ q) ↔ (p → (r ↔ q)) := λ p q r h1, by rw [← and.congr_right_iff, and_iff_right_of_imp h1], rw [this], { intro h2f, rw lintegral_congr_ae, refine h2f.mp _, apply eventually_of_forall, intros x hx, dsimp only, rw [of_real_to_real], rw [← lt_top_iff_ne_top], exact hx }, { intro h2f, refine ae_lt_top _ h2f.ne, exact h1f.ennnorm.lintegral_prod_right' }, end lemma has_finite_integral_prod_iff' ⦃f : α × β → E⦄ (h1f : ae_strongly_measurable f (μ.prod ν)) : has_finite_integral f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, has_finite_integral (λ y, f (x, y)) ν) ∧ has_finite_integral (λ x, ∫ y, ‖f (x, y)‖ ∂ν) μ := begin rw [has_finite_integral_congr h1f.ae_eq_mk, has_finite_integral_prod_iff h1f.strongly_measurable_mk], apply and_congr, { apply eventually_congr, filter_upwards [ae_ae_of_ae_prod h1f.ae_eq_mk.symm], assume x hx, exact has_finite_integral_congr hx }, { apply has_finite_integral_congr, filter_upwards [ae_ae_of_ae_prod h1f.ae_eq_mk.symm] with _ hx using integral_congr_ae (eventually_eq.fun_comp hx _), }, { apply_instance, }, end /-- A binary function is integrable if the function `y ↦ f (x, y)` is integrable for almost every `x` and the function `x ↦ ∫ ‖f (x, y)‖ dy` is integrable. -/ lemma integrable_prod_iff ⦃f : α × β → E⦄ (h1f : ae_strongly_measurable f (μ.prod ν)) : integrable f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, integrable (λ y, f (x, y)) ν) ∧ integrable (λ x, ∫ y, ‖f (x, y)‖ ∂ν) μ := by simp [integrable, h1f, has_finite_integral_prod_iff', h1f.norm.integral_prod_right', h1f.prod_mk_left] /-- A binary function is integrable if the function `x ↦ f (x, y)` is integrable for almost every `y` and the function `y ↦ ∫ ‖f (x, y)‖ dx` is integrable. -/ lemma integrable_prod_iff' [sigma_finite μ] ⦃f : α × β → E⦄ (h1f : ae_strongly_measurable f (μ.prod ν)) : integrable f (μ.prod ν) ↔ (∀ᵐ y ∂ ν, integrable (λ x, f (x, y)) μ) ∧ integrable (λ y, ∫ x, ‖f (x, y)‖ ∂μ) ν := by { convert integrable_prod_iff (h1f.prod_swap) using 1, rw [integrable_swap_iff] } lemma integrable.prod_left_ae [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : ∀ᵐ y ∂ ν, integrable (λ x, f (x, y)) μ := ((integrable_prod_iff' hf.ae_strongly_measurable).mp hf).1 lemma integrable.prod_right_ae [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : ∀ᵐ x ∂ μ, integrable (λ y, f (x, y)) ν := hf.swap.prod_left_ae lemma integrable.integral_norm_prod_left ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ x, ∫ y, ‖f (x, y)‖ ∂ν) μ := ((integrable_prod_iff hf.ae_strongly_measurable).mp hf).2 lemma integrable.integral_norm_prod_right [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ y, ∫ x, ‖f (x, y)‖ ∂μ) ν := hf.swap.integral_norm_prod_left lemma integrable_prod_mul {L : Type*} [is_R_or_C L] {f : α → L} {g : β → L} (hf : integrable f μ) (hg : integrable g ν) : integrable (λ (z : α × β), f z.1 * g z.2) (μ.prod ν) := begin refine (integrable_prod_iff _).2 ⟨_, _⟩, { exact hf.1.fst.mul hg.1.snd }, { exact eventually_of_forall (λ x, hg.const_mul (f x)) }, { simpa only [norm_mul, integral_mul_left] using hf.norm.mul_const _ } end end variables [normed_space ℝ E] [complete_space E] lemma integrable.integral_prod_left ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ x, ∫ y, f (x, y) ∂ν) μ := integrable.mono hf.integral_norm_prod_left hf.ae_strongly_measurable.integral_prod_right' $ eventually_of_forall $ λ x, (norm_integral_le_integral_norm _).trans_eq $ (norm_of_nonneg $ integral_nonneg_of_ae $ eventually_of_forall $ λ y, (norm_nonneg (f (x, y)) : _)).symm lemma integrable.integral_prod_right [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ y, ∫ x, f (x, y) ∂μ) ν := hf.swap.integral_prod_left /-! ### The Bochner integral on a product -/ variables [sigma_finite μ] lemma integral_prod_swap (f : α × β → E) (hf : ae_strongly_measurable f (μ.prod ν)) : ∫ z, f z.swap ∂(ν.prod μ) = ∫ z, f z ∂(μ.prod ν) := begin rw ← prod_swap at hf, rw [← integral_map measurable_swap.ae_measurable hf, prod_swap] end variables {E' : Type*} [normed_add_comm_group E'] [complete_space E'] [normed_space ℝ E'] /-! Some rules about the sum/difference of double integrals. They follow from `integral_add`, but we separate them out as separate lemmas, because they involve quite some steps. -/ /-- Integrals commute with addition inside another integral. `F` can be any function. -/ lemma integral_fn_integral_add ⦃f g : α × β → E⦄ (F : E → E') (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, F (∫ y, f (x, y) + g (x, y) ∂ν) ∂μ = ∫ x, F (∫ y, f (x, y) ∂ν + ∫ y, g (x, y) ∂ν) ∂μ := begin refine integral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae] with _ h2f h2g, simp [integral_add h2f h2g], end /-- Integrals commute with subtraction inside another integral. `F` can be any measurable function. -/ lemma integral_fn_integral_sub ⦃f g : α × β → E⦄ (F : E → E') (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, F (∫ y, f (x, y) - g (x, y) ∂ν) ∂μ = ∫ x, F (∫ y, f (x, y) ∂ν - ∫ y, g (x, y) ∂ν) ∂μ := begin refine integral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae] with _ h2f h2g, simp [integral_sub h2f h2g], end /-- Integrals commute with subtraction inside a lower Lebesgue integral. `F` can be any function. -/ lemma lintegral_fn_integral_sub ⦃f g : α × β → E⦄ (F : E → ℝ≥0∞) (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫⁻ x, F (∫ y, f (x, y) - g (x, y) ∂ν) ∂μ = ∫⁻ x, F (∫ y, f (x, y) ∂ν - ∫ y, g (x, y) ∂ν) ∂μ := begin refine lintegral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae] with _ h2f h2g, simp [integral_sub h2f h2g], end /-- Double integrals commute with addition. -/ lemma integral_integral_add ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, f (x, y) + g (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ + ∫ x, ∫ y, g (x, y) ∂ν ∂μ := (integral_fn_integral_add id hf hg).trans $ integral_add hf.integral_prod_left hg.integral_prod_left /-- Double integrals commute with addition. This is the version with `(f + g) (x, y)` (instead of `f (x, y) + g (x, y)`) in the LHS. -/ lemma integral_integral_add' ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, (f + g) (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ + ∫ x, ∫ y, g (x, y) ∂ν ∂μ := integral_integral_add hf hg /-- Double integrals commute with subtraction. -/ lemma integral_integral_sub ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, f (x, y) - g (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ - ∫ x, ∫ y, g (x, y) ∂ν ∂μ := (integral_fn_integral_sub id hf hg).trans $ integral_sub hf.integral_prod_left hg.integral_prod_left /-- Double integrals commute with subtraction. This is the version with `(f - g) (x, y)` (instead of `f (x, y) - g (x, y)`) in the LHS. -/ lemma integral_integral_sub' ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, (f - g) (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ - ∫ x, ∫ y, g (x, y) ∂ν ∂μ := integral_integral_sub hf hg /-- The map that sends an L¹-function `f : α × β → E` to `∫∫f` is continuous. -/ lemma continuous_integral_integral : continuous (λ (f : α × β →₁[μ.prod ν] E), ∫ x, ∫ y, f (x, y) ∂ν ∂μ) := begin rw [continuous_iff_continuous_at], intro g, refine tendsto_integral_of_L1 _ (L1.integrable_coe_fn g).integral_prod_left (eventually_of_forall $ λ h, (L1.integrable_coe_fn h).integral_prod_left) _, simp_rw [← lintegral_fn_integral_sub (λ x, (‖x‖₊ : ℝ≥0∞)) (L1.integrable_coe_fn _) (L1.integrable_coe_fn g)], refine tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds _ (λ i, zero_le _) _, { exact λ i, ∫⁻ x, ∫⁻ y, ‖i (x, y) - g (x, y)‖₊ ∂ν ∂μ }, swap, { exact λ i, lintegral_mono (λ x, ennnorm_integral_le_lintegral_ennnorm _) }, show tendsto (λ (i : α × β →₁[μ.prod ν] E), ∫⁻ x, ∫⁻ (y : β), ‖i (x, y) - g (x, y)‖₊ ∂ν ∂μ) (𝓝 g) (𝓝 0), have : ∀ (i : α × β →₁[μ.prod ν] E), measurable (λ z, (‖i z - g z‖₊ : ℝ≥0∞)) := λ i, ((Lp.strongly_measurable i).sub (Lp.strongly_measurable g)).ennnorm, simp_rw [← lintegral_prod_of_measurable _ (this _), ← L1.of_real_norm_sub_eq_lintegral, ← of_real_zero], refine (continuous_of_real.tendsto 0).comp _, rw [← tendsto_iff_norm_tendsto_zero], exact tendsto_id end /-- **Fubini's Theorem**: For integrable functions on `α × β`, the Bochner integral of `f` is equal to the iterated Bochner integral. `integrable_prod_iff` can be useful to show that the function in question in integrable. `measure_theory.integrable.integral_prod_right` is useful to show that the inner integral of the right-hand side is integrable. -/ lemma integral_prod : ∀ (f : α × β → E) (hf : integrable f (μ.prod ν)), ∫ z, f z ∂(μ.prod ν) = ∫ x, ∫ y, f (x, y) ∂ν ∂μ := begin apply integrable.induction, { intros c s hs h2s, simp_rw [integral_indicator hs, ← indicator_comp_right, function.comp, integral_indicator (measurable_prod_mk_left hs), set_integral_const, integral_smul_const, integral_to_real (measurable_measure_prod_mk_left hs).ae_measurable (ae_measure_lt_top hs h2s.ne), prod_apply hs] }, { intros f g hfg i_f i_g hf hg, simp_rw [integral_add' i_f i_g, integral_integral_add' i_f i_g, hf, hg] }, { exact is_closed_eq continuous_integral continuous_integral_integral }, { intros f g hfg i_f hf, convert hf using 1, { exact integral_congr_ae hfg.symm }, { refine integral_congr_ae _, refine (ae_ae_of_ae_prod hfg).mp _, apply eventually_of_forall, intros x hfgx, exact integral_congr_ae (ae_eq_symm hfgx) } } end /-- Symmetric version of **Fubini's Theorem**: For integrable functions on `α × β`, the Bochner integral of `f` is equal to the iterated Bochner integral. This version has the integrals on the right-hand side in the other order. -/ lemma integral_prod_symm (f : α × β → E) (hf : integrable f (μ.prod ν)) : ∫ z, f z ∂(μ.prod ν) = ∫ y, ∫ x, f (x, y) ∂μ ∂ν := by { simp_rw [← integral_prod_swap f hf.ae_strongly_measurable], exact integral_prod _ hf.swap } /-- Reversed version of **Fubini's Theorem**. -/ lemma integral_integral {f : α → β → E} (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ z, f z.1 z.2 ∂(μ.prod ν) := (integral_prod _ hf).symm /-- Reversed version of **Fubini's Theorem** (symmetric version). -/ lemma integral_integral_symm {f : α → β → E} (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ z, f z.2 z.1 ∂(ν.prod μ) := (integral_prod_symm _ hf.swap).symm /-- Change the order of Bochner integration. -/ lemma integral_integral_swap ⦃f : α → β → E⦄ (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ y, ∫ x, f x y ∂μ ∂ν := (integral_integral hf).trans (integral_prod_symm _ hf) /-- **Fubini's Theorem** for set integrals. -/ lemma set_integral_prod (f : α × β → E) {s : set α} {t : set β} (hf : integrable_on f (s ×ˢ t) (μ.prod ν)) : ∫ z in s ×ˢ t, f z ∂(μ.prod ν) = ∫ x in s, ∫ y in t, f (x, y) ∂ν ∂μ := begin simp only [← measure.prod_restrict s t, integrable_on] at hf ⊢, exact integral_prod f hf end lemma integral_prod_mul {L : Type*} [is_R_or_C L] (f : α → L) (g : β → L) : ∫ z, f z.1 * g z.2 ∂(μ.prod ν) = (∫ x, f x ∂μ) * (∫ y, g y ∂ν) := begin by_cases h : integrable (λ (z : α × β), f z.1 * g z.2) (μ.prod ν), { rw integral_prod _ h, simp_rw [integral_mul_left, integral_mul_right] }, have H : ¬(integrable f μ) ∨ ¬(integrable g ν), { contrapose! h, exact integrable_prod_mul h.1 h.2 }, cases H; simp [integral_undef h, integral_undef H], end lemma set_integral_prod_mul {L : Type*} [is_R_or_C L] (f : α → L) (g : β → L) (s : set α) (t : set β) : ∫ z in s ×ˢ t, f z.1 * g z.2 ∂(μ.prod ν) = (∫ x in s, f x ∂μ) * (∫ y in t, g y ∂ν) := by simp only [← measure.prod_restrict s t, integrable_on, integral_prod_mul] end measure_theory
6ad17e9fe2871c8928454d86d8f1d53c801944d1
86ee6b815fda4c9a5aa42e17b61336a913552e60
/src/groupoids/paths.lean
bf6dec9c92486b906a446ee006e2826ae98fba71
[ "Apache-2.0" ]
permissive
fgdorais/lean-groupoids
6487f6d84427a757708fd4e795038ab159cc4f2c
54d1582f8d6ae642fc818e7672ac1c49fe7733e5
refs/heads/master
1,587,778,056,386
1,551,055,383,000
1,551,055,383,000
172,408,995
0
0
null
null
null
null
UTF-8
Lean
false
false
2,020
lean
/- Copyright © 2019 François G. Dorais. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. -/ import .basic structure {u v} path {α : Type u} {γ : α → α → Type v} (Γ : groupoid γ) : Type (max u v) := (cod dom : α) (map : γ cod dom) attribute [pp_using_anonymous_constructor] path namespace path variables {α : Type*} {γ : α → α → Type*} (Γ : groupoid γ) @[reducible, pattern] definition make {x y : α} (g : γ x y) : path Γ := {cod := x, dom := y, map := g} definition id (x : α) : path Γ := path.make Γ (Γ.id x) definition inv (p : path Γ) : path Γ := path.make Γ (Γ.inv p.map) definition comp : Π (p q : path Γ), p.dom = q.cod → path Γ | (path.make _ g) (path.make _ h) rfl := path.make Γ (Γ.comp g h) definition pcomp [decidable_eq α] (p q : path Γ) : option (path Γ) := if H : p.dom = q.cod then some (path.comp Γ p q H) else none end path definition path_state {α : Type*} {γ : α → α → Type*} (Γ : groupoid γ) := list (path Γ) namespace path_state variables {α : Type*} {γ : α → α → Type*} {Γ : groupoid γ} [da : decidable_eq α] include da definition rep (x : α) : path_state Γ → psigma (λ y, γ y x) | [] := ⟨x, Γ.id x⟩ | (p::ps) := if H : p.dom = x then ⟨p.cod, eq.rec_on H p.map⟩ else rep ps definition remap (r : path Γ) : path_state Γ → path_state Γ | [] := [] | (p::ps) := if H : r.dom = p.cod then path.comp Γ r p H :: ps else p :: ps definition intro_ (r : path Γ) (ps : path_state Γ) : path_state Γ := let ⟨x, g⟩ := ps.rep r.dom in r :: ps.remap (path.make Γ (Γ.comp r.map (Γ.inv g))) definition intro (r : path Γ) (ps : path_state Γ) : path_state Γ := let ⟨z, g⟩ := ps.rep r.cod in ps.intro_ (path.make Γ (Γ.comp g r.map)) definition find (x y : α) (ps : path_state Γ) : option (γ x y) := let ⟨x, g⟩ := ps.rep x in let ⟨y, h⟩ := ps.rep y in if H : x = y then some (Γ.hcomp H (Γ.inv g) h) else none end path_state
f12248a234e9134e90d58b6b2d85ec5dfb63695e
df561f413cfe0a88b1056655515399c546ff32a5
/5-advanced-proposition-world/l10.lean
ff2bd14d351895699a9946b318c2aeeb3f6d2820
[]
no_license
nicholaspun/natural-number-game-solutions
31d5158415c6f582694680044c5c6469032c2a06
1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0
refs/heads/main
1,675,123,625,012
1,607,633,548,000
1,607,633,548,000
318,933,860
3
1
null
null
null
null
UTF-8
Lean
false
false
152
lean
lemma contrapositive2 (P Q : Prop) : (¬ Q → ¬ P) → (P → Q) := begin by_cases p : P; by_cases q : Q, intros h p2, exact q, repeat { tauto! }, end
f807527f1fe7251e9c6abc7fc5a1022c106b812e
618003631150032a5676f229d13a079ac875ff77
/src/data/rat/basic.lean
d74d3a154bd9edaf94f329f934d244d6790d5538
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
24,729
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.int.sqrt import data.equiv.encodable import algebra.group import algebra.euclidean_domain import algebra.ordered_field /-! # Basics for the Rational Numbers ## Summary We define a rational number `q` as a structure `{ num, denom, pos, cop }`, where - `num` is the numerator of `q`, - `denom` is the denominator of `q`, - `pos` is a proof that `denom > 0`, and - `cop` is a proof `num` and `denom` are coprime. We then define the expected (discrete) field structure on `ℚ` and prove basic lemmas about it. Moreoever, we provide the expected casts from `ℕ` and `ℤ` into `ℚ`, i.e. `(↑n : ℚ) = n / 1`. ## Main Definitions - `rat` is the structure encoding `ℚ`. - `rat.mk n d` constructs a rational number `q = n / d` from `n d : ℤ`. ## Notations - `/.` is infix notation for `rat.mk`. ## Tags rat, rationals, field, ℚ, numerator, denominator, num, denom -/ /-- `rat`, or `ℚ`, is the type of rational numbers. It is defined as the set of pairs ⟨n, d⟩ of integers such that `d` is positive and `n` and `d` are coprime. This representation is preferred to the quotient because without periodic reduction, the numerator and denominator can grow exponentially (for example, adding 1/2 to itself repeatedly). -/ structure rat := mk' :: (num : ℤ) (denom : ℕ) (pos : 0 < denom) (cop : num.nat_abs.coprime denom) notation `ℚ` := rat namespace rat protected def repr : ℚ → string | ⟨n, d, _, _⟩ := if d = 1 then _root_.repr n else _root_.repr n ++ "/" ++ _root_.repr d instance : has_repr ℚ := ⟨rat.repr⟩ instance : has_to_string ℚ := ⟨rat.repr⟩ meta instance : has_to_format ℚ := ⟨coe ∘ rat.repr⟩ instance : encodable ℚ := encodable.of_equiv (Σ n : ℤ, {d : ℕ // 0 < d ∧ n.nat_abs.coprime d}) ⟨λ ⟨a, b, c, d⟩, ⟨a, b, c, d⟩, λ⟨a, b, c, d⟩, ⟨a, b, c, d⟩, λ ⟨a, b, c, d⟩, rfl, λ⟨a, b, c, d⟩, rfl⟩ /-- Embed an integer as a rational number -/ def of_int (n : ℤ) : ℚ := ⟨n, 1, nat.one_pos, nat.coprime_one_right _⟩ instance : has_zero ℚ := ⟨of_int 0⟩ instance : has_one ℚ := ⟨of_int 1⟩ instance : inhabited ℚ := ⟨0⟩ /-- Form the quotient `n / d` where `n:ℤ` and `d:ℕ+` (not necessarily coprime) -/ def mk_pnat (n : ℤ) : ℕ+ → ℚ | ⟨d, dpos⟩ := let n' := n.nat_abs, g := n'.gcd d in ⟨n / g, d / g, begin apply (nat.le_div_iff_mul_le _ _ (nat.gcd_pos_of_pos_right _ dpos)).2, simp, exact nat.le_of_dvd dpos (nat.gcd_dvd_right _ _) end, begin have : int.nat_abs (n / ↑g) = n' / g, { cases int.nat_abs_eq n with e e; rw e, { refl }, rw [int.neg_div_of_dvd, int.nat_abs_neg], { refl }, exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) }, rw this, exact nat.coprime_div_gcd_div_gcd (nat.gcd_pos_of_pos_right _ dpos) end⟩ /-- Form the quotient `n / d` where `n:ℤ` and `d:ℕ`. In the case `d = 0`, we define `n / 0 = 0` by convention. -/ def mk_nat (n : ℤ) (d : ℕ) : ℚ := if d0 : d = 0 then 0 else mk_pnat n ⟨d, nat.pos_of_ne_zero d0⟩ /-- Form the quotient `n / d` where `n d : ℤ`. -/ def mk : ℤ → ℤ → ℚ | n (d : ℕ) := mk_nat n d | n -[1+ d] := mk_pnat (-n) d.succ_pnat localized "infix ` /. `:70 := rat.mk" in rat theorem mk_pnat_eq (n d h) : mk_pnat n ⟨d, h⟩ = n /. d := by change n /. d with dite _ _ _; simp [ne_of_gt h] theorem mk_nat_eq (n d) : mk_nat n d = n /. d := rfl @[simp] theorem mk_zero (n) : n /. 0 = 0 := rfl @[simp] theorem zero_mk_pnat (n) : mk_pnat 0 n = 0 := by cases n; simp [mk_pnat]; change int.nat_abs 0 with 0; simp *; refl @[simp] theorem zero_mk_nat (n) : mk_nat 0 n = 0 := by by_cases n = 0; simp [*, mk_nat] @[simp] theorem zero_mk (n) : 0 /. n = 0 := by cases n; simp [mk] private lemma gcd_abs_dvd_left {a b} : (nat.gcd (int.nat_abs a) b : ℤ) ∣ a := int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ nat.gcd_dvd_left (int.nat_abs a) b @[simp] theorem mk_eq_zero {a b : ℤ} (b0 : b ≠ 0) : a /. b = 0 ↔ a = 0 := begin constructor; intro h; [skip, {subst a, simp}], have : ∀ {a b}, mk_pnat a b = 0 → a = 0, { intros a b e, cases b with b h, injection e with e, apply int.eq_mul_of_div_eq_right gcd_abs_dvd_left e }, cases b with b; simp [mk, mk_nat] at h, { simp [mt (congr_arg int.of_nat) b0] at h, exact this h }, { apply neg_inj, simp [this h] } end theorem mk_eq : ∀ {a b c d : ℤ} (hb : b ≠ 0) (hd : d ≠ 0), a /. b = c /. d ↔ a * d = c * b := suffices ∀ a b c d hb hd, mk_pnat a ⟨b, hb⟩ = mk_pnat c ⟨d, hd⟩ ↔ a * d = c * b, begin intros, cases b with b b; simp [mk, mk_nat, nat.succ_pnat], simp [mt (congr_arg int.of_nat) hb], all_goals { cases d with d d; simp [mk, mk_nat, nat.succ_pnat], simp [mt (congr_arg int.of_nat) hd], all_goals { rw this, try {refl} } }, { change a * ↑(d.succ) = -c * ↑b ↔ a * -(d.succ) = c * b, constructor; intro h; apply neg_inj; simpa [left_distrib, neg_add_eq_iff_eq_add, eq_neg_iff_add_eq_zero, neg_eq_iff_add_eq_zero] using h }, { change -a * ↑d = c * b.succ ↔ a * d = c * -b.succ, constructor; intro h; apply neg_inj; simpa [left_distrib, eq_comm] using h }, { change -a * d.succ = -c * b.succ ↔ a * -d.succ = c * -b.succ, simp [left_distrib, sub_eq_add_neg], cc } end, begin intros, simp [mk_pnat], constructor; intro h, { cases h with ha hb, have ha, { have dv := @gcd_abs_dvd_left, have := int.eq_mul_of_div_eq_right dv ha, rw ← int.mul_div_assoc _ dv at this, exact int.eq_mul_of_div_eq_left (dvd_mul_of_dvd_right dv _) this.symm }, have hb, { have dv := λ {a b}, nat.gcd_dvd_right (int.nat_abs a) b, have := nat.eq_mul_of_div_eq_right dv hb, rw ← nat.mul_div_assoc _ dv at this, exact nat.eq_mul_of_div_eq_left (dvd_mul_of_dvd_right dv _) this.symm }, have m0 : (a.nat_abs.gcd b * c.nat_abs.gcd d : ℤ) ≠ 0, { refine int.coe_nat_ne_zero.2 (ne_of_gt _), apply mul_pos; apply nat.gcd_pos_of_pos_right; assumption }, apply eq_of_mul_eq_mul_right m0, simpa [mul_comm, mul_left_comm] using congr (congr_arg (*) ha.symm) (congr_arg coe hb) }, { suffices : ∀ a c, a * d = c * b → a / a.gcd b = c / c.gcd d ∧ b / a.gcd b = d / c.gcd d, { cases this a.nat_abs c.nat_abs (by simpa [int.nat_abs_mul] using congr_arg int.nat_abs h) with h₁ h₂, have hs := congr_arg int.sign h, simp [int.sign_eq_one_of_pos (int.coe_nat_lt.2 hb), int.sign_eq_one_of_pos (int.coe_nat_lt.2 hd)] at hs, conv in a { rw ← int.sign_mul_nat_abs a }, conv in c { rw ← int.sign_mul_nat_abs c }, rw [int.mul_div_assoc, int.mul_div_assoc], exact ⟨congr (congr_arg (*) hs) (congr_arg coe h₁), h₂⟩, all_goals { exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) } }, intros a c h, suffices bd : b / a.gcd b = d / c.gcd d, { refine ⟨_, bd⟩, apply nat.eq_of_mul_eq_mul_left hb, rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), bd, ← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), h, mul_comm, nat.mul_div_assoc _ (nat.gcd_dvd_left _ _)] }, suffices : ∀ {a c : ℕ} (b>0) (d>0), a * d = c * b → b / a.gcd b ≤ d / c.gcd d, { exact le_antisymm (this _ hb _ hd h) (this _ hd _ hb h.symm) }, intros a c b hb d hd h, have gb0 := nat.gcd_pos_of_pos_right a hb, have gd0 := nat.gcd_pos_of_pos_right c hd, apply nat.le_of_dvd, apply (nat.le_div_iff_mul_le _ _ gd0).2, simp, apply nat.le_of_dvd hd (nat.gcd_dvd_right _ _), apply (nat.coprime_div_gcd_div_gcd gb0).symm.dvd_of_dvd_mul_left, refine ⟨c / c.gcd d, _⟩, rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), ← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _)], apply congr_arg (/ c.gcd d), rw [mul_comm, ← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), mul_comm, h, nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), mul_comm] } end @[simp] theorem div_mk_div_cancel_left {a b c : ℤ} (c0 : c ≠ 0) : (a * c) /. (b * c) = a /. b := begin by_cases b0 : b = 0, { subst b0, simp }, apply (mk_eq (mul_ne_zero b0 c0) b0).2, simp [mul_comm, mul_assoc] end @[simp] theorem num_denom : ∀ {a : ℚ}, a.num /. a.denom = a | ⟨n, d, h, (c:_=1)⟩ := show mk_nat n d = _, by simp [mk_nat, ne_of_gt h, mk_pnat, c] theorem num_denom' {n d h c} : (⟨n, d, h, c⟩ : ℚ) = n /. d := num_denom.symm theorem of_int_eq_mk (z : ℤ) : of_int z = z /. 1 := num_denom' @[elab_as_eliminator] def {u} num_denom_cases_on {C : ℚ → Sort u} : ∀ (a : ℚ) (H : ∀ n d, 0 < d → (int.nat_abs n).coprime d → C (n /. d)), C a | ⟨n, d, h, c⟩ H := by rw num_denom'; exact H n d h c @[elab_as_eliminator] def {u} num_denom_cases_on' {C : ℚ → Sort u} (a : ℚ) (H : ∀ (n:ℤ) (d:ℕ), d ≠ 0 → C (n /. d)) : C a := num_denom_cases_on a $ λ n d h c, H n d $ ne_of_gt h theorem num_dvd (a) {b : ℤ} (b0 : b ≠ 0) : (a /. b).num ∣ a := begin cases e : a /. b with n d h c, rw [rat.num_denom', rat.mk_eq b0 (ne_of_gt (int.coe_nat_pos.2 h))] at e, refine (int.nat_abs_dvd.1 $ int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ c.dvd_of_dvd_mul_right _), have := congr_arg int.nat_abs e, simp [int.nat_abs_mul, int.nat_abs_of_nat] at this, simp [this] end theorem denom_dvd (a b : ℤ) : ((a /. b).denom : ℤ) ∣ b := begin by_cases b0 : b = 0, {simp [b0]}, cases e : a /. b with n d h c, rw [num_denom', mk_eq b0 (ne_of_gt (int.coe_nat_pos.2 h))] at e, refine (int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ c.symm.dvd_of_dvd_mul_left _), rw [← int.nat_abs_mul, ← int.coe_nat_dvd, int.dvd_nat_abs, ← e], simp end protected def add : ℚ → ℚ → ℚ | ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := mk_pnat (n₁ * d₂ + n₂ * d₁) ⟨d₁ * d₂, mul_pos h₁ h₂⟩ instance : has_add ℚ := ⟨rat.add⟩ theorem lift_binop_eq (f : ℚ → ℚ → ℚ) (f₁ : ℤ → ℤ → ℤ → ℤ → ℤ) (f₂ : ℤ → ℤ → ℤ → ℤ → ℤ) (fv : ∀ {n₁ d₁ h₁ c₁ n₂ d₂ h₂ c₂}, f ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ = f₁ n₁ d₁ n₂ d₂ /. f₂ n₁ d₁ n₂ d₂) (f0 : ∀ {n₁ d₁ n₂ d₂} (d₁0 : d₁ ≠ 0) (d₂0 : d₂ ≠ 0), f₂ n₁ d₁ n₂ d₂ ≠ 0) (a b c d : ℤ) (b0 : b ≠ 0) (d0 : d ≠ 0) (H : ∀ {n₁ d₁ n₂ d₂} (h₁ : a * d₁ = n₁ * b) (h₂ : c * d₂ = n₂ * d), f₁ n₁ d₁ n₂ d₂ * f₂ a b c d = f₁ a b c d * f₂ n₁ d₁ n₂ d₂) : f (a /. b) (c /. d) = f₁ a b c d /. f₂ a b c d := begin generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, generalize hc : c /. d = x, cases x with n₂ d₂ h₂ c₂, rw num_denom' at hc, rw fv, have d₁0 := ne_of_gt (int.coe_nat_lt.2 h₁), have d₂0 := ne_of_gt (int.coe_nat_lt.2 h₂), exact (mk_eq (f0 d₁0 d₂0) (f0 b0 d0)).2 (H ((mk_eq b0 d₁0).1 ha) ((mk_eq d0 d₂0).1 hc)) end @[simp] theorem add_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : a /. b + c /. d = (a * d + c * b) /. (b * d) := begin apply lift_binop_eq rat.add; intros; try {assumption}, { apply mk_pnat_eq }, { apply mul_ne_zero d₁0 d₂0 }, calc (n₁ * d₂ + n₂ * d₁) * (b * d) = (n₁ * b) * d₂ * d + (n₂ * d) * (d₁ * b) : by simp [mul_add, mul_comm, mul_left_comm] ... = (a * d₁) * d₂ * d + (c * d₂) * (d₁ * b) : by rw [h₁, h₂] ... = (a * d + c * b) * (d₁ * d₂) : by simp [mul_add, mul_comm, mul_left_comm] end protected def neg : ℚ → ℚ | ⟨n, d, h, c⟩ := ⟨-n, d, h, by simp [c]⟩ instance : has_neg ℚ := ⟨rat.neg⟩ @[simp] theorem neg_def {a b : ℤ} : -(a /. b) = -a /. b := begin by_cases b0 : b = 0, { subst b0, simp, refl }, generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, show rat.mk' _ _ _ _ = _, rw num_denom', have d0 := ne_of_gt (int.coe_nat_lt.2 h₁), apply (mk_eq d0 b0).2, have h₁ := (mk_eq b0 d0).1 ha, simp only [neg_mul_eq_neg_mul_symm, congr_arg has_neg.neg h₁] end protected def mul : ℚ → ℚ → ℚ | ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := mk_pnat (n₁ * n₂) ⟨d₁ * d₂, mul_pos h₁ h₂⟩ instance : has_mul ℚ := ⟨rat.mul⟩ @[simp] theorem mul_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : (a /. b) * (c /. d) = (a * c) /. (b * d) := begin apply lift_binop_eq rat.mul; intros; try {assumption}, { apply mk_pnat_eq }, { apply mul_ne_zero d₁0 d₂0 }, cc end protected def inv : ℚ → ℚ | ⟨(n+1:ℕ), d, h, c⟩ := ⟨d, n+1, n.succ_pos, c.symm⟩ | ⟨0, d, h, c⟩ := 0 | ⟨-[1+ n], d, h, c⟩ := ⟨-d, n+1, n.succ_pos, nat.coprime.symm $ by simp; exact c⟩ instance : has_inv ℚ := ⟨rat.inv⟩ @[simp] theorem inv_def {a b : ℤ} : (a /. b)⁻¹ = b /. a := begin by_cases a0 : a = 0, { subst a0, simp, refl }, by_cases b0 : b = 0, { subst b0, simp, refl }, generalize ha : a /. b = x, cases x with n d h c, rw num_denom' at ha, refine eq.trans (_ : rat.inv ⟨n, d, h, c⟩ = d /. n) _, { cases n with n; [cases n with n, skip], { refl }, { change int.of_nat n.succ with (n+1:ℕ), unfold rat.inv, rw num_denom' }, { unfold rat.inv, rw num_denom', refl } }, have n0 : n ≠ 0, { refine mt (λ (n0 : n = 0), _) a0, subst n0, simp at ha, exact (mk_eq_zero b0).1 ha }, have d0 := ne_of_gt (int.coe_nat_lt.2 h), have ha := (mk_eq b0 d0).1 ha, apply (mk_eq n0 a0).2, cc end variables (a b c : ℚ) protected theorem add_zero : a + 0 = a := num_denom_cases_on' a $ λ n d h, by rw [← zero_mk d]; simp [h, -zero_mk] protected theorem zero_add : 0 + a = a := num_denom_cases_on' a $ λ n d h, by rw [← zero_mk d]; simp [h, -zero_mk] protected theorem add_comm : a + b = b + a := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, by simp [h₁, h₂]; cc protected theorem add_assoc : a + b + c = a + (b + c) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero, mul_add, mul_comm, mul_left_comm, add_left_comm, add_assoc] protected theorem add_left_neg : -a + a = 0 := num_denom_cases_on' a $ λ n d h, by simp [h] protected theorem mul_one : a * 1 = a := num_denom_cases_on' a $ λ n d h, by change (1:ℚ) with 1 /. 1; simp [h] protected theorem one_mul : 1 * a = a := num_denom_cases_on' a $ λ n d h, by change (1:ℚ) with 1 /. 1; simp [h] protected theorem mul_comm : a * b = b * a := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, by simp [h₁, h₂, mul_comm] protected theorem mul_assoc : a * b * c = a * (b * c) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero, mul_comm, mul_left_comm] protected theorem add_mul : (a + b) * c = a * c + b * c := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero]; refine (div_mk_div_cancel_left (int.coe_nat_ne_zero.2 h₃)).symm.trans _; simp [mul_add, mul_comm, mul_assoc, mul_left_comm] protected theorem mul_add : a * (b + c) = a * b + a * c := by rw [rat.mul_comm, rat.add_mul, rat.mul_comm, rat.mul_comm c a] protected theorem zero_ne_one : 0 ≠ (1:ℚ) := mt (λ (h : 0 = 1 /. 1), (mk_eq_zero one_ne_zero).1 h.symm) one_ne_zero protected theorem mul_inv_cancel : a ≠ 0 → a * a⁻¹ = 1 := num_denom_cases_on' a $ λ n d h a0, have n0 : n ≠ 0, from mt (by intro e; subst e; simp) a0, by simp [h, n0, mul_comm]; exact eq.trans (by simp) (@div_mk_div_cancel_left 1 1 _ n0) protected theorem inv_mul_cancel (h : a ≠ 0) : a⁻¹ * a = 1 := eq.trans (rat.mul_comm _ _) (rat.mul_inv_cancel _ h) instance : decidable_eq ℚ := by tactic.mk_dec_eq_instance instance : field ℚ := { zero := 0, add := rat.add, neg := rat.neg, one := 1, mul := rat.mul, inv := rat.inv, zero_add := rat.zero_add, add_zero := rat.add_zero, add_comm := rat.add_comm, add_assoc := rat.add_assoc, add_left_neg := rat.add_left_neg, mul_one := rat.mul_one, one_mul := rat.one_mul, mul_comm := rat.mul_comm, mul_assoc := rat.mul_assoc, left_distrib := rat.mul_add, right_distrib := rat.add_mul, zero_ne_one := rat.zero_ne_one, mul_inv_cancel := rat.mul_inv_cancel, inv_zero := rfl } /- Extra instances to short-circuit type class resolution -/ instance : division_ring ℚ := by apply_instance instance : integral_domain ℚ := by apply_instance -- TODO(Mario): this instance slows down data.real.basic --instance : domain ℚ := by apply_instance instance : nonzero ℚ := by apply_instance instance : comm_ring ℚ := by apply_instance --instance : ring ℚ := by apply_instance instance : comm_semiring ℚ := by apply_instance instance : semiring ℚ := by apply_instance instance : add_comm_group ℚ := by apply_instance instance : add_group ℚ := by apply_instance instance : add_comm_monoid ℚ := by apply_instance instance : add_monoid ℚ := by apply_instance instance : add_left_cancel_semigroup ℚ := by apply_instance instance : add_right_cancel_semigroup ℚ := by apply_instance instance : add_comm_semigroup ℚ := by apply_instance instance : add_semigroup ℚ := by apply_instance instance : comm_monoid ℚ := by apply_instance instance : monoid ℚ := by apply_instance instance : comm_semigroup ℚ := by apply_instance instance : semigroup ℚ := by apply_instance theorem sub_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : a /. b - c /. d = (a * d - c * b) /. (b * d) := by simp [b0, d0, sub_eq_add_neg] @[simp] lemma denom_neg_eq_denom : ∀ q : ℚ, (-q).denom = q.denom | ⟨_, d, _, _⟩ := rfl @[simp] lemma num_neg_eq_neg_num : ∀ q : ℚ, (-q).num = -(q.num) | ⟨n, _, _, _⟩ := rfl @[simp] lemma num_zero : rat.num 0 = 0 := rfl lemma zero_of_num_zero {q : ℚ} (hq : q.num = 0) : q = 0 := have q = q.num /. q.denom, from num_denom.symm, by simpa [hq] lemma zero_iff_num_zero {q : ℚ} : q = 0 ↔ q.num = 0 := ⟨λ _, by simp *, zero_of_num_zero⟩ lemma num_ne_zero_of_ne_zero {q : ℚ} (h : q ≠ 0) : q.num ≠ 0 := assume : q.num = 0, h $ zero_of_num_zero this @[simp] lemma num_one : (1 : ℚ).num = 1 := rfl @[simp] lemma denom_one : (1 : ℚ).denom = 1 := rfl lemma denom_ne_zero (q : ℚ) : q.denom ≠ 0 := ne_of_gt q.pos lemma eq_iff_mul_eq_mul {p q : ℚ} : p = q ↔ p.num * q.denom = q.num * p.denom := begin conv_lhs { rw [←(@num_denom p), ←(@num_denom q)] }, apply rat.mk_eq, { exact_mod_cast p.denom_ne_zero }, { exact_mod_cast q.denom_ne_zero } end lemma mk_num_ne_zero_of_ne_zero {q : ℚ} {n d : ℤ} (hq : q ≠ 0) (hqnd : q = n /. d) : n ≠ 0 := assume : n = 0, hq $ by simpa [this] using hqnd lemma mk_denom_ne_zero_of_ne_zero {q : ℚ} {n d : ℤ} (hq : q ≠ 0) (hqnd : q = n /. d) : d ≠ 0 := assume : d = 0, hq $ by simpa [this] using hqnd lemma mk_ne_zero_of_ne_zero {n d : ℤ} (h : n ≠ 0) (hd : d ≠ 0) : n /. d ≠ 0 := assume : n /. d = 0, h $ (mk_eq_zero hd).1 this lemma mul_num_denom (q r : ℚ) : q * r = (q.num * r.num) /. ↑(q.denom * r.denom) := have hq' : (↑q.denom : ℤ) ≠ 0, by have := denom_ne_zero q; simpa, have hr' : (↑r.denom : ℤ) ≠ 0, by have := denom_ne_zero r; simpa, suffices (q.num /. ↑q.denom) * (r.num /. ↑r.denom) = (q.num * r.num) /. ↑(q.denom * r.denom), by simpa using this, by simp [mul_def hq' hr', -num_denom] lemma div_num_denom (q r : ℚ) : q / r = (q.num * r.denom) /. (q.denom * r.num) := if hr : r.num = 0 then have hr' : r = 0, from zero_of_num_zero hr, by simp * else calc q / r = q * r⁻¹ : div_eq_mul_inv ... = (q.num /. q.denom) * (r.num /. r.denom)⁻¹ : by simp ... = (q.num /. q.denom) * (r.denom /. r.num) : by rw inv_def ... = (q.num * r.denom) /. (q.denom * r.num) : mul_def (by simpa using denom_ne_zero q) hr lemma num_denom_mk {q : ℚ} {n d : ℤ} (hn : n ≠ 0) (hd : d ≠ 0) (qdf : q = n /. d) : ∃ c : ℤ, n = c * q.num ∧ d = c * q.denom := have hq : q ≠ 0, from assume : q = 0, hn $ (rat.mk_eq_zero hd).1 (by cc), have q.num /. q.denom = n /. d, by rwa [num_denom], have q.num * d = n * ↑(q.denom), from (rat.mk_eq (by simp [rat.denom_ne_zero]) hd).1 this, begin existsi n / q.num, have hqdn : q.num ∣ n, begin rw qdf, apply rat.num_dvd, assumption end, split, { rw int.div_mul_cancel hqdn }, { apply int.eq_mul_div_of_mul_eq_mul_of_dvd_left, { apply rat.num_ne_zero_of_ne_zero hq }, repeat { assumption } } end theorem mk_pnat_num (n : ℤ) (d : ℕ+) : (mk_pnat n d).num = n / nat.gcd n.nat_abs d := by cases d; refl theorem mk_pnat_denom (n : ℤ) (d : ℕ+) : (mk_pnat n d).denom = d / nat.gcd n.nat_abs d := by cases d; refl theorem mul_num (q₁ q₂ : ℚ) : (q₁ * q₂).num = (q₁.num * q₂.num) / nat.gcd (q₁.num * q₂.num).nat_abs (q₁.denom * q₂.denom) := by cases q₁; cases q₂; refl theorem mul_denom (q₁ q₂ : ℚ) : (q₁ * q₂).denom = (q₁.denom * q₂.denom) / nat.gcd (q₁.num * q₂.num).nat_abs (q₁.denom * q₂.denom) := by cases q₁; cases q₂; refl theorem mul_self_num (q : ℚ) : (q * q).num = q.num * q.num := by rw [mul_num, int.nat_abs_mul, nat.coprime.gcd_eq_one, int.coe_nat_one, int.div_one]; exact (q.cop.mul_right q.cop).mul (q.cop.mul_right q.cop) theorem mul_self_denom (q : ℚ) : (q * q).denom = q.denom * q.denom := by rw [rat.mul_denom, int.nat_abs_mul, nat.coprime.gcd_eq_one, nat.div_one]; exact (q.cop.mul_right q.cop).mul (q.cop.mul_right q.cop) lemma add_num_denom (q r : ℚ) : q + r = ((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ) := have hqd : (q.denom : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 q.3, have hrd : (r.denom : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 r.3, by conv_lhs { rw [←@num_denom q, ←@num_denom r, rat.add_def hqd hrd] }; simp [mul_comm] section casts theorem coe_int_eq_mk : ∀ (z : ℤ), ↑z = z /. 1 | (n : ℕ) := show (n:ℚ) = n /. 1, by induction n with n IH n; simp [*, show (1:ℚ) = 1 /. 1, from rfl] | -[1+ n] := show (-(n + 1) : ℚ) = -[1+ n] /. 1, begin induction n with n IH, {refl}, show -(n + 1 + 1 : ℚ) = -[1+ n.succ] /. 1, rw [neg_add, IH], simpa [show -1 = (-1) /. 1, from rfl] end theorem mk_eq_div (n d : ℤ) : n /. d = ((n : ℚ) / d) := begin by_cases d0 : d = 0, {simp [d0, div_zero]}, simp [division_def, coe_int_eq_mk, mul_def one_ne_zero d0] end theorem coe_int_eq_of_int (z : ℤ) : ↑z = of_int z := (coe_int_eq_mk z).trans (of_int_eq_mk z).symm @[simp, norm_cast] theorem coe_int_num (n : ℤ) : (n : ℚ).num = n := by rw coe_int_eq_of_int; refl @[simp, norm_cast] theorem coe_int_denom (n : ℤ) : (n : ℚ).denom = 1 := by rw coe_int_eq_of_int; refl lemma coe_int_num_of_denom_eq_one {q : ℚ} (hq : q.denom = 1) : ↑(q.num) = q := by { conv_rhs { rw [←(@num_denom q), hq] }, rw [coe_int_eq_mk], refl } instance : can_lift ℚ ℤ := ⟨coe, λ q, q.denom = 1, λ q hq, ⟨q.num, coe_int_num_of_denom_eq_one hq⟩⟩ theorem coe_nat_eq_mk (n : ℕ) : ↑n = n /. 1 := by rw [← int.cast_coe_nat, coe_int_eq_mk] @[simp, norm_cast] theorem coe_nat_num (n : ℕ) : (n : ℚ).num = n := by rw [← int.cast_coe_nat, coe_int_num] @[simp, norm_cast] theorem coe_nat_denom (n : ℕ) : (n : ℚ).denom = 1 := by rw [← int.cast_coe_nat, coe_int_denom] end casts lemma inv_def' {q : ℚ} : q⁻¹ = (q.denom : ℚ) / q.num := by { conv_lhs { rw ←(@num_denom q) }, cases q, simp [div_num_denom] } @[simp] lemma mul_own_denom_eq_num {q : ℚ} : q * q.denom = q.num := begin suffices : mk (q.num) ↑(q.denom) * mk ↑(q.denom) 1 = mk (q.num) 1, by { conv { for q [1] { rw ←(@num_denom q) }}, rwa [coe_int_eq_mk, coe_nat_eq_mk] }, have : (q.denom : ℤ) ≠ 0, from ne_of_gt (by exact_mod_cast q.pos), rw [(rat.mul_def this one_ne_zero), (mul_comm (q.denom : ℤ) 1), (div_mk_div_cancel_left this)] end end rat
cb638a02100bd5b8780e79d1b8ede52f536ffb40
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/qualifiedNamesRec.lean
066a5c6706bdf57cde7e70842cbf8eeab67daeec
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,111
lean
mutual inductive Foo | somefoo : Foo | bar : Bar → Foo inductive Bar | somebar : Bar | foobar : Foo → Bar → Bar end mutual private def Foo.toString : Foo → String | Foo.somefoo => "foo" | Foo.bar b => Bar.toString b def _root_.Bar.toString : Bar → String | Bar.somebar => "bar" | Bar.foobar f b => Foo.toString f ++ Bar.toString b end namespace Ex2 mutual inductive Foo | somefoo : Foo | bar : Bar → Foo → Foo inductive Bar | somebar : Bar | foobar : Foo → Bar → Bar end mutual private def Foo.toString : Foo → String | Foo.somefoo => go 2 ++ toString.go 2 ++ Foo.toString.go 2 | Foo.bar b f => Foo.toString f ++ Bar.toString b where go (x : Nat) := s!"foo {x}" private def _root_.Ex2.Bar.toString : Bar → String | Bar.somebar => "bar" | Bar.foobar f b => Foo.toString f ++ Bar.toString b end end Ex2 def Nat.fact : Nat → Nat | 0 => 1 | n+1 => (n+1) * Nat.fact n example : Nat.fact 3 = 6 := rfl namespace Boo def fact : Nat → Nat | 0 => 2 | n+1 => (n+1) * Boo.fact n example : Boo.fact 3 = 12 := rfl end Boo
3cd77d41111946948025239eeb58c6b459311ca0
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/algebra/group.lean
9ab01b43089c731b23f42dc7ed66b1a3bcf21138
[ "Apache-2.0" ]
permissive
sebzim4500/mathlib
e0b5a63b1655f910dee30badf09bd7e191d3cf30
6997cafbd3a7325af5cb318561768c316ceb7757
refs/heads/master
1,585,549,958,618
1,538,221,723,000
1,538,221,723,000
150,869,076
0
0
Apache-2.0
1,538,229,323,000
1,538,229,323,000
null
UTF-8
Lean
false
false
25,839
lean
/- 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 Various multiplicative and additive structures. -/ import tactic.interactive data.option section pending_1857 /- Transport multiplicative to additive -/ section transport open tactic @[user_attribute] meta def to_additive_attr : user_attribute (name_map name) name := { name := `to_additive, descr := "Transport multiplicative to additive", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do val ← to_additive_attr.get_param n, pure $ dict.insert n val) mk_name_map, []⟩, parser := lean.parser.ident, after_set := some $ λ src _ _, do env ← get_env, dict ← to_additive_attr.get_cache, tgt ← to_additive_attr.get_param src, (get_decl tgt >> skip) <|> transport_with_dict dict src tgt } end transport /- map operations -/ attribute [to_additive has_add.add] has_mul.mul attribute [to_additive has_zero.zero] has_one.one attribute [to_additive has_neg.neg] has_inv.inv attribute [to_additive has_add] has_mul attribute [to_additive has_zero] has_one attribute [to_additive has_neg] has_inv /- map constructors -/ attribute [to_additive has_add.mk] has_mul.mk attribute [to_additive has_zero.mk] has_one.mk attribute [to_additive has_neg.mk] has_inv.mk /- map structures -/ attribute [to_additive add_semigroup] semigroup attribute [to_additive add_semigroup.mk] semigroup.mk attribute [to_additive add_semigroup.to_has_add] semigroup.to_has_mul attribute [to_additive add_semigroup.add_assoc] semigroup.mul_assoc attribute [to_additive add_semigroup.add] semigroup.mul attribute [to_additive add_comm_semigroup] comm_semigroup attribute [to_additive add_comm_semigroup.mk] comm_semigroup.mk attribute [to_additive add_comm_semigroup.to_add_semigroup] comm_semigroup.to_semigroup attribute [to_additive add_comm_semigroup.add_comm] comm_semigroup.mul_comm attribute [to_additive add_left_cancel_semigroup] left_cancel_semigroup attribute [to_additive add_left_cancel_semigroup.mk] left_cancel_semigroup.mk attribute [to_additive add_left_cancel_semigroup.to_add_semigroup] left_cancel_semigroup.to_semigroup attribute [to_additive add_left_cancel_semigroup.add_left_cancel] left_cancel_semigroup.mul_left_cancel attribute [to_additive add_right_cancel_semigroup] right_cancel_semigroup attribute [to_additive add_right_cancel_semigroup.mk] right_cancel_semigroup.mk attribute [to_additive add_right_cancel_semigroup.to_add_semigroup] right_cancel_semigroup.to_semigroup attribute [to_additive add_right_cancel_semigroup.add_right_cancel] right_cancel_semigroup.mul_right_cancel attribute [to_additive add_monoid] monoid attribute [to_additive add_monoid.mk] monoid.mk attribute [to_additive add_monoid.to_has_zero] monoid.to_has_one attribute [to_additive add_monoid.to_add_semigroup] monoid.to_semigroup attribute [to_additive add_monoid.add] monoid.mul attribute [to_additive add_monoid.add_assoc] monoid.mul_assoc attribute [to_additive add_monoid.zero] monoid.one attribute [to_additive add_monoid.zero_add] monoid.one_mul attribute [to_additive add_monoid.add_zero] monoid.mul_one attribute [to_additive add_comm_monoid] comm_monoid attribute [to_additive add_comm_monoid.mk] comm_monoid.mk attribute [to_additive add_comm_monoid.to_add_monoid] comm_monoid.to_monoid attribute [to_additive add_comm_monoid.to_add_comm_semigroup] comm_monoid.to_comm_semigroup attribute [to_additive add_group] group attribute [to_additive add_group.mk] group.mk attribute [to_additive add_group.to_has_neg] group.to_has_inv attribute [to_additive add_group.to_add_monoid] group.to_monoid attribute [to_additive add_group.add_left_neg] group.mul_left_inv attribute [to_additive add_group.add] group.mul attribute [to_additive add_group.add_assoc] group.mul_assoc attribute [to_additive add_group.zero] group.one attribute [to_additive add_group.zero_add] group.one_mul attribute [to_additive add_group.add_zero] group.mul_one attribute [to_additive add_group.neg] group.inv attribute [to_additive add_comm_group] comm_group attribute [to_additive add_comm_group.mk] comm_group.mk attribute [to_additive add_comm_group.to_add_group] comm_group.to_group attribute [to_additive add_comm_group.to_add_comm_monoid] comm_group.to_comm_monoid /- map theorems -/ attribute [to_additive add_assoc] mul_assoc attribute [to_additive add_semigroup_to_is_associative] semigroup_to_is_associative attribute [to_additive add_comm] mul_comm attribute [to_additive add_comm_semigroup_to_is_commutative] comm_semigroup_to_is_commutative attribute [to_additive add_left_comm] mul_left_comm attribute [to_additive add_right_comm] mul_right_comm attribute [to_additive add_left_cancel] mul_left_cancel attribute [to_additive add_right_cancel] mul_right_cancel attribute [to_additive add_left_cancel_iff] mul_left_cancel_iff attribute [to_additive add_right_cancel_iff] mul_right_cancel_iff attribute [to_additive zero_add] one_mul attribute [to_additive add_zero] mul_one attribute [to_additive add_left_neg] mul_left_inv attribute [to_additive neg_add_self] inv_mul_self attribute [to_additive neg_add_cancel_left] inv_mul_cancel_left attribute [to_additive neg_add_cancel_right] inv_mul_cancel_right attribute [to_additive neg_eq_of_add_eq_zero] inv_eq_of_mul_eq_one attribute [to_additive neg_zero] one_inv attribute [to_additive neg_neg] inv_inv attribute [to_additive add_right_neg] mul_right_inv attribute [to_additive add_neg_self] mul_inv_self attribute [to_additive neg_inj] inv_inj attribute [to_additive add_group.add_left_cancel] group.mul_left_cancel attribute [to_additive add_group.add_right_cancel] group.mul_right_cancel attribute [to_additive add_group.to_left_cancel_add_semigroup] group.to_left_cancel_semigroup attribute [to_additive add_group.to_right_cancel_add_semigroup] group.to_right_cancel_semigroup attribute [to_additive add_neg_cancel_left] mul_inv_cancel_left attribute [to_additive add_neg_cancel_right] mul_inv_cancel_right attribute [to_additive neg_add_rev] mul_inv_rev attribute [to_additive eq_neg_of_eq_neg] eq_inv_of_eq_inv attribute [to_additive eq_neg_of_add_eq_zero] eq_inv_of_mul_eq_one attribute [to_additive eq_add_neg_of_add_eq] eq_mul_inv_of_mul_eq attribute [to_additive eq_neg_add_of_add_eq] eq_inv_mul_of_mul_eq attribute [to_additive neg_add_eq_of_eq_add] inv_mul_eq_of_eq_mul attribute [to_additive add_neg_eq_of_eq_add] mul_inv_eq_of_eq_mul attribute [to_additive eq_add_of_add_neg_eq] eq_mul_of_mul_inv_eq attribute [to_additive eq_add_of_neg_add_eq] eq_mul_of_inv_mul_eq attribute [to_additive add_eq_of_eq_neg_add] mul_eq_of_eq_inv_mul attribute [to_additive add_eq_of_eq_add_neg] mul_eq_of_eq_mul_inv attribute [to_additive neg_add] mul_inv end pending_1857 universes u v variables {α : Type u} {β : Type v} def additive (α : Type*) := α def multiplicative (α : Type*) := α instance [semigroup α] : add_semigroup (additive α) := { add := ((*) : α → α → α), add_assoc := @mul_assoc _ _ } instance [add_semigroup α] : semigroup (multiplicative α) := { mul := ((+) : α → α → α), mul_assoc := @add_assoc _ _ } instance [comm_semigroup α] : add_comm_semigroup (additive α) := { add_comm := @mul_comm _ _, ..additive.add_semigroup } instance [add_comm_semigroup α] : comm_semigroup (multiplicative α) := { mul_comm := @add_comm _ _, ..multiplicative.semigroup } instance [left_cancel_semigroup α] : add_left_cancel_semigroup (additive α) := { add_left_cancel := @mul_left_cancel _ _, ..additive.add_semigroup } instance [add_left_cancel_semigroup α] : left_cancel_semigroup (multiplicative α) := { mul_left_cancel := @add_left_cancel _ _, ..multiplicative.semigroup } instance [right_cancel_semigroup α] : add_right_cancel_semigroup (additive α) := { add_right_cancel := @mul_right_cancel _ _, ..additive.add_semigroup } instance [add_right_cancel_semigroup α] : right_cancel_semigroup (multiplicative α) := { mul_right_cancel := @add_right_cancel _ _, ..multiplicative.semigroup } @[simp, to_additive add_left_inj] theorem mul_left_inj [left_cancel_semigroup α] (a : α) {b c : α} : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congr_arg _⟩ @[simp, to_additive add_right_inj] theorem mul_right_inj [right_cancel_semigroup α] (a : α) {b c : α} : b * a = c * a ↔ b = c := ⟨mul_right_cancel, congr_arg _⟩ structure units (α : Type u) [monoid α] := (val : α) (inv : α) (val_inv : val * inv = 1) (inv_val : inv * val = 1) namespace units variables [monoid α] {a b c : units α} instance : has_coe (units α) α := ⟨val⟩ @[extensionality] theorem ext : ∀ {a b : units α}, (a : α) = b → a = b | ⟨v, i₁, vi₁, iv₁⟩ ⟨v', i₂, vi₂, iv₂⟩ e := by change v = v' at e; subst v'; congr; simpa [iv₂, vi₁] using mul_assoc i₂ v i₁ theorem ext_iff {a b : units α} : a = b ↔ (a : α) = b := ⟨congr_arg _, ext⟩ instance [decidable_eq α] : decidable_eq (units α) | a b := decidable_of_iff' _ ext_iff protected def mul (u₁ u₂ : units α) : units α := ⟨u₁.val * u₂.val, u₂.inv * u₁.inv, have u₁.val * (u₂.val * u₂.inv) * u₁.inv = 1, by rw [u₂.val_inv]; simp [u₁.val_inv], by simpa [mul_comm, mul_assoc], have u₂.inv * (u₁.inv * u₁.val) * u₂.val = 1, by rw [u₁.inv_val]; simp [u₂.inv_val], by simpa [mul_comm, mul_assoc]⟩ protected def inv' (u : units α) : units α := ⟨u.inv, u.val, u.inv_val, u.val_inv⟩ instance : has_mul (units α) := ⟨units.mul⟩ instance : has_one (units α) := ⟨⟨1, 1, mul_one 1, one_mul 1⟩⟩ instance : has_inv (units α) := ⟨units.inv'⟩ variables (a b) @[simp] lemma mul_coe : (↑(a * b) : α) = a * b := rfl @[simp] lemma one_coe : ((1 : units α) : α) = 1 := rfl lemma val_coe : (↑a : α) = a.val := rfl lemma inv_coe : ((a⁻¹ : units α) : α) = a.inv := rfl @[simp] lemma inv_mul : (↑a⁻¹ * a : α) = 1 := inv_val _ @[simp] lemma mul_inv : (a * ↑a⁻¹ : α) = 1 := val_inv _ @[simp] lemma mul_inv_cancel_left (a : units α) (b : α) : (a:α) * (↑a⁻¹ * b) = b := by rw [← mul_assoc, mul_inv, one_mul] @[simp] lemma inv_mul_cancel_left (a : units α) (b : α) : (↑a⁻¹:α) * (a * b) = b := by rw [← mul_assoc, inv_mul, one_mul] @[simp] lemma mul_inv_cancel_right (a : α) (b : units α) : a * b * ↑b⁻¹ = a := by rw [mul_assoc, mul_inv, mul_one] @[simp] lemma inv_mul_cancel_right (a : α) (b : units α) : a * ↑b⁻¹ * b = a := by rw [mul_assoc, inv_mul, mul_one] instance : group (units α) := by refine {mul := (*), one := 1, inv := has_inv.inv, ..}; { intros, apply ext, simp [mul_assoc] } instance {α} [comm_monoid α] : comm_group (units α) := { mul_comm := λ u₁ u₂, ext $ mul_comm _ _, ..units.group } instance [has_repr α] : has_repr (units α) := ⟨repr ∘ val⟩ @[simp] theorem mul_left_inj (a : units α) {b c : α} : (a:α) * b = a * c ↔ b = c := ⟨λ h, by simpa using congr_arg ((*) ↑(a⁻¹ : units α)) h, congr_arg _⟩ @[simp] theorem mul_right_inj (a : units α) {b c : α} : b * a = c * a ↔ b = c := ⟨λ h, by simpa using congr_arg (* ↑(a⁻¹ : units α)) h, congr_arg _⟩ end units theorem nat.units_eq_one (u : units ℕ) : u = 1 := units.ext begin cases nat.eq_zero_or_pos u with h h, { simpa [h] using u.inv_mul }, cases nat.eq_zero_or_pos ↑u⁻¹ with h' h', { simpa [h'] using u.inv_mul }, refine le_antisymm _ h, simpa using nat.mul_le_mul_left u h' end def units.mk_of_mul_eq_one [comm_monoid α] (a b : α) (hab : a * b = 1) : units α := ⟨a, b, hab, by rwa mul_comm a b at hab⟩ @[to_additive with_zero] def with_one (α) := option α @[to_additive with_zero.has_coe_t] instance : has_coe_t α (with_one α) := ⟨some⟩ instance [semigroup α] : monoid (with_one α) := { one := none, mul := option.lift_or_get (*), mul_assoc := (option.lift_or_get_assoc _).1, one_mul := (option.lift_or_get_is_left_id _).1, mul_one := (option.lift_or_get_is_right_id _).1 } attribute [to_additive with_zero.add_monoid._proof_1] with_one.monoid._proof_1 attribute [to_additive with_zero.add_monoid._proof_2] with_one.monoid._proof_2 attribute [to_additive with_zero.add_monoid._proof_3] with_one.monoid._proof_3 attribute [to_additive with_zero.add_monoid] with_one.monoid instance [semigroup α] : mul_zero_class (with_zero α) := { zero := none, mul := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a * b)), zero_mul := by simp, mul_zero := λ a, by cases a; simp, ..with_zero.add_monoid } instance [semigroup α] : semigroup (with_zero α) := { mul_assoc := λ a b c, begin cases a with a, {refl}, cases b with b, {refl}, cases c with c, {refl}, simp [mul_assoc] end, ..with_zero.mul_zero_class } instance [comm_semigroup α] : comm_semigroup (with_zero α) := { mul_comm := λ a b, begin cases a with a; cases b with b; try {refl}, exact congr_arg some (mul_comm _ _) end, ..with_zero.semigroup } instance [monoid α] : monoid (with_zero α) := { one := some 1, one_mul := λ a, by cases a; [refl, exact congr_arg some (one_mul a)], mul_one := λ a, by cases a; [refl, exact congr_arg some (mul_one a)], ..with_zero.semigroup } instance [comm_monoid α] : comm_monoid (with_zero α) := { ..with_zero.monoid, ..with_zero.comm_semigroup } instance [monoid α] : add_monoid (additive α) := { zero := (1 : α), zero_add := @one_mul _ _, add_zero := @mul_one _ _, ..additive.add_semigroup } instance [add_monoid α] : monoid (multiplicative α) := { one := (0 : α), one_mul := @zero_add _ _, mul_one := @add_zero _ _, ..multiplicative.semigroup } section monoid variables [monoid α] {a b c : α} /-- Partial division. It is defined when the second argument is invertible, and unlike the division operator in `division_ring` it is not totalized at zero. -/ def divp (a : α) (u) : α := a * (u⁻¹ : units α) infix ` /ₚ `:70 := divp @[simp] theorem divp_self (u : units α) : (u : α) /ₚ u = 1 := by simp [divp] @[simp] theorem divp_one (a : α) : a /ₚ 1 = a := by simp [divp] theorem divp_assoc (a b : α) (u : units α) : a * b /ₚ u = a * (b /ₚ u) := by simp [divp, mul_assoc] @[simp] theorem divp_mul_cancel (a : α) (u : units α) : a /ₚ u * u = a := by simp [divp, mul_assoc] @[simp] theorem mul_divp_cancel (a : α) (u : units α) : (a * u) /ₚ u = a := by simp [divp, mul_assoc] @[simp] theorem divp_right_inj (u : units α) {a b : α} : a /ₚ u = b /ₚ u ↔ a = b := units.mul_right_inj _ theorem divp_eq_one (a : α) (u : units α) : a /ₚ u = 1 ↔ a = u := (units.mul_right_inj u).symm.trans $ by simp @[simp] theorem one_divp (u : units α) : 1 /ₚ u = ↑u⁻¹ := by simp [divp] end monoid instance [comm_semigroup α] : comm_monoid (with_one α) := { mul_comm := (option.lift_or_get_comm _).1, ..with_one.monoid } instance [add_comm_semigroup α] : add_comm_monoid (with_zero α) := { add_comm := (option.lift_or_get_comm _).1, ..with_zero.add_monoid } attribute [to_additive with_zero.add_comm_monoid] with_one.comm_monoid instance [comm_monoid α] : add_comm_monoid (additive α) := { add_comm := @mul_comm α _, ..additive.add_monoid } instance [add_comm_monoid α] : comm_monoid (multiplicative α) := { mul_comm := @add_comm α _, ..multiplicative.monoid } instance [group α] : add_group (additive α) := { neg := @has_inv.inv α _, add_left_neg := @mul_left_inv _ _, ..additive.add_monoid } instance [add_group α] : group (multiplicative α) := { inv := @has_neg.neg α _, mul_left_inv := @add_left_neg _ _, ..multiplicative.monoid } section group variables [group α] {a b c : α} instance : has_lift α (units α) := ⟨λ a, ⟨a, a⁻¹, mul_inv_self _, inv_mul_self _⟩⟩ @[simp, to_additive neg_inj'] theorem inv_inj' : a⁻¹ = b⁻¹ ↔ a = b := ⟨λ h, by rw ← inv_inv a; simp [h], congr_arg _⟩ @[to_additive eq_of_neg_eq_neg] theorem eq_of_inv_eq_inv : a⁻¹ = b⁻¹ → a = b := inv_inj'.1 @[simp, to_additive add_self_iff_eq_zero] theorem mul_self_iff_eq_one : a * a = a ↔ a = 1 := by have := @mul_left_inj _ _ a a 1; rwa mul_one at this @[simp, to_additive neg_eq_zero] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 := by rw [← @inv_inj' _ _ a 1, one_inv] @[simp, to_additive neg_ne_zero] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := not_congr inv_eq_one @[to_additive left_inverse_neg] theorem left_inverse_inv (α) [group α] : function.left_inverse (λ a : α, a⁻¹) (λ a, a⁻¹) := assume a, inv_inv a attribute [simp] mul_inv_cancel_left add_neg_cancel_left mul_inv_cancel_right add_neg_cancel_right @[to_additive eq_neg_iff_eq_neg] theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ := ⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩ @[to_additive neg_eq_iff_neg_eq] theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a := by rw [eq_comm, @eq_comm _ _ a, eq_inv_iff_eq_inv] @[to_additive add_eq_zero_iff_eq_neg] theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ := by simpa [mul_left_inv, -mul_right_inj] using @mul_right_inj _ _ b a (b⁻¹) @[to_additive add_eq_zero_iff_neg_eq] theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b := by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm] @[to_additive eq_neg_iff_add_eq_zero] theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 := mul_eq_one_iff_eq_inv.symm @[to_additive neg_eq_iff_add_eq_zero] theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 := mul_eq_one_iff_inv_eq.symm @[to_additive eq_add_neg_iff_add_eq] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b := ⟨λ h, by simp [h], λ h, by simp [h.symm]⟩ @[to_additive eq_neg_add_iff_add_eq] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c := ⟨λ h, by simp [h], λ h, by simp [h.symm]⟩ @[to_additive neg_add_eq_iff_eq_add] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by simp [h.symm], λ h, by simp [h]⟩ @[to_additive add_neg_eq_iff_eq_add] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b := ⟨λ h, by simp [h.symm], λ h, by simp [h]⟩ @[to_additive add_neg_eq_zero] theorem mul_inv_eq_one {a b : α} : a * b⁻¹ = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inv] @[to_additive neg_comm_of_comm] theorem inv_comm_of_comm {a b : α} (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:α, a⁻¹ * x * a⁻¹) H.symm, rwa [mul_assoc, mul_assoc, mul_inv_self, mul_one, ← mul_assoc, inv_mul_self, one_mul] at this; exact h end end group instance [comm_group α] : add_comm_group (additive α) := { add_comm := @mul_comm α _, ..additive.add_group } instance [add_comm_group α] : comm_group (multiplicative α) := { mul_comm := @add_comm α _, ..multiplicative.group } section add_monoid variables [add_monoid α] {a b c : α} @[simp] lemma bit0_zero : bit0 (0 : α) = 0 := add_zero _ @[simp] lemma bit1_zero [has_one α] : bit1 (0 : α) = 1 := by simp [bit1] end add_monoid section add_group variables [add_group α] {a b c : α} local attribute [simp] sub_eq_add_neg def sub_sub_cancel := @sub_sub_self @[simp] lemma sub_left_inj : a - b = a - c ↔ b = c := (add_left_inj _).trans neg_inj' @[simp] lemma sub_right_inj : b - a = c - a ↔ b = c := add_right_inj _ lemma sub_add_sub_cancel (a b c : α) : (a - b) + (b - c) = a - c := by simp lemma sub_sub_sub_cancel_right (a b c : α) : (a - c) - (b - c) = a - b := by simp theorem sub_eq_zero : a - b = 0 ↔ a = b := ⟨eq_of_sub_eq_zero, λ h, by simp [h]⟩ theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b := not_congr sub_eq_zero theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b := by split; intro h; simp [h, eq_add_neg_iff_add_eq] theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b := by split; intro h; simp [*, add_neg_eq_iff_eq_add] at * theorem eq_iff_eq_of_sub_eq_sub {a b c d : α} (H : a - b = c - d) : a = b ↔ c = d := by rw [← sub_eq_zero, H, sub_eq_zero] theorem left_inverse_sub_add_left (c : α) : function.left_inverse (λ x, x - c) (λ x, x + c) := assume x, add_sub_cancel x c theorem left_inverse_add_left_sub (c : α) : function.left_inverse (λ x, x + c) (λ x, x - c) := assume x, sub_add_cancel x c theorem left_inverse_add_right_neg_add (c : α) : function.left_inverse (λ x, c + x) (λ x, - c + x) := assume x, add_neg_cancel_left c x theorem left_inverse_neg_add_add_right (c : α) : function.left_inverse (λ x, - c + x) (λ x, c + x) := assume x, neg_add_cancel_left c x end add_group section add_comm_group variables [add_comm_group α] {a b c : α} lemma sub_eq_neg_add (a b : α) : a - b = -b + a := by simp theorem neg_add' (a b : α) : -(a + b) = -a - b := neg_add a b lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b := by rw [eq_sub_iff_add_eq, add_comm] lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c := by rw [sub_eq_iff_eq_add, add_comm] lemma add_sub_cancel' (a b : α) : a + b - a = b := by simp lemma add_sub_cancel'_right (a b : α) : a + (b - a) = b := by rw [← add_sub_assoc, add_sub_cancel'] lemma sub_right_comm (a b c : α) : a - b - c = a - c - b := by simp lemma sub_add_sub_cancel' (a b c : α) : (a - b) + (c - a) = c - b := by rw add_comm; apply sub_add_sub_cancel lemma sub_sub_sub_cancel_left (a b c : α) : (c - a) - (c - b) = b - a := by simp end add_comm_group section is_conj variables [group α] [group β] def is_conj (a b : α) := ∃ c : α, c * a * c⁻¹ = b @[refl] lemma is_conj_refl (a : α) : is_conj a a := ⟨1, by simp⟩ @[symm] lemma is_conj_symm (a b : α) : is_conj a b → is_conj b a | ⟨c, hc⟩ := ⟨c⁻¹, by simp [hc.symm, mul_assoc]⟩ @[trans] lemma is_conj_trans (a b c : α) : is_conj a b → is_conj b c → is_conj a c | ⟨c₁, hc₁⟩ ⟨c₂, hc₂⟩ := ⟨c₂ * c₁, by simp [hc₁.symm, hc₂.symm, mul_assoc]⟩ @[simp] lemma is_conj_iff_eq {α : Type*} [comm_group α] {a b : α} : is_conj a b ↔ a = b := ⟨λ ⟨c, hc⟩, by rw [← hc, mul_right_comm, mul_inv_self, one_mul], λ h, by rw h⟩ end is_conj class is_monoid_hom [monoid α] [monoid β] (f : α → β) : Prop := (map_one : f 1 = 1) (map_mul : ∀ {x y}, f (x * y) = f x * f y) class is_add_monoid_hom [add_monoid α] [add_monoid β] (f : α → β) : Prop := (map_zero : f 0 = 0) (map_add : ∀ {x y}, f (x + y) = f x + f y) attribute [to_additive is_add_monoid_hom] is_monoid_hom attribute [to_additive is_add_monoid_hom.map_add] is_monoid_hom.map_mul attribute [to_additive is_add_monoid_hom.mk] is_monoid_hom.mk namespace is_monoid_hom variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] @[to_additive is_add_monoid_hom.id] instance id : is_monoid_hom (@id α) := by refine {..}; intros; refl @[to_additive is_add_monoid_hom.id] instance comp {γ} [monoid γ] (g : β → γ) [is_monoid_hom g] : is_monoid_hom (g ∘ f) := { map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl, map_one := by simp [map_one f]; exact map_one g } end is_monoid_hom -- TODO rename fields of is_group_hom: mul ↝ map_mul? /-- Predicate for group homomorphism. -/ class is_group_hom [group α] [group β] (f : α → β) : Prop := (mul : ∀ a b : α, f (a * b) = f a * f b) class is_add_group_hom [add_group α] [add_group β] (f : α → β) : Prop := (add : ∀ a b, f (a + b) = f a + f b) attribute [to_additive is_add_group_hom] is_group_hom attribute [to_additive is_add_group_hom.add] is_group_hom.mul attribute [to_additive is_add_group_hom.mk] is_group_hom.mk namespace is_group_hom variables [group α] [group β] (f : α → β) [is_group_hom f] @[to_additive is_add_group_hom.zero] theorem one : f 1 = 1 := mul_self_iff_eq_one.1 $ by simp [(mul f 1 1).symm] @[to_additive is_add_group_hom.neg] theorem inv (a : α) : f a⁻¹ = (f a)⁻¹ := eq.symm $ inv_eq_of_mul_eq_one $ by simp [(mul f a a⁻¹).symm, one f] @[to_additive is_add_group_hom.id] instance id : is_group_hom (@id α) := ⟨λ _ _, rfl⟩ @[to_additive is_add_group_hom.comp] instance comp {γ} [group γ] (g : β → γ) [is_group_hom g] : is_group_hom (g ∘ f) := ⟨λ x y, calc g (f (x * y)) = g (f x * f y) : by rw mul f ... = g (f x) * g (f y) : by rw mul g⟩ protected lemma is_conj (f : α → β) [is_group_hom f] {a b : α} : is_conj a b → is_conj (f a) (f b) | ⟨c, hc⟩ := ⟨f c, by rw [← is_group_hom.mul f, ← is_group_hom.inv f, ← is_group_hom.mul f, hc]⟩ end is_group_hom /-- Predicate for group anti-homomorphism, or a homomorphism into the opposite group. -/ class is_group_anti_hom {β : Type*} [group α] [group β] (f : α → β) : Prop := (mul : ∀ a b : α, f (a * b) = f b * f a) namespace is_group_anti_hom variables [group α] [group β] (f : α → β) [w : is_group_anti_hom f] include w theorem one : f 1 = 1 := mul_self_iff_eq_one.1 $ by simp [(mul f 1 1).symm] theorem inv (a : α) : f a⁻¹ = (f a)⁻¹ := eq.symm $ inv_eq_of_mul_eq_one $ by simp [(mul f a⁻¹ a).symm, one f] end is_group_anti_hom theorem inv_is_group_anti_hom [group α] : is_group_anti_hom (λ x : α, x⁻¹) := ⟨mul_inv_rev⟩ namespace is_add_group_hom variables [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] lemma sub (a b) : f (a - b) = f a - f b := calc f (a - b) = f (a + -b) : rfl ... = f a + f (-b) : add f _ _ ... = f a - f b : by simp[neg f] end is_add_group_hom
5e2126e5c506c355ebc6edfce703def4edec449b
437dc96105f48409c3981d46fb48e57c9ac3a3e4
/src/data/nat/modeq.lean
724e3390c0f321776eab9a151dce9cef80687032
[ "Apache-2.0" ]
permissive
dan-c-k/mathlib
08efec79bd7481ee6da9cc44c24a653bff4fbe0d
96efc220f6225bc7a5ed8349900391a33a38cc56
refs/heads/master
1,658,082,847,093
1,589,013,201,000
1,589,013,201,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,785
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.int.gcd import tactic.abel import data.list.rotate /- # Congruences modulo a natural number This file defines the equivalence relation `a ≡ b [MOD n]` on the natural numbers, and proves basic properties about it such as the Chinese Remainder Theorem `modeq_and_modeq_iff_modeq_mul`. ## Notations `a ≡ b [MOD n]` is notation for `modeq n a b`, which is defined to mean `a % n = b % n`. ## Tags modeq, congruence, mod, MOD, modulo -/ namespace nat /-- Modular equality. `modeq n a b`, or `a ≡ b [MOD n]`, means that `a - b` is a multiple of `n`. -/ @[derive decidable] def modeq (n a b : ℕ) := a % n = b % n notation a ` ≡ `:50 b ` [MOD `:50 n `]`:0 := modeq n a b namespace modeq variables {n m a b c d : ℕ} @[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := @rfl _ _ @[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := eq.symm @[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := eq.trans theorem modeq_zero_iff : a ≡ 0 [MOD n] ↔ n ∣ a := by rw [modeq, zero_mod, dvd_iff_mod_eq_zero] theorem modeq_iff_dvd : a ≡ b [MOD n] ↔ (n:ℤ) ∣ b - a := by rw [modeq, eq_comm, ← int.coe_nat_inj', int.coe_nat_mod, int.coe_nat_mod, int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero] theorem modeq_of_dvd : (n:ℤ) ∣ b - a → a ≡ b [MOD n] := modeq_iff_dvd.2 theorem dvd_of_modeq : a ≡ b [MOD n] → (n:ℤ) ∣ b - a := modeq_iff_dvd.1 /-- A variant of `modeq_iff_dvd` with `nat` divisibility -/ theorem modeq_iff_dvd' (h : a ≤ b) : a ≡ b [MOD n] ↔ n ∣ b - a := by rw [modeq_iff_dvd, ←int.coe_nat_dvd, int.coe_nat_sub h] theorem mod_modeq (a n) : a % n ≡ a [MOD n] := nat.mod_mod _ _ theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] := modeq_of_dvd $ dvd_trans (int.coe_nat_dvd.2 d) (dvd_of_modeq h) theorem modeq_mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD (c * n)] := by unfold modeq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h] theorem modeq_mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] := modeq_of_dvd_of_modeq (dvd_mul_left _ _) $ modeq_mul_left' _ h theorem modeq_mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD (n * c)] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact modeq_mul_left' c h theorem modeq_mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] := by rw [mul_comm a, mul_comm b]; exact modeq_mul_left c h theorem modeq_mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] := (modeq_mul_left _ h₂).trans (modeq_mul_right _ h₁) theorem modeq_add (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a + c ≡ b + d [MOD n] := modeq_of_dvd begin convert dvd_add (dvd_of_modeq h₁) (dvd_of_modeq h₂) using 1, simp [sub_eq_add_neg, add_left_comm, add_comm], end theorem modeq_add_cancel_left (h₁ : a ≡ b [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : c ≡ d [MOD n] := begin simp only [modeq_iff_dvd] at *, convert _root_.dvd_sub h₂ h₁ using 1, simp [sub_eq_add_neg], abel end theorem modeq_add_cancel_right (h₁ : c ≡ d [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : a ≡ b [MOD n] := by rw [add_comm a, add_comm b] at h₂; exact modeq_add_cancel_left h₁ h₂ theorem modeq_of_modeq_mul_left (m : ℕ) (h : a ≡ b [MOD m * n]) : a ≡ b [MOD n] := by rw [modeq_iff_dvd] at *; exact dvd.trans (dvd_mul_left (n : ℤ) (m : ℤ)) h theorem modeq_of_modeq_mul_right (m : ℕ) : a ≡ b [MOD n * m] → a ≡ b [MOD n] := mul_comm m n ▸ modeq_of_modeq_mul_left _ /-- The natural number less than `n*m` congruent to `a` mod `n` and `b` mod `m` -/ def chinese_remainder (co : coprime n m) (a b : ℕ) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} := ⟨let (c, d) := xgcd n m in int.to_nat ((b * c * n + a * d * m) % (n * m)), begin rw xgcd_val, dsimp [chinese_remainder._match_1], rw [modeq_iff_dvd, modeq_iff_dvd], rw [int.to_nat_of_nonneg], swap, { by_cases h₁ : n = 0, {simp [coprime, h₁] at co, substs m n, simp}, by_cases h₂ : m = 0, {simp [coprime, h₂] at co, substs m n, simp}, exact int.mod_nonneg _ (mul_ne_zero (int.coe_nat_ne_zero.2 h₁) (int.coe_nat_ne_zero.2 h₂)) }, have := gcd_eq_gcd_ab n m, simp [co.gcd_eq_one, mul_comm] at this, rw [int.mod_def, ← sub_add, ← sub_add]; split, { refine dvd_add _ (dvd_trans (dvd_mul_right _ _) (dvd_mul_right _ _)), rw [add_comm, ← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _), have := congr_arg ((*) ↑a) this, exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm, ← sub_eq_iff_eq_add] at this⟩ }, { refine dvd_add _ (dvd_trans (dvd_mul_left _ _) (dvd_mul_right _ _)), rw [← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _), have := congr_arg ((*) ↑b) this, exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm _ ↑m, ← sub_eq_iff_eq_add'] at this⟩ } end⟩ lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℕ} (hmn : coprime m n) : a ≡ b [MOD m] ∧ a ≡ b [MOD n] ↔ (a ≡ b [MOD m * n]) := ⟨λ h, begin rw [nat.modeq.modeq_iff_dvd, nat.modeq.modeq_iff_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd] at h, rw [nat.modeq.modeq_iff_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd], exact hmn.mul_dvd_of_dvd_of_dvd h.1 h.2 end, λ h, ⟨nat.modeq.modeq_of_modeq_mul_right _ h, nat.modeq.modeq_of_modeq_mul_left _ h⟩⟩ lemma coprime_of_mul_modeq_one (b : ℕ) {a n : ℕ} (h : a * b ≡ 1 [MOD n]) : coprime a n := nat.coprime_of_dvd' (λ k ⟨ka, hka⟩ ⟨kb, hkb⟩, int.coe_nat_dvd.1 begin rw [hka, hkb, modeq_iff_dvd] at h, cases h with z hz, rw [sub_eq_iff_eq_add] at hz, rw [hz, int.coe_nat_mul, mul_assoc, mul_assoc, int.coe_nat_mul, ← mul_add], exact dvd_mul_right _ _, end) end modeq @[simp] lemma mod_mul_right_mod (a b c : ℕ) : a % (b * c) % b = a % b := modeq.modeq_of_modeq_mul_right _ (modeq.mod_modeq _ _) @[simp] lemma mod_mul_left_mod (a b c : ℕ) : a % (b * c) % c = a % c := modeq.modeq_of_modeq_mul_left _ (modeq.mod_modeq _ _) lemma div_mod_eq_mod_mul_div (a b c : ℕ) : a / b % c = a % (b * c) / b := if hb0 : b = 0 then by simp [hb0] else by rw [← @add_right_cancel_iff _ _ (c * (a / b / c)), mod_add_div, nat.div_div_eq_div_mul, ← nat.mul_left_inj (nat.pos_of_ne_zero hb0),← @add_left_cancel_iff _ _ (a % b), mod_add_div, mul_add, ← @add_left_cancel_iff _ _ (a % (b * c) % b), add_left_comm, ← add_assoc (a % (b * c) % b), mod_add_div, ← mul_assoc, mod_add_div, mod_mul_right_mod] lemma add_mod_add_ite (a b c : ℕ) : (a + b) % c + (if c ≤ a % c + b % c then c else 0) = a % c + b % c := have (a + b) % c = (a % c + b % c) % c, from nat.modeq.modeq_add (nat.modeq.mod_modeq _ _).symm (nat.modeq.mod_modeq _ _).symm, if hc0 : c = 0 then by simp [hc0] else begin rw this, split_ifs, { have h2 : (a % c + b % c) / c < 2, from nat.div_lt_of_lt_mul (by rw mul_two; exact add_lt_add (nat.mod_lt _ (nat.pos_of_ne_zero hc0)) (nat.mod_lt _ (nat.pos_of_ne_zero hc0))), have h0 : 0 < (a % c + b % c) / c, from nat.div_pos h (nat.pos_of_ne_zero hc0), rw [← @add_right_cancel_iff _ _ (c * ((a % c + b % c) / c)), add_comm _ c, add_assoc, mod_add_div, le_antisymm (le_of_lt_succ h2) h0, mul_one, add_comm] }, { rw [nat.mod_eq_of_lt (lt_of_not_ge h), add_zero] } end lemma add_mod_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) : (a + b) % c = a % c + b % c := by rw [← add_mod_add_ite, if_neg (not_le_of_lt hc), add_zero] lemma add_mod_add_of_le_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) : (a + b) % c + c = a % c + b % c := by rw [← add_mod_add_ite, if_pos hc] lemma add_div {a b c : ℕ} (hc0 : 0 < c) : (a + b) / c = a / c + b / c + if c ≤ a % c + b % c then 1 else 0 := begin rw [← nat.mul_left_inj hc0, ← @add_left_cancel_iff _ _ ((a + b) % c + a % c + b % c)], suffices : (a + b) % c + c * ((a + b) / c) + a % c + b % c = a % c + c * (a / c) + (b % c + c * (b / c)) + c * (if c ≤ a % c + b % c then 1 else 0) + (a + b) % c, { simpa only [mul_add, add_comm, add_left_comm, add_assoc] }, rw [mod_add_div, mod_add_div, mod_add_div, mul_ite, add_assoc, add_assoc], conv_lhs { rw ← add_mod_add_ite }, simp, ac_refl end lemma add_div_eq_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) : (a + b) / c = a / c + b / c := if hc0 : c = 0 then by simp [hc0] else by rw [add_div (nat.pos_of_ne_zero hc0), if_neg (not_le_of_lt hc), add_zero] lemma add_div_eq_of_le_mod_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) (hc0 : 0 < c) : (a + b) / c = a / c + b / c + 1 := by rw [add_div hc0, if_pos hc] lemma add_div_le_add_div (a b c : ℕ) : a / c + b / c ≤ (a + b) / c := if hc0 : c = 0 then by simp [hc0] else by rw [nat.add_div (nat.pos_of_ne_zero hc0)]; exact le_add_right _ _ lemma le_mod_add_mod_of_dvd_add_of_not_dvd {a b c : ℕ} (h : c ∣ a + b) (ha : ¬ c ∣ a) : c ≤ a % c + b % c := by_contradiction $ λ hc, have (a + b) % c = a % c + b % c, from add_mod_of_add_mod_lt (lt_of_not_ge hc), by simp [dvd_iff_mod_eq_zero, *] at * lemma odd_mul_odd {n m : ℕ} (hn1 : n % 2 = 1) (hm1 : m % 2 = 1) : (n * m) % 2 = 1 := show (n * m) % 2 = (1 * 1) % 2, from nat.modeq.modeq_mul hn1 hm1 lemma odd_mul_odd_div_two {m n : ℕ} (hm1 : m % 2 = 1) (hn1 : n % 2 = 1) : (m * n) / 2 = m * (n / 2) + m / 2 := have hm0 : 0 < m := nat.pos_of_ne_zero (λ h, by simp * at *), have hn0 : 0 < n := nat.pos_of_ne_zero (λ h, by simp * at *), (nat.mul_left_inj (show 0 < 2, from dec_trivial)).1 $ by rw [mul_add, two_mul_odd_div_two hm1, mul_left_comm, two_mul_odd_div_two hn1, two_mul_odd_div_two (nat.odd_mul_odd hm1 hn1), nat.mul_sub_left_distrib, mul_one, ← nat.add_sub_assoc hm0, nat.sub_add_cancel (le_mul_of_one_le_right' (nat.zero_le _) hn0)] lemma odd_of_mod_four_eq_one {n : ℕ} (h : n % 4 = 1) : n % 2 = 1 := @modeq.modeq_of_modeq_mul_left 2 n 1 2 h lemma odd_of_mod_four_eq_three {n : ℕ} (h : n % 4 = 3) : n % 2 = 1 := @modeq.modeq_of_modeq_mul_left 2 n 3 2 h end nat namespace list variable {α : Type*} lemma nth_rotate : ∀ {l : list α} {n m : ℕ} (hml : m < l.length), (l.rotate n).nth m = l.nth ((m + n) % l.length) | [] n m hml := (nat.not_lt_zero _ hml).elim | l 0 m hml := by simp [nat.mod_eq_of_lt hml] | (a::l) (n+1) m hml := have h₃ : m < list.length (l ++ [a]), by simpa using hml, (lt_or_eq_of_le (nat.le_of_lt_succ $ nat.mod_lt (m + n) (lt_of_le_of_lt (nat.zero_le _) hml))).elim (λ hml', have h₁ : (m + (n + 1)) % ((a :: l : list α).length) = (m + n) % ((a :: l : list α).length) + 1, from calc (m + (n + 1)) % (l.length + 1) = ((m + n) % (l.length + 1) + 1) % (l.length + 1) : add_assoc m n 1 ▸ nat.modeq.modeq_add (nat.mod_mod _ _).symm rfl ... = (m + n) % (l.length + 1) + 1 : nat.mod_eq_of_lt (nat.succ_lt_succ hml'), have h₂ : (m + n) % (l ++ [a]).length < l.length, by simpa [nat.add_one] using hml', by rw [list.rotate_cons_succ, nth_rotate h₃, list.nth_append h₂, h₁, list.nth]; simp) (λ hml', have h₁ : (m + (n + 1)) % (l.length + 1) = 0, from calc (m + (n + 1)) % (l.length + 1) = (l.length + 1) % (l.length + 1) : add_assoc m n 1 ▸ nat.modeq.modeq_add (hml'.trans (nat.mod_eq_of_lt (nat.lt_succ_self _)).symm) rfl ... = 0 : by simp, have h₂ : l.length < (l ++ [a]).length, by simp [nat.lt_succ_self], by rw [list.length, list.rotate_cons_succ, nth_rotate h₃, list.length_append, list.length_cons, list.length, zero_add, hml', h₁, list.nth_concat_length]; refl) lemma rotate_eq_self_iff_eq_repeat [hα : nonempty α] : ∀ {l : list α}, (∀ n, l.rotate n = l) ↔ ∃ a, l = list.repeat a l.length | [] := ⟨λ h, nonempty.elim hα (λ a, ⟨a, by simp⟩), by simp⟩ | (a::l) := ⟨λ h, ⟨a, list.ext_le (by simp) $ λ n hn h₁, begin rw [← option.some_inj, ← list.nth_le_nth], conv {to_lhs, rw ← h ((list.length (a :: l)) - n)}, rw [nth_rotate hn, nat.add_sub_cancel' (le_of_lt hn), nat.mod_self, nth_le_repeat], refl end⟩, λ ⟨a, ha⟩ n, ha.symm ▸ list.ext_le (by simp) (λ m hm h, have hm' : (m + n) % (list.repeat a (list.length (a :: l))).length < list.length (a :: l), by rw list.length_repeat; exact nat.mod_lt _ (nat.succ_pos _), by rw [nth_le_repeat, ← option.some_inj, ← list.nth_le_nth, nth_rotate h, list.nth_le_nth, nth_le_repeat]; simp * at *)⟩ end list
617251f974e3e0dde9211a082624f1b053029b82
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_939.lean
816fb905b2e90466e5b366c8150cfa934588d503
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
124
lean
import tactic variables {a b c : ℕ} -- BEGIN example (divab : a ∣ b) (divac : a ∣ c) : a ∣ (b + c) := sorry -- END
8a9d8afdbf5691043e1b70a9b716fae1525c4963
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/algebra/category/Mon/basic.lean
a92f6f1a79da56b3077a4fa55c2de1976a238941
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
7,789
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.concrete_category import algebra.punit_instances /-! # Category instances for monoid, add_monoid, comm_monoid, and add_comm_monoid. We introduce the bundled categories: * `Mon` * `AddMon` * `CommMon` * `AddCommMon` along with the relevant forgetful functors between them. ## Implementation notes See the note [locally reducible category instances]. -/ /-- We make SemiRing (and the other categories) locally reducible in order to define its instances. This is because writing, for example, ``` instance : concrete_category SemiRing := by { delta SemiRing, apply_instance } ``` results in an instance of the form `id (bundled_hom.concrete_category _)` and this `id`, not being [reducible], prevents a later instance search (once SemiRing is no longer reducible) from seeing that the morphisms of SemiRing are really semiring morphisms (`→+*`), and therefore have a coercion to functions, for example. It's especially important that the `has_coe_to_sort` instance not contain an extra `id` as we want the `semiring ↥R` instance to also apply to `semiring R.α` (it seems to be impractical to guarantee that we always access `R.α` through the coercion rather than directly). TODO: Probably @[derive] should be able to create instances of the required form (without `id`), and then we could use that instead of this obscure `local attribute [reducible]` method. -/ library_note "locally reducible category instances" universes u v open category_theory /-- The category of monoids and monoid morphisms. -/ @[to_additive AddMon] def Mon : Type (u+1) := bundled monoid namespace Mon /-- Construct a bundled Mon from the underlying type and typeclass. -/ @[to_additive] def of (M : Type u) [monoid M] : Mon := bundled.of M @[to_additive] instance : inhabited Mon := -- The default instance for `monoid punit` is derived via `punit.comm_ring`, -- which breaks to_additive. ⟨@of punit $ @group.to_monoid _ $ @comm_group.to_group _ punit.comm_group⟩ local attribute [reducible] Mon @[to_additive] instance : has_coe_to_sort Mon := infer_instance -- short-circuit type class inference @[to_additive add_monoid] instance (M : Mon) : monoid M := M.str @[to_additive] instance bundled_hom : bundled_hom @monoid_hom := ⟨@monoid_hom.to_fun, @monoid_hom.id, @monoid_hom.comp, @monoid_hom.coe_inj⟩ @[to_additive] instance : category Mon := infer_instance -- short-circuit type class inference @[to_additive] instance : concrete_category Mon := infer_instance -- short-circuit type class inference end Mon /-- The category of commutative monoids and monoid morphisms. -/ @[to_additive AddCommMon] def CommMon : Type (u+1) := bundled comm_monoid namespace CommMon @[to_additive] instance : bundled_hom.parent_projection comm_monoid.to_monoid := ⟨⟩ /-- Construct a bundled CommMon from the underlying type and typeclass. -/ @[to_additive] def of (M : Type u) [comm_monoid M] : CommMon := bundled.of M @[to_additive] instance : inhabited CommMon := -- The default instance for `comm_monoid punit` is derived via `punit.comm_ring`, -- which breaks to_additive. ⟨@of punit $ @comm_group.to_comm_monoid _ punit.comm_group⟩ local attribute [reducible] CommMon @[to_additive] instance : has_coe_to_sort CommMon := infer_instance -- short-circuit type class inference @[to_additive add_comm_monoid] instance (M : CommMon) : comm_monoid M := M.str @[to_additive] instance : category CommMon := infer_instance -- short-circuit type class inference @[to_additive] instance : concrete_category CommMon := infer_instance -- short-circuit type class inference @[to_additive has_forget_to_AddMon] instance has_forget_to_Mon : has_forget₂ CommMon Mon := bundled_hom.forget₂ _ _ end CommMon -- We verify that the coercions of morphisms to functions work correctly: example {R S : Mon} (f : R ⟶ S) : (R : Type) → (S : Type) := f example {R S : CommMon} (f : R ⟶ S) : (R : Type) → (S : Type) := f -- We verify that when constructing a morphism in `CommMon`, -- when we construct the `to_fun` field, the types are presented as `↥R`, -- rather than `R.α` or (as we used to have) `↥(bundled.map comm_monoid.to_monoid R)`. example (R : CommMon.{u}) : R ⟶ R := { to_fun := λ x, begin match_target (R : Type u), match_hyp x := (R : Type u), exact x * x end , map_one' := by simp, map_mul' := λ x y, begin rw [mul_assoc x y (x * y), ←mul_assoc y x y, mul_comm y x, mul_assoc, mul_assoc], end, } variables {X Y : Type u} section variables [monoid X] [monoid Y] /-- Build an isomorphism in the category `Mon` from a `mul_equiv` between `monoid`s. -/ @[to_additive add_equiv.to_AddMon_iso "Build an isomorphism in the category `AddMon` from a `add_equiv` between `add_monoid`s."] def mul_equiv.to_Mon_iso (e : X ≃* Y) : Mon.of X ≅ Mon.of Y := { hom := e.to_monoid_hom, inv := e.symm.to_monoid_hom } @[simp, to_additive add_equiv.to_AddMon_iso_hom] lemma mul_equiv.to_Mon_iso_hom {e : X ≃* Y} : e.to_Mon_iso.hom = e.to_monoid_hom := rfl @[simp, to_additive add_equiv.to_AddMon_iso_inv] lemma mul_equiv.to_Mon_iso_inv {e : X ≃* Y} : e.to_Mon_iso.inv = e.symm.to_monoid_hom := rfl end section variables [comm_monoid X] [comm_monoid Y] /-- Build an isomorphism in the category `CommMon` from a `mul_equiv` between `comm_monoid`s. -/ @[to_additive add_equiv.to_AddCommMon_iso "Build an isomorphism in the category `AddCommMon` from a `add_equiv` between `add_comm_monoid`s."] def mul_equiv.to_CommMon_iso (e : X ≃* Y) : CommMon.of X ≅ CommMon.of Y := { hom := e.to_monoid_hom, inv := e.symm.to_monoid_hom } @[simp, to_additive add_equiv.to_AddCommMon_iso_hom] lemma mul_equiv.to_CommMon_iso_hom {e : X ≃* Y} : e.to_CommMon_iso.hom = e.to_monoid_hom := rfl @[simp, to_additive add_equiv.to_AddCommMon_iso_inv] lemma mul_equiv.to_CommMon_iso_inv {e : X ≃* Y} : e.to_CommMon_iso.inv = e.symm.to_monoid_hom := rfl end namespace category_theory.iso /-- Build a `mul_equiv` from an isomorphism in the category `Mon`. -/ @[to_additive AddMond_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddMon`."] def Mon_iso_to_mul_equiv {X Y : Mon.{u}} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, map_mul' := by tidy }. /-- Build a `mul_equiv` from an isomorphism in the category `CommMon`. -/ @[to_additive AddCommMon_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddCommMon`."] def CommMon_iso_to_mul_equiv {X Y : CommMon.{u}} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, map_mul' := by tidy }. end category_theory.iso /-- multiplicative equivalences between `monoid`s are the same as (isomorphic to) isomorphisms in `Mon` -/ @[to_additive add_equiv_iso_AddMon_iso "additive equivalences between `add_monoid`s are the same as (isomorphic to) isomorphisms in `AddMon`"] def mul_equiv_iso_Mon_iso {X Y : Type u} [monoid X] [monoid Y] : (X ≃* Y) ≅ (Mon.of X ≅ Mon.of Y) := { hom := λ e, e.to_Mon_iso, inv := λ i, i.Mon_iso_to_mul_equiv, } /-- multiplicative equivalences between `comm_monoid`s are the same as (isomorphic to) isomorphisms in `CommMon` -/ @[to_additive add_equiv_iso_AddCommMon_iso "additive equivalences between `add_comm_monoid`s are the same as (isomorphic to) isomorphisms in `AddCommMon`"] def mul_equiv_iso_CommMon_iso {X Y : Type u} [comm_monoid X] [comm_monoid Y] : (X ≃* Y) ≅ (CommMon.of X ≅ CommMon.of Y) := { hom := λ e, e.to_CommMon_iso, inv := λ i, i.CommMon_iso_to_mul_equiv, }
0a053196e3e5b31916566170e5b9925a1373ad03
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/data/nat/modeq.lean
24242e0f8f2668bfcc0610838e3db634051238b7
[ "Apache-2.0" ]
permissive
sebzim4500/mathlib
e0b5a63b1655f910dee30badf09bd7e191d3cf30
6997cafbd3a7325af5cb318561768c316ceb7757
refs/heads/master
1,585,549,958,618
1,538,221,723,000
1,538,221,723,000
150,869,076
0
0
Apache-2.0
1,538,229,323,000
1,538,229,323,000
null
UTF-8
Lean
false
false
4,181
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Modular equality relation. -/ import data.int.gcd namespace nat /-- Modular equality. `modeq n a b`, or `a ≡ b [MOD n]`, means that `a - b` is a multiple of `n`. -/ def modeq (n a b : ℕ) := a % n = b % n notation a ` ≡ `:50 b ` [MOD `:50 n `]`:0 := modeq n a b namespace modeq variables {n m a b c d : ℕ} @[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := @rfl _ _ @[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := eq.symm @[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := eq.trans instance : decidable (a ≡ b [MOD n]) := by unfold modeq; apply_instance theorem modeq_zero_iff : a ≡ 0 [MOD n] ↔ n ∣ a := by rw [modeq, zero_mod, dvd_iff_mod_eq_zero] theorem modeq_iff_dvd : a ≡ b [MOD n] ↔ (n:ℤ) ∣ b - a := by rw [modeq, eq_comm, ← int.coe_nat_inj']; simp [int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero] theorem modeq_of_dvd : (n:ℤ) ∣ b - a → a ≡ b [MOD n] := modeq_iff_dvd.2 theorem dvd_of_modeq : a ≡ b [MOD n] → (n:ℤ) ∣ b - a := modeq_iff_dvd.1 theorem mod_modeq (a n) : a % n ≡ a [MOD n] := nat.mod_mod _ _ theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] := modeq_of_dvd $ dvd_trans (int.coe_nat_dvd.2 d) (dvd_of_modeq h) theorem modeq_mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD (c * n)] := by unfold modeq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h] theorem modeq_mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] := modeq_of_dvd_of_modeq (dvd_mul_left _ _) $ modeq_mul_left' _ h theorem modeq_mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD (n * c)] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact modeq_mul_left' c h theorem modeq_mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] := by rw [mul_comm a, mul_comm b]; exact modeq_mul_left c h theorem modeq_mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] := (modeq_mul_left _ h₂).trans (modeq_mul_right _ h₁) theorem modeq_add (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a + c ≡ b + d [MOD n] := modeq_of_dvd $ by simpa using dvd_add (dvd_of_modeq h₁) (dvd_of_modeq h₂) theorem modeq_add_cancel_left (h₁ : a ≡ b [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : c ≡ d [MOD n] := have (n:ℤ) ∣ a + (-a + (d + -c)), by simpa using _root_.dvd_sub (dvd_of_modeq h₂) (dvd_of_modeq h₁), modeq_of_dvd $ by rwa add_neg_cancel_left at this theorem modeq_add_cancel_right (h₁ : c ≡ d [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : a ≡ b [MOD n] := by rw [add_comm a, add_comm b] at h₂; exact modeq_add_cancel_left h₁ h₂ theorem chinese_remainder (co : coprime n m) (a b : ℕ) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} := ⟨let (c, d) := xgcd n m in int.to_nat ((b * c * n + a * d * m) % (n * m)), begin rw xgcd_val, dsimp, rw [modeq_iff_dvd, modeq_iff_dvd], rw [int.to_nat_of_nonneg], swap, { by_cases h₁ : n = 0, {simp [coprime, h₁] at co, substs m n, simp}, by_cases h₂ : m = 0, {simp [coprime, h₂] at co, substs m n, simp}, exact int.mod_nonneg _ (mul_ne_zero (int.coe_nat_ne_zero.2 h₁) (int.coe_nat_ne_zero.2 h₂)) }, have := gcd_eq_gcd_ab n m, simp [co.gcd_eq_one, mul_comm] at this, rw [int.mod_def, ← sub_add, ← sub_add]; split, { refine dvd_add _ (dvd_trans (dvd_mul_right _ _) (dvd_mul_right _ _)), rw [add_comm, ← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _), have := congr_arg ((*) ↑a) this, exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm, ← sub_eq_iff_eq_add] at this⟩ }, { refine dvd_add _ (dvd_trans (dvd_mul_left _ _) (dvd_mul_right _ _)), rw [← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _), have := congr_arg ((*) ↑b) this, exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm _ ↑m, ← sub_eq_iff_eq_add'] at this⟩ } end⟩ end modeq end nat
20a5afc077da0b43f5dc287094d2026e78f8feb5
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Elab/DefView.lean
3c6ae9e45009b904dce3dc83a564c805ca7ff8e2
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
7,170
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Std.ShareCommon import Lean.Parser.Command import Lean.Util.CollectLevelParams import Lean.Util.FoldConsts import Lean.Meta.CollectFVars import Lean.Elab.Command import Lean.Elab.SyntheticMVars import Lean.Elab.Binders import Lean.Elab.DeclUtil namespace Lean.Elab inductive DefKind where | «def» | «theorem» | «example» | «opaque» | «abbrev» deriving Inhabited def DefKind.isTheorem : DefKind → Bool | «theorem» => true | _ => false def DefKind.isDefOrAbbrevOrOpaque : DefKind → Bool | «def» => true | «opaque» => true | «abbrev» => true | _ => false def DefKind.isExample : DefKind → Bool | «example» => true | _ => false structure DefView where kind : DefKind ref : Syntax modifiers : Modifiers declId : Syntax binders : Syntax type? : Option Syntax value : Syntax deriving Inhabited namespace Command open Meta def mkDefViewOfAbbrev (modifiers : Modifiers) (stx : Syntax) : DefView := -- leading_parser "abbrev " >> declId >> optDeclSig >> declVal let (binders, type) := expandOptDeclSig (stx.getArg 2) let modifiers := modifiers.addAttribute { name := `inline } let modifiers := modifiers.addAttribute { name := `reducible } { ref := stx, kind := DefKind.abbrev, modifiers := modifiers, declId := stx.getArg 1, binders := binders, type? := type, value := stx.getArg 3 } def mkDefViewOfDef (modifiers : Modifiers) (stx : Syntax) : DefView := -- leading_parser "def " >> declId >> optDeclSig >> declVal let (binders, type) := expandOptDeclSig (stx.getArg 2) { ref := stx, kind := DefKind.def, modifiers := modifiers, declId := stx.getArg 1, binders := binders, type? := type, value := stx.getArg 3 } def mkDefViewOfTheorem (modifiers : Modifiers) (stx : Syntax) : DefView := -- leading_parser "theorem " >> declId >> declSig >> declVal let (binders, type) := expandDeclSig (stx.getArg 2) { ref := stx, kind := DefKind.theorem, modifiers := modifiers, declId := stx.getArg 1, binders := binders, type? := some type, value := stx.getArg 3 } namespace MkInstanceName -- Table for `mkInstanceName` private def kindReplacements : NameMap String := Std.RBMap.ofList [ (``Parser.Term.depArrow, "DepArrow"), (``Parser.Term.«forall», "Forall"), (``Parser.Term.arrow, "Arrow"), (``Parser.Term.prop, "Prop"), (``Parser.Term.sort, "Sort"), (``Parser.Term.type, "Type") ] abbrev M := StateRefT String CommandElabM def isFirst : M Bool := return (← get) == "" def append (str : String) : M Unit := modify fun s => s ++ str partial def collect (stx : Syntax) : M Unit := do match stx with | Syntax.node k args => unless (← isFirst) do match kindReplacements.find? k with | some r => append r | none => pure () for arg in args do collect arg | Syntax.ident (preresolved := preresolved) .. => unless preresolved.isEmpty && (← resolveGlobalName stx.getId).isEmpty do match stx.getId.eraseMacroScopes with | Name.str _ str _ => if str[0].isLower then append str.capitalize else append str | _ => pure () | _ => pure () def mkFreshInstanceName : CommandElabM Name := do let s ← get let idx := s.nextInstIdx modify fun s => { s with nextInstIdx := s.nextInstIdx + 1 } return Lean.Elab.mkFreshInstanceName s.env idx partial def main (type : Syntax) : CommandElabM Name := do /- We use `expandMacros` to expand notation such as `x < y` into `LT.lt x y` -/ let type ← liftMacroM <| expandMacros type let (_, str) ← collect type |>.run "" if str.isEmpty then mkFreshInstanceName else liftMacroM <| mkUnusedBaseName <| Name.mkSimple ("inst" ++ str) end MkInstanceName def mkDefViewOfConstant (modifiers : Modifiers) (stx : Syntax) : CommandElabM DefView := do -- leading_parser "constant " >> declId >> declSig >> optional declValSimple let (binders, type) := expandDeclSig (stx.getArg 2) let val ← match (stx.getArg 3).getOptional? with | some val => pure val | none => let val ← `(arbitrary) pure $ Syntax.node ``Parser.Command.declValSimple #[ mkAtomFrom stx ":=", val ] return { ref := stx, kind := DefKind.opaque, modifiers := modifiers, declId := stx.getArg 1, binders := binders, type? := some type, value := val } def mkDefViewOfInstance (modifiers : Modifiers) (stx : Syntax) : CommandElabM DefView := do -- leading_parser Term.attrKind >> "instance " >> optNamedPrio >> optional declId >> declSig >> declVal let attrKind ← liftMacroM <| toAttributeKind stx[0] let prio ← liftMacroM <| expandOptNamedPrio stx[2] let attrStx ← `(attr| instance $(quote prio):numLit) let (binders, type) := expandDeclSig stx[4] let modifiers := modifiers.addAttribute { kind := attrKind, name := `instance, stx := attrStx } let declId ← match stx[3].getOptional? with | some declId => pure declId | none => let id ← MkInstanceName.main type pure <| Syntax.node ``Parser.Command.declId #[mkIdentFrom stx id, mkNullNode] return { ref := stx, kind := DefKind.def, modifiers := modifiers, declId := declId, binders := binders, type? := type, value := stx[5] } def mkDefViewOfExample (modifiers : Modifiers) (stx : Syntax) : DefView := -- leading_parser "example " >> declSig >> declVal let (binders, type) := expandDeclSig (stx.getArg 1) let id := mkIdentFrom stx `_example let declId := Syntax.node ``Parser.Command.declId #[id, mkNullNode] { ref := stx, kind := DefKind.example, modifiers := modifiers, declId := declId, binders := binders, type? := some type, value := stx.getArg 2 } def isDefLike (stx : Syntax) : Bool := let declKind := stx.getKind declKind == ``Parser.Command.«abbrev» || declKind == ``Parser.Command.«def» || declKind == ``Parser.Command.«theorem» || declKind == ``Parser.Command.«constant» || declKind == ``Parser.Command.«instance» || declKind == ``Parser.Command.«example» def mkDefView (modifiers : Modifiers) (stx : Syntax) : CommandElabM DefView := let declKind := stx.getKind if declKind == ``Parser.Command.«abbrev» then pure $ mkDefViewOfAbbrev modifiers stx else if declKind == ``Parser.Command.«def» then pure $ mkDefViewOfDef modifiers stx else if declKind == ``Parser.Command.«theorem» then pure $ mkDefViewOfTheorem modifiers stx else if declKind == ``Parser.Command.«constant» then mkDefViewOfConstant modifiers stx else if declKind == ``Parser.Command.«instance» then mkDefViewOfInstance modifiers stx else if declKind == ``Parser.Command.«example» then pure $ mkDefViewOfExample modifiers stx else throwError "unexpected kind of definition" builtin_initialize registerTraceClass `Elab.definition end Command end Lean.Elab
85765eaa7bb919098d00fb99d3d2b84cadfa3d11
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/data/set/lattice.lean
a9c398610554457a668241f85690a7c0b1ae2682
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
61,489
lean
/- 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, Johannes Hölzl, Mario Carneiro -/ import data.nat.basic import order.complete_boolean_algebra import order.directed import order.galois_connection /-! # The set lattice This file provides usual set notation for unions and intersections, a `complete_lattice` instance for `set α`, and some more set constructions. ## Main declarations * `set.Union`: Union of an indexed family of sets. * `set.Inter`: Intersection of an indexed family of sets. * `set.sInter`: **s**et **Inter**. Intersection of sets belonging to a set of sets. * `set.sUnion`: **s**et **Union**. Intersection of sets belonging to a set of sets. This is actually defined in core Lean. * `set.sInter_eq_bInter`, `set.sUnion_eq_bInter`: Shows that `⋂₀ s = ⋂ x ∈ s, x` and `⋃₀ s = ⋃ x ∈ s, x`. * `set.complete_boolean_algebra`: `set α` is a `complete_boolean_algebra` with `≤ = ⊆`, `< = ⊂`, `⊓ = ∩`, `⊔ = ∪`, `⨅ = ⋂`, `⨆ = ⋃` and `\` as the set difference. See `set.boolean_algebra`. * `set.kern_image`: For a function `f : α → β`, `s.kern_image f` is the set of `y` such that `f ⁻¹ y ⊆ s`. * `set.seq`: Union of the image of a set under a **seq**uence of functions. `seq s t` is the union of `f '' t` over all `f ∈ s`, where `t : set α` and `s : set (α → β)`. * `set.pairwise_disjoint`: `pairwise_disjoint s` states that all sets in `s` are either equal or disjoint. * `set.Union_eq_sigma_of_disjoint`: Equivalence between `⋃ i, t i` and `Σ i, t i`, where `t` is an indexed family of disjoint sets. ## Notation * `⋃`: `set.Union` * `⋂`: `set.Inter` * `⋃₀`: `set.sUnion` * `⋂₀`: `set.sInter` -/ open function tactic set auto universes u variables {α β γ : Type*} {ι ι' ι₂ : Sort*} namespace set /-! ### Complete lattice and complete Boolean algebra instances -/ instance : has_Inf (set α) := ⟨λ s, {a | ∀ t ∈ s, a ∈ t}⟩ instance : has_Sup (set α) := ⟨sUnion⟩ /-- Intersection of a set of sets. -/ def sInter (S : set (set α)) : set α := Inf S prefix `⋂₀`:110 := sInter @[simp] theorem mem_sInter {x : α} {S : set (set α)} : x ∈ ⋂₀ S ↔ ∀ t ∈ S, x ∈ t := iff.rfl /-- Indexed union of a family of sets -/ def Union (s : ι → set β) : set β := supr s /-- Indexed intersection of a family of sets -/ def Inter (s : ι → set β) : set β := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `⋂` binders `, ` r:(scoped f, Inter f) := r @[simp] lemma Sup_eq_sUnion (S : set (set α)) : Sup S = ⋃₀ S := rfl @[simp] lemma Inf_eq_sInter (S : set (set α)) : Inf S = ⋂₀ S := rfl @[simp] lemma supr_eq_Union (s : ι → set α) : supr s = Union s := rfl @[simp] lemma infi_eq_Inter (s : ι → set α) : infi s = Inter s := rfl @[simp] theorem mem_Union {x : β} {s : ι → set β} : x ∈ Union s ↔ ∃ i, x ∈ s i := ⟨λ ⟨t, ⟨⟨a, (t_eq : s a = t)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq.symm ▸ h⟩, λ ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ @[simp] theorem mem_Inter {x : β} {s : ι → set β} : x ∈ Inter s ↔ ∀ i, x ∈ s i := ⟨λ (h : ∀ a ∈ {a : set β | ∃ i, s i = a}, x ∈ a) a, h (s a) ⟨a, rfl⟩, λ h t ⟨a, (eq : s a = t)⟩, eq ▸ h a⟩ theorem mem_sUnion {x : α} {S : set (set α)} : x ∈ ⋃₀ S ↔ ∃ t ∈ S, x ∈ t := iff.rfl instance : complete_boolean_algebra (set α) := { Sup := Sup, Inf := Inf, le_Sup := λ s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := λ s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, le_Inf := λ s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := λ s t t_in a h, h _ t_in, infi_sup_le_sup_Inf := λ s S x, iff.mp $ by simp [forall_or_distrib_left], inf_Sup_le_supr_inf := λ s S x, iff.mp $ by simp [exists_and_distrib_left], .. set.boolean_algebra, .. pi.complete_lattice } /-- `set.image` is monotone. See `set.image_image` for the statement in terms of `⊆`. -/ lemma monotone_image {f : α → β} : monotone (image f) := λ s t, image_subset _ theorem monotone_inter [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λ x, f x ∩ g x) := λ b₁ b₂ h, inter_subset_inter (hf h) (hg h) theorem monotone_union [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λ x, f x ∪ g x) := λ b₁ b₂ h, union_subset_union (hf h) (hg h) theorem monotone_set_of [preorder α] {p : α → β → Prop} (hp : ∀ b, monotone (λ a, p a b)) : monotone (λ a, {b | p a b}) := λ a a' h b, hp b h section galois_connection variables {f : α → β} protected lemma image_preimage : galois_connection (image f) (preimage f) := λ a b, image_subset_iff /-- `kern_image f s` is the set of `y` such that `f ⁻¹ y ⊆ s`. -/ def kern_image (f : α → β) (s : set α) : set β := {y | ∀ ⦃x⦄, f x = y → x ∈ s} protected lemma preimage_kern_image : galois_connection (preimage f) (kern_image f) := λ a b, ⟨ λ h x hx y hy, have f y ∈ a, from hy.symm ▸ hx, h this, λ h x (hx : f x ∈ a), h hx rfl⟩ end galois_connection /-! ### Union and intersection over an indexed family of sets -/ @[congr] theorem Union_congr_Prop {p q : Prop} {f₁ : p → set α} {f₂ : q → set α} (pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : Union f₁ = Union f₂ := supr_congr_Prop pq f @[congr] theorem Inter_congr_Prop {p q : Prop} {f₁ : p → set α} {f₂ : q → set α} (pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : Inter f₁ = Inter f₂ := infi_congr_Prop pq f lemma Union_eq_if {p : Prop} [decidable p] (s : set α) : (⋃ h : p, s) = if p then s else ∅ := supr_eq_if _ lemma Union_eq_dif {p : Prop} [decidable p] (s : p → set α) : (⋃ (h : p), s h) = if h : p then s h else ∅ := supr_eq_dif _ lemma Inter_eq_if {p : Prop} [decidable p] (s : set α) : (⋂ h : p, s) = if p then s else univ := infi_eq_if _ lemma Infi_eq_dif {p : Prop} [decidable p] (s : p → set α) : (⋂ (h : p), s h) = if h : p then s h else univ := infi_eq_dif _ lemma exists_set_mem_of_union_eq_top {ι : Type*} (t : set ι) (s : ι → set β) (w : (⋃ i ∈ t, s i) = ⊤) (x : β) : ∃ (i ∈ t), x ∈ s i := begin have p : x ∈ ⊤ := set.mem_univ x, simpa only [←w, set.mem_Union] using p, end lemma nonempty_of_union_eq_top_of_nonempty {ι : Type*} (t : set ι) (s : ι → set α) (H : nonempty α) (w : (⋃ i ∈ t, s i) = ⊤) : t.nonempty := begin obtain ⟨x, m, -⟩ := exists_set_mem_of_union_eq_top t s w H.some, exact ⟨x, m⟩, end theorem set_of_exists (p : ι → β → Prop) : {x | ∃ i, p i x} = ⋃ i, {x | p i x} := ext $ λ i, mem_Union.symm theorem set_of_forall (p : ι → β → Prop) : {x | ∀ i, p i x} = ⋂ i, {x | p i x} := ext $ λ i, mem_Inter.symm theorem Union_subset {s : ι → set β} {t : set β} (h : ∀ i, s i ⊆ t) : (⋃ i, s i) ⊆ t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set β) _ _ _ _ h theorem Union_subset_iff {s : ι → set β} {t : set β} : (⋃ i, s i) ⊆ t ↔ (∀ i, s i ⊆ t) := ⟨λ h i, subset.trans (le_supr s _) h, Union_subset⟩ theorem mem_Inter_of_mem {x : β} {s : ι → set β} : (∀ i, x ∈ s i) → (x ∈ ⋂ i, s i) := mem_Inter.2 theorem subset_Inter {t : set β} {s : ι → set β} (h : ∀ i, t ⊆ s i) : t ⊆ ⋂ i, s i := @le_infi (set β) _ _ _ _ h theorem subset_Inter_iff {t : set β} {s : ι → set β} : t ⊆ (⋂ i, s i) ↔ ∀ i, t ⊆ s i := @le_infi_iff (set β) _ _ _ _ theorem subset_Union : ∀ (s : ι → set β) (i : ι), s i ⊆ (⋃ i, s i) := le_supr /-- This rather trivial consequence of `subset_Union`is convenient with `apply`, and has `i` explicit for this purpose. -/ theorem subset_subset_Union {A : set β} {s : ι → set β} (i : ι) (h : A ⊆ s i) : A ⊆ ⋃ (i : ι), s i := h.trans (subset_Union s i) theorem Inter_subset : ∀ (s : ι → set β) (i : ι), (⋂ i, s i) ⊆ s i := infi_le lemma Inter_subset_of_subset {s : ι → set α} {t : set α} (i : ι) (h : s i ⊆ t) : (⋂ i, s i) ⊆ t := set.subset.trans (set.Inter_subset s i) h lemma Inter_subset_Inter {s t : ι → set α} (h : ∀ i, s i ⊆ t i) : (⋂ i, s i) ⊆ (⋂ i, t i) := set.subset_Inter $ λ i, set.Inter_subset_of_subset i (h i) lemma Inter_subset_Inter2 {s : ι → set α} {t : ι' → set α} (h : ∀ j, ∃ i, s i ⊆ t j) : (⋂ i, s i) ⊆ (⋂ j, t j) := set.subset_Inter $ λ j, let ⟨i, hi⟩ := h j in Inter_subset_of_subset i hi lemma Inter_set_of (P : ι → α → Prop) : (⋂ i, {x : α | P i x}) = {x : α | ∀ i, P i x} := by { ext, simp } lemma Union_congr {f : ι → set α} {g : ι₂ → set α} (h : ι → ι₂) (h1 : surjective h) (h2 : ∀ x, g (h x) = f x) : (⋃ x, f x) = ⋃ y, g y := supr_congr h h1 h2 lemma Inter_congr {f : ι → set α} {g : ι₂ → set α} (h : ι → ι₂) (h1 : surjective h) (h2 : ∀ x, g (h x) = f x) : (⋂ x, f x) = ⋂ y, g y := infi_congr h h1 h2 theorem Union_const [nonempty ι] (s : set β) : (⋃ i : ι, s) = s := supr_const theorem Inter_const [nonempty ι] (s : set β) : (⋂ i : ι, s) = s := infi_const @[simp] theorem compl_Union (s : ι → set β) : (⋃ i, s i)ᶜ = (⋂ i, (s i)ᶜ) := compl_supr @[simp] theorem compl_Inter (s : ι → set β) : (⋂ i, s i)ᶜ = (⋃ i, (s i)ᶜ) := compl_infi -- classical -- complete_boolean_algebra theorem Union_eq_compl_Inter_compl (s : ι → set β) : (⋃ i, s i) = (⋂ i, (s i)ᶜ)ᶜ := by simp only [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_compl_Union_compl (s : ι → set β) : (⋂ i, s i) = (⋃ i, (s i)ᶜ)ᶜ := by simp only [compl_Union, compl_compl] theorem inter_Union (s : set β) (t : ι → set β) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := inf_supr_eq _ _ theorem Union_inter (s : set β) (t : ι → set β) : (⋃ i, t i) ∩ s = ⋃ i, t i ∩ s := supr_inf_eq _ _ theorem Union_union_distrib (s : ι → set β) (t : ι → set β) : (⋃ i, s i ∪ t i) = (⋃ i, s i) ∪ (⋃ i, t i) := supr_sup_eq theorem Inter_inter_distrib (s : ι → set β) (t : ι → set β) : (⋂ i, s i ∩ t i) = (⋂ i, s i) ∩ (⋂ i, t i) := infi_inf_eq theorem union_Union [nonempty ι] (s : set β) (t : ι → set β) : s ∪ (⋃ i, t i) = ⋃ i, s ∪ t i := sup_supr theorem Union_union [nonempty ι] (s : set β) (t : ι → set β) : (⋃ i, t i) ∪ s = ⋃ i, t i ∪ s := supr_sup theorem inter_Inter [nonempty ι] (s : set β) (t : ι → set β) : s ∩ (⋂ i, t i) = ⋂ i, s ∩ t i := inf_infi theorem Inter_inter [nonempty ι] (s : set β) (t : ι → set β) : (⋂ i, t i) ∩ s = ⋂ i, t i ∩ s := infi_inf -- classical theorem union_Inter (s : set β) (t : ι → set β) : s ∪ (⋂ i, t i) = ⋂ i, s ∪ t i := sup_infi_eq _ _ theorem Union_diff (s : set β) (t : ι → set β) : (⋃ i, t i) \ s = ⋃ i, t i \ s := Union_inter _ _ theorem diff_Union [nonempty ι] (s : set β) (t : ι → set β) : s \ (⋃ i, t i) = ⋂ i, s \ t i := by rw [diff_eq, compl_Union, inter_Inter]; refl theorem diff_Inter (s : set β) (t : ι → set β) : s \ (⋂ i, t i) = ⋃ i, s \ t i := by rw [diff_eq, compl_Inter, inter_Union]; refl lemma directed_on_Union {r} {f : ι → set α} (hd : directed (⊆) f) (h : ∀ x, directed_on r (f x)) : directed_on r (⋃ x, f x) := by simp only [directed_on, exists_prop, mem_Union, exists_imp_distrib]; exact λ a₁ b₁ fb₁ a₂ b₂ fb₂, let ⟨z, zb₁, zb₂⟩ := hd b₁ b₂, ⟨x, xf, xa₁, xa₂⟩ := h z a₁ (zb₁ fb₁) a₂ (zb₂ fb₂) in ⟨x, ⟨z, xf⟩, xa₁, xa₂⟩ lemma Union_inter_subset {ι α} {s t : ι → set α} : (⋃ i, s i ∩ t i) ⊆ (⋃ i, s i) ∩ (⋃ i, t i) := by { rintro x ⟨_, ⟨i, rfl⟩, xs, xt⟩, exact ⟨⟨_, ⟨i, rfl⟩, xs⟩, _, ⟨i, rfl⟩, xt⟩ } lemma Union_inter_of_monotone {ι α} [semilattice_sup ι] {s t : ι → set α} (hs : monotone s) (ht : monotone t) : (⋃ i, s i ∩ t i) = (⋃ i, s i) ∩ (⋃ i, t i) := begin ext x, refine ⟨λ hx, Union_inter_subset hx, _⟩, rintro ⟨⟨_, ⟨i, rfl⟩, xs⟩, _, ⟨j, rfl⟩, xt⟩, exact ⟨_, ⟨i ⊔ j, rfl⟩, hs le_sup_left xs, ht le_sup_right xt⟩ end /-- An equality version of this lemma is `Union_Inter_of_monotone` in `data.set.finite`. -/ lemma Union_Inter_subset {ι ι' α} {s : ι → ι' → set α} : (⋃ j, ⋂ i, s i j) ⊆ ⋂ i, ⋃ j, s i j := by { rintro x ⟨_, ⟨i, rfl⟩, hx⟩ _ ⟨j, rfl⟩, exact ⟨_, ⟨i, rfl⟩, hx _ ⟨j, rfl⟩⟩ } lemma Union_option {ι} (s : option ι → set α) : (⋃ o, s o) = s none ∪ ⋃ i, s (some i) := supr_option s lemma Inter_option {ι} (s : option ι → set α) : (⋂ o, s o) = s none ∩ ⋂ i, s (some i) := infi_option s section variables (p : ι → Prop) [decidable_pred p] lemma Union_dite (f : Π i, p i → set α) (g : Π i, ¬p i → set α) : (⋃ i, if h : p i then f i h else g i h) = (⋃ i (h : p i), f i h) ∪ (⋃ i (h : ¬ p i), g i h) := supr_dite _ _ _ lemma Union_ite (f g : ι → set α) : (⋃ i, if p i then f i else g i) = (⋃ i (h : p i), f i) ∪ (⋃ i (h : ¬ p i), g i) := Union_dite _ _ _ lemma Inter_dite (f : Π i, p i → set α) (g : Π i, ¬p i → set α) : (⋂ i, if h : p i then f i h else g i h) = (⋂ i (h : p i), f i h) ∩ (⋂ i (h : ¬ p i), g i h) := infi_dite _ _ _ lemma Inter_ite (f g : ι → set α) : (⋂ i, if p i then f i else g i) = (⋂ i (h : p i), f i) ∩ (⋂ i (h : ¬ p i), g i) := Inter_dite _ _ _ end lemma image_projection_prod {ι : Type*} {α : ι → Type*} {v : Π (i : ι), set (α i)} (hv : (pi univ v).nonempty) (i : ι) : (λ (x : Π (i : ι), α i), x i) '' (⋂ k, (λ (x : Π (j : ι), α j), x k) ⁻¹' v k) = v i:= begin classical, apply subset.antisymm, { simp [Inter_subset] }, { intros y y_in, simp only [mem_image, mem_Inter, mem_preimage], rcases hv with ⟨z, hz⟩, refine ⟨function.update z i y, _, update_same i y z⟩, rw @forall_update_iff ι α _ z i y (λ i t, t ∈ v i), exact ⟨y_in, λ j hj, by simpa using hz j⟩ }, end /-! ### Unions and intersections indexed by `Prop` -/ @[simp] theorem Inter_false {s : false → set α} : Inter s = univ := infi_false @[simp] theorem Union_false {s : false → set α} : Union s = ∅ := supr_false @[simp] theorem Inter_true {s : true → set α} : Inter s = s trivial := infi_true @[simp] theorem Union_true {s : true → set α} : Union s = s trivial := supr_true @[simp] theorem Inter_exists {p : ι → Prop} {f : Exists p → set α} : (⋂ x, f x) = (⋂ i (h : p i), f ⟨i, h⟩) := infi_exists @[simp] theorem Union_exists {p : ι → Prop} {f : Exists p → set α} : (⋃ x, f x) = (⋃ i (h : p i), f ⟨i, h⟩) := supr_exists @[simp] lemma Union_empty : (⋃ i : ι, ∅ : set α) = ∅ := supr_bot @[simp] lemma Inter_univ : (⋂ i : ι, univ : set α) = univ := infi_top section variables {s : ι → set α} @[simp] lemma Union_eq_empty : (⋃ i, s i) = ∅ ↔ ∀ i, s i = ∅ := supr_eq_bot @[simp] lemma Inter_eq_univ : (⋂ i, s i) = univ ↔ ∀ i, s i = univ := infi_eq_top @[simp] lemma nonempty_Union : (⋃ i, s i).nonempty ↔ ∃ i, (s i).nonempty := by simp [← ne_empty_iff_nonempty] lemma Union_nonempty_index (s : set α) (t : s.nonempty → set β) : (⋃ h, t h) = ⋃ x ∈ s, t ⟨x, ‹_›⟩ := supr_exists end @[simp] theorem Inter_Inter_eq_left {b : β} {s : Π x : β, x = b → set α} : (⋂ x (h : x = b), s x h) = s b rfl := infi_infi_eq_left @[simp] theorem Inter_Inter_eq_right {b : β} {s : Π x : β, b = x → set α} : (⋂ x (h : b = x), s x h) = s b rfl := infi_infi_eq_right @[simp] theorem Union_Union_eq_left {b : β} {s : Π x : β, x = b → set α} : (⋃ x (h : x = b), s x h) = s b rfl := supr_supr_eq_left @[simp] theorem Union_Union_eq_right {b : β} {s : Π x : β, b = x → set α} : (⋃ x (h : b = x), s x h) = s b rfl := supr_supr_eq_right theorem Inter_or {p q : Prop} (s : p ∨ q → set α) : (⋂ h, s h) = (⋂ h : p, s (or.inl h)) ∩ (⋂ h : q, s (or.inr h)) := infi_or theorem Union_or {p q : Prop} (s : p ∨ q → set α) : (⋃ h, s h) = (⋃ i, s (or.inl i)) ∪ (⋃ j, s (or.inr j)) := supr_or theorem Union_and {p q : Prop} (s : p ∧ q → set α) : (⋃ h, s h) = ⋃ hp hq, s ⟨hp, hq⟩ := supr_and theorem Inter_and {p q : Prop} (s : p ∧ q → set α) : (⋂ h, s h) = ⋂ hp hq, s ⟨hp, hq⟩ := infi_and theorem Union_comm (s : ι → ι' → set α) : (⋃ i i', s i i') = ⋃ i' i, s i i' := supr_comm theorem Inter_comm (s : ι → ι' → set α) : (⋂ i i', s i i') = ⋂ i' i, s i i' := infi_comm @[simp] theorem bUnion_and (p : ι → Prop) (q : ι → ι' → Prop) (s : Π x y, p x ∧ q x y → set α) : (⋃ (x : ι) (y : ι') (h : p x ∧ q x y), s x y h) = ⋃ (x : ι) (hx : p x) (y : ι') (hy : q x y), s x y ⟨hx, hy⟩ := by simp only [Union_and, @Union_comm _ ι'] @[simp] theorem bUnion_and' (p : ι' → Prop) (q : ι → ι' → Prop) (s : Π x y, p y ∧ q x y → set α) : (⋃ (x : ι) (y : ι') (h : p y ∧ q x y), s x y h) = ⋃ (y : ι') (hy : p y) (x : ι) (hx : q x y), s x y ⟨hy, hx⟩ := by simp only [Union_and, @Union_comm _ ι] @[simp] theorem bInter_and (p : ι → Prop) (q : ι → ι' → Prop) (s : Π x y, p x ∧ q x y → set α) : (⋂ (x : ι) (y : ι') (h : p x ∧ q x y), s x y h) = ⋂ (x : ι) (hx : p x) (y : ι') (hy : q x y), s x y ⟨hx, hy⟩ := by simp only [Inter_and, @Inter_comm _ ι'] @[simp] theorem bInter_and' (p : ι' → Prop) (q : ι → ι' → Prop) (s : Π x y, p y ∧ q x y → set α) : (⋂ (x : ι) (y : ι') (h : p y ∧ q x y), s x y h) = ⋂ (y : ι') (hy : p y) (x : ι) (hx : q x y), s x y ⟨hy, hx⟩ := by simp only [Inter_and, @Inter_comm _ ι] @[simp] theorem Union_Union_eq_or_left {b : β} {p : β → Prop} {s : Π x : β, (x = b ∨ p x) → set α} : (⋃ x h, s x h) = s b (or.inl rfl) ∪ ⋃ x (h : p x), s x (or.inr h) := by simp only [Union_or, Union_union_distrib, Union_Union_eq_left] @[simp] theorem Inter_Inter_eq_or_left {b : β} {p : β → Prop} {s : Π x : β, (x = b ∨ p x) → set α} : (⋂ x h, s x h) = s b (or.inl rfl) ∩ ⋂ x (h : p x), s x (or.inr h) := by simp only [Inter_or, Inter_inter_distrib, Inter_Inter_eq_left] /-! ### Bounded unions and intersections -/ theorem mem_bUnion_iff {s : set α} {t : α → set β} {y : β} : y ∈ (⋃ x ∈ s, t x) ↔ ∃ x ∈ s, y ∈ t x := by simp lemma mem_bUnion_iff' {p : α → Prop} {t : α → set β} {y : β} : y ∈ (⋃ i (h : p i), t i) ↔ ∃ i (h : p i), y ∈ t i := mem_bUnion_iff theorem mem_bInter_iff {s : set α} {t : α → set β} {y : β} : y ∈ (⋂ x ∈ s, t x) ↔ ∀ x ∈ s, y ∈ t x := by simp theorem mem_bUnion {s : set α} {t : α → set β} {x : α} {y : β} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := mem_bUnion_iff.2 ⟨x, ⟨xs, ytx⟩⟩ theorem mem_bInter {s : set α} {t : α → set β} {y : β} (h : ∀ x ∈ s, y ∈ t x) : y ∈ ⋂ x ∈ s, t x := mem_bInter_iff.2 h theorem bUnion_subset {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, u x ⊆ t) : (⋃ x ∈ s, u x) ⊆ t := Union_subset $ λ x, Union_subset (h x) theorem subset_bInter {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, t ⊆ u x) : t ⊆ (⋂ x ∈ s, u x) := subset_Inter $ λ x, subset_Inter $ h x theorem subset_bUnion_of_mem {s : set α} {u : α → set β} {x : α} (xs : x ∈ s) : u x ⊆ (⋃ x ∈ s, u x) := show u x ≤ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set α} {t : α → set β} {x : α} (xs : x ∈ s) : (⋂ x ∈ s, t x) ⊆ t x := show (⨅ x ∈ s, t x) ≤ t x, from infi_le_of_le x $ infi_le _ xs theorem bUnion_subset_bUnion_left {s s' : set α} {t : α → set β} (h : s ⊆ s') : (⋃ x ∈ s, t x) ⊆ (⋃ x ∈ s', t x) := bUnion_subset (λ x xs, subset_bUnion_of_mem (h xs)) theorem bInter_subset_bInter_left {s s' : set α} {t : α → set β} (h : s' ⊆ s) : (⋂ x ∈ s, t x) ⊆ (⋂ x ∈ s', t x) := subset_bInter (λ x xs, bInter_subset_of_mem (h xs)) theorem bUnion_subset_bUnion_right {s : set α} {t1 t2 : α → set β} (h : ∀ x ∈ s, t1 x ⊆ t2 x) : (⋃ x ∈ s, t1 x) ⊆ (⋃ x ∈ s, t2 x) := bUnion_subset (λ x xs, subset.trans (h x xs) (subset_bUnion_of_mem xs)) theorem bInter_subset_bInter_right {s : set α} {t1 t2 : α → set β} (h : ∀ x ∈ s, t1 x ⊆ t2 x) : (⋂ x ∈ s, t1 x) ⊆ (⋂ x ∈ s, t2 x) := subset_bInter (λ x xs, subset.trans (bInter_subset_of_mem xs) (h x xs)) theorem bUnion_subset_bUnion {γ : Type*} {s : set α} {t : α → set β} {s' : set γ} {t' : γ → set β} (h : ∀ x ∈ s, ∃ y ∈ s', t x ⊆ t' y) : (⋃ x ∈ s, t x) ⊆ (⋃ y ∈ s', t' y) := begin simp only [Union_subset_iff], rintros a a_in x ha, rcases h a a_in with ⟨c, c_in, hc⟩, exact mem_bUnion c_in (hc ha) end theorem bInter_mono' {s s' : set α} {t t' : α → set β} (hs : s ⊆ s') (h : ∀ x ∈ s, t x ⊆ t' x) : (⋂ x ∈ s', t x) ⊆ (⋂ x ∈ s, t' x) := begin intros x x_in, simp only [mem_Inter] at *, exact λ a a_in, h a a_in $ x_in _ (hs a_in) end theorem bInter_mono {s : set α} {t t' : α → set β} (h : ∀ x ∈ s, t x ⊆ t' x) : (⋂ x ∈ s, t x) ⊆ (⋂ x ∈ s, t' x) := bInter_mono' (subset.refl s) h theorem bUnion_mono {s : set α} {t t' : α → set β} (h : ∀ x ∈ s, t x ⊆ t' x) : (⋃ x ∈ s, t x) ⊆ (⋃ x ∈ s, t' x) := bUnion_subset_bUnion (λ x x_in, ⟨x, x_in, h x x_in⟩) theorem bUnion_eq_Union (s : set α) (t : Π x ∈ s, set β) : (⋃ x ∈ s, t x ‹_›) = (⋃ x : s, t x x.2) := supr_subtype' theorem bInter_eq_Inter (s : set α) (t : Π x ∈ s, set β) : (⋂ x ∈ s, t x ‹_›) = (⋂ x : s, t x x.2) := infi_subtype' theorem bInter_empty (u : α → set β) : (⋂ x ∈ (∅ : set α), u x) = univ := infi_emptyset theorem bInter_univ (u : α → set β) : (⋂ x ∈ @univ α, u x) = ⋂ x, u x := infi_univ @[simp] lemma bUnion_self (s : set α) : (⋃ x ∈ s, s) = s := subset.antisymm (bUnion_subset $ λ x hx, subset.refl s) (λ x hx, mem_bUnion hx hx) @[simp] lemma Union_nonempty_self (s : set α) : (⋃ h : s.nonempty, s) = s := by rw [Union_nonempty_index, bUnion_self] -- TODO(Jeremy): here is an artifact of the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : set α), s x) = s a := infi_singleton theorem bInter_union (s t : set α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := infi_union theorem bInter_insert (a : α) (s : set α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := by simp -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : α) (s : α → set β) : (⋂ x ∈ ({a, b} : set α), s x) = s a ∩ s b := by rw [bInter_insert, bInter_singleton] lemma bInter_inter {ι α : Type*} {s : set ι} (hs : s.nonempty) (f : ι → set α) (t : set α) : (⋂ i ∈ s, f i ∩ t) = (⋂ i ∈ s, f i) ∩ t := begin haveI : nonempty s := hs.to_subtype, simp [bInter_eq_Inter, ← Inter_inter] end lemma inter_bInter {ι α : Type*} {s : set ι} (hs : s.nonempty) (f : ι → set α) (t : set α) : (⋂ i ∈ s, t ∩ f i) = t ∩ ⋂ i ∈ s, f i := begin rw [inter_comm, ← bInter_inter hs], simp [inter_comm] end theorem bUnion_empty (s : α → set β) : (⋃ x ∈ (∅ : set α), s x) = ∅ := supr_emptyset theorem bUnion_univ (s : α → set β) : (⋃ x ∈ @univ α, s x) = ⋃ x, s x := supr_univ theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : set α), s x) = s a := supr_singleton @[simp] theorem bUnion_of_singleton (s : set α) : (⋃ x ∈ s, {x}) = s := ext $ by simp theorem bUnion_union (s t : set α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union @[simp] lemma Union_subtype {α β : Type*} (s : set α) (f : α → set β) : (⋃ (i : s), f i) = ⋃ (i ∈ s), f i := (set.bUnion_eq_Union s $ λ x _, f x).symm -- TODO(Jeremy): once again, simp doesn't do it alone. theorem bUnion_insert (a : α) (s : set α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := by simp theorem bUnion_pair (a b : α) (s : α → set β) : (⋃ x ∈ ({a, b} : set α), s x) = s a ∪ s b := by simp theorem compl_bUnion (s : set α) (t : α → set β) : (⋃ i ∈ s, t i)ᶜ = (⋂ i ∈ s, (t i)ᶜ) := by simp theorem compl_bInter (s : set α) (t : α → set β) : (⋂ i ∈ s, t i)ᶜ = (⋃ i ∈ s, (t i)ᶜ) := by simp theorem inter_bUnion (s : set α) (t : α → set β) (u : set β) : u ∩ (⋃ i ∈ s, t i) = ⋃ i ∈ s, u ∩ t i := by simp only [inter_Union] theorem bUnion_inter (s : set α) (t : α → set β) (u : set β) : (⋃ i ∈ s, t i) ∩ u = (⋃ i ∈ s, t i ∩ u) := by simp only [@inter_comm _ _ u, inter_bUnion] theorem mem_sUnion_of_mem {x : α} {t : set α} {S : set (set α)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ht, hx⟩ -- is this theorem really necessary? theorem not_mem_of_not_mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∉ ⋃₀ S) (ht : t ∈ S) : x ∉ t := λ h, hx ⟨t, ht, h⟩ theorem sInter_subset_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : ⋂₀ S ⊆ t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : t ⊆ ⋃₀ S := le_Sup tS lemma subset_sUnion_of_subset {s : set α} (t : set (set α)) (u : set α) (h₁ : s ⊆ u) (h₂ : u ∈ t) : s ⊆ ⋃₀ t := subset.trans h₁ (subset_sUnion_of_mem h₂) theorem sUnion_subset {S : set (set α)} {t : set α} (h : ∀ t' ∈ S, t' ⊆ t) : (⋃₀ S) ⊆ t := Sup_le h theorem sUnion_subset_iff {s : set (set α)} {t : set α} : ⋃₀ s ⊆ t ↔ ∀ t' ∈ s, t' ⊆ t := ⟨λ h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem subset_sInter {S : set (set α)} {t : set α} (h : ∀ t' ∈ S, t ⊆ t') : t ⊆ (⋂₀ S) := le_Inf h theorem sUnion_subset_sUnion {S T : set (set α)} (h : S ⊆ T) : ⋃₀ S ⊆ ⋃₀ T := sUnion_subset $ λ s hs, subset_sUnion_of_mem (h hs) theorem sInter_subset_sInter {S T : set (set α)} (h : S ⊆ T) : ⋂₀ T ⊆ ⋂₀ S := subset_sInter $ λ s hs, sInter_subset_of_mem (h hs) @[simp] theorem sUnion_empty : ⋃₀ ∅ = (∅ : set α) := Sup_empty @[simp] theorem sInter_empty : ⋂₀ ∅ = (univ : set α) := Inf_empty @[simp] theorem sUnion_singleton (s : set α) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set α) : ⋂₀ {s} = s := Inf_singleton @[simp] theorem sUnion_eq_empty {S : set (set α)} : (⋃₀ S) = ∅ ↔ ∀ s ∈ S, s = ∅ := Sup_eq_bot @[simp] theorem sInter_eq_univ {S : set (set α)} : (⋂₀ S) = univ ↔ ∀ s ∈ S, s = univ := Inf_eq_top @[simp] theorem nonempty_sUnion {S : set (set α)} : (⋃₀ S).nonempty ↔ ∃ s ∈ S, set.nonempty s := by simp [← ne_empty_iff_nonempty] lemma nonempty.of_sUnion {s : set (set α)} (h : (⋃₀ s).nonempty) : s.nonempty := let ⟨s, hs, _⟩ := nonempty_sUnion.1 h in ⟨s, hs⟩ lemma nonempty.of_sUnion_eq_univ [nonempty α] {s : set (set α)} (h : ⋃₀ s = univ) : s.nonempty := nonempty.of_sUnion $ h.symm ▸ univ_nonempty theorem sUnion_union (S T : set (set α)) : ⋃₀ (S ∪ T) = ⋃₀ S ∪ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set α)) : ⋂₀ (S ∪ T) = ⋂₀ S ∩ ⋂₀ T := Inf_union theorem sInter_Union (s : ι → set (set α)) : ⋂₀ (⋃ i, s i) = ⋂ i, ⋂₀ s i := begin ext x, simp only [mem_Union, mem_Inter, mem_sInter, exists_imp_distrib], split; tauto end @[simp] theorem sUnion_insert (s : set α) (T : set (set α)) : ⋃₀ (insert s T) = s ∪ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set α) (T : set (set α)) : ⋂₀ (insert s T) = s ∩ ⋂₀ T := Inf_insert theorem sUnion_pair (s t : set α) : ⋃₀ {s, t} = s ∪ t := Sup_pair theorem sInter_pair (s t : set α) : ⋂₀ {s, t} = s ∩ t := Inf_pair @[simp] theorem sUnion_image (f : α → set β) (s : set α) : ⋃₀ (f '' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : α → set β) (s : set α) : ⋂₀ (f '' s) = ⋂ x ∈ s, f x := Inf_image @[simp] theorem sUnion_range (f : ι → set β) : ⋃₀ (range f) = ⋃ x, f x := rfl @[simp] theorem sInter_range (f : ι → set β) : ⋂₀ (range f) = ⋂ x, f x := rfl lemma Union_eq_univ_iff {f : ι → set α} : (⋃ i, f i) = univ ↔ ∀ x, ∃ i, x ∈ f i := by simp only [eq_univ_iff_forall, mem_Union] lemma bUnion_eq_univ_iff {f : α → set β} {s : set α} : (⋃ x ∈ s, f x) = univ ↔ ∀ y, ∃ x ∈ s, y ∈ f x := by simp only [Union_eq_univ_iff, mem_Union] lemma sUnion_eq_univ_iff {c : set (set α)} : ⋃₀ c = univ ↔ ∀ a, ∃ b ∈ c, a ∈ b := by simp only [eq_univ_iff_forall, mem_sUnion] -- classical lemma Inter_eq_empty_iff {f : ι → set α} : (⋂ i, f i) = ∅ ↔ ∀ x, ∃ i, x ∉ f i := by simp [set.eq_empty_iff_forall_not_mem] -- classical lemma bInter_eq_empty_iff {f : α → set β} {s : set α} : (⋂ x ∈ s, f x) = ∅ ↔ ∀ y, ∃ x ∈ s, y ∉ f x := by simp [set.eq_empty_iff_forall_not_mem] -- classical lemma sInter_eq_empty_iff {c : set (set α)} : ⋂₀ c = ∅ ↔ ∀ a, ∃ b ∈ c, a ∉ b := by simp [set.eq_empty_iff_forall_not_mem] -- classical @[simp] theorem nonempty_Inter {f : ι → set α} : (⋂ i, f i).nonempty ↔ ∃ x, ∀ i, x ∈ f i := by simp [← ne_empty_iff_nonempty, Inter_eq_empty_iff] -- classical @[simp] theorem nonempty_bInter {f : α → set β} {s : set α} : (⋂ x ∈ s, f x).nonempty ↔ ∃ y, ∀ x ∈ s, y ∈ f x := by simp [← ne_empty_iff_nonempty, Inter_eq_empty_iff] -- classical @[simp] theorem nonempty_sInter {c : set (set α)}: (⋂₀ c).nonempty ↔ ∃ a, ∀ b ∈ c, a ∈ b := by simp [← ne_empty_iff_nonempty, sInter_eq_empty_iff] -- classical theorem compl_sUnion (S : set (set α)) : (⋃₀ S)ᶜ = ⋂₀ (compl '' S) := ext $ λ x, by simp -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set α)) : ⋃₀ S = (⋂₀ (compl '' S))ᶜ := by rw [←compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set α)) : (⋂₀ S)ᶜ = ⋃₀ (compl '' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_compl_sUnion_compl (S : set (set α)) : ⋂₀ S = (⋃₀ (compl '' S))ᶜ := by rw [←compl_compl (⋂₀ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set α} {S : set (set α)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = ∅) : s ∩ t = ∅ := eq_empty_of_subset_empty $ by rw ← h; exact inter_subset_inter_right _ (subset_sUnion_of_mem hs) theorem range_sigma_eq_Union_range {γ : α → Type*} (f : sigma γ → β) : range f = ⋃ a, range (λ b, f ⟨a, b⟩) := set.ext $ by simp theorem Union_eq_range_sigma (s : α → set β) : (⋃ i, s i) = range (λ a : Σ i, s i, a.2) := by simp [set.ext_iff] theorem Union_image_preimage_sigma_mk_eq_self {ι : Type*} {σ : ι → Type*} (s : set (sigma σ)) : (⋃ i, sigma.mk i '' (sigma.mk i ⁻¹' s)) = s := begin ext x, simp only [mem_Union, mem_image, mem_preimage], split, { rintro ⟨i, a, h, rfl⟩, exact h }, { intro h, cases x with i a, exact ⟨i, a, h, rfl⟩ } end lemma sUnion_mono {s t : set (set α)} (h : s ⊆ t) : (⋃₀ s) ⊆ (⋃₀ t) := sUnion_subset $ λ t' ht', subset_sUnion_of_mem $ h ht' lemma Union_subset_Union {s t : ι → set α} (h : ∀ i, s i ⊆ t i) : (⋃ i, s i) ⊆ (⋃ i, t i) := @supr_le_supr (set α) ι _ s t h lemma Union_subset_Union2 {s : ι → set α} {t : ι₂ → set α} (h : ∀ i, ∃ j, s i ⊆ t j) : (⋃ i, s i) ⊆ (⋃ i, t i) := @supr_le_supr2 (set α) ι ι₂ _ s t h lemma Union_subset_Union_const {s : set α} (h : ι → ι₂) : (⋃ i : ι, s) ⊆ (⋃ j : ι₂, s) := @supr_le_supr_const (set α) ι ι₂ _ s h @[simp] lemma Union_of_singleton (α : Type*) : (⋃ x, {x} : set α) = univ := Union_eq_univ_iff.2 $ λ x, ⟨x, rfl⟩ @[simp] lemma Union_of_singleton_coe (s : set α) : (⋃ (i : s), {i} : set α) = s := by simp theorem bUnion_subset_Union (s : set α) (t : α → set β) : (⋃ x ∈ s, t x) ⊆ (⋃ x, t x) := Union_subset_Union $ λ i, Union_subset $ λ h, by refl lemma sUnion_eq_bUnion {s : set (set α)} : (⋃₀ s) = (⋃ (i : set α) (h : i ∈ s), i) := by rw [← sUnion_image, image_id'] lemma sInter_eq_bInter {s : set (set α)} : (⋂₀ s) = (⋂ (i : set α) (h : i ∈ s), i) := by rw [← sInter_image, image_id'] lemma sUnion_eq_Union {s : set (set α)} : (⋃₀ s) = (⋃ (i : s), i) := by simp only [←sUnion_range, subtype.range_coe] lemma sInter_eq_Inter {s : set (set α)} : (⋂₀ s) = (⋂ (i : s), i) := by simp only [←sInter_range, subtype.range_coe] lemma union_eq_Union {s₁ s₂ : set α} : s₁ ∪ s₂ = ⋃ b : bool, cond b s₁ s₂ := sup_eq_supr s₁ s₂ lemma inter_eq_Inter {s₁ s₂ : set α} : s₁ ∩ s₂ = ⋂ b : bool, cond b s₁ s₂ := inf_eq_infi s₁ s₂ lemma sInter_union_sInter {S T : set (set α)} : (⋂₀ S) ∪ (⋂₀ T) = (⋂ p ∈ S.prod T, (p : (set α) × (set α)).1 ∪ p.2) := Inf_sup_Inf lemma sUnion_inter_sUnion {s t : set (set α)} : (⋃₀ s) ∩ (⋃₀ t) = (⋃ p ∈ s.prod t, (p : (set α) × (set α )).1 ∩ p.2) := Sup_inf_Sup lemma bUnion_Union (s : ι → set α) (t : α → set β) : (⋃ x ∈ ⋃ i, s i, t x) = ⋃ i (x ∈ s i), t x := by simp [@Union_comm _ ι] /-- If `S` is a set of sets, and each `s ∈ S` can be represented as an intersection of sets `T s hs`, then `⋂₀ S` is the intersection of the union of all `T s hs`. -/ lemma sInter_bUnion {S : set (set α)} {T : Π s ∈ S, set (set α)} (hT : ∀ s ∈ S, s = ⋂₀ T s ‹s ∈ S›) : ⋂₀ (⋃ s ∈ S, T s ‹_›) = ⋂₀ S := begin ext, simp only [and_imp, exists_prop, set.mem_sInter, set.mem_Union, exists_imp_distrib], split, { rintro H s sS, rw [hT s sS, mem_sInter], exact λ t, H t s sS }, { rintro H t s sS tTs, suffices : s ⊆ t, exact this (H s sS), rw [hT s sS, sInter_eq_bInter], exact bInter_subset_of_mem tTs } end /-- If `S` is a set of sets, and each `s ∈ S` can be represented as an union of sets `T s hs`, then `⋃₀ S` is the union of the union of all `T s hs`. -/ lemma sUnion_bUnion {S : set (set α)} {T : Π s ∈ S, set (set α)} (hT : ∀ s ∈ S, s = ⋃₀ T s ‹_›) : ⋃₀ (⋃ s ∈ S, T s ‹_›) = ⋃₀ S := begin ext, simp only [exists_prop, set.mem_Union, set.mem_set_of_eq], split, { rintro ⟨t, ⟨s, sS, tTs⟩, xt⟩, refine ⟨s, sS, _⟩, rw hT s sS, exact subset_sUnion_of_mem tTs xt }, { rintro ⟨s, sS, xs⟩, rw hT s sS at xs, rcases mem_sUnion.1 xs with ⟨t, tTs, xt⟩, exact ⟨t, ⟨s, sS, tTs⟩, xt⟩ } end lemma Union_range_eq_sUnion {α β : Type*} (C : set (set α)) {f : ∀ (s : C), β → s} (hf : ∀ (s : C), surjective (f s)) : (⋃ (y : β), range (λ (s : C), (f s y).val)) = ⋃₀ C := begin ext x, split, { rintro ⟨s, ⟨y, rfl⟩, ⟨s, hs⟩, rfl⟩, refine ⟨_, hs, _⟩, exact (f ⟨s, hs⟩ y).2 }, { rintro ⟨s, hs, hx⟩, cases hf ⟨s, hs⟩ ⟨x, hx⟩ with y hy, refine ⟨_, ⟨y, rfl⟩, ⟨s, hs⟩, _⟩, exact congr_arg subtype.val hy } end lemma Union_range_eq_Union {ι α β : Type*} (C : ι → set α) {f : ∀ (x : ι), β → C x} (hf : ∀ (x : ι), surjective (f x)) : (⋃ (y : β), range (λ (x : ι), (f x y).val)) = ⋃ x, C x := begin ext x, rw [mem_Union, mem_Union], split, { rintro ⟨y, i, rfl⟩, exact ⟨i, (f i y).2⟩ }, { rintro ⟨i, hx⟩, cases hf i ⟨x, hx⟩ with y hy, exact ⟨y, i, congr_arg subtype.val hy⟩ } end lemma union_distrib_Inter_right {ι : Type*} (s : ι → set α) (t : set α) : (⋂ i, s i) ∪ t = (⋂ i, s i ∪ t) := infi_sup_eq _ _ lemma union_distrib_Inter_left {ι : Type*} (s : ι → set α) (t : set α) : t ∪ (⋂ i, s i) = (⋂ i, t ∪ s i) := sup_infi_eq _ _ lemma union_distrib_bInter_left {ι : Type*} (s : ι → set α) (u : set ι) (t : set α) : t ∪ (⋂ i ∈ u, s i) = ⋂ i ∈ u, t ∪ s i := by rw [bInter_eq_Inter, bInter_eq_Inter, union_distrib_Inter_left] lemma union_distrib_bInter_right {ι : Type*} (s : ι → set α) (u : set ι) (t : set α) : (⋂ i ∈ u, s i) ∪ t = ⋂ i ∈ u, s i ∪ t := by rw [bInter_eq_Inter, bInter_eq_Inter, union_distrib_Inter_right] section function /-! ### `maps_to` -/ lemma maps_to_sUnion {S : set (set α)} {t : set β} {f : α → β} (H : ∀ s ∈ S, maps_to f s t) : maps_to f (⋃₀ S) t := λ x ⟨s, hs, hx⟩, H s hs hx lemma maps_to_Union {s : ι → set α} {t : set β} {f : α → β} (H : ∀ i, maps_to f (s i) t) : maps_to f (⋃ i, s i) t := maps_to_sUnion $ forall_range_iff.2 H lemma maps_to_bUnion {p : ι → Prop} {s : Π (i : ι) (hi : p i), set α} {t : set β} {f : α → β} (H : ∀ i hi, maps_to f (s i hi) t) : maps_to f (⋃ i hi, s i hi) t := maps_to_Union $ λ i, maps_to_Union (H i) lemma maps_to_Union_Union {s : ι → set α} {t : ι → set β} {f : α → β} (H : ∀ i, maps_to f (s i) (t i)) : maps_to f (⋃ i, s i) (⋃ i, t i) := maps_to_Union $ λ i, (H i).mono (subset.refl _) (subset_Union t i) lemma maps_to_bUnion_bUnion {p : ι → Prop} {s : Π i (hi : p i), set α} {t : Π i (hi : p i), set β} {f : α → β} (H : ∀ i hi, maps_to f (s i hi) (t i hi)) : maps_to f (⋃ i hi, s i hi) (⋃ i hi, t i hi) := maps_to_Union_Union $ λ i, maps_to_Union_Union (H i) lemma maps_to_sInter {s : set α} {T : set (set β)} {f : α → β} (H : ∀ t ∈ T, maps_to f s t) : maps_to f s (⋂₀ T) := λ x hx t ht, H t ht hx lemma maps_to_Inter {s : set α} {t : ι → set β} {f : α → β} (H : ∀ i, maps_to f s (t i)) : maps_to f s (⋂ i, t i) := λ x hx, mem_Inter.2 $ λ i, H i hx lemma maps_to_bInter {p : ι → Prop} {s : set α} {t : Π i (hi : p i), set β} {f : α → β} (H : ∀ i hi, maps_to f s (t i hi)) : maps_to f s (⋂ i hi, t i hi) := maps_to_Inter $ λ i, maps_to_Inter (H i) lemma maps_to_Inter_Inter {s : ι → set α} {t : ι → set β} {f : α → β} (H : ∀ i, maps_to f (s i) (t i)) : maps_to f (⋂ i, s i) (⋂ i, t i) := maps_to_Inter $ λ i, (H i).mono (Inter_subset s i) (subset.refl _) lemma maps_to_bInter_bInter {p : ι → Prop} {s : Π i (hi : p i), set α} {t : Π i (hi : p i), set β} {f : α → β} (H : ∀ i hi, maps_to f (s i hi) (t i hi)) : maps_to f (⋂ i hi, s i hi) (⋂ i hi, t i hi) := maps_to_Inter_Inter $ λ i, maps_to_Inter_Inter (H i) lemma image_Inter_subset (s : ι → set α) (f : α → β) : f '' (⋂ i, s i) ⊆ ⋂ i, f '' (s i) := (maps_to_Inter_Inter $ λ i, maps_to_image f (s i)).image_subset lemma image_bInter_subset {p : ι → Prop} (s : Π i (hi : p i), set α) (f : α → β) : f '' (⋂ i hi, s i hi) ⊆ ⋂ i hi, f '' (s i hi) := (maps_to_bInter_bInter $ λ i hi, maps_to_image f (s i hi)).image_subset lemma image_sInter_subset (S : set (set α)) (f : α → β) : f '' (⋂₀ S) ⊆ ⋂ s ∈ S, f '' s := by { rw sInter_eq_bInter, apply image_bInter_subset } /-! ### `inj_on` -/ lemma inj_on.image_Inter_eq [nonempty ι] {s : ι → set α} {f : α → β} (h : inj_on f (⋃ i, s i)) : f '' (⋂ i, s i) = ⋂ i, f '' (s i) := begin inhabit ι, refine subset.antisymm (image_Inter_subset s f) (λ y hy, _), simp only [mem_Inter, mem_image_iff_bex] at hy, choose x hx hy using hy, refine ⟨x (default ι), mem_Inter.2 $ λ i, _, hy _⟩, suffices : x (default ι) = x i, { rw this, apply hx }, replace hx : ∀ i, x i ∈ ⋃ j, s j := λ i, (subset_Union _ _) (hx i), apply h (hx _) (hx _), simp only [hy] end lemma inj_on.image_bInter_eq {p : ι → Prop} {s : Π i (hi : p i), set α} (hp : ∃ i, p i) {f : α → β} (h : inj_on f (⋃ i hi, s i hi)) : f '' (⋂ i hi, s i hi) = ⋂ i hi, f '' (s i hi) := begin simp only [Inter, infi_subtype'], haveI : nonempty {i // p i} := nonempty_subtype.2 hp, apply inj_on.image_Inter_eq, simpa only [Union, supr_subtype'] using h end lemma inj_on_Union_of_directed {s : ι → set α} (hs : directed (⊆) s) {f : α → β} (hf : ∀ i, inj_on f (s i)) : inj_on f (⋃ i, s i) := begin intros x hx y hy hxy, rcases mem_Union.1 hx with ⟨i, hx⟩, rcases mem_Union.1 hy with ⟨j, hy⟩, rcases hs i j with ⟨k, hi, hj⟩, exact hf k (hi hx) (hj hy) hxy end /-! ### `surj_on` -/ lemma surj_on_sUnion {s : set α} {T : set (set β)} {f : α → β} (H : ∀ t ∈ T, surj_on f s t) : surj_on f s (⋃₀ T) := λ x ⟨t, ht, hx⟩, H t ht hx lemma surj_on_Union {s : set α} {t : ι → set β} {f : α → β} (H : ∀ i, surj_on f s (t i)) : surj_on f s (⋃ i, t i) := surj_on_sUnion $ forall_range_iff.2 H lemma surj_on_Union_Union {s : ι → set α} {t : ι → set β} {f : α → β} (H : ∀ i, surj_on f (s i) (t i)) : surj_on f (⋃ i, s i) (⋃ i, t i) := surj_on_Union $ λ i, (H i).mono (subset_Union _ _) (subset.refl _) lemma surj_on_bUnion {p : ι → Prop} {s : set α} {t : Π i (hi : p i), set β} {f : α → β} (H : ∀ i hi, surj_on f s (t i hi)) : surj_on f s (⋃ i hi, t i hi) := surj_on_Union $ λ i, surj_on_Union (H i) lemma surj_on_bUnion_bUnion {p : ι → Prop} {s : Π i (hi : p i), set α} {t : Π i (hi : p i), set β} {f : α → β} (H : ∀ i hi, surj_on f (s i hi) (t i hi)) : surj_on f (⋃ i hi, s i hi) (⋃ i hi, t i hi) := surj_on_Union_Union $ λ i, surj_on_Union_Union (H i) lemma surj_on_Inter [hi : nonempty ι] {s : ι → set α} {t : set β} {f : α → β} (H : ∀ i, surj_on f (s i) t) (Hinj : inj_on f (⋃ i, s i)) : surj_on f (⋂ i, s i) t := begin intros y hy, rw [Hinj.image_Inter_eq, mem_Inter], exact λ i, H i hy end lemma surj_on_Inter_Inter [hi : nonempty ι] {s : ι → set α} {t : ι → set β} {f : α → β} (H : ∀ i, surj_on f (s i) (t i)) (Hinj : inj_on f (⋃ i, s i)) : surj_on f (⋂ i, s i) (⋂ i, t i) := surj_on_Inter (λ i, (H i).mono (subset.refl _) (Inter_subset _ _)) Hinj /-! ### `bij_on` -/ lemma bij_on_Union {s : ι → set α} {t : ι → set β} {f : α → β} (H : ∀ i, bij_on f (s i) (t i)) (Hinj : inj_on f (⋃ i, s i)) : bij_on f (⋃ i, s i) (⋃ i, t i) := ⟨maps_to_Union_Union $ λ i, (H i).maps_to, Hinj, surj_on_Union_Union $ λ i, (H i).surj_on⟩ lemma bij_on_Inter [hi :nonempty ι] {s : ι → set α} {t : ι → set β} {f : α → β} (H : ∀ i, bij_on f (s i) (t i)) (Hinj : inj_on f (⋃ i, s i)) : bij_on f (⋂ i, s i) (⋂ i, t i) := ⟨maps_to_Inter_Inter $ λ i, (H i).maps_to, hi.elim $ λ i, (H i).inj_on.mono (Inter_subset _ _), surj_on_Inter_Inter (λ i, (H i).surj_on) Hinj⟩ lemma bij_on_Union_of_directed {s : ι → set α} (hs : directed (⊆) s) {t : ι → set β} {f : α → β} (H : ∀ i, bij_on f (s i) (t i)) : bij_on f (⋃ i, s i) (⋃ i, t i) := bij_on_Union H $ inj_on_Union_of_directed hs (λ i, (H i).inj_on) lemma bij_on_Inter_of_directed [nonempty ι] {s : ι → set α} (hs : directed (⊆) s) {t : ι → set β} {f : α → β} (H : ∀ i, bij_on f (s i) (t i)) : bij_on f (⋂ i, s i) (⋂ i, t i) := bij_on_Inter H $ inj_on_Union_of_directed hs (λ i, (H i).inj_on) end function /-! ### `image`, `preimage` -/ section image lemma image_Union {f : α → β} {s : ι → set α} : f '' (⋃ i, s i) = (⋃ i, f '' s i) := begin ext1 x, simp [image, ← exists_and_distrib_right, @exists_swap α] end lemma image_bUnion {f : α → β} {s : ι → set α} {p : ι → Prop} : f '' (⋃ i (hi : p i), s i) = (⋃ i (hi : p i), f '' s i) := by simp only [image_Union] lemma univ_subtype {p : α → Prop} : (univ : set (subtype p)) = (⋃ x (h : p x), {⟨x, h⟩}) := set.ext $ λ ⟨x, h⟩, by simp [h] lemma range_eq_Union {ι} (f : ι → α) : range f = (⋃ i, {f i}) := set.ext $ λ a, by simp [@eq_comm α a] lemma image_eq_Union (f : α → β) (s : set α) : f '' s = (⋃ i ∈ s, {f i}) := set.ext $ λ b, by simp [@eq_comm β b] lemma bUnion_range {f : ι → α} {g : α → set β} : (⋃ x ∈ range f, g x) = (⋃ y, g (f y)) := supr_range @[simp] lemma Union_Union_eq' {f : ι → α} {g : α → set β} : (⋃ x y (h : f y = x), g x) = ⋃ y, g (f y) := by simpa using bUnion_range lemma bInter_range {f : ι → α} {g : α → set β} : (⋂ x ∈ range f, g x) = (⋂ y, g (f y)) := infi_range @[simp] lemma Inter_Inter_eq' {f : ι → α} {g : α → set β} : (⋂ x y (h : f y = x), g x) = ⋂ y, g (f y) := by simpa using bInter_range variables {s : set γ} {f : γ → α} {g : α → set β} lemma bUnion_image : (⋃ x ∈ f '' s, g x) = (⋃ y ∈ s, g (f y)) := supr_image lemma bInter_image : (⋂ x ∈ f '' s, g x) = (⋂ y ∈ s, g (f y)) := infi_image end image section preimage theorem monotone_preimage {f : α → β} : monotone (preimage f) := λ a b h, preimage_mono h @[simp] theorem preimage_Union {ι : Sort*} {f : α → β} {s : ι → set β} : f ⁻¹' (⋃ i, s i) = (⋃ i, f ⁻¹' s i) := set.ext $ by simp [preimage] theorem preimage_bUnion {ι} {f : α → β} {s : set ι} {t : ι → set β} : f ⁻¹' (⋃ i ∈ s, t i) = (⋃ i ∈ s, f ⁻¹' (t i)) := by simp @[simp] theorem preimage_sUnion {f : α → β} {s : set (set β)} : f ⁻¹' (⋃₀ s) = (⋃ t ∈ s, f ⁻¹' t) := set.ext $ by simp [preimage] lemma preimage_Inter {ι : Sort*} {s : ι → set β} {f : α → β} : f ⁻¹' (⋂ i, s i) = (⋂ i, f ⁻¹' s i) := by ext; simp lemma preimage_bInter {s : γ → set β} {t : set γ} {f : α → β} : f ⁻¹' (⋂ i ∈ t, s i) = (⋂ i ∈ t, f ⁻¹' s i) := by ext; simp @[simp] lemma bUnion_preimage_singleton (f : α → β) (s : set β) : (⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' s := by rw [← preimage_bUnion, bUnion_of_singleton] lemma bUnion_range_preimage_singleton (f : α → β) : (⋃ y ∈ range f, f ⁻¹' {y}) = univ := by rw [bUnion_preimage_singleton, preimage_range] end preimage section prod theorem monotone_prod [preorder α] {f : α → set β} {g : α → set γ} (hf : monotone f) (hg : monotone g) : monotone (λ x, (f x).prod (g x)) := λ a b h, prod_mono (hf h) (hg h) alias monotone_prod ← monotone.set_prod lemma prod_Union {ι} {s : set α} {t : ι → set β} : s.prod (⋃ i, t i) = ⋃ i, s.prod (t i) := by { ext, simp } lemma prod_bUnion {ι} {u : set ι} {s : set α} {t : ι → set β} : s.prod (⋃ i ∈ u, t i) = ⋃ i ∈ u, s.prod (t i) := by simp_rw [prod_Union] lemma prod_sUnion {s : set α} {C : set (set β)} : s.prod (⋃₀ C) = ⋃₀ ((λ t, s.prod t) '' C) := by { simp only [sUnion_eq_bUnion, prod_bUnion, bUnion_image] } lemma Union_prod_const {ι} {s : ι → set α} {t : set β} : (⋃ i, s i).prod t = ⋃ i, (s i).prod t := by { ext, simp } lemma bUnion_prod_const {ι} {u : set ι} {s : ι → set α} {t : set β} : (⋃ i ∈ u, s i).prod t = ⋃ i ∈ u, (s i).prod t := by simp_rw [Union_prod_const] lemma sUnion_prod_const {C : set (set α)} {t : set β} : (⋃₀ C).prod t = ⋃₀ ((λ s : set α, s.prod t) '' C) := by { simp only [sUnion_eq_bUnion, bUnion_prod_const, bUnion_image] } lemma Union_prod {ι α β} (s : ι → set α) (t : ι → set β) : (⋃ (x : ι × ι), (s x.1).prod (t x.2)) = (⋃ (i : ι), s i).prod (⋃ (i : ι), t i) := by { ext, simp } lemma Union_prod_of_monotone [semilattice_sup α] {s : α → set β} {t : α → set γ} (hs : monotone s) (ht : monotone t) : (⋃ x, (s x).prod (t x)) = (⋃ x, (s x)).prod (⋃ x, (t x)) := begin ext ⟨z, w⟩, simp only [mem_prod, mem_Union, exists_imp_distrib, and_imp, iff_def], split, { intros x hz hw, exact ⟨⟨x, hz⟩, x, hw⟩ }, { intros x hz x' hw, exact ⟨x ⊔ x', hs le_sup_left hz, ht le_sup_right hw⟩ } end end prod section image2 variables (f : α → β → γ) {s : set α} {t : set β} lemma Union_image_left : (⋃ a ∈ s, f a '' t) = image2 f s t := by { ext y, split; simp only [mem_Union]; rintro ⟨a, ha, x, hx, ax⟩; exact ⟨a, x, ha, hx, ax⟩ } lemma Union_image_right : (⋃ b ∈ t, (λ a, f a b) '' s) = image2 f s t := by { ext y, split; simp only [mem_Union]; rintro ⟨a, b, c, d, e⟩, exact ⟨c, a, d, b, e⟩, exact ⟨b, d, a, c, e⟩ } lemma image2_Union_left (s : ι → set α) (t : set β) : image2 f (⋃ i, s i) t = ⋃ i, image2 f (s i) t := by simp only [← image_prod, Union_prod_const, image_Union] lemma image2_Union_right (s : set α) (t : ι → set β) : image2 f s (⋃ i, t i) = ⋃ i, image2 f s (t i) := by simp only [← image_prod, prod_Union, image_Union] end image2 section seq /-- Given a set `s` of functions `α → β` and `t : set α`, `seq s t` is the union of `f '' t` over all `f ∈ s`. -/ def seq (s : set (α → β)) (t : set α) : set β := {b | ∃ f ∈ s, ∃ a ∈ t, (f : α → β) a = b} lemma seq_def {s : set (α → β)} {t : set α} : seq s t = ⋃ f ∈ s, f '' t := set.ext $ by simp [seq] @[simp] lemma mem_seq_iff {s : set (α → β)} {t : set α} {b : β} : b ∈ seq s t ↔ ∃ (f ∈ s) (a ∈ t), (f : α → β) a = b := iff.rfl lemma seq_subset {s : set (α → β)} {t : set α} {u : set β} : seq s t ⊆ u ↔ (∀ f ∈ s, ∀ a ∈ t, (f : α → β) a ∈ u) := iff.intro (λ h f hf a ha, h ⟨f, hf, a, ha, rfl⟩) (λ h b ⟨f, hf, a, ha, eq⟩, eq ▸ h f hf a ha) lemma seq_mono {s₀ s₁ : set (α → β)} {t₀ t₁ : set α} (hs : s₀ ⊆ s₁) (ht : t₀ ⊆ t₁) : seq s₀ t₀ ⊆ seq s₁ t₁ := λ b ⟨f, hf, a, ha, eq⟩, ⟨f, hs hf, a, ht ha, eq⟩ lemma singleton_seq {f : α → β} {t : set α} : set.seq {f} t = f '' t := set.ext $ by simp lemma seq_singleton {s : set (α → β)} {a : α} : set.seq s {a} = (λ f : α → β, f a) '' s := set.ext $ by simp lemma seq_seq {s : set (β → γ)} {t : set (α → β)} {u : set α} : seq s (seq t u) = seq (seq ((∘) '' s) t) u := begin refine set.ext (λ c, iff.intro _ _), { rintro ⟨f, hfs, b, ⟨g, hg, a, hau, rfl⟩, rfl⟩, exact ⟨f ∘ g, ⟨(∘) f, mem_image_of_mem _ hfs, g, hg, rfl⟩, a, hau, rfl⟩ }, { rintro ⟨fg, ⟨fc, ⟨f, hfs, rfl⟩, g, hgt, rfl⟩, a, ha, rfl⟩, exact ⟨f, hfs, g a, ⟨g, hgt, a, ha, rfl⟩, rfl⟩ } end lemma image_seq {f : β → γ} {s : set (α → β)} {t : set α} : f '' seq s t = seq ((∘) f '' s) t := by rw [← singleton_seq, ← singleton_seq, seq_seq, image_singleton] lemma prod_eq_seq {s : set α} {t : set β} : s.prod t = (prod.mk '' s).seq t := begin ext ⟨a, b⟩, split, { rintro ⟨ha, hb⟩, exact ⟨prod.mk a, ⟨a, ha, rfl⟩, b, hb, rfl⟩ }, { rintro ⟨f, ⟨x, hx, rfl⟩, y, hy, eq⟩, rw ← eq, exact ⟨hx, hy⟩ } end lemma prod_image_seq_comm (s : set α) (t : set β) : (prod.mk '' s).seq t = seq ((λ b a, (a, b)) '' t) s := by rw [← prod_eq_seq, ← image_swap_prod, prod_eq_seq, image_seq, ← image_comp, prod.swap] lemma image2_eq_seq (f : α → β → γ) (s : set α) (t : set β) : image2 f s t = seq (f '' s) t := by { ext, simp } end seq /-! ### `set` as a monad -/ instance : monad set := { pure := λ (α : Type u) a, {a}, bind := λ (α β : Type u) s f, ⋃ i ∈ s, f i, seq := λ (α β : Type u), set.seq, map := λ (α β : Type u), set.image } section monad variables {α' β' : Type u} {s : set α'} {f : α' → set β'} {g : set (α' → β')} @[simp] lemma bind_def : s >>= f = ⋃ i ∈ s, f i := rfl @[simp] lemma fmap_eq_image (f : α' → β') : f <$> s = f '' s := rfl @[simp] lemma seq_eq_set_seq {α β : Type*} (s : set (α → β)) (t : set α) : s <*> t = s.seq t := rfl @[simp] lemma pure_def (a : α) : (pure a : set α) = {a} := rfl end monad instance : is_lawful_monad set := { pure_bind := λ α β x f, by simp, bind_assoc := λ α β γ s f g, set.ext $ λ a, by simp [exists_and_distrib_right.symm, -exists_and_distrib_right, exists_and_distrib_left.symm, -exists_and_distrib_left, and_assoc]; exact exists_swap, id_map := λ α, id_map, bind_pure_comp_eq_map := λ α β f s, set.ext $ by simp [set.image, eq_comm], bind_map_eq_seq := λ α β s t, by simp [seq_def] } instance : is_comm_applicative (set : Type u → Type u) := ⟨ λ α β s t, prod_image_seq_comm s t ⟩ section pi variables {π : α → Type*} lemma pi_def (i : set α) (s : Π a, set (π a)) : pi i s = (⋂ a ∈ i, eval a ⁻¹' s a) := by { ext, simp } lemma univ_pi_eq_Inter (t : Π i, set (π i)) : pi univ t = ⋂ i, eval i ⁻¹' t i := by simp only [pi_def, Inter_true, mem_univ] lemma pi_diff_pi_subset (i : set α) (s t : Π a, set (π a)) : pi i s \ pi i t ⊆ ⋃ a ∈ i, (eval a ⁻¹' (s a \ t a)) := begin refine diff_subset_comm.2 (λ x hx a ha, _), simp only [mem_diff, mem_pi, mem_Union, not_exists, mem_preimage, not_and, not_not, eval_apply] at hx, exact hx.2 _ ha (hx.1 _ ha) end lemma Union_univ_pi (t : Π i, ι → set (π i)) : (⋃ (x : α → ι), pi univ (λ i, t i (x i))) = pi univ (λ i, ⋃ (j : ι), t i j) := by { ext, simp [classical.skolem] } end pi end set namespace function namespace surjective lemma Union_comp {f : ι → ι₂} (hf : surjective f) (g : ι₂ → set α) : (⋃ x, g (f x)) = ⋃ y, g y := hf.supr_comp g lemma Inter_comp {f : ι → ι₂} (hf : surjective f) (g : ι₂ → set α) : (⋂ x, g (f x)) = ⋂ y, g y := hf.infi_comp g end surjective end function /-! ### Disjoint sets We define some lemmas in the `disjoint` namespace to be able to use projection notation. -/ section disjoint variables {s t u : set α} namespace disjoint theorem union_left (hs : disjoint s u) (ht : disjoint t u) : disjoint (s ∪ t) u := hs.sup_left ht theorem union_right (ht : disjoint s t) (hu : disjoint s u) : disjoint s (t ∪ u) := ht.sup_right hu lemma inter_left (u : set α) (h : disjoint s t) : disjoint (s ∩ u) t := inf_left _ h lemma inter_left' (u : set α) (h : disjoint s t) : disjoint (u ∩ s) t := inf_left' _ h lemma inter_right (u : set α) (h : disjoint s t) : disjoint s (t ∩ u) := inf_right _ h lemma inter_right' (u : set α) (h : disjoint s t) : disjoint s (u ∩ t) := inf_right' _ h lemma preimage {α β} (f : α → β) {s t : set β} (h : disjoint s t) : disjoint (f ⁻¹' s) (f ⁻¹' t) := λ x hx, h hx end disjoint namespace set protected theorem disjoint_iff : disjoint s t ↔ s ∩ t ⊆ ∅ := iff.rfl theorem disjoint_iff_inter_eq_empty : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff lemma not_disjoint_iff : ¬disjoint s t ↔ ∃ x, x ∈ s ∧ x ∈ t := not_forall.trans $ exists_congr $ λ x, not_not lemma not_disjoint_iff_nonempty_inter {α : Type*} {s t : set α} : ¬disjoint s t ↔ (s ∩ t).nonempty := by simp [set.not_disjoint_iff, set.nonempty_def] lemma disjoint_left : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := show (∀ x, ¬(x ∈ s ∩ t)) ↔ _, from ⟨λ h a, not_and.1 $ h a, λ h a, not_and.2 $ h a⟩ theorem disjoint_right : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_of_subset_left (h : s ⊆ u) (d : disjoint u t) : disjoint s t := d.mono_left h theorem disjoint_of_subset_right (h : t ⊆ u) (d : disjoint s u) : disjoint s t := d.mono_right h theorem disjoint_of_subset {s t u v : set α} (h1 : s ⊆ u) (h2 : t ⊆ v) (d : disjoint u v) : disjoint s t := d.mono h1 h2 @[simp] theorem disjoint_union_left : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := disjoint_sup_left @[simp] theorem disjoint_union_right : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := disjoint_sup_right @[simp] theorem disjoint_Union_left {ι : Sort*} {s : ι → set α} : disjoint (⋃ i, s i) t ↔ ∀ i, disjoint (s i) t := supr_disjoint_iff @[simp] theorem disjoint_Union_right {ι : Sort*} {s : ι → set α} : disjoint t (⋃ i, s i) ↔ ∀ i, disjoint t (s i) := disjoint_supr_iff theorem disjoint_diff {a b : set α} : disjoint a (b \ a) := disjoint_iff.2 (inter_diff_self _ _) @[simp] theorem disjoint_empty (s : set α) : disjoint s ∅ := disjoint_bot_right @[simp] theorem empty_disjoint (s : set α) : disjoint ∅ s := disjoint_bot_left @[simp] lemma univ_disjoint {s : set α} : disjoint univ s ↔ s = ∅ := top_disjoint @[simp] lemma disjoint_univ {s : set α} : disjoint s univ ↔ s = ∅ := disjoint_top @[simp] theorem disjoint_singleton_left {a : α} {s : set α} : disjoint {a} s ↔ a ∉ s := by simp [set.disjoint_iff, subset_def]; exact iff.rfl @[simp] theorem disjoint_singleton_right {a : α} {s : set α} : disjoint s {a} ↔ a ∉ s := by rw [disjoint.comm]; exact disjoint_singleton_left theorem disjoint_image_image {f : β → α} {g : γ → α} {s : set β} {t : set γ} (h : ∀ b ∈ s, ∀ c ∈ t, f b ≠ g c) : disjoint (f '' s) (g '' t) := by rintro a ⟨⟨b, hb, eq⟩, c, hc, rfl⟩; exact h b hb c hc eq theorem pairwise_on_disjoint_fiber (f : α → β) (s : set β) : pairwise_on s (disjoint on (λ y, f ⁻¹' {y})) := λ y₁ _ y₂ _ hy x ⟨hx₁, hx₂⟩, hy (eq.trans (eq.symm hx₁) hx₂) lemma preimage_eq_empty {f : α → β} {s : set β} (h : disjoint s (range f)) : f ⁻¹' s = ∅ := by simpa using h.preimage f lemma preimage_eq_empty_iff {f : α → β} {s : set β} : disjoint s (range f) ↔ f ⁻¹' s = ∅ := ⟨preimage_eq_empty, λ h, by { simp [eq_empty_iff_forall_not_mem, set.disjoint_iff_inter_eq_empty] at h ⊢, finish }⟩ lemma disjoint_iff_subset_compl_right : disjoint s t ↔ s ⊆ tᶜ := disjoint_left lemma disjoint_iff_subset_compl_left : disjoint s t ↔ t ⊆ sᶜ := disjoint_right end set end disjoint namespace set /-- A collection of sets is `pairwise_disjoint`, if any two different sets in this collection are disjoint. -/ def pairwise_disjoint (s : set (set α)) : Prop := pairwise_on s disjoint lemma pairwise_disjoint.subset {s t : set (set α)} (h : s ⊆ t) (ht : pairwise_disjoint t) : pairwise_disjoint s := pairwise_on.mono h ht lemma pairwise_disjoint.range {s : set (set α)} (f : s → set α) (hf : ∀ (x : s), f x ⊆ x.1) (ht : pairwise_disjoint s) : pairwise_disjoint (range f) := begin rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ hxy, refine (ht _ x.2 _ y.2 _).mono (hf x) (hf y), intro h, apply hxy, apply congr_arg f, exact subtype.eq h end -- classical lemma pairwise_disjoint.elim {s : set (set α)} (h : pairwise_disjoint s) {x y : set α} (hx : x ∈ s) (hy : y ∈ s) (z : α) (hzx : z ∈ x) (hzy : z ∈ y) : x = y := not_not.1 $ λ h', h x hx y hy h' ⟨hzx, hzy⟩ end set namespace set variables (t : α → set β) lemma subset_diff {s t u : set α} : s ⊆ t \ u ↔ s ⊆ t ∧ disjoint s u := ⟨λ h, ⟨λ x hxs, (h hxs).1, λ x ⟨hxs, hxu⟩, (h hxs).2 hxu⟩, λ ⟨h1, h2⟩ x hxs, ⟨h1 hxs, λ hxu, h2 ⟨hxs, hxu⟩⟩⟩ /-- If `t` is an indexed family of sets, then there is a natural map from `Σ i, t i` to `⋃ i, t i` sending `⟨i, x⟩` to `x`. -/ def sigma_to_Union (x : Σ i, t i) : (⋃ i, t i) := ⟨x.2, mem_Union.2 ⟨x.1, x.2.2⟩⟩ lemma sigma_to_Union_surjective : surjective (sigma_to_Union t) | ⟨b, hb⟩ := have ∃ a, b ∈ t a, by simpa using hb, let ⟨a, hb⟩ := this in ⟨⟨a, b, hb⟩, rfl⟩ lemma sigma_to_Union_injective (h : ∀ i j, i ≠ j → disjoint (t i) (t j)) : injective (sigma_to_Union t) | ⟨a₁, b₁, h₁⟩ ⟨a₂, b₂, h₂⟩ eq := have b_eq : b₁ = b₂, from congr_arg subtype.val eq, have a_eq : a₁ = a₂, from classical.by_contradiction $ λ ne, have b₁ ∈ t a₁ ∩ t a₂, from ⟨h₁, b_eq.symm ▸ h₂⟩, h _ _ ne this, sigma.eq a_eq $ subtype.eq $ by subst b_eq; subst a_eq lemma sigma_to_Union_bijective (h : ∀ i j, i ≠ j → disjoint (t i) (t j)) : bijective (sigma_to_Union t) := ⟨sigma_to_Union_injective t h, sigma_to_Union_surjective t⟩ /-- Equivalence between a disjoint union and a dependent sum. -/ noncomputable def Union_eq_sigma_of_disjoint {t : α → set β} (h : ∀ i j, i ≠ j → disjoint (t i) (t j)) : (⋃ i, t i) ≃ (Σ i, t i) := (equiv.of_bijective _ $ sigma_to_Union_bijective t h).symm /-- Equivalence between a disjoint bounded union and a dependent sum. -/ noncomputable def bUnion_eq_sigma_of_disjoint {s : set α} {t : α → set β} (h : pairwise_on s (disjoint on t)) : (⋃ i ∈ s, t i) ≃ (Σ i : s, t i.val) := equiv.trans (equiv.set_congr (bUnion_eq_Union _ _)) $ Union_eq_sigma_of_disjoint $ λ ⟨i, hi⟩ ⟨j, hj⟩ ne, h _ hi _ hj $ λ eq, ne $ subtype.eq eq end set
cedb86fb859d67fb70fae69d7f69159f71140326
4e237972f72cffa663d6754857652da50d4b59f4
/modal.lean
be41eaeffb89493f3b0c210fddb9452126757b39
[]
no_license
technosentience/lean-modal-logic-2021
1956886e3611c46ea91464ff895474d947633393
c128091509f18d8e48ed9e9d828f40529f52f412
refs/heads/master
1,684,006,713,301
1,622,986,759,000
1,622,986,759,000
374,369,910
2
0
null
null
null
null
UTF-8
Lean
false
false
27,693
lean
import tactic open classical local attribute [instance] prop_decidable universe u inductive Formula : Type u | absurd : Formula | var : nat → Formula | impl : Formula → Formula → Formula | conj : Formula → Formula → Formula | disj : Formula → Formula → Formula | nec : Formula → Formula axiom formulas_countable : encodable Formula local attribute [instance] formulas_countable notation `⊥` : 85 := Formula.absurd prefix `#` : 65 := Formula.var infixr `→'` : 55 := Formula.impl notation `¬'` x : 60 := x →' ⊥ infixl `∧'` : 50 := Formula.conj infixl `∨'` : 50 := Formula.disj prefix `□` : 60 := Formula.nec notation `◇` x : 60 := ¬' (□ (¬'x)) inductive proof (Ax: set Formula) : set Formula → set Formula | ax : ∀ A : Formula, ∀ Γ : set Formula, A ∈ Ax → proof Γ A | ctx : ∀ A : Formula, ∀ Γ : set Formula, A ∈ Γ → proof Γ A | mp : ∀ A B : Formula, ∀ Γ : set Formula, proof Γ A → proof Γ (A →' B) → proof Γ B | rn : ∀ A : Formula, ∀ Γ : set Formula, proof ∅ A → proof Γ □A notation Γ `⊢[` A `]` f : 25 := proof A Γ f structure Model : Type (u + 1) := mk :: (W : Type u) (r : W → W → Prop) (v : W → nat → Prop) def loc_true (M : Model.{u}) : M.W → Formula → Prop | w ⊥ := false | w (#v) := M.v w v | w (f₁ →' f₂) := loc_true w f₁ → loc_true w f₂ | w (f₁ ∧' f₂) := loc_true w f₁ ∧ loc_true w f₂ | w (f₁ ∨' f₂) := loc_true w f₁ ∨ loc_true w f₂ | w (□f) := ∀ w' : M.W, M.r w w' → loc_true w' f def loc_true_ctx (M : Model.{u}) (Γ : set Formula.{u}) (w : M.W) : Prop := ∀ f : Formula, f ∈ Γ → loc_true M w f def models (M : set Model.{u}) (Γ : set Formula.{u}) (f : Formula.{u}) : Prop := ∀ m : Model, m ∈ M → ∀ w : m.W, loc_true_ctx m Γ w → loc_true m w f notation Γ `⊨[` M `]` f : 25 := models M Γ f def sound (A : set Formula.{u}) (M : set Model.{u}) : Prop := ∀ Γ : set Formula.{u}, ∀ f : Formula.{u}, (Γ ⊢[A] f) → (Γ ⊨[M] f) def complete (A : set Formula.{u}) (M : set Model.{u}) : Prop := ∀ Γ : set Formula.{u}, ∀ f : Formula.{u}, (Γ ⊨[M] f) → (Γ ⊢[A] f) inductive AxiomPC : set Formula | implIntro : ∀ A B : Formula, AxiomPC $ A →' B →' A | implChain : ∀ A B C : Formula, AxiomPC $ (A →' B →' C) →' (A →' B) →' A →' C | andIntro : ∀ A B : Formula, AxiomPC $ A →' B →' (A ∧' B) | andElimL : ∀ A B : Formula, AxiomPC $ (A ∧' B) →' A | andElimR : ∀ A B : Formula, AxiomPC $ (A ∧' B) →' B | orIntroL : ∀ A B : Formula, AxiomPC $ A →' (A ∨' B) | orIntroR : ∀ A B : Formula, AxiomPC $ B →' (A ∨' B) | orElim : ∀ A B C : Formula, AxiomPC $ (A →' C) →' (B →' C) →' (A ∨' B) →' C | absurdElim : ∀ A : Formula, AxiomPC $ ⊥ →' A | lem : ∀ A : Formula, AxiomPC $ A ∨' ¬' A notation Γ `⊢ₚ` f : 25 := Γ ⊢[AxiomPC] f inductive AxiomS5 : set Formula | prop : ∀ A : Formula, A ∈ AxiomPC → AxiomS5 A | axK : ∀ A B : Formula, AxiomS5 $ □(A →' B) →' □A →' □B | ref : ∀ A : Formula, AxiomS5 $ □A →' A | sym : ∀ A : Formula, AxiomS5 $ A →' □◇A | tran : ∀ A : Formula, AxiomS5 $ □A →' □□A notation Γ `⊢₅` f : 25 := Γ ⊢[AxiomS5] f def ModelS5 : set Model.{u} := λ M, equivalence M.r notation Γ `⊨₅` f : 25 := Γ ⊨[ModelS5] f theorem S5_sound : sound AxiomS5 ModelS5 := begin intros context formula prf model rel_equiv world loc_ctx_truth, induction prf with _ _ in_ax generalizing world, -- automatically solve prop, axK, mp cases cases in_ax with _ prf', cases prf', repeat { finish [loc_true] }, case ref : { intros boxA, exact boxA world (rel_equiv.1 world), }, case sym : { intros A w' rw' h, exact h world (rel_equiv.2.1 rw') A, }, case tran : { intros boxA w' rw' w'' rw'', exact boxA w'' (rel_equiv.2.2 rw' rw''), }, case ctx : _ _ in_ctx { exact loc_ctx_truth _ in_ctx, }, case rn : _ _ _ ih { intros w' _, apply ih, intros _ f, cases f, }, end lemma proof_mp_ctx (A : set Formula) (Γ : set Formula) (f₁ f₂ : Formula) : f₁ ∈ Γ → (f₁ →' f₂) ∈ Γ → (Γ ⊢[A] f₂) := begin intros h₁ h₂, have h₃ : (Γ ⊢[A] f₁) := proof.ctx _ _ h₁, have h₄ : (Γ ⊢[A] (f₁ →' f₂)) := proof.ctx _ _ h₂, have h₅ := proof.mp _ _ _ h₃ h₄, assumption, end lemma proof_subset (A : set Formula) (Γ Γ' : set Formula) (f : Formula) : Γ ⊆ Γ' → (Γ ⊢[A] f) → (Γ' ⊢[A] f) := begin intros h₁ h₂, induction h₂, case ax : { apply proof.ax, assumption, }, case ctx : { apply proof.ctx, tauto, }, case mp : _ _ _ _ _ ih₁ ih₂ { have h₂ := ih₁ h₁, have h₃ := ih₂ h₁, exact proof.mp _ _ _ h₂ h₃, }, case rn : { apply proof.rn, assumption, }, end lemma lift_subset (A₁ A₂ : set Formula) (as : A₁ ⊆ A₂) (Γ : set Formula) (f : Formula) : (Γ ⊢[A₁] f) → (Γ ⊢[A₂] f) := begin intro prf, induction prf, case proof.ax { apply proof.ax, apply as, assumption, }, case proof.ctx { apply proof.ctx, assumption, }, case proof.mp { apply proof.mp, assumption, assumption, }, case proof.rn { apply proof.rn, assumption, }, end lemma lift_S5 (Γ : set Formula) (f : Formula) : (Γ ⊢ₚ f) → (Γ ⊢₅ f) := lift_subset AxiomPC AxiomS5 (AxiomS5.prop) Γ f lemma append_PC' (A : set Formula) (pc : AxiomPC ⊆ A) (Γ : set Formula) (f : Formula) (g : Formula) (h : Γ ⊢[A] g) : Γ ⊢[A] (f →' g) := begin have h₁ : (Γ ⊢[A] (g →' f →' g)), apply proof.ax, apply pc, apply AxiomPC.implIntro, exact proof.mp _ _ _ h h₁, end lemma append_PC (Γ : set Formula) (f : Formula) (g : Formula) (h : Γ ⊢ₚ g) : Γ ⊢ₚ (f →' g) := append_PC' AxiomPC (by refl) _ _ _ h lemma append_S5 (Γ : set Formula) (f : Formula) (g : Formula) (h : Γ ⊢₅ g) : Γ ⊢₅ (f →' g) := append_PC' AxiomS5 (AxiomS5.prop) _ _ _ h lemma refl_PC (Γ : set Formula) (f : Formula) : Γ ⊢ₚ (f →' f) := begin have h₁ : (Γ ⊢ₚ ((f →' (f →' f) →' f) →' (f →' f →' f) →' (f →' f))), apply proof.ax, apply AxiomPC.implChain, have h₂ : (Γ ⊢ₚ (f →' (f →' f) →' f)), apply proof.ax, apply AxiomPC.implIntro, have h₃ : (Γ ⊢ₚ (f →' f →' f)), apply proof.ax, apply AxiomPC.implIntro, have h₄ := proof.mp _ _ _ h₂ h₁, have h₅ := proof.mp _ _ _ h₃ h₄, exact h₅, end lemma refl_S5 (Γ : set Formula) (f : Formula) : Γ ⊢₅ (f →' f) := lift_S5 _ _ $ refl_PC _ _ theorem deduction_PC' (A : set Formula) (pc : AxiomPC ⊆ A) (Γ : set Formula) (f : Formula) (g : Formula) : (insert f Γ ⊢[A] g) ↔ (Γ ⊢[A] (f →' g)) := begin split, generalize eq : insert f Γ = Γ', intro prf, induction prf, case proof.ax : A' _ ax { apply append_PC', assumption, apply proof.ax, assumption, }, case proof.ctx : A' _ ctx { rw [←eq] at ctx, cases ctx, case or.inl : { rw ctx, apply lift_subset AxiomPC A pc, apply refl_PC, }, case or.inr : { apply append_PC', assumption, apply proof.ctx, assumption, }, }, case proof.mp : A' B' _ _ _ ih₁ ih₂ { have h₁ : (Γ ⊢[A] ((f →' A' →' B') →' (f →' A') →' f →' B')), apply proof.ax, apply pc, apply AxiomPC.implChain, have h₂ := proof.mp _ _ _ (ih₂ eq) h₁, have h₃ := proof.mp _ _ _ (ih₁ eq) h₂, assumption, }, case proof.rn : A _ h₁ _ { apply append_PC', assumption, apply proof.rn, assumption, }, intro h₁, have h₂ : (insert f Γ ⊢[A] f) := proof.ctx _ _ (by finish), have h₃ : (insert f Γ ⊢[A] (f →' g)) := proof_subset _ _ _ _ (by finish) h₁, have h₄ := proof.mp _ _ _ h₂ h₃, assumption, end theorem deduction_PC (Γ : set Formula) (f: Formula) (g: Formula) : (insert f Γ ⊢ₚ g) ↔ (Γ ⊢ₚ (f →' g)) := deduction_PC' AxiomPC (by refl) _ _ _ theorem deduction_S5 (Γ : set Formula) (f: Formula) (g: Formula) : (insert f Γ ⊢₅ g) ↔ (Γ ⊢₅ (f →' g)) := deduction_PC' AxiomS5 AxiomS5.prop _ _ _ lemma dneg_intro_PC (Γ : set Formula) (f : Formula) : (Γ ⊢ₚ (f →' ¬' ¬'f)) := begin rw [←deduction_PC], rw [←deduction_PC], set Γ' := insert (f→'⊥) (insert f Γ), have h₁ : (Γ' ⊢ₚ f) := proof.ctx _ _ (by finish), have h₂ : (Γ' ⊢ₚ (f →' ⊥)) := proof.ctx _ _ (by finish), exact proof.mp _ _ _ h₁ h₂, end lemma dneg_elim_PC (Γ : set Formula) (f : Formula) : (Γ ⊢ₚ (¬' ¬' f →' f)) := begin rw [←deduction_PC], set Γ' := insert ((f→'⊥)→'⊥) Γ, have h₁ : (Γ' ⊢ₚ (f ∨' ¬' f)), apply proof.ax, apply AxiomPC.lem, have h₂ : (Γ' ⊢ₚ (⊥ →' f)), apply proof.ax, apply AxiomPC.absurdElim, have h₃ : (Γ' ⊢ₚ (¬'f →' ⊥ →' f) →' (¬' ¬' f) →' (¬' f) →' f), apply proof.ax, apply AxiomPC.implChain, have h₄ : (Γ' ⊢ₚ (¬'f →' ⊥ →' f)) := append_PC _ _ _ h₂, have h₅ : (Γ' ⊢ₚ (¬' ¬' f) →' (¬'f) →' f) := proof.mp _ _ _ h₄ h₃, have h₆ : (Γ' ⊢ₚ (¬' ¬' f)) := proof.ctx _ _ (by finish), have h₇ : (Γ' ⊢ₚ (¬' f →' f)) := proof.mp _ _ _ h₆ h₅, have h₈ : (Γ' ⊢ₚ (f →' f) →' (¬' f →' f) →' (f ∨' ¬' f) →' f), apply proof.ax, apply AxiomPC.orElim, have h₉ : (Γ' ⊢ₚ (f →' f)) := refl_PC _ _, have h₁₀ : (Γ' ⊢ₚ (¬' f →' f) →' (f ∨' ¬' f) →' f) := proof.mp _ _ _ h₉ h₈, have h₁₁ : (Γ' ⊢ₚ ((f ∨' ¬' f) →' f)) := proof.mp _ _ _ h₇ h₁₀, have h₁₂ : (Γ' ⊢ₚ f) := proof.mp _ _ _ h₁ h₁₁, assumption, end lemma contrapose_PC' (Γ : set Formula) (f g : Formula) : Γ ⊢ₚ ((f →' g) →' (¬'g →' ¬' f)) := begin rw [←deduction_PC], rw [←deduction_PC], rw [←deduction_PC], set Γ' := insert f (insert (g→'⊥) (insert (f→' g) Γ)), have h₁ : (Γ' ⊢ₚ f) := proof.ctx _ _ (by finish), have h₂ : (Γ' ⊢ₚ (f →' g)) := proof.ctx _ _ (by finish), have h₃ : (Γ' ⊢ₚ g) := proof.mp _ _ _ h₁ h₂, have h₄ : (Γ' ⊢ₚ (g →' ⊥)) := proof.ctx _ _ (by finish), have h₅ : (Γ' ⊢ₚ ⊥) := proof.mp _ _ _ h₃ h₄, assumption, end lemma contrapose_PC (Γ : set Formula) (f g : Formula) : (Γ ⊢ₚ (f →' g)) → (Γ ⊢ₚ (¬' g →' ¬' f)) := begin intro h₁, have h₂ := contrapose_PC' Γ f g, exact proof.mp _ _ _ h₁ h₂, end lemma contrapose_S5 (Γ : set Formula) (f g : Formula) : (Γ ⊢₅ (f →' g)) → (Γ ⊢₅ (¬' g →' ¬' f)) := begin intro h₁, have h₂ := lift_S5 _ _ (contrapose_PC' Γ f g), exact proof.mp _ _ _ h₁ h₂, end lemma nec_k (f g : Formula) : (∅ ⊢₅ (f →' g)) → (∅ ⊢₅ (□f →' □g)) := begin intro h₁, have h₂ : (∅ ⊢₅ □(f →' g)), apply proof.rn, assumption, have h₃ : (∅ ⊢₅ (□(f →' g) →' □f →' □g)), apply proof.ax, apply AxiomS5.axK, have h₄ : (∅ ⊢₅ (□f →' □g)) := proof.mp _ _ _ h₂ h₃, assumption, end lemma S5_sym_alt (Γ : set Formula) (f : Formula) : Γ ⊢₅ (◇ □ f →' f) := begin rw [←deduction_S5], set Γ' := insert (□(□f→'⊥)→'⊥) Γ, have h₁ : (Γ' ⊢₅ (¬' f →' □ ◇ ¬' f)), apply proof.ax, apply AxiomS5.sym, have h₂ := contrapose_S5 _ _ _ h₁, have h₃ : (∅ ⊢₅ (f →' ¬' ¬' f)), apply lift_S5, apply dneg_intro_PC, have h₄ := nec_k _ _ h₃, have h₅ := contrapose_S5 _ _ _ h₄, have h₆ := nec_k _ _ h₅, have h₇ := contrapose_S5 _ _ _ h₆, have h₈ := proof_subset AxiomS5 ∅ Γ' _ (by finish) h₇, have h₉ : (Γ' ⊢₅ (□(□f→'⊥)→'⊥)) := proof.ctx _ _ (by finish), have h₁₀ := proof.mp _ _ _ h₉ h₈, have h₁₁ := proof.mp _ _ _ h₁₀ h₂, have h₁₂ : (Γ' ⊢₅ (¬' ¬'f →' f)), apply lift_S5, apply dneg_elim_PC, have h₁₃ := proof.mp _ _ _ h₁₁ h₁₂, assumption, end def consistent (A : set Formula) (Γ : set Formula) : Prop := ¬(Γ ⊢[A] ⊥) def cons_ext (A : set Formula) (Γ : set Formula) (f : Formula) : set Formula := if consistent A (Γ ∪ {f}) then Γ ∪ {f} else Γ ∪ {¬'f} def cons_ext_nth (A : set Formula) (Γ : set Formula) (n : nat) : set Formula := match encodable.decode Formula n with | none := Γ | some f := cons_ext A Γ f end def cons_ext_chain (A : set Formula) (Γ : set Formula) : nat → set Formula | 0 := Γ | (n + 1) := cons_ext_nth A (cons_ext_chain n) n def cons_ext_max (A : set Formula) (Γ : set Formula) : set Formula := ⋃ n : nat, cons_ext_chain A Γ n lemma cons_ext_sub (A: set Formula) (Γ : set Formula) (f : Formula) : Γ ⊆ cons_ext A Γ f := begin simp [cons_ext], split_ifs, finish, finish, end lemma cons_ext_nth_sub (A: set Formula) (Γ : set Formula) (n : nat) : Γ ⊆ cons_ext_nth A Γ n := begin simp [cons_ext_nth], cases encodable.decode Formula n, finish, finish [cons_ext_nth, cons_ext_sub], end lemma cons_ext_chain_sub₁ (A : set Formula) (Γ : set Formula) (n : nat) : cons_ext_chain A Γ n ⊆ cons_ext_chain A Γ (n + 1) := by finish [cons_ext_chain, cons_ext_nth_sub] lemma cons_ext_chain_sub₂ (A : set Formula) (Γ : set Formula) (m n : nat) : m ≤ n → cons_ext_chain A Γ m ⊆ cons_ext_chain A Γ n := begin intro h₁, cases nat.le.dest h₁ with n' h₂, rw [←h₂] at *, clear h₂ h₁ n, induction n', refl, exact set.subset.trans n'_ih (cons_ext_chain_sub₁ _ _ _), end lemma cons_ext_max_sub (A : set Formula) (Γ : set Formula) : Γ ⊆ cons_ext_max A Γ := set.subset_Union (cons_ext_chain A Γ) 0 lemma cons_ext_cons (Γ : set Formula) (f : Formula) (h : consistent AxiomS5 Γ) : consistent AxiomS5 (cons_ext AxiomS5 Γ f) := begin by_cases h₁ : consistent AxiomS5 (Γ ∪ {f}), finish [cons_ext], simp [cons_ext, consistent] at *, split_ifs, by_contradiction h₂, have h₃ : ¬consistent AxiomS5 Γ, simp [consistent], have h₄ : (Γ ⊢[AxiomS5] (f ∨' ¬'f)), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.lem, have h₅ : (Γ ⊢[AxiomS5] ((f →' ⊥) →' (¬' f →' ⊥) →' (f ∨' ¬' f) →' ⊥)), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.orElim, have h₆ := (deduction_S5 Γ f ⊥).mp h₁, have h₇ := (deduction_S5 Γ (¬'f) ⊥).mp h₂, have h₈ := proof.mp _ _ Γ h₆ h₅, have h₉ := proof.mp _ _ Γ h₇ h₈, have h₁₀ := proof.mp _ _ Γ h₄ h₉, exact h₁₀, contradiction, end lemma cons_ext_nth_cons (Γ : set Formula) (n : nat) (h : consistent AxiomS5 Γ) : consistent AxiomS5 (cons_ext_nth AxiomS5 Γ n) := begin simp [cons_ext_nth], cases encodable.decode Formula n, finish, finish [cons_ext_nth, cons_ext_cons], end lemma cons_ext_chain_cons (Γ : set Formula) (n : nat) (h : consistent AxiomS5 Γ) : consistent AxiomS5 (cons_ext_chain AxiomS5 Γ n) := begin induction n, finish, finish [cons_ext_chain, cons_ext_nth_cons], end theorem finite_derivation (A : set Formula) (Γ : set Formula) (f : Formula) : ((cons_ext_max A Γ) ⊢[A] f) → ∃ n : ℕ, ((cons_ext_chain A Γ n) ⊢[A] f) := begin generalize eq : cons_ext_max A Γ = Γ', intro h, induction h, case ax : { use 0, apply proof.ax, assumption, }, case ctx : _ _ ctx { subst eq, simp [cons_ext_max] at ctx, cases ctx with n d, use n, apply proof.ctx, assumption, }, case mp : A' B' _ _ _ ih₁ ih₂ { subst eq, cases ih₁ (by refl) with n₁ d₁, cases ih₂ (by refl) with n₂ d₂, use max n₁ n₂, apply proof.mp A' B', apply proof_subset _ (cons_ext_chain A Γ n₁), apply cons_ext_chain_sub₂, finish, assumption, apply proof_subset _ (cons_ext_chain A Γ n₂), apply cons_ext_chain_sub₂, finish, assumption, }, case rn : { use 0, apply proof.rn, assumption, }, end lemma cons_ext_max_cons (Γ : set Formula) (h₁ : consistent AxiomS5 Γ) : consistent AxiomS5 (cons_ext_max AxiomS5 Γ) := begin by_contradiction h₂, simp [consistent] at *, cases finite_derivation _ _ _ h₂ with n h₃, have h₄ := cons_ext_chain_cons _ n h₁, contradiction, end lemma cons_ext_max_equicons (Γ : set Formula) : consistent AxiomS5 Γ ↔ consistent AxiomS5 (cons_ext_max AxiomS5 Γ) := begin split, apply cons_ext_max_cons, contrapose, simp [consistent], apply proof_subset, apply cons_ext_max_sub, end lemma cons_ext_max_em (A : set Formula) (Γ : set Formula) (f : Formula) : f ∈ cons_ext_max A Γ ∨ (¬'f) ∈ cons_ext_max A Γ := begin set n := encodable.encode f, have h₁ : cons_ext_chain A Γ (n + 1) ⊆ cons_ext_max A Γ := by simp [cons_ext_max, set.subset_Union], simp [cons_ext_chain, cons_ext_nth, cons_ext] at *, split_ifs at *, left, apply h₁, finish, right, apply h₁, finish, end theorem cons_ext_max_closed (Γ : set Formula) (f : Formula) (h₁ : consistent AxiomS5 Γ) (h₂ : (cons_ext_max AxiomS5 Γ) ⊢[AxiomS5] f) : f ∈ (cons_ext_max AxiomS5 Γ) := begin cases cons_ext_max_em AxiomS5 Γ f with _ h₃, assumption, have h₄ := proof.ctx _ _ h₃, have h₅ := proof.mp _ _ _ h₂ h₄, have h₆ := cons_ext_max_cons _ h₁, contradiction, end lemma cons_ext_max_der_equiv (Γ : set Formula) (f : Formula) (h : consistent AxiomS5 Γ) : (cons_ext_max AxiomS5 Γ ⊢₅ f) ↔ f ∈ cons_ext_max AxiomS5 Γ := begin split, exact cons_ext_max_closed _ _ h, exact proof.ctx _ _, end inductive CanonS5_W : set (set Formula.{u}) | mk : ∀ Γ : set Formula, consistent AxiomS5 Γ → CanonS5_W (cons_ext_max AxiomS5 Γ) def v_mem (w : CanonS5_W.{u}) (f : Formula.{u}) : Prop := f ∈ (↑w : set Formula.{u}) def unbox (w : set Formula.{u}) : set Formula.{u} := { f | □f ∈ w } def CanonS5_R (w₁ w₂ : CanonS5_W.{u}) : Prop := unbox (w₁ : set Formula.{u}) ⊆ (w₂ : set Formula.{u}) def CanonS5_v (w : CanonS5_W.{u}) (n : nat) : Prop := v_mem w #n lemma v_mem_der_equiv (w : CanonS5_W.{u}) (f : Formula.{u}) : (v_mem w f) ↔ ((w : set Formula.{u}) ⊢₅ f) := begin cases w, induction w_property, simp [v_mem], rw [cons_ext_max_der_equiv], assumption, end lemma v_mem_em (w : CanonS5_W.{u}) (f : Formula.{u}) : v_mem w f ∨ v_mem w ¬'f := begin cases w, induction w_property, apply cons_ext_max_em, end lemma CanonS5_R_refl : reflexive CanonS5_R := begin intros w₁ f h₁, cases w₁, simp [unbox] at *, induction w₁_property, apply cons_ext_max_closed, tauto, rw cons_ext_max_equicons at *, have h₂ : (_ ⊢₅ _) := proof.ctx _ _ h₁, have h₃ := proof.ax _ (cons_ext_max AxiomS5 w₁_property_Γ) (AxiomS5.ref f), exact proof.mp _ _ _ h₂ h₃, end lemma CanonS5_R_sym : symmetric CanonS5_R := begin intros w₁ w₂ h₁ f h₂, cases w₁, cases w₂, simp [CanonS5_R] at *, simp [unbox] at *, induction w₁_property, induction w₂_property, cases cons_ext_max_em AxiomS5 w₁_property_Γ □¬'□ f with h₃ h₃; rw [←cons_ext_max_der_equiv] at *; set Γ₁ := cons_ext_max AxiomS5 w₁_property_Γ, set Γ₂ := cons_ext_max AxiomS5 w₂_property_Γ, have h₄ := h₁ ((cons_ext_max_der_equiv _ _ _).mp h₃), rw [←cons_ext_max_der_equiv] at *, repeat { tauto }, have h₅ := proof.mp _ _ _ h₂ h₄, exfalso, have h₇ := cons_ext_max_cons _ w₂_property_ᾰ, contradiction, have h₄ := S5_sym_alt Γ₁ f, have h₅ := proof.mp _ _ _ h₃ h₄, assumption, end lemma CanonS5_R_trans : transitive CanonS5_R := begin intros w₁ w₂ w₃ h₁ h₂ f h₃, cases w₁, cases w₂, cases w₃, induction w₁_property, induction w₂_property, induction w₃_property, simp [CanonS5_R, v_mem, unbox] at *, rw [←cons_ext_max_der_equiv] at h₃, have h₄ : (cons_ext_max AxiomS5 w₁_property_Γ ⊢₅ (□f →' □□f)), apply proof.ax, apply AxiomS5.tran, have h₅ := proof.mp _ _ _ h₃ h₄, rw [cons_ext_max_der_equiv] at h₅, apply h₂, apply h₁, repeat {tauto}, end def CanonS5 : Model.{u} := { W := CanonS5_W.{u}, r := CanonS5_R.{u}, v := CanonS5_v.{u}} lemma CanonS5_models : ModelS5 CanonS5 := ⟨CanonS5_R_refl, ⟨CanonS5_R_sym, CanonS5_R_trans⟩⟩ lemma unbox_deriv (w : CanonS5_W.{u}) (f : Formula.{u}) : (unbox (↑w : set Formula.{u}) ⊢₅ f) → v_mem w (□f) := begin generalize eq : unbox (↑w : set Formula.{u}) = Γ', intro prf, induction prf, case ax { rw [v_mem_der_equiv], apply proof.rn, apply proof.ax, assumption, }, case ctx : A _ prf_A { rw [v_mem_der_equiv], apply proof.ctx, subst eq, simp [unbox] at *, rw [←v_mem] at *, rw [v_mem_der_equiv] at *, assumption, }, case mp : A B _ _ _ ihA ihB { subst eq, simp at *, rw [v_mem_der_equiv] at *, have h₁ : ((w : set Formula.{u}) ⊢₅ (□(A →' B) →' □A →' □B)), apply proof.ax, apply AxiomS5.axK, have h₂ := proof.mp _ _ _ ihB h₁, have h₃ := proof.mp _ _ _ ihA h₂, assumption, }, case rn { rw [v_mem_der_equiv], apply proof.rn, apply proof.rn, assumption, }, end lemma CanonS5_v_equiv (w : CanonS5_W.{u}) (f : Formula.{u}) : v_mem w f ↔ loc_true CanonS5 w f := begin induction f generalizing w, case absurd { simp [v_mem_der_equiv, loc_true], cases w, induction w_property, apply cons_ext_max_cons, assumption, }, case var { tauto, }, case impl : A B ih₁ ih₂ { specialize ih₁ w, specialize ih₂ w, simp [loc_true] at *, rw [←ih₁, ←ih₂] at *, simp [v_mem_der_equiv] at *, split, intros h₁ h₂, exact proof.mp _ _ _ h₂ h₁, intro h₁, cases v_mem_em w A with h₂ h₂; simp [v_mem_der_equiv] at *, apply append_S5, tauto, rw [←deduction_S5] at h₂, rw [←deduction_S5], have h₃ : (insert A (w : set Formula.{u}) ⊢₅ (⊥ →' B)), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.absurdElim, exact proof.mp _ _ _ h₂ h₃, }, case conj : A B ih₁ ih₂ { specialize ih₁ w, specialize ih₂ w, simp [v_mem_der_equiv, loc_true] at *, split, intro h₁, have h₂ : ((w : set Formula.{u}) ⊢₅ ((A ∧' B) →' A)), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.andElimL, have h₃ : ((w : set Formula.{u}) ⊢₅ ((A ∧' B) →' B)), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.andElimR, split, rw ←ih₁, exact proof.mp _ _ _ h₁ h₂, rw ←ih₂, exact proof.mp _ _ _ h₁ h₃, rw [←ih₁, ←ih₂], intro h₁, have h₂ : ((w : set Formula.{u}) ⊢₅ (A →' B →' (A ∧' B))), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.andIntro, have h₃ := proof.mp _ _ _ h₁.1 h₂, have h₄ := proof.mp _ _ _ h₁.2 h₃, assumption, }, case disj : A B ih₁ ih₂ { specialize ih₁ w, specialize ih₂ w, simp [loc_true] at *, rw [←ih₁, ←ih₂] at *, simp [v_mem_der_equiv] at *, split, intro h₁, cases v_mem_em w A with h₂ h₂, left, simp [v_mem_der_equiv] at *, assumption, cases v_mem_em w B with h₃ h₃, right, simp [v_mem_der_equiv] at *, assumption, simp [v_mem_der_equiv] at *, exfalso, have h₄ : ((w : set Formula.{u}) ⊢₅ (¬' A →' ¬' B →' ¬'(A ∨' B))), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.orElim, have h₅ := proof.mp _ _ _ h₂ h₄, have h₆ := proof.mp _ _ _ h₃ h₅, have h₇ := proof.mp _ _ _ h₁ h₆, cases w, induction w_property, have h₈ := cons_ext_max_cons _ w_property_ᾰ, contradiction, intro h₁, cases h₁, have h₂ : ((w : set Formula.{u}) ⊢₅ (A →' (A ∨' B))), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.orIntroL, exact proof.mp _ _ _ h₁ h₂, have h₂ : ((w : set Formula.{u}) ⊢₅ (B →' (A ∨' B))), apply proof.ax, apply AxiomS5.prop, apply AxiomPC.orIntroR, exact proof.mp _ _ _ h₁ h₂, }, case nec : A ih { split, intros h₁ w' acc, rw [←ih], apply acc, simp [*, v_mem, unbox] at *, intro h₁, cases v_mem_em w □A with h₂ h₂, assumption, exfalso, set Γ' : set Formula.{u} := insert (¬'A) (unbox (w : set Formula.{u})) with eq, have h₃ : consistent AxiomS5 Γ' := begin intro h₃, simp [eq] at h₃, simp [deduction_S5] at h₃, have h₄ := unbox_deriv _ _ h₃, rw [v_mem_der_equiv] at h₂, rw [v_mem_der_equiv] at h₄, have h₅ : ((w : set Formula.{u}) ⊢₅ □(¬' ¬' A →' A)), apply proof.rn, apply lift_S5, apply dneg_elim_PC, have h₆ : ((w : set Formula.{u}) ⊢₅ (□(¬' ¬' A →' A)) →' □ ¬' ¬' A →' □ A), apply proof.ax, apply AxiomS5.axK, have h₇ := proof.mp _ _ _ h₅ h₆, have h₈ := proof.mp _ _ _ h₄ h₇, have h₉ := proof.mp _ _ _ h₈ h₂, cases w, induction w_property, apply cons_ext_max_cons, assumption, assumption, end, set w_mem := CanonS5_W.mk.{u} Γ' h₃, simp [loc_true] at h₁, specialize h₁ ⟨cons_ext_max AxiomS5 Γ', w_mem⟩, have h₄ : CanonS5.r w ⟨cons_ext_max AxiomS5 Γ', w_mem⟩, intros f h₅, apply cons_ext_max_sub, finish, specialize h₁ h₄, rw [←ih] at h₁, simp [v_mem_der_equiv] at *, have h₅ : (cons_ext_max AxiomS5 Γ' ⊢₅ ¬' A), apply proof.ctx, apply cons_ext_max_sub, finish, have h₆ := proof.mp _ _ _ h₁ h₅, have h₇ := cons_ext_max_cons _ h₃, contradiction, }, end theorem S5_complete : complete AxiomS5.{u} ModelS5.{u} := begin simp [complete], intros Γ f, set Γ' := cons_ext_max AxiomS5 (Γ ∪ {¬' f}), by_cases h₁ : consistent AxiomS5 (Γ ∪ {¬' f}), contrapose, intros prf mdl, have h₂ : CanonS5_W Γ', fconstructor, assumption, specialize mdl CanonS5 CanonS5_models ⟨Γ', h₂⟩ _, rw [←CanonS5_v_equiv] at mdl, simp [v_mem] at mdl, rw [←cons_ext_max_der_equiv] at mdl, have h₃ : (Γ' ⊢₅ (f →' ⊥)), apply proof.ctx, apply cons_ext_max_sub, finish, have h₄ : (Γ' ⊢₅ ⊥) := proof.mp _ _ _ mdl h₃, have h₅ := cons_ext_max_cons _ h₁, contradiction, assumption, intros f' h₃, rw [←CanonS5_v_equiv], simp [v_mem], apply cons_ext_max_sub, finish, intro _, simp [consistent] at h₁, rw deduction_S5 at h₁, have h₂ := lift_S5 _ _ (dneg_elim_PC Γ f), have h₃ := proof.mp _ _ _ h₁ h₂, assumption, end
ab51f23b0d1ed9b633866af2e3faca05cfb01ac8
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/macro2.lean
0b2c8705abb078eb214e1cbcfb8712c48d2778ec
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
354
lean
notation:50 a "**" b:50 => b * a * b notation "~" a => a+a namespace Foo notation "~~" a => a+a end Foo syntax:60 term "+++" term:59 : term syntax "<||" term "||>" : term macro_rules | `($a +++ $b) => `($a + $b + $b) macro_rules | `(<|| $x ||>) => `($x +++ 1 ** 2) #check <|| 2 ||> #check <|| ~2 ||> #check <|| ~~2 ||> #check <|| <|| 3 ||> ||>
02d61e60b603507a963b7725e9f97e414720f78f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/logic/relation.lean
cd7c93e0cb3c1b84a5df7e8e87803beb597d27e8
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,627
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import tactic.basic /-! # Relation closures This file defines the reflexive, transitive, and reflexive transitive closures of relations. It also proves some basic results on definitions in core, such as `eqv_gen`. Note that this is about unbundled relations, that is terms of types of the form `α → β → Prop`. For the bundled version, see `rel`. ## Definitions * `relation.refl_gen`: Reflexive closure. `refl_gen r` relates everything `r` related, plus for all `a` it relates `a` with itself. So `refl_gen r a b ↔ r a b ∨ a = b`. * `relation.trans_gen`: Transitive closure. `trans_gen r` relates everything `r` related transitively. So `trans_gen r a b ↔ ∃ x₀ ... xₙ, r a x₀ ∧ r x₀ x₁ ∧ ... ∧ r xₙ b`. * `relation.refl_trans_gen`: Reflexive transitive closure. `refl_trans_gen r` relates everything `r` related transitively, plus for all `a` it relates `a` with itself. So `refl_trans_gen r a b ↔ (∃ x₀ ... xₙ, r a x₀ ∧ r x₀ x₁ ∧ ... ∧ r xₙ b) ∨ a = b`. It is the same as the reflexive closure of the transitive closure, or the transitive closure of the reflexive closure. In terms of rewriting systems, this means that `a` can be rewritten to `b` in a number of rewrites. * `relation.comp`: Relation composition. We provide notation `∘r`. For `r : α → β → Prop` and `s : β → γ → Prop`, `r ∘r s`relates `a : α` and `c : γ` iff there exists `b : β` that's related to both. * `relation.map`: Image of a relation under a pair of maps. For `r : α → β → Prop`, `f : α → γ`, `g : β → δ`, `map r f g` is the relation `γ → δ → Prop` relating `f a` and `g b` for all `a`, `b` related by `r`. * `relation.join`: Join of a relation. For `r : α → α → Prop`, `join r a b ↔ ∃ c, r a c ∧ r b c`. In terms of rewriting systems, this means that `a` and `b` can be rewritten to the same term. -/ variables {α β γ δ : Type*} section ne_imp variable {r : α → α → Prop} lemma is_refl.reflexive [is_refl α r] : reflexive r := λ x, is_refl.refl x /-- To show a reflexive relation `r : α → α → Prop` holds over `x y : α`, it suffices to show it holds when `x ≠ y`. -/ lemma reflexive.rel_of_ne_imp (h : reflexive r) {x y : α} (hr : x ≠ y → r x y) : r x y := begin by_cases hxy : x = y, { exact hxy ▸ h x }, { exact hr hxy } end /-- If a reflexive relation `r : α → α → Prop` holds over `x y : α`, then it holds whether or not `x ≠ y`. -/ lemma reflexive.ne_imp_iff (h : reflexive r) {x y : α} : (x ≠ y → r x y) ↔ r x y := ⟨h.rel_of_ne_imp, λ hr _, hr⟩ /-- If a reflexive relation `r : α → α → Prop` holds over `x y : α`, then it holds whether or not `x ≠ y`. Unlike `reflexive.ne_imp_iff`, this uses `[is_refl α r]`. -/ lemma reflexive_ne_imp_iff [is_refl α r] {x y : α} : (x ≠ y → r x y) ↔ r x y := is_refl.reflexive.ne_imp_iff protected lemma symmetric.iff (H : symmetric r) (x y : α) : r x y ↔ r y x := ⟨λ h, H h, λ h, H h⟩ end ne_imp section comap variables {r : β → β → Prop} lemma reflexive.comap (h : reflexive r) (f : α → β) : reflexive (r on f) := λ a, h (f a) lemma symmetric.comap (h : symmetric r) (f : α → β) : symmetric (r on f) := λ a b hab, h hab lemma transitive.comap (h : transitive r) (f : α → β) : transitive (r on f) := λ a b c hab hbc, h hab hbc lemma equivalence.comap (h : equivalence r) (f : α → β) : equivalence (r on f) := ⟨h.1.comap f, h.2.1.comap f, h.2.2.comap f⟩ end comap namespace relation section comp variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop} /-- The composition of two relations, yielding a new relation. The result relates a term of `α` and a term of `γ` if there is an intermediate term of `β` related to both. -/ def comp (r : α → β → Prop) (p : β → γ → Prop) (a : α) (c : γ) : Prop := ∃ b, r a b ∧ p b c local infixr ` ∘r ` : 80 := relation.comp lemma comp_eq : r ∘r (=) = r := funext $ λ a, funext $ λ b, propext $ iff.intro (λ ⟨c, h, eq⟩, eq ▸ h) (λ h, ⟨b, h, rfl⟩) lemma eq_comp : (=) ∘r r = r := funext $ λ a, funext $ λ b, propext $ iff.intro (λ ⟨c, eq, h⟩, eq.symm ▸ h) (λ h, ⟨a, rfl, h⟩) lemma iff_comp {r : Prop → α → Prop} : (↔) ∘r r = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, eq_comp] lemma comp_iff {r : α → Prop → Prop} : r ∘r (↔) = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, comp_eq] lemma comp_assoc : (r ∘r p) ∘r q = r ∘r p ∘r q := begin funext a d, apply propext, split, exact λ ⟨c, ⟨b, hab, hbc⟩, hcd⟩, ⟨b, hab, c, hbc, hcd⟩, exact λ ⟨b, hab, c, hbc, hcd⟩, ⟨c, ⟨b, hab, hbc⟩, hcd⟩ end lemma flip_comp : flip (r ∘r p) = (flip p) ∘r (flip r) := begin funext c a, apply propext, split, exact λ ⟨b, hab, hbc⟩, ⟨b, hbc, hab⟩, exact λ ⟨b, hbc, hab⟩, ⟨b, hab, hbc⟩ end end comp /-- The map of a relation `r` through a pair of functions pushes the relation to the codomains of the functions. The resulting relation is defined by having pairs of terms related if they have preimages related by `r`. -/ protected def map (r : α → β → Prop) (f : α → γ) (g : β → δ) : γ → δ → Prop := λ c d, ∃ a b, r a b ∧ f a = c ∧ g b = d variables {r : α → α → Prop} {a b c d : α} /-- `refl_trans_gen r`: reflexive transitive closure of `r` -/ @[mk_iff relation.refl_trans_gen.cases_tail_iff] inductive refl_trans_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_trans_gen a | tail {b c} : refl_trans_gen b → r b c → refl_trans_gen c attribute [refl] refl_trans_gen.refl /-- `refl_gen r`: reflexive closure of `r` -/ @[mk_iff] inductive refl_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_gen a | single {b} : r a b → refl_gen b /-- `trans_gen r`: transitive closure of `r` -/ @[mk_iff] inductive trans_gen (r : α → α → Prop) (a : α) : α → Prop | single {b} : r a b → trans_gen b | tail {b c} : trans_gen b → r b c → trans_gen c attribute [refl] refl_gen.refl lemma refl_gen.to_refl_trans_gen : ∀ {a b}, refl_gen r a b → refl_trans_gen r a b | a _ refl_gen.refl := by refl | a b (refl_gen.single h) := refl_trans_gen.tail refl_trans_gen.refl h namespace refl_trans_gen @[trans] lemma trans (hab : refl_trans_gen r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma single (hab : r a b) : refl_trans_gen r a b := refl.tail hab lemma head (hab : r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { exact refl.tail hab }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma symmetric (h : symmetric r) : symmetric (refl_trans_gen r) := begin intros x y h, induction h with z w a b c, { refl }, { apply relation.refl_trans_gen.head (h b) c } end lemma cases_tail : refl_trans_gen r a b → b = a ∨ (∃ c, refl_trans_gen r a c ∧ r c b) := (cases_tail_iff r a b).1 @[elab_as_eliminator] lemma head_induction_on {P : ∀ (a:α), refl_trans_gen r a b → Prop} {a : α} (h : refl_trans_gen r a b) (refl : P b refl) (head : ∀ {a c} (h' : r a c) (h : refl_trans_gen r c b), P c h → P a (h.head h')) : P a h := begin induction h generalizing P, case refl_trans_gen.refl { exact refl }, case refl_trans_gen.tail : b c hab hbc ih { apply ih, show P b _, from head hbc _ refl, show ∀ a a', r a a' → refl_trans_gen r a' b → P a' _ → P a _, from λ a a' hab hbc, head hab _ } end @[elab_as_eliminator] lemma trans_induction_on {P : ∀ {a b : α}, refl_trans_gen r a b → Prop} {a b : α} (h : refl_trans_gen r a b) (ih₁ : ∀ a, @P a a refl) (ih₂ : ∀ {a b} (h : r a b), P (single h)) (ih₃ : ∀ {a b c} (h₁ : refl_trans_gen r a b) (h₂ : refl_trans_gen r b c), P h₁ → P h₂ → P (h₁.trans h₂)) : P h := begin induction h, case refl_trans_gen.refl { exact ih₁ a }, case refl_trans_gen.tail : b c hab hbc ih { exact ih₃ hab (single hbc) ih (ih₂ hbc) } end lemma cases_head (h : refl_trans_gen r a b) : a = b ∨ (∃ c, r a c ∧ refl_trans_gen r c b) := begin induction h using relation.refl_trans_gen.head_induction_on, { left, refl }, { right, existsi _, split; assumption } end lemma cases_head_iff : refl_trans_gen r a b ↔ a = b ∨ (∃ c, r a c ∧ refl_trans_gen r c b) := begin use cases_head, rintro (rfl | ⟨c, hac, hcb⟩), { refl }, { exact head hac hcb } end lemma total_of_right_unique (U : relator.right_unique r) (ab : refl_trans_gen r a b) (ac : refl_trans_gen r a c) : refl_trans_gen r b c ∨ refl_trans_gen r c b := begin induction ab with b d ab bd IH, { exact or.inl ac }, { rcases IH with IH | IH, { rcases cases_head IH with rfl | ⟨e, be, ec⟩, { exact or.inr (single bd) }, { cases U bd be, exact or.inl ec } }, { exact or.inr (IH.tail bd) } } end end refl_trans_gen namespace trans_gen lemma to_refl {a b} (h : trans_gen r a b) : refl_trans_gen r a b := begin induction h with b h b c _ bc ab, exact refl_trans_gen.single h, exact refl_trans_gen.tail ab bc end @[trans] lemma trans_left (hab : trans_gen r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := begin induction hbc, case refl_trans_gen.refl : { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end @[trans] lemma trans (hab : trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := trans_left hab hbc.to_refl lemma head' (hab : r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := trans_left (single hab) hbc lemma tail' (hab : refl_trans_gen r a b) (hbc : r b c) : trans_gen r a c := begin induction hab generalizing c, case refl_trans_gen.refl : c hac { exact single hac }, case refl_trans_gen.tail : d b hab hdb IH { exact tail (IH hdb) hbc } end @[trans] lemma trans_right (hab : refl_trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := begin induction hbc, case trans_gen.single : c hbc { exact tail' hab hbc }, case trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma head (hab : r a b) (hbc : trans_gen r b c) : trans_gen r a c := head' hab hbc.to_refl lemma tail'_iff : trans_gen r a c ↔ ∃ b, refl_trans_gen r a b ∧ r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, tail' hab hbc⟩, cases h with _ hac b _ hab hbc, { exact ⟨_, by refl, hac⟩ }, { exact ⟨_, hab.to_refl, hbc⟩ } end lemma head'_iff : trans_gen r a c ↔ ∃ b, r a b ∧ refl_trans_gen r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, head' hab hbc⟩, induction h, case trans_gen.single : c hac { exact ⟨_, hac, by refl⟩ }, case trans_gen.tail : b c hab hbc IH { rcases IH with ⟨d, had, hdb⟩, exact ⟨_, had, hdb.tail hbc⟩ } end end trans_gen section trans_gen lemma trans_gen_eq_self (trans : transitive r) : trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h, case trans_gen.single : c hc { exact hc }, case trans_gen.tail : c d hac hcd hac { exact trans hac hcd } end, trans_gen.single⟩ lemma transitive_trans_gen : transitive (trans_gen r) := λ a b c, trans_gen.trans lemma trans_gen_idem : trans_gen (trans_gen r) = trans_gen r := trans_gen_eq_self transitive_trans_gen lemma trans_gen.lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → p (f a) (f b)) (hab : trans_gen r a b) : trans_gen p (f a) (f b) := begin induction hab, case trans_gen.single : c hac { exact trans_gen.single (h a c hac) }, case trans_gen.tail : c d hac hcd hac { exact trans_gen.tail hac (h c d hcd) } end lemma trans_gen.lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → trans_gen p (f a) (f b)) (hab : trans_gen r a b) : trans_gen p (f a) (f b) := by simpa [trans_gen_idem] using hab.lift f h lemma trans_gen.closed {p : α → α → Prop} : (∀ a b, r a b → trans_gen p a b) → trans_gen r a b → trans_gen p a b := trans_gen.lift' id end trans_gen section refl_trans_gen open refl_trans_gen lemma refl_trans_gen_iff_eq (h : ∀ b, ¬ r a b) : refl_trans_gen r a b ↔ b = a := by rw [cases_head_iff]; simp [h, eq_comm] lemma refl_trans_gen_iff_eq_or_trans_gen : refl_trans_gen r a b ↔ b = a ∨ trans_gen r a b := begin refine ⟨λ h, _, λ h, _⟩, { cases h with c _ hac hcb, { exact or.inl rfl }, { exact or.inr (trans_gen.tail' hac hcb) } }, { rcases h with rfl | h, {refl}, {exact h.to_refl} } end lemma refl_trans_gen.lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := refl_trans_gen.trans_induction_on hab (λ a, refl) (λ a b, refl_trans_gen.single ∘ h _ _) (λ a b c _ _, trans) lemma refl_trans_gen.mono {p : α → α → Prop} : (∀ a b, r a b → p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen.lift id lemma refl_trans_gen_eq_self (refl : reflexive r) (trans : transitive r) : refl_trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h with b c h₁ h₂ IH, {apply refl}, exact trans IH h₂, end, single⟩ lemma reflexive_refl_trans_gen : reflexive (refl_trans_gen r) := λ a, refl lemma transitive_refl_trans_gen : transitive (refl_trans_gen r) := λ a b c, trans lemma refl_trans_gen_idem : refl_trans_gen (refl_trans_gen r) = refl_trans_gen r := refl_trans_gen_eq_self reflexive_refl_trans_gen transitive_refl_trans_gen lemma refl_trans_gen.lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → refl_trans_gen p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := by simpa [refl_trans_gen_idem] using hab.lift f h lemma refl_trans_gen_closed {p : α → α → Prop} : (∀ a b, r a b → refl_trans_gen p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen.lift' id end refl_trans_gen /-- The join of a relation on a single type is a new relation for which pairs of terms are related if there is a third term they are both related to. For example, if `r` is a relation representing rewrites in a term rewriting system, then *confluence* is the property that if `a` rewrites to both `b` and `c`, then `join r` relates `b` and `c` (see `relation.church_rosser`). -/ def join (r : α → α → Prop) : α → α → Prop := λ a b, ∃ c, r a c ∧ r b c section join open refl_trans_gen refl_gen /-- A sufficient condition for the Church-Rosser property. -/ lemma church_rosser (h : ∀ a b c, r a b → r a c → ∃ d, refl_gen r b d ∧ refl_trans_gen r c d) (hab : refl_trans_gen r a b) (hac : refl_trans_gen r a c) : join (refl_trans_gen r) b c := begin induction hab, case refl_trans_gen.refl { exact ⟨c, hac, refl⟩ }, case refl_trans_gen.tail : d e had hde ih { clear hac had a, rcases ih with ⟨b, hdb, hcb⟩, have : ∃ a, refl_trans_gen r e a ∧ refl_gen r b a, { clear hcb, induction hdb, case refl_trans_gen.refl { exact ⟨e, refl, refl_gen.single hde⟩ }, case refl_trans_gen.tail : f b hdf hfb ih { rcases ih with ⟨a, hea, hfa⟩, cases hfa with _ hfa, { exact ⟨b, hea.tail hfb, refl_gen.refl⟩ }, { rcases h _ _ _ hfb hfa with ⟨c, hbc, hac⟩, exact ⟨c, hea.trans hac, hbc⟩ } } }, rcases this with ⟨a, hea, hba⟩, cases hba with _ hba, { exact ⟨b, hea, hcb⟩ }, { exact ⟨a, hea, hcb.tail hba⟩ } } end lemma join_of_single (h : reflexive r) (hab : r a b) : join r a b := ⟨b, hab, h b⟩ lemma symmetric_join : symmetric (join r) := λ a b ⟨c, hac, hcb⟩, ⟨c, hcb, hac⟩ lemma reflexive_join (h : reflexive r) : reflexive (join r) := λ a, ⟨a, h a, h a⟩ lemma transitive_join (ht : transitive r) (h : ∀ a b c, r a b → r a c → join r b c) : transitive (join r) := λ a b c ⟨x, hax, hbx⟩ ⟨y, hby, hcy⟩, let ⟨z, hxz, hyz⟩ := h b x y hbx hby in ⟨z, ht hax hxz, ht hcy hyz⟩ lemma equivalence_join (hr : reflexive r) (ht : transitive r) (h : ∀ a b c, r a b → r a c → join r b c) : equivalence (join r) := ⟨reflexive_join hr, symmetric_join, transitive_join ht h⟩ lemma equivalence_join_refl_trans_gen (h : ∀ a b c, r a b → r a c → ∃ d, refl_gen r b d ∧ refl_trans_gen r c d) : equivalence (join (refl_trans_gen r)) := equivalence_join reflexive_refl_trans_gen transitive_refl_trans_gen (λ a b c, church_rosser h) lemma join_of_equivalence {r' : α → α → Prop} (hr : equivalence r) (h : ∀ a b, r' a b → r a b) : join r' a b → r a b | ⟨c, hac, hbc⟩ := hr.2.2 (h _ _ hac) (hr.2.1 $ h _ _ hbc) lemma refl_trans_gen_of_transitive_reflexive {r' : α → α → Prop} (hr : reflexive r) (ht : transitive r) (h : ∀ a b, r' a b → r a b) (h' : refl_trans_gen r' a b) : r a b := begin induction h' with b c hab hbc ih, { exact hr _ }, { exact ht ih (h _ _ hbc) } end lemma refl_trans_gen_of_equivalence {r' : α → α → Prop} (hr : equivalence r) : (∀ a b, r' a b → r a b) → refl_trans_gen r' a b → r a b := refl_trans_gen_of_transitive_reflexive hr.1 hr.2.2 end join end relation section eqv_gen variables {r : α → α → Prop} {a b : α} lemma equivalence.eqv_gen_iff (h : equivalence r) : eqv_gen r a b ↔ r a b := iff.intro begin intro h, induction h, case eqv_gen.rel { assumption }, case eqv_gen.refl { exact h.1 _ }, case eqv_gen.symm { apply h.2.1, assumption }, case eqv_gen.trans : a b c _ _ hab hbc { exact h.2.2 hab hbc } end (eqv_gen.rel a b) lemma equivalence.eqv_gen_eq (h : equivalence r) : eqv_gen r = r := funext $ λ _, funext $ λ _, propext $ h.eqv_gen_iff lemma eqv_gen.mono {r p : α → α → Prop} (hrp : ∀ a b, r a b → p a b) (h : eqv_gen r a b) : eqv_gen p a b := begin induction h, case eqv_gen.rel : a b h { exact eqv_gen.rel _ _ (hrp _ _ h) }, case eqv_gen.refl : { exact eqv_gen.refl _ }, case eqv_gen.symm : a b h ih { exact eqv_gen.symm _ _ ih }, case eqv_gen.trans : a b c ih1 ih2 hab hbc { exact eqv_gen.trans _ _ _ hab hbc } end end eqv_gen
71a84baaebb0faf4340cf137cd1cde83655b72d0
ebf7140a9ea507409ff4c994124fa36e79b4ae35
/src/hints/category_theory/exercise4/hint4.lean
91f2e32d9f5b7e4253b3fb11bc404672b41df52c
[]
no_license
fundou/lftcm2020
3e88d58a92755ea5dd49f19c36239c35286ecf5e
99d11bf3bcd71ffeaef0250caa08ecc46e69b55b
refs/heads/master
1,685,610,799,304
1,624,070,416,000
1,624,070,416,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,703
lean
import algebra.category.CommRing import category_theory.yoneda import data.polynomial.algebra_map noncomputable theory open category_theory open opposite open polynomial /-! It would be nice to use `polynomial.aeval`, which produces an algebra homomorphism, and then convert that use `.to_ring_hom`. However we get a mysterious error message: -/ /- def CommRing_forget_representable : Σ (R : CommRing), (forget CommRing) ≅ coyoneda.obj (op R) := ⟨CommRing.of (polynomial ℤ), { hom := { app := λ R r, (polynomial.aeval ℤ R r).to_ring_hom, }, inv := { app := λ R f, by { dsimp at f, exact f X, }, }, }⟩ -/ /-! If you turn on `set_option pp.all true` above that definition, you'll get a more detailed message: ``` synthesized type class instance is not definitionally equal to expression inferred by typing rules, synthesized @polynomial.algebra'.{0 0} int int.comm_semiring int (@comm_semiring.to_semiring.{0} int int.comm_semiring) (@algebra_int.{0} int int.ring) inferred @polynomial.algebra'.{0 0} int int.comm_semiring int (@comm_semiring.to_semiring.{0} int int.comm_semiring) (@algebra.id.{0} int int.comm_semiring) ``` The difference here is that Lean has found two different ways to consider `ℤ` as an `ℤ`-algebra, via `(@algebra_int.{0} int int.ring)` and via `(@algebra.id.{0} int int.comm_semiring)`. The elaborator is not clever enough to see these are the same, even though there is actually a `subsingleton (algebra ℤ R)` instance available. At present, the work-around I know of is to use `polynomial.eval₂_ring_hom (algebra_map ℤ R) r` instead of `(polynomial.aeval ℤ R r).to_ring_hom`. If you find something better, let me know! -/
ece08ac53fdd35f54d4f5be8860e14c9d02a44d6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/sum_two_squares.lean
863876134230f4b298767ea519793f468be1162f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
10,847
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Michael Stoll -/ import number_theory.zsqrtd.quadratic_reciprocity import tactic.linear_combination /-! # Sums of two squares > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Fermat's theorem on the sum of two squares. Every prime `p` congruent to 1 mod 4 is the sum of two squares; see `nat.prime.sq_add_sq` (which has the weaker assumption `p % 4 ≠ 3`). We also give the result that characterizes the (positive) natural numbers that are sums of two squares as those numbers `n` such that for every prime `q` congruent to 3 mod 4, the exponent of the largest power of `q` dividing `n` is even; see `nat.eq_sq_add_sq_iff`. There is an alternative characterization as the numbers of the form `a^2 * b`, where `b` is a natural number such that `-1` is a square modulo `b`; see `nat.eq_sq_add_sq_iff_eq_sq_mul`. -/ section Fermat open gaussian_int /-- **Fermat's theorem on the sum of two squares**. Every prime not congruent to 3 mod 4 is the sum of two squares. Also known as **Fermat's Christmas theorem**. -/ theorem nat.prime.sq_add_sq {p : ℕ} [fact p.prime] (hp : p % 4 ≠ 3) : ∃ a b : ℕ, a ^ 2 + b ^ 2 = p := begin apply sq_add_sq_of_nat_prime_of_not_irreducible p, rwa [principal_ideal_ring.irreducible_iff_prime, prime_iff_mod_four_eq_three_of_nat_prime p], end end Fermat /-! ### Generalities on sums of two squares -/ section general /-- The set of sums of two squares is closed under multiplication in any commutative ring. See also `sq_add_sq_mul_sq_add_sq`. -/ lemma sq_add_sq_mul {R} [comm_ring R] {a b x y u v : R} (ha : a = x ^ 2 + y ^ 2) (hb : b = u ^ 2 + v ^ 2) : ∃ r s : R, a * b = r ^ 2 + s ^ 2 := ⟨x * u - y * v, x * v + y * u, by {rw [ha, hb], ring}⟩ /-- The set of natural numbers that are sums of two squares is closed under multiplication. -/ lemma nat.sq_add_sq_mul {a b x y u v : ℕ} (ha : a = x ^ 2 + y ^ 2) (hb : b = u ^ 2 + v ^ 2) : ∃ r s : ℕ, a * b = r ^ 2 + s ^ 2 := begin zify at ha hb ⊢, obtain ⟨r, s, h⟩ := sq_add_sq_mul ha hb, refine ⟨r.nat_abs, s.nat_abs, _⟩, simpa only [int.coe_nat_abs, sq_abs], end end general /-! ### Results on when -1 is a square modulo a natural number -/ section neg_one_square /-- If `-1` is a square modulo `n` and `m` divides `n`, then `-1` is also a square modulo `m`. -/ -- This could be formulated for a general integer `a` in place of `-1`, -- but it would not directly specialize to `-1`, -- because `((-1 : ℤ) : zmod n)` is not the same as `(-1 : zmod n)`. lemma zmod.is_square_neg_one_of_dvd {m n : ℕ} (hd : m ∣ n) (hs : is_square (-1 : zmod n)) : is_square (-1 : zmod m) := begin let f : zmod n →+* zmod m := zmod.cast_hom hd _, rw [← ring_hom.map_one f, ← ring_hom.map_neg], exact hs.map f, end /-- If `-1` is a square modulo coprime natural numbers `m` and `n`, then `-1` is also a square modulo `m*n`. -/ lemma zmod.is_square_neg_one_mul {m n : ℕ} (hc : m.coprime n) (hm : is_square (-1 : zmod m)) (hn : is_square (-1 : zmod n)) : is_square (-1 : zmod (m * n)) := begin have : is_square (-1 : (zmod m) × (zmod n)), { rw show (-1 : (zmod m) × (zmod n)) = ((-1 : zmod m), (-1 : zmod n)), from rfl, obtain ⟨x, hx⟩ := hm, obtain ⟨y, hy⟩ := hn, rw [hx, hy], exact ⟨(x, y), rfl⟩, }, simpa only [ring_equiv.map_neg_one] using this.map (zmod.chinese_remainder hc).symm, end /-- If a prime `p` divides `n` such that `-1` is a square modulo `n`, then `p % 4 ≠ 3`. -/ lemma nat.prime.mod_four_ne_three_of_dvd_is_square_neg_one {p n : ℕ} (hpp : p.prime) (hp : p ∣ n) (hs : is_square (-1 : zmod n)) : p % 4 ≠ 3 := begin obtain ⟨y, h⟩ := zmod.is_square_neg_one_of_dvd hp hs, rw [← sq, eq_comm, show (-1 : zmod p) = -1 ^ 2, from by ring] at h, haveI : fact p.prime := ⟨hpp⟩, exact zmod.mod_four_ne_three_of_sq_eq_neg_sq' one_ne_zero h, end /-- If `n` is a squarefree natural number, then `-1` is a square modulo `n` if and only if `n` is not divisible by a prime `q` such that `q % 4 = 3`. -/ lemma zmod.is_square_neg_one_iff {n : ℕ} (hn : squarefree n) : is_square (-1 : zmod n) ↔ ∀ {q : ℕ}, q.prime → q ∣ n → q % 4 ≠ 3 := begin refine ⟨λ H q hqp hqd, hqp.mod_four_ne_three_of_dvd_is_square_neg_one hqd H, λ H, _⟩, induction n using induction_on_primes with p n hpp ih, { exact false.elim (hn.ne_zero rfl), }, { exact ⟨0, by simp only [fin.zero_mul, neg_eq_zero, fin.one_eq_zero_iff]⟩, }, { haveI : fact p.prime := ⟨hpp⟩, have hcp : p.coprime n, { by_contra hc, exact hpp.not_unit (hn p $ mul_dvd_mul_left p $ hpp.dvd_iff_not_coprime.mpr hc), }, have hp₁ := zmod.exists_sq_eq_neg_one_iff.mpr (H hpp (dvd_mul_right p n)), exact zmod.is_square_neg_one_mul hcp hp₁ (ih hn.of_mul_right (λ q hqp hqd, H hqp $ dvd_mul_of_dvd_right hqd _)), } end /-- If `n` is a squarefree natural number, then `-1` is a square modulo `n` if and only if `n` has no divisor `q` that is `≡ 3 mod 4`. -/ lemma zmod.is_square_neg_one_iff' {n : ℕ} (hn : squarefree n) : is_square (-1 : zmod n) ↔ ∀ {q : ℕ}, q ∣ n → q % 4 ≠ 3 := begin have help : ∀ a b : zmod 4, a ≠ 3 → b ≠ 3 → a * b ≠ 3 := by dec_trivial, rw zmod.is_square_neg_one_iff hn, refine ⟨λ H, induction_on_primes _ _ (λ p q hp hq hpq, _), λ H q hq₁, H⟩, { exact λ _, by norm_num, }, { exact λ _, by norm_num, }, { replace hp := H hp (dvd_of_mul_right_dvd hpq), replace hq := hq (dvd_of_mul_left_dvd hpq), rw [(show 3 = 3 % 4, from by norm_num), ne.def, ← zmod.nat_coe_eq_nat_coe_iff'] at hp hq ⊢, rw nat.cast_mul, exact help p q hp hq, } end /-! ### Relation to sums of two squares -/ /-- If `-1` is a square modulo the natural number `n`, then `n` is a sum of two squares. -/ lemma nat.eq_sq_add_sq_of_is_square_mod_neg_one {n : ℕ} (h : is_square (-1 : zmod n)) : ∃ x y : ℕ, n = x ^ 2 + y ^ 2 := begin induction n using induction_on_primes with p n hpp ih, { exact ⟨0, 0, rfl⟩, }, { exact ⟨0, 1, rfl⟩, }, { haveI : fact p.prime := ⟨hpp⟩, have hp : is_square (-1 : zmod p) := zmod.is_square_neg_one_of_dvd ⟨n, rfl⟩ h, obtain ⟨u, v, huv⟩ := nat.prime.sq_add_sq (zmod.exists_sq_eq_neg_one_iff.mp hp), obtain ⟨x, y, hxy⟩ := ih (zmod.is_square_neg_one_of_dvd ⟨p, mul_comm _ _⟩ h), exact nat.sq_add_sq_mul huv.symm hxy, } end /-- If the integer `n` is a sum of two squares of coprime integers, then `-1` is a square modulo `n`. -/ lemma zmod.is_square_neg_one_of_eq_sq_add_sq_of_is_coprime {n x y : ℤ} (h : n = x ^ 2 + y ^ 2) (hc : is_coprime x y) : is_square (-1 : zmod n.nat_abs) := begin obtain ⟨u, v, huv⟩ : is_coprime x n, { have hc2 : is_coprime (x ^ 2) (y ^ 2) := hc.pow, rw show y ^ 2 = n + (-1) * x ^ 2, from by {rw h, ring} at hc2, exact (is_coprime.pow_left_iff zero_lt_two).mp hc2.of_add_mul_right_right, }, have H : (u * y) * (u * y) - (-1) = n * (-v ^ 2 * n + u ^ 2 + 2 * v) := by linear_combination -u ^ 2 * h + (n * v - u * x - 1) * huv, refine ⟨u * y, _⟩, norm_cast, rw (by push_cast : (-1 : zmod n.nat_abs) = (-1 : ℤ)), exact (zmod.int_coe_eq_int_coe_iff_dvd_sub _ _ _).mpr (int.nat_abs_dvd.mpr ⟨_, H⟩), end /-- If the natural number `n` is a sum of two squares of coprime natural numbers, then `-1` is a square modulo `n`. -/ lemma zmod.is_square_neg_one_of_eq_sq_add_sq_of_coprime {n x y : ℕ} (h : n = x ^ 2 + y ^ 2) (hc : x.coprime y) : is_square (-1 : zmod n) := begin zify at *, exact zmod.is_square_neg_one_of_eq_sq_add_sq_of_is_coprime h hc.is_coprime, end /-- A natural number `n` is a sum of two squares if and only if `n = a^2 * b` with natural numbers `a` and `b` such that `-1` is a square modulo `b`. -/ lemma nat.eq_sq_add_sq_iff_eq_sq_mul {n : ℕ} : (∃ x y : ℕ, n = x ^ 2 + y ^ 2) ↔ ∃ a b : ℕ, n = a ^ 2 * b ∧ is_square (-1 : zmod b) := begin split, { rintros ⟨x, y, h⟩, by_cases hxy : x = 0 ∧ y = 0, { exact ⟨0, 1, by rw [h, hxy.1, hxy.2, zero_pow zero_lt_two, add_zero, zero_mul], ⟨0, by rw [zero_mul, neg_eq_zero, fin.one_eq_zero_iff]⟩⟩, }, { have hg := nat.pos_of_ne_zero (mt nat.gcd_eq_zero_iff.mp hxy), obtain ⟨g, x₁, y₁, h₁, h₂, h₃, h₄⟩ := nat.exists_coprime' hg, exact ⟨g, x₁ ^ 2 + y₁ ^ 2, by {rw [h, h₃, h₄], ring}, zmod.is_square_neg_one_of_eq_sq_add_sq_of_coprime rfl h₂⟩, } }, { rintros ⟨a, b, h₁, h₂⟩, obtain ⟨x', y', h⟩ := nat.eq_sq_add_sq_of_is_square_mod_neg_one h₂, exact ⟨a * x', a * y', by {rw [h₁, h], ring}⟩, } end end neg_one_square /-! ### Characterization in terms of the prime factorization -/ section main /-- A (positive) natural number `n` is a sum of two squares if and only if the exponent of every prime `q` such that `q % 4 = 3` in the prime factorization of `n` is even. (The assumption `0 < n` is not present, since for `n = 0`, both sides are satisfied; the right hand side holds, since `padic_val_nat q 0 = 0` by definition.) -/ lemma nat.eq_sq_add_sq_iff {n : ℕ} : (∃ x y : ℕ, n = x ^ 2 + y ^ 2) ↔ ∀ {q : ℕ}, q.prime → q % 4 = 3 → even (padic_val_nat q n) := begin rcases n.eq_zero_or_pos with rfl | hn₀, { exact ⟨λ H q hq h, (@padic_val_nat.zero q).symm ▸ even_zero, λ H, ⟨0, 0, rfl⟩⟩, }, -- now `0 < n` rw nat.eq_sq_add_sq_iff_eq_sq_mul, refine ⟨λ H q hq h, _, λ H, _⟩, { obtain ⟨a, b, h₁, h₂⟩ := H, have hqb := padic_val_nat.eq_zero_of_not_dvd (λ hf, (hq.mod_four_ne_three_of_dvd_is_square_neg_one hf h₂) h), have hab : a ^ 2 * b ≠ 0 := h₁ ▸ hn₀.ne', have ha₂ := left_ne_zero_of_mul hab, have ha := mt sq_eq_zero_iff.mpr ha₂, have hb := right_ne_zero_of_mul hab, haveI hqi : fact q.prime := ⟨hq⟩, simp_rw [h₁, padic_val_nat.mul ha₂ hb, padic_val_nat.pow 2 ha, hqb, add_zero], exact even_two_mul _, }, { obtain ⟨b, a, hb₀, ha₀, hab, hb⟩ := nat.sq_mul_squarefree_of_pos hn₀, refine ⟨a, b, hab.symm, (zmod.is_square_neg_one_iff hb).mpr (λ q hqp hqb hq4, _)⟩, refine nat.odd_iff_not_even.mp _ (H hqp hq4), have hqb' : padic_val_nat q b = 1 := b.factorization_def hqp ▸ le_antisymm (nat.squarefree.factorization_le_one _ hb) ((hqp.dvd_iff_one_le_factorization hb₀.ne').mp hqb), haveI hqi : fact q.prime := ⟨hqp⟩, simp_rw [← hab, padic_val_nat.mul (pow_ne_zero 2 ha₀.ne') hb₀.ne', hqb', padic_val_nat.pow 2 ha₀.ne'], exact odd_two_mul_add_one _, } end end main
ccbf61411eaea8a5055ac34e02a5fb3e8c7c3df9
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/topology/algebra/monoid.lean
7439dce31e13bbca9e8b3d237418e7e3cac8a126
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
5,753
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Theory of topological monoids. TODO: generalize `topological_monoid` and `topological_add_monoid` to semigroups, or add a type class `topological_operator α (*)`. -/ import topology.constructions import algebra.pi_instances open classical set lattice filter topological_space local attribute [instance] classical.prop_decidable universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section topological_monoid /-- A topological monoid is a monoid in which the multiplication is continuous as a function `α × α → α`. -/ class topological_monoid (α : Type u) [topological_space α] [monoid α] : Prop := (continuous_mul : continuous (λp:α×α, p.1 * p.2)) /-- A topological (additive) monoid is a monoid in which the addition is continuous as a function `α × α → α`. -/ class topological_add_monoid (α : Type u) [topological_space α] [add_monoid α] : Prop := (continuous_add : continuous (λp:α×α, p.1 + p.2)) attribute [to_additive topological_add_monoid] topological_monoid attribute [to_additive topological_add_monoid.mk] topological_monoid.mk attribute [to_additive topological_add_monoid.continuous_add] topological_monoid.continuous_mul section variables [topological_space α] [monoid α] [topological_monoid α] @[to_additive continuous_add'] lemma continuous_mul' : continuous (λp:α×α, p.1 * p.2) := topological_monoid.continuous_mul α @[to_additive continuous_add] lemma continuous_mul [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λx, f x * g x) := continuous_mul'.comp (hf.prod_mk hg) @[to_additive continuous_add_left] lemma continuous_mul_left (a : α) : continuous (λ b:α, a * b) := continuous_mul continuous_const continuous_id @[to_additive continuous_add_right] lemma continuous_mul_right (a : α) : continuous (λ b:α, b * a) := continuous_mul continuous_id continuous_const @[to_additive continuous_on.add] lemma continuous_on.mul [topological_space β] {f : β → α} {g : β → α} {s : set β} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, f x * g x) s := (continuous_mul'.comp_continuous_on (hf.prod hg) : _) -- @[to_additive continuous_smul] lemma continuous_pow : ∀ n : ℕ, continuous (λ a : α, a ^ n) | 0 := by simpa using continuous_const | (k+1) := show continuous (λ (a : α), a * a ^ k), from continuous_mul continuous_id (continuous_pow _) @[to_additive tendsto_add'] lemma tendsto_mul' {a b : α} : tendsto (λp:α×α, p.fst * p.snd) (nhds (a, b)) (nhds (a * b)) := continuous_iff_continuous_at.mp (topological_monoid.continuous_mul α) (a, b) @[to_additive tendsto_add] lemma tendsto_mul {f : β → α} {g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (nhds a)) (hg : tendsto g x (nhds b)) : tendsto (λx, f x * g x) x (nhds (a * b)) := tendsto.comp (by rw [←nhds_prod_eq]; exact tendsto_mul') (hf.prod_mk hg) @[to_additive tendsto_list_sum] lemma tendsto_list_prod {f : γ → β → α} {x : filter β} {a : γ → α} : ∀l:list γ, (∀c∈l, tendsto (f c) x (nhds (a c))) → tendsto (λb, (l.map (λc, f c b)).prod) x (nhds ((l.map a).prod)) | [] _ := by simp [tendsto_const_nhds] | (f :: l) h := begin simp, exact tendsto_mul (h f (list.mem_cons_self _ _)) (tendsto_list_prod l (assume c hc, h c (list.mem_cons_of_mem _ hc))) end @[to_additive continuous_list_sum] lemma continuous_list_prod [topological_space β] {f : γ → β → α} (l : list γ) (h : ∀c∈l, continuous (f c)) : continuous (λa, (l.map (λc, f c a)).prod) := continuous_iff_continuous_at.2 $ assume x, tendsto_list_prod l $ assume c hc, continuous_iff_continuous_at.1 (h c hc) x @[to_additive prod.topological_add_monoid] instance [topological_space β] [monoid β] [topological_monoid β] : topological_monoid (α × β) := ⟨continuous.prod_mk (continuous_mul (continuous_fst.comp continuous_fst) (continuous_fst.comp continuous_snd)) (continuous_mul (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd)) ⟩ attribute [instance] prod.topological_add_monoid end section variables [topological_space α] [comm_monoid α] [topological_monoid α] @[to_additive tendsto_multiset_sum] lemma tendsto_multiset_prod {f : γ → β → α} {x : filter β} {a : γ → α} (s : multiset γ) : (∀c∈s, tendsto (f c) x (nhds (a c))) → tendsto (λb, (s.map (λc, f c b)).prod) x (nhds ((s.map a).prod)) := by { rcases s with ⟨l⟩, simp, exact tendsto_list_prod l } @[to_additive tendsto_finset_sum] lemma tendsto_finset_prod {f : γ → β → α} {x : filter β} {a : γ → α} (s : finset γ) : (∀c∈s, tendsto (f c) x (nhds (a c))) → tendsto (λb, s.prod (λc, f c b)) x (nhds (s.prod a)) := tendsto_multiset_prod _ @[to_additive continuous_multiset_sum] lemma continuous_multiset_prod [topological_space β] {f : γ → β → α} (s : multiset γ) : (∀c∈s, continuous (f c)) → continuous (λa, (s.map (λc, f c a)).prod) := by { rcases s with ⟨l⟩, simp, exact continuous_list_prod l } @[to_additive continuous_finset_sum] lemma continuous_finset_prod [topological_space β] {f : γ → β → α} (s : finset γ) : (∀c∈s, continuous (f c)) → continuous (λa, s.prod (λc, f c a)) := continuous_multiset_prod _ @[to_additive is_add_submonoid.mem_nhds_zero] lemma is_submonoid.mem_nhds_one (β : set α) [is_submonoid β] (oβ : is_open β) : β ∈ nhds (1 : α) := mem_nhds_sets_iff.2 ⟨β, (by refl), oβ, is_submonoid.one_mem _⟩ end end topological_monoid
bbd15f353071f8fd9b12b00572e0f37f21cce8bf
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/set_theory/cardinal/schroeder_bernstein.lean
a1dc7eee68bc7943f1e508e22abb99a86d3d9227
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,353
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import order.fixed_points import order.zorn /-! # Schröder-Bernstein theorem, well-ordering of cardinals This file proves the Schröder-Bernstein theorem (see `schroeder_bernstein`), the well-ordering of cardinals (see `min_injective`) and the totality of their order (see `total`). ## Notes Cardinals are naturally ordered by `α ≤ β ↔ ∃ f : a → β, injective f`: * `schroeder_bernstein` states that, given injections `α → β` and `β → α`, one can get a bijection `α → β`. This corresponds to the antisymmetry of the order. * The order is also well-founded: any nonempty set of cardinals has a minimal element. `min_injective` states that by saying that there exists an element of the set that injects into all others. Cardinals are defined and further developed in the file `set_theory.cardinal`. -/ open set function open_locale classical universes u v namespace function namespace embedding section antisymm variables {α : Type u} {β : Type v} /-- **The Schröder-Bernstein Theorem**: Given injections `α → β` and `β → α`, we can get a bijection `α → β`. -/ theorem schroeder_bernstein {f : α → β} {g : β → α} (hf : function.injective f) (hg : function.injective g) : ∃ h : α → β, bijective h := begin casesI is_empty_or_nonempty β with hβ hβ, { haveI : is_empty α, from function.is_empty f, exact ⟨_, ((equiv.equiv_empty α).trans (equiv.equiv_empty β).symm).bijective⟩ }, set F : set α →o set α := { to_fun := λ s, (g '' (f '' s)ᶜ)ᶜ, monotone' := λ s t hst, compl_subset_compl.mpr $ image_subset _ $ compl_subset_compl.mpr $ image_subset _ hst }, set s : set α := F.lfp, have hs : (g '' (f '' s)ᶜ)ᶜ = s, from F.map_lfp, have hns : g '' (f '' s)ᶜ = sᶜ, from compl_injective (by simp [hs]), set g' := inv_fun g, have g'g : left_inverse g' g, from left_inverse_inv_fun hg, have hg'ns : g' '' sᶜ = (f '' s)ᶜ, by rw [← hns, g'g.image_image], set h : α → β := s.piecewise f g', have : surjective h, by rw [← range_iff_surjective, range_piecewise, hg'ns, union_compl_self], have : injective h, { refine (injective_piecewise_iff _).2 ⟨hf.inj_on _, _, _⟩, { intros x hx y hy hxy, obtain ⟨x', hx', rfl⟩ : x ∈ g '' (f '' s)ᶜ, by rwa hns, obtain ⟨y', hy', rfl⟩ : y ∈ g '' (f '' s)ᶜ, by rwa hns, rw [g'g _, g'g _] at hxy, rw hxy }, { intros x hx y hy hxy, obtain ⟨y', hy', rfl⟩ : y ∈ g '' (f '' s)ᶜ, by rwa hns, rw [g'g _] at hxy, exact hy' ⟨x, hx, hxy⟩ } }, exact ⟨h, ‹injective h›, ‹surjective h›⟩ end /-- **The Schröder-Bernstein Theorem**: Given embeddings `α ↪ β` and `β ↪ α`, there exists an equivalence `α ≃ β`. -/ theorem antisymm : (α ↪ β) → (β ↪ α) → nonempty (α ≃ β) | ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ := let ⟨f, hf⟩ := schroeder_bernstein h₁ h₂ in ⟨equiv.of_bijective f hf⟩ end antisymm section wo parameters {ι : Type u} (β : ι → Type v) @[reducible] private def sets := {s : set (∀ i, β i) | ∀ (x ∈ s) (y ∈ s) i, (x : ∀ i, β i) i = y i → x = y} /-- The cardinals are well-ordered. We express it here by the fact that in any set of cardinals there is an element that injects into the others. See `cardinal.linear_order` for (one of) the lattice instances. -/ theorem min_injective [I : nonempty ι] : ∃ i, nonempty (∀ j, β i ↪ β j) := let ⟨s, hs, ms⟩ := show ∃ s ∈ sets, ∀ a ∈ sets, s ⊆ a → a = s, from zorn_subset sets (λ c hc hcc, ⟨⋃₀ c, λ x ⟨p, hpc, hxp⟩ y ⟨q, hqc, hyq⟩ i hi, (hcc.total hpc hqc).elim (λ h, hc hqc x (h hxp) y hyq i hi) (λ h, hc hpc x hxp y (h hyq) i hi), λ _, subset_sUnion_of_mem⟩) in let ⟨i, e⟩ := show ∃ i, ∀ y, ∃ x ∈ s, (x : ∀ i, β i) i = y, from classical.by_contradiction $ λ h, have h : ∀ i, ∃ y, ∀ x ∈ s, (x : ∀ i, β i) i ≠ y, by simpa only [not_exists, not_forall] using h, let ⟨f, hf⟩ := classical.axiom_of_choice h in have f ∈ s, from have insert f s ∈ sets := λ x hx y hy, begin cases hx; cases hy, {simp [hx, hy]}, { subst x, exact λ i e, (hf i y hy e.symm).elim }, { subst y, exact λ i e, (hf i x hx e).elim }, { exact hs x hx y hy } end, ms _ this (subset_insert f s) ▸ mem_insert _ _, let ⟨i⟩ := I in hf i f this rfl in let ⟨f, hf⟩ := classical.axiom_of_choice e in ⟨i, ⟨λ j, ⟨λ a, f a j, λ a b e', let ⟨sa, ea⟩ := hf a, ⟨sb, eb⟩ := hf b in by rw [← ea, ← eb, hs _ sa _ sb _ e']⟩⟩⟩ end wo /-- The cardinals are totally ordered. See `cardinal.linear_order` for (one of) the lattice instance. -/ theorem total (α : Type u) (β : Type v) : nonempty (α ↪ β) ∨ nonempty (β ↪ α) := match @min_injective bool (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) ⟨tt⟩ with | ⟨tt, ⟨h⟩⟩ := let ⟨f, hf⟩ := h ff in or.inl ⟨embedding.congr equiv.ulift equiv.ulift ⟨f, hf⟩⟩ | ⟨ff, ⟨h⟩⟩ := let ⟨f, hf⟩ := h tt in or.inr ⟨embedding.congr equiv.ulift equiv.ulift ⟨f, hf⟩⟩ end end embedding end function
dde9818091d609d7fee1f06214ce294d39abfa96
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/consumePPHint.lean
0ea876dd48c5c967d138b8d39084da4db41270da
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
252
lean
opaque p : Nat → Prop opaque q : Nat → Prop theorem p_of_q : q x → p x := sorry theorem pletfun : p (let_fun x := 0; x + 1) := by -- ⊢ p (let_fun x := 0; x + 1) apply p_of_q trace_state -- `let_fun` hint should not be consumed. sorry
c38f26cb718fc10267696ca636578b52305f64c5
590f94277ab689acdc713c44e3bbca2e012fc074
/Sequent Calculus (Lean)/src/leftRules.lean
fa1ef3a2d3f21027948fa0eb846d59409d218b69
[]
no_license
Bpalkmim/iALC
bd3f882ad942c876d65c2d33cb50a36b2f8e5d16
9c2982ae916d01d9ebab9d58e0842292ed974876
refs/heads/master
1,689,527,062,560
1,631,502,537,000
1,631,502,537,000
108,029,498
0
0
null
null
null
null
UTF-8
Lean
false
false
2,123
lean
-- Regras do lado esquerdo do sequente de SCiALC. -- Autor: Bernardo Alkmim -- bpalkmim@gmail.com import .basics namespace leftRulesSCiALC open iALCbasics -- Subjunção constant subj_l {Δ1 Δ2 : list Formula} {α β δ : Formula} : Proof (Sequent Δ1 α) → Proof (Sequent (β :: Δ2) δ) → Proof (Sequent ((Formula.subj α β :: Δ1) ++ Δ2) δ) -- Subjunção aditiva constant subj_l_add {Δ : list Formula} {α β δ : Formula} : Proof (Sequent Δ α) → Proof (Sequent (β :: Δ) δ) → Proof (Sequent (Formula.subj α β :: Δ) δ) -- Subjunção (com nominals, aditiva) constant subj_l_n_add {Δ : list Formula} {α β δ : Formula} (X : Nominal) : Proof (Sequent Δ (Formula.elemOf X α)) → Proof (Sequent (Formula.elemOf X β :: Δ) δ) → Proof (Sequent (Formula.elemOf X (Formula.subj α β) :: Δ) δ) -- Conjunção constant conj_l {Δ : list Formula} {α β : Formula} {δ : Formula} : Proof (Sequent (α :: (β :: Δ)) δ) → Proof (Sequent (Formula.conj α β :: Δ) δ) -- Disjunção constant disj_l {Δ : list Formula} {α β : Formula} {δ : Formula} : Proof (Sequent (α :: Δ) δ) → Proof (Sequent (β :: Δ) δ) → Proof (Sequent (Formula.disj α β :: Δ) δ) -- Disjunção (com nominals) constant disj_l_n {Δ : list Formula} {α β : Formula} {δ : Formula} (X : Nominal) : Proof (Sequent (Formula.elemOf X α :: Δ) δ) → Proof (Sequent (Formula.elemOf X β :: Δ) δ) → Proof (Sequent (Formula.elemOf X (Formula.disj α β) :: Δ) δ) -- Restrição universal constant forall_l {Δ : list Formula} {R : Role} {X Y : Nominal} {α : Formula} {δ : Formula} : Proof (Sequent (Formula.elemOf Y α :: (Formula.relation R X Y :: (Formula.elemOf X (Formula.univ R α) :: Δ))) δ) → Proof (Sequent (Formula.relation R X Y :: (Formula.elemOf X (Formula.univ R α) :: Δ)) δ) -- Restrição existencial constant exists_l {Δ : list Formula} {R : Role} {X Y : Nominal} {α : Formula} {δ : Formula} : Proof (Sequent (Formula.elemOf Y α :: (Formula.relation R X Y :: Δ)) δ) → Proof (Sequent (Formula.elemOf X (Formula.exis R α) :: Δ) δ) end leftRulesSCiALC
6d87b3cf93b3442d08fdc25920cdd4d7102a65c9
54f4ad05b219d444b709f56c2f619dd87d14ec29
/my_project/src/love06_monads_demo.lean
996c47f7a29ad0aabe4bba0b3ffbb08ceed709c1
[]
no_license
yizhou7/learning-lean
8efcf838c7276e235a81bd291f467fa43ce56e0a
91fb366c624df6e56e19555b2e482ce767cd8224
refs/heads/master
1,675,649,087,737
1,609,022,281,000
1,609,022,281,000
272,072,779
0
0
null
null
null
null
UTF-8
Lean
false
false
11,555
lean
import .lovelib /-! # LoVe Demo 6: Monads We take a look at an important functional programming abstraction: monads. Monads generalize computation with side effects. Haskell has shown that monads can be used very successful to write imperative programs. For us, they are interesting in three ways: * They are a useful concept in their own right. * They provide a nice example of axiomatic reasoning. * They are useful for programming Lean itself (metaprogramming). -/ set_option pp.beta true namespace LoVe /-! ## Introductory Example Consider the following programming task: Implement a function `sum_2_5_7 ns` that sums up the second, fifth, and seventh items of a list `ns` of natural numbers. Use `option ℕ` for the result so that if the list has fewer than seven elements, you can return `option.none`. A straightforward solution follows: -/ def sum_2_5_7 (ns : list ℕ) : option ℕ := match list.nth ns 1 with | option.none := option.none | option.some n2 := match list.nth ns 4 with | option.none := option.none | option.some n5 := match list.nth ns 6 with | option.none := option.none | option.some n7 := option.some (n2 + n5 + n7) end end end /-! The code is ugly, because of all the pattern matching on options. We can put all the ugliness in one function, which we call `bind_opt`: -/ def bind_opt {α : Type} {β : Type} : option α → (α → option β) → option β | option.none _ := option.none | (option.some a) f := f a def sum_2_5_7₂ (ns : list ℕ) : option ℕ := bind_opt (list.nth ns 1) (λn2, bind_opt (list.nth ns 4) (λn5, bind_opt (list.nth ns 6) (λn7, option.some (n2 + n5 + n7)))) /-! Instead of defining `bind_opt` ourselves, we can use Lean's predefined general `bind` operation. We can also use `pure` instead of `option.some`: -/ #check @bind def sum_2_5_7₃ (ns : list ℕ) : option ℕ := bind (list.nth ns 1) (λn2, bind (list.nth ns 4) (λn5, bind (list.nth ns 6) (λn7, pure (n2 + n5 + n7)))) /-! Syntactic sugar: `ma >>= f` := `bind ma f` -/ #check (>>=) def sum_2_5_7₄ (ns : list ℕ) : option ℕ := list.nth ns 1 >>= λn2, list.nth ns 4 >>= λn5, list.nth ns 6 >>= λn7, pure (n2 + n5 + n7) /-! Syntactic sugar: `do a ← ma, t` := `ma >>= (λa, t)` `do ma, t` := `ma >>= (λ_, t)` -/ def sum_2_5_7₅ (ns : list ℕ) : option ℕ := do n2 ← list.nth ns 1, do n5 ← list.nth ns 4, do n7 ← list.nth ns 6, pure (n2 + n5 + n7) /-! The `do`s can be combined: -/ def sum_2_5_7₆ (ns : list ℕ) : option ℕ := do n2 ← list.nth ns 1, n5 ← list.nth ns 4, n7 ← list.nth ns 6, pure (n2 + n5 + n7) /-! Although the notation has an imperative flavor, the function is a pure functional program. ## Two Operations and Three Laws The `option` type constructor is an example of a monad. In general, a __monad__ is a type constructor `m` that depends on some type parameter `α` (i.e., `m α`) equipped with two distinguished operations: `pure {α : Type} : α → m α` `bind {α β : Type} : m α → (α → m β) → m β` For `option`: `pure` := `option.some` `bind` := `bind_opt` Intuitively, we can think of a monad as a "box": * `pure` puts the data into the box. * `bind` allows us to access the data in the box and modify it (possibly even changing its type, since the result is an `m β` monad, not a `m α` monad). There is no general way to extract the data from the monad, i.e., to obtain an `α` from an `m α`. To summarize, `pure a` provides no side effect and simply provides a box containing the the value `a`, whereas `bind ma f` (also written `ma >>= f`) executes `ma`, then executes `f` with the boxed result `a` of `ma`. The option monad is only one instance among many. Type | Effect ------------ | -------------------------------------------------------------- `id α` | no effect `option α` | simple exceptions `σ → α × σ` | threading through a state of type `σ` `set α` | nondeterministic computation returning `α` values `t → α` | reading elements of type `t` (e.g., a configuration) `ℕ × α` | adjoining running time (e.g., to model algorithmic complexity) `string × α` | adjoining text output (e.g., for logging) `prob α` | probability (e.g., using random number generators) `io α` | interaction with the operating system `tactic α` | interaction with the proof assistant All of the above are type constructors `m` are parameterized by a type `α`. Some effects can be combined (e.g., `option (t → α)`). Some effects are not executable (e.g., `set α`, `prob α`). They are nonetheless useful for modeling programs abstractly in the logic. Specific monads may provide a way to extract the boxed value stored in the monad without `bind`'s requirement of putting it back in a monad. Monads have several benefits, including: * They provide the convenient and highly readable `do` notation. * They support generic operations, such as `mmap {α β : Type} : (α → m β) → list α → m (list β)`, which work uniformly across all monads. The `bind` and `pure` operations are normally required to obey three laws, called the monad laws. Pure data as the first program can be simplified away: do a' ← pure a, f a' = f a Pure data as the second program can be simplified away: do a ← x, pure a = x Nested programs `x`, `f`, `g` can be linearized using this associativity rule: do b ← do { a ← x, f a }, g b = do a ← x, b ← f a, g b ## A Type Class of Monads Monads are a mathematical structure, so we use class to add them as a type class (lecture 12). We can think of a type class as a structure that is parameterized by a type—or here, by a type constructor `m : Type → Type`. -/ @[class] structure lawful_monad (m : Type → Type) extends has_bind m, has_pure m : Type 1 := (pure_bind {α β : Type} (a : α) (f : α → m β) : (pure a >>= f) = f a) (bind_pure {α : Type} (ma : m α) : (ma >>= pure) = ma) (bind_assoc {α β γ : Type} (f : α → m β) (g : β → m γ) (ma : m α) : ((ma >>= f) >>= g) = (ma >>= (λa, f a >>= g))) #print monad #print is_lawful_monad /-! Step by step: * We are creating a structure parameterized by a unary type constructor `m`. * The structure inherits the fields, and any syntactic sugar, from structures called `has_bind` and `has_pure`, which provide the `bind` and `pure` operations on `m` and some syntactic sugar. * `Type 1` is necessary for reasons that will become clear in lecture 11. * The definition adds three fields to those already provided by `has_bind` and `has_pure`, to store the proofs of the monad laws. To instantiate this definition with a concrete monad, we must supply the type constructor `m` (e.g., `option`), `bind` and `pure` operators, and proofs of the monad laws. (Lean's actual definition of monads is more complicated.) ## Identity -/ #check id def id.pure {α : Type} : α → id α := id def id.bind {α β : Type} : id α → (α → id β) → id β | a f := f a @[instance] def id.lawful_monad : lawful_monad (@id Type) := { pure := @id.pure, bind := @id.bind, pure_bind := begin intros α β a f, refl end, bind_pure := begin intros α m, refl end, bind_assoc := begin intros α β γ f g m, refl end } /-! ## Exceptions -/ def option.pure {α : Type} : α → option α := option.some def option.bind {α β : Type} : option α → (α → option β) → option β | option.none f := option.none | (option.some a) f := f a @[instance] def option.lawful_monad : lawful_monad option := { pure := @option.pure, bind := @option.bind, pure_bind := begin intros α β a f, refl end, bind_pure := begin intros α m, cases m, { refl }, { refl } end, bind_assoc := begin intros α β γ f g m, cases m, { refl }, { refl } end } def option.throw {α : Type} : option α := option.none def option.catch {α : Type} : option α → option α → option α | option.none ma' := ma' | (option.some a) _ := option.some a @[instance] def option.has_orelse : has_orelse option := { orelse := @option.catch } /-! ## Mutable State -/ def action (σ α : Type) := σ → α × σ def action.read {σ : Type} : action σ σ | s := (s, s) def action.write {σ : Type} (s : σ) : action σ unit | _ := ((), s) def action.pure {σ α : Type} (a : α) : action σ α | s := (a, s) def action.bind {σ : Type} {α β : Type} (ma : action σ α) (f : α → action σ β) : action σ β | s := match ma s with | (a, s') := f a s' end @[instance] def action.lawful_monad {σ : Type} : lawful_monad (action σ) := { pure := @action.pure σ, bind := @action.bind σ, pure_bind := begin intros α β a f, apply funext, intro s, refl end, bind_pure := begin intros α m, apply funext, intro s, simp [action.bind], cases m s, refl end, bind_assoc := begin intros α β γ f g m, apply funext, intro s, simp [action.bind], cases m s, refl end } def diff_list : list ℕ → action ℕ (list ℕ) | [] := pure [] | (n :: ns) := do prev ← action.read, if n < prev then diff_list ns else do action.write n, ns' ← diff_list ns, pure (n :: ns') #eval diff_list [1, 2, 3, 2] 0 #eval diff_list [1, 2, 3, 2, 4, 5, 2] 0 /-! ## Nondeterminism -/ #check set def set.pure {α : Type} : α → set α | a := {a} def set.bind {α β : Type} : set α → (α → set β) → set β | A f := {b | ∃a, a ∈ A ∧ b ∈ f a} @[instance] def set.lawful_monad : lawful_monad set := { pure := @set.pure, bind := @set.bind, pure_bind := begin intros α β a f, simp [set.pure, set.bind] end, bind_pure := begin intros α m, simp [set.pure, set.bind] end, bind_assoc := begin intros α β γ f g m, simp [set.pure, set.bind], apply set.ext, simp, tautology end } /-! `tautology` performs elimination of the logical symbols `∧`, `∨`, `↔`, and `∃` in hypotheses and introduction of `∧`, `↔`, and `∃` in the conclusion, until all the emerging subgoals can be trivially proved (e.g., by `refl`). ## A Generic Algorithm: Iteration over a List -/ def mmap {m : Type → Type} [lawful_monad m] {α β : Type} (f : α → m β) : list α → m (list β) | [] := pure [] | (a :: as) := do b ← f a, bs ← mmap as, pure (b :: bs) lemma mmap_append {m : Type → Type} [lawful_monad m] {α β : Type} (f : α → m β) : ∀as as' : list α, mmap f (as ++ as') = do bs ← mmap f as, bs' ← mmap f as', pure (bs ++ bs') | [] _ := by simp [mmap, lawful_monad.bind_pure, lawful_monad.pure_bind] | (a :: as) as' := by simp [mmap, mmap_append as as', lawful_monad.pure_bind, lawful_monad.bind_assoc] def nths {α : Type} (xss : list (list α)) (n : ℕ) : option (list α) := mmap (λxs, list.nth xs n) xss #eval nths [[11, 12, 13, 14], [21, 22, 23], [31, 32, 33]] 2 end LoVe
423808a9890d7609578329d91813d995aa753eaa
05b503addd423dd68145d68b8cde5cd595d74365
/src/analysis/analytic/basic.lean
82f5cce24e57e0f72a0be4ee3af38810baccf133
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,529
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.times_cont_diff tactic.omega analysis.complex.exponential analysis.specific_limits /-! # Analytic functions A function is analytic in one dimension around `0` if it can be written as a converging power series `Σ pₙ zⁿ`. This definition can be extended to any dimension (even in infinite dimension) by requiring that `pₙ` is a continuous `n`-multilinear map. In general, `pₙ` is not unique (in two dimensions, taking `p₂ (x, y) (x', y') = x y'` or `y x'` gives the same map when applied to a vector `(x, y) (x, y)`). A way to guarantee uniqueness is to take a symmetric `pₙ`, but this is not always possible in nonzero characteristic (in characteristic 2, the previous example has no symmetric representative). Therefore, we do not insist on symmetry or uniqueness in the definition, and we only require the existence of a converging series. The general framework is important to say that the exponential map on bounded operators on a Banach space is analytic, as well as the inverse on invertible operators. ## Main definitions Let `p` be a formal multilinear series from `E` to `F`, i.e., `p n` is a multilinear map on `E^n` for `n : ℕ`. * `p.radius`: the largest `r : ennreal` such that `∥p n∥ * r^n` grows subexponentially, defined as a liminf. * `p.le_radius_of_bound`, `p.bound_of_lt_radius`, `p.geometric_bound_of_lt_radius`: relating the value of the radius with the growth of `∥p n∥ * r^n`. * `p.partial_sum n x`: the sum `∑_{i = 0}^{n-1} pᵢ xⁱ`. * `p.sum x`: the sum `∑_{i = 0}^{∞} pᵢ xⁱ`. Additionally, let `f` be a function from `E` to `F`. * `has_fpower_series_on_ball f p x r`: on the ball of center `x` with radius `r`, `f (x + y) = ∑_n pₙ yⁿ`. * `has_fpower_series_at f p x`: on some ball of center `x` with positive radius, holds `has_fpower_series_on_ball f p x r`. * `analytic_at 𝕜 f x`: there exists a power series `p` such that holds `has_fpower_series_at f p x`. We develop the basic properties of these notions, notably: * If a function admits a power series, it is continuous (see `has_fpower_series_on_ball.continuous_on` and `has_fpower_series_at.continuous_at` and `analytic_at.continuous_at`). * In a complete space, the sum of a formal power series with positive radius is well defined on the disk of convergence, see `formal_multilinear_series.has_fpower_series_on_ball`. ## Implementation details We only introduce the radius of convergence of a power series, as `p.radius`. For a power series in finitely many dimensions, there is a finer (directional, coordinate-dependent) notion, describing the polydisk of convergence. This notion is more specific, and not necessary to build the general theory. We do not define it here. -/ noncomputable theory variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] open_locale topological_space classical open filter /-! ### The radius of a formal multilinear series -/ namespace formal_multilinear_series /-- The radius of a formal multilinear series is the largest `r` such that the sum `Σ pₙ yⁿ` converges for all `∥y∥ < r`. -/ def radius (p : formal_multilinear_series 𝕜 E F) : ennreal := liminf at_top (λ n, 1/((nnnorm (p n)) ^ (1 / (n : ℝ)) : nnreal)) /--If `∥pₙ∥ rⁿ` is bounded in `n`, then the radius of `p` is at least `r`. -/ lemma le_radius_of_bound (p : formal_multilinear_series 𝕜 E F) (C : nnreal) {r : nnreal} (h : ∀ (n : ℕ), nnnorm (p n) * r^n ≤ C) : (r : ennreal) ≤ p.radius := begin have L : tendsto (λ n : ℕ, (r : ennreal) / ((C + 1)^(1/(n : ℝ)) : nnreal)) at_top (𝓝 ((r : ennreal) / ((C + 1)^(0 : ℝ) : nnreal))), { apply ennreal.tendsto.div tendsto_const_nhds, { simp }, { rw ennreal.tendsto_coe, apply tendsto_const_nhds.nnrpow (tendsto_const_div_at_top_nhds_0_nat 1), simp }, { simp } }, have A : ∀ n : ℕ , 0 < n → (r : ennreal) ≤ ((C + 1)^(1/(n : ℝ)) : nnreal) * (1 / (nnnorm (p n) ^ (1/(n:ℝ)) : nnreal)), { assume n npos, simp only [one_div_eq_inv, mul_assoc, mul_one, eq.symm ennreal.mul_div_assoc], rw [ennreal.le_div_iff_mul_le _ _, ← nnreal.pow_nat_rpow_nat_inv r npos, ← ennreal.coe_mul, ennreal.coe_le_coe, ← nnreal.mul_rpow, mul_comm], { exact nnreal.rpow_le_rpow (le_trans (h n) (le_add_right (le_refl _))) (by simp) }, { simp }, { simp } }, have B : ∀ᶠ (n : ℕ) in at_top, (r : ennreal) / ((C + 1)^(1/(n : ℝ)) : nnreal) ≤ 1 / (nnnorm (p n) ^ (1/(n:ℝ)) : nnreal), { apply eventually_at_top.2 ⟨1, λ n hn, _⟩, rw [ennreal.div_le_iff_le_mul, mul_comm], { apply A n hn }, { simp }, { simp } }, have D : liminf at_top (λ n : ℕ, (r : ennreal) / ((C + 1)^(1/(n : ℝ)) : nnreal)) ≤ p.radius := liminf_le_liminf B, rw liminf_eq_of_tendsto filter.at_top_ne_bot L at D, simpa using D end /-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` is bounded. -/ lemma bound_of_lt_radius (p : formal_multilinear_series 𝕜 E F) {r : nnreal} (h : (r : ennreal) < p.radius) : ∃ (C : nnreal), ∀ n, nnnorm (p n) * r^n ≤ C := begin obtain ⟨N, hN⟩ : ∃ (N : ℕ), ∀ n, n ≥ N → (r : ennreal) < 1 / ↑(nnnorm (p n) ^ (1 / (n : ℝ))) := eventually.exists_forall_of_at_top (eventually_lt_of_lt_liminf h), obtain ⟨D, hD⟩ : ∃D, ∀ x ∈ (↑((finset.range N.succ).image (λ i, nnnorm (p i) * r^i))), x ≤ D := finset.bdd_above _, refine ⟨max D 1, λ n, _⟩, cases le_or_lt n N with hn hn, { refine le_trans _ (le_max_left D 1), apply hD, have : n ∈ finset.range N.succ := list.mem_range.mpr (nat.lt_succ_iff.mpr hn), exact finset.mem_image_of_mem _ this }, { by_cases hpn : nnnorm (p n) = 0, { simp [hpn] }, have A : nnnorm (p n) ^ (1 / (n : ℝ)) ≠ 0, by simp [nnreal.rpow_eq_zero_iff, hpn], have B : r < (nnnorm (p n) ^ (1 / (n : ℝ)))⁻¹, { have := hN n (le_of_lt hn), rwa [ennreal.div_def, ← ennreal.coe_inv A, one_mul, ennreal.coe_lt_coe] at this }, rw [nnreal.lt_inv_iff_mul_lt A, mul_comm] at B, have : (nnnorm (p n) ^ (1 / (n : ℝ)) * r) ^ n ≤ 1 := pow_le_one n (zero_le (nnnorm (p n) ^ (1 / ↑n) * r)) (le_of_lt B), rw [mul_pow, one_div_eq_inv, nnreal.rpow_nat_inv_pow_nat _ (lt_of_le_of_lt (zero_le _) hn)] at this, exact le_trans this (le_max_right _ _) }, end /-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` tends to zero exponentially. -/ lemma geometric_bound_of_lt_radius (p : formal_multilinear_series 𝕜 E F) {r : nnreal} (h : (r : ennreal) < p.radius) : ∃ a C, a < 1 ∧ ∀ n, nnnorm (p n) * r^n ≤ C * a^n := begin obtain ⟨t, rt, tp⟩ : ∃ (t : nnreal), (r : ennreal) < t ∧ (t : ennreal) < p.radius := ennreal.lt_iff_exists_nnreal_btwn.1 h, rw ennreal.coe_lt_coe at rt, have tpos : t ≠ 0 := ne_of_gt (lt_of_le_of_lt (zero_le _) rt), obtain ⟨C, hC⟩ : ∃ (C : nnreal), ∀ n, nnnorm (p n) * t^n ≤ C := p.bound_of_lt_radius tp, refine ⟨r / t, C, nnreal.div_lt_one_of_lt rt, λ n, _⟩, calc nnnorm (p n) * r ^ n = (nnnorm (p n) * t ^ n) * (r / t) ^ n : by { field_simp [tpos], ac_refl } ... ≤ C * (r / t) ^ n : mul_le_mul_of_nonneg_right (hC n) (zero_le _) end /-- The radius of the sum of two formal series is at least the minimum of their two radii. -/ lemma min_radius_le_radius_add (p q : formal_multilinear_series 𝕜 E F) : min p.radius q.radius ≤ (p + q).radius := begin refine le_of_forall_ge_of_dense (λ r hr, _), cases r, { simpa using hr }, obtain ⟨Cp, hCp⟩ : ∃ (C : nnreal), ∀ n, nnnorm (p n) * r^n ≤ C := p.bound_of_lt_radius (lt_of_lt_of_le hr (min_le_left _ _)), obtain ⟨Cq, hCq⟩ : ∃ (C : nnreal), ∀ n, nnnorm (q n) * r^n ≤ C := q.bound_of_lt_radius (lt_of_lt_of_le hr (min_le_right _ _)), have : ∀ n, nnnorm ((p + q) n) * r^n ≤ Cp + Cq, { assume n, calc nnnorm (p n + q n) * r ^ n ≤ (nnnorm (p n) + nnnorm (q n)) * r ^ n : mul_le_mul_of_nonneg_right (norm_add_le (p n) (q n)) (zero_le (r ^ n)) ... ≤ Cp + Cq : by { rw add_mul, exact add_le_add (hCp n) (hCq n) } }, exact (p + q).le_radius_of_bound _ this end lemma radius_neg (p : formal_multilinear_series 𝕜 E F) : (-p).radius = p.radius := by simp [formal_multilinear_series.radius, nnnorm_neg] /-- Given a formal multilinear series `p` and a vector `x`, then `p.sum x` is the sum `Σ pₙ xⁿ`. A priori, it only behaves well when `∥x∥ < p.radius`. -/ protected def sum (p : formal_multilinear_series 𝕜 E F) (x : E) : F := tsum (λn:ℕ, p n (λ(i : fin n), x)) /-- Given a formal multilinear series `p` and a vector `x`, then `p.partial_sum n x` is the sum `Σ pₖ xᵏ` for `k ∈ {0,..., n-1}`. -/ def partial_sum (p : formal_multilinear_series 𝕜 E F) (n : ℕ) (x : E) : F := (finset.range n).sum (λ k, p k (λ(i : fin k), x)) /-- The partial sums of a formal multilinear series are continuous. -/ lemma partial_sum_continuous (p : formal_multilinear_series 𝕜 E F) (n : ℕ) : continuous (p.partial_sum n) := continuous_finset_sum (finset.range n) $ λ k hk, (p k).cont.comp (continuous_pi (λ i, continuous_id)) end formal_multilinear_series /-! ### Expanding a function as a power series -/ variables {f g : E → F} {p pf pg : formal_multilinear_series 𝕜 E F} {x : E} {r r' : ennreal} /-- Given a function `f : E → F` and a formal multilinear series `p`, we say that `f` has `p` as a power series on the ball of radius `r > 0` around `x` if `f (x + y) = ∑ pₙ yⁿ` for all `∥y∥ < r`. -/ structure has_fpower_series_on_ball (f : E → F) (p : formal_multilinear_series 𝕜 E F) (x : E) (r : ennreal) : Prop := (r_le : r ≤ p.radius) (r_pos : 0 < r) (has_sum : ∀ {y}, y ∈ emetric.ball (0 : E) r → has_sum (λn:ℕ, p n (λ(i : fin n), y)) (f (x + y))) /-- Given a function `f : E → F` and a formal multilinear series `p`, we say that `f` has `p` as a power series around `x` if `f (x + y) = ∑ pₙ yⁿ` for all `y` in a neighborhood of `0`. -/ def has_fpower_series_at (f : E → F) (p : formal_multilinear_series 𝕜 E F) (x : E) := ∃ r, has_fpower_series_on_ball f p x r variable (𝕜) /-- Given a function `f : E → F`, we say that `f` is analytic at `x` if it admits a convergent power series expansion around `x`. -/ def analytic_at (f : E → F) (x : E) := ∃ (p : formal_multilinear_series 𝕜 E F), has_fpower_series_at f p x variable {𝕜} lemma has_fpower_series_on_ball.has_fpower_series_at (hf : has_fpower_series_on_ball f p x r) : has_fpower_series_at f p x := ⟨r, hf⟩ lemma has_fpower_series_at.analytic_at (hf : has_fpower_series_at f p x) : analytic_at 𝕜 f x := ⟨p, hf⟩ lemma has_fpower_series_on_ball.analytic_at (hf : has_fpower_series_on_ball f p x r) : analytic_at 𝕜 f x := hf.has_fpower_series_at.analytic_at lemma has_fpower_series_on_ball.radius_pos (hf : has_fpower_series_on_ball f p x r) : 0 < p.radius := lt_of_lt_of_le hf.r_pos hf.r_le lemma has_fpower_series_at.radius_pos (hf : has_fpower_series_at f p x) : 0 < p.radius := let ⟨r, hr⟩ := hf in hr.radius_pos lemma has_fpower_series_on_ball.mono (hf : has_fpower_series_on_ball f p x r) (r'_pos : 0 < r') (hr : r' ≤ r) : has_fpower_series_on_ball f p x r' := ⟨le_trans hr hf.1, r'_pos, λ y hy, hf.has_sum (emetric.ball_subset_ball hr hy)⟩ lemma has_fpower_series_on_ball.add (hf : has_fpower_series_on_ball f pf x r) (hg : has_fpower_series_on_ball g pg x r) : has_fpower_series_on_ball (f + g) (pf + pg) x r := { r_le := le_trans (le_min_iff.2 ⟨hf.r_le, hg.r_le⟩) (pf.min_radius_le_radius_add pg), r_pos := hf.r_pos, has_sum := λ y hy, (hf.has_sum hy).add (hg.has_sum hy) } lemma has_fpower_series_at.add (hf : has_fpower_series_at f pf x) (hg : has_fpower_series_at g pg x) : has_fpower_series_at (f + g) (pf + pg) x := begin rcases hf with ⟨rf, hrf⟩, rcases hg with ⟨rg, hrg⟩, have P : 0 < min rf rg, by simp [hrf.r_pos, hrg.r_pos], exact ⟨min rf rg, (hrf.mono P (min_le_left _ _)).add (hrg.mono P (min_le_right _ _))⟩ end lemma analytic_at.add (hf : analytic_at 𝕜 f x) (hg : analytic_at 𝕜 g x) : analytic_at 𝕜 (f + g) x := let ⟨pf, hpf⟩ := hf, ⟨qf, hqf⟩ := hg in (hpf.add hqf).analytic_at lemma has_fpower_series_on_ball.neg (hf : has_fpower_series_on_ball f pf x r) : has_fpower_series_on_ball (-f) (-pf) x r := { r_le := by { rw pf.radius_neg, exact hf.r_le }, r_pos := hf.r_pos, has_sum := λ y hy, (hf.has_sum hy).neg } lemma has_fpower_series_at.neg (hf : has_fpower_series_at f pf x) : has_fpower_series_at (-f) (-pf) x := let ⟨rf, hrf⟩ := hf in hrf.neg.has_fpower_series_at lemma analytic_at.neg (hf : analytic_at 𝕜 f x) : analytic_at 𝕜 (-f) x := let ⟨pf, hpf⟩ := hf in hpf.neg.analytic_at lemma has_fpower_series_on_ball.sub (hf : has_fpower_series_on_ball f pf x r) (hg : has_fpower_series_on_ball g pg x r) : has_fpower_series_on_ball (f - g) (pf - pg) x r := hf.add hg.neg lemma has_fpower_series_at.sub (hf : has_fpower_series_at f pf x) (hg : has_fpower_series_at g pg x) : has_fpower_series_at (f - g) (pf - pg) x := hf.add hg.neg lemma analytic_at.sub (hf : analytic_at 𝕜 f x) (hg : analytic_at 𝕜 g x) : analytic_at 𝕜 (f - g) x := hf.add hg.neg lemma has_fpower_series_on_ball.coeff_zero (hf : has_fpower_series_on_ball f pf x r) (v : fin 0 → E) : pf 0 v = f x := begin have v_eq : v = (λ i, 0), by { ext i, apply fin_zero_elim i }, have zero_mem : (0 : E) ∈ emetric.ball (0 : E) r, by simp [hf.r_pos], have : ∀ i ≠ 0, pf i (λ j, 0) = 0, { assume i hi, have : 0 < i := bot_lt_iff_ne_bot.mpr hi, apply continuous_multilinear_map.map_coord_zero _ (⟨0, this⟩ : fin i), refl }, have A := has_sum_unique (hf.has_sum zero_mem) (has_sum_single _ this), simpa [v_eq] using A.symm, end lemma has_fpower_series_at.coeff_zero (hf : has_fpower_series_at f pf x) (v : fin 0 → E) : pf 0 v = f x := let ⟨rf, hrf⟩ := hf in hrf.coeff_zero v /-- If a function admits a power series expansion, then it is exponentially close to the partial sums of this power series on strict subdisks of the disk of convergence. -/ lemma has_fpower_series_on_ball.uniform_geometric_approx {r' : nnreal} (hf : has_fpower_series_on_ball f p x r) (h : (r' : ennreal) < r) : ∃ (a C : nnreal), a < 1 ∧ (∀ y ∈ metric.ball (0 : E) r', ∀ n, ∥f (x + y) - p.partial_sum n y∥ ≤ C * a ^ n) := begin obtain ⟨a, C, ha, hC⟩ : ∃ a C, a < 1 ∧ ∀ n, nnnorm (p n) * r' ^n ≤ C * a^n := p.geometric_bound_of_lt_radius (lt_of_lt_of_le h hf.r_le), refine ⟨a, C / (1 - a), ha, λ y hy n, _⟩, have yr' : ∥y∥ < r', by { rw ball_0_eq at hy, exact hy }, have : y ∈ emetric.ball (0 : E) r, { rw [emetric.mem_ball, edist_eq_coe_nnnorm], apply lt_trans _ h, rw [ennreal.coe_lt_coe, ← nnreal.coe_lt_coe], exact yr' }, simp only [nnreal.coe_sub (le_of_lt ha), nnreal.coe_sub, nnreal.coe_div, nnreal.coe_one], rw [← dist_eq_norm, dist_comm, dist_eq_norm, ← mul_div_right_comm], apply norm_sub_le_of_geometric_bound_of_has_sum ha _ (hf.has_sum this), assume n, calc ∥(p n) (λ (i : fin n), y)∥ ≤ ∥p n∥ * (finset.univ.prod (λ i : fin n, ∥y∥)) : continuous_multilinear_map.le_op_norm _ _ ... = nnnorm (p n) * (nnnorm y)^n : by simp ... ≤ nnnorm (p n) * r' ^ n : mul_le_mul_of_nonneg_left (pow_le_pow_of_le_left (nnreal.coe_nonneg _) (le_of_lt yr') _) (nnreal.coe_nonneg _) ... ≤ C * a ^ n : by exact_mod_cast hC n, end /-- If a function admits a power series expansion at `x`, then it is the uniform limit of the partial sums of this power series on strict subdisks of the disk of convergence, i.e., `f (x + y)` is the uniform limit of `p.partial_sum n y` there. -/ lemma has_fpower_series_on_ball.tendsto_uniformly_on {r' : nnreal} (hf : has_fpower_series_on_ball f p x r) (h : (r' : ennreal) < r) : tendsto_uniformly_on (λ n y, p.partial_sum n y) (λ y, f (x + y)) at_top (metric.ball (0 : E) r') := begin rcases hf.uniform_geometric_approx h with ⟨a, C, ha, hC⟩, refine metric.tendsto_uniformly_on_iff.2 (λ ε εpos, _), have L : tendsto (λ n, (C : ℝ) * a^n) at_top (𝓝 ((C : ℝ) * 0)) := tendsto_const_nhds.mul (tendsto_pow_at_top_nhds_0_of_lt_1 (a.2) ha), rw mul_zero at L, apply ((tendsto_order.1 L).2 ε εpos).mono (λ n hn, _), assume y hy, rw dist_eq_norm, exact lt_of_le_of_lt (hC y hy n) hn end /-- If a function admits a power series expansion at `x`, then it is the locally uniform limit of the partial sums of this power series on the disk of convergence, i.e., `f (x + y)` is the locally uniform limit of `p.partial_sum n y` there. -/ lemma has_fpower_series_on_ball.tendsto_locally_uniformly_on (hf : has_fpower_series_on_ball f p x r) : tendsto_locally_uniformly_on (λ n y, p.partial_sum n y) (λ y, f (x + y)) at_top (emetric.ball (0 : E) r) := begin assume u hu x hx, rcases ennreal.lt_iff_exists_nnreal_btwn.1 hx with ⟨r', xr', hr'⟩, have : emetric.ball (0 : E) r' ∈ 𝓝 x := mem_nhds_sets emetric.is_open_ball xr', refine ⟨emetric.ball (0 : E) r', mem_nhds_within_of_mem_nhds this, _⟩, simpa [metric.emetric_ball_nnreal] using hf.tendsto_uniformly_on hr' u hu end /-- If a function admits a power series expansion at `x`, then it is the uniform limit of the partial sums of this power series on strict subdisks of the disk of convergence, i.e., `f y` is the uniform limit of `p.partial_sum n (y - x)` there. -/ lemma has_fpower_series_on_ball.tendsto_uniformly_on' {r' : nnreal} (hf : has_fpower_series_on_ball f p x r) (h : (r' : ennreal) < r) : tendsto_uniformly_on (λ n y, p.partial_sum n (y - x)) f at_top (metric.ball (x : E) r') := begin convert (hf.tendsto_uniformly_on h).comp (λ y, y - x), { ext z, simp }, { ext z, simp [dist_eq_norm] } end /-- If a function admits a power series expansion at `x`, then it is the locally uniform limit of the partial sums of this power series on the disk of convergence, i.e., `f y` is the locally uniform limit of `p.partial_sum n (y - x)` there. -/ lemma has_fpower_series_on_ball.tendsto_locally_uniformly_on' (hf : has_fpower_series_on_ball f p x r) : tendsto_locally_uniformly_on (λ n y, p.partial_sum n (y - x)) f at_top (emetric.ball (x : E) r) := begin have A : continuous_on (λ (y : E), y - x) (emetric.ball (x : E) r) := (continuous_id.sub continuous_const).continuous_on, convert (hf.tendsto_locally_uniformly_on).comp (λ (y : E), y - x) _ A, { ext z, simp }, { assume z, simp [edist_eq_coe_nnnorm, edist_eq_coe_nnnorm_sub] } end /-- If a function admits a power series expansion on a disk, then it is continuous there. -/ lemma has_fpower_series_on_ball.continuous_on (hf : has_fpower_series_on_ball f p x r) : continuous_on f (emetric.ball x r) := begin apply hf.tendsto_locally_uniformly_on'.continuous_on _ at_top_ne_bot, exact λ n, ((p.partial_sum_continuous n).comp (continuous_id.sub continuous_const)).continuous_on end lemma has_fpower_series_at.continuous_at (hf : has_fpower_series_at f p x) : continuous_at f x := let ⟨r, hr⟩ := hf in hr.continuous_on.continuous_at (emetric.ball_mem_nhds x (hr.r_pos)) lemma analytic_at.continuous_at (hf : analytic_at 𝕜 f x) : continuous_at f x := let ⟨p, hp⟩ := hf in hp.continuous_at /-- In a complete space, the sum of a converging power series `p` admits `p` as a power series. This is not totally obvious as we need to check the convergence of the series. -/ lemma formal_multilinear_series.has_fpower_series_on_ball [complete_space F] (p : formal_multilinear_series 𝕜 E F) (h : 0 < p.radius) : has_fpower_series_on_ball p.sum p 0 p.radius := { r_le := le_refl _, r_pos := h, has_sum := λ y hy, begin rw zero_add, replace hy : (nnnorm y : ennreal) < p.radius, by { convert hy, exact (edist_eq_coe_nnnorm _).symm }, obtain ⟨a, C, ha, hC⟩ : ∃ a C, a < 1 ∧ ∀ n, nnnorm (p n) * (nnnorm y)^n ≤ C * a^n := p.geometric_bound_of_lt_radius hy, refine (summable_of_norm_bounded (λ n, (C : ℝ) * a ^ n) ((summable_geometric a.2 ha).mul_left _) (λ n, _)).has_sum, calc ∥(p n) (λ (i : fin n), y)∥ ≤ ∥p n∥ * (finset.univ.prod (λ i : fin n, ∥y∥)) : continuous_multilinear_map.le_op_norm _ _ ... = nnnorm (p n) * (nnnorm y)^n : by simp ... ≤ C * a ^ n : by exact_mod_cast hC n end } lemma has_fpower_series_on_ball.sum [complete_space F] (h : has_fpower_series_on_ball f p x r) {y : E} (hy : y ∈ emetric.ball (0 : E) r) : f (x + y) = p.sum y := begin have A := h.has_sum hy, have B := (p.has_fpower_series_on_ball h.radius_pos).has_sum (lt_of_lt_of_le hy h.r_le), simpa using has_sum_unique A B end /-- The sum of a converging power series is continuous in its disk of convergence. -/ lemma formal_multilinear_series.continuous_on [complete_space F] : continuous_on p.sum (emetric.ball 0 p.radius) := begin by_cases h : 0 < p.radius, { exact (p.has_fpower_series_on_ball h).continuous_on }, { simp at h, simp [h, continuous_on_empty] } end
fb3aa1a6e451e1c1eb470e1ffad7f324b67e6254
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/locally_convex/basic.lean
49ce8cc61ba98fd4b538ff475b0c6ab108e2dac7
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
15,171
lean
/- Copyright (c) 2019 Jean Lo. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jean Lo, Bhavik Mehta, Yaël Dillies -/ import analysis.convex.basic import analysis.convex.hull import analysis.normed_space.basic /-! # Local convexity > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines absorbent and balanced sets. An absorbent set is one that "surrounds" the origin. The idea is made precise by requiring that any point belongs to all large enough scalings of the set. This is the vector world analog of a topological neighborhood of the origin. A balanced set is one that is everywhere around the origin. This means that `a • s ⊆ s` for all `a` of norm less than `1`. ## Main declarations For a module over a normed ring: * `absorbs`: A set `s` absorbs a set `t` if all large scalings of `s` contain `t`. * `absorbent`: A set `s` is absorbent if every point eventually belongs to all large scalings of `s`. * `balanced`: A set `s` is balanced if `a • s ⊆ s` for all `a` of norm less than `1`. ## References * [H. H. Schaefer, *Topological Vector Spaces*][schaefer1966] ## Tags absorbent, balanced, locally convex, LCTVS -/ open set open_locale pointwise topology variables {𝕜 𝕝 E : Type*} {ι : Sort*} {κ : ι → Sort*} section semi_normed_ring variables [semi_normed_ring 𝕜] section has_smul variables (𝕜) [has_smul 𝕜 E] /-- A set `A` absorbs another set `B` if `B` is contained in all scalings of `A` by elements of sufficiently large norm. -/ def absorbs (A B : set E) := ∃ r, 0 < r ∧ ∀ a : 𝕜, r ≤ ‖a‖ → B ⊆ a • A variables {𝕜} {s t u v A B : set E} @[simp] lemma absorbs_empty {s : set E}: absorbs 𝕜 s (∅ : set E) := ⟨1, one_pos, λ a ha, set.empty_subset _⟩ lemma absorbs.mono (hs : absorbs 𝕜 s u) (hst : s ⊆ t) (hvu : v ⊆ u) : absorbs 𝕜 t v := let ⟨r, hr, h⟩ := hs in ⟨r, hr, λ a ha, hvu.trans $ (h _ ha).trans $ smul_set_mono hst⟩ lemma absorbs.mono_left (hs : absorbs 𝕜 s u) (h : s ⊆ t) : absorbs 𝕜 t u := hs.mono h subset.rfl lemma absorbs.mono_right (hs : absorbs 𝕜 s u) (h : v ⊆ u) : absorbs 𝕜 s v := hs.mono subset.rfl h lemma absorbs.union (hu : absorbs 𝕜 s u) (hv : absorbs 𝕜 s v) : absorbs 𝕜 s (u ∪ v) := begin obtain ⟨a, ha, hu⟩ := hu, obtain ⟨b, hb, hv⟩ := hv, exact ⟨max a b, lt_max_of_lt_left ha, λ c hc, union_subset (hu _ $ le_of_max_le_left hc) (hv _ $ le_of_max_le_right hc)⟩, end @[simp] lemma absorbs_union : absorbs 𝕜 s (u ∪ v) ↔ absorbs 𝕜 s u ∧ absorbs 𝕜 s v := ⟨λ h, ⟨h.mono_right $ subset_union_left _ _, h.mono_right $ subset_union_right _ _⟩, λ h, h.1.union h.2⟩ lemma absorbs_Union_finset {ι : Type*} {t : finset ι} {f : ι → set E} : absorbs 𝕜 s (⋃ i ∈ t, f i) ↔ ∀ i ∈ t, absorbs 𝕜 s (f i) := begin classical, induction t using finset.induction_on with i t ht hi, { simp only [finset.not_mem_empty, set.Union_false, set.Union_empty, absorbs_empty, is_empty.forall_iff, implies_true_iff] }, rw [finset.set_bUnion_insert, absorbs_union, hi], split; intro h, { refine λ _ hi', (finset.mem_insert.mp hi').elim _ (h.2 _), exact (λ hi'', by { rw hi'', exact h.1 }) }, exact ⟨h i (finset.mem_insert_self i t), λ i' hi', h i' (finset.mem_insert_of_mem hi')⟩, end lemma set.finite.absorbs_Union {ι : Type*} {s : set E} {t : set ι} {f : ι → set E} (hi : t.finite) : absorbs 𝕜 s (⋃ i ∈ t, f i) ↔ ∀ i ∈ t, absorbs 𝕜 s (f i) := begin lift t to finset ι using hi, simp only [finset.mem_coe], exact absorbs_Union_finset, end variables (𝕜) /-- A set is absorbent if it absorbs every singleton. -/ def absorbent (A : set E) := ∀ x, ∃ r, 0 < r ∧ ∀ a : 𝕜, r ≤ ‖a‖ → x ∈ a • A variables {𝕜} lemma absorbent.subset (hA : absorbent 𝕜 A) (hAB : A ⊆ B) : absorbent 𝕜 B := begin refine forall_imp (λ x, _) hA, exact Exists.imp (λ r, and.imp_right $ forall₂_imp $ λ a ha hx, set.smul_set_mono hAB hx), end lemma absorbent_iff_forall_absorbs_singleton : absorbent 𝕜 A ↔ ∀ x, absorbs 𝕜 A {x} := by simp_rw [absorbs, absorbent, singleton_subset_iff] lemma absorbent.absorbs (hs : absorbent 𝕜 s) {x : E} : absorbs 𝕜 s {x} := absorbent_iff_forall_absorbs_singleton.1 hs _ lemma absorbent_iff_nonneg_lt : absorbent 𝕜 A ↔ ∀ x, ∃ r, 0 ≤ r ∧ ∀ ⦃a : 𝕜⦄, r < ‖a‖ → x ∈ a • A := forall_congr $ λ x, ⟨λ ⟨r, hr, hx⟩, ⟨r, hr.le, λ a ha, hx a ha.le⟩, λ ⟨r, hr, hx⟩, ⟨r + 1, add_pos_of_nonneg_of_pos hr zero_lt_one, λ a ha, hx ((lt_add_of_pos_right r zero_lt_one).trans_le ha)⟩⟩ lemma absorbent.absorbs_finite {s : set E} (hs : absorbent 𝕜 s) {v : set E} (hv : v.finite) : absorbs 𝕜 s v := begin rw ←set.bUnion_of_singleton v, exact hv.absorbs_Union.mpr (λ _ _, hs.absorbs), end variables (𝕜) /-- A set `A` is balanced if `a • A` is contained in `A` whenever `a` has norm at most `1`. -/ def balanced (A : set E) := ∀ a : 𝕜, ‖a‖ ≤ 1 → a • A ⊆ A variables {𝕜} lemma balanced_iff_smul_mem : balanced 𝕜 s ↔ ∀ ⦃a : 𝕜⦄, ‖a‖ ≤ 1 → ∀ ⦃x : E⦄, x ∈ s → a • x ∈ s := forall₂_congr $ λ a ha, smul_set_subset_iff alias balanced_iff_smul_mem ↔ balanced.smul_mem _ @[simp] lemma balanced_empty : balanced 𝕜 (∅ : set E) := λ _ _, by { rw smul_set_empty } @[simp] lemma balanced_univ : balanced 𝕜 (univ : set E) := λ a ha, subset_univ _ lemma balanced.union (hA : balanced 𝕜 A) (hB : balanced 𝕜 B) : balanced 𝕜 (A ∪ B) := λ a ha, smul_set_union.subset.trans $ union_subset_union (hA _ ha) $ hB _ ha lemma balanced.inter (hA : balanced 𝕜 A) (hB : balanced 𝕜 B) : balanced 𝕜 (A ∩ B) := λ a ha, smul_set_inter_subset.trans $ inter_subset_inter (hA _ ha) $ hB _ ha lemma balanced_Union {f : ι → set E} (h : ∀ i, balanced 𝕜 (f i)) : balanced 𝕜 (⋃ i, f i) := λ a ha, (smul_set_Union _ _).subset.trans $ Union_mono $ λ _, h _ _ ha lemma balanced_Union₂ {f : Π i, κ i → set E} (h : ∀ i j, balanced 𝕜 (f i j)) : balanced 𝕜 (⋃ i j, f i j) := balanced_Union $ λ _, balanced_Union $ h _ lemma balanced_Inter {f : ι → set E} (h : ∀ i, balanced 𝕜 (f i)) : balanced 𝕜 (⋂ i, f i) := λ a ha, (smul_set_Inter_subset _ _).trans $ Inter_mono $ λ _, h _ _ ha lemma balanced_Inter₂ {f : Π i, κ i → set E} (h : ∀ i j, balanced 𝕜 (f i j)) : balanced 𝕜 (⋂ i j, f i j) := balanced_Inter $ λ _, balanced_Inter $ h _ variables [has_smul 𝕝 E] [smul_comm_class 𝕜 𝕝 E] lemma balanced.smul (a : 𝕝) (hs : balanced 𝕜 s) : balanced 𝕜 (a • s) := λ b hb, (smul_comm _ _ _).subset.trans $ smul_set_mono $ hs _ hb end has_smul section module variables [add_comm_group E] [module 𝕜 E] {s s₁ s₂ t t₁ t₂ : set E} lemma absorbs.neg : absorbs 𝕜 s t → absorbs 𝕜 (-s) (-t) := Exists.imp $ λ r, and.imp_right $ forall₂_imp $ λ _ _ h, (neg_subset_neg.2 h).trans (smul_set_neg _ _).superset lemma balanced.neg : balanced 𝕜 s → balanced 𝕜 (-s) := forall₂_imp $ λ _ _ h, (smul_set_neg _ _).subset.trans $ neg_subset_neg.2 h lemma absorbs.add : absorbs 𝕜 s₁ t₁ → absorbs 𝕜 s₂ t₂ → absorbs 𝕜 (s₁ + s₂) (t₁ + t₂) := λ ⟨r₁, hr₁, h₁⟩ ⟨r₂, hr₂, h₂⟩, ⟨max r₁ r₂, lt_max_of_lt_left hr₁, λ a ha, (add_subset_add (h₁ _ $ le_of_max_le_left ha) $ h₂ _ $ le_of_max_le_right ha).trans (smul_add _ _ _).superset⟩ lemma balanced.add (hs : balanced 𝕜 s) (ht : balanced 𝕜 t) : balanced 𝕜 (s + t) := λ a ha, (smul_add _ _ _).subset.trans $ add_subset_add (hs _ ha) $ ht _ ha lemma absorbs.sub (h₁ : absorbs 𝕜 s₁ t₁) (h₂ : absorbs 𝕜 s₂ t₂) : absorbs 𝕜 (s₁ - s₂) (t₁ - t₂) := by { simp_rw sub_eq_add_neg, exact h₁.add h₂.neg } lemma balanced.sub (hs : balanced 𝕜 s) (ht : balanced 𝕜 t) : balanced 𝕜 (s - t) := by { simp_rw sub_eq_add_neg, exact hs.add ht.neg } lemma balanced_zero : balanced 𝕜 (0 : set E) := λ a ha, (smul_zero _).subset end module end semi_normed_ring section normed_field variables [normed_field 𝕜] [normed_ring 𝕝] [normed_space 𝕜 𝕝] [add_comm_group E] [module 𝕜 E] [smul_with_zero 𝕝 E] [is_scalar_tower 𝕜 𝕝 E] {s t u v A B : set E} {x : E} {a b : 𝕜} /-- Scalar multiplication (by possibly different types) of a balanced set is monotone. -/ lemma balanced.smul_mono (hs : balanced 𝕝 s) {a : 𝕝} {b : 𝕜} (h : ‖a‖ ≤ ‖b‖) : a • s ⊆ b • s := begin obtain rfl | hb := eq_or_ne b 0, { rw norm_zero at h, rw norm_eq_zero.1 (h.antisymm $ norm_nonneg _), obtain rfl | h := s.eq_empty_or_nonempty, { simp_rw [smul_set_empty] }, { simp_rw [zero_smul_set h] } }, rintro _ ⟨x, hx, rfl⟩, refine ⟨b⁻¹ • a • x, _, smul_inv_smul₀ hb _⟩, rw ←smul_assoc, refine hs _ _ (smul_mem_smul_set hx), rw [norm_smul, norm_inv, ←div_eq_inv_mul], exact div_le_one_of_le h (norm_nonneg _), end /-- A balanced set absorbs itself. -/ lemma balanced.absorbs_self (hA : balanced 𝕜 A) : absorbs 𝕜 A A := begin refine ⟨1, zero_lt_one, λ a ha x hx, _⟩, rw mem_smul_set_iff_inv_smul_mem₀ (norm_pos_iff.1 $ zero_lt_one.trans_le ha), refine hA a⁻¹ _ (smul_mem_smul_set hx), rw norm_inv, exact inv_le_one ha, end lemma balanced.subset_smul (hA : balanced 𝕜 A) (ha : 1 ≤ ‖a‖) : A ⊆ a • A := begin refine (subset_set_smul_iff₀ _).2 (hA (a⁻¹) _), { rintro rfl, rw norm_zero at ha, exact zero_lt_one.not_le ha }, { rw norm_inv, exact inv_le_one ha } end lemma balanced.smul_eq (hA : balanced 𝕜 A) (ha : ‖a‖ = 1) : a • A = A := (hA _ ha.le).antisymm $ hA.subset_smul ha.ge lemma balanced.mem_smul_iff (hs : balanced 𝕜 s) (h : ‖a‖ = ‖b‖) : a • x ∈ s ↔ b • x ∈ s := begin obtain rfl | hb := eq_or_ne b 0, { rw [norm_zero, norm_eq_zero] at h, rw h }, have ha : a ≠ 0 := norm_ne_zero_iff.1 (ne_of_eq_of_ne h $ norm_ne_zero_iff.2 hb), split; intro h'; [rw ←inv_mul_cancel_right₀ ha b, rw ←inv_mul_cancel_right₀ hb a]; { rw [←smul_eq_mul, smul_assoc], refine hs.smul_mem _ h', simp [←h, ha] } end lemma balanced.neg_mem_iff (hs : balanced 𝕜 s) : -x ∈ s ↔ x ∈ s := by convert hs.mem_smul_iff (norm_neg 1); simp only [neg_smul, one_smul] lemma absorbs.inter (hs : absorbs 𝕜 s u) (ht : absorbs 𝕜 t u) : absorbs 𝕜 (s ∩ t) u := begin obtain ⟨a, ha, hs⟩ := hs, obtain ⟨b, hb, ht⟩ := ht, have h : 0 < max a b := lt_max_of_lt_left ha, refine ⟨max a b, lt_max_of_lt_left ha, λ c hc, _⟩, rw smul_set_inter₀ (norm_pos_iff.1 $ h.trans_le hc), exact subset_inter (hs _ $ le_of_max_le_left hc) (ht _ $ le_of_max_le_right hc), end @[simp] lemma absorbs_inter : absorbs 𝕜 (s ∩ t) u ↔ absorbs 𝕜 s u ∧ absorbs 𝕜 t u := ⟨λ h, ⟨h.mono_left $ inter_subset_left _ _, h.mono_left $ inter_subset_right _ _⟩, λ h, h.1.inter h.2⟩ lemma absorbent_univ : absorbent 𝕜 (univ : set E) := begin refine λ x, ⟨1, zero_lt_one, λ a ha, _⟩, rw smul_set_univ₀ (norm_pos_iff.1 $ zero_lt_one.trans_le ha), exact trivial, end variables [topological_space E] [has_continuous_smul 𝕜 E] /-- Every neighbourhood of the origin is absorbent. -/ lemma absorbent_nhds_zero (hA : A ∈ 𝓝 (0 : E)) : absorbent 𝕜 A := begin intro x, obtain ⟨w, hw₁, hw₂, hw₃⟩ := mem_nhds_iff.mp hA, have hc : continuous (λ t : 𝕜, t • x) := continuous_id.smul continuous_const, obtain ⟨r, hr₁, hr₂⟩ := metric.is_open_iff.mp (hw₂.preimage hc) 0 (by rwa [mem_preimage, zero_smul]), have hr₃ := inv_pos.mpr (half_pos hr₁), refine ⟨(r / 2)⁻¹, hr₃, λ a ha₁, _⟩, have ha₂ : 0 < ‖a‖ := hr₃.trans_le ha₁, refine (mem_smul_set_iff_inv_smul_mem₀ (norm_pos_iff.mp ha₂) _ _).2 (hw₁ $ hr₂ _), rw [metric.mem_ball, dist_zero_right, norm_inv], calc ‖a‖⁻¹ ≤ r/2 : (inv_le (half_pos hr₁) ha₂).mp ha₁ ... < r : half_lt_self hr₁, end /-- The union of `{0}` with the interior of a balanced set is balanced. -/ lemma balanced_zero_union_interior (hA : balanced 𝕜 A) : balanced 𝕜 ((0 : set E) ∪ interior A) := begin intros a ha, obtain rfl | h := eq_or_ne a 0, { rw zero_smul_set, exacts [subset_union_left _ _, ⟨0, or.inl rfl⟩] }, { rw [←image_smul, image_union], apply union_subset_union, { rw [image_zero, smul_zero], refl }, { calc a • interior A ⊆ interior (a • A) : (is_open_map_smul₀ h).image_interior_subset A ... ⊆ interior A : interior_mono (hA _ ha) } } end /-- The interior of a balanced set is balanced if it contains the origin. -/ lemma balanced.interior (hA : balanced 𝕜 A) (h : (0 : E) ∈ interior A) : balanced 𝕜 (interior A) := begin rw ←union_eq_self_of_subset_left (singleton_subset_iff.2 h), exact balanced_zero_union_interior hA, end lemma balanced.closure (hA : balanced 𝕜 A) : balanced 𝕜 (closure A) := λ a ha, (image_closure_subset_closure_image $ continuous_id.const_smul _).trans $ closure_mono $ hA _ ha end normed_field section nontrivially_normed_field variables [nontrivially_normed_field 𝕜] [add_comm_group E] [module 𝕜 E] {s : set E} lemma absorbs_zero_iff : absorbs 𝕜 s 0 ↔ (0 : E) ∈ s := begin refine ⟨_, λ h, ⟨1, zero_lt_one, λ a _, zero_subset.2 $ zero_mem_smul_set h⟩⟩, rintro ⟨r, hr, h⟩, obtain ⟨a, ha⟩ := normed_space.exists_lt_norm 𝕜 𝕜 r, have := h _ ha.le, rwa [zero_subset, zero_mem_smul_set_iff] at this, exact norm_ne_zero_iff.1 (hr.trans ha).ne', end lemma absorbent.zero_mem (hs : absorbent 𝕜 s) : (0 : E) ∈ s := absorbs_zero_iff.1 $ absorbent_iff_forall_absorbs_singleton.1 hs _ variables [module ℝ E] [smul_comm_class ℝ 𝕜 E] lemma balanced_convex_hull_of_balanced (hs : balanced 𝕜 s) : balanced 𝕜 (convex_hull ℝ s) := begin suffices : convex ℝ {x | ∀ a : 𝕜, ‖a‖ ≤ 1 → a • x ∈ convex_hull ℝ s}, { rw balanced_iff_smul_mem at hs ⊢, refine λ a ha x hx, convex_hull_min _ this hx a ha, exact λ y hy a ha, subset_convex_hull ℝ s (hs ha hy) }, intros x hx y hy u v hu hv huv a ha, simp only [smul_add, ← smul_comm], exact convex_convex_hull ℝ s (hx a ha) (hy a ha) hu hv huv end end nontrivially_normed_field section real variables [add_comm_group E] [module ℝ E] {s : set E} lemma balanced_iff_neg_mem (hs : convex ℝ s) : balanced ℝ s ↔ ∀ ⦃x⦄, x ∈ s → -x ∈ s := begin refine ⟨λ h x, h.neg_mem_iff.2, λ h a ha, smul_set_subset_iff.2 $ λ x hx, _⟩, rw [real.norm_eq_abs, abs_le] at ha, rw [show a = -((1 - a) / 2) + (a - -1)/2, by ring, add_smul, neg_smul, ←smul_neg], exact hs (h hx) hx (div_nonneg (sub_nonneg_of_le ha.2) zero_le_two) (div_nonneg (sub_nonneg_of_le ha.1) zero_le_two) (by ring), end end real
9f45750ea8fb4a533ec3e6bae43d0c38a2d3a97a
1a61aba1b67cddccce19532a9596efe44be4285f
/library/init/nat.lean
8ac028be33523b72d204eab81e67beb88b7cd52e
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
8,772
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import init.wf init.tactic init.num open eq.ops decidable or notation `ℕ` := nat namespace nat /- basic definitions on natural numbers -/ inductive le (a : ℕ) : ℕ → Prop := | refl : le a a | step : Π {b}, le a b → le a (succ b) infix `≤` := le attribute le.refl [refl] definition lt [reducible] (n m : ℕ) := succ n ≤ m definition ge [reducible] (n m : ℕ) := m ≤ n definition gt [reducible] (n m : ℕ) := succ m ≤ n infix `<` := lt infix `≥` := ge infix `>` := gt definition pred [unfold 1] (a : nat) : nat := nat.cases_on a zero (λ a₁, a₁) -- add is defined in init.num definition sub (a b : nat) : nat := nat.rec_on b a (λ b₁, pred) definition mul (a b : nat) : nat := nat.rec_on b zero (λ b₁ r, r + a) notation a - b := sub a b notation a * b := mul a b /- properties of ℕ -/ protected definition is_inhabited [instance] : inhabited nat := inhabited.mk zero protected definition has_decidable_eq [instance] : ∀ x y : nat, decidable (x = y) | has_decidable_eq zero zero := inl rfl | has_decidable_eq (succ x) zero := inr (by contradiction) | has_decidable_eq zero (succ y) := inr (by contradiction) | has_decidable_eq (succ x) (succ y) := match has_decidable_eq x y with | inl xeqy := inl (by rewrite xeqy) | inr xney := inr (λ h : succ x = succ y, by injection h with xeqy; exact absurd xeqy xney) end /- properties of inequality -/ theorem le_of_eq {n m : ℕ} (p : n = m) : n ≤ m := p ▸ !le.refl theorem le_succ (n : ℕ) : n ≤ succ n := le.step !le.refl theorem pred_le (n : ℕ) : pred n ≤ n := by cases n;repeat constructor theorem le_succ_iff_true [simp] (n : ℕ) : n ≤ succ n ↔ true := iff_true_intro (le_succ n) theorem pred_le_iff_true [simp] (n : ℕ) : pred n ≤ n ↔ true := iff_true_intro (pred_le n) theorem le.trans [trans] {n m k : ℕ} (H1 : n ≤ m) : m ≤ k → n ≤ k := le.rec H1 (λp H2, le.step) theorem le_succ_of_le {n m : ℕ} (H : n ≤ m) : n ≤ succ m := le.trans H !le_succ theorem le_of_succ_le {n m : ℕ} (H : succ n ≤ m) : n ≤ m := le.trans !le_succ H theorem le_of_lt {n m : ℕ} (H : n < m) : n ≤ m := le_of_succ_le H theorem succ_le_succ {n m : ℕ} : n ≤ m → succ n ≤ succ m := le.rec !le.refl (λa b, le.step) theorem pred_le_pred {n m : ℕ} : n ≤ m → pred n ≤ pred m := le.rec !le.refl (nat.rec (λa b, b) (λa b c, le.step)) theorem le_of_succ_le_succ {n m : ℕ} : succ n ≤ succ m → n ≤ m := pred_le_pred theorem le_succ_of_pred_le {n m : ℕ} : pred n ≤ m → n ≤ succ m := nat.cases_on n le.step (λa, succ_le_succ) theorem not_succ_le_zero (n : ℕ) : ¬succ n ≤ zero := by intro H; cases H theorem succ_le_zero_iff_false (n : ℕ) : succ n ≤ zero ↔ false := iff_false_intro !not_succ_le_zero theorem not_succ_le_self : Π {n : ℕ}, ¬succ n ≤ n := nat.rec !not_succ_le_zero (λa b c, b (le_of_succ_le_succ c)) theorem succ_le_self_iff_false [simp] (n : ℕ) : succ n ≤ n ↔ false := iff_false_intro not_succ_le_self theorem zero_le : ∀ (n : ℕ), 0 ≤ n := nat.rec !le.refl (λa, le.step) theorem zero_le_iff_true [simp] (n : ℕ) : 0 ≤ n ↔ true := iff_true_intro !zero_le theorem lt.step {n m : ℕ} : n < m → n < succ m := le.step theorem zero_lt_succ (n : ℕ) : 0 < succ n := succ_le_succ !zero_le theorem zero_lt_succ_iff_true [simp] (n : ℕ) : 0 < succ n ↔ true := iff_true_intro (zero_lt_succ n) theorem lt.trans [trans] {n m k : ℕ} (H1 : n < m) : m < k → n < k := le.trans (le.step H1) theorem lt_of_le_of_lt [trans] {n m k : ℕ} (H1 : n ≤ m) : m < k → n < k := le.trans (succ_le_succ H1) theorem lt_of_lt_of_le [trans] {n m k : ℕ} : n < m → m ≤ k → n < k := le.trans theorem lt.irrefl (n : ℕ) : ¬n < n := not_succ_le_self theorem lt_self_iff_false [simp] (n : ℕ) : n < n ↔ false := iff_false_intro (lt.irrefl n) theorem self_lt_succ (n : ℕ) : n < succ n := !le.refl theorem self_lt_succ_iff_true [simp] (n : ℕ) : n < succ n ↔ true := iff_true_intro (self_lt_succ n) theorem lt.base (n : ℕ) : n < succ n := !le.refl theorem le_lt_antisymm {n m : ℕ} (H1 : n ≤ m) (H2 : m < n) : false := !lt.irrefl (lt_of_le_of_lt H1 H2) theorem le.antisymm {n m : ℕ} (H1 : n ≤ m) : m ≤ n → n = m := le.cases_on H1 (λa, rfl) (λa b c, absurd (lt_of_le_of_lt b c) !lt.irrefl) theorem lt_le_antisymm {n m : ℕ} (H1 : n < m) (H2 : m ≤ n) : false := le_lt_antisymm H2 H1 theorem lt.asymm {n m : ℕ} (H1 : n < m) : ¬ m < n := le_lt_antisymm (le_of_lt H1) theorem not_lt_zero (a : ℕ) : ¬ a < zero := !not_succ_le_zero theorem lt_zero_iff_false [simp] (a : ℕ) : a < zero ↔ false := iff_false_intro (not_lt_zero a) theorem eq_or_lt_of_le {a b : ℕ} (H : a ≤ b) : a = b ∨ a < b := le.cases_on H (inl rfl) (λn h, inr (succ_le_succ h)) theorem le_of_eq_or_lt {a b : ℕ} (H : a = b ∨ a < b) : a ≤ b := or.elim H !le_of_eq !le_of_lt -- less-than is well-founded definition lt.wf [instance] : well_founded lt := well_founded.intro (nat.rec (!acc.intro (λn H, absurd H (not_lt_zero n))) (λn IH, !acc.intro (λm H, elim (eq_or_lt_of_le (le_of_succ_le_succ H)) (λe, eq.substr e IH) (acc.inv IH)))) definition measure {A : Type} : (A → ℕ) → A → A → Prop := inv_image lt definition measure.wf {A : Type} (f : A → ℕ) : well_founded (measure f) := inv_image.wf f lt.wf theorem succ_lt_succ {a b : ℕ} : a < b → succ a < succ b := succ_le_succ theorem lt_of_succ_lt {a b : ℕ} : succ a < b → a < b := le_of_succ_le theorem lt_of_succ_lt_succ {a b : ℕ} : succ a < succ b → a < b := le_of_succ_le_succ definition decidable_le [instance] : decidable_rel le := nat.rec (λm, (decidable.inl !zero_le)) (λn IH m, !nat.cases_on (decidable.inr (not_succ_le_zero n)) (λm, decidable.rec (λH, inl (succ_le_succ H)) (λH, inr (λa, H (le_of_succ_le_succ a))) (IH m))) definition decidable_lt [instance] : decidable_rel lt := _ definition decidable_gt [instance] : decidable_rel gt := _ definition decidable_ge [instance] : decidable_rel ge := _ theorem lt_or_ge (a b : ℕ) : a < b ∨ a ≥ b := nat.rec (inr !zero_le) (λn, or.rec (λh, inl (le_succ_of_le h)) (λh, elim (eq_or_lt_of_le h) (λe, inl (eq.subst e !le.refl)) inr)) b definition lt_ge_by_cases {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a ≥ b → P) : P := by_cases H1 (λh, H2 (elim !lt_or_ge (λa, absurd a h) (λa, a))) definition lt.by_cases {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P) (H3 : b < a → P) : P := lt_ge_by_cases H1 (λh₁, lt_ge_by_cases H3 (λh₂, H2 (le.antisymm h₂ h₁))) theorem lt.trichotomy (a b : ℕ) : a < b ∨ a = b ∨ b < a := lt.by_cases (λH, inl H) (λH, inr (inl H)) (λH, inr (inr H)) theorem eq_or_lt_of_not_lt {a b : ℕ} (hnlt : ¬ a < b) : a = b ∨ b < a := or.rec_on (lt.trichotomy a b) (λ hlt, absurd hlt hnlt) (λ h, h) theorem lt_succ_of_le {a b : ℕ} : a ≤ b → a < succ b := succ_le_succ theorem lt_of_succ_le {a b : ℕ} (h : succ a ≤ b) : a < b := h theorem succ_le_of_lt {a b : ℕ} (h : a < b) : succ a ≤ b := h theorem succ_sub_succ_eq_sub [simp] (a b : ℕ) : succ a - succ b = a - b := nat.rec rfl (λ b, congr_arg pred) b theorem sub_eq_succ_sub_succ (a b : ℕ) : a - b = succ a - succ b := eq.symm !succ_sub_succ_eq_sub theorem zero_sub_eq_zero [simp] (a : ℕ) : zero - a = zero := nat.rec rfl (λ a, congr_arg pred) a theorem zero_eq_zero_sub (a : ℕ) : zero = zero - a := eq.symm !zero_sub_eq_zero theorem sub_le (a b : ℕ) : a - b ≤ a := nat.rec_on b !le.refl (λ b₁, le.trans !pred_le) theorem sub_le_iff_true [simp] (a b : ℕ) : a - b ≤ a ↔ true := iff_true_intro (sub_le a b) theorem sub_lt {a b : ℕ} (H1 : zero < a) (H2 : zero < b) : a - b < a := !nat.cases_on (λh, absurd h !lt.irrefl) (λa h, succ_le_succ (!nat.cases_on (λh, absurd h !lt.irrefl) (λb c, eq.substr !succ_sub_succ_eq_sub !sub_le) H2)) H1 theorem sub_lt_succ (a b : ℕ) : a - b < succ a := lt_succ_of_le !sub_le theorem sub_lt_succ_iff_true [simp] (a b : ℕ) : a - b < succ a ↔ true := iff_true_intro !sub_lt_succ end nat namespace nat_esimp open nat attribute add mul sub [unfold 2] attribute of_num [unfold 1] end nat_esimp
857c8b609de3d24aaaa6cd75df153c133543b6a2
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/runtime.lean
cbdbe7890533544d8290e6ad12ba5826656c5b67
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
479
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.core namespace Lean @[extern "lean_closure_max_args"] constant closureMaxArgsFn : Unit → Nat := default _ @[extern "lean_max_small_nat"] constant maxSmallNatFn : Unit → Nat := default _ def closureMaxArgs : Nat := closureMaxArgsFn () def maxSmallNat : Nat := maxSmallNatFn () end Lean
0f1411d495fa71ce63ec048cbb1893cd2feac920
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/interactive/definition.lean
f302e1fe661de365e65f47071ee6b57430242440
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
100
lean
inductive Foo where | foo example : Foo := let c := Foo.foo c --^ textDocument/typeDefinition
1af7fd2d44a30da937d717532724b703e343e5a7
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/equiv/set.lean
3eb4ab14710ccb5137c7f3ae2fb22cbef9a4cfc5
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,230
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.equiv.basic import data.set.function /-! # Equivalences and sets In this file we provide lemmas linking equivalences to sets. Some notable definitions are: * `equiv.of_injective`: an injective function is (noncomputably) equivalent to its range. * `equiv.set_congr`: two equal sets are equivalent as types. * `equiv.set.union`: a disjoint union of sets is equivalent to their `sum`. This file is separate from `equiv/basic` such that we do not require the full lattice structure on sets before defining what an equivalence is. -/ open function universes u v w z variables {α : Sort u} {β : Sort v} {γ : Sort w} namespace equiv @[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : set.range e = set.univ := set.eq_univ_of_forall e.surjective protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv lemma _root_.set.mem_image_equiv {α β} {S : set α} {f : α ≃ β} {x : β} : x ∈ f '' S ↔ f.symm x ∈ S := set.ext_iff.mp (f.image_eq_preimage S) x /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.image_equiv_eq_preimage_symm {α β} (S : set α) (f : α ≃ β) : f '' S = f.symm ⁻¹' S := f.image_eq_preimage S /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.preimage_equiv_eq_image_symm {α β} (S : set α) (f : β ≃ α) : f ⁻¹' S = f.symm '' S := (f.symm.image_eq_preimage S).symm @[simp] protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : e.symm '' t ⊆ s ↔ t ⊆ e '' s := by rw [set.image_subset_iff, e.image_eq_preimage] @[simp] protected lemma subset_image' {α β} (e : α ≃ β) (s : set α) (t : set β) : s ⊆ e.symm '' t ↔ e '' s ⊆ t := calc s ⊆ e.symm '' t ↔ e.symm.symm '' s ⊆ t : by rw e.symm.subset_image ... ↔ e '' s ⊆ t : by rw e.symm_symm @[simp] lemma symm_image_image {α β} (e : α ≃ β) (s : set α) : e.symm '' (e '' s) = s := e.left_inverse_symm.image_image s lemma eq_image_iff_symm_image_eq {α β} (e : α ≃ β) (s : set α) (t : set β) : t = e '' s ↔ e.symm '' t = s := (e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm @[simp] lemma image_symm_image {α β} (e : α ≃ β) (s : set β) : e '' (e.symm '' s) = s := e.symm.symm_image_image s @[simp] lemma image_preimage {α β} (e : α ≃ β) (s : set β) : e '' (e ⁻¹' s) = s := e.surjective.image_preimage s @[simp] lemma preimage_image {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e '' s) = s := e.injective.preimage_image s protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' sᶜ = (f '' s)ᶜ := set.image_compl_eq f.bijective @[simp] lemma symm_preimage_preimage {α β} (e : α ≃ β) (s : set β) : e.symm ⁻¹' (e ⁻¹' s) = s := e.right_inverse_symm.preimage_preimage s @[simp] lemma preimage_symm_preimage {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e.symm ⁻¹' s) = s := e.left_inverse_symm.preimage_preimage s @[simp] lemma preimage_subset {α β} (e : α ≃ β) (s t : set β) : e ⁻¹' s ⊆ e ⁻¹' t ↔ s ⊆ t := e.surjective.preimage_subset_preimage_iff @[simp] lemma image_subset {α β} (e : α ≃ β) (s t : set α) : e '' s ⊆ e '' t ↔ s ⊆ t := set.image_subset_image_iff e.injective @[simp] lemma image_eq_iff_eq {α β} (e : α ≃ β) (s t : set α) : e '' s = e '' t ↔ s = t := set.image_eq_image e.injective lemma preimage_eq_iff_eq_image {α β} (e : α ≃ β) (s t) : e ⁻¹' s = t ↔ s = e '' t := set.preimage_eq_iff_eq_image e.bijective lemma eq_preimage_iff_image_eq {α β} (e : α ≃ β) (s t) : s = e ⁻¹' t ↔ e '' s = t := set.eq_preimage_iff_image_eq e.bijective lemma prod_assoc_preimage {α β γ} {s : set α} {t : set β} {u : set γ} : equiv.prod_assoc α β γ ⁻¹' s.prod (t.prod u) = (s.prod t).prod u := by { ext, simp [and_assoc] } /-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/ def set_prod_equiv_sigma {α β : Type*} (s : set (α × β)) : s ≃ Σ x : α, {y | (x, y) ∈ s} := { to_fun := λ x, ⟨x.1.1, x.1.2, by simp⟩, inv_fun := λ x, ⟨(x.1, x.2.1), x.2.2⟩, left_inv := λ ⟨⟨x, y⟩, h⟩, rfl, right_inv := λ ⟨x, y, h⟩, rfl } /-- The subtypes corresponding to equal sets are equivalent. -/ @[simps apply] def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t := subtype_equiv_prop h /-- A set is equivalent to its image under an equivalence. -/ -- We could construct this using `equiv.set.image e s e.injective`, -- but this definition provides an explicit inverse. @[simps] def image {α β : Type*} (e : α ≃ β) (s : set α) : s ≃ e '' s := { to_fun := λ x, ⟨e x.1, by simp⟩, inv_fun := λ y, ⟨e.symm y.1, by { rcases y with ⟨-, ⟨a, ⟨m, rfl⟩⟩⟩, simpa using m, }⟩, left_inv := λ x, by simp, right_inv := λ y, by simp, }. open set namespace set /-- `univ α` is equivalent to `α`. -/ @[simps apply symm_apply] protected def univ (α) : @univ α ≃ α := ⟨coe, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ /-- An empty set is equivalent to the `empty` type. -/ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty _ /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty _ /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t := { to_fun := λ x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩, inv_fun := λ o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := λ o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) : (s ∪ t : set α) ≃ s ⊕ t := set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ λ h, H ⟨h, ha⟩ @[simp] lemma union_symm_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ := rfl @[simp] lemma union_symm_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ := rfl /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by { simp at h, subst x }, λ ⟨⟩, rfl⟩ /-- Equal sets are equivalent. -/ @[simps apply symm_apply] protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t := { to_fun := λ x, ⟨x, h ▸ x.2⟩, inv_fun := λ x, ⟨x, h.symm ▸ x.2⟩, left_inv := λ _, subtype.eq rfl, right_inv := λ _, subtype.eq rfl } /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/ protected def insert {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ s ⊕ punit.{u+1} := calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp) ... ≃ s ⊕ ({a} : set α) : equiv.set.union (by finish [set.subset_def]) ... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) @[simp] lemma insert_symm_apply_inl {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ := rfl @[simp] lemma insert_symm_apply_inr {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ := rfl @[simp] lemma insert_apply_left {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) : equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma insert_apply_right {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl /-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sum_compl {α} (s : set α) [decidable_pred (∈ s)] : s ⊕ (sᶜ : set α) ≃ α := calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ α : equiv.set.of_eq (by simp) ... ≃ α : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : sᶜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α} (hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } @[simp] lemma sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : s} : (equiv.set.sum_compl s).symm x = sum.inl x := by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx @[simp] lemma sum_compl_symm_apply_compl {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x := by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx /-- `sum_diff_subset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] : s ⊕ (t \ s : set α) ≃ t := calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∉ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred (∈ s)] : (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t := calc (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self] ... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _ ... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- Given an equivalence `e₀` between sets `s : set α` and `t : set β`, the set of equivalences `e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences between `sᶜ` and `tᶜ`. -/ protected def compl {α : Type u} {β : Type v} {s : set α} {t : set β} [decidable_pred (∈ s)] [decidable_pred (∈ t)] (e₀ : s ≃ t) : {e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) := { to_fun := λ e, subtype_equiv e (λ a, not_congr $ iff.symm $ maps_to.mem_iff (maps_to_iff_exists_map_subtype.2 ⟨e₀, e.2⟩) (surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2 ⟨t, e₀, subset.refl t, e₀.surjective, e.2⟩) e.1.injective)), inv_fun := λ e₁, subtype.mk (calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm ... ≃ t ⊕ (tᶜ : set β) : e₀.sum_congr e₁ ... ≃ β : set.sum_compl t) (λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply, set.sum_compl_apply_inl, set.sum_compl_symm_apply]), left_inv := λ e, begin ext x, by_cases hx : x ∈ s, { simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩, sum.map_inl, sum_congr_apply, trans_apply, subtype.coe_mk, set.sum_compl_apply_inl] }, { simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, trans_apply, sum_congr_apply, subtype.coe_mk] }, end, right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans, subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : set α) (t : set β) : s.prod t ≃ s × t := @subtype_prod_equiv_prod α β s t /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ @[simps apply] protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (H.inj_on s) @[simp] protected lemma image_symm_apply {α β} (f : α → β) (s : set α) (H : injective f) (x : α) (h : x ∈ s) : (set.image f s H).symm ⟨f x, ⟨x, ⟨h, rfl⟩⟩⟩ = ⟨x, h⟩ := begin apply (set.image f s H).injective, simp [(set.image f s H).apply_symm_apply], end lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) : (λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) := begin ext ⟨b, a, has, rfl⟩, have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2, simp [equiv.set.image, equiv.set.image_of_inj_on, hf.eq_iff, this], end /-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/ @[simps] protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β := ⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : set α) (t : α → Prop) : ({ x ∈ s | t x } : set α) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm /-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `set S`. -/ protected def powerset {α} (S : set α) : 𝒫 S ≃ set S := { to_fun := λ x : 𝒫 S, coe ⁻¹' (x : set α), inv_fun := λ x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩, left_inv := λ x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, λ h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩, right_inv := λ x, by ext; simp } /-- If `s` is a set in `range f`, then its image under `range_splitting f` is in bijection (via `f`) with `s`. -/ @[simps] noncomputable def range_splitting_image_equiv {α β : Type*} (f : α → β) (s : set (range f)) : range_splitting f '' s ≃ s := { to_fun := λ x, ⟨⟨f x, by simp⟩, (by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simpa [apply_range_splitting f] using m, })⟩, inv_fun := λ x, ⟨range_splitting f x, ⟨x, ⟨x.2, rfl⟩⟩⟩, left_inv := λ x, by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simp [apply_range_splitting f] }, right_inv := λ x, by simp [apply_range_splitting f], } end set /-- If `f : α → β` has a left-inverse when `α` is nonempty, then `α` is computably equivalent to the range of `f`. While awkward, the `nonempty α` hypothesis on `f_inv` and `hf` allows this to be used when `α` is empty too. This hypothesis is absent on analogous definitions on stronger `equiv`s like `linear_equiv.of_left_inverse` and `ring_equiv.of_left_inverse` as their typeclass assumptions are already sufficient to ensure non-emptiness. -/ @[simps] def of_left_inverse {α β : Sort*} (f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) : α ≃ set.range f := { to_fun := λ a, ⟨f a, a, rfl⟩, inv_fun := λ b, f_inv (nonempty_of_exists b.2) b, left_inv := λ a, hf ⟨a⟩ a, right_inv := λ ⟨b, a, ha⟩, subtype.eq $ show f (f_inv ⟨a⟩ b) = b, from eq.trans (congr_arg f $ by exact ha ▸ (hf _ a)) ha } /-- If `f : α → β` has a left-inverse, then `α` is computably equivalent to the range of `f`. Note that if `α` is empty, no such `f_inv` exists and so this definition can't be used, unlike the stronger but less convenient `of_left_inverse`. -/ abbreviation of_left_inverse' {α β : Sort*} (f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) : α ≃ set.range f := of_left_inverse f (λ _, f_inv) (λ _, hf) /-- If `f : α → β` is an injective function, then domain `α` is equivalent to the range of `f`. -/ @[simps apply] noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ set.range f := equiv.of_left_inverse f (λ h, by exactI function.inv_fun f) (λ h, by exactI function.left_inverse_inv_fun hf) theorem apply_of_injective_symm {α β} (f : α → β) (hf : injective f) (b : set.range f) : f ((of_injective f hf).symm b) = b := subtype.ext_iff.1 $ (of_injective f hf).apply_symm_apply b @[simp] theorem of_injective_symm_apply {α β} (f : α → β) (hf : injective f) (a : α) : (of_injective f hf).symm ⟨f a, ⟨a, rfl⟩⟩ = a := begin apply (of_injective f hf).injective, simp [apply_of_injective_symm f hf], end lemma coe_of_injective_symm {α β} (f : α → β) (hf : injective f) : ((of_injective f hf).symm : range f → α) = range_splitting f := by { ext ⟨y, x, rfl⟩, apply hf, simp [apply_range_splitting f] } @[simp] lemma self_comp_of_injective_symm {α β} (f : α → β) (hf : injective f) : f ∘ ((of_injective f hf).symm) = coe := funext (λ x, apply_of_injective_symm f hf x) lemma of_left_inverse_eq_of_injective {α β : Type*} (f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) : of_left_inverse f f_inv hf = of_injective f ((em (nonempty α)).elim (λ h, (hf h).injective) (λ h _ _ _, by { haveI : subsingleton α := subsingleton_of_not_nonempty h, simp })) := by { ext, simp } lemma of_left_inverse'_eq_of_injective {α β : Type*} (f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) : of_left_inverse' f f_inv hf = of_injective f hf.injective := by { ext, simp } protected lemma set_forall_iff {α β} (e : α ≃ β) {p : set α → Prop} : (∀ a, p a) ↔ (∀ a, p (e ⁻¹' a)) := by simpa [equiv.image_eq_preimage] using (equiv.set.congr e).forall_congr_left' protected lemma preimage_sUnion {α β} (f : α ≃ β) {s : set (set β)} : f ⁻¹' (⋃₀ s) = ⋃₀ (_root_.set.image f ⁻¹' s) := by { ext x, simp [(equiv.set.congr f).symm.exists_congr_left] } end equiv /-- If a function is a bijection between two sets `s` and `t`, then it induces an equivalence between the types `↥s` and ``↥t`. -/ noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set α} {t : set β} (f : α → β) (h : set.bij_on f s t) : s ≃ t := equiv.of_bijective _ h.bijective /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {α : Type*} {β : Sort*} {γ : Sort*} {s : set α} (e : β ≃ s) (v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α] [∀ j, decidable (j ∈ s)] : (λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { rw [dif_pos h, function.update_apply_equiv_apply, equiv.symm_symm, function.comp, function.update_apply, function.update_apply, dif_pos h], have h_coe : (⟨i, h⟩ : s) = e j ↔ i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk), simp_rw h_coe, congr, }, { have : i ≠ e j, by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
1b25b4b93925786162327a70d942bce985012b20
7bf54883c04ff2856c37f76a79599ceb380c1996
/non-mathlib/early_version.lean
e22d6759997089dc7a161cc042925d6aa5f4cbd9
[]
no_license
anonymousLeanDocsHosting/lean-polynomials
4094466bf19acc0d1a47b4cabb60d8c21ddb2b00
361ef4cb7b68ef47d43b85cfa2d13f2ea0a47613
refs/heads/main
1,691,112,899,935
1,631,819,522,000
1,631,819,522,000
405,448,439
0
2
null
null
null
null
UTF-8
Lean
false
false
305,647
lean
/- A natural number is either zero or the successor of a natural number.-/ inductive mynat : Type | zero : mynat | mysucc : mynat → mynat open mynat lemma succ_eq (a b : mynat) : (a = b) -> (mysucc a = mysucc b) := begin intros h, rw h, end def nat_one : mynat := (mysucc zero) def myadd : mynat → mynat → mynat | x zero := x | x (mysucc y) := mysucc (myadd x y) lemma add_zero (n : mynat) : myadd n zero = n := begin unfold myadd, end lemma add_succ (n m : mynat) : myadd n (mysucc m) = mysucc (myadd n m) := begin unfold myadd, end lemma zero_add (n : mynat) : myadd zero n = n := begin induction n, rw add_zero, rw add_succ, rw n_ih, end lemma succ_add (n m : mynat) : myadd (mysucc n) m = mysucc (myadd n m) := begin induction m, repeat {rw add_zero}, rw add_succ, rw m_ih, refl, end lemma add_assoc (a b c : mynat) : myadd (myadd a b) c = myadd a (myadd b c) := begin induction c, repeat {rw add_zero}, repeat {rw add_succ}, rw c_ih, end lemma add_comm (a b : mynat) : myadd a b = myadd b a := begin induction a, rw add_zero, rw zero_add, rw add_succ, rw succ_add, rw a_ih, end lemma add_cancel_a (a b c : mynat) : myadd b a = myadd c a -> b = c := begin intros h, induction a, repeat {rw add_zero at h}, exact h, repeat {rw add_succ at h}, simp at h, exact a_ih h, end lemma add_cancel_a_rev (a b c : mynat) : myadd a b = myadd a c -> b = c := begin rw add_comm a b, rw add_comm a c, exact add_cancel_a a b c, end lemma add_cancel_b (a b c : mynat) : b = c -> myadd b a = myadd c a := begin intros h, induction a, repeat {rw add_zero}, exact h, repeat {rw add_succ}, simp, exact a_ih, end lemma add_comm_in_tree (a b c d : mynat) : (myadd (myadd a b) (myadd c d)) = (myadd (myadd a c) (myadd b d)) := begin rw add_assoc, rw <- add_assoc b c d, rw add_comm b c, rw add_assoc c b d, rw <- add_assoc, end lemma add_combine_eq (a b c d : mynat) : (a = b) -> ((c = d) -> (myadd a c = myadd b d)) := begin intros h1 h2, rw h1, rw h2, end def mymul : mynat → mynat → mynat | x zero := zero | x (mysucc y) := myadd x (mymul x y) lemma zero_mul (n : mynat) : mymul zero n = zero := begin induction n, unfold mymul, unfold mymul, rw n_ih, unfold myadd, end lemma mul_zero (n : mynat) : mymul n zero = zero := begin unfold mymul, end lemma one_mul_eq (n : mynat) : mymul (mysucc zero) n = n := begin induction n, unfold mymul, unfold mymul, rw succ_add, rw n_ih, rw zero_add, end lemma mul_one_eq (n : mynat) : mymul n (mysucc zero) = n := begin cases n, rw zero_mul, unfold mymul, rw add_zero, end lemma mul_succ (n m : mynat) : mymul (mysucc n) m = myadd (mymul n m) m := begin induction m, unfold mymul, unfold myadd, unfold mymul, rw m_ih, repeat {unfold myadd}, rw add_assoc, rw succ_add, end lemma succ_mul (n m : mynat) : mymul n (mysucc m) = myadd (mymul n m) n := begin unfold mymul, rw add_comm, end lemma mul_comm (n m : mynat) : mymul n m = mymul m n := begin induction n, rw zero_mul, unfold mymul, rw mul_succ, rw n_ih, unfold mymul, rw add_comm, end lemma mul_add (t a b : mynat) : (mymul t (myadd a b)) = (myadd (mymul t a) (mymul t b)) := begin induction b, unfold mymul, unfold myadd, repeat {rw add_succ}, repeat {unfold mymul}, rw b_ih, rw add_comm (mymul t a), rw <- add_assoc, rw add_comm, end lemma mul_assoc (a b c : mynat) : mymul (mymul a b) c = mymul a (mymul b c) := begin induction c, unfold mymul, repeat {rw succ_mul}, rw mul_add, rw c_ih, end lemma mul_add_distrib (a b c : mynat) : mymul (myadd a b) c = myadd (mymul a c) (mymul b c) := begin induction c with c1, unfold mymul, unfold myadd, unfold mymul, rw c_ih, rw add_comm a b, rw add_assoc, rw add_comm, rw add_comm b (mymul b c1), repeat {rw add_assoc}, end lemma mul_add_distrib_alt (a b c : mynat) : mymul a (myadd b c) = myadd (mymul a b) (mymul a c) := begin rw mul_comm, rw mul_add_distrib, rw mul_comm b a, rw mul_comm c a, end lemma mul_add_distrib_rev (a b c : mynat) : myadd (mymul a b) (mymul a c) = mymul (myadd b c) a := begin rw mul_comm, rw mul_comm a c, rw <- mul_add_distrib, end lemma mul_two (a : mynat) : mymul zero.mysucc.mysucc a = myadd a a := begin rw mul_comm, unfold mymul, rw add_zero, end lemma mul_result_zero (a b c : mynat) : (a = mysucc(c)) -> ((mymul a b = zero) -> (b = zero)) := begin intros h1 h2, rw h1 at h2, cases b, refl, unfold mymul at h2, rw add_comm at h2, unfold myadd at h2, contradiction, end lemma mul_cancel (a b c : mynat) : (mymul a (mysucc c) = mymul b (mysucc c)) -> (a = b) := begin rw mul_comm, rw mul_comm b c.mysucc, revert b, induction a with a1, intro b, cases b, intros, refl, intros h, repeat {unfold mymul at h}, rw add_comm at h, unfold myadd at h, contradiction, intro b, cases b, intros h, repeat {unfold mymul at h}, rw add_comm at h, unfold myadd at h, contradiction, intros h1, unfold mymul at h1, have h2 : (mymul c.mysucc a1) = (mymul c.mysucc b), exact add_cancel_a_rev c.mysucc (mymul c.mysucc a1) (mymul c.mysucc b) h1, apply succ_eq a1 b, exact a_ih b h2, end lemma mul_zero_collapse (a b c : mynat) : ¬(a = b) -> ((mymul a c = mymul b c) -> c = zero) := begin intros h1 h2, cases c, refl, have absurd : a = b, exact mul_cancel a b c h2, exfalso, exact h1 absurd, end lemma add_self_eq_mul_two (a : mynat) : (myadd a a) = (mymul (zero.mysucc.mysucc) a) := begin induction a with a1, unfold myadd, unfold mymul, unfold mymul, unfold myadd, rw add_comm, unfold myadd, rw add_comm zero.mysucc.mysucc (mymul zero.mysucc.mysucc a1), repeat {unfold myadd}, rw a_ih, end lemma add_self (a b : mynat) : (myadd a a = myadd b b) -> (a = b) := begin repeat {rw add_self_eq_mul_two}, intros h, rw mul_comm zero.mysucc.mysucc a at h, rw mul_comm zero.mysucc.mysucc b at h, exact mul_cancel a b zero.mysucc h, end lemma add_self_inv (a b : mynat) : ¬(a = b) -> ¬(myadd a a = myadd b b) := begin intros h, intros h2, exact h (add_self a b h2), end lemma mul_weird_sub_a (a b c d : mynat) : (∃e : mynat, d = (myadd c (mysucc e))) -> (myadd (mymul a c) (mymul b d)) = (myadd (mymul b c) (mymul a d)) -> (a = b) := begin intros h1 h2, cases h1 with e h1, rw h1 at h2, rw mul_add_distrib_alt b c e.mysucc at h2, rw mul_add_distrib_alt a c e.mysucc at h2, rw <- add_assoc (mymul a c) (mymul b c) (mymul b e.mysucc) at h2, rw <- add_assoc (mymul b c) (mymul a c) (mymul a e.mysucc) at h2, rw add_comm (mymul a c) (mymul b c) at h2, have tmp : (mymul b e.mysucc) = (mymul a e.mysucc), exact add_cancel_a_rev (myadd (mymul b c) (mymul a c)) (mymul b e.mysucc) (mymul a e.mysucc) h2, symmetry, exact mul_cancel b a e tmp, end lemma difference_exists (a b : mynat) : (∃c : mynat, (myadd a c) = b) \/ (∃c : mynat, a = (myadd b c)) := begin induction a, left, existsi b, rw zero_add, cases a_ih with h h, cases h with c hc, cases c, rw add_zero at hc, rw hc, right, existsi (zero.mysucc), unfold myadd, left, existsi c, unfold myadd at hc, rw succ_add, exact hc, cases h with c hc, cases c, rw add_zero at hc, rw hc, right, existsi (zero.mysucc), unfold myadd, right, existsi c.mysucc.mysucc, rw hc, unfold myadd, end lemma ne_implies_nonzero_diff (a b : mynat) : (a ≠ b) -> ((∃c : mynat, b = (myadd a c.mysucc)) \/ (∃c : mynat, a = (myadd b c.mysucc))) := begin intro h, have diff_h : (∃d : mynat, (myadd a d) = b) \/ (∃d : mynat, a = (myadd b d)), exact difference_exists a b, cases diff_h, left, cases diff_h with d hd, cases d, rw add_zero at hd, exfalso, exact h hd, existsi d, symmetry, exact hd, right, cases diff_h with d hd, cases d, rw add_zero at hd, exfalso, exact h hd, existsi d, exact hd, end /- This is a weird formula but we need it later, OK...-/ lemma mul_weird_sub (a b c d : mynat) : (c ≠ d) -> (myadd (mymul a c) (mymul b d)) = (myadd (mymul b c) (mymul a d)) -> (a = b) := begin intros h1 h2, cases (ne_implies_nonzero_diff c d h1) with h3 h3, exact mul_weird_sub_a a b c d h3 h2, symmetry, rw add_comm (mymul a c) (mymul b d) at h2, rw add_comm (mymul b c) (mymul a d) at h2, exact mul_weird_sub_a b a d c h3 h2, end /- We represent integers as a pair of natural numbers, where (a, b) represents (a - b).-/ inductive myint : Type | int : mynat -> mynat -> myint open myint def int_zero : myint := (int zero zero) def int_one : myint := (int nat_one zero) def iszero : myint -> Prop | (int a b) := (a = b) /- Because of the implementation, we need a new version of equals.-/ /- e.g. (0, 0) = (1, 1) as both represent zero, even though they are representationally different.-/ def int_equal : myint -> myint -> Prop | (int a b) (int c d) := ((myadd a d) = (myadd b c)) lemma int_zeros_eq (a b : myint) : (iszero a) -> (iszero b) -> (int_equal a b) := begin cases a with aplus aminus, cases b with bplus bminus, unfold iszero, unfold int_equal, intros h1 h2, rw h1, rw h2, end lemma int_zeros_only_eq (a b : myint) : (iszero a) -> ((int_equal a b) -> (iszero b)) := begin cases a with aplus aminus, cases b with bplus bminus, unfold iszero, unfold int_equal, intros h1 h2, rw h1 at h2, rw add_comm aminus bminus at h2, rw add_comm aminus bplus at h2, apply add_cancel_a aminus bplus bminus, symmetry, exact h2, end lemma int_zeros_only_eq_rev (a b : myint) : (iszero a) -> ((int_equal b a) -> (iszero b)) := begin cases a with aplus aminus, cases b with bplus bminus, unfold iszero, unfold int_equal, intros h1 h2, rw h1 at h2, apply add_cancel_a aminus bplus bminus, exact h2, end lemma int_equal_refl (a : myint) : (int_equal a a) := begin cases a with aplus aminus, unfold int_equal, rw add_comm, end lemma int_eq_comm (a b : myint) : (int_equal a b) -> (int_equal b a) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_equal, intros h, rw add_comm bplus aminus, rw add_comm bminus aplus, symmetry, exact h, end lemma int_eq_comm_double (a b : myint) : (int_equal a b) <-> (int_equal b a) := begin split, exact int_eq_comm a b, exact int_eq_comm b a, end lemma int_eq_trans (a b c : myint) : (int_equal a b) -> (int_equal b c) -> (int_equal a c) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold int_equal, intros h1 h2, have h3 : (myadd (myadd aplus bminus) (myadd bplus cminus) = myadd (myadd aminus bplus) (myadd bminus cplus)), exact add_combine_eq (myadd aplus bminus) (myadd aminus bplus) (myadd bplus cminus) (myadd bminus cplus) h1 h2, apply add_cancel_a (myadd bplus bminus) (myadd aplus cminus) (myadd aminus cplus), rw add_comm bplus bminus, rw add_comm_in_tree, rw add_comm cminus bplus, symmetry, rw add_comm bminus bplus, rw add_comm_in_tree, symmetry, rw add_comm cplus bminus, exact h3, end def int_add : myint -> myint -> myint | (int a b) (int c d) := (int (myadd a c) (myadd b d)) lemma add_zero_int (a b : myint) : (iszero a) -> (int_equal b (int_add a b)) := begin intros h, cases a with aplus aminus, unfold iszero at h, cases b with bplus bminus, unfold int_add, unfold int_equal, rw h, rw <- add_assoc, rw add_comm, have h : myadd bplus aminus = myadd aminus bplus, rw add_comm, rw h, end lemma add_zero_int_alt (a b : myint) : (iszero a) -> (int_equal (int_add a b) b) := begin intros h, have int : int_equal b (int_add a b), exact add_zero_int a b h, exact int_eq_comm b (int_add a b) int, end lemma add_comm_int (a b : myint) : int_equal (int_add a b) (int_add b a) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_add, unfold int_equal, rw add_comm (myadd aplus bplus) (myadd bminus aminus), rw add_comm aminus bminus, rw add_comm aplus bplus, end lemma add_comm_int_rw (a b : myint) : (int_add a b) = (int_add b a) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_add, rw add_comm bplus aplus, rw add_comm bminus aminus, end lemma add_eq (a b c : myint) : int_equal b c <-> int_equal (int_add a b) (int_add a c) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, split, intros h, unfold int_equal at h, unfold int_add, unfold int_equal, rw add_assoc, rw add_comm aminus cminus, rw <- add_assoc bplus cminus aminus, rw h, rw add_assoc, rw add_comm cplus aminus, rw <- add_assoc, rw <- add_assoc, rw add_comm aplus bminus, rw add_comm (myadd bminus aplus) aminus, repeat {rw add_assoc}, intros h, unfold int_equal, unfold int_add at h, unfold int_equal at h, rw add_assoc at h, rw add_comm at h, rw add_comm aminus cminus at h, rw add_comm aplus cplus at h, rw add_comm aminus bminus at h, rw add_assoc bminus aminus (myadd cplus aplus) at h, rw <- add_assoc aminus cplus aplus at h, rw add_comm aminus cplus at h, rw add_assoc cplus aminus aplus at h, rw <- add_assoc bminus cplus (myadd aminus aplus) at h, rw add_assoc at h, rw add_assoc cminus aminus aplus at h, rw <- add_assoc bplus cminus (myadd aminus aplus) at h, exact add_cancel_a (myadd aminus aplus) (myadd bplus cminus) (myadd bminus cplus) h, end lemma add_assoc_int (a b c : myint) : int_equal (int_add (int_add a b) c) (int_add a (int_add b c)) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold int_add, unfold int_equal, rw add_comm, repeat {rw add_assoc}, end lemma add_assoc_int_rw (a b c : myint) : (int_add (int_add a b) c) = (int_add a (int_add b c)) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold int_add, repeat {rw add_assoc}, end lemma add_sub_int (a b c d : myint) : (int_equal a b) -> (int_equal (int_add a c) d) -> (int_equal (int_add b c) d) := begin intros h1 h2, have h3 : int_equal a b <-> int_equal (int_add c a) (int_add c b), exact add_eq c a b, cases h3 with h3 _, have h4 : int_equal (int_add c a) (int_add c b), exact h3 h1, rw add_comm_int_rw c a at h4, rw add_comm_int_rw c b at h4, have h5 : int_equal d (int_add a c), exact int_eq_comm (int_add a c) d h2, apply int_eq_comm d (int_add b c), exact int_eq_trans d (int_add a c) (int_add b c) h5 h4, end lemma int_add_self_nonzero (a : myint) : ¬(iszero a) -> ¬(iszero (int_add a a)) := begin cases a with aplus aminus, unfold int_add, unfold iszero, intros h, exact add_self_inv aplus aminus h, end /- (a - b)(c - d) = (ac + bd) - (ad + bc)-/ def int_mul : myint -> myint -> myint | (int a b) (int c d) := (int (myadd (mymul a c) (mymul b d)) (myadd (mymul a d) (mymul b c))) lemma mul_comm_int (a b : myint) : int_equal (int_mul a b) (int_mul b a) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_mul, unfold int_equal, rw add_comm, rw add_comm (mymul bplus aminus) (mymul bminus aplus), rw mul_comm bminus aplus, rw mul_comm bplus aminus, rw mul_comm aplus bplus, rw mul_comm aminus bminus, end lemma mul_comm_int_rw (a b : myint) : (int_mul a b) = (int_mul b a) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_mul, rw mul_comm bplus aplus, rw mul_comm bminus aminus, rw add_comm (mymul bplus aminus) (mymul bminus aplus), rw mul_comm bminus aplus, rw mul_comm bplus aminus, end /- Trivial consequence of previous results but makes some of the rewriting easier.-/ lemma add_helper_1 (a b c : mynat) : myadd (myadd a b) c = myadd (myadd a c) b := begin rw add_assoc, rw add_comm b c, rw <- add_assoc, end /- ditto -/ lemma add_helper_2 (a b c d : mynat) : myadd a (myadd (myadd b c) d) = myadd b (myadd (myadd a c) d) := begin repeat {rw <- add_assoc}, rw add_comm a b, end /- unlike for addition this implication only goes one way. (a * b) = (a * c) -> b = c is WRONG if a = 0. -/ lemma mul_eq (a b c : myint) : int_equal b c -> int_equal (int_mul a b) (int_mul a c) := begin intros h, cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold int_equal at h, unfold int_mul, unfold int_equal, rw add_comm (mymul aplus bplus) (mymul aminus bminus), repeat {rw add_assoc}, rw <- add_assoc (mymul aplus bplus) (mymul aplus cminus) (mymul aminus cplus), rw mul_comm aplus bplus, rw mul_comm aplus cminus, rw <- mul_add_distrib, rw h, rw mul_add_distrib, rw add_comm (mymul aplus cplus) (mymul aminus cminus), rw <- add_assoc (mymul aminus bplus) (mymul aminus cminus) (mymul aplus cplus), rw mul_comm aminus bplus, rw mul_comm aminus cminus, rw <- mul_add_distrib bplus cminus aminus, rw h, rw mul_add_distrib, rw add_helper_1 (mymul bminus aminus) (mymul cplus aminus) (mymul aplus cplus), rw add_helper_2, rw mul_comm bminus aplus, rw mul_comm aminus bminus, rw mul_comm cplus aplus, rw mul_comm aminus cplus, end lemma mul_eq_alt (a b c : myint) : int_equal b c -> int_equal (int_mul b a) (int_mul c a) := begin intros h, have int1 : int_equal (int_mul a b) (int_mul a c), exact mul_eq a b c h, have int2 : int_equal (int_mul a b) (int_mul b a), exact mul_comm_int a b, have int3 : int_equal (int_mul b a) (int_mul a c), exact int_eq_trans (int_mul b a) (int_mul a b) (int_mul a c) (int_eq_comm (int_mul a b) (int_mul b a) int2) int1, exact int_eq_trans (int_mul b a) (int_mul a c) (int_mul c a) int3 (mul_comm_int a c), end attribute [simp] lemma mul_assoc_int (a b c : myint) : int_equal (int_mul (int_mul a b) c) (int_mul a (int_mul b c)) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold int_mul, unfold int_equal, repeat {rw mul_add_distrib}, repeat {rw mul_add_distrib_alt}, /- The below code was generated with an external python script.-/ /- It is almost certainly not the most efficient way to do this, but it works...-/ rw add_comm (mymul aminus (mymul bplus cplus)) (mymul aminus (mymul bminus cminus)), rw <- add_assoc (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus))) (mymul aminus (mymul bminus cminus)) (mymul aminus (mymul bplus cplus)), rw add_comm (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus))) (mymul aminus (mymul bminus cminus)), rw <- add_assoc (myadd (myadd (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bminus) cplus)) (myadd (mymul (mymul aplus bminus) cminus) (mymul (mymul aminus bplus) cminus))) (myadd (mymul aminus (mymul bminus cminus)) (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus)))) (mymul aminus (mymul bplus cplus)), rw <- add_assoc (myadd (myadd (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bminus) cplus)) (myadd (mymul (mymul aplus bminus) cminus) (mymul (mymul aminus bplus) cminus))) (mymul aminus (mymul bminus cminus)) (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus))), rw add_comm (myadd (myadd (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bminus) cplus)) (myadd (mymul (mymul aplus bminus) cminus) (mymul (mymul aminus bplus) cminus))) (mymul aminus (mymul bminus cminus)), rw add_comm (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bminus) cplus), rw <- add_assoc (mymul aminus (mymul bminus cminus)) (myadd (mymul (mymul aminus bminus) cplus) (mymul (mymul aplus bplus) cplus)) (myadd (mymul (mymul aplus bminus) cminus) (mymul (mymul aminus bplus) cminus)), rw <- add_assoc (mymul aminus (mymul bminus cminus)) (mymul (mymul aminus bminus) cplus) (mymul (mymul aplus bplus) cplus), rw add_comm (mymul aminus (mymul bminus cminus)) (mymul (mymul aminus bminus) cplus), rw add_comm (mymul (mymul aplus bminus) cminus) (mymul (mymul aminus bplus) cminus), rw <- add_assoc (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus)) (mymul (mymul aminus bplus) cminus) (mymul (mymul aplus bminus) cminus), rw add_comm (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus)) (mymul (mymul aminus bplus) cminus), rw add_comm (myadd (myadd (myadd (mymul (mymul aminus bplus) cminus) (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus))) (mymul (mymul aplus bminus) cminus)) (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus)))) (mymul aminus (mymul bplus cplus)), rw add_comm (myadd (mymul (mymul aminus bplus) cminus) (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus))) (mymul (mymul aplus bminus) cminus), rw <- add_assoc (mymul aminus (mymul bplus cplus)) (myadd (mymul (mymul aplus bminus) cminus) (myadd (mymul (mymul aminus bplus) cminus) (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus)))) (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus))), rw <- add_assoc (mymul aminus (mymul bplus cplus)) (mymul (mymul aplus bminus) cminus) (myadd (mymul (mymul aminus bplus) cminus) (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus))), rw add_comm (mymul aminus (mymul bplus cplus)) (mymul (mymul aplus bminus) cminus), rw add_comm (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus)), rw <- add_assoc (myadd (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus))) (myadd (mymul (mymul aminus bplus) cminus) (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus)))) (mymul aplus (mymul bminus cplus)) (mymul aplus (mymul bplus cminus)), rw add_comm (myadd (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus))) (myadd (mymul (mymul aminus bplus) cminus) (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus)))) (mymul aplus (mymul bminus cplus)), rw add_comm (myadd (mymul aplus (mymul bminus cplus)) (myadd (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus))) (myadd (mymul (mymul aminus bplus) cminus) (myadd (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus))))) (mymul aplus (mymul bplus cminus)), rw add_comm (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))) (mymul (mymul aplus bplus) cplus), rw <- add_assoc (mymul (mymul aminus bplus) cminus) (mymul (mymul aplus bplus) cplus) (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))), rw add_comm (mymul (mymul aminus bplus) cminus) (mymul (mymul aplus bplus) cplus), rw <- add_assoc (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus))) (myadd (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bplus) cminus)) (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))), rw <- add_assoc (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus))) (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bplus) cminus), rw add_comm (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus))) (mymul (mymul aplus bplus) cplus), rw <- add_assoc (mymul aplus (mymul bminus cplus)) (myadd (myadd (mymul (mymul aplus bplus) cplus) (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus)))) (mymul (mymul aminus bplus) cminus)) (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))), rw <- add_assoc (mymul aplus (mymul bminus cplus)) (myadd (mymul (mymul aplus bplus) cplus) (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus)))) (mymul (mymul aminus bplus) cminus), rw <- add_assoc (mymul aplus (mymul bminus cplus)) (mymul (mymul aplus bplus) cplus) (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus))), rw add_comm (mymul aplus (mymul bminus cplus)) (mymul (mymul aplus bplus) cplus), rw <- add_assoc (mymul aplus (mymul bplus cminus)) (myadd (myadd (myadd (mymul (mymul aplus bplus) cplus) (mymul aplus (mymul bminus cplus))) (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus)))) (mymul (mymul aminus bplus) cminus)) (myadd (mymul (mymul aminus bminus) cplus) (mymul aminus (mymul bminus cminus))), rw <- add_assoc (mymul aplus (mymul bplus cminus)) (myadd (myadd (mymul (mymul aplus bplus) cplus) (mymul aplus (mymul bminus cplus))) (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus)))) (mymul (mymul aminus bplus) cminus), rw <- add_assoc (mymul aplus (mymul bplus cminus)) (myadd (mymul (mymul aplus bplus) cplus) (mymul aplus (mymul bminus cplus))) (myadd (mymul (mymul aplus bminus) cminus) (mymul aminus (mymul bplus cplus))), rw <- add_assoc (mymul aplus (mymul bplus cminus)) (mymul (mymul aplus bplus) cplus) (mymul aplus (mymul bminus cplus)), rw add_comm (mymul aplus (mymul bplus cminus)) (mymul (mymul aplus bplus) cplus), symmetry, rw add_comm (mymul (mymul aplus bplus) cminus) (mymul (mymul aminus bminus) cminus), rw add_comm (mymul aminus (mymul bplus cminus)) (mymul aminus (mymul bminus cplus)), rw <- add_assoc (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus))) (mymul aminus (mymul bminus cplus)) (mymul aminus (mymul bplus cminus)), rw add_comm (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus))) (mymul aminus (mymul bminus cplus)), rw <- add_assoc (myadd (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)) (myadd (mymul (mymul aplus bminus) cplus) (mymul (mymul aminus bplus) cplus))) (myadd (mymul aminus (mymul bminus cplus)) (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus)))) (mymul aminus (mymul bplus cminus)), rw <- add_assoc (myadd (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)) (myadd (mymul (mymul aplus bminus) cplus) (mymul (mymul aminus bplus) cplus))) (mymul aminus (mymul bminus cplus)) (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus))), rw add_comm (myadd (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)) (myadd (mymul (mymul aplus bminus) cplus) (mymul (mymul aminus bplus) cplus))) (mymul aminus (mymul bminus cplus)), rw add_comm (myadd (myadd (mymul aminus (mymul bminus cplus)) (myadd (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)) (myadd (mymul (mymul aplus bminus) cplus) (mymul (mymul aminus bplus) cplus)))) (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus)))) (mymul aminus (mymul bplus cminus)), rw add_comm (mymul (mymul aplus bminus) cplus) (mymul (mymul aminus bplus) cplus), rw <- add_assoc (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)) (mymul (mymul aminus bplus) cplus) (mymul (mymul aplus bminus) cplus), rw add_comm (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)) (mymul (mymul aminus bplus) cplus), rw <- add_assoc (mymul aminus (mymul bminus cplus)) (myadd (mymul (mymul aminus bplus) cplus) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus))) (mymul (mymul aplus bminus) cplus), rw <- add_assoc (mymul aminus (mymul bminus cplus)) (mymul (mymul aminus bplus) cplus) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)), rw add_comm (mymul aminus (mymul bminus cplus)) (mymul (mymul aminus bplus) cplus), rw <- add_assoc (mymul aminus (mymul bplus cminus)) (myadd (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bminus cplus))) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus))) (mymul (mymul aplus bminus) cplus)) (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus))), rw <- add_assoc (mymul aminus (mymul bplus cminus)) (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bminus cplus))) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus))) (mymul (mymul aplus bminus) cplus), rw <- add_assoc (mymul aminus (mymul bplus cminus)) (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bminus cplus))) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)), rw <- add_assoc (mymul aminus (mymul bplus cminus)) (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bminus cplus)), rw add_comm (mymul aminus (mymul bplus cminus)) (mymul (mymul aminus bplus) cplus), rw add_comm (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus)), rw <- add_assoc (myadd (myadd (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus))) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus))) (mymul (mymul aplus bminus) cplus)) (mymul aplus (mymul bminus cminus)) (mymul aplus (mymul bplus cplus)), rw add_comm (myadd (myadd (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus))) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus))) (mymul (mymul aplus bminus) cplus)) (mymul aplus (mymul bminus cminus)), rw add_comm (myadd (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus))) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus))) (mymul (mymul aplus bminus) cplus), rw <- add_assoc (mymul aplus (mymul bminus cminus)) (mymul (mymul aplus bminus) cplus) (myadd (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus))) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus))), rw add_comm (mymul aplus (mymul bminus cminus)) (mymul (mymul aplus bminus) cplus), rw add_comm (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus), rw <- add_assoc (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus))) (mymul (mymul aplus bplus) cminus) (mymul (mymul aminus bminus) cminus), rw add_comm (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus))) (mymul (mymul aplus bplus) cminus), rw <- add_assoc (myadd (mymul (mymul aplus bminus) cplus) (mymul aplus (mymul bminus cminus))) (myadd (mymul (mymul aplus bplus) cminus) (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus)))) (mymul (mymul aminus bminus) cminus), rw <- add_assoc (myadd (mymul (mymul aplus bminus) cplus) (mymul aplus (mymul bminus cminus))) (mymul (mymul aplus bplus) cminus) (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus))), rw add_comm (myadd (mymul (mymul aplus bminus) cplus) (mymul aplus (mymul bminus cminus))) (mymul (mymul aplus bplus) cminus), rw add_comm (myadd (myadd (myadd (mymul (mymul aplus bplus) cminus) (myadd (mymul (mymul aplus bminus) cplus) (mymul aplus (mymul bminus cminus)))) (myadd (myadd (mymul (mymul aminus bplus) cplus) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus)))) (mymul (mymul aminus bminus) cminus)) (mymul aplus (mymul bplus cplus)), repeat {rw add_assoc}, repeat {rw mul_assoc}, end lemma mul_assoc_int_rw (a b c : myint) : (int_mul (int_mul a b) c) = (int_mul a (int_mul b c)) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold int_mul, repeat {rw mul_add_distrib}, /- Also python. -/ rw add_comm (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bminus) cplus), rw add_comm (mymul (mymul aplus bminus) cminus) (mymul (mymul aminus bplus) cminus), rw <- add_assoc (myadd (mymul (mymul aminus bminus) cplus) (mymul (mymul aplus bplus) cplus)) (mymul (mymul aminus bplus) cminus) (mymul (mymul aplus bminus) cminus), rw add_comm (myadd (mymul (mymul aminus bminus) cplus) (mymul (mymul aplus bplus) cplus)) (mymul (mymul aminus bplus) cminus), rw add_comm (myadd (mymul (mymul aminus bplus) cminus) (myadd (mymul (mymul aminus bminus) cplus) (mymul (mymul aplus bplus) cplus))) (mymul (mymul aplus bminus) cminus), rw add_comm (mymul (mymul aminus bminus) cplus) (mymul (mymul aplus bplus) cplus), rw <- add_assoc (mymul (mymul aminus bplus) cminus) (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bminus) cplus), rw add_comm (mymul (mymul aminus bplus) cminus) (mymul (mymul aplus bplus) cplus), rw <- add_assoc (mymul (mymul aplus bminus) cminus) (myadd (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bplus) cminus)) (mymul (mymul aminus bminus) cplus), rw <- add_assoc (mymul (mymul aplus bminus) cminus) (mymul (mymul aplus bplus) cplus) (mymul (mymul aminus bplus) cminus), rw add_comm (mymul (mymul aplus bminus) cminus) (mymul (mymul aplus bplus) cplus), rw add_comm (mymul (mymul aplus bplus) cminus) (mymul (mymul aminus bminus) cminus), rw add_comm (mymul (mymul aplus bminus) cplus) (mymul (mymul aminus bplus) cplus), rw <- add_assoc (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)) (mymul (mymul aminus bplus) cplus) (mymul (mymul aplus bminus) cplus), rw add_comm (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus)) (mymul (mymul aminus bplus) cplus), rw add_comm (myadd (mymul (mymul aminus bplus) cplus) (myadd (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus))) (mymul (mymul aplus bminus) cplus), rw add_comm (mymul (mymul aminus bminus) cminus) (mymul (mymul aplus bplus) cminus), rw <- add_assoc (mymul (mymul aminus bplus) cplus) (mymul (mymul aplus bplus) cminus) (mymul (mymul aminus bminus) cminus), rw add_comm (mymul (mymul aminus bplus) cplus) (mymul (mymul aplus bplus) cminus), rw <- add_assoc (mymul (mymul aplus bminus) cplus) (myadd (mymul (mymul aplus bplus) cminus) (mymul (mymul aminus bplus) cplus)) (mymul (mymul aminus bminus) cminus), rw <- add_assoc (mymul (mymul aplus bminus) cplus) (mymul (mymul aplus bplus) cminus) (mymul (mymul aminus bplus) cplus), rw add_comm (mymul (mymul aplus bminus) cplus) (mymul (mymul aplus bplus) cminus), symmetry, repeat {rw mul_add_distrib_alt}, rw add_comm (mymul aminus (mymul bplus cminus)) (mymul aminus (mymul bminus cplus)), rw <- add_assoc (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus))) (mymul aminus (mymul bminus cplus)) (mymul aminus (mymul bplus cminus)), rw add_comm (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus))) (mymul aminus (mymul bminus cplus)), rw add_comm (myadd (mymul aminus (mymul bminus cplus)) (myadd (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus)))) (mymul aminus (mymul bplus cminus)), rw add_comm (mymul aplus (mymul bplus cplus)) (mymul aplus (mymul bminus cminus)), rw <- add_assoc (mymul aminus (mymul bminus cplus)) (mymul aplus (mymul bminus cminus)) (mymul aplus (mymul bplus cplus)), rw add_comm (mymul aminus (mymul bminus cplus)) (mymul aplus (mymul bminus cminus)), rw <- add_assoc (mymul aminus (mymul bplus cminus)) (myadd (mymul aplus (mymul bminus cminus)) (mymul aminus (mymul bminus cplus))) (mymul aplus (mymul bplus cplus)), rw <- add_assoc (mymul aminus (mymul bplus cminus)) (mymul aplus (mymul bminus cminus)) (mymul aminus (mymul bminus cplus)), rw add_comm (mymul aminus (mymul bplus cminus)) (mymul aplus (mymul bminus cminus)), rw add_comm (myadd (myadd (mymul aplus (mymul bminus cminus)) (mymul aminus (mymul bplus cminus))) (mymul aminus (mymul bminus cplus))) (mymul aplus (mymul bplus cplus)), rw add_comm (mymul aminus (mymul bplus cplus)) (mymul aminus (mymul bminus cminus)), rw <- add_assoc (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus))) (mymul aminus (mymul bminus cminus)) (mymul aminus (mymul bplus cplus)), rw add_comm (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus))) (mymul aminus (mymul bminus cminus)), rw add_comm (myadd (mymul aminus (mymul bminus cminus)) (myadd (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus)))) (mymul aminus (mymul bplus cplus)), rw add_comm (mymul aplus (mymul bplus cminus)) (mymul aplus (mymul bminus cplus)), rw <- add_assoc (mymul aminus (mymul bminus cminus)) (mymul aplus (mymul bminus cplus)) (mymul aplus (mymul bplus cminus)), rw add_comm (mymul aminus (mymul bminus cminus)) (mymul aplus (mymul bminus cplus)), rw <- add_assoc (mymul aminus (mymul bplus cplus)) (myadd (mymul aplus (mymul bminus cplus)) (mymul aminus (mymul bminus cminus))) (mymul aplus (mymul bplus cminus)), rw <- add_assoc (mymul aminus (mymul bplus cplus)) (mymul aplus (mymul bminus cplus)) (mymul aminus (mymul bminus cminus)), rw add_comm (mymul aminus (mymul bplus cplus)) (mymul aplus (mymul bminus cplus)), rw add_comm (myadd (myadd (mymul aplus (mymul bminus cplus)) (mymul aminus (mymul bplus cplus))) (mymul aminus (mymul bminus cminus))) (mymul aplus (mymul bplus cminus)), repeat {rw mul_assoc}, repeat {rw add_assoc}, end /- This will be useful later.-/ lemma mul_tree_swap_int_rw (a b c d : myint) : (int_mul (int_mul a b) (int_mul c d)) = (int_mul (int_mul a c) (int_mul b d)) := begin rw mul_assoc_int_rw a b (int_mul c d), rw <- mul_assoc_int_rw b c d, rw mul_comm_int_rw b c, rw mul_assoc_int_rw c b d, rw <- mul_assoc_int_rw a c (int_mul b d), end lemma mul_distrib_int (a b c : myint) : int_equal (int_mul (int_add a b) c) (int_add (int_mul a c) (int_mul b c)) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold int_mul, unfold int_add, unfold int_mul, unfold int_equal, repeat {rw mul_add_distrib}, /- Ditto. Rewriting these massive chunks of addition manually seems like a bad use of time, even though it might produce marginally better code in the end.-/ rw <- add_assoc (myadd (mymul aplus cplus) (mymul bplus cplus)) (mymul aminus cminus) (mymul bminus cminus), rw add_comm (myadd (mymul aplus cplus) (mymul bplus cplus)) (mymul aminus cminus), rw add_comm (mymul aplus cminus) (mymul aminus cplus), rw <- add_assoc (myadd (myadd (mymul aminus cminus) (myadd (mymul aplus cplus) (mymul bplus cplus))) (mymul bminus cminus)) (myadd (mymul aminus cplus) (mymul aplus cminus)) (myadd (mymul bplus cminus) (mymul bminus cplus)), rw <- add_assoc (myadd (myadd (mymul aminus cminus) (myadd (mymul aplus cplus) (mymul bplus cplus))) (mymul bminus cminus)) (mymul aminus cplus) (mymul aplus cminus), rw add_comm (myadd (myadd (mymul aminus cminus) (myadd (mymul aplus cplus) (mymul bplus cplus))) (mymul bminus cminus)) (mymul aminus cplus), rw add_comm (myadd (mymul aminus cplus) (myadd (myadd (mymul aminus cminus) (myadd (mymul aplus cplus) (mymul bplus cplus))) (mymul bminus cminus))) (mymul aplus cminus), rw <- add_assoc (mymul aminus cminus) (mymul aplus cplus) (mymul bplus cplus), rw add_comm (mymul aminus cminus) (mymul aplus cplus), rw <- add_assoc (mymul aminus cplus) (myadd (myadd (mymul aplus cplus) (mymul aminus cminus)) (mymul bplus cplus)) (mymul bminus cminus), rw <- add_assoc (mymul aminus cplus) (myadd (mymul aplus cplus) (mymul aminus cminus)) (mymul bplus cplus), rw <- add_assoc (mymul aminus cplus) (mymul aplus cplus) (mymul aminus cminus), rw add_comm (mymul aminus cplus) (mymul aplus cplus), rw <- add_assoc (mymul aplus cminus) (myadd (myadd (myadd (mymul aplus cplus) (mymul aminus cplus)) (mymul aminus cminus)) (mymul bplus cplus)) (mymul bminus cminus), rw <- add_assoc (mymul aplus cminus) (myadd (myadd (mymul aplus cplus) (mymul aminus cplus)) (mymul aminus cminus)) (mymul bplus cplus), rw <- add_assoc (mymul aplus cminus) (myadd (mymul aplus cplus) (mymul aminus cplus)) (mymul aminus cminus), rw <- add_assoc (mymul aplus cminus) (mymul aplus cplus) (mymul aminus cplus), rw add_comm (mymul aplus cminus) (mymul aplus cplus), rw add_comm (myadd (myadd (myadd (myadd (mymul aplus cplus) (mymul aplus cminus)) (mymul aminus cplus)) (mymul aminus cminus)) (mymul bplus cplus)) (mymul bminus cminus), rw add_comm (mymul bplus cminus) (mymul bminus cplus), rw <- add_assoc (myadd (mymul bminus cminus) (myadd (myadd (myadd (myadd (mymul aplus cplus) (mymul aplus cminus)) (mymul aminus cplus)) (mymul aminus cminus)) (mymul bplus cplus))) (mymul bminus cplus) (mymul bplus cminus), rw add_comm (myadd (mymul bminus cminus) (myadd (myadd (myadd (myadd (mymul aplus cplus) (mymul aplus cminus)) (mymul aminus cplus)) (mymul aminus cminus)) (mymul bplus cplus))) (mymul bminus cplus), rw add_comm (myadd (mymul bminus cplus) (myadd (mymul bminus cminus) (myadd (myadd (myadd (myadd (mymul aplus cplus) (mymul aplus cminus)) (mymul aminus cplus)) (mymul aminus cminus)) (mymul bplus cplus)))) (mymul bplus cminus), rw add_comm (myadd (myadd (myadd (mymul aplus cplus) (mymul aplus cminus)) (mymul aminus cplus)) (mymul aminus cminus)) (mymul bplus cplus), rw <- add_assoc (mymul bminus cminus) (mymul bplus cplus) (myadd (myadd (myadd (mymul aplus cplus) (mymul aplus cminus)) (mymul aminus cplus)) (mymul aminus cminus)), rw add_comm (mymul bminus cminus) (mymul bplus cplus), rw <- add_assoc (mymul bminus cplus) (myadd (mymul bplus cplus) (mymul bminus cminus)) (myadd (myadd (myadd (mymul aplus cplus) (mymul aplus cminus)) (mymul aminus cplus)) (mymul aminus cminus)), rw <- add_assoc (mymul bminus cplus) (mymul bplus cplus) (mymul bminus cminus), rw add_comm (mymul bminus cplus) (mymul bplus cplus), rw <- add_assoc (mymul bplus cminus) (myadd (myadd (mymul bplus cplus) (mymul bminus cplus)) (mymul bminus cminus)) (myadd (myadd (myadd (mymul aplus cplus) (mymul aplus cminus)) (mymul aminus cplus)) (mymul aminus cminus)), rw <- add_assoc (mymul bplus cminus) (myadd (mymul bplus cplus) (mymul bminus cplus)) (mymul bminus cminus), rw <- add_assoc (mymul bplus cminus) (mymul bplus cplus) (mymul bminus cplus), rw add_comm (mymul bplus cminus) (mymul bplus cplus), symmetry, rw add_comm (mymul aplus cplus) (mymul aminus cminus), rw <- add_assoc (myadd (myadd (mymul aplus cminus) (mymul bplus cminus)) (myadd (mymul aminus cplus) (mymul bminus cplus))) (myadd (mymul aminus cminus) (mymul aplus cplus)) (myadd (mymul bplus cplus) (mymul bminus cminus)), rw <- add_assoc (myadd (myadd (mymul aplus cminus) (mymul bplus cminus)) (myadd (mymul aminus cplus) (mymul bminus cplus))) (mymul aminus cminus) (mymul aplus cplus), rw add_comm (myadd (myadd (mymul aplus cminus) (mymul bplus cminus)) (myadd (mymul aminus cplus) (mymul bminus cplus))) (mymul aminus cminus), rw <- add_assoc (myadd (mymul aplus cminus) (mymul bplus cminus)) (mymul aminus cplus) (mymul bminus cplus), rw add_comm (myadd (mymul aplus cminus) (mymul bplus cminus)) (mymul aminus cplus), rw <- add_assoc (mymul aminus cminus) (myadd (mymul aminus cplus) (myadd (mymul aplus cminus) (mymul bplus cminus))) (mymul bminus cplus), rw <- add_assoc (mymul aminus cminus) (mymul aminus cplus) (myadd (mymul aplus cminus) (mymul bplus cminus)), rw add_comm (mymul aminus cminus) (mymul aminus cplus), rw <- add_assoc (myadd (mymul aminus cplus) (mymul aminus cminus)) (mymul aplus cminus) (mymul bplus cminus), rw add_comm (myadd (mymul aminus cplus) (mymul aminus cminus)) (mymul aplus cminus), rw add_comm (myadd (myadd (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus))) (mymul bplus cminus)) (mymul bminus cplus)) (mymul aplus cplus), rw add_comm (mymul bplus cplus) (mymul bminus cminus), rw <- add_assoc (myadd (mymul aplus cplus) (myadd (myadd (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus))) (mymul bplus cminus)) (mymul bminus cplus))) (mymul bminus cminus) (mymul bplus cplus), rw add_comm (myadd (mymul aplus cplus) (myadd (myadd (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus))) (mymul bplus cminus)) (mymul bminus cplus))) (mymul bminus cminus), rw add_comm (myadd (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus))) (mymul bplus cminus)) (mymul bminus cplus), rw <- add_assoc (mymul aplus cplus) (mymul bminus cplus) (myadd (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus))) (mymul bplus cminus)), rw add_comm (mymul aplus cplus) (mymul bminus cplus), rw <- add_assoc (mymul bminus cminus) (myadd (mymul bminus cplus) (mymul aplus cplus)) (myadd (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus))) (mymul bplus cminus)), rw <- add_assoc (mymul bminus cminus) (mymul bminus cplus) (mymul aplus cplus), rw add_comm (mymul bminus cminus) (mymul bminus cplus), rw add_comm (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus))) (mymul bplus cminus), rw <- add_assoc (myadd (myadd (mymul bminus cplus) (mymul bminus cminus)) (mymul aplus cplus)) (mymul bplus cminus) (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus))), rw add_comm (myadd (myadd (mymul bminus cplus) (mymul bminus cminus)) (mymul aplus cplus)) (mymul bplus cminus), rw add_comm (myadd (myadd (mymul bplus cminus) (myadd (myadd (mymul bminus cplus) (mymul bminus cminus)) (mymul aplus cplus))) (myadd (mymul aplus cminus) (myadd (mymul aminus cplus) (mymul aminus cminus)))) (mymul bplus cplus), repeat {rw add_assoc}, end lemma mul_distrib_int_rw (a b c : myint) : (int_mul (int_add a b) c) = (int_add (int_mul a c) (int_mul b c)) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold int_mul, unfold int_add, unfold int_mul, repeat {rw mul_add_distrib}, rw add_comm_in_tree (mymul aplus cplus) (mymul bplus cplus) (mymul aminus cminus) (mymul bminus cminus), rw add_comm_in_tree (mymul aplus cminus) (mymul bplus cminus) (mymul aminus cplus) (mymul bminus cplus), end lemma mul_distrib_int_rw_alt (a b c : myint) : (int_mul a (int_add b c)) = (int_add (int_mul a b) (int_mul a c)) := begin rw mul_comm_int_rw, rw mul_distrib_int_rw, rw mul_comm_int_rw b a, rw mul_comm_int_rw c a, end lemma mul_zero_int (a b : myint) : (iszero a) -> (iszero (int_mul a b)) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_mul, unfold iszero, intros h, rw h, rw add_comm, end lemma mul_zero_int_alt (a b : myint) : (iszero a) -> (iszero (int_mul b a)) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_mul, unfold iszero, intros h, rw h, end lemma mul_zero_int_zero (a : myint) : int_equal (int_mul a int_zero) int_zero := begin cases a with aplus aminus, unfold int_zero, unfold int_mul, rw mul_zero, rw zero_add, rw mul_zero, exact int_equal_refl (int zero zero), end lemma mul_result_zero_int (a b : myint) : (¬iszero a) -> (iszero (int_mul a b)) -> (iszero b) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_mul, unfold iszero, intros h1 h2, rw mul_comm aplus bplus at h2, rw mul_comm aminus bminus at h2, rw mul_comm aplus bminus at h2, rw mul_comm aminus bplus at h2, exact mul_weird_sub bplus bminus aplus aminus h1 h2, end lemma mul_result_zero_int_rev (a b : myint) : (¬iszero b) -> (iszero (int_mul a b)) -> iszero a := begin intros h1 h2, rw mul_comm_int_rw at h2, exact mul_result_zero_int b a h1 h2, end lemma mul_one_int (a : myint) : int_equal a (int_mul a int_one) := begin cases a with aplus aminus, unfold int_one, unfold nat_one, unfold int_mul, unfold int_equal, repeat {rw mul_one_eq}, repeat {rw mul_zero}, rw add_zero, rw zero_add, rw add_comm, end lemma mul_one_int_rw (a : myint) : (int_mul a int_one) = a := begin cases a with aplus aminus, unfold int_one, unfold nat_one, unfold int_mul, repeat {rw mul_zero}, rw add_zero, rw zero_add, repeat {rw mul_one_eq}, end lemma mul_one_int_rw_alt (a : myint) : (int_mul (int zero.mysucc zero) a) = a := begin cases a with plus minus, unfold int_mul, repeat {rw zero_mul}, repeat {rw add_zero}, repeat {rw one_mul_eq}, end lemma mul_comm_zero_int (a b : myint) : (iszero (int_mul a b)) = (iszero (int_mul b a)) := begin have h1 : int_equal (int_mul a b) (int_mul b a), exact mul_comm_int a b, by_cases (iszero (int_mul a b)), have h2 : (iszero (int_mul b a)), exact int_zeros_only_eq (int_mul a b) (int_mul b a) h h1, cc, by_cases (iszero (int_mul b a)), have absurd : (iszero (int_mul a b)), exact int_zeros_only_eq (int_mul b a) (int_mul a b) h (mul_comm_int b a), contradiction, cc, end lemma mul_sub_int (a b c d : myint) : (int_equal a b) -> (int_equal (int_mul a c) d) -> (int_equal (int_mul b c) d) := begin intros h1 h2, exact int_eq_trans (int_mul b c) (int_mul a c) d (int_eq_comm (int_mul a c) (int_mul b c) (mul_eq_alt c a b h1)) h2, end lemma multiply_two_equalities_int (a b c d : myint) : int_equal a b -> int_equal c d -> (int_equal (int_mul a c) (int_mul b d)) := begin intros h1 h2, have tmp1 : int_equal (int_mul b d) (int_mul b d), exact int_equal_refl (int_mul b d), have tmp2 : int_equal (int_mul a d) (int_mul b d), exact mul_sub_int b a d (int_mul b d) (int_eq_comm a b h1) tmp1, have tmp3 : int_equal (int_mul a d) (int_mul d a), exact mul_comm_int a d, have tmp4 : int_equal (int_mul d a) (int_mul a c), exact mul_sub_int c d a (int_mul a c) h2 (mul_comm_int c a), exact int_eq_trans (int_mul a c) (int_mul a d) (int_mul b d) (int_eq_comm (int_mul a d) (int_mul a c) (int_eq_trans (int_mul a d) (int_mul d a) (int_mul a c) (mul_comm_int a d) tmp4)) tmp2, end lemma stupid (a b : mynat) : (a = b) = (b = a) := begin by_cases (a = b), cc, cc, end lemma mul_cancel_int (a b c : myint) : (¬ iszero b) -> (int_equal (int_mul a b) (int_mul c b)) -> (int_equal a c) := begin cases a with aplus aminus, cases b with bplus bminus, cases c with cplus cminus, unfold iszero, unfold int_mul, unfold int_equal, intros b_not_zero, intros h, /- Two python-generated lines.-/ rw add_comm (mymul aplus bplus) (mymul aminus bminus) at h, rw <- add_assoc (myadd (mymul aminus bminus) (mymul aplus bplus)) (mymul cplus bminus) (mymul cminus bplus) at h, rw add_comm (myadd (mymul aminus bminus) (mymul aplus bplus)) (mymul cplus bminus) at h, rw add_comm (mymul aminus bminus) (mymul aplus bplus) at h, rw <- add_assoc (mymul cplus bminus) (mymul aplus bplus) (mymul aminus bminus) at h, rw add_comm (mymul cplus bminus) (mymul aplus bplus) at h, rw add_comm (myadd (myadd (mymul aplus bplus) (mymul cplus bminus)) (mymul aminus bminus)) (mymul cminus bplus) at h, rw stupid at h, rw add_comm (mymul cplus bplus) (mymul cminus bminus) at h, rw <- add_assoc (myadd (mymul aplus bminus) (mymul aminus bplus)) (mymul cminus bminus) (mymul cplus bplus) at h, rw add_comm (myadd (mymul aplus bminus) (mymul aminus bplus)) (mymul cminus bminus) at h, rw add_comm (mymul aplus bminus) (mymul aminus bplus) at h, rw <- add_assoc (mymul cminus bminus) (mymul aminus bplus) (mymul aplus bminus) at h, rw add_comm (mymul cminus bminus) (mymul aminus bplus) at h, rw add_comm (myadd (myadd (mymul aminus bplus) (mymul cminus bminus)) (mymul aplus bminus)) (mymul cplus bplus) at h, repeat {rw add_assoc at h}, rw <- add_assoc (mymul cplus bplus) (mymul aminus bplus) (myadd (mymul cminus bminus) (mymul aplus bminus)) at h, rw <- mul_add_distrib cplus aminus bplus at h, rw <- mul_add_distrib cminus aplus bminus at h, rw <- add_assoc (mymul cminus bplus) (mymul aplus bplus) (myadd (mymul cplus bminus) (mymul aminus bminus)) at h, rw <- mul_add_distrib cminus aplus bplus at h, rw <- mul_add_distrib cplus aminus bminus at h, have tmp : (myadd cplus aminus) = (myadd cminus aplus), exact mul_weird_sub (myadd cplus aminus) (myadd cminus aplus) bplus bminus b_not_zero h, symmetry, rw add_comm aminus cplus, rw add_comm aplus cminus, exact tmp, end lemma mul_cancel_int_alt (a b c : myint) : (¬ iszero a) -> (int_equal (int_mul a b) (int_mul a c)) -> (int_equal b c) := begin rw mul_comm_int_rw a b, rw mul_comm_int_rw a c, exact mul_cancel_int b a c, end lemma mul_nonzero_int (a b : myint) : (¬ iszero a) -> (¬ iszero b) -> (¬ iszero (int_mul a b)) := begin cases a with aplus aminus, cases b with bplus bminus, unfold int_mul, unfold iszero, intros h1 h2, by_contradiction h, rw add_comm (mymul aplus bminus) (mymul aminus bplus) at h, exact h1 ((mul_weird_sub aplus aminus bplus bminus h2) h), end lemma mul_sub_in_add_int (a b c d e : myint) : (int_equal a b) -> (int_equal (int_add (int_mul a c) d) e) -> (int_equal (int_add (int_mul b c) d) e) := begin intros h1 h2, have h3 : int_equal (int_mul a c) (int_mul b c), exact mul_eq_alt c a b h1, exact add_sub_int (int_mul a c) (int_mul b c) d e h3 h2, end lemma mul_two_int (a : myint) : int_mul (int zero.mysucc.mysucc zero) a = (int_add a a) := begin cases a with plus minus, unfold int_add, unfold int_mul, repeat {rw zero_mul}, repeat {rw add_zero}, repeat {rw mul_two}, end def negative : myint -> myint | (int pos neg) := (int neg pos) def negative_eq (a b : myint) : (int_equal a b) -> (int_equal (negative a) (negative b)) := begin cases a with aplus aminus, cases b with bplus bminus, unfold negative, unfold int_equal, intros, cc, end def add_to_negative (a : myint) : (int_equal int_zero (int_add a (negative a))) := begin cases a with plus minus, unfold negative, unfold int_add, unfold int_zero, unfold int_equal, rw add_comm minus plus, end def add_to_negative_alt (a : myint) : iszero (int_add a (negative a)) := begin cases a with aplus aminus, unfold negative, unfold int_add, unfold iszero, rw add_comm aplus aminus, end def mul_negative (a b : myint) : int_mul a (negative b) = negative (int_mul a b) := begin cases a with apos aneg, cases b with bpos bneg, unfold negative, unfold int_mul, unfold negative, end def mul_negative_alt (a b : myint) : int_mul a (negative b) = int_mul (negative a) b := begin cases a with apos aneg, cases b with bpos bneg, unfold negative, unfold int_mul, rw add_comm (mymul apos bneg) (mymul aneg bpos), rw add_comm (mymul aneg bneg) (mymul apos bpos), end /- A rational number is a pair representing a/b -/ inductive myrat : Type | rat : myint -> myint -> myrat open myrat /- Definition: A myrat where iszero (second component) is True is an undefined value All operations on myrat types should return an undefined value if given one. In this way, failure can propagate. Also, all predicates are True about undefined values, so that proofs do not have to start with "defined (a) ->".-/ def rat_zero : myrat := (rat int_zero int_one) def rat_one : myrat := (rat int_one int_one) def rat_two : myrat := (rat (int zero.mysucc.mysucc zero) int_one) def defined : myrat -> Prop | (rat _ den) := (¬(iszero den)) def iszero_rat : myrat -> Prop | (rat num den) := (iszero num) \/ (iszero den) lemma undef_iszero (a : myrat) : ¬(defined a) -> (iszero_rat a) := begin cases a with anum aden, unfold defined, unfold iszero_rat, intros, right, cc, end def rat_eq : myrat -> myrat -> Prop | (rat anum aden) (rat bnum bden) := int_equal (int_mul anum bden) (int_mul aden bnum) \/ (iszero aden) \/ (iszero bden) lemma undef_eq (a b : myrat) : ¬(defined a) -> (rat_eq a b) := begin cases a with anum aden, cases b with bnum bden, unfold defined, unfold rat_eq, intros, right, left, by_cases (¬(iszero aden)), cc, cc, end lemma rat_eq_refl (a : myrat) : (rat_eq a a) := begin cases a with anum aden, unfold rat_eq, left, exact mul_comm_int anum aden, end lemma rat_eq_comm (a b : myrat) : (rat_eq a b) -> (rat_eq b a) := begin cases a with anum aden, cases b with bnum bden, unfold rat_eq, intros h, cases h, left, have tmp1 : int_equal (int_mul bden anum) (int_mul anum bden), exact mul_comm_int bden anum, have tmp2 : int_equal (int_mul bden anum) (int_mul aden bnum), exact int_eq_trans (int_mul bden anum) (int_mul anum bden) (int_mul aden bnum) tmp1 h, have tmp3 : int_equal (int_mul aden bnum) (int_mul bnum aden), exact mul_comm_int aden bnum, exact int_eq_comm (int_mul bden anum) (int_mul bnum aden) (int_eq_trans (int_mul bden anum) (int_mul aden bnum) (int_mul bnum aden) tmp2 tmp3), cases h, right, right, exact h, right, left, exact h, end lemma eq_undef (a b : myrat) : ¬(defined b) -> (rat_eq a b) := begin intros h, exact rat_eq_comm b a (undef_eq b a h), end lemma zeroes_only_eq_rat (a b : myrat) : (defined a) -> (rat_eq a b) -> (iszero_rat a) -> (iszero_rat b) := begin cases a with anum aden, cases b with bnum bden, unfold rat_eq, unfold iszero_rat, unfold defined, intros h2 h3 h1, cases h1, cases h3, have tmp : iszero (int_mul aden bnum), exact int_zeros_only_eq (int_mul anum bden) (int_mul aden bnum) (mul_zero_int anum bden h1) h3, left, by_cases (iszero bnum), cc, exfalso, exact mul_nonzero_int aden bnum h2 h tmp, cases h3, cc, cc, cc, end lemma rat_eq_trans (a b c : myrat) : (defined b) -> (rat_eq a b) -> (rat_eq b c) -> (rat_eq a c) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, unfold rat_eq, intros h1, intros h2, intros h3, by_cases h4 : (iszero aden), right, left, exact h4, by_cases h5 : (iszero cden), right, right, exact h5, unfold defined at h1, cases h2, cases h3, by_cases (iszero bnum), have h6 : iszero (int_mul aden bnum), rw mul_comm_int_rw aden bnum, exact mul_zero_int bnum aden h, have h7 : iszero (int_mul bnum cden), exact mul_zero_int bnum cden h, have h8 : iszero anum, exact mul_result_zero_int_rev anum bden h1 (int_zeros_only_eq_rev (int_mul aden bnum) (int_mul anum bden) h6 h2), have h9 : iszero cnum, exact mul_result_zero_int bden cnum h1 (int_zeros_only_eq (int_mul bnum cden) (int_mul bden cnum) h7 h3), left, exact int_zeros_eq (int_mul anum cden) (int_mul aden cnum) (mul_zero_int anum cden h8) (mul_zero_int_alt cnum aden h9), have tmp : int_equal (int_mul (int_mul anum bden) (int_mul bnum cden)) (int_mul (int_mul aden bnum) (int_mul bden cnum)), exact multiply_two_equalities_int (int_mul anum bden) (int_mul aden bnum) (int_mul bnum cden) (int_mul bden cnum) h2 h3, rw mul_assoc_int_rw aden bnum (int_mul bden cnum) at tmp, rw <- mul_assoc_int_rw bnum bden cnum at tmp, rw mul_comm_int_rw (int_mul bnum bden) cnum at tmp, rw mul_assoc_int_rw anum bden (int_mul bnum cden) at tmp, rw <- mul_assoc_int_rw bden bnum cden at tmp, rw mul_comm_int_rw (int_mul bden bnum) cden at tmp, rw mul_comm_int_rw bden bnum at tmp, repeat {rw <- mul_assoc_int_rw at tmp}, rw mul_assoc_int_rw (int_mul aden cnum) bnum bden at tmp, rw mul_assoc_int_rw (int_mul anum cden) bnum bden at tmp, have tmp2 : ¬ iszero (int_mul bnum bden), exact mul_nonzero_int bnum bden h h1, left, exact mul_cancel_int (int_mul anum cden) (int_mul bnum bden) (int_mul aden cnum) tmp2 tmp, cases h3, cc, cc, cases h2, cc, cc, end def rat_mul : myrat -> myrat -> myrat | (rat anum aden) (rat bnum bden) := (rat (int_mul anum bnum) (int_mul aden bden)) lemma rat_mul_comm (a b : myrat) : (rat_mul a b) = (rat_mul b a) := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, rw mul_comm_int_rw anum bnum, rw mul_comm_int_rw aden bden, end lemma rat_mul_def (a b : myrat) : (defined a) -> (defined b) -> (defined (rat_mul a b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, unfold defined, intros h1 h2, exact mul_nonzero_int aden bden h1 h2, end lemma rat_mul_undef (a b : myrat) : ¬(defined a) -> ¬(defined (rat_mul a b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, unfold defined, intros, by_cases (iszero aden), have tmp : iszero (int_mul aden bden), exact mul_zero_int aden bden h, cc, cc, end lemma rat_undef_mul (a b : myrat) : ¬(defined b) -> ¬(defined (rat_mul a b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, unfold defined, intros, by_cases (iszero bden), have tmp : iszero (int_mul aden bden), exact mul_zero_int_alt bden aden h, cc, cc, end lemma rat_mul_zero (a b : myrat) : (iszero_rat a) -> (iszero_rat (rat_mul a b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, unfold iszero_rat, intros h, cases h, left, exact mul_zero_int anum bnum h, right, exact mul_zero_int aden bden h, end lemma rat_mul_assoc (a b c : myrat) : (rat_mul (rat_mul a b) c) = (rat_mul a (rat_mul b c)) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, unfold rat_mul, repeat {rw mul_assoc_int_rw}, end lemma rat_mul_eq (a b c : myrat) : (rat_eq b c) -> (rat_eq (rat_mul a b) (rat_mul a c)) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, unfold rat_mul, unfold rat_eq, intros h, cases h with h1 h1, left, have h2 : int_equal (int_mul (int_mul bnum cden) (int_mul anum aden)) (int_mul (int_mul bden cnum) (int_mul anum aden)), exact mul_eq_alt (int_mul anum aden) (int_mul bnum cden) (int_mul bden cnum) h1, rw mul_comm_int_rw anum bnum, rw mul_comm_int_rw aden cden, rw mul_tree_swap_int_rw bnum anum cden aden, rw mul_comm_int_rw aden bden, rw mul_comm_int_rw anum cnum, rw mul_tree_swap_int_rw bden aden cnum anum, rw mul_comm_int_rw aden anum, exact h2, right, cases h1, left, exact mul_zero_int_alt bden aden h1, right, exact mul_zero_int_alt cden aden h1, end lemma mul_sub_rat (a b c d : myrat) : (defined a) -> (rat_eq a b) -> (rat_eq (rat_mul a c) d) -> (rat_eq (rat_mul b c) d) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, cases d with dnum dden, unfold rat_mul, unfold rat_eq, intros h1 h2 h3, by_cases h6 : (iszero bden), right, left, exact mul_zero_int bden cden h6, by_cases h8 : (iszero cden), right, left, exact mul_zero_int_alt cden bden h8, cases h2 with h2, cases h3 with h3, unfold defined at h1, have h4 : int_equal (int_mul (int_mul bnum bden) (int_mul (int_mul anum cnum) dden)) (int_mul (int_mul bnum bden) (int_mul (int_mul aden cden) dnum)), exact mul_eq (int_mul bnum bden) (int_mul (int_mul anum cnum) dden) (int_mul (int_mul aden cden) dnum) h3, repeat {rw <- mul_assoc_int_rw at h4}, rw mul_assoc_int_rw bnum bden anum at h4, rw mul_comm_int_rw bden anum at h4, rw mul_comm_int_rw bnum bden at h4, rw mul_assoc_int_rw bden bnum aden at h4, rw mul_comm_int_rw bnum aden at h4, by_cases h5 : (iszero bnum), have h7 : iszero anum, exact mul_result_zero_int_rev anum bden h6 (int_zeros_only_eq_rev (int_mul aden bnum) (int_mul anum bden) (mul_zero_int_alt bnum aden h5) h2), have h9 : iszero dnum, exact mul_result_zero_int (int_mul aden cden) dnum (mul_nonzero_int aden cden h1 h8) (int_zeros_only_eq (int_mul (int_mul anum cnum) dden) (int_mul (int_mul aden cden) dnum) (mul_zero_int (int_mul anum cnum) dden (mul_zero_int anum cnum h7)) h3), left, exact int_zeros_eq (int_mul (int_mul bnum cnum) dden) (int_mul (int_mul bden cden) dnum) (mul_zero_int (int_mul bnum cnum) dden (mul_zero_int bnum cnum h5)) (mul_zero_int_alt dnum (int_mul bden cden) h9), rw mul_comm_int_rw bden (int_mul aden bnum) at h4, rw mul_comm_int_rw bnum (int_mul anum bden) at h4, repeat {rw mul_assoc_int_rw at h4}, rw <- mul_assoc_int_rw at h4, rw <- mul_assoc_int_rw aden bnum (int_mul bden (int_mul cden dnum)) at h4, have h7 : int_equal (int_mul (int_mul aden bnum) (int_mul bnum (int_mul cnum dden))) (int_mul (int_mul aden bnum) (int_mul bden (int_mul cden dnum))), exact mul_sub_int (int_mul anum bden) (int_mul aden bnum) (int_mul bnum (int_mul cnum dden)) (int_mul (int_mul aden bnum) (int_mul bden (int_mul cden dnum))) h2 h4, left, repeat {rw mul_assoc_int_rw}, exact mul_cancel_int_alt (int_mul aden bnum) (int_mul bnum (int_mul cnum dden)) (int_mul bden (int_mul cden dnum)) (mul_nonzero_int aden bnum h1 h5) h7, cases h3 with h3, unfold defined at h1, right, left, have h7 : iszero cden, exact mul_result_zero_int aden cden h1 h3, contradiction, right, right, exact h3, cases h2, unfold defined at h1, contradiction, right, left, exact mul_zero_int bden cden h2, end lemma mul_eq_rat (a b c : myrat) : (rat_eq a b) -> (rat_eq (rat_mul a c) (rat_mul b c)) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, unfold rat_mul, unfold rat_eq, intros h1, cases h1 with h1, left, rw mul_tree_swap_int_rw anum cnum bden cden, have h2 : int_equal (int_mul aden bnum) (int_mul anum bden), exact int_eq_comm (int_mul anum bden) (int_mul aden bnum) h1, apply mul_sub_int (int_mul aden bnum) (int_mul anum bden) (int_mul cnum cden) (int_mul (int_mul aden cden) (int_mul bnum cnum)) h2, rw mul_comm_int_rw cnum cden, rw mul_tree_swap_int_rw aden bnum cden cnum, exact int_equal_refl (int_mul (int_mul aden cden) (int_mul bnum cnum)), cases h1 with h1, right, left, exact mul_zero_int aden cden h1, right, right, exact mul_zero_int bden cden h1, end lemma mul_eq_rat_alt (a b c : myrat) : (rat_eq a b) -> (rat_eq (rat_mul c a) (rat_mul c b)) := begin rw rat_mul_comm c a, rw rat_mul_comm c b, exact mul_eq_rat a b c, end def rat_add : myrat -> myrat -> myrat | (rat anum aden) (rat bnum bden) := (rat (int_add (int_mul anum bden) (int_mul bnum aden)) (int_mul aden bden)) lemma rat_add_undef (a b : myrat) : ¬(defined a) -> ¬(defined (rat_add a b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_add, unfold defined, intros h1, by_cases h2 : (iszero aden), have h3 : iszero (int_mul aden bden), exact mul_zero_int aden bden h2, cc, cc, end lemma rat_add_def (a b : myrat) : (defined a) -> (defined b) -> (defined (rat_add a b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_add, unfold defined, intros h1 h2, exact mul_nonzero_int aden bden h1 h2, end lemma rat_add_zero (a b : myrat) : (iszero_rat a) -> (rat_eq (rat_add a b) b) := begin cases a with anum aden, cases b with bnum bden, unfold rat_add, unfold rat_eq, unfold iszero_rat, intros h, cases h, left, have h1 : (int_equal (int_mul bnum aden) (int_add (int_mul anum bden) (int_mul bnum aden))), exact add_zero_int (int_mul anum bden) (int_mul bnum aden) (mul_zero_int anum bden h), have h2 : int_equal (int_mul bden (int_mul bnum aden)) (int_mul bden (int_add (int_mul anum bden) (int_mul bnum aden))), exact mul_eq bden (int_mul bnum aden) (int_add (int_mul anum bden) (int_mul bnum aden)) h1, rw mul_comm_int_rw (int_add (int_mul anum bden) (int_mul bnum aden)) bden, rw mul_comm_int_rw aden bden, rw mul_assoc_int_rw bden aden bnum, rw mul_comm_int_rw aden bnum, exact int_eq_comm (int_mul bden (int_mul bnum aden)) (int_mul bden (int_add (int_mul anum bden) (int_mul bnum aden))) h2, right, left, exact mul_zero_int aden bden h, end lemma rat_add_zero_alt (a : myrat) : (rat_add a rat_zero) = a := begin unfold rat_zero, cases a with anum aden, unfold rat_add, repeat {rw mul_one_int_rw _}, cases aden with adenplus adenminus, unfold int_zero, unfold int_mul, rw mul_comm, rw mul_comm zero adenminus, repeat {rw mul_zero}, unfold myadd, cases anum with anumplus anumminus, unfold int_add, repeat {rw add_zero}, end lemma add_eq_rat (a b c : myrat) : (rat_eq a b) -> (rat_eq (rat_add a c) (rat_add b c)) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, unfold rat_add, unfold rat_eq, intros h, cases h, left, repeat {rw mul_distrib_int_rw}, repeat {rw mul_distrib_int_rw_alt}, rw mul_assoc_int_rw anum cden (int_mul bden cden), rw <- mul_assoc_int_rw cden bden cden, rw mul_comm_int_rw cden bden, rw mul_assoc_int_rw bden cden cden, rw <- mul_assoc_int_rw anum bden (int_mul cden cden), apply mul_sub_in_add_int (int_mul aden bnum) (int_mul anum bden) (int_mul cden cden) (int_mul (int_mul cnum aden) (int_mul bden cden)) (int_add (int_mul (int_mul aden cden) (int_mul bnum cden)) (int_mul (int_mul aden cden) (int_mul cnum bden))) (int_eq_comm _ _ h), rw mul_assoc_int_rw aden bnum (int_mul cden cden), rw <- mul_assoc_int_rw bnum cden cden, rw mul_comm_int_rw bnum cden, rw mul_assoc_int_rw cden bnum cden, rw <- mul_assoc_int_rw aden cden (int_mul bnum cden), rw mul_comm_int_rw bnum cden, rw mul_comm_int_rw cnum aden, rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw aden cnum (int_mul cden bden), rw <- mul_assoc_int_rw cnum cden bden, rw mul_comm_int_rw cnum cden, rw mul_assoc_int_rw cden cnum bden, rw <- mul_assoc_int_rw aden cden (int_mul cnum bden), exact int_equal_refl _, cases h, right, left, exact mul_zero_int aden cden h, right, right, exact mul_zero_int bden cden h, end lemma rat_add_comm (a b : myrat) : (rat_add a b) = (rat_add b a) := begin cases a with anum aden, cases b with bnum bden, unfold rat_add, rw add_comm_int_rw (int_mul anum bden) (int_mul bnum aden), rw mul_comm_int_rw bden aden, end lemma rat_add_assoc (a b c : myrat) : (rat_add a (rat_add b c)) = (rat_add (rat_add a b) c) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, unfold rat_add, repeat {rw mul_assoc_int_rw}, repeat {rw mul_distrib_int_rw}, repeat {rw mul_assoc_int_rw}, repeat {rw add_assoc_int_rw}, rw mul_comm_int_rw aden bden, rw mul_comm_int_rw aden cden, end lemma add_sub_rat (a b c d : myrat) : (defined a) -> (rat_eq a b) -> (rat_eq (rat_add a c) d) -> (rat_eq (rat_add b c) d) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, cases d with dnum dden, unfold rat_add, unfold rat_eq, intros h1 h2 h3, unfold defined at h1, by_cases h4 : iszero bden, right, left, exact mul_zero_int bden cden h4, by_cases h5 : iszero cden, right, left, exact mul_zero_int_alt cden bden h5, by_cases h6 : iszero dden, right, right, exact h6, left, cases h2 with h2, cases h3 with h3, by_cases h7 : (iszero anum), have h8 : (iszero bnum), exact mul_result_zero_int aden bnum h1 (int_zeros_only_eq (int_mul anum bden) (int_mul aden bnum) (mul_zero_int anum bden h7) h2), have h9 : int_equal (int_mul (int_mul cnum aden) dden) (int_mul (int_mul aden cden) dnum), exact mul_sub_int (int_add (int_mul anum cden) (int_mul cnum aden)) (int_mul cnum aden) dden (int_mul (int_mul aden cden) dnum) (add_zero_int_alt (int_mul anum cden) (int_mul cnum aden) (mul_zero_int anum cden h7)) h3, rw mul_comm_int_rw cnum aden at h9, repeat {rw mul_assoc_int_rw at h9}, have h10 : int_equal (int_mul cnum dden) (int_mul cden dnum), exact mul_cancel_int_alt aden (int_mul cnum dden) (int_mul cden dnum) h1 h9, rw mul_distrib_int_rw, have h11 : int_equal (int_mul (int_mul cnum bden) dden) (int_add (int_mul (int_mul bnum cden) dden) (int_mul (int_mul cnum bden) dden)), exact add_zero_int (int_mul (int_mul bnum cden) dden) (int_mul (int_mul cnum bden) dden) (mul_zero_int (int_mul bnum cden) dden (mul_zero_int bnum cden h8)), rw mul_comm_int_rw cnum bden at h11, rw mul_assoc_int_rw bden cnum dden at h11, rw mul_comm_int_rw bden (int_mul cnum dden) at h11, have h12 : int_equal (int_mul (int_mul cden dnum) bden) (int_add (int_mul (int_mul bnum cden) dden) (int_mul (int_mul cnum dden) bden)), exact mul_sub_int (int_mul cnum dden) (int_mul cden dnum) bden (int_add (int_mul (int_mul bnum cden) dden) (int_mul (int_mul cnum dden) bden)) h10 h11, rw mul_assoc_int_rw cnum bden dden, rw mul_comm_int_rw bden dden, rw <- mul_assoc_int_rw cnum dden bden, rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden dnum, rw mul_comm_int_rw bden dnum, rw <- mul_assoc_int_rw cden dnum bden, exact int_eq_comm (int_mul (int_mul cden dnum) bden) (int_add (int_mul (int_mul bnum cden) dden) (int_mul (int_mul cnum dden) bden)) h12, rw mul_distrib_int_rw at h3, rw mul_distrib_int_rw, have h8 : int_equal (int_mul bden (int_add (int_mul (int_mul anum cden) dden) (int_mul (int_mul cnum aden) dden))) (int_mul bden (int_mul (int_mul aden cden) dnum)), exact mul_eq bden (int_add (int_mul (int_mul anum cden) dden) (int_mul (int_mul cnum aden) dden)) (int_mul (int_mul aden cden) dnum) h3, rw mul_distrib_int_rw_alt at h8, rw mul_assoc_int_rw anum cden dden at h8, rw <- mul_assoc_int_rw bden anum (int_mul cden dden) at h8, rw mul_comm_int_rw bden anum at h8, have h9 : int_equal (int_add (int_mul (int_mul aden bnum) (int_mul cden dden)) (int_mul bden (int_mul (int_mul cnum aden) dden))) (int_mul bden (int_mul (int_mul aden cden) dnum)), exact mul_sub_in_add_int (int_mul anum bden) (int_mul aden bnum) (int_mul cden dden) (int_mul bden (int_mul (int_mul cnum aden) dden)) (int_mul bden (int_mul (int_mul aden cden) dnum)) h2 h8, rw mul_assoc_int_rw aden bnum (int_mul cden dden) at h9, rw mul_comm_int_rw cnum aden at h9, rw mul_assoc_int_rw aden cnum dden at h9, rw <- mul_assoc_int_rw bden aden (int_mul cnum dden) at h9, rw mul_comm_int_rw bden aden at h9, rw mul_assoc_int_rw aden bden (int_mul cnum dden) at h9, rw <- mul_distrib_int_rw_alt aden (int_mul bnum (int_mul cden dden)) (int_mul bden (int_mul cnum dden)) at h9, rw mul_assoc_int_rw aden cden dnum at h9, rw <- mul_assoc_int_rw bden aden (int_mul cden dnum) at h9, rw mul_comm_int_rw bden aden at h9, rw mul_assoc_int_rw aden bden (int_mul cden dnum) at h9, rw mul_assoc_int_rw bnum cden dden, rw mul_assoc_int_rw bden cden dnum, rw mul_comm_int_rw cnum bden, rw mul_assoc_int_rw bden cnum dden, exact mul_cancel_int_alt aden (int_add (int_mul bnum (int_mul cden dden)) (int_mul bden (int_mul cnum dden))) (int_mul bden (int_mul cden dnum)) h1 h9, cases h3 with h3, exfalso, exact h5 (mul_result_zero_int aden cden h1 h3), contradiction, exfalso, cases h2 with h2, exact h1 h2, exact h4 h2, end lemma add_sub_rat_alt (a b c d : myrat) : (defined a) -> (rat_eq a b) -> (rat_eq (rat_add c a) d) -> (rat_eq (rat_add c b) d) := begin intros h1 h2 h3, rw rat_add_comm, rw rat_add_comm at h3, exact add_sub_rat a b c d h1 h2 h3, end lemma add_self_nonzero_rat (a : myrat) : ¬(iszero_rat a) -> ¬(iszero_rat (rat_add a a)) := begin cases a with anum aden, unfold rat_add, unfold iszero_rat, intros, by_cases (iszero (int_add (int_mul anum aden) (int_mul anum aden)) ∨ iszero (int_mul aden aden)), exfalso, cases h, by_cases h_anum : (iszero anum), cc, by_cases h_aden : (iszero aden), cc, exact int_add_self_nonzero (int_mul anum aden) (mul_nonzero_int anum aden h_anum h_aden) h, by_cases h_aden : (iszero aden), cc, exact (mul_nonzero_int aden aden h_aden h_aden) h, cc, end lemma rat_mul_zero_alt (a : myrat) : rat_eq rat_zero (rat_mul a rat_zero) := begin cases a with anum aden, unfold rat_zero, unfold int_zero, unfold int_one, unfold rat_mul, unfold rat_eq, by_cases (iszero aden), right, right, exact mul_zero_int aden (int nat_one zero) h, left, have int : iszero (int zero zero), unfold iszero, exact int_zeros_eq (int_mul (myint.int zero zero) (int_mul aden (myint.int nat_one zero))) ((int_mul (myint.int nat_one zero) (int_mul anum (myint.int zero zero)))) (mul_zero_int (myint.int zero zero) (int_mul aden (myint.int nat_one zero)) int) (mul_zero_int_alt (int_mul anum (myint.int zero zero)) ((myint.int nat_one zero)) (mul_zero_int_alt (myint.int zero zero) anum int)), end lemma rat_mul_one (a b : myrat) : rat_eq a rat_one -> rat_eq b (rat_mul a b) := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, unfold rat_one, unfold rat_eq, intros h, cases h, left, repeat {rw mul_one_int_rw at h}, have h1 : int_equal aden anum, exact int_eq_comm anum aden h, rw <- mul_assoc_int_rw bnum aden bden, rw mul_comm_int_rw bnum aden, rw mul_assoc_int_rw aden bnum bden, rw <- mul_assoc_int_rw bden anum bnum, rw mul_comm_int_rw bden anum, rw mul_assoc_int_rw anum bden bnum, rw mul_comm_int_rw bnum bden, exact mul_eq_alt (int_mul bden bnum) aden anum h1, cases h, right, right, exact mul_zero_int aden bden h, unfold int_one at h, unfold iszero at h, unfold nat_one at h, cc, end lemma rat_mul_one_rw (a : myrat) : rat_mul a rat_one = a := begin cases a with num den, unfold rat_one, unfold rat_mul, cases num with numplus numminus, cases den with denplus denminus, unfold int_one, unfold int_mul, rw mul_zero, rw add_zero, unfold nat_one, rw mul_one_eq, rw mul_zero, rw zero_add, rw mul_one_eq, rw mul_one_eq, rw mul_zero, rw add_zero, rw mul_zero, rw zero_add, rw mul_one_eq, end lemma rat_mul_distrib (a b c : myrat) : rat_eq (rat_mul (rat_add a b) c) (rat_add (rat_mul a c) (rat_mul b c)) := begin cases a with anum aden, cases b with bnum bden, cases c with cnum cden, unfold rat_mul, unfold rat_add, unfold rat_mul, rw mul_distrib_int_rw, repeat {rw mul_assoc_int_rw}, unfold rat_eq, left, rw mul_distrib_int_rw_alt, rw mul_distrib_int_rw, repeat {rw mul_assoc_int_rw}, /- Don't lie and try to tell me you haven't missed the inefficient Python-generated code.-/ rw <- mul_assoc_int_rw anum bden (int_mul cnum (int_mul aden (int_mul cden (int_mul bden cden)))), rw mul_comm_int_rw anum bden, rw mul_assoc_int_rw bden anum (int_mul cnum (int_mul aden (int_mul cden (int_mul bden cden)))), rw <- mul_assoc_int_rw anum cnum (int_mul aden (int_mul cden (int_mul bden cden))), rw mul_comm_int_rw anum cnum, rw mul_assoc_int_rw cnum anum (int_mul aden (int_mul cden (int_mul bden cden))), rw <- mul_assoc_int_rw aden cden (int_mul bden cden), rw mul_comm_int_rw aden cden, rw mul_assoc_int_rw cden aden (int_mul bden cden), rw <- mul_assoc_int_rw aden bden cden, rw mul_comm_int_rw aden bden, rw mul_assoc_int_rw bden aden cden, rw mul_comm_int_rw aden cden, rw <- mul_assoc_int_rw bden cnum (int_mul anum (int_mul cden (int_mul bden (int_mul cden aden)))), rw mul_comm_int_rw bden cnum, rw mul_assoc_int_rw cnum bden (int_mul anum (int_mul cden (int_mul bden (int_mul cden aden)))), rw <- mul_assoc_int_rw anum cden (int_mul bden (int_mul cden aden)), rw mul_comm_int_rw anum cden, rw mul_assoc_int_rw cden anum (int_mul bden (int_mul cden aden)), rw <- mul_assoc_int_rw anum bden (int_mul cden aden), rw mul_comm_int_rw anum bden, rw mul_assoc_int_rw bden anum (int_mul cden aden), rw <- mul_assoc_int_rw anum cden aden, rw mul_comm_int_rw anum cden, rw mul_assoc_int_rw cden anum aden, rw <- mul_assoc_int_rw bden cden (int_mul bden (int_mul cden (int_mul anum aden))), rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden (int_mul bden (int_mul cden (int_mul anum aden))), rw <- mul_assoc_int_rw bden cden (int_mul anum aden), rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden (int_mul anum aden), rw <- mul_assoc_int_rw bden cden (int_mul bden (int_mul anum aden)), rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden (int_mul bden (int_mul anum aden)), rw <- mul_assoc_int_rw aden cnum (int_mul cden (int_mul bden (int_mul cden aden))), rw mul_comm_int_rw aden cnum, rw mul_assoc_int_rw cnum aden (int_mul cden (int_mul bden (int_mul cden aden))), rw <- mul_assoc_int_rw aden cden (int_mul bden (int_mul cden aden)), rw mul_comm_int_rw aden cden, rw mul_assoc_int_rw cden aden (int_mul bden (int_mul cden aden)), rw <- mul_assoc_int_rw aden bden (int_mul cden aden), rw mul_comm_int_rw aden bden, rw mul_assoc_int_rw bden aden (int_mul cden aden), rw <- mul_assoc_int_rw aden cden aden, rw mul_comm_int_rw aden cden, rw mul_assoc_int_rw cden aden aden, rw <- mul_assoc_int_rw bnum cnum (int_mul cden (int_mul bden (int_mul cden (int_mul aden aden)))), rw mul_comm_int_rw bnum cnum, rw mul_assoc_int_rw cnum bnum (int_mul cden (int_mul bden (int_mul cden (int_mul aden aden)))), rw <- mul_assoc_int_rw bnum cden (int_mul bden (int_mul cden (int_mul aden aden))), rw mul_comm_int_rw bnum cden, rw mul_assoc_int_rw cden bnum (int_mul bden (int_mul cden (int_mul aden aden))), rw <- mul_assoc_int_rw bden cden (int_mul aden aden), rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden (int_mul aden aden), rw <- mul_assoc_int_rw bnum cden (int_mul bden (int_mul aden aden)), rw mul_comm_int_rw bnum cden, rw mul_assoc_int_rw cden bnum (int_mul bden (int_mul aden aden)), apply int_eq_comm, rw <- mul_assoc_int_rw aden bden (int_mul cden (int_mul anum (int_mul cnum (int_mul cden bden)))), rw mul_comm_int_rw aden bden, rw mul_assoc_int_rw bden aden (int_mul cden (int_mul anum (int_mul cnum (int_mul cden bden)))), rw <- mul_assoc_int_rw aden cden (int_mul anum (int_mul cnum (int_mul cden bden))), rw mul_comm_int_rw aden cden, rw mul_assoc_int_rw cden aden (int_mul anum (int_mul cnum (int_mul cden bden))), rw <- mul_assoc_int_rw aden anum (int_mul cnum (int_mul cden bden)), rw mul_comm_int_rw aden anum, rw mul_assoc_int_rw anum aden (int_mul cnum (int_mul cden bden)), rw <- mul_assoc_int_rw aden cnum (int_mul cden bden), rw mul_comm_int_rw aden cnum, rw mul_assoc_int_rw cnum aden (int_mul cden bden), rw <- mul_assoc_int_rw aden cden bden, rw mul_comm_int_rw aden cden, rw mul_assoc_int_rw cden aden bden, rw mul_comm_int_rw aden bden, rw <- mul_assoc_int_rw bden cden (int_mul anum (int_mul cnum (int_mul cden (int_mul bden aden)))), rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden (int_mul anum (int_mul cnum (int_mul cden (int_mul bden aden)))), rw <- mul_assoc_int_rw anum cnum (int_mul cden (int_mul bden aden)), rw mul_comm_int_rw anum cnum, rw mul_assoc_int_rw cnum anum (int_mul cden (int_mul bden aden)), rw <- mul_assoc_int_rw anum cden (int_mul bden aden), rw mul_comm_int_rw anum cden, rw mul_assoc_int_rw cden anum (int_mul bden aden), rw <- mul_assoc_int_rw anum bden aden, rw mul_comm_int_rw anum bden, rw mul_assoc_int_rw bden anum aden, rw <- mul_assoc_int_rw bden cnum (int_mul cden (int_mul bden (int_mul anum aden))), rw mul_comm_int_rw bden cnum, rw mul_assoc_int_rw cnum bden (int_mul cden (int_mul bden (int_mul anum aden))), rw <- mul_assoc_int_rw bden cden (int_mul bden (int_mul anum aden)), rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden (int_mul bden (int_mul anum aden)), rw <- mul_assoc_int_rw cden cnum (int_mul cden (int_mul bden (int_mul bden (int_mul anum aden)))), rw mul_comm_int_rw cden cnum, rw mul_assoc_int_rw cnum cden (int_mul cden (int_mul bden (int_mul bden (int_mul anum aden)))), rw <- mul_assoc_int_rw aden bden (int_mul cden (int_mul bnum (int_mul cnum (int_mul cden aden)))), rw mul_comm_int_rw aden bden, rw mul_assoc_int_rw bden aden (int_mul cden (int_mul bnum (int_mul cnum (int_mul cden aden)))), rw <- mul_assoc_int_rw aden cden (int_mul bnum (int_mul cnum (int_mul cden aden))), rw mul_comm_int_rw aden cden, rw mul_assoc_int_rw cden aden (int_mul bnum (int_mul cnum (int_mul cden aden))), rw <- mul_assoc_int_rw aden bnum (int_mul cnum (int_mul cden aden)), rw mul_comm_int_rw aden bnum, rw mul_assoc_int_rw bnum aden (int_mul cnum (int_mul cden aden)), rw <- mul_assoc_int_rw aden cnum (int_mul cden aden), rw mul_comm_int_rw aden cnum, rw mul_assoc_int_rw cnum aden (int_mul cden aden), rw <- mul_assoc_int_rw aden cden aden, rw mul_comm_int_rw aden cden, rw mul_assoc_int_rw cden aden aden, rw <- mul_assoc_int_rw bden cden (int_mul bnum (int_mul cnum (int_mul cden (int_mul aden aden)))), rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden (int_mul bnum (int_mul cnum (int_mul cden (int_mul aden aden)))), rw <- mul_assoc_int_rw bden bnum (int_mul cnum (int_mul cden (int_mul aden aden))), rw mul_comm_int_rw bden bnum, rw mul_assoc_int_rw bnum bden (int_mul cnum (int_mul cden (int_mul aden aden))), rw <- mul_assoc_int_rw bden cnum (int_mul cden (int_mul aden aden)), rw mul_comm_int_rw bden cnum, rw mul_assoc_int_rw cnum bden (int_mul cden (int_mul aden aden)), rw <- mul_assoc_int_rw bden cden (int_mul aden aden), rw mul_comm_int_rw bden cden, rw mul_assoc_int_rw cden bden (int_mul aden aden), rw <- mul_assoc_int_rw bnum cnum (int_mul cden (int_mul bden (int_mul aden aden))), rw mul_comm_int_rw bnum cnum, rw mul_assoc_int_rw cnum bnum (int_mul cden (int_mul bden (int_mul aden aden))), rw <- mul_assoc_int_rw bnum cden (int_mul bden (int_mul aden aden)), rw mul_comm_int_rw bnum cden, rw mul_assoc_int_rw cden bnum (int_mul bden (int_mul aden aden)), rw <- mul_assoc_int_rw cden cnum (int_mul cden (int_mul bnum (int_mul bden (int_mul aden aden)))), rw mul_comm_int_rw cden cnum, rw mul_assoc_int_rw cnum cden (int_mul cden (int_mul bnum (int_mul bden (int_mul aden aden)))), exact int_equal_refl (int_add (int_mul cnum (int_mul cden (int_mul cden (int_mul bden (int_mul bden (int_mul anum aden)))))) (int_mul cnum (int_mul cden (int_mul cden (int_mul bnum (int_mul bden (int_mul aden aden))))))), end lemma rat_mul_distrib_inv (a b c : myrat) : rat_eq (rat_add (rat_mul a c) (rat_mul b c)) (rat_mul (rat_add a b) c) := begin apply rat_eq_comm _ _, exact rat_mul_distrib a b c, end lemma rat_mul_distrib_alt (a b c : myrat) : rat_eq (rat_mul a (rat_add b c)) (rat_add (rat_mul a b) (rat_mul a c)) := begin rw rat_mul_comm, rw rat_mul_comm a b, rw rat_mul_comm a c, exact rat_mul_distrib b c a, end lemma rat_one_mul (a b : myrat) : rat_eq a rat_one -> rat_eq (rat_mul a b) b := begin intros h, exact rat_eq_comm b (rat_mul a b) (rat_mul_one a b h), end def rat_reciprocal : myrat -> myrat | (rat num den) := (rat (int_mul den den) (int_mul num den)) /- We can't just do (rat den num) because then reciprocal (1/0) is 0/1, so we get a defined value from an undefined value.-/ lemma reciprocal_mul_inv (a : myrat) : rat_eq rat_one (rat_mul a (rat_reciprocal a)) := begin cases a with anum aden, unfold rat_reciprocal, unfold rat_mul, unfold rat_one, unfold rat_eq, by_cases (iszero aden), right, right, exact mul_zero_int aden (int_mul anum aden) h, left, rw mul_comm_int_rw int_one (int_mul aden (int_mul anum aden)), rw mul_comm_int_rw int_one (int_mul anum (int_mul aden aden)), apply (mul_eq_alt int_one (int_mul aden (int_mul anum aden)) (int_mul anum (int_mul aden aden))), rw <- mul_assoc_int_rw anum aden aden, rw mul_comm_int_rw aden (int_mul anum aden), exact int_equal_refl (int_mul (int_mul anum aden) aden), end lemma reciprocal_inv_mul (a : myrat) : rat_eq (rat_mul a (rat_reciprocal a)) rat_one := begin exact rat_eq_comm rat_one (rat_mul a (rat_reciprocal a)) (reciprocal_mul_inv a), end lemma reciprocal_mul_cancel (a b : myrat) : rat_eq a (rat_mul a (rat_mul b (rat_reciprocal b))) := begin cases a with anum aden, cases b with bnum bden, unfold rat_reciprocal, unfold rat_mul, unfold rat_eq, left, rw <- mul_assoc_int_rw bden bnum bden, rw mul_comm_int_rw bden bnum, rw mul_assoc_int_rw bnum bden bden, rw <- mul_assoc_int_rw anum aden (int_mul bnum (int_mul bden bden)), rw mul_comm_int_rw anum aden, repeat {rw mul_assoc_int_rw}, exact int_equal_refl _, end lemma reciprocal_def (a : myrat) : (defined a) -> ¬(iszero_rat a) -> (defined (rat_reciprocal a)) := begin cases a with anum aden, unfold rat_reciprocal, unfold defined, unfold iszero_rat, intros, by_cases h1 : iszero anum, have tmp : iszero anum \/ iszero aden, left, exact h1, cc, by_cases h2 : iszero aden, have tmp : iszero anum \/ iszero aden, right, exact h2, cc, exact mul_nonzero_int anum aden h1 h2, end lemma reciprocal_undef_undef (a : myrat) : ¬(defined a) -> ¬(defined (rat_reciprocal a)) := begin cases a with anum aden, unfold rat_reciprocal, unfold defined, intros, by_cases h1 : (iszero aden), by_cases h2 : ¬(iszero (int_mul anum aden)), have h3 : iszero (int_mul anum aden), exact mul_zero_int_alt aden anum h1, cc, exact h2, cc, end lemma reciprocal_zero_undef (a : myrat) : (iszero_rat a) -> ¬(defined (rat_reciprocal a)) := begin cases a with anum aden, unfold rat_reciprocal, unfold defined, unfold iszero_rat, intros h, cases h with h, by_cases h1 : ¬(iszero (int_mul anum aden)), have h2 : iszero (int_mul anum aden), exact mul_zero_int anum aden h, cc, cc, have h1 : iszero (int_mul anum aden), exact mul_zero_int_alt aden anum h, cc, end lemma reciprocal_mul_inv_alt (a b : myrat) : rat_eq a (rat_mul (rat_mul a b) (rat_reciprocal b)) := begin rw rat_mul_assoc, apply rat_eq_comm _ _, rw rat_mul_comm _ _, by_cases h1 : (defined b), by_cases h2 : (iszero_rat b), exact undef_eq (rat_mul (rat_mul b (rat_reciprocal b)) a) a (rat_mul_undef (rat_mul b (rat_reciprocal b)) a (rat_undef_mul b (rat_reciprocal b) (reciprocal_zero_undef b h2))), have tmp : defined (rat_mul b (rat_reciprocal b)), exact rat_mul_def b (rat_reciprocal b) h1 (reciprocal_def b h1 h2), have tmp2 : defined rat_one, unfold rat_one, unfold defined, unfold int_one, unfold iszero, unfold nat_one, cc, apply mul_sub_rat rat_one (rat_mul b (rat_reciprocal b)) a a tmp2 (reciprocal_mul_inv b), rw rat_mul_comm, rw rat_mul_one_rw, exact rat_eq_refl a, exact undef_eq (rat_mul (rat_mul b (rat_reciprocal b)) a) a (rat_mul_undef (rat_mul b (rat_reciprocal b)) a (rat_mul_undef b (rat_reciprocal b) h1)), end lemma reciprocal_eq (a b : myrat) : (rat_eq a b) -> (rat_eq (rat_reciprocal a) (rat_reciprocal b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_reciprocal, unfold rat_eq, intros h, cases h, left, rw mul_comm_int_rw anum aden, repeat {rw mul_assoc_int_rw}, apply mul_eq aden (int_mul aden (int_mul bnum bden)) (int_mul anum (int_mul bden bden)), repeat {rw <- mul_assoc_int_rw}, apply mul_eq_alt bden (int_mul aden bnum) (int_mul anum bden), exact int_eq_comm _ _ h, cases h, right, left, exact mul_zero_int_alt aden anum h, right, right, exact mul_zero_int_alt bden bnum h, end /- We'll need this to cancel the /2a in the quadratic formula.-/ lemma half_reciprocal_mul (a : myrat) : rat_eq (rat_reciprocal rat_two) (rat_mul a (rat_reciprocal (rat_add a a))) := begin unfold rat_two, cases a with num den, unfold rat_add, unfold rat_reciprocal, unfold rat_mul, unfold rat_eq, left, unfold int_one, unfold int_mul, unfold mymul, unfold myadd, unfold nat_one, unfold mymul, unfold myadd, rw <- mul_two_int, rw mul_one_int_rw_alt, repeat {rw mul_assoc_int_rw}, rw <- mul_assoc_int_rw den (int zero.mysucc.mysucc zero) (int_mul num (int_mul den (int_mul den den))), rw mul_comm_int_rw den (int zero.mysucc.mysucc zero), repeat {rw mul_assoc_int_rw}, rw <- mul_assoc_int_rw den num (int_mul den (int_mul den den)), rw mul_comm_int_rw den num, repeat {rw mul_assoc_int_rw}, exact int_equal_refl _, end def negate_rat : myrat -> myrat | (rat num den) := (rat (negative num) den) lemma negate_def (a : myrat) : (defined a) -> (defined (negate_rat a)) := begin cases a with anum aden, unfold negate_rat, unfold defined, cc, end lemma negate_undef (a : myrat) : ¬(defined a) -> ¬(defined (negate_rat a)) := begin cases a with anum aden, unfold negate_rat, unfold defined, cc, end lemma negate_eq_rat (a b : myrat) : (rat_eq a b) -> (rat_eq (negate_rat a) (negate_rat b)) := begin cases a with anum aden, cases b with bnum bden, unfold negate_rat, unfold rat_eq, intros h, cases h, left, rw mul_comm_int_rw (negative anum) bden, repeat {rw mul_negative}, rw mul_comm_int_rw bden anum, exact negative_eq (int_mul anum bden) (int_mul aden bnum) h, cc, end lemma double_negate (a : myrat) : (negate_rat (negate_rat a)) = a := begin cases a with anum aden, unfold negate_rat, cases anum with aplus aminus, unfold negative, end lemma mul_negate (a b : myrat) : rat_eq (rat_mul a (negate_rat b)) (negate_rat (rat_mul a b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, unfold negate_rat, rw <- mul_negative anum bnum, unfold rat_mul, exact rat_eq_refl (rat (int_mul anum (negative bnum)) (int_mul aden bden)), end lemma mul_negate_rw (a b : myrat) : (rat_mul a (negate_rat b)) = (negate_rat (rat_mul a b)) := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, unfold negate_rat, rw <- mul_negative anum bnum, unfold rat_mul, end lemma mul_negate_alt_rw (a b : myrat) : (rat_mul (negate_rat a) b) = (rat_mul a (negate_rat b)) := begin cases a with anum aden, cases b with bnum bden, unfold negate_rat, unfold rat_mul, rw mul_negative_alt anum bnum, end lemma add_negate_rat (a : myrat) : rat_eq (rat_add a (negate_rat a)) rat_zero := begin cases a with anum aden, unfold negate_rat, unfold rat_zero, unfold rat_add, unfold rat_eq, left, rw mul_one_int_rw, rw mul_comm_int_rw (negative anum) aden, rw mul_negative aden anum, have h1 : int_equal int_zero (int_add (int_mul anum aden) (negative (int_mul anum aden))), exact add_to_negative (int_mul anum aden), have h2 : int_equal (int_mul (int_mul aden aden) int_zero) int_zero, exact mul_zero_int_zero (int_mul aden aden), have h3 : int_equal (int_mul (int_mul aden aden) int_zero) (int_add (int_mul anum aden) (negative (int_mul anum aden))), exact int_eq_trans (int_mul (int_mul aden aden) int_zero) int_zero (int_add (int_mul anum aden) (negative (int_mul anum aden))) h2 h1, rw mul_comm_int_rw aden anum, exact int_eq_comm (int_mul (int_mul aden aden) int_zero) (int_add (int_mul anum aden) (negative (int_mul anum aden))) h3, end lemma add_negate_rat_alt (a : myrat) : iszero_rat (rat_add a (negate_rat a)) := begin cases a with anum aden, unfold negate_rat, unfold rat_add, unfold iszero_rat, rw mul_comm_int_rw (negative anum) aden, rw mul_negative aden anum, rw mul_comm_int_rw anum aden, left, exact add_to_negative_alt (int_mul aden anum), end lemma iszero_mul_rat (a b : myrat) : ¬(iszero_rat b) -> (defined b) -> (iszero_rat (rat_mul a b)) -> iszero_rat a := begin cases a with anum aden, cases b with bnum bden, unfold rat_mul, unfold iszero_rat, unfold defined, intros h1 h2 h3, by_cases h4 : iszero bnum, cc, cases h3, left, rw mul_comm_int_rw at h3, exact mul_result_zero_int bnum anum h4 h3, right, exact mul_result_zero_int_rev aden bden h2 h3, end /- I don't know if you'd really call it a "formula", but the solution to ax + b = 0 is x = -b/a. We may as well prove that, right?-/ def linear_formula : myrat -> myrat -> myrat | a b := negate_rat (rat_mul b (rat_reciprocal a)) lemma linear_formula_works (a b : myrat) : rat_eq (rat_add (rat_mul a (linear_formula a b)) b) rat_zero := begin unfold linear_formula, repeat {rw <- mul_negate_rw}, rw <- rat_mul_assoc a b (negate_rat (rat_reciprocal a)), rw rat_mul_comm a b, rw rat_mul_assoc b a (negate_rat (rat_reciprocal a)), rw mul_negate_rw a (rat_reciprocal a), rw <- mul_negate_alt_rw b (rat_mul a (rat_reciprocal a)), have h1 : rat_eq (rat_mul a (rat_reciprocal a)) rat_one, exact rat_eq_comm rat_one (rat_mul a (rat_reciprocal a)) (reciprocal_mul_inv a), by_cases h2 : (defined a), by_cases h3 : (iszero_rat a), have h4 : ¬defined (rat_reciprocal a), exact reciprocal_zero_undef a h3, have h5 : ¬defined (rat_mul a (rat_reciprocal a)), exact rat_undef_mul a (rat_reciprocal a) h4, have h6 : ¬defined (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))), exact rat_undef_mul (negate_rat b) (rat_mul a (rat_reciprocal a)) h5, have h7 : ¬defined (rat_add (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))) b), exact rat_add_undef (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))) b h6, exact undef_eq (rat_add (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))) b) rat_zero h7, have h4 : rat_eq (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))) (negate_rat b), rw rat_mul_comm (negate_rat b) (rat_mul a (rat_reciprocal a)), exact rat_one_mul (rat_mul a (rat_reciprocal a)) (negate_rat b) h1, have h5 : defined (rat_mul a (rat_reciprocal a)), exact rat_mul_def a (rat_reciprocal a) h2 (reciprocal_def a h2 h3), have h6 : rat_eq (rat_add (negate_rat b) b) rat_zero, rw rat_add_comm, exact add_negate_rat b, have h7 : rat_eq (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))) (negate_rat b), rw rat_mul_comm, exact rat_one_mul (rat_mul a (rat_reciprocal a)) (negate_rat b) (reciprocal_inv_mul a), by_cases h8 : (defined b), exact add_sub_rat (negate_rat b) (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))) b rat_zero (negate_def b h8) (rat_eq_comm (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))) (negate_rat b) h7) h6, rw rat_add_comm, exact undef_eq (rat_add b (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a)))) rat_zero (rat_add_undef b (rat_mul (negate_rat b) (rat_mul a (rat_reciprocal a))) h8), rw rat_mul_comm (negate_rat b) (rat_mul a (rat_reciprocal a)), exact undef_eq (rat_add (rat_mul (rat_mul a (rat_reciprocal a)) (negate_rat b)) b) rat_zero (rat_add_undef (rat_mul (rat_mul a (rat_reciprocal a)) (negate_rat b)) b (rat_mul_undef (rat_mul a (rat_reciprocal a)) (negate_rat b) (rat_mul_undef a (rat_reciprocal a) h2))), end /- OK. a + (b * sqrt(c)) is not a field. However, a + (b * sqrt(c)) where c is constant IS a field. Ergo, we need to define a type constructor where c is part of the type.-/ inductive rat_plus_sqrt (a : myrat) : Type | rps : myrat -> myrat -> rat_plus_sqrt /- A value (a, b) of type (rat_plus_sqrt c) represents the number a + (b * sqrt(c)) This means that operations are only defined when the values inside the square roots are the same.-/ open rat_plus_sqrt def zero_rps (k : myrat) : rat_plus_sqrt k := (rps rat_zero rat_zero) def defined_rps (k : myrat) : rat_plus_sqrt k -> Prop | (rps a b) := (defined a) /\ (defined b) /\ (defined k) def rps_equal (k : myrat) : rat_plus_sqrt k -> rat_plus_sqrt k -> Prop | (rps a b) (rps c d) := (rat_eq a c) /\ (rat_eq b d) /- There are some niche scenarios where this could be wrong. E.g. 3 + 0√4 compared to 1 + 1√4 - they are equal as real numbers but not by the above. However, as we are treating roots as black boxes, it simplifies things dramatically to not do those sorts of comparisons, and for the sake of provign the quadratic formula it doesn't matter.-/ def iszero_rps (k : myrat) : rat_plus_sqrt k -> Prop | (rps a b) := (iszero_rat a) /\ (iszero_rat b) /- Technically this is slightly too strict, as for example 6 + (-3)√4 = 0 but is not detected as zero by the above. However this will do for the purposes of checking the quadratic formula.-/ /- The below definitions cannot be proven to be correct from within Lean, as we are simply defining √k to be the number that gives k when it is multiplied by itself, so Lean knows of no other properties of it. View the comments for an explanation of how each one follows from the field axioms. Therefore the only real assumptions that we are making here is that the square root of a given number exists (which it does if we allow complex numbers) and that the complex numbers are a field.-/ def add_rps (k : myrat) : rat_plus_sqrt k -> rat_plus_sqrt k -> rat_plus_sqrt k | (rps a b) (rps c d) := (rps (rat_add a c) (rat_add b d)) /- This is straightforwardly true because addition is associative and commutative. (a + bq) + (c + dq) = (a + c) + bq + dq no matter what properties q might have. And then because multiplication distributes over addition we can write that as (a + c) + (b + d)q-/ def negate_rps (k : myrat) : rat_plus_sqrt k -> rat_plus_sqrt k | (rps a b) := (rps (negate_rat a) (negate_rat b)) /- Simple. Multiplication distributes over addition, so multiplying both pats of a sum by -1 is equivalent to multiplying the whole thing by -1.-/ def mul_rps (k : myrat) : rat_plus_sqrt k -> rat_plus_sqrt k -> rat_plus_sqrt k | (rps a b) (rps c d) := (rps (rat_add (rat_mul a c) (rat_mul b (rat_mul d k))) (rat_add (rat_mul a d) (rat_mul b c))) /- Multiplication distributes over addition. Ergo (a + b√k)(c + d√k) = a(c + d√k) + b√k(c + d√k) = ac + ad√k + bc√k + bd√k√k Addition is commutative and associative, so we can rearrange terms... = ac + bd√k√k + ad√k + bc√k And then √k√k = k by definition. = ac + bdk + (ad + bd)√k Ergo the above equation makes no assumptions apart from the field axioms and the definition of a square root.-/ def add_rps_to_rat (k : myrat) : rat_plus_sqrt k -> myrat -> rat_plus_sqrt k | (rps a b) c := (rps (rat_add a c) b) /- Trivial by commutativity and associativity of addition.-/ def mul_rps_by_rat (k : myrat) : rat_plus_sqrt k -> myrat -> rat_plus_sqrt k | (rps a b) c := (rps (rat_mul a c) (rat_mul b c)) /- Again, distribution and commutativity of multiplication. (a + b√k)c = ac + b(√k)c = ac + bc√k-/ def sqrt (k : myrat) : rat_plus_sqrt k := (rps rat_zero rat_one) /- Follows easily from how we defined the rat_plus_sqrt type.-/ lemma add_zero_rps_alt (k : myrat) (a : rat_plus_sqrt k) : (add_rps k a (zero_rps k)) = a := begin unfold zero_rps, cases a with rata roota, unfold add_rps, repeat {rw rat_add_zero_alt}, end lemma add_comm_rps (k : myrat) (a b : rat_plus_sqrt k) : (add_rps k a b) = (add_rps k b a) := begin cases a with rata roota, cases b with ratb rootb, unfold add_rps, rw rat_add_comm rata ratb, rw rat_add_comm roota rootb, end lemma add_assoc_rps (k : myrat) (a b c : rat_plus_sqrt k) : (add_rps k (add_rps k a b) c) = (add_rps k a (add_rps k b c)) := begin cases a with rata roota, cases b with ratb rootb, cases c with ratc rootc, unfold add_rps, rw rat_add_assoc rata ratb ratc, rw rat_add_assoc roota rootb rootc, end lemma add_zero_rps (k : myrat) (a b : rat_plus_sqrt k) : (iszero_rps k a) -> (rps_equal k (add_rps k a b) b) := begin cases a with rata roota, cases b with ratb rootb, unfold add_rps, unfold iszero_rps, unfold rps_equal, intros h, cases h with h1 h2, split, exact rat_add_zero rata ratb h1, exact rat_add_zero roota rootb h2, end lemma add_def_rps (k : myrat) (a b : rat_plus_sqrt k) : (defined_rps k a) -> (defined_rps k b) -> (defined_rps k (add_rps k a b)) := begin cases a with rata roota, cases b with ratb rootb, unfold add_rps, unfold defined_rps, intros h1 h2, cases h1 with h11 h12, cases h12 with h12 h13, cases h2 with h21 h22, cases h22 with h22 h23, split, exact rat_add_def rata ratb h11 h21, split, exact rat_add_def roota rootb h12 h22, exact h13, end /- Woo classical logic! -/ lemma negate_and (a b c : Prop) : ¬(a /\ b /\ c) -> (¬a \/ ¬b \/ ¬c) := begin intros h, by_cases ha : a, by_cases hb : b, by_cases hc : c, have h1 : (a /\ b /\ c), split, exact ha, split, exact hb, exact hc, cc, right, right, cc, right, left, cc, left, cc, end lemma add_undef_rps (k : myrat) (a b : rat_plus_sqrt k) : ¬(defined_rps k a) -> ¬(defined_rps k (add_rps k a b)) := begin cases a with rata roota, cases b with ratb rootb, unfold add_rps, unfold defined_rps, intros h, have h1 : (¬(defined rata) \/ ¬(defined roota) \/ ¬(defined k)), exact negate_and (defined rata) (defined roota) (defined k) h, cases h1, intro h2, cases h2 with h2 _, exact rat_add_undef rata ratb h1 h2, cases h1, intro h2, cases h2 with _ h2, cases h2 with h2 _, exact rat_add_undef roota rootb h1 h2, intro h2, cases h2 with _ h2, cases h2 with _ h2, exact h1 h2, end lemma negate_add_inv_rps (k : myrat) (a : rat_plus_sqrt k) : iszero_rps k (add_rps k a (negate_rps k a)) := begin cases a with rata roota, unfold negate_rps, unfold add_rps, unfold iszero_rps, split, exact add_negate_rat_alt rata, exact add_negate_rat_alt roota, end lemma mul_comm_rps (k : myrat) (a b : rat_plus_sqrt k) : (mul_rps k a b) = (mul_rps k b a) := begin cases a with rata roota, cases b with ratb rootb, unfold mul_rps, rw <- rat_mul_assoc roota rootb k, rw rat_mul_comm roota rootb, rw rat_mul_assoc rootb roota k, rw rat_add_comm (rat_mul ratb roota) (rat_mul rootb rata), rw rat_mul_comm ratb roota, rw rat_mul_comm rata rootb, rw rat_mul_comm rata ratb, end lemma mul_assoc_rps (k : myrat) (a b c : rat_plus_sqrt k) : rps_equal k (mul_rps k (mul_rps k a b) c) (mul_rps k a (mul_rps k b c)) := begin cases a with rata roota, cases b with ratb rootb, cases c with ratc rootc, unfold mul_rps, unfold rps_equal, split, repeat {rw rat_add_assoc}, /- Using python for something a bit different this time. The proof is trivial for all the cases where one of the numbers isn't defined, but we don't want to have to through them all manually. Much better to leave that bit to the machine.-/ by_cases ratc_def_tmp : (¬defined ratc), rw rat_mul_comm (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc, exact undef_eq (rat_add (rat_mul ratc (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)))) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k))) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_undef (rat_mul ratc (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)))) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_mul_undef ratc (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc_def_tmp)), have ratc_defined : (defined ratc), cc, clear ratc_def_tmp, by_cases ratb_def_tmp : (¬defined ratb), rw rat_mul_comm rata ratb, exact undef_eq (rat_add (rat_mul (rat_add (rat_mul ratb rata) (rat_mul roota (rat_mul rootb k))) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k))) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_undef (rat_mul (rat_add (rat_mul ratb rata) (rat_mul roota (rat_mul rootb k))) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_mul_undef (rat_add (rat_mul ratb rata) (rat_mul roota (rat_mul rootb k))) ratc (rat_add_undef (rat_mul ratb rata) (rat_mul roota (rat_mul rootb k)) (rat_mul_undef ratb rata ratb_def_tmp)))), have ratb_defined : (defined ratb), cc, clear ratb_def_tmp, by_cases rootb_def_tmp : (¬defined rootb), rw rat_add_comm (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)), rw rat_mul_comm roota (rat_mul rootb k), exact undef_eq (rat_add (rat_mul (rat_add (rat_mul (rat_mul rootb k) roota) (rat_mul rata ratb)) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k))) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_undef (rat_mul (rat_add (rat_mul (rat_mul rootb k) roota) (rat_mul rata ratb)) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_mul_undef (rat_add (rat_mul (rat_mul rootb k) roota) (rat_mul rata ratb)) ratc (rat_add_undef (rat_mul (rat_mul rootb k) roota) (rat_mul rata ratb) (rat_mul_undef (rat_mul rootb k) roota (rat_mul_undef rootb k rootb_def_tmp))))), have rootb_defined : (defined rootb), cc, clear rootb_def_tmp, by_cases roota_def_tmp : (¬defined roota), rw rat_add_comm (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)), exact undef_eq (rat_add (rat_mul (rat_add (rat_mul roota (rat_mul rootb k)) (rat_mul rata ratb)) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k))) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_undef (rat_mul (rat_add (rat_mul roota (rat_mul rootb k)) (rat_mul rata ratb)) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_mul_undef (rat_add (rat_mul roota (rat_mul rootb k)) (rat_mul rata ratb)) ratc (rat_add_undef (rat_mul roota (rat_mul rootb k)) (rat_mul rata ratb) (rat_mul_undef roota (rat_mul rootb k) roota_def_tmp)))), have roota_defined : (defined roota), cc, clear roota_def_tmp, by_cases rata_def_tmp : (¬defined rata), exact undef_eq (rat_add (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k))) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_undef (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_mul_undef (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc (rat_add_undef (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)) (rat_mul_undef rata ratb rata_def_tmp)))), have rata_defined : (defined rata), cc, clear rata_def_tmp, by_cases rootc_def_tmp : (¬defined rootc), rw rat_add_comm (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)), rw rat_mul_comm (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k), exact undef_eq (rat_add (rat_mul (rat_mul rootc k) (rat_add (rat_mul rata rootb) (rat_mul roota ratb))) (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_undef (rat_mul (rat_mul rootc k) (rat_add (rat_mul rata rootb) (rat_mul roota ratb))) (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc) (rat_mul_undef (rat_mul rootc k) (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul_undef rootc k rootc_def_tmp))), have rootc_defined : (defined rootc), cc, clear rootc_def_tmp, by_cases k_def_tmp : (¬defined k), rw rat_add_comm (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)), rw rat_mul_comm roota (rat_mul rootb k), rw rat_mul_comm rootb k, exact undef_eq (rat_add (rat_mul (rat_add (rat_mul (rat_mul k rootb) roota) (rat_mul rata ratb)) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k))) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_undef (rat_mul (rat_add (rat_mul (rat_mul k rootb) roota) (rat_mul rata ratb)) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_mul_undef (rat_add (rat_mul (rat_mul k rootb) roota) (rat_mul rata ratb)) ratc (rat_add_undef (rat_mul (rat_mul k rootb) roota) (rat_mul rata ratb) (rat_mul_undef (rat_mul k rootb) roota (rat_mul_undef k rootb k_def_tmp))))), have k_defined : (defined k), cc, clear k_def_tmp, /- Basically, we now have hypotheses "x_defined : defined X" for every rational number in the proof.-/ apply rat_eq_trans (rat_add (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k))) (rat_add (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc)) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k))) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_def (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc)) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) ((rat_add_def (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc) ((rat_mul_def (rat_mul rata ratb) ratc ((rat_mul_def rata ratb (rata_defined) (ratb_defined))) (ratc_defined))) ((rat_mul_def (rat_mul roota (rat_mul rootb k)) ratc ((rat_mul_def roota (rat_mul rootb k) (roota_defined) ((rat_mul_def rootb k (rootb_defined) (k_defined))))) (ratc_defined))))) ((rat_mul_def (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k) ((rat_add_def (rat_mul rata rootb) (rat_mul roota ratb) ((rat_mul_def rata rootb (rata_defined) (rootb_defined))) ((rat_mul_def roota ratb (roota_defined) (ratb_defined))))) ((rat_mul_def rootc k (rootc_defined) (k_defined))))))(add_eq_rat (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) ratc) (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc)) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_mul_distrib (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)) ratc)), rw rat_add_comm (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc)) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)), apply rat_eq_trans (rat_add (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc))) (rat_add (rat_add (rat_mul (rat_mul rata rootb) (rat_mul rootc k)) (rat_mul (rat_mul roota ratb) (rat_mul rootc k))) (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc))) (rat_add (rat_mul rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add_def (rat_add (rat_mul (rat_mul rata rootb) (rat_mul rootc k)) (rat_mul (rat_mul roota ratb) (rat_mul rootc k))) (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc)) ((rat_add_def (rat_mul (rat_mul rata rootb) (rat_mul rootc k)) (rat_mul (rat_mul roota ratb) (rat_mul rootc k)) ((rat_mul_def (rat_mul rata rootb) (rat_mul rootc k) ((rat_mul_def rata rootb (rata_defined) (rootb_defined))) ((rat_mul_def rootc k (rootc_defined) (k_defined))))) ((rat_mul_def (rat_mul roota ratb) (rat_mul rootc k) ((rat_mul_def roota ratb (roota_defined) (ratb_defined))) ((rat_mul_def rootc k (rootc_defined) (k_defined))))))) ((rat_add_def (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc) ((rat_mul_def (rat_mul rata ratb) ratc ((rat_mul_def rata ratb (rata_defined) (ratb_defined))) (ratc_defined))) ((rat_mul_def (rat_mul roota (rat_mul rootb k)) ratc ((rat_mul_def roota (rat_mul rootb k) (roota_defined) ((rat_mul_def rootb k (rootb_defined) (k_defined))))) (ratc_defined))))))(add_eq_rat (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) (rat_mul rootc k)) (rat_add (rat_mul (rat_mul rata rootb) (rat_mul rootc k)) (rat_mul (rat_mul roota ratb) (rat_mul rootc k))) (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc)) (rat_mul_distrib (rat_mul rata rootb) (rat_mul roota ratb) (rat_mul rootc k))), apply rat_eq_comm _ _, rw rat_mul_comm rata (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))), apply rat_eq_trans (rat_add (rat_mul (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))) rata) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata)) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k))) (rat_add (rat_add (rat_mul (rat_mul rata rootb) (rat_mul rootc k)) (rat_mul (rat_mul roota ratb) (rat_mul rootc k))) (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc))) (rat_add_def (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata)) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k)) ((rat_add_def (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata) ((rat_mul_def (rat_mul ratb ratc) rata ((rat_mul_def ratb ratc (ratb_defined) (ratc_defined))) (rata_defined))) ((rat_mul_def (rat_mul rootb (rat_mul rootc k)) rata ((rat_mul_def rootb (rat_mul rootc k) (rootb_defined) ((rat_mul_def rootc k (rootc_defined) (k_defined))))) (rata_defined))))) ((rat_mul_def roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k) (roota_defined) ((rat_mul_def (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k ((rat_add_def (rat_mul ratb rootc) (rat_mul rootb ratc) ((rat_mul_def ratb rootc (ratb_defined) (rootc_defined))) ((rat_mul_def rootb ratc (rootb_defined) (ratc_defined))))) (k_defined))))))(add_eq_rat (rat_mul (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))) rata) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata)) (rat_mul roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k)) (rat_mul_distrib (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)) rata)), rw rat_mul_comm roota (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k), rw rat_add_comm (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata)) (rat_mul (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k) roota), apply rat_eq_trans (rat_add (rat_mul (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k) roota) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata))) (rat_add (rat_mul (rat_add (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k)) roota) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata))) (rat_add (rat_add (rat_mul (rat_mul rata rootb) (rat_mul rootc k)) (rat_mul (rat_mul roota ratb) (rat_mul rootc k))) (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc))) (rat_add_def (rat_mul (rat_add (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k)) roota) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata)) ((rat_mul_def (rat_add (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k)) roota ((rat_add_def (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k) ((rat_mul_def (rat_mul ratb rootc) k ((rat_mul_def ratb rootc (ratb_defined) (rootc_defined))) (k_defined))) ((rat_mul_def (rat_mul rootb ratc) k ((rat_mul_def rootb ratc (rootb_defined) (ratc_defined))) (k_defined))))) (roota_defined))) ((rat_add_def (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata) ((rat_mul_def (rat_mul ratb ratc) rata ((rat_mul_def ratb ratc (ratb_defined) (ratc_defined))) (rata_defined))) ((rat_mul_def (rat_mul rootb (rat_mul rootc k)) rata ((rat_mul_def rootb (rat_mul rootc k) (rootb_defined) ((rat_mul_def rootc k (rootc_defined) (k_defined))))) (rata_defined))))))(add_eq_rat (rat_mul (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k) roota) (rat_mul (rat_add (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k)) roota) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata)) (mul_eq_rat (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) k) (rat_add (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k)) roota (rat_mul_distrib (rat_mul ratb rootc) (rat_mul rootb ratc) k))), apply rat_eq_trans (rat_add (rat_mul (rat_add (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k)) roota) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata))) (rat_add (rat_add (rat_mul (rat_mul (rat_mul ratb rootc) k) roota) (rat_mul (rat_mul (rat_mul rootb ratc) k) roota)) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata))) (rat_add (rat_add (rat_mul (rat_mul rata rootb) (rat_mul rootc k)) (rat_mul (rat_mul roota ratb) (rat_mul rootc k))) (rat_add (rat_mul (rat_mul rata ratb) ratc) (rat_mul (rat_mul roota (rat_mul rootb k)) ratc))) (rat_add_def (rat_add (rat_mul (rat_mul (rat_mul ratb rootc) k) roota) (rat_mul (rat_mul (rat_mul rootb ratc) k) roota)) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata)) ((rat_add_def (rat_mul (rat_mul (rat_mul ratb rootc) k) roota) (rat_mul (rat_mul (rat_mul rootb ratc) k) roota) ((rat_mul_def (rat_mul (rat_mul ratb rootc) k) roota ((rat_mul_def (rat_mul ratb rootc) k ((rat_mul_def ratb rootc (ratb_defined) (rootc_defined))) (k_defined))) (roota_defined))) ((rat_mul_def (rat_mul (rat_mul rootb ratc) k) roota ((rat_mul_def (rat_mul rootb ratc) k ((rat_mul_def rootb ratc (rootb_defined) (ratc_defined))) (k_defined))) (roota_defined))))) ((rat_add_def (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata) ((rat_mul_def (rat_mul ratb ratc) rata ((rat_mul_def ratb ratc (ratb_defined) (ratc_defined))) (rata_defined))) ((rat_mul_def (rat_mul rootb (rat_mul rootc k)) rata ((rat_mul_def rootb (rat_mul rootc k) (rootb_defined) ((rat_mul_def rootc k (rootc_defined) (k_defined))))) (rata_defined))))))(add_eq_rat (rat_mul (rat_add (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k)) roota) (rat_add (rat_mul (rat_mul (rat_mul ratb rootc) k) roota) (rat_mul (rat_mul (rat_mul rootb ratc) k) roota)) (rat_add (rat_mul (rat_mul ratb ratc) rata) (rat_mul (rat_mul rootb (rat_mul rootc k)) rata)) (rat_mul_distrib (rat_mul (rat_mul ratb rootc) k) (rat_mul (rat_mul rootb ratc) k) roota)), /- The above lines do all the distributive rewrites. This was trivial for ints, but is less trivial for rats.-/ repeat {rw rat_mul_assoc}, repeat {rw rat_add_assoc}, rw <- rat_mul_assoc rootc k roota, rw rat_mul_comm rootc k, rw rat_mul_assoc k rootc roota, rw rat_mul_comm rootc roota, rw <- rat_mul_assoc ratb k (rat_mul roota rootc), rw rat_mul_comm ratb k, rw rat_mul_assoc k ratb (rat_mul roota rootc), rw <- rat_mul_assoc rootb ratc (rat_mul k roota), rw rat_mul_comm rootb ratc, rw rat_mul_assoc ratc rootb (rat_mul k roota), rw <- rat_mul_assoc rootb k roota, rw rat_mul_comm rootb k, rw rat_mul_assoc k rootb roota, rw rat_mul_comm rootb roota, rw <- rat_mul_assoc ratc k (rat_mul roota rootb), rw rat_mul_comm ratc k, rw rat_mul_assoc k ratc (rat_mul roota rootb), rw rat_mul_comm ratc rata, rw <- rat_mul_assoc ratb rata ratc, rw rat_mul_comm ratb rata, rw rat_mul_assoc rata ratb ratc, rw <- rat_mul_assoc rootc k rata, rw rat_mul_comm rootc k, rw rat_mul_assoc k rootc rata, rw rat_mul_comm rootc rata, rw <- rat_mul_assoc rootb k (rat_mul rata rootc), rw rat_mul_comm rootb k, rw rat_mul_assoc k rootb (rat_mul rata rootc), rw <- rat_mul_assoc rootb rata rootc, rw rat_mul_comm rootb rata, rw rat_mul_assoc rata rootb rootc, rw rat_add_comm (rat_add (rat_add (rat_mul k (rat_mul ratb (rat_mul roota rootc))) (rat_mul k (rat_mul ratc (rat_mul roota rootb)))) (rat_mul rata (rat_mul ratb ratc))) (rat_mul k (rat_mul rata (rat_mul rootb rootc))), rw <- rat_mul_assoc rootb k rootc, rw rat_mul_comm rootb k, rw rat_mul_assoc k rootb rootc, rw <- rat_mul_assoc rata k (rat_mul rootb rootc), rw rat_mul_comm rata k, rw rat_mul_assoc k rata (rat_mul rootb rootc), rw <- rat_mul_assoc roota ratb (rat_mul k rootc), rw rat_mul_comm roota ratb, rw rat_mul_assoc ratb roota (rat_mul k rootc), rw <- rat_mul_assoc roota k rootc, rw rat_mul_comm roota k, rw rat_mul_assoc k roota rootc, rw <- rat_mul_assoc ratb k (rat_mul roota rootc), rw rat_mul_comm ratb k, rw rat_mul_assoc k ratb (rat_mul roota rootc), rw <- rat_mul_assoc rootb k ratc, rw rat_mul_comm rootb k, rw rat_mul_assoc k rootb ratc, rw rat_mul_comm rootb ratc, rw <- rat_mul_assoc roota k (rat_mul ratc rootb), rw rat_mul_comm roota k, rw rat_mul_assoc k roota (rat_mul ratc rootb), rw <- rat_mul_assoc roota ratc rootb, rw rat_mul_comm roota ratc, rw rat_mul_assoc ratc roota rootb, repeat {rw <- rat_add_assoc}, rw rat_add_comm (rat_mul rata (rat_mul ratb ratc)) (rat_mul k (rat_mul ratc (rat_mul roota rootb))), exact rat_eq_refl _, by_cases k_def_tmp : (¬defined k), rw rat_add_comm (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)), rw rat_mul_comm roota (rat_mul rootb k), rw rat_mul_comm rootb k, exact undef_eq (rat_add (rat_mul (rat_add (rat_mul (rat_mul k rootb) roota) (rat_mul rata ratb)) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_undef (rat_mul (rat_add (rat_mul (rat_mul k rootb) roota) (rat_mul rata ratb)) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_mul_undef (rat_add (rat_mul (rat_mul k rootb) roota) (rat_mul rata ratb)) rootc (rat_add_undef (rat_mul (rat_mul k rootb) roota) (rat_mul rata ratb) (rat_mul_undef (rat_mul k rootb) roota (rat_mul_undef k rootb k_def_tmp))))), have k_defined : (defined k), cc, clear k_def_tmp, by_cases rootb_def_tmp : (¬defined rootb), rw rat_add_comm (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)), rw rat_mul_comm roota (rat_mul rootb k), exact undef_eq (rat_add (rat_mul (rat_add (rat_mul (rat_mul rootb k) roota) (rat_mul rata ratb)) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_undef (rat_mul (rat_add (rat_mul (rat_mul rootb k) roota) (rat_mul rata ratb)) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_mul_undef (rat_add (rat_mul (rat_mul rootb k) roota) (rat_mul rata ratb)) rootc (rat_add_undef (rat_mul (rat_mul rootb k) roota) (rat_mul rata ratb) (rat_mul_undef (rat_mul rootb k) roota (rat_mul_undef rootb k rootb_def_tmp))))), have rootb_defined : (defined rootb), cc, clear rootb_def_tmp, by_cases rootc_def_tmp : (¬defined rootc), rw rat_mul_comm (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc, exact undef_eq (rat_add (rat_mul rootc (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)))) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_undef (rat_mul rootc (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)))) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_mul_undef rootc (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc_def_tmp)), have rootc_defined : (defined rootc), cc, clear rootc_def_tmp, by_cases roota_def_tmp : (¬defined roota), rw rat_add_comm (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)), exact undef_eq (rat_add (rat_mul (rat_add (rat_mul roota (rat_mul rootb k)) (rat_mul rata ratb)) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_undef (rat_mul (rat_add (rat_mul roota (rat_mul rootb k)) (rat_mul rata ratb)) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_mul_undef (rat_add (rat_mul roota (rat_mul rootb k)) (rat_mul rata ratb)) rootc (rat_add_undef (rat_mul roota (rat_mul rootb k)) (rat_mul rata ratb) (rat_mul_undef roota (rat_mul rootb k) roota_def_tmp)))), have roota_defined : (defined roota), cc, clear roota_def_tmp, by_cases ratc_def_tmp : (¬defined ratc), rw rat_add_comm (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc), rw rat_mul_comm (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc, exact undef_eq (rat_add (rat_mul ratc (rat_add (rat_mul rata rootb) (rat_mul roota ratb))) (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_undef (rat_mul ratc (rat_add (rat_mul rata rootb) (rat_mul roota ratb))) (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc) (rat_mul_undef ratc (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc_def_tmp)), have ratc_defined : (defined ratc), cc, clear ratc_def_tmp, by_cases rata_def_tmp : (¬defined rata), exact undef_eq (rat_add (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_undef (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_mul_undef (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc (rat_add_undef (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)) (rat_mul_undef rata ratb rata_def_tmp)))), have rata_defined : (defined rata), cc, clear rata_def_tmp, by_cases ratb_def_tmp : (¬defined ratb), rw rat_mul_comm rata ratb, exact undef_eq (rat_add (rat_mul (rat_add (rat_mul ratb rata) (rat_mul roota (rat_mul rootb k))) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_undef (rat_mul (rat_add (rat_mul ratb rata) (rat_mul roota (rat_mul rootb k))) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_mul_undef (rat_add (rat_mul ratb rata) (rat_mul roota (rat_mul rootb k))) rootc (rat_add_undef (rat_mul ratb rata) (rat_mul roota (rat_mul rootb k)) (rat_mul_undef ratb rata ratb_def_tmp)))), have ratb_defined : (defined ratb), cc, clear ratb_def_tmp, apply rat_eq_trans (rat_add (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc)) (rat_add (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc)) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc)) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_def (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc)) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) ((rat_add_def (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc) ((rat_mul_def (rat_mul rata ratb) rootc ((rat_mul_def rata ratb (rata_defined) (ratb_defined))) (rootc_defined))) ((rat_mul_def (rat_mul roota (rat_mul rootb k)) rootc ((rat_mul_def roota (rat_mul rootb k) (roota_defined) ((rat_mul_def rootb k (rootb_defined) (k_defined))))) (rootc_defined))))) ((rat_mul_def (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc ((rat_add_def (rat_mul rata rootb) (rat_mul roota ratb) ((rat_mul_def rata rootb (rata_defined) (rootb_defined))) ((rat_mul_def roota ratb (roota_defined) (ratb_defined))))) (ratc_defined))))(add_eq_rat (rat_mul (rat_add (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k))) rootc) (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc)) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_mul_distrib (rat_mul rata ratb) (rat_mul roota (rat_mul rootb k)) rootc)), rw rat_add_comm (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc)) (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc), apply rat_eq_trans (rat_add (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc))) (rat_add (rat_add (rat_mul (rat_mul rata rootb) ratc) (rat_mul (rat_mul roota ratb) ratc)) (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc))) (rat_add (rat_mul rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc))) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add_def (rat_add (rat_mul (rat_mul rata rootb) ratc) (rat_mul (rat_mul roota ratb) ratc)) (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc)) ((rat_add_def (rat_mul (rat_mul rata rootb) ratc) (rat_mul (rat_mul roota ratb) ratc) ((rat_mul_def (rat_mul rata rootb) ratc ((rat_mul_def rata rootb (rata_defined) (rootb_defined))) (ratc_defined))) ((rat_mul_def (rat_mul roota ratb) ratc ((rat_mul_def roota ratb (roota_defined) (ratb_defined))) (ratc_defined))))) ((rat_add_def (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc) ((rat_mul_def (rat_mul rata ratb) rootc ((rat_mul_def rata ratb (rata_defined) (ratb_defined))) (rootc_defined))) ((rat_mul_def (rat_mul roota (rat_mul rootb k)) rootc ((rat_mul_def roota (rat_mul rootb k) (roota_defined) ((rat_mul_def rootb k (rootb_defined) (k_defined))))) (rootc_defined))))))(add_eq_rat (rat_mul (rat_add (rat_mul rata rootb) (rat_mul roota ratb)) ratc) (rat_add (rat_mul (rat_mul rata rootb) ratc) (rat_mul (rat_mul roota ratb) ratc)) (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc)) (rat_mul_distrib (rat_mul rata rootb) (rat_mul roota ratb) ratc)), apply rat_eq_comm _ _, rw rat_mul_comm rata (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)), apply rat_eq_trans (rat_add (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) rata) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add (rat_add (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata)) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))))) (rat_add (rat_add (rat_mul (rat_mul rata rootb) ratc) (rat_mul (rat_mul roota ratb) ratc)) (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc))) (rat_add_def (rat_add (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata)) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) ((rat_add_def (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata) ((rat_mul_def (rat_mul ratb rootc) rata ((rat_mul_def ratb rootc (ratb_defined) (rootc_defined))) (rata_defined))) ((rat_mul_def (rat_mul rootb ratc) rata ((rat_mul_def rootb ratc (rootb_defined) (ratc_defined))) (rata_defined))))) ((rat_mul_def roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))) (roota_defined) ((rat_add_def (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)) ((rat_mul_def ratb ratc (ratb_defined) (ratc_defined))) ((rat_mul_def rootb (rat_mul rootc k) (rootb_defined) ((rat_mul_def rootc k (rootc_defined) (k_defined))))))))))(add_eq_rat (rat_mul (rat_add (rat_mul ratb rootc) (rat_mul rootb ratc)) rata) (rat_add (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata)) (rat_mul roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_mul_distrib (rat_mul ratb rootc) (rat_mul rootb ratc) rata)), rw rat_mul_comm roota (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))), rw rat_add_comm (rat_add (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata)) (rat_mul (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))) roota), apply rat_eq_trans (rat_add (rat_mul (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))) roota) (rat_add (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata))) (rat_add (rat_add (rat_mul (rat_mul ratb ratc) roota) (rat_mul (rat_mul rootb (rat_mul rootc k)) roota)) (rat_add (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata))) (rat_add (rat_add (rat_mul (rat_mul rata rootb) ratc) (rat_mul (rat_mul roota ratb) ratc)) (rat_add (rat_mul (rat_mul rata ratb) rootc) (rat_mul (rat_mul roota (rat_mul rootb k)) rootc))) (rat_add_def (rat_add (rat_mul (rat_mul ratb ratc) roota) (rat_mul (rat_mul rootb (rat_mul rootc k)) roota)) (rat_add (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata)) ((rat_add_def (rat_mul (rat_mul ratb ratc) roota) (rat_mul (rat_mul rootb (rat_mul rootc k)) roota) ((rat_mul_def (rat_mul ratb ratc) roota ((rat_mul_def ratb ratc (ratb_defined) (ratc_defined))) (roota_defined))) ((rat_mul_def (rat_mul rootb (rat_mul rootc k)) roota ((rat_mul_def rootb (rat_mul rootc k) (rootb_defined) ((rat_mul_def rootc k (rootc_defined) (k_defined))))) (roota_defined))))) ((rat_add_def (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata) ((rat_mul_def (rat_mul ratb rootc) rata ((rat_mul_def ratb rootc (ratb_defined) (rootc_defined))) (rata_defined))) ((rat_mul_def (rat_mul rootb ratc) rata ((rat_mul_def rootb ratc (rootb_defined) (ratc_defined))) (rata_defined))))))(add_eq_rat (rat_mul (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k))) roota) (rat_add (rat_mul (rat_mul ratb ratc) roota) (rat_mul (rat_mul rootb (rat_mul rootc k)) roota)) (rat_add (rat_mul (rat_mul ratb rootc) rata) (rat_mul (rat_mul rootb ratc) rata)) (rat_mul_distrib (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)) roota)), repeat {rw rat_mul_assoc}, repeat {rw rat_add_assoc}, rw <- rat_mul_assoc rootc k roota, rw rat_mul_comm rootc k, rw rat_mul_assoc k rootc roota, rw rat_mul_comm rootc roota, rw <- rat_mul_assoc rootb k (rat_mul roota rootc), rw rat_mul_comm rootb k, rw rat_mul_assoc k rootb (rat_mul roota rootc), rw <- rat_mul_assoc rootb roota rootc, rw rat_mul_comm rootb roota, rw rat_mul_assoc roota rootb rootc, rw rat_add_comm (rat_mul ratb (rat_mul ratc roota)) (rat_mul k (rat_mul roota (rat_mul rootb rootc))), rw rat_mul_comm rootc rata, rw <- rat_mul_assoc ratb rata rootc, rw rat_mul_comm ratb rata, rw rat_mul_assoc rata ratb rootc, rw <- rat_mul_assoc rootb ratc rata, rw rat_mul_comm rootb ratc, rw rat_mul_assoc ratc rootb rata, rw rat_mul_comm rootb rata, rw <- rat_mul_assoc ratc rata rootb, rw rat_mul_comm ratc rata, rw rat_mul_assoc rata ratc rootb, apply rat_eq_comm _ _, rw <- rat_mul_assoc roota ratb ratc, rw rat_mul_comm roota ratb, rw rat_mul_assoc ratb roota ratc, rw rat_mul_comm roota ratc, rw rat_add_comm (rat_add (rat_mul rata (rat_mul ratc rootb)) (rat_mul ratb (rat_mul ratc roota))) (rat_mul rata (rat_mul ratb rootc)), rw <- rat_mul_assoc rootb k rootc, rw rat_mul_comm rootb k, rw rat_mul_assoc k rootb rootc, rw <- rat_mul_assoc roota k (rat_mul rootb rootc), rw rat_mul_comm roota k, rw rat_mul_assoc k roota (rat_mul rootb rootc), rw rat_add_comm (rat_add (rat_mul rata (rat_mul ratb rootc)) (rat_add (rat_mul rata (rat_mul ratc rootb)) (rat_mul ratb (rat_mul ratc roota)))) (rat_mul k (rat_mul roota (rat_mul rootb rootc))), rw rat_add_comm (rat_mul rata (rat_mul ratc rootb)) (rat_mul ratb (rat_mul ratc roota)), repeat {rw <- rat_add_assoc}, rw rat_add_assoc (rat_mul ratb (rat_mul ratc roota)) (rat_mul rata (rat_mul ratb rootc)) (rat_mul rata (rat_mul ratc rootb)), rw rat_add_comm (rat_mul ratb (rat_mul ratc roota)) (rat_mul rata (rat_mul ratb rootc)), repeat {rw rat_add_assoc}, exact rat_eq_refl _, end lemma mul_distrib_rps (k : myrat) (a b c : rat_plus_sqrt k) : rps_equal k (mul_rps k (add_rps k a b) c) (add_rps k (mul_rps k a c) (mul_rps k b c)) := begin cases a with rata roota, cases b with ratb rootb, cases c with ratc rootc, unfold add_rps, unfold mul_rps, unfold add_rps, unfold rps_equal, split, by_cases roota_def_tmp : (¬defined roota), rw rat_add_comm (rat_mul (rat_add rata ratb) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k)), exact undef_eq (rat_add (rat_mul (rat_add roota rootb) (rat_mul rootc k)) (rat_mul (rat_add rata ratb) ratc)) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul rootc k))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_add_undef (rat_mul (rat_add roota rootb) (rat_mul rootc k)) (rat_mul (rat_add rata ratb) ratc) (rat_mul_undef (rat_add roota rootb) (rat_mul rootc k) (rat_add_undef roota rootb roota_def_tmp))), have roota_defined : (defined roota), cc, clear roota_def_tmp, by_cases ratb_def_tmp : (¬defined ratb), rw rat_add_comm rata ratb, exact undef_eq (rat_add (rat_mul (rat_add ratb rata) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k))) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul rootc k))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_add_undef (rat_mul (rat_add ratb rata) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k)) (rat_mul_undef (rat_add ratb rata) ratc (rat_add_undef ratb rata ratb_def_tmp))), have ratb_defined : (defined ratb), cc, clear ratb_def_tmp, by_cases rata_def_tmp : (¬defined rata), exact undef_eq (rat_add (rat_mul (rat_add rata ratb) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k))) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul rootc k))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_add_undef (rat_mul (rat_add rata ratb) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k)) (rat_mul_undef (rat_add rata ratb) ratc (rat_add_undef rata ratb rata_def_tmp))), have rata_defined : (defined rata), cc, clear rata_def_tmp, by_cases ratc_def_tmp : (¬defined ratc), rw rat_mul_comm (rat_add rata ratb) ratc, exact undef_eq (rat_add (rat_mul ratc (rat_add rata ratb)) (rat_mul (rat_add roota rootb) (rat_mul rootc k))) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul rootc k))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_add_undef (rat_mul ratc (rat_add rata ratb)) (rat_mul (rat_add roota rootb) (rat_mul rootc k)) (rat_mul_undef ratc (rat_add rata ratb) ratc_def_tmp)), have ratc_defined : (defined ratc), cc, clear ratc_def_tmp, by_cases rootc_def_tmp : (¬defined rootc), rw rat_add_comm (rat_mul (rat_add rata ratb) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k)), rw rat_mul_comm (rat_add roota rootb) (rat_mul rootc k), exact undef_eq (rat_add (rat_mul (rat_mul rootc k) (rat_add roota rootb)) (rat_mul (rat_add rata ratb) ratc)) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul rootc k))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_add_undef (rat_mul (rat_mul rootc k) (rat_add roota rootb)) (rat_mul (rat_add rata ratb) ratc) (rat_mul_undef (rat_mul rootc k) (rat_add roota rootb) (rat_mul_undef rootc k rootc_def_tmp))), have rootc_defined : (defined rootc), cc, clear rootc_def_tmp, by_cases rootb_def_tmp : (¬defined rootb), rw rat_add_comm (rat_mul (rat_add rata ratb) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k)), rw rat_add_comm roota rootb, exact undef_eq (rat_add (rat_mul (rat_add rootb roota) (rat_mul rootc k)) (rat_mul (rat_add rata ratb) ratc)) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul rootc k))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_add_undef (rat_mul (rat_add rootb roota) (rat_mul rootc k)) (rat_mul (rat_add rata ratb) ratc) (rat_mul_undef (rat_add rootb roota) (rat_mul rootc k) (rat_add_undef rootb roota rootb_def_tmp))), have rootb_defined : (defined rootb), cc, clear rootb_def_tmp, by_cases k_def_tmp : (¬defined k), rw rat_add_comm (rat_mul (rat_add rata ratb) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k)), rw rat_mul_comm (rat_add roota rootb) (rat_mul rootc k), rw rat_mul_comm rootc k, exact undef_eq (rat_add (rat_mul (rat_mul k rootc) (rat_add roota rootb)) (rat_mul (rat_add rata ratb) ratc)) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul k rootc))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul k rootc)))) (rat_add_undef (rat_mul (rat_mul k rootc) (rat_add roota rootb)) (rat_mul (rat_add rata ratb) ratc) (rat_mul_undef (rat_mul k rootc) (rat_add roota rootb) (rat_mul_undef k rootc k_def_tmp))), have k_defined : (defined k), cc, clear k_def_tmp, apply rat_eq_trans (rat_add (rat_mul (rat_add rata ratb) ratc) (rat_mul (rat_add roota rootb) (rat_mul rootc k))) (rat_add (rat_add (rat_mul rata ratc) (rat_mul ratb ratc)) (rat_mul (rat_add roota rootb) (rat_mul rootc k))) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul rootc k))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_add_def (rat_add (rat_mul rata ratc) (rat_mul ratb ratc)) (rat_mul (rat_add roota rootb) (rat_mul rootc k)) ((rat_add_def (rat_mul rata ratc) (rat_mul ratb ratc) ((rat_mul_def rata ratc (rata_defined) (ratc_defined))) ((rat_mul_def ratb ratc (ratb_defined) (ratc_defined))))) ((rat_mul_def (rat_add roota rootb) (rat_mul rootc k) ((rat_add_def roota rootb (roota_defined) (rootb_defined))) ((rat_mul_def rootc k (rootc_defined) (k_defined))))))(add_eq_rat (rat_mul (rat_add rata ratb) ratc) (rat_add (rat_mul rata ratc) (rat_mul ratb ratc)) (rat_mul (rat_add roota rootb) (rat_mul rootc k)) (rat_mul_distrib rata ratb ratc)), rw rat_add_comm (rat_add (rat_mul rata ratc) (rat_mul ratb ratc)) (rat_mul (rat_add roota rootb) (rat_mul rootc k)), apply rat_eq_trans (rat_add (rat_mul (rat_add roota rootb) (rat_mul rootc k)) (rat_add (rat_mul rata ratc) (rat_mul ratb ratc))) (rat_add (rat_add (rat_mul roota (rat_mul rootc k)) (rat_mul rootb (rat_mul rootc k))) (rat_add (rat_mul rata ratc) (rat_mul ratb ratc))) (rat_add (rat_add (rat_mul rata ratc) (rat_mul roota (rat_mul rootc k))) (rat_add (rat_mul ratb ratc) (rat_mul rootb (rat_mul rootc k)))) (rat_add_def (rat_add (rat_mul roota (rat_mul rootc k)) (rat_mul rootb (rat_mul rootc k))) (rat_add (rat_mul rata ratc) (rat_mul ratb ratc)) ((rat_add_def (rat_mul roota (rat_mul rootc k)) (rat_mul rootb (rat_mul rootc k)) ((rat_mul_def roota (rat_mul rootc k) (roota_defined) ((rat_mul_def rootc k (rootc_defined) (k_defined))))) ((rat_mul_def rootb (rat_mul rootc k) (rootb_defined) ((rat_mul_def rootc k (rootc_defined) (k_defined))))))) ((rat_add_def (rat_mul rata ratc) (rat_mul ratb ratc) ((rat_mul_def rata ratc (rata_defined) (ratc_defined))) ((rat_mul_def ratb ratc (ratb_defined) (ratc_defined))))))(add_eq_rat (rat_mul (rat_add roota rootb) (rat_mul rootc k)) (rat_add (rat_mul roota (rat_mul rootc k)) (rat_mul rootb (rat_mul rootc k))) (rat_add (rat_mul rata ratc) (rat_mul ratb ratc)) (rat_mul_distrib roota rootb (rat_mul rootc k))), repeat {rw rat_mul_assoc}, repeat {rw rat_add_assoc}, rw rat_mul_comm rootc k, rw <- rat_mul_assoc roota k rootc, rw rat_mul_comm roota k, rw rat_mul_assoc k roota rootc, rw <- rat_mul_assoc rootb k rootc, rw rat_mul_comm rootb k, rw rat_mul_assoc k rootb rootc, repeat {rw rat_mul_assoc}, repeat {rw <- rat_add_assoc}, rw rat_add_comm (rat_mul k (rat_mul rootb rootc)) (rat_add (rat_mul rata ratc) (rat_mul ratb ratc)), repeat {rw rat_add_assoc}, rw rat_add_comm (rat_mul k (rat_mul roota rootc)) (rat_mul rata ratc), exact rat_eq_refl _, by_cases rootb_def_tmp : (¬defined rootb), rw rat_add_comm (rat_mul (rat_add rata ratb) rootc) (rat_mul (rat_add roota rootb) ratc), rw rat_add_comm roota rootb, exact undef_eq (rat_add (rat_mul (rat_add rootb roota) ratc) (rat_mul (rat_add rata ratb) rootc)) (rat_add (rat_add (rat_add (rat_mul rata rootc) (rat_mul roota ratc)) (rat_mul ratb rootc)) (rat_mul rootb ratc)) (rat_add_undef (rat_mul (rat_add rootb roota) ratc) (rat_mul (rat_add rata ratb) rootc) (rat_mul_undef (rat_add rootb roota) ratc (rat_add_undef rootb roota rootb_def_tmp))), have rootb_defined : (defined rootb), cc, clear rootb_def_tmp, by_cases rootc_def_tmp : (¬defined rootc), rw rat_mul_comm (rat_add rata ratb) rootc, exact undef_eq (rat_add (rat_mul rootc (rat_add rata ratb)) (rat_mul (rat_add roota rootb) ratc)) (rat_add (rat_add (rat_add (rat_mul rata rootc) (rat_mul roota ratc)) (rat_mul ratb rootc)) (rat_mul rootb ratc)) (rat_add_undef (rat_mul rootc (rat_add rata ratb)) (rat_mul (rat_add roota rootb) ratc) (rat_mul_undef rootc (rat_add rata ratb) rootc_def_tmp)), have rootc_defined : (defined rootc), cc, clear rootc_def_tmp, by_cases ratc_def_tmp : (¬defined ratc), rw rat_add_comm (rat_mul (rat_add rata ratb) rootc) (rat_mul (rat_add roota rootb) ratc), rw rat_mul_comm (rat_add roota rootb) ratc, exact undef_eq (rat_add (rat_mul ratc (rat_add roota rootb)) (rat_mul (rat_add rata ratb) rootc)) (rat_add (rat_add (rat_add (rat_mul rata rootc) (rat_mul roota ratc)) (rat_mul ratb rootc)) (rat_mul rootb ratc)) (rat_add_undef (rat_mul ratc (rat_add roota rootb)) (rat_mul (rat_add rata ratb) rootc) (rat_mul_undef ratc (rat_add roota rootb) ratc_def_tmp)), have ratc_defined : (defined ratc), cc, clear ratc_def_tmp, by_cases rata_def_tmp : (¬defined rata), exact undef_eq (rat_add (rat_mul (rat_add rata ratb) rootc) (rat_mul (rat_add roota rootb) ratc)) (rat_add (rat_add (rat_add (rat_mul rata rootc) (rat_mul roota ratc)) (rat_mul ratb rootc)) (rat_mul rootb ratc)) (rat_add_undef (rat_mul (rat_add rata ratb) rootc) (rat_mul (rat_add roota rootb) ratc) (rat_mul_undef (rat_add rata ratb) rootc (rat_add_undef rata ratb rata_def_tmp))), have rata_defined : (defined rata), cc, clear rata_def_tmp, by_cases ratb_def_tmp : (¬defined ratb), rw rat_add_comm rata ratb, exact undef_eq (rat_add (rat_mul (rat_add ratb rata) rootc) (rat_mul (rat_add roota rootb) ratc)) (rat_add (rat_add (rat_add (rat_mul rata rootc) (rat_mul roota ratc)) (rat_mul ratb rootc)) (rat_mul rootb ratc)) (rat_add_undef (rat_mul (rat_add ratb rata) rootc) (rat_mul (rat_add roota rootb) ratc) (rat_mul_undef (rat_add ratb rata) rootc (rat_add_undef ratb rata ratb_def_tmp))), have ratb_defined : (defined ratb), cc, clear ratb_def_tmp, by_cases roota_def_tmp : (¬defined roota), rw rat_add_comm (rat_mul (rat_add rata ratb) rootc) (rat_mul (rat_add roota rootb) ratc), exact undef_eq (rat_add (rat_mul (rat_add roota rootb) ratc) (rat_mul (rat_add rata ratb) rootc)) (rat_add (rat_add (rat_add (rat_mul rata rootc) (rat_mul roota ratc)) (rat_mul ratb rootc)) (rat_mul rootb ratc)) (rat_add_undef (rat_mul (rat_add roota rootb) ratc) (rat_mul (rat_add rata ratb) rootc) (rat_mul_undef (rat_add roota rootb) ratc (rat_add_undef roota rootb roota_def_tmp))), have roota_defined : (defined roota), cc, clear roota_def_tmp, apply rat_eq_trans (rat_add (rat_mul (rat_add rata ratb) rootc) (rat_mul (rat_add roota rootb) ratc)) (rat_add (rat_add (rat_mul rata rootc) (rat_mul ratb rootc)) (rat_mul (rat_add roota rootb) ratc)) (rat_add (rat_add (rat_add (rat_mul rata rootc) (rat_mul roota ratc)) (rat_mul ratb rootc)) (rat_mul rootb ratc)) (rat_add_def (rat_add (rat_mul rata rootc) (rat_mul ratb rootc)) (rat_mul (rat_add roota rootb) ratc) ((rat_add_def (rat_mul rata rootc) (rat_mul ratb rootc) ((rat_mul_def rata rootc (rata_defined) (rootc_defined))) ((rat_mul_def ratb rootc (ratb_defined) (rootc_defined))))) ((rat_mul_def (rat_add roota rootb) ratc ((rat_add_def roota rootb (roota_defined) (rootb_defined))) (ratc_defined))))(add_eq_rat (rat_mul (rat_add rata ratb) rootc) (rat_add (rat_mul rata rootc) (rat_mul ratb rootc)) (rat_mul (rat_add roota rootb) ratc) (rat_mul_distrib rata ratb rootc)), rw rat_add_comm (rat_add (rat_mul rata rootc) (rat_mul ratb rootc)) (rat_mul (rat_add roota rootb) ratc), apply rat_eq_trans (rat_add (rat_mul (rat_add roota rootb) ratc) (rat_add (rat_mul rata rootc) (rat_mul ratb rootc))) (rat_add (rat_add (rat_mul roota ratc) (rat_mul rootb ratc)) (rat_add (rat_mul rata rootc) (rat_mul ratb rootc))) (rat_add (rat_add (rat_add (rat_mul rata rootc) (rat_mul roota ratc)) (rat_mul ratb rootc)) (rat_mul rootb ratc)) (rat_add_def (rat_add (rat_mul roota ratc) (rat_mul rootb ratc)) (rat_add (rat_mul rata rootc) (rat_mul ratb rootc)) ((rat_add_def (rat_mul roota ratc) (rat_mul rootb ratc) ((rat_mul_def roota ratc (roota_defined) (ratc_defined))) ((rat_mul_def rootb ratc (rootb_defined) (ratc_defined))))) ((rat_add_def (rat_mul rata rootc) (rat_mul ratb rootc) ((rat_mul_def rata rootc (rata_defined) (rootc_defined))) ((rat_mul_def ratb rootc (ratb_defined) (rootc_defined))))))(add_eq_rat (rat_mul (rat_add roota rootb) ratc) (rat_add (rat_mul roota ratc) (rat_mul rootb ratc)) (rat_add (rat_mul rata rootc) (rat_mul ratb rootc)) (rat_mul_distrib roota rootb ratc)), repeat {rw <- rat_add_assoc}, rw rat_add_comm (rat_mul rootb ratc) (rat_add (rat_mul rata rootc) (rat_mul ratb rootc)), repeat {rw rat_add_assoc}, rw rat_add_comm (rat_mul roota ratc) (rat_mul rata rootc), exact rat_eq_refl _, end /- TODO: Prove that multipliplying an rps by a rat still works as a multiplication by the field axioms.-/ def rat_four : myrat := (rat (int zero.mysucc.mysucc.mysucc.mysucc zero) (int zero.mysucc zero)) /- b^2 - 4ac -/ def discriminant : myrat -> myrat -> myrat -> myrat | a b c := (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) /- As this is of type (rat_plus_sqrt (discriminant a b c)) then it represents a number of the form x + y * sqrt (discriminant) The various occurrences of (discriminant a b c) make the formula more difficult to read, so a clearer presentaion would be mul_rps_by_rat (add_rps_to_rat (sqrt (discriminant) - b)) (reciprocal (a + a) Which is hopefully fairly obviously the well-known quadratic formula.-/ def quadratic_formula (a b c : myrat) : (rat_plus_sqrt (discriminant a b c)) := mul_rps_by_rat (discriminant a b c) (add_rps_to_rat (discriminant a b c) (sqrt (discriminant a b c)) (negate_rat b)) (rat_reciprocal (rat_add a a)) /- rat_plus_sqrt is a dependent type that takes a rat as argument. -/ /- a + (b * sqrt(c)) is represented by the value (a,b) of type (rat_plus_sqrt c)-/ def quadratic_subst (k : myrat) : myrat -> myrat -> myrat -> (rat_plus_sqrt k) -> (rat_plus_sqrt k) | a b c x := add_rps_to_rat k (add_rps k (mul_rps_by_rat k (mul_rps k x x) a) (mul_rps_by_rat k x b)) c lemma half_defined : defined (rat_reciprocal rat_two) := begin unfold rat_two, unfold rat_reciprocal, unfold defined, apply mul_nonzero_int (int zero.mysucc.mysucc zero) int_one, unfold iszero, cc, unfold int_one, unfold nat_one, unfold iszero, cc, end lemma rat_two_defined : defined (rat_two) := begin unfold rat_two, unfold defined, unfold int_one, unfold iszero, unfold nat_one, cc, end lemma four_divided_by_two : rat_eq (rat_mul rat_four (rat_reciprocal rat_two)) rat_two := begin unfold rat_two, unfold rat_four, unfold rat_reciprocal, unfold rat_mul, unfold rat_eq, left, unfold int_one, unfold int_mul, unfold nat_one, repeat {unfold mymul}, repeat {rw zero_add}, repeat {rw add_zero}, unfold mymul, unfold myadd, exact int_equal_refl _, end lemma add_two_halves (a : myrat) : rat_eq a (rat_add (rat_mul a (rat_reciprocal rat_two)) (rat_mul a (rat_reciprocal rat_two))) := begin cases a with num den, unfold rat_two, unfold rat_reciprocal, unfold rat_mul, repeat {rw mul_one_int_rw}, unfold rat_add, unfold rat_eq, left, rw mul_comm_int_rw den (int zero.mysucc.mysucc zero), rw mul_two_int den, repeat {rw mul_distrib_int_rw_alt}, repeat {rw mul_distrib_int_rw}, repeat {rw mul_distrib_int_rw_alt}, repeat {rw mul_distrib_int_rw}, repeat {rw <- mul_assoc_int_rw den num den}, repeat {rw mul_comm_int_rw den num}, repeat {rw mul_assoc_int_rw num den den}, repeat {rw add_assoc_int_rw}, exact int_equal_refl _, end lemma mul_two_rat (a : myrat) : rat_eq (rat_mul a rat_two) (rat_add a a) := begin cases a with num den, unfold rat_two, unfold rat_mul, rw mul_comm_int_rw num (int zero.mysucc.mysucc zero), rw mul_two_int, unfold rat_add, unfold rat_eq, left, rw mul_one_int_rw, rw mul_distrib_int_rw, rw mul_comm_int_rw den (int_add (int_mul num den) (int_mul num den)), rw mul_distrib_int_rw, repeat {rw <- mul_assoc_int_rw}, exact int_equal_refl _, end lemma rat_zero_defined : defined (rat_zero) := begin unfold rat_zero, unfold defined, unfold int_one, unfold iszero, unfold nat_one, cc, end lemma quadratic_formula_works (a b c : myrat) : iszero_rps (discriminant a b c) (quadratic_subst (discriminant a b c) a b c (quadratic_formula a b c)) := begin /- As I have the intention of using a different approach for the cubic and quartic proofs, this is probably the swan-song of the python-generated code. But Python is going to go out with a bang, I promise. ;-) -/ unfold quadratic_subst, unfold quadratic_formula, unfold sqrt, unfold add_rps_to_rat, rw rat_add_comm rat_zero (negate_rat b), rw rat_add_zero_alt (negate_rat b), unfold mul_rps_by_rat, rw rat_mul_comm rat_one (rat_reciprocal (rat_add a a)), rw rat_mul_one_rw, unfold mul_rps, unfold mul_rps_by_rat, unfold add_rps, unfold add_rps_to_rat, unfold iszero_rps, split, unfold discriminant, by_cases a_def_tmp : (¬defined a), rw rat_mul_comm (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a, exact undef_iszero (rat_add (rat_add (rat_mul a (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))))) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add_undef (rat_add (rat_mul a (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))))) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c (rat_add_undef (rat_mul a (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))))) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_mul_undef a (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a_def_tmp))), have a_defined : (defined a), cc, clear a_def_tmp, by_cases c_def_tmp : (¬defined c), rw rat_add_comm (rat_add (rat_mul (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c, exact undef_iszero (rat_add c (rat_add (rat_mul (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b))) (rat_add_undef c (rat_add (rat_mul (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c_def_tmp), have c_defined : (defined c), cc, clear c_def_tmp, by_cases b_def_tmp : (¬defined b), rw rat_add_comm (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))), rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))), rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))), exact undef_iszero (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add_undef (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c (rat_add_undef (rat_mul (rat_add (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_mul_undef (rat_add (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))))) a (rat_add_undef (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul_undef (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) (rat_mul_undef (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a)) (rat_add_undef (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul_undef b b b_def_tmp)))))))), have b_defined : (defined b), cc, clear b_def_tmp, have rat_four_defined : (defined rat_four), unfold rat_four, unfold defined, unfold iszero, cc, by_cases (iszero_rat a), have int1 : ¬defined (rat_reciprocal (rat_add a a)), apply reciprocal_zero_undef (rat_add a a), exact zeroes_only_eq_rat a (rat_add a a) a_defined (rat_eq_comm (rat_add a a) a (rat_add_zero a a h)) h, rw rat_mul_comm (negate_rat b) (rat_reciprocal (rat_add a a)), exact undef_iszero (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) b)) c) ((rat_add_undef (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) b)) c (rat_add_undef (rat_mul (rat_add (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) b) (rat_mul_undef (rat_add (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a (rat_add_undef (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) (rat_mul_undef (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat b)) (rat_mul_undef (rat_reciprocal (rat_add a a)) (negate_rat b) int1))))))), have int1 : defined (rat_reciprocal (rat_add a a)), exact reciprocal_def (rat_add a a) (rat_add_def a a a_defined a_defined) (add_self_nonzero_rat a h), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add_def (rat_add (rat_add (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c ((rat_add_def (rat_add (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) ((rat_add_def (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) a) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))) ((rat_mul_def (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) a ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))) (int1) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (int1) ((rat_add_def (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))) ((rat_mul_def b b (b_defined) (b_defined))) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))))))))) (a_defined))))) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))))) (c_defined))(add_eq_rat (rat_add (rat_add (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add (rat_mul (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c (add_eq_rat (rat_add (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) a)) (rat_mul (rat_add (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))))))) a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) ( (rat_eq_comm _ _ (rat_mul_distrib (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))))) a))))), rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))), rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))), rw rat_add_comm (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add_def (rat_add (rat_add (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c ((rat_add_def (rat_add (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) ((rat_add_def (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) ((rat_mul_def (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a)) ((rat_add_def (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) ((rat_mul_def (rat_mul b b) (rat_reciprocal (rat_add a a)) ((rat_mul_def b b (b_defined) (b_defined))) (int1))) ((rat_mul_def (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))) (int1))))) (int1))) (a_defined))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))))) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))))) (c_defined))(add_eq_rat (rat_add (rat_add (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c (add_eq_rat (rat_add (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add (rat_mul (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (add_eq_rat (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (mul_eq_rat (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a (mul_eq_rat (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_add (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ( (rat_eq_comm _ _ (rat_mul_distrib (rat_mul b b) (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))))))))), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add_def (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c ((rat_add_def (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) ((rat_add_def (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) ((rat_mul_def (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a ((rat_add_def (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) ((rat_mul_def (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (rat_mul b b) (rat_reciprocal (rat_add a a)) ((rat_mul_def b b (b_defined) (b_defined))) (int1))) (int1))) ((rat_mul_def (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))) (int1))) (int1))))) (a_defined))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))))) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))))) (c_defined))(add_eq_rat (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add (rat_add (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c (add_eq_rat (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (add_eq_rat (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (mul_eq_rat (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) (rat_mul (rat_add (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a))) a ( (rat_eq_comm _ _ (rat_mul_distrib (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))))))))), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add_def (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c ((rat_add_def (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) ((rat_add_def (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) ((rat_add_def (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) ((rat_mul_def (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (rat_mul b b) (rat_reciprocal (rat_add a a)) ((rat_mul_def b b (b_defined) (b_defined))) (int1))) (int1))) (a_defined))) ((rat_mul_def (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))) (int1))) (int1))) (a_defined))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))))) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))))) (c_defined))(add_eq_rat (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c (add_eq_rat (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (add_eq_rat (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_add (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)))) a) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) ( (rat_eq_comm _ _ (rat_mul_distrib (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)))))), /- Multiply through by 2a. As a ≠ 0 this doesn't affect the results of the iszero, but can make things easier.-/ apply iszero_mul_rat (rat_add (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add a a) (add_self_nonzero_rat a h) (rat_add_def a a a_defined a_defined), /- Now we do a lot of work to cancel the 2a and the 1/2a in every term where they both appear.-/ apply zeroes_only_eq_rat (rat_add (rat_mul (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add a a)) (rat_mul c (rat_add a a))) (rat_mul (rat_add (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c) (rat_add a a)) (rat_add_def (rat_mul (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add a a)) (rat_mul c (rat_add a a)) ((rat_mul_def (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add a a) ((rat_add_def (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) ((rat_add_def (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) ((rat_add_def (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) ((rat_mul_def (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (rat_mul b b) (rat_reciprocal (rat_add a a)) ((rat_mul_def b b (b_defined) (b_defined))) (int1))) (int1))) (a_defined))) ((rat_mul_def (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))) (int1))) (int1))) (a_defined))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))))) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))))) ((rat_add_def a a (a_defined) (a_defined))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))( (rat_eq_comm _ _ (rat_mul_distrib (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) c (rat_add a a)))), apply zeroes_only_eq_rat (rat_add (rat_add (rat_mul (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add (rat_mul (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add a a)) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_mul (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_mul (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) ((rat_mul_def (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add a a) ((rat_add_def (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) ((rat_add_def (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) ((rat_mul_def (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (rat_mul b b) (rat_reciprocal (rat_add a a)) ((rat_mul_def b b (b_defined) (b_defined))) (int1))) (int1))) (a_defined))) ((rat_mul_def (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))) (int1))) (int1))) (a_defined))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))))) ((rat_add_def a a (a_defined) (a_defined))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_mul (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b)) (rat_add a a)) (rat_mul c (rat_add a a)) ( (rat_eq_comm _ _ (rat_mul_distrib (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))))), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add (rat_add (rat_mul (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) ((rat_add_def (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)) ((rat_mul_def (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a) ((rat_add_def (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) ((rat_mul_def (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (rat_mul b b) (rat_reciprocal (rat_add a a)) ((rat_mul_def b b (b_defined) (b_defined))) (int1))) (int1))) (a_defined))) ((rat_mul_def (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))) (int1))) (int1))) (a_defined))))) ((rat_add_def a a (a_defined) (a_defined))))) ((rat_mul_def (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_add (rat_mul (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) (add_eq_rat (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) ( (rat_eq_comm _ _ (rat_mul_distrib (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)))))), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) ((rat_add_def (rat_add (rat_mul (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)) ((rat_add_def (rat_mul (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) ((rat_mul_def (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a) ((rat_mul_def (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (rat_mul b b) (rat_reciprocal (rat_add a a)) ((rat_mul_def b b (b_defined) (b_defined))) (int1))) (int1))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))) ((rat_mul_def (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a) ((rat_mul_def (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))) (int1))) (int1))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_add (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) (add_eq_rat (rat_add (rat_add (rat_mul (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_add (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) (add_eq_rat (rat_add (rat_mul (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_add (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)) ( (rat_eq_comm _ _ (rat_mul_distrib (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))))))), rw rat_mul_comm (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a, rw rat_mul_comm (rat_add a a) (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_mul (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_add a a)) a, rw rat_mul_comm (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_add a a) (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_mul (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_add a a)) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_mul (rat_mul b b) (rat_reciprocal (rat_add a a))) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul b b) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_add a a) (rat_mul b b), rw rat_mul_assoc (rat_mul b b) (rat_add a a) (rat_reciprocal (rat_add a a)), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_mul b b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) ((rat_add_def (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)) ((rat_add_def (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) ((rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b b) (int1) ((rat_mul_def b b (b_defined) (b_defined))))))) ((rat_mul_def (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a) ((rat_mul_def (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a ((rat_mul_def (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)) ((rat_mul_def (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))) (int1))) (int1))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_mul b b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) (add_eq_rat (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_mul b b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) (add_eq_rat (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_mul b b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)) (add_eq_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_mul b b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)) (mul_eq_rat_alt (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_mul b b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))) a (mul_eq_rat_alt (rat_mul b b) (rat_mul (rat_mul b b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a)) (reciprocal_mul_cancel (rat_mul b b) (rat_add a a)))))))), rw rat_add_comm (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a)), rw rat_mul_comm (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) a, rw rat_mul_comm (rat_add a a) (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_mul (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_add a a)) a, rw rat_mul_comm (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a))) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_add a a) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_mul (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_add a a)) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a))) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_add a a) (negate_rat (rat_mul rat_four (rat_mul a c))), rw rat_mul_assoc (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_add a a) (rat_reciprocal (rat_add a a)), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) ((rat_add_def (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)) ((rat_add_def (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) ((rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c)))) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))) (int1) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))))))) ((rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b b) (int1) ((rat_mul_def b b (b_defined) (b_defined))))))))) ((rat_mul_def (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) (add_eq_rat (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_add (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) (add_eq_rat (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)) (add_eq_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (mul_eq_rat_alt (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c)))) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))) a (mul_eq_rat_alt (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))) (rat_reciprocal (rat_add a a)) (reciprocal_mul_cancel (negate_rat (rat_mul rat_four (rat_mul a c))) (rat_add a a)))))))), rw rat_add_comm (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a)), rw rat_mul_comm (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) a, rw rat_mul_comm (rat_add a a) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))), rw rat_mul_comm (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_add a a)) a, rw rat_mul_comm (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a)))) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_add a a) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_add a a)) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (negate_rat b) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_add a a) (negate_rat b), rw rat_mul_assoc (negate_rat b) (rat_add a a) (rat_reciprocal (rat_add a a)), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add (rat_add (rat_add (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_add (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_add (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) ((rat_add_def (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) ((rat_mul_def a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b)) (a_defined) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b) ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) ((negate_def b b_defined)))))) ((rat_add_def (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) ((rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c)))) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))) (int1) ((negate_def (rat_mul rat_four (rat_mul a c)) (rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))))))) ((rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b b) (int1) ((rat_mul_def b b (b_defined) (b_defined))))))))))) ((rat_mul_def (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a) ((rat_mul_def (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b ((rat_mul_def (negate_rat b) (rat_reciprocal (rat_add a a)) ((negate_def b b_defined)) (int1))) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_add (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_add (rat_add (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a))) (rat_mul c (rat_add a a)) (add_eq_rat (rat_add (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_add (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_mul (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) b) (rat_add a a)) (add_eq_rat (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b))) (rat_mul a (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (mul_eq_rat_alt (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (negate_rat b)) (rat_mul (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (rat_mul (negate_rat b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))) a (mul_eq_rat_alt (negate_rat b) (rat_mul (negate_rat b) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))) (rat_mul (negate_rat b) (rat_reciprocal (rat_add a a))) (reciprocal_mul_cancel (negate_rat b) (rat_add a a))))))), repeat {rw mul_negate_alt_rw}, rw rat_add_comm (rat_add (rat_mul a (rat_mul (rat_mul b (negate_rat (rat_reciprocal (rat_add a a)))) (negate_rat b))) (rat_add (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (negate_rat (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_mul (rat_mul (rat_mul b (negate_rat (rat_reciprocal (rat_add a a)))) b) (rat_add a a)), repeat {rw rat_mul_assoc}, repeat {rw mul_negate_rw}, repeat {rw mul_negate_alt_rw}, repeat {rw mul_negate_rw}, rw double_negate, rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_mul b (rat_add a a)), rw rat_mul_assoc b (rat_add a a) (rat_reciprocal (rat_add a a)), apply zeroes_only_eq_rat (rat_add (rat_add (negate_rat (rat_mul b b)) (rat_add (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))))) (rat_mul c (rat_add a a))) (rat_add (rat_add (negate_rat (rat_mul b (rat_mul b (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_add (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (negate_rat (rat_mul b b)) (rat_add (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))))) (rat_mul c (rat_add a a)) ((rat_add_def (negate_rat (rat_mul b b)) (rat_add (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) ((negate_def (rat_mul b b) (rat_mul_def b b (b_defined) (b_defined)))) ((rat_add_def (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) ((rat_mul_def a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b)) (a_defined) ((rat_mul_def b (rat_mul (rat_reciprocal (rat_add a a)) b) (b_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) b (int1) (b_defined))))))) ((rat_add_def (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) ((negate_def (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c)))) (rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c)) (int1) ((rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))))))) ((rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b b) (int1) ((rat_mul_def b b (b_defined) (b_defined))))))))))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (negate_rat (rat_mul b b)) (rat_add (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))))) (rat_add (negate_rat (rat_mul b (rat_mul b (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_add (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))))) (rat_mul c (rat_add a a)) (add_eq_rat (negate_rat (rat_mul b b)) (negate_rat (rat_mul b (rat_mul b (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (rat_add (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (negate_eq_rat (rat_mul b b) (rat_mul b (rat_mul b (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))) (mul_eq_rat_alt b (rat_mul b (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))) b (reciprocal_mul_cancel b (rat_add a a)))))), /- Now we have two locations with a * 1/(a + a), so we can cancel them down to 1/2 -/ rw rat_add_comm (negate_rat (rat_mul b b)) (rat_add (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) b))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))), repeat {rw rat_mul_assoc}, rw <- rat_mul_assoc a b (rat_mul (rat_reciprocal (rat_add a a)) b), rw rat_mul_comm a b, rw rat_mul_assoc b a (rat_mul (rat_reciprocal (rat_add a a)) b), rw <- rat_mul_assoc a (rat_reciprocal (rat_add a a)) b, rw rat_mul_comm a (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_mul (rat_reciprocal (rat_add a a)) a) b, rw rat_mul_comm (rat_reciprocal (rat_add a a)) a, apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a))) (rat_add (rat_add (rat_add (rat_mul b (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_add (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_add (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (negate_rat (rat_mul b b)) ((rat_add_def (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) ((rat_mul_def b (rat_mul b (rat_reciprocal rat_two)) (b_defined) ((rat_mul_def b (rat_reciprocal rat_two) (b_defined) (half_defined))))) ((rat_add_def (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) ((negate_def (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c)))) (rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c)) (int1) ((rat_mul_def rat_four (rat_mul a c) (rat_four_defined) ((rat_mul_def a c (a_defined) (c_defined)))))))))) ((rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b b) (int1) ((rat_mul_def b b (b_defined) (b_defined))))))))))) ((negate_def (rat_mul b b) (rat_mul_def b b (b_defined) (b_defined)))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_add (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (negate_rat (rat_mul b b))) (rat_add (rat_add (rat_mul b (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)) (add_eq_rat (rat_add (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (rat_add (rat_mul b (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))))) (negate_rat (rat_mul b b)) (add_eq_rat (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (rat_mul b (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (mul_eq_rat_alt (rat_mul b (rat_reciprocal rat_two)) (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a)))) b (mul_eq_rat_alt (rat_reciprocal rat_two) (rat_mul a (rat_reciprocal (rat_add a a))) b (half_reciprocal_mul a)))))), rw rat_add_comm (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))), rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_mul rat_four (rat_mul a c)), rw rat_mul_assoc rat_four (rat_mul a c) (rat_reciprocal (rat_add a a)), rw rat_mul_assoc a c (rat_reciprocal (rat_add a a)), rw <- rat_mul_assoc a c (rat_reciprocal (rat_add a a)), rw rat_mul_comm a c, rw rat_mul_assoc c a (rat_reciprocal (rat_add a a)), apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a))) (rat_add (rat_add (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_mul a (rat_reciprocal (rat_add a a))))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b)) ((rat_add_def (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two))) ((rat_add_def (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) ((negate_def (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))) (rat_mul_def a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))) (a_defined) ((rat_mul_def rat_four (rat_mul c (rat_reciprocal rat_two)) (rat_four_defined) ((rat_mul_def c (rat_reciprocal rat_two) (c_defined) (half_defined)))))))) ((rat_mul_def a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)) (a_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b b) (int1) ((rat_mul_def b b (b_defined) (b_defined))))))))) ((rat_mul_def b (rat_mul b (rat_reciprocal rat_two)) (b_defined) ((rat_mul_def b (rat_reciprocal rat_two) (b_defined) (half_defined))))))) ((negate_def (rat_mul b b) (rat_mul_def b b (b_defined) (b_defined)))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_add (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_mul a (rat_reciprocal (rat_add a a))))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)) (add_eq_rat (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_mul a (rat_reciprocal (rat_add a a))))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b)) (add_eq_rat (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_mul a (rat_reciprocal (rat_add a a))))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b)))) (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (add_eq_rat (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_mul a (rat_reciprocal (rat_add a a))))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))) (negate_eq_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))) (rat_mul a (rat_mul rat_four (rat_mul c (rat_mul a (rat_reciprocal (rat_add a a)))))) (mul_eq_rat_alt (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))) (rat_mul rat_four (rat_mul c (rat_mul a (rat_reciprocal (rat_add a a))))) a (mul_eq_rat_alt (rat_mul c (rat_reciprocal rat_two)) (rat_mul c (rat_mul a (rat_reciprocal (rat_add a a)))) rat_four (mul_eq_rat_alt (rat_reciprocal rat_two) (rat_mul a (rat_reciprocal (rat_add a a))) c (half_reciprocal_mul a))))))))), rw rat_add_comm (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul a (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b b))), rw <- rat_mul_assoc a (rat_reciprocal (rat_add a a)) (rat_mul b b), rw rat_mul_comm a (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_mul (rat_reciprocal (rat_add a a)) a) (rat_mul b b), rw rat_mul_comm (rat_reciprocal (rat_add a a)) a, apply zeroes_only_eq_rat (rat_add (rat_add (rat_add (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a))) (rat_add (rat_add (rat_add (rat_add (rat_mul (rat_mul b b) (rat_mul a (rat_reciprocal (rat_add a a)))) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a))) (rat_add_def (rat_add (rat_add (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)) ((rat_add_def (rat_add (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b)) ((rat_add_def (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two))) ((rat_add_def (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) ((rat_mul_def (rat_mul b b) (rat_reciprocal rat_two) ((rat_mul_def b b (b_defined) (b_defined))) (half_defined))) ((negate_def (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))) (rat_mul_def a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))) (a_defined) ((rat_mul_def rat_four (rat_mul c (rat_reciprocal rat_two)) (rat_four_defined) ((rat_mul_def c (rat_reciprocal rat_two) (c_defined) (half_defined)))))))))) ((rat_mul_def b (rat_mul b (rat_reciprocal rat_two)) (b_defined) ((rat_mul_def b (rat_reciprocal rat_two) (b_defined) (half_defined))))))) ((negate_def (rat_mul b b) (rat_mul_def b b (b_defined) (b_defined)))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_add (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_add (rat_add (rat_add (rat_mul (rat_mul b b) (rat_mul a (rat_reciprocal (rat_add a a)))) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)) (add_eq_rat (rat_add (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (rat_add (rat_add (rat_mul (rat_mul b b) (rat_mul a (rat_reciprocal (rat_add a a)))) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two)))) (negate_rat (rat_mul b b)) (add_eq_rat (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_add (rat_mul (rat_mul b b) (rat_mul a (rat_reciprocal (rat_add a a)))) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two))) (add_eq_rat (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (rat_mul (rat_mul b b) (rat_mul a (rat_reciprocal (rat_add a a)))) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (mul_eq_rat_alt (rat_reciprocal rat_two) (rat_mul a (rat_reciprocal (rat_add a a))) (rat_mul b b) (half_reciprocal_mul a)))))), /- Now we have two terms of the form ((b^2) * 1/2) -/ /- We can add them together to make a single b^2 term-/ rw rat_add_comm (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))))) (rat_mul b (rat_mul b (rat_reciprocal rat_two))), repeat {rw rat_add_assoc}, repeat {rw <- rat_mul_assoc b b (rat_reciprocal rat_two)}, repeat {rw <- rat_add_assoc (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (rat_mul (rat_mul b b) (rat_reciprocal rat_two))) _ _}, apply zeroes_only_eq_rat (rat_add (rat_mul b b) (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)))) (rat_add (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (rat_mul (rat_mul b b) (rat_reciprocal rat_two))) (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)))) (rat_add_def (rat_mul b b) (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a))) ((rat_mul_def b b (b_defined) (b_defined))) ((rat_add_def (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a)) ((rat_add_def (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (negate_rat (rat_mul b b)) ((negate_def (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))) (rat_mul_def a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))) (a_defined) ((rat_mul_def rat_four (rat_mul c (rat_reciprocal rat_two)) (rat_four_defined) ((rat_mul_def c (rat_reciprocal rat_two) (c_defined) (half_defined)))))))) ((negate_def (rat_mul b b) (rat_mul_def b b (b_defined) (b_defined)))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined)))))))) (add_eq_rat (rat_mul b b) (rat_add (rat_mul (rat_mul b b) (rat_reciprocal rat_two)) (rat_mul (rat_mul b b) (rat_reciprocal rat_two))) (rat_add (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (negate_rat (rat_mul b b))) (rat_mul c (rat_add a a))) (add_two_halves (rat_mul b b))), /- And now we have both b^2 and -(b^2), so we can cancel them.-/ rw rat_add_comm (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (negate_rat (rat_mul b b)), repeat {rw rat_add_assoc (rat_mul b b) _ _}, rw <- rat_add_assoc (rat_add (rat_mul b b) (negate_rat (rat_mul b b))) (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul c (rat_add a a)), apply zeroes_only_eq_rat (rat_add rat_zero (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul c (rat_add a a)))) (rat_add (rat_add (rat_mul b b) (negate_rat (rat_mul b b))) (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul c (rat_add a a)))) (rat_add_def rat_zero (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul c (rat_add a a))) (rat_zero_defined) ((rat_add_def (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul c (rat_add a a)) ((negate_def (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two)))) (rat_mul_def a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))) (a_defined) ((rat_mul_def rat_four (rat_mul c (rat_reciprocal rat_two)) (rat_four_defined) ((rat_mul_def c (rat_reciprocal rat_two) (c_defined) (half_defined)))))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined)))))))) (add_eq_rat rat_zero (rat_add (rat_mul b b) (negate_rat (rat_mul b b))) (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul c (rat_add a a))) (rat_eq_comm (rat_add (rat_mul b b) (negate_rat (rat_mul b b))) rat_zero (add_negate_rat (rat_mul b b)))), rw rat_add_comm rat_zero (rat_add (negate_rat (rat_mul a (rat_mul rat_four (rat_mul c (rat_reciprocal rat_two))))) (rat_mul c (rat_add a a))), rw rat_add_zero_alt, /- We have eliminated all the terms with b in them, all that's left is -4ac/2 + c(a + a) = 0. Not much left to do, first cancel the 4/2-/ rw <- rat_mul_assoc rat_four c (rat_reciprocal rat_two), rw rat_mul_comm rat_four c, rw rat_mul_comm a (rat_mul (rat_mul c rat_four) (rat_reciprocal rat_two)), rw rat_mul_assoc c rat_four (rat_reciprocal rat_two), rw rat_mul_comm c (rat_mul rat_four (rat_reciprocal rat_two)), rw rat_mul_assoc (rat_mul rat_four (rat_reciprocal rat_two)) c a, apply zeroes_only_eq_rat (rat_add (negate_rat (rat_mul rat_two (rat_mul c a))) (rat_mul c (rat_add a a))) (rat_add (negate_rat (rat_mul (rat_mul rat_four (rat_reciprocal rat_two)) (rat_mul c a))) (rat_mul c (rat_add a a))) (rat_add_def (negate_rat (rat_mul rat_two (rat_mul c a))) (rat_mul c (rat_add a a)) ((negate_def (rat_mul rat_two (rat_mul c a)) (rat_mul_def rat_two (rat_mul c a) (rat_two_defined) ((rat_mul_def c a (c_defined) (a_defined)))))) ((rat_mul_def c (rat_add a a) (c_defined) ((rat_add_def a a (a_defined) (a_defined)))))) (add_eq_rat (negate_rat (rat_mul rat_two (rat_mul c a))) (negate_rat (rat_mul (rat_mul rat_four (rat_reciprocal rat_two)) (rat_mul c a))) (rat_mul c (rat_add a a)) (negate_eq_rat (rat_mul rat_two (rat_mul c a)) (rat_mul (rat_mul rat_four (rat_reciprocal rat_two)) (rat_mul c a)) (mul_eq_rat rat_two (rat_mul rat_four (rat_reciprocal rat_two)) (rat_mul c a) (rat_eq_comm (rat_mul rat_four (rat_reciprocal rat_two)) rat_two four_divided_by_two)))), /- And finally, just prove that 2ca = c(a + a)-/ rw rat_add_comm (negate_rat (rat_mul rat_two (rat_mul c a))) (rat_mul c (rat_add a a)), rw rat_mul_comm c (rat_add a a), apply zeroes_only_eq_rat (rat_add (rat_mul (rat_mul a rat_two) c) (negate_rat (rat_mul rat_two (rat_mul c a)))) (rat_add (rat_mul (rat_add a a) c) (negate_rat (rat_mul rat_two (rat_mul c a)))) (rat_add_def (rat_mul (rat_mul a rat_two) c) (negate_rat (rat_mul rat_two (rat_mul c a))) ((rat_mul_def (rat_mul a rat_two) c ((rat_mul_def a rat_two (a_defined) (rat_two_defined))) (c_defined))) ((negate_def (rat_mul rat_two (rat_mul c a)) (rat_mul_def rat_two (rat_mul c a) (rat_two_defined) ((rat_mul_def c a (c_defined) (a_defined))))))) (add_eq_rat (rat_mul (rat_mul a rat_two) c) (rat_mul (rat_add a a) c) (negate_rat (rat_mul rat_two (rat_mul c a))) (mul_eq_rat (rat_mul a rat_two) (rat_add a a) c (mul_two_rat a))), rw rat_mul_comm c a, rw rat_mul_comm a rat_two, rw rat_mul_assoc, exact add_negate_rat_alt (rat_mul rat_two (rat_mul a c)), /- And we are DONE for the first goal (the part that isn't multiplied by the root of the discriminant)! -/ /- For the second goal, start by again doing all the trivial cases with undefined values, yay Python.-/ by_cases a_def_tmp : (¬defined a), rw rat_mul_comm (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a, exact undef_iszero (rat_add (rat_mul a (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))))) (rat_mul (rat_reciprocal (rat_add a a)) b)) (rat_add_undef (rat_mul a (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))))) (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_mul_undef a (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a_def_tmp)), have a_defined : (defined a), cc, clear a_def_tmp, by_cases b_def_tmp : (¬defined b), exact undef_iszero (rat_add (rat_mul (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a) (rat_mul (rat_reciprocal (rat_add a a)) b)) (rat_add_undef (rat_mul (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a) (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_mul_undef (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a (rat_add_undef (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_undef (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))) (rat_mul_undef b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) b_def_tmp))))), have b_defined : (defined b), cc, clear b_def_tmp, by_cases recip_def_tmp : (¬defined (rat_reciprocal (rat_add a a))), rw rat_mul_comm b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))), exact undef_iszero (rat_add (rat_mul (rat_add (negate_rat (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) b)) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a) (rat_mul (rat_reciprocal (rat_add a a)) b)) (rat_add_undef (rat_mul (rat_add (negate_rat (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) b)) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a) (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_mul_undef (rat_add (negate_rat (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) b)) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a (rat_add_undef (negate_rat (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) b)) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_undef (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) b) (rat_mul_undef (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) b (rat_mul_undef (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)) recip_def_tmp)))))), have int1 : defined (rat_reciprocal (rat_add a a)), cc, clear recip_def_tmp, have h : (¬iszero_rat a), intro h, exact reciprocal_zero_undef (rat_add a a) (zeroes_only_eq_rat a (rat_add a a) a_defined (rat_eq_comm (rat_add a a) a (rat_add_zero a a h)) h) int1, /- Now do the distributive rewrite.-/ apply zeroes_only_eq_rat (rat_add (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_mul (rat_reciprocal (rat_add a a)) b)) (rat_add (rat_mul (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a) (rat_mul (rat_reciprocal (rat_add a a)) b)) (rat_add_def (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_mul (rat_reciprocal (rat_add a a)) b) ((rat_add_def (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a) ((rat_mul_def (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a ((negate_def (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))) (rat_mul_def b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) (b_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)) (int1) (int1)))))) (a_defined))) ((rat_mul_def (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a ((negate_def (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))) (int1) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1)))))) (a_defined))))) ((rat_mul_def (rat_reciprocal (rat_add a a)) b (int1) (b_defined))))(add_eq_rat (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_mul (rat_add (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))))) a) (rat_mul (rat_reciprocal (rat_add a a)) b) ( (rat_eq_comm _ _ (rat_mul_distrib (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)))), /- Again. multiply through by 2a.-/ apply iszero_mul_rat (rat_add (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_mul (rat_reciprocal (rat_add a a)) b)) (rat_add a a) (add_self_nonzero_rat a h) (rat_add_def a a a_defined a_defined), apply zeroes_only_eq_rat (rat_add (rat_mul (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a))) (rat_mul (rat_add (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_mul (rat_reciprocal (rat_add a a)) b)) (rat_add a a)) (rat_add_def (rat_mul (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)) ((rat_mul_def (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_add a a) ((rat_add_def (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a) ((rat_mul_def (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a ((negate_def (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))) (rat_mul_def b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) (b_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)) (int1) (int1)))))) (a_defined))) ((rat_mul_def (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a ((negate_def (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))) (int1) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1)))))) (a_defined))))) ((rat_add_def a a (a_defined) (a_defined))))) ((rat_mul_def (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a) ((rat_mul_def (rat_reciprocal (rat_add a a)) b (int1) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))( (rat_eq_comm _ _ (rat_mul_distrib (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)))), apply zeroes_only_eq_rat (rat_add (rat_add (rat_mul (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_add a a)) (rat_mul (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a) (rat_add a a))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a))) (rat_add (rat_mul (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a))) (rat_add_def (rat_add (rat_mul (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_add a a)) (rat_mul (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a) (rat_add a a))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)) ((rat_add_def (rat_mul (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_add a a)) (rat_mul (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a) (rat_add a a)) ((rat_mul_def (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_add a a) ((rat_mul_def (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a ((negate_def (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))) (rat_mul_def b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))) (b_defined) ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)) (int1) (int1)))))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))) ((rat_mul_def (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a) (rat_add a a) ((rat_mul_def (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a ((negate_def (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))) (int1) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1)))))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined))))))) ((rat_mul_def (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a) ((rat_mul_def (rat_reciprocal (rat_add a a)) b (int1) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (rat_mul (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_add a a)) (rat_mul (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a) (rat_add a a))) (rat_mul (rat_add (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a)) (rat_add a a)) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)) ( (rat_eq_comm _ _ (rat_mul_distrib (rat_mul (negate_rat (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))))) a) (rat_mul (negate_rat (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))))) a) (rat_add a a))))), repeat {rw mul_negate_alt_rw}, repeat {rw mul_negate_rw}, repeat {rw mul_negate_alt_rw}, repeat {rw mul_negate_rw}, /- Cancel where possible (which is everywhere for this bit).-/ rw rat_mul_comm (rat_mul (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))) a) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))) a, rw rat_mul_comm (rat_add a a) (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))), rw rat_mul_comm (rat_mul (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))) (rat_add a a)) a, rw rat_mul_comm (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)))) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) b (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_add a a) b, rw rat_mul_assoc b (rat_add a a) (rat_mul (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a))), rw <- rat_mul_assoc (rat_add a a) (rat_reciprocal (rat_add a a)) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_add a a) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_mul (rat_reciprocal (rat_add a a)) (rat_add a a)) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_add a a), apply zeroes_only_eq_rat (rat_add (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a)))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a))) (rat_add (rat_add (negate_rat (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))))) (negate_rat (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a)))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a))) (rat_add_def (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a)))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)) ((rat_add_def (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) ((negate_def (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul_def a (rat_mul b (rat_reciprocal (rat_add a a))) (a_defined) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1)))))) ((negate_def (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a)) (rat_mul_def (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a) ((rat_mul_def (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a ((rat_mul_def (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))) (int1) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1))))) (a_defined))) ((rat_add_def a a (a_defined) (a_defined)))))))) ((rat_mul_def (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a) ((rat_mul_def (rat_reciprocal (rat_add a a)) b (int1) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a)))) (rat_add (negate_rat (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))))) (negate_rat (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a)))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)) (add_eq_rat (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))))) (negate_rat (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a))) (negate_eq_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul a (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (mul_eq_rat_alt (rat_mul b (rat_reciprocal (rat_add a a))) (rat_mul b (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))) a (mul_eq_rat_alt (rat_reciprocal (rat_add a a)) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))) b (reciprocal_mul_cancel (rat_reciprocal (rat_add a a)) (rat_add a a))))))), rw rat_add_comm (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a))), rw rat_mul_comm (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) a, rw rat_mul_comm (rat_add a a) (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))), rw rat_mul_comm (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_add a a)) a, rw rat_mul_comm (rat_mul (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_reciprocal (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_add a a) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_mul (rat_reciprocal (rat_add a a)) (rat_add a a)) (rat_mul b (rat_reciprocal (rat_add a a))), rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_add a a), apply zeroes_only_eq_rat (rat_add (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a))) (rat_add (rat_add (negate_rat (rat_mul a (rat_mul (rat_mul b (rat_reciprocal (rat_add a a))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a))) (rat_add_def (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)) ((rat_add_def (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) ((negate_def (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul_def a (rat_mul b (rat_reciprocal (rat_add a a))) (a_defined) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1)))))) ((negate_def (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul_def a (rat_mul b (rat_reciprocal (rat_add a a))) (a_defined) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1)))))))) ((rat_mul_def (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a) ((rat_mul_def (rat_reciprocal (rat_add a a)) b (int1) (b_defined))) ((rat_add_def a a (a_defined) (a_defined))))))(add_eq_rat (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))))) (rat_add (negate_rat (rat_mul a (rat_mul (rat_mul b (rat_reciprocal (rat_add a a))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)) (add_eq_rat (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul (rat_mul b (rat_reciprocal (rat_add a a))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_eq_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul a (rat_mul (rat_mul b (rat_reciprocal (rat_add a a))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a))))) (mul_eq_rat_alt (rat_mul b (rat_reciprocal (rat_add a a))) (rat_mul (rat_mul b (rat_reciprocal (rat_add a a))) (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))) a (reciprocal_mul_cancel (rat_mul b (rat_reciprocal (rat_add a a))) (rat_add a a)))))), rw rat_add_comm (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))))) (rat_mul (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a)), rw rat_mul_comm (rat_mul (rat_reciprocal (rat_add a a)) b) (rat_add a a), rw <- rat_mul_assoc (rat_add a a) (rat_reciprocal (rat_add a a)) b, rw rat_mul_comm (rat_add a a) (rat_reciprocal (rat_add a a)), rw rat_mul_comm (rat_mul (rat_reciprocal (rat_add a a)) (rat_add a a)) b, rw rat_mul_comm (rat_reciprocal (rat_add a a)) (rat_add a a), apply zeroes_only_eq_rat (rat_add b (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))))) (rat_add (rat_mul b (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))) (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))))) (rat_add_def b (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))))) b_defined ((rat_add_def (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) ((negate_def (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul_def a (rat_mul b (rat_reciprocal (rat_add a a))) (a_defined) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1)))))) ((negate_def (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))) (rat_mul_def a (rat_mul b (rat_reciprocal (rat_add a a))) (a_defined) ((rat_mul_def b (rat_reciprocal (rat_add a a)) (b_defined) (int1))))))))) (add_eq_rat b (rat_mul b (rat_mul (rat_add a a) (rat_reciprocal (rat_add a a)))) (rat_add (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a))))) (negate_rat (rat_mul a (rat_mul b (rat_reciprocal (rat_add a a)))))) (reciprocal_mul_cancel b (rat_add a a))), /- Cancel in the two nodes that have a * 1/(a + a)-/ rw rat_add_comm b _, rw rat_mul_comm b (rat_reciprocal (rat_add a a)), rw <- rat_mul_assoc a (rat_reciprocal (rat_add a a)) b, rw <- rat_add_assoc, rw rat_mul_comm (rat_mul a (rat_reciprocal (rat_add a a))) b, apply zeroes_only_eq_rat (rat_add (negate_rat (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) b)) (rat_add (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) (rat_add (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) b)) (rat_add_def (negate_rat (rat_mul b (rat_reciprocal rat_two))) (rat_add (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) b) ((negate_def (rat_mul b (rat_reciprocal rat_two)) (rat_mul_def b (rat_reciprocal rat_two) (b_defined) (half_defined)))) ((rat_add_def (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) b ((negate_def (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a)))) (rat_mul_def b (rat_mul a (rat_reciprocal (rat_add a a))) (b_defined) ((rat_mul_def a (rat_reciprocal (rat_add a a)) (a_defined) (int1)))))) (b_defined))))(add_eq_rat (negate_rat (rat_mul b (rat_reciprocal rat_two))) (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) (rat_add (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) b) (negate_eq_rat (rat_mul b (rat_reciprocal rat_two)) (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a)))) (mul_eq_rat_alt (rat_reciprocal rat_two) (rat_mul a (rat_reciprocal (rat_add a a))) b (half_reciprocal_mul a)))), rw rat_add_comm, apply zeroes_only_eq_rat (rat_add (rat_add (negate_rat (rat_mul b (rat_reciprocal rat_two))) b) (negate_rat (rat_mul b (rat_reciprocal rat_two)))) (rat_add (rat_add (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) b) (negate_rat (rat_mul b (rat_reciprocal rat_two)))) (rat_add_def (rat_add (negate_rat (rat_mul b (rat_reciprocal rat_two))) b) (negate_rat (rat_mul b (rat_reciprocal rat_two))) ((rat_add_def (negate_rat (rat_mul b (rat_reciprocal rat_two))) b ((negate_def (rat_mul b (rat_reciprocal rat_two)) (rat_mul_def b (rat_reciprocal rat_two) (b_defined) (half_defined)))) (b_defined))) ((negate_def (rat_mul b (rat_reciprocal rat_two)) (rat_mul_def b (rat_reciprocal rat_two) (b_defined) (half_defined)))))(add_eq_rat (rat_add (negate_rat (rat_mul b (rat_reciprocal rat_two))) b) (rat_add (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) b) (negate_rat (rat_mul b (rat_reciprocal rat_two))) (add_eq_rat (negate_rat (rat_mul b (rat_reciprocal rat_two))) (negate_rat (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a))))) b (negate_eq_rat (rat_mul b (rat_reciprocal rat_two)) (rat_mul b (rat_mul a (rat_reciprocal (rat_add a a)))) (mul_eq_rat_alt (rat_reciprocal rat_two) (rat_mul a (rat_reciprocal (rat_add a a))) b (half_reciprocal_mul a))))), /- Add the two -b/2 terms together...-/ rw rat_mul_comm b (rat_reciprocal rat_two), rw <- mul_negate_rw (rat_reciprocal rat_two) b, rw rat_mul_comm (rat_reciprocal rat_two) (negate_rat b), rw rat_add_comm (rat_mul (negate_rat b) (rat_reciprocal rat_two)) b, rw <- rat_add_assoc, rw rat_add_comm, apply zeroes_only_eq_rat (rat_add (negate_rat b) b) (rat_add (rat_add (rat_mul (negate_rat b) (rat_reciprocal rat_two)) (rat_mul (negate_rat b) (rat_reciprocal rat_two))) b) (rat_add_def (negate_rat b) b ((negate_def b b_defined)) (b_defined)) (add_eq_rat (negate_rat b) (rat_add (rat_mul (negate_rat b) (rat_reciprocal rat_two)) (rat_mul (negate_rat b) (rat_reciprocal rat_two))) b (add_two_halves (negate_rat b))), /- And all we have left is (-b) + b, which is obviously zero.-/ rw rat_add_comm, exact add_negate_rat_alt b, /- QUOD.-/ /- ERAT.-/ /- DEMONSTRANDUM.-/ end
0f5bdb4182fec828be1ef113a9c89a6ee53353b4
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/measure_theory/borel_space.lean
1a5b75b799cb3afa055817289b649005c6be16b0
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
24,998
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Borel (measurable) space -- the smallest σ-algebra generated by open sets It would be nice to encode this in the topological space type class, i.e. each topological space carries a measurable space, the Borel space. This would be similar how each uniform space carries a topological space. The idea is to allow definitional equality for product instances. We would like to have definitional equality for borel t₁ × borel t₂ = borel (t₁ × t₂) Unfortunately, this only holds if t₁ and t₂ are second-countable topologies. -/ import measure_theory.measurable_space topology.instances.ennreal analysis.normed_space.basic noncomputable theory open classical set lattice real local attribute [instance] prop_decidable universes u v w x y variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y} {s t u : set α} namespace measure_theory open measurable_space topological_space @[instance, priority 900] def borel (α : Type u) [topological_space α] : measurable_space α := generate_from {s : set α | is_open s} lemma borel_eq_generate_from_of_subbasis {s : set (set α)} [t : topological_space α] [second_countable_topology α] (hs : t = generate_from s) : borel α = generate_from s := le_antisymm (generate_from_le $ assume u (hu : t.is_open u), begin rw [hs] at hu, induction hu, case generate_open.basic : u hu { exact generate_measurable.basic u hu }, case generate_open.univ { exact @is_measurable.univ α (generate_from s) }, case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂ { exact @is_measurable.inter α (generate_from s) _ _ hs₁ hs₂ }, case generate_open.sUnion : f hf ih { rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩, rw ← vu, exact @is_measurable.sUnion α (generate_from s) _ hv (λ x xv, ih _ (vf xv)) } end) (generate_from_le $ assume u hu, generate_measurable.basic _ $ show t.is_open u, by rw [hs]; exact generate_open.basic _ hu) lemma borel_eq_generate_Iio (α) [topological_space α] [second_countable_topology α] [linear_order α] [orderable_topology α] : borel α = generate_from (range Iio) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (orderable_topology.topology_eq_generate_intervals α), have H : ∀ a:α, is_measurable (measurable_space.generate_from (range Iio)) (Iio a) := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H], by_cases h : ∃ a', ∀ b, a < b ↔ a' ≤ b, { rcases h with ⟨a', ha'⟩, rw (_ : {b | a < b} = -Iio a'), {exact (H _).compl _}, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a < a'}, {b | a'.1 < b}) (λ a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : {b | a < b} = ⋃ x : v, -Iio x.1.1, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), lt_of_lt_of_le ax⟩⟩ }, rw this, resetI, apply is_measurable.Union, exact λ _, (H _).compl _ } }, { simp, rintro _ a rfl, exact generate_measurable.basic _ is_open_Iio } end lemma borel_eq_generate_Ioi (α) [topological_space α] [second_countable_topology α] [linear_order α] [orderable_topology α] : borel α = generate_from (range (λ a, {x | a < x})) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (orderable_topology.topology_eq_generate_intervals α), have H : ∀ a:α, is_measurable (measurable_space.generate_from (range (λ a, {x | a < x}))) {x | a < x} := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩, {apply H}, by_cases h : ∃ a', ∀ b, b < a ↔ b ≤ a', { rcases h with ⟨a', ha'⟩, rw (_ : {b | b < a} = -{x | a' < x}), {exact (H _).compl _}, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a' < a}, {b | b < a'.1}) (λ a', is_open_gt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : {b | b < a} = ⋃ x : v, -{b | x.1.1 < b}, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_le_of_lt ax h⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), λ h, lt_of_le_of_lt h ax⟩⟩ }, rw this, resetI, apply is_measurable.Union, exact λ _, (H _).compl _ } }, { simp, rintro _ a rfl, exact generate_measurable.basic _ (is_open_lt' _) } end lemma borel_comap {f : α → β} {t : topological_space β} : @borel α (t.induced f) = (@borel β t).comap f := calc @borel α (t.induced f) = measurable_space.generate_from (preimage f '' {s | is_open s }) : congr_arg measurable_space.generate_from $ set.ext $ assume s : set α, show (t.induced f).is_open s ↔ s ∈ preimage f '' {s | is_open s}, by simp [topological_space.induced, set.image, eq_comm]; refl ... = (@borel β t).comap f : comap_generate_from.symm section variables [topological_space α] lemma is_measurable_of_is_open : is_open s → is_measurable s := generate_measurable.basic s lemma is_measurable_interior : is_measurable (interior s) := is_measurable_of_is_open is_open_interior lemma is_measurable_ball [metric_space β] {x : β} {ε : ℝ} : is_measurable (metric.ball x ε) := measure_theory.is_measurable_of_is_open metric.is_open_ball lemma is_measurable_of_is_closed (h : is_closed s) : is_measurable s := is_measurable.compl_iff.1 $ is_measurable_of_is_open h lemma is_measurable_singleton [t1_space α] {x : α} : is_measurable ({x} : set α) := measure_theory.is_measurable_of_is_closed is_closed_singleton lemma is_measurable_closure : is_measurable (closure s) := is_measurable_of_is_closed is_closed_closure lemma measurable_of_continuous [topological_space β] {f : α → β} (h : continuous f) : measurable f := measurable_generate_from $ assume t ht, is_measurable_of_is_open $ h t ht lemma borel_prod_le [topological_space β] : prod.measurable_space ≤ borel (α × β) := sup_le (comap_le_iff_le_map.mpr $ measurable_of_continuous continuous_fst) (comap_le_iff_le_map.mpr $ measurable_of_continuous continuous_snd) lemma borel_induced [t : topological_space β] (f : α → β) : @borel α (t.induced f) = (borel β).comap f := comap_generate_from.symm lemma borel_eq_subtype [topological_space α] (s : set α) : borel s = subtype.measurable_space := borel_induced coe lemma borel_prod [second_countable_topology α] [topological_space β] [second_countable_topology β] : prod.measurable_space = borel (α × β) := let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := @is_open_generated_countable_inter α _ _ in let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := @is_open_generated_countable_inter β _ _ in le_antisymm borel_prod_le begin have : prod.topological_space = generate_from {g | ∃u∈a, ∃v∈b, g = set.prod u v}, { rw [ha₅, hb₅], exact prod_generate_from_generate_from_eq ha₄ hb₄ }, rw [borel_eq_generate_from_of_subbasis this], exact generate_from_le (assume p ⟨u, hu, v, hv, eq⟩, have hu : is_open u, by rw [ha₅]; exact generate_open.basic _ hu, have hv : is_open v, by rw [hb₅]; exact generate_open.basic _ hv, eq.symm ▸ is_measurable_set_prod (is_measurable_of_is_open hu) (is_measurable_of_is_open hv)) end lemma measurable_of_continuous2 [topological_space α] [second_countable_topology α] [topological_space β] [second_countable_topology β] [topological_space γ] [measurable_space δ] {f : δ → α} {g : δ → β} {c : α → β → γ} (h : continuous (λp:α×β, c p.1 p.2)) (hf : measurable f) (hg : measurable g) : measurable (λa, c (f a) (g a)) := show measurable ((λp:α×β, c p.1 p.2) ∘ (λa, (f a, g a))), begin apply measurable.comp, { rw borel_prod, exact measurable_of_continuous h }, { exact measurable_prod_mk hf hg } end lemma measurable_add [add_monoid α] [topological_add_monoid α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a + g a) := measurable_of_continuous2 continuous_add' lemma measurable_finset_sum {ι : Type*} [add_comm_monoid α] [topological_add_monoid α] [second_countable_topology α] [measurable_space β] {f : ι → β → α} (s : finset ι) (hf : ∀i, measurable (f i)) : measurable (λa, s.sum (λi, f i a)) := finset.induction_on s (by simpa using measurable_const) (assume i s his ih, by simpa [his] using measurable_add (hf i) ih) lemma measurable_neg [add_group α] [topological_add_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, - f a) := (measurable_of_continuous continuous_neg').comp hf lemma measurable_sub [add_group α] [topological_add_group α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a - g a) := measurable_of_continuous2 continuous_sub' lemma measurable_mul [monoid α] [topological_monoid α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a * g a) := measurable_of_continuous2 continuous_mul' lemma measurable_le {α β} [topological_space α] [partial_order α] [ordered_topology α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} (hf : measurable f) (hg : measurable g) : is_measurable {a | f a ≤ g a} := have is_measurable {p : α × α | p.1 ≤ p.2}, by rw borel_prod; exact is_measurable_of_is_closed (ordered_topology.is_closed_le' _), show is_measurable {a | (f a, g a).1 ≤ (f a, g a).2}, begin refine measurable.preimage _ this, exact measurable_prod_mk hf hg end -- generalize lemma measurable_coe_int_real : measurable (λa, a : ℤ → ℝ) := assume s (hs : is_measurable s), by trivial section ordered_topology variables [linear_order α] [topological_space α] [ordered_topology α] {a b c : α} lemma is_measurable_Ioo : is_measurable (Ioo a b) := is_measurable_of_is_open is_open_Ioo lemma is_measurable_Iio : is_measurable (Iio a) := is_measurable_of_is_open is_open_Iio lemma is_measurable_Ico : is_measurable (Ico a b) := (is_measurable_of_is_closed $ is_closed_le continuous_const continuous_id).inter is_measurable_Iio end ordered_topology lemma measurable.is_lub {α} [topological_space α] [linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} {g : β → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_lub {a | ∃ i, f i b = a} (g b)) : measurable g := begin rw borel_eq_generate_Ioi α, apply measurable_generate_from, rintro _ ⟨a, rfl⟩, have : {b | a < g b} = ⋃ i, {b | a < f i b}, { simp [set.ext_iff], intro b, rw [lt_is_lub_iff (hg b)], exact ⟨λ ⟨_, ⟨i, rfl⟩, h⟩, ⟨i, h⟩, λ ⟨i, h⟩, ⟨_, ⟨i, rfl⟩, h⟩⟩ }, show is_measurable {b | a < g b}, rw this, exact is_measurable.Union (λ i, hf i _ (is_measurable_of_is_open (is_open_lt' _))) end lemma measurable.is_glb {α} [topological_space α] [linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} {g : β → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_glb {a | ∃ i, f i b = a} (g b)) : measurable g := begin rw borel_eq_generate_Iio α, apply measurable_generate_from, rintro _ ⟨a, rfl⟩, have : {b | g b < a} = ⋃ i, {b | f i b < a}, { simp [set.ext_iff], intro b, rw [is_glb_lt_iff (hg b)], exact ⟨λ ⟨_, ⟨i, rfl⟩, h⟩, ⟨i, h⟩, λ ⟨i, h⟩, ⟨_, ⟨i, rfl⟩, h⟩⟩ }, show is_measurable {b | g b < a}, rw this, exact is_measurable.Union (λ i, hf i _ (is_measurable_of_is_open (is_open_gt' _))) end lemma measurable.supr {α} [topological_space α] [complete_linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i, f i b) := measurable.is_lub hf $ λ b, is_lub_supr lemma measurable.infi {α} [topological_space α] [complete_linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i, f i b) := measurable.is_glb hf $ λ b, is_glb_infi lemma measurable.supr_Prop {α} [topological_space α] [complete_linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {p : Prop} {f : β → α} (hf : measurable f) : measurable (λ b, ⨆ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact supr_pos h end) (assume h : ¬p, begin convert measurable_const, funext, exact supr_neg h end) lemma measurable.infi_Prop {α} [topological_space α] [complete_linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {p : Prop} {f : β → α} (hf : measurable f) : measurable (λ b, ⨅ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact infi_pos h end ) (assume h : ¬p, begin convert measurable_const, funext, exact infi_neg h end) end end measure_theory def homemorph.to_measurable_equiv [topological_space α] [topological_space β] (h : α ≃ₜ β) : measurable_equiv α β := { to_equiv := h.to_equiv, measurable_to_fun := measure_theory.measurable_of_continuous h.continuous_to_fun, measurable_inv_fun := measure_theory.measurable_of_continuous h.continuous_inv_fun } namespace real open measure_theory measurable_space lemma borel_eq_generate_from_Ioo_rat : borel ℝ = generate_from (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := borel_eq_generate_from_of_subbasis is_topological_basis_Ioo_rat.2.2 lemma borel_eq_generate_from_Iio_rat : borel ℝ = generate_from (⋃a:ℚ, {Iio a}) := begin let g, swap, apply le_antisymm (_ : _ ≤ g) (measurable_space.generate_from_le (λ t, _)), { rw borel_eq_generate_from_Ioo_rat, refine generate_from_le (λ t, _), simp only [mem_Union], rintro ⟨a, b, h, rfl|⟨⟨⟩⟩⟩, rw (set.ext (λ x, _) : Ioo (a:ℝ) b = (⋃c>a, - Iio c) ∩ Iio b), { have hg : ∀q:ℚ, g.is_measurable (Iio q) := λ q, generate_measurable.basic _ (by simp; exact ⟨_, rfl⟩), refine @is_measurable.inter _ g _ _ _ (hg _), refine @is_measurable.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _), exact @is_measurable.compl _ _ g (hg _) }, { simp [Ioo, Iio], refine and_congr _ iff.rfl, exact ⟨λ h, let ⟨c, ac, cx⟩ := exists_rat_btwn h in ⟨c, rat.cast_lt.1 ac, le_of_lt cx⟩, λ ⟨c, ac, cx⟩, lt_of_lt_of_le (rat.cast_lt.2 ac) cx⟩ } }, { simp, rintro r rfl, exact is_measurable_of_is_open (is_open_gt' _) } end end real namespace nnreal open filter measure_theory lemma measurable_add [measurable_space α] {f : α → nnreal} {g : α → nnreal} : measurable f → measurable g → measurable (λa, f a + g a) := measurable_of_continuous2 continuous_add' lemma measurable_sub [measurable_space α] {f g: α → nnreal} (hf : measurable f) (hg : measurable g) : measurable (λ a, f a - g a) := measurable_of_continuous2 continuous_sub' hf hg lemma measurable_mul [measurable_space α] {f : α → nnreal} {g : α → nnreal} : measurable f → measurable g → measurable (λa, f a * g a) := measurable_of_continuous2 continuous_mul' lemma measurable_of_real : measurable nnreal.of_real := measurable_of_continuous nnreal.continuous_of_real end nnreal namespace ennreal open filter measure_theory lemma measurable_coe : measurable (coe : nnreal → ennreal) := measurable_of_continuous (continuous_coe.2 continuous_id) def ennreal_equiv_nnreal : measurable_equiv {r : ennreal | r < ⊤} nnreal := { to_fun := λr, ennreal.to_nnreal r.1, inv_fun := λr, ⟨r, coe_lt_top⟩, left_inv := assume ⟨r, hr⟩, by simp [coe_to_nnreal (ne_of_lt hr)], right_inv := assume r, to_nnreal_coe, measurable_to_fun := begin rw [← borel_eq_subtype], refine measurable_of_continuous (continuous_iff_continuous_at.2 _), rintros ⟨r, hr⟩, simp [continuous_at, nhds_subtype_eq_comap], refine tendsto.comp (tendsto_to_nnreal (ne_of_lt hr)) tendsto_comap end, measurable_inv_fun := measurable_subtype_mk measurable_coe } lemma measurable_of_measurable_nnreal [measurable_space α] {f : ennreal → α} (h : measurable (λp:nnreal, f p)) : measurable f := begin refine measurable_of_measurable_union_cover {⊤} {r : ennreal | r < ⊤} (is_measurable_of_is_closed $ is_closed_singleton) (is_measurable_of_is_open $ is_open_gt' _) (assume r _, by cases r; simp [ennreal.none_eq_top, ennreal.some_eq_coe]) _ _, exact (measurable_equiv.set.singleton ⊤).symm.measurable_coe_iff.1 (measurable_unit _), exact (ennreal_equiv_nnreal.symm.measurable_coe_iff.1 h) end def ennreal_equiv_sum : @measurable_equiv ennreal (nnreal ⊕ unit) _ sum.measurable_space := { to_fun := @option.rec nnreal (λ_, nnreal ⊕ unit) (sum.inr ()) (sum.inl : nnreal → nnreal ⊕ unit), inv_fun := @sum.rec nnreal unit (λ_, ennreal) (coe : nnreal → ennreal) (λ_, ⊤), left_inv := assume s, by cases s; refl, right_inv := assume s, by rcases s with r | ⟨⟨⟩⟩; refl, measurable_to_fun := measurable_of_measurable_nnreal measurable_inl, measurable_inv_fun := measurable_sum measurable_coe (@measurable_const ennreal unit _ _ ⊤) } lemma measurable_of_measurable_nnreal_nnreal [measurable_space α] [measurable_space β] (f : ennreal → ennreal → β) {g : α → ennreal} {h : α → ennreal} (h₁ : measurable (λp:nnreal × nnreal, f p.1 p.2)) (h₂ : measurable (λr:nnreal, f ⊤ r)) (h₃ : measurable (λr:nnreal, f r ⊤)) (hg : measurable g) (hh : measurable h) : measurable (λa, f (g a) (h a)) := let e : measurable_equiv (ennreal × ennreal) (((nnreal × nnreal) ⊕ (nnreal × unit)) ⊕ ((unit × nnreal) ⊕ (unit × unit))) := (measurable_equiv.prod_congr ennreal_equiv_sum ennreal_equiv_sum).trans (measurable_equiv.sum_prod_sum _ _ _ _) in have measurable (λp:ennreal×ennreal, f p.1 p.2), begin refine e.symm.measurable_coe_iff.1 (measurable_sum (measurable_sum _ _) (measurable_sum _ _)), { show measurable (λp:nnreal × nnreal, f p.1 p.2), exact h₁ }, { show measurable (λp:nnreal × unit, f p.1 ⊤), exact h₃.comp (measurable_fst measurable_id) }, { show measurable ((λp:nnreal, f ⊤ p) ∘ (λp:unit × nnreal, p.2)), exact h₂.comp (measurable_snd measurable_id) }, { show measurable (λp:unit × unit, f ⊤ ⊤), exact measurable_const } end, this.comp (measurable_prod_mk hg hh) lemma measurable_mul {α : Type*} [measurable_space α] {f g : α → ennreal} : measurable f → measurable g → measurable (λa, f a * g a) := begin refine measurable_of_measurable_nnreal_nnreal (*) _ _ _, { simp only [ennreal.coe_mul.symm], exact measurable_coe.comp (measurable_mul (measurable_fst measurable_id) (measurable_snd measurable_id)) }, { simp [top_mul], exact measurable.if (is_measurable_of_is_closed $ is_closed_eq continuous_id continuous_const) measurable_const measurable_const }, { simp [mul_top], exact measurable.if (is_measurable_of_is_closed $ is_closed_eq continuous_id continuous_const) measurable_const measurable_const } end lemma measurable_add {α : Type*} [measurable_space α] {f g : α → ennreal} : measurable f → measurable g → measurable (λa, f a + g a) := begin refine measurable_of_measurable_nnreal_nnreal (+) _ _ _, { simp only [ennreal.coe_add.symm], exact measurable_coe.comp (measurable_add (measurable_fst measurable_id) (measurable_snd measurable_id)) }, { simp [measurable_const] }, { simp [measurable_const] } end lemma measurable_sub {α : Type*} [measurable_space α] {f g : α → ennreal} : measurable f → measurable g → measurable (λa, f a - g a) := begin refine measurable_of_measurable_nnreal_nnreal (has_sub.sub) _ _ _, { simp only [ennreal.coe_sub.symm], exact measurable_coe.comp (nnreal.measurable_sub (measurable_fst measurable_id) (measurable_snd measurable_id)) }, { simp [measurable_const] }, { simp [measurable_const] } end lemma measurable_of_real : measurable ennreal.of_real := measurable_of_continuous ennreal.continuous_of_real end ennreal namespace measure_theory open topological_space lemma measurable_smul' {α : Type*} {β : Type*} {γ : Type*} [semiring α] [topological_space α] [second_countable_topology α] [topological_space β] [add_comm_monoid β] [second_countable_topology β] [semimodule α β] [topological_semimodule α β] [measurable_space γ] {f : γ → α} {g : γ → β} (hf : measurable f) (hg : measurable g) : measurable (λ c, f c • g c) := measurable_of_continuous2 (continuous_smul continuous_fst continuous_snd) hf hg lemma measurable_smul {α : Type*} {β : Type*} {γ : Type*} [semiring α] [topological_space α] [second_countable_topology α] [topological_space β] [add_comm_monoid β] [second_countable_topology β] [semimodule α β] [topological_semimodule α β] [measurable_space γ] {c : α} {g : γ → β} (hg : measurable g) : measurable (λ x, c • g x) := measurable.comp (measurable_of_continuous (continuous_smul continuous_const continuous_id)) hg lemma measurable_dist' {α : Type*} [metric_space α] [second_countable_topology α] : measurable (λp:α×α, dist p.1 p.2) := begin rw [borel_prod], apply measurable_of_continuous, exact continuous_dist continuous_fst continuous_snd end lemma measurable_dist {α : Type*} [metric_space α] [second_countable_topology α] [measurable_space β] {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, dist (f b) (g b)) := measurable.comp measurable_dist' (measurable_prod_mk hf hg) lemma measurable_nndist' {α : Type*} [metric_space α] [second_countable_topology α] : measurable (λp:α×α, nndist p.1 p.2) := begin rw [borel_prod], apply measurable_of_continuous, exact continuous_nndist continuous_fst continuous_snd end lemma measurable_nndist {α : Type*} [metric_space α] [second_countable_topology α] [measurable_space β] {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, nndist (f b) (g b)) := measurable.comp measurable_nndist' (measurable_prod_mk hf hg) lemma measurable_edist' {α : Type*} [emetric_space α] [second_countable_topology α] : measurable (λp:α×α, edist p.1 p.2) := begin rw [borel_prod], apply measurable_of_continuous, exact continuous_edist continuous_fst continuous_snd end lemma measurable_edist {α : Type*} [emetric_space α] [second_countable_topology α] [measurable_space β] {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, edist (f b) (g b)) := measurable.comp measurable_edist' (measurable_prod_mk hf hg) lemma measurable_norm' {α : Type*} [normed_group α] : measurable (norm : α → ℝ) := measurable_of_continuous continuous_norm lemma measurable_norm {α : Type*} [normed_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, norm (f a)) := measurable.comp measurable_norm' hf lemma measurable_nnnorm' {α : Type*} [normed_group α] : measurable (nnnorm : α → nnreal) := measurable_of_continuous continuous_nnnorm lemma measurable_nnnorm {α : Type*} [normed_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, nnnorm (f a)) := measurable.comp measurable_nnnorm' hf lemma measurable_coe_nnnorm {α : Type*} [normed_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, (nnnorm (f a) : ennreal)) := measurable.comp ennreal.measurable_coe $ measurable_nnnorm hf end measure_theory
374ec9f7a65652fe91e00c2f1805430bff252e91
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Lean/Meta/CollectMVars.lean
28761a798cc421178e8e337fe8e99f0edcbb70f6
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,980
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.CollectMVars import Lean.Meta.Basic namespace Lean.Meta /-- Collect unassigned metavariables occuring in the given expression. Remark: if `e` contains `?m` and there is a `t` assigned to `?m`, we collect unassigned metavariables occurring in `t`. Remark: if `e` contains `?m` and `?m` is delayed assigned to some term `t`, we collect `?m` and unassigned metavariables occurring in `t`. We collect `?m` because it has not been assigned yet. -/ partial def collectMVars (e : Expr) : StateRefT CollectMVars.State MetaM Unit := do let e ← instantiateMVars e let s ← get let resultSavedSize := s.result.size let s := e.collectMVars s set s for mvarId in s.result[resultSavedSize:] do match (← getDelayedAssignment? mvarId) with | none => pure () | some d => collectMVars d.val variables {m : Type → Type} [MonadLiftT MetaM m] def getMVarsImp (e : Expr) : MetaM (Array MVarId) := do let (_, s) ← (collectMVars e).run {} pure s.result /-- Return metavariables in occuring the given expression. See `collectMVars` -/ def getMVars (e : Expr) : m (Array MVarId) := liftM $ getMVarsImp e def getMVarsNoDelayedImp (e : Expr) : MetaM (Array MVarId) := do let mvarIds ← getMVars e mvarIds.filterM fun mvarId => not <$> isDelayedAssigned mvarId /-- Similar to getMVars, but removes delayed assignments. -/ def getMVarsNoDelayed (e : Expr) : m (Array MVarId) := liftM $ getMVarsNoDelayedImp e def collectMVarsAtDecl (d : Declaration) : StateRefT CollectMVars.State MetaM Unit := d.forExprM collectMVars def getMVarsAtDeclImp (d : Declaration) : MetaM (Array MVarId) := do let (_, s) ← (collectMVarsAtDecl d).run {} pure s.result def getMVarsAtDecl (d : Declaration) : m (Array MVarId) := liftM $ getMVarsAtDeclImp d end Lean.Meta
ee334c3ca96fa82c244a0d4ba094b528cabd0d5d
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/max/level01.lean
1b6976daf2ad9bd7fee0b967f68f7b09d6d152ee
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,609
lean
import tactic -- hide import data.real.basic -- imports the real numbers /- -/ open_locale classical -- allow proofs by contradiction /- -/ noncomputable theory -- don't fuss about the reals being noncomputable namespace xena -- hide -- Let a, b, c be real numbers variables {a b c : ℝ} /- # Chapter ? : Max and abs ## Level 1 In this chapter we develop a basic interface for the `max a b` and `abs a` function on the real numbers. Before we start, you will need to know the basic API for `≤` and `<`, which looks like this: ``` example : a ≤ a := le_refl a example : a ≤ b → b ≤ c → a ≤ c := le_trans example : a ≤ b → b ≤ a → a = b := le_antisymm example : a ≤ b ∨ b ≤ a := le_total a b example : a < b ↔ a ≤ b ∧ a ≠ b := lt_iff_le_and_ne example : a ≤ b → b < c → a < c := lt_of_le_of_lt example : a < b → b ≤ c → a < c := lt_of_lt_of_le ``` -/ /- Axiom : le_refl a : a ≤ a -/ /- Axiom : le_trans : a ≤ b → b ≤ c → a ≤ c -/ /- Axiom : le_antisymm : a ≤ b → b ≤ a → a = b -/ /- Axiom : le_total a b : a ≤ b ∨ b ≤ a -/ /- Axiom : lt_iff_le_and_ne : a < b ↔ a ≤ b ∧ a ≠ b -/ /- Axiom : lt_of_le_of_lt : a ≤ b → b < c → a < c -/ /- Axiom : lt_of_lt_of_le : a < b → b ≤ c → a < c -/ /- We start with `max a b := if a ≤ b then b else a`. It is uniquely characterised by the following two properties, which are hence all you will need to know: ``` theorem max_eq_right : a ≤ b → max a b = b theorem max_eq_left : b ≤ a → max a b = a ``` -/ /- Axiom : max_eq_right : a ≤ b → max a b = b -/ /- Axiom : max_eq_left : b ≤ a → max a b = a -/ -- begin hide def max (a b : ℝ) := if a ≤ b then b else a -- need if_pos to do this one theorem max_eq_right (hab : a ≤ b) : max a b = b := begin unfold max, rw if_pos hab, end -- need if_neg to do this one theorem max_eq_left (hba : b ≤ a) : max a b = a := begin by_cases hab : a ≤ b, { rw max_eq_right hab, exact le_antisymm hba hab, }, { unfold max, rw if_neg hab, } end -- end hide /- All of these theorems are in the theorem statement box on the left. See if you can now prove the useful `max_choice` lemma using them. -/ /- Hint : Hint Do a case split with `cases le_total a b`. -/ /- Lemma For any two real numbers $a$ and $b$, either $\max(a,b) = a$ or $\max(a,b) = b$. -/ theorem max_choice (a b : ℝ) : max a b = a ∨ max a b = b := begin cases le_total a b with hab hba, { right, exact max_eq_right hab }, { left, exact max_eq_left hba } end end xena --hide